summaryrefslogtreecommitdiffstats
path: root/utils/hooks/30-add-offb.c
blob: c635409f9708fc628409e01e8c7786c6833e752c (plain)
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492

#define _GNU_SOURCE

#include <stdlib.h>
#include <err.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/ioctl.h>
#include <string.h>
#include <stdbool.h>
#include <inttypes.h>

#include <linux/fb.h>

#include <libfdt.h>

#include <file/file.h>
#include <talloc/talloc.h>

static const char *fbdev_name = "fb0";

#define MAX_N_CELLS		4
#define ADDRESS_PROP_SIZE	4096

struct offb_ctx {
	const char			*dtb_name;
	void				*dtb;
	int				dtb_node;
	const char			*path;
	struct fb_fix_screeninfo	fscreeninfo;
	struct fb_var_screeninfo	vscreeninfo;
};

static int load_dtb(struct offb_ctx *ctx)
{
	char *buf;
	int len;
	int rc;

	rc = read_file(ctx, ctx->dtb_name, &buf, &len);
	if (rc) {
		warn("error reading %s", ctx->dtb_name);
		return rc;
	}

	rc = fdt_check_header(buf);
	if (rc || (int)fdt_totalsize(buf) > len) {
		warnx("invalid dtb: %s (rc %d)", ctx->dtb_name, rc);
		return -1;
	}

	len = fdt_totalsize(buf) + ADDRESS_PROP_SIZE;

	ctx->dtb = talloc_array(ctx, char, len);
	if (!ctx->dtb) {
		warn("Failed to allocate space for dtb\n");
		return -1;
	}
	fdt_open_into(buf, ctx->dtb, len);

	return 0;
}

static int fbdev_sysfs_lookup(struct offb_ctx *ctx)
{
	char *path, *linkpath, *nodepath;
	int fd, node;
	ssize_t rc __attribute__((unused));

	path = talloc_asprintf(ctx, "/sys/class/graphics/%s", fbdev_name);
	if (!path) {
		warn("Failed to allocate space for sysfs path\n");
		return -1;
	}

	fd = open(path, O_RDONLY | O_DIRECTORY);
	if (fd < 0) {
		warn("Can't open device %s in sysfs", fbdev_name);
		return -1;
	}

	linkpath = talloc_zero_array(ctx, char, PATH_MAX + 1);
	if (!linkpath) {
		warn("Failed to allocate space for link path\n");
		return -1;
	}

	rc = readlinkat(fd, "device/of_node", linkpath, PATH_MAX);
	if (rc < 0) {
		warn("Can't read of_node link for device %s", fbdev_name);
		return -1;
	}

	/* readlinkat() returns a relative path such as:
	 *
	 *  ../../../../../../../firmware/devicetree/base/pciex@n/…/vga@0
	 *
	 * We only need the path component from the device tree itself; so
	 * strip everything before /firmware/devicetree/base
	 */
	nodepath = strstr(linkpath, "/firmware/devicetree/base/");
	if (!nodepath) {
		warnx("Can't resolve device tree link for device %s",
				fbdev_name);
		return -1;
	}

	nodepath += strlen("/firmware/devicetree/base");

	node = fdt_path_offset(ctx->dtb, nodepath);
	if (node < 0) {
		warnx("Can't find node %s in device tree: %s",
				nodepath, fdt_strerror(node));
		return -1;
	}

	ctx->path = nodepath;
	ctx->dtb_node = node;

	return 0;
}

