Magisk/native/jni/magiskboot/dtb.cpp

114 lines
2.9 KiB
C++
Raw Normal View History

2017-09-14 20:52:27 +02:00
#include <unistd.h>
#include <sys/mman.h>
2018-10-25 03:08:06 +02:00
extern "C" {
#include <libfdt.h>
}
2017-09-14 20:52:27 +02:00
#include "magiskboot.h"
#include "utils.h"
2017-11-09 20:45:06 +01:00
static void print_props(const void *fdt, int node, int depth) {
int prop;
fdt_for_each_property_offset(prop, fdt, node) {
for (int i = 0; i < depth; ++i) printf(" ");
printf(" ");
int size;
const char *name;
2018-10-25 03:08:06 +02:00
const char *value = (char *) fdt_getprop_by_offset(fdt, prop, &name, &size);
2017-11-09 20:45:06 +01:00
printf("[%s]: [%s]\n", name, value);
}
}
2017-10-07 16:08:10 +02:00
static void print_subnode(const void *fdt, int parent, int depth) {
int node;
fdt_for_each_subnode(node, fdt, parent) {
2017-11-09 20:45:06 +01:00
for (int i = 0; i < depth; ++i) printf(" ");
printf("#%d: %s\n", node, fdt_get_name(fdt, node, NULL));
print_props(fdt, node, depth);
2017-10-07 16:08:10 +02:00
print_subnode(fdt, node, depth + 1);
}
}
2017-09-14 20:52:27 +02:00
static int find_fstab(const void *fdt, int parent) {
int node, fstab;
fdt_for_each_subnode(node, fdt, parent) {
if (strcmp(fdt_get_name(fdt, node, NULL), "fstab") == 0)
return node;
fstab = find_fstab(fdt, node);
if (fstab != -1)
return fstab;
}
return -1;
}
2017-11-10 18:30:33 +01:00
static void dtb_dump(const char *file) {
2017-10-07 16:08:10 +02:00
size_t size ;
2018-10-25 03:08:06 +02:00
uint8_t *dtb, *fdt;
2017-10-07 16:08:10 +02:00
fprintf(stderr, "Loading dtbs from [%s]\n", file);
2018-10-25 03:08:06 +02:00
mmap_ro(file, (void **) &dtb, &size);
2017-10-07 16:08:10 +02:00
// Loop through all the dtbs
int dtb_num = 0;
for (int i = 0; i < size; ++i) {
if (memcmp(dtb + i, DTB_MAGIC, 4) == 0) {
fdt = dtb + i;
fprintf(stderr, "Dumping dtb.%04d\n", dtb_num++);
2017-10-07 16:08:10 +02:00
print_subnode(fdt, 0, 0);
}
}
fprintf(stderr, "\n");
munmap(dtb, size);
exit(0);
}
2017-11-10 18:30:33 +01:00
static void dtb_patch(const char *file, int patch) {
2017-09-14 20:52:27 +02:00
size_t size ;
2018-10-25 03:08:06 +02:00
uint8_t *dtb, *fdt;
fprintf(stderr, "Loading dtbs from [%s]\n", file);
2017-11-10 18:30:33 +01:00
if (patch)
2018-10-25 03:08:06 +02:00
mmap_rw(file, (void **) &dtb, &size);
2017-11-10 18:30:33 +01:00
else
2018-10-25 03:08:06 +02:00
mmap_ro(file, (void **) &dtb, &size);
2017-09-14 20:52:27 +02:00
// Loop through all the dtbs
2017-11-10 18:30:33 +01:00
int dtb_num = 0, found = 0;
2017-09-14 20:52:27 +02:00
for (int i = 0; i < size; ++i) {
if (memcmp(dtb + i, DTB_MAGIC, 4) == 0) {
fdt = dtb + i;
int fstab = find_fstab(fdt, 0);
if (fstab > 0) {
fprintf(stderr, "Found fstab in dtb.%04d\n", dtb_num++);
int block;
fdt_for_each_subnode(block, fdt, fstab) {
fprintf(stderr, "Found block [%s] in fstab\n", fdt_get_name(fdt, block, NULL));
2017-12-06 18:30:48 +01:00
uint32_t value_size;
2018-07-13 16:14:32 +02:00
void *value = (void *) fdt_getprop(fdt, block, "fsmgr_flags", (int *)&value_size);
2017-12-31 12:30:56 +01:00
if (patch) {
void *dup = xmalloc(value_size);
memcpy(dup, value, value_size);
memset(value, 0, value_size);
found |= patch_verity(&dup, &value_size, 1);
memcpy(value, dup, value_size);
free(dup);
} else {
found |= patch_verity(&value, &value_size, 0);
}
2017-09-14 20:52:27 +02:00
}
}
}
}
munmap(dtb, size);
2017-11-10 18:30:33 +01:00
exit(!found);
}
int dtb_commands(const char *cmd, int argc, char *argv[]) {
if (argc == 0) return 1;
if (strcmp(cmd, "dump") == 0)
dtb_dump(argv[0]);
else if (strcmp(cmd, "patch") == 0)
dtb_patch(argv[0], 1);
else if (strcmp(cmd, "test") == 0)
dtb_patch(argv[0], 0);
return 0;
2017-09-14 20:52:27 +02:00
}