1 /* $NetBSD: installboot.c,v 1.7 2000/12/09 22:33:24 scw Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Paul Kranenburg. 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 the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/param.h> 40 #include <sys/cdefs.h> 41 #include <sys/mount.h> 42 #include <sys/time.h> 43 #include <sys/stat.h> 44 #include <ufs/ufs/dinode.h> 45 #include <ufs/ufs/dir.h> 46 #include <ufs/ffs/fs.h> 47 #include <err.h> 48 #include <fcntl.h> 49 #include <nlist.h> 50 #include <stdlib.h> 51 #include <stdio.h> 52 #include <string.h> 53 #include <unistd.h> 54 #include <sys/disklabel.h> 55 56 #include "loadfile.h" 57 58 int verbose, nowrite, hflag; 59 char *boot, *proto, *dev; 60 61 struct nlist nl[] = { 62 #define X_BLOCK_SIZE 0 63 #define X_BLOCK_COUNT 1 64 #define X_BLOCK_TABLE 2 65 #ifdef __ELF__ 66 { "block_size" }, 67 { "block_count" }, 68 { "block_table" }, 69 #else 70 { "_block_size" }, 71 { "_block_count" }, 72 { "_block_table" }, 73 #endif 74 { NULL } 75 }; 76 77 int *block_size_p; /* block size var. in prototype image */ 78 int *block_count_p; /* block count var. in prototype image */ 79 daddr_t *block_table; /* block number array in prototype image */ 80 int maxblocknum; /* size of this array */ 81 82 83 char *loadprotoblocks __P((char *, size_t *)); 84 int loadblocknums __P((char *, int)); 85 static void devread __P((int, void *, daddr_t, size_t, char *)); 86 static void usage __P((void)); 87 int main __P((int, char *[])); 88 89 90 static void 91 usage() 92 { 93 fprintf(stderr, 94 "usage: installboot [-n] [-v] [-h] <boot> <proto> <device>\n"); 95 exit(1); 96 } 97 98 int 99 main(argc, argv) 100 int argc; 101 char *argv[]; 102 { 103 int c; 104 int devfd; 105 char *protostore; 106 size_t protosize; 107 108 while ((c = getopt(argc, argv, "vnh")) != -1) { 109 switch (c) { 110 case 'h': 111 /* Don't strip a.out header */ 112 hflag = 1; 113 break; 114 case 'n': 115 /* Do not actually write the bootblock to disk */ 116 nowrite = 1; 117 break; 118 case 'v': 119 /* Chat */ 120 verbose = 1; 121 break; 122 default: 123 usage(); 124 } 125 } 126 127 if (argc - optind < 3) { 128 usage(); 129 } 130 131 boot = argv[optind]; 132 proto = argv[optind + 1]; 133 dev = argv[optind + 2]; 134 135 if (verbose) { 136 printf("boot: %s\n", boot); 137 printf("proto: %s\n", proto); 138 printf("device: %s\n", dev); 139 } 140 141 /* Load proto blocks into core */ 142 if ((protostore = loadprotoblocks(proto, &protosize)) == NULL) 143 exit(1); 144 145 /* XXX - Paranoia: Make sure size is aligned! */ 146 if (protosize & (DEV_BSIZE - 1)) 147 err(1, "proto bootblock bad size=%d", protosize); 148 149 /* Open and check raw disk device */ 150 if ((devfd = open(dev, O_RDONLY, 0)) < 0) 151 err(1, "open: %s", dev); 152 153 /* Extract and load block numbers */ 154 if (loadblocknums(boot, devfd) != 0) 155 exit(1); 156 157 (void)close(devfd); 158 159 if (nowrite) 160 return 0; 161 162 /* Write patched proto bootblocks into the superblock */ 163 if (protosize > SBSIZE - DEV_BSIZE) 164 errx(1, "proto bootblocks too big"); 165 166 /* The primary bootblock needs to be written to the raw partition */ 167 dev[strlen(dev) - 1] = 'a' + RAW_PART; 168 169 if ((devfd = open(dev, O_RDWR, 0)) < 0) 170 err(1, "open: %s", dev); 171 172 if (lseek(devfd, DEV_BSIZE, SEEK_SET) != DEV_BSIZE) 173 err(1, "lseek bootstrap"); 174 175 /* Sync filesystems (to clean in-memory superblock?) */ 176 sync(); 177 178 if (write(devfd, protostore, protosize) != protosize) 179 err(1, "write bootstrap"); 180 (void)close(devfd); 181 return 0; 182 } 183 184 char * 185 loadprotoblocks(fname, size) 186 char *fname; 187 size_t *size; 188 { 189 int fd; 190 u_long marks[MARK_MAX], bp, offs; 191 192 fd = -1; 193 194 /* Locate block number array in proto file */ 195 if (nlist(fname, nl) != 0) { 196 warnx("nlist: %s: symbols not found", fname); 197 return NULL; 198 } 199 200 marks[MARK_START] = 0; 201 if ((fd = loadfile(fname, marks, COUNT_TEXT|COUNT_DATA)) == -1) 202 return NULL; 203 (void)close(fd); 204 205 *size = roundup(marks[MARK_END] - marks[MARK_START], DEV_BSIZE); 206 bp = (u_long)malloc(*size); 207 208 offs = marks[MARK_START]; 209 marks[MARK_START] = bp - offs; 210 211 if ((fd = loadfile(fname, marks, LOAD_TEXT|LOAD_DATA)) == -1) 212 return NULL; 213 (void)close(fd); 214 215 /* Calculate the symbols' locations within the proto file */ 216 block_size_p = (int *) (bp + (nl[X_BLOCK_SIZE ].n_value - offs)); 217 block_count_p = (int *) (bp + (nl[X_BLOCK_COUNT].n_value - offs)); 218 block_table = (daddr_t *) (bp + (nl[X_BLOCK_TABLE].n_value - offs)); 219 maxblocknum = *block_count_p; 220 221 if (verbose) { 222 printf("%s: entry point %#lx\n", fname, marks[MARK_ENTRY]); 223 printf("proto bootblock size %d\n", *size); 224 printf("room for %d filesystem blocks at %#lx\n", 225 maxblocknum, nl[X_BLOCK_TABLE].n_value); 226 } 227 228 return (char *) bp; 229 230 if (bp) 231 free((void *)bp); 232 return NULL; 233 } 234 235 static void 236 devread(fd, buf, blk, size, msg) 237 int fd; 238 void *buf; 239 daddr_t blk; 240 size_t size; 241 char *msg; 242 { 243 if (lseek(fd, dbtob(blk), SEEK_SET) != dbtob(blk)) 244 err(1, "%s: devread: lseek", msg); 245 246 if (read(fd, buf, size) != size) 247 err(1, "%s: devread: read", msg); 248 } 249 250 static char sblock[SBSIZE]; 251 252 int 253 loadblocknums(boot, devfd) 254 char *boot; 255 int devfd; 256 { 257 int i, fd; 258 struct stat statbuf; 259 struct statfs statfsbuf; 260 struct fs *fs; 261 char *buf; 262 daddr_t blk, *ap; 263 struct dinode *ip; 264 int ndb; 265 266 /* 267 * Open 2nd-level boot program and record the block numbers 268 * it occupies on the filesystem represented by `devfd'. 269 */ 270 271 /* Make sure the (probably new) boot file is on disk. */ 272 sync(); sleep(1); 273 274 if ((fd = open(boot, O_RDONLY)) < 0) 275 err(1, "open: %s", boot); 276 277 if (fstatfs(fd, &statfsbuf) != 0) 278 err(1, "statfs: %s", boot); 279 280 if (strncmp(statfsbuf.f_fstypename, "ffs", MFSNAMELEN) && 281 strncmp(statfsbuf.f_fstypename, "ufs", MFSNAMELEN) ) { 282 errx(1, "%s: must be on an FFS filesystem", boot); 283 } 284 285 if (fsync(fd) != 0) 286 err(1, "fsync: %s", boot); 287 288 if (fstat(fd, &statbuf) != 0) 289 err(1, "fstat: %s", boot); 290 291 close(fd); 292 293 /* Read superblock */ 294 devread(devfd, sblock, SBLOCK, SBSIZE, "superblock"); 295 fs = (struct fs *)sblock; 296 297 /* Sanity-check super-block. */ 298 if (fs->fs_magic != FS_MAGIC) 299 errx(1, "Bad magic number in superblock"); 300 if (fs->fs_inopb <= 0) 301 err(1, "Bad inopb=%d in superblock", fs->fs_inopb); 302 303 /* Read inode */ 304 if ((buf = malloc(fs->fs_bsize)) == NULL) 305 errx(1, "No memory for filesystem block"); 306 307 blk = fsbtodb(fs, ino_to_fsba(fs, statbuf.st_ino)); 308 devread(devfd, buf, blk, fs->fs_bsize, "inode"); 309 ip = (struct dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino); 310 311 /* 312 * Have the inode. Figure out how many blocks we need. 313 */ 314 ndb = howmany(ip->di_size, fs->fs_bsize); 315 if (ndb > maxblocknum) 316 errx(1, "Too many blocks"); 317 *block_count_p = ndb; 318 *block_size_p = fs->fs_bsize; 319 if (verbose) 320 printf("Will load %d blocks of size %d each.\n", 321 ndb, fs->fs_bsize); 322 323 /* 324 * Get the block numbers; we don't handle fragments 325 */ 326 ap = ip->di_db; 327 for (i = 0; i < NDADDR && *ap && ndb; i++, ap++, ndb--) { 328 blk = fsbtodb(fs, *ap); 329 if (verbose) 330 printf("%d: %d\n", i, blk); 331 block_table[i] = blk; 332 } 333 if (ndb == 0) 334 return 0; 335 336 /* 337 * Just one level of indirections; there isn't much room 338 * for more in the 1st-level bootblocks anyway. 339 */ 340 blk = fsbtodb(fs, ip->di_ib[0]); 341 devread(devfd, buf, blk, fs->fs_bsize, "indirect block"); 342 ap = (daddr_t *)buf; 343 for (; i < NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) { 344 blk = fsbtodb(fs, *ap); 345 if (verbose) 346 printf("%d: %d\n", i, blk); 347 block_table[i] = blk; 348 } 349 350 return 0; 351 } 352 353