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