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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. /**
  20. * @file
  21. *
  22. * Linux bzImage image format
  23. *
  24. */
  25. #include <stdint.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <errno.h>
  29. #include <assert.h>
  30. #include <realmode.h>
  31. #include <bzimage.h>
  32. #include <ipxe/uaccess.h>
  33. #include <ipxe/image.h>
  34. #include <ipxe/segment.h>
  35. #include <ipxe/init.h>
  36. #include <ipxe/cpio.h>
  37. #include <ipxe/features.h>
  38. FEATURE ( FEATURE_IMAGE, "bzImage", DHCP_EB_FEATURE_BZIMAGE, 1 );
  39. struct image_type bzimage_image_type __image_type ( PROBE_NORMAL );
  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 =
  104. ( ( bzimg->bzhdr.setup_sects ? 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. bzimg->bzhdr.setup_move_size = bzimg->rm_memsz;
  193. }
  194. /* Set video mode */
  195. bzimg->bzhdr.vid_mode = bzimg->vid_mode;
  196. /* Set initrd address */
  197. if ( bzimg->version >= 0x0200 ) {
  198. bzimg->bzhdr.ramdisk_image = bzimg->ramdisk_image;
  199. bzimg->bzhdr.ramdisk_size = bzimg->ramdisk_size;
  200. }
  201. /* Write out header structures */
  202. copy_to_user ( dst, BZI_CMDLINE_OFFSET, &bzimg->cmdline_magic,
  203. sizeof ( bzimg->cmdline_magic ) );
  204. copy_to_user ( dst, BZI_HDR_OFFSET, &bzimg->bzhdr,
  205. sizeof ( bzimg->bzhdr ) );
  206. DBGC ( image, "bzImage %p vidmode %d\n", image, bzimg->vid_mode );
  207. }
  208. /**
  209. * Parse kernel command line for bootloader parameters
  210. *
  211. * @v image bzImage file
  212. * @v bzimg bzImage context
  213. * @v cmdline Kernel command line
  214. * @ret rc Return status code
  215. */
  216. static int bzimage_parse_cmdline ( struct image *image,
  217. struct bzimage_context *bzimg,
  218. const char *cmdline ) {
  219. char *vga;
  220. char *mem;
  221. /* Look for "vga=" */
  222. if ( ( vga = strstr ( cmdline, "vga=" ) ) ) {
  223. vga += 4;
  224. if ( strcmp ( vga, "normal" ) == 0 ) {
  225. bzimg->vid_mode = BZI_VID_MODE_NORMAL;
  226. } else if ( strcmp ( vga, "ext" ) == 0 ) {
  227. bzimg->vid_mode = BZI_VID_MODE_EXT;
  228. } else if ( strcmp ( vga, "ask" ) == 0 ) {
  229. bzimg->vid_mode = BZI_VID_MODE_ASK;
  230. } else {
  231. bzimg->vid_mode = strtoul ( vga, &vga, 0 );
  232. if ( *vga && ( *vga != ' ' ) ) {
  233. DBGC ( image, "bzImage %p strange \"vga=\""
  234. "terminator '%c'\n", image, *vga );
  235. }
  236. }
  237. }
  238. /* Look for "mem=" */
  239. if ( ( mem = strstr ( cmdline, "mem=" ) ) ) {
  240. mem += 4;
  241. bzimg->mem_limit = strtoul ( mem, &mem, 0 );
  242. switch ( *mem ) {
  243. case 'G':
  244. case 'g':
  245. bzimg->mem_limit <<= 10;
  246. case 'M':
  247. case 'm':
  248. bzimg->mem_limit <<= 10;
  249. case 'K':
  250. case 'k':
  251. bzimg->mem_limit <<= 10;
  252. break;
  253. case '\0':
  254. case ' ':
  255. break;
  256. default:
  257. DBGC ( image, "bzImage %p strange \"mem=\" "
  258. "terminator '%c'\n", image, *mem );
  259. break;
  260. }
  261. bzimg->mem_limit -= 1;
  262. }
  263. return 0;
  264. }
  265. /**
  266. * Set command line
  267. *
  268. * @v image bzImage image
  269. * @v bzimg bzImage context
  270. * @v cmdline Kernel command line
  271. * @ret rc Return status code
  272. */
  273. static int bzimage_set_cmdline ( struct image *image,
  274. struct bzimage_context *bzimg,
  275. const char *cmdline ) {
  276. size_t cmdline_len;
  277. /* Copy command line down to real-mode portion */
  278. cmdline_len = ( strlen ( cmdline ) + 1 );
  279. if ( cmdline_len > bzimg->cmdline_size )
  280. cmdline_len = bzimg->cmdline_size;
  281. copy_to_user ( bzimg->rm_kernel, bzimg->rm_cmdline,
  282. cmdline, cmdline_len );
  283. DBGC ( image, "bzImage %p command line \"%s\"\n", image, cmdline );
  284. return 0;
  285. }
  286. /**
  287. * Load initrd
  288. *
  289. * @v image bzImage image
  290. * @v initrd initrd image
  291. * @v address Address at which to load, or UNULL
  292. * @ret len Length of loaded image, rounded up to 4 bytes
  293. */
  294. static size_t bzimage_load_initrd ( struct image *image,
  295. struct image *initrd,
  296. userptr_t address ) {
  297. char *filename = initrd->cmdline;
  298. struct cpio_header cpio;
  299. size_t offset = 0;
  300. /* Do not include kernel image itself as an initrd */
  301. if ( initrd == image )
  302. return 0;
  303. /* Create cpio header before non-prebuilt images */
  304. if ( filename && filename[0] ) {
  305. size_t name_len = ( strlen ( filename ) + 1 );
  306. DBGC ( image, "bzImage %p inserting initrd %p as %s\n",
  307. image, initrd, filename );
  308. memset ( &cpio, '0', sizeof ( cpio ) );
  309. memcpy ( cpio.c_magic, CPIO_MAGIC, sizeof ( cpio.c_magic ) );
  310. cpio_set_field ( cpio.c_mode, 0100644 );
  311. cpio_set_field ( cpio.c_nlink, 1 );
  312. cpio_set_field ( cpio.c_filesize, initrd->len );
  313. cpio_set_field ( cpio.c_namesize, name_len );
  314. if ( address ) {
  315. copy_to_user ( address, offset, &cpio,
  316. sizeof ( cpio ) );
  317. }
  318. offset += sizeof ( cpio );
  319. if ( address ) {
  320. copy_to_user ( address, offset, filename,
  321. name_len );
  322. }
  323. offset += name_len;
  324. offset = ( ( offset + 0x03 ) & ~0x03 );
  325. }
  326. /* Copy in initrd image body */
  327. if ( address )
  328. memcpy_user ( address, offset, initrd->data, 0, initrd->len );
  329. offset += initrd->len;
  330. if ( address ) {
  331. DBGC ( image, "bzImage %p has initrd %p at [%lx,%lx)\n",
  332. image, initrd, user_to_phys ( address, 0 ),
  333. user_to_phys ( address, offset ) );
  334. }
  335. /* Round up to 4-byte boundary */
  336. offset = ( ( offset + 0x03 ) & ~0x03 );
  337. return offset;
  338. }
  339. /**
  340. * Load initrds, if any
  341. *
  342. * @v image bzImage image
  343. * @v bzimg bzImage context
  344. * @ret rc Return status code
  345. */
  346. static int bzimage_load_initrds ( struct image *image,
  347. struct bzimage_context *bzimg ) {
  348. struct image *initrd;
  349. size_t total_len = 0;
  350. physaddr_t address;
  351. int rc;
  352. /* Add up length of all initrd images */
  353. for_each_image ( initrd )
  354. total_len += bzimage_load_initrd ( image, initrd, UNULL );
  355. /* Give up if no initrd images found */
  356. if ( ! total_len )
  357. return 0;
  358. /* Find a suitable start address. Try 1MB boundaries,
  359. * starting from the downloaded kernel image itself and
  360. * working downwards until we hit an available region.
  361. */
  362. for ( address = ( user_to_phys ( image->data, 0 ) & ~0xfffff ) ; ;
  363. address -= 0x100000 ) {
  364. /* Check that we're not going to overwrite the
  365. * kernel itself. This check isn't totally
  366. * accurate, but errs on the side of caution.
  367. */
  368. if ( address <= ( BZI_LOAD_HIGH_ADDR + image->len ) ) {
  369. DBGC ( image, "bzImage %p could not find a location "
  370. "for initrd\n", image );
  371. return -ENOBUFS;
  372. }
  373. /* Check that we are within the kernel's range */
  374. if ( ( address + total_len - 1 ) > bzimg->mem_limit )
  375. continue;
  376. /* Prepare and verify segment */
  377. if ( ( rc = prep_segment ( phys_to_user ( address ), 0,
  378. total_len ) ) != 0 )
  379. continue;
  380. /* Use this address */
  381. break;
  382. }
  383. /* Record initrd location */
  384. bzimg->ramdisk_image = address;
  385. bzimg->ramdisk_size = total_len;
  386. /* Construct initrd */
  387. DBGC ( image, "bzImage %p constructing initrd at [%lx,%lx)\n",
  388. image, address, ( address + total_len ) );
  389. for_each_image ( initrd ) {
  390. address += bzimage_load_initrd ( image, initrd,
  391. phys_to_user ( address ) );
  392. }
  393. return 0;
  394. }
  395. /**
  396. * Execute bzImage image
  397. *
  398. * @v image bzImage image
  399. * @ret rc Return status code
  400. */
  401. static int bzimage_exec ( struct image *image ) {
  402. struct bzimage_context bzimg;
  403. const char *cmdline = ( image->cmdline ? image->cmdline : "" );
  404. int rc;
  405. /* Read and parse header from loaded kernel */
  406. if ( ( rc = bzimage_parse_header ( image, &bzimg,
  407. image->priv.user ) ) != 0 )
  408. return rc;
  409. assert ( bzimg.rm_kernel == image->priv.user );
  410. /* Parse command line for bootloader parameters */
  411. if ( ( rc = bzimage_parse_cmdline ( image, &bzimg, cmdline ) ) != 0)
  412. return rc;
  413. /* Store command line */
  414. if ( ( rc = bzimage_set_cmdline ( image, &bzimg, cmdline ) ) != 0 )
  415. return rc;
  416. /* Load any initrds */
  417. if ( ( rc = bzimage_load_initrds ( image, &bzimg ) ) != 0 )
  418. return rc;
  419. /* Update kernel header */
  420. bzimage_update_header ( image, &bzimg, bzimg.rm_kernel );
  421. /* Prepare for exiting */
  422. shutdown ( SHUTDOWN_BOOT );
  423. DBGC ( image, "bzImage %p jumping to RM kernel at %04x:0000 "
  424. "(stack %04x:%04zx)\n", image, ( bzimg.rm_kernel_seg + 0x20 ),
  425. bzimg.rm_kernel_seg, bzimg.rm_heap );
  426. /* Jump to the kernel */
  427. __asm__ __volatile__ ( REAL_CODE ( "movw %w0, %%ds\n\t"
  428. "movw %w0, %%es\n\t"
  429. "movw %w0, %%fs\n\t"
  430. "movw %w0, %%gs\n\t"
  431. "movw %w0, %%ss\n\t"
  432. "movw %w1, %%sp\n\t"
  433. "pushw %w2\n\t"
  434. "pushw $0\n\t"
  435. "lret\n\t" )
  436. : : "r" ( bzimg.rm_kernel_seg ),
  437. "r" ( bzimg.rm_heap ),
  438. "r" ( bzimg.rm_kernel_seg + 0x20 ) );
  439. /* There is no way for the image to return, since we provide
  440. * no return address.
  441. */
  442. assert ( 0 );
  443. return -ECANCELED; /* -EIMPOSSIBLE */
  444. }
  445. /**
  446. * Load bzImage image into memory
  447. *
  448. * @v image bzImage file
  449. * @ret rc Return status code
  450. */
  451. int bzimage_load ( struct image *image ) {
  452. struct bzimage_context bzimg;
  453. int rc;
  454. /* Read and parse header from image */
  455. if ( ( rc = bzimage_parse_header ( image, &bzimg,
  456. image->data ) ) != 0 )
  457. return rc;
  458. /* This is a bzImage image, valid or otherwise */
  459. if ( ! image->type )
  460. image->type = &bzimage_image_type;
  461. /* Prepare segments */
  462. if ( ( rc = prep_segment ( bzimg.rm_kernel, bzimg.rm_filesz,
  463. bzimg.rm_memsz ) ) != 0 ) {
  464. DBGC ( image, "bzImage %p could not prepare RM segment: %s\n",
  465. image, strerror ( rc ) );
  466. return rc;
  467. }
  468. if ( ( rc = prep_segment ( bzimg.pm_kernel, bzimg.pm_sz,
  469. bzimg.pm_sz ) ) != 0 ) {
  470. DBGC ( image, "bzImage %p could not prepare PM segment: %s\n",
  471. image, strerror ( rc ) );
  472. return rc;
  473. }
  474. /* Load segments */
  475. memcpy_user ( bzimg.rm_kernel, 0, image->data,
  476. 0, bzimg.rm_filesz );
  477. memcpy_user ( bzimg.pm_kernel, 0, image->data,
  478. bzimg.rm_filesz, bzimg.pm_sz );
  479. /* Update and write out header */
  480. bzimage_update_header ( image, &bzimg, bzimg.rm_kernel );
  481. /* Record real-mode segment in image private data field */
  482. image->priv.user = bzimg.rm_kernel;
  483. return 0;
  484. }
  485. /** Linux bzImage image type */
  486. struct image_type bzimage_image_type __image_type ( PROBE_NORMAL ) = {
  487. .name = "bzImage",
  488. .load = bzimage_load,
  489. .exec = bzimage_exec,
  490. };