This repository has been archived by the owner on Oct 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 522
/
memdump.c
92 lines (75 loc) · 2.06 KB
/
memdump.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "libkdump.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
static int running = 1;
void sigint(int signum __attribute__((unused))) {
running = 0;
}
int main(int argc, char *argv[]) {
size_t phys = 1ull * 1024ull * 1024ull * 1024ull; // start at first gigabyte
size_t size = (size_t)-1ULL;
if (argc >= 2) {
phys = strtoull(argv[1], NULL, 0);
}
if (argc >= 3) {
size = strtoull(argv[2], NULL, 0);
}
int width = 16; // characters per line
int suppress_empty = 1;
libkdump_config_t config;
config = libkdump_get_autoconfig();
config.retries = 10;
config.measurements = 2;
if (argc >= 4) {
config.physical_offset = strtoull(argv[3], NULL, 0);
}
libkdump_init(config);
size_t vaddr = libkdump_phys_to_virt(phys);
if (vaddr == -1ULL) {
fprintf(stderr, "Error converting physical to virtual address\n");
return -1;
}
printf(
"\x1b[32;1m[+]\x1b[0m Physical address : \x1b[33;1m0x%zx\x1b[0m\n", phys);
printf("\x1b[32;1m[+]\x1b[0m Physical offset : \x1b[33;1m0x%zx\x1b[0m\n", config.physical_offset);
printf(
"\x1b[32;1m[+]\x1b[0m Virtual address : \x1b[33;1m0x%zx\x1b[0m\n", vaddr);
if (width > size)
width = size;
size_t delta = 0;
int i;
char *buffer = malloc(width);
signal(SIGINT, sigint);
while (running && delta < size) {
int value = libkdump_read(vaddr + delta);
buffer[delta % width] = value;
if (delta % width == width - 1) {
int skip = 1;
for (i = 0; i < width; i++) {
if (buffer[i]) {
skip = 0;
break;
}
}
if (skip && suppress_empty) {
delta++;
continue;
}
printf("%10zx: ", delta + phys - (width - 1));
printf("| ");
for (i = 0; i < width; i++) {
printf("%02x ", (unsigned char)buffer[i]);
}
printf("| ");
for (i = 0; i < width; i++) {
printf("%c", (buffer[i] >= 32 && buffer[i] <= 126) ? buffer[i] : '.');
}
printf(" |\n");
}
delta++;
}
free(buffer);
libkdump_cleanup();
return 0;
}