Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

multiboot.c 13KB

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