1
0
Fork 0
mirror of https://gitlab.com/bztsrc/bootboot.git synced 2023-02-13 20:54:32 -05:00

Compressed built-in payloads

This commit is contained in:
bzt 2021-03-19 21:25:52 +01:00
parent 07d54a561a
commit d3766199d3
5 changed files with 61 additions and 33 deletions

View file

@ -32,11 +32,12 @@
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "zlib.c"
int main(int argc, char **argv)
{
FILE *f, *c, *h;
long size;
unsigned long int size, len;
unsigned char *buff = NULL, *buff2 = NULL;
char *fn, name[255];
int i, file;
@ -70,9 +71,19 @@ int main(int argc, char **argv)
for(i = 0; fn[i]; i++)
name[i] = fn[i] == '.' || fn[i] <= ' ' ? '_' : fn[i];
name[i] = 0;
fprintf(h, "extern unsigned char binary_%s[%ld];\n", name, size);
fprintf(c, "unsigned char binary_%s[%ld] = { ", name, size);
for(i = 0; i < size; i++)
if(size > 512) {
len = compressBound(size);
buff2 = (unsigned char*)malloc(len);
if(!buff2) {
fprintf(stderr, "bin2h: memory allocation error\r\n");
exit(2);
}
compress2(buff2, &len, buff, size, 9);
free(buff); buff = buff2;
} else len = size;
fprintf(h, "#define sizeof_%s %ld\nextern unsigned char binary_%s[%ld];\n", name, size, name, len);
fprintf(c, "unsigned char binary_%s[%ld] = { ", name, len);
for(i = 0; i < len; i++)
fprintf(c,"%s%d", i?",":"", buff[i]);
fprintf(c," };\n");
free(buff);

File diff suppressed because one or more lines are too long

View file

@ -1,11 +1,20 @@
/* generated by bin2h, do not edit */
#define sizeof_boot_bin 512
extern unsigned char binary_boot_bin[512];
extern unsigned char binary_bootboot_bin[13312];
extern unsigned char binary_bootboot_efi[103614];
extern unsigned char binary_bootboot_img[34888];
extern unsigned char binary_bootboot_rv64[8192];
extern unsigned char binary_LICENCE_broadcom[1594];
extern unsigned char binary_bootcode_bin[52456];
extern unsigned char binary_fixup_dat[7297];
extern unsigned char binary_start_elf[2939744];
#define sizeof_bootboot_bin 13312
extern unsigned char binary_bootboot_bin[9285];
#define sizeof_bootboot_efi 103614
extern unsigned char binary_bootboot_efi[46258];
#define sizeof_bootboot_img 35488
extern unsigned char binary_bootboot_img[20151];
#define sizeof_bootboot_rv64 8192
extern unsigned char binary_bootboot_rv64[31];
#define sizeof_LICENCE_broadcom 1594
extern unsigned char binary_LICENCE_broadcom[883];
#define sizeof_bootcode_bin 52456
extern unsigned char binary_bootcode_bin[30329];
#define sizeof_fixup_dat 7297
extern unsigned char binary_fixup_dat[1281];
#define sizeof_start_elf 2939744
extern unsigned char binary_start_elf[1717161];

View file

@ -98,6 +98,19 @@ unsigned char *esp_addfile(unsigned char *ptr, char *name, unsigned char *conten
return ptr;
}
/**
* Add a compressed file to the boot partition
*/
unsigned char *esp_addzfile(unsigned char *ptr, char *name, unsigned char *content, int size, unsigned long int len)
{
unsigned char *buf = malloc(len);
if(!buf) { fprintf(stderr,"mkbootimg: %s\r\n",lang[ERR_MEM]); exit(1); }
if(uncompress(buf,&len,content,size) == Z_OK && len > 0)
ptr = esp_addfile(ptr, name, buf, len);
free(buf);
return ptr;
}
/**
* Create EFI System Partition with FAT16 or FAT32
*/
@ -190,7 +203,7 @@ void esp_makepart()
/*** Risc-V 64 Microchip Icicle ***/
/* start and end address has to be added to the GPT too in a special partition */
bbp_start = ((data + nextcluster * bpc)-esp) / 512;
rootdir = esp_addfile(rootdir, "PAYLOAD.BIN", binary_bootboot_rv64, sizeof(binary_bootboot_rv64));
rootdir = esp_addzfile(rootdir, "PAYLOAD.BIN", binary_bootboot_rv64, sizeof(binary_bootboot_rv64), sizeof_bootboot_rv64);
bbp_end = (((data + nextcluster * bpc)-esp) / 512) - 1;
}
if(boot & (1 << 1)) {
@ -198,19 +211,19 @@ void esp_makepart()
/* start address has to be saved in PMBR too */
esp_bbs = ((data + nextcluster * bpc)-esp) / 512;
memcpy(esp + 0x1B0, &esp_bbs, 4);
rootdir = esp_addfile(rootdir, "BOOTBOOT.BIN", binary_bootboot_bin, sizeof(binary_bootboot_bin));
rootdir = esp_addzfile(rootdir, "BOOTBOOT.BIN", binary_bootboot_bin, sizeof(binary_bootboot_bin), sizeof_bootboot_bin);
/*** x86 PC (UEFI) ***/
ptr = esp_mkdir(rootdir, "EFI", 0); rootdir += 32;
ptr = esp_mkdir(ptr, "BOOT", lastcluster);
ptr = esp_addfile(ptr, "BOOTX64.EFI", binary_bootboot_efi, sizeof(binary_bootboot_efi));
ptr = esp_addzfile(ptr, "BOOTX64.EFI", binary_bootboot_efi, sizeof(binary_bootboot_efi), sizeof_bootboot_efi);
}
if(boot & (1 << 0)) {
/*** Raspberry Pi ***/
ptr = esp_addfile(rootdir, "KERNEL8.IMG", binary_bootboot_img, sizeof(binary_bootboot_img));
ptr = esp_addfile(ptr, "BOOTCODE.BIN", binary_bootcode_bin, sizeof(binary_bootcode_bin));
ptr = esp_addfile(ptr, "FIXUP.DAT", binary_fixup_dat, sizeof(binary_fixup_dat));
ptr = esp_addfile(ptr, "START.ELF", binary_start_elf, sizeof(binary_start_elf));
ptr = esp_addfile(ptr, "LICENCE.BCM", binary_LICENCE_broadcom, sizeof(binary_start_elf));
ptr = esp_addzfile(rootdir, "KERNEL8.IMG", binary_bootboot_img, sizeof(binary_bootboot_img), sizeof_bootboot_img);
ptr = esp_addzfile(ptr, "BOOTCODE.BIN", binary_bootcode_bin, sizeof(binary_bootcode_bin), sizeof_bootcode_bin);
ptr = esp_addzfile(ptr, "FIXUP.DAT", binary_fixup_dat, sizeof(binary_fixup_dat), sizeof_fixup_dat);
ptr = esp_addzfile(ptr, "START.ELF", binary_start_elf, sizeof(binary_start_elf), sizeof_start_elf);
ptr = esp_addzfile(ptr, "LICENCE.BCM", binary_LICENCE_broadcom, sizeof(binary_LICENCE_broadcom), sizeof_LICENCE_broadcom);
}
/* update fields in FS Information Sector */
if(boot_fat == 32) {

View file

@ -374,9 +374,4 @@ void mnx_add(struct stat *st, char *name, unsigned char *content, int size)
void mnx_close()
{
FILE *f;
if(!fs_base || fs_len < 2048) return;
f = fopen("test.bin", "w");
fwrite(fs_base, fs_len, 1, f);
fclose(f);
}