inst.c revision 1.4 1 1.4 thorpej /* $NetBSD: inst.c,v 1.4 1997/10/04 17:22:49 thorpej Exp $ */
2 1.1 thorpej
3 1.4 thorpej /*-
4 1.4 thorpej * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.4 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.4 thorpej * by Jason R. Thorpe.
9 1.4 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.1 thorpej * must display the following acknowledgement:
20 1.4 thorpej * This product includes software developed by the NetBSD
21 1.4 thorpej * Foundation, Inc. and its contributors.
22 1.4 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.4 thorpej * contributors may be used to endorse or promote products derived
24 1.4 thorpej * from this software without specific prior written permission.
25 1.1 thorpej *
26 1.4 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.4 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.4 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.4 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.4 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.4 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.4 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.4 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.4 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.4 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.4 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.4 thorpej */
38 1.4 thorpej
39 1.4 thorpej /*
40 1.1 thorpej * Portions of this program are inspired by (and have borrowed code from)
41 1.1 thorpej * the `editlabel' program that accompanies NetBSD/vax, which carries
42 1.1 thorpej * the following notice:
43 1.1 thorpej *
44 1.1 thorpej * Copyright (c) 1995 Ludd, University of Lule}, Sweden.
45 1.1 thorpej * All rights reserved.
46 1.1 thorpej *
47 1.1 thorpej * Redistribution and use in source and binary forms, with or without
48 1.1 thorpej * modification, are permitted provided that the following conditions
49 1.1 thorpej * are met:
50 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
51 1.1 thorpej * notice, this list of conditions and the following disclaimer.
52 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
53 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
54 1.1 thorpej * documentation and/or other materials provided with the distribution.
55 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
56 1.1 thorpej * must display the following acknowledgement:
57 1.1 thorpej * This product includes software developed at Ludd, University of
58 1.1 thorpej * Lule}, Sweden and its contributors.
59 1.1 thorpej * 4. The name of the author may not be used to endorse or promote products
60 1.1 thorpej * derived from this software without specific prior written permission
61 1.1 thorpej *
62 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
63 1.1 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
64 1.1 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
65 1.1 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
66 1.1 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
67 1.1 thorpej * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
68 1.1 thorpej * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
69 1.1 thorpej * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
70 1.1 thorpej * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 1.1 thorpej * SUCH DAMAGE.
73 1.1 thorpej */
74 1.1 thorpej
75 1.1 thorpej #define DKTYPENAMES
76 1.1 thorpej
77 1.1 thorpej #include <sys/param.h>
78 1.1 thorpej #include <sys/reboot.h>
79 1.1 thorpej #include <sys/disklabel.h>
80 1.1 thorpej #include <a.out.h>
81 1.1 thorpej
82 1.1 thorpej #include <lib/libsa/stand.h>
83 1.1 thorpej
84 1.1 thorpej #include <hp300/stand/common/samachdep.h>
85 1.1 thorpej
86 1.1 thorpej char line[100];
87 1.1 thorpej
88 1.1 thorpej extern u_int opendev;
89 1.1 thorpej extern char *lowram;
90 1.1 thorpej extern int noconsole;
91 1.1 thorpej extern int netio_ask;
92 1.1 thorpej
93 1.1 thorpej char *kernel_name = "/netbsd";
94 1.1 thorpej
95 1.1 thorpej void dsklabel __P((void));
96 1.1 thorpej void miniroot __P((void));
97 1.1 thorpej void bootmini __P((void));
98 1.1 thorpej void resetsys __P((void));
99 1.1 thorpej void gethelp __P((void));
100 1.1 thorpej int opendisk __P((char *, char *, int, char, int *));
101 1.1 thorpej void disklabel_edit __P((struct disklabel *));
102 1.1 thorpej void disklabel_show __P((struct disklabel *));
103 1.1 thorpej int disklabel_write __P((char *, int, struct open_file *));
104 1.1 thorpej int a2int __P((char *));
105 1.1 thorpej
106 1.1 thorpej struct inst_command {
107 1.1 thorpej char *ic_cmd; /* command name */
108 1.1 thorpej char *ic_desc; /* command description */
109 1.1 thorpej void (*ic_func) __P((void)); /* handling function */
110 1.1 thorpej } inst_commands[] = {
111 1.1 thorpej { "disklabel", "place partition map on disk", dsklabel },
112 1.1 thorpej { "miniroot", "place miniroot on disk", miniroot },
113 1.1 thorpej { "boot", "boot from miniroot", bootmini },
114 1.1 thorpej { "reset", "reset the system", resetsys },
115 1.1 thorpej { "help", "display command list", gethelp },
116 1.1 thorpej };
117 1.1 thorpej #define NCMDS (sizeof(inst_commands) / sizeof(inst_commands[0]))
118 1.1 thorpej
119 1.1 thorpej main()
120 1.1 thorpej {
121 1.1 thorpej int i, currname = 0;
122 1.1 thorpej
123 1.1 thorpej /*
124 1.1 thorpej * We want netopen() to ask for IP address, etc, rather
125 1.1 thorpej * that using bootparams.
126 1.1 thorpej */
127 1.1 thorpej netio_ask = 1;
128 1.1 thorpej
129 1.1 thorpej printf("\n");
130 1.1 thorpej printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
131 1.1 thorpej printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
132 1.3 thorpej printf(">> HP 9000/%s SPU\n", getmachineid());
133 1.1 thorpej gethelp();
134 1.1 thorpej
135 1.1 thorpej for (;;) {
136 1.1 thorpej printf("sys_inst> ");
137 1.1 thorpej bzero(line, sizeof(line));
138 1.1 thorpej gets(line);
139 1.1 thorpej if (line[0] == '\n' || line[0] == '\0')
140 1.1 thorpej continue;
141 1.1 thorpej
142 1.1 thorpej for (i = 0; i < NCMDS; ++i)
143 1.1 thorpej if (strcmp(line, inst_commands[i].ic_cmd) == 0) {
144 1.1 thorpej (*inst_commands[i].ic_func)();
145 1.1 thorpej break;
146 1.1 thorpej }
147 1.1 thorpej
148 1.1 thorpej
149 1.1 thorpej if (i == NCMDS)
150 1.1 thorpej printf("unknown command: %s\n", line);
151 1.1 thorpej }
152 1.1 thorpej }
153 1.1 thorpej
154 1.1 thorpej void
155 1.1 thorpej gethelp()
156 1.1 thorpej {
157 1.1 thorpej int i;
158 1.1 thorpej
159 1.1 thorpej printf(">> Available commands:\n");
160 1.1 thorpej for (i = 0; i < NCMDS; ++i)
161 1.1 thorpej printf(">> %s - %s\n", inst_commands[i].ic_cmd,
162 1.1 thorpej inst_commands[i].ic_desc);
163 1.1 thorpej }
164 1.1 thorpej
165 1.1 thorpej /*
166 1.1 thorpej * Do all the steps necessary to place a disklabel on a disk.
167 1.1 thorpej * Note, this assumes 512 byte sectors.
168 1.1 thorpej */
169 1.1 thorpej void
170 1.1 thorpej dsklabel()
171 1.1 thorpej {
172 1.1 thorpej struct disklabel *lp;
173 1.1 thorpej struct open_file *disk_ofp;
174 1.1 thorpej int dfd, error;
175 1.1 thorpej size_t xfersize;
176 1.1 thorpej char block[DEV_BSIZE], diskname[64];
177 1.1 thorpej extern struct open_file files[];
178 1.1 thorpej
179 1.1 thorpej printf("
180 1.1 thorpej You will be asked several questions about your disk, most of which
181 1.1 thorpej require prior knowledge of the disk's geometry. There is no easy way
182 1.1 thorpej for the system to provide this information for you. If you do not have
183 1.1 thorpej this information, please consult your disk's manual or another
184 1.1 thorpej informative source.\n\n");
185 1.1 thorpej
186 1.1 thorpej /* Error message printed by opendisk() */
187 1.1 thorpej if (opendisk("Disk to label?", diskname, sizeof(diskname),
188 1.1 thorpej ('a' + RAW_PART), &dfd))
189 1.1 thorpej return;
190 1.1 thorpej
191 1.1 thorpej disk_ofp = &files[dfd];
192 1.1 thorpej
193 1.1 thorpej bzero(block, sizeof(block));
194 1.1 thorpej if (error = (*disk_ofp->f_dev->dv_strategy)(disk_ofp->f_devdata,
195 1.1 thorpej F_READ, LABELSECTOR, sizeof(block), block, &xfersize)) {
196 1.1 thorpej printf("cannot read disk %s, errno = %d\n", diskname, error);
197 1.1 thorpej return;
198 1.1 thorpej }
199 1.1 thorpej
200 1.1 thorpej printf("Sucessfully read %d bytes from %s\n", xfersize, diskname);
201 1.1 thorpej
202 1.1 thorpej lp = (struct disklabel *)((void *)(&block[LABELOFFSET]));
203 1.1 thorpej
204 1.1 thorpej disklabel_loop:
205 1.1 thorpej bzero(line, sizeof(line));
206 1.1 thorpej printf("(z)ap, (e)dit, (s)how, (w)rite, (d)one > ");
207 1.1 thorpej gets(line);
208 1.1 thorpej if (line[0] == '\n' || line[0] == '\0')
209 1.1 thorpej goto disklabel_loop;
210 1.1 thorpej
211 1.1 thorpej switch (line[0]) {
212 1.1 thorpej case 'z':
213 1.1 thorpej case 'Z': {
214 1.1 thorpej char zap[DEV_BSIZE];
215 1.1 thorpej bzero(zap, sizeof(zap));
216 1.1 thorpej (void)(*disk_ofp->f_dev->dv_strategy)(disk_ofp->f_devdata,
217 1.1 thorpej F_WRITE, LABELSECTOR, sizeof(zap), zap, &xfersize);
218 1.1 thorpej }
219 1.1 thorpej goto out;
220 1.1 thorpej /* NOTREACHED */
221 1.1 thorpej
222 1.1 thorpej case 'e':
223 1.1 thorpej case 'E':
224 1.1 thorpej disklabel_edit(lp);
225 1.1 thorpej break;
226 1.1 thorpej
227 1.1 thorpej case 's':
228 1.1 thorpej case 'S':
229 1.1 thorpej disklabel_show(lp);
230 1.1 thorpej break;
231 1.1 thorpej
232 1.1 thorpej case 'w':
233 1.1 thorpej case 'W':
234 1.1 thorpej /*
235 1.1 thorpej * Error message will be displayed by disklabel_write()
236 1.1 thorpej */
237 1.1 thorpej if (disklabel_write(block, sizeof(block), disk_ofp))
238 1.1 thorpej goto out;
239 1.1 thorpej else
240 1.1 thorpej printf("Sucessfully wrote label to %s\n", diskname);
241 1.1 thorpej break;
242 1.1 thorpej
243 1.1 thorpej case 'd':
244 1.1 thorpej case 'D':
245 1.1 thorpej goto out;
246 1.1 thorpej /* NOTREACHED */
247 1.1 thorpej
248 1.1 thorpej default:
249 1.1 thorpej printf("unkown command: %s\n", line);
250 1.1 thorpej }
251 1.1 thorpej
252 1.1 thorpej goto disklabel_loop;
253 1.1 thorpej /* NOTREACHED */
254 1.1 thorpej
255 1.1 thorpej out:
256 1.1 thorpej /*
257 1.1 thorpej * Close disk. Marks disk `not alive' so that partition
258 1.1 thorpej * information will be reloaded upon next open.
259 1.1 thorpej */
260 1.1 thorpej (void)close(dfd);
261 1.1 thorpej }
262 1.1 thorpej
263 1.1 thorpej #define GETNUM(out, num) \
264 1.1 thorpej printf((out), (num)); \
265 1.1 thorpej bzero(line, sizeof(line)); \
266 1.1 thorpej gets(line); \
267 1.1 thorpej if (line[0]) \
268 1.1 thorpej (num) = atoi(line);
269 1.1 thorpej
270 1.1 thorpej #define GETNUM2(out, num1, num2) \
271 1.1 thorpej printf((out), (num1), (num2)); \
272 1.1 thorpej bzero(line, sizeof(line)); \
273 1.1 thorpej gets(line); \
274 1.1 thorpej if (line[0]) \
275 1.1 thorpej (num2) = atoi(line);
276 1.1 thorpej
277 1.1 thorpej #define GETSTR(out, str) \
278 1.1 thorpej printf((out), (str)); \
279 1.1 thorpej bzero(line, sizeof(line)); \
280 1.1 thorpej gets(line); \
281 1.1 thorpej if (line[0]) \
282 1.1 thorpej strcpy((str), line);
283 1.1 thorpej
284 1.1 thorpej #define FLAGS(out, flag) \
285 1.1 thorpej printf((out), lp->d_flags & (flag) ? 'y' : 'n'); \
286 1.1 thorpej bzero(line, sizeof(line)); \
287 1.1 thorpej gets(line); \
288 1.1 thorpej if (line[0] == 'y' || line[0] == 'Y') \
289 1.1 thorpej lp->d_flags |= (flag); \
290 1.1 thorpej else \
291 1.1 thorpej lp->d_flags &= ~(flag);
292 1.1 thorpej
293 1.1 thorpej void
294 1.1 thorpej disklabel_edit(lp)
295 1.1 thorpej struct disklabel *lp;
296 1.1 thorpej {
297 1.1 thorpej int i;
298 1.1 thorpej
299 1.1 thorpej printf("Select disk type. Valid types:\n");
300 1.1 thorpej for (i = 0; i < DKMAXTYPES; i++)
301 1.1 thorpej printf("%d %s\n", i, dktypenames[i]);
302 1.1 thorpej printf("\n");
303 1.1 thorpej
304 1.1 thorpej GETNUM("Disk type (number)? [%d] ", lp->d_type);
305 1.1 thorpej GETSTR("Disk model name? [%s] ", lp->d_typename);
306 1.1 thorpej GETSTR("Disk pack name? [%s] ", lp->d_packname);
307 1.1 thorpej FLAGS("Bad sectoring? [%c] ", D_BADSECT);
308 1.1 thorpej FLAGS("Ecc? [%c] ", D_ECC);
309 1.1 thorpej FLAGS("Removable? [%c] ", D_REMOVABLE);
310 1.1 thorpej
311 1.1 thorpej printf("\n");
312 1.1 thorpej
313 1.1 thorpej GETNUM("Interleave? [%d] ", lp->d_interleave);
314 1.1 thorpej GETNUM("Rpm? [%d] ", lp->d_rpm);
315 1.1 thorpej GETNUM("Trackskew? [%d] ", lp->d_trackskew);
316 1.1 thorpej GETNUM("Cylinderskew? [%d] ", lp->d_cylskew);
317 1.1 thorpej GETNUM("Headswitch? [%d] ", lp->d_headswitch);
318 1.1 thorpej GETNUM("Track-to-track? [%d] ", lp->d_trkseek);
319 1.1 thorpej GETNUM("Drivedata 0? [%d] ", lp->d_drivedata[0]);
320 1.1 thorpej GETNUM("Drivedata 1? [%d] ", lp->d_drivedata[1]);
321 1.1 thorpej GETNUM("Drivedata 2? [%d] ", lp->d_drivedata[2]);
322 1.1 thorpej GETNUM("Drivedata 3? [%d] ", lp->d_drivedata[3]);
323 1.1 thorpej GETNUM("Drivedata 4? [%d] ", lp->d_drivedata[4]);
324 1.1 thorpej
325 1.1 thorpej printf("\n");
326 1.1 thorpej
327 1.1 thorpej GETNUM("Bytes/sector? [%d] ", lp->d_secsize);
328 1.1 thorpej GETNUM("Sectors/track? [%d] ", lp->d_nsectors);
329 1.1 thorpej GETNUM("Tracks/cylinder? [%d] ", lp->d_ntracks);
330 1.1 thorpej GETNUM("Sectors/cylinder? [%d] ", lp->d_secpercyl);
331 1.1 thorpej GETNUM("Cylinders? [%d] ", lp->d_ncylinders);
332 1.1 thorpej
333 1.1 thorpej printf("
334 1.1 thorpej Enter partition table. Note, sizes and offsets are in sectors.\n\n");
335 1.1 thorpej
336 1.1 thorpej lp->d_npartitions = MAXPARTITIONS;
337 1.1 thorpej for (i = 0; i < lp->d_npartitions; ++i) {
338 1.1 thorpej GETNUM2("%c partition: offset? [%d] ", ('a' + i),
339 1.1 thorpej lp->d_partitions[i].p_offset);
340 1.1 thorpej GETNUM(" size? [%d] ", lp->d_partitions[i].p_size);
341 1.1 thorpej }
342 1.1 thorpej
343 1.1 thorpej /* Perform magic. */
344 1.1 thorpej lp->d_magic = lp->d_magic2 = DISKMAGIC;
345 1.1 thorpej
346 1.1 thorpej /* Calculate disklabel checksum. */
347 1.1 thorpej lp->d_checksum = 0;
348 1.1 thorpej lp->d_checksum = dkcksum(lp);
349 1.1 thorpej }
350 1.1 thorpej
351 1.1 thorpej void
352 1.1 thorpej disklabel_show(lp)
353 1.1 thorpej struct disklabel *lp;
354 1.1 thorpej {
355 1.1 thorpej int i, npart;
356 1.1 thorpej struct partition *pp;
357 1.1 thorpej
358 1.1 thorpej /*
359 1.1 thorpej * Check for valid disklabel.
360 1.1 thorpej */
361 1.1 thorpej if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC) {
362 1.1 thorpej printf("No disklabel to show.\n");
363 1.1 thorpej return;
364 1.1 thorpej }
365 1.1 thorpej
366 1.1 thorpej if (lp->d_npartitions > MAXPARTITIONS || dkcksum(lp) != 0) {
367 1.1 thorpej printf("Corrupted disklabel.\n");
368 1.1 thorpej return;
369 1.1 thorpej }
370 1.1 thorpej
371 1.1 thorpej printf("\ndisk type %d (%s), %s: %s%s%s\n", lp->d_type,
372 1.1 thorpej lp->d_type < DKMAXTYPES ? dktypenames[lp->d_type] :
373 1.1 thorpej dktypenames[0], lp->d_typename,
374 1.1 thorpej (lp->d_flags & D_REMOVABLE) ? " removable" : "",
375 1.1 thorpej (lp->d_flags & D_ECC) ? " ecc" : "",
376 1.1 thorpej (lp->d_flags & D_BADSECT) ? " badsect" : "");
377 1.1 thorpej
378 1.1 thorpej printf("interleave %d, rpm %d, trackskew %d, cylinderskew %d\n",
379 1.1 thorpej lp->d_interleave, lp->d_rpm, lp->d_trackskew, lp->d_cylskew);
380 1.1 thorpej
381 1.1 thorpej printf("headswitch %d, track-to-track %d, drivedata: %d %d %d %d %d\n",
382 1.1 thorpej lp->d_headswitch, lp->d_trkseek, lp->d_drivedata[0],
383 1.1 thorpej lp->d_drivedata[1], lp->d_drivedata[2], lp->d_drivedata[3],
384 1.1 thorpej lp->d_drivedata[4]);
385 1.1 thorpej
386 1.1 thorpej printf("\nbytes/sector: %d\n", lp->d_secsize);
387 1.1 thorpej printf("sectors/track: %d\n", lp->d_nsectors);
388 1.1 thorpej printf("tracks/cylinder: %d\n", lp->d_ntracks);
389 1.1 thorpej printf("sectors/cylinder: %d\n", lp->d_secpercyl);
390 1.1 thorpej printf("cylinders: %d\n", lp->d_ncylinders);
391 1.1 thorpej
392 1.1 thorpej printf("\n%d partitions:\n", lp->d_npartitions);
393 1.1 thorpej printf(" size offset\n");
394 1.1 thorpej pp = lp->d_partitions;
395 1.1 thorpej for (i = 0; i < lp->d_npartitions; i++) {
396 1.1 thorpej printf("%c: %d, %d\n", 97 + i, lp->d_partitions[i].p_size,
397 1.1 thorpej lp->d_partitions[i].p_offset);
398 1.1 thorpej }
399 1.1 thorpej printf("\n");
400 1.1 thorpej }
401 1.1 thorpej
402 1.1 thorpej int
403 1.1 thorpej disklabel_write(block, len, ofp)
404 1.1 thorpej char *block;
405 1.1 thorpej int len;
406 1.1 thorpej struct open_file *ofp;
407 1.1 thorpej {
408 1.1 thorpej int error = 0;
409 1.1 thorpej size_t xfersize;
410 1.1 thorpej
411 1.1 thorpej if (error = (*ofp->f_dev->dv_strategy)(ofp->f_devdata, F_WRITE,
412 1.1 thorpej LABELSECTOR, len, block, &xfersize))
413 1.1 thorpej printf("cannot write disklabel, errno = %d\n", error);
414 1.1 thorpej
415 1.1 thorpej return (error);
416 1.1 thorpej }
417 1.1 thorpej
418 1.1 thorpej int
419 1.1 thorpej opendisk(question, diskname, len, partition, fdp)
420 1.1 thorpej char *question, *diskname;
421 1.1 thorpej int len;
422 1.1 thorpej char partition;
423 1.1 thorpej int *fdp;
424 1.1 thorpej {
425 1.1 thorpej char fulldiskname[64], *filename;
426 1.1 thorpej int i, error = 0;
427 1.1 thorpej
428 1.1 thorpej getdiskname:
429 1.1 thorpej printf("%s ", question);
430 1.1 thorpej bzero(diskname, len);
431 1.1 thorpej bzero(fulldiskname, sizeof(fulldiskname));
432 1.1 thorpej gets(diskname);
433 1.1 thorpej if (diskname[0] == '\n' || diskname[0] == '\0')
434 1.1 thorpej goto getdiskname;
435 1.1 thorpej
436 1.1 thorpej /*
437 1.1 thorpej * devopen() is picky. Make sure it gets the sort of string it
438 1.1 thorpej * wants.
439 1.1 thorpej */
440 1.1 thorpej bcopy(diskname, fulldiskname,
441 1.1 thorpej len < sizeof(fulldiskname) ? len : sizeof(fulldiskname));
442 1.1 thorpej for (i = 0; fulldiskname[i + 1] != '\0'; ++i)
443 1.1 thorpej /* Nothing. */ ;
444 1.1 thorpej if (fulldiskname[i] < '0' || fulldiskname[i] > '9') {
445 1.1 thorpej printf("invalid disk name %s\n", diskname);
446 1.1 thorpej goto getdiskname;
447 1.1 thorpej }
448 1.1 thorpej fulldiskname[++i] = partition; fulldiskname[++i] = ':';
449 1.1 thorpej
450 1.1 thorpej /*
451 1.1 thorpej * We always open for writing.
452 1.1 thorpej */
453 1.1 thorpej if ((*fdp = open(fulldiskname, 1)) < 0) {
454 1.1 thorpej printf("cannot open %s\n", diskname);
455 1.1 thorpej return (1);
456 1.1 thorpej }
457 1.1 thorpej
458 1.1 thorpej return (0);
459 1.1 thorpej }
460 1.1 thorpej
461 1.1 thorpej /*
462 1.1 thorpej * Copy a miniroot image from an NFS server or tape to the `b' partition
463 1.1 thorpej * of the specified disk. Note, this assumes 512 byte sectors.
464 1.1 thorpej */
465 1.1 thorpej void
466 1.1 thorpej miniroot()
467 1.1 thorpej {
468 1.1 thorpej int sfd, dfd, i, nblks;
469 1.1 thorpej char diskname[64], minirootname[128];
470 1.1 thorpej char block[DEV_BSIZE];
471 1.1 thorpej char tapename[64];
472 1.2 thorpej int fileno, ignoreshread, eof, len;
473 1.1 thorpej struct stat st;
474 1.1 thorpej size_t xfersize;
475 1.1 thorpej struct open_file *disk_ofp;
476 1.1 thorpej extern struct open_file files[];
477 1.1 thorpej
478 1.1 thorpej /* Error message printed by opendisk() */
479 1.1 thorpej if (opendisk("Disk for miniroot?", diskname, sizeof(diskname),
480 1.1 thorpej 'b', &dfd))
481 1.1 thorpej return;
482 1.1 thorpej
483 1.1 thorpej disk_ofp = &files[dfd];
484 1.1 thorpej
485 1.1 thorpej getsource:
486 1.1 thorpej printf("Source? (N)FS, (t)ape, (d)one > ");
487 1.1 thorpej bzero(line, sizeof(line));
488 1.1 thorpej gets(line);
489 1.1 thorpej if (line[0] == '\0')
490 1.1 thorpej goto getsource;
491 1.1 thorpej
492 1.1 thorpej switch (line[0]) {
493 1.1 thorpej case 'n':
494 1.1 thorpej case 'N':
495 1.1 thorpej name_of_nfs_miniroot:
496 1.1 thorpej printf("Name of miniroot file? ");
497 1.1 thorpej bzero(line, sizeof(line));
498 1.1 thorpej bzero(minirootname, sizeof(minirootname));
499 1.1 thorpej gets(line);
500 1.1 thorpej if (line[0] == '\0')
501 1.1 thorpej goto name_of_nfs_miniroot;
502 1.1 thorpej (void)strcat(minirootname, "le0a:");
503 1.1 thorpej (void)strcat(minirootname, line);
504 1.1 thorpej if ((sfd = open(minirootname, 0)) < 0) {
505 1.1 thorpej printf("can't open %s\n", line);
506 1.1 thorpej return;
507 1.1 thorpej }
508 1.1 thorpej
509 1.1 thorpej /*
510 1.2 thorpej * Find out how big the miniroot is... we can't
511 1.2 thorpej * check for size because it may be compressed.
512 1.1 thorpej */
513 1.2 thorpej ignoreshread = 1;
514 1.1 thorpej if (fstat(sfd, &st) < 0) {
515 1.1 thorpej printf("can't stat %s\n", line);
516 1.1 thorpej goto done;
517 1.1 thorpej }
518 1.1 thorpej nblks = (int)(st.st_size / sizeof(block));
519 1.1 thorpej
520 1.2 thorpej printf("Copying miniroot from %s to %s...", line,
521 1.1 thorpej diskname);
522 1.1 thorpej break;
523 1.1 thorpej
524 1.1 thorpej case 't':
525 1.1 thorpej case 'T':
526 1.1 thorpej name_of_tape_miniroot:
527 1.1 thorpej printf("Which tape device? ");
528 1.1 thorpej bzero(line, sizeof(line));
529 1.1 thorpej bzero(minirootname, sizeof(minirootname));
530 1.1 thorpej bzero(tapename, sizeof(tapename));
531 1.1 thorpej gets(line);
532 1.1 thorpej if (line[0] == '\0')
533 1.1 thorpej goto name_of_tape_miniroot;
534 1.1 thorpej strcat(minirootname, line);
535 1.1 thorpej strcat(tapename, line);
536 1.1 thorpej
537 1.1 thorpej printf("File number (first == 1)? ");
538 1.1 thorpej bzero(line, sizeof(line));
539 1.1 thorpej gets(line);
540 1.1 thorpej fileno = a2int(line);
541 1.1 thorpej if (fileno < 1 || fileno > 8) {
542 1.1 thorpej printf("Invalid file number: %s\n", line);
543 1.1 thorpej goto getsource;
544 1.1 thorpej }
545 1.1 thorpej for (i = 0; i < sizeof(minirootname); ++i) {
546 1.1 thorpej if (minirootname[i] == '\0')
547 1.1 thorpej break;
548 1.1 thorpej }
549 1.1 thorpej if (i == sizeof(minirootname) ||
550 1.1 thorpej (sizeof(minirootname) - i) < 8) {
551 1.1 thorpej printf("Invalid device name: %s\n", tapename);
552 1.1 thorpej goto getsource;
553 1.1 thorpej }
554 1.1 thorpej minirootname[i++] = 'a' + (fileno - 1);
555 1.1 thorpej minirootname[i++] = ':';
556 1.1 thorpej strcat(minirootname, "XXX"); /* lameness in open() */
557 1.1 thorpej
558 1.2 thorpej ignoreshread = 0;
559 1.1 thorpej printf("Copy how many %d byte blocks? ", DEV_BSIZE);
560 1.1 thorpej bzero(line, sizeof(line));
561 1.1 thorpej gets(line);
562 1.1 thorpej nblks = a2int(line);
563 1.1 thorpej if (nblks < 0) {
564 1.1 thorpej printf("Invalid block count: %s\n", line);
565 1.1 thorpej goto getsource;
566 1.1 thorpej } else if (nblks == 0) {
567 1.1 thorpej printf("Zero blocks? Ok, aborting.\n");
568 1.1 thorpej return;
569 1.1 thorpej }
570 1.1 thorpej
571 1.1 thorpej if ((sfd = open(minirootname, 0)) < 0) {
572 1.1 thorpej printf("can't open %s file %c\n", tapename, fileno);
573 1.1 thorpej return;
574 1.1 thorpej }
575 1.1 thorpej
576 1.2 thorpej printf("Copying %s file %d to %s...", tapename, fileno,
577 1.1 thorpej diskname);
578 1.1 thorpej break;
579 1.1 thorpej
580 1.1 thorpej case 'd':
581 1.1 thorpej case 'D':
582 1.1 thorpej return;
583 1.1 thorpej
584 1.1 thorpej default:
585 1.1 thorpej printf("Unknown source: %s\n", line);
586 1.1 thorpej goto getsource;
587 1.1 thorpej }
588 1.1 thorpej
589 1.1 thorpej /*
590 1.1 thorpej * Copy loop...
591 1.1 thorpej * This is fairly slow... if someone wants to speed it
592 1.1 thorpej * up, they'll get no complaints from me.
593 1.1 thorpej */
594 1.2 thorpej for (i = 0, eof = 0; i < nblks || ignoreshread == 0; i++) {
595 1.2 thorpej if ((len = read(sfd, block, sizeof(block))) < 0) {
596 1.2 thorpej printf("Read error, errno = %d\n", errno);
597 1.2 thorpej goto out;
598 1.2 thorpej }
599 1.2 thorpej
600 1.2 thorpej /*
601 1.2 thorpej * Check for end-of-file.
602 1.2 thorpej */
603 1.2 thorpej if (len == 0)
604 1.1 thorpej goto done;
605 1.2 thorpej else if (len < sizeof(block))
606 1.2 thorpej eof = 1;
607 1.2 thorpej
608 1.1 thorpej if ((*disk_ofp->f_dev->dv_strategy)(disk_ofp->f_devdata,
609 1.2 thorpej F_WRITE, i, len, block, &xfersize) || xfersize != len) {
610 1.1 thorpej printf("Bad write at block %d, errno = %d\n",
611 1.1 thorpej i, errno);
612 1.2 thorpej goto out;
613 1.2 thorpej }
614 1.2 thorpej
615 1.2 thorpej if (eof)
616 1.1 thorpej goto done;
617 1.1 thorpej }
618 1.2 thorpej done:
619 1.1 thorpej printf("done\n");
620 1.1 thorpej
621 1.1 thorpej printf("Successfully copied miniroot image.\n");
622 1.1 thorpej
623 1.2 thorpej out:
624 1.1 thorpej close(sfd);
625 1.1 thorpej close(dfd);
626 1.1 thorpej }
627 1.1 thorpej
628 1.1 thorpej /*
629 1.1 thorpej * Boot the kernel from the miniroot image into single-user.
630 1.1 thorpej */
631 1.1 thorpej void
632 1.1 thorpej bootmini()
633 1.1 thorpej {
634 1.1 thorpej char diskname[64], bootname[64];
635 1.1 thorpej int i;
636 1.1 thorpej
637 1.1 thorpej getdiskname:
638 1.1 thorpej printf("Disk to boot from? ");
639 1.1 thorpej bzero(diskname, sizeof(diskname));
640 1.1 thorpej bzero(bootname, sizeof(bootname));
641 1.1 thorpej gets(diskname);
642 1.1 thorpej if (diskname[0] == '\n' || diskname[0] == '\0')
643 1.1 thorpej goto getdiskname;
644 1.1 thorpej
645 1.1 thorpej /*
646 1.1 thorpej * devopen() is picky. Make sure it gets the sort of string it
647 1.1 thorpej * wants.
648 1.1 thorpej */
649 1.1 thorpej (void)strcat(bootname, diskname);
650 1.1 thorpej for (i = 0; bootname[i + 1] != '\0'; ++i)
651 1.1 thorpej /* Nothing. */ ;
652 1.1 thorpej if (bootname[i] < '0' || bootname[i] > '9') {
653 1.1 thorpej printf("invalid disk name %s\n", diskname);
654 1.1 thorpej goto getdiskname;
655 1.1 thorpej }
656 1.1 thorpej bootname[++i] = 'b'; bootname[++i] = ':';
657 1.1 thorpej (void)strcat(bootname, kernel_name);
658 1.1 thorpej
659 1.1 thorpej howto = RB_SINGLE; /* _Always_ */
660 1.1 thorpej
661 1.1 thorpej printf("booting: %s -s\n", bootname);
662 1.1 thorpej exec(bootname, lowram, howto);
663 1.1 thorpej printf("boot: %s\n", strerror(errno));
664 1.1 thorpej }
665 1.1 thorpej
666 1.1 thorpej /*
667 1.1 thorpej * Reset the system.
668 1.1 thorpej */
669 1.1 thorpej void
670 1.1 thorpej resetsys()
671 1.1 thorpej {
672 1.1 thorpej
673 1.1 thorpej call_req_reboot();
674 1.1 thorpej printf("panic: can't reboot, halting\n");
675 1.1 thorpej asm("stop #0x2700");
676 1.1 thorpej }
677 1.1 thorpej
678 1.1 thorpej /*
679 1.1 thorpej * XXX Should have a generic atoi for libkern/libsa.
680 1.1 thorpej */
681 1.1 thorpej int
682 1.1 thorpej a2int(cp)
683 1.1 thorpej char *cp;
684 1.1 thorpej {
685 1.1 thorpej int i = 0;
686 1.1 thorpej
687 1.1 thorpej if (*cp == '\0')
688 1.1 thorpej return (-1);
689 1.1 thorpej
690 1.1 thorpej while (*cp != '\0')
691 1.1 thorpej i = i * 10 + *cp++ - '0';
692 1.1 thorpej return (i);
693 1.1 thorpej }
694