pmemdump.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/String.h>
  7. #include <AK/StringUtils.h>
  8. #include <LibCore/ArgsParser.h>
  9. #include <LibCore/System.h>
  10. #include <LibMain/Main.h>
  11. #include <assert.h>
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <inttypes.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <sys/mman.h>
  19. #include <sys/stat.h>
  20. #include <unistd.h>
  21. static bool try_set_offset_and_length_parameters(String const& arg_offset, String const& arg_length, u64& offset, u64& length)
  22. {
  23. // TODO: Add support for hex values
  24. auto possible_offset = arg_offset.to_uint<u64>();
  25. if (!possible_offset.has_value())
  26. return false;
  27. auto possible_length = arg_length.to_uint<u64>();
  28. if (!possible_length.has_value())
  29. return false;
  30. offset = possible_offset.value();
  31. length = possible_length.value();
  32. return true;
  33. }
  34. static void try_to_dump_with_memory_mapping(int fd, u64 offset, u64 length)
  35. {
  36. VERIFY(fd >= 0);
  37. u64 mmoffset = offset % sysconf(_SC_PAGESIZE);
  38. void* mmp = mmap(NULL, mmoffset + length, PROT_READ, MAP_SHARED, fd, offset - mmoffset);
  39. if (mmp == MAP_FAILED) {
  40. perror("mmap");
  41. return;
  42. }
  43. size_t ncomplete = 0;
  44. while (ncomplete < length) {
  45. ssize_t nwritten = write(STDOUT_FILENO, static_cast<u8*>(mmp) + ncomplete, length - ncomplete);
  46. if (nwritten < 0) {
  47. perror("write");
  48. return;
  49. }
  50. ncomplete += nwritten;
  51. }
  52. if (munmap(mmp, mmoffset + length) < 0) {
  53. perror("munmap");
  54. }
  55. }
  56. static void try_to_dump_with_read(int fd, u64 offset, u64 length)
  57. {
  58. VERIFY(fd >= 0);
  59. auto rs = lseek(fd, offset, SEEK_SET);
  60. if (rs < 0) {
  61. fprintf(stderr, "Couldn't seek to offset %" PRIi64 " while verifying: %s\n", offset, strerror(errno));
  62. return;
  63. }
  64. u8 buf[4096];
  65. size_t ncomplete = 0;
  66. while (ncomplete < length) {
  67. size_t length_to_be_read = min<size_t>((length - ncomplete), sizeof(buf));
  68. if (read(fd, buf, length_to_be_read) < 0) {
  69. perror("read");
  70. return;
  71. }
  72. ssize_t nwritten = write(STDOUT_FILENO, buf, length_to_be_read);
  73. if (nwritten < 0) {
  74. perror("write");
  75. return;
  76. }
  77. ncomplete += nwritten;
  78. }
  79. }
  80. ErrorOr<int> serenity_main(Main::Arguments arguments)
  81. {
  82. TRY(Core::System::pledge("stdio rpath"));
  83. StringView arg_offset;
  84. StringView arg_length;
  85. bool use_read_instead_of_mmap = false;
  86. Core::ArgsParser args;
  87. args.add_positional_argument(arg_offset, "Physical Address (Offset)", "offset", Core::ArgsParser::Required::Yes);
  88. args.add_positional_argument(arg_length, "Length of that region", "length", Core::ArgsParser::Required::Yes);
  89. args.add_option(use_read_instead_of_mmap, "Read /dev/mem instead of try to map it", nullptr, 'r');
  90. args.parse(arguments);
  91. u64 offset = 0;
  92. u64 length = 0;
  93. if (!try_set_offset_and_length_parameters(arg_offset, arg_length, offset, length)) {
  94. warnln("pmemdump: Invalid length or offset parameters\n");
  95. return 1;
  96. }
  97. int fd = open("/dev/mem", O_RDONLY);
  98. if (fd < 0) {
  99. perror("open");
  100. return 1;
  101. }
  102. if (use_read_instead_of_mmap)
  103. try_to_dump_with_read(fd, offset, length);
  104. else
  105. try_to_dump_with_memory_mapping(fd, offset, length);
  106. close(fd);
  107. return 0;
  108. }