2017-09-14 18:52:27 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
2019-02-25 04:09:34 +00:00
|
|
|
|
2018-10-25 01:08:06 +00:00
|
|
|
extern "C" {
|
|
|
|
#include <libfdt.h>
|
|
|
|
}
|
2019-02-10 08:57:51 +00:00
|
|
|
#include <utils.h>
|
2019-09-20 07:05:14 +00:00
|
|
|
#include <bitset>
|
2017-09-14 18:52:27 +00:00
|
|
|
|
|
|
|
#include "magiskboot.h"
|
2019-02-25 04:09:34 +00:00
|
|
|
#include "format.h"
|
2017-09-14 18:52:27 +00:00
|
|
|
|
2019-09-20 07:05:14 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
constexpr int MAX_DEPTH = 32;
|
|
|
|
static bitset<MAX_DEPTH> depth_set;
|
|
|
|
|
|
|
|
static void pretty_node(int depth) {
|
|
|
|
if (depth == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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 pretty_prop(int depth) {
|
|
|
|
for (int i = 0; i < depth; ++i) {
|
|
|
|
if (depth_set[i])
|
|
|
|
printf("│ ");
|
|
|
|
else
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
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;
|
2017-11-09 19:45:06 +00:00
|
|
|
int prop;
|
|
|
|
fdt_for_each_property_offset(prop, fdt, node) {
|
2019-09-20 07:05:14 +00:00
|
|
|
pretty_prop(depth);
|
2017-11-09 19:45:06 +00:00
|
|
|
int size;
|
|
|
|
const char *name;
|
2019-09-20 07:05:14 +00:00
|
|
|
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);
|
|
|
|
}
|
2017-11-09 19:45:06 +00:00
|
|
|
}
|
|
|
|
|
2019-09-20 07:05:14 +00:00
|
|
|
// 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);
|
2017-10-07 14:08:10 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-14 18:52:27 +00:00
|
|
|
|
|
|
|
static int find_fstab(const void *fdt, int parent) {
|
|
|
|
int node, fstab;
|
|
|
|
fdt_for_each_subnode(node, fdt, parent) {
|
2019-09-20 07:05:14 +00:00
|
|
|
if (strcmp(fdt_get_name(fdt, node, nullptr), "fstab") == 0)
|
2017-09-14 18:52:27 +00:00
|
|
|
return node;
|
|
|
|
fstab = find_fstab(fdt, node);
|
|
|
|
if (fstab != -1)
|
|
|
|
return fstab;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-11-10 17:30:33 +00:00
|
|
|
static void dtb_dump(const char *file) {
|
2017-10-07 14:08:10 +00:00
|
|
|
size_t size ;
|
2018-10-25 01:08:06 +00:00
|
|
|
uint8_t *dtb, *fdt;
|
2017-10-07 14:08:10 +00:00
|
|
|
fprintf(stderr, "Loading dtbs from [%s]\n", file);
|
2019-02-25 04:09:34 +00:00
|
|
|
mmap_ro(file, dtb, size);
|
2017-10-07 14:08:10 +00: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;
|
2017-12-24 19:01:33 +00:00
|
|
|
fprintf(stderr, "Dumping dtb.%04d\n", dtb_num++);
|
2019-09-20 07:05:14 +00:00
|
|
|
print_node(fdt);
|
2017-10-07 14:08:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
munmap(dtb, size);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2017-11-10 17:30:33 +00:00
|
|
|
static void dtb_patch(const char *file, int patch) {
|
2017-09-14 18:52:27 +00:00
|
|
|
size_t size ;
|
2018-10-25 01:08:06 +00:00
|
|
|
uint8_t *dtb, *fdt;
|
2017-12-24 19:01:33 +00:00
|
|
|
fprintf(stderr, "Loading dtbs from [%s]\n", file);
|
2017-11-10 17:30:33 +00:00
|
|
|
if (patch)
|
2019-02-25 04:09:34 +00:00
|
|
|
mmap_rw(file, dtb, size);
|
2017-11-10 17:30:33 +00:00
|
|
|
else
|
2019-02-25 04:09:34 +00:00
|
|
|
mmap_ro(file, dtb, size);
|
2017-09-14 18:52:27 +00:00
|
|
|
// Loop through all the dtbs
|
2019-02-25 04:09:34 +00:00
|
|
|
int dtb_num = 0;
|
|
|
|
bool found = false;
|
2017-09-14 18:52:27 +00: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) {
|
2019-09-20 07:05:14 +00:00
|
|
|
fprintf(stderr, "Found block [%s] in fstab\n", fdt_get_name(fdt, block, nullptr));
|
2017-12-06 17:30:48 +00:00
|
|
|
uint32_t value_size;
|
2018-07-13 14:14:32 +00:00
|
|
|
void *value = (void *) fdt_getprop(fdt, block, "fsmgr_flags", (int *)&value_size);
|
2017-12-31 11:30:56 +00:00
|
|
|
if (patch) {
|
|
|
|
void *dup = xmalloc(value_size);
|
|
|
|
memcpy(dup, value, value_size);
|
|
|
|
memset(value, 0, value_size);
|
2019-02-25 04:09:34 +00:00
|
|
|
found |= patch_verity(&dup, &value_size);
|
2017-12-31 11:30:56 +00:00
|
|
|
memcpy(value, dup, value_size);
|
|
|
|
free(dup);
|
|
|
|
} else {
|
2019-02-25 04:09:34 +00:00
|
|
|
found |= patch_verity(&value, &value_size, false);
|
2017-12-31 11:30:56 +00:00
|
|
|
}
|
2017-09-14 18:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
munmap(dtb, size);
|
2017-11-10 17:30:33 +00: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 18:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|