Home | History | Annotate | Line # | Download | only in gpt
      1 /*	$NetBSD: biosboot.c,v 1.36 2025/12/17 16:18:11 nia Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to the NetBSD Foundation
      8  * by Mike M. Volokhov. Development of this software was supported by the
      9  * Google Summer of Code program.
     10  * The GSoC project was mentored by Allen Briggs and Joerg Sonnenberger.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #if HAVE_NBTOOL_CONFIG_H
     35 #include "nbtool_config.h"
     36 #endif
     37 
     38 #include <sys/cdefs.h>
     39 #ifdef __RCSID
     40 __RCSID("$NetBSD: biosboot.c,v 1.36 2025/12/17 16:18:11 nia Exp $");
     41 #endif
     42 
     43 #include <sys/stat.h>
     44 #include <sys/types.h>
     45 #include <sys/ioctl.h>
     46 #if defined(HAVE_SYS_ENDIAN_H) || ! defined(HAVE_NBTOOL_CONFIG_H)
     47 #include <sys/endian.h>
     48 #endif
     49 #include <sys/bootblock.h>
     50 
     51 #if defined(DIOCGWEDGEINFO) && !defined(HAVE_NBTOOL_CONFIG_H)
     52 #define USE_WEDGES
     53 #endif
     54 #ifdef USE_WEDGES
     55 #include <sys/disk.h>
     56 #endif
     57 
     58 #include <err.h>
     59 #include <fcntl.h>
     60 #include <paths.h>
     61 #include <stddef.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #include <string.h>
     65 #include <unistd.h>
     66 
     67 #include "map.h"
     68 #include "gpt.h"
     69 #include "gpt_private.h"
     70 
     71 #define DEFAULT_BOOTDIR		"/usr/mdec"
     72 #define DEFAULT_BOOTCODE	"gptmbr.bin"
     73 
     74 static int cmd_biosboot(gpt_t, int, char *[]);
     75 
     76 static const char *biosboothelp[] = {
     77 	"[-A] [-c bootcode] [-i index] [-L label] [-b startsec]",
     78 #if notyet
     79 	"[-a alignment] [-b blocknr] [-i index] [-l label]",
     80 	"[-s size] [-t type]",
     81 #endif
     82 };
     83 
     84 const struct gpt_cmd c_biosboot = {
     85 	"biosboot",
     86 	cmd_biosboot,
     87 	biosboothelp, __arraycount(biosboothelp),
     88 	0,
     89 };
     90 
     91 #define usage() gpt_usage(NULL, &c_biosboot)
     92 
     93 static struct mbr*
     94 read_boot(gpt_t gpt, const char *bootpath)
     95 {
     96 	int bfd, ret = -1;
     97 	struct mbr *buf;
     98 	struct stat st;
     99 	char *bp;
    100 
    101 	buf = NULL;
    102 	bfd = -1;
    103 
    104 	if (bootpath == NULL)
    105 		bp = strdup(DEFAULT_BOOTDIR "/" DEFAULT_BOOTCODE);
    106 	else if (*bootpath == '/')
    107 		bp = strdup(bootpath);
    108 	else {
    109 		if (asprintf(&bp, "%s/%s", DEFAULT_BOOTDIR, bootpath) < 0)
    110 			bp = NULL;
    111 	}
    112 
    113 	if (bp == NULL) {
    114 		gpt_warn(gpt, "Can't allocate memory for bootpath");
    115 		goto fail;
    116 	}
    117 
    118 	if ((buf = malloc((size_t)gpt->secsz)) == NULL) {
    119 		gpt_warn(gpt, "Can't allocate memory for sector");
    120 		goto fail;
    121 	}
    122 
    123 
    124 	if ((bfd = open(bp, O_RDONLY)) < 0 || fstat(bfd, &st) == -1) {
    125 		gpt_warn(gpt, "Can't open `%s'", bp);
    126 		goto fail;
    127 	}
    128 
    129 	if (st.st_size != MBR_DSN_OFFSET) {
    130 		gpt_warnx(gpt, "The bootcode in `%s' does not match the"
    131 		    " expected size %u", bp, MBR_DSN_OFFSET);
    132 		goto fail;
    133 	}
    134 
    135 	if (read(bfd, buf, (size_t)st.st_size) != (ssize_t)st.st_size) {
    136 		gpt_warn(gpt, "Error reading from `%s'", bp);
    137 		goto fail;
    138 	}
    139 
    140 	ret = 0;
    141 fail:
    142 	if (bfd != -1)
    143 		close(bfd);
    144 	if (ret == -1) {
    145 		free(buf);
    146 		buf = NULL;
    147 	}
    148 	free(bp);
    149 	return buf;
    150 }
    151 
    152 static int
    153 set_bootable(gpt_t gpt, map_t map, map_t tbl, unsigned int i)
    154 {
    155 	unsigned int j;
    156 	struct gpt_hdr *hdr = map->map_data;
    157 	struct gpt_ent *ent;
    158 	unsigned int ne = le32toh(hdr->hdr_entries);
    159 
    160 	for (j = 0; j < ne; j++) {
    161 		ent = gpt_ent(map, tbl, j);
    162 		ent->ent_attr &= ~GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;
    163 	}
    164 
    165 	ent = gpt_ent(map, tbl, i);
    166 	ent->ent_attr |= GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;
    167 
    168 	return gpt_write_crc(gpt, map, tbl);
    169 }
    170 
    171 static int
    172 biosboot(gpt_t gpt, off_t start, uint64_t size, u_int entry, uint8_t *label,
    173     const char *bootpath, int active)
    174 {
    175 	map_t mbrmap, m;
    176 	struct mbr *mbr, *bootcode;
    177 	unsigned int i;
    178 	struct gpt_ent *ent;
    179 	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
    180 
    181 	/*
    182 	 * Parse and validate partition maps
    183 	 */
    184 	if (gpt_hdr(gpt) == NULL)
    185 		return -1;
    186 
    187 	mbrmap = map_find(gpt, MAP_TYPE_PMBR);
    188 	if (mbrmap == NULL || mbrmap->map_start != 0) {
    189 		gpt_warnx(gpt, "No valid Protective MBR found");
    190 		return -1;
    191 	}
    192 
    193 	mbr = mbrmap->map_data;
    194 
    195 	/*
    196 	 * Update the boot code
    197 	 */
    198 	if ((bootcode = read_boot(gpt, bootpath)) == NULL) {
    199 		gpt_warnx(gpt, "Error reading bootcode");
    200 		return -1;
    201 	}
    202 	(void)memcpy(&mbr->mbr_code, &bootcode->mbr_code,
    203 		sizeof(mbr->mbr_code));
    204 	free(bootcode);
    205 
    206 	for (i = 0; i < __arraycount(mbr->mbr_part); i++)
    207 		if (mbr->mbr_part[i].part_typ == MBR_PTYPE_PMBR)
    208 			mbr->mbr_part[i].part_flag = active ? 0x80 : 0;
    209 
    210 	/*
    211 	 * Walk through the GPT and see where we can boot from
    212 	 */
    213 	for (m = map_first(gpt); m != NULL; m = m->map_next) {
    214 		if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
    215 			continue;
    216 
    217 		ent = m->map_data;
    218 
    219 		/* first, prefer user selection */
    220 		if (entry > 0 && m->map_index == entry)
    221 			break;
    222 
    223 		if (label != NULL) {
    224 			utf16_to_utf8(ent->ent_name,
    225 			    __arraycount(ent->ent_name), utfbuf,
    226 			    __arraycount(utfbuf));
    227 			if (strcmp((char *)label, (char *)utfbuf) == 0)
    228 				break;
    229 		}
    230 
    231 		/* next, partition as could be specified by wedge */
    232 		if (entry < 1 && label == NULL && size > 0 &&
    233 		    m->map_start == start && m->map_size == (off_t)size)
    234 			break;
    235 		/* next could be start sector specified by -b option */
    236 		if (entry < 1 && label == NULL && size == 0 &&
    237 		    m->map_start == start)
    238 			break;
    239 	}
    240 
    241 	if (m == NULL) {
    242 		gpt_warnx(gpt, "No bootable partition");
    243 		return -1;
    244 	}
    245 
    246 	i = m->map_index - 1;
    247 
    248 
    249 	if (set_bootable(gpt, gpt->gpt, gpt->tbl, i) == -1)
    250 		return -1;
    251 
    252 	if (set_bootable(gpt, gpt->tpg, gpt->lbt, i) == -1)
    253 		return -1;
    254 
    255 	if (gpt_write(gpt, mbrmap) == -1) {
    256 		gpt_warnx(gpt, "Cannot update Protective MBR");
    257 		return -1;
    258 	}
    259 
    260 	gpt_msg(gpt, "Partition %d marked as bootable", i + 1);
    261 	return 0;
    262 }
    263 
    264 static int
    265 cmd_biosboot(gpt_t gpt, int argc, char *argv[])
    266 {
    267 #ifdef USE_WEDGES
    268 	struct dkwedge_info dkw;
    269 #endif
    270 	int ch;
    271 	gpt_t ngpt = gpt;
    272 	off_t start = 0;
    273 	uint64_t size = 0;
    274 	int active = 0;
    275 	unsigned int entry = 0;
    276 	uint8_t *label = NULL;
    277 	char *bootpath = NULL;
    278 
    279 	while ((ch = getopt(argc, argv, "Ac:i:L:b:")) != -1) {
    280 		switch(ch) {
    281 		case 'A':
    282 			active = 1;
    283 			break;
    284 		case 'c':
    285 			if (gpt_name_get(gpt, &bootpath) == -1)
    286 				goto usage;
    287 			break;
    288 		case 'i':
    289 			if (gpt_uint_get(gpt, &entry) == -1)
    290 				goto usage;
    291 			break;
    292 		case 'L':
    293 			if (gpt_name_get(gpt, &label) == -1)
    294 				goto usage;
    295 			break;
    296 		case 'b':
    297 			if (gpt_human_get(gpt, &start) == -1)
    298 				goto usage;
    299 			break;
    300 		default:
    301 			goto usage;
    302 		}
    303 	}
    304 
    305 	if (argc != optind)
    306 		return usage();
    307 
    308 #ifdef USE_WEDGES
    309 	if ((gpt->sb.st_mode & S_IFMT) != S_IFREG &&
    310 	    ioctl(gpt->fd, DIOCGWEDGEINFO, &dkw) != -1) {
    311 		if (entry > 0)
    312 			/* wedges and indexes are mutually exclusive */
    313 			goto usage;
    314 		start = dkw.dkw_offset;
    315 		size = dkw.dkw_size;
    316 		ngpt = gpt_open(dkw.dkw_parent, gpt->flags, gpt->verbose,
    317 		    0, 0, 0);
    318 		if (ngpt == NULL)
    319 			goto cleanup;
    320 	}
    321 #endif
    322 	if (biosboot(ngpt, start, size, entry, label, bootpath, active) == -1)
    323 		goto cleanup;
    324 	if (ngpt != gpt)
    325 		gpt_close(ngpt);
    326 	return 0;
    327 usage:
    328 	usage();
    329 cleanup:
    330 	if (ngpt != gpt)
    331 		gpt_close(ngpt);
    332 	free(bootpath);
    333 	free(label);
    334 	return -1;
    335 }
    336