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

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