/* $OpenBSD: bootstrap.c,v 1.11 2018/11/25 17:34:37 krw Exp $ */ /* * Copyright (c) 2013 Joel Sing * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include /* DEV_BSIZE */ #include #include #include #include #include #include #include #include #include #include #include "installboot.h" void bootstrap(int devfd, char *dev, char *bootfile) { struct disklabel dl; struct disklabel *lp; struct partition *pp; char *boot, *p, part; size_t bootsize; size_t bootsec; struct stat sb; int fd, i; /* * Install bootstrap code onto the given disk, preserving the * existing disklabel. */ /* Read disklabel from disk. */ if (ioctl(devfd, DIOCGDINFO, &dl) == -1) err(1, "disklabel"); if (dl.d_secsize == 0) { warnx("disklabel has sector size of 0, assuming %d", DEV_BSIZE); dl.d_secsize = DEV_BSIZE; } /* Read bootstrap file. */ if (verbose) fprintf(stderr, "reading bootstrap from %s\n", bootfile); fd = open(bootfile, O_RDONLY); if (fd < 0) err(1, "open %s", bootfile); if (fstat(fd, &sb) != 0) err(1, "fstat %s", bootfile); bootsec = howmany((ssize_t)sb.st_size, dl.d_secsize); bootsize = bootsec * dl.d_secsize; if (verbose) fprintf(stderr, "bootstrap is %zu bytes " "(%zu sectors @ %u bytes = %zu bytes)\n", (ssize_t)sb.st_size, bootsec, dl.d_secsize, bootsize); boot = calloc(1, bootsize); if (boot == NULL) err(1, "calloc"); if (read(fd, boot, bootsize) != (ssize_t)sb.st_size) err(1, "read"); close(fd); /* * Check that the bootstrap will not intrude into the OpenBSD area * of the disk. */ if (bootsec > DL_GETBSTART(&dl)) errx(1, "bootstrap (sectors 0 - %zu) overlaps OpenBSD " " area (sectors %zu - %zu)", bootsec, DL_GETBSTART(&dl), DL_GETBEND(&dl)); else if (verbose) fprintf(stderr, "bootstrap (sectors 0 - %zu) does not overlap " "OpenBSD area.\n", bootsec); /* * Make sure the bootstrap has left space for the disklabel. * N.B.: LABELSECTOR *is* a DEV_BSIZE quantity! */ lp = (struct disklabel *)(boot + (LABELSECTOR * DEV_BSIZE) + LABELOFFSET); for (i = 0, p = (char *)lp; i < (int)sizeof(*lp); i++) if (p[i] != 0) errx(1, "bootstrap has data in disklabel area"); /* Patch the disklabel into the bootstrap code. */ memcpy(lp, &dl, sizeof(dl)); /* Write the bootstrap out to the disk. */ if (verbose) fprintf(stderr, "%s bootstrap to disk\n", (nowrite ? "would write" : "writing")); if (nowrite) return; if (pwrite(devfd, boot, bootsize, 0) != (ssize_t)bootsize) err(1, "pwrite"); }