Cleanup magiskboot cpio code

This commit is contained in:
topjohnwu 2021-08-13 04:53:11 -07:00
parent 844bc2d808
commit cf8f042a20
6 changed files with 127 additions and 152 deletions

View File

@ -97,7 +97,7 @@ LOCAL_SRC_FILES := \
magiskboot/dtb.cpp \ magiskboot/dtb.cpp \
magiskboot/ramdisk.cpp \ magiskboot/ramdisk.cpp \
magiskboot/pattern.cpp \ magiskboot/pattern.cpp \
utils/cpio.cpp magiskboot/cpio.cpp
LOCAL_LDFLAGS := -static LOCAL_LDFLAGS := -static
include $(BUILD_EXECUTABLE) include $(BUILD_EXECUTABLE)

View File

@ -3,8 +3,7 @@ LOCAL_PATH := $(call my-dir)
# Header only library # Header only library
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_MODULE:= libphmap LOCAL_MODULE:= libphmap
LOCAL_C_INCLUDES := $(LOCAL_PATH)/parallel-hashmap LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/parallel-hashmap
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
include $(BUILD_STATIC_LIBRARY) include $(BUILD_STATIC_LIBRARY)
# libxz.a # libxz.a

View File

@ -1,11 +1,10 @@
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm> #include <algorithm>
#include <utils.hpp> #include <utils.hpp>
#include <cpio.hpp>
#include "cpio.hpp"
using namespace std; using namespace std;
@ -43,8 +42,11 @@ static uint32_t x8u(const char *hex) {
return val; return val;
} }
cpio_entry_base::cpio_entry_base(const cpio_newc_header *h) cpio_entry::cpio_entry(uint32_t mode) : mode(mode), uid(0), gid(0), filesize(0), data(nullptr) {}
: mode(x8u(h->mode)), uid(x8u(h->uid)), gid(x8u(h->gid)), filesize(x8u(h->filesize)) {};
cpio_entry::cpio_entry(const cpio_newc_header *h) :
mode(x8u(h->mode)), uid(x8u(h->uid)), gid(x8u(h->gid)), filesize(x8u(h->filesize)), data(nullptr)
{}
void cpio::dump(const char *file) { void cpio::dump(const char *file) {
fprintf(stderr, "Dump cpio: [%s]\n", file); fprintf(stderr, "Dump cpio: [%s]\n", file);
@ -71,12 +73,12 @@ void cpio::rm(const char *name, bool r) {
} }
} }
static void extract_entry(const entry_map::value_type &e, const char *file) { void cpio::extract_entry(const 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);
if (S_ISDIR(e.second->mode)) { if (S_ISDIR(e.second->mode)) {
mkdir(file, e.second->mode & 0777); ::mkdir(file, e.second->mode & 0777);
} else if (S_ISREG(e.second->mode)) { } else if (S_ISREG(e.second->mode)) {
int fd = creat(file, e.second->mode & 0777); int fd = creat(file, e.second->mode & 0777);
xwrite(fd, e.second->data, e.second->filesize); xwrite(fd, e.second->data, e.second->filesize);
@ -148,11 +150,7 @@ void cpio::dump(FILE *out) {
fclose(out); fclose(out);
} }
cpio_rw::cpio_rw(const char *file) { void cpio::load_cpio(const char *file) {
load_cpio(file);
}
void cpio_rw::load_cpio(const char *file) {
char *buf; char *buf;
size_t sz; size_t sz;
mmap_ro(file, buf, sz); mmap_ro(file, buf, sz);
@ -161,54 +159,49 @@ void cpio_rw::load_cpio(const char *file) {
munmap(buf, sz); munmap(buf, sz);
} }
void cpio_rw::insert(cpio_entry *e) { void cpio::insert(string_view name, cpio_entry *e) {
auto ex = entries.extract(e->filename); auto it = entries.find(name);
if (!ex) { if (it != entries.end()) {
entries[e->filename].reset(e); it->second.reset(e);
} else { } else {
ex.key() = e->filename; entries.emplace(name, e);
ex.mapped().reset(e);
entries.insert(std::move(ex));
} }
} }
void cpio_rw::add(mode_t mode, const char *name, const char *file) { void cpio::add(mode_t mode, const char *name, const char *file) {
void *buf; void *buf;
size_t sz; size_t sz;
mmap_ro(file, buf, sz); mmap_ro(file, buf, sz);
auto e = new cpio_entry(name, S_IFREG | mode); auto e = new cpio_entry(S_IFREG | mode);
e->filesize = sz; e->filesize = sz;
e->data = xmalloc(sz); e->data = xmalloc(sz);
memcpy(e->data, buf, sz); memcpy(e->data, buf, sz);
munmap(buf, sz); munmap(buf, sz);
insert(e); insert(name, e);
fprintf(stderr, "Add entry [%s] (%04o)\n", name, mode); fprintf(stderr, "Add entry [%s] (%04o)\n", name, mode);
} }
void cpio_rw::mkdir(mode_t mode, const char *name) { void cpio::mkdir(mode_t mode, const char *name) {
insert(new cpio_entry(name, S_IFDIR | mode)); insert(name, new cpio_entry(S_IFDIR | mode));
fprintf(stderr, "Create directory [%s] (%04o)\n", name, mode); fprintf(stderr, "Create directory [%s] (%04o)\n", name, mode);
} }
void cpio_rw::ln(const char *target, const char *name) { void cpio::ln(const char *target, const char *name) {
auto e = new cpio_entry(name, S_IFLNK); auto e = new cpio_entry(S_IFLNK);
e->filesize = strlen(target); e->filesize = strlen(target);
e->data = strdup(target); e->data = strdup(target);
insert(e); insert(name, e);
fprintf(stderr, "Create symlink [%s] -> [%s]\n", name, target); fprintf(stderr, "Create symlink [%s] -> [%s]\n", name, target);
} }
void cpio_rw::mv(entry_map::iterator &it, const char *to) { void cpio::mv(entry_map::iterator &it, const char *name) {
fprintf(stderr, "Move [%s] -> [%s]\n", it->first.data(), to); fprintf(stderr, "Move [%s] -> [%s]\n", it->first.data(), name);
auto ex = entries.extract(it); auto e = it->second.release();
auto &name = static_cast<cpio_entry*>(ex.mapped().get())->filename; entries.erase(it);
name = to; insert(name, e);
ex.key() = name;
entries.erase(name);
entries.insert(std::move(ex));
} }
bool cpio_rw::mv(const char *from, const char *to) { bool cpio::mv(const char *from, const char *to) {
auto it = entries.find(from); auto it = entries.find(from);
if (it != entries.end()) { if (it != entries.end()) {
mv(it, to); mv(it, to);
@ -220,26 +213,23 @@ bool cpio_rw::mv(const char *from, const char *to) {
#define pos_align(p) p = do_align(p, 4) #define pos_align(p) p = do_align(p, 4)
void cpio_rw::load_cpio(const char *buf, size_t sz) { void cpio::load_cpio(const char *buf, size_t sz) {
size_t pos = 0; size_t pos = 0;
const cpio_newc_header *header;
unique_ptr<cpio_entry> entry;
while (pos < sz) { while (pos < sz) {
header = reinterpret_cast<const cpio_newc_header *>(buf + pos); auto header = reinterpret_cast<const cpio_newc_header *>(buf + pos);
entry = make_unique<cpio_entry>(header); pos += sizeof(cpio_newc_header);
pos += sizeof(*header); string_view name(buf + pos);
string_view name_view(buf + pos);
pos += x8u(header->namesize); pos += x8u(header->namesize);
pos_align(pos); pos_align(pos);
if (name_view == "." || name_view == "..") if (name == "." || name == "..")
continue; continue;
if (name_view == "TRAILER!!!") if (name == "TRAILER!!!")
break; break;
entry->filename = name_view; auto entry = new cpio_entry(header);
entry->data = xmalloc(entry->filesize); entry->data = xmalloc(entry->filesize);
memcpy(entry->data, buf + pos, entry->filesize); memcpy(entry->data, buf + pos, entry->filesize);
pos += entry->filesize; pos += entry->filesize;
entries[entry->filename] = std::move(entry); insert(name, entry);
pos_align(pos); pos_align(pos);
} }
} }

View File

@ -0,0 +1,55 @@
#pragma once
#include <stdint.h>
#include <string>
#include <memory>
#include <map>
#include <string_view>
struct cpio_newc_header;
struct cpio_entry {
uint32_t mode;
uint32_t uid;
uint32_t gid;
uint32_t filesize;
void *data;
explicit cpio_entry(uint32_t mode = 0);
explicit cpio_entry(const cpio_newc_header *h);
~cpio_entry() { free(data); }
};
class cpio {
public:
struct StringCmp {
using is_transparent = void;
bool operator()(std::string_view a, std::string_view b) const {
return a < b;
}
};
using entry_map = std::map<std::string, std::unique_ptr<cpio_entry>, StringCmp>;
void load_cpio(const char *file);
void dump(const char *file);
void rm(const char *name, bool r = false);
void extract();
bool extract(const char *name, const char *file);
bool exists(const char *name);
void add(mode_t mode, const char *name, const char *file);
void mkdir(mode_t mode, const char *name);
void ln(const char *target, const char *name);
bool mv(const char *from, const char *to);
protected:
entry_map entries;
static void extract_entry(const entry_map::value_type &e, const char *file);
void rm(entry_map::iterator &it);
void mv(entry_map::iterator &it, const char *name);
private:
void dump(FILE *out);
void insert(std::string_view name, cpio_entry *e);
void load_cpio(const char *buf, size_t sz);
};

View File

@ -1,6 +1,6 @@
#include <utils.hpp> #include <utils.hpp>
#include <cpio.hpp>
#include "cpio.hpp"
#include "magiskboot.hpp" #include "magiskboot.hpp"
#include "compress.hpp" #include "compress.hpp"
@ -14,9 +14,8 @@ static const char *MAGISK_LIST[] =
{ ".backup/.magisk", "init.magisk.rc", { ".backup/.magisk", "init.magisk.rc",
"overlay/init.magisk.rc" }; "overlay/init.magisk.rc" };
class magisk_cpio : public cpio_rw { class magisk_cpio : public cpio {
public: public:
using cpio_rw::cpio_rw;
void patch(); void patch();
int test(); int test();
char *sha1(); char *sha1();
@ -95,7 +94,7 @@ char *magisk_cpio::sha1() {
} }
} else if (e.first == ".backup/.magisk") { } else if (e.first == ".backup/.magisk") {
for_each_line(line, e.second->data, e.second->filesize) { for_each_line(line, e.second->data, e.second->filesize) {
if (strncmp(line, "SHA1=", 5) == 0) { if (str_starts(line, "SHA1=")) {
strncpy(sha1, line + 5, 40); strncpy(sha1, line + 5, 40);
sha1[40] = '\0'; sha1[40] = '\0';
return strdup(sha1); return strdup(sha1);
@ -114,15 +113,16 @@ for (str = (char *) buf; str < (char *) buf + size; str = str += strlen(str) + 1
void magisk_cpio::restore() { void magisk_cpio::restore() {
if (auto it = entries.find(".backup/.rmlist"); it != entries.end()) { if (auto it = entries.find(".backup/.rmlist"); it != entries.end()) {
char *file; char *file;
for_each_str(file, it->second->data, it->second->filesize) for_each_str(file, it->second->data, it->second->filesize) {
rm(file, false); rm(file);
}
rm(it); rm(it);
} }
for (auto it = entries.begin(); it != entries.end();) { for (auto it = entries.begin(); it != entries.end();) {
auto cur = it++; auto cur = it++;
if (str_starts(cur->first, ".backup")) { if (str_starts(cur->first, ".backup")) {
if (cur->first.length() == 7 || cur->first.substr(8) == ".magisk") { if (cur->first.length() == 7 || &cur->first[8] == ".magisk"sv) {
rm(cur); rm(cur);
} else { } else {
mv(cur, &cur->first[8]); mv(cur, &cur->first[8]);
@ -140,15 +140,15 @@ void magisk_cpio::restore() {
void magisk_cpio::backup(const char *orig) { void magisk_cpio::backup(const char *orig) {
if (access(orig, R_OK)) if (access(orig, R_OK))
return; return;
entry_map bkup_entries;
string remv;
auto b = new cpio_entry(".backup", S_IFDIR); entry_map backups;
bkup_entries[b->filename].reset(b); string rm_list;
backups.emplace(".backup", new cpio_entry(S_IFDIR));
magisk_cpio o(orig); magisk_cpio o;
o.load_cpio(orig);
// Remove possible backups in original ramdisk // Remove existing backups in original ramdisk
o.rm(".backup", true); o.rm(".backup", true);
rm(".backup", true); rm(".backup", true);
@ -157,7 +157,7 @@ void magisk_cpio::backup(const char *orig) {
while (lhs != o.entries.end() || rhs != entries.end()) { while (lhs != o.entries.end() || rhs != entries.end()) {
int res; int res;
bool backup = false; bool do_backup = false;
if (lhs != o.entries.end() && rhs != entries.end()) { if (lhs != o.entries.end() && rhs != entries.end()) {
res = lhs->first.compare(rhs->first); res = lhs->first.compare(rhs->first);
} else if (lhs == o.entries.end()) { } else if (lhs == o.entries.end()) {
@ -167,29 +167,28 @@ void magisk_cpio::backup(const char *orig) {
} }
if (res < 0) { if (res < 0) {
// Something is missing in new ramdisk, backup! // Something is missing in new ramdisk, do_backup!
backup = true; do_backup = true;
fprintf(stderr, "Backup missing entry: "); fprintf(stderr, "Backup missing entry: ");
} else if (res == 0) { } else if (res == 0) {
if (lhs->second->filesize != rhs->second->filesize || if (lhs->second->filesize != rhs->second->filesize ||
memcmp(lhs->second->data, rhs->second->data, lhs->second->filesize) != 0) { memcmp(lhs->second->data, rhs->second->data, lhs->second->filesize) != 0) {
// Not the same! // Not the same!
backup = true; do_backup = true;
fprintf(stderr, "Backup mismatch entry: "); fprintf(stderr, "Backup mismatch entry: ");
} }
} else { } else {
// Something new in ramdisk // Something new in ramdisk
remv += rhs->first; rm_list += rhs->first;
remv += (char) '\0'; rm_list += (char) '\0';
fprintf(stderr, "Record new entry: [%s] -> [.backup/.rmlist]\n", rhs->first.data()); fprintf(stderr, "Record new entry: [%s] -> [.backup/.rmlist]\n", rhs->first.data());
} }
if (backup) {
string back_name(".backup/"); if (do_backup) {
back_name += lhs->first; string name = ".backup/" + lhs->first;
fprintf(stderr, "[%s] -> [%s]\n", lhs->first.data(), back_name.data()); fprintf(stderr, "[%s] -> [%s]\n", lhs->first.data(), name.data());
auto ex = static_cast<cpio_entry*>(lhs->second.release()); auto e = lhs->second.release();
ex->filename = back_name; backups.emplace(name, e);
bkup_entries[ex->filename].reset(ex);
} }
// Increment positions // Increment positions
@ -202,16 +201,16 @@ void magisk_cpio::backup(const char *orig) {
} }
} }
if (!remv.empty()) { if (!rm_list.empty()) {
auto rmlist = new cpio_entry(".backup/.rmlist", S_IFREG); auto rm_list_file = new cpio_entry(S_IFREG);
rmlist->filesize = remv.length(); rm_list_file->filesize = rm_list.length();
rmlist->data = xmalloc(remv.length()); rm_list_file->data = xmalloc(rm_list.length());
memcpy(rmlist->data, remv.data(), remv.length()); memcpy(rm_list_file->data, rm_list.data(), rm_list.length());
bkup_entries[rmlist->filename].reset(rmlist); backups.emplace(".backup/.rmlist", rm_list_file);
} }
if (bkup_entries.size() > 1) if (backups.size() > 1)
entries.merge(bkup_entries); entries.merge(backups);
} }
int cpio_commands(int argc, char *argv[]) { int cpio_commands(int argc, char *argv[]) {

View File

@ -1,68 +0,0 @@
#pragma once
#include <stdint.h>
#include <string>
#include <memory>
#include <map>
#include <string_view>
#include <stream.hpp>
struct cpio_newc_header;
struct cpio_entry_base {
uint32_t mode = 0;
uint32_t uid = 0;
uint32_t gid = 0;
uint32_t filesize = 0;
void *data = nullptr;
cpio_entry_base() : mode(0), uid(0), gid(0), filesize(0) {};
explicit cpio_entry_base(const cpio_newc_header *h);
virtual ~cpio_entry_base() = default;
};
struct cpio_entry : public cpio_entry_base {
std::string filename;
cpio_entry() = default;
explicit cpio_entry(const char *name, uint32_t mode) : filename(name) {
this->mode = mode;
}
explicit cpio_entry(const cpio_newc_header *h) : cpio_entry_base(h) {}
~cpio_entry() override { free(data); };
};
typedef std::map<std::string_view, std::unique_ptr<cpio_entry_base>> entry_map;
class cpio {
public:
void dump(const char *file);
void rm(const char *name, bool r = false);
void extract();
bool extract(const char *name, const char *file);
bool exists(const char *name);
protected:
entry_map entries;
void rm(entry_map::iterator &it);
void dump(FILE *out);
};
class cpio_rw : public cpio {
public:
cpio_rw() = default;
explicit cpio_rw(const char *file);
void load_cpio(const char *file);
void add(mode_t mode, const char *name, const char *file);
void mkdir(mode_t mode, const char *name);
void ln(const char *target, const char *name);
bool mv(const char *from, const char *to);
protected:
void insert(cpio_entry *e);
void mv(entry_map::iterator &it, const char *to);
void load_cpio(const char *buf, size_t sz);
};