xref: /openbsd-src/usr.sbin/installboot/bootstrap.c (revision df69c215c7c66baf660f3f65414fd34796c96152)
1*df69c215Sderaadt /*	$OpenBSD: bootstrap.c,v 1.13 2019/06/28 13:32:48 deraadt Exp $	*/
2935c4ba1Stobias 
333c4b066Sjsing /*
433c4b066Sjsing  * Copyright (c) 2013 Joel Sing <jsing@openbsd.org>
533c4b066Sjsing  *
633c4b066Sjsing  * Permission to use, copy, modify, and distribute this software for any
733c4b066Sjsing  * purpose with or without fee is hereby granted, provided that the above
833c4b066Sjsing  * copyright notice and this permission notice appear in all copies.
933c4b066Sjsing  *
1033c4b066Sjsing  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1133c4b066Sjsing  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1233c4b066Sjsing  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1333c4b066Sjsing  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1433c4b066Sjsing  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1533c4b066Sjsing  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1633c4b066Sjsing  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1733c4b066Sjsing  */
1833c4b066Sjsing 
199b2ea772Sderaadt #include <sys/param.h>	/* DEV_BSIZE */
2033c4b066Sjsing #include <sys/disklabel.h>
2133c4b066Sjsing #include <sys/dkio.h>
2233c4b066Sjsing #include <sys/ioctl.h>
2333c4b066Sjsing #include <sys/stat.h>
2433c4b066Sjsing 
25808c6500Sjsing #include <err.h>
2633c4b066Sjsing #include <fcntl.h>
2733c4b066Sjsing #include <stdio.h>
2833c4b066Sjsing #include <stdlib.h>
2933c4b066Sjsing #include <string.h>
3033c4b066Sjsing #include <unistd.h>
3133c4b066Sjsing 
3233c4b066Sjsing #include "installboot.h"
3333c4b066Sjsing 
34808c6500Sjsing void
bootstrap(int devfd,char * dev,char * bootfile)35f917af54Skrw bootstrap(int devfd, char *dev, char *bootfile)
3633c4b066Sjsing {
3733c4b066Sjsing 	struct disklabel dl;
3833c4b066Sjsing 	struct disklabel *lp;
3933c4b066Sjsing 	struct partition *pp;
4033c4b066Sjsing 	char *boot, *p, part;
41808c6500Sjsing 	size_t bootsize;
42f7ad209eSjsing 	size_t bootsec;
4333c4b066Sjsing 	struct stat sb;
44808c6500Sjsing 	int fd, i;
4533c4b066Sjsing 
4633c4b066Sjsing 	/*
4733c4b066Sjsing 	 * Install bootstrap code onto the given disk, preserving the
4833c4b066Sjsing 	 * existing disklabel.
4933c4b066Sjsing 	 */
5033c4b066Sjsing 
5133c4b066Sjsing 	/* Read disklabel from disk. */
5233c4b066Sjsing 	if (ioctl(devfd, DIOCGDINFO, &dl) == -1)
5333c4b066Sjsing 		err(1, "disklabel");
5433c4b066Sjsing 	if (dl.d_secsize == 0) {
5533c4b066Sjsing 		warnx("disklabel has sector size of 0, assuming %d", DEV_BSIZE);
5633c4b066Sjsing 		dl.d_secsize = DEV_BSIZE;
5733c4b066Sjsing 	}
5833c4b066Sjsing 
5933c4b066Sjsing 	/* Read bootstrap file. */
6033c4b066Sjsing 	if (verbose)
6133c4b066Sjsing 		fprintf(stderr, "reading bootstrap from %s\n", bootfile);
62808c6500Sjsing 	fd = open(bootfile, O_RDONLY);
63*df69c215Sderaadt 	if (fd == -1)
6433c4b066Sjsing 		err(1, "open %s", bootfile);
65*df69c215Sderaadt 	if (fstat(fd, &sb) == -1)
6633c4b066Sjsing 		err(1, "fstat %s", bootfile);
67f7ad209eSjsing 	bootsec = howmany((ssize_t)sb.st_size, dl.d_secsize);
68f7ad209eSjsing 	bootsize = bootsec * dl.d_secsize;
69f7ad209eSjsing 	if (verbose)
70f7ad209eSjsing 		fprintf(stderr, "bootstrap is %zu bytes "
71f7ad209eSjsing 		    "(%zu sectors @ %u bytes = %zu bytes)\n",
72f7ad209eSjsing 		    (ssize_t)sb.st_size, bootsec, dl.d_secsize, bootsize);
73210008b3Sjsing 	boot = calloc(1, bootsize);
7433c4b066Sjsing 	if (boot == NULL)
75210008b3Sjsing 		err(1, "calloc");
76f7ad209eSjsing 	if (read(fd, boot, bootsize) != (ssize_t)sb.st_size)
7733c4b066Sjsing 		err(1, "read");
78808c6500Sjsing 	close(fd);
7933c4b066Sjsing 
8033c4b066Sjsing 	/*
811f3b8215Skrw 	 * Check that the bootstrap will fit - partitions must not overlap,
821f3b8215Skrw 	 * or if they do, the partition type must be either FS_BOOT or
831f3b8215Skrw 	 * FS_UNUSED. The 'c' partition will always overlap and is ignored.
8433c4b066Sjsing 	 */
851f3b8215Skrw 	if (verbose)
861f3b8215Skrw 		fprintf(stderr, "ensuring used partitions do not overlap "
871f3b8215Skrw 		    "with bootstrap sectors 0-%zu\n", bootsec);
881f3b8215Skrw 	for (i = 0; i < dl.d_npartitions; i++) {
891f3b8215Skrw 		part = 'a' + i;
901f3b8215Skrw 		pp = &dl.d_partitions[i];
911f3b8215Skrw 		if (i == RAW_PART)
921f3b8215Skrw 			continue;
931f3b8215Skrw 		if (DL_GETPSIZE(pp) == 0)
941f3b8215Skrw 			continue;
951f3b8215Skrw 		if ((u_int64_t)bootsec <= DL_GETPOFFSET(pp))
961f3b8215Skrw 			continue;
971f3b8215Skrw 		switch (pp->p_fstype) {
981f3b8215Skrw 		case FS_BOOT:
991f3b8215Skrw 			break;
1001f3b8215Skrw 		case FS_UNUSED:
1011f3b8215Skrw 			warnx("bootstrap overlaps with unused partition %c",
1021f3b8215Skrw 			    part);
1031f3b8215Skrw 			break;
1041f3b8215Skrw 		default:
1051f3b8215Skrw 			errx(1, "bootstrap overlaps with partition %c", part);
1061f3b8215Skrw 		}
1071f3b8215Skrw 	}
10833c4b066Sjsing 
109569d6d7eSkrw 	/*
110569d6d7eSkrw 	 * Make sure the bootstrap has left space for the disklabel.
111569d6d7eSkrw 	 * N.B.: LABELSECTOR *is* a DEV_BSIZE quantity!
112569d6d7eSkrw 	 */
113569d6d7eSkrw 	lp = (struct disklabel *)(boot + (LABELSECTOR * DEV_BSIZE) +
11433c4b066Sjsing 	    LABELOFFSET);
115808c6500Sjsing 	for (i = 0, p = (char *)lp; i < (int)sizeof(*lp); i++)
11633c4b066Sjsing 		if (p[i] != 0)
11733c4b066Sjsing 			errx(1, "bootstrap has data in disklabel area");
11833c4b066Sjsing 
11933c4b066Sjsing 	/* Patch the disklabel into the bootstrap code. */
12033c4b066Sjsing 	memcpy(lp, &dl, sizeof(dl));
12133c4b066Sjsing 
12233c4b066Sjsing 	/* Write the bootstrap out to the disk. */
12333c4b066Sjsing 	if (verbose)
12433c4b066Sjsing 		fprintf(stderr, "%s bootstrap to disk\n",
12533c4b066Sjsing 		    (nowrite ? "would write" : "writing"));
12633c4b066Sjsing 	if (nowrite)
12733c4b066Sjsing 		return;
128c462e017Skrw 	if (pwrite(devfd, boot, bootsize, 0) != (ssize_t)bootsize)
129c462e017Skrw 		err(1, "pwrite");
13033c4b066Sjsing }
131