static int fbdev_device_query(struct offb_ctx *ctx)
{
	int fd, rc = -1;
	char *path;

	path = talloc_asprintf(ctx, "/dev/%s", fbdev_name);
	if (!path) {
		warn("Failed to allocate space for device path\n");
		return -1;
	}

	fd = open(path, O_RDWR);
	if (fd < 0) {
		warn("Can't open fb device %s", path);
		return -1;
	}

	rc = ioctl(fd, FBIOGET_VSCREENINFO, &ctx->vscreeninfo);
	if (rc) {
		warn("ioctl(FBIOGET_VSCREENINFO) failed");
		goto out;
	}

	rc = ioctl(fd, FBIOGET_FSCREENINFO, &ctx->fscreeninfo);
	if (rc) {
		warn("ioctl(FBIOGET_FSCREENINFO) failed");
		goto out;
	}

	fprintf(stderr, "Retrieved framebuffer details:\n");
	fprintf(stderr, "device %s:\n", fbdev_name);
	fprintf(stderr, "  addr: %lx\n", ctx->fscreeninfo.smem_start);
	fprintf(stderr, "   len: %" PRIu32 "\n", ctx->fscreeninfo.smem_len);
	fprintf(stderr, "  line: %d\n", ctx->fscreeninfo.line_length);
	fprintf(stderr, "   res:  %dx%d@%d\n", ctx->vscreeninfo.xres,
			ctx->vscreeninfo.yres,
			ctx->vscreeninfo.bits_per_pixel);

	rc = 0;

out:
	close(fd);
	return rc;
}

static char *next_dt_name(struct offb_ctx *ctx, const char **path)
{
	const char *c, *p;
	char *name;

	p = *path;

	if (p[0] == '/')
		p++;

	if (p[0] == '\0')
		return NULL;

	c = strchrnul(p, '/');

	name = talloc_strndup(ctx, p, c - p);

	*path = c;

	return name;
}

static uint64_t of_read_number(const fdt32_t *data, int n)
{
	uint64_t x;

	x = fdt32_to_cpu(data[0]);
	if (n > 1) {
		x <<= 32;
		x |= fdt32_to_cpu(data[1]);
	}
	return x;
}

/* Do a single translation across a PCI bridge. This results in either;
 * - Translating a 2-cell CPU address into a 3-cell PCI address, or
 * - Translating a 3-cell PCI address into a 3-cell PCI address with a
 *   different offset.
 *
 * To simplify translation we make some assumptions about addresses:
 * Addresses are either 3 or 2 cells wide
 * Size is always 2 cells wide
 * The first cell of a 3 cell address is the PCI memory type
 */
