Commit 02dc1172 authored by topjohnwu's avatar topjohnwu

Revert DTB patches to in-place binary patches

Since we no longer need to add new properties in the device tree, and
all the patches we do removes strings, we can just directly patch
the flat device tree in-place, ignoring basically all the higher level
DTB structure and format to accomplish 100% compatibility.
parent dbf8c412
This diff is collapsed.
...@@ -19,6 +19,6 @@ int hexpatch(const char *image, const char *from, const char *to); ...@@ -19,6 +19,6 @@ 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(int argc, char *argv[]); int dtb_commands(int argc, char *argv[]);
char *patch_verity(const void *buf, uint32_t &size, bool inplace = false); uint32_t patch_verity(void *buf, uint32_t size);
void patch_encryption(void *buf, uint32_t &size); uint32_t patch_encryption(void *buf, uint32_t size);
bool check_env(const char *name); bool check_env(const char *name);
...@@ -42,7 +42,7 @@ Supported actions: ...@@ -42,7 +42,7 @@ Supported actions:
Search <hexpattern1> in <file>, and replace with <hexpattern2> Search <hexpattern1> in <file>, and replace with <hexpattern2>
cpio <incpio> [commands...] cpio <incpio> [commands...]
Do cpio commands to <incpio> (modifications are done directly) Do cpio commands to <incpio> (modifications are done in-place)
Each command is a single argument, add quotes for each command Each command is a single argument, add quotes for each command
Supported commands: Supported commands:
exists ENTRY exists ENTRY
...@@ -64,8 +64,8 @@ Supported actions: ...@@ -64,8 +64,8 @@ Supported actions:
Return values: Return values:
0:stock 1:Magisk 2:unsupported (phh, SuperSU, Xposed) 0:stock 1:Magisk 2:unsupported (phh, SuperSU, Xposed)
patch patch
Apply ramdisk patches. Configure settings with env variables: Apply ramdisk patches
KEEPVERITY KEEPFORCEENCRYPT Configure with env variables: KEEPVERITY KEEPFORCEENCRYPT
backup ORIG backup ORIG
Create ramdisk backups from ORIG Create ramdisk backups from ORIG
restore restore
...@@ -79,10 +79,10 @@ Supported actions: ...@@ -79,10 +79,10 @@ Supported actions:
print [-f] print [-f]
Print all contents of dtb for debugging Print all contents of dtb for debugging
Specify [-f] to only print fstab nodes Specify [-f] to only print fstab nodes
patch [OUT] patch
Search for fstab and remove verity/avb Search for fstab and remove verity/avb
If [OUT] is not specified, it will directly output to <input> Modifications are done directly to the file in-place
Configure with env variables: KEEPVERITY TWOSTAGEINIT Configure with env variables: KEEPVERITY
split <input> split <input>
Split image.*-dtb into kernel + kernel_dtb Split image.*-dtb into kernel + kernel_dtb
...@@ -186,7 +186,7 @@ int main(int argc, char *argv[]) { ...@@ -186,7 +186,7 @@ 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 && action == "dtb") { } else if (argc > 3 && action == "dtb") {
if (dtb_commands(argc - 2, argv + 2)) if (dtb_commands(argc - 2, argv + 2))
usage(argv[0]); usage(argv[0]);
} else { } else {
......
...@@ -37,42 +37,27 @@ static int check_encryption_pattern(const char *s) { ...@@ -37,42 +37,27 @@ static int check_encryption_pattern(const char *s) {
else return -1; else return -1;
} }
char *patch_verity(const void *buf, uint32_t &size, bool inplace) { static uint32_t remove_pattern(void *buf, uint32_t size, int(*pattern_skip)(const char *)) {
auto src = static_cast<const char *>(buf); auto src = static_cast<char *>(buf);
auto dest = (char *)(inplace ? buf : xmalloc(size)); int orig_sz = size;
int src_size = size;
bool found = false;
int write = 0; int write = 0;
for (int read = 0; read < src_size;) { for (int read = 0; read < orig_sz;) {
if (int skip; (skip = check_verity_pattern(src + read)) > 0) { if (int skip = pattern_skip(src + read); skip > 0) {
fprintf(stderr, "Found pattern [%.*s]\n", skip, src + read); fprintf(stderr, "Remove pattern [%.*s]\n", skip, src + read);
size -= skip; size -= skip;
read += skip; read += skip;
found = true;
} else { } else {
dest[write++] = src[read++]; src[write++] = src[read++];
} }
} }
dest[write] = '\0'; memset(src + write, 0, orig_sz - write);
if (!found) { return size;
if (!inplace)
free(dest);
return nullptr;
}
return dest;
} }
void patch_encryption(void *buf, uint32_t &size) { uint32_t patch_verity(void *buf, uint32_t size) {
auto src = static_cast<char *>(buf); return remove_pattern(buf, size, check_verity_pattern);
int src_size = size; }
int write = 0;
for (int read = 0; read < src_size; ++read, ++write) { uint32_t patch_encryption(void *buf, uint32_t size) {
if (int skip; (skip = check_encryption_pattern(src + read)) > 0) { return remove_pattern(buf, size, check_encryption_pattern);
fprintf(stderr, "Found pattern [%.*s]\n", skip, src + read);
size -= skip;
read += skip;
}
src[write] = src[read];
}
src[write] = '\0';
} }
...@@ -55,7 +55,7 @@ void magisk_cpio::patch() { ...@@ -55,7 +55,7 @@ void magisk_cpio::patch() {
if (!keepverity) { if (!keepverity) {
if (fstab) { if (fstab) {
fprintf(stderr, "Found fstab file [%s]\n", cur->first.data()); fprintf(stderr, "Found fstab file [%s]\n", cur->first.data());
patch_verity(cur->second->data, cur->second->filesize, true); cur->second->filesize = patch_verity(cur->second->data, cur->second->filesize);
} else if (cur->first == "verity_key") { } else if (cur->first == "verity_key") {
rm(cur); rm(cur);
continue; continue;
...@@ -63,7 +63,7 @@ void magisk_cpio::patch() { ...@@ -63,7 +63,7 @@ void magisk_cpio::patch() {
} }
if (!keepforceencrypt) { if (!keepforceencrypt) {
if (fstab) { if (fstab) {
patch_encryption(cur->second->data, cur->second->filesize); cur->second->filesize = patch_encryption(cur->second->data, cur->second->filesize);
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment