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

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