fdisk.c revision 1.38 1 1.38 soren /* $NetBSD: fdisk.c,v 1.38 1999/09/06 23:58:59 soren Exp $ */
2 1.10 cgd
3 1.4 mycroft /*
4 1.1 cgd * Mach Operating System
5 1.1 cgd * Copyright (c) 1992 Carnegie Mellon University
6 1.1 cgd * All Rights Reserved.
7 1.4 mycroft *
8 1.1 cgd * Permission to use, copy, modify and distribute this software and its
9 1.1 cgd * documentation is hereby granted, provided that both the copyright
10 1.1 cgd * notice and this permission notice appear in all copies of the
11 1.1 cgd * software, derivative works or modified versions, and any portions
12 1.1 cgd * thereof, and that both notices appear in supporting documentation.
13 1.4 mycroft *
14 1.1 cgd * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 1.1 cgd * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 1.1 cgd * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 1.4 mycroft *
18 1.1 cgd * Carnegie Mellon requests users of this software to return to
19 1.4 mycroft *
20 1.1 cgd * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 1.1 cgd * School of Computer Science
22 1.1 cgd * Carnegie Mellon University
23 1.1 cgd * Pittsburgh PA 15213-3890
24 1.4 mycroft *
25 1.1 cgd * any improvements or extensions that they make and grant Carnegie Mellon
26 1.1 cgd * the rights to redistribute these changes.
27 1.1 cgd */
28 1.1 cgd
29 1.16 phil #include <sys/cdefs.h>
30 1.16 phil
31 1.2 mycroft #ifndef lint
32 1.38 soren __RCSID("$NetBSD: fdisk.c,v 1.38 1999/09/06 23:58:59 soren Exp $");
33 1.2 mycroft #endif /* not lint */
34 1.2 mycroft
35 1.1 cgd #include <sys/types.h>
36 1.1 cgd #include <sys/disklabel.h>
37 1.32 thorpej #include <sys/disklabel_mbr.h>
38 1.4 mycroft #include <sys/ioctl.h>
39 1.18 lukem #include <sys/param.h>
40 1.1 cgd #include <sys/stat.h>
41 1.4 mycroft
42 1.8 cgd #include <ctype.h>
43 1.4 mycroft #include <err.h>
44 1.18 lukem #include <errno.h>
45 1.1 cgd #include <fcntl.h>
46 1.18 lukem #include <paths.h>
47 1.4 mycroft #include <stdio.h>
48 1.4 mycroft #include <stdlib.h>
49 1.8 cgd #include <string.h>
50 1.8 cgd #include <unistd.h>
51 1.19 lukem #include <util.h>
52 1.1 cgd
53 1.34 fvdl #ifdef __i386__
54 1.34 fvdl #include <ctype.h>
55 1.34 fvdl #include <machine/cpu.h>
56 1.34 fvdl #include <sys/sysctl.h>
57 1.34 fvdl #endif
58 1.34 fvdl
59 1.1 cgd #define LBUF 100
60 1.1 cgd static char lbuf[LBUF];
61 1.1 cgd
62 1.1 cgd /*
63 1.1 cgd * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
64 1.1 cgd * Copyright (c) 1989 Robert. V. Baron
65 1.1 cgd * Created.
66 1.1 cgd */
67 1.1 cgd
68 1.1 cgd char *disk = "/dev/rwd0d";
69 1.1 cgd
70 1.1 cgd struct disklabel disklabel; /* disk parameters */
71 1.1 cgd
72 1.5 mycroft int cylinders, sectors, heads, cylindersectors, disksectors;
73 1.1 cgd
74 1.4 mycroft struct mboot {
75 1.32 thorpej u_int8_t padding[2]; /* force the longs to be long alligned */
76 1.32 thorpej u_int8_t bootinst[MBR_PARTOFF];
77 1.32 thorpej struct mbr_partition parts[NMBRPART];
78 1.32 thorpej u_int16_t signature;
79 1.1 cgd };
80 1.1 cgd struct mboot mboot;
81 1.1 cgd
82 1.30 ws #ifdef __i386__
83 1.34 fvdl
84 1.38 soren #define PARTNAMESIZE 8 /* From mbr_bootsel.S */
85 1.38 soren
86 1.34 fvdl struct mbr_bootsel {
87 1.34 fvdl u_int8_t defkey;
88 1.34 fvdl u_int8_t flags;
89 1.34 fvdl u_int16_t timeo;
90 1.38 soren char nametab[4][PARTNAMESIZE + 1];
91 1.34 fvdl u_int16_t magic;
92 1.34 fvdl } __attribute__((packed));
93 1.34 fvdl
94 1.34 fvdl #define BFL_SELACTIVE 0x01
95 1.35 fvdl #define BFL_EXTINT13 0x02
96 1.34 fvdl
97 1.34 fvdl #define SCAN_ENTER 0x1c
98 1.34 fvdl #define SCAN_F1 0x3b
99 1.34 fvdl
100 1.34 fvdl #define MBR_BOOTSELOFF (MBR_PARTOFF - sizeof (struct mbr_bootsel))
101 1.34 fvdl
102 1.30 ws #define DEFAULT_BOOTCODE "/usr/mdec/mbr"
103 1.34 fvdl #define DEFAULT_BOOTSELCODE "/usr/mdec/mbr_bootsel"
104 1.34 fvdl #define OPTIONS "0123BSafius:b:c:"
105 1.34 fvdl #else
106 1.34 fvdl #define OPTIONS "0123Safius:b:c:"
107 1.30 ws #endif
108 1.30 ws
109 1.1 cgd #define ACTIVE 0x80
110 1.1 cgd
111 1.5 mycroft int dos_cylinders;
112 1.1 cgd int dos_heads;
113 1.1 cgd int dos_sectors;
114 1.5 mycroft int dos_cylindersectors;
115 1.1 cgd
116 1.4 mycroft #define DOSSECT(s,c) (((s) & 0x3f) | (((c) >> 2) & 0xc0))
117 1.4 mycroft #define DOSCYL(c) ((c) & 0xff)
118 1.28 ws
119 1.28 ws #define MAXCYL 1024
120 1.4 mycroft int partition = -1;
121 1.4 mycroft
122 1.4 mycroft int a_flag; /* set active partition */
123 1.28 ws int i_flag; /* init bootcode */
124 1.4 mycroft int u_flag; /* update partition data */
125 1.15 phil int sh_flag; /* Output data as shell defines */
126 1.15 phil int f_flag; /* force --not interactive */
127 1.15 phil int s_flag; /* set id,offset,size */
128 1.16 phil int b_flag; /* Set cyl, heads, secs (as c/h/s) */
129 1.34 fvdl int B_flag; /* Edit/install bootselect code */
130 1.16 phil int b_cyl, b_head, b_sec; /* b_flag values. */
131 1.34 fvdl int bootsel_modified;
132 1.4 mycroft
133 1.30 ws unsigned char bootcode[8192]; /* maximum size of bootcode */
134 1.34 fvdl unsigned char tempcode[8192];
135 1.30 ws int bootsize; /* actual size of bootcode */
136 1.30 ws
137 1.1 cgd
138 1.25 drochner static char reserved[] = "reserved";
140 1.4 mycroft
141 1.4 mycroft struct part_type {
142 1.4 mycroft int type;
143 1.4 mycroft char *name;
144 1.4 mycroft } part_types[] = {
145 1.4 mycroft {0x00, "unused"},
146 1.4 mycroft {0x01, "Primary DOS with 12 bit FAT"},
147 1.4 mycroft {0x02, "XENIX / filesystem"},
148 1.13 perry {0x03, "XENIX /usr filesystem"},
149 1.25 drochner {0x04, "Primary DOS with 16 bit FAT <32M"},
150 1.13 perry {0x05, "Extended partition"},
151 1.13 perry {0x06, "Primary 'big' DOS, 16-bit FAT (> 32MB)"},
152 1.25 drochner {0x07, "OS/2 HPFS or NTFS or QNX2 or Advanced UNIX"},
153 1.25 drochner {0x08, "AIX filesystem or OS/2 (thru v1.3) or DELL multiple drives"
154 1.4 mycroft "or Commodore DOS or SplitDrive"},
155 1.13 perry {0x09, "AIX boot partition or Coherent"},
156 1.25 drochner {0x0A, "OS/2 Boot Manager or Coherent swap or OPUS"},
157 1.25 drochner {0x0b, "Primary DOS with 32 bit FAT"},
158 1.25 drochner {0x0c, "Primary DOS with 32 bit FAT - LBA"},
159 1.25 drochner {0x0d, "Type 7??? - LBA"},
160 1.25 drochner {0x0E, "DOS (16-bit FAT) - LBA"},
161 1.4 mycroft {0x0F, "Ext. partition - LBA"},
162 1.13 perry {0x10, "OPUS"},
163 1.13 perry {0x11, "OS/2 BM: hidden DOS 12-bit FAT"},
164 1.25 drochner {0x12, "Compaq diagnostics"},
165 1.13 perry {0x14, "OS/2 BM: hidden DOS 16-bit FAT <32M or Novell DOS 7.0 bug"},
166 1.13 perry {0x16, "OS/2 BM: hidden DOS 16-bit FAT >=32M"},
167 1.13 perry {0x17, "OS/2 BM: hidden IFS"},
168 1.25 drochner {0x18, "AST Windows swapfile"},
169 1.25 drochner {0x19, "Willowtech Photon coS"},
170 1.25 drochner {0x1e, "hidden FAT95"},
171 1.25 drochner {0x20, "Willowsoft OFS1"},
172 1.25 drochner {0x21, reserved},
173 1.13 perry {0x23, reserved},
174 1.25 drochner {0x24, "NEC DOS"},
175 1.25 drochner {0x26, reserved},
176 1.25 drochner {0x31, reserved},
177 1.25 drochner {0x33, reserved},
178 1.25 drochner {0x34, reserved},
179 1.25 drochner {0x36, reserved},
180 1.13 perry {0x38, "Theos"},
181 1.25 drochner {0x3C, "PartitionMagic recovery"},
182 1.25 drochner {0x40, "VENIX 286 or LynxOS"},
183 1.13 perry {0x41, "Linux/MINIX (sharing disk with DRDOS) or Personal RISC boot"},
184 1.13 perry {0x42, "SFS or Linux swap (sharing disk with DRDOS)"},
185 1.13 perry {0x43, "Linux native (sharing disk with DRDOS)"},
186 1.13 perry {0x50, "DM (disk manager)"},
187 1.4 mycroft {0x51, "DM6 Aux1 (or Novell)"},
188 1.13 perry {0x52, "CP/M or Microport SysV/AT"},
189 1.25 drochner {0x53, "DM6 Aux3"},
190 1.13 perry {0x54, "DM6 DDO"},
191 1.13 perry {0x55, "EZ-Drive (disk manager)"},
192 1.13 perry {0x56, "Golden Bow (disk manager)"},
193 1.13 perry {0x5C, "Priam Edisk (disk manager)"},
194 1.25 drochner {0x61, "SpeedStor"},
195 1.25 drochner {0x63, "GNU HURD or Mach or Sys V/386 (such as ISC UNIX) or MtXinu"},
196 1.4 mycroft {0x64, "Novell Netware 2.xx or Speedstore"},
197 1.25 drochner {0x65, "Novell Netware 3.xx"},
198 1.25 drochner {0x66, "Novell 386 Netware"},
199 1.25 drochner {0x67, "Novell"},
200 1.25 drochner {0x68, "Novell"},
201 1.13 perry {0x69, "Novell"},
202 1.25 drochner {0x70, "DiskSecure Multi-Boot"},
203 1.25 drochner {0x71, reserved},
204 1.25 drochner {0x73, reserved},
205 1.13 perry {0x74, reserved},
206 1.25 drochner {0x75, "PC/IX"},
207 1.13 perry {0x76, reserved},
208 1.13 perry {0x77, "QNX4.x"},
209 1.13 perry {0x78, "QNX4.x 2nd part"},
210 1.13 perry {0x79, "QNX4.x 3rd part"},
211 1.13 perry {0x80, "MINIX until 1.4a"},
212 1.25 drochner {0x81, "MINIX since 1.4b, early Linux, Mitac dmgr"},
213 1.13 perry {0x82, "Linux swap or Prime or Solaris"},
214 1.13 perry {0x83, "Linux native"},
215 1.13 perry {0x84, "OS/2 hidden C: drive"},
216 1.25 drochner {0x85, "Linux extended"},
217 1.25 drochner {0x86, "NT FAT volume set"},
218 1.4 mycroft {0x87, "NTFS volume set or HPFS mirrored"},
219 1.4 mycroft {0x93, "Amoeba filesystem"},
220 1.25 drochner {0x94, "Amoeba bad block table"},
221 1.25 drochner {0x99, "Mylex EISA SCSI"},
222 1.13 perry {0x9f, "BSDI?"},
223 1.25 drochner {0xA0, "IBM Thinkpad hibernation"},
224 1.25 drochner {0xa1, reserved},
225 1.25 drochner {0xa3, reserved},
226 1.25 drochner {0xa4, reserved},
227 1.13 perry {0xA5, "FreeBSD or 386BSD or old NetBSD"},
228 1.13 perry {0xA6, "OpenBSD"},
229 1.25 drochner {0xA7, "NeXTSTEP 486"},
230 1.25 drochner {0xa9, "NetBSD"},
231 1.25 drochner {0xb1, reserved},
232 1.25 drochner {0xb3, reserved},
233 1.25 drochner {0xb4, reserved},
234 1.4 mycroft {0xb6, reserved},
235 1.4 mycroft {0xB7, "BSDI BSD/386 filesystem"},
236 1.25 drochner {0xB8, "BSDI BSD/386 swap"},
237 1.13 perry {0xc0, "CTOS"},
238 1.13 perry {0xC1, "DRDOS/sec (FAT-12)"},
239 1.13 perry {0xC4, "DRDOS/sec (FAT-16, < 32M)"},
240 1.25 drochner {0xC6, "DRDOS/sec (FAT-16, >= 32M)"},
241 1.25 drochner {0xC7, "Syrinx (Cyrnix?) or HPFS disabled"},
242 1.13 perry {0xd8, "CP/M 86"},
243 1.13 perry {0xDB, "CP/M or Concurrent CP/M or Concurrent DOS or CTOS"},
244 1.25 drochner {0xE1, "DOS access or SpeedStor 12-bit FAT extended partition"},
245 1.13 perry {0xE3, "DOS R/O or SpeedStor or Storage Dimensions"},
246 1.25 drochner {0xE4, "SpeedStor 16-bit FAT extended partition < 1024 cyl."},
247 1.25 drochner {0xe5, reserved},
248 1.33 perry {0xe6, reserved},
249 1.25 drochner {0xeb, "BeOS"},
250 1.4 mycroft {0xF1, "SpeedStor or Storage Dimensions"},
251 1.25 drochner {0xF2, "DOS 3.3+ Secondary"},
252 1.25 drochner {0xf3, reserved},
253 1.25 drochner {0xF4, "SpeedStor large partition or Storage Dimensions"},
254 1.25 drochner {0xf6, reserved},
255 1.13 perry {0xFE, "SpeedStor >1024 cyl. or LANstep or IBM PS/2 IML"},
256 1.1 cgd {0xFF, "Xenix Bad Block Table"},
257 1.1 cgd };
258 1.4 mycroft
259 1.4 mycroft void usage __P((void));
260 1.4 mycroft void print_s0 __P((int));
261 1.34 fvdl void print_part __P((int));
262 1.28 ws int read_boot __P((char *, char *, size_t));
263 1.11 ghudson void init_sector0 __P((int, int));
264 1.34 fvdl void intuit_translated_geometry __P((void));
265 1.34 fvdl void get_geometry __P((void));
266 1.11 ghudson void get_diskname __P((char *, char *, size_t));
267 1.11 ghudson int try_heads __P((quad_t, quad_t, quad_t, quad_t, quad_t, quad_t, quad_t,
268 1.11 ghudson quad_t));
269 1.15 phil int try_sectors __P((quad_t, quad_t, quad_t, quad_t, quad_t));
270 1.4 mycroft void change_part __P((int, int, int, int));
271 1.4 mycroft void print_params __P((void));
272 1.4 mycroft void change_active __P((int));
273 1.4 mycroft void get_params_to_use __P((void));
274 1.4 mycroft void dos __P((int, unsigned char *, unsigned char *, unsigned char *));
275 1.4 mycroft int open_disk __P((int));
276 1.4 mycroft int read_disk __P((int, void *));
277 1.4 mycroft int write_disk __P((int, void *));
278 1.4 mycroft int get_params __P((void));
279 1.4 mycroft int read_s0 __P((void));
280 1.4 mycroft int write_s0 __P((void));
281 1.5 mycroft int yesno __P((char *));
282 1.4 mycroft void decimal __P((char *, int *));
283 1.4 mycroft int type_match __P((const void *, const void *));
284 1.16 phil char *get_type __P((int));
285 1.34 fvdl int get_mapping __P((int, int *, int *, int *, long *));
286 1.34 fvdl #ifdef __i386__
287 1.34 fvdl void configure_bootsel __P((void));
288 1.16 phil #endif
289 1.16 phil
290 1.16 phil static inline unsigned short getshort __P((void *));
291 1.16 phil static inline void putshort __P((void *p, unsigned short));
292 1.16 phil static inline unsigned long getlong __P((void *));
293 1.16 phil static inline void putlong __P((void *, unsigned long));
294 1.16 phil
295 1.16 phil
296 1.1 cgd int main __P((int, char **));
297 1.4 mycroft
298 1.1 cgd int
299 1.4 mycroft main(argc, argv)
300 1.4 mycroft int argc;
301 1.1 cgd char *argv[];
302 1.4 mycroft {
303 1.4 mycroft int ch;
304 1.1 cgd int part;
305 1.15 phil
306 1.15 phil int csysid, cstart, csize; /* For the b_flag. */
307 1.16 phil
308 1.15 phil a_flag = i_flag = u_flag = sh_flag = f_flag = s_flag = b_flag = 0;
309 1.34 fvdl csysid = cstart = csize = 0;
310 1.4 mycroft while ((ch = getopt(argc, argv, OPTIONS)) != -1)
311 1.4 mycroft switch (ch) {
312 1.4 mycroft case '0':
313 1.4 mycroft partition = 0;
314 1.4 mycroft break;
315 1.4 mycroft case '1':
316 1.4 mycroft partition = 1;
317 1.4 mycroft break;
318 1.4 mycroft case '2':
319 1.4 mycroft partition = 2;
320 1.4 mycroft break;
321 1.4 mycroft case '3':
322 1.4 mycroft partition = 3;
323 1.34 fvdl break;
324 1.34 fvdl #ifdef __i386__
325 1.34 fvdl case 'B':
326 1.34 fvdl B_flag = 1;
327 1.34 fvdl break;
328 1.15 phil #endif
329 1.15 phil case 'S':
330 1.15 phil sh_flag = 1;
331 1.4 mycroft break;
332 1.4 mycroft case 'a':
333 1.4 mycroft a_flag = 1;
334 1.15 phil break;
335 1.15 phil case 'f':
336 1.15 phil f_flag = 1;
337 1.4 mycroft break;
338 1.4 mycroft case 'i':
339 1.15 phil i_flag = 1;
340 1.4 mycroft break;
341 1.4 mycroft case 'u':
342 1.1 cgd u_flag = 1;
343 1.14 phil break;
344 1.14 phil case 's':
345 1.16 phil s_flag = 1;
346 1.16 phil if (sscanf (optarg, "%d/%d/%d",
347 1.16 phil &csysid, &cstart, &csize) != 3) {
348 1.16 phil (void)fprintf (stderr, "%s: Bad argument "
349 1.16 phil "to the -s flag.\n",
350 1.16 phil argv[0]);
351 1.16 phil exit (1);
352 1.16 phil }
353 1.16 phil break;
354 1.16 phil case 'b':
355 1.16 phil b_flag = 1;
356 1.16 phil if (sscanf (optarg, "%d/%d/%d",
357 1.16 phil &b_cyl, &b_head, &b_sec) != 3) {
358 1.16 phil (void)fprintf (stderr, "%s: Bad argument "
359 1.16 phil "to the -b flag.\n",
360 1.16 phil argv[0]);
361 1.16 phil exit (1);
362 1.28 ws }
363 1.28 ws if (b_cyl > MAXCYL)
364 1.14 phil b_cyl = MAXCYL;
365 1.30 ws break;
366 1.34 fvdl case 'c':
367 1.30 ws bootsize = read_boot(optarg, bootcode, sizeof bootcode);
368 1.4 mycroft break;
369 1.4 mycroft default:
370 1.1 cgd usage();
371 1.4 mycroft }
372 1.4 mycroft argc -= optind;
373 1.1 cgd argv += optind;
374 1.15 phil
375 1.15 phil if (sh_flag && (a_flag || i_flag || u_flag || f_flag || s_flag))
376 1.15 phil usage();
377 1.34 fvdl
378 1.34 fvdl if (B_flag && (a_flag || i_flag || u_flag || f_flag || s_flag))
379 1.34 fvdl usage();
380 1.15 phil
381 1.15 phil if (partition == -1 && s_flag) {
382 1.15 phil (void) fprintf (stderr,
383 1.14 phil "-s flag requires a partition selected.\n");
384 1.15 phil usage();
385 1.15 phil }
386 1.1 cgd
387 1.1 cgd if (argc > 0)
388 1.4 mycroft disk = argv[0];
389 1.34 fvdl
390 1.1 cgd if (open_disk(B_flag || a_flag || i_flag || u_flag) < 0)
391 1.1 cgd exit(1);
392 1.11 ghudson
393 1.28 ws if (read_s0())
394 1.11 ghudson init_sector0(sectors > 63 ? 63 : sectors, 1);
395 1.34 fvdl
396 1.34 fvdl #ifdef __i386__
397 1.34 fvdl get_geometry();
398 1.11 ghudson #else
399 1.34 fvdl intuit_translated_geometry();
400 1.15 phil #endif
401 1.15 phil
402 1.16 phil
403 1.1 cgd if ((i_flag || u_flag) && (!f_flag || b_flag))
404 1.1 cgd get_params_to_use();
405 1.15 phil
406 1.28 ws if (i_flag)
407 1.15 phil init_sector0(dos_sectors > 63 ? 63 : dos_sectors, 0);
408 1.15 phil
409 1.15 phil /* Do the update stuff! */
410 1.15 phil if (u_flag) {
411 1.34 fvdl if (!f_flag)
412 1.34 fvdl printf("Partition table:\n");
413 1.32 thorpej if (partition == -1)
414 1.15 phil for (part = 0; part < NMBRPART; part++)
415 1.15 phil change_part(part,-1, -1, -1);
416 1.15 phil else
417 1.4 mycroft change_part(partition, csysid, cstart, csize);
418 1.15 phil } else
419 1.15 phil if (!i_flag)
420 1.1 cgd print_s0(partition);
421 1.15 phil
422 1.1 cgd if (a_flag)
423 1.1 cgd change_active(partition);
424 1.34 fvdl
425 1.34 fvdl #ifdef __i386__
426 1.34 fvdl if (B_flag) {
427 1.34 fvdl configure_bootsel();
428 1.34 fvdl if (B_flag && bootsel_modified)
429 1.34 fvdl write_s0();
430 1.34 fvdl }
431 1.34 fvdl #endif
432 1.15 phil
433 1.15 phil if (u_flag || a_flag || i_flag) {
434 1.34 fvdl if (!f_flag) {
435 1.15 phil printf("\nWe haven't written the MBR back to disk "
436 1.15 phil "yet. This is your last chance.\n");
437 1.15 phil print_s0(-1);
438 1.15 phil if (yesno("Should we write new partition table?"))
439 1.15 phil write_s0();
440 1.1 cgd } else
441 1.1 cgd write_s0();
442 1.1 cgd }
443 1.1 cgd
444 1.4 mycroft exit(0);
445 1.1 cgd }
446 1.4 mycroft
447 1.4 mycroft void
448 1.4 mycroft usage()
449 1.38 soren {
450 1.38 soren (void)fprintf(stderr, "usage: fdisk [-aiufBS] [-0|-1|-2|-3] "
451 1.38 soren "[-b cylinders/heads/sectors]\n"
452 1.38 soren " [-s id/start/size] [-c bootcode] "
453 1.4 mycroft "[device]\n");
454 1.1 cgd exit(1);
455 1.1 cgd }
456 1.4 mycroft
457 1.1 cgd void
458 1.4 mycroft print_s0(which)
459 1.1 cgd int which;
460 1.4 mycroft {
461 1.1 cgd int part;
462 1.1 cgd
463 1.15 phil print_params();
464 1.34 fvdl if (!sh_flag)
465 1.4 mycroft printf("Partition table:\n");
466 1.32 thorpej if (which == -1) {
467 1.15 phil for (part = 0; part < NMBRPART; part++) {
468 1.15 phil if (!sh_flag)
469 1.15 phil printf("%d: ", part);
470 1.15 phil print_part(part);
471 1.4 mycroft }
472 1.1 cgd } else
473 1.1 cgd print_part(which);
474 1.1 cgd }
475 1.12 thorpej
476 1.12 thorpej static inline unsigned short
477 1.12 thorpej getshort(p)
478 1.12 thorpej void *p;
479 1.12 thorpej {
480 1.12 thorpej unsigned char *cp = p;
481 1.12 thorpej
482 1.12 thorpej return cp[0] | (cp[1] << 8);
483 1.12 thorpej }
484 1.12 thorpej
485 1.12 thorpej static inline void
486 1.12 thorpej putshort(p, l)
487 1.12 thorpej void *p;
488 1.12 thorpej unsigned short l;
489 1.12 thorpej {
490 1.12 thorpej unsigned char *cp = p;
491 1.12 thorpej
492 1.12 thorpej *cp++ = l;
493 1.12 thorpej *cp++ = l >> 8;
494 1.12 thorpej }
495 1.12 thorpej
496 1.12 thorpej static inline unsigned long
497 1.12 thorpej getlong(p)
498 1.12 thorpej void *p;
499 1.12 thorpej {
500 1.12 thorpej unsigned char *cp = p;
501 1.12 thorpej
502 1.12 thorpej return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
503 1.12 thorpej }
504 1.12 thorpej
505 1.12 thorpej static inline void
506 1.12 thorpej putlong(p, l)
507 1.12 thorpej void *p;
508 1.12 thorpej unsigned long l;
509 1.12 thorpej {
510 1.12 thorpej unsigned char *cp = p;
511 1.12 thorpej
512 1.12 thorpej *cp++ = l;
513 1.12 thorpej *cp++ = l >> 8;
514 1.12 thorpej *cp++ = l >> 16;
515 1.12 thorpej *cp++ = l >> 24;
516 1.12 thorpej }
517 1.4 mycroft
518 1.4 mycroft void
519 1.4 mycroft print_part(part)
520 1.1 cgd int part;
521 1.32 thorpej {
522 1.14 phil struct mbr_partition *partp;
523 1.1 cgd int empty;
524 1.4 mycroft
525 1.34 fvdl partp = &mboot.parts[part];
526 1.14 phil empty = (partp->mbrp_typ == 0);
527 1.15 phil
528 1.14 phil if (sh_flag) {
529 1.14 phil if (empty) {
530 1.14 phil printf("PART%dSIZE=0\n", part);
531 1.14 phil return;
532 1.14 phil }
533 1.32 thorpej
534 1.32 thorpej printf("PART%dID=%d\n", part, partp->mbrp_typ);
535 1.32 thorpej printf("PART%dSIZE=%ld\n", part, getlong(&partp->mbrp_size));
536 1.32 thorpej printf("PART%dSTART=%ld\n", part, getlong(&partp->mbrp_start));
537 1.32 thorpej printf("PART%dFLAG=0x%x\n", part, partp->mbrp_flag);
538 1.32 thorpej printf("PART%dBCYL=%d\n", part, MBR_PCYL(partp->mbrp_scyl,
539 1.32 thorpej partp->mbrp_ssect));
540 1.32 thorpej printf("PART%dBHEAD=%d\n", part, partp->mbrp_shd);
541 1.32 thorpej printf("PART%dBSEC=%d\n", part, MBR_PSECT(partp->mbrp_ssect));
542 1.32 thorpej printf("PART%dECYL=%d\n", part, MBR_PCYL(partp->mbrp_ecyl,
543 1.32 thorpej partp->mbrp_esect));
544 1.32 thorpej printf("PART%dEHEAD=%d\n", part, partp->mbrp_ehd);
545 1.14 phil printf("PART%dESEC=%d\n", part, MBR_PSECT(partp->mbrp_esect));
546 1.14 phil return;
547 1.14 phil }
548 1.15 phil
549 1.14 phil /* Not sh_flag. */
550 1.1 cgd if (empty) {
551 1.1 cgd printf("<UNUSED>\n");
552 1.1 cgd return;
553 1.32 thorpej }
554 1.18 lukem printf("sysid %d (%s)\n", partp->mbrp_typ, get_type(partp->mbrp_typ));
555 1.32 thorpej printf(" start %ld, size %ld (%ld MB), flag 0x%x\n",
556 1.32 thorpej getlong(&partp->mbrp_start), getlong(&partp->mbrp_size),
557 1.5 mycroft getlong(&partp->mbrp_size) * 512 / (1024 * 1024), partp->mbrp_flag);
558 1.32 thorpej printf("\tbeg: cylinder %4d, head %3d, sector %2d\n",
559 1.32 thorpej MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect),
560 1.5 mycroft partp->mbrp_shd, MBR_PSECT(partp->mbrp_ssect));
561 1.32 thorpej printf("\tend: cylinder %4d, head %3d, sector %2d\n",
562 1.32 thorpej MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect),
563 1.1 cgd partp->mbrp_ehd, MBR_PSECT(partp->mbrp_esect));
564 1.1 cgd }
565 1.34 fvdl
566 1.34 fvdl int
567 1.30 ws read_boot(name, buf, len)
568 1.34 fvdl char *name;
569 1.34 fvdl char *buf;
570 1.30 ws size_t len;
571 1.34 fvdl {
572 1.30 ws int bfd, ret;
573 1.30 ws struct stat st;
574 1.34 fvdl
575 1.30 ws if ((bfd = open(name, O_RDONLY)) < 0)
576 1.34 fvdl err(1, "%s", name);
577 1.30 ws if (fstat(bfd, &st) == -1)
578 1.34 fvdl err(1, "%s", name);
579 1.30 ws if (st.st_size > len)
580 1.34 fvdl errx(1, "%s: bootcode too large", name);
581 1.34 fvdl ret = st.st_size;
582 1.30 ws if (ret < 0x200)
583 1.34 fvdl errx(1, "%s: bootcode too small", name);
584 1.30 ws if (read(bfd, buf, len) != ret)
585 1.34 fvdl err(1, "%s", name);
586 1.30 ws close(bfd);
587 1.30 ws
588 1.30 ws /*
589 1.30 ws * Do some sanity checking here
590 1.32 thorpej */
591 1.30 ws if (getshort(bootcode + MBR_MAGICOFF) != MBR_MAGIC)
592 1.34 fvdl errx(1, "%s: invalid magic", name);
593 1.34 fvdl ret = (ret + 0x1ff) / 0x200;
594 1.34 fvdl ret *= 0x200;
595 1.30 ws return ret;
596 1.30 ws }
597 1.30 ws
598 1.28 ws void
599 1.28 ws init_sector0(start, dopart)
600 1.1 cgd int start, dopart;
601 1.15 phil {
602 1.15 phil int i;
603 1.30 ws
604 1.30 ws #ifdef DEFAULT_BOOTCODE
605 1.34 fvdl if (!bootsize)
606 1.34 fvdl bootsize = read_boot(DEFAULT_BOOTCODE, bootcode,
607 1.30 ws sizeof bootcode);
608 1.30 ws #endif
609 1.30 ws
610 1.32 thorpej memcpy(mboot.bootinst, bootcode, sizeof(mboot.bootinst));
611 1.15 phil putshort(&mboot.signature, MBR_MAGIC);
612 1.28 ws
613 1.34 fvdl if (dopart)
614 1.34 fvdl for (i=0; i<4; i++)
615 1.34 fvdl memset(&mboot.parts[i], 0, sizeof(struct mbr_partition));
616 1.34 fvdl
617 1.1 cgd }
618 1.34 fvdl
619 1.34 fvdl #ifdef __i386__
620 1.34 fvdl
621 1.34 fvdl void
622 1.34 fvdl get_diskname(fullname, diskname, size)
623 1.34 fvdl char *fullname;
624 1.34 fvdl char *diskname;
625 1.34 fvdl size_t size;
626 1.34 fvdl {
627 1.34 fvdl char *p;
628 1.34 fvdl char *p2;
629 1.34 fvdl size_t len;
630 1.34 fvdl
631 1.34 fvdl p = strrchr(fullname, '/');
632 1.34 fvdl if (p == NULL)
633 1.34 fvdl p = fullname;
634 1.34 fvdl else
635 1.34 fvdl p++;
636 1.34 fvdl
637 1.34 fvdl if (*p == 0) {
638 1.34 fvdl strncpy(diskname, fullname, size - 1);
639 1.34 fvdl diskname[size - 1] = '\0';
640 1.34 fvdl return;
641 1.34 fvdl }
642 1.34 fvdl
643 1.34 fvdl if (*p == 'r')
644 1.34 fvdl p++;
645 1.34 fvdl
646 1.34 fvdl for (p2 = p; *p2 != 0; p2++)
647 1.34 fvdl if (isdigit(*p2))
648 1.34 fvdl break;
649 1.34 fvdl if (*p2 == 0) {
650 1.34 fvdl /* XXX invalid diskname? */
651 1.34 fvdl strncpy(diskname, fullname, size - 1);
652 1.34 fvdl diskname[size - 1] = '\0';
653 1.34 fvdl return;
654 1.34 fvdl }
655 1.34 fvdl while (isdigit(*p2))
656 1.34 fvdl p2++;
657 1.34 fvdl
658 1.34 fvdl len = p2 - p;
659 1.34 fvdl if (len > size) {
660 1.34 fvdl /* XXX */
661 1.34 fvdl strncpy(diskname, fullname, size - 1);
662 1.34 fvdl diskname[size - 1] = '\0';
663 1.34 fvdl return;
664 1.34 fvdl }
665 1.34 fvdl
666 1.34 fvdl strncpy(diskname, p, len);
667 1.34 fvdl diskname[len] = 0;
668 1.34 fvdl }
669 1.34 fvdl
670 1.34 fvdl void
671 1.34 fvdl get_geometry()
672 1.34 fvdl {
673 1.34 fvdl int mib[2], i;
674 1.34 fvdl size_t len;
675 1.34 fvdl struct disklist *dl;
676 1.34 fvdl struct biosdisk_info *bip;
677 1.34 fvdl struct nativedisk_info *nip;
678 1.34 fvdl char diskname[8];
679 1.34 fvdl
680 1.34 fvdl mib[0] = CTL_MACHDEP;
681 1.34 fvdl mib[1] = CPU_DISKINFO;
682 1.34 fvdl if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0) {
683 1.34 fvdl intuit_translated_geometry();
684 1.34 fvdl return;
685 1.34 fvdl }
686 1.34 fvdl dl = (struct disklist *) malloc(len);
687 1.34 fvdl sysctl(mib, 2, dl, &len, NULL, 0);
688 1.34 fvdl
689 1.34 fvdl get_diskname(disk, diskname, sizeof diskname);
690 1.34 fvdl
691 1.34 fvdl for (i = 0; i < dl->dl_nnativedisks; i++) {
692 1.34 fvdl nip = &dl->dl_nativedisks[i];
693 1.34 fvdl if (strcmp(diskname, nip->ni_devname))
694 1.34 fvdl continue;
695 1.34 fvdl /*
696 1.34 fvdl * XXX listing possible matches is better. This is ok
697 1.34 fvdl * for now because the user has a chance to change
698 1.34 fvdl * it later.
699 1.34 fvdl */
700 1.34 fvdl if (nip->ni_nmatches != 0) {
701 1.34 fvdl bip = &dl->dl_biosdisks[nip->ni_biosmatches[0]];
702 1.34 fvdl dos_cylinders = bip->bi_cyl;
703 1.34 fvdl dos_heads = bip->bi_head;
704 1.34 fvdl dos_sectors = bip->bi_sec;
705 1.34 fvdl dos_cylindersectors = bip->bi_head * bip->bi_sec;
706 1.34 fvdl return;
707 1.34 fvdl }
708 1.34 fvdl }
709 1.34 fvdl /* Allright, allright, make a stupid guess.. */
710 1.11 ghudson intuit_translated_geometry();
711 1.11 ghudson }
712 1.34 fvdl
713 1.34 fvdl void
714 1.34 fvdl configure_bootsel()
715 1.34 fvdl {
716 1.34 fvdl struct mbr_bootsel *mbs =
717 1.34 fvdl (struct mbr_bootsel *)&mboot.bootinst[MBR_BOOTSELOFF];
718 1.38 soren int i, nused, firstpart = -1, item;
719 1.34 fvdl char desc[PARTNAMESIZE + 2], *p;
720 1.34 fvdl int timo, entry_changed = 0;
721 1.38 soren
722 1.34 fvdl for (i = nused = 0; i < NMBRPART; ++i) {
723 1.34 fvdl if (mboot.parts[i].mbrp_typ != 0) {
724 1.34 fvdl if (firstpart == -1)
725 1.34 fvdl firstpart = i;
726 1.34 fvdl nused++;
727 1.34 fvdl }
728 1.34 fvdl }
729 1.34 fvdl
730 1.34 fvdl if (nused == 0) {
731 1.34 fvdl printf("No used partitions found. Partition the disk first.\n");
732 1.34 fvdl return;
733 1.34 fvdl }
734 1.34 fvdl
735 1.34 fvdl if (mbs->magic != MBR_MAGIC) {
736 1.34 fvdl if (!yesno("Bootselector not yet installed. Install it now?")) {
737 1.34 fvdl printf("Bootselector not installed.\n");
738 1.34 fvdl return;
739 1.34 fvdl }
740 1.34 fvdl bootsize = read_boot(DEFAULT_BOOTSELCODE, bootcode,
741 1.34 fvdl sizeof bootcode);
742 1.34 fvdl memcpy(mboot.bootinst, bootcode, sizeof(mboot.bootinst));
743 1.34 fvdl bootsel_modified = 1;
744 1.34 fvdl mbs->flags |= BFL_SELACTIVE;
745 1.34 fvdl } else {
746 1.34 fvdl if (mbs->flags & BFL_SELACTIVE) {
747 1.34 fvdl printf("The bootselector is installed and active.\n");
748 1.34 fvdl if (!yesno("Do you want to change its settings?")) {
749 1.34 fvdl if (yesno("Do you want to deactivate it?")) {
750 1.34 fvdl mbs->flags &= ~BFL_SELACTIVE;
751 1.34 fvdl bootsel_modified = 1;
752 1.34 fvdl goto done;
753 1.34 fvdl }
754 1.34 fvdl return;
755 1.34 fvdl }
756 1.34 fvdl } else {
757 1.34 fvdl printf("The bootselector is installed but not active.\n");
758 1.34 fvdl if (yesno("Do you want to activate it?")) {
759 1.34 fvdl mbs->flags |= BFL_SELACTIVE;
760 1.34 fvdl bootsel_modified = 1;
761 1.34 fvdl }
762 1.34 fvdl if (!yesno("Do you want to change its settings?"))
763 1.34 fvdl goto done;
764 1.34 fvdl }
765 1.34 fvdl }
766 1.34 fvdl
767 1.34 fvdl printf("\n\nPartition table:\n");
768 1.34 fvdl for (i = 0; i < NMBRPART; i++) {
769 1.34 fvdl printf("%d: ", i);
770 1.34 fvdl print_part(i);
771 1.34 fvdl }
772 1.34 fvdl
773 1.34 fvdl printf("\n\nCurrent boot selection menu option names:\n");
774 1.34 fvdl for (i = 0; i < NMBRPART; i++) {
775 1.34 fvdl if (mbs->nametab[i][0] != 0)
776 1.34 fvdl printf("%d: %s\n", i, &mbs->nametab[i][0]);
777 1.38 soren else
778 1.34 fvdl printf("%d: <UNUSED>\n", i);
779 1.34 fvdl }
780 1.34 fvdl printf("\n");
781 1.34 fvdl
782 1.34 fvdl item = firstpart;
783 1.34 fvdl
784 1.34 fvdl editentries:
785 1.34 fvdl while (1) {
786 1.34 fvdl decimal("Change which entry (-1 quits)?", &item);
787 1.34 fvdl if (item == -1)
788 1.34 fvdl break;
789 1.34 fvdl if (item < 0 || item >= NMBRPART) {
790 1.38 soren printf("Invalid entry number\n");
791 1.34 fvdl item = -1;
792 1.34 fvdl continue;
793 1.34 fvdl }
794 1.38 soren if (mboot.parts[item].mbrp_typ == 0) {
795 1.38 soren printf("The partition entry is unused\n");
796 1.34 fvdl item = -1;
797 1.34 fvdl continue;
798 1.34 fvdl }
799 1.34 fvdl
800 1.34 fvdl printf("Enter descriptions (max. 8 characters): ");
801 1.38 soren rewind(stdin);
802 1.38 soren fgets(desc, PARTNAMESIZE + 1, stdin);
803 1.34 fvdl fpurge(stdin);
804 1.34 fvdl p = strchr(desc, '\n');
805 1.34 fvdl if (p != NULL)
806 1.34 fvdl *p = 0;
807 1.34 fvdl strcpy(&mbs->nametab[item][0], desc);
808 1.38 soren entry_changed = bootsel_modified = 1;
809 1.38 soren
810 1.34 fvdl item++;
811 1.34 fvdl }
812 1.34 fvdl
813 1.34 fvdl if (entry_changed)
814 1.34 fvdl printf("Boot selection menu option names are now:\n");
815 1.34 fvdl
816 1.34 fvdl firstpart = -1;
817 1.34 fvdl for (i = 0; i < NMBRPART; i++) {
818 1.34 fvdl if (mbs->nametab[i][0] != 0) {
819 1.34 fvdl firstpart = i;
820 1.34 fvdl if (entry_changed)
821 1.34 fvdl printf("%d: %s\n", i, &mbs->nametab[i][0]);
822 1.34 fvdl } else {
823 1.38 soren if (entry_changed)
824 1.34 fvdl printf("%d: <UNUSED>\n", i);
825 1.34 fvdl }
826 1.34 fvdl }
827 1.34 fvdl if (entry_changed)
828 1.34 fvdl printf("\n");
829 1.34 fvdl
830 1.34 fvdl if (firstpart == -1) {
831 1.34 fvdl printf("All menu entries are now inactive.\n");
832 1.34 fvdl if (!yesno("Are you sure about this?"))
833 1.34 fvdl goto editentries;
834 1.34 fvdl } else {
835 1.34 fvdl if (!(mbs->flags & BFL_SELACTIVE)) {
836 1.34 fvdl printf("The bootselector is not yet active.\n");
837 1.34 fvdl if (yesno("Activate it now?"))
838 1.34 fvdl mbs->flags |= BFL_SELACTIVE;
839 1.34 fvdl }
840 1.34 fvdl }
841 1.37 thorpej
842 1.37 thorpej /* bootsel is dirty from here on out. */
843 1.37 thorpej bootsel_modified = 1;
844 1.34 fvdl
845 1.34 fvdl /* The timeout value is in ticks, 18.2 Hz. Avoid using floats. */
846 1.34 fvdl timo = ((1000 * mbs->timeo) / 18200);
847 1.34 fvdl do {
848 1.34 fvdl decimal("Timeout value", &timo);
849 1.34 fvdl } while (timo < 0 || timo > 3600);
850 1.34 fvdl mbs->timeo = (u_int16_t)((timo * 18200) / 1000);
851 1.34 fvdl
852 1.34 fvdl printf("Select the default boot option. Options are:\n\n");
853 1.34 fvdl for (i = 0; i < NMBRPART; i++) {
854 1.34 fvdl if (mbs->nametab[i][0] != 0)
855 1.34 fvdl printf("%d: %s\n", i, &mbs->nametab[i][0]);
856 1.34 fvdl }
857 1.34 fvdl for (i = 4; i < 10; i++)
858 1.34 fvdl printf("%d: Harddisk %d\n", i, i - 4);
859 1.34 fvdl printf("10: The first active partition\n");
860 1.34 fvdl
861 1.34 fvdl if (mbs->defkey == SCAN_ENTER)
862 1.34 fvdl item = 10;
863 1.34 fvdl else
864 1.34 fvdl item = mbs->defkey - SCAN_F1;
865 1.34 fvdl
866 1.34 fvdl if (item < 0 || item > 10 || mbs->nametab[item][0] == 0)
867 1.34 fvdl item = 10;
868 1.34 fvdl
869 1.34 fvdl do {
870 1.34 fvdl decimal("Default boot option", &item);
871 1.34 fvdl } while (item < 0 || item > 10 ||
872 1.34 fvdl (item <= 3 && mbs->nametab[item][0] == 0));
873 1.34 fvdl
874 1.34 fvdl if (item == 10)
875 1.34 fvdl mbs->defkey = SCAN_ENTER;
876 1.34 fvdl else
877 1.34 fvdl mbs->defkey = SCAN_F1 + item;
878 1.34 fvdl
879 1.35 fvdl done:
880 1.35 fvdl for (i = 0; i < NMBRPART; i++) {
881 1.36 fvdl if (mboot.parts[i].mbrp_typ != 0 &&
882 1.36 fvdl mboot.parts[i].mbrp_start >=
883 1.35 fvdl (dos_cylinders * dos_heads * dos_sectors)) {
884 1.35 fvdl mbs->flags |= BFL_EXTINT13;
885 1.35 fvdl break;
886 1.35 fvdl }
887 1.35 fvdl }
888 1.37 thorpej
889 1.34 fvdl if (bootsel_modified != 0 && !yesno("Update the bootselector?"))
890 1.34 fvdl bootsel_modified = 0;
891 1.34 fvdl }
892 1.34 fvdl #endif
893 1.34 fvdl
894 1.11 ghudson
895 1.11 ghudson /* Prerequisite: the disklabel parameters and master boot record must
896 1.11 ghudson * have been read (i.e. dos_* and mboot are meaningful).
897 1.11 ghudson * Specification: modifies dos_cylinders, dos_heads, dos_sectors, and
898 1.11 ghudson * dos_cylindersectors to be consistent with what the
899 1.11 ghudson * partition table is using, if we can find a geometry
900 1.11 ghudson * which is consistent with all partition table entries.
901 1.11 ghudson * We may get the number of cylinders slightly wrong (in
902 1.11 ghudson * the conservative direction). The idea is to be able
903 1.11 ghudson * to create a NetBSD partition on a disk we don't know
904 1.11 ghudson * the translated geometry of.
905 1.11 ghudson * This whole routine should be replaced with a kernel interface to get
906 1.11 ghudson * the BIOS geometry (which in turn requires modifications to the i386
907 1.11 ghudson * boot loader to pass in the BIOS geometry for each disk). */
908 1.11 ghudson void
909 1.11 ghudson intuit_translated_geometry()
910 1.15 phil {
911 1.11 ghudson
912 1.11 ghudson int cylinders = -1, heads = -1, sectors = -1, i, j;
913 1.11 ghudson int c1, h1, s1, c2, h2, s2;
914 1.11 ghudson long a1, a2;
915 1.11 ghudson quad_t num, denom;
916 1.11 ghudson
917 1.32 thorpej /* Try to deduce the number of heads from two different mappings. */
918 1.11 ghudson for (i = 0; i < NMBRPART * 2; i++) {
919 1.11 ghudson if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
920 1.11 ghudson continue;
921 1.11 ghudson for (j = 0; j < 8; j++) {
922 1.11 ghudson if (get_mapping(j, &c2, &h2, &s2, &a2) < 0)
923 1.22 enami continue;
924 1.22 enami num = (quad_t)h1*(a2-s2) - (quad_t)h2*(a1-s1);
925 1.11 ghudson denom = (quad_t)c2*(a1-s1) - (quad_t)c1*(a2-s2);
926 1.11 ghudson if (denom != 0 && num % denom == 0) {
927 1.11 ghudson heads = num / denom;
928 1.11 ghudson break;
929 1.11 ghudson }
930 1.11 ghudson }
931 1.11 ghudson if (heads != -1)
932 1.11 ghudson break;
933 1.11 ghudson }
934 1.11 ghudson
935 1.11 ghudson if (heads == -1)
936 1.11 ghudson return;
937 1.11 ghudson
938 1.32 thorpej /* Now figure out the number of sectors from a single mapping. */
939 1.11 ghudson for (i = 0; i < NMBRPART * 2; i++) {
940 1.11 ghudson if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
941 1.11 ghudson continue;
942 1.11 ghudson num = a1 - s1;
943 1.11 ghudson denom = c1 * heads + h1;
944 1.11 ghudson if (denom != 0 && num % denom == 0) {
945 1.11 ghudson sectors = num / denom;
946 1.11 ghudson break;
947 1.11 ghudson }
948 1.11 ghudson }
949 1.11 ghudson
950 1.11 ghudson if (sectors == -1)
951 1.11 ghudson return;
952 1.11 ghudson
953 1.24 ghudson /* Estimate the number of cylinders. */
954 1.11 ghudson cylinders = disklabel.d_secperunit / heads / sectors;
955 1.11 ghudson
956 1.11 ghudson /* Now verify consistency with each of the partition table entries.
957 1.11 ghudson * Be willing to shove cylinders up a little bit to make things work,
958 1.32 thorpej * but translation mismatches are fatal. */
959 1.11 ghudson for (i = 0; i < NMBRPART * 2; i++) {
960 1.11 ghudson if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
961 1.11 ghudson continue;
962 1.11 ghudson if (sectors * (c1 * heads + h1) + s1 != a1)
963 1.11 ghudson return;
964 1.11 ghudson if (c1 >= cylinders)
965 1.11 ghudson cylinders = c1 + 1;
966 1.11 ghudson }
967 1.11 ghudson
968 1.11 ghudson /* Everything checks out. Reset the geometry to use for further
969 1.11 ghudson * calculations. */
970 1.11 ghudson dos_cylinders = cylinders;
971 1.11 ghudson dos_heads = heads;
972 1.11 ghudson dos_sectors = sectors;
973 1.11 ghudson dos_cylindersectors = heads * sectors;
974 1.11 ghudson }
975 1.11 ghudson
976 1.11 ghudson /* For the purposes of intuit_translated_geometry(), treat the partition
977 1.11 ghudson * table as a list of eight mapping between (cylinder, head, sector)
978 1.11 ghudson * triplets and absolute sectors. Get the relevant geometry triplet and
979 1.11 ghudson * absolute sectors for a given entry, or return -1 if it isn't present.
980 1.11 ghudson * Note: for simplicity, the returned sector is 0-based. */
981 1.11 ghudson int
982 1.11 ghudson get_mapping(i, cylinder, head, sector, absolute)
983 1.11 ghudson int i, *cylinder, *head, *sector;
984 1.11 ghudson long *absolute;
985 1.32 thorpej {
986 1.11 ghudson struct mbr_partition *part = &mboot.parts[i / 2];
987 1.32 thorpej
988 1.11 ghudson if (part->mbrp_typ == 0)
989 1.11 ghudson return -1;
990 1.32 thorpej if (i % 2 == 0) {
991 1.32 thorpej *cylinder = MBR_PCYL(part->mbrp_scyl, part->mbrp_ssect);
992 1.32 thorpej *head = part->mbrp_shd;
993 1.32 thorpej *sector = MBR_PSECT(part->mbrp_ssect) - 1;
994 1.11 ghudson *absolute = getlong(&part->mbrp_start);
995 1.32 thorpej } else {
996 1.32 thorpej *cylinder = MBR_PCYL(part->mbrp_ecyl, part->mbrp_esect);
997 1.32 thorpej *head = part->mbrp_ehd;
998 1.32 thorpej *sector = MBR_PSECT(part->mbrp_esect) - 1;
999 1.32 thorpej *absolute = getlong(&part->mbrp_start)
1000 1.11 ghudson + getlong(&part->mbrp_size) - 1;
1001 1.11 ghudson }
1002 1.1 cgd return 0;
1003 1.1 cgd }
1004 1.4 mycroft
1005 1.15 phil void
1006 1.15 phil change_part(part, csysid, cstart, csize)
1007 1.1 cgd int part, csysid, cstart, csize;
1008 1.32 thorpej {
1009 1.1 cgd struct mbr_partition *partp;
1010 1.4 mycroft
1011 1.1 cgd partp = &mboot.parts[part];
1012 1.15 phil
1013 1.29 ws if (s_flag) {
1014 1.29 ws if (csysid == 0 && cstart == 0 && csize == 0)
1015 1.29 ws memset(partp, 0, sizeof *partp);
1016 1.32 thorpej else {
1017 1.28 ws partp->mbrp_typ = csysid;
1018 1.29 ws #if 0
1019 1.28 ws checkcyl(cstart / dos_cylindersectors);
1020 1.32 thorpej #endif
1021 1.32 thorpej putlong(&partp->mbrp_start, cstart);
1022 1.32 thorpej putlong(&partp->mbrp_size, csize);
1023 1.32 thorpej dos(getlong(&partp->mbrp_start),
1024 1.32 thorpej &partp->mbrp_scyl, &partp->mbrp_shd, &partp->mbrp_ssect);
1025 1.32 thorpej dos(getlong(&partp->mbrp_start)
1026 1.32 thorpej + getlong(&partp->mbrp_size) - 1,
1027 1.29 ws &partp->mbrp_ecyl, &partp->mbrp_ehd, &partp->mbrp_esect);
1028 1.15 phil }
1029 1.15 phil if (f_flag)
1030 1.15 phil return;
1031 1.15 phil }
1032 1.15 phil
1033 1.4 mycroft printf("The data for partition %d is:\n", part);
1034 1.4 mycroft print_part(part);
1035 1.4 mycroft if (!u_flag || !yesno("Do you want to change it?"))
1036 1.1 cgd return;
1037 1.1 cgd
1038 1.5 mycroft do {
1039 1.5 mycroft {
1040 1.5 mycroft int sysid, start, size;
1041 1.32 thorpej
1042 1.32 thorpej sysid = partp->mbrp_typ,
1043 1.32 thorpej start = getlong(&partp->mbrp_start),
1044 1.5 mycroft size = getlong(&partp->mbrp_size);
1045 1.5 mycroft decimal("sysid", &sysid);
1046 1.5 mycroft decimal("start", &start);
1047 1.32 thorpej decimal("size", &size);
1048 1.32 thorpej partp->mbrp_typ = sysid;
1049 1.32 thorpej putlong(&partp->mbrp_start, start);
1050 1.5 mycroft putlong(&partp->mbrp_size, size);
1051 1.1 cgd }
1052 1.7 mycroft
1053 1.5 mycroft if (yesno("Explicitly specify beg/end address?")) {
1054 1.4 mycroft int tsector, tcylinder, thead;
1055 1.32 thorpej
1056 1.32 thorpej tcylinder = MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect);
1057 1.32 thorpej thead = partp->mbrp_shd;
1058 1.5 mycroft tsector = MBR_PSECT(partp->mbrp_ssect);
1059 1.28 ws decimal("beginning cylinder", &tcylinder);
1060 1.28 ws #if 0
1061 1.28 ws checkcyl(tcylinder);
1062 1.5 mycroft #endif
1063 1.5 mycroft decimal("beginning head", &thead);
1064 1.32 thorpej decimal("beginning sector", &tsector);
1065 1.32 thorpej partp->mbrp_scyl = DOSCYL(tcylinder);
1066 1.32 thorpej partp->mbrp_shd = thead;
1067 1.32 thorpej partp->mbrp_ssect = DOSSECT(tsector, tcylinder);
1068 1.32 thorpej
1069 1.32 thorpej tcylinder = MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect);
1070 1.32 thorpej thead = partp->mbrp_ehd;
1071 1.5 mycroft tsector = MBR_PSECT(partp->mbrp_esect);
1072 1.5 mycroft decimal("ending cylinder", &tcylinder);
1073 1.5 mycroft decimal("ending head", &thead);
1074 1.32 thorpej decimal("ending sector", &tsector);
1075 1.32 thorpej partp->mbrp_ecyl = DOSCYL(tcylinder);
1076 1.32 thorpej partp->mbrp_ehd = thead;
1077 1.1 cgd partp->mbrp_esect = DOSSECT(tsector, tcylinder);
1078 1.28 ws } else {
1079 1.32 thorpej
1080 1.32 thorpej if (partp->mbrp_typ == 0
1081 1.32 thorpej && getlong(&partp->mbrp_start) == 0
1082 1.29 ws && getlong(&partp->mbrp_size) == 0)
1083 1.29 ws memset(partp, 0, sizeof *partp);
1084 1.28 ws else {
1085 1.32 thorpej #if 0
1086 1.29 ws checkcyl(getlong(&partp->mbrp_start)
1087 1.28 ws / dos_cylindersectors);
1088 1.32 thorpej #endif
1089 1.32 thorpej dos(getlong(&partp->mbrp_start), &partp->mbrp_scyl,
1090 1.32 thorpej &partp->mbrp_shd, &partp->mbrp_ssect);
1091 1.32 thorpej dos(getlong(&partp->mbrp_start)
1092 1.32 thorpej + getlong(&partp->mbrp_size) - 1,
1093 1.32 thorpej &partp->mbrp_ecyl, &partp->mbrp_ehd,
1094 1.29 ws &partp->mbrp_esect);
1095 1.1 cgd }
1096 1.4 mycroft }
1097 1.4 mycroft
1098 1.7 mycroft print_part(part);
1099 1.1 cgd } while (!yesno("Is this entry okay?"));
1100 1.1 cgd }
1101 1.4 mycroft
1102 1.1 cgd void
1103 1.1 cgd print_params()
1104 1.4 mycroft {
1105 1.15 phil
1106 1.31 jonathan if (sh_flag) {
1107 1.31 jonathan printf ("DLCYL=%d\nDLHEAD=%d\nDLSEC=%d\nDLSIZE=%d\n",
1108 1.14 phil cylinders, heads, sectors, disksectors);
1109 1.14 phil printf ("BCYL=%d\nBHEAD=%d\nBSEC=%d\n",
1110 1.14 phil dos_cylinders, dos_heads, dos_sectors);
1111 1.14 phil return;
1112 1.14 phil }
1113 1.15 phil
1114 1.34 fvdl /* Not sh_flag */
1115 1.34 fvdl printf("NetBSD disklabel disk geometry:\n");
1116 1.5 mycroft printf("cylinders: %d heads: %d sectors/track: %d (%d sectors/cylinder)\n\n",
1117 1.34 fvdl cylinders, heads, sectors, cylindersectors);
1118 1.34 fvdl printf("BIOS disk geometry:\n");
1119 1.5 mycroft printf("cylinders: %d heads: %d sectors/track: %d (%d sectors/cylinder)\n\n",
1120 1.1 cgd dos_cylinders, dos_heads, dos_sectors, dos_cylindersectors);
1121 1.1 cgd }
1122 1.4 mycroft
1123 1.1 cgd void
1124 1.4 mycroft change_active(which)
1125 1.1 cgd int which;
1126 1.32 thorpej {
1127 1.4 mycroft struct mbr_partition *partp;
1128 1.15 phil int part;
1129 1.4 mycroft int active = 4;
1130 1.4 mycroft
1131 1.1 cgd partp = &mboot.parts[0];
1132 1.1 cgd
1133 1.1 cgd if (a_flag && which != -1)
1134 1.4 mycroft active = which;
1135 1.32 thorpej else {
1136 1.32 thorpej for (part = 0; part < NMBRPART; part++)
1137 1.4 mycroft if (partp[part].mbrp_flag & ACTIVE)
1138 1.4 mycroft active = part;
1139 1.15 phil }
1140 1.15 phil if (!f_flag) {
1141 1.15 phil if (yesno("Do you want to change the active partition?")) {
1142 1.15 phil printf ("Choosing 4 will make no partition active.\n");
1143 1.15 phil do {
1144 1.15 phil decimal("active partition", &active);
1145 1.15 phil } while (!yesno("Are you happy with this choice?"));
1146 1.15 phil } else
1147 1.15 phil return;
1148 1.15 phil } else
1149 1.15 phil if (active != 4)
1150 1.15 phil printf ("Making partition %d active.\n", active);
1151 1.32 thorpej
1152 1.32 thorpej for (part = 0; part < NMBRPART; part++)
1153 1.15 phil partp[part].mbrp_flag &= ~ACTIVE;
1154 1.32 thorpej if (active < 4)
1155 1.1 cgd partp[active].mbrp_flag |= ACTIVE;
1156 1.1 cgd }
1157 1.4 mycroft
1158 1.1 cgd void
1159 1.1 cgd get_params_to_use()
1160 1.16 phil {
1161 1.16 phil if (b_flag) {
1162 1.16 phil dos_cylinders = b_cyl;
1163 1.16 phil dos_heads = b_head;
1164 1.16 phil dos_sectors = b_sec;
1165 1.16 phil dos_cylindersectors = dos_heads * dos_sectors;
1166 1.16 phil return;
1167 1.4 mycroft }
1168 1.1 cgd
1169 1.4 mycroft print_params();
1170 1.4 mycroft if (yesno("Do you want to change our idea of what BIOS thinks?")) {
1171 1.5 mycroft do {
1172 1.5 mycroft decimal("BIOS's idea of #cylinders", &dos_cylinders);
1173 1.5 mycroft decimal("BIOS's idea of #heads", &dos_heads);
1174 1.5 mycroft decimal("BIOS's idea of #sectors", &dos_sectors);
1175 1.1 cgd dos_cylindersectors = dos_heads * dos_sectors;
1176 1.4 mycroft print_params();
1177 1.1 cgd } while (!yesno("Are you happy with this choice?"));
1178 1.1 cgd }
1179 1.1 cgd }
1180 1.1 cgd
1181 1.1 cgd /***********************************************\
1182 1.1 cgd * Change real numbers into strange dos numbers *
1183 1.4 mycroft \***********************************************/
1184 1.5 mycroft void
1185 1.5 mycroft dos(sector, cylinderp, headp, sectorp)
1186 1.5 mycroft int sector;
1187 1.4 mycroft unsigned char *cylinderp, *headp, *sectorp;
1188 1.5 mycroft {
1189 1.4 mycroft int cylinder, head;
1190 1.5 mycroft
1191 1.28 ws cylinder = sector / dos_cylindersectors;
1192 1.5 mycroft
1193 1.4 mycroft sector -= cylinder * dos_cylindersectors;
1194 1.5 mycroft
1195 1.5 mycroft head = sector / dos_sectors;
1196 1.4 mycroft sector -= head * dos_sectors;
1197 1.28 ws
1198 1.28 ws if (cylinder >= MAXCYL)
1199 1.5 mycroft cylinder = MAXCYL - 1;
1200 1.5 mycroft *cylinderp = DOSCYL(cylinder);
1201 1.5 mycroft *headp = head;
1202 1.1 cgd *sectorp = DOSSECT(sector + 1, cylinder);
1203 1.1 cgd }
1204 1.28 ws
1205 1.28 ws #if 0
1206 1.28 ws void
1207 1.28 ws checkcyl(cyl)
1208 1.28 ws int cyl;
1209 1.28 ws {
1210 1.28 ws if (cyl >= MAXCYL)
1211 1.28 ws warnx("partition start beyond BIOS limit");
1212 1.28 ws }
1213 1.28 ws #endif
1214 1.1 cgd
1215 1.1 cgd int fd;
1216 1.4 mycroft
1217 1.1 cgd int
1218 1.4 mycroft open_disk(u_flag)
1219 1.1 cgd int u_flag;
1220 1.18 lukem {
1221 1.4 mycroft static char namebuf[MAXPATHLEN + 1];
1222 1.1 cgd struct stat st;
1223 1.19 lukem
1224 1.19 lukem fd = opendisk(disk, u_flag ? O_RDWR : O_RDONLY, namebuf,
1225 1.18 lukem sizeof(namebuf), 0);
1226 1.18 lukem if (fd < 0) {
1227 1.4 mycroft warn("%s", namebuf);
1228 1.4 mycroft return (-1);
1229 1.18 lukem }
1230 1.4 mycroft disk = namebuf;
1231 1.4 mycroft if (fstat(fd, &st) == -1) {
1232 1.4 mycroft close(fd);
1233 1.4 mycroft warn("%s", disk);
1234 1.1 cgd return (-1);
1235 1.7 mycroft }
1236 1.4 mycroft if (!S_ISCHR(st.st_mode) && !S_ISREG(st.st_mode)) {
1237 1.7 mycroft close(fd);
1238 1.4 mycroft warnx("%s is not a character device or regular file", disk);
1239 1.1 cgd return (-1);
1240 1.4 mycroft }
1241 1.4 mycroft if (get_params() == -1) {
1242 1.4 mycroft close(fd);
1243 1.1 cgd return (-1);
1244 1.4 mycroft }
1245 1.1 cgd return (0);
1246 1.1 cgd }
1247 1.4 mycroft
1248 1.1 cgd int
1249 1.4 mycroft read_disk(sector, buf)
1250 1.4 mycroft int sector;
1251 1.1 cgd void *buf;
1252 1.4 mycroft {
1253 1.4 mycroft
1254 1.4 mycroft if (lseek(fd, (off_t)(sector * 512), 0) == -1)
1255 1.4 mycroft return (-1);
1256 1.1 cgd return (read(fd, buf, 512));
1257 1.1 cgd }
1258 1.4 mycroft
1259 1.1 cgd int
1260 1.4 mycroft write_disk(sector, buf)
1261 1.4 mycroft int sector;
1262 1.1 cgd void *buf;
1263 1.4 mycroft {
1264 1.4 mycroft
1265 1.4 mycroft if (lseek(fd, (off_t)(sector * 512), 0) == -1)
1266 1.4 mycroft return (-1);
1267 1.1 cgd return (write(fd, buf, 512));
1268 1.1 cgd }
1269 1.4 mycroft
1270 1.4 mycroft int
1271 1.1 cgd get_params()
1272 1.1 cgd {
1273 1.4 mycroft
1274 1.4 mycroft if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
1275 1.4 mycroft warn("DIOCGDINFO");
1276 1.4 mycroft return (-1);
1277 1.1 cgd }
1278 1.5 mycroft
1279 1.4 mycroft dos_cylinders = cylinders = disklabel.d_ncylinders;
1280 1.4 mycroft dos_heads = heads = disklabel.d_ntracks;
1281 1.5 mycroft dos_sectors = sectors = disklabel.d_nsectors;
1282 1.31 jonathan dos_cylindersectors = cylindersectors = heads * sectors;
1283 1.1 cgd disksectors = disklabel.d_secperunit;
1284 1.4 mycroft
1285 1.1 cgd return (0);
1286 1.1 cgd }
1287 1.4 mycroft
1288 1.1 cgd int
1289 1.1 cgd read_s0()
1290 1.4 mycroft {
1291 1.4 mycroft
1292 1.4 mycroft if (read_disk(0, mboot.bootinst) == -1) {
1293 1.4 mycroft warn("can't read fdisk partition table");
1294 1.1 cgd return (-1);
1295 1.32 thorpej }
1296 1.12 thorpej if (getshort(&mboot.signature) != MBR_MAGIC) {
1297 1.4 mycroft warnx("invalid fdisk partition table found");
1298 1.1 cgd return (-1);
1299 1.4 mycroft }
1300 1.1 cgd return (0);
1301 1.1 cgd }
1302 1.4 mycroft
1303 1.1 cgd int
1304 1.1 cgd write_s0()
1305 1.30 ws {
1306 1.4 mycroft int flag, i;
1307 1.1 cgd
1308 1.1 cgd /*
1309 1.1 cgd * write enable label sector before write (if necessary),
1310 1.1 cgd * disable after writing.
1311 1.1 cgd * needed if the disklabel protected area also protects
1312 1.1 cgd * sector 0. (e.g. empty disk)
1313 1.1 cgd */
1314 1.1 cgd flag = 1;
1315 1.4 mycroft if (ioctl(fd, DIOCWLABEL, &flag) < 0)
1316 1.4 mycroft warn("DIOCWLABEL");
1317 1.4 mycroft if (write_disk(0, mboot.bootinst) == -1) {
1318 1.1 cgd warn("can't write fdisk partition table");
1319 1.4 mycroft return -1;
1320 1.30 ws }
1321 1.30 ws for (i = bootsize; (i -= 0x200) > 0;)
1322 1.30 ws if (write_disk(i / 0x200, bootcode + i) == -1) {
1323 1.30 ws warn("can't write bootcode");
1324 1.30 ws return -1;
1325 1.1 cgd }
1326 1.4 mycroft flag = 0;
1327 1.4 mycroft if (ioctl(fd, DIOCWLABEL, &flag) < 0)
1328 1.16 phil warn("DIOCWLABEL");
1329 1.1 cgd return 0;
1330 1.1 cgd }
1331 1.4 mycroft
1332 1.4 mycroft int
1333 1.4 mycroft yesno(str)
1334 1.4 mycroft char *str;
1335 1.4 mycroft {
1336 1.1 cgd int ch, first;
1337 1.1 cgd
1338 1.1 cgd printf("%s [n] ", str);
1339 1.4 mycroft
1340 1.4 mycroft first = ch = getchar();
1341 1.4 mycroft while (ch != '\n' && ch != EOF)
1342 1.4 mycroft ch = getchar();
1343 1.1 cgd return (first == 'y' || first == 'Y');
1344 1.1 cgd }
1345 1.5 mycroft
1346 1.5 mycroft void
1347 1.4 mycroft decimal(str, num)
1348 1.5 mycroft char *str;
1349 1.1 cgd int *num;
1350 1.8 cgd {
1351 1.4 mycroft int acc = 0;
1352 1.1 cgd char *cp;
1353 1.5 mycroft
1354 1.34 fvdl for (;; printf("%s is not a valid decimal number.\n", lbuf)) {
1355 1.1 cgd printf("%s: [%d] ", str, *num);
1356 1.1 cgd
1357 1.7 mycroft fgets(lbuf, LBUF, stdin);
1358 1.7 mycroft lbuf[strlen(lbuf)-1] = '\0';
1359 1.7 mycroft cp = lbuf;
1360 1.7 mycroft
1361 1.7 mycroft cp += strspn(cp, " \t");
1362 1.5 mycroft if (*cp == '\0')
1363 1.1 cgd return;
1364 1.34 fvdl
1365 1.7 mycroft if (!isdigit(*cp) && *cp != '-')
1366 1.5 mycroft continue;
1367 1.7 mycroft acc = strtol(lbuf, &cp, 10);
1368 1.5 mycroft
1369 1.5 mycroft cp += strspn(cp, " \t");
1370 1.5 mycroft if (*cp != '\0')
1371 1.1 cgd continue;
1372 1.5 mycroft
1373 1.5 mycroft *num = acc;
1374 1.1 cgd return;
1375 1.1 cgd }
1376 1.1 cgd
1377 1.1 cgd }
1378 1.4 mycroft
1379 1.4 mycroft int
1380 1.4 mycroft type_match(key, item)
1381 1.4 mycroft const void *key, *item;
1382 1.4 mycroft {
1383 1.4 mycroft const int *typep = key;
1384 1.4 mycroft const struct part_type *ptr = item;
1385 1.4 mycroft
1386 1.4 mycroft if (*typep < ptr->type)
1387 1.4 mycroft return (-1);
1388 1.4 mycroft if (*typep > ptr->type)
1389 1.4 mycroft return (1);
1390 1.4 mycroft return (0);
1391 1.4 mycroft }
1392 1.4 mycroft
1393 1.4 mycroft char *
1394 1.4 mycroft get_type(type)
1395 1.4 mycroft int type;
1396 1.4 mycroft {
1397 1.4 mycroft struct part_type *ptr;
1398 1.4 mycroft
1399 1.4 mycroft ptr = bsearch(&type, part_types,
1400 1.4 mycroft sizeof(part_types) / sizeof(struct part_type),
1401 1.4 mycroft sizeof(struct part_type), type_match);
1402 1.4 mycroft if (ptr == 0)
1403 1.16 phil return ("unknown");
1404 1.1 cgd return (ptr->name);
1405 }
1406