exec_multiboot2.c revision 1.2.2.2 1 /* $NetBSD: exec_multiboot2.c,v 1.2.2.2 2019/09/17 19:32:00 martin Exp $ */
2
3 /*
4 * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/reboot.h>
31 #include <sys/types.h>
32
33 #include <i386/multiboot2.h>
34
35 #include <dev/acpi/acpica.h>
36 #include <x86/acpi_machdep.h>
37 #include <x86/smbiosvar.h>
38
39 #include <lib/libsa/stand.h>
40 #include <lib/libkern/libkern.h>
41
42
43 #include "loadfile.h"
44 #include "libi386.h"
45 #include "biosdisk.h"
46 #include "bootinfo.h"
47 #include "bootmod.h"
48 #include "vbe.h"
49 #ifdef EFIBOOT
50 #include "efiboot.h"
51 #endif
52
53 #define CGA_BUF 0xb8000 /* From isa_machdep.h */
54
55 extern const char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
56 extern const uint8_t rasops_cmap[];
57 extern struct btinfo_framebuffer btinfo_framebuffer;
58 extern struct btinfo_modulelist *btinfo_modulelist;
59 #ifdef EFIBOOT
60 extern struct btinfo_efimemmap *btinfo_efimemmap;
61 #else
62 extern struct btinfo_memmap *btinfo_memmap;
63 #endif
64
65
66 struct multiboot_package_priv {
67 struct multiboot_tag *mpp_mbi;
68 size_t mpp_mbi_len;
69 struct multiboot_header_tag_information_request*mpp_info_req;
70 struct multiboot_header_tag_address *mpp_address;
71 struct multiboot_header_tag_entry_address *mpp_entry;
72 struct multiboot_header_tag_console_flags *mpp_console;
73 struct multiboot_header_tag_framebuffer *mpp_framebuffer;
74 struct multiboot_header_tag *mpp_module_align;
75 struct multiboot_header_tag *mpp_efi_bs;
76 struct multiboot_header_tag_entry_address *mpp_entry_elf32;
77 struct multiboot_header_tag_entry_address *mpp_entry_elf64;
78 struct multiboot_header_tag_relocatable *mpp_relocatable;
79 };
80
81 #ifndef NO_MULTIBOOT2
82
83 #ifdef MULTIBOOT2_DEBUG
84 static void
85 mbi_hexdump(char *addr, size_t len)
86 {
87 int i,j;
88
89 for (i = 0; i < len; i += 16) {
90 printf(" %p ", addr + i);
91 for (j = 0; j < 16 && i + j < len; j++) {
92 char *cp = addr + i + j;
93 printf("%s%s%x",
94 (i+j) % 4 ? "" : " ",
95 (unsigned char)*cp < 0x10 ? "0" : "",
96 (unsigned char)*cp);
97 }
98 printf("\n");
99 }
100
101 return;
102 }
103
104 static const char *
105 mbi_tag_name(uint32_t type)
106 {
107 const char *tag_name;
108
109 switch (type) {
110 case MULTIBOOT_TAG_TYPE_END:
111 tag_name = "END"; break;
112 case MULTIBOOT_TAG_TYPE_CMDLINE:
113 tag_name = "CMDLINE"; break;
114 case MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME:
115 tag_name = "BOOT_LOADER_NAME"; break;
116 case MULTIBOOT_TAG_TYPE_MODULE:
117 tag_name = "MODULE"; break;
118 case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO:
119 tag_name = "BASIC_MEMINFO"; break;
120 case MULTIBOOT_TAG_TYPE_BOOTDEV:
121 tag_name = "BOOTDEV"; break;
122 case MULTIBOOT_TAG_TYPE_MMAP:
123 tag_name = "MMAP"; break;
124 case MULTIBOOT_TAG_TYPE_VBE:
125 tag_name = "VBE"; break;
126 case MULTIBOOT_TAG_TYPE_FRAMEBUFFER:
127 tag_name = "FRAMEBUFFER"; break;
128 case MULTIBOOT_TAG_TYPE_ELF_SECTIONS:
129 tag_name = "ELF_SECTIONS"; break;
130 case MULTIBOOT_TAG_TYPE_APM:
131 tag_name = "APM"; break;
132 case MULTIBOOT_TAG_TYPE_EFI32:
133 tag_name = "EFI32"; break;
134 case MULTIBOOT_TAG_TYPE_EFI64:
135 tag_name = "EFI64"; break;
136 case MULTIBOOT_TAG_TYPE_SMBIOS:
137 tag_name = "SMBIOS"; break;
138 case MULTIBOOT_TAG_TYPE_ACPI_OLD:
139 tag_name = "ACPI_OLD"; break;
140 case MULTIBOOT_TAG_TYPE_ACPI_NEW:
141 tag_name = "ACPI_NEW"; break;
142 case MULTIBOOT_TAG_TYPE_NETWORK:
143 tag_name = "NETWORK"; break;
144 case MULTIBOOT_TAG_TYPE_EFI_MMAP:
145 tag_name = "EFI_MMAP"; break;
146 case MULTIBOOT_TAG_TYPE_EFI_BS:
147 tag_name = "EFI_BS"; break;
148 case MULTIBOOT_TAG_TYPE_EFI32_IH:
149 tag_name = "EFI32_IH"; break;
150 case MULTIBOOT_TAG_TYPE_EFI64_IH:
151 tag_name = "EFI64_IH"; break;
152 case MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR:
153 tag_name = "LOAD_BASE_ADDR"; break;
154 default:
155 tag_name = "unknown"; break;
156 }
157
158 return tag_name;
159 }
160
161 static void
162 multiboot2_info_dump(uint32_t magic, char *mbi)
163 {
164 struct multiboot_tag *mbt;
165 char *cp;
166 uint32_t total_size;
167 uint32_t actual_size;
168 uint32_t reserved;
169 int i = 0;
170
171 printf("=== multiboot2 info dump start ===\n");
172
173 if (magic != MULTIBOOT2_BOOTLOADER_MAGIC) {
174 printf("Unexpected multiboot2 magic number: 0x%x\n", magic);
175 goto out;
176 }
177
178 if (mbi != (char *)rounddown((vaddr_t)mbi, MULTIBOOT_TAG_ALIGN)) {
179 printf("mbi at %p is not properly aligned\n", mbi);
180 goto out;
181 }
182
183 total_size = *(uint32_t *)mbi;
184 reserved = *(uint32_t *)mbi + 1;
185 mbt = (struct multiboot_tag *)(uint32_t *)mbi + 2;
186 actual_size = (char *)mbt - mbi;
187 printf("mbi.total_size = %d\n", total_size);
188 printf("mbi.reserved = %d\n", reserved);
189
190 for (cp = mbi + sizeof(total_size) + sizeof(reserved);
191 cp - mbi < total_size;
192 cp = cp + roundup(mbt->size, MULTIBOOT_TAG_ALIGN)) {
193 mbt = (struct multiboot_tag *)cp;
194 actual_size += roundup(mbt->size, MULTIBOOT_TAG_ALIGN);
195
196 printf("mbi[%d].type = %d(%s), .size = %d ",
197 i++, mbt->type, mbi_tag_name(mbt->type), mbt->size);
198
199 switch (mbt->type) {
200 case MULTIBOOT_TAG_TYPE_CMDLINE:
201 printf(".string = \"%s\"\n",
202 ((struct multiboot_tag_string *)mbt)->string);
203 break;
204 case MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME:
205 printf(".string = \"%s\"\n",
206 ((struct multiboot_tag_string *)mbt)->string);
207 break;
208 case MULTIBOOT_TAG_TYPE_MODULE:
209 printf(".mod_start = 0x%x, mod_end = 0x%x, "
210 "string = \"%s\"\n",
211 ((struct multiboot_tag_module *)mbt)->mod_start,
212 ((struct multiboot_tag_module *)mbt)->mod_end,
213 ((struct multiboot_tag_module *)mbt)->cmdline);
214 break;
215 case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO: {
216 struct multiboot_tag_basic_meminfo *meminfo;
217
218 meminfo = (struct multiboot_tag_basic_meminfo *)mbt;
219 printf(".mem_lower = %uKB, .mem_upper = %uKB\n",
220 meminfo->mem_lower, meminfo->mem_upper);
221 break;
222 }
223 case MULTIBOOT_TAG_TYPE_BOOTDEV:
224 printf (".biosdev = 0x%x, .slice = %d, .part = %d\n",
225 ((struct multiboot_tag_bootdev *)mbt)->biosdev,
226 ((struct multiboot_tag_bootdev *)mbt)->slice,
227 ((struct multiboot_tag_bootdev *)mbt)->part);
228 break;
229 case MULTIBOOT_TAG_TYPE_MMAP: {
230 struct multiboot_tag_mmap *memmap;
231 multiboot_memory_map_t *mmap;
232 uint32_t entry_size;
233 uint32_t entry_version;
234 int j = 0;
235
236 memmap = (struct multiboot_tag_mmap *)mbt;
237 entry_size = memmap->entry_size;
238 entry_version = memmap->entry_version;
239 printf (".entry_size = %d, .entry_version = %d\n",
240 entry_size, entry_version);
241
242 for (mmap = ((struct multiboot_tag_mmap *)mbt)->entries;
243 (char *)mmap - (char *)mbt < mbt->size;
244 mmap = (void *)((char *)mmap + entry_size))
245 printf(" entry[%d].addr = 0x%"PRIx64",\t"
246 ".len = 0x%"PRIx64",\t.type = 0x%x\n",
247 j++, (uint64_t)mmap->addr,
248 (uint64_t)mmap->len,
249 mmap->type);
250 break;
251 }
252 case MULTIBOOT_TAG_TYPE_FRAMEBUFFER: {
253 struct multiboot_tag_framebuffer *fb = (void *)mbt;
254
255 printf ("%dx%dx%d at 0x%"PRIx64"\n",
256 fb->common.framebuffer_width,
257 fb->common.framebuffer_height,
258 fb->common.framebuffer_bpp,
259 (uint64_t)fb->common.framebuffer_addr);
260 mbi_hexdump((char *)mbt, mbt->size);
261 break;
262 }
263 case MULTIBOOT_TAG_TYPE_ELF_SECTIONS:
264 printf(".num = %d, .entsize = %d, .shndx = %d\n",
265 ((struct multiboot_tag_elf_sections *)mbt)->num,
266 ((struct multiboot_tag_elf_sections *)mbt)->entsize,
267 ((struct multiboot_tag_elf_sections *)mbt)->shndx);
268 mbi_hexdump((char *)mbt, mbt->size);
269 break;
270 case MULTIBOOT_TAG_TYPE_APM:
271 printf(".version = %d, .cseg = 0x%x, .offset = 0x%x, "
272 ".cseg_16 = 0x%x, .dseg = 0x%x, .flags = 0x%x, "
273 ".cseg_len = %d, .cseg_16_len = %d, "
274 ".dseg_len = %d\n",
275 ((struct multiboot_tag_apm *)mbt)->version,
276 ((struct multiboot_tag_apm *)mbt)->cseg,
277 ((struct multiboot_tag_apm *)mbt)->offset,
278 ((struct multiboot_tag_apm *)mbt)->cseg_16,
279 ((struct multiboot_tag_apm *)mbt)->dseg,
280 ((struct multiboot_tag_apm *)mbt)->flags,
281 ((struct multiboot_tag_apm *)mbt)->cseg_len,
282 ((struct multiboot_tag_apm *)mbt)->cseg_16_len,
283 ((struct multiboot_tag_apm *)mbt)->dseg_len);
284 break;
285 case MULTIBOOT_TAG_TYPE_EFI32:
286 printf(".pointer = 0x%x\n",
287 ((struct multiboot_tag_efi32 *)mbt)->pointer);
288 break;
289 case MULTIBOOT_TAG_TYPE_EFI64:
290 printf(".pointer = 0x%"PRIx64"\n", (uint64_t)
291 ((struct multiboot_tag_efi64 *)mbt)->pointer);
292 break;
293 case MULTIBOOT_TAG_TYPE_SMBIOS:
294 printf(".major = %d, .minor = %d\n",
295 ((struct multiboot_tag_smbios *)mbt)->major,
296 ((struct multiboot_tag_smbios *)mbt)->minor);
297 mbi_hexdump((char *)mbt, mbt->size);
298 break;
299 case MULTIBOOT_TAG_TYPE_ACPI_OLD:
300 printf("\n");
301 mbi_hexdump((char *)mbt, mbt->size);
302 break;
303 case MULTIBOOT_TAG_TYPE_ACPI_NEW:
304 printf("\n");
305 mbi_hexdump((char *)mbt, mbt->size);
306 break;
307 case MULTIBOOT_TAG_TYPE_NETWORK:
308 printf("\n");
309 mbi_hexdump((char *)mbt, mbt->size);
310 break;
311 case MULTIBOOT_TAG_TYPE_EFI_MMAP:
312 printf("\n");
313 mbi_hexdump((char *)mbt, mbt->size);
314 break;
315 case MULTIBOOT_TAG_TYPE_EFI_BS:
316 printf("\n");
317 break;
318 case MULTIBOOT_TAG_TYPE_EFI32_IH:
319 printf(".pointer = 0x%"PRIx32"\n",
320 ((struct multiboot_tag_efi32_ih *)mbt)->pointer);
321 break;
322 case MULTIBOOT_TAG_TYPE_EFI64_IH:
323 printf(".pointer = 0x%"PRIx64"\n", (uint64_t)
324 ((struct multiboot_tag_efi64_ih *)mbt)->pointer);
325 break;
326 case MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR: {
327 struct multiboot_tag_load_base_addr *ld = (void *)mbt;
328 printf(".load_base_addr = 0x%x\n", ld->load_base_addr);
329 break;
330 }
331 case MULTIBOOT_TAG_TYPE_END:
332 break;
333 default:
334 printf("\n");
335 mbi_hexdump((char *)mbt, mbt->size);
336 break;
337 }
338 }
339
340 if (total_size != actual_size)
341 printf("Size mismatch: announded %d, actual %d\n",
342 total_size, actual_size);
343
344 out:
345 printf("=== multiboot2 info dump start ===\n");
346 return;
347 }
348
349 #define MPP_OPT(flags) \
350 (flags & MULTIBOOT_HEADER_TAG_OPTIONAL) ? " (opt)" : " (req)"
351
352 static
353 void multiboot2_header_dump(struct multiboot_package *mbp)
354 {
355 struct multiboot_package_priv *mpp = mbp->mbp_priv;
356
357 printf("=== multiboot2 header dump start ===\n");
358 if (mpp->mpp_info_req) {
359 struct multiboot_header_tag_information_request *info_req;
360 size_t nreq;
361 int i;
362
363 info_req = mpp->mpp_info_req;
364
365 nreq = (info_req->size - sizeof(*info_req))
366 / sizeof(info_req->requests[0]);
367
368 printf("Information tag request%s: ",
369 MPP_OPT(info_req->flags));
370 for (i = 0; i < nreq; i++)
371 printf("%d(%s) ",
372 info_req->requests[i],
373 mbi_tag_name(info_req->requests[i]));
374 printf("\n");
375 }
376
377 if (mpp->mpp_address)
378 printf("Addresses%s: header = %"PRIx32", load = %"PRIx32", "
379 "end = %"PRIx32", bss = %"PRIx32"\n",
380 MPP_OPT(mpp->mpp_address->flags),
381 mpp->mpp_address->header_addr,
382 mpp->mpp_address->load_addr,
383 mpp->mpp_address->load_end_addr,
384 mpp->mpp_address->bss_end_addr);
385
386 if (mpp->mpp_entry)
387 printf("Entry point%s: %"PRIx32"\n",
388 MPP_OPT(mpp->mpp_entry->flags),
389 mpp->mpp_entry->entry_addr);
390
391 if (mpp->mpp_console) {
392 int flags = mpp->mpp_console->console_flags;
393 char *req_flag = "";
394 char *ega_flag = "";
395
396 if (flags & MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED)
397 ega_flag = " EGA";
398 if (flags & MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED)
399 req_flag = " required";
400
401 printf("Console flags%s: %s %s\n",
402 MPP_OPT(mpp->mpp_console->flags),
403 ega_flag, req_flag);
404 }
405
406 if (mpp->mpp_framebuffer)
407 printf("Framebuffer%s: width = %d, height = %d, depth = %d\n",
408 MPP_OPT(mpp->mpp_framebuffer->flags),
409 mpp->mpp_framebuffer->width,
410 mpp->mpp_framebuffer->height,
411 mpp->mpp_framebuffer->depth);
412
413 if (mpp->mpp_module_align)
414 printf("Module alignmenet%s\n",
415 MPP_OPT(mpp->mpp_module_align->flags));
416
417 if (mpp->mpp_efi_bs)
418 printf("Do not call EFI Boot service exit%s\n",
419 MPP_OPT(mpp->mpp_efi_bs->flags));
420
421 if (mpp->mpp_entry_elf32)
422 printf("EFI32 entry point%s: %"PRIx32"\n",
423 MPP_OPT(mpp->mpp_entry_elf32->flags),
424 mpp->mpp_entry_elf32->entry_addr);
425
426 if (mpp->mpp_entry_elf64)
427 printf("EFI64 entry point%s: %"PRIx32"\n",
428 MPP_OPT(mpp->mpp_entry_elf64->flags),
429 mpp->mpp_entry_elf64->entry_addr);
430
431 if (mpp->mpp_relocatable) {
432 char *pref;
433
434 switch (mpp->mpp_relocatable->preference) {
435 case MULTIBOOT_LOAD_PREFERENCE_NONE: pref = "none"; break;
436 case MULTIBOOT_LOAD_PREFERENCE_LOW: pref = "low"; break;
437 case MULTIBOOT_LOAD_PREFERENCE_HIGH: pref = "high"; break;
438 default:
439 pref = "(unknown)"; break;
440 }
441 printf("Relocatable%s: min_addr = %"PRIx32", "
442 "max_addr = %"PRIx32", align = %"PRIx32", pref %s\n",
443 MPP_OPT(mpp->mpp_relocatable->flags),
444 mpp->mpp_relocatable->min_addr,
445 mpp->mpp_relocatable->max_addr,
446 mpp->mpp_relocatable->align, pref);
447 }
448
449 printf("=== multiboot2 header dump end ===\n");
450 return;
451 }
452 #endif /* MULTIBOOT2_DEBUG */
453
454 static size_t
455 mbi_cmdline(struct multiboot_package *mbp, void *buf)
456 {
457 struct multiboot_tag_string *mbt = buf;
458 size_t cmdlen;
459 size_t len;
460 const char fmt[] = "%s %s";
461
462 /* +1 for trailing \0 */
463 cmdlen = snprintf(NULL, SIZE_T_MAX, fmt, mbp->mbp_file, mbp->mbp_args)
464 + 1;
465 len = sizeof(*mbt) + cmdlen;
466
467 if (mbt) {
468 mbt->type = MULTIBOOT_TAG_TYPE_CMDLINE;
469 mbt->size = len;
470 (void)snprintf(mbt->string, cmdlen, fmt,
471 mbp->mbp_file, mbp->mbp_args);
472 }
473
474 return roundup(len, MULTIBOOT_TAG_ALIGN);
475 }
476
477 static size_t
478 mbi_boot_loader_name(struct multiboot_package *mbp, void *buf)
479 {
480 struct multiboot_tag_string *mbt = buf;
481 size_t len;
482 size_t strlen;
483 const char fmt[] = "%s, Revision %s (from NetBSD %s)";
484
485
486 /* +1 for trailing \0 */
487 strlen = snprintf(NULL, SIZE_T_MAX, fmt,
488 bootprog_name, bootprog_rev, bootprog_kernrev)
489 + 1;
490 len = sizeof(*mbt) + strlen;
491
492 if (mbt) {
493 mbt->type = MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME;
494 mbt->size = len;
495 (void)snprintf(mbt->string, strlen, fmt, bootprog_name,
496 bootprog_rev, bootprog_kernrev);
497 }
498
499 return roundup(len, MULTIBOOT_TAG_ALIGN);
500 }
501
502 static size_t
503 mbi_modules(struct multiboot_package *mbp, void *buf)
504 {
505 struct multiboot_tag_module *mbt = buf;
506 struct bi_modulelist_entry *bim;
507 size_t len;
508 int i;
509
510 if (btinfo_modulelist == NULL)
511 return 0;
512
513 len = 0;
514
515 bim = (struct bi_modulelist_entry *)(btinfo_modulelist + 1);
516 for (i = 0; i < btinfo_modulelist->num; i++) {
517 size_t pathlen = strlen(bim->path) + 1;
518 size_t mbt_len = sizeof(*mbt) + pathlen;
519 size_t mbt_len_align = roundup(mbt_len, MULTIBOOT_TAG_ALIGN);
520 len += mbt_len_align;
521
522 if (mbt) {
523 mbt->type = MULTIBOOT_TAG_TYPE_MODULE;
524 mbt->size = mbt_len;
525 mbt->mod_start = bim->base;
526 mbt->mod_end = bim->base + bim->len;
527 strncpy(mbt->cmdline, bim->path, pathlen);
528
529 mbt = (struct multiboot_tag_module *)
530 ((char *)mbt + mbt_len_align);
531 }
532 }
533
534 return len;
535 }
536
537 static size_t
538 mbi_basic_meminfo(struct multiboot_package *mbp, void *buf)
539 {
540 struct multiboot_tag_basic_meminfo *mbt = buf;
541 size_t len;
542
543 len = sizeof(*mbt);
544
545 if (mbt) {
546 mbt->type = MULTIBOOT_TAG_TYPE_BASIC_MEMINFO;
547 mbt->size = len;
548 mbt->mem_lower = mbp->mbp_basemem;
549 mbt->mem_upper = mbp->mbp_extmem;
550 }
551
552 return roundup(len, MULTIBOOT_TAG_ALIGN);
553 }
554
555 static size_t
556 mbi_bootdev(struct multiboot_package *mbp, void *buf)
557 {
558 struct multiboot_tag_bootdev *mbt = buf;
559 size_t len;
560
561 len = sizeof(*mbt);
562
563 /*
564 * According to the specification:
565 * - sub_partition is used for BSD disklabel.
566 * - Extendded MBR partitions are counted from 4 and increasing,
567 * with no subpartition.
568 */
569 if (mbt) {
570 mbt->type = MULTIBOOT_TAG_TYPE_BOOTDEV;
571 mbt->size = len;
572 mbt->biosdev = bi_disk.biosdev;
573 mbt->slice = bi_disk.partition;
574 mbt->part = 0xFFFFFFFF; /* aka sub_partition, for disklabel */
575 }
576
577 return roundup(len, MULTIBOOT_TAG_ALIGN);
578 return 0;
579 }
580
581 static size_t
582 mbi_mmap(struct multiboot_package *mbp, void *buf)
583 {
584 size_t len = 0;
585 struct multiboot_tag_mmap *mbt = buf;
586 struct bi_memmap_entry *memmap;
587 size_t num;
588
589 #ifndef EFIBOOT
590 bi_getmemmap();
591
592 if (btinfo_memmap == NULL)
593 goto out;
594
595 memmap = btinfo_memmap->entry;
596 num = btinfo_memmap->num;
597 #else
598 if (efi_memory_get_memmap(&memmap, &num) != 0)
599 goto out;
600 #endif
601
602 len = sizeof(*mbt) + num * sizeof(mbt->entries[0]);
603
604 if (mbt) {
605 int i;
606 struct multiboot_mmap_entry *mbte;
607
608 mbt->type = MULTIBOOT_TAG_TYPE_MMAP;
609 mbt->size = len;
610 mbt->entry_size = sizeof(mbt->entries[0]);
611 mbt->entry_version = 0;
612
613 mbte = (struct multiboot_mmap_entry *)(mbt + 1);
614 for (i = 0; i < num; i++) {
615 mbte[i].addr = memmap[i].addr;
616 mbte[i].len = memmap[i].size;
617 switch(memmap[i].type) {
618 case BIM_Memory:
619 mbte[i].type = MULTIBOOT_MEMORY_AVAILABLE;
620 break;
621 case BIM_Reserved:
622 mbte[i].type = MULTIBOOT_MEMORY_RESERVED;
623 break;
624 case BIM_ACPI:
625 mbte[i].type =
626 MULTIBOOT_MEMORY_ACPI_RECLAIMABLE;
627 break;
628 case BIM_NVS:
629 mbte[i].type = MULTIBOOT_MEMORY_NVS;
630 break;
631 case BIM_Unusable:
632 mbte[i].type = MULTIBOOT_MEMORY_BADRAM;
633 break;
634 default:
635 mbte[i].type = MULTIBOOT_MEMORY_RESERVED;
636 break;
637 }
638 mbte[i].zero = 0;
639 }
640 }
641 #ifdef EFIBOOT
642 dealloc(memmap, num * sizeof(memmap));
643 #endif
644 out:
645 return roundup(len, MULTIBOOT_TAG_ALIGN);
646 }
647
648 static size_t
649 mbi_vbe(struct multiboot_package *mbp, void *buf)
650 {
651 size_t len = 0;
652
653 #ifndef EFIBOOT
654 struct multiboot_tag_vbe *mbt = buf;
655
656 len = sizeof(*mbt);
657
658 if (mbt) {
659 mbt->type = MULTIBOOT_TAG_TYPE_VBE;
660 mbt->size = len;
661 mbt->vbe_mode = btinfo_framebuffer.vbemode;
662 mbt->vbe_interface_seg = 0;
663 mbt->vbe_interface_off = 0;
664 mbt->vbe_interface_len = 0;
665 biosvbe_info((struct vbeinfoblock *)&mbt->vbe_control_info);
666 biosvbe_get_mode_info(mbt->vbe_mode,
667 (struct modeinfoblock *)&mbt->vbe_mode_info);
668 }
669 #endif
670 return roundup(len, MULTIBOOT_TAG_ALIGN);
671 }
672
673 static size_t
674 mbi_framebuffer(struct multiboot_package *mbp, void *buf)
675 {
676 size_t len = 0;
677 struct multiboot_tag_framebuffer *mbt = buf;
678 struct btinfo_framebuffer *fb = &btinfo_framebuffer;
679
680 #ifndef EFIBOOT
681 struct modeinfoblock mi;
682
683 if (fb->physaddr != 0) {
684 int ret;
685
686 ret = biosvbe_get_mode_info(fb->vbemode, &mi);
687 if (ret != 0x004f)
688 return 0;
689 }
690 #endif
691
692 len = sizeof(*mbt);
693
694 if (mbt) {
695 mbt->common.type = MULTIBOOT_TAG_TYPE_FRAMEBUFFER;
696 mbt->common.size = len;
697 mbt->common.reserved = 0;
698
699 /*
700 * No framebuffer, default to 80x25 console
701 */
702 if (fb->physaddr == 0) {
703 int width = 80;
704 int height = 25;
705 int charlen = 2;
706 mbt->common.framebuffer_addr = CGA_BUF;
707 mbt->common.framebuffer_width = width;
708 mbt->common.framebuffer_height = height;
709 mbt->common.framebuffer_bpp = charlen * 8;
710 mbt->common.framebuffer_pitch = width * charlen;
711 mbt->common.framebuffer_type =
712 MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT;
713 } else {
714 mbt->common.framebuffer_addr = fb->physaddr;
715 mbt->common.framebuffer_pitch = fb->stride;
716 mbt->common.framebuffer_width = fb->width;
717 mbt->common.framebuffer_height = fb->height;
718 mbt->common.framebuffer_bpp = fb->depth;
719 mbt->common.framebuffer_type =
720 MULTIBOOT_FRAMEBUFFER_TYPE_RGB;
721 #ifndef EFIBOOT
722 if (mi.MemoryModel == 0x04)
723 mbt->common.framebuffer_type =
724 MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED;
725 #endif
726 }
727
728 switch (mbt->common.framebuffer_type) {
729 #ifndef EFIBOOT
730 case MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED:
731 mbt->framebuffer_palette_num_colors = 256;
732
733 for (int i = 0; i < 256; i++) {
734 mbt->framebuffer_palette[i].red =
735 rasops_cmap[3 * i];
736 mbt->framebuffer_palette[i].green =
737 rasops_cmap[(3 * i) + 1];
738 mbt->framebuffer_palette[i].blue =
739 rasops_cmap[(3 * i) + 2];
740 }
741 break;
742 #endif
743 case MULTIBOOT_FRAMEBUFFER_TYPE_RGB:
744 mbt->framebuffer_red_field_position = fb->rpos;
745 mbt->framebuffer_red_mask_size = fb->rnum;
746 mbt->framebuffer_green_field_position = fb->gpos;
747 mbt->framebuffer_green_mask_size = fb->gnum;
748 mbt->framebuffer_blue_field_position = fb->bpos;
749 mbt->framebuffer_blue_mask_size = fb->bnum;
750 break;
751 default:
752 break;
753 }
754 }
755
756 return roundup(len, MULTIBOOT_TAG_ALIGN);
757 }
758
759 static size_t
760 mbi_acpi_old(struct multiboot_package *mbp, void *buf)
761 {
762 size_t len = 0;
763 struct multiboot_tag_old_acpi *mbt = buf;
764 ACPI_PHYSICAL_ADDRESS rsdp_phys = -1;
765 ACPI_RSDP_COMMON rsdp;
766 #ifdef EFIBOOT
767 const EFI_GUID acpi_table_guid = ACPI_TABLE_GUID;
768 int i;
769
770 if (ST == NULL)
771 goto out;
772
773 for (i = 0; i < ST->NumberOfTableEntries; i++) {
774 if (memcmp(&ST->ConfigurationTable[i].VendorGuid,
775 &acpi_table_guid, sizeof(acpi_table_guid)) == 0) {
776 rsdp_phys = (ACPI_PHYSICAL_ADDRESS)
777 ST->ConfigurationTable[i].VendorTable;
778 break;
779 }
780 }
781 #else
782 #ifdef notyet
783 rsdp_phys = acpi_md_OsGetRootPointer();
784 pvbcopy((void *)(vaddr_t)rsdp_phys, &rsdp, sizeof(rsdp));
785
786 /* Check ACPI 1.0 */
787 if (rsdp.Revision != 0)
788 rsdp_phys = -1;
789 #endif
790 #endif
791
792 if (rsdp_phys == -1)
793 goto out;
794
795 len = sizeof(*mbt) + sizeof(rsdp);
796 if (mbt) {
797 mbt->type = MULTIBOOT_TAG_TYPE_ACPI_OLD;
798 mbt->size = len;
799 pvbcopy((void *)(vaddr_t)rsdp_phys, mbt->rsdp, sizeof(rsdp));
800 }
801 out:
802 return roundup(len, MULTIBOOT_TAG_ALIGN);
803 }
804
805 static size_t
806 mbi_acpi_new(struct multiboot_package *mbp, void *buf)
807 {
808 size_t len = 0;
809 struct multiboot_tag_new_acpi *mbt = buf;
810 ACPI_PHYSICAL_ADDRESS rsdp_phys = -1;
811 ACPI_TABLE_RSDP rsdp;
812 #ifdef EFIBOOT
813 const EFI_GUID acpi_20_table_guid = ACPI_20_TABLE_GUID;
814 int i;
815
816 if (ST == NULL)
817 goto out;
818
819 for (i = 0; i < ST->NumberOfTableEntries; i++) {
820 if (memcmp(&ST->ConfigurationTable[i].VendorGuid,
821 &acpi_20_table_guid, sizeof(acpi_20_table_guid)) == 0) {
822 rsdp_phys = (ACPI_PHYSICAL_ADDRESS)
823 ST->ConfigurationTable[i].VendorTable;
824 break;
825 }
826 }
827 #else
828 #ifdef notyet
829 rsdp_phys = acpi_md_OsGetRootPointer();
830 pvbcopy((void *)(vaddr_t)rsdp_phys, &rsdp, sizeof(rsdp));
831
832 /* Check ACPI 2.0 */
833 if (rsdp.Revision != 2)
834 rsdp_phys = -1;
835 #endif
836 #endif
837 if (rsdp_phys == -1)
838 goto out;
839
840 len = sizeof(*mbt) + sizeof(rsdp);
841 if (mbt) {
842 mbt->type = MULTIBOOT_TAG_TYPE_ACPI_NEW;
843 mbt->size = len;
844 pvbcopy((void *)(vaddr_t)rsdp_phys, mbt->rsdp, sizeof(rsdp));
845 }
846 out:
847 return roundup(len, MULTIBOOT_TAG_ALIGN);
848 }
849
850 static size_t
851 mbi_apm(struct multiboot_package *mbp, void *buf)
852 {
853 size_t len = 0;
854 #ifdef notyet
855 struct multiboot_tag_apm *mbt = buf;
856
857 len = sizeof(*mbt):
858
859 if (mbt) {
860 mbt->type = MULTIBOOT_TAG_TYPE_A;
861 mbt->size = len;
862 mbt->version = 0;
863 mbt->cseg = 0;
864 mbt->offset = 0;
865 mbt->cseg_16 = 0;
866 mbt->dseg = 0;;
867 mbt->flags = 0;
868 mbt->cseg_len = 0;
869 mbt->cseg_16_len = 0;
870 mbt->dseg_len = 0;
871 }
872 out:
873 #endif
874 return roundup(len, MULTIBOOT_TAG_ALIGN);
875 }
876
877 static size_t
878 mbi_smbios(struct multiboot_package *mbp, void *buf)
879 {
880 size_t len = 0;
881 struct multiboot_tag_smbios *mbt = buf;
882 void *smbios_phys;
883 struct smb3hdr *smbios3_phys = NULL;
884 struct smb3hdr smbios3;
885 struct smbhdr *smbios21_phys = NULL;
886 struct smbhdr smbios21;
887 size_t smbios_len;
888 int major;
889 int minor;
890 #ifdef EFIBOOT
891 const EFI_GUID smbios3_guid = SMBIOS3_TABLE_GUID;
892 const EFI_GUID smbios21_guid = SMBIOS_TABLE_GUID;
893 int i;
894
895 if (ST == NULL)
896 goto out;
897
898 for (i = 0; i < ST->NumberOfTableEntries; i++) {
899 if (memcmp(&ST->ConfigurationTable[i].VendorGuid,
900 &smbios3_guid, sizeof(smbios3_guid)) == 0)
901 smbios3_phys = ST->ConfigurationTable[i].VendorTable;
902
903 if (memcmp(&ST->ConfigurationTable[i].VendorGuid,
904 &smbios21_guid, sizeof(smbios21_guid)) == 0)
905 smbios21_phys = ST->ConfigurationTable[i].VendorTable;
906 }
907 #else
908 char *cp;
909 char line[16];
910 const char *smbios21_anchor = "_SM_";
911 const char *smbios3_anchor = "_SM3_";
912
913 for (cp = (char *)SMBIOS_START;
914 cp < (char *)SMBIOS_END;
915 cp += sizeof(buf)) {
916 pvbcopy(cp, line, sizeof(line));
917 if (memcmp(line, smbios3_anchor, strlen(smbios3_anchor)) == 0)
918 smbios3_phys = (struct smb3hdr *)cp;
919 if (memcmp(line, smbios21_anchor, strlen(smbios21_anchor)) == 0)
920 smbios21_phys = (struct smbhdr *)cp;
921 }
922 #endif
923 if (smbios3_phys != NULL) {
924 pvbcopy(smbios3_phys, &smbios3, sizeof(smbios3));
925 smbios_len = smbios3.len;
926 major = smbios3.majrev;
927 minor = smbios3.minrev;
928 smbios_phys = smbios3_phys;
929 } else if (smbios21_phys != NULL) {
930 pvbcopy(smbios21_phys, &smbios21, sizeof(smbios21));
931 smbios_len = smbios21.len;
932 major = smbios21.majrev;
933 minor = smbios21.minrev;
934 smbios_phys = smbios21_phys;
935 } else {
936 goto out;
937 }
938
939 len = sizeof(*mbt) + smbios_len;
940 if (mbt) {
941 mbt->type = MULTIBOOT_TAG_TYPE_SMBIOS;
942 mbt->size = len;
943 mbt->major = major;
944 mbt->minor = minor;
945 pvbcopy(smbios_phys, mbt->tables, smbios_len);
946 }
947 out:
948 return roundup(len, MULTIBOOT_TAG_ALIGN);
949 }
950
951 static size_t
952 mbi_network(struct multiboot_package *mbp, void *buf)
953 {
954 size_t len = 0;
955 #ifdef notyet
956 struct multiboot_tag_network *mbt = buf;
957
958 if (saved_dhcpack == NULL || saved_dhcpack_len == 0)
959 goto out;
960
961 len = sizeof(*mbt) + saved_dhcpack_len;
962
963 if (mbt) {
964 mbt->type = MULTIBOOT_TAG_TYPE_NETWORK;
965 mbt->size = len;
966 memcpy(mbt->dhcpack, saved_dhcpack, saved_dhcpack_len);
967 }
968 out:
969 #endif
970 return roundup(len, MULTIBOOT_TAG_ALIGN);
971 }
972
973 static size_t
974 mbi_elf_sections(struct multiboot_package *mbp, void *buf)
975 {
976 size_t len = 0;
977 struct multiboot_tag_elf_sections *mbt = buf;
978 Elf_Ehdr ehdr;
979 Elf32_Ehdr *ehdr32 = NULL;
980 Elf64_Ehdr *ehdr64 = NULL;
981 uint32_t shnum, shentsize, shstrndx, shoff;
982 size_t shdr_len;
983
984 if (mbp->mbp_marks[MARK_SYM] == 0)
985 goto out;
986
987 pvbcopy((void *)mbp->mbp_marks[MARK_SYM], &ehdr, sizeof(ehdr));
988
989 /*
990 * Check this is a ELF header
991 */
992 if (memcmp(&ehdr.e_ident, ELFMAG, SELFMAG) != 0)
993 goto out;
994
995 switch (ehdr.e_ident[EI_CLASS]) {
996 case ELFCLASS32:
997 ehdr32 = (Elf32_Ehdr *)&ehdr;
998 shnum = ehdr32->e_shnum;
999 shentsize = ehdr32->e_shentsize;
1000 shstrndx = ehdr32->e_shstrndx;
1001 shoff = ehdr32->e_shoff;
1002 break;
1003 case ELFCLASS64:
1004 ehdr64 = (Elf64_Ehdr *)&ehdr;
1005 shnum = ehdr64->e_shnum;
1006 shentsize = ehdr64->e_shentsize;
1007 shstrndx = ehdr64->e_shstrndx;
1008 shoff = ehdr64->e_shoff;
1009 break;
1010 default:
1011 goto out;
1012 }
1013
1014 shdr_len = shnum * shentsize;
1015 if (shdr_len == 0)
1016 goto out;
1017
1018 len = sizeof(*mbt) + shdr_len;
1019 if (mbt) {
1020 int fd = -1;
1021 int ret = -1;
1022
1023 mbt->type = MULTIBOOT_TAG_TYPE_ELF_SECTIONS;
1024 mbt->size = len;
1025 mbt->num = shnum;
1026 mbt->entsize = shentsize;
1027 mbt->shndx = shstrndx;
1028
1029 if ((fd = open(mbp->mbp_file, 0)) == -1)
1030 goto out_read;
1031
1032 if (lseek(fd, shoff, SEEK_SET) != shoff)
1033 goto out_read;
1034
1035 if (read(fd, mbt + 1, shdr_len) != shdr_len)
1036 goto out_read;
1037
1038 ret = 0;
1039 out_read:
1040 if (fd != -1)
1041 close(fd);
1042
1043 if (ret != 0) {
1044 printf("Error reading ELF sections from %s\n",
1045 mbp->mbp_file);
1046 len = 0;
1047 }
1048 }
1049 out:
1050 return roundup(len, MULTIBOOT_TAG_ALIGN);
1051 }
1052
1053 static size_t
1054 mbi_end(struct multiboot_package *mbp, void *buf)
1055 {
1056 struct multiboot_tag *mbt = buf;
1057 size_t len = sizeof(*mbt);
1058
1059 if (mbt) {
1060 mbt->type = MULTIBOOT_TAG_TYPE_END;
1061 mbt->size = len;
1062 }
1063
1064 return roundup(len, MULTIBOOT_TAG_ALIGN);
1065 }
1066
1067 static size_t
1068 mbi_load_base_addr(struct multiboot_package *mbp, void *buf)
1069 {
1070 size_t len = 0;
1071 struct multiboot_tag_load_base_addr *mbt = buf;
1072
1073 len = sizeof(*mbt);
1074
1075 if (mbt) {
1076 mbt->type = MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR;
1077 mbt->size = len;
1078 mbt->load_base_addr = mbp->mbp_marks[MARK_START];
1079 }
1080 return roundup(len, MULTIBOOT_TAG_ALIGN);
1081 }
1082
1083 #ifdef EFIBOOT
1084 /* Set if EFI ExitBootServices was not called */
1085 static size_t
1086 mbi_efi_bs(struct multiboot_package *mbp, void *buf)
1087 {
1088 size_t len = 0;
1089 struct multiboot_tag *mbt = buf;
1090
1091 if (mbp->mbp_priv->mpp_efi_bs == NULL)
1092 goto out;
1093
1094 len = sizeof(*mbt);
1095
1096 if (mbt) {
1097 mbt->type = MULTIBOOT_TAG_TYPE_EFI_BS;
1098 mbt->size = len;
1099 }
1100
1101 out:
1102 return roundup(len, MULTIBOOT_TAG_ALIGN);
1103 }
1104
1105
1106 static size_t
1107 mbi_efi_mmap(struct multiboot_package *mbp, void *buf)
1108 {
1109 size_t len = 0;
1110 struct multiboot_tag_efi_mmap *mbt = buf;
1111 size_t memmap_len;
1112
1113 if (btinfo_efimemmap == NULL)
1114 goto out;
1115
1116 memmap_len = btinfo_efimemmap->num * btinfo_efimemmap->size;
1117 len = sizeof(*mbt) + memmap_len;
1118
1119 if (mbt) {
1120 mbt->type = MULTIBOOT_TAG_TYPE_EFI_MMAP;
1121 mbt->size = len;
1122 mbt->descr_size = btinfo_efimemmap->size;
1123 mbt->descr_vers = btinfo_efimemmap->version;
1124 memcpy(mbt + 1, btinfo_efimemmap->memmap, memmap_len);
1125 }
1126
1127 out:
1128 return roundup(len, MULTIBOOT_TAG_ALIGN);
1129 }
1130
1131
1132
1133 #ifndef __LP64__
1134 static size_t
1135 mbi_efi32_ih(struct multiboot_package *mbp, void *buf)
1136 {
1137 size_t len = 0;
1138 struct multiboot_tag_efi32_ih *mbt = buf;
1139
1140 len = sizeof(*mbt);
1141
1142 if (mbt) {
1143 mbt->type = MULTIBOOT_TAG_TYPE_EFI32_IH;
1144 mbt->size = len;
1145 mbt->pointer = (multiboot_uint32_t)IH;
1146 }
1147 return roundup(len, MULTIBOOT_TAG_ALIGN);
1148 }
1149
1150 static size_t
1151 mbi_efi32(struct multiboot_package *mbp, void *buf)
1152 {
1153 size_t len = 0;
1154 struct multiboot_tag_efi32 *mbt = buf;
1155
1156 len = sizeof(*mbt);
1157
1158 if (mbt) {
1159 mbt->type = MULTIBOOT_TAG_TYPE_EFI32;
1160 mbt->size = len;
1161 mbt->pointer = (multiboot_uint32_t)ST;
1162 }
1163 return roundup(len, MULTIBOOT_TAG_ALIGN);
1164 }
1165 #endif
1166
1167 #ifdef __LP64__
1168 static size_t
1169 mbi_efi64_ih(struct multiboot_package *mbp, void *buf)
1170 {
1171 size_t len = 0;
1172 struct multiboot_tag_efi64_ih *mbt = buf;
1173
1174 len = sizeof(*mbt);
1175
1176 if (mbt) {
1177 mbt->type = MULTIBOOT_TAG_TYPE_EFI64_IH;
1178 mbt->size = len;
1179 mbt->pointer = (multiboot_uint64_t)IH;
1180 }
1181 return roundup(len, MULTIBOOT_TAG_ALIGN);
1182 }
1183
1184 static size_t
1185 mbi_efi64(struct multiboot_package *mbp, void *buf)
1186 {
1187 size_t len = 0;
1188 struct multiboot_tag_efi64 *mbt = buf;
1189
1190 len = sizeof(*mbt);
1191
1192 if (mbt) {
1193 mbt->type = MULTIBOOT_TAG_TYPE_EFI64;
1194 mbt->size = len;
1195 mbt->pointer = (multiboot_uint64_t)ST;
1196 }
1197 return roundup(len, MULTIBOOT_TAG_ALIGN);
1198 }
1199 #endif /* __LP64__ */
1200 #endif /* EFIBOOT */
1201
1202 static bool
1203 is_tag_required(struct multiboot_package *mbp, uint16_t tag)
1204 {
1205 bool ret = false;
1206 int i;
1207 struct multiboot_header_tag_information_request *info_req;
1208 size_t nreq;
1209
1210 info_req = mbp->mbp_priv->mpp_info_req;
1211
1212 if (info_req == NULL)
1213 goto out;
1214
1215 if (info_req->flags & MULTIBOOT_HEADER_TAG_OPTIONAL)
1216 goto out;
1217
1218 nreq = (info_req->size - sizeof(*info_req))
1219 / sizeof(info_req->requests[0]);
1220
1221 for (i = 0; i < nreq; i++) {
1222 if (info_req->requests[i] == tag) {
1223 ret = true;
1224 break;
1225 }
1226 }
1227
1228 out:
1229 return ret;
1230 }
1231
1232 static int
1233 mbi_dispatch(struct multiboot_package *mbp, uint16_t type,
1234 char *bp, size_t *total_len)
1235 {
1236 int ret = 0;
1237 size_t len = 0;
1238
1239 switch (type) {
1240 case MULTIBOOT_TAG_TYPE_END:
1241 len = mbi_end(mbp, bp);
1242 break;
1243 case MULTIBOOT_TAG_TYPE_CMDLINE:
1244 len = mbi_cmdline(mbp, bp);
1245 break;
1246 case MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME:
1247 len = mbi_boot_loader_name(mbp, bp);
1248 break;
1249 case MULTIBOOT_TAG_TYPE_MODULE:
1250 len = mbi_modules(mbp, bp);
1251 break;
1252 case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO:
1253 len = mbi_basic_meminfo(mbp, bp);
1254 break;
1255 case MULTIBOOT_TAG_TYPE_BOOTDEV:
1256 len = mbi_bootdev(mbp, bp);
1257 break;
1258 case MULTIBOOT_TAG_TYPE_MMAP:
1259 len = mbi_mmap(mbp, bp);
1260 break;
1261 case MULTIBOOT_TAG_TYPE_VBE:
1262 len = mbi_vbe(mbp, bp);
1263 break;
1264 case MULTIBOOT_TAG_TYPE_FRAMEBUFFER:
1265 len = mbi_framebuffer(mbp, bp);
1266 break;
1267 case MULTIBOOT_TAG_TYPE_ACPI_OLD:
1268 len = mbi_acpi_old(mbp, bp);
1269 break;
1270 case MULTIBOOT_TAG_TYPE_ACPI_NEW:
1271 len = mbi_acpi_new(mbp, bp);
1272 break;
1273 case MULTIBOOT_TAG_TYPE_ELF_SECTIONS:
1274 len = mbi_elf_sections(mbp, bp);
1275 break;
1276 case MULTIBOOT_TAG_TYPE_APM:
1277 len = mbi_apm(mbp, bp);
1278 break;
1279 case MULTIBOOT_TAG_TYPE_SMBIOS:
1280 len = mbi_smbios(mbp, bp);
1281 break;
1282 case MULTIBOOT_TAG_TYPE_NETWORK:
1283 len = mbi_network(mbp, bp);
1284 break;
1285 #ifdef EFIBOOT
1286 case MULTIBOOT_TAG_TYPE_EFI_MMAP:
1287 len = mbi_efi_mmap(mbp, bp);
1288 break;
1289 case MULTIBOOT_TAG_TYPE_EFI_BS:
1290 len = mbi_efi_bs(mbp, bp);
1291 break;
1292 #ifndef __LP64__
1293 case MULTIBOOT_TAG_TYPE_EFI32_IH:
1294 len = mbi_efi32_ih(mbp, bp);
1295 break;
1296 case MULTIBOOT_TAG_TYPE_EFI32:
1297 len = mbi_efi32(mbp, bp);
1298 break;
1299 #else /* __LP64__ */
1300 case MULTIBOOT_TAG_TYPE_EFI64_IH:
1301 len = mbi_efi64_ih(mbp, bp);
1302 break;
1303 case MULTIBOOT_TAG_TYPE_EFI64:
1304 len = mbi_efi64(mbp, bp);
1305 break;
1306 #endif /* __LP64__ */
1307 #endif /* EFIBOOT */
1308 case MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR:
1309 len = mbi_load_base_addr(mbp, bp);
1310 break;
1311 default:
1312 len = 0;
1313 break;
1314 }
1315
1316 if (len == 0 && is_tag_required(mbp, type))
1317 ret = -1;
1318
1319 *total_len += len;
1320 return ret;
1321 }
1322
1323 static int
1324 exec_multiboot2(struct multiboot_package *mbp)
1325 {
1326 size_t len, alen;
1327 char *mbi = NULL;
1328 struct multiboot_package_priv *mpp = mbp->mbp_priv;
1329 uint16_t tags[] = {
1330 MULTIBOOT_TAG_TYPE_CMDLINE,
1331 MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME,
1332 MULTIBOOT_TAG_TYPE_MODULE,
1333 MULTIBOOT_TAG_TYPE_BASIC_MEMINFO,
1334 MULTIBOOT_TAG_TYPE_BOOTDEV,
1335 MULTIBOOT_TAG_TYPE_VBE,
1336 MULTIBOOT_TAG_TYPE_FRAMEBUFFER,
1337 MULTIBOOT_TAG_TYPE_ELF_SECTIONS,
1338 MULTIBOOT_TAG_TYPE_APM,
1339 MULTIBOOT_TAG_TYPE_SMBIOS,
1340 MULTIBOOT_TAG_TYPE_ACPI_OLD,
1341 MULTIBOOT_TAG_TYPE_ACPI_NEW,
1342 MULTIBOOT_TAG_TYPE_NETWORK,
1343 MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR,
1344 #ifdef EFIBOOT
1345 MULTIBOOT_TAG_TYPE_EFI_BS,
1346 #ifndef __LP64__
1347 MULTIBOOT_TAG_TYPE_EFI32,
1348 MULTIBOOT_TAG_TYPE_EFI32_IH,
1349 #else
1350 MULTIBOOT_TAG_TYPE_EFI64,
1351 MULTIBOOT_TAG_TYPE_EFI64_IH,
1352 #endif /* __LP64__ */
1353 /*
1354 * EFI_MMAP and MMAP at the end so that they
1355 * catch page allocation made for other tags.
1356 */
1357 MULTIBOOT_TAG_TYPE_EFI_MMAP,
1358 #endif /* EFIGOOT */
1359 MULTIBOOT_TAG_TYPE_MMAP,
1360 MULTIBOOT_TAG_TYPE_END, /* Must be last */
1361 };
1362 physaddr_t entry;
1363 int i;
1364
1365 BI_ALLOC(BTINFO_MAX);
1366
1367 /* set new video mode if text mode was not requested */
1368 if (mpp->mpp_framebuffer == NULL ||
1369 mpp->mpp_framebuffer->depth != 0)
1370 vbe_commit();
1371
1372 len = 2 * sizeof(multiboot_uint32_t);
1373 for (i = 0; i < sizeof(tags) / sizeof(*tags); i++) {
1374 if (mbi_dispatch(mbp, tags[i], NULL, &len) != 0)
1375 goto fail;
1376 }
1377
1378 mpp->mpp_mbi_len = len + MULTIBOOT_TAG_ALIGN;
1379 mpp->mpp_mbi = alloc(mpp->mpp_mbi_len);
1380 mbi = (char *)roundup((vaddr_t)mpp->mpp_mbi, MULTIBOOT_TAG_ALIGN);
1381
1382 alen = 2 * sizeof(multiboot_uint32_t);
1383 for (i = 0; i < sizeof(tags) / sizeof(*tags); i++) {
1384 if (mbi_dispatch(mbp, tags[i], mbi + alen, &alen) != 0)
1385 goto fail;
1386
1387 /*
1388 * It may shrink because of failure when filling
1389 * structures, but it should not grow.
1390 */
1391 if (alen > len)
1392 panic("multiboot2 info size mismatch");
1393 }
1394
1395
1396 ((multiboot_uint32_t *)mbi)[0] = alen; /* total size */
1397 ((multiboot_uint32_t *)mbi)[1] = 0; /* reserved */
1398
1399 #if 0
1400 for (i = 0; i < len; i += 16) {
1401 printf("%p ", mbi + i);
1402 for (int j = 0; j < 16; j++)
1403 printf("%s%s%x",
1404 (i+j) % 4 ? "" : " ",
1405 (unsigned char)mbi[i+j] < 0x10 ? "0" : "",
1406 (unsigned char)(mbi[i+j]));
1407 printf("\n");
1408 }
1409 #endif
1410
1411 printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n",
1412 mbp->mbp_marks[MARK_ENTRY],
1413 mbp->mbp_marks[MARK_NSYM],
1414 mbp->mbp_marks[MARK_SYM],
1415 mbp->mbp_marks[MARK_END]);
1416
1417 #ifdef MULTIBOOT2_DEBUG
1418 multiboot2_info_dump(MULTIBOOT2_BOOTLOADER_MAGIC, mbi);
1419 #endif /* MULTIBOOT2_DEBUG */
1420
1421 entry = mbp->mbp_marks[MARK_ENTRY];
1422
1423 if (mpp->mpp_entry)
1424 entry = mpp->mpp_entry->entry_addr;
1425 #ifdef EFIBOOT
1426 #ifdef __LP64__
1427 if (mpp->mpp_entry_elf64)
1428 entry = mpp->mpp_entry_elf64->entry_addr
1429 + efi_loadaddr;
1430 #else
1431 if (mpp->mpp_entry_elf32)
1432 entry = mpp->mpp_entry_elf32->entry_addr
1433 + efi_loadaddr;
1434 #endif /* __LP64__ */
1435 if (mpp->mpp_efi_bs == NULL)
1436 efi_cleanup();
1437 #endif /* EFIBOOT */
1438
1439 /* Does not return */
1440 multiboot(entry, vtophys(mbi),
1441 x86_trunc_page(mbp->mbp_basemem * 1024),
1442 MULTIBOOT2_BOOTLOADER_MAGIC);
1443 fail:
1444 return -1;
1445 }
1446
1447 static void
1448 cleanup_multiboot2(struct multiboot_package *mbp)
1449 {
1450 if (mbp->mbp_header)
1451 dealloc(mbp->mbp_header, mbp->mbp_header->header_length);
1452 if (mbp->mbp_priv && mbp->mbp_priv->mpp_mbi)
1453 dealloc(mbp->mbp_priv->mpp_mbi, mbp->mbp_priv->mpp_mbi_len);
1454 if (mbp->mbp_priv)
1455 dealloc(mbp->mbp_priv, sizeof(*mbp->mbp_priv));
1456
1457 dealloc(mbp, sizeof(*mbp));
1458
1459 return;
1460 }
1461
1462 static bool
1463 is_header_required(struct multiboot_header_tag *mbt)
1464 {
1465 bool ret = false;
1466
1467 if (mbt == NULL)
1468 goto out;
1469
1470 if (mbt->flags & MULTIBOOT_HEADER_TAG_OPTIONAL)
1471 goto out;
1472
1473 ret = true;
1474 out:
1475 return ret;
1476 }
1477
1478 #define NEXT_HEADER(mbt) ((struct multiboot_header_tag *) \
1479 ((char *)mbt + roundup(mbt->size, MULTIBOOT_HEADER_ALIGN)))
1480
1481 struct multiboot_package *
1482 probe_multiboot2(const char *path)
1483 {
1484 int fd = -1;
1485 size_t i;
1486 char buf[MULTIBOOT_SEARCH + sizeof(struct multiboot_header)];
1487 ssize_t readen;
1488 struct multiboot_package *mbp = NULL;
1489 struct multiboot_header *mbh;
1490 struct multiboot_header_tag *mbt;
1491 size_t mbh_len = 0;
1492
1493 if ((fd = open(path, 0)) == -1)
1494 goto out;
1495
1496 readen = read(fd, buf, sizeof(buf));
1497 if (readen < sizeof(struct multiboot_header))
1498 goto out;
1499
1500 for (i = 0; i < readen; i += MULTIBOOT_HEADER_ALIGN) {
1501 mbh = (struct multiboot_header *)(buf + i);
1502
1503 if (mbh->magic != MULTIBOOT2_HEADER_MAGIC)
1504 continue;
1505
1506 if (mbh->architecture != MULTIBOOT_ARCHITECTURE_I386)
1507 continue;
1508
1509 if (mbh->magic + mbh->architecture +
1510 mbh->header_length + mbh->checksum)
1511 continue;
1512 mbh_len = mbh->header_length;
1513
1514 mbp = alloc(sizeof(*mbp));
1515 mbp->mbp_version = 2;
1516 mbp->mbp_file = path;
1517 mbp->mbp_header = alloc(mbh_len);
1518 mbp->mbp_priv = alloc(sizeof(*mbp->mbp_priv));
1519 memset(mbp->mbp_priv, 0, sizeof (*mbp->mbp_priv));
1520 mbp->mbp_probe = *probe_multiboot2;
1521 mbp->mbp_exec = *exec_multiboot2;
1522 mbp->mbp_cleanup = *cleanup_multiboot2;
1523
1524 break;
1525 }
1526
1527 if (mbp == NULL)
1528 goto out;
1529
1530 if (lseek(fd, i, SEEK_SET) != i) {
1531 printf("lseek failed");
1532 mbp->mbp_cleanup(mbp);
1533 mbp = NULL;
1534 goto out;
1535 }
1536
1537 mbh = mbp->mbp_header;
1538 if (read(fd, mbh, mbh_len) != mbh_len) {
1539 printf("read failed");
1540 mbp->mbp_cleanup(mbp);
1541 mbp = NULL;
1542 goto out;
1543 }
1544
1545 for (mbt = (struct multiboot_header_tag *)(mbh + 1);
1546 (char *)mbt - (char *)mbh < mbh_len;
1547 mbt = NEXT_HEADER(mbt)) {
1548
1549 switch(mbt->type) {
1550 case MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST:
1551 mbp->mbp_priv->mpp_info_req = (void *)mbt;
1552 break;
1553 case MULTIBOOT_HEADER_TAG_ADDRESS:
1554 mbp->mbp_priv->mpp_address = (void *)mbt;
1555 break;
1556 case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS:
1557 mbp->mbp_priv->mpp_entry = (void *)mbt;
1558 break;
1559 case MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS:
1560 mbp->mbp_priv->mpp_console = (void *)mbt;
1561
1562 case MULTIBOOT_HEADER_TAG_FRAMEBUFFER:
1563 mbp->mbp_priv->mpp_framebuffer = (void *)mbt;
1564 break;
1565 case MULTIBOOT_HEADER_TAG_MODULE_ALIGN:
1566 mbp->mbp_priv->mpp_module_align = (void *)mbt;
1567 break;
1568 #ifdef EFIBOOT
1569 case MULTIBOOT_HEADER_TAG_EFI_BS:
1570 mbp->mbp_priv->mpp_efi_bs = (void *)mbt;
1571 break;
1572 case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI32:
1573 mbp->mbp_priv->mpp_entry_elf32 = (void *)mbt;
1574 break;
1575 case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64:
1576 mbp->mbp_priv->mpp_entry_elf64 = (void *)mbt;
1577 break;
1578 #endif
1579 case MULTIBOOT_HEADER_TAG_RELOCATABLE:
1580 mbp->mbp_priv->mpp_relocatable = (void *)mbt;
1581 break;
1582 case MULTIBOOT_HEADER_TAG_END: /* FALLTHROUGH */
1583 default:
1584 break;
1585 }
1586 }
1587
1588 #ifdef MULTIBOOT2_DEBUG
1589 multiboot2_header_dump(mbp);
1590 #endif /* MULTIBOOT2_DEBUG */
1591
1592 /*
1593 * multiboot header fully supported
1594 * MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST
1595 * MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS
1596 * MULTIBOOT_HEADER_TAG_MODULE_ALIGN (we always load as page aligned)
1597 * MULTIBOOT_HEADER_TAG_EFI_BS
1598 * MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI32
1599 * MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64
1600 * MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS (we always have a console)
1601 *
1602 * Not supported:
1603 * MULTIBOOT_HEADER_TAG_ADDRESS
1604 * MULTIBOOT_HEADER_TAG_FRAMEBUFFER (but spec says it is onty a hint)
1605 * MULTIBOOT_HEADER_TAG_RELOCATABLE
1606 */
1607
1608 if (is_header_required((void *)mbp->mbp_priv->mpp_address)) {
1609 printf("Unsupported multiboot address header\n");
1610 mbp->mbp_cleanup(mbp);
1611 mbp = NULL;
1612 goto out;
1613 }
1614
1615 #ifdef EFIBOOT
1616 /*
1617 * We do not fully support the relocatable header, but
1618 * at least we honour the alignment request. Xen requires
1619 * that to boot.
1620 */
1621 struct multiboot_header_tag_relocatable *reloc =
1622 mbp->mbp_priv->mpp_relocatable;
1623 if (reloc)
1624 efi_loadaddr = roundup(efi_loadaddr, reloc->align);
1625 #endif
1626
1627 if (is_header_required((void *)mbp->mbp_priv->mpp_relocatable)) {
1628 printf("Unsupported multiboot relocatable header\n");
1629 mbp->mbp_cleanup(mbp);
1630 mbp = NULL;
1631 goto out;
1632 }
1633
1634 out:
1635
1636 if (fd != -1)
1637 close(fd);
1638
1639 return mbp;
1640 }
1641
1642 #endif /* NO_MULTIBOOT2 */
1643