xref: /openbsd-src/usr.sbin/installboot/octeon_installboot.c (revision 505ee9ea3b177e2387d907a91ca7da069f3f14d8)
1 /*	$OpenBSD: octeon_installboot.c,v 1.2 2020/07/18 16:42:00 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 2011 Joel Sing <jsing@openbsd.org>
5  * Copyright (c) 2010 Otto Moerbeek <otto@openbsd.org>
6  * Copyright (c) 2003 Tom Cosgrove <tom.cosgrove@arches-consulting.com>
7  * Copyright (c) 1997 Michael Shalayeff
8  * Copyright (c) 1994 Paul Kranenburg
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by Paul Kranenburg.
22  * 4. The name of the author may not be used to endorse or promote products
23  *    derived from this software without specific prior written permission
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #include <sys/param.h>	/* DEV_BSIZE */
38 #include <sys/disklabel.h>
39 #include <sys/dkio.h>
40 #include <sys/ioctl.h>
41 #include <sys/mount.h>
42 #include <sys/stat.h>
43 
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <util.h>
52 #include <endian.h>
53 
54 #include "installboot.h"
55 
56 static void	write_filesystem(struct disklabel *, char);
57 static int	findmbrfat(int, struct disklabel *);
58 
59 void
60 md_init(void)
61 {
62 }
63 
64 void
65 md_loadboot(void)
66 {
67 }
68 
69 void
70 md_installboot(int devfd, char *dev)
71 {
72 	struct disklabel dl;
73 	int part;
74 
75 	/* Get and check disklabel. */
76 	if (ioctl(devfd, DIOCGDINFO, &dl) == -1)
77 		err(1, "disklabel: %s", dev);
78 	if (dl.d_magic != DISKMAGIC)
79 		errx(1, "bad disklabel magic=0x%08x", dl.d_magic);
80 
81 	/* Warn on unknown disklabel types. */
82 	if (dl.d_type == 0)
83 		warnx("disklabel type unknown");
84 
85 	part = findmbrfat(devfd, &dl);
86 	if (part != -1) {
87 		write_filesystem(&dl, (char)part);
88 		return;
89 	}
90 }
91 
92 
93 static void
94 write_filesystem(struct disklabel *dl, char part)
95 {
96 	static char *fsckfmt = "/sbin/fsck_msdos %s >/dev/null";
97 	static char *newfsfmt ="/sbin/newfs_msdos %s >/dev/null";
98 	struct msdosfs_args args;
99 	char cmd[60];
100 	char dst[PATH_MAX];
101 	char *src;
102 	size_t mntlen, pathlen, srclen;
103 	int rslt;
104 
105 	src = NULL;
106 
107 	/* Create directory for temporary mount point. */
108 	strlcpy(dst, "/tmp/installboot.XXXXXXXXXX", sizeof(dst));
109 	if (mkdtemp(dst) == NULL)
110 		err(1, "mkdtemp('%s') failed", dst);
111 	mntlen = strlen(dst);
112 
113 	/* Mount <duid>.<part> as msdos filesystem. */
114 	memset(&args, 0, sizeof(args));
115 	rslt = asprintf(&args.fspec,
116 	    "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx.%c",
117             dl->d_uid[0], dl->d_uid[1], dl->d_uid[2], dl->d_uid[3],
118             dl->d_uid[4], dl->d_uid[5], dl->d_uid[6], dl->d_uid[7],
119 	    part);
120 	if (rslt == -1) {
121 		warn("bad special device");
122 		goto rmdir;
123 	}
124 
125 	args.export_info.ex_root = -2;
126 	args.export_info.ex_flags = 0;
127 
128 	if (mount(MOUNT_MSDOS, dst, 0, &args) == -1) {
129 		/* Try fsck'ing it. */
130 		rslt = snprintf(cmd, sizeof(cmd), fsckfmt, args.fspec);
131 		if (rslt >= sizeof(cmd)) {
132 			warnx("can't build fsck command");
133 			rslt = -1;
134 			goto rmdir;
135 		}
136 		rslt = system(cmd);
137 		if (rslt == -1) {
138 			warn("system('%s') failed", cmd);
139 			goto rmdir;
140 		}
141 		if (mount(MOUNT_MSDOS, dst, 0, &args) == -1) {
142 			/* Try newfs'ing it. */
143 			rslt = snprintf(cmd, sizeof(cmd), newfsfmt,
144 			    args.fspec);
145 			if (rslt >= sizeof(cmd)) {
146 				warnx("can't build newfs command");
147 				rslt = -1;
148 				goto rmdir;
149 			}
150 			rslt = system(cmd);
151 			if (rslt == -1) {
152 				warn("system('%s') failed", cmd);
153 				goto rmdir;
154 			}
155 			rslt = mount(MOUNT_MSDOS, dst, 0, &args);
156 			if (rslt == -1) {
157 				warn("unable to mount MSDOS partition");
158 				goto rmdir;
159 			}
160 		}
161 	}
162 
163 	/*
164 	 * Copy /usr/mdec/boot to /mnt/boot.
165 	 */
166 	pathlen = strlen(dst);
167 	if (strlcat(dst, "/boot", sizeof(dst)) >= sizeof(dst)) {
168 		rslt = -1;
169 		warn("unable to build /boot path");
170 		goto umount;
171 	}
172 	src = fileprefix(root, "/usr/mdec/boot");
173 	if (src == NULL) {
174 		rslt = -1;
175 		goto umount;
176 	}
177 	srclen = strlen(src);
178 	if (verbose)
179 		fprintf(stderr, "%s %s to %s\n",
180 		    (nowrite ? "would copy" : "copying"), src, dst);
181 	if (!nowrite) {
182 		rslt = filecopy(src, dst);
183 		if (rslt == -1)
184 			goto umount;
185 	}
186 
187 	rslt = 0;
188 
189 umount:
190 	dst[mntlen] = '\0';
191 	if (unmount(dst, MNT_FORCE) == -1)
192 		err(1, "unmount('%s') failed", dst);
193 
194 rmdir:
195 	free(args.fspec);
196 	dst[mntlen] = '\0';
197 	if (rmdir(dst) == -1)
198 		err(1, "rmdir('%s') failed", dst);
199 
200 	free(src);
201 
202 	if (rslt == -1)
203 		exit(1);
204 }
205 
206 int
207 findmbrfat(int devfd, struct disklabel *dl)
208 {
209 	struct dos_partition	 dp[NDOSPART];
210 	ssize_t			 len;
211 	u_int64_t		 start = 0;
212 	int			 i;
213 	u_int8_t		*secbuf;
214 
215 	if ((secbuf = malloc(dl->d_secsize)) == NULL)
216 		err(1, NULL);
217 
218 	/* Read MBR. */
219 	len = pread(devfd, secbuf, dl->d_secsize, 0);
220 	if (len != dl->d_secsize)
221 		err(4, "can't read mbr");
222 	memcpy(dp, &secbuf[DOSPARTOFF], sizeof(dp));
223 
224 	for (i = 0; i < NDOSPART; i++) {
225 		if (dp[i].dp_typ == DOSPTYP_UNUSED)
226 			continue;
227 		if (dp[i].dp_typ == DOSPTYP_FAT16L ||
228 		    dp[i].dp_typ == DOSPTYP_FAT32L ||
229 		    dp[i].dp_typ == DOSPTYP_FAT16B)
230 			start = letoh32(dp[i].dp_start);
231 	}
232 
233 	free(secbuf);
234 
235 	if (start) {
236 		for (i = 0; i < MAXPARTITIONS; i++) {
237 			if (DL_GETPSIZE(&dl->d_partitions[i]) > 0 &&
238 			    DL_GETPOFFSET(&dl->d_partitions[i]) == start)
239 				return ('a' + i);
240 		}
241 	}
242 
243 	return (-1);
244 }
245