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