2 * Copyright (c) 2014 Roger Pau Monné <royger@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * This multiboot implementation only implements a subset of the full
29 * multiboot specification in order to be able to boot Xen and a
30 * FreeBSD Dom0. Trying to use it to boot other multiboot compliant
31 * kernels will most surely fail.
33 * The full multiboot specification can be found here:
34 * http://www.gnu.org/software/grub/manual/multiboot/multiboot.html
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
42 #include <sys/linker.h>
43 #include <sys/module.h>
44 #include <sys/stdint.h>
45 #define _MACHINE_ELF_WANT_32BIT
46 #include <machine/elf.h>
50 #include "bootstrap.h"
51 #include "multiboot.h"
52 #include "../i386/libi386/libi386.h"
53 #include "../i386/btx/lib/btxv86.h"
55 #define MULTIBOOT_SUPPORTED_FLAGS \
56 (MULTIBOOT_PAGE_ALIGN|MULTIBOOT_MEMORY_INFO)
58 #define METADATA_FIXED_SIZE (PAGE_SIZE*4)
59 #define METADATA_MODULE_SIZE PAGE_SIZE
61 #define METADATA_RESV_SIZE(mod_num) \
62 roundup(METADATA_FIXED_SIZE + METADATA_MODULE_SIZE * mod_num, PAGE_SIZE)
64 extern int elf32_loadfile_raw(char *filename, u_int64_t dest,
65 struct preloaded_file **result, int multiboot);
66 extern int elf64_load_modmetadata(struct preloaded_file *fp, u_int64_t dest);
67 extern int elf64_obj_loadfile(char *filename, u_int64_t dest,
68 struct preloaded_file **result);
70 static int multiboot_loadfile(char *, u_int64_t, struct preloaded_file **);
71 static int multiboot_exec(struct preloaded_file *);
73 static int multiboot_obj_loadfile(char *, u_int64_t, struct preloaded_file **);
74 static int multiboot_obj_exec(struct preloaded_file *fp);
76 struct file_format multiboot = { multiboot_loadfile, multiboot_exec };
77 struct file_format multiboot_obj =
78 { multiboot_obj_loadfile, multiboot_obj_exec };
80 extern void multiboot_tramp();
82 static const char mbl_name[] = "FreeBSD Loader";
85 num_modules(struct preloaded_file *kfp)
87 struct kernel_module *kmp;
90 for (kmp = kfp->f_modules; kmp != NULL; kmp = kmp->m_next)
99 struct preloaded_file *fp;
100 vm_offset_t addr = 0;
102 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
103 if (addr < (fp->f_addr + fp->f_size))
104 addr = fp->f_addr + fp->f_size;
111 multiboot_loadfile(char *filename, u_int64_t dest,
112 struct preloaded_file **result)
116 caddr_t header_search;
119 struct multiboot_header *header;
123 * Read MULTIBOOT_SEARCH size in order to search for the
124 * multiboot magic header.
126 if (filename == NULL)
128 if ((fd = open(filename, O_RDONLY)) == -1)
130 header_search = malloc(MULTIBOOT_SEARCH);
131 if (header_search == NULL) {
135 search_size = read(fd, header_search, MULTIBOOT_SEARCH);
136 magic = (uint32_t *)header_search;
139 for (i = 0; i < (search_size / sizeof(uint32_t)); i++) {
140 if (magic[i] == MULTIBOOT_HEADER_MAGIC) {
141 header = (struct multiboot_header *)&magic[i];
146 if (header == NULL) {
151 /* Valid multiboot header has been found, validate checksum */
152 if (header->magic + header->flags + header->checksum != 0) {
154 "Multiboot checksum failed, magic: 0x%x flags: 0x%x checksum: 0x%x\n",
155 header->magic, header->flags, header->checksum);
160 if ((header->flags & ~MULTIBOOT_SUPPORTED_FLAGS) != 0) {
161 printf("Unsupported multiboot flags found: 0x%x\n",
167 error = elf32_loadfile_raw(filename, dest, result, 1);
170 "elf32_loadfile_raw failed: %d unable to load multiboot kernel\n",
176 * f_addr is already aligned to PAGE_SIZE, make sure
177 * f_size it's also aligned so when the modules are loaded
178 * they are aligned to PAGE_SIZE.
180 (*result)->f_size = roundup((*result)->f_size, PAGE_SIZE);
189 multiboot_exec(struct preloaded_file *fp)
191 vm_offset_t module_start, last_addr, metadata_size;
192 vm_offset_t modulep, kernend, entry;
193 struct file_metadata *md;
195 struct multiboot_info *mb_info = NULL;
196 struct multiboot_mod_list *mb_mod = NULL;
197 char *cmdline = NULL;
202 * Don't pass the memory size found by the bootloader, the memory
203 * available to Dom0 will be lower than that.
205 unsetenv("smbios.memory.enabled");
207 /* Allocate the multiboot struct and fill the basic details. */
208 mb_info = malloc(sizeof(struct multiboot_info));
209 if (mb_info == NULL) {
213 bzero(mb_info, sizeof(struct multiboot_info));
214 mb_info->flags = MULTIBOOT_INFO_MEMORY|MULTIBOOT_INFO_BOOT_LOADER_NAME;
215 mb_info->mem_lower = bios_basemem / 1024;
216 mb_info->mem_upper = bios_extmem / 1024;
217 mb_info->boot_loader_name = VTOP(mbl_name);
219 /* Set the Xen command line. */
220 if (fp->f_args == NULL) {
221 /* Add the Xen command line if it is set. */
222 cmdline = getenv("xen_cmdline");
223 if (cmdline != NULL) {
224 fp->f_args = strdup(cmdline);
225 if (fp->f_args == NULL) {
231 if (fp->f_args != NULL) {
232 len = strlen(fp->f_name) + 1 + strlen(fp->f_args) + 1;
233 cmdline = malloc(len);
234 if (cmdline == NULL) {
238 snprintf(cmdline, len, "%s %s", fp->f_name, fp->f_args);
239 mb_info->cmdline = VTOP(cmdline);
240 mb_info->flags |= MULTIBOOT_INFO_CMDLINE;
243 /* Find the entry point of the Xen kernel and save it for later */
244 if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL) {
245 printf("Unable to find %s entry point\n", fp->f_name);
249 ehdr = (Elf_Ehdr *)&(md->md_data);
250 entry = ehdr->e_entry & 0xffffff;
253 * Prepare the multiboot module list, Xen assumes the first
254 * module is the Dom0 kernel, and the second one is the initramfs.
255 * This is not optimal for FreeBSD, that doesn't have a initramfs
256 * but instead loads modules dynamically and creates the metadata
259 * As expected, the first multiboot module is going to be the
260 * FreeBSD kernel loaded as a raw file. The second module is going
261 * to contain the metadata info and the loaded modules.
263 * On native FreeBSD loads all the modules and then places the
264 * metadata info at the end, but this is painful when running on Xen,
265 * because it relocates the second multiboot module wherever it
266 * likes. In order to workaround this limitation the metadata
267 * information is placed at the start of the second module and
268 * the original modulep value is saved together with the other
269 * metadata, so we can relocate everything.
272 * fp->f_addr + fp->f_size
273 * +---------+----------------+------------+
275 * | Kernel | Modules | Metadata |
277 * +---------+----------------+------------+
278 * fp->f_addr modulep kernend
283 * fp->f_addr + fp->f_size
284 * +---------+----------+----------------+------------+
286 * | Kernel | Reserved | Modules | Metadata |
288 * +---------+----------+----------------+------------+
291 * After metadata polacement (ie: final):
292 * fp->f_addr + fp->f_size
293 * +-----------+---------+----------+----------------+
295 * | Kernel | Free | Metadata | Modules |
297 * +-----------+---------+----------+----------------+
298 * fp->f_addr modulep kernend
299 * \__________/ \__________________________/
300 * Multiboot module 0 Multiboot module 1
303 fp = file_findfile(NULL, "elf kernel");
305 printf("No FreeBSD kernel provided, aborting\n");
310 if (fp->f_metadata != NULL) {
311 printf("FreeBSD kernel already contains metadata, aborting\n");
317 mb_mod = malloc(sizeof(struct multiboot_mod_list) * NUM_MODULES);
318 if (mb_mod == NULL) {
323 bzero(mb_mod, sizeof(struct multiboot_mod_list) * NUM_MODULES);
326 * Calculate how much memory is needed for the metatdata. We did
327 * an approximation of the maximum size when loading the kernel,
328 * but now we know the exact size, so we can release some of this
329 * preallocated memory if not needed.
331 last_addr = roundup(max_addr(), PAGE_SIZE);
332 mod_num = num_modules(fp);
335 * Place the metadata after the last used address in order to
336 * calculate it's size, this will not be used.
338 error = bi_load64(fp->f_args, last_addr, &modulep, &kernend, 0);
340 printf("bi_load64 failed: %d\n", error);
343 metadata_size = roundup(kernend - last_addr, PAGE_SIZE);
345 /* Check that the size is not greater than what we have reserved */
346 if (metadata_size > METADATA_RESV_SIZE(mod_num)) {
347 printf("Required memory for metadata is greater than reserved "
348 "space, please increase METADATA_FIXED_SIZE and "
349 "METADATA_MODULE_SIZE and rebuild the loader\n");
354 /* Clean the metadata added to the kernel in the bi_load64 dry run */
355 file_removemetadata(fp);
358 * This is the position where the second multiboot module
361 module_start = fp->f_addr + fp->f_size - metadata_size;
363 error = bi_load64(fp->f_args, module_start, &modulep, &kernend, 0);
365 printf("bi_load64 failed: %d\n", error);
369 mb_mod[0].mod_start = fp->f_addr;
370 mb_mod[0].mod_end = fp->f_addr + fp->f_size;
371 mb_mod[0].mod_end -= METADATA_RESV_SIZE(mod_num);
373 mb_mod[1].mod_start = module_start;
374 mb_mod[1].mod_end = last_addr;
376 mb_info->mods_count = NUM_MODULES;
377 mb_info->mods_addr = VTOP(mb_mod);
378 mb_info->flags |= MULTIBOOT_INFO_MODS;
381 __exec((void *)VTOP(multiboot_tramp), (void *)entry,
382 (void *)VTOP(mb_info));
384 panic("exec returned");
397 multiboot_obj_loadfile(char *filename, u_int64_t dest,
398 struct preloaded_file **result)
400 struct preloaded_file *mfp, *kfp, *rfp;
401 struct kernel_module *kmp;
404 /* See if there's a multiboot kernel loaded */
405 mfp = file_findfile(NULL, "elf multiboot kernel");
410 * We have a multiboot kernel loaded, see if there's a FreeBSD
411 * kernel loaded also.
413 kfp = file_findfile(NULL, "elf kernel");
416 * No kernel loaded, this must be it. The kernel has to
417 * be loaded as a raw file, it will be processed by
418 * Xen and correctly loaded as an ELF file.
420 rfp = file_loadraw(filename, "elf kernel", 0);
423 "Unable to load %s as a multiboot payload kernel\n",
428 /* Load kernel metadata... */
429 setenv("kernelname", filename, 1);
430 error = elf64_load_modmetadata(rfp, rfp->f_addr + rfp->f_size);
432 printf("Unable to load kernel %s metadata error: %d\n",
438 * Save space at the end of the kernel in order to place
439 * the metadata information. We do an approximation of the
440 * max metadata size, this is not optimal but it's probably
441 * the best we can do at this point. Once all modules are
442 * loaded and the size of the metadata is known this
443 * space will be recovered if not used.
445 mod_num = num_modules(rfp);
446 rfp->f_size = roundup(rfp->f_size, PAGE_SIZE);
447 rfp->f_size += METADATA_RESV_SIZE(mod_num);
450 /* The rest should be loaded as regular modules */
451 error = elf64_obj_loadfile(filename, dest, result);
453 printf("Unable to load %s as an object file, error: %d",
463 multiboot_obj_exec(struct preloaded_file *fp)