2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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.
22 * Multiboot image format
30 #include <multiboot.h>
31 #include <gpxe/uaccess.h>
32 #include <gpxe/image.h>
33 #include <gpxe/segment.h>
34 #include <gpxe/memmap.h>
36 #include <gpxe/init.h>
37 #include <gpxe/features.h>
39 FEATURE ( FEATURE_IMAGE, "Multiboot", DHCP_EB_FEATURE_MULTIBOOT, 1 );
41 struct image_type multiboot_image_type __image_type ( PROBE_MULTIBOOT );
44 * Maximum number of modules we will allow for
46 * If this has bitten you: sorry. I did have a perfect scheme with a
47 * dynamically allocated list of modules on the protected-mode stack,
48 * but it was incompatible with some broken OSes that can only access
49 * low memory at boot time (even though we kindly set up 4GB flat
50 * physical addressing as per the multiboot specification.
56 * Maximum combined length of command lines
58 * Again; sorry. Some broken OSes zero out any non-base memory that
59 * isn't part of the loaded module set, so we can't just use
60 * virt_to_phys(cmdline) to point to the command lines, even though
61 * this would comply with the Multiboot spec.
63 #define MB_MAX_CMDLINE 512
65 /** Multiboot flags that we support */
66 #define MB_SUPPORTED_FLAGS ( MB_FLAG_PGALIGN | MB_FLAG_MEMMAP | \
67 MB_FLAG_VIDMODE | MB_FLAG_RAW )
69 /** Compulsory feature multiboot flags */
70 #define MB_COMPULSORY_FLAGS 0x0000ffff
72 /** Optional feature multiboot flags */
73 #define MB_OPTIONAL_FLAGS 0xffff0000
76 * Multiboot flags that we don't support
78 * We only care about the compulsory feature flags (bits 0-15); we are
79 * allowed to ignore the optional feature flags.
81 #define MB_UNSUPPORTED_FLAGS ( MB_COMPULSORY_FLAGS & ~MB_SUPPORTED_FLAGS )
83 /** A multiboot header descriptor */
84 struct multiboot_header_info {
85 /** The actual multiboot header */
86 struct multiboot_header mb;
87 /** Offset of header within the multiboot image */
91 /** Multiboot module command lines */
92 static char __bss16_array ( mb_cmdlines, [MB_MAX_CMDLINE] );
93 #define mb_cmdlines __use_data16 ( mb_cmdlines )
95 /** Offset within module command lines */
96 static unsigned int mb_cmdline_offset;
99 * Build multiboot memory map
101 * @v image Multiboot image
102 * @v mbinfo Multiboot information structure
103 * @v mbmemmap Multiboot memory map
104 * @v limit Maxmimum number of memory map entries
106 static void multiboot_build_memmap ( struct image *image,
107 struct multiboot_info *mbinfo,
108 struct multiboot_memory_map *mbmemmap,
109 unsigned int limit ) {
110 struct memory_map memmap;
114 get_memmap ( &memmap );
116 /* Translate into multiboot format */
117 memset ( mbmemmap, 0, sizeof ( *mbmemmap ) );
118 for ( i = 0 ; i < memmap.count ; i++ ) {
120 DBGC ( image, "MULTIBOOT %p limit of %d memmap "
121 "entries reached\n", image, limit );
124 mbmemmap[i].size = ( sizeof ( mbmemmap[i] ) -
125 sizeof ( mbmemmap[i].size ) );
126 mbmemmap[i].base_addr = memmap.regions[i].start;
127 mbmemmap[i].length = ( memmap.regions[i].end -
128 memmap.regions[i].start );
129 mbmemmap[i].type = MBMEM_RAM;
130 mbinfo->mmap_length += sizeof ( mbmemmap[i] );
131 if ( memmap.regions[i].start == 0 )
132 mbinfo->mem_lower = ( memmap.regions[i].end / 1024 );
133 if ( memmap.regions[i].start == 0x100000 )
134 mbinfo->mem_upper = ( ( memmap.regions[i].end -
140 * Add command line in base memory
142 * @v cmdline Command line
143 * @ret physaddr Physical address of command line
145 physaddr_t multiboot_add_cmdline ( const char *cmdline ) {
151 /* Copy command line to base memory buffer */
152 mb_cmdline = ( mb_cmdlines + mb_cmdline_offset );
154 ( snprintf ( mb_cmdline,
155 ( sizeof ( mb_cmdlines ) - mb_cmdline_offset ),
156 "%s", cmdline ) + 1 );
158 /* Truncate to terminating NUL in buffer if necessary */
159 if ( mb_cmdline_offset > sizeof ( mb_cmdlines ) )
160 mb_cmdline_offset = ( sizeof ( mb_cmdlines ) - 1 );
162 return virt_to_phys ( mb_cmdline );
166 * Build multiboot module list
168 * @v image Multiboot image
169 * @v modules Module list to fill, or NULL
170 * @ret count Number of modules
173 multiboot_build_module_list ( struct image *image,
174 struct multiboot_module *modules,
175 unsigned int limit ) {
176 struct image *module_image;
177 struct multiboot_module *module;
178 unsigned int count = 0;
184 /* Add each image as a multiboot module */
185 for_each_image ( module_image ) {
187 if ( count >= limit ) {
188 DBGC ( image, "MULTIBOOT %p limit of %d modules "
189 "reached\n", image, limit );
193 /* Do not include kernel image itself as a module */
194 if ( module_image == image )
197 /* At least some OSes expect the multiboot modules to
198 * be in ascending order, so we have to support it.
200 start = user_to_phys ( module_image->data, 0 );
201 end = user_to_phys ( module_image->data, module_image->len );
202 for ( insert = 0 ; insert < count ; insert++ ) {
203 if ( start < modules[insert].mod_start )
206 module = &modules[insert];
207 memmove ( ( module + 1 ), module,
208 ( ( count - insert ) * sizeof ( *module ) ) );
209 module->mod_start = start;
210 module->mod_end = end;
212 multiboot_add_cmdline ( module_image->cmdline );
213 module->reserved = 0;
215 /* We promise to page-align modules */
216 assert ( ( module->mod_start & 0xfff ) == 0 );
221 /* Dump module configuration */
222 for ( i = 0 ; i < count ; i++ ) {
223 DBGC ( image, "MULTIBOOT %p module %d is [%lx,%lx)\n",
224 image, i, modules[i].mod_start,
225 modules[i].mod_end );
232 * The multiboot information structure
234 * Kept in base memory because some OSes won't find it elsewhere,
235 * along with the other structures belonging to the Multiboot
238 static struct multiboot_info __bss16 ( mbinfo );
239 #define mbinfo __use_data16 ( mbinfo )
241 /** The multiboot bootloader name */
242 static char __data16_array ( mb_bootloader_name, [] ) = "gPXE " VERSION;
243 #define mb_bootloader_name __use_data16 ( mb_bootloader_name )
245 /** The multiboot memory map */
246 static struct multiboot_memory_map
247 __bss16_array ( mbmemmap, [MAX_MEMORY_REGIONS] );
248 #define mbmemmap __use_data16 ( mbmemmap )
250 /** The multiboot module list */
251 static struct multiboot_module __bss16_array ( mbmodules, [MAX_MODULES] );
252 #define mbmodules __use_data16 ( mbmodules )
255 * Execute multiboot image
257 * @v image Multiboot image
258 * @ret rc Return status code
260 static int multiboot_exec ( struct image *image ) {
261 physaddr_t entry = image->priv.phys;
263 /* Populate multiboot information structure */
264 memset ( &mbinfo, 0, sizeof ( mbinfo ) );
265 mbinfo.flags = ( MBI_FLAG_LOADER | MBI_FLAG_MEM | MBI_FLAG_MMAP |
266 MBI_FLAG_CMDLINE | MBI_FLAG_MODS );
267 multiboot_build_memmap ( image, &mbinfo, mbmemmap,
268 ( sizeof(mbmemmap) / sizeof(mbmemmap[0]) ) );
269 mb_cmdline_offset = 0;
270 mbinfo.cmdline = multiboot_add_cmdline ( image->cmdline );
271 mbinfo.mods_count = multiboot_build_module_list ( image, mbmodules,
272 ( sizeof(mbmodules) / sizeof(mbmodules[0]) ) );
273 mbinfo.mods_addr = virt_to_phys ( mbmodules );
274 mbinfo.mmap_addr = virt_to_phys ( mbmemmap );
275 mbinfo.boot_loader_name = virt_to_phys ( mb_bootloader_name );
277 /* Multiboot images may not return and have no callback
278 * interface, so shut everything down prior to booting the OS.
282 /* Jump to OS with flat physical addressing */
283 __asm__ __volatile__ ( PHYS_CODE ( "call *%%edi\n\t" )
284 : : "a" ( MULTIBOOT_BOOTLOADER_MAGIC ),
285 "b" ( virt_to_phys ( &mbinfo ) ),
287 : "ecx", "edx", "esi", "ebp", "memory" );
289 DBGC ( image, "MULTIBOOT %p returned\n", image );
291 /* It isn't safe to continue after calling shutdown() */
294 return -ECANCELED; /* -EIMPOSSIBLE, anyone? */
298 * Find multiboot header
300 * @v image Multiboot file
301 * @v hdr Multiboot header descriptor to fill in
302 * @ret rc Return status code
304 static int multiboot_find_header ( struct image *image,
305 struct multiboot_header_info *hdr ) {
308 unsigned int buf_idx;
311 /* Scan through first 8kB of image file 256 bytes at a time.
312 * (Use the buffering to avoid the overhead of a
313 * copy_from_user() for every dword.)
315 for ( offset = 0 ; offset < 8192 ; offset += sizeof ( buf[0] ) ) {
316 /* Check for end of image */
317 if ( offset > image->len )
319 /* Refill buffer if applicable */
320 buf_idx = ( ( offset % sizeof ( buf ) ) / sizeof ( buf[0] ) );
321 if ( buf_idx == 0 ) {
322 copy_from_user ( buf, image->data, offset,
325 /* Check signature */
326 if ( buf[buf_idx] != MULTIBOOT_HEADER_MAGIC )
328 /* Copy header and verify checksum */
329 copy_from_user ( &hdr->mb, image->data, offset,
330 sizeof ( hdr->mb ) );
331 checksum = ( hdr->mb.magic + hdr->mb.flags +
335 /* Record offset of multiboot header and return */
336 hdr->offset = offset;
340 /* No multiboot header found */
345 * Load raw multiboot image into memory
347 * @v image Multiboot file
348 * @v hdr Multiboot header descriptor
349 * @ret rc Return status code
351 static int multiboot_load_raw ( struct image *image,
352 struct multiboot_header_info *hdr ) {
359 /* Verify and prepare segment */
360 offset = ( hdr->offset - hdr->mb.header_addr + hdr->mb.load_addr );
361 filesz = ( hdr->mb.load_end_addr - hdr->mb.load_addr );
362 memsz = ( hdr->mb.bss_end_addr - hdr->mb.load_addr );
363 buffer = phys_to_user ( hdr->mb.load_addr );
364 if ( ( rc = prep_segment ( buffer, filesz, memsz ) ) != 0 ) {
365 DBGC ( image, "MULTIBOOT %p could not prepare segment: %s\n",
366 image, strerror ( rc ) );
370 /* Copy image to segment */
371 memcpy_user ( buffer, 0, image->data, offset, filesz );
373 /* Record execution entry point in image private data field */
374 image->priv.phys = hdr->mb.entry_addr;
380 * Load ELF multiboot image into memory
382 * @v image Multiboot file
383 * @ret rc Return status code
385 static int multiboot_load_elf ( struct image *image ) {
389 if ( ( rc = elf_load ( image ) ) != 0 ) {
390 DBGC ( image, "MULTIBOOT %p ELF image failed to load: %s\n",
391 image, strerror ( rc ) );
399 * Load multiboot image into memory
401 * @v image Multiboot file
402 * @ret rc Return status code
404 static int multiboot_load ( struct image *image ) {
405 struct multiboot_header_info hdr;
408 /* Locate multiboot header, if present */
409 if ( ( rc = multiboot_find_header ( image, &hdr ) ) != 0 ) {
410 DBGC ( image, "MULTIBOOT %p has no multiboot header\n",
414 DBGC ( image, "MULTIBOOT %p found header with flags %08lx\n",
415 image, hdr.mb.flags );
417 /* This is a multiboot image, valid or otherwise */
419 image->type = &multiboot_image_type;
421 /* Abort if we detect flags that we cannot support */
422 if ( hdr.mb.flags & MB_UNSUPPORTED_FLAGS ) {
423 DBGC ( image, "MULTIBOOT %p flags %08lx not supported\n",
424 image, ( hdr.mb.flags & MB_UNSUPPORTED_FLAGS ) );
428 /* Load the actual image */
429 if ( hdr.mb.flags & MB_FLAG_RAW ) {
430 if ( ( rc = multiboot_load_raw ( image, &hdr ) ) != 0 )
433 if ( ( rc = multiboot_load_elf ( image ) ) != 0 )
440 /** Multiboot image type */
441 struct image_type multiboot_image_type __image_type ( PROBE_MULTIBOOT ) = {
443 .load = multiboot_load,
444 .exec = multiboot_exec,