mkbootimage.c revision 1.1 1 /* $NetBSD: mkbootimage.c,v 1.1 1999/04/02 08:40:27 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 extern char *__progname;
47
48 static void
49 usage()
50 {
51 fprintf(stderr,
52 "usage: %s [-n] [-v] inputfile [outputfile]\n", __progname);
53 exit(1);
54 }
55
56 int
57 main(int argc, char **argv)
58 {
59 struct stat insb;
60 struct 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, i;
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
91 infile = argv[0];
92 outfile = argv[1];
93
94 if (verbose) {
95 fprintf(stderr, "input file: %s\n", infile);
96 fprintf(stderr, "output file: %s\n",
97 outfile != NULL ? outfile : "<stdout>");
98 }
99 if (sizeof (struct boot_block) != BOOT_BLOCK_BLOCKSIZE)
100 errx(1, "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(1, "open %s", infile);
105 if (fstat(infd, &insb) == -1)
106 err(1, "fstat %s", infile);
107 if (!S_ISREG(insb.st_mode))
108 errx(1, "%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, BOOT_BLOCK_BLOCKSIZE);
116 outbufsize += sizeof (struct boot_block);
117
118 outbuf = malloc(outbufsize);
119 if (outbuf == NULL)
120 err(1, "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 boot_block), insb.st_size);
125 if (rv == -1)
126 err(1, "read %s", infile);
127 else if (rv != insb.st_size)
128 errx(1, "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 boot_block *)outbuf;
133 bb->bb_secsize = howmany(insb.st_size, BOOT_BLOCK_BLOCKSIZE);
134 bb->bb_secstart = 1;
135 for (i = 0; i < 63; i++) /* XXX use something better */
136 bb->bb_cksum += ((u_int64_t *)bb)[i];
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(0);
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(1, "open %s", outfile);
156
157 /* write the data */
158 rv = write(outfd, outbuf, outbufsize);
159 if (rv == -1)
160 err(1, "write %s", outfile);
161 else if (rv != outbufsize)
162 errx(1, "write %s: short write", outfile);
163 (void)close(outfd);
164
165 exit(0);
166 }
167