1 /*-
2 * Copyright (c) 2014 Roger Pau Monné <royger@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27 /*
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.
32 *
33 * The full multiboot specification can be found here:
34 * http://www.gnu.org/software/grub/manual/multiboot/multiboot.html
35 */
36
37 #include <sys/param.h>
38 #include <sys/exec.h>
39 #include <sys/linker.h>
40 #include <sys/module.h>
41 #include <sys/stdint.h>
42 #define _MACHINE_ELF_WANT_32BIT
43 #include <machine/elf.h>
44 #include <machine/metadata.h>
45 #include <string.h>
46 #include <stand.h>
47
48 #include "bootstrap.h"
49 #include "multiboot.h"
50 #include "libi386.h"
51 #include "modinfo.h"
52 #include <btxv86.h>
53
54 #define MULTIBOOT_SUPPORTED_FLAGS \
55 (MULTIBOOT_PAGE_ALIGN|MULTIBOOT_MEMORY_INFO)
56 #define NUM_MODULES 2
57
58 extern int elf32_loadfile_raw(char *filename, uint64_t dest,
59 struct preloaded_file **result, int multiboot);
60 extern int elf64_load_modmetadata(struct preloaded_file *fp, uint64_t dest);
61 extern int elf64_obj_loadfile(char *filename, uint64_t dest,
62 struct preloaded_file **result);
63
64 static int multiboot_loadfile(char *, uint64_t, struct preloaded_file **);
65 static int multiboot_exec(struct preloaded_file *);
66
67 static int multiboot_obj_loadfile(char *, uint64_t, struct preloaded_file **);
68 static int multiboot_obj_exec(struct preloaded_file *fp);
69
70 struct file_format multiboot = { multiboot_loadfile, multiboot_exec };
71 struct file_format multiboot_obj =
72 { multiboot_obj_loadfile, multiboot_obj_exec };
73
74 extern void multiboot_tramp();
75
76 static const char mbl_name[] = "FreeBSD Loader";
77
78 static int
multiboot_loadfile(char * filename,uint64_t dest,struct preloaded_file ** result)79 multiboot_loadfile(char *filename, uint64_t dest,
80 struct preloaded_file **result)
81 {
82 uint32_t *magic;
83 int i, error;
84 caddr_t header_search;
85 ssize_t search_size;
86 int fd;
87 struct multiboot_header *header;
88 char *cmdline;
89
90 /*
91 * Read MULTIBOOT_SEARCH size in order to search for the
92 * multiboot magic header.
93 */
94 if (filename == NULL)
95 return (EFTYPE);
96 if ((fd = open(filename, O_RDONLY)) == -1)
97 return (errno);
98 header_search = malloc(MULTIBOOT_SEARCH);
99 if (header_search == NULL) {
100 close(fd);
101 return (ENOMEM);
102 }
103 search_size = read(fd, header_search, MULTIBOOT_SEARCH);
104 magic = (uint32_t *)header_search;
105
106 header = NULL;
107 for (i = 0; i < (search_size / sizeof(uint32_t)); i++) {
108 if (magic[i] == MULTIBOOT_HEADER_MAGIC) {
109 header = (struct multiboot_header *)&magic[i];
110 break;
111 }
112 }
113
114 if (header == NULL) {
115 error = EFTYPE;
116 goto out;
117 }
118
119 /* Valid multiboot header has been found, validate checksum */
120 if (header->magic + header->flags + header->checksum != 0) {
121 printf(
122 "Multiboot checksum failed, magic: 0x%x flags: 0x%x checksum: 0x%x\n",
123 header->magic, header->flags, header->checksum);
124 error = EFTYPE;
125 goto out;
126 }
127
128 if ((header->flags & ~MULTIBOOT_SUPPORTED_FLAGS) != 0) {
129 printf("Unsupported multiboot flags found: 0x%x\n",
130 header->flags);
131 error = EFTYPE;
132 goto out;
133 }
134
135 error = elf32_loadfile_raw(filename, dest, result, 1);
136 if (error != 0) {
137 printf(
138 "elf32_loadfile_raw failed: %d unable to load multiboot kernel\n",
139 error);
140 goto out;
141 }
142
143 /*
144 * f_addr is already aligned to PAGE_SIZE, make sure
145 * f_size it's also aligned so when the modules are loaded
146 * they are aligned to PAGE_SIZE.
147 */
148 (*result)->f_size = roundup((*result)->f_size, PAGE_SIZE);
149
150 out:
151 free(header_search);
152 close(fd);
153 return (error);
154 }
155
156 static int
multiboot_exec(struct preloaded_file * fp)157 multiboot_exec(struct preloaded_file *fp)
158 {
159 vm_offset_t modulep, kernend, entry;
160 struct file_metadata *md;
161 Elf_Ehdr *ehdr;
162 struct multiboot_info *mb_info = NULL;
163 struct multiboot_mod_list *mb_mod = NULL;
164 char *cmdline = NULL;
165 size_t len;
166 int error, mod_num;
167 struct xen_header header;
168
169 _Static_assert(sizeof(header) <= PAGE_SIZE, "header too large for page");
170
171 /*
172 * Don't pass the memory size found by the bootloader, the memory
173 * available to Dom0 will be lower than that.
174 */
175 unsetenv("smbios.memory.enabled");
176
177 /* Allocate the multiboot struct and fill the basic details. */
178 mb_info = malloc(sizeof(struct multiboot_info));
179 if (mb_info == NULL) {
180 error = ENOMEM;
181 goto error;
182 }
183 bzero(mb_info, sizeof(struct multiboot_info));
184 mb_info->flags = MULTIBOOT_INFO_MEMORY|MULTIBOOT_INFO_BOOT_LOADER_NAME;
185 mb_info->mem_lower = bios_basemem / 1024;
186 mb_info->mem_upper = bios_extmem / 1024;
187 mb_info->boot_loader_name = VTOP(mbl_name);
188
189 /* Set the Xen command line. */
190 if (fp->f_args == NULL) {
191 /* Add the Xen command line if it is set. */
192 cmdline = getenv("xen_cmdline");
193 if (cmdline != NULL) {
194 fp->f_args = strdup(cmdline);
195 if (fp->f_args == NULL) {
196 error = ENOMEM;
197 goto error;
198 }
199 }
200 }
201 if (fp->f_args != NULL) {
202 len = strlen(fp->f_name) + 1 + strlen(fp->f_args) + 1;
203 cmdline = malloc(len);
204 if (cmdline == NULL) {
205 error = ENOMEM;
206 goto error;
207 }
208 snprintf(cmdline, len, "%s %s", fp->f_name, fp->f_args);
209 mb_info->cmdline = VTOP(cmdline);
210 mb_info->flags |= MULTIBOOT_INFO_CMDLINE;
211 }
212
213 /* Find the entry point of the Xen kernel and save it for later */
214 if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL) {
215 printf("Unable to find %s entry point\n", fp->f_name);
216 error = EINVAL;
217 goto error;
218 }
219 ehdr = (Elf_Ehdr *)&(md->md_data);
220 entry = ehdr->e_entry & 0xffffff;
221
222 /*
223 * Prepare the multiboot module list, Xen assumes the first
224 * module is the Dom0 kernel, and the second one is the initramfs.
225 * This is not optimal for FreeBSD, that doesn't have a initramfs
226 * but instead loads modules dynamically and creates the metadata
227 * info on-the-fly.
228 *
229 * As expected, the first multiboot module is going to be the
230 * FreeBSD kernel loaded as a raw file. The second module is going
231 * to contain the metadata info and the loaded modules.
232 *
233 * There's a small header prefixed in the second module that contains
234 * some information required to calculate the relocated address of
235 * modulep based on the original offset of modulep from the start of
236 * the module address. Note other fields might be added to this header
237 * if required.
238 *
239 * Native layout:
240 * fp->f_addr + fp->f_size
241 * +---------+----------------+------------+
242 * | | | |
243 * | Kernel | Modules | Metadata |
244 * | | | |
245 * +---------+----------------+------------+
246 * fp->f_addr modulep kernend
247 *
248 * Xen dom0 layout:
249 * fp->f_addr fp->f_addr + fp->f_size
250 * +---------+------------+----------------+------------+
251 * | | | | |
252 * | Kernel | xen_header | Modules | Metadata |
253 * | | | | |
254 * +---------+------------+----------------+------------+
255 * modulep kernend
256 * \________/\__________________________________________/
257 * module 0 module 1
258 */
259
260 fp = file_findfile(NULL, md_kerntype);
261 if (fp == NULL) {
262 printf("No FreeBSD kernel provided, aborting\n");
263 error = EINVAL;
264 goto error;
265 }
266
267 mb_mod = malloc(sizeof(struct multiboot_mod_list) * NUM_MODULES);
268 if (mb_mod == NULL) {
269 error = ENOMEM;
270 goto error;
271 }
272
273 bzero(mb_mod, sizeof(struct multiboot_mod_list) * NUM_MODULES);
274
275 error = bi_load64(fp->f_args, &modulep, &kernend, 0);
276 if (error != 0) {
277 printf("bi_load64 failed: %d\n", error);
278 goto error;
279 }
280
281 mb_mod[0].mod_start = fp->f_addr;
282 mb_mod[0].mod_end = fp->f_addr + fp->f_size - PAGE_SIZE;
283
284 mb_mod[1].mod_start = mb_mod[0].mod_end;
285 mb_mod[1].mod_end = kernend;
286 /* Indicate the kernel metadata is prefixed with a xen_header. */
287 cmdline = strdup("header");
288 if (cmdline == NULL) {
289 printf("Out of memory, aborting\n");
290 error = ENOMEM;
291 goto error;
292 }
293 mb_mod[1].cmdline = VTOP(cmdline);
294
295 mb_info->mods_count = NUM_MODULES;
296 mb_info->mods_addr = VTOP(mb_mod);
297 mb_info->flags |= MULTIBOOT_INFO_MODS;
298
299 header.flags = XENHEADER_HAS_MODULEP_OFFSET;
300 header.modulep_offset = modulep - mb_mod[1].mod_start;
301 archsw.arch_copyin(&header, mb_mod[1].mod_start, sizeof(header));
302
303 dev_cleanup();
304 __exec((void *)VTOP(multiboot_tramp), (void *)entry,
305 (void *)VTOP(mb_info));
306
307 panic("exec returned");
308
309 error:
310 if (mb_mod)
311 free(mb_mod);
312 if (mb_info)
313 free(mb_info);
314 if (cmdline)
315 free(cmdline);
316 return (error);
317 }
318
319 static int
multiboot_obj_loadfile(char * filename,uint64_t dest,struct preloaded_file ** result)320 multiboot_obj_loadfile(char *filename, uint64_t dest,
321 struct preloaded_file **result)
322 {
323 struct preloaded_file *mfp, *kfp, *rfp;
324 struct kernel_module *kmp;
325 int error, mod_num;
326
327 /* See if there's a multiboot kernel loaded */
328 mfp = file_findfile(NULL, md_kerntype_mb);
329 if (mfp == NULL)
330 return (EFTYPE);
331
332 /*
333 * We have a multiboot kernel loaded, see if there's a FreeBSD
334 * kernel loaded also.
335 */
336 kfp = file_findfile(NULL, md_kerntype);
337 if (kfp == NULL) {
338 /*
339 * No kernel loaded, this must be it. The kernel has to
340 * be loaded as a raw file, it will be processed by
341 * Xen and correctly loaded as an ELF file.
342 */
343 rfp = file_loadraw(filename, md_kerntype, 0);
344 if (rfp == NULL) {
345 printf(
346 "Unable to load %s as a multiboot payload kernel\n",
347 filename);
348 return (EINVAL);
349 }
350
351 /* Load kernel metadata... */
352 setenv("kernelname", filename, 1);
353 error = elf64_load_modmetadata(rfp, rfp->f_addr + rfp->f_size);
354 if (error) {
355 printf("Unable to load kernel %s metadata error: %d\n",
356 rfp->f_name, error);
357 return (EINVAL);
358 }
359
360
361 /*
362 * Reserve one page at the end of the kernel to place some
363 * metadata in order to cope for Xen relocating the modules and
364 * the metadata information.
365 */
366 rfp->f_size = roundup(rfp->f_size, PAGE_SIZE);
367 rfp->f_size += PAGE_SIZE;
368 *result = rfp;
369 } else {
370 /* The rest should be loaded as regular modules */
371 error = elf64_obj_loadfile(filename, dest, result);
372 if (error != 0) {
373 printf("Unable to load %s as an object file, error: %d",
374 filename, error);
375 return (error);
376 }
377 }
378
379 return (0);
380 }
381
382 static int
multiboot_obj_exec(struct preloaded_file * fp)383 multiboot_obj_exec(struct preloaded_file *fp)
384 {
385
386 return (EFTYPE);
387 }
388