summaryrefslogtreecommitdiff
path: root/vc-log.c
blob: dce8822495acf29d00971eaf3834d288ac11b075 (plain) (blame)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <string.h>

static int vc_fd;
static uint32_t vc_mem_size, vc_mem_base, vc_mem_load, vc_mem_phys;
static void *vc_mem;
static uint32_t vc_syms_off;

#define VC_MEM_IOC_MAGIC  'v'

#define VC_MEM_IOC_MEM_PHYS_ADDR    _IOR( VC_MEM_IOC_MAGIC, 0, unsigned long )
#define VC_MEM_IOC_MEM_SIZE         _IOR( VC_MEM_IOC_MAGIC, 1, unsigned int )
#define VC_MEM_IOC_MEM_BASE         _IOR( VC_MEM_IOC_MAGIC, 2, unsigned int )
#define VC_MEM_IOC_MEM_LOAD         _IOR( VC_MEM_IOC_MAGIC, 3, unsigned int )
#define VC_MEM_IOC_MEM_COPY         _IOWR( VC_MEM_IOC_MAGIC, 4, char *)

#define VC_DEBUG_HEADER_OFFSET 0x2800
#define VC_SYMBOL_BASE_OFFSET VC_DEBUG_HEADER_OFFSET

static void vc_read(void *buf, size_t off, size_t size) {
	// if(off < vc_mem_base) {
	// 	fprintf(stderr, "small address\n");
	// 	abort();
	// }
	if(off > vc_mem_size || size > vc_mem_size - off) {
		fprintf(stderr, "out of bounds read: %zx+%zx/%x\n", off, size, vc_mem_size);
		abort();
	}

	if(size == 0) return;

	size_t skip = off % 4;
	volatile uint32_t *inp = (volatile uint32_t*)((char*)vc_mem + (off - skip));
	char *out = buf;
	if(skip) {
		uint32_t v = *inp;
		inp++;
		size_t have = 4 - skip;
		if(have >= size) {
			memcpy(out, (char*)&v + skip, size);
			return;
		}
		memcpy(out, (char*)&v + skip, have);
		out += have;
		size -= have;
	}

	while(1) {
		uint32_t v = *inp;
		inp++;
		if(size < 4) {
			memcpy(out, &v, size);
			return;
		}
		memcpy(out, &v, 4);
		out += 4;
		size -= 4;
	}
}

static uint32_t vc_read_u32(size_t off) {
	uint32_t r = 42;
	vc_read(&r, off, 4);
	return r;
}

static uint16_t vc_read_u16(size_t off) {
	uint16_t r = 42;
	vc_read(&r, off, 2);
	return r;
}

static uint8_t vc_read_u8(size_t off) {
	uint8_t r = 42;
	vc_read(&r, off, 1);
	return r;
}

static size_t vc_read_ptr(size_t off) {
	uint32_t r = vc_read_u32(off);
	if(r == 0) return 0;
	if(r <= 0xc0000000) {
		fprintf(stderr, "invalid pointer: %x\n", r);
		abort();
	}
	return r - 0xc0000000;
}

static char *vc_read_str(size_t off) {
	size_t len = 0, alloc = 16;
	char *r = malloc(alloc);
	if(!r) abort();
	while(1) {
		size_t roff = off + len;
		size_t try_read = alloc - len;
		if(try_read > vc_mem_size - roff) {
			try_read = vc_mem_size - roff;
		}
		if(try_read == 0) abort();
		vc_read(r + len, roff, try_read);
		size_t add = strnlen(r + len, try_read);
		len += add;
		if(add < try_read) {
			break;
		}

		size_t alloc2 = alloc + (alloc << 2);
		char *r2 = realloc(r, alloc2);
		if(!r2) abort();
		r = r2;
		alloc = alloc2;
	}

	return r;
}

