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.
19 FILE_LICENCE ( GPL2_OR_LATER );
24 * Multiboot image format
32 #include <multiboot.h>
33 #include <gpxe/uaccess.h>
34 #include <gpxe/image.h>
35 #include <gpxe/segment.h>
36 #include <gpxe/memmap.h>
38 #include <gpxe/init.h>
39 #include <gpxe/features.h>
41 FEATURE ( FEATURE_IMAGE, "Multiboot", DHCP_EB_FEATURE_MULTIBOOT, 1 );
43 struct image_type multiboot_image_type __image_type ( PROBE_MULTIBOOT );
46 * Maximum number of modules we will allow for
48 * If this has bitten you: sorry. I did have a perfect scheme with a
49 * dynamically allocated list of modules on the protected-mode stack,
50 * but it was incompatible with some broken OSes that can only access
51 * low memory at boot time (even though we kindly set up 4GB flat
52 * physical addressing as per the multiboot specification.
58 * Maximum combined length of command lines
60 * Again; sorry. Some broken OSes zero out any non-base memory that
61 * isn't part of the loaded module set, so we can't just use
62 * virt_to_phys(cmdline) to point to the command lines, even though
63 * this would comply with the Multiboot spec.
65 #define MB_MAX_CMDLINE 512
67 /** Multiboot flags that we support */
68 #define MB_SUPPORTED_FLAGS ( MB_FLAG_PGALIGN | MB_FLAG_MEMMAP | \
69 MB_FLAG_VIDMODE | MB_FLAG_RAW )
71 /** Compulsory feature multiboot flags */
72 #define MB_COMPULSORY_FLAGS 0x0000ffff
74 /** Optional feature multiboot flags */
75 #define MB_OPTIONAL_FLAGS 0xffff0000
78 * Multiboot flags that we don't support
80 * We only care about the compulsory feature flags (bits 0-15); we are
81 * allowed to ignore the optional feature flags.
83 #define MB_UNSUPPORTED_FLAGS ( MB_COMPULSORY_FLAGS & ~MB_SUPPORTED_FLAGS )
85 /** A multiboot header descriptor */
86 struct multiboot_header_info {
87 /** The actual multiboot header */
88 struct multiboot_header mb;
89 /** Offset of header within the multiboot image */
93 /** Multiboot module command lines */
94 static char __bss16_array ( mb_cmdlines, [MB_MAX_CMDLINE] );
95 #define mb_cmdlines __use_data16 ( mb_cmdlines )
97 /** Offset within module command lines */
98 static unsigned int mb_cmdline_offset;
101 * Build multiboot memory map
103 * @v image Multiboot image
104 * @v mbinfo Multiboot information structure
105 * @v mbmemmap Multiboot memory map
106 * @v limit Maxmimum number of memory map entries
108 static void multiboot_build_memmap ( struct image *image,
109 struct multiboot_info *mbinfo,
110 struct multiboot_memory_map *mbmemmap,
111 unsigned int limit ) {
112 struct memory_map memmap;
116 get_memmap ( &memmap );
118 /* Translate into multiboot format */
119 memset ( mbmemmap, 0, sizeof ( *mbmemmap ) );
120 for ( i = 0 ; i < memmap.count ; i++ ) {
122 DBGC ( image, "MULTIBOOT %p limit of %d memmap "
123 "entries reached\n", image, limit );
126 mbmemmap[i].size = ( sizeof ( mbmemmap[i] ) -
127 sizeof ( mbmemmap[i].size ) );
128 mbmemmap[i].base_addr = memmap.regions[i].start;
129 mbmemmap[i].length = ( memmap.regions[i].end -
130 memmap.regions[i].start );
131 mbmemmap[i].type = MBMEM_RAM;
132 mbinfo->mmap_length += sizeof ( mbmemmap[i] );
133 if ( memmap.regions[i].start == 0 )
134 mbinfo->mem_lower = ( memmap.regions[i].end / 1024 );
135 if ( memmap.regions[i].start == 0x100000 )
136 mbinfo->mem_upper = ( ( memmap.regions[i].end -
142 * Add command line in base memory
144 * @v imgname Image name
145 * @v cmdline Command line
146 * @ret physaddr Physical address of command line
148 physaddr_t multiboot_add_cmdline ( const char *imgname, const char *cmdline ) {
154 /* Copy command line to base memory buffer */
155 mb_cmdline = ( mb_cmdlines + mb_cmdline_offset );
157 ( snprintf ( mb_cmdline,
158 ( sizeof ( mb_cmdlines ) - mb_cmdline_offset ),
159 "%s %s", imgname, cmdline ) + 1 );
161 /* Truncate to terminating NUL in buffer if necessary */
162 if ( mb_cmdline_offset > sizeof ( mb_cmdlines ) )
163 mb_cmdline_offset = ( sizeof ( mb_cmdlines ) - 1 );
165 return virt_to_phys ( mb_cmdline );
169 * Build multiboot module list
171 * @v image Multiboot image
172 * @v modules Module list to fill, or NULL
173 * @ret count Number of modules
176 multiboot_build_module_list ( struct image *image,
177 struct multiboot_module *modules,
178 unsigned int limit ) {
179 struct image *module_image;
180 struct multiboot_module *module;
181 unsigned int count = 0;
187 /* Add each image as a multiboot module */
188 for_each_image ( module_image ) {
190 if ( count >= limit ) {
191 DBGC ( image, "MULTIBOOT %p limit of %d modules "
192 "reached\n", image, limit );
196 /* Do not include kernel image itself as a module */
197 if ( module_image == image )
200 /* At least some OSes expect the multiboot modules to
201 * be in ascending order, so we have to support it.
203 start = user_to_phys ( module_image->data, 0 );
204 end = user_to_phys ( module_image->data, module_image->len );
205 for ( insert = 0 ; insert < count ; insert++ ) {
206 if ( start < modules[insert].mod_start )
209 module = &modules[insert];
210 memmove ( ( module + 1 ), module,
211 ( ( count - insert ) * sizeof ( *module ) ) );
212 module->mod_start = start;
213 module->mod_end = end;
214 module->string = multiboot_add_cmdline ( module_image->name,
215 module_image->cmdline );
216 module->reserved = 0;
218 /* We promise to page-align modules */
219 assert ( ( module->mod_start & 0xfff ) == 0 );
224 /* Dump module configuration */
225 for ( i = 0 ; i < count ; i++ ) {
226 DBGC ( image, "MULTIBOOT %p module %d is [%x,%x)\n",
227 image, i, modules[i].mod_start,
228 modules[i].mod_end );
235 * The multiboot information structure
237 * Kept in base memory because some OSes won't find it elsewhere,
238 * along with the other structures belonging to the Multiboot
241 static struct multiboot_info __bss16 ( mbinfo );
242 #define mbinfo __use_data16 ( mbinfo )
244 /** The multiboot bootloader name */
245 static char __data16_array ( mb_bootloader_name, [] ) = "gPXE " VERSION;
246 #define mb_bootloader_name __use_data16 ( mb_bootloader_name )
248 /** The multiboot memory map */
249 static struct multiboot_memory_map
250 __bss16_array ( mbmemmap, [MAX_MEMORY_REGIONS] );
251 #define mbmemmap __use_data16 ( mbmemmap )
253 /** The multiboot module list */
254 static struct multiboot_module __bss16_array ( mbmodules, [MAX_MODULES] );
255 #define mbmodules __use_data16 ( mbmodules )
258 * Execute multiboot image
260 * @v image Multiboot image
261 * @ret rc Return status code
263 static int multiboot_exec ( struct image *image ) {
264 physaddr_t entry = image->priv.phys;
266 /* Populate multiboot information structure */
267 memset ( &mbinfo, 0, sizeof ( mbinfo ) );
268 mbinfo.flags = ( MBI_FLAG_LOADER | MBI_FLAG_MEM | MBI_FLAG_MMAP |
269 MBI_FLAG_CMDLINE | MBI_FLAG_MODS );
270 multiboot_build_memmap ( image, &mbinfo, mbmemmap,
271 ( sizeof(mbmemmap) / sizeof(mbmemmap[0]) ) );
272 mb_cmdline_offset = 0;
273 mbinfo.cmdline = multiboot_add_cmdline ( image->name, image->cmdline );
274 mbinfo.mods_count = multiboot_build_module_list ( image, mbmodules,
275 ( sizeof(mbmodules) / sizeof(mbmodules[0]) ) );
276 mbinfo.mods_addr = virt_to_phys ( mbmodules );
277 mbinfo.mmap_addr = virt_to_phys ( mbmemmap );
278 mbinfo.boot_loader_name = virt_to_phys ( mb_bootloader_name );
280 /* Multiboot images may not return and have no callback
281 * interface, so shut everything down prior to booting the OS.
283 shutdown ( SHUTDOWN_BOOT );
285 /* Jump to OS with flat physical addressing */
286 DBGC ( image, "MULTIBOOT %p starting execution at %lx\n",
288 __asm__ __volatile__ ( PHYS_CODE ( "pushl %%ebp\n\t"
291 : : "a" ( MULTIBOOT_BOOTLOADER_MAGIC ),
292 "b" ( virt_to_phys ( &mbinfo ) ),
294 : "ecx", "edx", "esi", "memory" );
296 DBGC ( image, "MULTIBOOT %p returned\n", image );
298 /* It isn't safe to continue after calling shutdown() */
301 return -ECANCELED; /* -EIMPOSSIBLE, anyone? */
305 * Find multiboot header
307 * @v image Multiboot file
308 * @v hdr Multiboot header descriptor to fill in
309 * @ret rc Return status code
311 static int multiboot_find_header ( struct image *image,
312 struct multiboot_header_info *hdr ) {
315 unsigned int buf_idx;
318 /* Scan through first 8kB of image file 256 bytes at a time.
319 * (Use the buffering to avoid the overhead of a
320 * copy_from_user() for every dword.)
322 for ( offset = 0 ; offset < 8192 ; offset += sizeof ( buf[0] ) ) {
323 /* Check for end of image */
324 if ( offset > image->len )
326 /* Refill buffer if applicable */
327 buf_idx = ( ( offset % sizeof ( buf ) ) / sizeof ( buf[0] ) );
328 if ( buf_idx == 0 ) {
329 copy_from_user ( buf, image->data, offset,
332 /* Check signature */
333 if ( buf[buf_idx] != MULTIBOOT_HEADER_MAGIC )
335 /* Copy header and verify checksum */
336 copy_from_user ( &hdr->mb, image->data, offset,
337 sizeof ( hdr->mb ) );
338 checksum = ( hdr->mb.magic + hdr->mb.flags +
342 /* Record offset of multiboot header and return */
343 hdr->offset = offset;
347 /* No multiboot header found */
352 * Load raw multiboot image into memory
354 * @v image Multiboot file
355 * @v hdr Multiboot header descriptor
356 * @ret rc Return status code
358 static int multiboot_load_raw ( struct image *image,
359 struct multiboot_header_info *hdr ) {
367 if ( ! ( hdr->mb.flags & MB_FLAG_RAW ) ) {
368 DBGC ( image, "MULTIBOOT %p is not flagged as a raw image\n",
373 /* Verify and prepare segment */
374 offset = ( hdr->offset - hdr->mb.header_addr + hdr->mb.load_addr );
375 filesz = ( hdr->mb.load_end_addr ?
376 ( hdr->mb.load_end_addr - hdr->mb.load_addr ) :
377 ( image->len - offset ) );
378 memsz = ( hdr->mb.bss_end_addr ?
379 ( hdr->mb.bss_end_addr - hdr->mb.load_addr ) : filesz );
380 buffer = phys_to_user ( hdr->mb.load_addr );
381 if ( ( rc = prep_segment ( buffer, filesz, memsz ) ) != 0 ) {
382 DBGC ( image, "MULTIBOOT %p could not prepare segment: %s\n",
383 image, strerror ( rc ) );
387 /* Copy image to segment */
388 memcpy_user ( buffer, 0, image->data, offset, filesz );
390 /* Record execution entry point in image private data field */
391 image->priv.phys = hdr->mb.entry_addr;
397 * Load ELF multiboot image into memory
399 * @v image Multiboot file
400 * @ret rc Return status code
402 static int multiboot_load_elf ( struct image *image ) {
406 if ( ( rc = elf_load ( image ) ) != 0 ) {
407 DBGC ( image, "MULTIBOOT %p ELF image failed to load: %s\n",
408 image, strerror ( rc ) );
416 * Load multiboot image into memory
418 * @v image Multiboot file
419 * @ret rc Return status code
421 static int multiboot_load ( struct image *image ) {
422 struct multiboot_header_info hdr;
425 /* Locate multiboot header, if present */
426 if ( ( rc = multiboot_find_header ( image, &hdr ) ) != 0 ) {
427 DBGC ( image, "MULTIBOOT %p has no multiboot header\n",
431 DBGC ( image, "MULTIBOOT %p found header with flags %08x\n",
432 image, hdr.mb.flags );
434 /* This is a multiboot image, valid or otherwise */
436 image->type = &multiboot_image_type;
438 /* Abort if we detect flags that we cannot support */
439 if ( hdr.mb.flags & MB_UNSUPPORTED_FLAGS ) {
440 DBGC ( image, "MULTIBOOT %p flags %08x not supported\n",
441 image, ( hdr.mb.flags & MB_UNSUPPORTED_FLAGS ) );
445 /* There is technically a bit MB_FLAG_RAW to indicate whether
446 * this is an ELF or a raw image. In practice, grub will use
447 * the ELF header if present, and Solaris relies on this
450 if ( ( ( rc = multiboot_load_elf ( image ) ) != 0 ) &&
451 ( ( rc = multiboot_load_raw ( image, &hdr ) ) != 0 ) )
457 /** Multiboot image type */
458 struct image_type multiboot_image_type __image_type ( PROBE_MULTIBOOT ) = {
460 .load = multiboot_load,
461 .exec = multiboot_exec,