installboot.c revision 1.1 1 1.1 wdk /* $NetBSD: installboot.c,v 1.1 2000/09/26 10:00:19 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 extern char *__progname;
77 1.1 wdk
78 1.1 wdk fprintf(stderr, "usage:\n");
79 1.1 wdk fprintf(stderr, "\t%s [-nv] disk bootstrap [name]\n", __progname);
80 1.1 wdk fprintf(stderr, "\t%s -r [-nv] disk [name]\n", __progname);
81 1.1 wdk fprintf(stderr, "\t%s -l [-nv] disk\n", __progname);
82 1.1 wdk exit(EXIT_FAILURE);
83 1.1 wdk }
84 1.1 wdk
85 1.1 wdk int
86 1.1 wdk main(int argc, char *argv[])
87 1.1 wdk {
88 1.1 wdk const char *disk;
89 1.1 wdk int c, rflag, lflag;
90 1.1 wdk
91 1.1 wdk rflag = lflag = verbose = nowrite = 0;
92 1.1 wdk
93 1.1 wdk while ((c = getopt(argc, argv, "lnrv")) != -1) {
94 1.1 wdk switch (c) {
95 1.1 wdk case 'l':
96 1.1 wdk /* List volume directory contents */
97 1.1 wdk lflag = 1;
98 1.1 wdk break;
99 1.1 wdk case 'n':
100 1.1 wdk /* Disable write of boot sectors */
101 1.1 wdk nowrite = 1;
102 1.1 wdk break;
103 1.1 wdk case 'r':
104 1.1 wdk /* Clear any existing boot block */
105 1.1 wdk rflag = 1;
106 1.1 wdk break;
107 1.1 wdk case 'v':
108 1.1 wdk /* Verbose output */
109 1.1 wdk verbose = 1;
110 1.1 wdk break;
111 1.1 wdk default:
112 1.1 wdk usage();
113 1.1 wdk }
114 1.1 wdk }
115 1.1 wdk
116 1.1 wdk argc -= optind;
117 1.1 wdk argv += optind;
118 1.1 wdk
119 1.1 wdk if ((lflag && rflag) || argc < 1 || (lflag && argc != 1) ||
120 1.1 wdk (rflag && argc > 3) || argc > 4)
121 1.1 wdk usage();
122 1.1 wdk
123 1.1 wdk disk = argv[0];
124 1.1 wdk
125 1.1 wdk if (lflag)
126 1.1 wdk do_list(disk);
127 1.1 wdk else if (rflag)
128 1.1 wdk do_remove(disk, argc==2?argv[1]:DEFAULT_BOOTFILE);
129 1.1 wdk else
130 1.1 wdk do_install(disk, argv[1], argc==3?argv[2]:DEFAULT_BOOTFILE);
131 1.1 wdk
132 1.1 wdk exit(EXIT_SUCCESS);
133 1.1 wdk }
134 1.1 wdk
135 1.1 wdk static void
136 1.1 wdk do_list(disk)
137 1.1 wdk const char *disk;
138 1.1 wdk {
139 1.1 wdk struct mips_volheader vh;
140 1.1 wdk struct mips_voldir *vdp;
141 1.1 wdk int i;
142 1.1 wdk
143 1.1 wdk read_volheader(disk, &vh);
144 1.1 wdk
145 1.1 wdk printf("Slot\t LBN\tLength\tFilename\n");
146 1.1 wdk printf("------------------------------------------\n");
147 1.1 wdk for (i=0, vdp=vh.vh_voldir; i<MIPS_NVOLDIR; i++, vdp++)
148 1.1 wdk if (vdp->vd_len)
149 1.1 wdk printf("%2d:\t%5d\t%6d\t%s\n", i, vdp->vd_lba,
150 1.1 wdk vdp->vd_len, vdp->vd_name);
151 1.1 wdk }
152 1.1 wdk
153 1.1 wdk static void
154 1.1 wdk do_remove(disk, filename)
155 1.1 wdk const char *disk;
156 1.1 wdk const char *filename;
157 1.1 wdk {
158 1.1 wdk struct mips_volheader vh;
159 1.1 wdk struct mips_voldir *vdp;
160 1.1 wdk
161 1.1 wdk read_volheader(disk, &vh);
162 1.1 wdk vdp = voldir_findfile(&vh, filename, 0);
163 1.1 wdk if (vdp == NULL)
164 1.1 wdk FATAL("%s: file not found", disk);
165 1.1 wdk
166 1.1 wdk bzero(vdp, sizeof(*vdp));
167 1.1 wdk
168 1.1 wdk /* Update volume header */
169 1.1 wdk write_volheader(disk, &vh);
170 1.1 wdk }
171 1.1 wdk
172 1.1 wdk static void
173 1.1 wdk do_install(disk, bootstrap, bootname)
174 1.1 wdk const char *disk;
175 1.1 wdk const char *bootstrap;
176 1.1 wdk const char *bootname;
177 1.1 wdk {
178 1.1 wdk struct stat bootstrapsb;
179 1.1 wdk struct mips_volheader vh;
180 1.1 wdk struct mips_voldir *vdp;
181 1.1 wdk int fd;
182 1.1 wdk char *boot_code;
183 1.1 wdk size_t boot_size;
184 1.1 wdk ssize_t len;
185 1.1 wdk
186 1.1 wdk /* Open the input file and check it out */
187 1.1 wdk if ((fd = open(bootstrap, O_RDONLY)) == -1)
188 1.1 wdk FATALIO("open %s", bootstrap);
189 1.1 wdk if (fstat(fd, &bootstrapsb) == -1)
190 1.1 wdk FATALIO("fstat %s", bootstrap);
191 1.1 wdk if (!S_ISREG(bootstrapsb.st_mode))
192 1.1 wdk FATAL("%s must be a regular file", bootstrap);
193 1.1 wdk
194 1.1 wdk boot_size = roundup(bootstrapsb.st_size, DEV_BSIZE);
195 1.1 wdk
196 1.1 wdk if (boot_size > 8192-1024)
197 1.1 wdk FATAL("bootstrap program too large (%d bytes)", boot_size);
198 1.1 wdk
199 1.1 wdk boot_code = malloc(boot_size);
200 1.1 wdk if (boot_code == NULL)
201 1.1 wdk FATAL("malloc %d bytes failed", boot_size);
202 1.1 wdk bzero(boot_code, boot_size);
203 1.1 wdk
204 1.1 wdk /* read the file into the buffer */
205 1.1 wdk len = read(fd, boot_code, bootstrapsb.st_size);
206 1.1 wdk if (len == -1)
207 1.1 wdk FATALIO("read %s", bootstrap);
208 1.1 wdk else if (len != bootstrapsb.st_size)
209 1.1 wdk FATAL("read %s: short read", bootstrap);
210 1.1 wdk (void)close(fd);
211 1.1 wdk
212 1.1 wdk read_volheader(disk, &vh);
213 1.1 wdk
214 1.1 wdk vdp = voldir_findfile(&vh, bootname, 1);
215 1.1 wdk if (vdp == NULL)
216 1.1 wdk FATAL("%s: volume directory full", disk);
217 1.1 wdk
218 1.1 wdk strcpy(vdp->vd_name, bootname);
219 1.1 wdk vdp->vd_lba = BOOTBLOCK_NUMBER;
220 1.1 wdk vdp->vd_len = bootstrapsb.st_size;
221 1.1 wdk
222 1.1 wdk if (nowrite) {
223 1.1 wdk if (verbose)
224 1.1 wdk fprintf(stderr, "not writing\n");
225 1.1 wdk return;
226 1.1 wdk }
227 1.1 wdk
228 1.1 wdk if (verbose)
229 1.1 wdk fprintf(stderr, "writing bootstrap (%d bytes at logical block %d)\n",
230 1.1 wdk boot_size, 2);
231 1.1 wdk
232 1.1 wdk /* Write bootstrap */
233 1.1 wdk if ((fd = open(disk, O_WRONLY)) == -1)
234 1.1 wdk FATALIO("open %s", bootstrap);
235 1.1 wdk len = pwrite(fd, boot_code, boot_size, BOOTBLOCK_OFFSET);
236 1.1 wdk if (len == -1)
237 1.1 wdk FATAL("write %s", disk);
238 1.1 wdk if (len != boot_size)
239 1.1 wdk FATAL("write %s: short write", disk);
240 1.1 wdk (void) close(fd);
241 1.1 wdk
242 1.1 wdk /* Update volume header */
243 1.1 wdk write_volheader(disk, &vh);
244 1.1 wdk }
245 1.1 wdk
246 1.1 wdk static void
247 1.1 wdk read_volheader(disk, vhp)
248 1.1 wdk const char *disk;
249 1.1 wdk struct mips_volheader *vhp;
250 1.1 wdk {
251 1.1 wdk int vfd;
252 1.1 wdk ssize_t len;
253 1.1 wdk
254 1.1 wdk if ((vfd = open(disk, O_RDONLY)) == -1)
255 1.1 wdk FATALIO("open %s", disk);
256 1.1 wdk
257 1.1 wdk len = pread(vfd, vhp, sizeof(*vhp), MIPS_VHSECTOR*DEV_BSIZE);
258 1.1 wdk
259 1.1 wdk (void) close(vfd);
260 1.1 wdk
261 1.1 wdk if (len == -1)
262 1.1 wdk FATALIO("read %s", disk);
263 1.1 wdk if (len != sizeof(*vhp))
264 1.1 wdk FATAL("read %s: short read", disk);
265 1.1 wdk
266 1.1 wdk /* Check volume header magic */
267 1.1 wdk if (vhp->vh_magic != MIPS_VHMAGIC)
268 1.1 wdk FATAL("%s: no volume header", disk);
269 1.1 wdk
270 1.1 wdk /* check volume header checksum */
271 1.1 wdk if (mipsvh_cksum(vhp))
272 1.1 wdk FATAL("%s: volume header corrupted", disk);
273 1.1 wdk }
274 1.1 wdk
275 1.1 wdk static void
276 1.1 wdk write_volheader(disk, vhp)
277 1.1 wdk const char *disk;
278 1.1 wdk struct mips_volheader *vhp;
279 1.1 wdk {
280 1.1 wdk int vfd;
281 1.1 wdk ssize_t len;
282 1.1 wdk
283 1.1 wdk /* update volume header checksum */
284 1.1 wdk vhp->vh_cksum = 0;
285 1.1 wdk vhp->vh_cksum = -mipsvh_cksum(vhp);
286 1.1 wdk
287 1.1 wdk if ((vfd = open(disk, O_WRONLY)) == -1)
288 1.1 wdk FATALIO("open %s", disk);
289 1.1 wdk
290 1.1 wdk if (verbose)
291 1.1 wdk fprintf(stderr, "%s: writing volume header\n", disk);
292 1.1 wdk
293 1.1 wdk len = pwrite(vfd, vhp, sizeof(*vhp), MIPS_VHSECTOR*512); /* XXX */
294 1.1 wdk if (len == -1)
295 1.1 wdk FATALIO("write %s", disk);
296 1.1 wdk if (len != sizeof(*vhp))
297 1.1 wdk FATAL("write %s: short write", disk);
298 1.1 wdk
299 1.1 wdk (void) close(vfd);
300 1.1 wdk }
301 1.1 wdk
302 1.1 wdk /*
303 1.1 wdk * Compute checksum for MIPS disk volume header
304 1.1 wdk *
305 1.1 wdk * Mips volume header checksum is the 32bit 2's complement sum
306 1.1 wdk * of the entire volume header structure
307 1.1 wdk */
308 1.1 wdk int
309 1.1 wdk mipsvh_cksum(vhp)
310 1.1 wdk struct mips_volheader *vhp;
311 1.1 wdk {
312 1.1 wdk int i, *ptr;
313 1.1 wdk int cksum = 0;
314 1.1 wdk
315 1.1 wdk ptr = (int *)vhp;
316 1.1 wdk i = sizeof(*vhp) / sizeof(*ptr);
317 1.1 wdk while (i--)
318 1.1 wdk cksum += *ptr++;
319 1.1 wdk return cksum;
320 1.1 wdk }
321 1.1 wdk
322 1.1 wdk
323 1.1 wdk /*
324 1.1 wdk * Locate the volume directory slot that matches a filename
325 1.1 wdk *
326 1.1 wdk * If the file entry cannot be found and create is non-zero the next
327 1.1 wdk * empty slot is returned, otherwise return NULL
328 1.1 wdk */
329 1.1 wdk static struct mips_voldir *
330 1.1 wdk voldir_findfile(vhp, file, create)
331 1.1 wdk struct mips_volheader *vhp;
332 1.1 wdk const char *file;
333 1.1 wdk int create; /* return unused entry if not found */
334 1.1 wdk {
335 1.1 wdk struct mips_voldir *vdp = vhp->vh_voldir;
336 1.1 wdk int i;
337 1.1 wdk
338 1.1 wdk for (i=0; i<MIPS_NVOLDIR; i++, vdp++) {
339 1.1 wdk if (strcmp(vdp->vd_name, file) == 0)
340 1.1 wdk return vdp;
341 1.1 wdk }
342 1.1 wdk if (create) {
343 1.1 wdk vdp = vhp->vh_voldir;
344 1.1 wdk for (i=0; i<MIPS_NVOLDIR; i++, vdp++)
345 1.1 wdk if (vdp->vd_len == 0)
346 1.1 wdk return vdp;
347 1.1 wdk }
348 1.1 wdk return NULL;
349 1.1 wdk }
350