exec_multiboot1.c revision 1.2 1 1.2 manu /* $NetBSD: exec_multiboot1.c,v 1.2 2019/10/18 01:04:24 manu Exp $ */
2 1.1 manu
3 1.1 manu /*
4 1.1 manu * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 1.1 manu * All rights reserved.
6 1.1 manu *
7 1.1 manu * Redistribution and use in source and binary forms, with or without
8 1.1 manu * modification, are permitted provided that the following conditions
9 1.1 manu * are met:
10 1.1 manu * 1. Redistributions of source code must retain the above copyright
11 1.1 manu * notice, this list of conditions and the following disclaimer.
12 1.1 manu * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 manu * notice, this list of conditions and the following disclaimer in the
14 1.1 manu * documentation and/or other materials provided with the distribution.
15 1.1 manu *
16 1.1 manu * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 manu * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 manu * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 manu * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 manu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 manu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 manu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 manu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 manu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 manu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 manu * POSSIBILITY OF SUCH DAMAGE.
27 1.1 manu */
28 1.1 manu
29 1.1 manu #include <sys/param.h>
30 1.1 manu #include <sys/reboot.h>
31 1.1 manu
32 1.1 manu #include <i386/multiboot.h>
33 1.1 manu
34 1.1 manu #include <lib/libsa/stand.h>
35 1.1 manu #include <lib/libkern/libkern.h>
36 1.1 manu
37 1.1 manu #include "loadfile.h"
38 1.1 manu #include "libi386.h"
39 1.1 manu #include "bootinfo.h"
40 1.1 manu #include "bootmod.h"
41 1.1 manu #include "vbe.h"
42 1.1 manu
43 1.1 manu extern struct btinfo_modulelist *btinfo_modulelist;
44 1.1 manu
45 1.1 manu static int
46 1.1 manu exec_multiboot1(struct multiboot_package *mbp)
47 1.1 manu {
48 1.1 manu struct multiboot_info *mbi;
49 1.1 manu struct multiboot_module *mbm;
50 1.1 manu int i, len;
51 1.1 manu char *cmdline;
52 1.1 manu struct bi_modulelist_entry *bim;
53 1.1 manu
54 1.1 manu mbi = alloc(sizeof(struct multiboot_info));
55 1.1 manu mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY;
56 1.1 manu
57 1.1 manu mbi->mi_mem_upper = mbp->mbp_extmem;
58 1.1 manu mbi->mi_mem_lower = mbp->mbp_basemem;
59 1.1 manu
60 1.1 manu if (mbp->mbp_args) {
61 1.1 manu mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE;
62 1.1 manu len = strlen(mbp->mbp_file) + 1 + strlen(mbp->mbp_args) + 1;
63 1.1 manu cmdline = alloc(len);
64 1.1 manu snprintf(cmdline, len, "%s %s", mbp->mbp_file, mbp->mbp_args);
65 1.1 manu mbi->mi_cmdline = (char *) vtophys(cmdline);
66 1.1 manu }
67 1.1 manu
68 1.1 manu /* pull in any modules if necessary */
69 1.1 manu if (btinfo_modulelist) {
70 1.1 manu mbm = alloc(sizeof(struct multiboot_module) *
71 1.1 manu btinfo_modulelist->num);
72 1.1 manu
73 1.1 manu bim = (struct bi_modulelist_entry *)
74 1.1 manu (((char *) btinfo_modulelist) +
75 1.1 manu sizeof(struct btinfo_modulelist));
76 1.1 manu for (i = 0; i < btinfo_modulelist->num; i++) {
77 1.1 manu mbm[i].mmo_start = bim->base;
78 1.1 manu mbm[i].mmo_end = bim->base + bim->len;
79 1.1 manu mbm[i].mmo_string = (char *)vtophys(bim->path);
80 1.1 manu mbm[i].mmo_reserved = 0;
81 1.1 manu bim++;
82 1.1 manu }
83 1.1 manu mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS;
84 1.1 manu mbi->mi_mods_count = btinfo_modulelist->num;
85 1.1 manu mbi->mi_mods_addr = vtophys(mbm);
86 1.1 manu }
87 1.1 manu
88 1.1 manu #ifdef DEBUG
89 1.1 manu printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n",
90 1.1 manu mbp->mbp_marks[MARK_ENTRY],
91 1.1 manu mbp->mbp_marks[MARK_NSYM],
92 1.1 manu mbp->mbp_marks[MARK_SYM],
93 1.1 manu mbp->mbp_marks[MARK_END]);
94 1.1 manu #endif
95 1.1 manu
96 1.1 manu /* Does not return */
97 1.1 manu multiboot(mbp->mbp_marks[MARK_ENTRY], vtophys(mbi),
98 1.1 manu x86_trunc_page(mbi->mi_mem_lower * 1024), MULTIBOOT_INFO_MAGIC);
99 1.1 manu
100 1.1 manu return 0;
101 1.1 manu }
102 1.1 manu
103 1.1 manu static void
104 1.1 manu cleanup_multiboot1(struct multiboot_package *mbp)
105 1.1 manu {
106 1.1 manu dealloc(mbp->mbp_header, sizeof(*mbp->mbp_header));
107 1.1 manu dealloc(mbp, sizeof(*mbp));
108 1.1 manu
109 1.1 manu return;
110 1.1 manu }
111 1.1 manu
112 1.1 manu
113 1.1 manu struct multiboot_package *
114 1.1 manu probe_multiboot1(const char *path)
115 1.1 manu {
116 1.1 manu int fd = -1;
117 1.1 manu size_t i;
118 1.1 manu char buf[8192 + sizeof(struct multiboot_header)];
119 1.1 manu ssize_t readen;
120 1.1 manu struct multiboot_package *mbp = NULL;
121 1.1 manu
122 1.1 manu if ((fd = open(path, 0)) == -1)
123 1.1 manu goto out;
124 1.1 manu
125 1.1 manu readen = read(fd, buf, sizeof(buf));
126 1.1 manu if (readen < sizeof(struct multiboot_header))
127 1.1 manu goto out;
128 1.1 manu
129 1.2 manu for (i = 0; i < readen; i += 4) {
130 1.1 manu struct multiboot_header *mbh;
131 1.1 manu
132 1.1 manu mbh = (struct multiboot_header *)(buf + i);
133 1.1 manu
134 1.1 manu if (mbh->mh_magic != MULTIBOOT_HEADER_MAGIC)
135 1.1 manu continue;
136 1.1 manu
137 1.1 manu if (mbh->mh_magic + mbh->mh_flags + mbh->mh_checksum)
138 1.1 manu continue;
139 1.1 manu
140 1.1 manu mbp = alloc(sizeof(*mbp));
141 1.1 manu mbp->mbp_version = 1;
142 1.1 manu mbp->mbp_file = path;
143 1.1 manu mbp->mbp_header = alloc(sizeof(*mbp->mbp_header));
144 1.1 manu mbp->mbp_probe = *probe_multiboot1;
145 1.1 manu mbp->mbp_exec = *exec_multiboot1;
146 1.1 manu mbp->mbp_cleanup = *cleanup_multiboot1;
147 1.1 manu
148 1.1 manu memcpy(mbp->mbp_header, mbh, sizeof(*mbp->mbp_header));
149 1.1 manu
150 1.1 manu goto out;
151 1.1 manu
152 1.1 manu }
153 1.1 manu
154 1.1 manu out:
155 1.1 manu if (fd != -1)
156 1.1 manu close(fd);
157 1.1 manu
158 1.1 manu return mbp;
159 1.1 manu }
160