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

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