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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. /**
  21. * @file
  22. *
  23. * Linux bzImage image format
  24. *
  25. */
  26. #include <stdint.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include <assert.h>
  31. #include <realmode.h>
  32. #include <bzimage.h>
  33. #include <ipxe/uaccess.h>
  34. #include <ipxe/image.h>
  35. #include <ipxe/segment.h>
  36. #include <ipxe/init.h>
  37. #include <ipxe/cpio.h>
  38. #include <ipxe/features.h>
  39. FEATURE ( FEATURE_IMAGE, "bzImage", DHCP_EB_FEATURE_BZIMAGE, 1 );
  40. /**
  41. * bzImage context
  42. */
  43. struct bzimage_context {
  44. /** Boot protocol version */
  45. unsigned int version;
  46. /** Real-mode kernel portion load segment address */
  47. unsigned int rm_kernel_seg;
  48. /** Real-mode kernel portion load address */
  49. userptr_t rm_kernel;
  50. /** Real-mode kernel portion file size */
  51. size_t rm_filesz;
  52. /** Real-mode heap top (offset from rm_kernel) */
  53. size_t rm_heap;
  54. /** Command line (offset from rm_kernel) */
  55. size_t rm_cmdline;
  56. /** Command line maximum length */
  57. size_t cmdline_size;
  58. /** Real-mode kernel portion total memory size */
  59. size_t rm_memsz;
  60. /** Non-real-mode kernel portion load address */
  61. userptr_t pm_kernel;
  62. /** Non-real-mode kernel portion file and memory size */
  63. size_t pm_sz;
  64. /** Video mode */
  65. unsigned int vid_mode;
  66. /** Memory limit */
  67. uint64_t mem_limit;
  68. /** Initrd address */
  69. physaddr_t ramdisk_image;
  70. /** Initrd size */
  71. physaddr_t ramdisk_size;
  72. /** Command line magic block */
  73. struct bzimage_cmdline cmdline_magic;
  74. /** bzImage header */
  75. struct bzimage_header bzhdr;
  76. };
  77. /**
  78. * Parse bzImage header
  79. *
  80. * @v image bzImage file
  81. * @v bzimg bzImage context
  82. * @v src bzImage to parse
  83. * @ret rc Return status code
  84. */
  85. static int bzimage_parse_header ( struct image *image,
  86. struct bzimage_context *bzimg,
  87. userptr_t src ) {
  88. unsigned int syssize;
  89. int is_bzimage;
  90. /* Sanity check */
  91. if ( image->len < ( BZI_HDR_OFFSET + sizeof ( bzimg->bzhdr ) ) ) {
  92. DBGC ( image, "bzImage %p too short for kernel header\n",
  93. image );
  94. return -ENOEXEC;
  95. }
  96. /* Read in header structures */
  97. memset ( bzimg, 0, sizeof ( *bzimg ) );
  98. copy_from_user ( &bzimg->cmdline_magic, src, BZI_CMDLINE_OFFSET,
  99. sizeof ( bzimg->cmdline_magic ) );
  100. copy_from_user ( &bzimg->bzhdr, src, BZI_HDR_OFFSET,
  101. sizeof ( bzimg->bzhdr ) );
  102. /* Calculate size of real-mode portion */
  103. bzimg->rm_filesz = ( ( ( bzimg->bzhdr.setup_sects ?
  104. bzimg->bzhdr.setup_sects : 4 ) + 1 ) << 9 );
  105. if ( bzimg->rm_filesz > image->len ) {
  106. DBGC ( image, "bzImage %p too short for %zd byte of setup\n",
  107. image, bzimg->rm_filesz );
  108. return -ENOEXEC;
  109. }
  110. bzimg->rm_memsz = BZI_ASSUMED_RM_SIZE;
  111. /* Calculate size of protected-mode portion */
  112. bzimg->pm_sz = ( image->len - bzimg->rm_filesz );
  113. syssize = ( ( bzimg->pm_sz + 15 ) / 16 );
  114. /* Check for signatures and determine version */
  115. if ( bzimg->bzhdr.boot_flag != BZI_BOOT_FLAG ) {
  116. DBGC ( image, "bzImage %p missing 55AA signature\n", image );
  117. return -ENOEXEC;
  118. }
  119. if ( bzimg->bzhdr.header == BZI_SIGNATURE ) {
  120. /* 2.00+ */
  121. bzimg->version = bzimg->bzhdr.version;
  122. } else {
  123. /* Pre-2.00. Check that the syssize field is correct,
  124. * as a guard against accepting arbitrary binary data,
  125. * since the 55AA check is pretty lax. Note that the
  126. * syssize field is unreliable for protocols between
  127. * 2.00 and 2.03 inclusive, so we should not always
  128. * check this field.
  129. */
  130. bzimg->version = 0x0100;
  131. if ( bzimg->bzhdr.syssize != syssize ) {
  132. DBGC ( image, "bzImage %p bad syssize %x (expected "
  133. "%x)\n", image, bzimg->bzhdr.syssize, syssize );
  134. return -ENOEXEC;
  135. }
  136. }
  137. /* Determine image type */
  138. is_bzimage = ( ( bzimg->version >= 0x0200 ) ?
  139. ( bzimg->bzhdr.loadflags & BZI_LOAD_HIGH ) : 0 );
  140. /* Calculate load address of real-mode portion */
  141. bzimg->rm_kernel_seg = ( is_bzimage ? 0x1000 : 0x9000 );
  142. bzimg->rm_kernel = real_to_user ( bzimg->rm_kernel_seg, 0 );
  143. /* Allow space for the stack and heap */
  144. bzimg->rm_memsz += BZI_STACK_SIZE;
  145. bzimg->rm_heap = bzimg->rm_memsz;
  146. /* Allow space for the command line */
  147. bzimg->rm_cmdline = bzimg->rm_memsz;
  148. bzimg->rm_memsz += BZI_CMDLINE_SIZE;
  149. /* Calculate load address of protected-mode portion */
  150. bzimg->pm_kernel = phys_to_user ( is_bzimage ? BZI_LOAD_HIGH_ADDR
  151. : BZI_LOAD_LOW_ADDR );
  152. /* Extract video mode */
  153. bzimg->vid_mode = bzimg->bzhdr.vid_mode;
  154. /* Extract memory limit */
  155. bzimg->mem_limit = ( ( bzimg->version >= 0x0203 ) ?
  156. bzimg->bzhdr.initrd_addr_max : BZI_INITRD_MAX );
  157. /* Extract command line size */
  158. bzimg->cmdline_size = ( ( bzimg->version >= 0x0206 ) ?
  159. bzimg->bzhdr.cmdline_size : BZI_CMDLINE_SIZE );
  160. DBGC ( image, "bzImage %p version %04x RM %#lx+%#zx PM %#lx+%#zx "
  161. "cmdlen %zd\n", image, bzimg->version,
  162. user_to_phys ( bzimg->rm_kernel, 0 ), bzimg->rm_filesz,
  163. user_to_phys ( bzimg->pm_kernel, 0 ), bzimg->pm_sz,
  164. bzimg->cmdline_size );
  165. return 0;
  166. }
  167. /**
  168. * Update bzImage header in loaded kernel
  169. *
  170. * @v image bzImage file
  171. * @v bzimg bzImage context
  172. * @v dst bzImage to update
  173. */
  174. static void bzimage_update_header ( struct image *image,
  175. struct bzimage_context *bzimg,
  176. userptr_t dst ) {
  177. /* Set loader type */
  178. if ( bzimg->version >= 0x0200 )
  179. bzimg->bzhdr.type_of_loader = BZI_LOADER_TYPE_IPXE;
  180. /* Set heap end pointer */
  181. if ( bzimg->version >= 0x0201 ) {
  182. bzimg->bzhdr.heap_end_ptr = ( bzimg->rm_heap - 0x200 );
  183. bzimg->bzhdr.loadflags |= BZI_CAN_USE_HEAP;
  184. }
  185. /* Set command line */
  186. if ( bzimg->version >= 0x0202 ) {
  187. bzimg->bzhdr.cmd_line_ptr = user_to_phys ( bzimg->rm_kernel,
  188. bzimg->rm_cmdline );
  189. } else {
  190. bzimg->cmdline_magic.magic = BZI_CMDLINE_MAGIC;
  191. bzimg->cmdline_magic.offset = bzimg->rm_cmdline;
  192. if ( bzimg->version >= 0x0200 )
  193. bzimg->bzhdr.setup_move_size = bzimg->rm_memsz;
  194. }
  195. /* Set video mode */
  196. bzimg->bzhdr.vid_mode = bzimg->vid_mode;
  197. /* Set initrd address */
  198. if ( bzimg->version >= 0x0200 ) {
  199. bzimg->bzhdr.ramdisk_image = bzimg->ramdisk_image;
  200. bzimg->bzhdr.ramdisk_size = bzimg->ramdisk_size;
  201. }
  202. /* Write out header structures */
  203. copy_to_user ( dst, BZI_CMDLINE_OFFSET, &bzimg->cmdline_magic,
  204. sizeof ( bzimg->cmdline_magic ) );
  205. copy_to_user ( dst, BZI_HDR_OFFSET, &bzimg->bzhdr,
  206. sizeof ( bzimg->bzhdr ) );
  207. DBGC ( image, "bzImage %p vidmode %d\n", image, bzimg->vid_mode );
  208. }
  209. /**
  210. * Parse kernel command line for bootloader parameters
  211. *
  212. * @v image bzImage file
  213. * @v bzimg bzImage context
  214. * @v cmdline Kernel command line
  215. * @ret rc Return status code
  216. */
  217. static int bzimage_parse_cmdline ( struct image *image,
  218. struct bzimage_context *bzimg,
  219. const char *cmdline ) {
  220. char *vga;
  221. char *mem;
  222. /* Look for "vga=" */
  223. if ( ( vga = strstr ( cmdline, "vga=" ) ) ) {
  224. vga += 4;
  225. if ( strcmp ( vga, "normal" ) == 0 ) {
  226. bzimg->vid_mode = BZI_VID_MODE_NORMAL;
  227. } else if ( strcmp ( vga, "ext" ) == 0 ) {
  228. bzimg->vid_mode = BZI_VID_MODE_EXT;
  229. } else if ( strcmp ( vga, "ask" ) == 0 ) {
  230. bzimg->vid_mode = BZI_VID_MODE_ASK;
  231. } else {
  232. bzimg->vid_mode = strtoul ( vga, &vga, 0 );
  233. if ( *vga && ( *vga != ' ' ) ) {
  234. DBGC ( image, "bzImage %p strange \"vga=\""
  235. "terminator '%c'\n", image, *vga );
  236. }
  237. }
  238. }
  239. /* Look for "mem=" */
  240. if ( ( mem = strstr ( cmdline, "mem=" ) ) ) {
  241. mem += 4;
  242. bzimg->mem_limit = strtoul ( mem, &mem, 0 );
  243. switch ( *mem ) {
  244. case 'G':
  245. case 'g':
  246. bzimg->mem_limit <<= 10;
  247. case 'M':
  248. case 'm':
  249. bzimg->mem_limit <<= 10;
  250. case 'K':
  251. case 'k':
  252. bzimg->mem_limit <<= 10;
  253. break;
  254. case '\0':
  255. case ' ':
  256. break;
  257. default:
  258. DBGC ( image, "bzImage %p strange \"mem=\" "
  259. "terminator '%c'\n", image, *mem );
  260. break;
  261. }
  262. bzimg->mem_limit -= 1;
  263. }
  264. return 0;
  265. }
  266. /**
  267. * Set command line
  268. *
  269. * @v image bzImage image
  270. * @v bzimg bzImage context
  271. * @v cmdline Kernel command line
  272. * @ret rc Return status code
  273. */
  274. static int bzimage_set_cmdline ( struct image *image,
  275. struct bzimage_context *bzimg,
  276. const char *cmdline ) {
  277. size_t cmdline_len;
  278. /* Copy command line down to real-mode portion */
  279. cmdline_len = ( strlen ( cmdline ) + 1 );
  280. if ( cmdline_len > bzimg->cmdline_size )
  281. cmdline_len = bzimg->cmdline_size;
  282. copy_to_user ( bzimg->rm_kernel, bzimg->rm_cmdline,
  283. cmdline, cmdline_len );
  284. DBGC ( image, "bzImage %p command line \"%s\"\n", image, cmdline );
  285. return 0;
  286. }
  287. /**
  288. * Load initrd
  289. *
  290. * @v image bzImage image
  291. * @v initrd initrd image
  292. * @v address Address at which to load, or UNULL
  293. * @ret len Length of loaded image, rounded up to 4 bytes
  294. */
  295. static size_t bzimage_load_initrd ( struct image *image,
  296. struct image *initrd,
  297. userptr_t address ) {
  298. char *filename = initrd->cmdline;
  299. struct cpio_header cpio;
  300. size_t offset = 0;
  301. /* Do not include kernel image itself as an initrd */
  302. if ( initrd == image )
  303. return 0;
  304. /* Create cpio header before non-prebuilt images */
  305. if ( filename && filename[0] ) {
  306. size_t name_len = ( strlen ( filename ) + 1 );
  307. DBGC ( image, "bzImage %p inserting initrd %p as %s\n",
  308. image, initrd, filename );
  309. memset ( &cpio, '0', sizeof ( cpio ) );
  310. memcpy ( cpio.c_magic, CPIO_MAGIC, sizeof ( cpio.c_magic ) );
  311. cpio_set_field ( cpio.c_mode, 0100644 );
  312. cpio_set_field ( cpio.c_nlink, 1 );
  313. cpio_set_field ( cpio.c_filesize, initrd->len );
  314. cpio_set_field ( cpio.c_namesize, name_len );
  315. if ( address ) {
  316. copy_to_user ( address, offset, &cpio,
  317. sizeof ( cpio ) );
  318. }
  319. offset += sizeof ( cpio );
  320. if ( address ) {
  321. copy_to_user ( address, offset, filename,
  322. name_len );
  323. }
  324. offset += name_len;
  325. offset = ( ( offset + 0x03 ) & ~0x03 );
  326. }
  327. /* Copy in initrd image body */
  328. if ( address )
  329. memcpy_user ( address, offset, initrd->data, 0, initrd->len );
  330. offset += initrd->len;
  331. if ( address ) {
  332. DBGC ( image, "bzImage %p has initrd %p at [%lx,%lx)\n",
  333. image, initrd, user_to_phys ( address, 0 ),
  334. user_to_phys ( address, offset ) );
  335. }
  336. /* Round up to 4-byte boundary */
  337. offset = ( ( offset + 0x03 ) & ~0x03 );
  338. return offset;
  339. }
  340. /**
  341. * Load initrds, if any
  342. *
  343. * @v image bzImage image
  344. * @v bzimg bzImage context
  345. * @ret rc Return status code
  346. */
  347. static int bzimage_load_initrds ( struct image *image,
  348. struct bzimage_context *bzimg ) {
  349. struct image *initrd;
  350. size_t total_len = 0;
  351. physaddr_t address;
  352. int rc;
  353. /* Add up length of all initrd images */
  354. for_each_image ( initrd )
  355. total_len += bzimage_load_initrd ( image, initrd, UNULL );
  356. /* Give up if no initrd images found */
  357. if ( ! total_len )
  358. return 0;
  359. /* Find a suitable start address. Try 1MB boundaries,
  360. * starting from the downloaded kernel image itself and
  361. * working downwards until we hit an available region.
  362. */
  363. for ( address = ( user_to_phys ( image->data, 0 ) & ~0xfffff ) ; ;
  364. address -= 0x100000 ) {
  365. /* Check that we're not going to overwrite the
  366. * kernel itself. This check isn't totally
  367. * accurate, but errs on the side of caution.
  368. */
  369. if ( address <= ( BZI_LOAD_HIGH_ADDR + image->len ) ) {
  370. DBGC ( image, "bzImage %p could not find a location "
  371. "for initrd\n", image );
  372. return -ENOBUFS;
  373. }
  374. /* Check that we are within the kernel's range */
  375. if ( ( address + total_len - 1 ) > bzimg->mem_limit )
  376. continue;
  377. /* Prepare and verify segment */
  378. if ( ( rc = prep_segment ( phys_to_user ( address ), 0,
  379. total_len ) ) != 0 )
  380. continue;
  381. /* Use this address */
  382. break;
  383. }
  384. /* Record initrd location */
  385. bzimg->ramdisk_image = address;
  386. bzimg->ramdisk_size = total_len;
  387. /* Construct initrd */
  388. DBGC ( image, "bzImage %p constructing initrd at [%lx,%lx)\n",
  389. image, address, ( address + total_len ) );
  390. for_each_image ( initrd ) {
  391. address += bzimage_load_initrd ( image, initrd,
  392. phys_to_user ( address ) );
  393. }
  394. return 0;
  395. }
  396. /**
  397. * Execute bzImage image
  398. *
  399. * @v image bzImage image
  400. * @ret rc Return status code
  401. */
  402. static int bzimage_exec ( struct image *image ) {
  403. struct bzimage_context bzimg;
  404. const char *cmdline = ( image->cmdline ? image->cmdline : "" );
  405. int rc;
  406. /* Read and parse header from image */
  407. if ( ( rc = bzimage_parse_header ( image, &bzimg,
  408. image->data ) ) != 0 )
  409. return rc;
  410. /* Prepare segments */
  411. if ( ( rc = prep_segment ( bzimg.rm_kernel, bzimg.rm_filesz,
  412. bzimg.rm_memsz ) ) != 0 ) {
  413. DBGC ( image, "bzImage %p could not prepare RM segment: %s\n",
  414. image, strerror ( rc ) );
  415. return rc;
  416. }
  417. if ( ( rc = prep_segment ( bzimg.pm_kernel, bzimg.pm_sz,
  418. bzimg.pm_sz ) ) != 0 ) {
  419. DBGC ( image, "bzImage %p could not prepare PM segment: %s\n",
  420. image, strerror ( rc ) );
  421. return rc;
  422. }
  423. /* Load segments */
  424. memcpy_user ( bzimg.rm_kernel, 0, image->data,
  425. 0, bzimg.rm_filesz );
  426. memcpy_user ( bzimg.pm_kernel, 0, image->data,
  427. bzimg.rm_filesz, bzimg.pm_sz );
  428. /* Update and write out header */
  429. bzimage_update_header ( image, &bzimg, bzimg.rm_kernel );
  430. /* Parse command line for bootloader parameters */
  431. if ( ( rc = bzimage_parse_cmdline ( image, &bzimg, cmdline ) ) != 0)
  432. return rc;
  433. /* Store command line */
  434. if ( ( rc = bzimage_set_cmdline ( image, &bzimg, cmdline ) ) != 0 )
  435. return rc;
  436. /* Load any initrds */
  437. if ( ( rc = bzimage_load_initrds ( image, &bzimg ) ) != 0 )
  438. return rc;
  439. /* Update kernel header */
  440. bzimage_update_header ( image, &bzimg, bzimg.rm_kernel );
  441. /* Prepare for exiting */
  442. shutdown_boot();
  443. DBGC ( image, "bzImage %p jumping to RM kernel at %04x:0000 "
  444. "(stack %04x:%04zx)\n", image, ( bzimg.rm_kernel_seg + 0x20 ),
  445. bzimg.rm_kernel_seg, bzimg.rm_heap );
  446. /* Jump to the kernel */
  447. __asm__ __volatile__ ( REAL_CODE ( "movw %w0, %%ds\n\t"
  448. "movw %w0, %%es\n\t"
  449. "movw %w0, %%fs\n\t"
  450. "movw %w0, %%gs\n\t"
  451. "movw %w0, %%ss\n\t"
  452. "movw %w1, %%sp\n\t"
  453. "pushw %w2\n\t"
  454. "pushw $0\n\t"
  455. "lret\n\t" )
  456. : : "r" ( bzimg.rm_kernel_seg ),
  457. "r" ( bzimg.rm_heap ),
  458. "r" ( bzimg.rm_kernel_seg + 0x20 ) );
  459. /* There is no way for the image to return, since we provide
  460. * no return address.
  461. */
  462. assert ( 0 );
  463. return -ECANCELED; /* -EIMPOSSIBLE */
  464. }
  465. /**
  466. * Probe bzImage image
  467. *
  468. * @v image bzImage file
  469. * @ret rc Return status code
  470. */
  471. int bzimage_probe ( struct image *image ) {
  472. struct bzimage_context bzimg;
  473. int rc;
  474. /* Read and parse header from image */
  475. if ( ( rc = bzimage_parse_header ( image, &bzimg,
  476. image->data ) ) != 0 )
  477. return rc;
  478. return 0;
  479. }
  480. /** Linux bzImage image type */
  481. struct image_type bzimage_image_type __image_type ( PROBE_NORMAL ) = {
  482. .name = "bzImage",
  483. .probe = bzimage_probe,
  484. .exec = bzimage_exec,
  485. };