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