mkimage.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  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 = "loongarch64-efi",
  617. .names = { "loongarch64-efi", NULL },
  618. .voidp_sizeof = 8,
  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 = EFI64_HEADER_SIZE,
  628. .pe_target = GRUB_PE32_MACHINE_LOONGARCH64,
  629. .elf_target = EM_LOONGARCH,
  630. },
  631. {
  632. .dirname = "riscv32-efi",
  633. .names = { "riscv32-efi", NULL },
  634. .voidp_sizeof = 4,
  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 = EFI32_HEADER_SIZE,
  644. .pe_target = GRUB_PE32_MACHINE_RISCV32,
  645. .elf_target = EM_RISCV,
  646. },
  647. {
  648. .dirname = "riscv64-efi",
  649. .names = { "riscv64-efi", NULL },
  650. .voidp_sizeof = 8,
  651. .bigendian = 0,
  652. .id = IMAGE_EFI,
  653. .flags = PLATFORM_FLAGS_NONE,
  654. .total_module_size = TARGET_NO_FIELD,
  655. .decompressor_compressed_size = TARGET_NO_FIELD,
  656. .decompressor_uncompressed_size = TARGET_NO_FIELD,
  657. .decompressor_uncompressed_addr = TARGET_NO_FIELD,
  658. .section_align = GRUB_PE32_SECTION_ALIGNMENT,
  659. .vaddr_offset = EFI64_HEADER_SIZE,
  660. .pe_target = GRUB_PE32_MACHINE_RISCV64,
  661. .elf_target = EM_RISCV,
  662. },
  663. };
  664. #include <grub/lib/LzmaEnc.h>
  665. static void *SzAlloc(void *p __attribute__ ((unused)), size_t size) { return xmalloc(size); }
  666. static void SzFree(void *p __attribute__ ((unused)), void *address) { free(address); }
  667. static ISzAlloc g_Alloc = { SzAlloc, SzFree };
  668. static void
  669. compress_kernel_lzma (char *kernel_img, size_t kernel_size,
  670. char **core_img, size_t *core_size)
  671. {
  672. CLzmaEncProps props;
  673. unsigned char out_props[5];
  674. size_t out_props_size = 5;
  675. LzmaEncProps_Init(&props);
  676. props.dictSize = 1 << 16;
  677. props.lc = 3;
  678. props.lp = 0;
  679. props.pb = 2;
  680. props.numThreads = 1;
  681. *core_img = xmalloc (kernel_size);
  682. *core_size = kernel_size;
  683. if (LzmaEncode ((unsigned char *) *core_img, core_size,
  684. (unsigned char *) kernel_img,
  685. kernel_size,
  686. &props, out_props, &out_props_size,
  687. 0, NULL, &g_Alloc, &g_Alloc) != SZ_OK)
  688. grub_util_error ("%s", _("cannot compress the kernel image"));
  689. }
  690. #ifdef USE_LIBLZMA
  691. static void
  692. compress_kernel_xz (char *kernel_img, size_t kernel_size,
  693. char **core_img, size_t *core_size)
  694. {
  695. lzma_stream strm = LZMA_STREAM_INIT;
  696. lzma_ret xzret;
  697. lzma_options_lzma lzopts = {
  698. .dict_size = 1 << 16,
  699. .preset_dict = NULL,
  700. .preset_dict_size = 0,
  701. .lc = 3,
  702. .lp = 0,
  703. .pb = 2,
  704. .mode = LZMA_MODE_NORMAL,
  705. .nice_len = 64,
  706. .mf = LZMA_MF_BT4,
  707. .depth = 0,
  708. };
  709. lzma_filter fltrs[] = {
  710. { .id = LZMA_FILTER_LZMA2, .options = &lzopts},
  711. { .id = LZMA_VLI_UNKNOWN, .options = NULL}
  712. };
  713. xzret = lzma_stream_encoder (&strm, fltrs, LZMA_CHECK_NONE);
  714. if (xzret != LZMA_OK)
  715. grub_util_error ("%s", _("cannot compress the kernel image"));
  716. *core_img = xmalloc (kernel_size);
  717. *core_size = kernel_size;
  718. strm.next_in = (unsigned char *) kernel_img;
  719. strm.avail_in = kernel_size;
  720. strm.next_out = (unsigned char *) *core_img;
  721. strm.avail_out = *core_size;
  722. while (1)
  723. {
  724. xzret = lzma_code (&strm, LZMA_FINISH);
  725. if (xzret == LZMA_OK)
  726. continue;
  727. if (xzret == LZMA_STREAM_END)
  728. break;
  729. grub_util_error ("%s", _("cannot compress the kernel image"));
  730. }
  731. *core_size -= strm.avail_out;
  732. }
  733. #endif
  734. static void
  735. compress_kernel (const struct grub_install_image_target_desc *image_target, char *kernel_img,
  736. size_t kernel_size, char **core_img, size_t *core_size,
  737. grub_compression_t comp)
  738. {
  739. if (image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS
  740. && (comp == GRUB_COMPRESSION_LZMA))
  741. {
  742. compress_kernel_lzma (kernel_img, kernel_size, core_img,
  743. core_size);
  744. return;
  745. }
  746. #ifdef USE_LIBLZMA
  747. if (image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS
  748. && (comp == GRUB_COMPRESSION_XZ))
  749. {
  750. compress_kernel_xz (kernel_img, kernel_size, core_img,
  751. core_size);
  752. return;
  753. }
  754. #endif
  755. if (image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS
  756. && (comp != GRUB_COMPRESSION_NONE))
  757. grub_util_error (_("unknown compression %d"), comp);
  758. *core_img = xmalloc (kernel_size);
  759. memcpy (*core_img, kernel_img, kernel_size);
  760. *core_size = kernel_size;
  761. }
  762. const struct grub_install_image_target_desc *
  763. grub_install_get_image_target (const char *arg)
  764. {
  765. unsigned i, j;
  766. for (i = 0; i < ARRAY_SIZE (image_targets); i++)
  767. for (j = 0; j < ARRAY_SIZE (image_targets[i].names) &&
  768. image_targets[i].names[j]; j++)
  769. if (strcmp (arg, image_targets[i].names[j]) == 0)
  770. return &image_targets[i];
  771. return NULL;
  772. }
  773. const char *
  774. grub_util_get_target_dirname (const struct grub_install_image_target_desc *t)
  775. {
  776. return t->dirname;
  777. }
  778. const char *
  779. grub_util_get_target_name (const struct grub_install_image_target_desc *t)
  780. {
  781. return t->names[0];
  782. }
  783. char *
  784. grub_install_get_image_targets_string (void)
  785. {
  786. int format_len = 0;
  787. char *formats;
  788. char *ptr;
  789. unsigned i;
  790. for (i = 0; i < ARRAY_SIZE (image_targets); i++)
  791. format_len += strlen (image_targets[i].names[0]) + 2;
  792. ptr = formats = xmalloc (format_len);
  793. for (i = 0; i < ARRAY_SIZE (image_targets); i++)
  794. {
  795. strcpy (ptr, image_targets[i].names[0]);
  796. ptr += strlen (image_targets[i].names[0]);
  797. *ptr++ = ',';
  798. *ptr++ = ' ';
  799. }
  800. ptr[-2] = 0;
  801. return formats;
  802. }
  803. /*
  804. * The image_target parameter is used by the grub_host_to_target32() macro.
  805. */
  806. static struct grub_pe32_section_table *
  807. init_pe_section(const struct grub_install_image_target_desc *image_target,
  808. struct grub_pe32_section_table *section,
  809. const char * const name,
  810. grub_uint32_t *vma, grub_uint32_t vsz, grub_uint32_t valign,
  811. grub_uint32_t *rda, grub_uint32_t rsz,
  812. grub_uint32_t characteristics)
  813. {
  814. size_t len = strlen (name);
  815. if (len > sizeof (section->name))
  816. grub_util_error (_("section name %s length is bigger than %lu"),
  817. name, (unsigned long) sizeof (section->name));
  818. memcpy (section->name, name, len);
  819. section->virtual_address = grub_host_to_target32 (*vma);
  820. section->virtual_size = grub_host_to_target32 (vsz);
  821. (*vma) = ALIGN_UP (*vma + vsz, valign);
  822. section->raw_data_offset = grub_host_to_target32 (*rda);
  823. section->raw_data_size = grub_host_to_target32 (rsz);
  824. (*rda) = ALIGN_UP (*rda + rsz, GRUB_PE32_FILE_ALIGNMENT);
  825. section->characteristics = grub_host_to_target32 (characteristics);
  826. return section + 1;
  827. }
  828. /*
  829. * tmp_ is just here so the compiler knows we'll never derefernce a NULL.
  830. * It should get fully optimized away.
  831. */
  832. #define PE_OHDR(o32, o64, field) (*( \
  833. { \
  834. __typeof__((o64)->field) tmp_; \
  835. __typeof__((o64)->field) *ret_ = &tmp_; \
  836. if (o32) \
  837. ret_ = (void *)(&((o32)->field)); \
  838. else if (o64) \
  839. ret_ = (void *)(&((o64)->field)); \
  840. ret_; \
  841. }))
  842. void
  843. grub_install_generate_image (const char *dir, const char *prefix,
  844. FILE *out, const char *outname, char *mods[],
  845. char *memdisk_path, char **pubkey_paths,
  846. size_t npubkeys, char *config_path,
  847. const struct grub_install_image_target_desc *image_target,
  848. int note, grub_compression_t comp, const char *dtb_path,
  849. const char *sbat_path, int disable_shim_lock)
  850. {
  851. char *kernel_img, *core_img;
  852. size_t total_module_size, core_size;
  853. size_t memdisk_size = 0, config_size = 0;
  854. size_t prefix_size = 0, dtb_size = 0, sbat_size = 0;
  855. char *kernel_path;
  856. size_t offset;
  857. struct grub_util_path_list *path_list, *p;
  858. size_t decompress_size = 0;
  859. struct grub_mkimage_layout layout;
  860. if (comp == GRUB_COMPRESSION_AUTO)
  861. comp = image_target->default_compression;
  862. if (image_target->id == IMAGE_I386_PC
  863. || image_target->id == IMAGE_I386_PC_PXE
  864. || image_target->id == IMAGE_I386_PC_ELTORITO)
  865. comp = GRUB_COMPRESSION_LZMA;
  866. path_list = grub_util_resolve_dependencies (dir, "moddep.lst", mods);
  867. kernel_path = grub_util_get_path (dir, "kernel.img");
  868. if (image_target->voidp_sizeof == 8)
  869. total_module_size = sizeof (struct grub_module_info64);
  870. else
  871. total_module_size = sizeof (struct grub_module_info32);
  872. {
  873. size_t i;
  874. for (i = 0; i < npubkeys; i++)
  875. {
  876. size_t curs;
  877. curs = ALIGN_ADDR (grub_util_get_image_size (pubkey_paths[i]));
  878. grub_util_info ("the size of public key %u is 0x%"
  879. GRUB_HOST_PRIxLONG_LONG,
  880. (unsigned) i, (unsigned long long) curs);
  881. total_module_size += curs + sizeof (struct grub_module_header);
  882. }
  883. }
  884. if (memdisk_path)
  885. {
  886. memdisk_size = ALIGN_UP(grub_util_get_image_size (memdisk_path), 512);
  887. grub_util_info ("the size of memory disk is 0x%" GRUB_HOST_PRIxLONG_LONG,
  888. (unsigned long long) memdisk_size);
  889. total_module_size += memdisk_size + sizeof (struct grub_module_header);
  890. }
  891. if (dtb_path)
  892. {
  893. dtb_size = ALIGN_ADDR(grub_util_get_image_size (dtb_path));
  894. total_module_size += dtb_size + sizeof (struct grub_module_header);
  895. }
  896. if (sbat_path != NULL && image_target->id != IMAGE_EFI)
  897. grub_util_error (_(".sbat section can be embedded into EFI images only"));
  898. if (disable_shim_lock)
  899. total_module_size += sizeof (struct grub_module_header);
  900. if (config_path)
  901. {
  902. config_size = ALIGN_ADDR (grub_util_get_image_size (config_path) + 1);
  903. grub_util_info ("the size of config file is 0x%" GRUB_HOST_PRIxLONG_LONG,
  904. (unsigned long long) config_size);
  905. total_module_size += config_size + sizeof (struct grub_module_header);
  906. }
  907. if (prefix)
  908. {
  909. prefix_size = ALIGN_ADDR (strlen (prefix) + 1);
  910. total_module_size += prefix_size + sizeof (struct grub_module_header);
  911. }
  912. for (p = path_list; p; p = p->next)
  913. total_module_size += (ALIGN_ADDR (grub_util_get_image_size (p->name))
  914. + sizeof (struct grub_module_header));
  915. grub_util_info ("the total module size is 0x%" GRUB_HOST_PRIxLONG_LONG,
  916. (unsigned long long) total_module_size);
  917. if (image_target->voidp_sizeof == 4)
  918. kernel_img = grub_mkimage_load_image32 (kernel_path, total_module_size,
  919. &layout, image_target);
  920. else
  921. kernel_img = grub_mkimage_load_image64 (kernel_path, total_module_size,
  922. &layout, image_target);
  923. if ((image_target->id == IMAGE_XEN || image_target->id == IMAGE_XEN_PVH) &&
  924. layout.align < 4096)
  925. layout.align = 4096;
  926. if ((image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS)
  927. && (image_target->total_module_size != TARGET_NO_FIELD))
  928. *((grub_uint32_t *) (kernel_img + image_target->total_module_size))
  929. = grub_host_to_target32 (total_module_size);
  930. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  931. {
  932. memmove (kernel_img + total_module_size, kernel_img, layout.kernel_size);
  933. memset (kernel_img, 0, total_module_size);
  934. }
  935. if (image_target->voidp_sizeof == 8)
  936. {
  937. /* Fill in the grub_module_info structure. */
  938. struct grub_module_info64 *modinfo;
  939. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  940. modinfo = (struct grub_module_info64 *) kernel_img;
  941. else
  942. modinfo = (struct grub_module_info64 *) (kernel_img + layout.kernel_size);
  943. modinfo->magic = grub_host_to_target32 (GRUB_MODULE_MAGIC);
  944. modinfo->offset = grub_host_to_target_addr (sizeof (struct grub_module_info64));
  945. modinfo->size = grub_host_to_target_addr (total_module_size);
  946. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  947. offset = sizeof (struct grub_module_info64);
  948. else
  949. offset = layout.kernel_size + sizeof (struct grub_module_info64);
  950. }
  951. else
  952. {
  953. /* Fill in the grub_module_info structure. */
  954. struct grub_module_info32 *modinfo;
  955. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  956. modinfo = (struct grub_module_info32 *) kernel_img;
  957. else
  958. modinfo = (struct grub_module_info32 *) (kernel_img + layout.kernel_size);
  959. modinfo->magic = grub_host_to_target32 (GRUB_MODULE_MAGIC);
  960. modinfo->offset = grub_host_to_target_addr (sizeof (struct grub_module_info32));
  961. modinfo->size = grub_host_to_target_addr (total_module_size);
  962. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  963. offset = sizeof (struct grub_module_info32);
  964. else
  965. offset = layout.kernel_size + sizeof (struct grub_module_info32);
  966. }
  967. for (p = path_list; p; p = p->next)
  968. {
  969. struct grub_module_header *header;
  970. size_t mod_size;
  971. mod_size = ALIGN_ADDR (grub_util_get_image_size (p->name));
  972. header = (struct grub_module_header *) (kernel_img + offset);
  973. header->type = grub_host_to_target32 (OBJ_TYPE_ELF);
  974. header->size = grub_host_to_target32 (mod_size + sizeof (*header));
  975. offset += sizeof (*header);
  976. grub_util_load_image (p->name, kernel_img + offset);
  977. offset += mod_size;
  978. }
  979. {
  980. size_t i;
  981. for (i = 0; i < npubkeys; i++)
  982. {
  983. size_t curs;
  984. struct grub_module_header *header;
  985. curs = grub_util_get_image_size (pubkey_paths[i]);
  986. header = (struct grub_module_header *) (kernel_img + offset);
  987. header->type = grub_host_to_target32 (OBJ_TYPE_PUBKEY);
  988. header->size = grub_host_to_target32 (curs + sizeof (*header));
  989. offset += sizeof (*header);
  990. grub_util_load_image (pubkey_paths[i], kernel_img + offset);
  991. offset += ALIGN_ADDR (curs);
  992. }
  993. }
  994. if (memdisk_path)
  995. {
  996. struct grub_module_header *header;
  997. header = (struct grub_module_header *) (kernel_img + offset);
  998. header->type = grub_host_to_target32 (OBJ_TYPE_MEMDISK);
  999. header->size = grub_host_to_target32 (memdisk_size + sizeof (*header));
  1000. offset += sizeof (*header);
  1001. grub_util_load_image (memdisk_path, kernel_img + offset);
  1002. offset += memdisk_size;
  1003. }
  1004. if (dtb_path)
  1005. {
  1006. struct grub_module_header *header;
  1007. header = (struct grub_module_header *) (kernel_img + offset);
  1008. header->type = grub_host_to_target32 (OBJ_TYPE_DTB);
  1009. header->size = grub_host_to_target32 (dtb_size + sizeof (*header));
  1010. offset += sizeof (*header);
  1011. grub_util_load_image (dtb_path, kernel_img + offset);
  1012. offset += dtb_size;
  1013. }
  1014. if (disable_shim_lock)
  1015. {
  1016. struct grub_module_header *header;
  1017. header = (struct grub_module_header *) (kernel_img + offset);
  1018. header->type = grub_host_to_target32 (OBJ_TYPE_DISABLE_SHIM_LOCK);
  1019. header->size = grub_host_to_target32 (sizeof (*header));
  1020. offset += sizeof (*header);
  1021. }
  1022. if (config_path)
  1023. {
  1024. struct grub_module_header *header;
  1025. header = (struct grub_module_header *) (kernel_img + offset);
  1026. header->type = grub_host_to_target32 (OBJ_TYPE_CONFIG);
  1027. header->size = grub_host_to_target32 (config_size + sizeof (*header));
  1028. offset += sizeof (*header);
  1029. grub_util_load_image (config_path, kernel_img + offset);
  1030. offset += config_size;
  1031. }
  1032. if (prefix)
  1033. {
  1034. struct grub_module_header *header;
  1035. header = (struct grub_module_header *) (kernel_img + offset);
  1036. header->type = grub_host_to_target32 (OBJ_TYPE_PREFIX);
  1037. header->size = grub_host_to_target32 (prefix_size + sizeof (*header));
  1038. offset += sizeof (*header);
  1039. grub_strcpy (kernel_img + offset, prefix);
  1040. offset += prefix_size;
  1041. }
  1042. grub_util_info ("kernel_img=%p, kernel_size=0x%" GRUB_HOST_PRIxLONG_LONG,
  1043. kernel_img,
  1044. (unsigned long long) layout.kernel_size);
  1045. compress_kernel (image_target, kernel_img, layout.kernel_size + total_module_size,
  1046. &core_img, &core_size, comp);
  1047. free (kernel_img);
  1048. grub_util_info ("the core size is 0x%" GRUB_HOST_PRIxLONG_LONG,
  1049. (unsigned long long) core_size);
  1050. if (!(image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS)
  1051. && image_target->total_module_size != TARGET_NO_FIELD)
  1052. *((grub_uint32_t *) (core_img + image_target->total_module_size))
  1053. = grub_host_to_target32 (total_module_size);
  1054. if (image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS)
  1055. {
  1056. char *full_img;
  1057. size_t full_size;
  1058. char *decompress_path, *decompress_img;
  1059. const char *name;
  1060. switch (comp)
  1061. {
  1062. case GRUB_COMPRESSION_XZ:
  1063. name = "xz_decompress.img";
  1064. break;
  1065. case GRUB_COMPRESSION_LZMA:
  1066. name = "lzma_decompress.img";
  1067. break;
  1068. case GRUB_COMPRESSION_NONE:
  1069. name = "none_decompress.img";
  1070. break;
  1071. default:
  1072. grub_util_error (_("unknown compression %d"), comp);
  1073. }
  1074. decompress_path = grub_util_get_path (dir, name);
  1075. decompress_size = grub_util_get_image_size (decompress_path);
  1076. decompress_img = grub_util_read_image (decompress_path);
  1077. if ((image_target->id == IMAGE_I386_PC
  1078. || image_target->id == IMAGE_I386_PC_PXE
  1079. || image_target->id == IMAGE_I386_PC_ELTORITO)
  1080. && decompress_size > GRUB_KERNEL_I386_PC_LINK_ADDR - 0x8200)
  1081. grub_util_error ("%s", _("Decompressor is too big"));
  1082. if (image_target->decompressor_compressed_size != TARGET_NO_FIELD)
  1083. *((grub_uint32_t *) (decompress_img
  1084. + image_target->decompressor_compressed_size))
  1085. = grub_host_to_target32 (core_size);
  1086. if (image_target->decompressor_uncompressed_size != TARGET_NO_FIELD)
  1087. *((grub_uint32_t *) (decompress_img
  1088. + image_target->decompressor_uncompressed_size))
  1089. = grub_host_to_target32 (layout.kernel_size + total_module_size);
  1090. if (image_target->decompressor_uncompressed_addr != TARGET_NO_FIELD)
  1091. {
  1092. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  1093. *((grub_uint32_t *) (decompress_img + image_target->decompressor_uncompressed_addr))
  1094. = grub_host_to_target_addr (image_target->link_addr - total_module_size);
  1095. else
  1096. *((grub_uint32_t *) (decompress_img + image_target->decompressor_uncompressed_addr))
  1097. = grub_host_to_target_addr (image_target->link_addr);
  1098. }
  1099. full_size = core_size + decompress_size;
  1100. full_img = xmalloc (full_size);
  1101. memcpy (full_img, decompress_img, decompress_size);
  1102. memcpy (full_img + decompress_size, core_img, core_size);
  1103. free (core_img);
  1104. core_img = full_img;
  1105. core_size = full_size;
  1106. free (decompress_img);
  1107. free (decompress_path);
  1108. }
  1109. switch (image_target->id)
  1110. {
  1111. case IMAGE_I386_PC:
  1112. case IMAGE_I386_PC_PXE:
  1113. case IMAGE_I386_PC_ELTORITO:
  1114. if (GRUB_KERNEL_I386_PC_LINK_ADDR + core_size > 0x78000
  1115. || (core_size > (0xffff << GRUB_DISK_SECTOR_BITS))
  1116. || (layout.kernel_size + layout.bss_size
  1117. + GRUB_KERNEL_I386_PC_LINK_ADDR > 0x68000))
  1118. grub_util_error (_("core image is too big (0x%x > 0x%x)"),
  1119. GRUB_KERNEL_I386_PC_LINK_ADDR + (unsigned) core_size,
  1120. 0x78000);
  1121. /* fallthrough */
  1122. case IMAGE_COREBOOT:
  1123. case IMAGE_QEMU:
  1124. if (image_target->elf_target != EM_ARM && layout.kernel_size + layout.bss_size + GRUB_KERNEL_I386_PC_LINK_ADDR > 0x68000)
  1125. grub_util_error (_("kernel image is too big (0x%x > 0x%x)"),
  1126. (unsigned) layout.kernel_size + (unsigned) layout.bss_size
  1127. + GRUB_KERNEL_I386_PC_LINK_ADDR,
  1128. 0x68000);
  1129. break;
  1130. case IMAGE_LOONGSON_ELF:
  1131. case IMAGE_YEELOONG_FLASH:
  1132. case IMAGE_FULOONG2F_FLASH:
  1133. case IMAGE_EFI:
  1134. case IMAGE_MIPS_ARC:
  1135. case IMAGE_QEMU_MIPS_FLASH:
  1136. case IMAGE_XEN:
  1137. case IMAGE_XEN_PVH:
  1138. break;
  1139. case IMAGE_SPARC64_AOUT:
  1140. case IMAGE_SPARC64_RAW:
  1141. case IMAGE_SPARC64_CDCORE:
  1142. case IMAGE_I386_IEEE1275:
  1143. case IMAGE_PPC:
  1144. case IMAGE_UBOOT:
  1145. break;
  1146. }
  1147. switch (image_target->id)
  1148. {
  1149. case IMAGE_I386_PC:
  1150. case IMAGE_I386_PC_PXE:
  1151. case IMAGE_I386_PC_ELTORITO:
  1152. {
  1153. unsigned num;
  1154. char *boot_path, *boot_img;
  1155. size_t boot_size;
  1156. num = ((core_size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS);
  1157. if (image_target->id == IMAGE_I386_PC_PXE)
  1158. {
  1159. char *pxeboot_path, *pxeboot_img;
  1160. size_t pxeboot_size;
  1161. grub_uint32_t *ptr;
  1162. pxeboot_path = grub_util_get_path (dir, "pxeboot.img");
  1163. pxeboot_size = grub_util_get_image_size (pxeboot_path);
  1164. pxeboot_img = grub_util_read_image (pxeboot_path);
  1165. grub_util_write_image (pxeboot_img, pxeboot_size, out,
  1166. outname);
  1167. free (pxeboot_img);
  1168. free (pxeboot_path);
  1169. /* Remove Multiboot header to avoid confusing ipxe. */
  1170. for (ptr = (grub_uint32_t *) core_img;
  1171. ptr < (grub_uint32_t *) (core_img + MULTIBOOT_SEARCH); ptr++)
  1172. if (*ptr == grub_host_to_target32 (MULTIBOOT_HEADER_MAGIC)
  1173. && grub_target_to_host32 (ptr[0])
  1174. + grub_target_to_host32 (ptr[1])
  1175. + grub_target_to_host32 (ptr[2]) == 0)
  1176. {
  1177. *ptr = 0;
  1178. break;
  1179. }
  1180. }
  1181. if (image_target->id == IMAGE_I386_PC_ELTORITO)
  1182. {
  1183. char *eltorito_path, *eltorito_img;
  1184. size_t eltorito_size;
  1185. eltorito_path = grub_util_get_path (dir, "cdboot.img");
  1186. eltorito_size = grub_util_get_image_size (eltorito_path);
  1187. eltorito_img = grub_util_read_image (eltorito_path);
  1188. grub_util_write_image (eltorito_img, eltorito_size, out,
  1189. outname);
  1190. free (eltorito_img);
  1191. free (eltorito_path);
  1192. }
  1193. boot_path = grub_util_get_path (dir, "diskboot.img");
  1194. boot_size = grub_util_get_image_size (boot_path);
  1195. if (boot_size != GRUB_DISK_SECTOR_SIZE)
  1196. grub_util_error (_("diskboot.img size must be %u bytes"),
  1197. GRUB_DISK_SECTOR_SIZE);
  1198. boot_img = grub_util_read_image (boot_path);
  1199. {
  1200. struct grub_pc_bios_boot_blocklist *block;
  1201. block = (struct grub_pc_bios_boot_blocklist *) (boot_img
  1202. + GRUB_DISK_SECTOR_SIZE
  1203. - sizeof (*block));
  1204. block->len = grub_host_to_target16 (num);
  1205. /* This is filled elsewhere. Verify it just in case. */
  1206. assert (block->segment
  1207. == grub_host_to_target16 (GRUB_BOOT_I386_PC_KERNEL_SEG
  1208. + (GRUB_DISK_SECTOR_SIZE >> 4)));
  1209. }
  1210. grub_util_write_image (boot_img, boot_size, out, outname);
  1211. free (boot_img);
  1212. free (boot_path);
  1213. }
  1214. break;
  1215. case IMAGE_EFI:
  1216. {
  1217. char *pe_img, *pe_sbat, *header;
  1218. struct grub_pe32_section_table *section;
  1219. size_t n_sections = 4;
  1220. size_t scn_size;
  1221. grub_uint32_t vma, raw_data;
  1222. size_t pe_size, header_size;
  1223. struct grub_pe32_coff_header *c;
  1224. static const grub_uint8_t stub[] = GRUB_PE32_MSDOS_STUB;
  1225. struct grub_pe32_optional_header *o32 = NULL;
  1226. struct grub_pe64_optional_header *o64 = NULL;
  1227. if (image_target->voidp_sizeof == 4)
  1228. header_size = EFI32_HEADER_SIZE;
  1229. else
  1230. header_size = EFI64_HEADER_SIZE;
  1231. vma = raw_data = header_size;
  1232. if (sbat_path != NULL)
  1233. {
  1234. sbat_size = ALIGN_ADDR (grub_util_get_image_size (sbat_path));
  1235. sbat_size = ALIGN_UP (sbat_size, GRUB_PE32_FILE_ALIGNMENT);
  1236. }
  1237. pe_size = ALIGN_UP (header_size + core_size, GRUB_PE32_FILE_ALIGNMENT) +
  1238. ALIGN_UP (layout.reloc_size, GRUB_PE32_FILE_ALIGNMENT) + sbat_size;
  1239. header = pe_img = xcalloc (1, pe_size);
  1240. memcpy (pe_img + raw_data, core_img, core_size);
  1241. /* The magic. */
  1242. memcpy (header, stub, GRUB_PE32_MSDOS_STUB_SIZE);
  1243. memcpy (header + GRUB_PE32_MSDOS_STUB_SIZE, "PE\0\0",
  1244. GRUB_PE32_SIGNATURE_SIZE);
  1245. /* The COFF file header. */
  1246. c = (struct grub_pe32_coff_header *) (header + GRUB_PE32_MSDOS_STUB_SIZE
  1247. + GRUB_PE32_SIGNATURE_SIZE);
  1248. c->machine = grub_host_to_target16 (image_target->pe_target);
  1249. if (sbat_path != NULL)
  1250. n_sections++;
  1251. c->num_sections = grub_host_to_target16 (n_sections);
  1252. c->time = grub_host_to_target32 (STABLE_EMBEDDING_TIMESTAMP);
  1253. c->characteristics = grub_host_to_target16 (GRUB_PE32_EXECUTABLE_IMAGE
  1254. | GRUB_PE32_LINE_NUMS_STRIPPED
  1255. | ((image_target->voidp_sizeof == 4)
  1256. ? GRUB_PE32_32BIT_MACHINE
  1257. : 0)
  1258. | GRUB_PE32_LOCAL_SYMS_STRIPPED
  1259. | GRUB_PE32_DEBUG_STRIPPED);
  1260. /* The PE Optional header. */
  1261. if (image_target->voidp_sizeof == 4)
  1262. {
  1263. c->optional_header_size = grub_host_to_target16 (sizeof (struct grub_pe32_optional_header));
  1264. o32 = (struct grub_pe32_optional_header *)
  1265. (header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE +
  1266. sizeof (struct grub_pe32_coff_header));
  1267. o32->magic = grub_host_to_target16 (GRUB_PE32_PE32_MAGIC);
  1268. o32->data_base = grub_host_to_target32 (header_size + layout.exec_size);
  1269. section = (struct grub_pe32_section_table *)(o32 + 1);
  1270. }
  1271. else
  1272. {
  1273. c->optional_header_size = grub_host_to_target16 (sizeof (struct grub_pe64_optional_header));
  1274. o64 = (struct grub_pe64_optional_header *)
  1275. (header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE +
  1276. sizeof (struct grub_pe32_coff_header));
  1277. o64->magic = grub_host_to_target16 (GRUB_PE32_PE64_MAGIC);
  1278. section = (struct grub_pe32_section_table *)(o64 + 1);
  1279. }
  1280. #if __GNUC__ >= 12
  1281. #pragma GCC diagnostic push
  1282. #pragma GCC diagnostic ignored "-Wdangling-pointer"
  1283. #endif
  1284. PE_OHDR (o32, o64, header_size) = grub_host_to_target32 (header_size);
  1285. PE_OHDR (o32, o64, entry_addr) = grub_host_to_target32 (layout.start_address);
  1286. PE_OHDR (o32, o64, image_base) = 0;
  1287. PE_OHDR (o32, o64, image_size) = grub_host_to_target32 (pe_size);
  1288. PE_OHDR (o32, o64, section_alignment) = grub_host_to_target32 (image_target->section_align);
  1289. PE_OHDR (o32, o64, file_alignment) = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);
  1290. PE_OHDR (o32, o64, subsystem) = grub_host_to_target16 (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION);
  1291. /* Do these really matter? */
  1292. PE_OHDR (o32, o64, stack_reserve_size) = grub_host_to_target32 (0x10000);
  1293. PE_OHDR (o32, o64, stack_commit_size) = grub_host_to_target32 (0x10000);
  1294. PE_OHDR (o32, o64, heap_reserve_size) = grub_host_to_target32 (0x10000);
  1295. PE_OHDR (o32, o64, heap_commit_size) = grub_host_to_target32 (0x10000);
  1296. PE_OHDR (o32, o64, num_data_directories) = grub_host_to_target32 (GRUB_PE32_NUM_DATA_DIRECTORIES);
  1297. /* The sections. */
  1298. PE_OHDR (o32, o64, code_base) = grub_host_to_target32 (vma);
  1299. PE_OHDR (o32, o64, code_size) = grub_host_to_target32 (layout.exec_size);
  1300. #if __GNUC__ >= 12
  1301. #pragma GCC diagnostic pop
  1302. #endif
  1303. section = init_pe_section (image_target, section, ".text",
  1304. &vma, layout.exec_size,
  1305. image_target->section_align,
  1306. &raw_data, layout.exec_size,
  1307. GRUB_PE32_SCN_CNT_CODE |
  1308. GRUB_PE32_SCN_MEM_EXECUTE |
  1309. GRUB_PE32_SCN_MEM_READ);
  1310. scn_size = ALIGN_UP (layout.kernel_size - layout.exec_size, GRUB_PE32_FILE_ALIGNMENT);
  1311. #if __GNUC__ >= 12
  1312. #pragma GCC diagnostic push
  1313. #pragma GCC diagnostic ignored "-Wdangling-pointer"
  1314. #endif
  1315. /* ALIGN_UP (sbat_size, GRUB_PE32_FILE_ALIGNMENT) is done earlier. */
  1316. PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (scn_size + sbat_size +
  1317. ALIGN_UP (total_module_size,
  1318. GRUB_PE32_FILE_ALIGNMENT));
  1319. #if __GNUC__ >= 12
  1320. #pragma GCC diagnostic pop
  1321. #endif
  1322. section = init_pe_section (image_target, section, ".data",
  1323. &vma, scn_size, image_target->section_align,
  1324. &raw_data, scn_size,
  1325. GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
  1326. GRUB_PE32_SCN_MEM_READ |
  1327. GRUB_PE32_SCN_MEM_WRITE);
  1328. scn_size = pe_size - layout.reloc_size - sbat_size - raw_data;
  1329. section = init_pe_section (image_target, section, "mods",
  1330. &vma, scn_size, image_target->section_align,
  1331. &raw_data, scn_size,
  1332. GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
  1333. GRUB_PE32_SCN_MEM_READ |
  1334. GRUB_PE32_SCN_MEM_WRITE);
  1335. if (sbat_path != NULL)
  1336. {
  1337. pe_sbat = pe_img + raw_data;
  1338. grub_util_load_image (sbat_path, pe_sbat);
  1339. section = init_pe_section (image_target, section, ".sbat",
  1340. &vma, sbat_size,
  1341. image_target->section_align,
  1342. &raw_data, sbat_size,
  1343. GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
  1344. GRUB_PE32_SCN_MEM_READ);
  1345. }
  1346. scn_size = layout.reloc_size;
  1347. #if __GNUC__ >= 12
  1348. #pragma GCC diagnostic push
  1349. #pragma GCC diagnostic ignored "-Wdangling-pointer"
  1350. #endif
  1351. PE_OHDR (o32, o64, base_relocation_table.rva) = grub_host_to_target32 (vma);
  1352. PE_OHDR (o32, o64, base_relocation_table.size) = grub_host_to_target32 (scn_size);
  1353. #if __GNUC__ >= 12
  1354. #pragma GCC diagnostic pop
  1355. #endif
  1356. memcpy (pe_img + raw_data, layout.reloc_section, scn_size);
  1357. init_pe_section (image_target, section, ".reloc",
  1358. &vma, scn_size, image_target->section_align,
  1359. &raw_data, scn_size,
  1360. GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
  1361. GRUB_PE32_SCN_MEM_DISCARDABLE |
  1362. GRUB_PE32_SCN_MEM_READ);
  1363. free (core_img);
  1364. core_img = pe_img;
  1365. core_size = pe_size;
  1366. }
  1367. break;
  1368. case IMAGE_QEMU:
  1369. {
  1370. char *rom_img;
  1371. size_t rom_size;
  1372. char *boot_path, *boot_img;
  1373. size_t boot_size;
  1374. boot_path = grub_util_get_path (dir, "boot.img");
  1375. boot_size = grub_util_get_image_size (boot_path);
  1376. boot_img = grub_util_read_image (boot_path);
  1377. /* Rom sizes must be 64k-aligned. */
  1378. rom_size = ALIGN_UP (core_size + boot_size, 64 * 1024);
  1379. rom_img = xmalloc (rom_size);
  1380. memset (rom_img, 0, rom_size);
  1381. *((grub_int32_t *) (core_img + GRUB_KERNEL_I386_QEMU_CORE_ENTRY_ADDR))
  1382. = grub_host_to_target32 ((grub_uint32_t) -rom_size);
  1383. memcpy (rom_img, core_img, core_size);
  1384. *((grub_int32_t *) (boot_img + GRUB_BOOT_I386_QEMU_CORE_ENTRY_ADDR))
  1385. = grub_host_to_target32 ((grub_uint32_t) -rom_size);
  1386. memcpy (rom_img + rom_size - boot_size, boot_img, boot_size);
  1387. free (core_img);
  1388. core_img = rom_img;
  1389. core_size = rom_size;
  1390. free (boot_img);
  1391. free (boot_path);
  1392. }
  1393. break;
  1394. case IMAGE_SPARC64_AOUT:
  1395. {
  1396. void *aout_img;
  1397. size_t aout_size;
  1398. struct grub_aout32_header *aout_head;
  1399. aout_size = core_size + sizeof (*aout_head);
  1400. aout_img = xmalloc (aout_size);
  1401. aout_head = aout_img;
  1402. grub_memset (aout_head, 0, sizeof (*aout_head));
  1403. aout_head->a_midmag = grub_host_to_target32 ((AOUT_MID_SUN << 16)
  1404. | AOUT32_OMAGIC);
  1405. aout_head->a_text = grub_host_to_target32 (core_size);
  1406. aout_head->a_entry
  1407. = grub_host_to_target32 (GRUB_BOOT_SPARC64_IEEE1275_IMAGE_ADDRESS);
  1408. memcpy ((char *) aout_img + sizeof (*aout_head), core_img, core_size);
  1409. free (core_img);
  1410. core_img = aout_img;
  1411. core_size = aout_size;
  1412. }
  1413. break;
  1414. case IMAGE_SPARC64_RAW:
  1415. {
  1416. unsigned int num;
  1417. char *boot_path, *boot_img;
  1418. size_t boot_size;
  1419. num = ((core_size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS);
  1420. num <<= GRUB_DISK_SECTOR_BITS;
  1421. boot_path = grub_util_get_path (dir, "diskboot.img");
  1422. boot_size = grub_util_get_image_size (boot_path);
  1423. if (boot_size != GRUB_DISK_SECTOR_SIZE)
  1424. grub_util_error (_("diskboot.img size must be %u bytes"),
  1425. GRUB_DISK_SECTOR_SIZE);
  1426. boot_img = grub_util_read_image (boot_path);
  1427. *((grub_uint32_t *) (boot_img + GRUB_DISK_SECTOR_SIZE
  1428. - GRUB_BOOT_SPARC64_IEEE1275_LIST_SIZE + 8))
  1429. = grub_host_to_target32 (num);
  1430. grub_util_write_image (boot_img, boot_size, out, outname);
  1431. free (boot_img);
  1432. free (boot_path);
  1433. }
  1434. break;
  1435. case IMAGE_SPARC64_CDCORE:
  1436. break;
  1437. case IMAGE_YEELOONG_FLASH:
  1438. case IMAGE_FULOONG2F_FLASH:
  1439. {
  1440. char *rom_img;
  1441. size_t rom_size;
  1442. char *boot_path, *boot_img;
  1443. size_t boot_size;
  1444. /* fwstart.img is the only part which can't be tested by using *-elf
  1445. target. Check it against the checksum. */
  1446. const grub_uint8_t yeeloong_fwstart_good_hash[512 / 8] =
  1447. {
  1448. 0x5f, 0x67, 0x46, 0x57, 0x31, 0x30, 0xc5, 0x0a,
  1449. 0xe9, 0x98, 0x18, 0xc9, 0xf3, 0xca, 0x45, 0xa5,
  1450. 0x75, 0x64, 0x6b, 0xbb, 0x24, 0xcd, 0xb4, 0xbc,
  1451. 0xf2, 0x3e, 0x23, 0xf9, 0xc2, 0x6a, 0x8c, 0xde,
  1452. 0x3b, 0x94, 0x9c, 0xcc, 0xa5, 0xa7, 0x58, 0xb1,
  1453. 0xbe, 0x8b, 0x3d, 0x73, 0x98, 0x18, 0x7e, 0x68,
  1454. 0x5e, 0x5f, 0x23, 0x7d, 0x7a, 0xe8, 0x51, 0xf7,
  1455. 0x1a, 0xaf, 0x2f, 0x54, 0x11, 0x2e, 0x5c, 0x25
  1456. };
  1457. const grub_uint8_t fuloong2f_fwstart_good_hash[512 / 8] =
  1458. {
  1459. 0x76, 0x9b, 0xad, 0x6e, 0xa2, 0x39, 0x47, 0x62,
  1460. 0x1f, 0xc9, 0x3a, 0x6d, 0x05, 0x5c, 0x43, 0x5c,
  1461. 0x29, 0x4a, 0x7e, 0x08, 0x2a, 0x31, 0x8f, 0x5d,
  1462. 0x02, 0x84, 0xa0, 0x85, 0xf2, 0xd1, 0xb9, 0x53,
  1463. 0xa2, 0xbc, 0xf2, 0xe1, 0x39, 0x1e, 0x51, 0xb5,
  1464. 0xaf, 0xec, 0x9e, 0xf2, 0xf1, 0xf3, 0x0a, 0x2f,
  1465. 0xe6, 0xf1, 0x08, 0x89, 0xbe, 0xbc, 0x73, 0xab,
  1466. 0x46, 0x50, 0xd6, 0x21, 0xce, 0x8e, 0x24, 0xa7
  1467. };
  1468. const grub_uint8_t *fwstart_good_hash;
  1469. grub_uint8_t fwstart_hash[512 / 8];
  1470. if (image_target->id == IMAGE_FULOONG2F_FLASH)
  1471. {
  1472. fwstart_good_hash = fuloong2f_fwstart_good_hash;
  1473. boot_path = grub_util_get_path (dir, "fwstart_fuloong2f.img");
  1474. }
  1475. else
  1476. {
  1477. fwstart_good_hash = yeeloong_fwstart_good_hash;
  1478. boot_path = grub_util_get_path (dir, "fwstart.img");
  1479. }
  1480. boot_size = grub_util_get_image_size (boot_path);
  1481. boot_img = grub_util_read_image (boot_path);
  1482. grub_crypto_hash (GRUB_MD_SHA512, fwstart_hash, boot_img, boot_size);
  1483. if (grub_memcmp (fwstart_hash, fwstart_good_hash,
  1484. GRUB_MD_SHA512->mdlen) != 0)
  1485. /* TRANSLATORS: fwstart.img may still be good, just it wasn't checked. */
  1486. grub_util_warn ("%s",
  1487. _("fwstart.img doesn't match the known good version. "
  1488. "proceed at your own risk"));
  1489. if (core_size + boot_size > 512 * 1024)
  1490. grub_util_error ("%s", _("firmware image is too big"));
  1491. rom_size = 512 * 1024;
  1492. rom_img = xmalloc (rom_size);
  1493. memset (rom_img, 0, rom_size);
  1494. memcpy (rom_img, boot_img, boot_size);
  1495. memcpy (rom_img + boot_size, core_img, core_size);
  1496. memset (rom_img + boot_size + core_size, 0,
  1497. rom_size - (boot_size + core_size));
  1498. free (core_img);
  1499. core_img = rom_img;
  1500. core_size = rom_size;
  1501. free (boot_img);
  1502. free (boot_path);
  1503. }
  1504. break;
  1505. case IMAGE_QEMU_MIPS_FLASH:
  1506. {
  1507. char *rom_img;
  1508. size_t rom_size;
  1509. if (core_size > 512 * 1024)
  1510. grub_util_error ("%s", _("firmware image is too big"));
  1511. rom_size = 512 * 1024;
  1512. rom_img = xmalloc (rom_size);
  1513. memset (rom_img, 0, rom_size);
  1514. memcpy (rom_img, core_img, core_size);
  1515. memset (rom_img + core_size, 0,
  1516. rom_size - core_size);
  1517. free (core_img);
  1518. core_img = rom_img;
  1519. core_size = rom_size;
  1520. }
  1521. break;
  1522. case IMAGE_UBOOT:
  1523. {
  1524. struct grub_uboot_image_header *hdr;
  1525. hdr = xmalloc (core_size + sizeof (struct grub_uboot_image_header));
  1526. memcpy (hdr + 1, core_img, core_size);
  1527. memset (hdr, 0, sizeof (*hdr));
  1528. hdr->ih_magic = grub_cpu_to_be32_compile_time (GRUB_UBOOT_IH_MAGIC);
  1529. hdr->ih_time = grub_cpu_to_be32 (STABLE_EMBEDDING_TIMESTAMP);
  1530. hdr->ih_size = grub_cpu_to_be32 (core_size);
  1531. hdr->ih_load = 0;
  1532. hdr->ih_ep = 0;
  1533. hdr->ih_type = GRUB_UBOOT_IH_TYPE_KERNEL_NOLOAD;
  1534. hdr->ih_os = GRUB_UBOOT_IH_OS_LINUX;
  1535. hdr->ih_arch = GRUB_UBOOT_IH_ARCH_ARM;
  1536. hdr->ih_comp = GRUB_UBOOT_IH_COMP_NONE;
  1537. grub_crypto_hash (GRUB_MD_CRC32, &hdr->ih_dcrc, hdr + 1, core_size);
  1538. grub_crypto_hash (GRUB_MD_CRC32, &hdr->ih_hcrc, hdr, sizeof (*hdr));
  1539. free (core_img);
  1540. core_img = (char *) hdr;
  1541. core_size += sizeof (struct grub_uboot_image_header);
  1542. }
  1543. break;
  1544. case IMAGE_MIPS_ARC:
  1545. {
  1546. char *ecoff_img;
  1547. struct ecoff_header {
  1548. grub_uint16_t magic;
  1549. grub_uint16_t nsec;
  1550. grub_uint32_t time;
  1551. grub_uint32_t syms;
  1552. grub_uint32_t nsyms;
  1553. grub_uint16_t opt;
  1554. grub_uint16_t flags;
  1555. grub_uint16_t magic2;
  1556. grub_uint16_t version;
  1557. grub_uint32_t textsize;
  1558. grub_uint32_t datasize;
  1559. grub_uint32_t bsssize;
  1560. grub_uint32_t entry;
  1561. grub_uint32_t text_start;
  1562. grub_uint32_t data_start;
  1563. grub_uint32_t bss_start;
  1564. grub_uint32_t gprmask;
  1565. grub_uint32_t cprmask[4];
  1566. grub_uint32_t gp_value;
  1567. };
  1568. struct ecoff_section
  1569. {
  1570. char name[8];
  1571. grub_uint32_t paddr;
  1572. grub_uint32_t vaddr;
  1573. grub_uint32_t size;
  1574. grub_uint32_t file_offset;
  1575. grub_uint32_t reloc;
  1576. grub_uint32_t gp;
  1577. grub_uint16_t nreloc;
  1578. grub_uint16_t ngp;
  1579. grub_uint32_t flags;
  1580. };
  1581. struct ecoff_header *head;
  1582. struct ecoff_section *section;
  1583. grub_uint32_t target_addr;
  1584. size_t program_size;
  1585. program_size = ALIGN_ADDR (core_size);
  1586. if (comp == GRUB_COMPRESSION_NONE)
  1587. target_addr = (image_target->link_addr - decompress_size);
  1588. else
  1589. target_addr = ALIGN_UP (image_target->link_addr
  1590. + layout.kernel_size + total_module_size, 32);
  1591. ecoff_img = xmalloc (program_size + sizeof (*head) + sizeof (*section));
  1592. grub_memset (ecoff_img, 0, program_size + sizeof (*head) + sizeof (*section));
  1593. head = (void *) ecoff_img;
  1594. section = (void *) (head + 1);
  1595. head->magic = image_target->bigendian ? grub_host_to_target16 (0x160)
  1596. : grub_host_to_target16 (0x166);
  1597. head->nsec = grub_host_to_target16 (1);
  1598. head->time = grub_host_to_target32 (0);
  1599. head->opt = grub_host_to_target16 (0x38);
  1600. head->flags = image_target->bigendian
  1601. ? grub_host_to_target16 (0x207)
  1602. : grub_host_to_target16 (0x103);
  1603. head->magic2 = grub_host_to_target16 (0x107);
  1604. head->textsize = grub_host_to_target32 (program_size);
  1605. head->entry = grub_host_to_target32 (target_addr);
  1606. head->text_start = grub_host_to_target32 (target_addr);
  1607. head->data_start = grub_host_to_target32 (target_addr + program_size);
  1608. grub_memcpy (section->name, ".text", sizeof (".text") - 1);
  1609. section->vaddr = grub_host_to_target32 (target_addr);
  1610. section->size = grub_host_to_target32 (program_size);
  1611. section->file_offset = grub_host_to_target32 (sizeof (*head) + sizeof (*section));
  1612. if (!image_target->bigendian)
  1613. {
  1614. section->paddr = grub_host_to_target32 (0xaa60);
  1615. section->flags = grub_host_to_target32 (0x20);
  1616. }
  1617. memcpy (section + 1, core_img, core_size);
  1618. free (core_img);
  1619. core_img = ecoff_img;
  1620. core_size = program_size + sizeof (*head) + sizeof (*section);
  1621. }
  1622. break;
  1623. case IMAGE_LOONGSON_ELF:
  1624. case IMAGE_PPC:
  1625. case IMAGE_XEN:
  1626. case IMAGE_XEN_PVH:
  1627. case IMAGE_COREBOOT:
  1628. case IMAGE_I386_IEEE1275:
  1629. {
  1630. grub_uint64_t target_addr;
  1631. if (image_target->id == IMAGE_LOONGSON_ELF)
  1632. {
  1633. if (comp == GRUB_COMPRESSION_NONE)
  1634. target_addr = (image_target->link_addr - decompress_size);
  1635. else
  1636. target_addr = ALIGN_UP (image_target->link_addr
  1637. + layout.kernel_size + total_module_size, 32);
  1638. }
  1639. else
  1640. target_addr = image_target->link_addr;
  1641. if (image_target->voidp_sizeof == 4)
  1642. grub_mkimage_generate_elf32 (image_target, note, &core_img, &core_size,
  1643. target_addr, &layout);
  1644. else
  1645. grub_mkimage_generate_elf64 (image_target, note, &core_img, &core_size,
  1646. target_addr, &layout);
  1647. }
  1648. break;
  1649. }
  1650. grub_util_write_image (core_img, core_size, out, outname);
  1651. free (core_img);
  1652. free (kernel_path);
  1653. free (layout.reloc_section);
  1654. grub_util_free_path_list (path_list);
  1655. }