Support patching mnt_point in fstab in dtb

This commit is contained in:
topjohnwu 2019-09-22 04:17:15 -04:00
parent a1ccd44013
commit 0e5a113a0c
4 changed files with 66 additions and 44 deletions

View File

@ -96,15 +96,15 @@ static int find_fstab(const void *fdt, int node = 0) {
} }
static void dtb_print(const char *file, bool fstab) { static void dtb_print(const char *file, bool fstab) {
size_t size ; size_t size;
uint8_t *dtb, *fdt; uint8_t *dtb;
fprintf(stderr, "Loading dtbs from [%s]\n", file); fprintf(stderr, "Loading dtbs from [%s]\n", file);
mmap_ro(file, dtb, size); mmap_ro(file, dtb, size);
// Loop through all the dtbs // Loop through all the dtbs
int dtb_num = 0; int dtb_num = 0;
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; auto fdt = dtb + i;
if (fstab) { if (fstab) {
int node = find_fstab(fdt); int node = find_fstab(fdt);
if (node >= 0) { if (node >= 0) {
@ -124,6 +124,9 @@ static void dtb_print(const char *file, bool fstab) {
} }
static void dtb_patch(const char *in, const char *out) { static void dtb_patch(const char *in, const char *out) {
bool keepverity = check_env("KEEPVERITY");
bool redirect = check_env("REDIRSYSMNT");
vector<uint8_t *> fdt_list; vector<uint8_t *> fdt_list;
size_t dtb_sz ; size_t dtb_sz ;
uint8_t *dtb; uint8_t *dtb;
@ -132,24 +135,34 @@ static void dtb_patch(const char *in, const char *out) {
bool modified = false; bool modified = false;
for (int i = 0; i < dtb_sz; ++i) { for (int i = 0; i < dtb_sz; ++i) {
if (memcmp(dtb + i, DTB_MAGIC, 4) == 0) { if (memcmp(dtb + i, DTB_MAGIC, 4) == 0) {
// Patched will only be smaller
int len = fdt_totalsize(dtb + i); int len = fdt_totalsize(dtb + i);
auto fdt = static_cast<uint8_t *>(xmalloc(len)); auto fdt = static_cast<uint8_t *>(xmalloc(redirect ? len + 256 : len));
memcpy(fdt, dtb + i, len); memcpy(fdt, dtb + i, len);
if (redirect)
fdt_open_into(fdt, fdt, len + 256);
int fstab = find_fstab(fdt); int fstab = find_fstab(fdt);
if (fstab < 0) if (fstab < 0)
continue; continue;
fprintf(stderr, "Found fstab in dtb.%04d\n", fdt_list.size()); fprintf(stderr, "Found fstab in dtb.%04d\n", fdt_list.size());
int block; int block;
fdt_for_each_subnode(block, fdt, fstab) { fdt_for_each_subnode(block, fdt, fstab) {
fprintf(stderr, "Found entry [%s] in fstab\n", fdt_get_name(fdt, block, nullptr)); const char *name = fdt_get_name(fdt, block, nullptr);
uint32_t size; fprintf(stderr, "Found entry [%s] in fstab\n", name);
auto value = static_cast<const char *>( if (!keepverity) {
fdt_getprop(fdt, block, "fsmgr_flags", reinterpret_cast<int *>(&size))); uint32_t size;
char *pval = patch_verity(value, size); auto value = static_cast<const char *>(
if (pval) { fdt_getprop(fdt, block, "fsmgr_flags", reinterpret_cast<int *>(&size)));
char *pval = patch_verity(value, size);
if (pval) {
modified = true;
fdt_setprop_string(fdt, block, "fsmgr_flags", pval);
}
}
if (redirect && name == "system"sv) {
modified = true; modified = true;
fdt_setprop_string(fdt, block, "fsmgr_flags", pval); fprintf(stderr, "Changing mnt_point to /system_root\n");
fdt_setprop_string(fdt, block, "mnt_point", "/system_root");
} }
} }
fdt_list.push_back(fdt); fdt_list.push_back(fdt);

View File

@ -12,13 +12,12 @@
#define DTB_FILE "dtb" #define DTB_FILE "dtb"
#define NEW_BOOT "new-boot.img" #define NEW_BOOT "new-boot.img"
// Main entries
int unpack(const char *image, bool hdr = false); 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(int argc, char *argv[]); int dtb_commands(int argc, char *argv[]);
// Pattern
char *patch_verity(const void *buf, uint32_t &size); char *patch_verity(const void *buf, uint32_t &size);
void patch_encryption(void **buf, uint32_t *size); void patch_encryption(void **buf, uint32_t *size);
bool check_env(const char *name);

View File

@ -10,7 +10,7 @@
using namespace std; using namespace std;
static const char *ramdisk_xz = "ramdisk.cpio.xz"; constexpr char RAMDISK_XZ[] = "ramdisk.cpio.xz";
static const char *UNSUPPORT_LIST[] = static const char *UNSUPPORT_LIST[] =
{ "sbin/launch_daemonsu.sh", "sbin/su", "init.xposed.rc", { "sbin/launch_daemonsu.sh", "sbin/su", "init.xposed.rc",
@ -33,7 +33,7 @@ public:
void decompress(); void decompress();
}; };
static bool check_env(const char *name) { bool check_env(const char *name) {
const char *val = getenv(name); const char *val = getenv(name);
return val ? strcmp(val, "true") == 0 : false; return val ? strcmp(val, "true") == 0 : false;
} }
@ -74,28 +74,41 @@ void magisk_cpio::patch() {
} }
#define STOCK_BOOT 0 #define STOCK_BOOT 0
#define MAGISK_PATCHED 1 << 0 #define MAGISK_PATCHED (1 << 0)
#define UNSUPPORTED_CPIO 1 << 1 #define UNSUPPORTED_CPIO (1 << 1)
#define COMPRESSED_CPIO 1 << 2 #define COMPRESSED_CPIO (1 << 2)
int magisk_cpio::test() { #define TWO_STAGE_INIT (1 << 3)
if (exists(ramdisk_xz))
return MAGISK_PATCHED | COMPRESSED_CPIO;
int magisk_cpio::test() {
for (auto file : UNSUPPORT_LIST) for (auto file : UNSUPPORT_LIST)
if (exists(file)) if (exists(file))
return UNSUPPORTED_CPIO; return UNSUPPORTED_CPIO;
for (auto file : MAGISK_LIST) int flags = STOCK_BOOT;
if (exists(file))
return MAGISK_PATCHED;
return STOCK_BOOT; if (exists(RAMDISK_XZ)) {
flags |= COMPRESSED_CPIO | MAGISK_PATCHED;
decompress();
}
if (exists("apex") || exists("first_stage_ramdisk"))
flags |= TWO_STAGE_INIT;
for (auto file : MAGISK_LIST) {
if (exists(file)) {
flags |= MAGISK_PATCHED;
break;
}
}
return flags;
} }
#define for_each_line(line, buf, size) \ #define for_each_line(line, buf, size) \
for (line = (char *) buf; line < (char *) buf + size && line[0]; line = strchr(line + 1, '\n') + 1) for (line = (char *) buf; line < (char *) buf + size && line[0]; line = strchr(line + 1, '\n') + 1)
char *magisk_cpio::sha1() { char *magisk_cpio::sha1() {
decompress();
char sha1[41]; char sha1[41];
char *line; char *line;
for (auto &e : entries) { for (auto &e : entries) {
@ -231,32 +244,26 @@ void magisk_cpio::backup(const char *orig) {
} }
void magisk_cpio::compress() { void magisk_cpio::compress() {
if (exists(ramdisk_xz)) if (exists(RAMDISK_XZ))
return; return;
fprintf(stderr, "Compressing cpio -> [%s]\n", ramdisk_xz); fprintf(stderr, "Compressing cpio -> [%s]\n", RAMDISK_XZ);
auto init = entries.extract("init"); auto init = entries.extract("init");
XZEncoder encoder; XZEncoder encoder;
encoder.set_out(make_unique<BufOutStream>()); encoder.set_out(make_unique<BufOutStream>());
output(encoder); output(encoder);
encoder.finalize(); encoder.finalize();
auto backup = entries.extract(".backup");
auto config = entries.extract(".backup/.magisk");
entries.clear(); entries.clear();
entries.insert(std::move(init)); entries.insert(std::move(init));
entries.insert(std::move(backup)); auto xz = new cpio_entry(RAMDISK_XZ, S_IFREG);
entries.insert(std::move(config));
auto xz = new cpio_entry(ramdisk_xz, S_IFREG);
static_cast<BufOutStream *>(encoder.get_out())->release(xz->data, xz->filesize); static_cast<BufOutStream *>(encoder.get_out())->release(xz->data, xz->filesize);
insert(xz); insert(xz);
} }
void magisk_cpio::decompress() { void magisk_cpio::decompress() {
auto it = entries.find(ramdisk_xz); auto it = entries.find(RAMDISK_XZ);
if (it == entries.end()) if (it == entries.end())
return; return;
fprintf(stderr, "Decompressing cpio [%s]\n", ramdisk_xz); fprintf(stderr, "Decompressing cpio [%s]\n", RAMDISK_XZ);
entries.erase(".backup");
entries.erase(".backup/.magisk");
LZMADecoder decoder; LZMADecoder decoder;
decoder.set_out(make_unique<BufOutStream>()); decoder.set_out(make_unique<BufOutStream>());
decoder.write(it->second->data, it->second->filesize); decoder.write(it->second->data, it->second->filesize);

View File

@ -113,7 +113,7 @@ 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 # Find SHA1 of stock boot image
[ -z $SHA1 ] && SHA1=`./magiskboot --cpio ramdisk.cpio sha1 2>/dev/null` [ -z $SHA1 ] && SHA1=`./magiskboot cpio ramdisk.cpio sha1 2>/dev/null`
./magiskboot cpio ramdisk.cpio restore ./magiskboot cpio ramdisk.cpio restore
if ./magiskboot cpio ramdisk.cpio "exists init.rc"; then if ./magiskboot cpio ramdisk.cpio "exists init.rc"; then
# Normal boot image # Normal boot image
@ -129,6 +129,11 @@ case $((STATUS & 3)) in
;; ;;
esac esac
if [ $((STATUS & 8)) -ne 0 ]; then
ui_print "- 2 Stage Init ramdisk detected"
export REDIRSYSMNT=true
fi
########################################################################################## ##########################################################################################
# Ramdisk patches # Ramdisk patches
########################################################################################## ##########################################################################################
@ -149,7 +154,7 @@ echo "RECOVERYMODE=$RECOVERYMODE" >> config
if [ $((STATUS & 4)) -ne 0 ]; then if [ $((STATUS & 4)) -ne 0 ]; then
ui_print "- Compressing ramdisk" ui_print "- Compressing ramdisk"
./magiskboot --cpio ramdisk.cpio compress ./magiskboot cpio ramdisk.cpio compress
fi fi
rm -f ramdisk.cpio.orig config rm -f ramdisk.cpio.orig config
@ -158,11 +163,9 @@ rm -f ramdisk.cpio.orig config
# Binary patches # Binary patches
########################################################################################## ##########################################################################################
if ! $KEEPVERITY; then for dt in dtb kernel_dtb extra recovery_dtbo; do
for dt in dtb kernel_dtb extra recovery_dtbo; do [ -f $dt ] && ./magiskboot dtb $dt patch && ui_print "- Patching fstab in $dt"
[ -f $dt ] && ./magiskboot dtb $dt patch && ui_print "- Removing dm(avb)-verity in $dt" done
done
fi
if [ -f kernel ]; then if [ -f kernel ]; then
# Remove Samsung RKP # Remove Samsung RKP