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