1/*	$OpenBSD: bootstrap.c,v 1.13 2019/06/28 13:32:48 deraadt 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
34void
35bootstrap(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 == -1)
64		err(1, "open %s", bootfile);
65	if (fstat(fd, &sb) == -1)
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 fit - partitions must not overlap,
82	 * or if they do, the partition type must be either FS_BOOT or
83	 * FS_UNUSED. The 'c' partition will always overlap and is ignored.
84	 */
85	if (verbose)
86		fprintf(stderr, "ensuring used partitions do not overlap "
87		    "with bootstrap sectors 0-%zu\n", bootsec);
88	for (i = 0; i < dl.d_npartitions; i++) {
89		part = 'a' + i;
90		pp = &dl.d_partitions[i];
91		if (i == RAW_PART)
92			continue;
93		if (DL_GETPSIZE(pp) == 0)
94			continue;
95		if ((u_int64_t)bootsec <= DL_GETPOFFSET(pp))
96			continue;
97		switch (pp->p_fstype) {
98		case FS_BOOT:
99			break;
100		case FS_UNUSED:
101			warnx("bootstrap overlaps with unused partition %c",
102			    part);
103			break;
104		default:
105			errx(1, "bootstrap overlaps with partition %c", part);
106		}
107	}
108
109	/*
110	 * Make sure the bootstrap has left space for the disklabel.
111	 * N.B.: LABELSECTOR *is* a DEV_BSIZE quantity!
112	 */
113	lp = (struct disklabel *)(boot + (LABELSECTOR * DEV_BSIZE) +
114	    LABELOFFSET);
115	for (i = 0, p = (char *)lp; i < (int)sizeof(*lp); i++)
116		if (p[i] != 0)
117			errx(1, "bootstrap has data in disklabel area");
118
119	/* Patch the disklabel into the bootstrap code. */
120	memcpy(lp, &dl, sizeof(dl));
121
122	/* Write the bootstrap out to the disk. */
123	if (verbose)
124		fprintf(stderr, "%s bootstrap to disk\n",
125		    (nowrite ? "would write" : "writing"));
126	if (nowrite)
127		return;
128	if (pwrite(devfd, boot, bootsize, 0) != (ssize_t)bootsize)
129		err(1, "pwrite");
130}
131