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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. /* Fall through */
  253. case 'M':
  254. case 'm':
  255. bzimg->mem_limit <<= 10;
  256. /* Fall through */
  257. case 'K':
  258. case 'k':
  259. bzimg->mem_limit <<= 10;
  260. break;
  261. case '\0':
  262. case ' ':
  263. break;
  264. default:
  265. DBGC ( image, "bzImage %p strange \"mem=\" "
  266. "terminator '%c'\n", image, *mem );
  267. break;
  268. }
  269. bzimg->mem_limit -= 1;
  270. }
  271. return 0;
  272. }
  273. /**
  274. * Set command line
  275. *
  276. * @v image bzImage image
  277. * @v bzimg bzImage context
  278. * @v cmdline Kernel command line
  279. */
  280. static void bzimage_set_cmdline ( struct image *image,
  281. struct bzimage_context *bzimg,
  282. const char *cmdline ) {
  283. size_t cmdline_len;
  284. /* Copy command line down to real-mode portion */
  285. cmdline_len = ( strlen ( cmdline ) + 1 );
  286. if ( cmdline_len > bzimg->cmdline_size )
  287. cmdline_len = bzimg->cmdline_size;
  288. copy_to_user ( bzimg->rm_kernel, bzimg->rm_cmdline,
  289. cmdline, cmdline_len );
  290. DBGC ( image, "bzImage %p command line \"%s\"\n", image, cmdline );
  291. }
  292. /**
  293. * Parse standalone image command line for cpio parameters
  294. *
  295. * @v image bzImage file
  296. * @v cpio CPIO header
  297. * @v cmdline Command line
  298. */
  299. static void bzimage_parse_cpio_cmdline ( struct image *image,
  300. struct cpio_header *cpio,
  301. const char *cmdline ) {
  302. char *arg;
  303. char *end;
  304. unsigned int mode;
  305. /* Look for "mode=" */
  306. if ( ( arg = strstr ( cmdline, "mode=" ) ) ) {
  307. arg += 5;
  308. mode = strtoul ( arg, &end, 8 /* Octal for file mode */ );
  309. if ( *end && ( *end != ' ' ) ) {
  310. DBGC ( image, "bzImage %p strange \"mode=\""
  311. "terminator '%c'\n", image, *end );
  312. }
  313. cpio_set_field ( cpio->c_mode, ( 0100000 | mode ) );
  314. }
  315. }
  316. /**
  317. * Align initrd length
  318. *
  319. * @v len Length
  320. * @ret len Length rounded up to INITRD_ALIGN
  321. */
  322. static inline size_t bzimage_align ( size_t len ) {
  323. return ( ( len + INITRD_ALIGN - 1 ) & ~( INITRD_ALIGN - 1 ) );
  324. }
  325. /**
  326. * Load initrd
  327. *
  328. * @v image bzImage image
  329. * @v initrd initrd image
  330. * @v address Address at which to load, or UNULL
  331. * @ret len Length of loaded image, excluding zero-padding
  332. */
  333. static size_t bzimage_load_initrd ( struct image *image,
  334. struct image *initrd,
  335. userptr_t address ) {
  336. char *filename = initrd->cmdline;
  337. char *cmdline;
  338. struct cpio_header cpio;
  339. size_t offset;
  340. size_t name_len;
  341. size_t pad_len;
  342. /* Do not include kernel image itself as an initrd */
  343. if ( initrd == image )
  344. return 0;
  345. /* Create cpio header for non-prebuilt images */
  346. if ( filename && filename[0] ) {
  347. cmdline = strchr ( filename, ' ' );
  348. name_len = ( ( cmdline ? ( ( size_t ) ( cmdline - filename ) )
  349. : strlen ( filename ) ) + 1 /* NUL */ );
  350. memset ( &cpio, '0', sizeof ( cpio ) );
  351. memcpy ( cpio.c_magic, CPIO_MAGIC, sizeof ( cpio.c_magic ) );
  352. cpio_set_field ( cpio.c_mode, 0100644 );
  353. cpio_set_field ( cpio.c_nlink, 1 );
  354. cpio_set_field ( cpio.c_filesize, initrd->len );
  355. cpio_set_field ( cpio.c_namesize, name_len );
  356. if ( cmdline ) {
  357. bzimage_parse_cpio_cmdline ( image, &cpio,
  358. ( cmdline + 1 /* ' ' */ ));
  359. }
  360. offset = ( ( sizeof ( cpio ) + name_len + 0x03 ) & ~0x03 );
  361. } else {
  362. offset = 0;
  363. name_len = 0;
  364. }
  365. /* Copy in initrd image body (and cpio header if applicable) */
  366. if ( address ) {
  367. memmove_user ( address, offset, initrd->data, 0, initrd->len );
  368. if ( offset ) {
  369. memset_user ( address, 0, 0, offset );
  370. copy_to_user ( address, 0, &cpio, sizeof ( cpio ) );
  371. copy_to_user ( address, sizeof ( cpio ), filename,
  372. ( name_len - 1 /* NUL (or space) */ ) );
  373. }
  374. DBGC ( image, "bzImage %p initrd %p [%#08lx,%#08lx,%#08lx)"
  375. "%s%s\n", image, initrd, user_to_phys ( address, 0 ),
  376. user_to_phys ( address, offset ),
  377. user_to_phys ( address, ( offset + initrd->len ) ),
  378. ( filename ? " " : "" ), ( filename ? filename : "" ) );
  379. DBGC2_MD5A ( image, user_to_phys ( address, offset ),
  380. user_to_virt ( address, offset ), initrd->len );
  381. }
  382. offset += initrd->len;
  383. /* Zero-pad to next INITRD_ALIGN boundary */
  384. pad_len = ( ( -offset ) & ( INITRD_ALIGN - 1 ) );
  385. if ( address )
  386. memset_user ( address, offset, 0, pad_len );
  387. return offset;
  388. }
  389. /**
  390. * Check that initrds can be loaded
  391. *
  392. * @v image bzImage image
  393. * @v bzimg bzImage context
  394. * @ret rc Return status code
  395. */
  396. static int bzimage_check_initrds ( struct image *image,
  397. struct bzimage_context *bzimg ) {
  398. struct image *initrd;
  399. userptr_t bottom;
  400. size_t len = 0;
  401. int rc;
  402. /* Calculate total loaded length of initrds */
  403. for_each_image ( initrd ) {
  404. /* Skip kernel */
  405. if ( initrd == image )
  406. continue;
  407. /* Calculate length */
  408. len += bzimage_load_initrd ( image, initrd, UNULL );
  409. len = bzimage_align ( len );
  410. DBGC ( image, "bzImage %p initrd %p from [%#08lx,%#08lx)%s%s\n",
  411. image, initrd, user_to_phys ( initrd->data, 0 ),
  412. user_to_phys ( initrd->data, initrd->len ),
  413. ( initrd->cmdline ? " " : "" ),
  414. ( initrd->cmdline ? initrd->cmdline : "" ) );
  415. DBGC2_MD5A ( image, user_to_phys ( initrd->data, 0 ),
  416. user_to_virt ( initrd->data, 0 ), initrd->len );
  417. }
  418. /* Calculate lowest usable address */
  419. bottom = userptr_add ( bzimg->pm_kernel, bzimg->pm_sz );
  420. /* Check that total length fits within space available for
  421. * reshuffling. This is a conservative check, since CPIO
  422. * headers are not present during reshuffling, but this
  423. * doesn't hurt and keeps the code simple.
  424. */
  425. if ( ( rc = initrd_reshuffle_check ( len, bottom ) ) != 0 ) {
  426. DBGC ( image, "bzImage %p failed reshuffle check: %s\n",
  427. image, strerror ( rc ) );
  428. return rc;
  429. }
  430. /* Check that total length fits within kernel's memory limit */
  431. if ( user_to_phys ( bottom, len ) > bzimg->mem_limit ) {
  432. DBGC ( image, "bzImage %p not enough space for initrds\n",
  433. image );
  434. return -ENOBUFS;
  435. }
  436. return 0;
  437. }
  438. /**
  439. * Load initrds, if any
  440. *
  441. * @v image bzImage image
  442. * @v bzimg bzImage context
  443. */
  444. static void bzimage_load_initrds ( struct image *image,
  445. struct bzimage_context *bzimg ) {
  446. struct image *initrd;
  447. struct image *highest = NULL;
  448. struct image *other;
  449. userptr_t top;
  450. userptr_t dest;
  451. size_t offset;
  452. size_t len;
  453. /* Reshuffle initrds into desired order */
  454. initrd_reshuffle ( userptr_add ( bzimg->pm_kernel, bzimg->pm_sz ) );
  455. /* Find highest initrd */
  456. for_each_image ( initrd ) {
  457. if ( ( highest == NULL ) ||
  458. ( userptr_sub ( initrd->data, highest->data ) > 0 ) ) {
  459. highest = initrd;
  460. }
  461. }
  462. /* Do nothing if there are no initrds */
  463. if ( ! highest )
  464. return;
  465. /* Find highest usable address */
  466. top = userptr_add ( highest->data, bzimage_align ( highest->len ) );
  467. if ( user_to_phys ( top, -1 ) > bzimg->mem_limit ) {
  468. top = phys_to_user ( ( bzimg->mem_limit + 1 ) &
  469. ~( INITRD_ALIGN - 1 ) );
  470. }
  471. DBGC ( image, "bzImage %p loading initrds from %#08lx downwards\n",
  472. image, user_to_phys ( top, -1 ) );
  473. /* Load initrds in order */
  474. for_each_image ( initrd ) {
  475. /* Calculate cumulative length of following
  476. * initrds (including padding).
  477. */
  478. offset = 0;
  479. for_each_image ( other ) {
  480. if ( other == initrd )
  481. offset = 0;
  482. offset += bzimage_load_initrd ( image, other, UNULL );
  483. offset = bzimage_align ( offset );
  484. }
  485. /* Load initrd at this address */
  486. dest = userptr_add ( top, -offset );
  487. len = bzimage_load_initrd ( image, initrd, dest );
  488. /* Record initrd location */
  489. if ( ! bzimg->ramdisk_image )
  490. bzimg->ramdisk_image = user_to_phys ( dest, 0 );
  491. bzimg->ramdisk_size = ( user_to_phys ( dest, len ) -
  492. bzimg->ramdisk_image );
  493. }
  494. DBGC ( image, "bzImage %p initrds at [%#08lx,%#08lx)\n",
  495. image, bzimg->ramdisk_image,
  496. ( bzimg->ramdisk_image + bzimg->ramdisk_size ) );
  497. }
  498. /**
  499. * Execute bzImage image
  500. *
  501. * @v image bzImage image
  502. * @ret rc Return status code
  503. */
  504. static int bzimage_exec ( struct image *image ) {
  505. struct bzimage_context bzimg;
  506. const char *cmdline = ( image->cmdline ? image->cmdline : "" );
  507. int rc;
  508. /* Read and parse header from image */
  509. if ( ( rc = bzimage_parse_header ( image, &bzimg,
  510. image->data ) ) != 0 )
  511. return rc;
  512. /* Prepare segments */
  513. if ( ( rc = prep_segment ( bzimg.rm_kernel, bzimg.rm_filesz,
  514. bzimg.rm_memsz ) ) != 0 ) {
  515. DBGC ( image, "bzImage %p could not prepare RM segment: %s\n",
  516. image, strerror ( rc ) );
  517. return rc;
  518. }
  519. if ( ( rc = prep_segment ( bzimg.pm_kernel, bzimg.pm_sz,
  520. bzimg.pm_sz ) ) != 0 ) {
  521. DBGC ( image, "bzImage %p could not prepare PM segment: %s\n",
  522. image, strerror ( rc ) );
  523. return rc;
  524. }
  525. /* Parse command line for bootloader parameters */
  526. if ( ( rc = bzimage_parse_cmdline ( image, &bzimg, cmdline ) ) != 0)
  527. return rc;
  528. /* Check that initrds can be loaded */
  529. if ( ( rc = bzimage_check_initrds ( image, &bzimg ) ) != 0 )
  530. return rc;
  531. /* Remove kernel from image list (without invalidating image pointer) */
  532. unregister_image ( image_get ( image ) );
  533. /* Load segments */
  534. memcpy_user ( bzimg.rm_kernel, 0, image->data,
  535. 0, bzimg.rm_filesz );
  536. memcpy_user ( bzimg.pm_kernel, 0, image->data,
  537. bzimg.rm_filesz, bzimg.pm_sz );
  538. /* Store command line */
  539. bzimage_set_cmdline ( image, &bzimg, cmdline );
  540. /* Prepare for exiting. Must do this before loading initrds,
  541. * since loading the initrds will corrupt the external heap.
  542. */
  543. shutdown_boot();
  544. /* Load any initrds */
  545. bzimage_load_initrds ( image, &bzimg );
  546. /* Update kernel header */
  547. bzimage_update_header ( image, &bzimg, bzimg.rm_kernel );
  548. DBGC ( image, "bzImage %p jumping to RM kernel at %04x:0000 "
  549. "(stack %04x:%04zx)\n", image, ( bzimg.rm_kernel_seg + 0x20 ),
  550. bzimg.rm_kernel_seg, bzimg.rm_heap );
  551. /* Jump to the kernel */
  552. __asm__ __volatile__ ( REAL_CODE ( "movw %w0, %%ds\n\t"
  553. "movw %w0, %%es\n\t"
  554. "movw %w0, %%fs\n\t"
  555. "movw %w0, %%gs\n\t"
  556. "movw %w0, %%ss\n\t"
  557. "movw %w1, %%sp\n\t"
  558. "pushw %w2\n\t"
  559. "pushw $0\n\t"
  560. "lret\n\t" )
  561. : : "R" ( bzimg.rm_kernel_seg ),
  562. "R" ( bzimg.rm_heap ),
  563. "R" ( bzimg.rm_kernel_seg + 0x20 ) );
  564. /* There is no way for the image to return, since we provide
  565. * no return address.
  566. */
  567. assert ( 0 );
  568. return -ECANCELED; /* -EIMPOSSIBLE */
  569. }
  570. /**
  571. * Probe bzImage image
  572. *
  573. * @v image bzImage file
  574. * @ret rc Return status code
  575. */
  576. int bzimage_probe ( struct image *image ) {
  577. struct bzimage_context bzimg;
  578. int rc;
  579. /* Read and parse header from image */
  580. if ( ( rc = bzimage_parse_header ( image, &bzimg,
  581. image->data ) ) != 0 )
  582. return rc;
  583. return 0;
  584. }
  585. /** Linux bzImage image type */
  586. struct image_type bzimage_image_type __image_type ( PROBE_NORMAL ) = {
  587. .name = "bzImage",
  588. .probe = bzimage_probe,
  589. .exec = bzimage_exec,
  590. };