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.

multiboot.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. * Multiboot image format
  28. *
  29. */
  30. #include <stdio.h>
  31. #include <errno.h>
  32. #include <assert.h>
  33. #include <realmode.h>
  34. #include <multiboot.h>
  35. #include <ipxe/uaccess.h>
  36. #include <ipxe/image.h>
  37. #include <ipxe/segment.h>
  38. #include <ipxe/io.h>
  39. #include <ipxe/elf.h>
  40. #include <ipxe/init.h>
  41. #include <ipxe/features.h>
  42. #include <ipxe/uri.h>
  43. #include <ipxe/version.h>
  44. FEATURE ( FEATURE_IMAGE, "MBOOT", DHCP_EB_FEATURE_MULTIBOOT, 1 );
  45. /**
  46. * Maximum number of modules we will allow for
  47. *
  48. * If this has bitten you: sorry. I did have a perfect scheme with a
  49. * dynamically allocated list of modules on the protected-mode stack,
  50. * but it was incompatible with some broken OSes that can only access
  51. * low memory at boot time (even though we kindly set up 4GB flat
  52. * physical addressing as per the multiboot specification.
  53. *
  54. */
  55. #define MAX_MODULES 8
  56. /**
  57. * Maximum combined length of command lines
  58. *
  59. * Again; sorry. Some broken OSes zero out any non-base memory that
  60. * isn't part of the loaded module set, so we can't just use
  61. * virt_to_phys(cmdline) to point to the command lines, even though
  62. * this would comply with the Multiboot spec.
  63. */
  64. #define MB_MAX_CMDLINE 512
  65. /** Multiboot flags that we support */
  66. #define MB_SUPPORTED_FLAGS ( MB_FLAG_PGALIGN | MB_FLAG_MEMMAP | \
  67. MB_FLAG_VIDMODE | MB_FLAG_RAW )
  68. /** Compulsory feature multiboot flags */
  69. #define MB_COMPULSORY_FLAGS 0x0000ffff
  70. /** Optional feature multiboot flags */
  71. #define MB_OPTIONAL_FLAGS 0xffff0000
  72. /**
  73. * Multiboot flags that we don't support
  74. *
  75. * We only care about the compulsory feature flags (bits 0-15); we are
  76. * allowed to ignore the optional feature flags.
  77. */
  78. #define MB_UNSUPPORTED_FLAGS ( MB_COMPULSORY_FLAGS & ~MB_SUPPORTED_FLAGS )
  79. /** A multiboot header descriptor */
  80. struct multiboot_header_info {
  81. /** The actual multiboot header */
  82. struct multiboot_header mb;
  83. /** Offset of header within the multiboot image */
  84. size_t offset;
  85. };
  86. /** Multiboot module command lines */
  87. static char __bss16_array ( mb_cmdlines, [MB_MAX_CMDLINE] );
  88. #define mb_cmdlines __use_data16 ( mb_cmdlines )
  89. /** Offset within module command lines */
  90. static unsigned int mb_cmdline_offset;
  91. /**
  92. * Build multiboot memory map
  93. *
  94. * @v image Multiboot image
  95. * @v mbinfo Multiboot information structure
  96. * @v mbmemmap Multiboot memory map
  97. * @v limit Maxmimum number of memory map entries
  98. */
  99. static void multiboot_build_memmap ( struct image *image,
  100. struct multiboot_info *mbinfo,
  101. struct multiboot_memory_map *mbmemmap,
  102. unsigned int limit ) {
  103. struct memory_map memmap;
  104. unsigned int i;
  105. /* Get memory map */
  106. get_memmap ( &memmap );
  107. /* Translate into multiboot format */
  108. memset ( mbmemmap, 0, sizeof ( *mbmemmap ) );
  109. for ( i = 0 ; i < memmap.count ; i++ ) {
  110. if ( i >= limit ) {
  111. DBGC ( image, "MULTIBOOT %p limit of %d memmap "
  112. "entries reached\n", image, limit );
  113. break;
  114. }
  115. mbmemmap[i].size = ( sizeof ( mbmemmap[i] ) -
  116. sizeof ( mbmemmap[i].size ) );
  117. mbmemmap[i].base_addr = memmap.regions[i].start;
  118. mbmemmap[i].length = ( memmap.regions[i].end -
  119. memmap.regions[i].start );
  120. mbmemmap[i].type = MBMEM_RAM;
  121. mbinfo->mmap_length += sizeof ( mbmemmap[i] );
  122. if ( memmap.regions[i].start == 0 )
  123. mbinfo->mem_lower = ( memmap.regions[i].end / 1024 );
  124. if ( memmap.regions[i].start == 0x100000 )
  125. mbinfo->mem_upper = ( ( memmap.regions[i].end -
  126. 0x100000 ) / 1024 );
  127. }
  128. }
  129. /**
  130. * Add command line in base memory
  131. *
  132. * @v image Image
  133. * @ret physaddr Physical address of command line
  134. */
  135. static physaddr_t multiboot_add_cmdline ( struct image *image ) {
  136. char *mb_cmdline = ( mb_cmdlines + mb_cmdline_offset );
  137. size_t remaining = ( sizeof ( mb_cmdlines ) - mb_cmdline_offset );
  138. char *buf = mb_cmdline;
  139. size_t len;
  140. /* Copy image URI to base memory buffer as start of command line */
  141. len = ( format_uri ( image->uri, buf, remaining ) + 1 /* NUL */ );
  142. if ( len > remaining )
  143. len = remaining;
  144. mb_cmdline_offset += len;
  145. buf += len;
  146. remaining -= len;
  147. /* Copy command line to base memory buffer, if present */
  148. if ( image->cmdline ) {
  149. mb_cmdline_offset--; /* Strip NUL */
  150. buf--;
  151. remaining++;
  152. len = ( snprintf ( buf, remaining, " %s",
  153. image->cmdline ) + 1 /* NUL */ );
  154. if ( len > remaining )
  155. len = remaining;
  156. mb_cmdline_offset += len;
  157. }
  158. return virt_to_phys ( mb_cmdline );
  159. }
  160. /**
  161. * Add multiboot modules
  162. *
  163. * @v image Multiboot image
  164. * @v start Start address for modules
  165. * @v mbinfo Multiboot information structure
  166. * @v modules Multiboot module list
  167. * @ret rc Return status code
  168. */
  169. static int multiboot_add_modules ( struct image *image, physaddr_t start,
  170. struct multiboot_info *mbinfo,
  171. struct multiboot_module *modules,
  172. unsigned int limit ) {
  173. struct image *module_image;
  174. struct multiboot_module *module;
  175. int rc;
  176. /* Add each image as a multiboot module */
  177. for_each_image ( module_image ) {
  178. if ( mbinfo->mods_count >= limit ) {
  179. DBGC ( image, "MULTIBOOT %p limit of %d modules "
  180. "reached\n", image, limit );
  181. break;
  182. }
  183. /* Do not include kernel image itself as a module */
  184. if ( module_image == image )
  185. continue;
  186. /* Page-align the module */
  187. start = ( ( start + 0xfff ) & ~0xfff );
  188. /* Prepare segment */
  189. if ( ( rc = prep_segment ( phys_to_user ( start ),
  190. module_image->len,
  191. module_image->len ) ) != 0 ) {
  192. DBGC ( image, "MULTIBOOT %p could not prepare module "
  193. "%s: %s\n", image, module_image->name,
  194. strerror ( rc ) );
  195. return rc;
  196. }
  197. /* Copy module */
  198. memcpy_user ( phys_to_user ( start ), 0,
  199. module_image->data, 0, module_image->len );
  200. /* Add module to list */
  201. module = &modules[mbinfo->mods_count++];
  202. module->mod_start = start;
  203. module->mod_end = ( start + module_image->len );
  204. module->string = multiboot_add_cmdline ( module_image );
  205. module->reserved = 0;
  206. DBGC ( image, "MULTIBOOT %p module %s is [%x,%x)\n",
  207. image, module_image->name, module->mod_start,
  208. module->mod_end );
  209. start += module_image->len;
  210. }
  211. return 0;
  212. }
  213. /**
  214. * The multiboot information structure
  215. *
  216. * Kept in base memory because some OSes won't find it elsewhere,
  217. * along with the other structures belonging to the Multiboot
  218. * information table.
  219. */
  220. static struct multiboot_info __bss16 ( mbinfo );
  221. #define mbinfo __use_data16 ( mbinfo )
  222. /** The multiboot bootloader name */
  223. static char __bss16_array ( mb_bootloader_name, [32] );
  224. #define mb_bootloader_name __use_data16 ( mb_bootloader_name )
  225. /** The multiboot memory map */
  226. static struct multiboot_memory_map
  227. __bss16_array ( mbmemmap, [MAX_MEMORY_REGIONS] );
  228. #define mbmemmap __use_data16 ( mbmemmap )
  229. /** The multiboot module list */
  230. static struct multiboot_module __bss16_array ( mbmodules, [MAX_MODULES] );
  231. #define mbmodules __use_data16 ( mbmodules )
  232. /**
  233. * Find multiboot header
  234. *
  235. * @v image Multiboot file
  236. * @v hdr Multiboot header descriptor to fill in
  237. * @ret rc Return status code
  238. */
  239. static int multiboot_find_header ( struct image *image,
  240. struct multiboot_header_info *hdr ) {
  241. uint32_t buf[64];
  242. size_t offset;
  243. unsigned int buf_idx;
  244. uint32_t checksum;
  245. /* Scan through first 8kB of image file 256 bytes at a time.
  246. * (Use the buffering to avoid the overhead of a
  247. * copy_from_user() for every dword.)
  248. */
  249. for ( offset = 0 ; offset < 8192 ; offset += sizeof ( buf[0] ) ) {
  250. /* Check for end of image */
  251. if ( offset > image->len )
  252. break;
  253. /* Refill buffer if applicable */
  254. buf_idx = ( ( offset % sizeof ( buf ) ) / sizeof ( buf[0] ) );
  255. if ( buf_idx == 0 ) {
  256. copy_from_user ( buf, image->data, offset,
  257. sizeof ( buf ) );
  258. }
  259. /* Check signature */
  260. if ( buf[buf_idx] != MULTIBOOT_HEADER_MAGIC )
  261. continue;
  262. /* Copy header and verify checksum */
  263. copy_from_user ( &hdr->mb, image->data, offset,
  264. sizeof ( hdr->mb ) );
  265. checksum = ( hdr->mb.magic + hdr->mb.flags +
  266. hdr->mb.checksum );
  267. if ( checksum != 0 )
  268. continue;
  269. /* Record offset of multiboot header and return */
  270. hdr->offset = offset;
  271. return 0;
  272. }
  273. /* No multiboot header found */
  274. return -ENOEXEC;
  275. }
  276. /**
  277. * Load raw multiboot image into memory
  278. *
  279. * @v image Multiboot file
  280. * @v hdr Multiboot header descriptor
  281. * @ret entry Entry point
  282. * @ret max Maximum used address
  283. * @ret rc Return status code
  284. */
  285. static int multiboot_load_raw ( struct image *image,
  286. struct multiboot_header_info *hdr,
  287. physaddr_t *entry, physaddr_t *max ) {
  288. size_t offset;
  289. size_t filesz;
  290. size_t memsz;
  291. userptr_t buffer;
  292. int rc;
  293. /* Sanity check */
  294. if ( ! ( hdr->mb.flags & MB_FLAG_RAW ) ) {
  295. DBGC ( image, "MULTIBOOT %p is not flagged as a raw image\n",
  296. image );
  297. return -EINVAL;
  298. }
  299. /* Verify and prepare segment */
  300. offset = ( hdr->offset - hdr->mb.header_addr + hdr->mb.load_addr );
  301. filesz = ( hdr->mb.load_end_addr ?
  302. ( hdr->mb.load_end_addr - hdr->mb.load_addr ) :
  303. ( image->len - offset ) );
  304. memsz = ( hdr->mb.bss_end_addr ?
  305. ( hdr->mb.bss_end_addr - hdr->mb.load_addr ) : filesz );
  306. buffer = phys_to_user ( hdr->mb.load_addr );
  307. if ( ( rc = prep_segment ( buffer, filesz, memsz ) ) != 0 ) {
  308. DBGC ( image, "MULTIBOOT %p could not prepare segment: %s\n",
  309. image, strerror ( rc ) );
  310. return rc;
  311. }
  312. /* Copy image to segment */
  313. memcpy_user ( buffer, 0, image->data, offset, filesz );
  314. /* Record execution entry point and maximum used address */
  315. *entry = hdr->mb.entry_addr;
  316. *max = ( hdr->mb.load_addr + memsz );
  317. return 0;
  318. }
  319. /**
  320. * Load ELF multiboot image into memory
  321. *
  322. * @v image Multiboot file
  323. * @ret entry Entry point
  324. * @ret max Maximum used address
  325. * @ret rc Return status code
  326. */
  327. static int multiboot_load_elf ( struct image *image, physaddr_t *entry,
  328. physaddr_t *max ) {
  329. int rc;
  330. /* Load ELF image*/
  331. if ( ( rc = elf_load ( image, entry, max ) ) != 0 ) {
  332. DBGC ( image, "MULTIBOOT %p ELF image failed to load: %s\n",
  333. image, strerror ( rc ) );
  334. return rc;
  335. }
  336. return 0;
  337. }
  338. /**
  339. * Execute multiboot image
  340. *
  341. * @v image Multiboot image
  342. * @ret rc Return status code
  343. */
  344. static int multiboot_exec ( struct image *image ) {
  345. struct multiboot_header_info hdr;
  346. physaddr_t entry;
  347. physaddr_t max;
  348. int rc;
  349. /* Locate multiboot header, if present */
  350. if ( ( rc = multiboot_find_header ( image, &hdr ) ) != 0 ) {
  351. DBGC ( image, "MULTIBOOT %p has no multiboot header\n",
  352. image );
  353. return rc;
  354. }
  355. /* Abort if we detect flags that we cannot support */
  356. if ( hdr.mb.flags & MB_UNSUPPORTED_FLAGS ) {
  357. DBGC ( image, "MULTIBOOT %p flags %08x not supported\n",
  358. image, ( hdr.mb.flags & MB_UNSUPPORTED_FLAGS ) );
  359. return -ENOTSUP;
  360. }
  361. /* There is technically a bit MB_FLAG_RAW to indicate whether
  362. * this is an ELF or a raw image. In practice, grub will use
  363. * the ELF header if present, and Solaris relies on this
  364. * behaviour.
  365. */
  366. if ( ( ( rc = multiboot_load_elf ( image, &entry, &max ) ) != 0 ) &&
  367. ( ( rc = multiboot_load_raw ( image, &hdr, &entry, &max ) ) != 0 ))
  368. return rc;
  369. /* Populate multiboot information structure */
  370. memset ( &mbinfo, 0, sizeof ( mbinfo ) );
  371. mbinfo.flags = ( MBI_FLAG_LOADER | MBI_FLAG_MEM | MBI_FLAG_MMAP |
  372. MBI_FLAG_CMDLINE | MBI_FLAG_MODS );
  373. mb_cmdline_offset = 0;
  374. mbinfo.cmdline = multiboot_add_cmdline ( image );
  375. mbinfo.mods_addr = virt_to_phys ( mbmodules );
  376. mbinfo.mmap_addr = virt_to_phys ( mbmemmap );
  377. snprintf ( mb_bootloader_name, sizeof ( mb_bootloader_name ),
  378. "iPXE %s", product_version );
  379. mbinfo.boot_loader_name = virt_to_phys ( mb_bootloader_name );
  380. if ( ( rc = multiboot_add_modules ( image, max, &mbinfo, mbmodules,
  381. ( sizeof ( mbmodules ) /
  382. sizeof ( mbmodules[0] ) ) ) ) !=0)
  383. return rc;
  384. /* Multiboot images may not return and have no callback
  385. * interface, so shut everything down prior to booting the OS.
  386. */
  387. shutdown_boot();
  388. /* Build memory map after unhiding bootloader memory regions as part of
  389. * shutting everything down.
  390. */
  391. multiboot_build_memmap ( image, &mbinfo, mbmemmap,
  392. ( sizeof(mbmemmap) / sizeof(mbmemmap[0]) ) );
  393. /* Jump to OS with flat physical addressing */
  394. DBGC ( image, "MULTIBOOT %p starting execution at %lx\n",
  395. image, entry );
  396. __asm__ __volatile__ ( PHYS_CODE ( "pushl %%ebp\n\t"
  397. "call *%%edi\n\t"
  398. "popl %%ebp\n\t" )
  399. : : "a" ( MULTIBOOT_BOOTLOADER_MAGIC ),
  400. "b" ( virt_to_phys ( &mbinfo ) ),
  401. "D" ( entry )
  402. : "ecx", "edx", "esi", "memory" );
  403. DBGC ( image, "MULTIBOOT %p returned\n", image );
  404. /* It isn't safe to continue after calling shutdown() */
  405. while ( 1 ) {}
  406. return -ECANCELED; /* -EIMPOSSIBLE, anyone? */
  407. }
  408. /**
  409. * Probe multiboot image
  410. *
  411. * @v image Multiboot file
  412. * @ret rc Return status code
  413. */
  414. static int multiboot_probe ( struct image *image ) {
  415. struct multiboot_header_info hdr;
  416. int rc;
  417. /* Locate multiboot header, if present */
  418. if ( ( rc = multiboot_find_header ( image, &hdr ) ) != 0 ) {
  419. DBGC ( image, "MULTIBOOT %p has no multiboot header\n",
  420. image );
  421. return rc;
  422. }
  423. DBGC ( image, "MULTIBOOT %p found header with flags %08x\n",
  424. image, hdr.mb.flags );
  425. return 0;
  426. }
  427. /** Multiboot image type */
  428. struct image_type multiboot_image_type __image_type ( PROBE_MULTIBOOT ) = {
  429. .name = "Multiboot",
  430. .probe = multiboot_probe,
  431. .exec = multiboot_exec,
  432. };