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