xref: /openbsd-src/usr.sbin/installboot/bootstrap.c (revision f6b75673f6c960a9743bfd16c1e52dd100265c68)
1 /*	$OpenBSD: bootstrap.c,v 1.11 2018/11/25 17:34:37 krw Exp $	*/
2 
3 /*
4  * Copyright (c) 2013 Joel Sing <jsing@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>	/* DEV_BSIZE */
20 #include <sys/disklabel.h>
21 #include <sys/dkio.h>
22 #include <sys/ioctl.h>
23 #include <sys/stat.h>
24 
25 #include <err.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 
32 #include "installboot.h"
33 
34 void
35 bootstrap(int devfd, char *dev, char *bootfile)
36 {
37 	struct disklabel dl;
38 	struct disklabel *lp;
39 	struct partition *pp;
40 	char *boot, *p, part;
41 	size_t bootsize;
42 	size_t bootsec;
43 	struct stat sb;
44 	int fd, i;
45 
46 	/*
47 	 * Install bootstrap code onto the given disk, preserving the
48 	 * existing disklabel.
49 	 */
50 
51 	/* Read disklabel from disk. */
52 	if (ioctl(devfd, DIOCGDINFO, &dl) == -1)
53 		err(1, "disklabel");
54 	if (dl.d_secsize == 0) {
55 		warnx("disklabel has sector size of 0, assuming %d", DEV_BSIZE);
56 		dl.d_secsize = DEV_BSIZE;
57 	}
58 
59 	/* Read bootstrap file. */
60 	if (verbose)
61 		fprintf(stderr, "reading bootstrap from %s\n", bootfile);
62 	fd = open(bootfile, O_RDONLY);
63 	if (fd < 0)
64 		err(1, "open %s", bootfile);
65 	if (fstat(fd, &sb) != 0)
66 		err(1, "fstat %s", bootfile);
67 	bootsec = howmany((ssize_t)sb.st_size, dl.d_secsize);
68 	bootsize = bootsec * dl.d_secsize;
69 	if (verbose)
70 		fprintf(stderr, "bootstrap is %zu bytes "
71 		    "(%zu sectors @ %u bytes = %zu bytes)\n",
72 		    (ssize_t)sb.st_size, bootsec, dl.d_secsize, bootsize);
73 	boot = calloc(1, bootsize);
74 	if (boot == NULL)
75 		err(1, "calloc");
76 	if (read(fd, boot, bootsize) != (ssize_t)sb.st_size)
77 		err(1, "read");
78 	close(fd);
79 
80 	/*
81 	 * Check that the bootstrap will not intrude into the OpenBSD area
82 	 * of the disk.
83 	 */
84 	if (bootsec > DL_GETBSTART(&dl))
85 		errx(1, "bootstrap (sectors 0 - %zu) overlaps OpenBSD "
86 		    " area (sectors %zu - %zu)", bootsec,
87 		    DL_GETBSTART(&dl), DL_GETBEND(&dl));
88 	else if (verbose)
89 		fprintf(stderr, "bootstrap (sectors 0 - %zu) does not overlap "
90 		    "OpenBSD area.\n", bootsec);
91 
92 	/*
93 	 * Make sure the bootstrap has left space for the disklabel.
94 	 * N.B.: LABELSECTOR *is* a DEV_BSIZE quantity!
95 	 */
96 	lp = (struct disklabel *)(boot + (LABELSECTOR * DEV_BSIZE) +
97 	    LABELOFFSET);
98 	for (i = 0, p = (char *)lp; i < (int)sizeof(*lp); i++)
99 		if (p[i] != 0)
100 			errx(1, "bootstrap has data in disklabel area");
101 
102 	/* Patch the disklabel into the bootstrap code. */
103 	memcpy(lp, &dl, sizeof(dl));
104 
105 	/* Write the bootstrap out to the disk. */
106 	if (verbose)
107 		fprintf(stderr, "%s bootstrap to disk\n",
108 		    (nowrite ? "would write" : "writing"));
109 	if (nowrite)
110 		return;
111 	if (pwrite(devfd, boot, bootsize, 0) != (ssize_t)bootsize)
112 		err(1, "pwrite");
113 }
114