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 = malloc(bootsize); 72 if (boot == NULL) 73 err(1, "malloc"); 74 memset(boot, 0, bootsize); 75 if (read(fd, boot, bootsize) != (ssize_t)sb.st_size) 76 err(1, "read"); 77 close(fd); 78 79 /* 80 * Check that the bootstrap will fit - partitions must not overlap, 81 * or if they do, the partition type must be either FS_BOOT or 82 * FS_UNUSED. The 'c' partition will always overlap and is ignored. 83 */ 84 if (verbose) 85 fprintf(stderr, "ensuring used partitions do not overlap " 86 "with bootstrap sectors 0-%zu\n", bootsec); 87 for (i = 0; i < dl.d_npartitions; i++) { 88 part = 'a' + i; 89 pp = &dl.d_partitions[i]; 90 if (i == RAW_PART) 91 continue; 92 if (DL_GETPSIZE(pp) == 0) 93 continue; 94 if ((u_int64_t)bootsec <= DL_GETPOFFSET(pp)) 95 continue; 96 switch (pp->p_fstype) { 97 case FS_BOOT: 98 break; 99 case FS_UNUSED: 100 warnx("bootstrap overlaps with unused partition %c", 101 part); 102 break; 103 default: 104 errx(1, "bootstrap overlaps with partition %c", part); 105 } 106 } 107 108 /* Make sure the bootstrap has left space for the disklabel. */ 109 lp = (struct disklabel *)(boot + (LABELSECTOR * dl.d_secsize) + 110 LABELOFFSET); 111 for (i = 0, p = (char *)lp; i < (int)sizeof(*lp); i++) 112 if (p[i] != 0) 113 errx(1, "bootstrap has data in disklabel area"); 114 115 /* Patch the disklabel into the bootstrap code. */ 116 memcpy(lp, &dl, sizeof(dl)); 117 118 /* Write the bootstrap out to the disk. */ 119 if (lseek(devfd, 0, SEEK_SET) != 0) 120 err(1, "lseek"); 121 if (verbose) 122 fprintf(stderr, "%s bootstrap to disk\n", 123 (nowrite ? "would write" : "writing")); 124 if (nowrite) 125 return; 126 if (write(devfd, boot, bootsize) != (ssize_t)bootsize) 127 err(1, "write"); 128 } 129