17 #include <file/file.h>
18 #include <talloc/talloc.h>
20 static const char *fbdev_name = "fb0";
23 #define ADDRESS_PROP_SIZE 4096
30 struct fb_fix_screeninfo fscreeninfo;
31 struct fb_var_screeninfo vscreeninfo;
34 static int load_dtb(struct offb_ctx *ctx)
40 rc = read_file(ctx, ctx->dtb_name, &buf, &len);
42 warn("error reading %s", ctx->dtb_name);
46 rc = fdt_check_header(buf);
47 if (rc || (int)fdt_totalsize(buf) > len) {
48 warnx("invalid dtb: %s (rc %d)", ctx->dtb_name, rc);
52 len = fdt_totalsize(buf) + ADDRESS_PROP_SIZE;
54 ctx->dtb = talloc_array(ctx, char, len);
56 warn("Failed to allocate space for dtb\n");
59 fdt_open_into(buf, ctx->dtb, len);
64 static int fbdev_sysfs_lookup(struct offb_ctx *ctx)
66 char *path, *linkpath, *nodepath;
68 ssize_t rc __attribute__((unused));
70 path = talloc_asprintf(ctx, "/sys/class/graphics/%s", fbdev_name);
72 warn("Failed to allocate space for sysfs path\n");
76 fd = open(path, O_RDONLY | O_DIRECTORY);
78 warn("Can't open device %s in sysfs", fbdev_name);
82 linkpath = talloc_zero_array(ctx, char, PATH_MAX + 1);
84 warn("Failed to allocate space for link path\n");
88 rc = readlinkat(fd, "device/of_node", linkpath, PATH_MAX);
90 warn("Can't read of_node link for device %s", fbdev_name);
94 /* readlinkat() returns a relative path such as:
96 * ../../../../../../../firmware/devicetree/base/pciex@n/…/vga@0
98 * We only need the path component from the device tree itself; so
99 * strip everything before /firmware/devicetree/base
101 nodepath = strstr(linkpath, "/firmware/devicetree/base/");
103 warnx("Can't resolve device tree link for device %s",
108 nodepath += strlen("/firmware/devicetree/base");
110 node = fdt_path_offset(ctx->dtb, nodepath);
112 warnx("Can't find node %s in device tree: %s",
113 nodepath, fdt_strerror(node));
117 ctx->path = nodepath;
118 ctx->dtb_node = node;
123 static int fbdev_device_query(struct offb_ctx *ctx)
128 path = talloc_asprintf(ctx, "/dev/%s", fbdev_name);
130 warn("Failed to allocate space for device path\n");
134 fd = open(path, O_RDWR);
136 warn("Can't open fb device %s", path);
140 rc = ioctl(fd, FBIOGET_VSCREENINFO, &ctx->vscreeninfo);
142 warn("ioctl(FBIOGET_VSCREENINFO) failed");
146 rc = ioctl(fd, FBIOGET_FSCREENINFO, &ctx->fscreeninfo);
148 warn("ioctl(FBIOGET_FSCREENINFO) failed");
152 fprintf(stderr, "Retrieved framebuffer details:\n");
153 fprintf(stderr, "device %s:\n", fbdev_name);
154 fprintf(stderr, " addr: %lx\n", ctx->fscreeninfo.smem_start);
155 fprintf(stderr, " len: %" PRIu32 "\n", ctx->fscreeninfo.smem_len);
156 fprintf(stderr, " line: %d\n", ctx->fscreeninfo.line_length);
157 fprintf(stderr, " res: %dx%d@%d\n", ctx->vscreeninfo.xres,
158 ctx->vscreeninfo.yres,
159 ctx->vscreeninfo.bits_per_pixel);
168 static char *next_dt_name(struct offb_ctx *ctx, const char **path)
181 c = strchrnul(p, '/');
183 name = talloc_strndup(ctx, p, c - p);
190 static uint64_t of_read_number(const fdt32_t *data, int n)
194 x = fdt32_to_cpu(data[0]);
197 x |= fdt32_to_cpu(data[1]);
202 /* Do a single translation across a PCI bridge. This results in either;
203 * - Translating a 2-cell CPU address into a 3-cell PCI address, or
204 * - Translating a 3-cell PCI address into a 3-cell PCI address with a
207 * To simplify translation we make some assumptions about addresses:
208 * Addresses are either 3 or 2 cells wide
209 * Size is always 2 cells wide
210 * The first cell of a 3 cell address is the PCI memory type
212 static int do_translate(void *fdt, int node,
213 const fdt32_t *ranges, int range_size,
214 uint32_t *addr, uint32_t *size,
215 int *addr_cells, int *size_cells)
217 uint64_t addr_current_base, addr_child_base, addr_size;
218 uint64_t addr_current, offset, new_addr;
219 uint64_t current_pci_flags, child_pci_flags;
220 int i, na, ns, cna, cns, prop_len;
225 type = fdt_getprop(fdt, node, "device_type", NULL);
226 pci = type && (!strcmp(type, "pci") || !strcmp(type, "pciex"));
228 /* We don't translate at vga@0, so we should always see a pci or
229 * pciex device_type */
233 if (range_size == 0) {
234 fprintf(stderr, "Empty ranges property, 1:1 translation\n");
238 /* Number of cells for address and size at current level */
242 /* Number of cells for address and size at child level */
243 prop = fdt_getprop(fdt, node, "#address-cells", &prop_len);
244 cna = prop ? fdt32_to_cpu(*prop) : 2;
245 prop = fdt_getprop(fdt, node, "#size-cells", &prop_len);
246 cns = prop ? fdt32_to_cpu(*prop) : 2;
248 /* We're translating back to a PCI address, so the size should grow */
250 fprintf(stderr, "na > cna, unexpected\n");
254 /* If the current address is a PCI address, its type should match the
255 * type of every subsequent child address */
256 current_pci_flags = na > 2 ? of_read_number(addr, 1) : 0;
257 child_pci_flags = cna > 2 ? of_read_number(ranges, 1) : 0;
258 if (current_pci_flags != 0 && current_pci_flags != child_pci_flags) {
259 fprintf(stderr, "Unexpected change in flags: %lx, %lx\n",
260 current_pci_flags, child_pci_flags);
265 fprintf(stderr, "Unexpected change in #size-cells: %d vs %d\n",
271 * The ranges property is of the form
272 * < upstream addr base > < downstream addr base > < size >
273 * The current address stored in addr is similarly of the form
274 * < current address > < size >
275 * Where either address base and the current address can be a 2-cell
276 * CPU address or a 3-cell PCI address.
278 * For PCI addresses ignore the type flag in the first cell and use the
279 * 64-bit address in the remaining 2 cells.
282 addr_current_base = of_read_number(ranges + cna + 1, na - 1);
283 addr_current = of_read_number(addr + 1, na - 1);
285 addr_current_base = of_read_number(ranges + cna, na);
286 addr_current = of_read_number(addr, na);
289 addr_child_base = of_read_number(ranges + 1, cna - 1);
291 addr_child_base = of_read_number(ranges, cna);
294 * Perform the actual translation. Find the offset of the current
295 * address from the upstream base, and add the offset to the
296 * downstream base to find the new address.
297 * The new address will be cna-cells wide, inheriting child_pci_flags
298 * as the memory type.
300 addr_size = of_read_number(size, ns);
301 offset = addr_current - addr_current_base;
302 new_addr = addr_child_base + offset;
304 memset(addr, 0, *addr_cells);
305 memset(size, 0, *size_cells);
309 /* Update the current address in addr.
310 * It's highly unlikely any translation will leave us with a 2-cell
311 * CPU address, but for completeness only include PCI flags if the
312 * child offset was definitely a PCI address */
314 addr[0] = cpu_to_fdt32(child_pci_flags);
315 for (i = *addr_cells - 1; i >= *addr_cells - 2; i--) {
316 addr[i] = cpu_to_fdt32(new_addr & 0xffffffff);
319 for (i = *size_cells - 1; i >= 0; i--) {
320 size[i] = cpu_to_fdt32(addr_size & 0xffffffff);
324 fprintf(stderr, "New address:\n\t");
325 for (i = 0; i < *addr_cells; i++)
326 fprintf(stderr, " %lx ", of_read_number(&addr[i], 1));
327 fprintf(stderr, "\n");
332 static int create_translated_addresses(struct offb_ctx *ctx,
333 int dev_node, const char *path,
334 uint64_t in_addr, uint64_t in_size,
335 fdt32_t *reg, int reg_cells)
337 uint32_t addr[MAX_N_CELLS], size[MAX_N_CELLS];
338 int addr_cells, size_cells, node, prop_len, ranges_len, rc, i;
339 const fdt32_t *ranges, *prop;
342 prop = fdt_getprop(ctx->dtb, 0, "#address-cells", &prop_len);
343 addr_cells = prop ? fdt32_to_cpu(*prop) : 2;
345 prop = fdt_getprop(ctx->dtb, 0, "#size-cells", &prop_len);
346 size_cells = prop ? fdt32_to_cpu(*prop) : 2;
348 memset(addr, 0, sizeof(uint32_t) * MAX_N_CELLS);
349 for (i = addr_cells - 1; i >= 0; i--) {
350 addr[i] = cpu_to_fdt32(in_addr & 0xffffffff);
353 memset(size, 0, sizeof(uint32_t) * MAX_N_CELLS);
354 for (i = size_cells - 1; i >= 0; i--) {
355 size[i] = cpu_to_fdt32(in_size & 0xffffffff);
361 /* get the name of the next child node to 'node' */
362 name = next_dt_name(ctx, &path);
366 node = fdt_subnode_offset(ctx->dtb, node, name);
369 if (node == dev_node)
372 ranges = fdt_getprop(ctx->dtb, node, "ranges", &ranges_len);
376 rc = do_translate(ctx->dtb, node, ranges, ranges_len,
377 addr, size, &addr_cells, &size_cells);
382 fprintf(stderr, "Final address:\n\t");
383 for (i = 0; i < addr_cells; i++)
384 fprintf(stderr, " %lx ", of_read_number(&addr[i], 1));
385 fprintf(stderr, "\n");
387 if (addr_cells + size_cells > reg_cells) {
388 fprintf(stderr, "Error: na + ns larger than reg\n");
392 memcpy(reg, addr, sizeof(fdt32_t) * addr_cells);
393 memcpy(reg + addr_cells, size, sizeof(fdt32_t) * size_cells);
398 #define fdt_set_check(dtb, node, fn, prop, ...) \
400 int __x = fn(dtb, node, prop, __VA_ARGS__); \
402 warnx("failed to update device tree (%s): %s", \
403 prop, fdt_strerror(__x)); \
408 static int populate_devicetree(struct offb_ctx *ctx)
411 void *dtb = ctx->dtb;
412 int rc, node = ctx->dtb_node;
414 memset(reg, 0, sizeof(reg));
415 rc = create_translated_addresses(ctx, node, ctx->path,
416 ctx->fscreeninfo.smem_start,
417 ctx->fscreeninfo.smem_len,
421 fprintf(stderr, "Failed to translate address\n");
425 fdt_set_check(dtb, node, fdt_setprop_string, "device_type", "display");
427 fdt_set_check(dtb, node, fdt_setprop, "assigned-addresses",
430 fdt_set_check(dtb, node, fdt_setprop_cell,
431 "width", ctx->vscreeninfo.xres);
432 fdt_set_check(dtb, node, fdt_setprop_cell,
433 "height", ctx->vscreeninfo.yres);
434 fdt_set_check(dtb, node, fdt_setprop_cell,
435 "depth", ctx->vscreeninfo.bits_per_pixel);
437 fdt_set_check(dtb, node, fdt_setprop, "little-endian", NULL, 0);
438 fdt_set_check(dtb, node, fdt_setprop, "linux,opened", NULL, 0);
439 fdt_set_check(dtb, node, fdt_setprop, "linux,boot-display", NULL, 0);
444 static int write_devicetree(struct offb_ctx *ctx)
450 rc = replace_file(ctx->dtb_name, ctx->dtb, fdt_totalsize(ctx->dtb));
452 warn("failed to write file %s", ctx->dtb_name);
460 struct offb_ctx *ctx;
463 ctx = talloc_zero(NULL, struct offb_ctx);
465 ctx->dtb_name = getenv("boot_dtb");
466 if (!ctx->dtb_name) {
475 rc = fbdev_sysfs_lookup(ctx);
479 rc = fbdev_device_query(ctx);
483 rc = populate_devicetree(ctx);
487 rc = write_devicetree(ctx);
491 return rc ? EXIT_FAILURE : EXIT_SUCCESS;