Well, /dev/zero cannot be used like that...

This commit is contained in:
topjohnwu 2017-03-13 05:05:51 +08:00
parent 1442e29d0e
commit 7877ac0c3b
3 changed files with 11 additions and 13 deletions

View File

@ -94,7 +94,6 @@ extern unsigned char *kernel, *ramdisk, *second, *dtb, *extra;
extern boot_img_hdr hdr; extern boot_img_hdr hdr;
extern file_t boot_type, ramdisk_type, dtb_type; extern file_t boot_type, ramdisk_type, dtb_type;
extern int mtk_kernel, mtk_ramdisk; extern int mtk_kernel, mtk_ramdisk;
extern int zero;
// Main entries // Main entries
void unpack(const char *image); void unpack(const char *image);
@ -119,6 +118,7 @@ void decomp_file(char *from, const char *to);
void mmap_ro(const char *filename, unsigned char **buf, size_t *size); void mmap_ro(const char *filename, unsigned char **buf, size_t *size);
void mmap_rw(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); file_t check_type(const unsigned char *buf);
void write_zero(int fd, size_t size);
void mem_align(size_t *pos, size_t align); void mem_align(size_t *pos, size_t align);
void file_align(int fd, size_t align, int out); void file_align(int fd, size_t align, int out);
int open_new(const char *filename); int open_new(const char *filename);

View File

@ -21,9 +21,6 @@ static void restore_buf(int fd, const void *buf, size_t size) {
} }
void repack(const char* orig_image, const char* out_image) { void repack(const char* orig_image, const char* out_image) {
zero = open("/dev/zero", O_RDONLY);
if (zero < 0) error(1, "Cannot open /dev/zero");
size_t size; size_t size;
unsigned char *orig; unsigned char *orig;
char name[PATH_MAX]; char name[PATH_MAX];
@ -51,12 +48,12 @@ void repack(const char* orig_image, const char* out_image) {
hdr.dt_size = 0; hdr.dt_size = 0;
// Skip a page for header // Skip a page for header
sendfile(fd, zero, NULL, hdr.page_size); write_zero(fd, hdr.page_size);
// Restore kernel // Restore kernel
if (mtk_kernel) { if (mtk_kernel) {
mtk_kernel_off = lseek(fd, 0, SEEK_CUR); mtk_kernel_off = lseek(fd, 0, SEEK_CUR);
sendfile(fd, zero, NULL, 512); write_zero(fd, 512);
memcpy(&mtk_kernel_hdr, kernel, sizeof(mtk_kernel_hdr)); memcpy(&mtk_kernel_hdr, kernel, sizeof(mtk_kernel_hdr));
} }
hdr.kernel_size = restore(KERNEL_FILE, fd); hdr.kernel_size = restore(KERNEL_FILE, fd);
@ -65,7 +62,7 @@ void repack(const char* orig_image, const char* out_image) {
// Restore ramdisk // Restore ramdisk
if (mtk_ramdisk) { if (mtk_ramdisk) {
mtk_ramdisk_off = lseek(fd, 0, SEEK_CUR); mtk_ramdisk_off = lseek(fd, 0, SEEK_CUR);
sendfile(fd, zero, NULL, 512); write_zero(fd, 512);
memcpy(&mtk_ramdisk_hdr, ramdisk, sizeof(mtk_ramdisk_hdr)); memcpy(&mtk_ramdisk_hdr, ramdisk, sizeof(mtk_ramdisk_hdr));
} }
if (access(RAMDISK_FILE, R_OK) == 0) { if (access(RAMDISK_FILE, R_OK) == 0) {

View File

@ -3,7 +3,6 @@
char *SUP_EXT_LIST[SUP_NUM] = { "gz", "xz", "lzma", "bz2", "lz4" }; char *SUP_EXT_LIST[SUP_NUM] = { "gz", "xz", "lzma", "bz2", "lz4" };
file_t SUP_TYPE_LIST[SUP_NUM] = { GZIP, XZ, LZMA, BZIP2, LZ4 }; file_t SUP_TYPE_LIST[SUP_NUM] = { GZIP, XZ, LZMA, BZIP2, LZ4 };
int zero = -1;
void mmap_ro(const char *filename, unsigned char **buf, size_t *size) { void mmap_ro(const char *filename, unsigned char **buf, size_t *size) {
int fd = open(filename, O_RDONLY); int fd = open(filename, O_RDONLY);
@ -56,6 +55,12 @@ file_t check_type(const unsigned char *buf) {
} }
} }
void write_zero(int fd, size_t size) {
size_t pos = lseek(fd, 0, SEEK_CUR);
ftruncate(fd, pos + size);
lseek(fd, pos + size, SEEK_SET);
}
void mem_align(size_t *pos, size_t align) { void mem_align(size_t *pos, size_t align) {
size_t mask = align - 1; size_t mask = align - 1;
if (*pos & mask) { if (*pos & mask) {
@ -70,11 +75,7 @@ void file_align(int fd, size_t align, int out) {
if (pos & mask) { if (pos & mask) {
off = align - (pos & mask); off = align - (pos & mask);
if (out) { if (out) {
if (zero < 0) { write_zero(fd, off);
zero = open("/dev/zero", O_RDONLY);
if (zero < 0) error(1, "Cannot open /dev/zero");
}
sendfile(fd, zero, NULL, off);
} else { } else {
lseek(fd, pos + off, SEEK_SET); lseek(fd, pos + off, SEEK_SET);
} }