1 /* 2 * Copyright (c) 2013 Joel Sing <jsing@openbsd.org> 3 * 4 * Permission to use, copy, modify, and distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #include <sys/param.h> 18 #include <sys/disklabel.h> 19 #include <sys/dkio.h> 20 #include <sys/ioctl.h> 21 #include <sys/stat.h> 22 23 #include <fcntl.h> 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 #include <unistd.h> 28 29 #include "installboot.h" 30 31 int 32 bootstrap(int devfd, char *dev, char *bootfile) 33 { 34 struct disklabel dl; 35 struct disklabel *lp; 36 struct partition *pp; 37 char *boot, *p, part; 38 u_int64_t bootend; 39 struct stat sb; 40 int bootsize; 41 int bf, i; 42 43 /* 44 * Install bootstrap code onto the given disk, preserving the 45 * existing disklabel. 46 */ 47 48 /* Read disklabel from disk. */ 49 if (ioctl(devfd, DIOCGDINFO, &dl) == -1) 50 err(1, "disklabel"); 51 if (dl.d_secsize == 0) { 52 warnx("disklabel has sector size of 0, assuming %d", DEV_BSIZE); 53 dl.d_secsize = DEV_BSIZE; 54 } 55 56 /* Read bootstrap file. */ 57 if (verbose) 58 fprintf(stderr, "reading bootstrap from %s\n", bootfile); 59 bf = open(bootfile, O_RDONLY); 60 if (bf < 0) 61 err(1, "open %s", bootfile); 62 if (fstat(bf, &sb) != 0) 63 err(1, "fstat %s", bootfile); 64 bootsize = sb.st_size; 65 bootend = howmany((u_int64_t)bootsize, dl.d_secsize); 66 boot = malloc(bootsize); 67 if (boot == NULL) 68 err(1, "malloc"); 69 if (read(bf, boot, bootsize) != bootsize) 70 err(1, "read"); 71 if (verbose) 72 fprintf(stderr, "bootstrap is %lld bytes (%llu sectors)\n", 73 bootsize, bootend); 74 close(bf); 75 76 /* 77 * Check that the bootstrap will fit - partitions must not overlap, 78 * or if they do, the partition type must be either FS_BOOT or 79 * FS_UNUSED. The 'c' partition will always overlap and is ignored. 80 */ 81 if (verbose) 82 fprintf(stderr, "ensuring used partitions do not overlap " 83 "with bootstrap sectors 0-%lld\n", bootend); 84 for (i = 0; i < dl.d_npartitions; i++) { 85 part = 'a' + i; 86 pp = &dl.d_partitions[i]; 87 if (i == RAW_PART) 88 continue; 89 if (DL_GETPSIZE(pp) == 0) 90 continue; 91 if (bootend <= DL_GETPOFFSET(pp)) 92 continue; 93 switch (pp->p_fstype) { 94 case FS_BOOT: 95 break; 96 case FS_UNUSED: 97 warnx("bootstrap overlaps with unused partition %c", 98 part); 99 break; 100 default: 101 errx(1, "bootstrap overlaps with partition %c", part); 102 } 103 } 104 105 /* Make sure the bootstrap has left space for the disklabel. */ 106 lp = (struct disklabel *)(boot + (LABELSECTOR * dl.d_secsize) + 107 LABELOFFSET); 108 for (i = 0, p = (char *)lp; i < sizeof(*lp); i++) 109 if (p[i] != 0) 110 errx(1, "bootstrap has data in disklabel area"); 111 112 /* Patch the disklabel into the bootstrap code. */ 113 memcpy(lp, &dl, sizeof(dl)); 114 115 /* Write the bootstrap out to the disk. */ 116 if (lseek(devfd, 0, SEEK_SET) != 0) 117 err(1, "lseek"); 118 if (verbose) 119 fprintf(stderr, "%s bootstrap to disk\n", 120 (nowrite ? "would write" : "writing")); 121 if (nowrite) 122 return; 123 if (write(devfd, boot, bootsize) != bootsize) 124 err(1, "write"); 125 } 126