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 mb_cmdline_offset
= 0;
271 mbinfo
.cmdline
= multiboot_add_cmdline ( image
->name
, image
->cmdline
);
272 mbinfo
.mods_count
= multiboot_build_module_list ( image
, mbmodules
,
273 ( sizeof(mbmodules
) / sizeof(mbmodules
[0]) ) );
274 mbinfo
.mods_addr
= virt_to_phys ( mbmodules
);
275 mbinfo
.mmap_addr
= virt_to_phys ( mbmemmap
);
276 mbinfo
.boot_loader_name
= virt_to_phys ( mb_bootloader_name
);
278 /* Multiboot images may not return and have no callback
279 * interface, so shut everything down prior to booting the OS.
281 shutdown ( SHUTDOWN_BOOT
);
283 /* Build memory map after unhiding bootloader memory regions as part of
284 * shutting everything down.
286 multiboot_build_memmap ( image
, &mbinfo
, mbmemmap
,
287 ( sizeof(mbmemmap
) / sizeof(mbmemmap
[0]) ) );
289 /* Jump to OS with flat physical addressing */
290 DBGC ( image
, "MULTIBOOT %p starting execution at %lx\n",
292 __asm__
__volatile__ ( PHYS_CODE ( "pushl %%ebp\n\t"
295 : : "a" ( MULTIBOOT_BOOTLOADER_MAGIC
),
296 "b" ( virt_to_phys ( &mbinfo
) ),
298 : "ecx", "edx", "esi", "memory" );
300 DBGC ( image
, "MULTIBOOT %p returned\n", image
);
302 /* It isn't safe to continue after calling shutdown() */
305 return -ECANCELED
; /* -EIMPOSSIBLE, anyone? */
309 * Find multiboot header
311 * @v image Multiboot file
312 * @v hdr Multiboot header descriptor to fill in
313 * @ret rc Return status code
315 static int multiboot_find_header ( struct image
*image
,
316 struct multiboot_header_info
*hdr
) {
319 unsigned int buf_idx
;
322 /* Scan through first 8kB of image file 256 bytes at a time.
323 * (Use the buffering to avoid the overhead of a
324 * copy_from_user() for every dword.)
326 for ( offset
= 0 ; offset
< 8192 ; offset
+= sizeof ( buf
[0] ) ) {
327 /* Check for end of image */
328 if ( offset
> image
->len
)
330 /* Refill buffer if applicable */
331 buf_idx
= ( ( offset
% sizeof ( buf
) ) / sizeof ( buf
[0] ) );
332 if ( buf_idx
== 0 ) {
333 copy_from_user ( buf
, image
->data
, offset
,
336 /* Check signature */
337 if ( buf
[buf_idx
] != MULTIBOOT_HEADER_MAGIC
)
339 /* Copy header and verify checksum */
340 copy_from_user ( &hdr
->mb
, image
->data
, offset
,
341 sizeof ( hdr
->mb
) );
342 checksum
= ( hdr
->mb
.magic
+ hdr
->mb
.flags
+
346 /* Record offset of multiboot header and return */
347 hdr
->offset
= offset
;
351 /* No multiboot header found */
356 * Load raw multiboot image into memory
358 * @v image Multiboot file
359 * @v hdr Multiboot header descriptor
360 * @ret rc Return status code
362 static int multiboot_load_raw ( struct image
*image
,
363 struct multiboot_header_info
*hdr
) {
371 if ( ! ( hdr
->mb
.flags
& MB_FLAG_RAW
) ) {
372 DBGC ( image
, "MULTIBOOT %p is not flagged as a raw image\n",
377 /* Verify and prepare segment */
378 offset
= ( hdr
->offset
- hdr
->mb
.header_addr
+ hdr
->mb
.load_addr
);
379 filesz
= ( hdr
->mb
.load_end_addr
?
380 ( hdr
->mb
.load_end_addr
- hdr
->mb
.load_addr
) :
381 ( image
->len
- offset
) );
382 memsz
= ( hdr
->mb
.bss_end_addr
?
383 ( hdr
->mb
.bss_end_addr
- hdr
->mb
.load_addr
) : filesz
);
384 buffer
= phys_to_user ( hdr
->mb
.load_addr
);
385 if ( ( rc
= prep_segment ( buffer
, filesz
, memsz
) ) != 0 ) {
386 DBGC ( image
, "MULTIBOOT %p could not prepare segment: %s\n",
387 image
, strerror ( rc
) );
391 /* Copy image to segment */
392 memcpy_user ( buffer
, 0, image
->data
, offset
, filesz
);
394 /* Record execution entry point in image private data field */
395 image
->priv
.phys
= hdr
->mb
.entry_addr
;
401 * Load ELF multiboot image into memory
403 * @v image Multiboot file
404 * @ret rc Return status code
406 static int multiboot_load_elf ( struct image
*image
) {
410 if ( ( rc
= elf_load ( image
) ) != 0 ) {
411 DBGC ( image
, "MULTIBOOT %p ELF image failed to load: %s\n",
412 image
, strerror ( rc
) );
420 * Load multiboot image into memory
422 * @v image Multiboot file
423 * @ret rc Return status code
425 static int multiboot_load ( struct image
*image
) {
426 struct multiboot_header_info hdr
;
429 /* Locate multiboot header, if present */
430 if ( ( rc
= multiboot_find_header ( image
, &hdr
) ) != 0 ) {
431 DBGC ( image
, "MULTIBOOT %p has no multiboot header\n",
435 DBGC ( image
, "MULTIBOOT %p found header with flags %08x\n",
436 image
, hdr
.mb
.flags
);
438 /* This is a multiboot image, valid or otherwise */
440 image
->type
= &multiboot_image_type
;
442 /* Abort if we detect flags that we cannot support */
443 if ( hdr
.mb
.flags
& MB_UNSUPPORTED_FLAGS
) {
444 DBGC ( image
, "MULTIBOOT %p flags %08x not supported\n",
445 image
, ( hdr
.mb
.flags
& MB_UNSUPPORTED_FLAGS
) );
449 /* There is technically a bit MB_FLAG_RAW to indicate whether
450 * this is an ELF or a raw image. In practice, grub will use
451 * the ELF header if present, and Solaris relies on this
454 if ( ( ( rc
= multiboot_load_elf ( image
) ) != 0 ) &&
455 ( ( rc
= multiboot_load_raw ( image
, &hdr
) ) != 0 ) )
461 /** Multiboot image type */
462 struct image_type multiboot_image_type
__image_type ( PROBE_MULTIBOOT
) = {
464 .load
= multiboot_load
,
465 .exec
= multiboot_exec
,