Revise dtb commands CLI

This commit is contained in:
topjohnwu 2019-09-20 03:53:58 -04:00
parent d81ccde569
commit 120668c7bc
3 changed files with 121 additions and 103 deletions

View File

@ -94,7 +94,7 @@ static void print_node(const void *fdt, int node = 0, int depth = 0) {
} }
} }
static int find_fstab(const void *fdt, int parent) { static int find_fstab(const void *fdt, int parent = 0) {
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, nullptr), "fstab") == 0) if (strcmp(fdt_get_name(fdt, node, nullptr), "fstab") == 0)
@ -106,7 +106,7 @@ static int find_fstab(const void *fdt, int parent) {
return -1; return -1;
} }
static void dtb_dump(const char *file) { static void dtb_print(const char *file, bool fstab) {
size_t size ; size_t size ;
uint8_t *dtb, *fdt; uint8_t *dtb, *fdt;
fprintf(stderr, "Loading dtbs from [%s]\n", file); fprintf(stderr, "Loading dtbs from [%s]\n", file);
@ -116,8 +116,17 @@ static void dtb_dump(const char *file) {
for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) {
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++); if (fstab) {
print_node(fdt); int node = find_fstab(fdt);
if (node >= 0) {
fprintf(stderr, "Found fstab in dtb.%04d\n", dtb_num);
print_node(fdt, node);
}
} else {
fprintf(stderr, "Printing dtb.%04d\n", dtb_num);
print_node(fdt);
}
dtb_num++;
} }
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
@ -165,15 +174,20 @@ static void dtb_patch(const char *file, int patch) {
exit(!found); exit(!found);
} }
int dtb_commands(const char *cmd, int argc, char *argv[]) { int dtb_commands(int argc, char *argv[]) {
if (argc == 0) return 1; char *dtb = argv[0];
if (strcmp(cmd, "dump") == 0) ++argv;
dtb_dump(argv[0]); --argc;
else if (strcmp(cmd, "patch") == 0)
dtb_patch(argv[0], 1); if (argv[0] == "print"sv) {
else if (strcmp(cmd, "test") == 0) dtb_print(dtb, argc > 1 && argv[1] == "-f"sv);
dtb_patch(argv[0], 0); } else if (argv[0] == "patch"sv) {
dtb_patch(dtb, 1);
} else if (argv[0] == "test"sv) {
dtb_patch(dtb, 0);
} else {
return 1;
}
return 0; return 0;
} }

View File

@ -17,7 +17,7 @@ int unpack(const char *image, bool hdr = false);
void repack(const char* orig_image, const char* out_image, bool force_nocomp = false); void repack(const char* orig_image, const char* out_image, bool force_nocomp = false);
int hexpatch(const char *image, const char *from, const char *to); int hexpatch(const char *image, const char *from, const char *to);
int cpio_commands(int argc, char *argv[]); int cpio_commands(int argc, char *argv[]);
int dtb_commands(const char *cmd, int argc, char *argv[]); int dtb_commands(int argc, char *argv[]);
// Pattern // Pattern
bool patch_verity(void **buf, uint32_t *size, bool patch = true); bool patch_verity(void **buf, uint32_t *size, bool patch = true);

View File

