mkimage.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  1. /* grub-mkimage.c - make a bootable image */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <grub/types.h>
  21. #include <grub/elf.h>
  22. #include <grub/aout.h>
  23. #include <grub/i18n.h>
  24. #include <grub/kernel.h>
  25. #include <grub/disk.h>
  26. #include <grub/emu/misc.h>
  27. #include <grub/util/misc.h>
  28. #include <grub/util/resolve.h>
  29. #include <grub/misc.h>
  30. #include <grub/offsets.h>
  31. #include <grub/crypto.h>
  32. #include <grub/dl.h>
  33. #include <time.h>
  34. #include <multiboot.h>
  35. #include <stdio.h>
  36. #include <unistd.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include <assert.h>
  40. #include <grub/efi/pe32.h>
  41. #include <grub/uboot/image.h>
  42. #include <grub/arm/reloc.h>
  43. #include <grub/arm64/reloc.h>
  44. #include <grub/ia64/reloc.h>
  45. #include <grub/osdep/hostfile.h>
  46. #include <grub/util/install.h>
  47. #include <grub/util/mkimage.h>
  48. #define ALIGN_ADDR(x) (ALIGN_UP((x), image_target->voidp_sizeof))
  49. #ifdef USE_LIBLZMA
  50. #include <lzma.h>
  51. #endif
  52. #pragma GCC diagnostic ignored "-Wcast-align"
  53. #define TARGET_NO_FIELD 0xffffffff
  54. /* use 2015-01-01T00:00:00+0000 as a stock timestamp */
  55. #define STABLE_EMBEDDING_TIMESTAMP 1420070400
  56. #define EFI32_HEADER_SIZE ALIGN_UP (GRUB_PE32_MSDOS_STUB_SIZE \
  57. + GRUB_PE32_SIGNATURE_SIZE \
  58. + sizeof (struct grub_pe32_coff_header) \
  59. + sizeof (struct grub_pe32_optional_header) \
  60. + 4 * sizeof (struct grub_pe32_section_table), \
  61. GRUB_PE32_FILE_ALIGNMENT)
  62. #define EFI64_HEADER_SIZE ALIGN_UP (GRUB_PE32_MSDOS_STUB_SIZE \
  63. + GRUB_PE32_SIGNATURE_SIZE \
  64. + sizeof (struct grub_pe32_coff_header) \
  65. + sizeof (struct grub_pe64_optional_header) \
  66. + 4 * sizeof (struct grub_pe32_section_table), \
  67. GRUB_PE32_FILE_ALIGNMENT)
  68. static const struct grub_install_image_target_desc image_targets[] =
  69. {
  70. {
  71. .dirname = "i386-coreboot",
  72. .names = { "i386-coreboot", NULL },
  73. .voidp_sizeof = 4,
  74. .bigendian = 0,
  75. .id = IMAGE_COREBOOT,
  76. .flags = PLATFORM_FLAGS_NONE,
  77. .total_module_size = TARGET_NO_FIELD,
  78. .decompressor_compressed_size = TARGET_NO_FIELD,
  79. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  80. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  81. .reloc_table_offset = TARGET_NO_FIELD,
  82. .section_align = 1,
  83. .vaddr_offset = 0,
  84. .link_addr = GRUB_KERNEL_I386_COREBOOT_LINK_ADDR,
  85. .elf_target = EM_386,
  86. .link_align = 4,
  87. .mod_gap = GRUB_KERNEL_I386_COREBOOT_MOD_GAP,
  88. .mod_align = GRUB_KERNEL_I386_COREBOOT_MOD_ALIGN
  89. },
  90. {
  91. .dirname = "i386-multiboot",
  92. .names = { "i386-multiboot", NULL},
  93. .voidp_sizeof = 4,
  94. .bigendian = 0,
  95. .id = IMAGE_COREBOOT,
  96. .flags = PLATFORM_FLAGS_NONE,
  97. .total_module_size = TARGET_NO_FIELD,
  98. .decompressor_compressed_size = TARGET_NO_FIELD,
  99. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  100. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  101. .section_align = 1,
  102. .vaddr_offset = 0,
  103. .link_addr = GRUB_KERNEL_I386_COREBOOT_LINK_ADDR,
  104. .elf_target = EM_386,
  105. .link_align = 4,
  106. .mod_gap = GRUB_KERNEL_I386_COREBOOT_MOD_GAP,
  107. .mod_align = GRUB_KERNEL_I386_COREBOOT_MOD_ALIGN
  108. },
  109. {
  110. .dirname = "i386-pc",
  111. .names = { "i386-pc", NULL },
  112. .voidp_sizeof = 4,
  113. .bigendian = 0,
  114. .id = IMAGE_I386_PC,
  115. .flags = PLATFORM_FLAGS_DECOMPRESSORS,
  116. .total_module_size = TARGET_NO_FIELD,
  117. .decompressor_compressed_size = GRUB_DECOMPRESSOR_I386_PC_COMPRESSED_SIZE,
  118. .decompressor_uncompressed_size = GRUB_DECOMPRESSOR_I386_PC_UNCOMPRESSED_SIZE,
  119. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  120. .section_align = 1,
  121. .vaddr_offset = 0,
  122. .link_addr = GRUB_KERNEL_I386_PC_LINK_ADDR,
  123. .default_compression = GRUB_COMPRESSION_LZMA
  124. },
  125. {
  126. .dirname = "i386-xen_pvh",
  127. .names = { "i386-xen_pvh", NULL },
  128. .voidp_sizeof = 4,
  129. .bigendian = 0,
  130. .id = IMAGE_XEN_PVH,
  131. .flags = PLATFORM_FLAGS_NONE,
  132. .total_module_size = TARGET_NO_FIELD,
  133. .decompressor_compressed_size = TARGET_NO_FIELD,
  134. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  135. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  136. .elf_target = EM_386,
  137. .section_align = 1,
  138. .vaddr_offset = 0,
  139. .link_addr = GRUB_KERNEL_I386_XEN_PVH_LINK_ADDR,
  140. .mod_align = GRUB_KERNEL_I386_XEN_PVH_MOD_ALIGN,
  141. .link_align = 4
  142. },
  143. {
  144. .dirname = "i386-pc",
  145. .names = { "i386-pc-pxe", NULL },
  146. .voidp_sizeof = 4,
  147. .bigendian = 0,
  148. .id = IMAGE_I386_PC_PXE,
  149. .flags = PLATFORM_FLAGS_DECOMPRESSORS,
  150. .total_module_size = TARGET_NO_FIELD,
  151. .decompressor_compressed_size = GRUB_DECOMPRESSOR_I386_PC_COMPRESSED_SIZE,
  152. .decompressor_uncompressed_size = GRUB_DECOMPRESSOR_I386_PC_UNCOMPRESSED_SIZE,
  153. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  154. .section_align = 1,
  155. .vaddr_offset = 0,
  156. .link_addr = GRUB_KERNEL_I386_PC_LINK_ADDR,
  157. .default_compression = GRUB_COMPRESSION_LZMA
  158. },
  159. {
  160. .dirname = "i386-pc",
  161. .names = { "i386-pc-eltorito", NULL },
  162. .voidp_sizeof = 4,
  163. .bigendian = 0,
  164. .id = IMAGE_I386_PC_ELTORITO,
  165. .flags = PLATFORM_FLAGS_DECOMPRESSORS,
  166. .total_module_size = TARGET_NO_FIELD,
  167. .decompressor_compressed_size = GRUB_DECOMPRESSOR_I386_PC_COMPRESSED_SIZE,
  168. .decompressor_uncompressed_size = GRUB_DECOMPRESSOR_I386_PC_UNCOMPRESSED_SIZE,
  169. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  170. .section_align = 1,
  171. .vaddr_offset = 0,
  172. .link_addr = GRUB_KERNEL_I386_PC_LINK_ADDR,
  173. .default_compression = GRUB_COMPRESSION_LZMA
  174. },
  175. {
  176. .dirname = "i386-efi",
  177. .names = { "i386-efi", NULL },
  178. .voidp_sizeof = 4,
  179. .bigendian = 0,
  180. .id = IMAGE_EFI,
  181. .flags = PLATFORM_FLAGS_NONE,
  182. .total_module_size = TARGET_NO_FIELD,
  183. .decompressor_compressed_size = TARGET_NO_FIELD,
  184. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  185. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  186. .section_align = GRUB_PE32_SECTION_ALIGNMENT,
  187. .vaddr_offset = EFI32_HEADER_SIZE,
  188. .pe_target = GRUB_PE32_MACHINE_I386,
  189. .elf_target = EM_386,
  190. },
  191. {
  192. .dirname = "i386-ieee1275",
  193. .names = { "i386-ieee1275", NULL },
  194. .voidp_sizeof = 4,
  195. .bigendian = 0,
  196. .id = IMAGE_I386_IEEE1275,
  197. .flags = PLATFORM_FLAGS_NONE,
  198. .total_module_size = TARGET_NO_FIELD,
  199. .decompressor_compressed_size = TARGET_NO_FIELD,
  200. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  201. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  202. .section_align = 1,
  203. .vaddr_offset = 0,
  204. .link_addr = GRUB_KERNEL_I386_IEEE1275_LINK_ADDR,
  205. .elf_target = EM_386,
  206. .mod_gap = GRUB_KERNEL_I386_IEEE1275_MOD_GAP,
  207. .mod_align = GRUB_KERNEL_I386_IEEE1275_MOD_ALIGN,
  208. .link_align = 4,
  209. },
  210. {
  211. .dirname = "i386-qemu",
  212. .names = { "i386-qemu", NULL },
  213. .voidp_sizeof = 4,
  214. .bigendian = 0,
  215. .id = IMAGE_QEMU,
  216. .flags = PLATFORM_FLAGS_NONE,
  217. .total_module_size = TARGET_NO_FIELD,
  218. .decompressor_compressed_size = TARGET_NO_FIELD,
  219. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  220. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  221. .section_align = 1,
  222. .vaddr_offset = 0,
  223. .link_addr = GRUB_KERNEL_I386_QEMU_LINK_ADDR
  224. },
  225. {
  226. .dirname = "x86_64-efi",
  227. .names = { "x86_64-efi", NULL },
  228. .voidp_sizeof = 8,
  229. .bigendian = 0,
  230. .id = IMAGE_EFI,
  231. .flags = PLATFORM_FLAGS_NONE,
  232. .total_module_size = TARGET_NO_FIELD,
  233. .decompressor_compressed_size = TARGET_NO_FIELD,
  234. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  235. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  236. .section_align = GRUB_PE32_SECTION_ALIGNMENT,
  237. .vaddr_offset = EFI64_HEADER_SIZE,
  238. .pe_target = GRUB_PE32_MACHINE_X86_64,
  239. .elf_target = EM_X86_64,
  240. },
  241. {
  242. .dirname = "i386-xen",
  243. .names = { "i386-xen", NULL },
  244. .voidp_sizeof = 4,
  245. .bigendian = 0,
  246. .id = IMAGE_XEN,
  247. .flags = PLATFORM_FLAGS_NONE,
  248. .total_module_size = TARGET_NO_FIELD,
  249. .decompressor_compressed_size = TARGET_NO_FIELD,
  250. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  251. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  252. .section_align = 1,
  253. .vaddr_offset = 0,
  254. .link_addr = 0,
  255. .elf_target = EM_386,
  256. .mod_gap = GRUB_KERNEL_I386_XEN_MOD_GAP,
  257. .mod_align = GRUB_KERNEL_I386_XEN_MOD_ALIGN,
  258. .link_align = 4
  259. },
  260. {
  261. .dirname = "x86_64-xen",
  262. .names = { "x86_64-xen", NULL },
  263. .voidp_sizeof = 8,
  264. .bigendian = 0,
  265. .id = IMAGE_XEN,
  266. .flags = PLATFORM_FLAGS_NONE,
  267. .total_module_size = TARGET_NO_FIELD,
  268. .decompressor_compressed_size = TARGET_NO_FIELD,
  269. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  270. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  271. .section_align = 1,
  272. .vaddr_offset = 0,
  273. .link_addr = 0,
  274. .elf_target = EM_X86_64,
  275. .mod_gap = GRUB_KERNEL_X86_64_XEN_MOD_GAP,
  276. .mod_align = GRUB_KERNEL_X86_64_XEN_MOD_ALIGN,
  277. .link_align = 8
  278. },
  279. {
  280. .dirname = "mipsel-loongson",
  281. .names = { "mipsel-yeeloong-flash", NULL },
  282. .voidp_sizeof = 4,
  283. .bigendian = 0,
  284. .id = IMAGE_YEELOONG_FLASH,
  285. .flags = PLATFORM_FLAGS_DECOMPRESSORS,
  286. .total_module_size = GRUB_KERNEL_MIPS_LOONGSON_TOTAL_MODULE_SIZE,
  287. .decompressor_compressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_COMPRESSED_SIZE,
  288. .decompressor_uncompressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_SIZE,
  289. .decompressor_uncompressed_addr = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_ADDR,
  290. .section_align = 1,
  291. .vaddr_offset = 0,
  292. .link_addr = GRUB_KERNEL_MIPS_LOONGSON_LINK_ADDR,
  293. .elf_target = EM_MIPS,
  294. .link_align = GRUB_KERNEL_MIPS_LOONGSON_LINK_ALIGN,
  295. .default_compression = GRUB_COMPRESSION_NONE
  296. },
  297. {
  298. .dirname = "mipsel-loongson",
  299. .names = { "mipsel-fuloong2f-flash", NULL },
  300. .voidp_sizeof = 4,
  301. .bigendian = 0,
  302. .id = IMAGE_FULOONG2F_FLASH,
  303. .flags = PLATFORM_FLAGS_DECOMPRESSORS,
  304. .total_module_size = GRUB_KERNEL_MIPS_LOONGSON_TOTAL_MODULE_SIZE,
  305. .decompressor_compressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_COMPRESSED_SIZE,
  306. .decompressor_uncompressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_SIZE,
  307. .decompressor_uncompressed_addr = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_ADDR,
  308. .section_align = 1,
  309. .vaddr_offset = 0,
  310. .link_addr = GRUB_KERNEL_MIPS_LOONGSON_LINK_ADDR,
  311. .elf_target = EM_MIPS,
  312. .link_align = GRUB_KERNEL_MIPS_LOONGSON_LINK_ALIGN,
  313. .default_compression = GRUB_COMPRESSION_NONE
  314. },
  315. {
  316. .dirname = "mipsel-loongson",
  317. .names = { "mipsel-loongson-elf", "mipsel-yeeloong-elf",
  318. "mipsel-fuloong2f-elf", "mipsel-fuloong2e-elf",
  319. "mipsel-fuloong-elf", NULL },
  320. .voidp_sizeof = 4,
  321. .bigendian = 0,
  322. .id = IMAGE_LOONGSON_ELF,
  323. .flags = PLATFORM_FLAGS_DECOMPRESSORS,
  324. .total_module_size = GRUB_KERNEL_MIPS_LOONGSON_TOTAL_MODULE_SIZE,
  325. .decompressor_compressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_COMPRESSED_SIZE,
  326. .decompressor_uncompressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_SIZE,
  327. .decompressor_uncompressed_addr = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_ADDR,
  328. .section_align = 1,
  329. .vaddr_offset = 0,
  330. .link_addr = GRUB_KERNEL_MIPS_LOONGSON_LINK_ADDR,
  331. .elf_target = EM_MIPS,
  332. .link_align = GRUB_KERNEL_MIPS_LOONGSON_LINK_ALIGN,
  333. .default_compression = GRUB_COMPRESSION_NONE
  334. },
  335. {
  336. .dirname = "powerpc-ieee1275",
  337. .names = { "powerpc-ieee1275", NULL },
  338. .voidp_sizeof = 4,
  339. .bigendian = 1,
  340. .id = IMAGE_PPC,
  341. .flags = PLATFORM_FLAGS_NONE,
  342. .total_module_size = TARGET_NO_FIELD,
  343. .decompressor_compressed_size = TARGET_NO_FIELD,
  344. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  345. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  346. .section_align = 1,
  347. .vaddr_offset = 0,
  348. .link_addr = GRUB_KERNEL_POWERPC_IEEE1275_LINK_ADDR,
  349. .elf_target = EM_PPC,
  350. .mod_gap = GRUB_KERNEL_POWERPC_IEEE1275_MOD_GAP,
  351. .mod_align = GRUB_KERNEL_POWERPC_IEEE1275_MOD_ALIGN,
  352. .link_align = 4
  353. },
  354. {
  355. .dirname = "sparc64-ieee1275",
  356. .names = { "sparc64-ieee1275-raw", NULL },
  357. .voidp_sizeof = 8,
  358. .bigendian = 1,
  359. .id = IMAGE_SPARC64_RAW,
  360. .flags = PLATFORM_FLAGS_NONE,
  361. .total_module_size = GRUB_KERNEL_SPARC64_IEEE1275_TOTAL_MODULE_SIZE,
  362. .decompressor_compressed_size = TARGET_NO_FIELD,
  363. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  364. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  365. .section_align = 1,
  366. .vaddr_offset = 0,
  367. .link_addr = GRUB_KERNEL_SPARC64_IEEE1275_LINK_ADDR,
  368. .mod_align = GRUB_KERNEL_SPARC64_IEEE1275_MOD_ALIGN,
  369. },
  370. {
  371. .dirname = "sparc64-ieee1275",
  372. .names = { "sparc64-ieee1275-cdcore", NULL },
  373. .voidp_sizeof = 8,
  374. .bigendian = 1,
  375. .id = IMAGE_SPARC64_CDCORE,
  376. .flags = PLATFORM_FLAGS_NONE,
  377. .total_module_size = GRUB_KERNEL_SPARC64_IEEE1275_TOTAL_MODULE_SIZE,
  378. .decompressor_compressed_size = TARGET_NO_FIELD,
  379. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  380. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  381. .section_align = 1,
  382. .vaddr_offset = 0,
  383. .link_addr = GRUB_KERNEL_SPARC64_IEEE1275_LINK_ADDR,
  384. .mod_align = GRUB_KERNEL_SPARC64_IEEE1275_MOD_ALIGN,
  385. },
  386. {
  387. .dirname = "sparc64-ieee1275",
  388. .names = { "sparc64-ieee1275-aout", NULL },
  389. .voidp_sizeof = 8,
  390. .bigendian = 1,
  391. .id = IMAGE_SPARC64_AOUT,
  392. .flags = PLATFORM_FLAGS_NONE,
  393. .total_module_size = GRUB_KERNEL_SPARC64_IEEE1275_TOTAL_MODULE_SIZE,
  394. .decompressor_compressed_size = TARGET_NO_FIELD,
  395. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  396. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  397. .section_align = 1,
  398. .vaddr_offset = 0,
  399. .link_addr = GRUB_KERNEL_SPARC64_IEEE1275_LINK_ADDR,
  400. .mod_align = GRUB_KERNEL_SPARC64_IEEE1275_MOD_ALIGN,
  401. },
  402. {
  403. .dirname = "ia64-efi",
  404. .names = {"ia64-efi", NULL},
  405. .voidp_sizeof = 8,
  406. .bigendian = 0,
  407. .id = IMAGE_EFI,
  408. .flags = PLATFORM_FLAGS_NONE,
  409. .total_module_size = TARGET_NO_FIELD,
  410. .decompressor_compressed_size = TARGET_NO_FIELD,
  411. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  412. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  413. .section_align = GRUB_PE32_SECTION_ALIGNMENT,
  414. .vaddr_offset = EFI64_HEADER_SIZE,
  415. .pe_target = GRUB_PE32_MACHINE_IA64,
  416. .elf_target = EM_IA_64,
  417. },
  418. {
  419. .dirname = "mips-arc",
  420. .names = {"mips-arc", NULL},
  421. .voidp_sizeof = 4,
  422. .bigendian = 1,
  423. .id = IMAGE_MIPS_ARC,
  424. .flags = PLATFORM_FLAGS_DECOMPRESSORS,
  425. .total_module_size = GRUB_KERNEL_MIPS_ARC_TOTAL_MODULE_SIZE,
  426. .decompressor_compressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_COMPRESSED_SIZE,
  427. .decompressor_uncompressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_SIZE,
  428. .decompressor_uncompressed_addr = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_ADDR,
  429. .section_align = 1,
  430. .vaddr_offset = 0,
  431. .link_addr = GRUB_KERNEL_MIPS_ARC_LINK_ADDR,
  432. .elf_target = EM_MIPS,
  433. .link_align = GRUB_KERNEL_MIPS_ARC_LINK_ALIGN,
  434. .default_compression = GRUB_COMPRESSION_NONE
  435. },
  436. {
  437. .dirname = "mipsel-arc",
  438. .names = {"mipsel-arc", NULL},
  439. .voidp_sizeof = 4,
  440. .bigendian = 0,
  441. .id = IMAGE_MIPS_ARC,
  442. .flags = PLATFORM_FLAGS_DECOMPRESSORS,
  443. .total_module_size = GRUB_KERNEL_MIPS_ARC_TOTAL_MODULE_SIZE,
  444. .decompressor_compressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_COMPRESSED_SIZE,
  445. .decompressor_uncompressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_SIZE,
  446. .decompressor_uncompressed_addr = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_ADDR,
  447. .section_align = 1,
  448. .vaddr_offset = 0,
  449. .link_addr = GRUB_KERNEL_MIPSEL_ARC_LINK_ADDR,
  450. .elf_target = EM_MIPS,
  451. .link_align = GRUB_KERNEL_MIPS_ARC_LINK_ALIGN,
  452. .default_compression = GRUB_COMPRESSION_NONE
  453. },
  454. {
  455. .dirname = "mipsel-qemu_mips",
  456. .names = { "mipsel-qemu_mips-elf", NULL },
  457. .voidp_sizeof = 4,
  458. .bigendian = 0,
  459. .id = IMAGE_LOONGSON_ELF,
  460. .flags = PLATFORM_FLAGS_DECOMPRESSORS,
  461. .total_module_size = GRUB_KERNEL_MIPS_QEMU_MIPS_TOTAL_MODULE_SIZE,
  462. .decompressor_compressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_COMPRESSED_SIZE,
  463. .decompressor_uncompressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_SIZE,
  464. .decompressor_uncompressed_addr = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_ADDR,
  465. .section_align = 1,
  466. .vaddr_offset = 0,
  467. .link_addr = GRUB_KERNEL_MIPS_QEMU_MIPS_LINK_ADDR,
  468. .elf_target = EM_MIPS,
  469. .link_align = GRUB_KERNEL_MIPS_QEMU_MIPS_LINK_ALIGN,
  470. .default_compression = GRUB_COMPRESSION_NONE
  471. },
  472. {
  473. .dirname = "mips-qemu_mips",
  474. .names = { "mips-qemu_mips-flash", NULL },
  475. .voidp_sizeof = 4,
  476. .bigendian = 1,
  477. .id = IMAGE_QEMU_MIPS_FLASH,
  478. .flags = PLATFORM_FLAGS_DECOMPRESSORS,
  479. .total_module_size = GRUB_KERNEL_MIPS_QEMU_MIPS_TOTAL_MODULE_SIZE,
  480. .decompressor_compressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_COMPRESSED_SIZE,
  481. .decompressor_uncompressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_SIZE,
  482. .decompressor_uncompressed_addr = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_ADDR,
  483. .section_align = 1,
  484. .vaddr_offset = 0,
  485. .link_addr = GRUB_KERNEL_MIPS_QEMU_MIPS_LINK_ADDR,
  486. .elf_target = EM_MIPS,
  487. .link_align = GRUB_KERNEL_MIPS_QEMU_MIPS_LINK_ALIGN,
  488. .default_compression = GRUB_COMPRESSION_NONE
  489. },
  490. {
  491. .dirname = "mipsel-qemu_mips",
  492. .names = { "mipsel-qemu_mips-flash", NULL },
  493. .voidp_sizeof = 4,
  494. .bigendian = 0,
  495. .id = IMAGE_QEMU_MIPS_FLASH,
  496. .flags = PLATFORM_FLAGS_DECOMPRESSORS,
  497. .total_module_size = GRUB_KERNEL_MIPS_QEMU_MIPS_TOTAL_MODULE_SIZE,
  498. .decompressor_compressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_COMPRESSED_SIZE,
  499. .decompressor_uncompressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_SIZE,
  500. .decompressor_uncompressed_addr = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_ADDR,
  501. .section_align = 1,
  502. .vaddr_offset = 0,
  503. .link_addr = GRUB_KERNEL_MIPS_QEMU_MIPS_LINK_ADDR,
  504. .elf_target = EM_MIPS,
  505. .link_align = GRUB_KERNEL_MIPS_QEMU_MIPS_LINK_ALIGN,
  506. .default_compression = GRUB_COMPRESSION_NONE
  507. },
  508. {
  509. .dirname = "mips-qemu_mips",
  510. .names = { "mips-qemu_mips-elf", NULL },
  511. .voidp_sizeof = 4,
  512. .bigendian = 1,
  513. .id = IMAGE_LOONGSON_ELF,
  514. .flags = PLATFORM_FLAGS_DECOMPRESSORS,
  515. .total_module_size = GRUB_KERNEL_MIPS_QEMU_MIPS_TOTAL_MODULE_SIZE,
  516. .decompressor_compressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_COMPRESSED_SIZE,
  517. .decompressor_uncompressed_size = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_SIZE,
  518. .decompressor_uncompressed_addr = GRUB_DECOMPRESSOR_MIPS_LOONGSON_UNCOMPRESSED_ADDR,
  519. .section_align = 1,
  520. .vaddr_offset = 0,
  521. .link_addr = GRUB_KERNEL_MIPS_QEMU_MIPS_LINK_ADDR,
  522. .elf_target = EM_MIPS,
  523. .link_align = GRUB_KERNEL_MIPS_QEMU_MIPS_LINK_ALIGN,
  524. .default_compression = GRUB_COMPRESSION_NONE
  525. },
  526. {
  527. .dirname = "arm-uboot",
  528. .names = { "arm-uboot", NULL },
  529. .voidp_sizeof = 4,
  530. .bigendian = 0,
  531. .id = IMAGE_UBOOT,
  532. .flags = PLATFORM_FLAGS_NONE,
  533. .total_module_size = GRUB_KERNEL_ARM_UBOOT_TOTAL_MODULE_SIZE,
  534. .decompressor_compressed_size = TARGET_NO_FIELD,
  535. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  536. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  537. .section_align = GRUB_KERNEL_ARM_UBOOT_MOD_ALIGN,
  538. .vaddr_offset = 0,
  539. .elf_target = EM_ARM,
  540. .mod_gap = GRUB_KERNEL_ARM_UBOOT_MOD_GAP,
  541. .mod_align = GRUB_KERNEL_ARM_UBOOT_MOD_ALIGN,
  542. .link_align = 4
  543. },
  544. /* For coreboot versions that don't support self-relocating images. */
  545. {
  546. .dirname = "arm-coreboot-vexpress",
  547. .names = { "arm-coreboot-vexpress", NULL },
  548. .voidp_sizeof = 4,
  549. .bigendian = 0,
  550. .id = IMAGE_COREBOOT,
  551. .flags = PLATFORM_FLAGS_NONE,
  552. .total_module_size = GRUB_KERNEL_ARM_COREBOOT_TOTAL_MODULE_SIZE,
  553. .decompressor_compressed_size = TARGET_NO_FIELD,
  554. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  555. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  556. .section_align = GRUB_KERNEL_ARM_COREBOOT_MOD_ALIGN,
  557. .vaddr_offset = 0,
  558. .elf_target = EM_ARM,
  559. .mod_gap = GRUB_KERNEL_ARM_COREBOOT_MOD_GAP,
  560. .mod_align = GRUB_KERNEL_ARM_COREBOOT_MOD_ALIGN,
  561. .link_align = 4,
  562. .link_addr = 0x62000000,
  563. },
  564. {
  565. .dirname = "arm-coreboot-veyron",
  566. .names = { "arm-coreboot-veyron", NULL },
  567. .voidp_sizeof = 4,
  568. .bigendian = 0,
  569. .id = IMAGE_COREBOOT,
  570. .flags = PLATFORM_FLAGS_NONE,
  571. .total_module_size = GRUB_KERNEL_ARM_COREBOOT_TOTAL_MODULE_SIZE,
  572. .decompressor_compressed_size = TARGET_NO_FIELD,
  573. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  574. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  575. .section_align = GRUB_KERNEL_ARM_COREBOOT_MOD_ALIGN,
  576. .vaddr_offset = 0,
  577. .elf_target = EM_ARM,
  578. .mod_gap = GRUB_KERNEL_ARM_COREBOOT_MOD_GAP,
  579. .mod_align = GRUB_KERNEL_ARM_COREBOOT_MOD_ALIGN,
  580. .link_align = 4,
  581. .link_addr = 0x43000000,
  582. },
  583. {
  584. .dirname = "arm-efi",
  585. .names = { "arm-efi", NULL },
  586. .voidp_sizeof = 4,
  587. .bigendian = 0,
  588. .id = IMAGE_EFI,
  589. .flags = PLATFORM_FLAGS_NONE,
  590. .total_module_size = TARGET_NO_FIELD,
  591. .decompressor_compressed_size = TARGET_NO_FIELD,
  592. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  593. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  594. .section_align = GRUB_PE32_SECTION_ALIGNMENT,
  595. .vaddr_offset = EFI32_HEADER_SIZE,
  596. .pe_target = GRUB_PE32_MACHINE_ARMTHUMB_MIXED,
  597. .elf_target = EM_ARM,
  598. },
  599. {
  600. .dirname = "arm64-efi",
  601. .names = { "arm64-efi", NULL },
  602. .voidp_sizeof = 8,
  603. .bigendian = 0,
  604. .id = IMAGE_EFI,
  605. .flags = PLATFORM_FLAGS_NONE,
  606. .total_module_size = TARGET_NO_FIELD,
  607. .decompressor_compressed_size = TARGET_NO_FIELD,
  608. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  609. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  610. .section_align = GRUB_PE32_SECTION_ALIGNMENT,
  611. .vaddr_offset = EFI64_HEADER_SIZE,
  612. .pe_target = GRUB_PE32_MACHINE_ARM64,
  613. .elf_target = EM_AARCH64,
  614. },
  615. {
  616. .dirname = "riscv32-efi",
  617. .names = { "riscv32-efi", NULL },
  618. .voidp_sizeof = 4,
  619. .bigendian = 0,
  620. .id = IMAGE_EFI,
  621. .flags = PLATFORM_FLAGS_NONE,
  622. .total_module_size = TARGET_NO_FIELD,
  623. .decompressor_compressed_size = TARGET_NO_FIELD,
  624. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  625. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  626. .section_align = GRUB_PE32_SECTION_ALIGNMENT,
  627. .vaddr_offset = EFI32_HEADER_SIZE,
  628. .pe_target = GRUB_PE32_MACHINE_RISCV32,
  629. .elf_target = EM_RISCV,
  630. },
  631. {
  632. .dirname = "riscv64-efi",
  633. .names = { "riscv64-efi", NULL },
  634. .voidp_sizeof = 8,
  635. .bigendian = 0,
  636. .id = IMAGE_EFI,
  637. .flags = PLATFORM_FLAGS_NONE,
  638. .total_module_size = TARGET_NO_FIELD,
  639. .decompressor_compressed_size = TARGET_NO_FIELD,
  640. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  641. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  642. .section_align = GRUB_PE32_SECTION_ALIGNMENT,
  643. .vaddr_offset = EFI64_HEADER_SIZE,
  644. .pe_target = GRUB_PE32_MACHINE_RISCV64,
  645. .elf_target = EM_RISCV,
  646. },
  647. };
  648. #include <grub/lib/LzmaEnc.h>
  649. static void *SzAlloc(void *p __attribute__ ((unused)), size_t size) { return xmalloc(size); }
  650. static void SzFree(void *p __attribute__ ((unused)), void *address) { free(address); }
  651. static ISzAlloc g_Alloc = { SzAlloc, SzFree };
  652. static void
  653. compress_kernel_lzma (char *kernel_img, size_t kernel_size,
  654. char **core_img, size_t *core_size)
  655. {
  656. CLzmaEncProps props;
  657. unsigned char out_props[5];
  658. size_t out_props_size = 5;
  659. LzmaEncProps_Init(&props);
  660. props.dictSize = 1 << 16;
  661. props.lc = 3;
  662. props.lp = 0;
  663. props.pb = 2;
  664. props.numThreads = 1;
  665. *core_img = xmalloc (kernel_size);
  666. *core_size = kernel_size;
  667. if (LzmaEncode ((unsigned char *) *core_img, core_size,
  668. (unsigned char *) kernel_img,
  669. kernel_size,
  670. &props, out_props, &out_props_size,
  671. 0, NULL, &g_Alloc, &g_Alloc) != SZ_OK)
  672. grub_util_error ("%s", _("cannot compress the kernel image"));
  673. }
  674. #ifdef USE_LIBLZMA
  675. static void
  676. compress_kernel_xz (char *kernel_img, size_t kernel_size,
  677. char **core_img, size_t *core_size)
  678. {
  679. lzma_stream strm = LZMA_STREAM_INIT;
  680. lzma_ret xzret;
  681. lzma_options_lzma lzopts = {
  682. .dict_size = 1 << 16,
  683. .preset_dict = NULL,
  684. .preset_dict_size = 0,
  685. .lc = 3,
  686. .lp = 0,
  687. .pb = 2,
  688. .mode = LZMA_MODE_NORMAL,
  689. .nice_len = 64,
  690. .mf = LZMA_MF_BT4,
  691. .depth = 0,
  692. };
  693. lzma_filter fltrs[] = {
  694. { .id = LZMA_FILTER_LZMA2, .options = &lzopts},
  695. { .id = LZMA_VLI_UNKNOWN, .options = NULL}
  696. };
  697. xzret = lzma_stream_encoder (&strm, fltrs, LZMA_CHECK_NONE);
  698. if (xzret != LZMA_OK)
  699. grub_util_error ("%s", _("cannot compress the kernel image"));
  700. *core_img = xmalloc (kernel_size);
  701. *core_size = kernel_size;
  702. strm.next_in = (unsigned char *) kernel_img;
  703. strm.avail_in = kernel_size;
  704. strm.next_out = (unsigned char *) *core_img;
  705. strm.avail_out = *core_size;
  706. while (1)
  707. {
  708. xzret = lzma_code (&strm, LZMA_FINISH);
  709. if (xzret == LZMA_OK)
  710. continue;
  711. if (xzret == LZMA_STREAM_END)
  712. break;
  713. grub_util_error ("%s", _("cannot compress the kernel image"));
  714. }
  715. *core_size -= strm.avail_out;
  716. }
  717. #endif
  718. static void
  719. compress_kernel (const struct grub_install_image_target_desc *image_target, char *kernel_img,
  720. size_t kernel_size, char **core_img, size_t *core_size,
  721. grub_compression_t comp)
  722. {
  723. if (image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS
  724. && (comp == GRUB_COMPRESSION_LZMA))
  725. {
  726. compress_kernel_lzma (kernel_img, kernel_size, core_img,
  727. core_size);
  728. return;
  729. }
  730. #ifdef USE_LIBLZMA
  731. if (image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS
  732. && (comp == GRUB_COMPRESSION_XZ))
  733. {
  734. compress_kernel_xz (kernel_img, kernel_size, core_img,
  735. core_size);
  736. return;
  737. }
  738. #endif
  739. if (image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS
  740. && (comp != GRUB_COMPRESSION_NONE))
  741. grub_util_error (_("unknown compression %d"), comp);
  742. *core_img = xmalloc (kernel_size);
  743. memcpy (*core_img, kernel_img, kernel_size);
  744. *core_size = kernel_size;
  745. }
  746. const struct grub_install_image_target_desc *
  747. grub_install_get_image_target (const char *arg)
  748. {
  749. unsigned i, j;
  750. for (i = 0; i < ARRAY_SIZE (image_targets); i++)
  751. for (j = 0; j < ARRAY_SIZE (image_targets[i].names) &&
  752. image_targets[i].names[j]; j++)
  753. if (strcmp (arg, image_targets[i].names[j]) == 0)
  754. return &image_targets[i];
  755. return NULL;
  756. }
  757. const char *
  758. grub_util_get_target_dirname (const struct grub_install_image_target_desc *t)
  759. {
  760. return t->dirname;
  761. }
  762. const char *
  763. grub_util_get_target_name (const struct grub_install_image_target_desc *t)
  764. {
  765. return t->names[0];
  766. }
  767. char *
  768. grub_install_get_image_targets_string (void)
  769. {
  770. int format_len = 0;
  771. char *formats;
  772. char *ptr;
  773. unsigned i;
  774. for (i = 0; i < ARRAY_SIZE (image_targets); i++)
  775. format_len += strlen (image_targets[i].names[0]) + 2;
  776. ptr = formats = xmalloc (format_len);
  777. for (i = 0; i < ARRAY_SIZE (image_targets); i++)
  778. {
  779. strcpy (ptr, image_targets[i].names[0]);
  780. ptr += strlen (image_targets[i].names[0]);
  781. *ptr++ = ',';
  782. *ptr++ = ' ';
  783. }
  784. ptr[-2] = 0;
  785. return formats;
  786. }
  787. void
  788. grub_install_generate_image (const char *dir, const char *prefix,
  789. FILE *out, const char *outname, char *mods[],
  790. char *memdisk_path, char **pubkey_paths,
  791. size_t npubkeys, char *config_path,
  792. const struct grub_install_image_target_desc *image_target,
  793. int note, grub_compression_t comp, const char *dtb_path)
  794. {
  795. char *kernel_img, *core_img;
  796. size_t total_module_size, core_size;
  797. size_t memdisk_size = 0, config_size = 0;
  798. size_t prefix_size = 0, dtb_size = 0;
  799. char *kernel_path;
  800. size_t offset;
  801. struct grub_util_path_list *path_list, *p;
  802. size_t decompress_size = 0;
  803. struct grub_mkimage_layout layout;
  804. if (comp == GRUB_COMPRESSION_AUTO)
  805. comp = image_target->default_compression;
  806. if (image_target->id == IMAGE_I386_PC
  807. || image_target->id == IMAGE_I386_PC_PXE
  808. || image_target->id == IMAGE_I386_PC_ELTORITO)
  809. comp = GRUB_COMPRESSION_LZMA;
  810. path_list = grub_util_resolve_dependencies (dir, "moddep.lst", mods);
  811. kernel_path = grub_util_get_path (dir, "kernel.img");
  812. if (image_target->voidp_sizeof == 8)
  813. total_module_size = sizeof (struct grub_module_info64);
  814. else
  815. total_module_size = sizeof (struct grub_module_info32);
  816. {
  817. size_t i;
  818. for (i = 0; i < npubkeys; i++)
  819. {
  820. size_t curs;
  821. curs = ALIGN_ADDR (grub_util_get_image_size (pubkey_paths[i]));
  822. grub_util_info ("the size of public key %u is 0x%"
  823. GRUB_HOST_PRIxLONG_LONG,
  824. (unsigned) i, (unsigned long long) curs);
  825. total_module_size += curs + sizeof (struct grub_module_header);
  826. }
  827. }
  828. if (memdisk_path)
  829. {
  830. memdisk_size = ALIGN_UP(grub_util_get_image_size (memdisk_path), 512);
  831. grub_util_info ("the size of memory disk is 0x%" GRUB_HOST_PRIxLONG_LONG,
  832. (unsigned long long) memdisk_size);
  833. total_module_size += memdisk_size + sizeof (struct grub_module_header);
  834. }
  835. if (dtb_path)
  836. {
  837. dtb_size = ALIGN_ADDR(grub_util_get_image_size (dtb_path));
  838. total_module_size += dtb_size + sizeof (struct grub_module_header);
  839. }
  840. if (config_path)
  841. {
  842. config_size = ALIGN_ADDR (grub_util_get_image_size (config_path) + 1);
  843. grub_util_info ("the size of config file is 0x%" GRUB_HOST_PRIxLONG_LONG,
  844. (unsigned long long) config_size);
  845. total_module_size += config_size + sizeof (struct grub_module_header);
  846. }
  847. if (prefix)
  848. {
  849. prefix_size = ALIGN_ADDR (strlen (prefix) + 1);
  850. total_module_size += prefix_size + sizeof (struct grub_module_header);
  851. }
  852. for (p = path_list; p; p = p->next)
  853. total_module_size += (ALIGN_ADDR (grub_util_get_image_size (p->name))
  854. + sizeof (struct grub_module_header));
  855. grub_util_info ("the total module size is 0x%" GRUB_HOST_PRIxLONG_LONG,
  856. (unsigned long long) total_module_size);
  857. if (image_target->voidp_sizeof == 4)
  858. kernel_img = grub_mkimage_load_image32 (kernel_path, total_module_size,
  859. &layout, image_target);
  860. else
  861. kernel_img = grub_mkimage_load_image64 (kernel_path, total_module_size,
  862. &layout, image_target);
  863. if ((image_target->id == IMAGE_XEN || image_target->id == IMAGE_XEN_PVH) &&
  864. layout.align < 4096)
  865. layout.align = 4096;
  866. if ((image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS)
  867. && (image_target->total_module_size != TARGET_NO_FIELD))
  868. *((grub_uint32_t *) (kernel_img + image_target->total_module_size))
  869. = grub_host_to_target32 (total_module_size);
  870. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  871. {
  872. memmove (kernel_img + total_module_size, kernel_img, layout.kernel_size);
  873. memset (kernel_img, 0, total_module_size);
  874. }
  875. if (image_target->voidp_sizeof == 8)
  876. {
  877. /* Fill in the grub_module_info structure. */
  878. struct grub_module_info64 *modinfo;
  879. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  880. modinfo = (struct grub_module_info64 *) kernel_img;
  881. else
  882. modinfo = (struct grub_module_info64 *) (kernel_img + layout.kernel_size);
  883. modinfo->magic = grub_host_to_target32 (GRUB_MODULE_MAGIC);
  884. modinfo->offset = grub_host_to_target_addr (sizeof (struct grub_module_info64));
  885. modinfo->size = grub_host_to_target_addr (total_module_size);
  886. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  887. offset = sizeof (struct grub_module_info64);
  888. else
  889. offset = layout.kernel_size + sizeof (struct grub_module_info64);
  890. }
  891. else
  892. {
  893. /* Fill in the grub_module_info structure. */
  894. struct grub_module_info32 *modinfo;
  895. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  896. modinfo = (struct grub_module_info32 *) kernel_img;
  897. else
  898. modinfo = (struct grub_module_info32 *) (kernel_img + layout.kernel_size);
  899. modinfo->magic = grub_host_to_target32 (GRUB_MODULE_MAGIC);
  900. modinfo->offset = grub_host_to_target_addr (sizeof (struct grub_module_info32));
  901. modinfo->size = grub_host_to_target_addr (total_module_size);
  902. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  903. offset = sizeof (struct grub_module_info32);
  904. else
  905. offset = layout.kernel_size + sizeof (struct grub_module_info32);
  906. }
  907. for (p = path_list; p; p = p->next)
  908. {
  909. struct grub_module_header *header;
  910. size_t mod_size;
  911. mod_size = ALIGN_ADDR (grub_util_get_image_size (p->name));
  912. header = (struct grub_module_header *) (kernel_img + offset);
  913. header->type = grub_host_to_target32 (OBJ_TYPE_ELF);
  914. header->size = grub_host_to_target32 (mod_size + sizeof (*header));
  915. offset += sizeof (*header);
  916. grub_util_load_image (p->name, kernel_img + offset);
  917. offset += mod_size;
  918. }
  919. {
  920. size_t i;
  921. for (i = 0; i < npubkeys; i++)
  922. {
  923. size_t curs;
  924. struct grub_module_header *header;
  925. curs = grub_util_get_image_size (pubkey_paths[i]);
  926. header = (struct grub_module_header *) (kernel_img + offset);
  927. header->type = grub_host_to_target32 (OBJ_TYPE_PUBKEY);
  928. header->size = grub_host_to_target32 (curs + sizeof (*header));
  929. offset += sizeof (*header);
  930. grub_util_load_image (pubkey_paths[i], kernel_img + offset);
  931. offset += ALIGN_ADDR (curs);
  932. }
  933. }
  934. if (memdisk_path)
  935. {
  936. struct grub_module_header *header;
  937. header = (struct grub_module_header *) (kernel_img + offset);
  938. header->type = grub_host_to_target32 (OBJ_TYPE_MEMDISK);
  939. header->size = grub_host_to_target32 (memdisk_size + sizeof (*header));
  940. offset += sizeof (*header);
  941. grub_util_load_image (memdisk_path, kernel_img + offset);
  942. offset += memdisk_size;
  943. }
  944. if (dtb_path)
  945. {
  946. struct grub_module_header *header;
  947. header = (struct grub_module_header *) (kernel_img + offset);
  948. header->type = grub_host_to_target32 (OBJ_TYPE_DTB);
  949. header->size = grub_host_to_target32 (dtb_size + sizeof (*header));
  950. offset += sizeof (*header);
  951. grub_util_load_image (dtb_path, kernel_img + offset);
  952. offset += dtb_size;
  953. }
  954. if (config_path)
  955. {
  956. struct grub_module_header *header;
  957. header = (struct grub_module_header *) (kernel_img + offset);
  958. header->type = grub_host_to_target32 (OBJ_TYPE_CONFIG);
  959. header->size = grub_host_to_target32 (config_size + sizeof (*header));
  960. offset += sizeof (*header);
  961. grub_util_load_image (config_path, kernel_img + offset);
  962. offset += config_size;
  963. }
  964. if (prefix)
  965. {
  966. struct grub_module_header *header;
  967. header = (struct grub_module_header *) (kernel_img + offset);
  968. header->type = grub_host_to_target32 (OBJ_TYPE_PREFIX);
  969. header->size = grub_host_to_target32 (prefix_size + sizeof (*header));
  970. offset += sizeof (*header);
  971. grub_strcpy (kernel_img + offset, prefix);
  972. offset += prefix_size;
  973. }
  974. grub_util_info ("kernel_img=%p, kernel_size=0x%" GRUB_HOST_PRIxLONG_LONG,
  975. kernel_img,
  976. (unsigned long long) layout.kernel_size);
  977. compress_kernel (image_target, kernel_img, layout.kernel_size + total_module_size,
  978. &core_img, &core_size, comp);
  979. free (kernel_img);
  980. grub_util_info ("the core size is 0x%" GRUB_HOST_PRIxLONG_LONG,
  981. (unsigned long long) core_size);
  982. if (!(image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS)
  983. && image_target->total_module_size != TARGET_NO_FIELD)
  984. *((grub_uint32_t *) (core_img + image_target->total_module_size))
  985. = grub_host_to_target32 (total_module_size);
  986. if (image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS)
  987. {
  988. char *full_img;
  989. size_t full_size;
  990. char *decompress_path, *decompress_img;
  991. const char *name;
  992. switch (comp)
  993. {
  994. case GRUB_COMPRESSION_XZ:
  995. name = "xz_decompress.img";
  996. break;
  997. case GRUB_COMPRESSION_LZMA:
  998. name = "lzma_decompress.img";
  999. break;
  1000. case GRUB_COMPRESSION_NONE:
  1001. name = "none_decompress.img";
  1002. break;
  1003. default:
  1004. grub_util_error (_("unknown compression %d"), comp);
  1005. }
  1006. decompress_path = grub_util_get_path (dir, name);
  1007. decompress_size = grub_util_get_image_size (decompress_path);
  1008. decompress_img = grub_util_read_image (decompress_path);
  1009. if ((image_target->id == IMAGE_I386_PC
  1010. || image_target->id == IMAGE_I386_PC_PXE
  1011. || image_target->id == IMAGE_I386_PC_ELTORITO)
  1012. && decompress_size > GRUB_KERNEL_I386_PC_LINK_ADDR - 0x8200)
  1013. grub_util_error ("%s", _("Decompressor is too big"));
  1014. if (image_target->decompressor_compressed_size != TARGET_NO_FIELD)
  1015. *((grub_uint32_t *) (decompress_img
  1016. + image_target->decompressor_compressed_size))
  1017. = grub_host_to_target32 (core_size);
  1018. if (image_target->decompressor_uncompressed_size != TARGET_NO_FIELD)
  1019. *((grub_uint32_t *) (decompress_img
  1020. + image_target->decompressor_uncompressed_size))
  1021. = grub_host_to_target32 (layout.kernel_size + total_module_size);
  1022. if (image_target->decompressor_uncompressed_addr != TARGET_NO_FIELD)
  1023. {
  1024. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  1025. *((grub_uint32_t *) (decompress_img + image_target->decompressor_uncompressed_addr))
  1026. = grub_host_to_target_addr (image_target->link_addr - total_module_size);
  1027. else
  1028. *((grub_uint32_t *) (decompress_img + image_target->decompressor_uncompressed_addr))
  1029. = grub_host_to_target_addr (image_target->link_addr);
  1030. }
  1031. full_size = core_size + decompress_size;
  1032. full_img = xmalloc (full_size);
  1033. memcpy (full_img, decompress_img, decompress_size);
  1034. memcpy (full_img + decompress_size, core_img, core_size);
  1035. free (core_img);
  1036. core_img = full_img;
  1037. core_size = full_size;
  1038. free (decompress_img);
  1039. free (decompress_path);
  1040. }
  1041. switch (image_target->id)
  1042. {
  1043. case IMAGE_I386_PC:
  1044. case IMAGE_I386_PC_PXE:
  1045. case IMAGE_I386_PC_ELTORITO:
  1046. if (GRUB_KERNEL_I386_PC_LINK_ADDR + core_size > 0x78000
  1047. || (core_size > (0xffff << GRUB_DISK_SECTOR_BITS))
  1048. || (layout.kernel_size + layout.bss_size
  1049. + GRUB_KERNEL_I386_PC_LINK_ADDR > 0x68000))
  1050. grub_util_error (_("core image is too big (0x%x > 0x%x)"),
  1051. GRUB_KERNEL_I386_PC_LINK_ADDR + (unsigned) core_size,
  1052. 0x78000);
  1053. /* fallthrough */
  1054. case IMAGE_COREBOOT:
  1055. case IMAGE_QEMU:
  1056. if (image_target->elf_target != EM_ARM && layout.kernel_size + layout.bss_size + GRUB_KERNEL_I386_PC_LINK_ADDR > 0x68000)
  1057. grub_util_error (_("kernel image is too big (0x%x > 0x%x)"),
  1058. (unsigned) layout.kernel_size + (unsigned) layout.bss_size
  1059. + GRUB_KERNEL_I386_PC_LINK_ADDR,
  1060. 0x68000);
  1061. break;
  1062. case IMAGE_LOONGSON_ELF:
  1063. case IMAGE_YEELOONG_FLASH:
  1064. case IMAGE_FULOONG2F_FLASH:
  1065. case IMAGE_EFI:
  1066. case IMAGE_MIPS_ARC:
  1067. case IMAGE_QEMU_MIPS_FLASH:
  1068. case IMAGE_XEN:
  1069. case IMAGE_XEN_PVH:
  1070. break;
  1071. case IMAGE_SPARC64_AOUT:
  1072. case IMAGE_SPARC64_RAW:
  1073. case IMAGE_SPARC64_CDCORE:
  1074. case IMAGE_I386_IEEE1275:
  1075. case IMAGE_PPC:
  1076. case IMAGE_UBOOT:
  1077. break;
  1078. }
  1079. switch (image_target->id)
  1080. {
  1081. case IMAGE_I386_PC:
  1082. case IMAGE_I386_PC_PXE:
  1083. case IMAGE_I386_PC_ELTORITO:
  1084. {
  1085. unsigned num;
  1086. char *boot_path, *boot_img;
  1087. size_t boot_size;
  1088. num = ((core_size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS);
  1089. if (image_target->id == IMAGE_I386_PC_PXE)
  1090. {
  1091. char *pxeboot_path, *pxeboot_img;
  1092. size_t pxeboot_size;
  1093. grub_uint32_t *ptr;
  1094. pxeboot_path = grub_util_get_path (dir, "pxeboot.img");
  1095. pxeboot_size = grub_util_get_image_size (pxeboot_path);
  1096. pxeboot_img = grub_util_read_image (pxeboot_path);
  1097. grub_util_write_image (pxeboot_img, pxeboot_size, out,
  1098. outname);
  1099. free (pxeboot_img);
  1100. free (pxeboot_path);
  1101. /* Remove Multiboot header to avoid confusing ipxe. */
  1102. for (ptr = (grub_uint32_t *) core_img;
  1103. ptr < (grub_uint32_t *) (core_img + MULTIBOOT_SEARCH); ptr++)
  1104. if (*ptr == grub_host_to_target32 (MULTIBOOT_HEADER_MAGIC)
  1105. && grub_target_to_host32 (ptr[0])
  1106. + grub_target_to_host32 (ptr[1])
  1107. + grub_target_to_host32 (ptr[2]) == 0)
  1108. {
  1109. *ptr = 0;
  1110. break;
  1111. }
  1112. }
  1113. if (image_target->id == IMAGE_I386_PC_ELTORITO)
  1114. {
  1115. char *eltorito_path, *eltorito_img;
  1116. size_t eltorito_size;
  1117. eltorito_path = grub_util_get_path (dir, "cdboot.img");
  1118. eltorito_size = grub_util_get_image_size (eltorito_path);
  1119. eltorito_img = grub_util_read_image (eltorito_path);
  1120. grub_util_write_image (eltorito_img, eltorito_size, out,
  1121. outname);
  1122. free (eltorito_img);
  1123. free (eltorito_path);
  1124. }
  1125. boot_path = grub_util_get_path (dir, "diskboot.img");
  1126. boot_size = grub_util_get_image_size (boot_path);
  1127. if (boot_size != GRUB_DISK_SECTOR_SIZE)
  1128. grub_util_error (_("diskboot.img size must be %u bytes"),
  1129. GRUB_DISK_SECTOR_SIZE);
  1130. boot_img = grub_util_read_image (boot_path);
  1131. {
  1132. struct grub_pc_bios_boot_blocklist *block;
  1133. block = (struct grub_pc_bios_boot_blocklist *) (boot_img
  1134. + GRUB_DISK_SECTOR_SIZE
  1135. - sizeof (*block));
  1136. block->len = grub_host_to_target16 (num);
  1137. /* This is filled elsewhere. Verify it just in case. */
  1138. assert (block->segment
  1139. == grub_host_to_target16 (GRUB_BOOT_I386_PC_KERNEL_SEG
  1140. + (GRUB_DISK_SECTOR_SIZE >> 4)));
  1141. }
  1142. grub_util_write_image (boot_img, boot_size, out, outname);
  1143. free (boot_img);
  1144. free (boot_path);
  1145. }
  1146. break;
  1147. case IMAGE_EFI:
  1148. {
  1149. void *pe_img;
  1150. grub_uint8_t *header;
  1151. void *sections;
  1152. size_t pe_size;
  1153. struct grub_pe32_coff_header *c;
  1154. struct grub_pe32_section_table *text_section, *data_section;
  1155. struct grub_pe32_section_table *mods_section, *reloc_section;
  1156. static const grub_uint8_t stub[] = GRUB_PE32_MSDOS_STUB;
  1157. int header_size;
  1158. int reloc_addr;
  1159. if (image_target->voidp_sizeof == 4)
  1160. header_size = EFI32_HEADER_SIZE;
  1161. else
  1162. header_size = EFI64_HEADER_SIZE;
  1163. reloc_addr = ALIGN_UP (header_size + core_size,
  1164. GRUB_PE32_FILE_ALIGNMENT);
  1165. pe_size = ALIGN_UP (reloc_addr + layout.reloc_size,
  1166. GRUB_PE32_FILE_ALIGNMENT);
  1167. pe_img = xmalloc (reloc_addr + layout.reloc_size);
  1168. memset (pe_img, 0, header_size);
  1169. memcpy ((char *) pe_img + header_size, core_img, core_size);
  1170. memset ((char *) pe_img + header_size + core_size, 0, reloc_addr - (header_size + core_size));
  1171. memcpy ((char *) pe_img + reloc_addr, layout.reloc_section, layout.reloc_size);
  1172. header = pe_img;
  1173. /* The magic. */
  1174. memcpy (header, stub, GRUB_PE32_MSDOS_STUB_SIZE);
  1175. memcpy (header + GRUB_PE32_MSDOS_STUB_SIZE, "PE\0\0",
  1176. GRUB_PE32_SIGNATURE_SIZE);
  1177. /* The COFF file header. */
  1178. c = (struct grub_pe32_coff_header *) (header + GRUB_PE32_MSDOS_STUB_SIZE
  1179. + GRUB_PE32_SIGNATURE_SIZE);
  1180. c->machine = grub_host_to_target16 (image_target->pe_target);
  1181. c->num_sections = grub_host_to_target16 (4);
  1182. c->time = grub_host_to_target32 (STABLE_EMBEDDING_TIMESTAMP);
  1183. c->characteristics = grub_host_to_target16 (GRUB_PE32_EXECUTABLE_IMAGE
  1184. | GRUB_PE32_LINE_NUMS_STRIPPED
  1185. | ((image_target->voidp_sizeof == 4)
  1186. ? GRUB_PE32_32BIT_MACHINE
  1187. : 0)
  1188. | GRUB_PE32_LOCAL_SYMS_STRIPPED
  1189. | GRUB_PE32_DEBUG_STRIPPED);
  1190. /* The PE Optional header. */
  1191. if (image_target->voidp_sizeof == 4)
  1192. {
  1193. struct grub_pe32_optional_header *o;
  1194. c->optional_header_size = grub_host_to_target16 (sizeof (struct grub_pe32_optional_header));
  1195. o = (struct grub_pe32_optional_header *)
  1196. (header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE
  1197. + sizeof (struct grub_pe32_coff_header));
  1198. o->magic = grub_host_to_target16 (GRUB_PE32_PE32_MAGIC);
  1199. o->code_size = grub_host_to_target32 (layout.exec_size);
  1200. o->data_size = grub_cpu_to_le32 (reloc_addr - layout.exec_size
  1201. - header_size);
  1202. o->bss_size = grub_cpu_to_le32 (layout.bss_size);
  1203. o->entry_addr = grub_cpu_to_le32 (layout.start_address);
  1204. o->code_base = grub_cpu_to_le32 (header_size);
  1205. o->data_base = grub_host_to_target32 (header_size + layout.exec_size);
  1206. o->image_base = 0;
  1207. o->section_alignment = grub_host_to_target32 (image_target->section_align);
  1208. o->file_alignment = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);
  1209. o->image_size = grub_host_to_target32 (pe_size);
  1210. o->header_size = grub_host_to_target32 (header_size);
  1211. o->subsystem = grub_host_to_target16 (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION);
  1212. /* Do these really matter? */
  1213. o->stack_reserve_size = grub_host_to_target32 (0x10000);
  1214. o->stack_commit_size = grub_host_to_target32 (0x10000);
  1215. o->heap_reserve_size = grub_host_to_target32 (0x10000);
  1216. o->heap_commit_size = grub_host_to_target32 (0x10000);
  1217. o->num_data_directories = grub_host_to_target32 (GRUB_PE32_NUM_DATA_DIRECTORIES);
  1218. o->base_relocation_table.rva = grub_host_to_target32 (reloc_addr);
  1219. o->base_relocation_table.size = grub_host_to_target32 (layout.reloc_size);
  1220. sections = o + 1;
  1221. }
  1222. else
  1223. {
  1224. struct grub_pe64_optional_header *o;
  1225. c->optional_header_size = grub_host_to_target16 (sizeof (struct grub_pe64_optional_header));
  1226. o = (struct grub_pe64_optional_header *)
  1227. (header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE
  1228. + sizeof (struct grub_pe32_coff_header));
  1229. o->magic = grub_host_to_target16 (GRUB_PE32_PE64_MAGIC);
  1230. o->code_size = grub_host_to_target32 (layout.exec_size);
  1231. o->data_size = grub_cpu_to_le32 (reloc_addr - layout.exec_size
  1232. - header_size);
  1233. o->bss_size = grub_cpu_to_le32 (layout.bss_size);
  1234. o->entry_addr = grub_cpu_to_le32 (layout.start_address);
  1235. o->code_base = grub_cpu_to_le32 (header_size);
  1236. o->image_base = 0;
  1237. o->section_alignment = grub_host_to_target32 (image_target->section_align);
  1238. o->file_alignment = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);
  1239. o->image_size = grub_host_to_target32 (pe_size);
  1240. o->header_size = grub_host_to_target32 (header_size);
  1241. o->subsystem = grub_host_to_target16 (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION);
  1242. /* Do these really matter? */
  1243. o->stack_reserve_size = grub_host_to_target64 (0x10000);
  1244. o->stack_commit_size = grub_host_to_target64 (0x10000);
  1245. o->heap_reserve_size = grub_host_to_target64 (0x10000);
  1246. o->heap_commit_size = grub_host_to_target64 (0x10000);
  1247. o->num_data_directories
  1248. = grub_host_to_target32 (GRUB_PE32_NUM_DATA_DIRECTORIES);
  1249. o->base_relocation_table.rva = grub_host_to_target32 (reloc_addr);
  1250. o->base_relocation_table.size = grub_host_to_target32 (layout.reloc_size);
  1251. sections = o + 1;
  1252. }
  1253. /* The sections. */
  1254. text_section = sections;
  1255. strcpy (text_section->name, ".text");
  1256. text_section->virtual_size = grub_cpu_to_le32 (layout.exec_size);
  1257. text_section->virtual_address = grub_cpu_to_le32 (header_size);
  1258. text_section->raw_data_size = grub_cpu_to_le32 (layout.exec_size);
  1259. text_section->raw_data_offset = grub_cpu_to_le32 (header_size);
  1260. text_section->characteristics = grub_cpu_to_le32_compile_time (
  1261. GRUB_PE32_SCN_CNT_CODE
  1262. | GRUB_PE32_SCN_MEM_EXECUTE
  1263. | GRUB_PE32_SCN_MEM_READ);
  1264. data_section = text_section + 1;
  1265. strcpy (data_section->name, ".data");
  1266. data_section->virtual_size = grub_cpu_to_le32 (layout.kernel_size - layout.exec_size);
  1267. data_section->virtual_address = grub_cpu_to_le32 (header_size + layout.exec_size);
  1268. data_section->raw_data_size = grub_cpu_to_le32 (layout.kernel_size - layout.exec_size);
  1269. data_section->raw_data_offset = grub_cpu_to_le32 (header_size + layout.exec_size);
  1270. data_section->characteristics
  1271. = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
  1272. | GRUB_PE32_SCN_MEM_READ
  1273. | GRUB_PE32_SCN_MEM_WRITE);
  1274. #if 0
  1275. bss_section = data_section + 1;
  1276. strcpy (bss_section->name, ".bss");
  1277. bss_section->virtual_size = grub_cpu_to_le32 (layout.bss_size);
  1278. bss_section->virtual_address = grub_cpu_to_le32 (header_size + layout.kernel_size);
  1279. bss_section->raw_data_size = 0;
  1280. bss_section->raw_data_offset = 0;
  1281. bss_section->characteristics
  1282. = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_MEM_READ
  1283. | GRUB_PE32_SCN_MEM_WRITE
  1284. | GRUB_PE32_SCN_ALIGN_64BYTES
  1285. | GRUB_PE32_SCN_CNT_INITIALIZED_DATA
  1286. | 0x80);
  1287. #endif
  1288. mods_section = data_section + 1;
  1289. strcpy (mods_section->name, "mods");
  1290. mods_section->virtual_size = grub_cpu_to_le32 (reloc_addr - layout.kernel_size - header_size);
  1291. mods_section->virtual_address = grub_cpu_to_le32 (header_size + layout.kernel_size + layout.bss_size);
  1292. mods_section->raw_data_size = grub_cpu_to_le32 (reloc_addr - layout.kernel_size - header_size);
  1293. mods_section->raw_data_offset = grub_cpu_to_le32 (header_size + layout.kernel_size);
  1294. mods_section->characteristics
  1295. = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
  1296. | GRUB_PE32_SCN_MEM_READ
  1297. | GRUB_PE32_SCN_MEM_WRITE);
  1298. reloc_section = mods_section + 1;
  1299. strcpy (reloc_section->name, ".reloc");
  1300. reloc_section->virtual_size = grub_cpu_to_le32 (layout.reloc_size);
  1301. reloc_section->virtual_address = grub_cpu_to_le32 (reloc_addr + layout.bss_size);
  1302. reloc_section->raw_data_size = grub_cpu_to_le32 (layout.reloc_size);
  1303. reloc_section->raw_data_offset = grub_cpu_to_le32 (reloc_addr);
  1304. reloc_section->characteristics
  1305. = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
  1306. | GRUB_PE32_SCN_MEM_DISCARDABLE
  1307. | GRUB_PE32_SCN_MEM_READ);
  1308. free (core_img);
  1309. core_img = pe_img;
  1310. core_size = pe_size;
  1311. }
  1312. break;
  1313. case IMAGE_QEMU:
  1314. {
  1315. char *rom_img;
  1316. size_t rom_size;
  1317. char *boot_path, *boot_img;
  1318. size_t boot_size;
  1319. boot_path = grub_util_get_path (dir, "boot.img");
  1320. boot_size = grub_util_get_image_size (boot_path);
  1321. boot_img = grub_util_read_image (boot_path);
  1322. /* Rom sizes must be 64k-aligned. */
  1323. rom_size = ALIGN_UP (core_size + boot_size, 64 * 1024);
  1324. rom_img = xmalloc (rom_size);
  1325. memset (rom_img, 0, rom_size);
  1326. *((grub_int32_t *) (core_img + GRUB_KERNEL_I386_QEMU_CORE_ENTRY_ADDR))
  1327. = grub_host_to_target32 ((grub_uint32_t) -rom_size);
  1328. memcpy (rom_img, core_img, core_size);
  1329. *((grub_int32_t *) (boot_img + GRUB_BOOT_I386_QEMU_CORE_ENTRY_ADDR))
  1330. = grub_host_to_target32 ((grub_uint32_t) -rom_size);
  1331. memcpy (rom_img + rom_size - boot_size, boot_img, boot_size);
  1332. free (core_img);
  1333. core_img = rom_img;
  1334. core_size = rom_size;
  1335. free (boot_img);
  1336. free (boot_path);
  1337. }
  1338. break;
  1339. case IMAGE_SPARC64_AOUT:
  1340. {
  1341. void *aout_img;
  1342. size_t aout_size;
  1343. struct grub_aout32_header *aout_head;
  1344. aout_size = core_size + sizeof (*aout_head);
  1345. aout_img = xmalloc (aout_size);
  1346. aout_head = aout_img;
  1347. grub_memset (aout_head, 0, sizeof (*aout_head));
  1348. aout_head->a_midmag = grub_host_to_target32 ((AOUT_MID_SUN << 16)
  1349. | AOUT32_OMAGIC);
  1350. aout_head->a_text = grub_host_to_target32 (core_size);
  1351. aout_head->a_entry
  1352. = grub_host_to_target32 (GRUB_BOOT_SPARC64_IEEE1275_IMAGE_ADDRESS);
  1353. memcpy ((char *) aout_img + sizeof (*aout_head), core_img, core_size);
  1354. free (core_img);
  1355. core_img = aout_img;
  1356. core_size = aout_size;
  1357. }
  1358. break;
  1359. case IMAGE_SPARC64_RAW:
  1360. {
  1361. unsigned int num;
  1362. char *boot_path, *boot_img;
  1363. size_t boot_size;
  1364. num = ((core_size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS);
  1365. num <<= GRUB_DISK_SECTOR_BITS;
  1366. boot_path = grub_util_get_path (dir, "diskboot.img");
  1367. boot_size = grub_util_get_image_size (boot_path);
  1368. if (boot_size != GRUB_DISK_SECTOR_SIZE)
  1369. grub_util_error (_("diskboot.img size must be %u bytes"),
  1370. GRUB_DISK_SECTOR_SIZE);
  1371. boot_img = grub_util_read_image (boot_path);
  1372. *((grub_uint32_t *) (boot_img + GRUB_DISK_SECTOR_SIZE
  1373. - GRUB_BOOT_SPARC64_IEEE1275_LIST_SIZE + 8))
  1374. = grub_host_to_target32 (num);
  1375. grub_util_write_image (boot_img, boot_size, out, outname);
  1376. free (boot_img);
  1377. free (boot_path);
  1378. }
  1379. break;
  1380. case IMAGE_SPARC64_CDCORE:
  1381. break;
  1382. case IMAGE_YEELOONG_FLASH:
  1383. case IMAGE_FULOONG2F_FLASH:
  1384. {
  1385. char *rom_img;
  1386. size_t rom_size;
  1387. char *boot_path, *boot_img;
  1388. size_t boot_size;
  1389. /* fwstart.img is the only part which can't be tested by using *-elf
  1390. target. Check it against the checksum. */
  1391. const grub_uint8_t yeeloong_fwstart_good_hash[512 / 8] =
  1392. {
  1393. 0x5f, 0x67, 0x46, 0x57, 0x31, 0x30, 0xc5, 0x0a,
  1394. 0xe9, 0x98, 0x18, 0xc9, 0xf3, 0xca, 0x45, 0xa5,
  1395. 0x75, 0x64, 0x6b, 0xbb, 0x24, 0xcd, 0xb4, 0xbc,
  1396. 0xf2, 0x3e, 0x23, 0xf9, 0xc2, 0x6a, 0x8c, 0xde,
  1397. 0x3b, 0x94, 0x9c, 0xcc, 0xa5, 0xa7, 0x58, 0xb1,
  1398. 0xbe, 0x8b, 0x3d, 0x73, 0x98, 0x18, 0x7e, 0x68,
  1399. 0x5e, 0x5f, 0x23, 0x7d, 0x7a, 0xe8, 0x51, 0xf7,
  1400. 0x1a, 0xaf, 0x2f, 0x54, 0x11, 0x2e, 0x5c, 0x25
  1401. };
  1402. const grub_uint8_t fuloong2f_fwstart_good_hash[512 / 8] =
  1403. {
  1404. 0x76, 0x9b, 0xad, 0x6e, 0xa2, 0x39, 0x47, 0x62,
  1405. 0x1f, 0xc9, 0x3a, 0x6d, 0x05, 0x5c, 0x43, 0x5c,
  1406. 0x29, 0x4a, 0x7e, 0x08, 0x2a, 0x31, 0x8f, 0x5d,
  1407. 0x02, 0x84, 0xa0, 0x85, 0xf2, 0xd1, 0xb9, 0x53,
  1408. 0xa2, 0xbc, 0xf2, 0xe1, 0x39, 0x1e, 0x51, 0xb5,
  1409. 0xaf, 0xec, 0x9e, 0xf2, 0xf1, 0xf3, 0x0a, 0x2f,
  1410. 0xe6, 0xf1, 0x08, 0x89, 0xbe, 0xbc, 0x73, 0xab,
  1411. 0x46, 0x50, 0xd6, 0x21, 0xce, 0x8e, 0x24, 0xa7
  1412. };
  1413. const grub_uint8_t *fwstart_good_hash;
  1414. grub_uint8_t fwstart_hash[512 / 8];
  1415. if (image_target->id == IMAGE_FULOONG2F_FLASH)
  1416. {
  1417. fwstart_good_hash = fuloong2f_fwstart_good_hash;
  1418. boot_path = grub_util_get_path (dir, "fwstart_fuloong2f.img");
  1419. }
  1420. else
  1421. {
  1422. fwstart_good_hash = yeeloong_fwstart_good_hash;
  1423. boot_path = grub_util_get_path (dir, "fwstart.img");
  1424. }
  1425. boot_size = grub_util_get_image_size (boot_path);
  1426. boot_img = grub_util_read_image (boot_path);
  1427. grub_crypto_hash (GRUB_MD_SHA512, fwstart_hash, boot_img, boot_size);
  1428. if (grub_memcmp (fwstart_hash, fwstart_good_hash,
  1429. GRUB_MD_SHA512->mdlen) != 0)
  1430. /* TRANSLATORS: fwstart.img may still be good, just it wasn't checked. */
  1431. grub_util_warn ("%s",
  1432. _("fwstart.img doesn't match the known good version. "
  1433. "proceed at your own risk"));
  1434. if (core_size + boot_size > 512 * 1024)
  1435. grub_util_error ("%s", _("firmware image is too big"));
  1436. rom_size = 512 * 1024;
  1437. rom_img = xmalloc (rom_size);
  1438. memset (rom_img, 0, rom_size);
  1439. memcpy (rom_img, boot_img, boot_size);
  1440. memcpy (rom_img + boot_size, core_img, core_size);
  1441. memset (rom_img + boot_size + core_size, 0,
  1442. rom_size - (boot_size + core_size));
  1443. free (core_img);
  1444. core_img = rom_img;
  1445. core_size = rom_size;
  1446. free (boot_img);
  1447. free (boot_path);
  1448. }
  1449. break;
  1450. case IMAGE_QEMU_MIPS_FLASH:
  1451. {
  1452. char *rom_img;
  1453. size_t rom_size;
  1454. if (core_size > 512 * 1024)
  1455. grub_util_error ("%s", _("firmware image is too big"));
  1456. rom_size = 512 * 1024;
  1457. rom_img = xmalloc (rom_size);
  1458. memset (rom_img, 0, rom_size);
  1459. memcpy (rom_img, core_img, core_size);
  1460. memset (rom_img + core_size, 0,
  1461. rom_size - core_size);
  1462. free (core_img);
  1463. core_img = rom_img;
  1464. core_size = rom_size;
  1465. }
  1466. break;
  1467. case IMAGE_UBOOT:
  1468. {
  1469. struct grub_uboot_image_header *hdr;
  1470. hdr = xmalloc (core_size + sizeof (struct grub_uboot_image_header));
  1471. memcpy (hdr + 1, core_img, core_size);
  1472. memset (hdr, 0, sizeof (*hdr));
  1473. hdr->ih_magic = grub_cpu_to_be32_compile_time (GRUB_UBOOT_IH_MAGIC);
  1474. hdr->ih_time = grub_cpu_to_be32 (STABLE_EMBEDDING_TIMESTAMP);
  1475. hdr->ih_size = grub_cpu_to_be32 (core_size);
  1476. hdr->ih_load = 0;
  1477. hdr->ih_ep = 0;
  1478. hdr->ih_type = GRUB_UBOOT_IH_TYPE_KERNEL_NOLOAD;
  1479. hdr->ih_os = GRUB_UBOOT_IH_OS_LINUX;
  1480. hdr->ih_arch = GRUB_UBOOT_IH_ARCH_ARM;
  1481. hdr->ih_comp = GRUB_UBOOT_IH_COMP_NONE;
  1482. grub_crypto_hash (GRUB_MD_CRC32, &hdr->ih_dcrc, hdr + 1, core_size);
  1483. grub_crypto_hash (GRUB_MD_CRC32, &hdr->ih_hcrc, hdr, sizeof (*hdr));
  1484. free (core_img);
  1485. core_img = (char *) hdr;
  1486. core_size += sizeof (struct grub_uboot_image_header);
  1487. }
  1488. break;
  1489. case IMAGE_MIPS_ARC:
  1490. {
  1491. char *ecoff_img;
  1492. struct ecoff_header {
  1493. grub_uint16_t magic;
  1494. grub_uint16_t nsec;
  1495. grub_uint32_t time;
  1496. grub_uint32_t syms;
  1497. grub_uint32_t nsyms;
  1498. grub_uint16_t opt;
  1499. grub_uint16_t flags;
  1500. grub_uint16_t magic2;
  1501. grub_uint16_t version;
  1502. grub_uint32_t textsize;
  1503. grub_uint32_t datasize;
  1504. grub_uint32_t bsssize;
  1505. grub_uint32_t entry;
  1506. grub_uint32_t text_start;
  1507. grub_uint32_t data_start;
  1508. grub_uint32_t bss_start;
  1509. grub_uint32_t gprmask;
  1510. grub_uint32_t cprmask[4];
  1511. grub_uint32_t gp_value;
  1512. };
  1513. struct ecoff_section
  1514. {
  1515. char name[8];
  1516. grub_uint32_t paddr;
  1517. grub_uint32_t vaddr;
  1518. grub_uint32_t size;
  1519. grub_uint32_t file_offset;
  1520. grub_uint32_t reloc;
  1521. grub_uint32_t gp;
  1522. grub_uint16_t nreloc;
  1523. grub_uint16_t ngp;
  1524. grub_uint32_t flags;
  1525. };
  1526. struct ecoff_header *head;
  1527. struct ecoff_section *section;
  1528. grub_uint32_t target_addr;
  1529. size_t program_size;
  1530. program_size = ALIGN_ADDR (core_size);
  1531. if (comp == GRUB_COMPRESSION_NONE)
  1532. target_addr = (image_target->link_addr - decompress_size);
  1533. else
  1534. target_addr = ALIGN_UP (image_target->link_addr
  1535. + layout.kernel_size + total_module_size, 32);
  1536. ecoff_img = xmalloc (program_size + sizeof (*head) + sizeof (*section));
  1537. grub_memset (ecoff_img, 0, program_size + sizeof (*head) + sizeof (*section));
  1538. head = (void *) ecoff_img;
  1539. section = (void *) (head + 1);
  1540. head->magic = image_target->bigendian ? grub_host_to_target16 (0x160)
  1541. : grub_host_to_target16 (0x166);
  1542. head->nsec = grub_host_to_target16 (1);
  1543. head->time = grub_host_to_target32 (0);
  1544. head->opt = grub_host_to_target16 (0x38);
  1545. head->flags = image_target->bigendian
  1546. ? grub_host_to_target16 (0x207)
  1547. : grub_host_to_target16 (0x103);
  1548. head->magic2 = grub_host_to_target16 (0x107);
  1549. head->textsize = grub_host_to_target32 (program_size);
  1550. head->entry = grub_host_to_target32 (target_addr);
  1551. head->text_start = grub_host_to_target32 (target_addr);
  1552. head->data_start = grub_host_to_target32 (target_addr + program_size);
  1553. grub_memcpy (section->name, ".text", sizeof (".text") - 1);
  1554. section->vaddr = grub_host_to_target32 (target_addr);
  1555. section->size = grub_host_to_target32 (program_size);
  1556. section->file_offset = grub_host_to_target32 (sizeof (*head) + sizeof (*section));
  1557. if (!image_target->bigendian)
  1558. {
  1559. section->paddr = grub_host_to_target32 (0xaa60);
  1560. section->flags = grub_host_to_target32 (0x20);
  1561. }
  1562. memcpy (section + 1, core_img, core_size);
  1563. free (core_img);
  1564. core_img = ecoff_img;
  1565. core_size = program_size + sizeof (*head) + sizeof (*section);
  1566. }
  1567. break;
  1568. case IMAGE_LOONGSON_ELF:
  1569. case IMAGE_PPC:
  1570. case IMAGE_XEN:
  1571. case IMAGE_XEN_PVH:
  1572. case IMAGE_COREBOOT:
  1573. case IMAGE_I386_IEEE1275:
  1574. {
  1575. grub_uint64_t target_addr;
  1576. if (image_target->id == IMAGE_LOONGSON_ELF)
  1577. {
  1578. if (comp == GRUB_COMPRESSION_NONE)
  1579. target_addr = (image_target->link_addr - decompress_size);
  1580. else
  1581. target_addr = ALIGN_UP (image_target->link_addr
  1582. + layout.kernel_size + total_module_size, 32);
  1583. }
  1584. else
  1585. target_addr = image_target->link_addr;
  1586. if (image_target->voidp_sizeof == 4)
  1587. grub_mkimage_generate_elf32 (image_target, note, &core_img, &core_size,
  1588. target_addr, &layout);
  1589. else
  1590. grub_mkimage_generate_elf64 (image_target, note, &core_img, &core_size,
  1591. target_addr, &layout);
  1592. }
  1593. break;
  1594. }
  1595. grub_util_write_image (core_img, core_size, out, outname);
  1596. free (core_img);
  1597. free (kernel_path);
  1598. free (layout.reloc_section);
  1599. grub_util_free_path_list (path_list);
  1600. }