boot/efi/dtb: code cleanup for FDT dump

* conditional compile for dump_fdt() with #ifdef
* rename functions according to coding standard
* use dprintf() for printing

Change-Id: Ie486739d87f1cb9a005188a46a43f470b6000319
Reviewed-on: https://review.haiku-os.org/c/haiku/+/5536
Tested-by: Commit checker robot <[email protected]>
Reviewed-by: Fredrik Holmqvist <[email protected]>
This commit is contained in:
David Karoly
2022-08-09 13:32:26 +00:00
parent 3ef6915cf3
commit f5aba47edd
+74 -102
View File
@@ -49,6 +49,9 @@ extern "C" {
#define GIC_INTERRUPT_BASE_PPI 16 #define GIC_INTERRUPT_BASE_PPI 16
//#define TRACE_DUMP_FDT
#define INFO(x...) dprintf("efi/fdt: " x) #define INFO(x...) dprintf("efi/fdt: " x)
#define ERROR(x...) dprintf("efi/fdt: " x) #define ERROR(x...) dprintf("efi/fdt: " x)
@@ -56,12 +59,6 @@ extern "C" {
static void* sDtbTable = NULL; static void* sDtbTable = NULL;
static uint32 sDtbSize = 0; static uint32 sDtbSize = 0;
static void WriteString(const char *str) {dprintf("%s", str);}
static void WriteLn() {dprintf("\n");}
static void WriteHex(uint64_t val, int n) {dprintf("%08" B_PRIx64, val);}
static void WriteInt(int64_t val) {dprintf("%" B_PRId64, val);}
template <typename T> DebugUART* template <typename T> DebugUART*
get_uart(addr_t base, int64 clock) { get_uart(addr_t base, int64 clock) {
static char buffer[sizeof(T)]; static char buffer[sizeof(T)];
@@ -90,61 +87,58 @@ const struct supported_uarts {
}; };
static void WriteStringList(const char* prop, size_t size) #ifdef TRACE_DUMP_FDT
static void
write_string_list(const char* prop, size_t size)
{ {
bool first = true; bool first = true;
const char* propEnd = prop + size; const char* propEnd = prop + size;
while (propEnd - prop > 0) { while (propEnd - prop > 0) {
if (first) first = false; else WriteString(", "); if (first)
first = false;
else
dprintf(", ");
int curLen = strlen(prop); int curLen = strlen(prop);
WriteString("'"); dprintf("'%s'", prop);
WriteString(prop);
WriteString("'");
prop += curLen + 1; prop += curLen + 1;
} }
} }
static void DumpFdt(const void *fdt) static void
dump_fdt(const void *fdt)
{ {
if (!fdt) if (!fdt)
return; return;
int err = fdt_check_header(fdt); int err = fdt_check_header(fdt);
if (err) { if (err) {
WriteString("fdt error: "); dprintf("fdt error: %s\n", fdt_strerror(err));
WriteString(fdt_strerror(err));
WriteLn();
return; return;
} }
WriteString("fdt tree:"); WriteLn(); dprintf("fdt tree:\n");
int node = -1; int node = -1;
int depth = -1; int depth = -1;
while ((node = fdt_next_node(fdt, node, &depth)) >= 0 && depth >= 0) { while ((node = fdt_next_node(fdt, node, &depth)) >= 0 && depth >= 0) {
for (int i = 0; i < depth; i++) WriteString(" "); for (int i = 0; i < depth; i++)
dprintf(" ");
// WriteInt(node); WriteString(", "); WriteInt(depth); WriteString(": "); // WriteInt(node); WriteString(", "); WriteInt(depth); WriteString(": ");
WriteString("node('"); dprintf("node('%s')\n", fdt_get_name(fdt, node, NULL));
WriteString(fdt_get_name(fdt, node, NULL));
WriteString("')"); WriteLn();
depth++; depth++;
for (int prop = fdt_first_property_offset(fdt, node); prop >= 0; prop = fdt_next_property_offset(fdt, prop)) { for (int prop = fdt_first_property_offset(fdt, node); prop >= 0; prop = fdt_next_property_offset(fdt, prop)) {
int len; int len;
const struct fdt_property *property = fdt_get_property_by_offset(fdt, prop, &len); const struct fdt_property *property = fdt_get_property_by_offset(fdt, prop, &len);
if (property == NULL) { if (property == NULL) {
for (int i = 0; i < depth; i++) WriteString(" "); for (int i = 0; i < depth; i++)
WriteString("getting prop at "); dprintf(" ");
WriteInt(prop); dprintf("getting prop at %d: %s\n", prop, fdt_strerror(len));
WriteString(": ");
WriteString(fdt_strerror(len));
WriteLn();
break; break;
} }
for (int i = 0; i < depth; i++) WriteString(" "); for (int i = 0; i < depth; i++)
WriteString("prop('"); dprintf(" ");
WriteString(fdt_string(fdt, fdt32_to_cpu(property->nameoff))); dprintf("prop('%s'): ", fdt_string(fdt, fdt32_to_cpu(property->nameoff)));
WriteString("'): ");
if ( if (
strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "compatible") == 0 || strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "compatible") == 0 ||
strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "model") == 0 || strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "model") == 0 ||
@@ -161,15 +155,13 @@ static void DumpFdt(const void *fdt)
strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "clock-names") == 0 || strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "clock-names") == 0 ||
strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "clock-output-names") == 0 strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "clock-output-names") == 0
) { ) {
WriteStringList((const char*)property->data, fdt32_to_cpu(property->len)); write_string_list((const char*)property->data, fdt32_to_cpu(property->len));
} else if (strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "reg") == 0) { } else if (strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "reg") == 0) {
for (uint64_t *it = (uint64_t*)property->data; (uint8_t*)it - (uint8_t*)property->data < fdt32_to_cpu(property->len); it += 2) { for (uint64_t *it = (uint64_t*)property->data; (uint8_t*)it - (uint8_t*)property->data < fdt32_to_cpu(property->len); it += 2) {
if (it != (uint64_t*)property->data) WriteString(", "); if (it != (uint64_t*)property->data)
WriteString("(0x"); dprintf(", ");
WriteHex(fdt64_to_cpu(*it), 8); dprintf("(0x%08" B_PRIx64 ", 0x%08" B_PRIx64 ")",
WriteString(", 0x"); fdt64_to_cpu(*it), fdt64_to_cpu(*(it + 1)));
WriteHex(fdt64_to_cpu(*(it + 1)), 8);
WriteString(")");
} }
} else if ( } else if (
strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "phandle") == 0 || strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "phandle") == 0 ||
@@ -190,101 +182,80 @@ static void DumpFdt(const void *fdt)
strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "height") == 0 || strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "height") == 0 ||
strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "stride") == 0 strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "stride") == 0
) { ) {
WriteInt(fdt32_to_cpu(*(uint32_t*)property->data)); dprintf("%" B_PRId32, fdt32_to_cpu(*(uint32_t*)property->data));
} else if ( } else if (
strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "interrupts-extended") == 0 strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "interrupts-extended") == 0
) { ) {
for (uint32_t *it = (uint32_t*)property->data; (uint8_t*)it - (uint8_t*)property->data < fdt32_to_cpu(property->len); it += 2) { for (uint32_t *it = (uint32_t*)property->data; (uint8_t*)it - (uint8_t*)property->data < fdt32_to_cpu(property->len); it += 2) {
if (it != (uint32_t*)property->data) WriteString(", "); if (it != (uint32_t*)property->data)
WriteString("("); dprintf(", ");
WriteInt(fdt32_to_cpu(*it)); dprintf("(%" B_PRId32 ", %" B_PRId32 ")",
WriteString(", "); fdt32_to_cpu(*it), fdt32_to_cpu(*(it + 1)));
WriteInt(fdt32_to_cpu(*(it + 1)));
WriteString(")");
} }
} else if ( } else if (
strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "ranges") == 0 strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "ranges") == 0
) { ) {
WriteLn(); dprintf("\n");
depth++; depth++;
// kind // kind
// child address // child address
// parent address // parent address
// size // size
for (uint32_t *it = (uint32_t*)property->data; (uint8_t*)it - (uint8_t*)property->data < fdt32_to_cpu(property->len); it += 7) { for (uint32_t *it = (uint32_t*)property->data; (uint8_t*)it - (uint8_t*)property->data < fdt32_to_cpu(property->len); it += 7) {
for (int i = 0; i < depth; i++) WriteString(" "); for (int i = 0; i < depth; i++)
dprintf(" ");
uint32_t kind = fdt32_to_cpu(*(it + 0)); uint32_t kind = fdt32_to_cpu(*(it + 0));
switch (kind & 0x03000000) { switch (kind & 0x03000000) {
case 0x00000000: WriteString("CONFIG"); break; case 0x00000000: dprintf("CONFIG"); break;
case 0x01000000: WriteString("IOPORT"); break; case 0x01000000: dprintf("IOPORT"); break;
case 0x02000000: WriteString("MMIO"); break; case 0x02000000: dprintf("MMIO"); break;
case 0x03000000: WriteString("MMIO_64BIT"); break; case 0x03000000: dprintf("MMIO_64BIT"); break;
} }
WriteString(" (0x"); WriteHex(kind, 8); dprintf(" (0x%08" PRIx32 "), child: 0x%08" PRIx64 ", parent: 0x%08" PRIx64 ", len: 0x%08" PRIx64 "\n",
WriteString("), "); kind, fdt64_to_cpu(*(uint64_t*)(it + 1)), fdt64_to_cpu(*(uint64_t*)(it + 3)), fdt64_to_cpu(*(uint64_t*)(it + 5)));
WriteString("child: 0x"); WriteHex(fdt64_to_cpu(*(uint64_t*)(it + 1)), 8);
WriteString(", ");
WriteString("parent: 0x"); WriteHex(fdt64_to_cpu(*(uint64_t*)(it + 3)), 8);
WriteString(", ");
WriteString("len: 0x"); WriteHex(fdt64_to_cpu(*(uint64_t*)(it + 5)), 8);
WriteLn();
} }
for (int i = 0; i < depth; i++) WriteString(" "); for (int i = 0; i < depth; i++)
dprintf(" ");
depth--; depth--;
} else if (strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "bus-range") == 0) { } else if (strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "bus-range") == 0) {
uint32_t *it = (uint32_t*)property->data; uint32_t *it = (uint32_t*)property->data;
WriteInt(fdt32_to_cpu(*it)); dprintf("%" PRId32 ", %" PRId32, fdt32_to_cpu(*it), fdt32_to_cpu(*(it + 1)));
WriteString(", ");
WriteInt(fdt32_to_cpu(*(it + 1)));
} else if (strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "interrupt-map-mask") == 0) { } else if (strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "interrupt-map-mask") == 0) {
WriteLn(); dprintf("\n");
depth++; depth++;
for (uint32_t *it = (uint32_t*)property->data; (uint8_t*)it - (uint8_t*)property->data < fdt32_to_cpu(property->len); it++) { for (uint32_t *it = (uint32_t*)property->data; (uint8_t*)it - (uint8_t*)property->data < fdt32_to_cpu(property->len); it++) {
for (int i = 0; i < depth; i++) WriteString(" "); for (int i = 0; i < depth; i++)
WriteString("0x"); WriteHex(fdt32_to_cpu(*(uint32_t*)it), 8); dprintf(" ");
WriteLn(); dprintf("0x%08" PRIx32 "\n", fdt32_to_cpu(*(uint32_t*)it));
} }
for (int i = 0; i < depth; i++) WriteString(" "); for (int i = 0; i < depth; i++)
dprintf(" ");
depth--; depth--;
} else if (strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "interrupt-map") == 0) { } else if (strcmp(fdt_string(fdt, fdt32_to_cpu(property->nameoff)), "interrupt-map") == 0) {
WriteLn(); dprintf("\n");
depth++; depth++;
for (uint32_t *it = (uint32_t*)property->data; (uint8_t*)it - (uint8_t*)property->data < fdt32_to_cpu(property->len); it += 6) { for (uint32_t *it = (uint32_t*)property->data; (uint8_t*)it - (uint8_t*)property->data < fdt32_to_cpu(property->len); it += 6) {
for (int i = 0; i < depth; i++) WriteString(" "); for (int i = 0; i < depth; i++)
dprintf(" ");
// child unit address // child unit address
WriteString("0x"); WriteHex(fdt32_to_cpu(*(it + 0)), 8); dprintf("0x%08" PRIx32 ", 0x%08" PRIx32 ", 0x%08" PRIx32 ", 0x%08" PRIx32
WriteString(", "); ", bus: %" PRId32 ", dev: %" PRId32 ", fn: %" PRId32,
WriteString("0x"); WriteHex(fdt32_to_cpu(*(it + 1)), 8); fdt32_to_cpu(*(it + 0)), fdt32_to_cpu(*(it + 1)), fdt32_to_cpu(*(it + 2)), fdt32_to_cpu(*(it + 3)),
WriteString(", "); fdt32_to_cpu(*(it + 0)) / (1 << 16) % (1 << 8),
WriteString("0x"); WriteHex(fdt32_to_cpu(*(it + 2)), 8); fdt32_to_cpu(*(it + 0)) / (1 << 11) % (1 << 5),
WriteString(", "); fdt32_to_cpu(*(it + 0)) % (1 << 3));
WriteString("0x"); WriteHex(fdt32_to_cpu(*(it + 3)), 8); dprintf(", childIrq: %" PRId32 ", parentIrq: (%" PRId32 ", %" PRId32 ")\n",
fdt32_to_cpu(*(it + 3)), fdt32_to_cpu(*(it + 4)), fdt32_to_cpu(*(it + 5)));
WriteString(", bus: "); WriteInt(fdt32_to_cpu(*(it + 0)) / (1 << 16) % (1 << 8));
WriteString(", dev: "); WriteInt(fdt32_to_cpu(*(it + 0)) / (1 << 11) % (1 << 5));
WriteString(", fn: "); WriteInt(fdt32_to_cpu(*(it + 0)) % (1 << 3));
WriteString(", childIrq: ");
// child interrupt specifier
WriteInt(fdt32_to_cpu(*(it + 3)));
WriteString(", parentIrq: (");
// interrupt-parent
WriteInt(fdt32_to_cpu(*(it + 4)));
WriteString(", ");
WriteInt(fdt32_to_cpu(*(it + 5)));
WriteString(")");
WriteLn();
if (((it - (uint32_t*)property->data) / 6) % 4 == 3 && ((uint8_t*)(it + 6) - (uint8_t*)property->data < fdt32_to_cpu(property->len))) if (((it - (uint32_t*)property->data) / 6) % 4 == 3 && ((uint8_t*)(it + 6) - (uint8_t*)property->data < fdt32_to_cpu(property->len)))
WriteLn(); dprintf("\n");
} }
for (int i = 0; i < depth; i++) WriteString(" "); for (int i = 0; i < depth; i++)
dprintf(" ");
depth--; depth--;
} else { } else {
WriteString("?"); dprintf("?");
} }
WriteString(" (len "); dprintf(" (len %" PRId32 ")\n", fdt32_to_cpu(property->len));
WriteInt(fdt32_to_cpu(property->len));
WriteString(")"); WriteLn();
/* /*
dump_hex(property->data, fdt32_to_cpu(property->len), depth); dump_hex(property->data, fdt32_to_cpu(property->len), depth);
*/ */
@@ -292,7 +263,7 @@ static void DumpFdt(const void *fdt)
depth--; depth--;
} }
} }
#endif
bool bool
@@ -319,7 +290,7 @@ dtb_get_address_cells(const void* fdt, int node)
if (parent < 0) if (parent < 0)
return res; return res;
uint32 *prop = (uint32*)fdt_getprop(sDtbTable, parent, "#address-cells", NULL); uint32 *prop = (uint32*)fdt_getprop(fdt, parent, "#address-cells", NULL);
if (prop == NULL) if (prop == NULL)
return res; return res;
@@ -337,7 +308,7 @@ dtb_get_size_cells(const void* fdt, int node)
if (parent < 0) if (parent < 0)
return res; return res;
uint32 *prop = (uint32*)fdt_getprop(sDtbTable, parent, "#size-cells", NULL); uint32 *prop = (uint32*)fdt_getprop(fdt, parent, "#size-cells", NULL);
if (prop == NULL) if (prop == NULL)
return res; return res;
@@ -602,8 +573,9 @@ dtb_init()
INFO("Valid FDT from UEFI table %d, size: %" B_PRIu32 "\n", i, sDtbSize); INFO("Valid FDT from UEFI table %d, size: %" B_PRIu32 "\n", i, sDtbSize);
if (false) #ifdef TRACE_DUMP_FDT
DumpFdt(sDtbTable); dump_fdt(sDtbTable);
#endif
dtb_handle_chosen_node(sDtbTable); dtb_handle_chosen_node(sDtbTable);