biosboot.c revision 1.1 1 1.1 jakllsch /* $NetBSD: biosboot.c,v 1.1 2011/01/06 01:08:48 jakllsch Exp $ */
2 1.1 jakllsch
3 1.1 jakllsch /*
4 1.1 jakllsch * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 1.1 jakllsch * All rights reserved.
6 1.1 jakllsch *
7 1.1 jakllsch * This code is derived from software contributed to the NetBSD Foundation
8 1.1 jakllsch * by Mike M. Volokhov. Development of this software was supported by the
9 1.1 jakllsch * Google Summer of Code program.
10 1.1 jakllsch * The GSoC project was mentored by Allen Briggs and Joerg Sonnenberger.
11 1.1 jakllsch *
12 1.1 jakllsch * Redistribution and use in source and binary forms, with or without
13 1.1 jakllsch * modification, are permitted provided that the following conditions
14 1.1 jakllsch * are met:
15 1.1 jakllsch * 1. Redistributions of source code must retain the above copyright
16 1.1 jakllsch * notice, this list of conditions and the following disclaimer.
17 1.1 jakllsch * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 jakllsch * notice, this list of conditions and the following disclaimer in the
19 1.1 jakllsch * documentation and/or other materials provided with the distribution.
20 1.1 jakllsch *
21 1.1 jakllsch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 1.1 jakllsch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.1 jakllsch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1 jakllsch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 1.1 jakllsch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 jakllsch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 jakllsch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 jakllsch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 jakllsch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 jakllsch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 jakllsch * POSSIBILITY OF SUCH DAMAGE.
32 1.1 jakllsch */
33 1.1 jakllsch
34 1.1 jakllsch #include <sys/cdefs.h>
35 1.1 jakllsch #ifdef __RCSID
36 1.1 jakllsch __RCSID("$NetBSD: biosboot.c,v 1.1 2011/01/06 01:08:48 jakllsch Exp $");
37 1.1 jakllsch #endif
38 1.1 jakllsch
39 1.1 jakllsch #include <sys/stat.h>
40 1.1 jakllsch #include <sys/types.h>
41 1.1 jakllsch #include <sys/ioctl.h>
42 1.1 jakllsch #include <sys/disk.h>
43 1.1 jakllsch #include <sys/param.h>
44 1.1 jakllsch #include <sys/bootblock.h>
45 1.1 jakllsch
46 1.1 jakllsch #include <err.h>
47 1.1 jakllsch #include <fcntl.h>
48 1.1 jakllsch #include <inttypes.h>
49 1.1 jakllsch #include <paths.h>
50 1.1 jakllsch #include <stddef.h>
51 1.1 jakllsch #include <stdio.h>
52 1.1 jakllsch #include <stdlib.h>
53 1.1 jakllsch #include <string.h>
54 1.1 jakllsch #include <unistd.h>
55 1.1 jakllsch #include <util.h>
56 1.1 jakllsch
57 1.1 jakllsch #include "map.h"
58 1.1 jakllsch #include "gpt.h"
59 1.1 jakllsch
60 1.1 jakllsch #define DEFAULT_BOOTDIR "/usr/mdec"
61 1.1 jakllsch #define DEFAULT_BOOTCODE "mbr_gpt"
62 1.1 jakllsch
63 1.1 jakllsch static daddr_t start;
64 1.1 jakllsch static uint64_t size;
65 1.1 jakllsch
66 1.1 jakllsch static char *bootpath;
67 1.1 jakllsch static unsigned int entry;
68 1.1 jakllsch
69 1.1 jakllsch const uuid_t uuid_mbr_guid_default = MBR_GPT_GUID_DEFAULT;
70 1.1 jakllsch
71 1.1 jakllsch const char biosbootmsg[] = "biosboot [-c bootcode] [-i index] device ...";
72 1.1 jakllsch
73 1.1 jakllsch static void
74 1.1 jakllsch usage_biosboot(void)
75 1.1 jakllsch {
76 1.1 jakllsch fprintf(stderr, "usage: %s %s\n", getprogname(), biosbootmsg);
77 1.1 jakllsch exit(1);
78 1.1 jakllsch }
79 1.1 jakllsch
80 1.1 jakllsch static struct mbr*
81 1.1 jakllsch read_boot(void)
82 1.1 jakllsch {
83 1.1 jakllsch int bfd, ret = 0;
84 1.1 jakllsch struct mbr *buf;
85 1.1 jakllsch struct stat st;
86 1.1 jakllsch uuid_t uuid_patch_magic;
87 1.1 jakllsch
88 1.1 jakllsch /* XXX how to do the following better? */
89 1.1 jakllsch if (bootpath == NULL) {
90 1.1 jakllsch bootpath = strdup(DEFAULT_BOOTDIR "/" DEFAULT_BOOTCODE);
91 1.1 jakllsch } else {
92 1.1 jakllsch if (strchr(bootpath, '/') == 0) {
93 1.1 jakllsch char *p;
94 1.1 jakllsch if ((p = strdup(bootpath)) == NULL)
95 1.1 jakllsch err(1, "Malloc failed");
96 1.1 jakllsch free(bootpath);
97 1.1 jakllsch (void)asprintf(&bootpath, "%s/%s", DEFAULT_BOOTDIR, p);
98 1.1 jakllsch free(p);
99 1.1 jakllsch }
100 1.1 jakllsch }
101 1.1 jakllsch if (bootpath == NULL)
102 1.1 jakllsch err(1, "Malloc failed");
103 1.1 jakllsch
104 1.1 jakllsch if ((buf = malloc((size_t)secsz)) == NULL)
105 1.1 jakllsch err(1, "Malloc failed");
106 1.1 jakllsch
107 1.1 jakllsch if ((bfd = open(bootpath, O_RDONLY)) < 0 || fstat(bfd, &st) == -1) {
108 1.1 jakllsch warn("%s", bootpath);
109 1.1 jakllsch goto fail;
110 1.1 jakllsch }
111 1.1 jakllsch
112 1.1 jakllsch if (st.st_size != secsz) {
113 1.1 jakllsch warnx("%s: the bootcode does not match '%s' sector size",
114 1.1 jakllsch bootpath, device_name);
115 1.1 jakllsch goto fail;
116 1.1 jakllsch }
117 1.1 jakllsch
118 1.1 jakllsch if (read(bfd, buf, secsz) != st.st_size) {
119 1.1 jakllsch warn("%s", bootpath);
120 1.1 jakllsch goto fail;
121 1.1 jakllsch }
122 1.1 jakllsch
123 1.1 jakllsch if (le32toh(buf->mbr_sig) != MBR_SIG) {
124 1.1 jakllsch warnx("%s: invalid MBR magic", bootpath);
125 1.1 jakllsch goto fail;
126 1.1 jakllsch }
127 1.1 jakllsch
128 1.1 jakllsch uuid_dec_le(&buf->mbr_code[MBR_GPT_GUID_OFFSET], &uuid_patch_magic);
129 1.1 jakllsch
130 1.1 jakllsch /*
131 1.1 jakllsch * The loader have to contain some magic in patchable area,
132 1.1 jakllsch * such we can be sure that we won't trash something important
133 1.1 jakllsch */
134 1.1 jakllsch if (!uuid_equal(&uuid_patch_magic, &uuid_mbr_guid_default, NULL)) {
135 1.1 jakllsch warnx("%s: the bootcode does not support required options",
136 1.1 jakllsch bootpath);
137 1.1 jakllsch goto fail;
138 1.1 jakllsch }
139 1.1 jakllsch
140 1.1 jakllsch ret++;
141 1.1 jakllsch
142 1.1 jakllsch fail:
143 1.1 jakllsch if (bfd >= 0)
144 1.1 jakllsch close(bfd);
145 1.1 jakllsch if (ret == 0) {
146 1.1 jakllsch free(buf);
147 1.1 jakllsch buf = NULL;
148 1.1 jakllsch }
149 1.1 jakllsch return buf;
150 1.1 jakllsch }
151 1.1 jakllsch
152 1.1 jakllsch static void
153 1.1 jakllsch biosboot(int fd)
154 1.1 jakllsch {
155 1.1 jakllsch map_t *gpt, *tpg;
156 1.1 jakllsch map_t *tbl, *lbt;
157 1.1 jakllsch map_t *mbrmap, *m;
158 1.1 jakllsch struct mbr *mbr, *bootcode;
159 1.1 jakllsch struct gpt_ent *pp;
160 1.1 jakllsch uuid_t buuid;
161 1.1 jakllsch
162 1.1 jakllsch /*
163 1.1 jakllsch * Parse and validate partition maps
164 1.1 jakllsch */
165 1.1 jakllsch gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
166 1.1 jakllsch if (gpt == NULL) {
167 1.1 jakllsch warnx("%s: error: no primary GPT header; run create or recover",
168 1.1 jakllsch device_name);
169 1.1 jakllsch return;
170 1.1 jakllsch }
171 1.1 jakllsch
172 1.1 jakllsch tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
173 1.1 jakllsch if (tpg == NULL) {
174 1.1 jakllsch warnx("%s: error: no secondary GPT header; run recover",
175 1.1 jakllsch device_name);
176 1.1 jakllsch return;
177 1.1 jakllsch }
178 1.1 jakllsch
179 1.1 jakllsch tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
180 1.1 jakllsch lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
181 1.1 jakllsch if (tbl == NULL || lbt == NULL) {
182 1.1 jakllsch warnx("%s: error: run recover -- trust me", device_name);
183 1.1 jakllsch return;
184 1.1 jakllsch }
185 1.1 jakllsch
186 1.1 jakllsch mbrmap = map_find(MAP_TYPE_PMBR);
187 1.1 jakllsch if (mbrmap == NULL || mbrmap->map_start != 0) {
188 1.1 jakllsch warnx("%s: error: no valid Protective MBR found", device_name);
189 1.1 jakllsch return;
190 1.1 jakllsch }
191 1.1 jakllsch
192 1.1 jakllsch mbr = mbrmap->map_data;
193 1.1 jakllsch
194 1.1 jakllsch /*
195 1.1 jakllsch * Update the boot code
196 1.1 jakllsch */
197 1.1 jakllsch if ((bootcode = read_boot()) == NULL) {
198 1.1 jakllsch warnx("error reading bootcode");
199 1.1 jakllsch return;
200 1.1 jakllsch }
201 1.1 jakllsch (void)memcpy(&mbr->mbr_code, &bootcode->mbr_code,
202 1.1 jakllsch sizeof(mbr->mbr_code));
203 1.1 jakllsch free(bootcode);
204 1.1 jakllsch
205 1.1 jakllsch /*
206 1.1 jakllsch * Walk through the GPT and see where we can boot from
207 1.1 jakllsch */
208 1.1 jakllsch for (m = map_first(); m != NULL; m = m->map_next) {
209 1.1 jakllsch if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
210 1.1 jakllsch continue;
211 1.1 jakllsch
212 1.1 jakllsch pp = m->map_data;
213 1.1 jakllsch
214 1.1 jakllsch /* first, prefer user selection */
215 1.1 jakllsch if (entry > 0 && m->map_index == entry)
216 1.1 jakllsch break;
217 1.1 jakllsch
218 1.1 jakllsch /* next, partition as could be specified by wedge */
219 1.1 jakllsch if (entry < 1 && size > 0 &&
220 1.1 jakllsch m->map_start == start && m->map_size == (off_t)size)
221 1.1 jakllsch break;
222 1.1 jakllsch }
223 1.1 jakllsch
224 1.1 jakllsch if (m == NULL) {
225 1.1 jakllsch warnx("error: no bootable partition");
226 1.1 jakllsch return;
227 1.1 jakllsch } else
228 1.1 jakllsch uuid_dec_le(pp->ent_guid, &buuid);
229 1.1 jakllsch
230 1.1 jakllsch #if 1
231 1.1 jakllsch {
232 1.1 jakllsch char *p;
233 1.1 jakllsch
234 1.1 jakllsch uuid_to_string(&buuid, &p, NULL);
235 1.1 jakllsch printf("Boot device: %s\n", device_name);
236 1.1 jakllsch printf("Partition GUID: %s\n", p);
237 1.1 jakllsch printf("Boot code: %s\n", bootpath);
238 1.1 jakllsch
239 1.1 jakllsch free(p);
240 1.1 jakllsch }
241 1.1 jakllsch #endif
242 1.1 jakllsch
243 1.1 jakllsch uuid_enc_le(&mbr->mbr_code[MBR_GPT_GUID_OFFSET], &buuid);
244 1.1 jakllsch if (gpt_write(fd, mbrmap) == -1) {
245 1.1 jakllsch warnx("error: cannot update Protective MBR");
246 1.1 jakllsch return;
247 1.1 jakllsch }
248 1.1 jakllsch }
249 1.1 jakllsch
250 1.1 jakllsch int
251 1.1 jakllsch cmd_biosboot(int argc, char *argv[])
252 1.1 jakllsch {
253 1.1 jakllsch struct dkwedge_info dkw;
254 1.1 jakllsch struct stat sb;
255 1.1 jakllsch char devpath[MAXPATHLEN];
256 1.1 jakllsch char *dev, *p;
257 1.1 jakllsch int ch, fd;
258 1.1 jakllsch
259 1.1 jakllsch while ((ch = getopt(argc, argv, "c:i:")) != -1) {
260 1.1 jakllsch switch(ch) {
261 1.1 jakllsch case 'c':
262 1.1 jakllsch if (bootpath != NULL)
263 1.1 jakllsch usage_biosboot();
264 1.1 jakllsch if ((bootpath = strdup(optarg)) == NULL)
265 1.1 jakllsch err(1, "Malloc failed");
266 1.1 jakllsch break;
267 1.1 jakllsch case 'i':
268 1.1 jakllsch if (entry > 0)
269 1.1 jakllsch usage_biosboot();
270 1.1 jakllsch entry = strtoul(optarg, &p, 10);
271 1.1 jakllsch if (*p != 0 || entry < 1)
272 1.1 jakllsch usage_biosboot();
273 1.1 jakllsch break;
274 1.1 jakllsch default:
275 1.1 jakllsch usage_biosboot();
276 1.1 jakllsch }
277 1.1 jakllsch }
278 1.1 jakllsch
279 1.1 jakllsch if (argc == optind)
280 1.1 jakllsch usage_biosboot();
281 1.1 jakllsch
282 1.1 jakllsch while (optind < argc) {
283 1.1 jakllsch dev = argv[optind++];
284 1.1 jakllsch start = 0;
285 1.1 jakllsch size = 0;
286 1.1 jakllsch
287 1.1 jakllsch #ifdef __NetBSD__
288 1.1 jakllsch /*
289 1.1 jakllsch * If a dk wedge was specified, loader should be
290 1.1 jakllsch * installed onto parent device
291 1.1 jakllsch */
292 1.1 jakllsch if ((fd = opendisk(dev, O_RDONLY, devpath, sizeof(devpath), 0))
293 1.1 jakllsch == -1)
294 1.1 jakllsch goto next;
295 1.1 jakllsch if (fstat(fd, &sb) == -1)
296 1.1 jakllsch goto close;
297 1.1 jakllsch
298 1.1 jakllsch #ifdef DIOCGWEDGEINFO
299 1.1 jakllsch if ((sb.st_mode & S_IFMT) != S_IFREG &&
300 1.1 jakllsch ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
301 1.1 jakllsch if (entry > 0)
302 1.1 jakllsch /* wedges and indexes are mutually exclusive */
303 1.1 jakllsch usage_biosboot();
304 1.1 jakllsch dev = dkw.dkw_parent;
305 1.1 jakllsch start = dkw.dkw_offset;
306 1.1 jakllsch size = dkw.dkw_size;
307 1.1 jakllsch }
308 1.1 jakllsch #endif
309 1.1 jakllsch close:
310 1.1 jakllsch close(fd);
311 1.1 jakllsch #endif /* __NetBSD__*/
312 1.1 jakllsch
313 1.1 jakllsch fd = gpt_open(dev);
314 1.1 jakllsch next:
315 1.1 jakllsch if (fd == -1) {
316 1.1 jakllsch warn("unable to open device '%s'", device_name);
317 1.1 jakllsch continue;
318 1.1 jakllsch }
319 1.1 jakllsch
320 1.1 jakllsch biosboot(fd);
321 1.1 jakllsch
322 1.1 jakllsch gpt_close(fd);
323 1.1 jakllsch }
324 1.1 jakllsch
325 1.1 jakllsch return (0);
326 1.1 jakllsch }
327