You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bzimage.c 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /**
  25. * @file
  26. *
  27. * Linux bzImage image format
  28. *
  29. */
  30. #include <stdint.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <errno.h>
  34. #include <assert.h>
  35. #include <realmode.h>
  36. #include <bzimage.h>
  37. #include <initrd.h>
  38. #include <ipxe/uaccess.h>
  39. #include <ipxe/image.h>
  40. #include <ipxe/segment.h>
  41. #include <ipxe/init.h>
  42. #include <ipxe/cpio.h>
  43. #include <ipxe/features.h>
  44. FEATURE ( FEATURE_IMAGE, "bzImage", DHCP_EB_FEATURE_BZIMAGE, 1 );
  45. /**
  46. * bzImage context
  47. */
  48. struct bzimage_context {
  49. /** Boot protocol version */
  50. unsigned int version;
  51. /** Real-mode kernel portion load segment address */
  52. unsigned int rm_kernel_seg;
  53. /** Real-mode kernel portion load address */
  54. userptr_t rm_kernel;
  55. /** Real-mode kernel portion file size */
  56. size_t rm_filesz;
  57. /** Real-mode heap top (offset from rm_kernel) */
  58. size_t rm_heap;
  59. /** Command line (offset from rm_kernel) */
  60. size_t rm_cmdline;
  61. /** Command line maximum length */
  62. size_t cmdline_size;
  63. /** Real-mode kernel portion total memory size */
  64. size_t rm_memsz;
  65. /** Non-real-mode kernel portion load address */
  66. userptr_t pm_kernel;
  67. /** Non-real-mode kernel portion file and memory size */
  68. size_t pm_sz;
  69. /** Video mode */
  70. unsigned int vid_mode;
  71. /** Memory limit */
  72. uint64_t mem_limit;
  73. /** Initrd address */
  74. physaddr_t ramdisk_image;
  75. /** Initrd size */
  76. physaddr_t ramdisk_size;
  77. /** Command line magic block */
  78. struct bzimage_cmdline cmdline_magic;
  79. /** bzImage header */
  80. struct bzimage_header bzhdr;
  81. };
  82. /**
  83. * Parse bzImage header
  84. *
  85. * @v image bzImage file
  86. * @v bzimg bzImage context
  87. * @v src bzImage to parse
  88. * @ret rc Return status code
  89. */
  90. static int bzimage_parse_header ( struct image *image,
  91. struct bzimage_context *bzimg,
  92. userptr_t src ) {
  93. unsigned int syssize;
  94. int is_bzimage;
  95. /* Sanity check */
  96. if ( image->len < ( BZI_HDR_OFFSET + sizeof ( bzimg->bzhdr ) ) ) {
  97. DBGC ( image, "bzImage %p too short for kernel header\n",
  98. image );
  99. return -ENOEXEC;
  100. }
  101. /* Read in header structures */
  102. memset ( bzimg, 0, sizeof ( *bzimg ) );
  103. copy_from_user ( &bzimg->cmdline_magic, src, BZI_CMDLINE_OFFSET,
  104. sizeof ( bzimg->cmdline_magic ) );
  105. copy_from_user ( &bzimg->bzhdr, src, BZI_HDR_OFFSET,
  106. sizeof ( bzimg->bzhdr ) );
  107. /* Calculate size of real-mode portion */
  108. bzimg->rm_filesz = ( ( ( bzimg->bzhdr.setup_sects ?
  109. bzimg->bzhdr.setup_sects : 4 ) + 1 ) << 9 );
  110. if ( bzimg->rm_filesz > image->len ) {
  111. DBGC ( image, "bzImage %p too short for %zd byte of setup\n",
  112. image, bzimg->rm_filesz );
  113. return -ENOEXEC;
  114. }
  115. bzimg->rm_memsz = BZI_ASSUMED_RM_SIZE;
  116. /* Calculate size of protected-mode portion */
  117. bzimg->pm_sz = ( image->len - bzimg->rm_filesz );
  118. syssize = ( ( bzimg->pm_sz + 15 ) / 16 );
  119. /* Check for signatures and determine version */
  120. if ( bzimg->bzhdr.boot_flag != BZI_BOOT_FLAG ) {
  121. DBGC ( image, "bzImage %p missing 55AA signature\n", image );
  122. return -ENOEXEC;
  123. }
  124. if ( bzimg->bzhdr.header == BZI_SIGNATURE ) {
  125. /* 2.00+ */
  126. bzimg->version = bzimg->bzhdr.version;
  127. } else {
  128. /* Pre-2.00. Check that the syssize field is correct,
  129. * as a guard against accepting arbitrary binary data,
  130. * since the 55AA check is pretty lax. Note that the
  131. * syssize field is unreliable for protocols between
  132. * 2.00 and 2.03 inclusive, so we should not always
  133. * check this field.
  134. */
  135. bzimg->version = 0x0100;
  136. if ( bzimg->bzhdr.syssize != syssize ) {
  137. DBGC ( image, "bzImage %p bad syssize %x (expected "
  138. "%x)\n", image, bzimg->bzhdr.syssize, syssize );
  139. return -ENOEXEC;
  140. }
  141. }
  142. /* Determine image type */
  143. is_bzimage = ( ( bzimg->version >= 0x0200 ) ?
  144. ( bzimg->bzhdr.loadflags & BZI_LOAD_HIGH ) : 0 );
  145. /* Calculate load address of real-mode portion */
  146. bzimg->rm_kernel_seg = ( is_bzimage ? 0x1000 : 0x9000 );
  147. bzimg->rm_kernel = real_to_user ( bzimg->rm_kernel_seg, 0 );
  148. /* Allow space for the stack and heap */
  149. bzimg->rm_memsz += BZI_STACK_SIZE;
  150. bzimg->rm_heap = bzimg->rm_memsz;
  151. /* Allow space for the command line */
  152. bzimg->rm_cmdline = bzimg->rm_memsz;
  153. bzimg->rm_memsz += BZI_CMDLINE_SIZE;
  154. /* Calculate load address of protected-mode portion */
  155. bzimg->pm_kernel = phys_to_user ( is_bzimage ? BZI_LOAD_HIGH_ADDR
  156. : BZI_LOAD_LOW_ADDR );
  157. /* Extract video mode */
  158. bzimg->vid_mode = bzimg->bzhdr.vid_mode;
  159. /* Extract memory limit */
  160. bzimg->mem_limit = ( ( bzimg->version >= 0x0203 ) ?
  161. bzimg->bzhdr.initrd_addr_max : BZI_INITRD_MAX );
  162. /* Extract command line size */
  163. bzimg->cmdline_size = ( ( bzimg->version >= 0x0206 ) ?
  164. bzimg->bzhdr.cmdline_size : BZI_CMDLINE_SIZE );
  165. DBGC ( image, "bzImage %p version %04x RM %#lx+%#zx PM %#lx+%#zx "
  166. "cmdlen %zd\n", image, bzimg->version,
  167. user_to_phys ( bzimg->rm_kernel, 0 ), bzimg->rm_filesz,
  168. user_to_phys ( bzimg->pm_kernel, 0 ), bzimg->pm_sz,
  169. bzimg->cmdline_size );
  170. return 0;
  171. }
  172. /**
  173. * Update bzImage header in loaded kernel
  174. *
  175. * @v image bzImage file
  176. * @v bzimg bzImage context
  177. * @v dst bzImage to update
  178. */
  179. static void bzimage_update_header ( struct image *image,
  180. struct bzimage_context *bzimg,
  181. userptr_t dst ) {
  182. /* Set loader type */
  183. if ( bzimg->version >= 0x0200 )
  184. bzimg->bzhdr.type_of_loader = BZI_LOADER_TYPE_IPXE;
  185. /* Set heap end pointer */
  186. if ( bzimg->version >= 0x0201 ) {
  187. bzimg->bzhdr.heap_end_ptr = ( bzimg->rm_heap - 0x200 );
  188. bzimg->bzhdr.loadflags |= BZI_CAN_USE_HEAP;
  189. }
  190. /* Set command line */
  191. if ( bzimg->version >= 0x0202 ) {
  192. bzimg->bzhdr.cmd_line_ptr = user_to_phys ( bzimg->rm_kernel,
  193. bzimg->rm_cmdline );
  194. } else {
  195. bzimg->cmdline_magic.magic = BZI_CMDLINE_MAGIC;
  196. bzimg->cmdline_magic.offset = bzimg->rm_cmdline;
  197. if ( bzimg->version >= 0x0200 )
  198. bzimg->bzhdr.setup_move_size = bzimg->rm_memsz;
  199. }
  200. /* Set video mode */
  201. bzimg->bzhdr.vid_mode = bzimg->vid_mode;
  202. /* Set initrd address */
  203. if ( bzimg->version >= 0x0200 ) {
  204. bzimg->bzhdr.ramdisk_image = bzimg->ramdisk_image;
  205. bzimg->bzhdr.ramdisk_size = bzimg->ramdisk_size;
  206. }
  207. /* Write out header structures */
  208. copy_to_user ( dst, BZI_CMDLINE_OFFSET, &bzimg->cmdline_magic,
  209. sizeof ( bzimg->cmdline_magic ) );
  210. copy_to_user ( dst, BZI_HDR_OFFSET, &bzimg->bzhdr,
  211. sizeof ( bzimg->bzhdr ) );
  212. DBGC ( image, "bzImage %p vidmode %d\n", image, bzimg->vid_mode );
  213. }
  214. /**
  215. * Parse kernel command line for bootloader parameters
  216. *
  217. * @v image bzImage file
  218. * @v bzimg bzImage context
  219. * @v cmdline Kernel command line
  220. * @ret rc Return status code
  221. */
  222. static int bzimage_parse_cmdline ( struct image *image,
  223. struct bzimage_context *bzimg,
  224. const char *cmdline ) {
  225. char *vga;
  226. char *mem;
  227. /* Look for "vga=" */
  228. if ( ( vga = strstr ( cmdline, "vga=" ) ) ) {
  229. vga += 4;
  230. if ( strcmp ( vga, "normal" ) == 0 ) {
  231. bzimg->vid_mode = BZI_VID_MODE_NORMAL;
  232. } else if ( strcmp ( vga, "ext" ) == 0 ) {
  233. bzimg->vid_mode = BZI_VID_MODE_EXT;
  234. } else if ( strcmp ( vga, "ask" ) == 0 ) {
  235. bzimg->vid_mode = BZI_VID_MODE_ASK;
  236. } else {
  237. bzimg->vid_mode = strtoul ( vga, &vga, 0 );
  238. if ( *vga && ( *vga != ' ' ) ) {
  239. DBGC ( image, "bzImage %p strange \"vga=\""
  240. "terminator '%c'\n", image, *vga );
  241. }
  242. }
  243. }
  244. /* Look for "mem=" */
  245. if ( ( mem = strstr ( cmdline, "mem=" ) ) ) {
  246. mem += 4;
  247. bzimg->mem_limit = strtoul ( mem, &mem, 0 );
  248. switch ( *mem ) {
  249. case 'G':
  250. case 'g':
  251. bzimg->mem_limit <<= 10;
  252. case 'M':
  253. case 'm':
  254. bzimg->mem_limit <<= 10;
  255. case 'K':
  256. case 'k':
  257. bzimg->mem_limit <<= 10;
  258. break;
  259. case '\0':
  260. case ' ':
  261. break;
  262. default:
  263. DBGC ( image, "bzImage %p strange \"mem=\" "
  264. "terminator '%c'\n", image, *mem );
  265. break;
  266. }
  267. bzimg->mem_limit -= 1;
  268. }
  269. return 0;
  270. }
  271. /**
  272. * Set command line
  273. *
  274. * @v image bzImage image
  275. * @v bzimg bzImage context
  276. * @v cmdline Kernel command line
  277. */
  278. static void bzimage_set_cmdline ( struct image *image,
  279. struct bzimage_context *bzimg,
  280. const char *cmdline ) {
  281. size_t cmdline_len;
  282. /* Copy command line down to real-mode portion */
  283. cmdline_len = ( strlen ( cmdline ) + 1 );
  284. if ( cmdline_len > bzimg->cmdline_size )
  285. cmdline_len = bzimg->cmdline_size;
  286. copy_to_user ( bzimg->rm_kernel, bzimg->rm_cmdline,
  287. cmdline, cmdline_len );
  288. DBGC ( image, "bzImage %p command line \"%s\"\n", image, cmdline );
  289. }
  290. /**
  291. * Parse standalone image command line for cpio parameters
  292. *
  293. * @v image bzImage file
  294. * @v cpio CPIO header
  295. * @v cmdline Command line
  296. */
  297. static void bzimage_parse_cpio_cmdline ( struct image *image,
  298. struct cpio_header *cpio,
  299. const char *cmdline ) {
  300. char *arg;
  301. char *end;
  302. unsigned int mode;
  303. /* Look for "mode=" */
  304. if ( ( arg = strstr ( cmdline, "mode=" ) ) ) {
  305. arg += 5;
  306. mode = strtoul ( arg, &end, 8 /* Octal for file mode */ );
  307. if ( *end && ( *end != ' ' ) ) {
  308. DBGC ( image, "bzImage %p strange \"mode=\""
  309. "terminator '%c'\n", image, *end );
  310. }
  311. cpio_set_field ( cpio->c_mode, ( 0100000 | mode ) );
  312. }
  313. }
  314. /**
  315. * Align initrd length
  316. *
  317. * @v len Length
  318. * @ret len Length rounded up to INITRD_ALIGN
  319. */
  320. static inline size_t bzimage_align ( size_t len ) {
  321. return ( ( len + INITRD_ALIGN - 1 ) & ~( INITRD_ALIGN - 1 ) );
  322. }
  323. /**
  324. * Load initrd
  325. *
  326. * @v image bzImage image
  327. * @v initrd initrd image
  328. * @v address Address at which to load, or UNULL
  329. * @ret len Length of loaded image, excluding zero-padding
  330. */
  331. static size_t bzimage_load_initrd ( struct image *image,
  332. struct image *initrd,
  333. userptr_t address ) {
  334. char *filename = initrd->cmdline;
  335. char *cmdline;
  336. struct cpio_header cpio;
  337. size_t offset;
  338. size_t name_len;
  339. size_t pad_len;
  340. /* Do not include kernel image itself as an initrd */
  341. if ( initrd == image )
  342. return 0;
  343. /* Create cpio header for non-prebuilt images */
  344. if ( filename && filename[0] ) {
  345. cmdline = strchr ( filename, ' ' );
  346. name_len = ( ( cmdline ? ( ( size_t ) ( cmdline - filename ) )
  347. : strlen ( filename ) ) + 1 /* NUL */ );
  348. memset ( &cpio, '0', sizeof ( cpio ) );
  349. memcpy ( cpio.c_magic, CPIO_MAGIC, sizeof ( cpio.c_magic ) );
  350. cpio_set_field ( cpio.c_mode, 0100644 );
  351. cpio_set_field ( cpio.c_nlink, 1 );
  352. cpio_set_field ( cpio.c_filesize, initrd->len );
  353. cpio_set_field ( cpio.c_namesize, name_len );
  354. if ( cmdline ) {
  355. bzimage_parse_cpio_cmdline ( image, &cpio,
  356. ( cmdline + 1 /* ' ' */ ));
  357. }
  358. offset = ( ( sizeof ( cpio ) + name_len + 0x03 ) & ~0x03 );
  359. } else {
  360. offset = 0;
  361. name_len = 0;
  362. }
  363. /* Copy in initrd image body (and cpio header if applicable) */
  364. if ( address ) {
  365. memmove_user ( address, offset, initrd->data, 0, initrd->len );
  366. if ( offset ) {
  367. memset_user ( address, 0, 0, offset );
  368. copy_to_user ( address, 0, &cpio, sizeof ( cpio ) );
  369. copy_to_user ( address, sizeof ( cpio ), filename,
  370. ( name_len - 1 /* NUL (or space) */ ) );
  371. }
  372. DBGC ( image, "bzImage %p initrd %p [%#08lx,%#08lx,%#08lx)"
  373. "%s%s\n", image, initrd, user_to_phys ( address, 0 ),
  374. user_to_phys ( address, offset ),
  375. user_to_phys ( address, ( offset + initrd->len ) ),
  376. ( filename ? " " : "" ), ( filename ? filename : "" ) );
  377. DBGC2_MD5A ( image, user_to_phys ( address, offset ),
  378. user_to_virt ( address, offset ), initrd->len );
  379. }
  380. offset += initrd->len;
  381. /* Zero-pad to next INITRD_ALIGN boundary */
  382. pad_len = ( ( -offset ) & ( INITRD_ALIGN - 1 ) );
  383. if ( address )
  384. memset_user ( address, offset, 0, pad_len );
  385. return offset;
  386. }
  387. /**
  388. * Check that initrds can be loaded
  389. *
  390. * @v image bzImage image
  391. * @v bzimg bzImage context
  392. * @ret rc Return status code
  393. */
  394. static int bzimage_check_initrds ( struct image *image,
  395. struct bzimage_context *bzimg ) {
  396. struct image *initrd;
  397. userptr_t bottom;
  398. size_t len = 0;
  399. int rc;
  400. /* Calculate total loaded length of initrds */
  401. for_each_image ( initrd ) {
  402. /* Skip kernel */
  403. if ( initrd == image )
  404. continue;
  405. /* Calculate length */
  406. len += bzimage_load_initrd ( image, initrd, UNULL );
  407. len = bzimage_align ( len );
  408. DBGC ( image, "bzImage %p initrd %p from [%#08lx,%#08lx)%s%s\n",
  409. image, initrd, user_to_phys ( initrd->data, 0 ),
  410. user_to_phys ( initrd->data, initrd->len ),
  411. ( initrd->cmdline ? " " : "" ),
  412. ( initrd->cmdline ? initrd->cmdline : "" ) );
  413. DBGC2_MD5A ( image, user_to_phys ( initrd->data, 0 ),
  414. user_to_virt ( initrd->data, 0 ), initrd->len );
  415. }
  416. /* Calculate lowest usable address */
  417. bottom = userptr_add ( bzimg->pm_kernel, bzimg->pm_sz );
  418. /* Check that total length fits within space available for
  419. * reshuffling. This is a conservative check, since CPIO
  420. * headers are not present during reshuffling, but this
  421. * doesn't hurt and keeps the code simple.
  422. */
  423. if ( ( rc = initrd_reshuffle_check ( len, bottom ) ) != 0 ) {
  424. DBGC ( image, "bzImage %p failed reshuffle check: %s\n",
  425. image, strerror ( rc ) );
  426. return rc;
  427. }
  428. /* Check that total length fits within kernel's memory limit */
  429. if ( user_to_phys ( bottom, len ) > bzimg->mem_limit ) {
  430. DBGC ( image, "bzImage %p not enough space for initrds\n",
  431. image );
  432. return -ENOBUFS;
  433. }
  434. return 0;
  435. }
  436. /**
  437. * Load initrds, if any
  438. *
  439. * @v image bzImage image
  440. * @v bzimg bzImage context
  441. */
  442. static void bzimage_load_initrds ( struct image *image,
  443. struct bzimage_context *bzimg ) {
  444. struct image *initrd;
  445. struct image *highest = NULL;
  446. struct image *other;
  447. userptr_t top;
  448. userptr_t dest;
  449. size_t offset;
  450. size_t len;
  451. /* Reshuffle initrds into desired order */
  452. initrd_reshuffle ( userptr_add ( bzimg->pm_kernel, bzimg->pm_sz ) );
  453. /* Find highest initrd */
  454. for_each_image ( initrd ) {
  455. if ( ( highest == NULL ) ||
  456. ( userptr_sub ( initrd->data, highest->data ) > 0 ) ) {
  457. highest = initrd;
  458. }
  459. }
  460. /* Do nothing if there are no initrds */
  461. if ( ! highest )
  462. return;
  463. /* Find highest usable address */
  464. top = userptr_add ( highest->data, bzimage_align ( highest->len ) );
  465. if ( user_to_phys ( top, 0 ) > bzimg->mem_limit )
  466. top = phys_to_user ( bzimg->mem_limit );
  467. DBGC ( image, "bzImage %p loading initrds from %#08lx downwards\n",
  468. image, user_to_phys ( top, 0 ) );
  469. /* Load initrds in order */
  470. for_each_image ( initrd ) {
  471. /* Calculate cumulative length of following
  472. * initrds (including padding).
  473. */
  474. offset = 0;
  475. for_each_image ( other ) {
  476. if ( other == initrd )
  477. offset = 0;
  478. offset += bzimage_load_initrd ( image, other, UNULL );
  479. offset = bzimage_align ( offset );
  480. }
  481. /* Load initrd at this address */
  482. dest = userptr_add ( top, -offset );
  483. len = bzimage_load_initrd ( image, initrd, dest );
  484. /* Record initrd location */
  485. if ( ! bzimg->ramdisk_image )
  486. bzimg->ramdisk_image = user_to_phys ( dest, 0 );
  487. bzimg->ramdisk_size = ( user_to_phys ( dest, len ) -
  488. bzimg->ramdisk_image );
  489. }
  490. DBGC ( image, "bzImage %p initrds at [%#08lx,%#08lx)\n",
  491. image, bzimg->ramdisk_image,
  492. ( bzimg->ramdisk_image + bzimg->ramdisk_size ) );
  493. }
  494. /**
  495. * Execute bzImage image
  496. *
  497. * @v image bzImage image
  498. * @ret rc Return status code
  499. */
  500. static int bzimage_exec ( struct image *image ) {
  501. struct bzimage_context bzimg;
  502. const char *cmdline = ( image->cmdline ? image->cmdline : "" );
  503. int rc;
  504. /* Read and parse header from image */
  505. if ( ( rc = bzimage_parse_header ( image, &bzimg,
  506. image->data ) ) != 0 )
  507. return rc;
  508. /* Prepare segments */
  509. if ( ( rc = prep_segment ( bzimg.rm_kernel, bzimg.rm_filesz,
  510. bzimg.rm_memsz ) ) != 0 ) {
  511. DBGC ( image, "bzImage %p could not prepare RM segment: %s\n",
  512. image, strerror ( rc ) );
  513. return rc;
  514. }
  515. if ( ( rc = prep_segment ( bzimg.pm_kernel, bzimg.pm_sz,
  516. bzimg.pm_sz ) ) != 0 ) {
  517. DBGC ( image, "bzImage %p could not prepare PM segment: %s\n",
  518. image, strerror ( rc ) );
  519. return rc;
  520. }
  521. /* Parse command line for bootloader parameters */
  522. if ( ( rc = bzimage_parse_cmdline ( image, &bzimg, cmdline ) ) != 0)
  523. return rc;
  524. /* Check that initrds can be loaded */
  525. if ( ( rc = bzimage_check_initrds ( image, &bzimg ) ) != 0 )
  526. return rc;
  527. /* Remove kernel from image list (without invalidating image pointer) */
  528. unregister_image ( image_get ( image ) );
  529. /* Load segments */
  530. memcpy_user ( bzimg.rm_kernel, 0, image->data,
  531. 0, bzimg.rm_filesz );
  532. memcpy_user ( bzimg.pm_kernel, 0, image->data,
  533. bzimg.rm_filesz, bzimg.pm_sz );
  534. /* Store command line */
  535. bzimage_set_cmdline ( image, &bzimg, cmdline );
  536. /* Prepare for exiting. Must do this before loading initrds,
  537. * since loading the initrds will corrupt the external heap.
  538. */
  539. shutdown_boot();
  540. /* Load any initrds */
  541. bzimage_load_initrds ( image, &bzimg );
  542. /* Update kernel header */
  543. bzimage_update_header ( image, &bzimg, bzimg.rm_kernel );
  544. DBGC ( image, "bzImage %p jumping to RM kernel at %04x:0000 "
  545. "(stack %04x:%04zx)\n", image, ( bzimg.rm_kernel_seg + 0x20 ),
  546. bzimg.rm_kernel_seg, bzimg.rm_heap );
  547. /* Jump to the kernel */
  548. __asm__ __volatile__ ( REAL_CODE ( "movw %w0, %%ds\n\t"
  549. "movw %w0, %%es\n\t"
  550. "movw %w0, %%fs\n\t"
  551. "movw %w0, %%gs\n\t"
  552. "movw %w0, %%ss\n\t"
  553. "movw %w1, %%sp\n\t"
  554. "pushw %w2\n\t"
  555. "pushw $0\n\t"
  556. "lret\n\t" )
  557. : : "r" ( bzimg.rm_kernel_seg ),
  558. "r" ( bzimg.rm_heap ),
  559. "r" ( bzimg.rm_kernel_seg + 0x20 ) );
  560. /* There is no way for the image to return, since we provide
  561. * no return address.
  562. */
  563. assert ( 0 );
  564. return -ECANCELED; /* -EIMPOSSIBLE */
  565. }
  566. /**
  567. * Probe bzImage image
  568. *
  569. * @v image bzImage file
  570. * @ret rc Return status code
  571. */
  572. int bzimage_probe ( struct image *image ) {
  573. struct bzimage_context bzimg;
  574. int rc;
  575. /* Read and parse header from image */
  576. if ( ( rc = bzimage_parse_header ( image, &bzimg,
  577. image->data ) ) != 0 )
  578. return rc;
  579. return 0;
  580. }
  581. /** Linux bzImage image type */
  582. struct image_type bzimage_image_type __image_type ( PROBE_NORMAL ) = {
  583. .name = "bzImage",
  584. .probe = bzimage_probe,
  585. .exec = bzimage_exec,
  586. };