1 /* $NetBSD: installboot.c,v 1.5 2000/07/10 22:01:16 jdolecek 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 <a.out.h> 49 #include <fcntl.h> 50 #include <nlist.h> 51 #include <stdlib.h> 52 #include <stdio.h> 53 #include <string.h> 54 #include <unistd.h> 55 #include <sys/disklabel.h> 56 57 int verbose, nowrite, hflag; 58 char *boot, *proto, *dev; 59 struct nlist nl[] = { 60 #define X_BLOCK_SIZE 0 61 { { "_block_size" } }, 62 #define X_BLOCK_COUNT 1 63 { { "_block_count" } }, 64 #define X_BLOCK_TABLE 2 65 { { "_block_table" } }, 66 { {NULL} } 67 }; 68 69 int *block_size_p; /* block size var. in prototype image */ 70 int *block_count_p; /* block count var. in prototype image */ 71 daddr_t *block_table; /* block number array in prototype image */ 72 int maxblocknum; /* size of this array */ 73 74 75 char *loadprotoblocks __P((char *, long *)); 76 int loadblocknums __P((char *, int)); 77 static void devread __P((int, void *, daddr_t, size_t, char *)); 78 static void usage __P((void)); 79 int main __P((int, char *[])); 80 81 82 static void 83 usage() 84 { 85 fprintf(stderr, 86 "usage: installboot [-n] [-v] [-h] <boot> <proto> <device>\n"); 87 exit(1); 88 } 89 90 int 91 main(argc, argv) 92 int argc; 93 char *argv[]; 94 { 95 int c; 96 int devfd; 97 char *protostore; 98 long protosize; 99 100 while ((c = getopt(argc, argv, "vnh")) != -1) { 101 switch (c) { 102 case 'h': 103 /* Don't strip a.out header */ 104 hflag = 1; 105 break; 106 case 'n': 107 /* Do not actually write the bootblock to disk */ 108 nowrite = 1; 109 break; 110 case 'v': 111 /* Chat */ 112 verbose = 1; 113 break; 114 default: 115 usage(); 116 } 117 } 118 119 if (argc - optind < 3) { 120 usage(); 121 } 122 123 boot = argv[optind]; 124 proto = argv[optind + 1]; 125 dev = argv[optind + 2]; 126 127 if (verbose) { 128 printf("boot: %s\n", boot); 129 printf("proto: %s\n", proto); 130 printf("device: %s\n", dev); 131 } 132 133 /* Load proto blocks into core */ 134 if ((protostore = loadprotoblocks(proto, &protosize)) == NULL) 135 exit(1); 136 137 /* XXX - Paranoia: Make sure size is aligned! */ 138 if (protosize & (DEV_BSIZE - 1)) 139 err(1, "proto bootblock bad size=%ld", protosize); 140 141 /* Open and check raw disk device */ 142 if ((devfd = open(dev, O_RDONLY, 0)) < 0) 143 err(1, "open: %s", dev); 144 145 /* Extract and load block numbers */ 146 if (loadblocknums(boot, devfd) != 0) 147 exit(1); 148 149 (void)close(devfd); 150 151 if (nowrite) 152 return 0; 153 154 /* Write patched proto bootblocks into the superblock */ 155 if (protosize > SBSIZE - DEV_BSIZE) 156 errx(1, "proto bootblocks too big"); 157 158 /* The primary bootblock needs to be written to the raw partition */ 159 dev[strlen(dev) - 1] = 'a' + RAW_PART; 160 161 if ((devfd = open(dev, O_RDWR, 0)) < 0) 162 err(1, "open: %s", dev); 163 164 if (lseek(devfd, DEV_BSIZE, SEEK_SET) != DEV_BSIZE) 165 err(1, "lseek bootstrap"); 166 167 /* Sync filesystems (to clean in-memory superblock?) */ 168 sync(); 169 170 if (write(devfd, protostore, protosize) != protosize) 171 err(1, "write bootstrap"); 172 (void)close(devfd); 173 return 0; 174 } 175 176 char * 177 loadprotoblocks(fname, size) 178 char *fname; 179 long *size; 180 { 181 int fd; 182 size_t tdsize; /* text+data size */ 183 size_t bbsize; /* boot block size (block aligned) */ 184 char *bp; 185 struct nlist *nlp; 186 struct exec eh; 187 long off; 188 189 fd = -1; 190 bp = NULL; 191 192 /* Locate block number array in proto file */ 193 if (nlist(fname, nl) != 0) { 194 warnx("nlist: %s: symbols not found", fname); 195 return NULL; 196 } 197 /* Validate symbol types (global data). */ 198 for (nlp = nl; nlp->n_un.n_name; nlp++) { 199 if (nlp->n_type != (N_DATA | N_EXT)) { 200 warnx("nlist: %s: wrong type", nlp->n_un.n_name); 201 return NULL; 202 } 203 } 204 205 if ((fd = open(fname, O_RDONLY)) < 0) { 206 warn("open: %s", fname); 207 return NULL; 208 } 209 if (read(fd, &eh, sizeof(eh)) != sizeof(eh)) { 210 warn("read: %s", fname); 211 goto bad; 212 } 213 if (N_GETMAGIC(eh) != OMAGIC) { 214 warn("bad magic: 0x%lx", eh.a_midmag); 215 goto bad; 216 } 217 /* 218 * We have to include the exec header in the beginning of 219 * the buffer, and leave extra space at the end in case 220 * the actual write to disk wants to skip the header. 221 */ 222 tdsize = eh.a_text + eh.a_data; 223 bbsize = tdsize + sizeof(eh); 224 bbsize = roundup(bbsize, DEV_BSIZE); 225 226 /* 227 * Allocate extra space here because the caller may copy 228 * the boot block starting at the end of the exec header. 229 * This prevents reading beyond the end of the buffer. 230 */ 231 if ((bp = calloc(bbsize + sizeof(eh), 1)) == NULL) { 232 warnx("malloc: %s: no memory", fname); 233 goto bad; 234 } 235 /* Copy the exec header and read the rest of the file. */ 236 memcpy(bp, &eh, sizeof(eh)); 237 if (read(fd, bp+sizeof(eh), tdsize) != tdsize) { 238 warn("read: %s", fname); 239 goto bad; 240 } 241 242 *size = bbsize; /* aligned to DEV_BSIZE */ 243 244 /* Calculate the symbols' locations within the proto file */ 245 off = N_DATOFF(eh) - N_DATADDR(eh) - (eh.a_entry - N_TXTADDR(eh)); 246 block_size_p = (int *) (bp + nl[X_BLOCK_SIZE ].n_value + off); 247 block_count_p = (int *) (bp + nl[X_BLOCK_COUNT].n_value + off); 248 block_table = (daddr_t *) (bp + nl[X_BLOCK_TABLE].n_value + off); 249 maxblocknum = *block_count_p; 250 251 if (verbose) { 252 printf("%s: entry point %#lx\n", fname, eh.a_entry); 253 printf("proto bootblock size %ld\n", *size); 254 printf("room for %d filesystem blocks at %#lx\n", 255 maxblocknum, nl[X_BLOCK_TABLE].n_value); 256 } 257 258 close(fd); 259 if (!hflag) 260 bp += sizeof(struct exec); 261 return bp; 262 263 bad: 264 if (bp) 265 free(bp); 266 if (fd >= 0) 267 close(fd); 268 return NULL; 269 } 270 271 static void 272 devread(fd, buf, blk, size, msg) 273 int fd; 274 void *buf; 275 daddr_t blk; 276 size_t size; 277 char *msg; 278 { 279 if (lseek(fd, dbtob(blk), SEEK_SET) != dbtob(blk)) 280 err(1, "%s: devread: lseek", msg); 281 282 if (read(fd, buf, size) != size) 283 err(1, "%s: devread: read", msg); 284 } 285 286 static char sblock[SBSIZE]; 287 288 int 289 loadblocknums(boot, devfd) 290 char *boot; 291 int devfd; 292 { 293 int i, fd; 294 struct stat statbuf; 295 struct statfs statfsbuf; 296 struct fs *fs; 297 char *buf; 298 daddr_t blk, *ap; 299 struct dinode *ip; 300 int ndb; 301 302 /* 303 * Open 2nd-level boot program and record the block numbers 304 * it occupies on the filesystem represented by `devfd'. 305 */ 306 307 /* Make sure the (probably new) boot file is on disk. */ 308 sync(); sleep(1); 309 310 if ((fd = open(boot, O_RDONLY)) < 0) 311 err(1, "open: %s", boot); 312 313 if (fstatfs(fd, &statfsbuf) != 0) 314 err(1, "statfs: %s", boot); 315 316 if (strncmp(statfsbuf.f_fstypename, "ffs", MFSNAMELEN) && 317 strncmp(statfsbuf.f_fstypename, "ufs", MFSNAMELEN) ) { 318 errx(1, "%s: must be on an FFS filesystem", boot); 319 } 320 321 if (fsync(fd) != 0) 322 err(1, "fsync: %s", boot); 323 324 if (fstat(fd, &statbuf) != 0) 325 err(1, "fstat: %s", boot); 326 327 close(fd); 328 329 /* Read superblock */ 330 devread(devfd, sblock, SBLOCK, SBSIZE, "superblock"); 331 fs = (struct fs *)sblock; 332 333 /* Sanity-check super-block. */ 334 if (fs->fs_magic != FS_MAGIC) 335 errx(1, "Bad magic number in superblock"); 336 if (fs->fs_inopb <= 0) 337 err(1, "Bad inopb=%d in superblock", fs->fs_inopb); 338 339 /* Read inode */ 340 if ((buf = malloc(fs->fs_bsize)) == NULL) 341 errx(1, "No memory for filesystem block"); 342 343 blk = fsbtodb(fs, ino_to_fsba(fs, statbuf.st_ino)); 344 devread(devfd, buf, blk, fs->fs_bsize, "inode"); 345 ip = (struct dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino); 346 347 /* 348 * Have the inode. Figure out how many blocks we need. 349 */ 350 ndb = howmany(ip->di_size, fs->fs_bsize); 351 if (ndb > maxblocknum) 352 errx(1, "Too many blocks"); 353 *block_count_p = ndb; 354 *block_size_p = fs->fs_bsize; 355 if (verbose) 356 printf("Will load %d blocks of size %d each.\n", 357 ndb, fs->fs_bsize); 358 359 /* 360 * Get the block numbers; we don't handle fragments 361 */ 362 ap = ip->di_db; 363 for (i = 0; i < NDADDR && *ap && ndb; i++, ap++, ndb--) { 364 blk = fsbtodb(fs, *ap); 365 if (verbose) 366 printf("%d: %d\n", i, blk); 367 block_table[i] = blk; 368 } 369 if (ndb == 0) 370 return 0; 371 372 /* 373 * Just one level of indirections; there isn't much room 374 * for more in the 1st-level bootblocks anyway. 375 */ 376 blk = fsbtodb(fs, ip->di_ib[0]); 377 devread(devfd, buf, blk, fs->fs_bsize, "indirect block"); 378 ap = (daddr_t *)buf; 379 for (; i < NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) { 380 blk = fsbtodb(fs, *ap); 381 if (verbose) 382 printf("%d: %d\n", i, blk); 383 block_table[i] = blk; 384 } 385 386 return 0; 387 } 388 389