Small refactor of magiskboot

This commit is contained in:
topjohnwu 2018-01-29 03:12:35 +08:00
parent eed86c760f
commit dfee9954e0
7 changed files with 37 additions and 56 deletions

View File

@ -102,7 +102,7 @@ LOCAL_SRC_FILES := \
magiskboot/bootimg.c \
magiskboot/hexpatch.c \
magiskboot/compress.c \
magiskboot/types.c \
magiskboot/format.c \
magiskboot/dtb.c \
magiskboot/ramdisk.c \
$(UTIL_SRC)

View File

@ -87,7 +87,7 @@ int parse_img(const char *image, boot_img *boot) {
for (void *head = boot->map_addr; head < boot->map_addr + boot->map_size; head += 256) {
size_t pos = 0;
switch (check_type(head)) {
switch (check_fmt(head)) {
case CHROMEOS:
// The caller should know it's chromeos, as it needs additional signing
boot->flags |= CHROMEOS_FLAG;
@ -147,11 +147,11 @@ int parse_img(const char *image, boot_img *boot) {
}
}
boot->kernel_type = check_type(boot->kernel);
boot->ramdisk_type = check_type(boot->ramdisk);
boot->k_fmt = check_fmt(boot->kernel);
boot->r_fmt = check_fmt(boot->ramdisk);
// Check MTK
if (boot->kernel_type == MTK) {
if (boot->k_fmt == MTK) {
fprintf(stderr, "MTK_KERNEL_HDR\n");
boot->flags |= MTK_KERNEL;
boot->k_hdr = malloc(sizeof(mtk_hdr));
@ -160,9 +160,9 @@ int parse_img(const char *image, boot_img *boot) {
fprintf(stderr, "NAME [%s]\n", boot->k_hdr->name);
boot->kernel += 512;
lheader(boot, kernel_size, -= 512);
boot->kernel_type = check_type(boot->kernel);
boot->k_fmt = check_fmt(boot->kernel);
}
if (boot->ramdisk_type == MTK) {
if (boot->r_fmt == MTK) {
fprintf(stderr, "MTK_RAMDISK_HDR\n");
boot->flags |= MTK_RAMDISK;
boot->r_hdr = malloc(sizeof(mtk_hdr));
@ -171,14 +171,13 @@ int parse_img(const char *image, boot_img *boot) {
fprintf(stderr, "NAME [%s]\n", boot->r_hdr->name);
boot->ramdisk += 512;
lheader(boot, ramdisk_size, -= 512);
boot->ramdisk_type = check_type(boot->ramdisk);
boot->r_fmt = check_fmt(boot->ramdisk);
}
char fmt[16];
get_type_name(boot->kernel_type, fmt);
get_fmt_name(boot->k_fmt, fmt);
fprintf(stderr, "KERNEL_FMT [%s]\n", fmt);
get_type_name(boot->ramdisk_type, fmt);
get_fmt_name(boot->r_fmt, fmt);
fprintf(stderr, "RAMDISK_FMT [%s]\n", fmt);
return boot->flags & CHROMEOS_FLAG ? CHROMEOS_RET :
@ -196,9 +195,9 @@ int unpack(const char *image) {
int fd;
// Dump kernel
if (COMPRESSED(boot.kernel_type)) {
if (COMPRESSED(boot.k_fmt)) {
fd = creat(KERNEL_FILE, 0644);
decomp(boot.kernel_type, fd, boot.kernel, header(&boot, kernel_size));
decomp(boot.k_fmt, fd, boot.kernel, header(&boot, kernel_size));
close(fd);
} else {
dump(boot.kernel, header(&boot, kernel_size), KERNEL_FILE);
@ -210,9 +209,9 @@ int unpack(const char *image) {
}
// Dump ramdisk
if (COMPRESSED(boot.ramdisk_type)) {
if (COMPRESSED(boot.r_fmt)) {
fd = creat(RAMDISK_FILE, 0644);
decomp(boot.ramdisk_type, fd, boot.ramdisk, header(&boot, ramdisk_size));
decomp(boot.r_fmt, fd, boot.ramdisk, header(&boot, ramdisk_size));
close(fd);
} else {
dump(boot.ramdisk, header(&boot, ramdisk_size), RAMDISK_FILE ".raw");
@ -255,11 +254,11 @@ void repack(const char* orig_image, const char* out_image) {
mtk_kernel_off = lseek(fd, 0, SEEK_CUR);
write_zero(fd, 512);
}
if (COMPRESSED(boot.kernel_type)) {
if (COMPRESSED(boot.k_fmt)) {
size_t raw_size;
void *kernel_raw;
mmap_ro(KERNEL_FILE, &kernel_raw, &raw_size);
lheader(&boot, kernel_size, = comp(boot.kernel_type, fd, kernel_raw, raw_size));
lheader(&boot, kernel_size, = comp(boot.k_fmt, fd, kernel_raw, raw_size));
munmap(kernel_raw, raw_size);
} else {
lheader(&boot, kernel_size, = restore(KERNEL_FILE, fd));
@ -280,7 +279,7 @@ void repack(const char* orig_image, const char* out_image) {
size_t cpio_size;
void *cpio;
mmap_ro(RAMDISK_FILE, &cpio, &cpio_size);
lheader(&boot, ramdisk_size, = comp(boot.ramdisk_type, fd, cpio, cpio_size));
lheader(&boot, ramdisk_size, = comp(boot.r_fmt, fd, cpio, cpio_size));
munmap(cpio, cpio_size);
} else {
// Find compressed ramdisk

View File

@ -1,28 +1,9 @@
/* tools/mkbootimg/bootimg.h
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include <stdint.h>
#include "types.h"
#include "format.h"
#ifndef _BOOT_IMAGE_H_
#define _BOOT_IMAGE_H_
#define BOOT_MAGIC "ANDROID!"
typedef struct boot_img_hdr {
char magic[8];
@ -141,8 +122,8 @@ typedef struct boot_img {
uint8_t flags;
// The format of kernel and ramdisk
file_t kernel_type;
file_t ramdisk_type;
format_t k_fmt;
format_t r_fmt;
// Pointer to dtb that is appended after kernel
void *dtb;

View File

@ -343,7 +343,7 @@ done:
return total;
}
long long decomp(file_t type, int to, const void *from, size_t size) {
long long decomp(format_t type, int to, const void *from, size_t size) {
switch (type) {
case GZIP:
return gzip(0, to, from, size);
@ -363,7 +363,7 @@ long long decomp(file_t type, int to, const void *from, size_t size) {
}
}
long long comp(file_t type, int to, const void *from, size_t size) {
long long comp(format_t type, int to, const void *from, size_t size) {
switch (type) {
case GZIP:
return gzip(1, to, from, size);
@ -395,7 +395,7 @@ void decomp_file(char *from, const char *to) {
stream_full_read(STDIN_FILENO, &file, &size);
else
mmap_ro(from, &file, &size);
file_t type = check_type(file);
format_t type = check_fmt(file);
char *ext;
ext = strrchr(from, '.');
if (to == NULL)
@ -453,7 +453,7 @@ void decomp_file(char *from, const char *to) {
}
void comp_file(const char *method, const char *from, const char *to) {
file_t type;
format_t type;
char *ext, dest[PATH_MAX];
if (strcmp(method, "gzip") == 0) {
type = GZIP;

View File

@ -1,9 +1,9 @@
#include <string.h>
#include "bootimg.h"
#include "types.h"
#include "format.h"
file_t check_type(const void *buf) {
format_t check_fmt(const void *buf) {
if (memcmp(buf, CHROMEOS_MAGIC, 8) == 0) {
return CHROMEOS;
} else if (memcmp(buf, BOOT_MAGIC, 8) == 0) {
@ -36,9 +36,9 @@ file_t check_type(const void *buf) {
}
}
void get_type_name(file_t type, char *name) {
void get_fmt_name(format_t fmt, char *name) {
char *s;
switch (type) {
switch (fmt) {
case CHROMEOS:
s = "chromeos";
break;

View File

@ -1,5 +1,5 @@
#ifndef _TYPES_H_
#define _TYPES_H_
#ifndef _FORMAT_H_
#define _FORMAT_H_
typedef enum {
UNKNOWN,
@ -16,10 +16,11 @@ typedef enum {
LZ4_LEGACY,
MTK,
DTB
} file_t;
} format_t;
#define COMPRESSED(type) (type >= GZIP && type <= LZ4_LEGACY)
#define COMPRESSED(fmt) (fmt >= GZIP && fmt <= LZ4_LEGACY)
#define BOOT_MAGIC "ANDROID!"
#define CHROMEOS_MAGIC "CHROMEOS"
#define ELF32_MAGIC "\x7f""ELF\x01"
#define ELF64_MAGIC "\x7f""ELF\x02"
@ -36,7 +37,7 @@ typedef enum {
#define SUP_LIST ((char *[]) { "gzip", "xz", "lzma", "bzip2", "lz4", "lz4_legacy", NULL })
#define SUP_EXT_LIST ((char *[]) { "gz", "xz", "lzma", "bz2", "lz4", "lz4", NULL })
file_t check_type(const void *buf);
void get_type_name(file_t type, char *name);
format_t check_fmt(const void *buf);
void get_fmt_name(format_t fmt, char *name);
#endif

View File

@ -29,7 +29,7 @@ size_t lzma(int mode, int fd, const void *buf, size_t size);
size_t lz4(int mode, int fd, const void *buf, size_t size);
size_t bzip2(int mode, int fd, const void *buf, size_t size);
size_t lz4_legacy(int mode, int fd, const void *buf, size_t size);
long long comp(file_t type, int to, const void *from, size_t size);
long long decomp(file_t type, int to, const void *from, size_t size);
long long comp(format_t type, int to, const void *from, size_t size);
long long decomp(format_t type, int to, const void *from, size_t size);
#endif