biosboot.c revision 1.12 1 1.12 christos /* $NetBSD: biosboot.c,v 1.12 2014/09/30 17:59:59 christos Exp $ */
2 1.1 jakllsch
3 1.1 jakllsch /*
4 1.6 jakllsch * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 1.1 jakllsch * All rights reserved.
6 1.6 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.10 christos #if HAVE_NBTOOL_CONFIG_H
35 1.10 christos #include "nbtool_config.h"
36 1.10 christos #endif
37 1.10 christos
38 1.1 jakllsch #include <sys/cdefs.h>
39 1.1 jakllsch #ifdef __RCSID
40 1.12 christos __RCSID("$NetBSD: biosboot.c,v 1.12 2014/09/30 17:59:59 christos Exp $");
41 1.1 jakllsch #endif
42 1.1 jakllsch
43 1.1 jakllsch #include <sys/stat.h>
44 1.1 jakllsch #include <sys/types.h>
45 1.1 jakllsch #include <sys/ioctl.h>
46 1.1 jakllsch #include <sys/disk.h>
47 1.1 jakllsch #include <sys/param.h>
48 1.1 jakllsch #include <sys/bootblock.h>
49 1.1 jakllsch
50 1.1 jakllsch #include <err.h>
51 1.1 jakllsch #include <fcntl.h>
52 1.1 jakllsch #include <paths.h>
53 1.1 jakllsch #include <stddef.h>
54 1.1 jakllsch #include <stdio.h>
55 1.1 jakllsch #include <stdlib.h>
56 1.1 jakllsch #include <string.h>
57 1.1 jakllsch #include <unistd.h>
58 1.1 jakllsch
59 1.1 jakllsch #include "map.h"
60 1.1 jakllsch #include "gpt.h"
61 1.1 jakllsch
62 1.1 jakllsch #define DEFAULT_BOOTDIR "/usr/mdec"
63 1.3 jakllsch #define DEFAULT_BOOTCODE "gptmbr.bin"
64 1.1 jakllsch
65 1.1 jakllsch static daddr_t start;
66 1.1 jakllsch static uint64_t size;
67 1.1 jakllsch
68 1.1 jakllsch static char *bootpath;
69 1.1 jakllsch static unsigned int entry;
70 1.9 jnemeth static uint8_t *label;
71 1.1 jakllsch
72 1.9 jnemeth const char biosbootmsg[] = "biosboot [-c bootcode] [-i index] "
73 1.9 jnemeth "[-L label] device ...";
74 1.1 jakllsch
75 1.5 joerg __dead static void
76 1.1 jakllsch usage_biosboot(void)
77 1.1 jakllsch {
78 1.1 jakllsch fprintf(stderr, "usage: %s %s\n", getprogname(), biosbootmsg);
79 1.1 jakllsch exit(1);
80 1.1 jakllsch }
81 1.1 jakllsch
82 1.1 jakllsch static struct mbr*
83 1.1 jakllsch read_boot(void)
84 1.1 jakllsch {
85 1.1 jakllsch int bfd, ret = 0;
86 1.1 jakllsch struct mbr *buf;
87 1.1 jakllsch struct stat st;
88 1.1 jakllsch
89 1.1 jakllsch /* XXX how to do the following better? */
90 1.1 jakllsch if (bootpath == NULL) {
91 1.1 jakllsch bootpath = strdup(DEFAULT_BOOTDIR "/" DEFAULT_BOOTCODE);
92 1.1 jakllsch } else {
93 1.1 jakllsch if (strchr(bootpath, '/') == 0) {
94 1.1 jakllsch char *p;
95 1.1 jakllsch if ((p = strdup(bootpath)) == NULL)
96 1.1 jakllsch err(1, "Malloc failed");
97 1.1 jakllsch free(bootpath);
98 1.1 jakllsch (void)asprintf(&bootpath, "%s/%s", DEFAULT_BOOTDIR, p);
99 1.1 jakllsch free(p);
100 1.1 jakllsch }
101 1.1 jakllsch }
102 1.1 jakllsch if (bootpath == NULL)
103 1.1 jakllsch err(1, "Malloc failed");
104 1.1 jakllsch
105 1.1 jakllsch if ((buf = malloc((size_t)secsz)) == NULL)
106 1.1 jakllsch err(1, "Malloc failed");
107 1.1 jakllsch
108 1.1 jakllsch if ((bfd = open(bootpath, O_RDONLY)) < 0 || fstat(bfd, &st) == -1) {
109 1.1 jakllsch warn("%s", bootpath);
110 1.1 jakllsch goto fail;
111 1.1 jakllsch }
112 1.1 jakllsch
113 1.2 jakllsch if (st.st_size != MBR_DSN_OFFSET) {
114 1.2 jakllsch warnx("%s: the bootcode does not match expected size",
115 1.2 jakllsch bootpath);
116 1.1 jakllsch goto fail;
117 1.1 jakllsch }
118 1.1 jakllsch
119 1.2 jakllsch if (read(bfd, buf, st.st_size) != st.st_size) {
120 1.1 jakllsch warn("%s", bootpath);
121 1.1 jakllsch goto fail;
122 1.1 jakllsch }
123 1.1 jakllsch
124 1.1 jakllsch ret++;
125 1.1 jakllsch
126 1.1 jakllsch fail:
127 1.1 jakllsch if (bfd >= 0)
128 1.1 jakllsch close(bfd);
129 1.1 jakllsch if (ret == 0) {
130 1.1 jakllsch free(buf);
131 1.1 jakllsch buf = NULL;
132 1.1 jakllsch }
133 1.1 jakllsch return buf;
134 1.1 jakllsch }
135 1.1 jakllsch
136 1.1 jakllsch static void
137 1.1 jakllsch biosboot(int fd)
138 1.1 jakllsch {
139 1.1 jakllsch map_t *gpt, *tpg;
140 1.1 jakllsch map_t *tbl, *lbt;
141 1.1 jakllsch map_t *mbrmap, *m;
142 1.1 jakllsch struct mbr *mbr, *bootcode;
143 1.2 jakllsch struct gpt_hdr *hdr;
144 1.2 jakllsch struct gpt_ent *ent;
145 1.8 jnemeth unsigned int i, j;
146 1.1 jakllsch
147 1.1 jakllsch /*
148 1.1 jakllsch * Parse and validate partition maps
149 1.1 jakllsch */
150 1.1 jakllsch gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
151 1.1 jakllsch if (gpt == NULL) {
152 1.1 jakllsch warnx("%s: error: no primary GPT header; run create or recover",
153 1.1 jakllsch device_name);
154 1.1 jakllsch return;
155 1.1 jakllsch }
156 1.1 jakllsch
157 1.1 jakllsch tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
158 1.1 jakllsch if (tpg == NULL) {
159 1.1 jakllsch warnx("%s: error: no secondary GPT header; run recover",
160 1.1 jakllsch device_name);
161 1.1 jakllsch return;
162 1.1 jakllsch }
163 1.1 jakllsch
164 1.1 jakllsch tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
165 1.1 jakllsch lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
166 1.1 jakllsch if (tbl == NULL || lbt == NULL) {
167 1.1 jakllsch warnx("%s: error: run recover -- trust me", device_name);
168 1.1 jakllsch return;
169 1.1 jakllsch }
170 1.1 jakllsch
171 1.1 jakllsch mbrmap = map_find(MAP_TYPE_PMBR);
172 1.1 jakllsch if (mbrmap == NULL || mbrmap->map_start != 0) {
173 1.1 jakllsch warnx("%s: error: no valid Protective MBR found", device_name);
174 1.1 jakllsch return;
175 1.1 jakllsch }
176 1.1 jakllsch
177 1.1 jakllsch mbr = mbrmap->map_data;
178 1.1 jakllsch
179 1.1 jakllsch /*
180 1.1 jakllsch * Update the boot code
181 1.1 jakllsch */
182 1.1 jakllsch if ((bootcode = read_boot()) == NULL) {
183 1.1 jakllsch warnx("error reading bootcode");
184 1.1 jakllsch return;
185 1.1 jakllsch }
186 1.1 jakllsch (void)memcpy(&mbr->mbr_code, &bootcode->mbr_code,
187 1.1 jakllsch sizeof(mbr->mbr_code));
188 1.1 jakllsch free(bootcode);
189 1.1 jakllsch
190 1.1 jakllsch /*
191 1.1 jakllsch * Walk through the GPT and see where we can boot from
192 1.1 jakllsch */
193 1.1 jakllsch for (m = map_first(); m != NULL; m = m->map_next) {
194 1.1 jakllsch if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
195 1.1 jakllsch continue;
196 1.1 jakllsch
197 1.2 jakllsch ent = m->map_data;
198 1.1 jakllsch
199 1.1 jakllsch /* first, prefer user selection */
200 1.1 jakllsch if (entry > 0 && m->map_index == entry)
201 1.1 jakllsch break;
202 1.1 jakllsch
203 1.9 jnemeth if (label != NULL)
204 1.9 jnemeth if (strcmp((char *)label,
205 1.9 jnemeth (char *)utf16_to_utf8(ent->ent_name)) == 0)
206 1.9 jnemeth break;
207 1.9 jnemeth
208 1.1 jakllsch /* next, partition as could be specified by wedge */
209 1.9 jnemeth if (entry < 1 && label == NULL && size > 0 &&
210 1.1 jakllsch m->map_start == start && m->map_size == (off_t)size)
211 1.1 jakllsch break;
212 1.1 jakllsch }
213 1.1 jakllsch
214 1.1 jakllsch if (m == NULL) {
215 1.1 jakllsch warnx("error: no bootable partition");
216 1.1 jakllsch return;
217 1.2 jakllsch }
218 1.2 jakllsch
219 1.2 jakllsch i = m->map_index - 1;
220 1.1 jakllsch
221 1.1 jakllsch
222 1.2 jakllsch hdr = gpt->map_data;
223 1.2 jakllsch
224 1.8 jnemeth for (j = 0; j < le32toh(hdr->hdr_entries); j++) {
225 1.2 jakllsch ent = (void*)((char*)tbl->map_data + j * le32toh(hdr->hdr_entsz));
226 1.2 jakllsch ent->ent_attr &= ~GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;
227 1.1 jakllsch }
228 1.1 jakllsch
229 1.2 jakllsch ent = (void*)((char*)tbl->map_data + i * le32toh(hdr->hdr_entsz));
230 1.2 jakllsch ent->ent_attr |= GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;
231 1.2 jakllsch
232 1.2 jakllsch hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
233 1.2 jakllsch le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
234 1.2 jakllsch hdr->hdr_crc_self = 0;
235 1.2 jakllsch hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
236 1.2 jakllsch
237 1.2 jakllsch gpt_write(fd, gpt);
238 1.2 jakllsch gpt_write(fd, tbl);
239 1.2 jakllsch
240 1.2 jakllsch
241 1.2 jakllsch hdr = tpg->map_data;
242 1.2 jakllsch
243 1.8 jnemeth for (j = 0; j < le32toh(hdr->hdr_entries); j++) {
244 1.2 jakllsch ent = (void*)((char*)lbt->map_data + j * le32toh(hdr->hdr_entsz));
245 1.2 jakllsch ent->ent_attr &= ~GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;
246 1.2 jakllsch }
247 1.2 jakllsch
248 1.2 jakllsch ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
249 1.2 jakllsch ent->ent_attr |= GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;
250 1.2 jakllsch
251 1.2 jakllsch hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
252 1.2 jakllsch le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
253 1.2 jakllsch hdr->hdr_crc_self = 0;
254 1.2 jakllsch hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
255 1.2 jakllsch
256 1.2 jakllsch gpt_write(fd, lbt);
257 1.2 jakllsch gpt_write(fd, tpg);
258 1.2 jakllsch
259 1.2 jakllsch
260 1.1 jakllsch if (gpt_write(fd, mbrmap) == -1) {
261 1.1 jakllsch warnx("error: cannot update Protective MBR");
262 1.1 jakllsch return;
263 1.1 jakllsch }
264 1.9 jnemeth
265 1.9 jnemeth printf("partition %d marked as bootable\n", i + 1);
266 1.1 jakllsch }
267 1.1 jakllsch
268 1.1 jakllsch int
269 1.1 jakllsch cmd_biosboot(int argc, char *argv[])
270 1.1 jakllsch {
271 1.12 christos #ifdef DIOCGWEDGEINFO
272 1.1 jakllsch struct dkwedge_info dkw;
273 1.12 christos #endif
274 1.1 jakllsch struct stat sb;
275 1.1 jakllsch char devpath[MAXPATHLEN];
276 1.1 jakllsch char *dev, *p;
277 1.1 jakllsch int ch, fd;
278 1.1 jakllsch
279 1.9 jnemeth while ((ch = getopt(argc, argv, "c:i:L:")) != -1) {
280 1.1 jakllsch switch(ch) {
281 1.1 jakllsch case 'c':
282 1.1 jakllsch if (bootpath != NULL)
283 1.1 jakllsch usage_biosboot();
284 1.1 jakllsch if ((bootpath = strdup(optarg)) == NULL)
285 1.1 jakllsch err(1, "Malloc failed");
286 1.1 jakllsch break;
287 1.1 jakllsch case 'i':
288 1.1 jakllsch if (entry > 0)
289 1.1 jakllsch usage_biosboot();
290 1.1 jakllsch entry = strtoul(optarg, &p, 10);
291 1.1 jakllsch if (*p != 0 || entry < 1)
292 1.1 jakllsch usage_biosboot();
293 1.1 jakllsch break;
294 1.9 jnemeth case 'L':
295 1.9 jnemeth if (label != NULL)
296 1.9 jnemeth usage_biosboot();
297 1.9 jnemeth label = (uint8_t *)strdup(optarg);
298 1.9 jnemeth break;
299 1.1 jakllsch default:
300 1.1 jakllsch usage_biosboot();
301 1.1 jakllsch }
302 1.1 jakllsch }
303 1.1 jakllsch
304 1.1 jakllsch if (argc == optind)
305 1.1 jakllsch usage_biosboot();
306 1.1 jakllsch
307 1.1 jakllsch while (optind < argc) {
308 1.1 jakllsch dev = argv[optind++];
309 1.1 jakllsch start = 0;
310 1.1 jakllsch size = 0;
311 1.1 jakllsch
312 1.1 jakllsch /*
313 1.1 jakllsch * If a dk wedge was specified, loader should be
314 1.1 jakllsch * installed onto parent device
315 1.1 jakllsch */
316 1.1 jakllsch if ((fd = opendisk(dev, O_RDONLY, devpath, sizeof(devpath), 0))
317 1.1 jakllsch == -1)
318 1.1 jakllsch goto next;
319 1.1 jakllsch if (fstat(fd, &sb) == -1)
320 1.1 jakllsch goto close;
321 1.1 jakllsch
322 1.1 jakllsch #ifdef DIOCGWEDGEINFO
323 1.1 jakllsch if ((sb.st_mode & S_IFMT) != S_IFREG &&
324 1.1 jakllsch ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
325 1.1 jakllsch if (entry > 0)
326 1.1 jakllsch /* wedges and indexes are mutually exclusive */
327 1.1 jakllsch usage_biosboot();
328 1.1 jakllsch dev = dkw.dkw_parent;
329 1.1 jakllsch start = dkw.dkw_offset;
330 1.1 jakllsch size = dkw.dkw_size;
331 1.1 jakllsch }
332 1.1 jakllsch #endif
333 1.1 jakllsch close:
334 1.1 jakllsch close(fd);
335 1.1 jakllsch
336 1.1 jakllsch fd = gpt_open(dev);
337 1.1 jakllsch next:
338 1.1 jakllsch if (fd == -1) {
339 1.1 jakllsch warn("unable to open device '%s'", device_name);
340 1.1 jakllsch continue;
341 1.1 jakllsch }
342 1.1 jakllsch
343 1.1 jakllsch biosboot(fd);
344 1.1 jakllsch
345 1.1 jakllsch gpt_close(fd);
346 1.1 jakllsch }
347 1.1 jakllsch
348 1.1 jakllsch return (0);
349 1.1 jakllsch }
350