Only compress kernel and ramdisk if input not compressed

This commit is contained in:
topjohnwu 2019-03-07 07:24:06 -05:00
parent a3fc6d2a27
commit 0442d6d509
5 changed files with 50 additions and 60 deletions

View File

@ -1,8 +1,11 @@
#include <sys/mman.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <libfdt.h>
#include <sys/mman.h>
#include <functional>
#include <memory>
#include <mincrypt/sha.h>
#include <mincrypt/sha256.h>
@ -13,9 +16,26 @@
#include "magiskboot.h"
#include "compress.h"
using namespace std;
uint32_t dyn_img_hdr::j32 = 0;
uint64_t dyn_img_hdr::j64 = 0;
static int64_t one_step(unique_ptr<Compression> &&ptr, int fd, const void *in, size_t size) {
ptr->set_out(make_unique<FDOutStream>(fd));
if (!ptr->write(in, size))
return -1;
return ptr->finalize();
}
static int64_t decompress(format_t type, int fd, const void *in, size_t size) {
return one_step(unique_ptr<Compression>(get_decoder(type)), fd, in, size);
}
static int64_t compress(format_t type, int fd, const void *in, size_t size) {
return one_step(unique_ptr<Compression>(get_encoder(type)), fd, in, size);
}
static void dump(void *buf, size_t size, const char *filename) {
if (size == 0)
return;
@ -329,15 +349,15 @@ void repack(const char* orig_image, const char* out_image) {
write_zero(fd, 512);
}
if (access(KERNEL_FILE, R_OK) == 0) {
if (COMPRESSED(boot.k_fmt)) {
size_t raw_size;
void *kernel_raw;
mmap_ro(KERNEL_FILE, kernel_raw, raw_size);
boot.hdr->kernel_size = compress(boot.k_fmt, fd, kernel_raw, raw_size);
munmap(kernel_raw, raw_size);
size_t raw_size;
void *raw_buf;
mmap_ro(KERNEL_FILE, raw_buf, raw_size);
if (!COMPRESSED(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.k_fmt)) {
boot.hdr->kernel_size = compress(boot.k_fmt, fd, raw_buf, raw_size);
} else {
boot.hdr->kernel_size = restore(KERNEL_FILE, fd);
boot.hdr->kernel_size = write(fd, raw_buf, raw_size);
}
munmap(raw_buf, raw_size);
}
// dtb
@ -352,15 +372,15 @@ void repack(const char* orig_image, const char* out_image) {
write_zero(fd, 512);
}
if (access(RAMDISK_FILE, R_OK) == 0) {
if (COMPRESSED(boot.r_fmt)) {
size_t cpio_size;
void *cpio;
mmap_ro(RAMDISK_FILE, cpio, cpio_size);
boot.hdr->ramdisk_size = compress(boot.r_fmt, fd, cpio, cpio_size);
munmap(cpio, cpio_size);
size_t raw_size;
void *raw_buf;
mmap_ro(RAMDISK_FILE, raw_buf, raw_size);
if (!COMPRESSED(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.r_fmt)) {
boot.hdr->ramdisk_size = compress(boot.r_fmt, fd, raw_buf, raw_size);
} else {
boot.hdr->ramdisk_size = restore(RAMDISK_FILE, fd);
boot.hdr->ramdisk_size = write(fd, raw_buf, raw_size);
}
munmap(raw_buf, raw_size);
file_align();
}

View File

@ -14,14 +14,6 @@
using namespace std;
int64_t decompress(format_t type, int fd, const void *from, size_t size) {
return unique_ptr<Compression>(get_decoder(type))->one_step(fd, from, size);
}
int64_t compress(format_t type, int fd, const void *from, size_t size) {
return unique_ptr<Compression>(get_encoder(type))->one_step(fd, from, size);
}
static bool read_file(FILE *fp, const function<void (void *, size_t)> &fn) {
char buf[4096];
size_t len;
@ -164,13 +156,6 @@ Compression *get_decoder(format_t type) {
}
}
int64_t Compression::one_step(int outfd, const void *in, size_t size) {
set_out(make_unique<FDOutStream>(outfd));
if (!write(in, size))
return -1;
return finalize();
}
GZStream::GZStream(int mode) : mode(mode), strm({}) {
switch(mode) {
case 0:

View File

@ -14,7 +14,6 @@
class Compression : public FilterOutStream {
public:
int64_t one_step(int outfd, const void *in, size_t size);
virtual uint64_t finalize() = 0;
};
@ -171,7 +170,5 @@ private:
Compression *get_encoder(format_t type);
Compression *get_decoder(format_t type);
int64_t compress(format_t type, int fd, const void *from, size_t size);
int64_t decompress(format_t type, int fd, const void *from, size_t size);
void compress(const char *method, const char *infile, const char *outfile);
void decompress(char *infile, const char *outfile);

View File

@ -1,5 +1,4 @@
#ifndef _FORMAT_H_
#define _FORMAT_H_
#pragma once
#include <map>
#include <string_view>
@ -50,9 +49,6 @@ typedef enum {
#define ACCLAIM_MAGIC "BauwksBoot"
#define ACCLAIM_PRE_HEADER_SZ 262144
#define SUP_LIST ((const char *[]) { "gzip", "xz", "lzma", "bzip2", "lz4", "lz4_legacy", NULL })
#define SUP_EXT_LIST ((const char *[]) { "gz", "xz", "lzma", "bz2", "lz4", "lz4", NULL })
class Fmt2Name {
public:
const char *operator[](format_t fmt);
@ -68,5 +64,3 @@ format_t check_fmt(const void *buf, size_t len);
extern std::map<std::string_view, format_t> name2fmt;
extern Fmt2Name fmt2name;
extern Fmt2Ext fmt2ext;
#endif

View File

@ -14,20 +14,21 @@
static void usage(char *arg0) {
fprintf(stderr,
FULL_VER(MagiskBoot) " - Boot Image Modification Tool\n"
"Usage: %s <action> [args...]\n"
"\n"
"Supported actions:\n"
" --unpack <bootimg>\n"
" Unpack <bootimg> to kernel, ramdisk.cpio, and if available, second, dtb,\n"
" and extra into the current directory. Return values:\n"
" Unpack <bootimg> to, if available, kernel, ramdisk.cpio, \n"
" second, dtb, extra, and recovery_dtbo into current directory.\n"
" Return values:\n"
" 0:valid 1:error 2:chromeos 3:ELF32 4:ELF64\n"
"\n"
" --repack <origbootimg> [outbootimg]\n"
" Repack kernel, ramdisk.cpio[.ext], second, dtb... from current directory\n"
" Repack boot image components from current directory\n"
" to [outbootimg], or new-boot.img if not specified.\n"
" It will compress ramdisk.cpio with the same method used in <origbootimg>,\n"
" or attempt to find ramdisk.cpio.[ext], and repack directly with the\n"
" compressed ramdisk file\n"
" It will compress ramdisk.cpio and kernel with the same method in\n"
" <origbootimg> if the file provided is not already compressed.\n"
"\n"
" --hexpatch <file> <hexpattern1> <hexpattern2>\n"
" Search <hexpattern1> in <file>, and replace with <hexpattern2>\n"
@ -80,16 +81,16 @@ static void usage(char *arg0) {
" <infile>/[outfile] can be '-' to be STDIN/STDOUT\n"
" Supported methods: "
, arg0);
for (int i = 0; SUP_LIST[i]; ++i)
fprintf(stderr, "%s ", SUP_LIST[i]);
for (auto &it : name2fmt)
fprintf(stderr, "%s ", it.first.data());
fprintf(stderr,
"\n\n"
" --decompress <infile> [outfile]\n"
" Detect method and decompress <infile>, optionally to [outfile]\n"
" <infile>/[outfile] can be '-' to be STDIN/STDOUT\n"
" Supported methods: ");
for (int i = 0; SUP_LIST[i]; ++i)
fprintf(stderr, "%s ", SUP_LIST[i]);
for (auto &it : name2fmt)
fprintf(stderr, "%s ", it.first.data());
fprintf(stderr,
"\n\n"
" --sha1 <file>\n"
@ -104,31 +105,24 @@ static void usage(char *arg0) {
int main(int argc, char *argv[]) {
cmdline_logging();
fprintf(stderr, FULL_VER(MagiskBoot) " - Boot Image Modification Tool\n");
umask(0);
if (argc > 1 && strcmp(argv[1], "--cleanup") == 0) {
fprintf(stderr, "Cleaning up...\n");
char name[PATH_MAX];
unlink(KERNEL_FILE);
unlink(RAMDISK_FILE);
unlink(RAMDISK_FILE ".raw");
unlink(SECOND_FILE);
unlink(DTB_FILE);
unlink(EXTRA_FILE);
unlink(RECV_DTBO_FILE);
for (int i = 0; SUP_EXT_LIST[i]; ++i) {
sprintf(name, "%s.%s", RAMDISK_FILE, SUP_EXT_LIST[i]);
unlink(name);
}
} else if (argc > 2 && strcmp(argv[1], "--sha1") == 0) {
uint8_t sha1[SHA_DIGEST_SIZE];
void *buf;
size_t size;
mmap_ro(argv[2], buf, size);
SHA_hash(buf, size, sha1);
for (int i = 0; i < SHA_DIGEST_SIZE; ++i)
printf("%02x", sha1[i]);
for (uint8_t i : sha1)
printf("%02x", i);
printf("\n");
munmap(buf, size);
} else if (argc > 2 && strcmp(argv[1], "--unpack") == 0) {