mkimage.c 56 KB

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