main.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * mkbootimg/main.c
  3. *
  4. * Copyright (C) 2017 - 2021 bzt (bztsrc@gitlab)
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * This file is part of the BOOTBOOT Protocol package.
  27. * @brief Bootable image creator main header
  28. *
  29. */
  30. #include <stdint.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <dirent.h>
  35. #include <time.h>
  36. #include <sys/stat.h>
  37. #include "lang.h"
  38. #include "zlib.h"
  39. #define NUMARCH 3
  40. #define MAXPATH 1024
  41. #ifndef S_ISLNK
  42. #define S_ISLNK(x) (0)
  43. #endif
  44. /*** ELF64 defines and structs ***/
  45. #define ELFMAG "\177ELF"
  46. #define SELFMAG 4
  47. #define EI_CLASS 4 /* File class byte index */
  48. #define ELFCLASS64 2 /* 64-bit objects */
  49. #define EI_DATA 5 /* Data encoding byte index */
  50. #define ELFDATA2LSB 1 /* 2's complement, little endian */
  51. #define PT_LOAD 1 /* Loadable program segment */
  52. #define EM_X86_64 62 /* AMD x86-64 architecture */
  53. #define EM_AARCH64 183 /* ARM aarch64 architecture */
  54. #define EM_RISCV 243 /* RISC-V */
  55. typedef struct
  56. {
  57. unsigned char e_ident[16];/* Magic number and other info */
  58. uint16_t e_type; /* Object file type */
  59. uint16_t e_machine; /* Architecture */
  60. uint32_t e_version; /* Object file version */
  61. uint64_t e_entry; /* Entry point virtual address */
  62. uint64_t e_phoff; /* Program header table file offset */
  63. uint64_t e_shoff; /* Section header table file offset */
  64. uint32_t e_flags; /* Processor-specific flags */
  65. uint16_t e_ehsize; /* ELF header size in bytes */
  66. uint16_t e_phentsize; /* Program header table entry size */
  67. uint16_t e_phnum; /* Program header table entry count */
  68. uint16_t e_shentsize; /* Section header table entry size */
  69. uint16_t e_shnum; /* Section header table entry count */
  70. uint16_t e_shstrndx; /* Section header string table index */
  71. } Elf64_Ehdr;
  72. typedef struct
  73. {
  74. uint32_t p_type; /* Segment type */
  75. uint32_t p_flags; /* Segment flags */
  76. uint64_t p_offset; /* Segment file offset */
  77. uint64_t p_vaddr; /* Segment virtual address */
  78. uint64_t p_paddr; /* Segment physical address */
  79. uint64_t p_filesz; /* Segment size in file */
  80. uint64_t p_memsz; /* Segment size in memory */
  81. uint64_t p_align; /* Segment alignment */
  82. } Elf64_Phdr;
  83. typedef struct
  84. {
  85. uint32_t sh_name; /* Section name (string tbl index) */
  86. uint32_t sh_type; /* Section type */
  87. uint64_t sh_flags; /* Section flags */
  88. uint64_t sh_addr; /* Section virtual addr at execution */
  89. uint64_t sh_offset; /* Section file offset */
  90. uint64_t sh_size; /* Section size in bytes */
  91. uint32_t sh_link; /* Link to another section */
  92. uint32_t sh_info; /* Additional section information */
  93. uint64_t sh_addralign; /* Section alignment */
  94. uint64_t sh_entsize; /* Entry size if section holds table */
  95. } Elf64_Shdr;
  96. typedef struct
  97. {
  98. uint32_t st_name; /* Symbol name (string tbl index) */
  99. uint8_t st_info; /* Symbol type and binding */
  100. uint8_t st_other; /* Symbol visibility */
  101. uint16_t st_shndx; /* Section index */
  102. uint64_t st_value; /* Symbol value */
  103. uint64_t st_size; /* Symbol size */
  104. } Elf64_Sym;
  105. /*** PE32+ defines and structs ***/
  106. #define MZ_MAGIC 0x5a4d /* "MZ" */
  107. #define PE_MAGIC 0x00004550 /* "PE\0\0" */
  108. #define IMAGE_FILE_MACHINE_AMD64 0x8664 /* AMD x86_64 architecture */
  109. #define IMAGE_FILE_MACHINE_ARM64 0xaa64 /* ARM aarch64 architecture */
  110. #define IMAGE_FILE_MACHINE_RISCV64 0x5064 /* RISC-V riscv64 architecture */
  111. #define PE_OPT_MAGIC_PE32PLUS 0x020b /* PE32+ format */
  112. typedef struct
  113. {
  114. uint16_t magic; /* MZ magic */
  115. uint16_t reserved[29]; /* reserved */
  116. uint32_t peaddr; /* address of pe header */
  117. } mz_hdr;
  118. typedef struct {
  119. uint32_t magic; /* PE magic */
  120. uint16_t machine; /* machine type */
  121. uint16_t sections; /* number of sections */
  122. uint32_t timestamp; /* time_t */
  123. uint32_t sym_table; /* symbol table offset */
  124. uint32_t numsym; /* number of symbols */
  125. uint16_t opt_hdr_size; /* size of optional header */
  126. uint16_t flags; /* flags */
  127. uint16_t file_type; /* file type, PE32PLUS magic */
  128. uint8_t ld_major; /* linker major version */
  129. uint8_t ld_minor; /* linker minor version */
  130. uint32_t text_size; /* size of text section(s) */
  131. uint32_t data_size; /* size of data section(s) */
  132. uint32_t bss_size; /* size of bss section(s) */
  133. int32_t entry_point; /* file offset of entry point */
  134. int32_t code_base; /* relative code addr in ram */
  135. } pe_hdr;
  136. typedef struct {
  137. uint32_t iszero; /* if this is not zero, then iszero+nameoffs gives UTF-8 string */
  138. uint32_t nameoffs;
  139. int32_t value; /* value of the symbol */
  140. uint16_t section; /* section it belongs to */
  141. uint16_t type; /* symbol type */
  142. uint8_t storclass; /* storage class */
  143. uint8_t auxsyms; /* number of pe_sym records following */
  144. } pe_sym;
  145. typedef struct {
  146. uint32_t Data1;
  147. uint16_t Data2;
  148. uint16_t Data3;
  149. uint8_t Data4[8];
  150. } __attribute__((packed)) guid_t;
  151. typedef struct {
  152. guid_t type;
  153. guid_t guid;
  154. uint64_t start;
  155. uint64_t last;
  156. uint64_t attrib;
  157. uint16_t name[36];
  158. } __attribute__((packed)) gpt_t;
  159. typedef void (*initrd_open)(gpt_t *gpt_entry);
  160. typedef void (*initrd_add)(struct stat *st, char *name, unsigned char *content, int size);
  161. typedef void (*initrd_close)();
  162. typedef struct {
  163. char *name;
  164. guid_t type;
  165. initrd_open open;
  166. initrd_add add;
  167. initrd_close close;
  168. } fsdrv_t;
  169. extern time_t t;
  170. extern struct tm *ts;
  171. extern guid_t diskguid;
  172. extern char *json, *config, *kernelname, *initrd_dir[NUMARCH], initrd_arch[NUMARCH];
  173. extern int fs_len, fs_no, initrd_size[NUMARCH], initrd_gzip, boot_size, boot_fat, disk_size, esp_size, esp_bbs;
  174. extern int iso9660, skipbytes, np, bbp_start, bbp_end;
  175. extern unsigned char *esp, *gpt, gpt2[512], *fs_base, *initrd_buf[NUMARCH];
  176. extern unsigned long int tsize, es, esiz, disk_align, gpt_parts[248];
  177. extern fsdrv_t fsdrv[];
  178. extern initrd_open rd_open;
  179. extern initrd_add rd_add;
  180. extern initrd_close rd_close;
  181. extern int64_t read_size;
  182. unsigned char* readfileall(char *file);
  183. unsigned int gethex(char *ptr, int len);
  184. void getguid(char *ptr, guid_t *guid);
  185. void parsedir(char *directory, int parent);
  186. void initrdcompress();
  187. void initrduncompress();
  188. char *json_get(const char *jsonstr, char *key);
  189. unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality);
  190. void esp_makepart();
  191. void gpt_maketable();
  192. void img_write(char *fn);
  193. uint32_t crc32_calc(unsigned char *start,int length);