xref: /openbsd-src/usr.sbin/installboot/loongson_installboot.c (revision 1a8dbaac879b9f3335ad7fb25429ce63ac1d6bac)
1 /*	$OpenBSD: loongson_installboot.c,v 1.3 2020/07/19 15:23:08 visa Exp $	*/
2 /*	$NetBSD: installboot.c,v 1.5 1995/11/17 23:23:50 gwr Exp $ */
3 
4 /*
5  * Copyright (c) 2011 Joel Sing <jsing@openbsd.org>
6  * Copyright (c) 2010 Otto Moerbeek <otto@openbsd.org>
7  * Copyright (c) 2003 Tom Cosgrove <tom.cosgrove@arches-consulting.com>
8  * Copyright (c) 1997 Michael Shalayeff
9  * Copyright (c) 1994 Paul Kranenburg
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *      This product includes software developed by Paul Kranenburg.
23  * 4. The name of the author may not be used to endorse or promote products
24  *    derived from this software without specific prior written permission
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <sys/param.h>	/* DEV_BSIZE */
39 #include <sys/disklabel.h>
40 #include <sys/dkio.h>
41 #include <sys/ioctl.h>
42 #include <sys/mount.h>
43 #include <sys/stat.h>
44 
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <util.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_ext2fs %s >/dev/null";
97 	static char *newfsfmt ="/sbin/newfs_ext2fs %s >/dev/null";
98 	struct ufs_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 ext2fs 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_EXT2FS, 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_EXT2FS, 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_EXT2FS, dst, 0, &args);
156 			if (rslt == -1) {
157 				warn("unable to mount ext2fs partition");
158 				goto rmdir;
159 			}
160 		}
161 	}
162 
163 	/* Create "/boot" directory in <duid>.<part>. */
164 	if (strlcat(dst, "/boot", sizeof(dst)) >= sizeof(dst)) {
165 		rslt = -1;
166 		warn("unable to build /boot directory");
167 		goto umount;
168 	}
169 	rslt = mkdir(dst, 0755);
170 	if (rslt == -1 && errno != EEXIST) {
171 		warn("mkdir('%s') failed", dst);
172 		goto umount;
173 	}
174 
175 	/*
176 	 * Copy /usr/mdec/boot to /boot/boot.
177 	 */
178 	pathlen = strlen(dst);
179 	if (strlcat(dst, "/boot", sizeof(dst)) >= sizeof(dst)) {
180 		rslt = -1;
181 		warn("unable to build /boot path");
182 		goto umount;
183 	}
184 	src = fileprefix(root, "/usr/mdec/boot");
185 	if (src == NULL) {
186 		rslt = -1;
187 		goto umount;
188 	}
189 	srclen = strlen(src);
190 	if (verbose)
191 		fprintf(stderr, "%s %s to %s\n",
192 		    (nowrite ? "would copy" : "copying"), src, dst);
193 	if (!nowrite) {
194 		rslt = filecopy(src, dst);
195 		if (rslt == -1)
196 			goto umount;
197 	}
198 
199 	rslt = 0;
200 
201 umount:
202 	dst[mntlen] = '\0';
203 	if (unmount(dst, MNT_FORCE) == -1)
204 		err(1, "unmount('%s') failed", dst);
205 
206 rmdir:
207 	free(args.fspec);
208 	dst[mntlen] = '\0';
209 	if (rmdir(dst) == -1)
210 		err(1, "rmdir('%s') failed", dst);
211 
212 	free(src);
213 
214 	if (rslt == -1)
215 		exit(1);
216 }
217 
218 int
219 findmbrfat(int devfd, struct disklabel *dl)
220 {
221 	struct dos_partition	 dp[NDOSPART];
222 	ssize_t			 len;
223 	u_int64_t		 start = 0;
224 	int			 i;
225 	u_int8_t		*secbuf;
226 
227 	if ((secbuf = malloc(dl->d_secsize)) == NULL)
228 		err(1, NULL);
229 
230 	/* Read MBR. */
231 	len = pread(devfd, secbuf, dl->d_secsize, 0);
232 	if (len != dl->d_secsize)
233 		err(4, "can't read mbr");
234 	memcpy(dp, &secbuf[DOSPARTOFF], sizeof(dp));
235 
236 	for (i = 0; i < NDOSPART; i++) {
237 		if (dp[i].dp_typ == DOSPTYP_UNUSED)
238 			continue;
239 		if (dp[i].dp_typ == DOSPTYP_LINUX)
240 			start = dp[i].dp_start;
241 	}
242 
243 	free(secbuf);
244 
245 	if (start) {
246 		for (i = 0; i < MAXPARTITIONS; i++) {
247 			if (DL_GETPSIZE(&dl->d_partitions[i]) > 0 &&
248 			    DL_GETPOFFSET(&dl->d_partitions[i]) == start)
249 				return ('a' + i);
250 		}
251 	}
252 
253 	return (-1);
254 }
255