2017-03-02 04:12:47 +08:00
|
|
|
#ifndef _MAGISKBOOT_H_
|
|
|
|
#define _MAGISKBOOT_H_
|
2017-02-28 05:37:47 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-03-08 00:54:23 +08:00
|
|
|
#include <stdarg.h>
|
2017-02-28 05:37:47 +08:00
|
|
|
#include <unistd.h>
|
2017-03-08 00:54:23 +08:00
|
|
|
#include <ctype.h>
|
2017-02-28 05:37:47 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/sendfile.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "bootimg.h"
|
2017-03-10 04:08:17 +08:00
|
|
|
#include "sha1.h"
|
2017-02-28 05:37:47 +08:00
|
|
|
|
|
|
|
#define windowBits 15
|
|
|
|
#define ZLIB_GZIP 16
|
|
|
|
#define memLevel 8
|
|
|
|
#define CHUNK 0x40000
|
|
|
|
|
2017-03-02 04:12:47 +08:00
|
|
|
#define LZ4_HEADER_SIZE 19
|
|
|
|
#define LZ4_FOOTER_SIZE 4
|
|
|
|
|
2017-02-28 05:37:47 +08:00
|
|
|
#define CHROMEOS_MAGIC "CHROMEOS"
|
|
|
|
#define CHROMEOS_MAGIC_SIZE 8
|
|
|
|
|
|
|
|
#define KERNEL_FILE "kernel"
|
|
|
|
#define RAMDISK_FILE "ramdisk.cpio"
|
|
|
|
#define SECOND_FILE "second"
|
|
|
|
#define DTB_FILE "dtb"
|
2017-03-05 01:50:36 +08:00
|
|
|
#define NEW_BOOT "new-boot.img"
|
|
|
|
|
|
|
|
#define SUP_LIST "gzip, xz, lzma, lz4, bzip2"
|
|
|
|
#define SUP_NUM 5
|
2017-02-28 05:37:47 +08:00
|
|
|
|
|
|
|
typedef enum {
|
2017-03-02 21:59:37 +08:00
|
|
|
UNKNOWN,
|
2017-02-28 05:37:47 +08:00
|
|
|
CHROMEOS,
|
|
|
|
AOSP,
|
|
|
|
ELF,
|
|
|
|
GZIP,
|
|
|
|
LZOP,
|
|
|
|
XZ,
|
|
|
|
LZMA,
|
|
|
|
BZIP2,
|
|
|
|
LZ4,
|
2017-03-02 21:59:37 +08:00
|
|
|
MTK,
|
2017-02-28 05:37:47 +08:00
|
|
|
QCDT,
|
|
|
|
} file_t;
|
|
|
|
|
2017-03-08 00:54:23 +08:00
|
|
|
typedef enum {
|
|
|
|
NONE,
|
|
|
|
RM,
|
|
|
|
MKDIR,
|
2017-03-10 04:08:17 +08:00
|
|
|
ADD,
|
|
|
|
EXTRACT,
|
|
|
|
TEST,
|
|
|
|
DMVERITY,
|
2017-03-10 07:52:59 +08:00
|
|
|
FORCEENCRYPT,
|
|
|
|
BACKUP,
|
|
|
|
RESTORE
|
2017-03-08 00:54:23 +08:00
|
|
|
} command_t;
|
|
|
|
|
2017-03-05 01:50:36 +08:00
|
|
|
extern char *SUP_EXT_LIST[SUP_NUM];
|
|
|
|
extern file_t SUP_TYPE_LIST[SUP_NUM];
|
|
|
|
// Cannot declare in header, but place a copy here for convenience
|
|
|
|
// char *SUP_EXT_LIST[SUP_NUM] = { "gz", "xz", "lzma", "bz2", "lz4" };
|
|
|
|
// file_t SUP_TYPE_LIST[SUP_NUM] = { GZIP, XZ, LZMA, BZIP2, LZ4 };
|
|
|
|
|
2017-03-08 00:54:23 +08:00
|
|
|
// Vector
|
|
|
|
typedef struct vector {
|
|
|
|
size_t size;
|
|
|
|
size_t cap;
|
|
|
|
void **data;
|
|
|
|
} vector;
|
|
|
|
void vec_init(vector *v);
|
|
|
|
void vec_push_back(vector *v, void *p);
|
2017-03-10 04:08:17 +08:00
|
|
|
void vec_sort(vector *v, int (*compar)(const void *, const void *));
|
2017-03-08 00:54:23 +08:00
|
|
|
void vec_destroy(vector *v);
|
|
|
|
|
|
|
|
#define vec_size(v) (v)->size
|
|
|
|
#define vec_cap(v) (v)->cap
|
|
|
|
#define vec_entry(v) (v)->data
|
|
|
|
// vec_for_each(vector *v, void *e)
|
|
|
|
#define vec_for_each(v, e) \
|
|
|
|
e = (v)->data[0]; \
|
|
|
|
for (size_t _i = 0; _i < (v)->size; ++_i, e = (v)->data[_i])
|
|
|
|
|
2017-02-28 05:37:47 +08:00
|
|
|
// Global variables
|
2017-03-12 18:12:16 +08:00
|
|
|
extern unsigned char *kernel, *ramdisk, *second, *dtb, *extra;
|
2017-02-28 05:37:47 +08:00
|
|
|
extern boot_img_hdr hdr;
|
|
|
|
extern file_t boot_type, ramdisk_type, dtb_type;
|
|
|
|
extern int mtk_kernel, mtk_ramdisk;
|
|
|
|
|
|
|
|
// Main entries
|
|
|
|
void unpack(const char *image);
|
2017-03-08 00:54:23 +08:00
|
|
|
void repack(const char* orig_image, const char* out_image);
|
2017-03-04 21:16:59 +08:00
|
|
|
void hexpatch(const char *image, const char *from, const char *to);
|
2017-02-28 05:37:47 +08:00
|
|
|
void error(int rc, const char *msg, ...);
|
|
|
|
void parse_img(unsigned char *orig, size_t size);
|
2017-03-10 04:08:17 +08:00
|
|
|
int cpio_commands(const char *command, int argc, char *argv[]);
|
2017-03-05 01:50:36 +08:00
|
|
|
void cleanup();
|
2017-02-28 05:37:47 +08:00
|
|
|
|
|
|
|
// Compressions
|
2017-03-04 21:16:59 +08:00
|
|
|
void gzip(int mode, const char* filename, const unsigned char* buf, size_t size);
|
|
|
|
void lzma(int mode, const char* filename, const unsigned char* buf, size_t size);
|
|
|
|
void lz4(int mode, const char* filename, const unsigned char* buf, size_t size);
|
|
|
|
void bzip2(int mode, const char* filename, const unsigned char* buf, size_t size);
|
|
|
|
int comp(file_t type, const char *to, const unsigned char *from, size_t size);
|
2017-03-12 18:12:16 +08:00
|
|
|
void comp_file(const char *method, const char *from, const char *to);
|
2017-03-04 21:16:59 +08:00
|
|
|
int decomp(file_t type, const char *to, const unsigned char *from, size_t size);
|
2017-03-12 18:12:16 +08:00
|
|
|
void decomp_file(char *from, const char *to);
|
2017-03-04 21:16:59 +08:00
|
|
|
|
|
|
|
// Utils
|
|
|
|
void mmap_ro(const char *filename, unsigned char **buf, size_t *size);
|
|
|
|
void mmap_rw(const char *filename, unsigned char **buf, size_t *size);
|
|
|
|
file_t check_type(const unsigned char *buf);
|
2017-03-13 05:05:51 +08:00
|
|
|
void write_zero(int fd, size_t size);
|
2017-03-04 21:16:59 +08:00
|
|
|
void mem_align(size_t *pos, size_t align);
|
2017-03-08 00:54:23 +08:00
|
|
|
void file_align(int fd, size_t align, int out);
|
2017-03-04 21:16:59 +08:00
|
|
|
int open_new(const char *filename);
|
2017-03-05 01:50:36 +08:00
|
|
|
void print_info();
|
2017-02-28 05:37:47 +08:00
|
|
|
|
|
|
|
#endif
|