mkimage.c 60 KB

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