Use mmap_data more widely

This commit is contained in:
topjohnwu
2021-11-30 01:50:55 -08:00
parent 2d82ad93dd
commit 1443a5b175
19 changed files with 168 additions and 276 deletions

View File

@@ -94,32 +94,29 @@ static int find_fstab(const void *fdt, int node = 0) {
}
static void dtb_print(const char *file, bool fstab) {
size_t size;
uint8_t *dtb;
fprintf(stderr, "Loading dtbs from [%s]\n", file);
mmap_ro(file, dtb, size);
auto m = mmap_data(file);
// Loop through all the dtbs
int dtb_num = 0;
uint8_t * const end = dtb + size;
for (uint8_t *fdt = dtb; fdt < end;) {
uint8_t * const end = m.buf + m.sz;
for (uint8_t *fdt = m.buf; fdt < end;) {
fdt = static_cast<uint8_t*>(memmem(fdt, end - fdt, DTB_MAGIC, sizeof(fdt32_t)));
if (fdt == nullptr)
break;
if (fstab) {
int node = find_fstab(fdt);
if (node >= 0) {
fprintf(stderr, "Found fstab in dtb.%04d\n", dtb_num);
fprintf(stderr, "Found fstab in buf.%04d\n", dtb_num);
print_node(fdt, node);
}
} else {
fprintf(stderr, "Printing dtb.%04d\n", dtb_num);
fprintf(stderr, "Printing buf.%04d\n", dtb_num);
print_node(fdt);
}
++dtb_num;
fdt += fdt_totalsize(fdt);
}
fprintf(stderr, "\n");
munmap(dtb, size);
}
[[maybe_unused]]
@@ -128,14 +125,12 @@ static bool dtb_patch_rebuild(uint8_t *dtb, size_t dtb_sz, const char *file);
static bool dtb_patch(const char *file) {
bool keep_verity = check_env("KEEPVERITY");
size_t size;
uint8_t *dtb;
fprintf(stderr, "Loading dtbs from [%s]\n", file);
mmap_rw(file, dtb, size);
auto m = mmap_data(file, true);
bool patched = false;
uint8_t * const end = dtb + size;
for (uint8_t *fdt = dtb; fdt < end;) {
uint8_t * const end = m.buf + m.sz;
for (uint8_t *fdt = m.buf; fdt < end;) {
fdt = static_cast<uint8_t*>(memmem(fdt, end - fdt, DTB_MAGIC, sizeof(fdt32_t)));
if (fdt == nullptr)
break;
@@ -165,8 +160,6 @@ static bool dtb_patch(const char *file) {
}
fdt += fdt_totalsize(fdt);
}
munmap(dtb, size);
return patched;
}