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

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