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