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 <err.h> 24 #include <fcntl.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <unistd.h> 29 30 #include "installboot.h" 31 32 void 33 bootstrap(int devfd, char *dev, char *bootfile) 34 { 35 struct disklabel dl; 36 struct disklabel *lp; 37 struct partition *pp; 38 char *boot, *p, part; 39 size_t bootsize; 40 size_t bootsec; 41 struct stat sb; 42 int fd, i; 43 44 /* 45 * Install bootstrap code onto the given disk, preserving the 46 * existing disklabel. 47 */ 48 49 /* Read disklabel from disk. */ 50 if (ioctl(devfd, DIOCGDINFO, &dl) == -1) 51 err(1, "disklabel"); 52 if (dl.d_secsize == 0) { 53 warnx("disklabel has sector size of 0, assuming %d", DEV_BSIZE); 54 dl.d_secsize = DEV_BSIZE; 55 } 56 57 /* Read bootstrap file. */ 58 if (verbose) 59 fprintf(stderr, "reading bootstrap from %s\n", bootfile); 60 fd = open(bootfile, O_RDONLY); 61 if (fd < 0) 62 err(1, "open %s", bootfile); 63 if (fstat(fd, &sb) != 0) 64 err(1, "fstat %s", bootfile); 65 bootsec = howmany((ssize_t)sb.st_size, dl.d_secsize); 66 bootsize = bootsec * dl.d_secsize; 67 if (verbose) 68 fprintf(stderr, "bootstrap is %zu bytes " 69 "(%zu sectors @ %u bytes = %zu bytes)\n", 70 (ssize_t)sb.st_size, bootsec, dl.d_secsize, bootsize); 71 boot = calloc(1, bootsize); 72 if (boot == NULL) 73 err(1, "calloc"); 74 if (read(fd, boot, bootsize) != (ssize_t)sb.st_size) 75 err(1, "read"); 76 close(fd); 77 78 /* 79 * Check that the bootstrap will fit - partitions must not overlap, 80 * or if they do, the partition type must be either FS_BOOT or 81 * FS_UNUSED. The 'c' partition will always overlap and is ignored. 82 */ 83 if (verbose) 84 fprintf(stderr, "ensuring used partitions do not overlap " 85 "with bootstrap sectors 0-%zu\n", bootsec); 86 for (i = 0; i < dl.d_npartitions; i++) { 87 part = 'a' + i; 88 pp = &dl.d_partitions[i]; 89 if (i == RAW_PART) 90 continue; 91 if (DL_GETPSIZE(pp) == 0) 92 continue; 93 if ((u_int64_t)bootsec <= DL_GETPOFFSET(pp)) 94 continue; 95 switch (pp->p_fstype) { 96 case FS_BOOT: 97 break; 98 case FS_UNUSED: 99 warnx("bootstrap overlaps with unused partition %c", 100 part); 101 break; 102 default: 103 errx(1, "bootstrap overlaps with partition %c", part); 104 } 105 } 106 107 /* Make sure the bootstrap has left space for the disklabel. */ 108 lp = (struct disklabel *)(boot + (LABELSECTOR * dl.d_secsize) + 109 LABELOFFSET); 110 for (i = 0, p = (char *)lp; i < (int)sizeof(*lp); i++) 111 if (p[i] != 0) 112 errx(1, "bootstrap has data in disklabel area"); 113 114 /* Patch the disklabel into the bootstrap code. */ 115 memcpy(lp, &dl, sizeof(dl)); 116 117 /* Write the bootstrap out to the disk. */ 118 if (lseek(devfd, 0, SEEK_SET) != 0) 119 err(1, "lseek"); 120 if (verbose) 121 fprintf(stderr, "%s bootstrap to disk\n", 122 (nowrite ? "would write" : "writing")); 123 if (nowrite) 124 return; 125 if (write(devfd, boot, bootsize) != (ssize_t)bootsize) 126 err(1, "write"); 127 } 128