1 /* $NetBSD: installboot.c,v 1.14 2008/01/12 09:54:30 tsutsui 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 <sys/statvfs.h> 45 #include <ufs/ufs/dinode.h> 46 #include <ufs/ufs/dir.h> 47 #include <ufs/ffs/fs.h> 48 #include <err.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 #include "loadfile.h" 58 59 int verbose, nowrite, hflag; 60 char *boot, *proto, *dev; 61 62 struct nlist nl[] = { 63 #define X_BLOCK_SIZE 0 64 #define X_BLOCK_COUNT 1 65 #define X_BLOCK_TABLE 2 66 #ifdef __ELF__ 67 { "block_size" }, 68 { "block_count" }, 69 { "block_table" }, 70 #else 71 { "_block_size" }, 72 { "_block_count" }, 73 { "_block_table" }, 74 #endif 75 { NULL } 76 }; 77 78 int *block_size_p; /* block size var. in prototype image */ 79 int *block_count_p; /* block count var. in prototype image */ 80 /* XXX ondisk32 */ 81 int32_t *block_table; /* block number array in prototype image */ 82 int maxblocknum; /* size of this array */ 83 84 85 char *loadprotoblocks(char *, size_t *); 86 int loadblocknums(char *, int); 87 static void devread(int, void *, daddr_t, size_t, char *); 88 static void usage(void); 89 int main(int, char *[]); 90 91 92 static void 93 usage(void) 94 { 95 96 fprintf(stderr, 97 "usage: installboot [-n] [-v] [-h] <boot> <proto> <device>\n"); 98 exit(1); 99 } 100 101 int 102 main(int argc, char *argv[]) 103 { 104 int c; 105 int devfd; 106 char *protostore; 107 size_t protosize; 108 109 while ((c = getopt(argc, argv, "vnh")) != -1) { 110 switch (c) { 111 case 'h': 112 /* Don't strip a.out header */ 113 hflag = 1; 114 break; 115 case 'n': 116 /* Do not actually write the bootblock to disk */ 117 nowrite = 1; 118 break; 119 case 'v': 120 /* Chat */ 121 verbose = 1; 122 break; 123 default: 124 usage(); 125 } 126 } 127 128 if (argc - optind < 3) { 129 usage(); 130 } 131 132 boot = argv[optind]; 133 proto = argv[optind + 1]; 134 dev = argv[optind + 2]; 135 136 if (verbose) { 137 printf("boot: %s\n", boot); 138 printf("proto: %s\n", proto); 139 printf("device: %s\n", dev); 140 } 141 142 /* Load proto blocks into core */ 143 if ((protostore = loadprotoblocks(proto, &protosize)) == NULL) 144 exit(1); 145 146 /* XXX - Paranoia: Make sure size is aligned! */ 147 if (protosize & (DEV_BSIZE - 1)) 148 err(1, "proto bootblock bad size=%d", protosize); 149 150 /* Open and check raw disk device */ 151 if ((devfd = open(dev, O_RDONLY, 0)) < 0) 152 err(1, "open: %s", dev); 153 154 /* Extract and load block numbers */ 155 if (loadblocknums(boot, devfd) != 0) 156 exit(1); 157 158 (void)close(devfd); 159 160 if (nowrite) 161 return 0; 162 163 /* Write patched proto bootblocks into the superblock */ 164 if (protosize > SBLOCKSIZE - DEV_BSIZE) 165 errx(1, "proto bootblocks too big"); 166 167 /* The primary bootblock needs to be written to the raw partition */ 168 dev[strlen(dev) - 1] = 'a' + RAW_PART; 169 170 if ((devfd = open(dev, O_RDWR, 0)) < 0) 171 err(1, "open: %s", dev); 172 173 if (lseek(devfd, DEV_BSIZE, SEEK_SET) != DEV_BSIZE) 174 err(1, "lseek bootstrap"); 175 176 /* Sync filesystems (to clean in-memory superblock?) */ 177 sync(); 178 179 if (write(devfd, protostore, protosize) != protosize) 180 err(1, "write bootstrap"); 181 (void)close(devfd); 182 return 0; 183 } 184 185 char * 186 loadprotoblocks(char *fname, size_t *size) 187 { 188 int fd; 189 u_long marks[MARK_MAX], bp, offs; 190 191 fd = -1; 192 193 /* Locate block number array in proto file */ 194 if (nlist(fname, nl) != 0) { 195 warnx("nlist: %s: symbols not found", fname); 196 return NULL; 197 } 198 199 marks[MARK_START] = 0; 200 if ((fd = loadfile(fname, marks, COUNT_TEXT|COUNT_DATA)) == -1) 201 return NULL; 202 (void)close(fd); 203 204 *size = roundup(marks[MARK_END] - marks[MARK_START], DEV_BSIZE); 205 bp = (u_long)malloc(*size); 206 207 offs = marks[MARK_START]; 208 marks[MARK_START] = bp - offs; 209 210 if ((fd = loadfile(fname, marks, LOAD_TEXT|LOAD_DATA)) == -1) 211 return NULL; 212 (void)close(fd); 213 214 /* Calculate the symbols' locations within the proto file */ 215 block_size_p = (int *)(bp + (nl[X_BLOCK_SIZE ].n_value - offs)); 216 block_count_p = (int *)(bp + (nl[X_BLOCK_COUNT].n_value - offs)); 217 /* XXX ondisk32 */ 218 block_table = (int32_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(int fd, void *buf, daddr_t blk, size_t size, char *msg) 237 { 238 239 if (lseek(fd, dbtob(blk), SEEK_SET) != dbtob(blk)) 240 err(1, "%s: devread: lseek", msg); 241 242 if (read(fd, buf, size) != size) 243 err(1, "%s: devread: read", msg); 244 } 245 246 static char sblock[SBLOCKSIZE]; 247 248 int 249 loadblocknums(char *boot, int devfd) 250 { 251 int i, fd; 252 struct stat statbuf; 253 struct statvfs statvfsbuf; 254 struct fs *fs; 255 char *buf; 256 daddr_t blk, *ap; 257 struct ufs1_dinode *ip; 258 int ndb; 259 260 /* 261 * Open 2nd-level boot program and record the block numbers 262 * it occupies on the filesystem represented by `devfd'. 263 */ 264 265 /* Make sure the (probably new) boot file is on disk. */ 266 sync(); sleep(1); 267 268 if ((fd = open(boot, O_RDONLY)) < 0) 269 err(1, "open: %s", boot); 270 271 if (fstatvfs(fd, &statvfsbuf) != 0) 272 err(1, "statfs: %s", boot); 273 274 if (strncmp(statvfsbuf.f_fstypename, "ffs", 275 sizeof(statvfsbuf.f_fstypename)) && 276 strncmp(statvfsbuf.f_fstypename, "ufs", 277 sizeof(statvfsbuf.f_fstypename))) { 278 errx(1, "%s: must be on an FFS filesystem", boot); 279 } 280 281 if (fsync(fd) != 0) 282 err(1, "fsync: %s", boot); 283 284 if (fstat(fd, &statbuf) != 0) 285 err(1, "fstat: %s", boot); 286 287 close(fd); 288 289 /* Read superblock */ 290 devread(devfd, sblock, (daddr_t)(BBSIZE / DEV_BSIZE), 291 SBLOCKSIZE, "superblock"); 292 fs = (struct fs *)sblock; 293 294 /* Sanity-check super-block. */ 295 if (fs->fs_magic != FS_UFS1_MAGIC) 296 errx(1, "Bad magic number in superblock, must be UFS1"); 297 if (fs->fs_inopb <= 0) 298 err(1, "Bad inopb=%d in superblock", fs->fs_inopb); 299 300 /* Read inode */ 301 if ((buf = malloc(fs->fs_bsize)) == NULL) 302 errx(1, "No memory for filesystem block"); 303 304 blk = fsbtodb(fs, ino_to_fsba(fs, statbuf.st_ino)); 305 devread(devfd, buf, blk, fs->fs_bsize, "inode"); 306 ip = (struct ufs1_dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino); 307 308 /* 309 * Have the inode. Figure out how many blocks we need. 310 */ 311 ndb = howmany(ip->di_size, fs->fs_bsize); 312 if (ndb > maxblocknum) 313 errx(1, "Too many blocks"); 314 *block_count_p = ndb; 315 *block_size_p = fs->fs_bsize; 316 if (verbose) 317 printf("Will load %d blocks of size %d each.\n", 318 ndb, fs->fs_bsize); 319 320 /* 321 * Get the block numbers; we don't handle fragments 322 */ 323 ap = ip->di_db; 324 for (i = 0; i < NDADDR && *ap && ndb; i++, ap++, ndb--) { 325 blk = fsbtodb(fs, *ap); 326 if (verbose) 327 printf("%d: %d\n", i, blk); 328 block_table[i] = blk; 329 } 330 if (ndb == 0) 331 return 0; 332 333 /* 334 * Just one level of indirections; there isn't much room 335 * for more in the 1st-level bootblocks anyway. 336 */ 337 blk = fsbtodb(fs, ip->di_ib[0]); 338 devread(devfd, buf, blk, fs->fs_bsize, "indirect block"); 339 /* XXX ondisk32 */ 340 ap = (int32_t *)buf; 341 for (; i < NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) { 342 blk = fsbtodb(fs, *ap); 343 if (verbose) 344 printf("%d: %d\n", i, blk); 345 block_table[i] = blk; 346 } 347 348 return 0; 349 } 350