Home | History | Annotate | Line # | Download | only in gpt
biosboot.c revision 1.16
      1  1.16  christos /*	$NetBSD: biosboot.c,v 1.16 2015/12/01 09:05:33 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.16  christos __RCSID("$NetBSD: biosboot.c,v 1.16 2015/12/01 09:05:33 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.14  christos #ifdef DIOCGWEDGEINFO
     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.16  christos #include "gpt_private.h"
     64   1.1  jakllsch 
     65   1.1  jakllsch #define DEFAULT_BOOTDIR		"/usr/mdec"
     66   1.3  jakllsch #define DEFAULT_BOOTCODE	"gptmbr.bin"
     67   1.1  jakllsch 
     68   1.1  jakllsch static daddr_t start;
     69   1.1  jakllsch static uint64_t size;
     70   1.1  jakllsch 
     71   1.1  jakllsch static char *bootpath;
     72   1.1  jakllsch static unsigned int entry;
     73   1.9   jnemeth static uint8_t *label;
     74   1.1  jakllsch 
     75   1.9   jnemeth const char biosbootmsg[] = "biosboot [-c bootcode] [-i index] "
     76  1.16  christos 	"[-L label]";
     77   1.1  jakllsch 
     78  1.16  christos static int
     79   1.1  jakllsch usage_biosboot(void)
     80   1.1  jakllsch {
     81   1.1  jakllsch 	fprintf(stderr, "usage: %s %s\n", getprogname(), biosbootmsg);
     82  1.16  christos 	return -1;
     83   1.1  jakllsch }
     84   1.1  jakllsch 
     85   1.1  jakllsch static struct mbr*
     86  1.16  christos read_boot(gpt_t gpt)
     87   1.1  jakllsch {
     88   1.1  jakllsch 	int bfd, ret = 0;
     89   1.1  jakllsch 	struct mbr *buf;
     90   1.1  jakllsch 	struct stat st;
     91   1.1  jakllsch 
     92  1.16  christos 	buf = NULL;
     93  1.16  christos 	bfd = -1;
     94  1.16  christos 
     95  1.16  christos 	if (bootpath == NULL)
     96  1.16  christos 		bootpath = strdup(DEFAULT_BOOTDIR "/" DEFAULT_BOOTCODE);
     97  1.16  christos 	else if (*bootpath == '/')
     98  1.16  christos 		bootpath = strdup(bootpath);
     99  1.16  christos 	else {
    100  1.16  christos 		if (asprintf(&bootpath, "%s/%s", DEFAULT_BOOTDIR, bootpath) < 0)
    101  1.16  christos 			bootpath = NULL;
    102  1.16  christos 	}
    103  1.16  christos 
    104   1.1  jakllsch 	if (bootpath == NULL) {
    105  1.16  christos 		gpt_warn(gpt, "Can't allocate memory for bootpath");
    106  1.16  christos 		goto fail;
    107  1.16  christos 	}
    108  1.16  christos 
    109  1.16  christos 	if ((buf = malloc((size_t)gpt->secsz)) == NULL) {
    110  1.16  christos 		gpt_warn(gpt, "Can't allocate memory for sector");
    111  1.16  christos 		goto fail;
    112   1.1  jakllsch 	}
    113   1.1  jakllsch 
    114   1.1  jakllsch 
    115   1.1  jakllsch 	if ((bfd = open(bootpath, O_RDONLY)) < 0 || fstat(bfd, &st) == -1) {
    116  1.16  christos 		gpt_warn(gpt, "Can't open `%s'", bootpath);
    117   1.1  jakllsch 		goto fail;
    118   1.1  jakllsch 	}
    119   1.1  jakllsch 
    120   1.2  jakllsch 	if (st.st_size != MBR_DSN_OFFSET) {
    121  1.16  christos 		gpt_warnx(gpt, "The bootcode in `%s' does not match the"
    122  1.16  christos 		    " expected size %u", bootpath, MBR_DSN_OFFSET);
    123   1.1  jakllsch 		goto fail;
    124   1.1  jakllsch 	}
    125   1.1  jakllsch 
    126   1.2  jakllsch 	if (read(bfd, buf, st.st_size) != st.st_size) {
    127  1.16  christos 		gpt_warn(gpt, "Error reading from `%s'", bootpath);
    128   1.1  jakllsch 		goto fail;
    129   1.1  jakllsch 	}
    130   1.1  jakllsch 
    131   1.1  jakllsch 	ret++;
    132   1.1  jakllsch 
    133   1.1  jakllsch     fail:
    134  1.16  christos 	if (bfd != -1)
    135   1.1  jakllsch 		close(bfd);
    136   1.1  jakllsch 	if (ret == 0) {
    137   1.1  jakllsch 		free(buf);
    138   1.1  jakllsch 		buf = NULL;
    139   1.1  jakllsch 	}
    140   1.1  jakllsch 	return buf;
    141   1.1  jakllsch }
    142   1.1  jakllsch 
    143  1.16  christos static int
    144  1.16  christos set_bootable(gpt_t gpt, map_t map, map_t tbl, unsigned int i)
    145   1.1  jakllsch {
    146  1.16  christos 	unsigned int j;
    147  1.16  christos 	struct gpt_hdr *hdr = map->map_data;
    148  1.16  christos 	struct gpt_ent *ent;
    149  1.16  christos 	unsigned int ne = le32toh(hdr->hdr_entries);
    150  1.16  christos 
    151  1.16  christos 	for (j = 0; j < ne; j++) {
    152  1.16  christos 		ent = gpt_ent(map, tbl, j);
    153  1.16  christos 		ent->ent_attr &= ~GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;
    154  1.16  christos 	}
    155  1.16  christos 
    156  1.16  christos 	ent = gpt_ent(map, tbl, i);
    157  1.16  christos 	ent->ent_attr |= GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;
    158  1.16  christos 
    159  1.16  christos 	return gpt_write_crc(gpt, map, tbl);
    160  1.16  christos }
    161  1.16  christos 
    162  1.16  christos static int
    163  1.16  christos biosboot(gpt_t gpt)
    164  1.16  christos {
    165  1.16  christos 	map_t mbrmap, m;
    166   1.1  jakllsch 	struct mbr *mbr, *bootcode;
    167  1.16  christos 	unsigned int i;
    168   1.2  jakllsch 	struct gpt_ent *ent;
    169   1.1  jakllsch 
    170   1.1  jakllsch 	/*
    171   1.1  jakllsch 	 * Parse and validate partition maps
    172   1.1  jakllsch 	 */
    173  1.16  christos 	if (gpt_hdr(gpt) == NULL)
    174  1.16  christos 		return -1;
    175   1.1  jakllsch 
    176  1.16  christos 	mbrmap = map_find(gpt, MAP_TYPE_PMBR);
    177   1.1  jakllsch 	if (mbrmap == NULL || mbrmap->map_start != 0) {
    178  1.16  christos 		gpt_warnx(gpt, "No valid Protective MBR found");
    179  1.16  christos 		return -1;
    180   1.1  jakllsch 	}
    181   1.1  jakllsch 
    182   1.1  jakllsch 	mbr = mbrmap->map_data;
    183   1.1  jakllsch 
    184   1.1  jakllsch 	/*
    185   1.1  jakllsch 	 * Update the boot code
    186   1.1  jakllsch 	 */
    187  1.16  christos 	if ((bootcode = read_boot(gpt)) == NULL) {
    188  1.16  christos 		gpt_warnx(gpt, "Error reading bootcode");
    189  1.16  christos 		return -1;
    190   1.1  jakllsch 	}
    191   1.1  jakllsch 	(void)memcpy(&mbr->mbr_code, &bootcode->mbr_code,
    192   1.1  jakllsch 		sizeof(mbr->mbr_code));
    193   1.1  jakllsch 	free(bootcode);
    194   1.1  jakllsch 
    195   1.1  jakllsch 	/*
    196   1.1  jakllsch 	 * Walk through the GPT and see where we can boot from
    197   1.1  jakllsch 	 */
    198  1.16  christos 	for (m = map_first(gpt); m != NULL; m = m->map_next) {
    199   1.1  jakllsch 		if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
    200   1.1  jakllsch 			continue;
    201   1.1  jakllsch 
    202   1.2  jakllsch 		ent = m->map_data;
    203   1.1  jakllsch 
    204   1.1  jakllsch 		/* first, prefer user selection */
    205   1.1  jakllsch 		if (entry > 0 && m->map_index == entry)
    206   1.1  jakllsch 			break;
    207   1.1  jakllsch 
    208   1.9   jnemeth 		if (label != NULL)
    209   1.9   jnemeth 			if (strcmp((char *)label,
    210   1.9   jnemeth 			    (char *)utf16_to_utf8(ent->ent_name)) == 0)
    211   1.9   jnemeth 				break;
    212   1.9   jnemeth 
    213   1.1  jakllsch 		/* next, partition as could be specified by wedge */
    214   1.9   jnemeth 		if (entry < 1 && label == NULL && size > 0 &&
    215   1.1  jakllsch 		    m->map_start == start && m->map_size == (off_t)size)
    216   1.1  jakllsch 			break;
    217   1.1  jakllsch 	}
    218   1.1  jakllsch 
    219   1.1  jakllsch 	if (m == NULL) {
    220  1.16  christos 		gpt_warnx(gpt, "No bootable partition");
    221  1.16  christos 		return -1;
    222   1.2  jakllsch 	}
    223   1.2  jakllsch 
    224   1.2  jakllsch 	i = m->map_index - 1;
    225   1.1  jakllsch 
    226   1.1  jakllsch 
    227  1.16  christos 	if (set_bootable(gpt, gpt->gpt, gpt->tbl, i) == -1)
    228  1.16  christos 		return -1;
    229   1.2  jakllsch 
    230  1.16  christos 	if (set_bootable(gpt, gpt->tpg, gpt->lbt, i) == -1)
    231  1.16  christos 		return -1;
    232   1.2  jakllsch 
    233  1.16  christos 	if (gpt_write(gpt, mbrmap) == -1) {
    234  1.16  christos 		gpt_warnx(gpt, "Cannot update Protective MBR");
    235  1.16  christos 		return -1;
    236   1.1  jakllsch 	}
    237   1.9   jnemeth 
    238  1.16  christos 	gpt_msg(gpt, "Partition %d marked as bootable", i + 1);
    239  1.16  christos 	return 0;
    240   1.1  jakllsch }
    241   1.1  jakllsch 
    242   1.1  jakllsch int
    243  1.16  christos cmd_biosboot(gpt_t gpt, int argc, char *argv[])
    244   1.1  jakllsch {
    245  1.12  christos #ifdef DIOCGWEDGEINFO
    246   1.1  jakllsch 	struct dkwedge_info dkw;
    247  1.12  christos #endif
    248   1.1  jakllsch 	char *dev, *p;
    249  1.16  christos 	int ch;
    250  1.16  christos 	gpt_t ngpt = gpt;
    251   1.1  jakllsch 
    252   1.9   jnemeth 	while ((ch = getopt(argc, argv, "c:i:L:")) != -1) {
    253   1.1  jakllsch 		switch(ch) {
    254   1.1  jakllsch 		case 'c':
    255   1.1  jakllsch 			if (bootpath != NULL)
    256   1.1  jakllsch 				usage_biosboot();
    257   1.1  jakllsch 			if ((bootpath = strdup(optarg)) == NULL)
    258   1.1  jakllsch 				err(1, "Malloc failed");
    259   1.1  jakllsch 			break;
    260   1.1  jakllsch 		case 'i':
    261   1.1  jakllsch 			if (entry > 0)
    262   1.1  jakllsch 				usage_biosboot();
    263   1.1  jakllsch 			entry = strtoul(optarg, &p, 10);
    264   1.1  jakllsch 			if (*p != 0 || entry < 1)
    265   1.1  jakllsch 				usage_biosboot();
    266   1.1  jakllsch 			break;
    267   1.9   jnemeth 		case 'L':
    268   1.9   jnemeth 			if (label != NULL)
    269   1.9   jnemeth 				usage_biosboot();
    270   1.9   jnemeth 			label = (uint8_t *)strdup(optarg);
    271   1.9   jnemeth 			break;
    272   1.1  jakllsch 		default:
    273  1.16  christos 			return usage_biosboot();
    274   1.1  jakllsch 		}
    275   1.1  jakllsch 	}
    276   1.1  jakllsch 
    277  1.16  christos 	if (argc != optind)
    278  1.16  christos 		return usage_biosboot();
    279   1.1  jakllsch 
    280  1.16  christos 	start = 0;
    281  1.16  christos 	size = 0;
    282   1.1  jakllsch 
    283   1.1  jakllsch #ifdef DIOCGWEDGEINFO
    284  1.16  christos 	if ((gpt->sb.st_mode & S_IFMT) != S_IFREG &&
    285  1.16  christos 	    ioctl(gpt->fd, DIOCGWEDGEINFO, &dkw) != -1) {
    286  1.16  christos 		if (entry > 0)
    287  1.16  christos 			/* wedges and indexes are mutually exclusive */
    288  1.16  christos 			return usage_biosboot();
    289  1.16  christos 		dev = dkw.dkw_parent;
    290  1.16  christos 		start = dkw.dkw_offset;
    291  1.16  christos 		size = dkw.dkw_size;
    292  1.16  christos 		ngpt = gpt_open(dev, gpt->flags, gpt->verbose,
    293  1.16  christos 		    gpt->mediasz, gpt->secsz);
    294  1.16  christos 		if (ngpt == NULL)
    295  1.16  christos 			return -1;
    296  1.16  christos 	}
    297   1.1  jakllsch #endif
    298  1.16  christos 	biosboot(ngpt);
    299  1.16  christos 	if (ngpt != gpt)
    300  1.16  christos 		gpt_close(ngpt);
    301   1.1  jakllsch 
    302  1.16  christos 	return 0;
    303   1.1  jakllsch }
    304