static int do_translate(void *fdt, int node,
		const fdt32_t *ranges, int range_size,
		uint32_t *addr, uint32_t *size,
		int *addr_cells, int *size_cells)
{
	uint64_t addr_current_base, addr_child_base, addr_size;
	uint64_t addr_current, offset, new_addr;
	uint64_t current_pci_flags, child_pci_flags;
	int i, na, ns, cna, cns, prop_len;
	const fdt32_t *prop;
	const char *type;
	bool pci = false;

	type = fdt_getprop(fdt, node, "device_type", NULL);
	pci = type && (!strcmp(type, "pci") || !strcmp(type, "pciex"));

	/* We don't translate at vga@0, so we should always see a pci or
	 * pciex device_type */
	if (!pci)
		return -1;

	if (range_size == 0) {
		fprintf(stderr, "Empty ranges property, 1:1 translation\n");
		return 0;
	}

	/* Number of cells for address and size at current level */
	na = *addr_cells;
	ns = *size_cells;

	/* Number of cells for address and size at child level */
	prop = fdt_getprop(fdt, node, "#address-cells", &prop_len);
	cna = prop ? fdt32_to_cpu(*prop) : 2;
	prop = fdt_getprop(fdt, node, "#size-cells", &prop_len);
	cns = prop ? fdt32_to_cpu(*prop) : 2;

	/* We're translating back to a PCI address, so the size should grow */
	if (na > cna) {
		fprintf(stderr, "na > cna, unexpected\n");
		return -1;
	}

	/* If the current address is a PCI address, its type should match the
	 * type of every subsequent child address */
	current_pci_flags = na > 2 ? of_read_number(addr, 1) : 0;
	child_pci_flags = cna > 2 ? of_read_number(ranges, 1) : 0;
	if (current_pci_flags != 0 && current_pci_flags != child_pci_flags) {
		fprintf(stderr, "Unexpected change in flags: %lx, %lx\n",
			current_pci_flags, child_pci_flags);
		return -1;
	}

	if (ns != cns) {
		fprintf(stderr, "Unexpected change in #size-cells: %d vs %d\n",
			ns, cns);
		return -1;
	}

	/*
	 * The ranges property is of the form
	 *	< upstream addr base > < downstream addr base > < size >
	 * The current address stored in addr is similarly of the form
	 *	< current address > < size >
	 * Where either address base and the current address can be a 2-cell
	 * CPU address or a 3-cell PCI address.
	 *
	 * For PCI addresses ignore the type flag in the first cell and use the
	 * 64-bit address in the remaining 2 cells.
	 */
	if (na > 2) {
		addr_current_base =  of_read_number(ranges + cna + 1, na - 1);
		addr_current =  of_read_number(addr + 1, na - 1);
	} else {
		addr_current_base =  of_read_number(ranges + cna, na);
		addr_current =  of_read_number(addr, na);
	}
	if (cna > 2)
		addr_child_base =  of_read_number(ranges + 1, cna - 1);
	else
		addr_child_base =  of_read_number(ranges, cna);

	/*
	 * Perform the actual translation. Find the offset of the current
	 * address from the upstream base, and add the offset to the
	 * downstream base to find the new address.
	 * The new address will be cna-cells wide, inheriting child_pci_flags
	 * as the memory type.
	 */
	addr_size = of_read_number(size, ns);
	offset = addr_current - addr_current_base;
	new_addr = addr_child_base + offset;

	memset(addr, 0, *addr_cells);
	memset(size, 0, *size_cells);
	*addr_cells = cna;
	*size_cells = cns;

	/* Update the current address in addr.
	 * It's highly unlikely any translation will leave us with a 2-cell
	 * CPU address, but for completeness only include PCI flags if the
	 * child offset was definitely a PCI address */
	if (*addr_cells > 2)
		addr[0] = cpu_to_fdt32(child_pci_flags);
	for (i = *addr_cells - 1; i >= *addr_cells - 2; i--) {
		addr[i] = cpu_to_fdt32(new_addr & 0xffffffff);
		new_addr >>= 32;
	}
	for (i = *size_cells - 1; i >= 0; i--) {
		size[i] = cpu_to_fdt32(addr_size & 0xffffffff);
		addr_size >>= 32;
	}

	fprintf(stderr, "New address:\n\t");
	for (i = 0; i < *addr_cells; i++)
		fprintf(stderr, " %lx ", of_read_number(&addr[i], 1));
	fprintf(stderr, "\n");

	return 0;
}

static int create_translated_addresses(struct offb_ctx *ctx,
		int dev_node, const char *path,
		uint64_t in_addr, uint64_t in_size,
		fdt32_t *reg, int reg_cells)
{
	uint32_t addr[MAX_N_CELLS], size[MAX_N_CELLS];
	int addr_cells, size_cells, node, prop_len, ranges_len, rc, i;
	const fdt32_t *ranges, *prop;
	char *name;

	prop = fdt_getprop(ctx->dtb, 0, "#address-cells", &prop_len);
	addr_cells = prop ? fdt32_to_cpu(*prop) : 2;

	prop = fdt_getprop(ctx->dtb, 0, "#size-cells", &prop_len);
	size_cells = prop ? fdt32_to_cpu(*prop) : 2;

	memset(addr, 0, sizeof(uint32_t) * MAX_N_CELLS);
	for (i = addr_cells - 1; i >= 0; i--) {
		addr[i] = cpu_to_fdt32(in_addr & 0xffffffff);
		in_addr >>= 32;
	}
	memset(size, 0, sizeof(uint32_t) * MAX_N_CELLS);
	for (i = size_cells - 1; i >= 0; i--) {
		size[i] = cpu_to_fdt32(in_size & 0xffffffff);
		in_size >>= 32;
	}

	node = 0;
	for (;;) {
		/* get the name of the next child node to 'node' */
		name = next_dt_name(ctx, &path);
		if (!name)
			return -1;

		node = fdt_subnode_offset(ctx->dtb, node, name);
		if (node < 0)
			return -1;
		if (node == dev_node)
			break;

		ranges = fdt_getprop(ctx->dtb, node, "ranges", &ranges_len);
		if (!ranges)
			return -1;

		rc = do_translate(ctx->dtb, node, ranges, ranges_len,
			     addr, size, &addr_cells, &size_cells);
		if (rc)
			return -1;
	}

	fprintf(stderr, "Final address:\n\t");
	for (i = 0; i < addr_cells; i++)
		fprintf(stderr, " %lx ", of_read_number(&addr[i], 1));
	fprintf(stderr, "\n");

	if (addr_cells + size_cells > reg_cells) {
		fprintf(stderr, "Error: na + ns larger than reg\n");
		return -1;
	}

	memcpy(reg, addr, sizeof(fdt32_t) * addr_cells);
	memcpy(reg + addr_cells, size, sizeof(fdt32_t) * size_cells);

	return 0;
}

