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