Pretty print dtb content

This commit is contained in:
topjohnwu 2019-09-20 03:05:14 -04:00
parent e8581b4adb
commit d81ccde569

View File

@ -5,36 +5,99 @@ extern "C" {
#include <libfdt.h> #include <libfdt.h>
} }
#include <utils.h> #include <utils.h>
#include <bitset>
#include "magiskboot.h" #include "magiskboot.h"
#include "format.h" #include "format.h"
static void print_props(const void *fdt, int node, int depth) { using namespace std;
int prop;
fdt_for_each_property_offset(prop, fdt, node) { constexpr int MAX_DEPTH = 32;
for (int i = 0; i < depth; ++i) printf(" "); static bitset<MAX_DEPTH> depth_set;
printf(" ");
int size; static void pretty_node(int depth) {
const char *name; if (depth == 0)
const char *value = (char *) fdt_getprop_by_offset(fdt, prop, &name, &size); return;
printf("[%s]: [%s]\n", name, value);
for (int i = 0; i < depth - 1; ++i) {
if (depth_set[i])
printf("");
else
printf(" ");
} }
if (depth_set[depth - 1])
printf("├── ");
else
printf("└── ");
} }
static void print_subnode(const void *fdt, int parent, int depth) { static void pretty_prop(int depth) {
int node; for (int i = 0; i < depth; ++i) {
fdt_for_each_subnode(node, fdt, parent) { if (depth_set[i])
for (int i = 0; i < depth; ++i) printf(" "); printf("");
printf("#%d: %s\n", node, fdt_get_name(fdt, node, NULL)); else
print_props(fdt, node, depth); printf(" ");
print_subnode(fdt, node, depth + 1); }
if (depth_set[depth])
printf("");
else
printf(" ");
}
static void print_node(const void *fdt, int node = 0, int depth = 0) {
// Print node itself
pretty_node(depth);
printf("#%d: %s\n", node, fdt_get_name(fdt, node, nullptr));
// Print properties
depth_set[depth] = fdt_first_subnode(fdt, node) >= 0;
int prop;
fdt_for_each_property_offset(prop, fdt, node) {
pretty_prop(depth);
int size;
const char *name;
auto value = static_cast<const char *>(fdt_getprop_by_offset(fdt, prop, &name, &size));
bool is_str = !(size > 1 && value[0] == 0);
if (is_str) {
// Scan through value to see if printable
for (int i = 0; i < size; ++i) {
char c = value[i];
if (i == size - 1) {
// Make sure null terminate
is_str = c == '\0';
} else if ((c > 0 && c < 32) || c >= 127) {
is_str = false;
break;
}
}
}
if (is_str) {
printf("[%s]: [%s]\n", name, value);
} else {
printf("[%s]: <bytes>(%d)\n", name, size);
}
}
// Recursive
if (depth_set[depth]) {
int child;
int prev = -1;
fdt_for_each_subnode(child, fdt, node) {
if (prev >= 0)
print_node(fdt, prev, depth + 1);
prev = child;
}
depth_set[depth] = false;
print_node(fdt, prev, depth + 1);
} }
} }
static int find_fstab(const void *fdt, int parent) { static int find_fstab(const void *fdt, int parent) {
int node, fstab; int node, fstab;
fdt_for_each_subnode(node, fdt, parent) { fdt_for_each_subnode(node, fdt, parent) {
if (strcmp(fdt_get_name(fdt, node, NULL), "fstab") == 0) if (strcmp(fdt_get_name(fdt, node, nullptr), "fstab") == 0)
return node; return node;
fstab = find_fstab(fdt, node); fstab = find_fstab(fdt, node);
if (fstab != -1) if (fstab != -1)
@ -54,7 +117,7 @@ static void dtb_dump(const char *file) {
if (memcmp(dtb + i, DTB_MAGIC, 4) == 0) { if (memcmp(dtb + i, DTB_MAGIC, 4) == 0) {
fdt = dtb + i; fdt = dtb + i;
fprintf(stderr, "Dumping dtb.%04d\n", dtb_num++); fprintf(stderr, "Dumping dtb.%04d\n", dtb_num++);
print_subnode(fdt, 0, 0); print_node(fdt);
} }
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
@ -81,7 +144,7 @@ static void dtb_patch(const char *file, int patch) {
fprintf(stderr, "Found fstab in dtb.%04d\n", dtb_num++); fprintf(stderr, "Found fstab in dtb.%04d\n", dtb_num++);
int block; int block;
fdt_for_each_subnode(block, fdt, fstab) { fdt_for_each_subnode(block, fdt, fstab) {
fprintf(stderr, "Found block [%s] in fstab\n", fdt_get_name(fdt, block, NULL)); fprintf(stderr, "Found block [%s] in fstab\n", fdt_get_name(fdt, block, nullptr));
uint32_t value_size; uint32_t value_size;
void *value = (void *) fdt_getprop(fdt, block, "fsmgr_flags", (int *)&value_size); void *value = (void *) fdt_getprop(fdt, block, "fsmgr_flags", (int *)&value_size);
if (patch) { if (patch) {