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>
47 #include <machine/metadata.h>
51 #include "bootstrap.h"
52 #include "multiboot.h"
56 #define MULTIBOOT_SUPPORTED_FLAGS \
57 (MULTIBOOT_PAGE_ALIGN|MULTIBOOT_MEMORY_INFO)
60 extern int elf32_loadfile_raw(char *filename, uint64_t dest,
61 struct preloaded_file **result, int multiboot);
62 extern int elf64_load_modmetadata(struct preloaded_file *fp, uint64_t dest);
63 extern int elf64_obj_loadfile(char *filename, uint64_t dest,
64 struct preloaded_file **result);
66 static int multiboot_loadfile(char *, uint64_t, struct preloaded_file **);
67 static int multiboot_exec(struct preloaded_file *);
69 static int multiboot_obj_loadfile(char *, uint64_t, struct preloaded_file **);
70 static int multiboot_obj_exec(struct preloaded_file *fp);
72 struct file_format multiboot = { multiboot_loadfile, multiboot_exec };
73 struct file_format multiboot_obj =
74 { multiboot_obj_loadfile, multiboot_obj_exec };
76 extern void multiboot_tramp();
78 static const char mbl_name[] = "FreeBSD Loader";
81 multiboot_loadfile(char *filename, uint64_t dest,
82 struct preloaded_file **result)
86 caddr_t header_search;
89 struct multiboot_header *header;
93 * Read MULTIBOOT_SEARCH size in order to search for the
94 * multiboot magic header.
98 if ((fd = open(filename, O_RDONLY)) == -1)
100 header_search = malloc(MULTIBOOT_SEARCH);
101 if (header_search == NULL) {
105 search_size = read(fd, header_search, MULTIBOOT_SEARCH);
106 magic = (uint32_t *)header_search;
109 for (i = 0; i < (search_size / sizeof(uint32_t)); i++) {
110 if (magic[i] == MULTIBOOT_HEADER_MAGIC) {
111 header = (struct multiboot_header *)&magic[i];
116 if (header == NULL) {
121 /* Valid multiboot header has been found, validate checksum */
122 if (header->magic + header->flags + header->checksum != 0) {
124 "Multiboot checksum failed, magic: 0x%x flags: 0x%x checksum: 0x%x\n",
125 header->magic, header->flags, header->checksum);
130 if ((header->flags & ~MULTIBOOT_SUPPORTED_FLAGS) != 0) {
131 printf("Unsupported multiboot flags found: 0x%x\n",
137 error = elf32_loadfile_raw(filename, dest, result, 1);
140 "elf32_loadfile_raw failed: %d unable to load multiboot kernel\n",
146 * f_addr is already aligned to PAGE_SIZE, make sure
147 * f_size it's also aligned so when the modules are loaded
148 * they are aligned to PAGE_SIZE.
150 (*result)->f_size = roundup((*result)->f_size, PAGE_SIZE);
159 multiboot_exec(struct preloaded_file *fp)
161 vm_offset_t modulep, kernend, entry;
162 struct file_metadata *md;
164 struct multiboot_info *mb_info = NULL;
165 struct multiboot_mod_list *mb_mod = NULL;
166 char *cmdline = NULL;
169 struct xen_header header;
171 CTASSERT(sizeof(header) <= PAGE_SIZE);
174 * Don't pass the memory size found by the bootloader, the memory
175 * available to Dom0 will be lower than that.
177 unsetenv("smbios.memory.enabled");
179 /* Allocate the multiboot struct and fill the basic details. */
180 mb_info = malloc(sizeof(struct multiboot_info));
181 if (mb_info == NULL) {
185 bzero(mb_info, sizeof(struct multiboot_info));
186 mb_info->flags = MULTIBOOT_INFO_MEMORY|MULTIBOOT_INFO_BOOT_LOADER_NAME;
187 mb_info->mem_lower = bios_basemem / 1024;
188 mb_info->mem_upper = bios_extmem / 1024;
189 mb_info->boot_loader_name = VTOP(mbl_name);
191 /* Set the Xen command line. */
192 if (fp->f_args == NULL) {
193 /* Add the Xen command line if it is set. */
194 cmdline = getenv("xen_cmdline");
195 if (cmdline != NULL) {
196 fp->f_args = strdup(cmdline);
197 if (fp->f_args == NULL) {
203 if (fp->f_args != NULL) {
204 len = strlen(fp->f_name) + 1 + strlen(fp->f_args) + 1;
205 cmdline = malloc(len);
206 if (cmdline == NULL) {
210 snprintf(cmdline, len, "%s %s", fp->f_name, fp->f_args);
211 mb_info->cmdline = VTOP(cmdline);
212 mb_info->flags |= MULTIBOOT_INFO_CMDLINE;
215 /* Find the entry point of the Xen kernel and save it for later */
216 if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL) {
217 printf("Unable to find %s entry point\n", fp->f_name);
221 ehdr = (Elf_Ehdr *)&(md->md_data);
222 entry = ehdr->e_entry & 0xffffff;
225 * Prepare the multiboot module list, Xen assumes the first
226 * module is the Dom0 kernel, and the second one is the initramfs.
227 * This is not optimal for FreeBSD, that doesn't have a initramfs
228 * but instead loads modules dynamically and creates the metadata
231 * As expected, the first multiboot module is going to be the
232 * FreeBSD kernel loaded as a raw file. The second module is going
233 * to contain the metadata info and the loaded modules.
235 * There's a small header prefixed in the second module that contains
236 * some information required to calculate the relocated address of
237 * modulep based on the original offset of modulep from the start of
238 * the module address. Note other fields might be added to this header
242 * fp->f_addr + fp->f_size
243 * +---------+----------------+------------+
245 * | Kernel | Modules | Metadata |
247 * +---------+----------------+------------+
248 * fp->f_addr modulep kernend
251 * fp->f_addr fp->f_addr + fp->f_size
252 * +---------+------------+----------------+------------+
254 * | Kernel | xen_header | Modules | Metadata |
256 * +---------+------------+----------------+------------+
258 * \________/\__________________________________________/
262 fp = file_findfile(NULL, "elf kernel");
264 printf("No FreeBSD kernel provided, aborting\n");
269 mb_mod = malloc(sizeof(struct multiboot_mod_list) * NUM_MODULES);
270 if (mb_mod == NULL) {
275 bzero(mb_mod, sizeof(struct multiboot_mod_list) * NUM_MODULES);
277 error = bi_load64(fp->f_args, &modulep, &kernend, 0);
279 printf("bi_load64 failed: %d\n", error);
283 mb_mod[0].mod_start = fp->f_addr;
284 mb_mod[0].mod_end = fp->f_addr + fp->f_size - PAGE_SIZE;
286 mb_mod[1].mod_start = mb_mod[0].mod_end;
287 mb_mod[1].mod_end = kernend;
288 /* Indicate the kernel metadata is prefixed with a xen_header. */
289 cmdline = strdup("header");
290 if (cmdline == NULL) {
291 printf("Out of memory, aborting\n");
295 mb_mod[1].cmdline = VTOP(cmdline);
297 mb_info->mods_count = NUM_MODULES;
298 mb_info->mods_addr = VTOP(mb_mod);
299 mb_info->flags |= MULTIBOOT_INFO_MODS;
301 header.flags = XENHEADER_HAS_MODULEP_OFFSET;
302 header.modulep_offset = modulep - mb_mod[1].mod_start;
303 archsw.arch_copyin(&header, mb_mod[1].mod_start, sizeof(header));
306 __exec((void *)VTOP(multiboot_tramp), (void *)entry,
307 (void *)VTOP(mb_info));
309 panic("exec returned");
322 multiboot_obj_loadfile(char *filename, uint64_t dest,
323 struct preloaded_file **result)
325 struct preloaded_file *mfp, *kfp, *rfp;
326 struct kernel_module *kmp;
329 /* See if there's a multiboot kernel loaded */
330 mfp = file_findfile(NULL, "elf multiboot kernel");
335 * We have a multiboot kernel loaded, see if there's a FreeBSD
336 * kernel loaded also.
338 kfp = file_findfile(NULL, "elf kernel");
341 * No kernel loaded, this must be it. The kernel has to
342 * be loaded as a raw file, it will be processed by
343 * Xen and correctly loaded as an ELF file.
345 rfp = file_loadraw(filename, "elf kernel", 0);
348 "Unable to load %s as a multiboot payload kernel\n",
353 /* Load kernel metadata... */
354 setenv("kernelname", filename, 1);
355 error = elf64_load_modmetadata(rfp, rfp->f_addr + rfp->f_size);
357 printf("Unable to load kernel %s metadata error: %d\n",
364 * Reserve one page at the end of the kernel to place some
365 * metadata in order to cope for Xen relocating the modules and
366 * the metadata information.
368 rfp->f_size = roundup(rfp->f_size, PAGE_SIZE);
369 rfp->f_size += PAGE_SIZE;
372 /* The rest should be loaded as regular modules */
373 error = elf64_obj_loadfile(filename, dest, result);
375 printf("Unable to load %s as an object file, error: %d",
385 multiboot_obj_exec(struct preloaded_file *fp)