#define fdt_set_check(dtb, node, fn, prop, ...) \
	do {								\
		int __x = fn(dtb, node,	prop, __VA_ARGS__);		\
		if (__x) {						\
			warnx("failed to update device tree (%s): %s",	\
					prop, fdt_strerror(__x));	\
			return -1;					\
		}							\
	} while (0);

static int populate_devicetree(struct offb_ctx *ctx)
{
	fdt32_t reg[5];
	void *dtb = ctx->dtb;
	int rc, node = ctx->dtb_node;

	memset(reg, 0, sizeof(reg));
	rc = create_translated_addresses(ctx, node, ctx->path,
				ctx->fscreeninfo.smem_start,
				ctx->fscreeninfo.smem_len,
				reg, 5);

	if (rc) {
		fprintf(stderr, "Failed to translate address\n");
		return rc;
	}

	fdt_set_check(dtb, node, fdt_setprop_string, "device_type", "display");

	fdt_set_check(dtb, node, fdt_setprop, "assigned-addresses",
			reg, sizeof(reg));

	fdt_set_check(dtb, node, fdt_setprop_cell,
			"width", ctx->vscreeninfo.xres);
	fdt_set_check(dtb, node, fdt_setprop_cell,
			"height", ctx->vscreeninfo.yres);
	fdt_set_check(dtb, node, fdt_setprop_cell,
			"depth", ctx->vscreeninfo.bits_per_pixel);

	fdt_set_check(dtb, node, fdt_setprop, "little-endian", NULL, 0);
	fdt_set_check(dtb, node, fdt_setprop, "linux,opened", NULL, 0);
	fdt_set_check(dtb, node, fdt_setprop, "linux,boot-display", NULL, 0);

	return 0;
}

static int write_devicetree(struct offb_ctx *ctx)
{
	int rc;

	fdt_pack(ctx->dtb);

	rc = replace_file(ctx->dtb_name, ctx->dtb, fdt_totalsize(ctx->dtb));
	if (rc)
		warn("failed to write file %s", ctx->dtb_name);

	return rc;
}


int main(void)
{
	struct offb_ctx *ctx;
	int rc;

	ctx = talloc_zero(NULL, struct offb_ctx);

	ctx->dtb_name = getenv("boot_dtb");
	if (!ctx->dtb_name) {
		talloc_free(ctx);
		return EXIT_SUCCESS;
	}

	rc = load_dtb(ctx);
	if (rc)
		goto out;

	rc = fbdev_sysfs_lookup(ctx);
	if (rc)
		goto out;

	rc = fbdev_device_query(ctx);
	if (rc)
		goto out;

	rc = populate_devicetree(ctx);
	if (rc)
		goto out;

	rc = write_devicetree(ctx);

out:
	talloc_free(ctx);
	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
}
OpenPOWER on IntegriCloud