Home | History | Annotate | Line # | Download | only in mkbootimage
mkbootimage.c revision 1.1
      1  1.1  cgd /* $NetBSD: mkbootimage.c,v 1.1 1999/04/02 08:40:27 cgd Exp $ */
      2  1.1  cgd 
      3  1.1  cgd /*
      4  1.1  cgd  * Copyright (c) 1999 Christopher G. Demetriou.  All rights reserved.
      5  1.1  cgd  *
      6  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      7  1.1  cgd  * modification, are permitted provided that the following conditions
      8  1.1  cgd  * are met:
      9  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     10  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     11  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     14  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     15  1.1  cgd  *    must display the following acknowledgement:
     16  1.1  cgd  *      This product includes software developed by Christopher G. Demetriou
     17  1.1  cgd  *	for the NetBSD Project.
     18  1.1  cgd  * 4. The name of the author may not be used to endorse or promote products
     19  1.1  cgd  *    derived from this software without specific prior written permission
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1  cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1  cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1  cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1  cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1  cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1  cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1  cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1  cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1  cgd  */
     32  1.1  cgd 
     33  1.1  cgd #include <sys/param.h>				/* XXX for roundup, howmany */
     34  1.1  cgd #include <sys/stat.h>
     35  1.1  cgd #include <sys/disklabel.h>
     36  1.1  cgd #include <assert.h>
     37  1.1  cgd #include <err.h>
     38  1.1  cgd #include <fcntl.h>
     39  1.1  cgd #include <stdlib.h>
     40  1.1  cgd #include <stdio.h>
     41  1.1  cgd #include <string.h>
     42  1.1  cgd #include <unistd.h>
     43  1.1  cgd 
     44  1.1  cgd static void usage(void);
     45  1.1  cgd 
     46  1.1  cgd extern char *__progname;
     47  1.1  cgd 
     48  1.1  cgd static void
     49  1.1  cgd usage()
     50  1.1  cgd {
     51  1.1  cgd 	fprintf(stderr,
     52  1.1  cgd 	    "usage: %s [-n] [-v] inputfile [outputfile]\n", __progname);
     53  1.1  cgd 	exit(1);
     54  1.1  cgd }
     55  1.1  cgd 
     56  1.1  cgd int
     57  1.1  cgd main(int argc, char **argv)
     58  1.1  cgd {
     59  1.1  cgd 	struct stat insb;
     60  1.1  cgd 	struct boot_block *bb;
     61  1.1  cgd 	const char *infile, *outfile;
     62  1.1  cgd 	char *outbuf;
     63  1.1  cgd 	size_t outbufsize;
     64  1.1  cgd 	ssize_t rv;
     65  1.1  cgd 	int c, verbose, nowrite, infd, outfd, i;
     66  1.1  cgd 
     67  1.1  cgd 	verbose = nowrite = 0;
     68  1.1  cgd 
     69  1.1  cgd 	while ((c = getopt(argc, argv, "nv")) != -1) {
     70  1.1  cgd 		switch (c) {
     71  1.1  cgd 		case 'n':
     72  1.1  cgd 			/* Do not actually write the boot file */
     73  1.1  cgd 			nowrite = 1;
     74  1.1  cgd 			break;
     75  1.1  cgd 		case 'v':
     76  1.1  cgd 			/* Chat */
     77  1.1  cgd 			verbose = 1;
     78  1.1  cgd 			break;
     79  1.1  cgd 		default:
     80  1.1  cgd 			usage();
     81  1.1  cgd 		}
     82  1.1  cgd 	}
     83  1.1  cgd 
     84  1.1  cgd 	argc -= optind;
     85  1.1  cgd 	argv += optind;
     86  1.1  cgd 
     87  1.1  cgd 	if (argc != 1 && argc != 2) {
     88  1.1  cgd 		usage();
     89  1.1  cgd 	}
     90  1.1  cgd 
     91  1.1  cgd 	infile = argv[0];
     92  1.1  cgd 	outfile = argv[1];
     93  1.1  cgd 
     94  1.1  cgd 	if (verbose) {
     95  1.1  cgd 		fprintf(stderr, "input file: %s\n", infile);
     96  1.1  cgd 		fprintf(stderr, "output file: %s\n",
     97  1.1  cgd 		    outfile != NULL ? outfile : "<stdout>");
     98  1.1  cgd 	}
     99  1.1  cgd 	if (sizeof (struct boot_block) != BOOT_BLOCK_BLOCKSIZE)
    100  1.1  cgd 		errx(1, "boot_block structure badly sized (build error)");
    101  1.1  cgd 
    102  1.1  cgd 	/* Open the input file and check it out */
    103  1.1  cgd 	if ((infd = open(infile, O_RDONLY)) == -1)
    104  1.1  cgd 		err(1, "open %s", infile);
    105  1.1  cgd 	if (fstat(infd, &insb) == -1)
    106  1.1  cgd 		err(1, "fstat %s", infile);
    107  1.1  cgd 	if (!S_ISREG(insb.st_mode))
    108  1.1  cgd 		errx(1, "%s must be a regular file", infile);
    109  1.1  cgd 
    110  1.1  cgd 	/*
    111  1.1  cgd 	 * Allocate a buffer, with space to round up the input file
    112  1.1  cgd 	 * to the next block size boundary, and with space for the boot
    113  1.1  cgd 	 * block.
    114  1.1  cgd 	 */
    115  1.1  cgd 	outbufsize = roundup(insb.st_size, BOOT_BLOCK_BLOCKSIZE);
    116  1.1  cgd 	outbufsize += sizeof (struct boot_block);
    117  1.1  cgd 
    118  1.1  cgd 	outbuf = malloc(outbufsize);
    119  1.1  cgd 	if (outbuf == NULL)
    120  1.1  cgd 		err(1, "allocating output buffer");
    121  1.1  cgd 	memset(outbuf, 0, outbufsize);
    122  1.1  cgd 
    123  1.1  cgd 	/* read the file into the buffer, leaving room for the boot block */
    124  1.1  cgd 	rv = read(infd, outbuf + sizeof (struct boot_block), insb.st_size);
    125  1.1  cgd 	if (rv == -1)
    126  1.1  cgd 		err(1, "read %s", infile);
    127  1.1  cgd 	else if (rv != insb.st_size)
    128  1.1  cgd 		errx(1, "read %s: short read", infile);
    129  1.1  cgd 	(void)close(infd);
    130  1.1  cgd 
    131  1.1  cgd 	/* fill in the boot block fields, and checksum the boot block */
    132  1.1  cgd 	bb = (struct boot_block *)outbuf;
    133  1.1  cgd 	bb->bb_secsize = howmany(insb.st_size, BOOT_BLOCK_BLOCKSIZE);
    134  1.1  cgd 	bb->bb_secstart = 1;
    135  1.1  cgd 	for (i = 0; i < 63; i++)		/* XXX use something better */
    136  1.1  cgd 		bb->bb_cksum += ((u_int64_t *)bb)[i];
    137  1.1  cgd 
    138  1.1  cgd 	if (verbose) {
    139  1.1  cgd 		fprintf(stderr, "starting sector: %qu\n",
    140  1.1  cgd 		    (unsigned long long)bb->bb_secstart);
    141  1.1  cgd 		fprintf(stderr, "sector count: %qu\n",
    142  1.1  cgd 		    (unsigned long long)bb->bb_secsize);
    143  1.1  cgd 		fprintf(stderr, "checksum: %#qx\n",
    144  1.1  cgd 		    (unsigned long long)bb->bb_cksum);
    145  1.1  cgd 	}
    146  1.1  cgd 
    147  1.1  cgd 	if (nowrite)
    148  1.1  cgd 		exit(0);
    149  1.1  cgd 
    150  1.1  cgd 	/* set up the output file descriptor */
    151  1.1  cgd 	if (outfile == NULL) {
    152  1.1  cgd 		outfd = STDOUT_FILENO;
    153  1.1  cgd 		outfile = "<stdout>";
    154  1.1  cgd 	} else if ((outfd = open(outfile, O_WRONLY|O_CREAT, 0666)) == -1)
    155  1.1  cgd 		err(1, "open %s", outfile);
    156  1.1  cgd 
    157  1.1  cgd 	/* write the data */
    158  1.1  cgd 	rv = write(outfd, outbuf, outbufsize);
    159  1.1  cgd 	if (rv == -1)
    160  1.1  cgd 		err(1, "write %s", outfile);
    161  1.1  cgd 	else if (rv != outbufsize)
    162  1.1  cgd 		errx(1, "write %s: short write", outfile);
    163  1.1  cgd 	(void)close(outfd);
    164  1.1  cgd 
    165  1.1  cgd 	exit(0);
    166  1.1  cgd }
    167