Home | History | Annotate | Line # | Download | only in mkbootimage
mkbootimage.c revision 1.5
      1  1.5  lukem /* $NetBSD: mkbootimage.c,v 1.5 2002/04/03 06:16:03 lukem 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.5  lukem #include <dev/dec/dec_boot.h>
     45  1.5  lukem 
     46  1.1    cgd static void usage(void);
     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.4    cgd 	    "usage: %s [-n] [-v] inputfile [outputfile]\n", getprogname());
     53  1.2    cgd 	exit(EXIT_FAILURE);
     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.5  lukem 	struct alpha_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.2    cgd 	int c, verbose, nowrite, infd, outfd;
     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.2    cgd 	if (argc != 1 && argc != 2)
     88  1.1    cgd 		usage();
     89  1.1    cgd 
     90  1.1    cgd 	infile = argv[0];
     91  1.2    cgd 	outfile = argv[1];		/* NULL if argc == 1 */
     92  1.1    cgd 
     93  1.1    cgd 	if (verbose) {
     94  1.1    cgd 		fprintf(stderr, "input file: %s\n", infile);
     95  1.1    cgd 		fprintf(stderr, "output file: %s\n",
     96  1.1    cgd 		    outfile != NULL ? outfile : "<stdout>");
     97  1.1    cgd 	}
     98  1.5  lukem 	if (sizeof (struct alpha_boot_block) != ALPHA_BOOT_BLOCK_BLOCKSIZE)
     99  1.2    cgd 		errx(EXIT_FAILURE,
    100  1.5  lukem 		    "alpha_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.2    cgd 		err(EXIT_FAILURE, "open %s", infile);
    105  1.1    cgd 	if (fstat(infd, &insb) == -1)
    106  1.2    cgd 		err(EXIT_FAILURE, "fstat %s", infile);
    107  1.1    cgd 	if (!S_ISREG(insb.st_mode))
    108  1.2    cgd 		errx(EXIT_FAILURE, "%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.5  lukem 	outbufsize = roundup(insb.st_size, ALPHA_BOOT_BLOCK_BLOCKSIZE);
    116  1.5  lukem 	outbufsize += sizeof (struct alpha_boot_block);
    117  1.1    cgd 
    118  1.1    cgd 	outbuf = malloc(outbufsize);
    119  1.1    cgd 	if (outbuf == NULL)
    120  1.2    cgd 		err(EXIT_FAILURE, "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.5  lukem 	rv = read(infd, outbuf + sizeof (struct alpha_boot_block),
    125  1.5  lukem 	    insb.st_size);
    126  1.1    cgd 	if (rv == -1)
    127  1.2    cgd 		err(EXIT_FAILURE, "read %s", infile);
    128  1.1    cgd 	else if (rv != insb.st_size)
    129  1.2    cgd 		errx(EXIT_FAILURE, "read %s: short read", infile);
    130  1.1    cgd 	(void)close(infd);
    131  1.1    cgd 
    132  1.1    cgd 	/* fill in the boot block fields, and checksum the boot block */
    133  1.5  lukem 	bb = (struct alpha_boot_block *)outbuf;
    134  1.5  lukem 	bb->bb_secsize = howmany(insb.st_size, ALPHA_BOOT_BLOCK_BLOCKSIZE);
    135  1.1    cgd 	bb->bb_secstart = 1;
    136  1.3    cgd 	bb->bb_flags = 0;
    137  1.5  lukem 	ALPHA_BOOT_BLOCK_CKSUM(bb, &bb->bb_cksum);
    138  1.1    cgd 
    139  1.1    cgd 	if (verbose) {
    140  1.1    cgd 		fprintf(stderr, "starting sector: %qu\n",
    141  1.1    cgd 		    (unsigned long long)bb->bb_secstart);
    142  1.1    cgd 		fprintf(stderr, "sector count: %qu\n",
    143  1.1    cgd 		    (unsigned long long)bb->bb_secsize);
    144  1.1    cgd 		fprintf(stderr, "checksum: %#qx\n",
    145  1.1    cgd 		    (unsigned long long)bb->bb_cksum);
    146  1.1    cgd 	}
    147  1.1    cgd 
    148  1.1    cgd 	if (nowrite)
    149  1.2    cgd 		exit(EXIT_SUCCESS);
    150  1.1    cgd 
    151  1.1    cgd 	/* set up the output file descriptor */
    152  1.1    cgd 	if (outfile == NULL) {
    153  1.1    cgd 		outfd = STDOUT_FILENO;
    154  1.1    cgd 		outfile = "<stdout>";
    155  1.1    cgd 	} else if ((outfd = open(outfile, O_WRONLY|O_CREAT, 0666)) == -1)
    156  1.2    cgd 		err(EXIT_FAILURE, "open %s", outfile);
    157  1.1    cgd 
    158  1.1    cgd 	/* write the data */
    159  1.1    cgd 	rv = write(outfd, outbuf, outbufsize);
    160  1.1    cgd 	if (rv == -1)
    161  1.2    cgd 		err(EXIT_FAILURE, "write %s", outfile);
    162  1.1    cgd 	else if (rv != outbufsize)
    163  1.2    cgd 		errx(EXIT_FAILURE, "write %s: short write", outfile);
    164  1.1    cgd 	(void)close(outfd);
    165  1.1    cgd 
    166  1.2    cgd 	exit(EXIT_SUCCESS);
    167  1.1    cgd }
    168