static void vc_open(void) {
	int r = open("/dev/vc-mem", O_RDWR | O_SYNC | O_CLOEXEC);
	if(r < 0) {
		perror("open");
		abort();
	}
	vc_fd = r;

	r = ioctl(vc_fd, VC_MEM_IOC_MEM_SIZE, &vc_mem_size);
	if(r < 0) {
		perror("VC_MEM_IOC_MEM_SIZE");
		abort();
	}
	r = ioctl(vc_fd, VC_MEM_IOC_MEM_BASE, &vc_mem_base);
	if(r < 0) {
		perror("VC_MEM_IOC_MEM_BASE");
		abort();
	}
	r = ioctl(vc_fd, VC_MEM_IOC_MEM_LOAD, &vc_mem_load);
	if(r < 0) {
		perror("VC_MEM_IOC_MEM_LOAD");
		abort();
	}
	r = ioctl(vc_fd, VC_MEM_IOC_MEM_PHYS_ADDR, &vc_mem_phys);
	if(r < 0) {
		perror("VC_MEM_IOC_MEM_PHYS_ADDR");
		abort();
	}

	vc_mem = mmap(NULL, vc_mem_size, PROT_READ, MAP_SHARED, vc_fd, 0);
	if(vc_mem == MAP_FAILED) {
		perror("mmap");
		abort();
	}

	uint32_t dbg_off = vc_mem_load + VC_DEBUG_HEADER_OFFSET;
	uint32_t dbg_magic = vc_read_u32(dbg_off + 4);
	if(dbg_magic != 0x48444356) {
		fprintf(stderr, "bad debug magic: %x, expected 48444356\n", dbg_magic);
		abort();
	}
	vc_syms_off = vc_read_u32(dbg_off);
}

static size_t vc_sym_find(const char *want) {
	uint32_t at = vc_syms_off;
	while(1) {
		uint32_t label = vc_read_u32(at + 0);
		uint32_t addr = vc_read_u32(at + 4);
		if(label == 0) return 0;

		char *name = vc_read_str(label);
		int r = strcmp(name, want);
		free(name);
		if(!r) return addr;

		at += 12;
	}
}

static void vc_log_read(size_t descriptor_ptr) {
	uint8_t fmt = vc_read_u8(descriptor_ptr);
	size_t ptr = vc_read_ptr(descriptor_ptr + 4);
	if(!ptr) {
		printf("no data\n");
	}
	if(fmt != 1) {
		printf("unsupported log format %u\n", fmt);
		return;
	}

	uint32_t magic = vc_read_u32(ptr);
	if(magic != 0x5353454d) {
		printf("bad magic %x, wanted 5353454d", magic);
		return;
	}

	size_t l_start = vc_read_ptr(ptr + 4);
	size_t l_end = vc_read_ptr(ptr + 8);
	size_t l_ptr = vc_read_ptr(ptr + 12);
	size_t l_next_msg = vc_read_ptr(ptr + 16);

	size_t at = l_next_msg;
	do {
		uint32_t time = vc_read_u32(at + 0);
		uint32_t seq = vc_read_u16(at + 4);
		size_t size = vc_read_u16(at + 6);
		if(size < 12) {
			printf("short message: %zu\n", size);
			return;
		}
		size_t len = size - 12;
		char *s = malloc(len + 1);
		if(!s) abort();
		vc_read(s, at + 12, len);
		s[len] = 0;
		printf("[%u : %u] %s\n", time, seq, s);
		free(s);

		at += size;
		if(at == l_end) at = l_start;
	} while(at != l_ptr);
}

static void vc_log_read_all(void) {
	size_t log_start = vc_sym_find("__LOG_START");
	if(!log_start) {
		fprintf(stderr, "no __LOG_START\n");
		abort();
	}
	log_start = vc_read_ptr(log_start);

	uint32_t log_magic = vc_read_u32(log_start);
	if(log_magic != 0x564c4f47) {
		fprintf(stderr, "bad log magic: %x, wanted 564c4f47\n", log_magic);
		abort();
	}

	vc_log_read(log_start + 40);
}

int main() {
	vc_open();
	vc_log_read_all();

	close(vc_fd);
	return 0;
}