mkimage.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  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. int disable_cli)
  851. {
  852. char *kernel_img, *core_img;
  853. size_t total_module_size, core_size;
  854. size_t memdisk_size = 0, config_size = 0;
  855. size_t prefix_size = 0, dtb_size = 0, sbat_size = 0;
  856. char *kernel_path;
  857. size_t offset;
  858. struct grub_util_path_list *path_list, *p;
  859. size_t decompress_size = 0;
  860. struct grub_mkimage_layout layout;
  861. if (comp == GRUB_COMPRESSION_AUTO)
  862. comp = image_target->default_compression;
  863. if (image_target->id == IMAGE_I386_PC
  864. || image_target->id == IMAGE_I386_PC_PXE
  865. || image_target->id == IMAGE_I386_PC_ELTORITO)
  866. comp = GRUB_COMPRESSION_LZMA;
  867. path_list = grub_util_resolve_dependencies (dir, "moddep.lst", mods);
  868. kernel_path = grub_util_get_path (dir, "kernel.img");
  869. if (image_target->voidp_sizeof == 8)
  870. total_module_size = sizeof (struct grub_module_info64);
  871. else
  872. total_module_size = sizeof (struct grub_module_info32);
  873. {
  874. size_t i;
  875. for (i = 0; i < npubkeys; i++)
  876. {
  877. size_t curs;
  878. curs = ALIGN_ADDR (grub_util_get_image_size (pubkey_paths[i]));
  879. grub_util_info ("the size of public key %u is 0x%"
  880. GRUB_HOST_PRIxLONG_LONG,
  881. (unsigned) i, (unsigned long long) curs);
  882. total_module_size += curs + sizeof (struct grub_module_header);
  883. }
  884. }
  885. if (memdisk_path)
  886. {
  887. memdisk_size = ALIGN_UP(grub_util_get_image_size (memdisk_path), 512);
  888. grub_util_info ("the size of memory disk is 0x%" GRUB_HOST_PRIxLONG_LONG,
  889. (unsigned long long) memdisk_size);
  890. total_module_size += memdisk_size + sizeof (struct grub_module_header);
  891. }
  892. if (dtb_path)
  893. {
  894. dtb_size = ALIGN_ADDR(grub_util_get_image_size (dtb_path));
  895. total_module_size += dtb_size + sizeof (struct grub_module_header);
  896. }
  897. if (sbat_path != NULL && (image_target->id != IMAGE_EFI && image_target->id != IMAGE_PPC))
  898. grub_util_error (_("SBAT data can be added only to EFI or powerpc-ieee1275 images"));
  899. if (disable_shim_lock)
  900. total_module_size += sizeof (struct grub_module_header);
  901. if (disable_cli)
  902. total_module_size += sizeof (struct grub_module_header);
  903. if (config_path)
  904. {
  905. config_size = ALIGN_ADDR (grub_util_get_image_size (config_path) + 1);
  906. grub_util_info ("the size of config file is 0x%" GRUB_HOST_PRIxLONG_LONG,
  907. (unsigned long long) config_size);
  908. total_module_size += config_size + sizeof (struct grub_module_header);
  909. }
  910. if (prefix)
  911. {
  912. prefix_size = ALIGN_ADDR (strlen (prefix) + 1);
  913. total_module_size += prefix_size + sizeof (struct grub_module_header);
  914. }
  915. for (p = path_list; p; p = p->next)
  916. total_module_size += (ALIGN_ADDR (grub_util_get_image_size (p->name))
  917. + sizeof (struct grub_module_header));
  918. grub_util_info ("the total module size is 0x%" GRUB_HOST_PRIxLONG_LONG,
  919. (unsigned long long) total_module_size);
  920. if (image_target->voidp_sizeof == 4)
  921. kernel_img = grub_mkimage_load_image32 (kernel_path, total_module_size,
  922. &layout, image_target);
  923. else
  924. kernel_img = grub_mkimage_load_image64 (kernel_path, total_module_size,
  925. &layout, image_target);
  926. if ((image_target->id == IMAGE_XEN || image_target->id == IMAGE_XEN_PVH) &&
  927. layout.align < 4096)
  928. layout.align = 4096;
  929. if ((image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS)
  930. && (image_target->total_module_size != TARGET_NO_FIELD))
  931. *((grub_uint32_t *) (kernel_img + image_target->total_module_size))
  932. = grub_host_to_target32 (total_module_size);
  933. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  934. {
  935. memmove (kernel_img + total_module_size, kernel_img, layout.kernel_size);
  936. memset (kernel_img, 0, total_module_size);
  937. }
  938. if (image_target->voidp_sizeof == 8)
  939. {
  940. /* Fill in the grub_module_info structure. */
  941. struct grub_module_info64 *modinfo;
  942. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  943. modinfo = (struct grub_module_info64 *) kernel_img;
  944. else
  945. modinfo = (struct grub_module_info64 *) (kernel_img + layout.kernel_size);
  946. modinfo->magic = grub_host_to_target32 (GRUB_MODULE_MAGIC);
  947. modinfo->offset = grub_host_to_target_addr (sizeof (struct grub_module_info64));
  948. modinfo->size = grub_host_to_target_addr (total_module_size);
  949. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  950. offset = sizeof (struct grub_module_info64);
  951. else
  952. offset = layout.kernel_size + sizeof (struct grub_module_info64);
  953. }
  954. else
  955. {
  956. /* Fill in the grub_module_info structure. */
  957. struct grub_module_info32 *modinfo;
  958. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  959. modinfo = (struct grub_module_info32 *) kernel_img;
  960. else
  961. modinfo = (struct grub_module_info32 *) (kernel_img + layout.kernel_size);
  962. modinfo->magic = grub_host_to_target32 (GRUB_MODULE_MAGIC);
  963. modinfo->offset = grub_host_to_target_addr (sizeof (struct grub_module_info32));
  964. modinfo->size = grub_host_to_target_addr (total_module_size);
  965. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  966. offset = sizeof (struct grub_module_info32);
  967. else
  968. offset = layout.kernel_size + sizeof (struct grub_module_info32);
  969. }
  970. for (p = path_list; p; p = p->next)
  971. {
  972. struct grub_module_header *header;
  973. size_t mod_size;
  974. mod_size = ALIGN_ADDR (grub_util_get_image_size (p->name));
  975. header = (struct grub_module_header *) (kernel_img + offset);
  976. header->type = grub_host_to_target32 (OBJ_TYPE_ELF);
  977. header->size = grub_host_to_target32 (mod_size + sizeof (*header));
  978. offset += sizeof (*header);
  979. grub_util_load_image (p->name, kernel_img + offset);
  980. offset += mod_size;
  981. }
  982. {
  983. size_t i;
  984. for (i = 0; i < npubkeys; i++)
  985. {
  986. size_t curs;
  987. struct grub_module_header *header;
  988. curs = grub_util_get_image_size (pubkey_paths[i]);
  989. header = (struct grub_module_header *) (kernel_img + offset);
  990. header->type = grub_host_to_target32 (OBJ_TYPE_PUBKEY);
  991. header->size = grub_host_to_target32 (curs + sizeof (*header));
  992. offset += sizeof (*header);
  993. grub_util_load_image (pubkey_paths[i], kernel_img + offset);
  994. offset += ALIGN_ADDR (curs);
  995. }
  996. }
  997. if (memdisk_path)
  998. {
  999. struct grub_module_header *header;
  1000. header = (struct grub_module_header *) (kernel_img + offset);
  1001. header->type = grub_host_to_target32 (OBJ_TYPE_MEMDISK);
  1002. header->size = grub_host_to_target32 (memdisk_size + sizeof (*header));
  1003. offset += sizeof (*header);
  1004. grub_util_load_image (memdisk_path, kernel_img + offset);
  1005. offset += memdisk_size;
  1006. }
  1007. if (dtb_path)
  1008. {
  1009. struct grub_module_header *header;
  1010. header = (struct grub_module_header *) (kernel_img + offset);
  1011. header->type = grub_host_to_target32 (OBJ_TYPE_DTB);
  1012. header->size = grub_host_to_target32 (dtb_size + sizeof (*header));
  1013. offset += sizeof (*header);
  1014. grub_util_load_image (dtb_path, kernel_img + offset);
  1015. offset += dtb_size;
  1016. }
  1017. if (disable_shim_lock)
  1018. {
  1019. struct grub_module_header *header;
  1020. header = (struct grub_module_header *) (kernel_img + offset);
  1021. header->type = grub_host_to_target32 (OBJ_TYPE_DISABLE_SHIM_LOCK);
  1022. header->size = grub_host_to_target32 (sizeof (*header));
  1023. offset += sizeof (*header);
  1024. }
  1025. if (disable_cli)
  1026. {
  1027. struct grub_module_header *header;
  1028. header = (struct grub_module_header *) (kernel_img + offset);
  1029. header->type = grub_host_to_target32 (OBJ_TYPE_DISABLE_CLI);
  1030. header->size = grub_host_to_target32 (sizeof (*header));
  1031. offset += sizeof (*header);
  1032. }
  1033. if (config_path)
  1034. {
  1035. struct grub_module_header *header;
  1036. header = (struct grub_module_header *) (kernel_img + offset);
  1037. header->type = grub_host_to_target32 (OBJ_TYPE_CONFIG);
  1038. header->size = grub_host_to_target32 (config_size + sizeof (*header));
  1039. offset += sizeof (*header);
  1040. grub_util_load_image (config_path, kernel_img + offset);
  1041. offset += config_size;
  1042. }
  1043. if (prefix)
  1044. {
  1045. struct grub_module_header *header;
  1046. header = (struct grub_module_header *) (kernel_img + offset);
  1047. header->type = grub_host_to_target32 (OBJ_TYPE_PREFIX);
  1048. header->size = grub_host_to_target32 (prefix_size + sizeof (*header));
  1049. offset += sizeof (*header);
  1050. grub_strcpy (kernel_img + offset, prefix);
  1051. offset += prefix_size;
  1052. }
  1053. grub_util_info ("kernel_img=%p, kernel_size=0x%" GRUB_HOST_PRIxLONG_LONG,
  1054. kernel_img,
  1055. (unsigned long long) layout.kernel_size);
  1056. compress_kernel (image_target, kernel_img, layout.kernel_size + total_module_size,
  1057. &core_img, &core_size, comp);
  1058. free (kernel_img);
  1059. grub_util_info ("the core size is 0x%" GRUB_HOST_PRIxLONG_LONG,
  1060. (unsigned long long) core_size);
  1061. if (!(image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS)
  1062. && image_target->total_module_size != TARGET_NO_FIELD)
  1063. *((grub_uint32_t *) (core_img + image_target->total_module_size))
  1064. = grub_host_to_target32 (total_module_size);
  1065. if (image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS)
  1066. {
  1067. char *full_img;
  1068. size_t full_size;
  1069. char *decompress_path, *decompress_img;
  1070. const char *name;
  1071. switch (comp)
  1072. {
  1073. case GRUB_COMPRESSION_XZ:
  1074. name = "xz_decompress.img";
  1075. break;
  1076. case GRUB_COMPRESSION_LZMA:
  1077. name = "lzma_decompress.img";
  1078. break;
  1079. case GRUB_COMPRESSION_NONE:
  1080. name = "none_decompress.img";
  1081. break;
  1082. default:
  1083. grub_util_error (_("unknown compression %d"), comp);
  1084. }
  1085. decompress_path = grub_util_get_path (dir, name);
  1086. decompress_size = grub_util_get_image_size (decompress_path);
  1087. decompress_img = grub_util_read_image (decompress_path);
  1088. if ((image_target->id == IMAGE_I386_PC
  1089. || image_target->id == IMAGE_I386_PC_PXE
  1090. || image_target->id == IMAGE_I386_PC_ELTORITO)
  1091. && decompress_size > GRUB_KERNEL_I386_PC_LINK_ADDR - 0x8200)
  1092. grub_util_error ("%s", _("Decompressor is too big"));
  1093. if (image_target->decompressor_compressed_size != TARGET_NO_FIELD)
  1094. *((grub_uint32_t *) (decompress_img
  1095. + image_target->decompressor_compressed_size))
  1096. = grub_host_to_target32 (core_size);
  1097. if (image_target->decompressor_uncompressed_size != TARGET_NO_FIELD)
  1098. *((grub_uint32_t *) (decompress_img
  1099. + image_target->decompressor_uncompressed_size))
  1100. = grub_host_to_target32 (layout.kernel_size + total_module_size);
  1101. if (image_target->decompressor_uncompressed_addr != TARGET_NO_FIELD)
  1102. {
  1103. if (image_target->flags & PLATFORM_FLAGS_MODULES_BEFORE_KERNEL)
  1104. *((grub_uint32_t *) (decompress_img + image_target->decompressor_uncompressed_addr))
  1105. = grub_host_to_target_addr (image_target->link_addr - total_module_size);
  1106. else
  1107. *((grub_uint32_t *) (decompress_img + image_target->decompressor_uncompressed_addr))
  1108. = grub_host_to_target_addr (image_target->link_addr);
  1109. }
  1110. full_size = core_size + decompress_size;
  1111. full_img = xmalloc (full_size);
  1112. memcpy (full_img, decompress_img, decompress_size);
  1113. memcpy (full_img + decompress_size, core_img, core_size);
  1114. free (core_img);
  1115. core_img = full_img;
  1116. core_size = full_size;
  1117. free (decompress_img);
  1118. free (decompress_path);
  1119. }
  1120. switch (image_target->id)
  1121. {
  1122. case IMAGE_I386_PC:
  1123. case IMAGE_I386_PC_PXE:
  1124. case IMAGE_I386_PC_ELTORITO:
  1125. if (GRUB_KERNEL_I386_PC_LINK_ADDR + core_size > 0x78000
  1126. || (core_size > (0xffff << GRUB_DISK_SECTOR_BITS))
  1127. || (layout.kernel_size + layout.bss_size
  1128. + GRUB_KERNEL_I386_PC_LINK_ADDR > 0x68000))
  1129. grub_util_error (_("core image is too big (0x%x > 0x%x)"),
  1130. GRUB_KERNEL_I386_PC_LINK_ADDR + (unsigned) core_size,
  1131. 0x78000);
  1132. /* fallthrough */
  1133. case IMAGE_COREBOOT:
  1134. case IMAGE_QEMU:
  1135. if (image_target->elf_target != EM_ARM && layout.kernel_size + layout.bss_size + GRUB_KERNEL_I386_PC_LINK_ADDR > 0x68000)
  1136. grub_util_error (_("kernel image is too big (0x%x > 0x%x)"),
  1137. (unsigned) layout.kernel_size + (unsigned) layout.bss_size
  1138. + GRUB_KERNEL_I386_PC_LINK_ADDR,
  1139. 0x68000);
  1140. break;
  1141. case IMAGE_LOONGSON_ELF:
  1142. case IMAGE_YEELOONG_FLASH:
  1143. case IMAGE_FULOONG2F_FLASH:
  1144. case IMAGE_EFI:
  1145. case IMAGE_MIPS_ARC:
  1146. case IMAGE_QEMU_MIPS_FLASH:
  1147. case IMAGE_XEN:
  1148. case IMAGE_XEN_PVH:
  1149. break;
  1150. case IMAGE_SPARC64_AOUT:
  1151. case IMAGE_SPARC64_RAW:
  1152. case IMAGE_SPARC64_CDCORE:
  1153. case IMAGE_I386_IEEE1275:
  1154. case IMAGE_PPC:
  1155. case IMAGE_UBOOT:
  1156. break;
  1157. }
  1158. switch (image_target->id)
  1159. {
  1160. case IMAGE_I386_PC:
  1161. case IMAGE_I386_PC_PXE:
  1162. case IMAGE_I386_PC_ELTORITO:
  1163. {
  1164. unsigned num;
  1165. char *boot_path, *boot_img;
  1166. size_t boot_size;
  1167. num = ((core_size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS);
  1168. if (image_target->id == IMAGE_I386_PC_PXE)
  1169. {
  1170. char *pxeboot_path, *pxeboot_img;
  1171. size_t pxeboot_size;
  1172. grub_uint32_t *ptr;
  1173. pxeboot_path = grub_util_get_path (dir, "pxeboot.img");
  1174. pxeboot_size = grub_util_get_image_size (pxeboot_path);
  1175. pxeboot_img = grub_util_read_image (pxeboot_path);
  1176. grub_util_write_image (pxeboot_img, pxeboot_size, out,
  1177. outname);
  1178. free (pxeboot_img);
  1179. free (pxeboot_path);
  1180. /* Remove Multiboot header to avoid confusing ipxe. */
  1181. for (ptr = (grub_uint32_t *) core_img;
  1182. ptr < (grub_uint32_t *) (core_img + MULTIBOOT_SEARCH); ptr++)
  1183. if (*ptr == grub_host_to_target32 (MULTIBOOT_HEADER_MAGIC)
  1184. && grub_target_to_host32 (ptr[0])
  1185. + grub_target_to_host32 (ptr[1])
  1186. + grub_target_to_host32 (ptr[2]) == 0)
  1187. {
  1188. *ptr = 0;
  1189. break;
  1190. }
  1191. }
  1192. if (image_target->id == IMAGE_I386_PC_ELTORITO)
  1193. {
  1194. char *eltorito_path, *eltorito_img;
  1195. size_t eltorito_size;
  1196. eltorito_path = grub_util_get_path (dir, "cdboot.img");
  1197. eltorito_size = grub_util_get_image_size (eltorito_path);
  1198. eltorito_img = grub_util_read_image (eltorito_path);
  1199. grub_util_write_image (eltorito_img, eltorito_size, out,
  1200. outname);
  1201. free (eltorito_img);
  1202. free (eltorito_path);
  1203. }
  1204. boot_path = grub_util_get_path (dir, "diskboot.img");
  1205. boot_size = grub_util_get_image_size (boot_path);
  1206. if (boot_size != GRUB_DISK_SECTOR_SIZE)
  1207. grub_util_error (_("diskboot.img size must be %u bytes"),
  1208. GRUB_DISK_SECTOR_SIZE);
  1209. boot_img = grub_util_read_image (boot_path);
  1210. {
  1211. struct grub_pc_bios_boot_blocklist *block;
  1212. block = (struct grub_pc_bios_boot_blocklist *) (boot_img
  1213. + GRUB_DISK_SECTOR_SIZE
  1214. - sizeof (*block));
  1215. block->len = grub_host_to_target16 (num);
  1216. /* This is filled elsewhere. Verify it just in case. */
  1217. assert (block->segment
  1218. == grub_host_to_target16 (GRUB_BOOT_I386_PC_KERNEL_SEG
  1219. + (GRUB_DISK_SECTOR_SIZE >> 4)));
  1220. }
  1221. grub_util_write_image (boot_img, boot_size, out, outname);
  1222. free (boot_img);
  1223. free (boot_path);
  1224. }
  1225. break;
  1226. case IMAGE_EFI:
  1227. {
  1228. char *pe_img, *pe_sbat, *header;
  1229. struct grub_pe32_section_table *section;
  1230. size_t n_sections = 4;
  1231. size_t scn_size;
  1232. grub_uint32_t vma, raw_data;
  1233. size_t pe_size, header_size;
  1234. struct grub_pe32_coff_header *c;
  1235. static const grub_uint8_t stub[] = GRUB_PE32_MSDOS_STUB;
  1236. struct grub_pe32_optional_header *o32 = NULL;
  1237. struct grub_pe64_optional_header *o64 = NULL;
  1238. if (image_target->voidp_sizeof == 4)
  1239. header_size = EFI32_HEADER_SIZE;
  1240. else
  1241. header_size = EFI64_HEADER_SIZE;
  1242. vma = raw_data = header_size;
  1243. if (sbat_path != NULL)
  1244. {
  1245. sbat_size = ALIGN_ADDR (grub_util_get_image_size (sbat_path));
  1246. sbat_size = ALIGN_UP (sbat_size, GRUB_PE32_FILE_ALIGNMENT);
  1247. }
  1248. pe_size = ALIGN_UP (header_size + core_size, GRUB_PE32_FILE_ALIGNMENT) +
  1249. ALIGN_UP (layout.reloc_size, GRUB_PE32_FILE_ALIGNMENT) + sbat_size;
  1250. header = pe_img = xcalloc (1, pe_size);
  1251. memcpy (pe_img + raw_data, core_img, core_size);
  1252. /* The magic. */
  1253. memcpy (header, stub, GRUB_PE32_MSDOS_STUB_SIZE);
  1254. memcpy (header + GRUB_PE32_MSDOS_STUB_SIZE, "PE\0\0",
  1255. GRUB_PE32_SIGNATURE_SIZE);
  1256. /* The COFF file header. */
  1257. c = (struct grub_pe32_coff_header *) (header + GRUB_PE32_MSDOS_STUB_SIZE
  1258. + GRUB_PE32_SIGNATURE_SIZE);
  1259. c->machine = grub_host_to_target16 (image_target->pe_target);
  1260. if (sbat_path != NULL)
  1261. n_sections++;
  1262. c->num_sections = grub_host_to_target16 (n_sections);
  1263. c->time = grub_host_to_target32 (STABLE_EMBEDDING_TIMESTAMP);
  1264. c->characteristics = grub_host_to_target16 (GRUB_PE32_EXECUTABLE_IMAGE
  1265. | GRUB_PE32_LINE_NUMS_STRIPPED
  1266. | ((image_target->voidp_sizeof == 4)
  1267. ? GRUB_PE32_32BIT_MACHINE
  1268. : 0)
  1269. | GRUB_PE32_LOCAL_SYMS_STRIPPED
  1270. | GRUB_PE32_DEBUG_STRIPPED);
  1271. /* The PE Optional header. */
  1272. if (image_target->voidp_sizeof == 4)
  1273. {
  1274. c->optional_header_size = grub_host_to_target16 (sizeof (struct grub_pe32_optional_header));
  1275. o32 = (struct grub_pe32_optional_header *)
  1276. (header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE +
  1277. sizeof (struct grub_pe32_coff_header));
  1278. o32->magic = grub_host_to_target16 (GRUB_PE32_PE32_MAGIC);
  1279. o32->data_base = grub_host_to_target32 (header_size + layout.exec_size);
  1280. section = (struct grub_pe32_section_table *)(o32 + 1);
  1281. }
  1282. else
  1283. {
  1284. c->optional_header_size = grub_host_to_target16 (sizeof (struct grub_pe64_optional_header));
  1285. o64 = (struct grub_pe64_optional_header *)
  1286. (header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE +
  1287. sizeof (struct grub_pe32_coff_header));
  1288. o64->magic = grub_host_to_target16 (GRUB_PE32_PE64_MAGIC);
  1289. section = (struct grub_pe32_section_table *)(o64 + 1);
  1290. }
  1291. #if __GNUC__ >= 12
  1292. #pragma GCC diagnostic push
  1293. #pragma GCC diagnostic ignored "-Wdangling-pointer"
  1294. #endif
  1295. PE_OHDR (o32, o64, dll_characteristics) = grub_host_to_target16 (GRUB_PE32_NX_COMPAT);
  1296. PE_OHDR (o32, o64, header_size) = grub_host_to_target32 (header_size);
  1297. PE_OHDR (o32, o64, entry_addr) = grub_host_to_target32 (layout.start_address);
  1298. PE_OHDR (o32, o64, image_base) = 0;
  1299. PE_OHDR (o32, o64, image_size) = grub_host_to_target32 (pe_size);
  1300. PE_OHDR (o32, o64, section_alignment) = grub_host_to_target32 (image_target->section_align);
  1301. PE_OHDR (o32, o64, file_alignment) = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);
  1302. PE_OHDR (o32, o64, subsystem) = grub_host_to_target16 (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION);
  1303. /* Do these really matter? */
  1304. PE_OHDR (o32, o64, stack_reserve_size) = grub_host_to_target32 (0x10000);
  1305. PE_OHDR (o32, o64, stack_commit_size) = grub_host_to_target32 (0x10000);
  1306. PE_OHDR (o32, o64, heap_reserve_size) = grub_host_to_target32 (0x10000);
  1307. PE_OHDR (o32, o64, heap_commit_size) = grub_host_to_target32 (0x10000);
  1308. PE_OHDR (o32, o64, num_data_directories) = grub_host_to_target32 (GRUB_PE32_NUM_DATA_DIRECTORIES);
  1309. /* The sections. */
  1310. PE_OHDR (o32, o64, code_base) = grub_host_to_target32 (vma);
  1311. PE_OHDR (o32, o64, code_size) = grub_host_to_target32 (layout.exec_size);
  1312. #if __GNUC__ >= 12
  1313. #pragma GCC diagnostic pop
  1314. #endif
  1315. section = init_pe_section (image_target, section, ".text",
  1316. &vma, layout.exec_size,
  1317. image_target->section_align,
  1318. &raw_data, layout.exec_size,
  1319. GRUB_PE32_SCN_CNT_CODE |
  1320. GRUB_PE32_SCN_MEM_EXECUTE |
  1321. GRUB_PE32_SCN_MEM_READ);
  1322. scn_size = ALIGN_UP (layout.kernel_size - layout.exec_size, GRUB_PE32_FILE_ALIGNMENT);
  1323. #if __GNUC__ >= 12
  1324. #pragma GCC diagnostic push
  1325. #pragma GCC diagnostic ignored "-Wdangling-pointer"
  1326. #endif
  1327. /* ALIGN_UP (sbat_size, GRUB_PE32_FILE_ALIGNMENT) is done earlier. */
  1328. PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (scn_size + sbat_size +
  1329. ALIGN_UP (total_module_size,
  1330. GRUB_PE32_FILE_ALIGNMENT));
  1331. #if __GNUC__ >= 12
  1332. #pragma GCC diagnostic pop
  1333. #endif
  1334. section = init_pe_section (image_target, section, ".data",
  1335. &vma, scn_size, image_target->section_align,
  1336. &raw_data, scn_size,
  1337. GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
  1338. GRUB_PE32_SCN_MEM_READ |
  1339. GRUB_PE32_SCN_MEM_WRITE);
  1340. scn_size = pe_size - layout.reloc_size - sbat_size - raw_data;
  1341. section = init_pe_section (image_target, section, "mods",
  1342. &vma, scn_size, image_target->section_align,
  1343. &raw_data, scn_size,
  1344. GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
  1345. GRUB_PE32_SCN_MEM_READ |
  1346. GRUB_PE32_SCN_MEM_WRITE);
  1347. if (sbat_path != NULL)
  1348. {
  1349. pe_sbat = pe_img + raw_data;
  1350. grub_util_load_image (sbat_path, pe_sbat);
  1351. section = init_pe_section (image_target, section, ".sbat",
  1352. &vma, sbat_size,
  1353. image_target->section_align,
  1354. &raw_data, sbat_size,
  1355. GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
  1356. GRUB_PE32_SCN_MEM_READ);
  1357. }
  1358. scn_size = layout.reloc_size;
  1359. #if __GNUC__ >= 12
  1360. #pragma GCC diagnostic push
  1361. #pragma GCC diagnostic ignored "-Wdangling-pointer"
  1362. #endif
  1363. PE_OHDR (o32, o64, base_relocation_table.rva) = grub_host_to_target32 (vma);
  1364. PE_OHDR (o32, o64, base_relocation_table.size) = grub_host_to_target32 (scn_size);
  1365. #if __GNUC__ >= 12
  1366. #pragma GCC diagnostic pop
  1367. #endif
  1368. memcpy (pe_img + raw_data, layout.reloc_section, scn_size);
  1369. init_pe_section (image_target, section, ".reloc",
  1370. &vma, scn_size, image_target->section_align,
  1371. &raw_data, scn_size,
  1372. GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
  1373. GRUB_PE32_SCN_MEM_DISCARDABLE |
  1374. GRUB_PE32_SCN_MEM_READ);
  1375. free (core_img);
  1376. core_img = pe_img;
  1377. core_size = pe_size;
  1378. }
  1379. break;
  1380. case IMAGE_QEMU:
  1381. {
  1382. char *rom_img;
  1383. size_t rom_size;
  1384. char *boot_path, *boot_img;
  1385. size_t boot_size;
  1386. boot_path = grub_util_get_path (dir, "boot.img");
  1387. boot_size = grub_util_get_image_size (boot_path);
  1388. boot_img = grub_util_read_image (boot_path);
  1389. /* Rom sizes must be 64k-aligned. */
  1390. rom_size = ALIGN_UP (core_size + boot_size, 64 * 1024);
  1391. rom_img = xmalloc (rom_size);
  1392. memset (rom_img, 0, rom_size);
  1393. *((grub_int32_t *) (core_img + GRUB_KERNEL_I386_QEMU_CORE_ENTRY_ADDR))
  1394. = grub_host_to_target32 ((grub_uint32_t) -rom_size);
  1395. memcpy (rom_img, core_img, core_size);
  1396. *((grub_int32_t *) (boot_img + GRUB_BOOT_I386_QEMU_CORE_ENTRY_ADDR))
  1397. = grub_host_to_target32 ((grub_uint32_t) -rom_size);
  1398. memcpy (rom_img + rom_size - boot_size, boot_img, boot_size);
  1399. free (core_img);
  1400. core_img = rom_img;
  1401. core_size = rom_size;
  1402. free (boot_img);
  1403. free (boot_path);
  1404. }
  1405. break;
  1406. case IMAGE_SPARC64_AOUT:
  1407. {
  1408. void *aout_img;
  1409. size_t aout_size;
  1410. struct grub_aout32_header *aout_head;
  1411. aout_size = core_size + sizeof (*aout_head);
  1412. aout_img = xmalloc (aout_size);
  1413. aout_head = aout_img;
  1414. grub_memset (aout_head, 0, sizeof (*aout_head));
  1415. aout_head->a_midmag = grub_host_to_target32 ((AOUT_MID_SUN << 16)
  1416. | AOUT32_OMAGIC);
  1417. aout_head->a_text = grub_host_to_target32 (core_size);
  1418. aout_head->a_entry
  1419. = grub_host_to_target32 (GRUB_BOOT_SPARC64_IEEE1275_IMAGE_ADDRESS);
  1420. memcpy ((char *) aout_img + sizeof (*aout_head), core_img, core_size);
  1421. free (core_img);
  1422. core_img = aout_img;
  1423. core_size = aout_size;
  1424. }
  1425. break;
  1426. case IMAGE_SPARC64_RAW:
  1427. {
  1428. unsigned int num;
  1429. char *boot_path, *boot_img;
  1430. size_t boot_size;
  1431. num = ((core_size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS);
  1432. num <<= GRUB_DISK_SECTOR_BITS;
  1433. boot_path = grub_util_get_path (dir, "diskboot.img");
  1434. boot_size = grub_util_get_image_size (boot_path);
  1435. if (boot_size != GRUB_DISK_SECTOR_SIZE)
  1436. grub_util_error (_("diskboot.img size must be %u bytes"),
  1437. GRUB_DISK_SECTOR_SIZE);
  1438. boot_img = grub_util_read_image (boot_path);
  1439. *((grub_uint32_t *) (boot_img + GRUB_DISK_SECTOR_SIZE
  1440. - GRUB_BOOT_SPARC64_IEEE1275_LIST_SIZE + 8))
  1441. = grub_host_to_target32 (num);
  1442. grub_util_write_image (boot_img, boot_size, out, outname);
  1443. free (boot_img);
  1444. free (boot_path);
  1445. }
  1446. break;
  1447. case IMAGE_SPARC64_CDCORE:
  1448. break;
  1449. case IMAGE_YEELOONG_FLASH:
  1450. case IMAGE_FULOONG2F_FLASH:
  1451. {
  1452. char *rom_img;
  1453. size_t rom_size;
  1454. char *boot_path, *boot_img;
  1455. size_t boot_size;
  1456. /* fwstart.img is the only part which can't be tested by using *-elf
  1457. target. Check it against the checksum. */
  1458. const grub_uint8_t yeeloong_fwstart_good_hash[512 / 8] =
  1459. {
  1460. 0x5f, 0x67, 0x46, 0x57, 0x31, 0x30, 0xc5, 0x0a,
  1461. 0xe9, 0x98, 0x18, 0xc9, 0xf3, 0xca, 0x45, 0xa5,
  1462. 0x75, 0x64, 0x6b, 0xbb, 0x24, 0xcd, 0xb4, 0xbc,
  1463. 0xf2, 0x3e, 0x23, 0xf9, 0xc2, 0x6a, 0x8c, 0xde,
  1464. 0x3b, 0x94, 0x9c, 0xcc, 0xa5, 0xa7, 0x58, 0xb1,
  1465. 0xbe, 0x8b, 0x3d, 0x73, 0x98, 0x18, 0x7e, 0x68,
  1466. 0x5e, 0x5f, 0x23, 0x7d, 0x7a, 0xe8, 0x51, 0xf7,
  1467. 0x1a, 0xaf, 0x2f, 0x54, 0x11, 0x2e, 0x5c, 0x25
  1468. };
  1469. const grub_uint8_t fuloong2f_fwstart_good_hash[512 / 8] =
  1470. {
  1471. 0x76, 0x9b, 0xad, 0x6e, 0xa2, 0x39, 0x47, 0x62,
  1472. 0x1f, 0xc9, 0x3a, 0x6d, 0x05, 0x5c, 0x43, 0x5c,
  1473. 0x29, 0x4a, 0x7e, 0x08, 0x2a, 0x31, 0x8f, 0x5d,
  1474. 0x02, 0x84, 0xa0, 0x85, 0xf2, 0xd1, 0xb9, 0x53,
  1475. 0xa2, 0xbc, 0xf2, 0xe1, 0x39, 0x1e, 0x51, 0xb5,
  1476. 0xaf, 0xec, 0x9e, 0xf2, 0xf1, 0xf3, 0x0a, 0x2f,
  1477. 0xe6, 0xf1, 0x08, 0x89, 0xbe, 0xbc, 0x73, 0xab,
  1478. 0x46, 0x50, 0xd6, 0x21, 0xce, 0x8e, 0x24, 0xa7
  1479. };
  1480. const grub_uint8_t *fwstart_good_hash;
  1481. grub_uint8_t fwstart_hash[512 / 8];
  1482. if (image_target->id == IMAGE_FULOONG2F_FLASH)
  1483. {
  1484. fwstart_good_hash = fuloong2f_fwstart_good_hash;
  1485. boot_path = grub_util_get_path (dir, "fwstart_fuloong2f.img");
  1486. }
  1487. else
  1488. {
  1489. fwstart_good_hash = yeeloong_fwstart_good_hash;
  1490. boot_path = grub_util_get_path (dir, "fwstart.img");
  1491. }
  1492. boot_size = grub_util_get_image_size (boot_path);
  1493. boot_img = grub_util_read_image (boot_path);
  1494. grub_crypto_hash (GRUB_MD_SHA512, fwstart_hash, boot_img, boot_size);
  1495. if (grub_memcmp (fwstart_hash, fwstart_good_hash,
  1496. GRUB_MD_SHA512->mdlen) != 0)
  1497. /* TRANSLATORS: fwstart.img may still be good, just it wasn't checked. */
  1498. grub_util_warn ("%s",
  1499. _("fwstart.img doesn't match the known good version. "
  1500. "proceed at your own risk"));
  1501. if (core_size + boot_size > 512 * 1024)
  1502. grub_util_error ("%s", _("firmware image is too big"));
  1503. rom_size = 512 * 1024;
  1504. rom_img = xmalloc (rom_size);
  1505. memset (rom_img, 0, rom_size);
  1506. memcpy (rom_img, boot_img, boot_size);
  1507. memcpy (rom_img + boot_size, core_img, core_size);
  1508. memset (rom_img + boot_size + core_size, 0,
  1509. rom_size - (boot_size + core_size));
  1510. free (core_img);
  1511. core_img = rom_img;
  1512. core_size = rom_size;
  1513. free (boot_img);
  1514. free (boot_path);
  1515. }
  1516. break;
  1517. case IMAGE_QEMU_MIPS_FLASH:
  1518. {
  1519. char *rom_img;
  1520. size_t rom_size;
  1521. if (core_size > 512 * 1024)
  1522. grub_util_error ("%s", _("firmware image is too big"));
  1523. rom_size = 512 * 1024;
  1524. rom_img = xmalloc (rom_size);
  1525. memset (rom_img, 0, rom_size);
  1526. memcpy (rom_img, core_img, core_size);
  1527. memset (rom_img + core_size, 0,
  1528. rom_size - core_size);
  1529. free (core_img);
  1530. core_img = rom_img;
  1531. core_size = rom_size;
  1532. }
  1533. break;
  1534. case IMAGE_UBOOT:
  1535. {
  1536. struct grub_uboot_image_header *hdr;
  1537. hdr = xmalloc (core_size + sizeof (struct grub_uboot_image_header));
  1538. memcpy (hdr + 1, core_img, core_size);
  1539. memset (hdr, 0, sizeof (*hdr));
  1540. hdr->ih_magic = grub_cpu_to_be32_compile_time (GRUB_UBOOT_IH_MAGIC);
  1541. hdr->ih_time = grub_cpu_to_be32 (STABLE_EMBEDDING_TIMESTAMP);
  1542. hdr->ih_size = grub_cpu_to_be32 (core_size);
  1543. hdr->ih_load = 0;
  1544. hdr->ih_ep = 0;
  1545. hdr->ih_type = GRUB_UBOOT_IH_TYPE_KERNEL_NOLOAD;
  1546. hdr->ih_os = GRUB_UBOOT_IH_OS_LINUX;
  1547. hdr->ih_arch = GRUB_UBOOT_IH_ARCH_ARM;
  1548. hdr->ih_comp = GRUB_UBOOT_IH_COMP_NONE;
  1549. grub_crypto_hash (GRUB_MD_CRC32, &hdr->ih_dcrc, hdr + 1, core_size);
  1550. grub_crypto_hash (GRUB_MD_CRC32, &hdr->ih_hcrc, hdr, sizeof (*hdr));
  1551. free (core_img);
  1552. core_img = (char *) hdr;
  1553. core_size += sizeof (struct grub_uboot_image_header);
  1554. }
  1555. break;
  1556. case IMAGE_MIPS_ARC:
  1557. {
  1558. char *ecoff_img;
  1559. struct ecoff_header {
  1560. grub_uint16_t magic;
  1561. grub_uint16_t nsec;
  1562. grub_uint32_t time;
  1563. grub_uint32_t syms;
  1564. grub_uint32_t nsyms;
  1565. grub_uint16_t opt;
  1566. grub_uint16_t flags;
  1567. grub_uint16_t magic2;
  1568. grub_uint16_t version;
  1569. grub_uint32_t textsize;
  1570. grub_uint32_t datasize;
  1571. grub_uint32_t bsssize;
  1572. grub_uint32_t entry;
  1573. grub_uint32_t text_start;
  1574. grub_uint32_t data_start;
  1575. grub_uint32_t bss_start;
  1576. grub_uint32_t gprmask;
  1577. grub_uint32_t cprmask[4];
  1578. grub_uint32_t gp_value;
  1579. };
  1580. struct ecoff_section
  1581. {
  1582. char name[8];
  1583. grub_uint32_t paddr;
  1584. grub_uint32_t vaddr;
  1585. grub_uint32_t size;
  1586. grub_uint32_t file_offset;
  1587. grub_uint32_t reloc;
  1588. grub_uint32_t gp;
  1589. grub_uint16_t nreloc;
  1590. grub_uint16_t ngp;
  1591. grub_uint32_t flags;
  1592. };
  1593. struct ecoff_header *head;
  1594. struct ecoff_section *section;
  1595. grub_uint32_t target_addr;
  1596. size_t program_size;
  1597. program_size = ALIGN_ADDR (core_size);
  1598. if (comp == GRUB_COMPRESSION_NONE)
  1599. target_addr = (image_target->link_addr - decompress_size);
  1600. else
  1601. target_addr = ALIGN_UP (image_target->link_addr
  1602. + layout.kernel_size + total_module_size, 32);
  1603. ecoff_img = xmalloc (program_size + sizeof (*head) + sizeof (*section));
  1604. grub_memset (ecoff_img, 0, program_size + sizeof (*head) + sizeof (*section));
  1605. head = (void *) ecoff_img;
  1606. section = (void *) (head + 1);
  1607. head->magic = image_target->bigendian ? grub_host_to_target16 (0x160)
  1608. : grub_host_to_target16 (0x166);
  1609. head->nsec = grub_host_to_target16 (1);
  1610. head->time = grub_host_to_target32 (0);
  1611. head->opt = grub_host_to_target16 (0x38);
  1612. head->flags = image_target->bigendian
  1613. ? grub_host_to_target16 (0x207)
  1614. : grub_host_to_target16 (0x103);
  1615. head->magic2 = grub_host_to_target16 (0x107);
  1616. head->textsize = grub_host_to_target32 (program_size);
  1617. head->entry = grub_host_to_target32 (target_addr);
  1618. head->text_start = grub_host_to_target32 (target_addr);
  1619. head->data_start = grub_host_to_target32 (target_addr + program_size);
  1620. grub_memcpy (section->name, ".text", sizeof (".text") - 1);
  1621. section->vaddr = grub_host_to_target32 (target_addr);
  1622. section->size = grub_host_to_target32 (program_size);
  1623. section->file_offset = grub_host_to_target32 (sizeof (*head) + sizeof (*section));
  1624. if (!image_target->bigendian)
  1625. {
  1626. section->paddr = grub_host_to_target32 (0xaa60);
  1627. section->flags = grub_host_to_target32 (0x20);
  1628. }
  1629. memcpy (section + 1, core_img, core_size);
  1630. free (core_img);
  1631. core_img = ecoff_img;
  1632. core_size = program_size + sizeof (*head) + sizeof (*section);
  1633. }
  1634. break;
  1635. case IMAGE_LOONGSON_ELF:
  1636. case IMAGE_PPC:
  1637. case IMAGE_XEN:
  1638. case IMAGE_XEN_PVH:
  1639. case IMAGE_COREBOOT:
  1640. case IMAGE_I386_IEEE1275:
  1641. {
  1642. grub_uint64_t target_addr;
  1643. char *sbat = NULL;
  1644. if (sbat_path != NULL)
  1645. {
  1646. sbat_size = grub_util_get_image_size (sbat_path);
  1647. sbat = xmalloc (sbat_size);
  1648. grub_util_load_image (sbat_path, sbat);
  1649. layout.sbat_size = sbat_size;
  1650. }
  1651. if (image_target->id == IMAGE_LOONGSON_ELF)
  1652. {
  1653. if (comp == GRUB_COMPRESSION_NONE)
  1654. target_addr = (image_target->link_addr - decompress_size);
  1655. else
  1656. target_addr = ALIGN_UP (image_target->link_addr
  1657. + layout.kernel_size + total_module_size, 32);
  1658. }
  1659. else
  1660. target_addr = image_target->link_addr;
  1661. if (image_target->voidp_sizeof == 4)
  1662. grub_mkimage_generate_elf32 (image_target, note, sbat, &core_img, &core_size,
  1663. target_addr, &layout);
  1664. else
  1665. grub_mkimage_generate_elf64 (image_target, note, sbat, &core_img, &core_size,
  1666. target_addr, &layout);
  1667. }
  1668. break;
  1669. }
  1670. grub_util_write_image (core_img, core_size, out, outname);
  1671. free (core_img);
  1672. free (kernel_path);
  1673. free (layout.reloc_section);
  1674. grub_util_free_path_list (path_list);
  1675. }