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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 <gpxe/uaccess.h>
  31. #include <gpxe/image.h>
  32. #include <gpxe/segment.h>
  33. #include <gpxe/memmap.h>
  34. #include <gpxe/elf.h>
  35. #include <gpxe/init.h>
  36. #include <gpxe/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 cmdline Command line
  127. * @ret physaddr Physical address of command line
  128. */
  129. physaddr_t multiboot_add_cmdline ( const char *cmdline ) {
  130. char *mb_cmdline;
  131. if ( ! cmdline )
  132. cmdline = "";
  133. /* Copy command line to base memory buffer */
  134. mb_cmdline = ( mb_cmdlines + mb_cmdline_offset );
  135. mb_cmdline_offset +=
  136. ( snprintf ( mb_cmdline,
  137. ( sizeof ( mb_cmdlines ) - mb_cmdline_offset ),
  138. "%s", cmdline ) + 1 );
  139. /* Truncate to terminating NUL in buffer if necessary */
  140. if ( mb_cmdline_offset > sizeof ( mb_cmdlines ) )
  141. mb_cmdline_offset = ( sizeof ( mb_cmdlines ) - 1 );
  142. return virt_to_phys ( mb_cmdline );
  143. }
  144. /**
  145. * Build multiboot module list
  146. *
  147. * @v image Multiboot image
  148. * @v modules Module list to fill, or NULL
  149. * @ret count Number of modules
  150. */
  151. static unsigned int
  152. multiboot_build_module_list ( struct image *image,
  153. struct multiboot_module *modules,
  154. unsigned int limit ) {
  155. struct image *module_image;
  156. struct multiboot_module *module;
  157. unsigned int count = 0;
  158. unsigned int insert;
  159. physaddr_t start;
  160. physaddr_t end;
  161. unsigned int i;
  162. /* Add each image as a multiboot module */
  163. for_each_image ( module_image ) {
  164. if ( count >= limit ) {
  165. DBGC ( image, "MULTIBOOT %p limit of %d modules "
  166. "reached\n", image, limit );
  167. break;
  168. }
  169. /* Do not include kernel image itself as a module */
  170. if ( module_image == image )
  171. continue;
  172. /* At least some OSes expect the multiboot modules to
  173. * be in ascending order, so we have to support it.
  174. */
  175. start = user_to_phys ( module_image->data, 0 );
  176. end = user_to_phys ( module_image->data, module_image->len );
  177. for ( insert = 0 ; insert < count ; insert++ ) {
  178. if ( start < modules[insert].mod_start )
  179. break;
  180. }
  181. module = &modules[insert];
  182. memmove ( ( module + 1 ), module,
  183. ( ( count - insert ) * sizeof ( *module ) ) );
  184. module->mod_start = start;
  185. module->mod_end = end;
  186. module->string =
  187. multiboot_add_cmdline ( module_image->cmdline );
  188. module->reserved = 0;
  189. /* We promise to page-align modules */
  190. assert ( ( module->mod_start & 0xfff ) == 0 );
  191. count++;
  192. }
  193. /* Dump module configuration */
  194. for ( i = 0 ; i < count ; i++ ) {
  195. DBGC ( image, "MULTIBOOT %p module %d is [%x,%x)\n",
  196. image, i, modules[i].mod_start,
  197. modules[i].mod_end );
  198. }
  199. return count;
  200. }
  201. /**
  202. * The multiboot information structure
  203. *
  204. * Kept in base memory because some OSes won't find it elsewhere,
  205. * along with the other structures belonging to the Multiboot
  206. * information table.
  207. */
  208. static struct multiboot_info __bss16 ( mbinfo );
  209. #define mbinfo __use_data16 ( mbinfo )
  210. /** The multiboot bootloader name */
  211. static char __data16_array ( mb_bootloader_name, [] ) = "gPXE " VERSION;
  212. #define mb_bootloader_name __use_data16 ( mb_bootloader_name )
  213. /** The multiboot memory map */
  214. static struct multiboot_memory_map
  215. __bss16_array ( mbmemmap, [MAX_MEMORY_REGIONS] );
  216. #define mbmemmap __use_data16 ( mbmemmap )
  217. /** The multiboot module list */
  218. static struct multiboot_module __bss16_array ( mbmodules, [MAX_MODULES] );
  219. #define mbmodules __use_data16 ( mbmodules )
  220. /**
  221. * Execute multiboot image
  222. *
  223. * @v image Multiboot image
  224. * @ret rc Return status code
  225. */
  226. static int multiboot_exec ( struct image *image ) {
  227. physaddr_t entry = image->priv.phys;
  228. /* Populate multiboot information structure */
  229. memset ( &mbinfo, 0, sizeof ( mbinfo ) );
  230. mbinfo.flags = ( MBI_FLAG_LOADER | MBI_FLAG_MEM | MBI_FLAG_MMAP |
  231. MBI_FLAG_CMDLINE | MBI_FLAG_MODS );
  232. multiboot_build_memmap ( image, &mbinfo, mbmemmap,
  233. ( sizeof(mbmemmap) / sizeof(mbmemmap[0]) ) );
  234. mb_cmdline_offset = 0;
  235. mbinfo.cmdline = multiboot_add_cmdline ( image->cmdline );
  236. mbinfo.mods_count = multiboot_build_module_list ( image, mbmodules,
  237. ( sizeof(mbmodules) / sizeof(mbmodules[0]) ) );
  238. mbinfo.mods_addr = virt_to_phys ( mbmodules );
  239. mbinfo.mmap_addr = virt_to_phys ( mbmemmap );
  240. mbinfo.boot_loader_name = virt_to_phys ( mb_bootloader_name );
  241. /* Multiboot images may not return and have no callback
  242. * interface, so shut everything down prior to booting the OS.
  243. */
  244. shutdown ( SHUTDOWN_BOOT );
  245. /* Jump to OS with flat physical addressing */
  246. DBGC ( image, "MULTIBOOT %p starting execution at %lx\n",
  247. image, entry );
  248. __asm__ __volatile__ ( PHYS_CODE ( "pushl %%ebp\n\t"
  249. "call *%%edi\n\t"
  250. "popl %%ebp\n\t" )
  251. : : "a" ( MULTIBOOT_BOOTLOADER_MAGIC ),
  252. "b" ( virt_to_phys ( &mbinfo ) ),
  253. "D" ( entry )
  254. : "ecx", "edx", "esi", "memory" );
  255. DBGC ( image, "MULTIBOOT %p returned\n", image );
  256. /* It isn't safe to continue after calling shutdown() */
  257. while ( 1 ) {}
  258. return -ECANCELED; /* -EIMPOSSIBLE, anyone? */
  259. }
  260. /**
  261. * Find multiboot header
  262. *
  263. * @v image Multiboot file
  264. * @v hdr Multiboot header descriptor to fill in
  265. * @ret rc Return status code
  266. */
  267. static int multiboot_find_header ( struct image *image,
  268. struct multiboot_header_info *hdr ) {
  269. uint32_t buf[64];
  270. size_t offset;
  271. unsigned int buf_idx;
  272. uint32_t checksum;
  273. /* Scan through first 8kB of image file 256 bytes at a time.
  274. * (Use the buffering to avoid the overhead of a
  275. * copy_from_user() for every dword.)
  276. */
  277. for ( offset = 0 ; offset < 8192 ; offset += sizeof ( buf[0] ) ) {
  278. /* Check for end of image */
  279. if ( offset > image->len )
  280. break;
  281. /* Refill buffer if applicable */
  282. buf_idx = ( ( offset % sizeof ( buf ) ) / sizeof ( buf[0] ) );
  283. if ( buf_idx == 0 ) {
  284. copy_from_user ( buf, image->data, offset,
  285. sizeof ( buf ) );
  286. }
  287. /* Check signature */
  288. if ( buf[buf_idx] != MULTIBOOT_HEADER_MAGIC )
  289. continue;
  290. /* Copy header and verify checksum */
  291. copy_from_user ( &hdr->mb, image->data, offset,
  292. sizeof ( hdr->mb ) );
  293. checksum = ( hdr->mb.magic + hdr->mb.flags +
  294. hdr->mb.checksum );
  295. if ( checksum != 0 )
  296. continue;
  297. /* Record offset of multiboot header and return */
  298. hdr->offset = offset;
  299. return 0;
  300. }
  301. /* No multiboot header found */
  302. return -ENOEXEC;
  303. }
  304. /**
  305. * Load raw multiboot image into memory
  306. *
  307. * @v image Multiboot file
  308. * @v hdr Multiboot header descriptor
  309. * @ret rc Return status code
  310. */
  311. static int multiboot_load_raw ( struct image *image,
  312. struct multiboot_header_info *hdr ) {
  313. size_t offset;
  314. size_t filesz;
  315. size_t memsz;
  316. userptr_t buffer;
  317. int rc;
  318. /* Sanity check */
  319. if ( ! ( hdr->mb.flags & MB_FLAG_RAW ) ) {
  320. DBGC ( image, "MULTIBOOT %p is not flagged as a raw image\n",
  321. image );
  322. return -EINVAL;
  323. }
  324. /* Verify and prepare segment */
  325. offset = ( hdr->offset - hdr->mb.header_addr + hdr->mb.load_addr );
  326. filesz = ( hdr->mb.load_end_addr ?
  327. ( hdr->mb.load_end_addr - hdr->mb.load_addr ) :
  328. ( image->len - offset ) );
  329. memsz = ( hdr->mb.bss_end_addr ?
  330. ( hdr->mb.bss_end_addr - hdr->mb.load_addr ) : filesz );
  331. buffer = phys_to_user ( hdr->mb.load_addr );
  332. if ( ( rc = prep_segment ( buffer, filesz, memsz ) ) != 0 ) {
  333. DBGC ( image, "MULTIBOOT %p could not prepare segment: %s\n",
  334. image, strerror ( rc ) );
  335. return rc;
  336. }
  337. /* Copy image to segment */
  338. memcpy_user ( buffer, 0, image->data, offset, filesz );
  339. /* Record execution entry point in image private data field */
  340. image->priv.phys = hdr->mb.entry_addr;
  341. return 0;
  342. }
  343. /**
  344. * Load ELF multiboot image into memory
  345. *
  346. * @v image Multiboot file
  347. * @ret rc Return status code
  348. */
  349. static int multiboot_load_elf ( struct image *image ) {
  350. int rc;
  351. /* Load ELF image*/
  352. if ( ( rc = elf_load ( image ) ) != 0 ) {
  353. DBGC ( image, "MULTIBOOT %p ELF image failed to load: %s\n",
  354. image, strerror ( rc ) );
  355. return rc;
  356. }
  357. return 0;
  358. }
  359. /**
  360. * Load multiboot image into memory
  361. *
  362. * @v image Multiboot file
  363. * @ret rc Return status code
  364. */
  365. static int multiboot_load ( struct image *image ) {
  366. struct multiboot_header_info hdr;
  367. int rc;
  368. /* Locate multiboot header, if present */
  369. if ( ( rc = multiboot_find_header ( image, &hdr ) ) != 0 ) {
  370. DBGC ( image, "MULTIBOOT %p has no multiboot header\n",
  371. image );
  372. return rc;
  373. }
  374. DBGC ( image, "MULTIBOOT %p found header with flags %08x\n",
  375. image, hdr.mb.flags );
  376. /* This is a multiboot image, valid or otherwise */
  377. if ( ! image->type )
  378. image->type = &multiboot_image_type;
  379. /* Abort if we detect flags that we cannot support */
  380. if ( hdr.mb.flags & MB_UNSUPPORTED_FLAGS ) {
  381. DBGC ( image, "MULTIBOOT %p flags %08x not supported\n",
  382. image, ( hdr.mb.flags & MB_UNSUPPORTED_FLAGS ) );
  383. return -ENOTSUP;
  384. }
  385. /* There is technically a bit MB_FLAG_RAW to indicate whether
  386. * this is an ELF or a raw image. In practice, grub will use
  387. * the ELF header if present, and Solaris relies on this
  388. * behaviour.
  389. */
  390. if ( ( ( rc = multiboot_load_elf ( image ) ) != 0 ) &&
  391. ( ( rc = multiboot_load_raw ( image, &hdr ) ) != 0 ) )
  392. return rc;
  393. return 0;
  394. }
  395. /** Multiboot image type */
  396. struct image_type multiboot_image_type __image_type ( PROBE_MULTIBOOT ) = {
  397. .name = "Multiboot",
  398. .load = multiboot_load,
  399. .exec = multiboot_exec,
  400. };