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