summaryrefslogtreecommitdiff
path: root/sbin/gpt/biosboot.c
blob: 9a35a77c0ef9caa75d53d6bcd8071b1c94a9e1b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*	$NetBSD: biosboot.c,v 1.7 2013/11/27 01:47:53 jnemeth Exp $ */

/*
 * Copyright (c) 2009 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to the NetBSD Foundation
 * by Mike M. Volokhov. Development of this software was supported by the
 * Google Summer of Code program.
 * The GSoC project was mentored by Allen Briggs and Joerg Sonnenberger.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifdef __RCSID
__RCSID("$NetBSD: biosboot.c,v 1.7 2013/11/27 01:47:53 jnemeth Exp $");
#endif

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/disk.h>
#include <sys/param.h>
#include <sys/bootblock.h>

#include <err.h>
#include <fcntl.h>
#include <inttypes.h>
#include <paths.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>

#include "map.h"
#include "gpt.h"

#define DEFAULT_BOOTDIR		"/usr/mdec"
#define DEFAULT_BOOTCODE	"gptmbr.bin"

static daddr_t start;
static uint64_t size;

static char *bootpath;
static unsigned int entry;

const char biosbootmsg[] = "biosboot [-c bootcode] [-i index] device ...";

__dead static void
usage_biosboot(void)
{
	fprintf(stderr, "usage: %s %s\n", getprogname(), biosbootmsg);
	exit(1);
}

static struct mbr*
read_boot(void)
{
	int bfd, ret = 0;
	struct mbr *buf;
	struct stat st;

	/* XXX how to do the following better? */
	if (bootpath == NULL) {
		bootpath = strdup(DEFAULT_BOOTDIR "/" DEFAULT_BOOTCODE);
	} else {
		if (strchr(bootpath, '/') == 0) {
			char *p;
			if ((p = strdup(bootpath)) == NULL)
				err(1, "Malloc failed");
			free(bootpath);
			(void)asprintf(&bootpath, "%s/%s", DEFAULT_BOOTDIR, p);
			free(p);
		}
	}
	if (bootpath == NULL)
		err(1, "Malloc failed");

	if ((buf = malloc((size_t)secsz)) == NULL)
		err(1, "Malloc failed");

	if ((bfd = open(bootpath, O_RDONLY)) < 0 || fstat(bfd, &st) == -1) {
		warn("%s", bootpath);
		goto fail;
	}

	if (st.st_size != MBR_DSN_OFFSET) {
		warnx("%s: the bootcode does not match expected size",
			bootpath);
		goto fail;
	}

	if (read(bfd, buf, st.st_size) != st.st_size) {
		warn("%s", bootpath);
		goto fail;
	}

	ret++;

    fail:
	if (bfd >= 0)
		close(bfd);
	if (ret == 0) {
		free(buf);
		buf = NULL;
	}
	return buf;
}

static void
biosboot(int fd)
{
	map_t *gpt, *tpg;
	map_t *tbl, *lbt;
	map_t *mbrmap, *m;
	struct mbr *mbr, *bootcode;
	struct gpt_hdr *hdr;
	struct gpt_ent *ent;
	int i;

	/*
	 * Parse and validate partition maps
	 */
	gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
	if (gpt == NULL) {
		warnx("%s: error: no primary GPT header; run create or recover",
		    device_name);
		return;
	}

	tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
	if (tpg == NULL) {
		warnx("%s: error: no secondary GPT header; run recover",
		    device_name);
		return;
	}

	tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
	lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
	if (tbl == NULL || lbt == NULL) {
		warnx("%s: error: run recover -- trust me", device_name);
		return;
	}

	mbrmap = map_find(MAP_TYPE_PMBR);
	if (mbrmap == NULL || mbrmap->map_start != 0) {
		warnx("%s: error: no valid Protective MBR found", device_name);
		return;
	}

	mbr = mbrmap->map_data;

	/*
	 * Update the boot code
	 */
	if ((bootcode = read_boot()) == NULL) {
		warnx("error reading bootcode");
		return;
	}
	(void)memcpy(&mbr->mbr_code, &bootcode->mbr_code,
		sizeof(mbr->mbr_code));
	free(bootcode);

	/*
	 * Walk through the GPT and see where we can boot from
	 */
	for (m = map_first(); m != NULL; m = m->map_next) {
		if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
			continue;

		ent = m->map_data;

		/* first, prefer user selection */
		if (entry > 0 && m->map_index == entry)
			break;

		/* next, partition as could be specified by wedge */
		if (entry < 1 && size > 0 &&
		    m->map_start == start && m->map_size == (off_t)size)
			break;
	}

	if (m == NULL) {
		warnx("error: no bootable partition");
		return;
	}

	i = m->map_index - 1;


	hdr = gpt->map_data;

	for (uint32_t j = 0; j < le32toh(hdr->hdr_entries); j++) {
		ent = (void*)((char*)tbl->map_data + j * le32toh(hdr->hdr_entsz));
		ent->ent_attr &= ~GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;
	}

	ent = (void*)((char*)tbl->map_data + i * le32toh(hdr->hdr_entsz));
	ent->ent_attr |= GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;

	hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
	    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
	hdr->hdr_crc_self = 0;
	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));

	gpt_write(fd, gpt);
	gpt_write(fd, tbl);


	hdr = tpg->map_data;

	for (uint32_t j = 0; j < le32toh(hdr->hdr_entries); j++) {
		ent = (void*)((char*)lbt->map_data + j * le32toh(hdr->hdr_entsz));
		ent->ent_attr &= ~GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;
	}

	ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
	ent->ent_attr |= GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;

	hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
	    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
	hdr->hdr_crc_self = 0;
	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));

	gpt_write(fd, lbt);
	gpt_write(fd, tpg);


	if (gpt_write(fd, mbrmap) == -1) {
		warnx("error: cannot update Protective MBR");
		return;
	}
}

int
cmd_biosboot(int argc, char *argv[])
{
	struct dkwedge_info dkw;
	struct stat sb;
	char devpath[MAXPATHLEN];
	char *dev, *p;
	int ch, fd;

	while ((ch = getopt(argc, argv, "c:i:")) != -1) {
		switch(ch) {
		case 'c':
			if (bootpath != NULL)
				usage_biosboot();
			if ((bootpath = strdup(optarg)) == NULL)
				err(1, "Malloc failed");
			break;
		case 'i':
			if (entry > 0)
				usage_biosboot();
			entry = strtoul(optarg, &p, 10);
			if (*p != 0 || entry < 1)
				usage_biosboot();
			break;
		default:
			usage_biosboot();
		}
	}

	if (argc == optind)
		usage_biosboot();

	while (optind < argc) {
		dev = argv[optind++];
		start = 0;
		size = 0;

		/*
		 * If a dk wedge was specified, loader should be
		 * installed onto parent device
		 */
		if ((fd = opendisk(dev, O_RDONLY, devpath, sizeof(devpath), 0))
				== -1)
			goto next;
		if (fstat(fd, &sb) == -1)
			goto close;

#ifdef DIOCGWEDGEINFO
		if ((sb.st_mode & S_IFMT) != S_IFREG &&
		    ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
			if (entry > 0)
				/* wedges and indexes are mutually exclusive */
				usage_biosboot();
			dev = dkw.dkw_parent;
			start = dkw.dkw_offset;
			size = dkw.dkw_size;
		}
#endif
	close:
		close(fd);

		fd = gpt_open(dev);
	next:
		if (fd == -1) {
			warn("unable to open device '%s'", device_name);
			continue;
		}

		biosboot(fd);

		gpt_close(fd);
	}

	return (0);
}