1/* $NetBSD: mkbootimage.c,v 1.6 2002/05/14 06:34:20 lukem 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/bootblock.h>
36#include <sys/disklabel.h>
37#include <assert.h>
38#include <err.h>
39#include <fcntl.h>
40#include <stdlib.h>
41#include <stdio.h>
42#include <string.h>
43#include <unistd.h>
44
45static void usage(void);
46
47static void
48usage(void)
49{
50	fprintf(stderr,
51	    "usage: %s [-n] [-v] inputfile [outputfile]\n", getprogname());
52	exit(EXIT_FAILURE);
53}
54
55int
56main(int argc, char **argv)
57{
58	struct stat insb;
59	struct alpha_boot_block *bb;
60	const char *infile, *outfile;
61	char *outbuf;
62	size_t outbufsize;
63	ssize_t rv;
64	int c, verbose, nowrite, infd, outfd;
65
66	verbose = nowrite = 0;
67
68	while ((c = getopt(argc, argv, "nv")) != -1) {
69		switch (c) {
70		case 'n':
71			/* Do not actually write the boot file */
72			nowrite = 1;
73			break;
74		case 'v':
75			/* Chat */
76			verbose = 1;
77			break;
78		default:
79			usage();
80		}
81	}
82
83	argc -= optind;
84	argv += optind;
85
86	if (argc != 1 && argc != 2)
87		usage();
88
89	infile = argv[0];
90	outfile = argv[1];		/* NULL if argc == 1 */
91
92	if (verbose) {
93		fprintf(stderr, "input file: %s\n", infile);
94		fprintf(stderr, "output file: %s\n",
95		    outfile != NULL ? outfile : "<stdout>");
96	}
97	if (sizeof (struct alpha_boot_block) != ALPHA_BOOT_BLOCK_BLOCKSIZE)
98		errx(EXIT_FAILURE,
99		    "alpha_boot_block structure badly sized (build error)");
100
101	/* Open the input file and check it out */
102	if ((infd = open(infile, O_RDONLY)) == -1)
103		err(EXIT_FAILURE, "open %s", infile);
104	if (fstat(infd, &insb) == -1)
105		err(EXIT_FAILURE, "fstat %s", infile);
106	if (!S_ISREG(insb.st_mode))
107		errx(EXIT_FAILURE, "%s must be a regular file", infile);
108
109	/*
110	 * Allocate a buffer, with space to round up the input file
111	 * to the next block size boundary, and with space for the boot
112	 * block.
113	 */
114	outbufsize = roundup(insb.st_size, ALPHA_BOOT_BLOCK_BLOCKSIZE);
115	outbufsize += sizeof (struct alpha_boot_block);
116
117	outbuf = malloc(outbufsize);
118	if (outbuf == NULL)
119		err(EXIT_FAILURE, "allocating output buffer");
120	memset(outbuf, 0, outbufsize);
121
122	/* read the file into the buffer, leaving room for the boot block */
123	rv = read(infd, outbuf + sizeof (struct alpha_boot_block),
124	    insb.st_size);
125	if (rv == -1)
126		err(EXIT_FAILURE, "read %s", infile);
127	else if (rv != insb.st_size)
128		errx(EXIT_FAILURE, "read %s: short read", infile);
129	(void)close(infd);
130
131	/* fill in the boot block fields, and checksum the boot block */
132	bb = (struct alpha_boot_block *)outbuf;
133	bb->bb_secsize = howmany(insb.st_size, ALPHA_BOOT_BLOCK_BLOCKSIZE);
134	bb->bb_secstart = 1;
135	bb->bb_flags = 0;
136	ALPHA_BOOT_BLOCK_CKSUM(bb, &bb->bb_cksum);
137
138	if (verbose) {
139		fprintf(stderr, "starting sector: %qu\n",
140		    (unsigned long long)bb->bb_secstart);
141		fprintf(stderr, "sector count: %qu\n",
142		    (unsigned long long)bb->bb_secsize);
143		fprintf(stderr, "checksum: %#qx\n",
144		    (unsigned long long)bb->bb_cksum);
145	}
146
147	if (nowrite)
148		exit(EXIT_SUCCESS);
149
150	/* set up the output file descriptor */
151	if (outfile == NULL) {
152		outfd = STDOUT_FILENO;
153		outfile = "<stdout>";
154	} else if ((outfd = open(outfile, O_WRONLY|O_CREAT, 0666)) == -1)
155		err(EXIT_FAILURE, "open %s", outfile);
156
157	/* write the data */
158	rv = write(outfd, outbuf, outbufsize);
159	if (rv == -1)
160		err(EXIT_FAILURE, "write %s", outfile);
161	else if (rv != outbufsize)
162		errx(EXIT_FAILURE, "write %s: short write", outfile);
163	(void)close(outfd);
164
165	exit(EXIT_SUCCESS);
166}
167