1 /* $NetBSD: installboot.c,v 1.1 1996/05/17 20:00:55 chuck Exp $ */ 2 3 /* 4 * Copyright (c) 1994 Paul Kranenburg 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Paul Kranenburg. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/mount.h> 35 #include <sys/time.h> 36 #include <sys/stat.h> 37 #include <ufs/ufs/dinode.h> 38 #include <ufs/ufs/dir.h> 39 #include <ufs/ffs/fs.h> 40 #include <err.h> 41 #include <a.out.h> 42 #include <fcntl.h> 43 #include <nlist.h> 44 #include <stdlib.h> 45 #include <stdio.h> 46 #include <string.h> 47 #include <unistd.h> 48 49 int verbose, nowrite, hflag; 50 char *boot, *proto, *dev; 51 struct nlist nl[] = { 52 #define X_BLOCK_SIZE 0 53 {"_block_size"}, 54 #define X_BLOCK_COUNT 1 55 {"_block_count"}, 56 #define X_BLOCK_TABLE 2 57 {"_block_table"}, 58 {NULL} 59 }; 60 61 int *block_size_p; /* block size var. in prototype image */ 62 int *block_count_p; /* block count var. in prototype image */ 63 daddr_t *block_table; /* block number array in prototype image */ 64 int maxblocknum; /* size of this array */ 65 66 67 char *loadprotoblocks __P((char *, long *)); 68 int loadblocknums __P((char *, int)); 69 static void devread __P((int, void *, daddr_t, size_t, char *)); 70 static void usage __P((void)); 71 int main __P((int, char *[])); 72 73 74 static void 75 usage() 76 { 77 fprintf(stderr, 78 "usage: installboot [-n] [-v] [-h] <boot> <proto> <device>\n"); 79 exit(1); 80 } 81 82 int 83 main(argc, argv) 84 int argc; 85 char *argv[]; 86 { 87 int c; 88 int devfd; 89 char *protostore; 90 long protosize; 91 92 while ((c = getopt(argc, argv, "vnh")) != EOF) { 93 switch (c) { 94 case 'h': 95 /* Don't strip a.out header */ 96 hflag = 1; 97 break; 98 case 'n': 99 /* Do not actually write the bootblock to disk */ 100 nowrite = 1; 101 break; 102 case 'v': 103 /* Chat */ 104 verbose = 1; 105 break; 106 default: 107 usage(); 108 } 109 } 110 111 if (argc - optind < 3) { 112 usage(); 113 } 114 115 boot = argv[optind]; 116 proto = argv[optind + 1]; 117 dev = argv[optind + 2]; 118 119 if (verbose) { 120 printf("boot: %s\n", boot); 121 printf("proto: %s\n", proto); 122 printf("device: %s\n", dev); 123 } 124 125 /* Load proto blocks into core */ 126 if ((protostore = loadprotoblocks(proto, &protosize)) == NULL) 127 exit(1); 128 129 /* XXX - Paranoia: Make sure size is aligned! */ 130 if (protosize & (DEV_BSIZE - 1)) 131 err(1, "proto bootblock bad size=%d", protosize); 132 133 /* Open and check raw disk device */ 134 if ((devfd = open(dev, O_RDONLY, 0)) < 0) 135 err(1, "open: %s", dev); 136 137 /* Extract and load block numbers */ 138 if (loadblocknums(boot, devfd) != 0) 139 exit(1); 140 141 (void)close(devfd); 142 143 if (nowrite) 144 return 0; 145 146 /* Write patched proto bootblocks into the superblock */ 147 if (protosize > SBSIZE - DEV_BSIZE) 148 errx(1, "proto bootblocks too big"); 149 150 if ((devfd = open(dev, O_RDWR, 0)) < 0) 151 err(1, "open: %s", dev); 152 153 if (lseek(devfd, DEV_BSIZE, SEEK_SET) != DEV_BSIZE) 154 err(1, "lseek bootstrap"); 155 156 /* Sync filesystems (to clean in-memory superblock?) */ 157 sync(); 158 159 if (write(devfd, protostore, protosize) != protosize) 160 err(1, "write bootstrap"); 161 (void)close(devfd); 162 return 0; 163 } 164 165 char * 166 loadprotoblocks(fname, size) 167 char *fname; 168 long *size; 169 { 170 int fd; 171 size_t tdsize; /* text+data size */ 172 size_t bbsize; /* boot block size (block aligned) */ 173 char *bp; 174 struct nlist *nlp; 175 struct exec eh; 176 long off; 177 178 fd = -1; 179 bp = NULL; 180 181 /* Locate block number array in proto file */ 182 if (nlist(fname, nl) != 0) { 183 warnx("nlist: %s: symbols not found", fname); 184 return NULL; 185 } 186 /* Validate symbol types (global data). */ 187 for (nlp = nl; nlp->n_un.n_name; nlp++) { 188 if (nlp->n_type != (N_DATA | N_EXT)) { 189 warnx("nlist: %s: wrong type", nlp->n_un.n_name); 190 return NULL; 191 } 192 } 193 194 if ((fd = open(fname, O_RDONLY)) < 0) { 195 warn("open: %s", fname); 196 return NULL; 197 } 198 if (read(fd, &eh, sizeof(eh)) != sizeof(eh)) { 199 warn("read: %s", fname); 200 goto bad; 201 } 202 if (N_GETMAGIC(eh) != OMAGIC) { 203 warn("bad magic: 0x%x", eh.a_midmag); 204 goto bad; 205 } 206 /* 207 * We have to include the exec header in the beginning of 208 * the buffer, and leave extra space at the end in case 209 * the actual write to disk wants to skip the header. 210 */ 211 tdsize = eh.a_text + eh.a_data; 212 bbsize = tdsize + sizeof(eh); 213 bbsize = roundup(bbsize, DEV_BSIZE); 214 215 /* 216 * Allocate extra space here because the caller may copy 217 * the boot block starting at the end of the exec header. 218 * This prevents reading beyond the end of the buffer. 219 */ 220 if ((bp = calloc(bbsize + sizeof(eh), 1)) == NULL) { 221 warnx("malloc: %s: no memory", fname); 222 goto bad; 223 } 224 /* Copy the exec header and read the rest of the file. */ 225 memcpy(bp, &eh, sizeof(eh)); 226 if (read(fd, bp+sizeof(eh), tdsize) != tdsize) { 227 warn("read: %s", fname); 228 goto bad; 229 } 230 231 *size = bbsize; /* aligned to DEV_BSIZE */ 232 233 /* Calculate the symbols' locations within the proto file */ 234 off = N_DATOFF(eh) - N_DATADDR(eh) - (eh.a_entry - N_TXTADDR(eh)); 235 block_size_p = (int *) (bp + nl[X_BLOCK_SIZE ].n_value + off); 236 block_count_p = (int *) (bp + nl[X_BLOCK_COUNT].n_value + off); 237 block_table = (daddr_t *) (bp + nl[X_BLOCK_TABLE].n_value + off); 238 maxblocknum = *block_count_p; 239 240 if (verbose) { 241 printf("%s: entry point %#x\n", fname, eh.a_entry); 242 printf("proto bootblock size %ld\n", *size); 243 printf("room for %d filesystem blocks at %#x\n", 244 maxblocknum, nl[X_BLOCK_TABLE].n_value); 245 } 246 247 close(fd); 248 if (!hflag) 249 bp += sizeof(struct exec); 250 return bp; 251 252 bad: 253 if (bp) 254 free(bp); 255 if (fd >= 0) 256 close(fd); 257 return NULL; 258 } 259 260 static void 261 devread(fd, buf, blk, size, msg) 262 int fd; 263 void *buf; 264 daddr_t blk; 265 size_t size; 266 char *msg; 267 { 268 if (lseek(fd, dbtob(blk), SEEK_SET) != dbtob(blk)) 269 err(1, "%s: devread: lseek", msg); 270 271 if (read(fd, buf, size) != size) 272 err(1, "%s: devread: read", msg); 273 } 274 275 static char sblock[SBSIZE]; 276 277 int 278 loadblocknums(boot, devfd) 279 char *boot; 280 int devfd; 281 { 282 int i, fd; 283 struct stat statbuf; 284 struct statfs statfsbuf; 285 struct fs *fs; 286 char *buf; 287 daddr_t blk, *ap; 288 struct dinode *ip; 289 int ndb; 290 291 /* 292 * Open 2nd-level boot program and record the block numbers 293 * it occupies on the filesystem represented by `devfd'. 294 */ 295 296 /* Make sure the (probably new) boot file is on disk. */ 297 sync(); sleep(1); 298 299 if ((fd = open(boot, O_RDONLY)) < 0) 300 err(1, "open: %s", boot); 301 302 if (fstatfs(fd, &statfsbuf) != 0) 303 err(1, "statfs: %s", boot); 304 305 if (strncmp(statfsbuf.f_fstypename, "ffs", MFSNAMELEN) && 306 strncmp(statfsbuf.f_fstypename, "ufs", MFSNAMELEN) ) { 307 errx(1, "%s: must be on an FFS filesystem", boot); 308 } 309 310 if (fsync(fd) != 0) 311 err(1, "fsync: %s", boot); 312 313 if (fstat(fd, &statbuf) != 0) 314 err(1, "fstat: %s", boot); 315 316 close(fd); 317 318 /* Read superblock */ 319 devread(devfd, sblock, SBLOCK, SBSIZE, "superblock"); 320 fs = (struct fs *)sblock; 321 322 /* Sanity-check super-block. */ 323 if (fs->fs_magic != FS_MAGIC) 324 errx(1, "Bad magic number in superblock"); 325 if (fs->fs_inopb <= 0) 326 err(1, "Bad inopb=%d in superblock", fs->fs_inopb); 327 328 /* Read inode */ 329 if ((buf = malloc(fs->fs_bsize)) == NULL) 330 errx(1, "No memory for filesystem block"); 331 332 blk = fsbtodb(fs, ino_to_fsba(fs, statbuf.st_ino)); 333 devread(devfd, buf, blk, fs->fs_bsize, "inode"); 334 ip = (struct dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino); 335 336 /* 337 * Have the inode. Figure out how many blocks we need. 338 */ 339 ndb = howmany(ip->di_size, fs->fs_bsize); 340 if (ndb > maxblocknum) 341 errx(1, "Too many blocks"); 342 *block_count_p = ndb; 343 *block_size_p = fs->fs_bsize; 344 if (verbose) 345 printf("Will load %d blocks of size %d each.\n", 346 ndb, fs->fs_bsize); 347 348 /* 349 * Get the block numbers; we don't handle fragments 350 */ 351 ap = ip->di_db; 352 for (i = 0; i < NDADDR && *ap && ndb; i++, ap++, ndb--) { 353 blk = fsbtodb(fs, *ap); 354 if (verbose) 355 printf("%d: %d\n", i, blk); 356 block_table[i] = blk; 357 } 358 if (ndb == 0) 359 return 0; 360 361 /* 362 * Just one level of indirections; there isn't much room 363 * for more in the 1st-level bootblocks anyway. 364 */ 365 blk = fsbtodb(fs, ip->di_ib[0]); 366 devread(devfd, buf, blk, fs->fs_bsize, "indirect block"); 367 ap = (daddr_t *)buf; 368 for (; i < NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) { 369 blk = fsbtodb(fs, *ap); 370 if (verbose) 371 printf("%d: %d\n", i, blk); 372 block_table[i] = blk; 373 } 374 375 return 0; 376 } 377 378