Add compressed ramdisk support

This commit is contained in:
topjohnwu
2019-02-24 20:39:01 -05:00
parent 16ec37a226
commit 63b18246d8
7 changed files with 63 additions and 14 deletions

View File

@@ -30,7 +30,6 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/mman.h>
#include <sys/sendfile.h>
#include <sys/sysmacros.h>
#include <functional>
@@ -40,6 +39,7 @@
#include <magisk.h>
#include <magiskpolicy.h>
#include <selinux.h>
#include <cpio.h>
#include <utils.h>
#include <flags.h>
@@ -320,6 +320,24 @@ static bool unxz(int fd, const uint8_t *buf, size_t size) {
return true;
}
static void decompress_ramdisk() {
constexpr char tmp[] = "tmp.cpio";
constexpr char ramdisk_xz[] = "ramdisk.cpio.xz";
if (access(ramdisk_xz, F_OK))
return;
uint8_t *buf;
size_t sz;
mmap_ro(ramdisk_xz, (void **) &buf, &sz);
int fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC);
unxz(fd, buf, sz);
munmap(buf, sz);
close(fd);
cpio_mmap cpio(tmp);
cpio.extract();
unlink(tmp);
unlink(ramdisk_xz);
}
static int dump_magisk(const char *path, mode_t mode) {
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode);
if (fd < 0)
@@ -496,6 +514,8 @@ int main(int argc, char *argv[]) {
frm_rf(root);
excl_list = nullptr;
} else {
decompress_ramdisk();
// Revert original init binary
rename("/.backup/init", "/init");
rm_rf("/.backup");

View File

@@ -18,7 +18,7 @@ static const char *UNSUPPORT_LIST[] =
static const char *MAGISK_LIST[] =
{ ".backup/.magisk", "init.magisk.rc",
"overlay/init.magisk.rc", ramdisk_xz };
"overlay/init.magisk.rc" };
class magisk_cpio : public cpio_rw {
public:
@@ -59,17 +59,21 @@ void magisk_cpio::patch(bool keepverity, bool keepforceencrypt) {
}
}
#define STOCK_BOOT 0x0
#define MAGISK_PATCH 0x1
#define UNSUPPORT_PATCH 0x2
#define STOCK_BOOT 0
#define MAGISK_PATCHED 1 << 0
#define UNSUPPORTED_CPIO 1 << 1
#define COMPRESSED_CPIO 1 << 2
int magisk_cpio::test() {
if (exists(ramdisk_xz))
return MAGISK_PATCHED | COMPRESSED_CPIO;
for (auto file : UNSUPPORT_LIST)
if (exists(file))
return UNSUPPORT_PATCH;
return UNSUPPORTED_CPIO;
for (auto file : MAGISK_LIST)
if (exists(file))
return MAGISK_PATCH;
return MAGISK_PATCHED;
return STOCK_BOOT;
}
@@ -108,6 +112,7 @@ char *magisk_cpio::sha1() {
for (str = (char *) buf; str < (char *) buf + size; str = str += strlen(str) + 1)
void magisk_cpio::restore() {
decompress();
char *file;
auto next = entries.begin();
decltype(next) cur;
@@ -218,8 +223,12 @@ void magisk_cpio::compress() {
encoder.set_out(make_unique<BufOutStream>());
output(encoder);
encoder.finalize();
auto backup = entries.extract(".backup");
auto config = entries.extract(".backup/.magisk");
entries.clear();
entries.insert(std::move(init));
entries.insert(std::move(backup));
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);
insert(xz);
@@ -230,6 +239,8 @@ void magisk_cpio::decompress() {
if (it == entries.end())
return;
fprintf(stderr, "Decompressing cpio [%s]\n", ramdisk_xz);
entries.erase(".backup");
entries.erase(".backup/.magisk");
LZMADecoder decoder;
decoder.set_out(make_unique<BufOutStream>());
decoder.write(it->second->data, it->second->filesize);

View File

@@ -3,6 +3,8 @@
#include <unistd.h>
#include <memory>
#include "utils.h"
class OutStream {
public:
virtual bool write(const void *buf, size_t len) = 0;