Home | History | Annotate | Line # | Download | only in mkbootimage
      1  1.7  cegger /* $NetBSD: mkbootimage.c,v 1.7 2009/03/18 10:22:22 cegger 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.6   lukem #include <sys/bootblock.h>
     36  1.1     cgd #include <sys/disklabel.h>
     37  1.1     cgd #include <assert.h>
     38  1.1     cgd #include <err.h>
     39  1.1     cgd #include <fcntl.h>
     40  1.1     cgd #include <stdlib.h>
     41  1.1     cgd #include <stdio.h>
     42  1.1     cgd #include <string.h>
     43  1.1     cgd #include <unistd.h>
     44  1.5   lukem 
     45  1.1     cgd static void usage(void);
     46  1.1     cgd 
     47  1.1     cgd static void
     48  1.7  cegger usage(void)
     49  1.1     cgd {
     50  1.1     cgd 	fprintf(stderr,
     51  1.4     cgd 	    "usage: %s [-n] [-v] inputfile [outputfile]\n", getprogname());
     52  1.2     cgd 	exit(EXIT_FAILURE);
     53  1.1     cgd }
     54  1.1     cgd 
     55  1.1     cgd int
     56  1.1     cgd main(int argc, char **argv)
     57  1.1     cgd {
     58  1.1     cgd 	struct stat insb;
     59  1.5   lukem 	struct alpha_boot_block *bb;
     60  1.1     cgd 	const char *infile, *outfile;
     61  1.1     cgd 	char *outbuf;
     62  1.1     cgd 	size_t outbufsize;
     63  1.1     cgd 	ssize_t rv;
     64  1.2     cgd 	int c, verbose, nowrite, infd, outfd;
     65  1.1     cgd 
     66  1.1     cgd 	verbose = nowrite = 0;
     67  1.1     cgd 
     68  1.1     cgd 	while ((c = getopt(argc, argv, "nv")) != -1) {
     69  1.1     cgd 		switch (c) {
     70  1.1     cgd 		case 'n':
     71  1.1     cgd 			/* Do not actually write the boot file */
     72  1.1     cgd 			nowrite = 1;
     73  1.1     cgd 			break;
     74  1.1     cgd 		case 'v':
     75  1.1     cgd 			/* Chat */
     76  1.1     cgd 			verbose = 1;
     77  1.1     cgd 			break;
     78  1.1     cgd 		default:
     79  1.1     cgd 			usage();
     80  1.1     cgd 		}
     81  1.1     cgd 	}
     82  1.1     cgd 
     83  1.1     cgd 	argc -= optind;
     84  1.1     cgd 	argv += optind;
     85  1.1     cgd 
     86  1.2     cgd 	if (argc != 1 && argc != 2)
     87  1.1     cgd 		usage();
     88  1.1     cgd 
     89  1.1     cgd 	infile = argv[0];
     90  1.2     cgd 	outfile = argv[1];		/* NULL if argc == 1 */
     91  1.1     cgd 
     92  1.1     cgd 	if (verbose) {
     93  1.1     cgd 		fprintf(stderr, "input file: %s\n", infile);
     94  1.1     cgd 		fprintf(stderr, "output file: %s\n",
     95  1.1     cgd 		    outfile != NULL ? outfile : "<stdout>");
     96  1.1     cgd 	}
     97  1.5   lukem 	if (sizeof (struct alpha_boot_block) != ALPHA_BOOT_BLOCK_BLOCKSIZE)
     98  1.2     cgd 		errx(EXIT_FAILURE,
     99  1.5   lukem 		    "alpha_boot_block structure badly sized (build error)");
    100  1.1     cgd 
    101  1.1     cgd 	/* Open the input file and check it out */
    102  1.1     cgd 	if ((infd = open(infile, O_RDONLY)) == -1)
    103  1.2     cgd 		err(EXIT_FAILURE, "open %s", infile);
    104  1.1     cgd 	if (fstat(infd, &insb) == -1)
    105  1.2     cgd 		err(EXIT_FAILURE, "fstat %s", infile);
    106  1.1     cgd 	if (!S_ISREG(insb.st_mode))
    107  1.2     cgd 		errx(EXIT_FAILURE, "%s must be a regular file", infile);
    108  1.1     cgd 
    109  1.1     cgd 	/*
    110  1.1     cgd 	 * Allocate a buffer, with space to round up the input file
    111  1.1     cgd 	 * to the next block size boundary, and with space for the boot
    112  1.1     cgd 	 * block.
    113  1.1     cgd 	 */
    114  1.5   lukem 	outbufsize = roundup(insb.st_size, ALPHA_BOOT_BLOCK_BLOCKSIZE);
    115  1.5   lukem 	outbufsize += sizeof (struct alpha_boot_block);
    116  1.1     cgd 
    117  1.1     cgd 	outbuf = malloc(outbufsize);
    118  1.1     cgd 	if (outbuf == NULL)
    119  1.2     cgd 		err(EXIT_FAILURE, "allocating output buffer");
    120  1.1     cgd 	memset(outbuf, 0, outbufsize);
    121  1.1     cgd 
    122  1.1     cgd 	/* read the file into the buffer, leaving room for the boot block */
    123  1.5   lukem 	rv = read(infd, outbuf + sizeof (struct alpha_boot_block),
    124  1.5   lukem 	    insb.st_size);
    125  1.1     cgd 	if (rv == -1)
    126  1.2     cgd 		err(EXIT_FAILURE, "read %s", infile);
    127  1.1     cgd 	else if (rv != insb.st_size)
    128  1.2     cgd 		errx(EXIT_FAILURE, "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.5   lukem 	bb = (struct alpha_boot_block *)outbuf;
    133  1.5   lukem 	bb->bb_secsize = howmany(insb.st_size, ALPHA_BOOT_BLOCK_BLOCKSIZE);
    134  1.1     cgd 	bb->bb_secstart = 1;
    135  1.3     cgd 	bb->bb_flags = 0;
    136  1.5   lukem 	ALPHA_BOOT_BLOCK_CKSUM(bb, &bb->bb_cksum);
    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.2     cgd 		exit(EXIT_SUCCESS);
    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.2     cgd 		err(EXIT_FAILURE, "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.2     cgd 		err(EXIT_FAILURE, "write %s", outfile);
    161  1.1     cgd 	else if (rv != outbufsize)
    162  1.2     cgd 		errx(EXIT_FAILURE, "write %s: short write", outfile);
    163  1.1     cgd 	(void)close(outfd);
    164  1.1     cgd 
    165  1.2     cgd 	exit(EXIT_SUCCESS);
    166  1.1     cgd }
    167