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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 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, [] ) = "gPXE " 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. multiboot_build_memmap ( image, &mbinfo, mbmemmap,
  234. ( sizeof(mbmemmap) / sizeof(mbmemmap[0]) ) );
  235. mb_cmdline_offset = 0;
  236. mbinfo.cmdline = multiboot_add_cmdline ( image->name, image->cmdline );
  237. mbinfo.mods_count = multiboot_build_module_list ( image, mbmodules,
  238. ( sizeof(mbmodules) / sizeof(mbmodules[0]) ) );
  239. mbinfo.mods_addr = virt_to_phys ( mbmodules );
  240. mbinfo.mmap_addr = virt_to_phys ( mbmemmap );
  241. mbinfo.boot_loader_name = virt_to_phys ( mb_bootloader_name );
  242. /* Multiboot images may not return and have no callback
  243. * interface, so shut everything down prior to booting the OS.
  244. */
  245. shutdown ( SHUTDOWN_BOOT );
  246. /* Jump to OS with flat physical addressing */
  247. DBGC ( image, "MULTIBOOT %p starting execution at %lx\n",
  248. image, entry );
  249. __asm__ __volatile__ ( PHYS_CODE ( "pushl %%ebp\n\t"
  250. "call *%%edi\n\t"
  251. "popl %%ebp\n\t" )
  252. : : "a" ( MULTIBOOT_BOOTLOADER_MAGIC ),
  253. "b" ( virt_to_phys ( &mbinfo ) ),
  254. "D" ( entry )
  255. : "ecx", "edx", "esi", "memory" );
  256. DBGC ( image, "MULTIBOOT %p returned\n", image );
  257. /* It isn't safe to continue after calling shutdown() */
  258. while ( 1 ) {}
  259. return -ECANCELED; /* -EIMPOSSIBLE, anyone? */
  260. }
  261. /**
  262. * Find multiboot header
  263. *
  264. * @v image Multiboot file
  265. * @v hdr Multiboot header descriptor to fill in
  266. * @ret rc Return status code
  267. */
  268. static int multiboot_find_header ( struct image *image,
  269. struct multiboot_header_info *hdr ) {
  270. uint32_t buf[64];
  271. size_t offset;
  272. unsigned int buf_idx;
  273. uint32_t checksum;
  274. /* Scan through first 8kB of image file 256 bytes at a time.
  275. * (Use the buffering to avoid the overhead of a
  276. * copy_from_user() for every dword.)
  277. */
  278. for ( offset = 0 ; offset < 8192 ; offset += sizeof ( buf[0] ) ) {
  279. /* Check for end of image */
  280. if ( offset > image->len )
  281. break;
  282. /* Refill buffer if applicable */
  283. buf_idx = ( ( offset % sizeof ( buf ) ) / sizeof ( buf[0] ) );
  284. if ( buf_idx == 0 ) {
  285. copy_from_user ( buf, image->data, offset,
  286. sizeof ( buf ) );
  287. }
  288. /* Check signature */
  289. if ( buf[buf_idx] != MULTIBOOT_HEADER_MAGIC )
  290. continue;
  291. /* Copy header and verify checksum */
  292. copy_from_user ( &hdr->mb, image->data, offset,
  293. sizeof ( hdr->mb ) );
  294. checksum = ( hdr->mb.magic + hdr->mb.flags +
  295. hdr->mb.checksum );
  296. if ( checksum != 0 )
  297. continue;
  298. /* Record offset of multiboot header and return */
  299. hdr->offset = offset;
  300. return 0;
  301. }
  302. /* No multiboot header found */
  303. return -ENOEXEC;
  304. }
  305. /**
  306. * Load raw multiboot image into memory
  307. *
  308. * @v image Multiboot file
  309. * @v hdr Multiboot header descriptor
  310. * @ret rc Return status code
  311. */
  312. static int multiboot_load_raw ( struct image *image,
  313. struct multiboot_header_info *hdr ) {
  314. size_t offset;
  315. size_t filesz;
  316. size_t memsz;
  317. userptr_t buffer;
  318. int rc;
  319. /* Sanity check */
  320. if ( ! ( hdr->mb.flags & MB_FLAG_RAW ) ) {
  321. DBGC ( image, "MULTIBOOT %p is not flagged as a raw image\n",
  322. image );
  323. return -EINVAL;
  324. }
  325. /* Verify and prepare segment */
  326. offset = ( hdr->offset - hdr->mb.header_addr + hdr->mb.load_addr );
  327. filesz = ( hdr->mb.load_end_addr ?
  328. ( hdr->mb.load_end_addr - hdr->mb.load_addr ) :
  329. ( image->len - offset ) );
  330. memsz = ( hdr->mb.bss_end_addr ?
  331. ( hdr->mb.bss_end_addr - hdr->mb.load_addr ) : filesz );
  332. buffer = phys_to_user ( hdr->mb.load_addr );
  333. if ( ( rc = prep_segment ( buffer, filesz, memsz ) ) != 0 ) {
  334. DBGC ( image, "MULTIBOOT %p could not prepare segment: %s\n",
  335. image, strerror ( rc ) );
  336. return rc;
  337. }
  338. /* Copy image to segment */
  339. memcpy_user ( buffer, 0, image->data, offset, filesz );
  340. /* Record execution entry point in image private data field */
  341. image->priv.phys = hdr->mb.entry_addr;
  342. return 0;
  343. }
  344. /**
  345. * Load ELF multiboot image into memory
  346. *
  347. * @v image Multiboot file
  348. * @ret rc Return status code
  349. */
  350. static int multiboot_load_elf ( struct image *image ) {
  351. int rc;
  352. /* Load ELF image*/
  353. if ( ( rc = elf_load ( image ) ) != 0 ) {
  354. DBGC ( image, "MULTIBOOT %p ELF image failed to load: %s\n",
  355. image, strerror ( rc ) );
  356. return rc;
  357. }
  358. return 0;
  359. }
  360. /**
  361. * Load multiboot image into memory
  362. *
  363. * @v image Multiboot file
  364. * @ret rc Return status code
  365. */
  366. static int multiboot_load ( struct image *image ) {
  367. struct multiboot_header_info hdr;
  368. int rc;
  369. /* Locate multiboot header, if present */
  370. if ( ( rc = multiboot_find_header ( image, &hdr ) ) != 0 ) {
  371. DBGC ( image, "MULTIBOOT %p has no multiboot header\n",
  372. image );
  373. return rc;
  374. }
  375. DBGC ( image, "MULTIBOOT %p found header with flags %08x\n",
  376. image, hdr.mb.flags );
  377. /* This is a multiboot image, valid or otherwise */
  378. if ( ! image->type )
  379. image->type = &multiboot_image_type;
  380. /* Abort if we detect flags that we cannot support */
  381. if ( hdr.mb.flags & MB_UNSUPPORTED_FLAGS ) {
  382. DBGC ( image, "MULTIBOOT %p flags %08x not supported\n",
  383. image, ( hdr.mb.flags & MB_UNSUPPORTED_FLAGS ) );
  384. return -ENOTSUP;
  385. }
  386. /* There is technically a bit MB_FLAG_RAW to indicate whether
  387. * this is an ELF or a raw image. In practice, grub will use
  388. * the ELF header if present, and Solaris relies on this
  389. * behaviour.
  390. */
  391. if ( ( ( rc = multiboot_load_elf ( image ) ) != 0 ) &&
  392. ( ( rc = multiboot_load_raw ( image, &hdr ) ) != 0 ) )
  393. return rc;
  394. return 0;
  395. }
  396. /** Multiboot image type */
  397. struct image_type multiboot_image_type __image_type ( PROBE_MULTIBOOT ) = {
  398. .name = "Multiboot",
  399. .load = multiboot_load,
  400. .exec = multiboot_exec,
  401. };