Support legacy lzma

This commit is contained in:
topjohnwu 2017-03-01 00:46:11 +08:00
parent ecaafd1b70
commit 77eec3d21d
4 changed files with 64 additions and 32 deletions

View File

@ -3,8 +3,9 @@
#include "magiskboot.h" #include "magiskboot.h"
void gzip(int dec, const char* filename, unsigned char* buf, size_t size) { // Mode: 0 = decode gz; 1 = encode gz
int ret, flush, have, pos = 0; void gzip(int mode, const char* filename, unsigned char* buf, size_t size) {
size_t ret, flush, have, pos = 0;
z_stream strm; z_stream strm;
unsigned char out[CHUNK]; unsigned char out[CHUNK];
@ -17,10 +18,15 @@ void gzip(int dec, const char* filename, unsigned char* buf, size_t size) {
strm.zfree = Z_NULL; strm.zfree = Z_NULL;
strm.opaque = Z_NULL; strm.opaque = Z_NULL;
if (dec) { switch(mode) {
ret = inflateInit2(&strm, windowBits | ZLIB_GZIP); case 0:
} else { ret = inflateInit2(&strm, windowBits | ZLIB_GZIP);
ret = deflateInit2(&strm, 9, Z_DEFLATED, windowBits | ZLIB_GZIP, memLevel, Z_DEFAULT_STRATEGY); break;
case 1:
ret = deflateInit2(&strm, 9, Z_DEFLATED, windowBits | ZLIB_GZIP, memLevel, Z_DEFAULT_STRATEGY);
break;
default:
error(1, "Unsupported gzip mode!");
} }
if (ret != Z_OK) if (ret != Z_OK)
@ -41,28 +47,40 @@ void gzip(int dec, const char* filename, unsigned char* buf, size_t size) {
do { do {
strm.avail_out = CHUNK; strm.avail_out = CHUNK;
strm.next_out = out; strm.next_out = out;
if (dec) { switch(mode) {
inflate(&strm, flush); case 0:
} else { ret = inflate(&strm, flush);
deflate(&strm, flush); break;
case 1:
ret = deflate(&strm, flush);
break;
} }
if (ret == Z_STREAM_ERROR)
error(1, "Error when running gzip");
have = CHUNK - strm.avail_out; have = CHUNK - strm.avail_out;
if (write(fd, out, have) != have) if (write(fd, out, have) != have)
error(1, "Error in writing %s", filename); error(1, "Error when writing %s", filename);
} while (strm.avail_out == 0); } while (strm.avail_out == 0);
} while(pos < size); } while(pos < size);
if (dec) { switch(mode) {
inflateEnd(&strm); case 0:
} else { inflateEnd(&strm);
deflateEnd(&strm); break;
case 1:
deflateEnd(&strm);
break;
} }
close(fd); close(fd);
} }
void lzma(int dec, const char* filename, unsigned char* buf, size_t size) {
int have, pos = 0; // Mode: 0 = decode xz/lzma; 1 = encode xz; 2 = encode lzma
void lzma(int mode, const char* filename, unsigned char* buf, size_t size) {
size_t have, pos = 0;
lzma_ret ret; lzma_ret ret;
lzma_stream strm = LZMA_STREAM_INIT; lzma_stream strm = LZMA_STREAM_INIT;
lzma_options_lzma opt; lzma_options_lzma opt;
@ -74,17 +92,28 @@ void lzma(int dec, const char* filename, unsigned char* buf, size_t size) {
if (fd < 0) if (fd < 0)
error(1, "Unable to create %s", filename); error(1, "Unable to create %s", filename);
if (dec) { // Initialize preset
ret = lzma_auto_decoder(&strm, UINT64_MAX, 0); lzma_lzma_preset(&opt, LZMA_PRESET_DEFAULT);
} else { lzma_filter filters[] = {
lzma_lzma_preset(&opt, LZMA_PRESET_DEFAULT); { .id = LZMA_FILTER_LZMA2, .options = &opt },
lzma_filter filters[] = { { .id = LZMA_VLI_UNKNOWN, .options = NULL },
{ .id = LZMA_FILTER_LZMA2, .options = &opt }, };
{ .id = LZMA_VLI_UNKNOWN, .options = NULL },
}; switch(mode) {
ret = lzma_stream_encoder(&strm, filters, LZMA_CHECK_CRC64); case 0:
ret = lzma_auto_decoder(&strm, UINT64_MAX, 0);
break;
case 1:
ret = lzma_stream_encoder(&strm, filters, LZMA_CHECK_CRC64);
break;
case 2:
ret = lzma_alone_encoder(&strm, &opt);
break;
default:
error(1, "Unsupported lzma mode!");
} }
if (ret != LZMA_OK) if (ret != LZMA_OK)
error(1, "Unable to init lzma"); error(1, "Unable to init lzma");

View File

@ -56,7 +56,7 @@ void error(int rc, const char *msg, ...);
void parse_img(unsigned char *orig, size_t size); void parse_img(unsigned char *orig, size_t size);
// Compressions // Compressions
void gzip(int dec, const char* filename, unsigned char* buf, size_t size); void gzip(int mode, const char* filename, unsigned char* buf, size_t size);
void lzma(int dec, const char* filename, unsigned char* buf, size_t size); void lzma(int mode, const char* filename, unsigned char* buf, size_t size);
#endif #endif

View File

@ -94,16 +94,18 @@ void repack(const char* image) {
switch (ramdisk_type) { switch (ramdisk_type) {
case GZIP: case GZIP:
sprintf(name, "%s.%s", RAMDISK_FILE, "gz"); sprintf(name, "%s.%s", RAMDISK_FILE, "gz");
gzip(0, name, cpio, cpio_size); gzip(1, name, cpio, cpio_size);
break; break;
case LZOP: case LZOP:
sprintf(name, "%s.%s", RAMDISK_FILE, "lzo"); sprintf(name, "%s.%s", RAMDISK_FILE, "lzo");
break; break;
case XZ: case XZ:
sprintf(name, "%s.%s", RAMDISK_FILE, "xz"); sprintf(name, "%s.%s", RAMDISK_FILE, "xz");
lzma(1, name, cpio, cpio_size);
break; break;
case LZMA: case LZMA:
sprintf(name, "%s.%s", RAMDISK_FILE, "lzma"); sprintf(name, "%s.%s", RAMDISK_FILE, "lzma");
lzma(2, name, cpio, cpio_size);
break; break;
case BZIP2: case BZIP2:
sprintf(name, "%s.%s", RAMDISK_FILE, "bz2"); sprintf(name, "%s.%s", RAMDISK_FILE, "bz2");

View File

@ -44,17 +44,18 @@ void unpack(const char* image) {
switch (ramdisk_type) { switch (ramdisk_type) {
case GZIP: case GZIP:
sprintf(name, "%s.%s", RAMDISK_FILE, "gz"); sprintf(name, "%s.%s", RAMDISK_FILE, "gz");
gzip(1, RAMDISK_FILE, ramdisk, hdr.ramdisk_size); gzip(0, RAMDISK_FILE, ramdisk, hdr.ramdisk_size);
break; break;
case LZOP: case LZOP:
sprintf(name, "%s.%s", RAMDISK_FILE, "lzo"); sprintf(name, "%s.%s", RAMDISK_FILE, "lzo");
break; break;
case XZ: case XZ:
sprintf(name, "%s.%s", RAMDISK_FILE, "xz"); sprintf(name, "%s.%s", RAMDISK_FILE, "xz");
lzma(1, RAMDISK_FILE, ramdisk, hdr.ramdisk_size); lzma(0, RAMDISK_FILE, ramdisk, hdr.ramdisk_size);
break; break;
case LZMA: case LZMA:
sprintf(name, "%s.%s", RAMDISK_FILE, "lzma"); sprintf(name, "%s.%s", RAMDISK_FILE, "lzma");
lzma(0, RAMDISK_FILE, ramdisk, hdr.ramdisk_size);
break; break;
case BZIP2: case BZIP2:
sprintf(name, "%s.%s", RAMDISK_FILE, "bz2"); sprintf(name, "%s.%s", RAMDISK_FILE, "bz2");
@ -77,7 +78,7 @@ void unpack(const char* image) {
if (hdr.dt_size) { if (hdr.dt_size) {
if (boot_type == ELF && (dtb_type != QCDT && dtb_type != ELF )) { if (boot_type == ELF && (dtb_type != QCDT && dtb_type != ELF )) {
printf("Non QC dtb found in ELF kernel, recreate kernel\n"); printf("Non QC dtb found in ELF kernel, recreate kernel\n");
gzip(0, KERNEL_FILE, kernel, hdr.kernel_size); gzip(1, KERNEL_FILE, kernel, hdr.kernel_size);
int kfp = open(KERNEL_FILE, O_WRONLY | O_APPEND); int kfp = open(KERNEL_FILE, O_WRONLY | O_APPEND);
write(kfp, dtb, hdr.dt_size); write(kfp, dtb, hdr.dt_size);
close(kfp); close(kfp);