1 /*	$OpenBSD: landisk_installboot.c,v 1.12 2022/12/28 21:30:16 jmc 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 <ufs/ffs/fs.h>
26 
27 #include <err.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 
34 #include "installboot.h"
35 
36 void	md_bootstrap(int, char *, char *);
37 
38 char	*bootldr;
39 
40 void
41 md_init(void)
42 {
43 	stages = 2;
44 	stage1 = "/usr/mdec/xxboot";
45 	stage2 = "/usr/mdec/boot";
46 
47 	bootldr = "/boot";
48 }
49 
50 void
51 md_loadboot(void)
52 {
53 }
54 
55 void
56 md_prepareboot(int devfd, char *dev)
57 {
58 }
59 
60 void
61 md_installboot(int devfd, char *dev)
62 {
63 	/* XXX - is this necessary? */
64 	sync();
65 
66 	bootldr = fileprefix(root, bootldr);
67 	if (bootldr == NULL)
68 		exit(1);
69 	if (!nowrite)
70 		if (filecopy(stage2, bootldr) == -1)
71 			exit(1);
72 
73 	/*
74 	 * Write bootblock into the beginning of the OpenBSD partition or
75 	 * at the beginning of the disk.
76 	 */
77 	md_bootstrap(devfd, dev, stage1);
78 }
79 
80 void
81 md_bootstrap(int devfd, char *dev, char *bootfile)
82 {
83 	struct disklabel dl;
84 	struct disklabel *lp;
85 	struct partition *pp;
86 	char *boot, *p, part;
87 	size_t bootsize;
88 	size_t bootsec;
89 	struct stat sb;
90 	daddr_t bootpos = 0;
91 	int fd, i;
92 
93 	/*
94 	 * Install bootstrap code onto the given disk, preserving the
95 	 * existing disklabel.
96 	 */
97 
98 	/* Read disklabel from disk. */
99 	if (ioctl(devfd, DIOCGDINFO, &dl) == -1)
100 		err(1, "disklabel");
101 	if (dl.d_secsize == 0) {
102 		warnx("disklabel has sector size of 0, assuming %d", DEV_BSIZE);
103 		dl.d_secsize = DEV_BSIZE;
104 	}
105 
106 	/* Read bootstrap file. */
107 	if (verbose)
108 		fprintf(stderr, "reading bootstrap from %s\n", bootfile);
109 	fd = open(bootfile, O_RDONLY);
110 	if (fd == -1)
111 		err(1, "open %s", bootfile);
112 	if (fstat(fd, &sb) == -1)
113 		err(1, "fstat %s", bootfile);
114 	bootsec = howmany((ssize_t)sb.st_size, dl.d_secsize);
115 	bootsize = bootsec * dl.d_secsize;
116 	if (verbose)
117 		fprintf(stderr, "bootstrap is %zu bytes "
118 		    "(%zu sectors @ %u bytes = %zu bytes)\n",
119 		    (ssize_t)sb.st_size, bootsec, dl.d_secsize, bootsize);
120 	boot = calloc(1, bootsize);
121 	if (boot == NULL)
122 		err(1, "calloc");
123 	if (read(fd, boot, bootsize) != (ssize_t)sb.st_size)
124 		err(1, "read");
125 	close(fd);
126 
127 	/*
128 	 * The landisk bootstrap can work when put either at the start of the
129 	 * disk, or at the start of an OpenBSD MBR partition, invoked from
130 	 * /usr/mdec/mbr.
131 	 * Check for a partition table in order to decide where to put the
132 	 * first-level bootstrap code.
133 	 */
134 
135 	if (dl.d_secsize >= sizeof(struct dos_mbr)) {
136 		uint8_t *secbuf;
137 		struct dos_mbr *mbr;
138 
139 		secbuf = malloc(dl.d_secsize);
140 		if (secbuf == NULL)
141 			err(1, "malloc");
142 
143 		/* Read MBR. */
144 		if (pread(devfd, secbuf, dl.d_secsize, 0) != dl.d_secsize)
145 			err(4, "can't read mbr");
146 		mbr = (struct dos_mbr *)secbuf;
147 		/* safe check because landisk is little endian */
148 		if (mbr->dmbr_sign == DOSMBR_SIGNATURE) {
149 			for (i = 0; i < NDOSPART; i++) {
150 				if (mbr->dmbr_parts[i].dp_typ ==
151 				    DOSPTYP_OPENBSD) {
152 					bootpos = mbr->dmbr_parts[i].dp_start;
153 					break;
154 				}
155 			}
156 		}
157 
158 		free(secbuf);
159 	}
160 
161 	if (bootpos == 0) {
162 		/*
163 		 * Installing at the start of the disk.
164 		 * Check that the bootstrap will fit - partitions must not
165 		 * overlap, or if they do, the partition type must be either
166 		 * FS_BOOT or FS_UNUSED. The 'c' partition will always overlap
167 		 * and is ignored.
168 		 */
169 		if (verbose)
170 			fprintf(stderr,
171 			    "ensuring used partitions do not overlap "
172 			    "with bootstrap sectors 0-%zu\n", bootsec);
173 		for (i = 0; i < dl.d_npartitions; i++) {
174 			part = 'a' + i;
175 			pp = &dl.d_partitions[i];
176 			if (i == RAW_PART)
177 				continue;
178 			if (DL_GETPSIZE(pp) == 0)
179 				continue;
180 			if (bootpos + (u_int64_t)bootsec <= DL_GETPOFFSET(pp))
181 				continue;
182 			switch (pp->p_fstype) {
183 			case FS_BOOT:
184 				break;
185 			case FS_UNUSED:
186 				warnx("bootstrap overlaps "
187 				    "with unused partition %c", part);
188 				break;
189 			default:
190 				errx(1, "bootstrap overlaps with partition %c",
191 				    part);
192 			}
193 		}
194 	} else {
195 		/*
196 		 * Installing at the start of the OpenBSD partition.
197 		 * We only need to ensure the bootstrap code fits in the
198 		 * BBSIZE reserved area at the beginning of the file
199 		 * system.
200 		 */
201 		if (bootsize > BBSIZE)
202 			errx(1, "bootstrap is too large");
203 	}
204 
205 	/*
206 	 * Make sure the bootstrap has left space for the disklabel.
207 	 * N.B.: LABELSECTOR *is* a DEV_BSIZE quantity!
208 	 */
209 	lp = (struct disklabel *)(boot + (LABELSECTOR * DEV_BSIZE) +
210 	    LABELOFFSET);
211 	for (i = 0, p = (char *)lp; i < (int)sizeof(*lp); i++)
212 		if (p[i] != 0)
213 			errx(1, "bootstrap has data in disklabel area");
214 
215 	/* Patch the disklabel into the bootstrap code. */
216 	memcpy(lp, &dl, sizeof(dl));
217 
218 	/* Write the bootstrap out to the disk. */
219 	bootpos *= dl.d_secsize;
220 	if (verbose)
221 		fprintf(stderr, "%s bootstrap to disk at offset %llx\n",
222 		    (nowrite ? "would write" : "writing"), bootpos);
223 	if (nowrite)
224 		return;
225 	if (pwrite(devfd, boot, bootsize, bootpos) != (ssize_t)bootsize)
226 		err(1, "pwrite");
227 }
228