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