installboot.c revision 1.3 1 1.3 wdk /* $NetBSD: installboot.c,v 1.3 2001/07/08 04:25:37 wdk Exp $ */
2 1.1 wdk
3 1.1 wdk /*
4 1.1 wdk * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.1 wdk * All rights reserved.
6 1.1 wdk *
7 1.1 wdk * This code is derived from software contributed to The NetBSD Foundation
8 1.1 wdk * by Wayne Knowles
9 1.1 wdk *
10 1.1 wdk * Redistribution and use in source and binary forms, with or without
11 1.1 wdk * modification, are permitted provided that the following conditions
12 1.1 wdk * are met:
13 1.1 wdk * 1. Redistributions of source code must retain the above copyright
14 1.1 wdk * notice, this list of conditions and the following disclaimer.
15 1.1 wdk * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 wdk * notice, this list of conditions and the following disclaimer in the
17 1.1 wdk * documentation and/or other materials provided with the distribution.
18 1.1 wdk * 3. All advertising materials mentioning features or use of this software
19 1.1 wdk * must display the following acknowledgement:
20 1.1 wdk * This product includes software developed by the NetBSD
21 1.1 wdk * Foundation, Inc. and its contributors.
22 1.1 wdk * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 wdk * contributors may be used to endorse or promote products derived
24 1.1 wdk * from this software without specific prior written permission.
25 1.1 wdk *
26 1.1 wdk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 wdk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 wdk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 wdk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 wdk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 wdk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 wdk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 wdk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 wdk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 wdk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 wdk * POSSIBILITY OF SUCH DAMAGE.
37 1.1 wdk */
38 1.1 wdk
39 1.1 wdk #include <assert.h>
40 1.1 wdk #include <err.h>
41 1.1 wdk #include <fcntl.h>
42 1.1 wdk #include <stdlib.h>
43 1.1 wdk #include <stdio.h>
44 1.1 wdk #include <string.h>
45 1.1 wdk #include <unistd.h>
46 1.1 wdk
47 1.1 wdk #include <sys/param.h>
48 1.1 wdk #include <sys/stat.h>
49 1.1 wdk #include <sys/disklabel.h>
50 1.1 wdk
51 1.1 wdk
52 1.1 wdk #define VERBOSE(msg) if (verbose) \
53 1.1 wdk fprintf(stderr, msg)
54 1.1 wdk #define FATAL(a1,a2) errx(EXIT_FAILURE, a1, a2)
55 1.1 wdk #define FATALIO(a1,a2) err(EXIT_FAILURE, a1, a2)
56 1.1 wdk
57 1.1 wdk #define BOOTBLOCK_NUMBER 2
58 1.1 wdk #define BOOTBLOCK_OFFSET BOOTBLOCK_NUMBER*DEV_BSIZE
59 1.1 wdk #define DEFAULT_BOOTFILE "boot"
60 1.1 wdk
61 1.1 wdk static void usage __P((void));
62 1.1 wdk static void do_list __P((const char *));
63 1.1 wdk static void do_remove __P((const char *, const char *));
64 1.1 wdk static void do_install __P((const char *, const char *, const char *));
65 1.1 wdk static int mipsvh_cksum __P((struct mips_volheader *));
66 1.1 wdk static void read_volheader __P((const char *, struct mips_volheader *));
67 1.1 wdk static void write_volheader __P((const char *, struct mips_volheader *));
68 1.1 wdk static struct mips_voldir *voldir_findfile __P((struct mips_volheader *,
69 1.1 wdk const char *, int));
70 1.1 wdk
71 1.1 wdk int verbose, nowrite;
72 1.1 wdk
73 1.1 wdk static void
74 1.1 wdk usage()
75 1.1 wdk {
76 1.1 wdk
77 1.1 wdk fprintf(stderr, "usage:\n");
78 1.2 cgd fprintf(stderr, "\t%s [-nv] disk bootstrap [name]\n", getprogname());
79 1.2 cgd fprintf(stderr, "\t%s -r [-nv] disk [name]\n", getprogname());
80 1.2 cgd fprintf(stderr, "\t%s -l [-nv] disk\n", getprogname());
81 1.1 wdk exit(EXIT_FAILURE);
82 1.1 wdk }
83 1.1 wdk
84 1.1 wdk int
85 1.1 wdk main(int argc, char *argv[])
86 1.1 wdk {
87 1.1 wdk const char *disk;
88 1.1 wdk int c, rflag, lflag;
89 1.1 wdk
90 1.1 wdk rflag = lflag = verbose = nowrite = 0;
91 1.1 wdk
92 1.1 wdk while ((c = getopt(argc, argv, "lnrv")) != -1) {
93 1.1 wdk switch (c) {
94 1.1 wdk case 'l':
95 1.1 wdk /* List volume directory contents */
96 1.1 wdk lflag = 1;
97 1.1 wdk break;
98 1.1 wdk case 'n':
99 1.1 wdk /* Disable write of boot sectors */
100 1.1 wdk nowrite = 1;
101 1.1 wdk break;
102 1.1 wdk case 'r':
103 1.1 wdk /* Clear any existing boot block */
104 1.1 wdk rflag = 1;
105 1.1 wdk break;
106 1.1 wdk case 'v':
107 1.1 wdk /* Verbose output */
108 1.1 wdk verbose = 1;
109 1.1 wdk break;
110 1.1 wdk default:
111 1.1 wdk usage();
112 1.1 wdk }
113 1.1 wdk }
114 1.1 wdk
115 1.1 wdk argc -= optind;
116 1.1 wdk argv += optind;
117 1.1 wdk
118 1.1 wdk if ((lflag && rflag) || argc < 1 || (lflag && argc != 1) ||
119 1.1 wdk (rflag && argc > 3) || argc > 4)
120 1.1 wdk usage();
121 1.1 wdk
122 1.1 wdk disk = argv[0];
123 1.1 wdk
124 1.1 wdk if (lflag)
125 1.1 wdk do_list(disk);
126 1.1 wdk else if (rflag)
127 1.1 wdk do_remove(disk, argc==2?argv[1]:DEFAULT_BOOTFILE);
128 1.1 wdk else
129 1.1 wdk do_install(disk, argv[1], argc==3?argv[2]:DEFAULT_BOOTFILE);
130 1.1 wdk
131 1.1 wdk exit(EXIT_SUCCESS);
132 1.1 wdk }
133 1.1 wdk
134 1.1 wdk static void
135 1.1 wdk do_list(disk)
136 1.1 wdk const char *disk;
137 1.1 wdk {
138 1.1 wdk struct mips_volheader vh;
139 1.1 wdk struct mips_voldir *vdp;
140 1.1 wdk int i;
141 1.1 wdk
142 1.1 wdk read_volheader(disk, &vh);
143 1.1 wdk
144 1.1 wdk printf("Slot\t LBN\tLength\tFilename\n");
145 1.1 wdk printf("------------------------------------------\n");
146 1.1 wdk for (i=0, vdp=vh.vh_voldir; i<MIPS_NVOLDIR; i++, vdp++)
147 1.1 wdk if (vdp->vd_len)
148 1.1 wdk printf("%2d:\t%5d\t%6d\t%s\n", i, vdp->vd_lba,
149 1.1 wdk vdp->vd_len, vdp->vd_name);
150 1.1 wdk }
151 1.1 wdk
152 1.1 wdk static void
153 1.1 wdk do_remove(disk, filename)
154 1.1 wdk const char *disk;
155 1.1 wdk const char *filename;
156 1.1 wdk {
157 1.1 wdk struct mips_volheader vh;
158 1.1 wdk struct mips_voldir *vdp;
159 1.1 wdk
160 1.1 wdk read_volheader(disk, &vh);
161 1.1 wdk vdp = voldir_findfile(&vh, filename, 0);
162 1.1 wdk if (vdp == NULL)
163 1.1 wdk FATAL("%s: file not found", disk);
164 1.1 wdk
165 1.3 wdk memset(vdp, 0, sizeof(*vdp));
166 1.1 wdk
167 1.1 wdk /* Update volume header */
168 1.1 wdk write_volheader(disk, &vh);
169 1.1 wdk }
170 1.1 wdk
171 1.1 wdk static void
172 1.1 wdk do_install(disk, bootstrap, bootname)
173 1.1 wdk const char *disk;
174 1.1 wdk const char *bootstrap;
175 1.1 wdk const char *bootname;
176 1.1 wdk {
177 1.1 wdk struct stat bootstrapsb;
178 1.1 wdk struct mips_volheader vh;
179 1.1 wdk struct mips_voldir *vdp;
180 1.1 wdk int fd;
181 1.1 wdk char *boot_code;
182 1.1 wdk size_t boot_size;
183 1.1 wdk ssize_t len;
184 1.1 wdk
185 1.1 wdk /* Open the input file and check it out */
186 1.1 wdk if ((fd = open(bootstrap, O_RDONLY)) == -1)
187 1.1 wdk FATALIO("open %s", bootstrap);
188 1.1 wdk if (fstat(fd, &bootstrapsb) == -1)
189 1.1 wdk FATALIO("fstat %s", bootstrap);
190 1.1 wdk if (!S_ISREG(bootstrapsb.st_mode))
191 1.1 wdk FATAL("%s must be a regular file", bootstrap);
192 1.1 wdk
193 1.1 wdk boot_size = roundup(bootstrapsb.st_size, DEV_BSIZE);
194 1.1 wdk
195 1.1 wdk if (boot_size > 8192-1024)
196 1.1 wdk FATAL("bootstrap program too large (%d bytes)", boot_size);
197 1.1 wdk
198 1.1 wdk boot_code = malloc(boot_size);
199 1.1 wdk if (boot_code == NULL)
200 1.1 wdk FATAL("malloc %d bytes failed", boot_size);
201 1.3 wdk memset(boot_code, 0, boot_size);
202 1.1 wdk
203 1.1 wdk /* read the file into the buffer */
204 1.1 wdk len = read(fd, boot_code, bootstrapsb.st_size);
205 1.1 wdk if (len == -1)
206 1.1 wdk FATALIO("read %s", bootstrap);
207 1.1 wdk else if (len != bootstrapsb.st_size)
208 1.1 wdk FATAL("read %s: short read", bootstrap);
209 1.1 wdk (void)close(fd);
210 1.1 wdk
211 1.1 wdk read_volheader(disk, &vh);
212 1.1 wdk
213 1.1 wdk vdp = voldir_findfile(&vh, bootname, 1);
214 1.1 wdk if (vdp == NULL)
215 1.1 wdk FATAL("%s: volume directory full", disk);
216 1.1 wdk
217 1.1 wdk strcpy(vdp->vd_name, bootname);
218 1.1 wdk vdp->vd_lba = BOOTBLOCK_NUMBER;
219 1.1 wdk vdp->vd_len = bootstrapsb.st_size;
220 1.1 wdk
221 1.1 wdk if (nowrite) {
222 1.1 wdk if (verbose)
223 1.1 wdk fprintf(stderr, "not writing\n");
224 1.1 wdk return;
225 1.1 wdk }
226 1.1 wdk
227 1.1 wdk if (verbose)
228 1.1 wdk fprintf(stderr, "writing bootstrap (%d bytes at logical block %d)\n",
229 1.1 wdk boot_size, 2);
230 1.1 wdk
231 1.1 wdk /* Write bootstrap */
232 1.1 wdk if ((fd = open(disk, O_WRONLY)) == -1)
233 1.1 wdk FATALIO("open %s", bootstrap);
234 1.1 wdk len = pwrite(fd, boot_code, boot_size, BOOTBLOCK_OFFSET);
235 1.1 wdk if (len == -1)
236 1.1 wdk FATAL("write %s", disk);
237 1.1 wdk if (len != boot_size)
238 1.1 wdk FATAL("write %s: short write", disk);
239 1.1 wdk (void) close(fd);
240 1.1 wdk
241 1.1 wdk /* Update volume header */
242 1.1 wdk write_volheader(disk, &vh);
243 1.1 wdk }
244 1.1 wdk
245 1.1 wdk static void
246 1.1 wdk read_volheader(disk, vhp)
247 1.1 wdk const char *disk;
248 1.1 wdk struct mips_volheader *vhp;
249 1.1 wdk {
250 1.1 wdk int vfd;
251 1.1 wdk ssize_t len;
252 1.1 wdk
253 1.1 wdk if ((vfd = open(disk, O_RDONLY)) == -1)
254 1.1 wdk FATALIO("open %s", disk);
255 1.1 wdk
256 1.1 wdk len = pread(vfd, vhp, sizeof(*vhp), MIPS_VHSECTOR*DEV_BSIZE);
257 1.1 wdk
258 1.1 wdk (void) close(vfd);
259 1.1 wdk
260 1.1 wdk if (len == -1)
261 1.1 wdk FATALIO("read %s", disk);
262 1.1 wdk if (len != sizeof(*vhp))
263 1.1 wdk FATAL("read %s: short read", disk);
264 1.1 wdk
265 1.1 wdk /* Check volume header magic */
266 1.1 wdk if (vhp->vh_magic != MIPS_VHMAGIC)
267 1.1 wdk FATAL("%s: no volume header", disk);
268 1.1 wdk
269 1.1 wdk /* check volume header checksum */
270 1.1 wdk if (mipsvh_cksum(vhp))
271 1.1 wdk FATAL("%s: volume header corrupted", disk);
272 1.1 wdk }
273 1.1 wdk
274 1.1 wdk static void
275 1.1 wdk write_volheader(disk, vhp)
276 1.1 wdk const char *disk;
277 1.1 wdk struct mips_volheader *vhp;
278 1.1 wdk {
279 1.1 wdk int vfd;
280 1.1 wdk ssize_t len;
281 1.1 wdk
282 1.1 wdk /* update volume header checksum */
283 1.1 wdk vhp->vh_cksum = 0;
284 1.1 wdk vhp->vh_cksum = -mipsvh_cksum(vhp);
285 1.1 wdk
286 1.1 wdk if ((vfd = open(disk, O_WRONLY)) == -1)
287 1.1 wdk FATALIO("open %s", disk);
288 1.1 wdk
289 1.1 wdk if (verbose)
290 1.1 wdk fprintf(stderr, "%s: writing volume header\n", disk);
291 1.1 wdk
292 1.1 wdk len = pwrite(vfd, vhp, sizeof(*vhp), MIPS_VHSECTOR*512); /* XXX */
293 1.1 wdk if (len == -1)
294 1.1 wdk FATALIO("write %s", disk);
295 1.1 wdk if (len != sizeof(*vhp))
296 1.1 wdk FATAL("write %s: short write", disk);
297 1.1 wdk
298 1.1 wdk (void) close(vfd);
299 1.1 wdk }
300 1.1 wdk
301 1.1 wdk /*
302 1.1 wdk * Compute checksum for MIPS disk volume header
303 1.1 wdk *
304 1.1 wdk * Mips volume header checksum is the 32bit 2's complement sum
305 1.1 wdk * of the entire volume header structure
306 1.1 wdk */
307 1.1 wdk int
308 1.1 wdk mipsvh_cksum(vhp)
309 1.1 wdk struct mips_volheader *vhp;
310 1.1 wdk {
311 1.1 wdk int i, *ptr;
312 1.1 wdk int cksum = 0;
313 1.1 wdk
314 1.1 wdk ptr = (int *)vhp;
315 1.1 wdk i = sizeof(*vhp) / sizeof(*ptr);
316 1.1 wdk while (i--)
317 1.1 wdk cksum += *ptr++;
318 1.1 wdk return cksum;
319 1.1 wdk }
320 1.1 wdk
321 1.1 wdk
322 1.1 wdk /*
323 1.1 wdk * Locate the volume directory slot that matches a filename
324 1.1 wdk *
325 1.1 wdk * If the file entry cannot be found and create is non-zero the next
326 1.1 wdk * empty slot is returned, otherwise return NULL
327 1.1 wdk */
328 1.1 wdk static struct mips_voldir *
329 1.1 wdk voldir_findfile(vhp, file, create)
330 1.1 wdk struct mips_volheader *vhp;
331 1.1 wdk const char *file;
332 1.1 wdk int create; /* return unused entry if not found */
333 1.1 wdk {
334 1.1 wdk struct mips_voldir *vdp = vhp->vh_voldir;
335 1.1 wdk int i;
336 1.1 wdk
337 1.1 wdk for (i=0; i<MIPS_NVOLDIR; i++, vdp++) {
338 1.1 wdk if (strcmp(vdp->vd_name, file) == 0)
339 1.1 wdk return vdp;
340 1.1 wdk }
341 1.1 wdk if (create) {
342 1.1 wdk vdp = vhp->vh_voldir;
343 1.1 wdk for (i=0; i<MIPS_NVOLDIR; i++, vdp++)
344 1.1 wdk if (vdp->vd_len == 0)
345 1.1 wdk return vdp;
346 1.1 wdk }
347 1.1 wdk return NULL;
348 1.1 wdk }
349