1 /* $OpenBSD: octeon_installboot.c,v 1.8 2022/09/27 11:42:16 kn 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 int create_filesystem(struct disklabel *, char); 57 static void write_filesystem(struct disklabel *, char); 58 static int findmbrfat(int, struct disklabel *); 59 60 void 61 md_init(void) 62 { 63 stages = 1; 64 stage1 = "/usr/mdec/boot"; 65 } 66 67 void 68 md_loadboot(void) 69 { 70 } 71 72 void 73 md_prepareboot(int devfd, char *dev) 74 { 75 struct disklabel dl; 76 int part; 77 78 /* Get and check disklabel. */ 79 if (ioctl(devfd, DIOCGDINFO, &dl) == -1) 80 err(1, "disklabel: %s", dev); 81 if (dl.d_magic != DISKMAGIC) 82 errx(1, "bad disklabel magic=0x%08x", dl.d_magic); 83 84 /* Warn on unknown disklabel types. */ 85 if (dl.d_type == 0) 86 warnx("disklabel type unknown"); 87 88 part = findmbrfat(devfd, &dl); 89 if (part != -1) { 90 create_filesystem(&dl, (char)part); 91 return; 92 } 93 } 94 95 void 96 md_installboot(int devfd, char *dev) 97 { 98 struct disklabel dl; 99 int part; 100 101 /* Get and check disklabel. */ 102 if (ioctl(devfd, DIOCGDINFO, &dl) == -1) 103 err(1, "disklabel: %s", dev); 104 if (dl.d_magic != DISKMAGIC) 105 errx(1, "bad disklabel magic=0x%08x", dl.d_magic); 106 107 /* Warn on unknown disklabel types. */ 108 if (dl.d_type == 0) 109 warnx("disklabel type unknown"); 110 111 part = findmbrfat(devfd, &dl); 112 if (part != -1) { 113 write_filesystem(&dl, (char)part); 114 return; 115 } 116 } 117 118 static int 119 create_filesystem(struct disklabel *dl, char part) 120 { 121 static const char *newfsfmt = "/sbin/newfs -t msdos %s >/dev/null"; 122 struct msdosfs_args args; 123 char cmd[60]; 124 int rslt; 125 126 /* Mount <duid>.<part> as msdos filesystem. */ 127 memset(&args, 0, sizeof(args)); 128 rslt = asprintf(&args.fspec, 129 "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx.%c", 130 dl->d_uid[0], dl->d_uid[1], dl->d_uid[2], dl->d_uid[3], 131 dl->d_uid[4], dl->d_uid[5], dl->d_uid[6], dl->d_uid[7], 132 part); 133 if (rslt == -1) { 134 warn("bad special device"); 135 return rslt; 136 } 137 138 rslt = snprintf(cmd, sizeof(cmd), newfsfmt, args.fspec); 139 if (rslt >= sizeof(cmd)) { 140 warnx("can't build newfs command"); 141 rslt = -1; 142 return rslt; 143 } 144 145 if (verbose) 146 fprintf(stderr, "%s %s\n", 147 (nowrite ? "would newfs" : "newfsing"), args.fspec); 148 if (!nowrite) { 149 rslt = system(cmd); 150 if (rslt == -1) { 151 warn("system('%s') failed", cmd); 152 return rslt; 153 } 154 } 155 156 return 0; 157 } 158 159 static void 160 write_filesystem(struct disklabel *dl, char part) 161 { 162 static char *fsckfmt = "/sbin/fsck -t msdos %s >/dev/null"; 163 struct msdosfs_args args; 164 char cmd[60]; 165 char dst[PATH_MAX]; 166 size_t mntlen, pathlen; 167 int rslt; 168 169 /* Create directory for temporary mount point. */ 170 strlcpy(dst, "/tmp/installboot.XXXXXXXXXX", sizeof(dst)); 171 if (mkdtemp(dst) == NULL) 172 err(1, "mkdtemp('%s') failed", dst); 173 mntlen = strlen(dst); 174 175 /* Mount <duid>.<part> as msdos filesystem. */ 176 memset(&args, 0, sizeof(args)); 177 rslt = asprintf(&args.fspec, 178 "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx.%c", 179 dl->d_uid[0], dl->d_uid[1], dl->d_uid[2], dl->d_uid[3], 180 dl->d_uid[4], dl->d_uid[5], dl->d_uid[6], dl->d_uid[7], 181 part); 182 if (rslt == -1) { 183 warn("bad special device"); 184 goto rmdir; 185 } 186 187 args.export_info.ex_root = -2; 188 args.export_info.ex_flags = 0; 189 args.flags = MSDOSFSMNT_LONGNAME; 190 191 if (mount(MOUNT_MSDOS, dst, 0, &args) == -1) { 192 /* Try fsck'ing it. */ 193 rslt = snprintf(cmd, sizeof(cmd), fsckfmt, args.fspec); 194 if (rslt >= sizeof(cmd)) { 195 warnx("can't build fsck command"); 196 rslt = -1; 197 goto rmdir; 198 } 199 rslt = system(cmd); 200 if (rslt == -1) { 201 warn("system('%s') failed", cmd); 202 goto rmdir; 203 } 204 if (mount(MOUNT_MSDOS, dst, 0, &args) == -1) { 205 /* Try newfs'ing it. */ 206 rslt = create_filesystem(dl, part); 207 if (rslt == -1) 208 goto rmdir; 209 rslt = mount(MOUNT_MSDOS, dst, 0, &args); 210 if (rslt == -1) { 211 warn("unable to mount MSDOS partition"); 212 goto rmdir; 213 } 214 } 215 } 216 217 /* 218 * Copy /usr/mdec/boot to /mnt/boot. 219 */ 220 pathlen = strlen(dst); 221 if (strlcat(dst, "/boot", sizeof(dst)) >= sizeof(dst)) { 222 rslt = -1; 223 warn("unable to build /boot path"); 224 goto umount; 225 } 226 if (verbose) 227 fprintf(stderr, "%s %s to %s\n", 228 (nowrite ? "would copy" : "copying"), stage1, dst); 229 if (!nowrite) { 230 rslt = filecopy(stage1, dst); 231 if (rslt == -1) 232 goto umount; 233 } 234 235 rslt = 0; 236 237 umount: 238 dst[mntlen] = '\0'; 239 if (unmount(dst, MNT_FORCE) == -1) 240 err(1, "unmount('%s') failed", dst); 241 242 rmdir: 243 free(args.fspec); 244 dst[mntlen] = '\0'; 245 if (rmdir(dst) == -1) 246 err(1, "rmdir('%s') failed", dst); 247 248 if (rslt == -1) 249 exit(1); 250 } 251 252 int 253 findmbrfat(int devfd, struct disklabel *dl) 254 { 255 struct dos_partition dp[NDOSPART]; 256 ssize_t len; 257 u_int64_t start = 0; 258 int i; 259 u_int8_t *secbuf; 260 261 if ((secbuf = malloc(dl->d_secsize)) == NULL) 262 err(1, NULL); 263 264 /* Read MBR. */ 265 len = pread(devfd, secbuf, dl->d_secsize, 0); 266 if (len != dl->d_secsize) 267 err(4, "can't read mbr"); 268 memcpy(dp, &secbuf[DOSPARTOFF], sizeof(dp)); 269 270 for (i = 0; i < NDOSPART; i++) { 271 if (dp[i].dp_typ == DOSPTYP_UNUSED) 272 continue; 273 if (dp[i].dp_typ == DOSPTYP_FAT16L || 274 dp[i].dp_typ == DOSPTYP_FAT32L || 275 dp[i].dp_typ == DOSPTYP_FAT16B) 276 start = letoh32(dp[i].dp_start); 277 } 278 279 free(secbuf); 280 281 if (start) { 282 for (i = 0; i < MAXPARTITIONS; i++) { 283 if (DL_GETPSIZE(&dl->d_partitions[i]) > 0 && 284 DL_GETPOFFSET(&dl->d_partitions[i]) == start) 285 return ('a' + i); 286 } 287 } 288 289 return (-1); 290 } 291