Annotation of sys/arch/amd64/stand/installboot/installboot.c, Revision 1.1
1.1 ! nbrk 1: /* $OpenBSD: installboot.c,v 1.6 2006/02/14 17:16:19 aaron Exp $ */
! 2: /* $NetBSD: installboot.c,v 1.5 1995/11/17 23:23:50 gwr Exp $ */
! 3:
! 4: /*
! 5: * Copyright (c) 2003 Tom Cosgrove <tom.cosgrove@arches-consulting.com>
! 6: * Copyright (c) 1997 Michael Shalayeff
! 7: * Copyright (c) 1994 Paul Kranenburg
! 8: * All rights reserved.
! 9: *
! 10: * Redistribution and use in source and binary forms, with or without
! 11: * modification, are permitted provided that the following conditions
! 12: * are met:
! 13: * 1. Redistributions of source code must retain the above copyright
! 14: * notice, this list of conditions and the following disclaimer.
! 15: * 2. Redistributions in binary form must reproduce the above copyright
! 16: * notice, this list of conditions and the following disclaimer in the
! 17: * documentation and/or other materials provided with the distribution.
! 18: * 3. All advertising materials mentioning features or use of this software
! 19: * must display the following acknowledgement:
! 20: * This product includes software developed by Paul Kranenburg.
! 21: * 4. The name of the author may not be used to endorse or promote products
! 22: * derived from this software without specific prior written permission
! 23: *
! 24: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
! 25: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
! 26: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
! 27: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
! 28: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
! 29: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
! 30: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
! 31: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
! 32: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
! 33: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
! 34: */
! 35:
! 36: #define ELFSIZE 32
! 37:
! 38: #include <sys/param.h>
! 39: #include <sys/mount.h>
! 40: #include <sys/time.h>
! 41: #include <sys/stat.h>
! 42: #include <sys/disklabel.h>
! 43: #include <sys/dkio.h>
! 44: #include <sys/ioctl.h>
! 45: #include <ufs/ufs/dinode.h>
! 46: #include <ufs/ufs/dir.h>
! 47: #include <ufs/ffs/fs.h>
! 48: #include <sys/reboot.h>
! 49:
! 50: #include <uvm/uvm_extern.h>
! 51: #include <sys/sysctl.h>
! 52:
! 53: #include <machine/cpu.h>
! 54: #include <machine/biosvar.h>
! 55:
! 56: #include <err.h>
! 57: #include <a.out.h>
! 58: #include <sys/exec_elf.h>
! 59: #include <fcntl.h>
! 60: #include <nlist.h>
! 61: #include <stdlib.h>
! 62: #include <stdio.h>
! 63: #include <string.h>
! 64: #include <unistd.h>
! 65: #include <util.h>
! 66:
! 67: #include "nlist.c"
! 68:
! 69: struct sym_data {
! 70: char *sym_name; /* Must be initialised */
! 71: int sym_size; /* And this one */
! 72: int sym_set; /* Rest set at runtime */
! 73: u_int32_t sym_value;
! 74: };
! 75:
! 76: extern char *__progname;
! 77: int verbose, nowrite = 0;
! 78: char *boot, *proto, *dev, *realdev;
! 79: struct sym_data pbr_symbols[] = {
! 80: {"_fs_bsize_p", 2},
! 81: {"_fs_bsize_s", 2},
! 82: {"_fsbtodb", 1},
! 83: {"_p_offset", 4},
! 84: {"_inodeblk", 4},
! 85: {"_inodedbl", 4},
! 86: {"_nblocks", 2},
! 87: {NULL}
! 88: };
! 89:
! 90: #define INODESEG 0x07e0 /* where we will put /boot's inode's block */
! 91: #define BOOTSEG 0x07c0 /* biosboot loaded here */
! 92:
! 93: #define INODEOFF ((INODESEG-BOOTSEG) << 4)
! 94:
! 95: static char *loadproto(char *, long *);
! 96: static int getbootparams(char *, int, struct disklabel *);
! 97: static void devread(int, void *, daddr_t, size_t, char *);
! 98: static void sym_set_value(struct sym_data *, char *, u_int32_t);
! 99: static void pbr_set_symbols(char *, char *, struct sym_data *);
! 100: static void usage(void);
! 101:
! 102: static void
! 103: usage(void)
! 104: {
! 105: fprintf(stderr, "usage: %s [-nv] boot biosboot device\n", __progname);
! 106: exit(1);
! 107: }
! 108:
! 109: /*
! 110: * Read information about /boot's inode and filesystem parameters, then
! 111: * put biosboot (partition boot record) on the target drive with these
! 112: * parameters patched in.
! 113: */
! 114: int
! 115: main(int argc, char *argv[])
! 116: {
! 117: int c;
! 118: int devfd;
! 119: char *protostore;
! 120: long protosize;
! 121: struct stat sb;
! 122: struct disklabel dl;
! 123: struct dos_mbr mbr;
! 124: struct dos_partition *dp;
! 125: off_t startoff = 0;
! 126:
! 127: while ((c = getopt(argc, argv, "vn")) != -1) {
! 128: switch (c) {
! 129: case 'n':
! 130: /* Do not actually write the bootblock to disk. */
! 131: nowrite = 1;
! 132: break;
! 133: case 'v':
! 134: /* Give more information. */
! 135: verbose = 1;
! 136: break;
! 137: default:
! 138: usage();
! 139: }
! 140: }
! 141:
! 142: if (argc - optind < 3)
! 143: usage();
! 144:
! 145: boot = argv[optind];
! 146: proto = argv[optind + 1];
! 147: realdev = dev = argv[optind + 2];
! 148:
! 149: /* Open and check raw disk device. */
! 150: if ((devfd = opendev(dev, (nowrite? O_RDONLY:O_RDWR),
! 151: OPENDEV_PART, &realdev)) < 0)
! 152: err(1, "open: %s", realdev);
! 153:
! 154: if (verbose) {
! 155: fprintf(stderr, "boot: %s\n", boot);
! 156: fprintf(stderr, "proto: %s\n", proto);
! 157: fprintf(stderr, "device: %s\n", realdev);
! 158: }
! 159:
! 160: if (ioctl(devfd, DIOCGDINFO, &dl) != 0)
! 161: err(1, "disklabel: %s", realdev);
! 162:
! 163: /* Check disklabel. */
! 164: if (dl.d_magic != DISKMAGIC)
! 165: err(1, "bad disklabel magic=0x%08x", dl.d_magic);
! 166:
! 167: /* Warn on unknown disklabel types. */
! 168: if (dl.d_type == 0)
! 169: warnx("disklabel type unknown");
! 170:
! 171: /* Load proto blocks into core. */
! 172: if ((protostore = loadproto(proto, &protosize)) == NULL)
! 173: exit(1);
! 174:
! 175: /* XXX - Paranoia: Make sure size is aligned! */
! 176: if (protosize & (DEV_BSIZE - 1))
! 177: errx(1, "proto %s bad size=%ld", proto, protosize);
! 178:
! 179: /* Write patched proto bootblock(s) into the superblock. */
! 180: if (protosize > SBSIZE - DEV_BSIZE)
! 181: errx(1, "proto bootblocks too big");
! 182:
! 183: if (fstat(devfd, &sb) < 0)
! 184: err(1, "stat: %s", realdev);
! 185:
! 186: if (!S_ISCHR(sb.st_mode))
! 187: errx(1, "%s: not a character device", realdev);
! 188:
! 189: /* Get bootstrap parameters that are to be patched into proto. */
! 190: if (getbootparams(boot, devfd, &dl) != 0)
! 191: exit(1);
! 192:
! 193: /* Patch the parameters into the proto bootstrap sector. */
! 194: pbr_set_symbols(proto, protostore, pbr_symbols);
! 195:
! 196: if (!nowrite) {
! 197: /* Sync filesystems (to clean in-memory superblock?). */
! 198: sync(); sleep(1);
! 199: }
! 200:
! 201: if (dl.d_type != 0 && dl.d_type != DTYPE_FLOPPY &&
! 202: dl.d_type != DTYPE_VND) {
! 203: if (lseek(devfd, (off_t)DOSBBSECTOR, SEEK_SET) < 0 ||
! 204: read(devfd, &mbr, sizeof(mbr)) < sizeof(mbr))
! 205: err(4, "can't read master boot record");
! 206:
! 207: if (mbr.dmbr_sign != DOSMBR_SIGNATURE)
! 208: errx(1, "broken MBR");
! 209:
! 210: /* Find OpenBSD partition. */
! 211: for (dp = mbr.dmbr_parts; dp < &mbr.dmbr_parts[NDOSPART];
! 212: dp++) {
! 213: if (dp->dp_size && dp->dp_typ == DOSPTYP_OPENBSD) {
! 214: startoff = (off_t)dp->dp_start * dl.d_secsize;
! 215: fprintf(stderr, "using MBR partition %ld: "
! 216: "type %d (0x%02x) offset %d (0x%x)\n",
! 217: (long)(dp - mbr.dmbr_parts),
! 218: dp->dp_typ, dp->dp_typ,
! 219: dp->dp_start, dp->dp_start);
! 220: break;
! 221: }
! 222: }
! 223: /* Don't check for old part number, that is ;-p */
! 224: if (dp >= &mbr.dmbr_parts[NDOSPART])
! 225: errx(1, "no OpenBSD partition");
! 226: }
! 227:
! 228: if (!nowrite) {
! 229: if (lseek(devfd, startoff, SEEK_SET) < 0 ||
! 230: write(devfd, protostore, protosize) != protosize)
! 231: err(1, "write bootstrap");
! 232: }
! 233:
! 234: (void)close(devfd);
! 235:
! 236: return 0;
! 237: }
! 238:
! 239: /*
! 240: * Load the prototype boot sector (biosboot) into memory.
! 241: */
! 242: static char *
! 243: loadproto(char *fname, long *size)
! 244: {
! 245: int fd;
! 246: size_t tdsize; /* text+data size */
! 247: char *bp;
! 248: Elf_Ehdr eh;
! 249: Elf_Word phsize;
! 250: Elf_Phdr *ph;
! 251:
! 252: if ((fd = open(fname, O_RDONLY)) < 0)
! 253: err(1, "%s", fname);
! 254:
! 255: if (read(fd, &eh, sizeof(eh)) != sizeof(eh))
! 256: errx(1, "%s: read failed", fname);
! 257:
! 258: if (!IS_ELF(eh))
! 259: errx(1, "%s: bad magic: 0x%02x%02x%02x%02x",
! 260: boot,
! 261: eh.e_ident[EI_MAG0], eh.e_ident[EI_MAG1],
! 262: eh.e_ident[EI_MAG2], eh.e_ident[EI_MAG3]);
! 263:
! 264: /*
! 265: * We have to include the exec header in the beginning of
! 266: * the buffer, and leave extra space at the end in case
! 267: * the actual write to disk wants to skip the header.
! 268: */
! 269:
! 270: /* Program load header. */
! 271: if (eh.e_phnum != 1)
! 272: errx(1, "%s: %u ELF load sections (only support 1)",
! 273: boot, eh.e_phnum);
! 274:
! 275: phsize = eh.e_phnum * sizeof(Elf_Phdr);
! 276: ph = malloc(phsize);
! 277: if (ph == NULL)
! 278: err(1, NULL);
! 279:
! 280: lseek(fd, eh.e_phoff, SEEK_SET);
! 281:
! 282: if (read(fd, ph, phsize) != phsize)
! 283: errx(1, "%s: can't read header", boot);
! 284:
! 285: tdsize = ph->p_filesz;
! 286:
! 287: /*
! 288: * Allocate extra space here because the caller may copy
! 289: * the boot block starting at the end of the exec header.
! 290: * This prevents reading beyond the end of the buffer.
! 291: */
! 292: if ((bp = calloc(tdsize, 1)) == NULL) {
! 293: err(1, NULL);
! 294: }
! 295:
! 296: /* Read the rest of the file. */
! 297: lseek(fd, ph->p_offset, SEEK_SET);
! 298: if (read(fd, bp, tdsize) != tdsize) {
! 299: errx(1, "%s: read failed", fname);
! 300: }
! 301:
! 302: *size = tdsize; /* not aligned to DEV_BSIZE */
! 303:
! 304: if (verbose) {
! 305: fprintf(stderr, "%s: entry point %#x\n", fname, eh.e_entry);
! 306: fprintf(stderr, "proto bootblock size %ld\n", *size);
! 307: }
! 308:
! 309: close(fd);
! 310: return bp;
! 311: }
! 312:
! 313: static void
! 314: devread(int fd, void *buf, daddr_t blk, size_t size, char *msg)
! 315: {
! 316: if (lseek(fd, dbtob((off_t)blk), SEEK_SET) != dbtob((off_t)blk))
! 317: err(1, "%s: devread: lseek", msg);
! 318:
! 319: if (read(fd, buf, size) != size)
! 320: err(1, "%s: devread: read", msg);
! 321: }
! 322:
! 323: static char sblock[SBSIZE];
! 324:
! 325: /*
! 326: * Read information about /boot's inode, then put this and filesystem
! 327: * parameters from the superblock into pbr_symbols.
! 328: */
! 329: static int
! 330: getbootparams(char *boot, int devfd, struct disklabel *dl)
! 331: {
! 332: int fd;
! 333: struct stat statbuf, sb;
! 334: struct statfs statfsbuf;
! 335: struct partition *pl;
! 336: struct fs *fs;
! 337: char *buf;
! 338: daddr_t blk, *ap;
! 339: struct ufs1_dinode *ip;
! 340: int ndb;
! 341: int mib[3];
! 342: size_t size;
! 343: dev_t dev;
! 344:
! 345: /*
! 346: * Open 2nd-level boot program and record enough details about
! 347: * where it is on the filesystem represented by `devfd'
! 348: * (inode block, offset within that block, and various filesystem
! 349: * parameters essentially taken from the superblock) for biosboot
! 350: * to be able to load it later.
! 351: */
! 352:
! 353: /* Make sure the (probably new) boot file is on disk. */
! 354: sync(); sleep(1);
! 355:
! 356: if ((fd = open(boot, O_RDONLY)) < 0)
! 357: err(1, "open: %s", boot);
! 358:
! 359: if (fstatfs(fd, &statfsbuf) != 0)
! 360: err(1, "statfs: %s", boot);
! 361:
! 362: if (strncmp(statfsbuf.f_fstypename, "ffs", MFSNAMELEN) &&
! 363: strncmp(statfsbuf.f_fstypename, "ufs", MFSNAMELEN) )
! 364: errx(1, "%s: not on an FFS filesystem", boot);
! 365:
! 366: #if 0
! 367: if (read(fd, &eh, sizeof(eh)) != sizeof(eh))
! 368: errx(1, "read: %s", boot);
! 369:
! 370: if (!IS_ELF(eh)) {
! 371: errx(1, "%s: bad magic: 0x%02x%02x%02x%02x",
! 372: boot,
! 373: eh.e_ident[EI_MAG0], eh.e_ident[EI_MAG1],
! 374: eh.e_ident[EI_MAG2], eh.e_ident[EI_MAG3]);
! 375: }
! 376: #endif
! 377:
! 378: if (fsync(fd) != 0)
! 379: err(1, "fsync: %s", boot);
! 380:
! 381: if (fstat(fd, &statbuf) != 0)
! 382: err(1, "fstat: %s", boot);
! 383:
! 384: if (fstat(devfd, &sb) != 0)
! 385: err(1, "fstat: %s", realdev);
! 386:
! 387: /* Check devices. */
! 388: mib[0] = CTL_MACHDEP;
! 389: mib[1] = CPU_CHR2BLK;
! 390: mib[2] = sb.st_rdev;
! 391: size = sizeof(dev);
! 392: if (sysctl(mib, 3, &dev, &size, NULL, 0) >= 0) {
! 393: if (statbuf.st_dev / MAXPARTITIONS != dev / MAXPARTITIONS)
! 394: errx(1, "cross-device install");
! 395: }
! 396:
! 397: pl = &dl->d_partitions[DISKPART(statbuf.st_dev)];
! 398: close(fd);
! 399:
! 400: /* Read superblock. */
! 401: devread(devfd, sblock, pl->p_offset + SBLOCK, SBSIZE, "superblock");
! 402: fs = (struct fs *)sblock;
! 403:
! 404: /* Sanity-check super-block. */
! 405: if (fs->fs_magic != FS_MAGIC)
! 406: errx(1, "Bad magic number in superblock");
! 407: if (fs->fs_inopb <= 0)
! 408: err(1, "Bad inopb=%d in superblock", fs->fs_inopb);
! 409:
! 410: /* Read inode. */
! 411: if ((buf = malloc(fs->fs_bsize)) == NULL)
! 412: err(1, NULL);
! 413:
! 414: blk = fsbtodb(fs, ino_to_fsba(fs, statbuf.st_ino));
! 415:
! 416: devread(devfd, buf, pl->p_offset + blk, fs->fs_bsize, "inode");
! 417: ip = (struct ufs1_dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino);
! 418:
! 419: /*
! 420: * Have the inode. Figure out how many filesystem blocks (not disk
! 421: * sectors) there are for biosboot to load.
! 422: */
! 423: ndb = howmany(ip->di_size, fs->fs_bsize);
! 424: if (ndb <= 0)
! 425: errx(1, "No blocks to load");
! 426:
! 427: /*
! 428: * Now set the values that will need to go into biosboot
! 429: * (the partition boot record, a.k.a. the PBR).
! 430: */
! 431: sym_set_value(pbr_symbols, "_fs_bsize_p", (fs->fs_bsize / 16));
! 432: sym_set_value(pbr_symbols, "_fs_bsize_s", (fs->fs_bsize / 512));
! 433: sym_set_value(pbr_symbols, "_fsbtodb", fs->fs_fsbtodb);
! 434: sym_set_value(pbr_symbols, "_p_offset", pl->p_offset);
! 435: sym_set_value(pbr_symbols, "_inodeblk",
! 436: ino_to_fsba(fs, statbuf.st_ino));
! 437: ap = ip->di_db;
! 438: sym_set_value(pbr_symbols, "_inodedbl",
! 439: ((((char *)ap) - buf) + INODEOFF));
! 440: sym_set_value(pbr_symbols, "_nblocks", ndb);
! 441:
! 442: if (verbose) {
! 443: fprintf(stderr, "%s is %d blocks x %d bytes\n",
! 444: boot, ndb, fs->fs_bsize);
! 445: fprintf(stderr, "fs block shift %u; part offset %u; "
! 446: "inode block %u, offset %u\n",
! 447: fs->fs_fsbtodb, pl->p_offset,
! 448: ino_to_fsba(fs, statbuf.st_ino),
! 449: (unsigned int)((((char *)ap) - buf) + INODEOFF));
! 450: }
! 451:
! 452: return 0;
! 453: }
! 454:
! 455: static void
! 456: sym_set_value(struct sym_data *sym_list, char *sym, u_int32_t value)
! 457: {
! 458: struct sym_data *p;
! 459:
! 460: for (p = sym_list; p->sym_name != NULL; p++) {
! 461: if (strcmp(p->sym_name, sym) == 0)
! 462: break;
! 463: }
! 464:
! 465: if (p->sym_name == NULL)
! 466: errx(1, "%s: no such symbol", sym);
! 467:
! 468: if (p->sym_set)
! 469: errx(1, "%s already set", p->sym_name);
! 470:
! 471: p->sym_value = value;
! 472: p->sym_set = 1;
! 473: }
! 474:
! 475: /*
! 476: * Write the parameters stored in sym_list into the in-memory copy of
! 477: * the prototype biosboot (proto), ready for it to be written to disk.
! 478: */
! 479: static void
! 480: pbr_set_symbols(char *fname, char *proto, struct sym_data *sym_list)
! 481: {
! 482: struct sym_data *sym;
! 483: struct nlist *nl;
! 484: char *vp;
! 485: u_int32_t *lp;
! 486: u_int16_t *wp;
! 487: u_int8_t *bp;
! 488:
! 489: for (sym = sym_list; sym->sym_name != NULL; sym++) {
! 490: if (!sym->sym_set)
! 491: errx(1, "%s not set", sym->sym_name);
! 492:
! 493: /* Allocate space for 2; second is null-terminator for list. */
! 494: nl = calloc(2, sizeof(struct nlist));
! 495: if (nl == NULL)
! 496: err(1, NULL);
! 497:
! 498: nl->n_un.n_name = sym->sym_name;
! 499:
! 500: if (nlist(fname, nl) != 0)
! 501: errx(1, "%s: symbol %s not found",
! 502: fname, sym->sym_name);
! 503:
! 504: if (nl->n_type != (N_TEXT))
! 505: errx(1, "%s: %s: wrong type (%x)",
! 506: fname, sym->sym_name, nl->n_type);
! 507:
! 508: /* Get a pointer to where the symbol's value needs to go. */
! 509: vp = proto + nl->n_value;
! 510:
! 511: switch (sym->sym_size) {
! 512: case 4: /* u_int32_t */
! 513: lp = (u_int32_t *) vp;
! 514: *lp = sym->sym_value;
! 515: break;
! 516: case 2: /* u_int16_t */
! 517: if (sym->sym_value >= 0x10000) /* out of range */
! 518: errx(1, "%s: symbol out of range (%u)",
! 519: sym->sym_name, sym->sym_value);
! 520: wp = (u_int16_t *) vp;
! 521: *wp = (u_int16_t) sym->sym_value;
! 522: break;
! 523: case 1: /* u_int16_t */
! 524: if (sym->sym_value >= 0x100) /* out of range */
! 525: errx(1, "%s: symbol out of range (%u)",
! 526: sym->sym_name, sym->sym_value);
! 527: bp = (u_int8_t *) vp;
! 528: *bp = (u_int8_t) sym->sym_value;
! 529: break;
! 530: default:
! 531: errx(1, "%s: bad symbol size %d",
! 532: sym->sym_name, sym->sym_size);
! 533: /* NOTREACHED */
! 534: }
! 535:
! 536: free(nl);
! 537: }
! 538: }
CVSweb