mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-02 18:31:53 +00:00
Cleanup magiskboot cpio code
This commit is contained in:
235
native/jni/magiskboot/cpio.cpp
Normal file
235
native/jni/magiskboot/cpio.cpp
Normal file
@@ -0,0 +1,235 @@
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <algorithm>
|
||||
|
||||
#include <utils.hpp>
|
||||
|
||||
#include "cpio.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct cpio_newc_header {
|
||||
char magic[6];
|
||||
char ino[8];
|
||||
char mode[8];
|
||||
char uid[8];
|
||||
char gid[8];
|
||||
char nlink[8];
|
||||
char mtime[8];
|
||||
char filesize[8];
|
||||
char devmajor[8];
|
||||
char devminor[8];
|
||||
char rdevmajor[8];
|
||||
char rdevminor[8];
|
||||
char namesize[8];
|
||||
char check[8];
|
||||
} __attribute__((packed));
|
||||
|
||||
static uint32_t x8u(const char *hex) {
|
||||
uint32_t val, inpos = 8, outpos;
|
||||
char pattern[6];
|
||||
|
||||
while (*hex == '0') {
|
||||
hex++;
|
||||
if (!--inpos) return 0;
|
||||
}
|
||||
// Because scanf gratuitously treats %*X differently than printf does.
|
||||
sprintf(pattern, "%%%dx%%n", inpos);
|
||||
sscanf(hex, pattern, &val, &outpos);
|
||||
if (inpos != outpos)
|
||||
LOGE("bad cpio header\n");
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
cpio_entry::cpio_entry(uint32_t mode) : mode(mode), uid(0), gid(0), filesize(0), data(nullptr) {}
|
||||
|
||||
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) {
|
||||
fprintf(stderr, "Dump cpio: [%s]\n", file);
|
||||
dump(xfopen(file, "we"));
|
||||
}
|
||||
|
||||
void cpio::rm(entry_map::iterator &it) {
|
||||
fprintf(stderr, "Remove [%s]\n", it->first.data());
|
||||
entries.erase(it);
|
||||
}
|
||||
|
||||
void cpio::rm(const char *name, bool r) {
|
||||
size_t len = strlen(name);
|
||||
for (auto it = entries.begin(); it != entries.end();) {
|
||||
if (it->first.compare(0, len, name) == 0 &&
|
||||
((r && it->first[len] == '/') || it->first[len] == '\0')) {
|
||||
auto tmp = it;
|
||||
++it;
|
||||
rm(tmp);
|
||||
if (!r) return;
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void cpio::extract_entry(const entry_map::value_type &e, const char *file) {
|
||||
fprintf(stderr, "Extract [%s] to [%s]\n", e.first.data(), file);
|
||||
unlink(file);
|
||||
rmdir(file);
|
||||
if (S_ISDIR(e.second->mode)) {
|
||||
::mkdir(file, e.second->mode & 0777);
|
||||
} else if (S_ISREG(e.second->mode)) {
|
||||
int fd = creat(file, e.second->mode & 0777);
|
||||
xwrite(fd, e.second->data, e.second->filesize);
|
||||
fchown(fd, e.second->uid, e.second->gid);
|
||||
close(fd);
|
||||
} else if (S_ISLNK(e.second->mode)) {
|
||||
auto target = strndup((char *) e.second->data, e.second->filesize);
|
||||
symlink(target, file);
|
||||
free(target);
|
||||
}
|
||||
}
|
||||
|
||||
void cpio::extract() {
|
||||
for (auto &e : entries)
|
||||
extract_entry(e, e.first.data());
|
||||
}
|
||||
|
||||
bool cpio::extract(const char *name, const char *file) {
|
||||
auto it = entries.find(name);
|
||||
if (it != entries.end()) {
|
||||
extract_entry(*it, file);
|
||||
return true;
|
||||
}
|
||||
fprintf(stderr, "Cannot find the file entry [%s]\n", name);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool cpio::exists(const char *name) {
|
||||
return entries.count(name) != 0;
|
||||
}
|
||||
|
||||
#define do_out(buf, len) pos += fwrite(buf, 1, len, out);
|
||||
#define out_align() do_out(zeros, align_off(pos, 4))
|
||||
void cpio::dump(FILE *out) {
|
||||
size_t pos = 0;
|
||||
unsigned inode = 300000;
|
||||
char header[111];
|
||||
char zeros[4] = {0};
|
||||
for (auto &e : entries) {
|
||||
sprintf(header, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
|
||||
inode++, // e->ino
|
||||
e.second->mode,
|
||||
e.second->uid,
|
||||
e.second->gid,
|
||||
1, // e->nlink
|
||||
0, // e->mtime
|
||||
e.second->filesize,
|
||||
0, // e->devmajor
|
||||
0, // e->devminor
|
||||
0, // e->rdevmajor
|
||||
0, // e->rdevminor
|
||||
(uint32_t) e.first.size() + 1,
|
||||
0 // e->check
|
||||
);
|
||||
do_out(header, 110);
|
||||
do_out(e.first.data(), e.first.size() + 1);
|
||||
out_align();
|
||||
if (e.second->filesize) {
|
||||
do_out(e.second->data, e.second->filesize);
|
||||
out_align();
|
||||
}
|
||||
}
|
||||
// Write trailer
|
||||
sprintf(header, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
|
||||
inode++, 0755, 0, 0, 1, 0, 0, 0, 0, 0, 0, 11, 0);
|
||||
do_out(header, 110);
|
||||
do_out("TRAILER!!!\0", 11);
|
||||
out_align();
|
||||
fclose(out);
|
||||
}
|
||||
|
||||
void cpio::load_cpio(const char *file) {
|
||||
char *buf;
|
||||
size_t sz;
|
||||
mmap_ro(file, buf, sz);
|
||||
fprintf(stderr, "Loading cpio: [%s]\n", file);
|
||||
load_cpio(buf, sz);
|
||||
munmap(buf, sz);
|
||||
}
|
||||
|
||||
void cpio::insert(string_view name, cpio_entry *e) {
|
||||
auto it = entries.find(name);
|
||||
if (it != entries.end()) {
|
||||
it->second.reset(e);
|
||||
} else {
|
||||
entries.emplace(name, e);
|
||||
}
|
||||
}
|
||||
|
||||
void cpio::add(mode_t mode, const char *name, const char *file) {
|
||||
void *buf;
|
||||
size_t sz;
|
||||
mmap_ro(file, buf, sz);
|
||||
auto e = new cpio_entry(S_IFREG | mode);
|
||||
e->filesize = sz;
|
||||
e->data = xmalloc(sz);
|
||||
memcpy(e->data, buf, sz);
|
||||
munmap(buf, sz);
|
||||
insert(name, e);
|
||||
fprintf(stderr, "Add entry [%s] (%04o)\n", name, mode);
|
||||
}
|
||||
|
||||
void cpio::mkdir(mode_t mode, const char *name) {
|
||||
insert(name, new cpio_entry(S_IFDIR | mode));
|
||||
fprintf(stderr, "Create directory [%s] (%04o)\n", name, mode);
|
||||
}
|
||||
|
||||
void cpio::ln(const char *target, const char *name) {
|
||||
auto e = new cpio_entry(S_IFLNK);
|
||||
e->filesize = strlen(target);
|
||||
e->data = strdup(target);
|
||||
insert(name, e);
|
||||
fprintf(stderr, "Create symlink [%s] -> [%s]\n", name, target);
|
||||
}
|
||||
|
||||
void cpio::mv(entry_map::iterator &it, const char *name) {
|
||||
fprintf(stderr, "Move [%s] -> [%s]\n", it->first.data(), name);
|
||||
auto e = it->second.release();
|
||||
entries.erase(it);
|
||||
insert(name, e);
|
||||
}
|
||||
|
||||
bool cpio::mv(const char *from, const char *to) {
|
||||
auto it = entries.find(from);
|
||||
if (it != entries.end()) {
|
||||
mv(it, to);
|
||||
return true;
|
||||
}
|
||||
fprintf(stderr, "Cannot find entry %s\n", from);
|
||||
return false;
|
||||
}
|
||||
|
||||
#define pos_align(p) p = do_align(p, 4)
|
||||
|
||||
void cpio::load_cpio(const char *buf, size_t sz) {
|
||||
size_t pos = 0;
|
||||
while (pos < sz) {
|
||||
auto header = reinterpret_cast<const cpio_newc_header *>(buf + pos);
|
||||
pos += sizeof(cpio_newc_header);
|
||||
string_view name(buf + pos);
|
||||
pos += x8u(header->namesize);
|
||||
pos_align(pos);
|
||||
if (name == "." || name == "..")
|
||||
continue;
|
||||
if (name == "TRAILER!!!")
|
||||
break;
|
||||
auto entry = new cpio_entry(header);
|
||||
entry->data = xmalloc(entry->filesize);
|
||||
memcpy(entry->data, buf + pos, entry->filesize);
|
||||
pos += entry->filesize;
|
||||
insert(name, entry);
|
||||
pos_align(pos);
|
||||
}
|
||||
}
|
||||
55
native/jni/magiskboot/cpio.hpp
Normal file
55
native/jni/magiskboot/cpio.hpp
Normal 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);
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <utils.hpp>
|
||||
#include <cpio.hpp>
|
||||
|
||||
#include "cpio.hpp"
|
||||
#include "magiskboot.hpp"
|
||||
#include "compress.hpp"
|
||||
|
||||
@@ -14,9 +14,8 @@ static const char *MAGISK_LIST[] =
|
||||
{ ".backup/.magisk", "init.magisk.rc",
|
||||
"overlay/init.magisk.rc" };
|
||||
|
||||
class magisk_cpio : public cpio_rw {
|
||||
class magisk_cpio : public cpio {
|
||||
public:
|
||||
using cpio_rw::cpio_rw;
|
||||
void patch();
|
||||
int test();
|
||||
char *sha1();
|
||||
@@ -95,7 +94,7 @@ char *magisk_cpio::sha1() {
|
||||
}
|
||||
} else if (e.first == ".backup/.magisk") {
|
||||
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);
|
||||
sha1[40] = '\0';
|
||||
return strdup(sha1);
|
||||
@@ -114,15 +113,16 @@ for (str = (char *) buf; str < (char *) buf + size; str = str += strlen(str) + 1
|
||||
void magisk_cpio::restore() {
|
||||
if (auto it = entries.find(".backup/.rmlist"); it != entries.end()) {
|
||||
char *file;
|
||||
for_each_str(file, it->second->data, it->second->filesize)
|
||||
rm(file, false);
|
||||
for_each_str(file, it->second->data, it->second->filesize) {
|
||||
rm(file);
|
||||
}
|
||||
rm(it);
|
||||
}
|
||||
|
||||
for (auto it = entries.begin(); it != entries.end();) {
|
||||
auto cur = it++;
|
||||
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);
|
||||
} else {
|
||||
mv(cur, &cur->first[8]);
|
||||
@@ -140,15 +140,15 @@ void magisk_cpio::restore() {
|
||||
void magisk_cpio::backup(const char *orig) {
|
||||
if (access(orig, R_OK))
|
||||
return;
|
||||
entry_map bkup_entries;
|
||||
string remv;
|
||||
|
||||
auto b = new cpio_entry(".backup", S_IFDIR);
|
||||
bkup_entries[b->filename].reset(b);
|
||||
entry_map backups;
|
||||
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);
|
||||
rm(".backup", true);
|
||||
|
||||
@@ -157,7 +157,7 @@ void magisk_cpio::backup(const char *orig) {
|
||||
|
||||
while (lhs != o.entries.end() || rhs != entries.end()) {
|
||||
int res;
|
||||
bool backup = false;
|
||||
bool do_backup = false;
|
||||
if (lhs != o.entries.end() && rhs != entries.end()) {
|
||||
res = lhs->first.compare(rhs->first);
|
||||
} else if (lhs == o.entries.end()) {
|
||||
@@ -167,29 +167,28 @@ void magisk_cpio::backup(const char *orig) {
|
||||
}
|
||||
|
||||
if (res < 0) {
|
||||
// Something is missing in new ramdisk, backup!
|
||||
backup = true;
|
||||
// Something is missing in new ramdisk, do_backup!
|
||||
do_backup = true;
|
||||
fprintf(stderr, "Backup missing entry: ");
|
||||
} else if (res == 0) {
|
||||
if (lhs->second->filesize != rhs->second->filesize ||
|
||||
memcmp(lhs->second->data, rhs->second->data, lhs->second->filesize) != 0) {
|
||||
// Not the same!
|
||||
backup = true;
|
||||
do_backup = true;
|
||||
fprintf(stderr, "Backup mismatch entry: ");
|
||||
}
|
||||
} else {
|
||||
// Something new in ramdisk
|
||||
remv += rhs->first;
|
||||
remv += (char) '\0';
|
||||
rm_list += rhs->first;
|
||||
rm_list += (char) '\0';
|
||||
fprintf(stderr, "Record new entry: [%s] -> [.backup/.rmlist]\n", rhs->first.data());
|
||||
}
|
||||
if (backup) {
|
||||
string back_name(".backup/");
|
||||
back_name += lhs->first;
|
||||
fprintf(stderr, "[%s] -> [%s]\n", lhs->first.data(), back_name.data());
|
||||
auto ex = static_cast<cpio_entry*>(lhs->second.release());
|
||||
ex->filename = back_name;
|
||||
bkup_entries[ex->filename].reset(ex);
|
||||
|
||||
if (do_backup) {
|
||||
string name = ".backup/" + lhs->first;
|
||||
fprintf(stderr, "[%s] -> [%s]\n", lhs->first.data(), name.data());
|
||||
auto e = lhs->second.release();
|
||||
backups.emplace(name, e);
|
||||
}
|
||||
|
||||
// Increment positions
|
||||
@@ -202,16 +201,16 @@ void magisk_cpio::backup(const char *orig) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!remv.empty()) {
|
||||
auto rmlist = new cpio_entry(".backup/.rmlist", S_IFREG);
|
||||
rmlist->filesize = remv.length();
|
||||
rmlist->data = xmalloc(remv.length());
|
||||
memcpy(rmlist->data, remv.data(), remv.length());
|
||||
bkup_entries[rmlist->filename].reset(rmlist);
|
||||
if (!rm_list.empty()) {
|
||||
auto rm_list_file = new cpio_entry(S_IFREG);
|
||||
rm_list_file->filesize = rm_list.length();
|
||||
rm_list_file->data = xmalloc(rm_list.length());
|
||||
memcpy(rm_list_file->data, rm_list.data(), rm_list.length());
|
||||
backups.emplace(".backup/.rmlist", rm_list_file);
|
||||
}
|
||||
|
||||
if (bkup_entries.size() > 1)
|
||||
entries.merge(bkup_entries);
|
||||
if (backups.size() > 1)
|
||||
entries.merge(backups);
|
||||
}
|
||||
|
||||
int cpio_commands(int argc, char *argv[]) {
|
||||
|
||||
Reference in New Issue
Block a user