@ -16,94 +16,100 @@ using namespace std;
static void usage(char *arg0) { static void usage(char *arg0) {
fprintf(stderr, fprintf(stderr,
FULL_VER(MagiskBoot) " - Boot Image Modification Tool\n" FULL_VER(MagiskBoot) R"EOF( - Boot Image Modification Tool
"Usage: %s <action> [args...]\n"
"\n" Usage: %s <action> [args...]
"Supported actions:\n"
" unpack [-h] <bootimg>\n" Supported actions:
" Unpack <bootimg> to, if available, kernel, kernel_dtb, ramdisk.cpio,\n" unpack [-h] <bootimg>
" second, dtb, extra, and recovery_dtbo into current directory.\n" Unpack <bootimg> to, if available, kernel, kernel_dtb, ramdisk.cpio,
" If '-h' is provided, it will dump header info to 'header',\n" second, dtb, extra, and recovery_dtbo into current directory.
" which will be parsed when repacking.\n" If '-h' is provided, it will dump header info to 'header',
" Return values:\n" which will be parsed when repacking.
" 0:valid 1:error 2:chromeos\n" Return values:
"\n" 0:valid 1:error 2:chromeos
" repack [-n] <origbootimg> [outbootimg]\n"
" Repack boot image components from current directory\n" repack [-n] <origbootimg> [outbootimg]
" to [outbootimg], or new-boot.img if not specified.\n" Repack boot image components from current directory
" If '-n' is provided, it will not attempt to recompress ramdisk.cpio,\n" to [outbootimg], or new-boot.img if not specified.
" otherwise it will compress ramdisk.cpio and kernel with the same method\n" If '-n' is provided, it will not attempt to recompress ramdisk.cpio,
" in <origbootimg> if the file provided is not already compressed.\n" otherwise it will compress ramdisk.cpio and kernel with the same method
"\n" in <origbootimg> if the file provided is not already compressed.
" hexpatch <file> <hexpattern1> <hexpattern2>\n"
" Search <hexpattern1> in <file>, and replace with <hexpattern2>\n" hexpatch <file> <hexpattern1> <hexpattern2>
"\n" Search <hexpattern1> in <file>, and replace with <hexpattern2>
" cpio <incpio> [commands...]\n"
" Do cpio commands to <incpio> (modifications are done directly)\n" cpio <incpio> [commands...]
" Each command is a single argument, use quotes if necessary\n" Do cpio commands to <incpio> (modifications are done directly)
" Supported commands:\n" Each command is a single argument, add quotes for each command
" exists ENTRY\n" Supported commands:
" Return 0 if ENTRY exists, else return 1\n" exists ENTRY
" rm [-r] ENTRY\n" Return 0 if ENTRY exists, else return 1
" Remove ENTRY, specify [-r] to remove recursively\n" rm [-r] ENTRY
" mkdir MODE ENTRY\n" Remove ENTRY, specify [-r] to remove recursively
" Create directory ENTRY in permissions MODE\n" mkdir MODE ENTRY
" ln TARGET ENTRY\n" Create directory ENTRY in permissions MODE
" Create a symlink to TARGET with the name ENTRY\n" ln TARGET ENTRY
" mv SOURCE DEST\n" Create a symlink to TARGET with the name ENTRY
" Move SOURCE to DEST\n" mv SOURCE DEST
" add MODE ENTRY INFILE\n" Move SOURCE to DEST
" Add INFILE as ENTRY in permissions MODE; replaces ENTRY if exists\n" add MODE ENTRY INFILE
" extract [ENTRY OUT]\n" Add INFILE as ENTRY in permissions MODE; replaces ENTRY if exists
" Extract ENTRY to OUT, or extract all entries to current directory\n" extract [ENTRY OUT]
" test\n" Extract ENTRY to OUT, or extract all entries to current directory
" Test the current cpio's patch status\n" test
" Return values:\n" Test the current cpio's patch status
" 0:stock 1:Magisk 2:unsupported (phh, SuperSU, Xposed)\n" Return values:
" patch KEEPVERITY KEEPFORCEENCRYPT\n" 0:stock 1:Magisk 2:unsupported (phh, SuperSU, Xposed)
" Ramdisk patches. KEEP**** are boolean values\n" patch KEEPVERITY KEEPFORCEENCRYPT
" backup ORIG\n" Ramdisk patches. KEEP**** are boolean values
" Create ramdisk backups from ORIG\n" backup ORIG
" restore\n" Create ramdisk backups from ORIG
" Restore ramdisk from ramdisk backup stored within incpio\n" restore
" sha1\n" Restore ramdisk from ramdisk backup stored within incpio
" Print stock boot SHA1 if previously backed up in ramdisk\n" sha1
"\n" Print stock boot SHA1 if previously backed up in ramdisk
" dtb-<cmd> <dtb>\n"
" Do dtb related cmds to <dtb> (modifications are done directly)\n" dtb <dtb> <command> [args...]
" Supported commands:\n" Do commands to <dtb> (modifications are done directly)
" dump\n" Supported commands:
" Dump all contents from dtb for debugging\n" print [-f]
" test\n" Print all contents from dtb for debugging
" Check if fstab has verity/avb flags\n" Specify [-f] to only print fstab nodes
" Return values:\n" test
" 0:flag exists 1:no flags\n" Check if fstab has verity/avb flags
" patch\n" Return values:
" Search for fstab and remove verity/avb\n" 0:flag exists 1:no flags
"\n" patch
" compress[=method] <infile> [outfile]\n" Search for fstab and remove verity/avb
" Compress <infile> with [method] (default: gzip), optionally to [outfile]\n"
" <infile>/[outfile] can be '-' to be STDIN/STDOUT\n" compress[=method] <infile> [outfile]
" Supported methods: " Compress <infile> with [method] (default: gzip), optionally to [outfile]
, arg0); <infile>/[outfile] can be '-' to be STDIN/STDOUT
Supported methods: )EOF", arg0);
for (auto &it : name2fmt) for (auto &it : name2fmt)
fprintf(stderr, "%s ", it.first.data()); fprintf(stderr, "%s ", it.first.data());
fprintf(stderr,
"\n\n" fprintf(stderr, R"EOF(
" decompress <infile> [outfile]\n"
" Detect method and decompress <infile>, optionally to [outfile]\n" decompress <infile> [outfile]
" <infile>/[outfile] can be '-' to be STDIN/STDOUT\n" Detect method and decompress <infile>, optionally to [outfile]
" Supported methods: "); <infile>/[outfile] can be '-' to be STDIN/STDOUT
Supported methods: )EOF");
for (auto &it : name2fmt) for (auto &it : name2fmt)
fprintf(stderr, "%s ", it.first.data()); fprintf(stderr, "%s ", it.first.data());
fprintf(stderr,
"\n\n" fprintf(stderr, R"EOF(
" sha1 <file>\n"
" Print the SHA1 checksum for <file>\n" sha1 <file>
"\n" Print the SHA1 checksum for <file>
" cleanup\n"
" Cleanup the current working directory\n" cleanup
"\n"); Cleanup the current working directory
)EOF");
exit(1); exit(1);
} }
@ -165,10 +171,8 @@ int main(int argc, char *argv[]) {
} else if (argc > 2 && action == "cpio"sv) { } else if (argc > 2 && action == "cpio"sv) {
if (cpio_commands(argc - 2, argv + 2)) if (cpio_commands(argc - 2, argv + 2))
usage(argv[0]); usage(argv[0]);
} else if (argc > 2 && str_starts(action, "dtb")) { } else if (argc > 2 && action == "dtb") {
if (action[3] != '-') if (dtb_commands(argc - 2, argv + 2))
usage(argv[0]);
if (dtb_commands(&action[4], argc - 2, argv + 2))
usage(argv[0]); usage(argv[0]);
} else { } else {
usage(argv[0]); usage(argv[0]);