1 /*	$OpenBSD: macppc_installboot.c,v 1.3 2020/07/22 05:06:38 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 	args.flags = MSDOSFSMNT_LONGNAME;
128 
129 	if (mount(MOUNT_MSDOS, dst, 0, &args) == -1) {
130 		/* Try fsck'ing it. */
131 		rslt = snprintf(cmd, sizeof(cmd), fsckfmt, args.fspec);
132 		if (rslt >= sizeof(cmd)) {
133 			warnx("can't build fsck command");
134 			rslt = -1;
135 			goto rmdir;
136 		}
137 		rslt = system(cmd);
138 		if (rslt == -1) {
139 			warn("system('%s') failed", cmd);
140 			goto rmdir;
141 		}
142 		if (mount(MOUNT_MSDOS, dst, 0, &args) == -1) {
143 			/* Try newfs'ing it. */
144 			rslt = snprintf(cmd, sizeof(cmd), newfsfmt,
145 			    args.fspec);
146 			if (rslt >= sizeof(cmd)) {
147 				warnx("can't build newfs command");
148 				rslt = -1;
149 				goto rmdir;
150 			}
151 			rslt = system(cmd);
152 			if (rslt == -1) {
153 				warn("system('%s') failed", cmd);
154 				goto rmdir;
155 			}
156 			rslt = mount(MOUNT_MSDOS, dst, 0, &args);
157 			if (rslt == -1) {
158 				warn("unable to mount MSDOS partition");
159 				goto rmdir;
160 			}
161 		}
162 	}
163 
164 	/*
165 	 * Copy /usr/mdec/ofwboot to $FS/ofwboot.
166 	 */
167 	pathlen = strlen(dst);
168 	if (strlcat(dst, "/ofwboot", sizeof(dst)) >= sizeof(dst)) {
169 		rslt = -1;
170 		warn("unable to build /ofwboot path");
171 		goto umount;
172 	}
173 	src = fileprefix(root, "/usr/mdec/ofwboot");
174 	if (src == NULL) {
175 		rslt = -1;
176 		goto umount;
177 	}
178 	srclen = strlen(src);
179 	if (verbose)
180 		fprintf(stderr, "%s %s to %s\n",
181 		    (nowrite ? "would copy" : "copying"), src, dst);
182 	if (!nowrite) {
183 		rslt = filecopy(src, dst);
184 		if (rslt == -1)
185 			goto umount;
186 	}
187 
188 	rslt = 0;
189 
190 umount:
191 	dst[mntlen] = '\0';
192 	if (unmount(dst, MNT_FORCE) == -1)
193 		err(1, "unmount('%s') failed", dst);
194 
195 rmdir:
196 	free(args.fspec);
197 	dst[mntlen] = '\0';
198 	if (rmdir(dst) == -1)
199 		err(1, "rmdir('%s') failed", dst);
200 
201 	free(src);
202 
203 	if (rslt == -1)
204 		exit(1);
205 }
206 
207 int
208 findmbrfat(int devfd, struct disklabel *dl)
209 {
210 	struct dos_partition	 dp[NDOSPART];
211 	ssize_t			 len;
212 	u_int64_t		 start = 0;
213 	int			 i;
214 	u_int8_t		*secbuf;
215 
216 	if ((secbuf = malloc(dl->d_secsize)) == NULL)
217 		err(1, NULL);
218 
219 	/* Read MBR. */
220 	len = pread(devfd, secbuf, dl->d_secsize, 0);
221 	if (len != dl->d_secsize)
222 		err(4, "can't read mbr");
223 	memcpy(dp, &secbuf[DOSPARTOFF], sizeof(dp));
224 
225 	for (i = 0; i < NDOSPART; i++) {
226 		if (dp[i].dp_typ == DOSPTYP_UNUSED)
227 			continue;
228 		if (dp[i].dp_typ == DOSPTYP_FAT16L ||
229 		    dp[i].dp_typ == DOSPTYP_FAT32L ||
230 		    dp[i].dp_typ == DOSPTYP_FAT16B)
231 			start = letoh32(dp[i].dp_start);
232 	}
233 
234 	free(secbuf);
235 
236 	if (start) {
237 		for (i = 0; i < MAXPARTITIONS; i++) {
238 			if (DL_GETPSIZE(&dl->d_partitions[i]) > 0 &&
239 			    DL_GETPOFFSET(&dl->d_partitions[i]) == start)
240 				return ('a' + i);
241 		}
242 	}
243 
244 	return (-1);
245 }
246