Migrate PREINITDEVICE in recovery mode

Close #6917
This commit is contained in:
topjohnwu 2023-06-02 16:49:04 -07:00
parent 7eebe62bb6
commit feb44f875e
7 changed files with 23 additions and 44 deletions

View File

@ -66,7 +66,7 @@ struct heap_data : public byte_data {
MOVE_ONLY(heap_data) MOVE_ONLY(heap_data)
explicit heap_data(size_t sz) { this->sz = sz; buf = new uint8_t[sz]; } explicit heap_data(size_t sz) { this->sz = sz; buf = new uint8_t[sz]; }
~heap_data() { free(buf); } ~heap_data() { delete[] buf; }
}; };
struct mmap_data : public byte_data { struct mmap_data : public byte_data {

View File

@ -76,7 +76,7 @@ void cpio::rm(const char *name, bool r) {
} }
} }
void cpio::extract_entry(const entry_map::value_type &e, const char *file) { static void extract_entry(const cpio::entry_map::value_type &e, const char *file) {
fprintf(stderr, "Extract [%s] to [%s]\n", e.first.data(), file); fprintf(stderr, "Extract [%s] to [%s]\n", e.first.data(), file);
unlink(file); unlink(file);
rmdir(file); rmdir(file);

View File

@ -44,7 +44,6 @@ public:
protected: protected:
entry_map entries; entry_map entries;
static void extract_entry(const entry_map::value_type &e, const char *file);
void rm(entry_map::iterator it); void rm(entry_map::iterator it);
void mv(entry_map::iterator it, const char *name); void mv(entry_map::iterator it, const char *name);

View File

@ -85,8 +85,6 @@ Supported actions:
Create ramdisk backups from ORIG Create ramdisk backups from ORIG
restore restore
Restore ramdisk from ramdisk backup stored within incpio Restore ramdisk from ramdisk backup stored within incpio
sha1
Print stock boot SHA1 if previously backed up in ramdisk
dtb <file> <action> [args...] dtb <file> <action> [args...]
Do dtb related actions to <file> Do dtb related actions to <file>

View File

@ -18,7 +18,6 @@ class magisk_cpio : public cpio {
public: public:
void patch(); void patch();
int test(); int test();
char *sha1();
void restore(); void restore();
void backup(const char *orig); void backup(const char *orig);
}; };
@ -81,35 +80,6 @@ int magisk_cpio::test() {
return ret; return ret;
} }
#define for_each_line(line, buf, size) \
for (char *line = (char *) buf; line < (char *) buf + size && line[0]; line = strchr(line + 1, '\n') + 1)
char *magisk_cpio::sha1() {
char sha1[41];
for (auto &e : entries) {
if (e.first == "init.magisk.rc" || e.first == "overlay/init.magisk.rc") {
for_each_line(line, e.second->data, e.second->filesize) {
if (strncmp(line, "#STOCKSHA1=", 11) == 0) {
strncpy(sha1, line + 12, 40);
sha1[40] = '\0';
return strdup(sha1);
}
}
} else if (e.first == ".backup/.magisk") {
for_each_line(line, e.second->data, e.second->filesize) {
if (str_starts(line, "SHA1=")) {
strncpy(sha1, line + 5, 40);
sha1[40] = '\0';
return strdup(sha1);
}
}
} else if (e.first == ".backup/.sha1") {
return (char *) e.second->data;
}
}
return nullptr;
}
#define for_each_str(str, buf, size) \ #define for_each_str(str, buf, size) \
for (char *str = (char *) buf; str < (char *) buf + size; str += strlen(str) + 1) for (char *str = (char *) buf; str < (char *) buf + size; str += strlen(str) + 1)
@ -263,10 +233,6 @@ int cpio_commands(int argc, char *argv[]) {
exit(cpio.test()); exit(cpio.test());
} else if (cmdv[0] == "restore"sv) { } else if (cmdv[0] == "restore"sv) {
cpio.restore(); cpio.restore();
} else if (cmdv[0] == "sha1"sv) {
char *sha1 = cpio.sha1();
if (sha1) printf("%s\n", sha1);
return 0;
} else if (cmdv[0] == "patch"sv) { } else if (cmdv[0] == "patch"sv) {
cpio.patch(); cpio.patch();
} else if (cmdc == 2 && cmdv[0] == "exists"sv) { } else if (cmdc == 2 && cmdv[0] == "exists"sv) {

View File

@ -125,9 +125,9 @@ case $((STATUS & 3)) in
;; ;;
1 ) # Magisk patched 1 ) # Magisk patched
ui_print "- Magisk patched boot image detected" ui_print "- Magisk patched boot image detected"
# Find SHA1 of stock boot image ./magiskboot cpio ramdisk.cpio \
[ -z $SHA1 ] && SHA1=$(./magiskboot cpio ramdisk.cpio sha1 2>/dev/null) "extract .backup/.magisk config.orig" \
./magiskboot cpio ramdisk.cpio restore "restore"
cp -af ramdisk.cpio ramdisk.cpio.orig cp -af ramdisk.cpio ramdisk.cpio.orig
rm -f stock_boot.img rm -f stock_boot.img
;; ;;
@ -143,6 +143,17 @@ if [ $((STATUS & 4)) -ne 0 ]; then
INIT=init.real INIT=init.real
fi fi
if [ -f config.orig ]; then
# Read existing configs
chmod 0644 config.orig
SHA1=$(grep_prop SHA1 config.orig)
if ! $BOOTMODE; then
# Do not inherit config if not in recovery
PREINITDEVICE=$(grep_prop PREINITDEVICE config.orig)
fi
rm config.orig
fi
################## ##################
# Ramdisk Patches # Ramdisk Patches
################## ##################
@ -169,7 +180,7 @@ echo "KEEPFORCEENCRYPT=$KEEPFORCEENCRYPT" >> config
echo "PATCHVBMETAFLAG=$PATCHVBMETAFLAG" >> config echo "PATCHVBMETAFLAG=$PATCHVBMETAFLAG" >> config
echo "RECOVERYMODE=$RECOVERYMODE" >> config echo "RECOVERYMODE=$RECOVERYMODE" >> config
if [ -n "$PREINITDEVICE" ]; then if [ -n "$PREINITDEVICE" ]; then
ui_print "- Pre-init storage partition device ID: $PREINITDEVICE" ui_print "- Pre-init storage partition: $PREINITDEVICE"
echo "PREINITDEVICE=$PREINITDEVICE" >> config echo "PREINITDEVICE=$PREINITDEVICE" >> config
fi fi
[ -n "$SHA1" ] && echo "SHA1=$SHA1" >> config [ -n "$SHA1" ] && echo "SHA1=$SHA1" >> config

View File

@ -104,7 +104,12 @@ case $((STATUS & 3)) in
1 ) # Magisk patched 1 ) # Magisk patched
ui_print "- Magisk patched image detected" ui_print "- Magisk patched image detected"
# Find SHA1 of stock boot image # Find SHA1 of stock boot image
SHA1=$(./magiskboot cpio ramdisk.cpio sha1 2>/dev/null) ./magiskboot cpio ramdisk.cpio "extract .backup/.magisk config.orig"
if [ -f config.orig ]; then
chmod 0644 config.orig
SHA1=$(grep_prop SHA1 config.orig)
rm config.orig
fi
BACKUPDIR=/data/magisk_backup_$SHA1 BACKUPDIR=/data/magisk_backup_$SHA1
if [ -d $BACKUPDIR ]; then if [ -d $BACKUPDIR ]; then
ui_print "- Restoring stock boot image" ui_print "- Restoring stock boot image"