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