fdisk.c revision 1.23 1 1.23 mikel /* $NetBSD: fdisk.c,v 1.23 1998/02/02 06:39:41 mikel 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.23 mikel __RCSID("$NetBSD: fdisk.c,v 1.23 1998/02/02 06:39:41 mikel 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.4 mycroft #include <sys/ioctl.h>
38 1.18 lukem #include <sys/param.h>
39 1.1 cgd #include <sys/stat.h>
40 1.4 mycroft
41 1.8 cgd #include <ctype.h>
42 1.4 mycroft #include <err.h>
43 1.18 lukem #include <errno.h>
44 1.1 cgd #include <fcntl.h>
45 1.18 lukem #include <paths.h>
46 1.4 mycroft #include <stdio.h>
47 1.4 mycroft #include <stdlib.h>
48 1.8 cgd #include <string.h>
49 1.8 cgd #include <unistd.h>
50 1.19 lukem #include <util.h>
51 1.1 cgd
52 1.1 cgd #define LBUF 100
53 1.1 cgd static char lbuf[LBUF];
54 1.1 cgd
55 1.1 cgd /*
56 1.1 cgd * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
57 1.1 cgd * Copyright (c) 1989 Robert. V. Baron
58 1.1 cgd * Created.
59 1.1 cgd */
60 1.1 cgd
61 1.1 cgd char *disk = "/dev/rwd0d";
62 1.1 cgd
63 1.1 cgd struct disklabel disklabel; /* disk parameters */
64 1.1 cgd
65 1.5 mycroft int cylinders, sectors, heads, cylindersectors, disksectors;
66 1.1 cgd
67 1.4 mycroft struct mboot {
68 1.1 cgd unsigned char padding[2]; /* force the longs to be long alligned */
69 1.1 cgd unsigned char bootinst[DOSPARTOFF];
70 1.1 cgd struct dos_partition parts[4];
71 1.1 cgd unsigned short int signature;
72 1.1 cgd };
73 1.1 cgd struct mboot mboot;
74 1.1 cgd
75 1.1 cgd #define ACTIVE 0x80
76 1.1 cgd #define BOOT_MAGIC 0xAA55
77 1.1 cgd
78 1.5 mycroft int dos_cylinders;
79 1.1 cgd int dos_heads;
80 1.1 cgd int dos_sectors;
81 1.5 mycroft int dos_cylindersectors;
82 1.1 cgd
83 1.4 mycroft #define DOSSECT(s,c) (((s) & 0x3f) | (((c) >> 2) & 0xc0))
84 1.4 mycroft #define DOSCYL(c) ((c) & 0xff)
85 1.4 mycroft int partition = -1;
86 1.4 mycroft
87 1.4 mycroft int a_flag; /* set active partition */
88 1.4 mycroft int i_flag; /* replace partition data */
89 1.4 mycroft int u_flag; /* update partition data */
90 1.15 phil int sh_flag; /* Output data as shell defines */
91 1.15 phil int f_flag; /* force --not interactive */
92 1.15 phil int s_flag; /* set id,offset,size */
93 1.16 phil int b_flag; /* Set cyl, heads, secs (as c/h/s) */
94 1.16 phil int b_cyl, b_head, b_sec; /* b_flag values. */
95 1.4 mycroft
96 1.4 mycroft unsigned char bootcode[] = {
97 1.4 mycroft 0x33, 0xc0, 0xfa, 0x8e, 0xd0, 0xbc, 0x00, 0x7c, 0x8e, 0xc0, 0x8e, 0xd8, 0xfb, 0x8b, 0xf4, 0xbf,
98 1.4 mycroft 0x00, 0x06, 0xb9, 0x00, 0x02, 0xfc, 0xf3, 0xa4, 0xea, 0x1d, 0x06, 0x00, 0x00, 0xb0, 0x04, 0xbe,
99 1.4 mycroft 0xbe, 0x07, 0x80, 0x3c, 0x80, 0x74, 0x0c, 0x83, 0xc6, 0x10, 0xfe, 0xc8, 0x75, 0xf4, 0xbe, 0xbd,
100 1.4 mycroft 0x06, 0xeb, 0x43, 0x8b, 0xfe, 0x8b, 0x14, 0x8b, 0x4c, 0x02, 0x83, 0xc6, 0x10, 0xfe, 0xc8, 0x74,
101 1.4 mycroft 0x0a, 0x80, 0x3c, 0x80, 0x75, 0xf4, 0xbe, 0xbd, 0x06, 0xeb, 0x2b, 0xbd, 0x05, 0x00, 0xbb, 0x00,
102 1.4 mycroft 0x7c, 0xb8, 0x01, 0x02, 0xcd, 0x13, 0x73, 0x0c, 0x33, 0xc0, 0xcd, 0x13, 0x4d, 0x75, 0xef, 0xbe,
103 1.4 mycroft 0x9e, 0x06, 0xeb, 0x12, 0x81, 0x3e, 0xfe, 0x7d, 0x55, 0xaa, 0x75, 0x07, 0x8b, 0xf7, 0xea, 0x00,
104 1.4 mycroft 0x7c, 0x00, 0x00, 0xbe, 0x85, 0x06, 0x2e, 0xac, 0x0a, 0xc0, 0x74, 0x06, 0xb4, 0x0e, 0xcd, 0x10,
105 1.1 cgd 0xeb, 0xf4, 0xfb, 0xeb, 0xfe,
106 1.1 cgd 'M', 'i', 's', 's', 'i', 'n', 'g', ' ',
107 1.1 cgd 'o', 'p', 'e', 'r', 'a', 't', 'i', 'n', 'g', ' ', 's', 'y', 's', 't', 'e', 'm', 0,
108 1.4 mycroft 'E', 'r', 'r', 'o', 'r', ' ', 'l', 'o', 'a', 'd', 'i', 'n', 'g', ' ',
109 1.1 cgd 'o', 'p', 'e', 'r', 'a', 't', 'i', 'n', 'g', ' ', 's', 'y', 's', 't', 'e', 'm', 0,
110 1.1 cgd 'I', 'n', 'v', 'a', 'l', 'i', 'd', ' ',
111 1.1 cgd 'p', 'a', 'r', 't', 'i', 't', 'i', 'o', 'n', ' ', 't', 'a', 'b', 'l', 'e', 0,
112 1.1 cgd 'A', 'u', 't', 'h', 'o', 'r', ' ', '-', ' ',
113 1.4 mycroft 'S', 'i', 'e', 'g', 'm', 'a', 'r', ' ', 'S', 'c', 'h', 'm', 'i', 'd', 't', 0,0,0,
114 1.1 cgd
115 1.4 mycroft 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
116 1.4 mycroft 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
117 1.4 mycroft 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
118 1.4 mycroft 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
119 1.4 mycroft 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
120 1.4 mycroft 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121 1.4 mycroft 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
122 1.4 mycroft 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123 1.4 mycroft 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
124 1.4 mycroft 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
125 1.4 mycroft 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
126 1.4 mycroft 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
127 1.4 mycroft 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
128 1.1 cgd };
129 1.1 cgd
130 1.4 mycroft struct part_type {
132 1.4 mycroft int type;
133 1.4 mycroft char *name;
134 1.4 mycroft } part_types[] = {
135 1.4 mycroft {0x00, "unused"},
136 1.4 mycroft {0x01, "Primary DOS with 12 bit FAT"},
137 1.4 mycroft {0x02, "XENIX / filesystem"},
138 1.13 perry {0x03, "XENIX /usr filesystem"},
139 1.4 mycroft {0x04, "Primary DOS with 16 bit FAT <32M"},
140 1.13 perry {0x05, "Extended DOS"},
141 1.13 perry {0x06, "Primary 'big' DOS, 16-bit FAT (> 32MB)"},
142 1.4 mycroft {0x07, "OS/2 HPFS or NTFS or QNX2 or Advanced UNIX"},
143 1.4 mycroft {0x08, "AIX filesystem"},
144 1.13 perry {0x09, "AIX boot partition or Coherent"},
145 1.13 perry {0x0A, "OS/2 Boot Manager or Coherent swap or OPUS"},
146 1.13 perry {0x0E, "DOS (16-bit FAT), CHS-mapped"},
147 1.4 mycroft {0x0F, "Ext. partition, CHS-mapped"},
148 1.13 perry {0x10, "OPUS"},
149 1.13 perry {0x11, "OS/2 BM: hidden DOS 12-bit FAT"},
150 1.13 perry {0x12, "Compaq diagnostics"},
151 1.13 perry {0x14, "OS/2 BM: hidden DOS 16-bit FAT <32M"},
152 1.13 perry {0x16, "OS/2 BM: hidden DOS 16-bit FAT >=32M"},
153 1.13 perry {0x17, "OS/2 BM: hidden IFS"},
154 1.13 perry {0x18, "AST Windows swapfile"},
155 1.13 perry {0x24, "NEC DOS"},
156 1.4 mycroft {0x3C, "PartitionMagic recovery"},
157 1.13 perry {0x40, "VENIX 286"},
158 1.13 perry {0x41, "Linux/MINIX (sharing disk with DRDOS)"},
159 1.13 perry {0x42, "SFS or Linux swap (sharing disk with DRDOS)"},
160 1.13 perry {0x43, "Linux native (sharing disk with DRDOS)"},
161 1.13 perry {0x50, "DM (disk manager)"},
162 1.4 mycroft {0x51, "DM6 Aux1 (or Novell)"},
163 1.13 perry {0x52, "CP/M or Microport SysV/AT"},
164 1.13 perry {0x53, "DM6 Aux3"},
165 1.13 perry {0x54, "DM6"},
166 1.13 perry {0x55, "EZ-Drive (disk manager)"},
167 1.13 perry {0x56, "Golden Bow (disk manager)"},
168 1.13 perry {0x5C, "Priam Edisk (disk manager)"},
169 1.13 perry {0x61, "SpeedStor"},
170 1.4 mycroft {0x63, "GNU HURD or Mach or Sys V/386 (such as ISC UNIX)"},
171 1.4 mycroft {0x64, "Novell Netware 2.xx"},
172 1.13 perry {0x65, "Novell Netware 3.xx"},
173 1.13 perry {0x70, "DiskSecure Multi-Boot"},
174 1.13 perry {0x75, "PC/IX"},
175 1.13 perry {0x77, "QNX4.x"},
176 1.13 perry {0x78, "QNX4.x 2nd part"},
177 1.13 perry {0x79, "QNX4.x 3rd part"},
178 1.13 perry {0x80, "MINIX until 1.4a"},
179 1.9 mycroft {0x81, "MINIX since 1.4b, early Linux, Mitac dmgr"},
180 1.13 perry {0x82, "Linux swap"},
181 1.13 perry {0x83, "Linux native"},
182 1.13 perry {0x84, "OS/2 hidden C: drive"},
183 1.13 perry {0x85, "Linux extended"},
184 1.13 perry {0x86, "NTFS volume set??"},
185 1.4 mycroft {0x87, "NTFS volume set??"},
186 1.4 mycroft {0x93, "Amoeba filesystem"},
187 1.13 perry {0x94, "Amoeba bad block table"},
188 1.13 perry {0xA0, "IBM Thinkpad hibernation"},
189 1.13 perry {0xA5, "NetBSD or FreeBSD or 386BSD"},
190 1.13 perry {0xA6, "OpenBSD"},
191 1.4 mycroft {0xA7, "NeXTSTEP 486"},
192 1.4 mycroft {0xB7, "BSDI BSD/386 filesystem"},
193 1.13 perry {0xB8, "BSDI BSD/386 swap"},
194 1.13 perry {0xC1, "DRDOS/sec (FAT-12)"},
195 1.13 perry {0xC4, "DRDOS/sec (FAT-16, < 32M)"},
196 1.13 perry {0xC6, "DRDOS/sec (FAT-16, >= 32M)"},
197 1.13 perry {0xC7, "Syrinx"},
198 1.13 perry {0xDB, "CP/M or Concurrent CP/M or Concurrent DOS or CTOS"},
199 1.13 perry {0xE1, "DOS access or SpeedStor 12-bit FAT extended partition"},
200 1.13 perry {0xE3, "DOS R/O or SpeedStor"},
201 1.13 perry {0xE4, "SpeedStor 16-bit FAT extended partition < 1024 cyl."},
202 1.4 mycroft {0xF1, "SpeedStor"},
203 1.13 perry {0xF2, "DOS 3.3+ Secondary"},
204 1.13 perry {0xF4, "SpeedStor large partition"},
205 1.13 perry {0xFE, "SpeedStor >1024 cyl. or LANstep"},
206 1.1 cgd {0xFF, "Xenix Bad Block Table"},
207 1.1 cgd };
208 1.4 mycroft
209 1.4 mycroft void usage __P((void));
210 1.4 mycroft void print_s0 __P((int));
211 1.4 mycroft void print_part __P((int));
212 1.11 ghudson void init_sector0 __P((int));
213 1.11 ghudson void intuit_translated_geometry __P((void));
214 1.11 ghudson int try_heads __P((quad_t, quad_t, quad_t, quad_t, quad_t, quad_t, quad_t,
215 1.11 ghudson quad_t));
216 1.15 phil int try_sectors __P((quad_t, quad_t, quad_t, quad_t, quad_t));
217 1.4 mycroft void change_part __P((int, int, int, int));
218 1.4 mycroft void print_params __P((void));
219 1.4 mycroft void change_active __P((int));
220 1.4 mycroft void get_params_to_use __P((void));
221 1.4 mycroft void dos __P((int, unsigned char *, unsigned char *, unsigned char *));
222 1.4 mycroft int open_disk __P((int));
223 1.4 mycroft int read_disk __P((int, void *));
224 1.4 mycroft int write_disk __P((int, void *));
225 1.4 mycroft int get_params __P((void));
226 1.4 mycroft int read_s0 __P((void));
227 1.4 mycroft int write_s0 __P((void));
228 1.5 mycroft int yesno __P((char *));
229 1.4 mycroft void decimal __P((char *, int *));
230 1.4 mycroft int type_match __P((const void *, const void *));
231 1.16 phil char *get_type __P((int));
232 1.16 phil int get_mapping __P((int, int *, int *, int *, long *));
233 1.16 phil
234 1.16 phil static inline unsigned short getshort __P((void *));
235 1.16 phil static inline void putshort __P((void *p, unsigned short));
236 1.16 phil static inline unsigned long getlong __P((void *));
237 1.16 phil static inline void putlong __P((void *, unsigned long));
238 1.16 phil
239 1.16 phil
240 1.1 cgd int main __P((int, char **));
241 1.4 mycroft
242 1.1 cgd int
243 1.4 mycroft main(argc, argv)
244 1.4 mycroft int argc;
245 1.1 cgd char *argv[];
246 1.4 mycroft {
247 1.4 mycroft int ch;
248 1.1 cgd int part;
249 1.15 phil
250 1.15 phil int csysid, cstart, csize; /* For the b_flag. */
251 1.16 phil
252 1.15 phil a_flag = i_flag = u_flag = sh_flag = f_flag = s_flag = b_flag = 0;
253 1.16 phil csysid = cstart = csize = 0;
254 1.4 mycroft while ((ch = getopt(argc, argv, "0123Safius:b:")) != -1)
255 1.4 mycroft switch (ch) {
256 1.4 mycroft case '0':
257 1.4 mycroft partition = 0;
258 1.4 mycroft break;
259 1.4 mycroft case '1':
260 1.4 mycroft partition = 1;
261 1.4 mycroft break;
262 1.4 mycroft case '2':
263 1.4 mycroft partition = 2;
264 1.4 mycroft break;
265 1.4 mycroft case '3':
266 1.4 mycroft partition = 3;
267 1.15 phil break;
268 1.15 phil case 'S':
269 1.15 phil sh_flag = 1;
270 1.4 mycroft break;
271 1.4 mycroft case 'a':
272 1.4 mycroft a_flag = 1;
273 1.15 phil break;
274 1.15 phil case 'f':
275 1.15 phil f_flag = 1;
276 1.4 mycroft break;
277 1.4 mycroft case 'i':
278 1.15 phil i_flag = 1;
279 1.4 mycroft break;
280 1.4 mycroft case 'u':
281 1.1 cgd u_flag = 1;
282 1.14 phil break;
283 1.14 phil case 's':
284 1.16 phil s_flag = 1;
285 1.16 phil if (sscanf (optarg, "%d/%d/%d",
286 1.16 phil &csysid, &cstart, &csize) != 3) {
287 1.16 phil (void)fprintf (stderr, "%s: Bad argument "
288 1.16 phil "to the -s flag.\n",
289 1.16 phil argv[0]);
290 1.16 phil exit (1);
291 1.16 phil }
292 1.16 phil break;
293 1.16 phil case 'b':
294 1.16 phil b_flag = 1;
295 1.16 phil if (sscanf (optarg, "%d/%d/%d",
296 1.16 phil &b_cyl, &b_head, &b_sec) != 3) {
297 1.16 phil (void)fprintf (stderr, "%s: Bad argument "
298 1.16 phil "to the -b flag.\n",
299 1.16 phil argv[0]);
300 1.16 phil exit (1);
301 1.14 phil }
302 1.4 mycroft break;
303 1.4 mycroft default:
304 1.1 cgd usage();
305 1.4 mycroft }
306 1.4 mycroft argc -= optind;
307 1.1 cgd argv += optind;
308 1.15 phil
309 1.15 phil if (sh_flag && (a_flag || i_flag || u_flag || f_flag || s_flag))
310 1.15 phil usage();
311 1.15 phil
312 1.15 phil if (partition == -1 && s_flag) {
313 1.15 phil (void) fprintf (stderr,
314 1.14 phil "-s flag requires a partition selected.\n");
315 1.15 phil usage();
316 1.15 phil }
317 1.1 cgd
318 1.1 cgd if (argc > 0)
319 1.4 mycroft disk = argv[0];
320 1.4 mycroft
321 1.1 cgd if (open_disk(a_flag || i_flag || u_flag) < 0)
322 1.1 cgd exit(1);
323 1.11 ghudson
324 1.15 phil if (read_s0())
325 1.11 ghudson init_sector0(sectors > 63 ? 63 : sectors);
326 1.11 ghudson
327 1.11 ghudson intuit_translated_geometry();
328 1.17 phil
329 1.14 phil if (!sh_flag && !f_flag)
330 1.15 phil printf("******* Working on device %s *******\n", disk);
331 1.15 phil
332 1.16 phil
333 1.1 cgd if ((i_flag || u_flag) && (!f_flag || b_flag))
334 1.1 cgd get_params_to_use();
335 1.15 phil
336 1.15 phil if (i_flag)
337 1.15 phil init_sector0(dos_sectors > 63 ? 63 : dos_sectors);
338 1.17 phil
339 1.14 phil if (!sh_flag && !f_flag)
340 1.14 phil printf("Warning: BIOS sector numbering starts with sector 1\n");
341 1.15 phil
342 1.15 phil /* Do the update stuff! */
343 1.15 phil if (u_flag) {
344 1.15 phil if (!f_flag)
345 1.15 phil printf("Information from DOS bootblock is:\n");
346 1.15 phil if (partition == -1)
347 1.15 phil for (part = 0; part < NDOSPART; part++)
348 1.15 phil change_part(part,-1, -1, -1);
349 1.15 phil else
350 1.4 mycroft change_part(partition, csysid, cstart, csize);
351 1.15 phil } else
352 1.15 phil if (!i_flag)
353 1.1 cgd print_s0(partition);
354 1.15 phil
355 1.1 cgd if (a_flag)
356 1.1 cgd change_active(partition);
357 1.15 phil
358 1.15 phil if (u_flag || a_flag || i_flag) {
359 1.15 phil if (!f_flag) {
360 1.15 phil printf("\nWe haven't changed the partition table "
361 1.15 phil "yet. This is your last chance.\n");
362 1.15 phil print_s0(-1);
363 1.15 phil if (yesno("Should we write new partition table?"))
364 1.15 phil write_s0();
365 1.1 cgd } else
366 1.1 cgd write_s0();
367 1.1 cgd }
368 1.1 cgd
369 1.4 mycroft exit(0);
370 1.1 cgd }
371 1.4 mycroft
372 1.4 mycroft void
373 1.4 mycroft usage()
374 1.15 phil {
375 1.16 phil (void)fprintf(stderr, "usage: fdisk [-aiufS] [-0|-1|-2|-3] "
376 1.4 mycroft "[device]\n");
377 1.1 cgd exit(1);
378 1.1 cgd }
379 1.4 mycroft
380 1.1 cgd void
381 1.4 mycroft print_s0(which)
382 1.1 cgd int which;
383 1.4 mycroft {
384 1.1 cgd int part;
385 1.1 cgd
386 1.15 phil print_params();
387 1.15 phil if (!sh_flag)
388 1.4 mycroft printf("Information from DOS bootblock is:\n");
389 1.15 phil if (which == -1) {
390 1.15 phil for (part = 0; part < NDOSPART; part++) {
391 1.15 phil if (!sh_flag)
392 1.15 phil printf("%d: ", part);
393 1.15 phil print_part(part);
394 1.4 mycroft }
395 1.1 cgd } else
396 1.1 cgd print_part(which);
397 1.1 cgd }
398 1.1 cgd
399 1.1 cgd static struct dos_partition mtpart = { 0 };
400 1.12 thorpej
401 1.12 thorpej static inline unsigned short
402 1.12 thorpej getshort(p)
403 1.12 thorpej void *p;
404 1.12 thorpej {
405 1.12 thorpej unsigned char *cp = p;
406 1.12 thorpej
407 1.12 thorpej return cp[0] | (cp[1] << 8);
408 1.12 thorpej }
409 1.12 thorpej
410 1.12 thorpej static inline void
411 1.12 thorpej putshort(p, l)
412 1.12 thorpej void *p;
413 1.12 thorpej unsigned short l;
414 1.12 thorpej {
415 1.12 thorpej unsigned char *cp = p;
416 1.12 thorpej
417 1.12 thorpej *cp++ = l;
418 1.12 thorpej *cp++ = l >> 8;
419 1.12 thorpej }
420 1.12 thorpej
421 1.12 thorpej static inline unsigned long
422 1.12 thorpej getlong(p)
423 1.12 thorpej void *p;
424 1.12 thorpej {
425 1.12 thorpej unsigned char *cp = p;
426 1.12 thorpej
427 1.12 thorpej return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
428 1.12 thorpej }
429 1.12 thorpej
430 1.12 thorpej static inline void
431 1.12 thorpej putlong(p, l)
432 1.12 thorpej void *p;
433 1.12 thorpej unsigned long l;
434 1.12 thorpej {
435 1.12 thorpej unsigned char *cp = p;
436 1.12 thorpej
437 1.12 thorpej *cp++ = l;
438 1.12 thorpej *cp++ = l >> 8;
439 1.12 thorpej *cp++ = l >> 16;
440 1.12 thorpej *cp++ = l >> 24;
441 1.12 thorpej }
442 1.4 mycroft
443 1.4 mycroft void
444 1.4 mycroft print_part(part)
445 1.1 cgd int part;
446 1.4 mycroft {
447 1.14 phil struct dos_partition *partp;
448 1.1 cgd int empty;
449 1.4 mycroft
450 1.14 phil partp = &mboot.parts[part];
451 1.14 phil empty = !memcmp(partp, &mtpart, sizeof(struct dos_partition));
452 1.15 phil
453 1.14 phil if (sh_flag) {
454 1.14 phil if (empty) {
455 1.14 phil printf("PART%dSIZE=0\n", part);
456 1.14 phil return;
457 1.14 phil }
458 1.14 phil
459 1.16 phil printf("PART%dID=%d\n", part, partp->dp_typ);
460 1.16 phil printf("PART%dSIZE=%ld\n", part, getlong(&partp->dp_size));
461 1.18 lukem printf("PART%dSTART=%ld\n", part, getlong(&partp->dp_start));
462 1.14 phil printf("PART%dFLAG=0x%x\n", part, partp->dp_flag);
463 1.14 phil printf("PART%dBCYL=%d\n", part, DPCYL(partp->dp_scyl,
464 1.14 phil partp->dp_ssect));
465 1.14 phil printf("PART%dBHEAD=%d\n", part, partp->dp_shd);
466 1.14 phil printf("PART%dBSEC=%d\n", part, DPSECT(partp->dp_ssect));
467 1.14 phil printf("PART%dECYL=%d\n", part, DPCYL(partp->dp_ecyl,
468 1.14 phil partp->dp_esect));
469 1.14 phil printf("PART%dEHEAD=%d\n", part, partp->dp_ehd);
470 1.14 phil printf("PART%dESEC=%d\n", part, DPSECT(partp->dp_esect));
471 1.14 phil return;
472 1.14 phil }
473 1.15 phil
474 1.14 phil /* Not sh_flag. */
475 1.1 cgd if (empty) {
476 1.1 cgd printf("<UNUSED>\n");
477 1.1 cgd return;
478 1.5 mycroft }
479 1.18 lukem printf("sysid %d (%s)\n", partp->dp_typ, get_type(partp->dp_typ));
480 1.12 thorpej printf(" start %ld, size %ld (%ld MB), flag 0x%x\n",
481 1.12 thorpej getlong(&partp->dp_start), getlong(&partp->dp_size),
482 1.5 mycroft getlong(&partp->dp_size) * 512 / (1024 * 1024), partp->dp_flag);
483 1.5 mycroft printf("\tbeg: cylinder %4d, head %3d, sector %2d\n",
484 1.5 mycroft DPCYL(partp->dp_scyl, partp->dp_ssect),
485 1.5 mycroft partp->dp_shd, DPSECT(partp->dp_ssect));
486 1.5 mycroft printf("\tend: cylinder %4d, head %3d, sector %2d\n",
487 1.5 mycroft DPCYL(partp->dp_ecyl, partp->dp_esect),
488 1.1 cgd partp->dp_ehd, DPSECT(partp->dp_esect));
489 1.1 cgd }
490 1.4 mycroft
491 1.1 cgd void
492 1.4 mycroft init_sector0(start)
493 1.1 cgd int start;
494 1.15 phil {
495 1.4 mycroft int i;
496 1.1 cgd struct dos_partition *partp;
497 1.15 phil
498 1.15 phil int dos_disksectors = dos_cylinders * dos_heads * dos_sectors;
499 1.4 mycroft
500 1.12 thorpej memcpy(mboot.bootinst, bootcode, sizeof(bootcode));
501 1.15 phil putshort(&mboot.signature, BOOT_MAGIC);
502 1.15 phil
503 1.15 phil for (i=0; i<3; i++)
504 1.1 cgd memset (&mboot.parts[i], 0, sizeof(struct dos_partition));
505 1.4 mycroft
506 1.1 cgd partp = &mboot.parts[3];
507 1.1 cgd partp->dp_typ = DOSPTYP_386BSD;
508 1.12 thorpej partp->dp_flag = ACTIVE;
509 1.15 phil putlong(&partp->dp_start, start);
510 1.1 cgd putlong(&partp->dp_size, dos_disksectors - start);
511 1.12 thorpej
512 1.4 mycroft dos(getlong(&partp->dp_start),
513 1.12 thorpej &partp->dp_scyl, &partp->dp_shd, &partp->dp_ssect);
514 1.4 mycroft dos(getlong(&partp->dp_start) + getlong(&partp->dp_size) - 1,
515 1.15 phil &partp->dp_ecyl, &partp->dp_ehd, &partp->dp_esect);
516 1.23 mikel
517 1.23 mikel if (!sh_flag)
518 1.11 ghudson printf ("DOS partition table initialized.\n");
519 1.11 ghudson }
520 1.11 ghudson
521 1.11 ghudson /* Prerequisite: the disklabel parameters and master boot record must
522 1.11 ghudson * have been read (i.e. dos_* and mboot are meaningful).
523 1.11 ghudson * Specification: modifies dos_cylinders, dos_heads, dos_sectors, and
524 1.11 ghudson * dos_cylindersectors to be consistent with what the
525 1.11 ghudson * partition table is using, if we can find a geometry
526 1.11 ghudson * which is consistent with all partition table entries.
527 1.11 ghudson * We may get the number of cylinders slightly wrong (in
528 1.11 ghudson * the conservative direction). The idea is to be able
529 1.11 ghudson * to create a NetBSD partition on a disk we don't know
530 1.11 ghudson * the translated geometry of.
531 1.11 ghudson * This whole routine should be replaced with a kernel interface to get
532 1.11 ghudson * the BIOS geometry (which in turn requires modifications to the i386
533 1.11 ghudson * boot loader to pass in the BIOS geometry for each disk). */
534 1.11 ghudson void
535 1.11 ghudson intuit_translated_geometry()
536 1.15 phil {
537 1.11 ghudson
538 1.11 ghudson int cylinders = -1, heads = -1, sectors = -1, i, j;
539 1.11 ghudson int c1, h1, s1, c2, h2, s2;
540 1.11 ghudson long a1, a2;
541 1.11 ghudson quad_t num, denom;
542 1.11 ghudson
543 1.11 ghudson /* Try to deduce the number of heads from two different mappings. */
544 1.11 ghudson for (i = 0; i < NDOSPART * 2; i++) {
545 1.11 ghudson if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
546 1.11 ghudson continue;
547 1.11 ghudson for (j = 0; j < 8; j++) {
548 1.11 ghudson if (get_mapping(j, &c2, &h2, &s2, &a2) < 0)
549 1.22 enami continue;
550 1.22 enami num = (quad_t)h1*(a2-s2) - (quad_t)h2*(a1-s1);
551 1.11 ghudson denom = (quad_t)c2*(a1-s1) - (quad_t)c1*(a2-s2);
552 1.11 ghudson if (denom != 0 && num % denom == 0) {
553 1.11 ghudson heads = num / denom;
554 1.11 ghudson break;
555 1.11 ghudson }
556 1.11 ghudson }
557 1.11 ghudson if (heads != -1)
558 1.11 ghudson break;
559 1.11 ghudson }
560 1.11 ghudson
561 1.11 ghudson if (heads == -1)
562 1.11 ghudson return;
563 1.11 ghudson
564 1.11 ghudson /* Now figure out the number of sectors from a single mapping. */
565 1.11 ghudson for (i = 0; i < NDOSPART * 2; i++) {
566 1.11 ghudson if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
567 1.11 ghudson continue;
568 1.11 ghudson num = a1 - s1;
569 1.11 ghudson denom = c1 * heads + h1;
570 1.11 ghudson if (denom != 0 && num % denom == 0) {
571 1.11 ghudson sectors = num / denom;
572 1.11 ghudson break;
573 1.11 ghudson }
574 1.11 ghudson }
575 1.11 ghudson
576 1.11 ghudson if (sectors == -1)
577 1.11 ghudson return;
578 1.11 ghudson
579 1.11 ghudson /* Estimate the number of cylinders. */
580 1.11 ghudson cylinders = dos_cylinders * dos_cylindersectors / heads / sectors;
581 1.11 ghudson
582 1.11 ghudson /* Now verify consistency with each of the partition table entries.
583 1.11 ghudson * Be willing to shove cylinders up a little bit to make things work,
584 1.11 ghudson * but translation mismatches are fatal. */
585 1.11 ghudson for (i = 0; i < NDOSPART * 2; i++) {
586 1.11 ghudson if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
587 1.11 ghudson continue;
588 1.11 ghudson if (sectors * (c1 * heads + h1) + s1 != a1)
589 1.11 ghudson return;
590 1.11 ghudson if (c1 >= cylinders)
591 1.11 ghudson cylinders = c1 + 1;
592 1.11 ghudson }
593 1.11 ghudson
594 1.11 ghudson /* Everything checks out. Reset the geometry to use for further
595 1.11 ghudson * calculations. */
596 1.11 ghudson dos_cylinders = cylinders;
597 1.11 ghudson dos_heads = heads;
598 1.11 ghudson dos_sectors = sectors;
599 1.11 ghudson dos_cylindersectors = heads * sectors;
600 1.11 ghudson }
601 1.11 ghudson
602 1.11 ghudson /* For the purposes of intuit_translated_geometry(), treat the partition
603 1.11 ghudson * table as a list of eight mapping between (cylinder, head, sector)
604 1.11 ghudson * triplets and absolute sectors. Get the relevant geometry triplet and
605 1.11 ghudson * absolute sectors for a given entry, or return -1 if it isn't present.
606 1.11 ghudson * Note: for simplicity, the returned sector is 0-based. */
607 1.11 ghudson int
608 1.11 ghudson get_mapping(i, cylinder, head, sector, absolute)
609 1.11 ghudson int i, *cylinder, *head, *sector;
610 1.11 ghudson long *absolute;
611 1.11 ghudson {
612 1.11 ghudson struct dos_partition *part = &mboot.parts[i / 2];
613 1.11 ghudson
614 1.11 ghudson if (part->dp_typ == 0)
615 1.11 ghudson return -1;
616 1.11 ghudson if (i % 2 == 0) {
617 1.11 ghudson *cylinder = DPCYL(part->dp_scyl, part->dp_ssect);
618 1.11 ghudson *head = part->dp_shd;
619 1.12 thorpej *sector = DPSECT(part->dp_ssect) - 1;
620 1.11 ghudson *absolute = getlong(&part->dp_start);
621 1.11 ghudson } else {
622 1.11 ghudson *cylinder = DPCYL(part->dp_ecyl, part->dp_esect);
623 1.11 ghudson *head = part->dp_ehd;
624 1.12 thorpej *sector = DPSECT(part->dp_esect) - 1;
625 1.12 thorpej *absolute = getlong(&part->dp_start)
626 1.11 ghudson + getlong(&part->dp_size) - 1;
627 1.11 ghudson }
628 1.1 cgd return 0;
629 1.1 cgd }
630 1.4 mycroft
631 1.15 phil void
632 1.15 phil change_part(part, csysid, cstart, csize)
633 1.1 cgd int part, csysid, cstart, csize;
634 1.4 mycroft {
635 1.1 cgd struct dos_partition *partp;
636 1.4 mycroft
637 1.1 cgd partp = &mboot.parts[part];
638 1.15 phil
639 1.15 phil if (s_flag) {
640 1.15 phil partp->dp_typ = csysid;
641 1.15 phil putlong(&partp->dp_start, cstart);
642 1.15 phil putlong(&partp->dp_size, csize);
643 1.15 phil dos(getlong(&partp->dp_start),
644 1.15 phil &partp->dp_scyl, &partp->dp_shd, &partp->dp_ssect);
645 1.15 phil dos(getlong(&partp->dp_start)
646 1.15 phil + getlong(&partp->dp_size) - 1,
647 1.15 phil &partp->dp_ecyl, &partp->dp_ehd, &partp->dp_esect);
648 1.15 phil if (f_flag)
649 1.15 phil return;
650 1.15 phil }
651 1.15 phil
652 1.4 mycroft printf("The data for partition %d is:\n", part);
653 1.4 mycroft print_part(part);
654 1.4 mycroft if (!u_flag || !yesno("Do you want to change it?"))
655 1.1 cgd return;
656 1.1 cgd
657 1.5 mycroft do {
658 1.5 mycroft {
659 1.5 mycroft int sysid, start, size;
660 1.5 mycroft
661 1.12 thorpej sysid = partp->dp_typ,
662 1.12 thorpej start = getlong(&partp->dp_start),
663 1.5 mycroft size = getlong(&partp->dp_size);
664 1.5 mycroft decimal("sysid", &sysid);
665 1.5 mycroft decimal("start", &start);
666 1.5 mycroft decimal("size", &size);
667 1.12 thorpej partp->dp_typ = sysid;
668 1.12 thorpej putlong(&partp->dp_start, start);
669 1.5 mycroft putlong(&partp->dp_size, size);
670 1.1 cgd }
671 1.7 mycroft
672 1.5 mycroft if (yesno("Explicitly specify beg/end address?")) {
673 1.4 mycroft int tsector, tcylinder, thead;
674 1.5 mycroft
675 1.5 mycroft tcylinder = DPCYL(partp->dp_scyl, partp->dp_ssect);
676 1.5 mycroft thead = partp->dp_shd;
677 1.5 mycroft tsector = DPSECT(partp->dp_ssect);
678 1.5 mycroft decimal("beginning cylinder", &tcylinder);
679 1.5 mycroft decimal("beginning head", &thead);
680 1.5 mycroft decimal("beginning sector", &tsector);
681 1.5 mycroft partp->dp_scyl = DOSCYL(tcylinder);
682 1.5 mycroft partp->dp_shd = thead;
683 1.5 mycroft partp->dp_ssect = DOSSECT(tsector, tcylinder);
684 1.5 mycroft
685 1.5 mycroft tcylinder = DPCYL(partp->dp_ecyl, partp->dp_esect);
686 1.5 mycroft thead = partp->dp_ehd;
687 1.5 mycroft tsector = DPSECT(partp->dp_esect);
688 1.5 mycroft decimal("ending cylinder", &tcylinder);
689 1.5 mycroft decimal("ending head", &thead);
690 1.5 mycroft decimal("ending sector", &tsector);
691 1.5 mycroft partp->dp_ecyl = DOSCYL(tcylinder);
692 1.5 mycroft partp->dp_ehd = thead;
693 1.1 cgd partp->dp_esect = DOSSECT(tsector, tcylinder);
694 1.12 thorpej } else {
695 1.4 mycroft dos(getlong(&partp->dp_start),
696 1.12 thorpej &partp->dp_scyl, &partp->dp_shd, &partp->dp_ssect);
697 1.12 thorpej dos(getlong(&partp->dp_start)
698 1.4 mycroft + getlong(&partp->dp_size) - 1,
699 1.1 cgd &partp->dp_ecyl, &partp->dp_ehd, &partp->dp_esect);
700 1.4 mycroft }
701 1.4 mycroft
702 1.7 mycroft print_part(part);
703 1.1 cgd } while (!yesno("Is this entry okay?"));
704 1.1 cgd }
705 1.4 mycroft
706 1.1 cgd void
707 1.1 cgd print_params()
708 1.4 mycroft {
709 1.15 phil
710 1.14 phil if (sh_flag) {
711 1.14 phil printf ("DLCYL=%d\nDLHEAD=%d\nDLSEC=%d\n",
712 1.14 phil cylinders, heads, sectors);
713 1.14 phil printf ("BCYL=%d\nBHEAD=%d\nBSEC=%d\n",
714 1.14 phil dos_cylinders, dos_heads, dos_sectors);
715 1.14 phil return;
716 1.14 phil }
717 1.15 phil
718 1.1 cgd /* Not sh_flag */
719 1.5 mycroft printf("parameters extracted from in-core disklabel are:\n");
720 1.5 mycroft printf("cylinders=%d heads=%d sectors/track=%d (%d sectors/cylinder)\n\n",
721 1.5 mycroft cylinders, heads, sectors, cylindersectors);
722 1.5 mycroft if (dos_sectors > 63 || dos_cylinders > 1023 || dos_heads > 255)
723 1.1 cgd printf("Figures below won't work with BIOS for partitions not in cylinder 1\n");
724 1.5 mycroft printf("parameters to be used for BIOS calculations are:\n");
725 1.5 mycroft printf("cylinders=%d heads=%d sectors/track=%d (%d sectors/cylinder)\n\n",
726 1.1 cgd dos_cylinders, dos_heads, dos_sectors, dos_cylindersectors);
727 1.1 cgd }
728 1.4 mycroft
729 1.1 cgd void
730 1.4 mycroft change_active(which)
731 1.1 cgd int which;
732 1.4 mycroft {
733 1.4 mycroft struct dos_partition *partp;
734 1.15 phil int part;
735 1.4 mycroft int active = 4;
736 1.4 mycroft
737 1.1 cgd partp = &mboot.parts[0];
738 1.1 cgd
739 1.1 cgd if (a_flag && which != -1)
740 1.4 mycroft active = which;
741 1.4 mycroft else {
742 1.4 mycroft for (part = 0; part < NDOSPART; part++)
743 1.4 mycroft if (partp[part].dp_flag & ACTIVE)
744 1.4 mycroft active = part;
745 1.15 phil }
746 1.15 phil if (!f_flag) {
747 1.15 phil if (yesno("Do you want to change the active partition?")) {
748 1.15 phil printf ("Choosing 4 will make no partition active.\n");
749 1.15 phil do {
750 1.15 phil decimal("active partition", &active);
751 1.15 phil } while (!yesno("Are you happy with this choice?"));
752 1.15 phil } else
753 1.15 phil return;
754 1.15 phil } else
755 1.15 phil if (active != 4)
756 1.15 phil printf ("Making partition %d active.\n", active);
757 1.4 mycroft
758 1.4 mycroft for (part = 0; part < NDOSPART; part++)
759 1.15 phil partp[part].dp_flag &= ~ACTIVE;
760 1.15 phil if (active < 4)
761 1.1 cgd partp[active].dp_flag |= ACTIVE;
762 1.1 cgd }
763 1.4 mycroft
764 1.1 cgd void
765 1.1 cgd get_params_to_use()
766 1.16 phil {
767 1.16 phil if (b_flag) {
768 1.16 phil dos_cylinders = b_cyl;
769 1.16 phil dos_heads = b_head;
770 1.16 phil dos_sectors = b_sec;
771 1.16 phil dos_cylindersectors = dos_heads * dos_sectors;
772 1.16 phil return;
773 1.4 mycroft }
774 1.1 cgd
775 1.4 mycroft print_params();
776 1.4 mycroft if (yesno("Do you want to change our idea of what BIOS thinks?")) {
777 1.5 mycroft do {
778 1.5 mycroft decimal("BIOS's idea of #cylinders", &dos_cylinders);
779 1.5 mycroft decimal("BIOS's idea of #heads", &dos_heads);
780 1.5 mycroft decimal("BIOS's idea of #sectors", &dos_sectors);
781 1.1 cgd dos_cylindersectors = dos_heads * dos_sectors;
782 1.4 mycroft print_params();
783 1.1 cgd } while (!yesno("Are you happy with this choice?"));
784 1.1 cgd }
785 1.1 cgd }
786 1.1 cgd
787 1.1 cgd /***********************************************\
788 1.1 cgd * Change real numbers into strange dos numbers *
789 1.4 mycroft \***********************************************/
790 1.5 mycroft void
791 1.5 mycroft dos(sector, cylinderp, headp, sectorp)
792 1.5 mycroft int sector;
793 1.4 mycroft unsigned char *cylinderp, *headp, *sectorp;
794 1.5 mycroft {
795 1.4 mycroft int cylinder, head;
796 1.5 mycroft
797 1.5 mycroft cylinder = sector / dos_cylindersectors;
798 1.4 mycroft sector -= cylinder * dos_cylindersectors;
799 1.5 mycroft
800 1.5 mycroft head = sector / dos_sectors;
801 1.4 mycroft sector -= head * dos_sectors;
802 1.5 mycroft
803 1.5 mycroft *cylinderp = DOSCYL(cylinder);
804 1.5 mycroft *headp = head;
805 1.1 cgd *sectorp = DOSSECT(sector + 1, cylinder);
806 1.1 cgd }
807 1.1 cgd
808 1.1 cgd int fd;
809 1.4 mycroft
810 1.1 cgd int
811 1.4 mycroft open_disk(u_flag)
812 1.1 cgd int u_flag;
813 1.18 lukem {
814 1.4 mycroft static char namebuf[MAXPATHLEN + 1];
815 1.1 cgd struct stat st;
816 1.19 lukem
817 1.19 lukem fd = opendisk(disk, u_flag ? O_RDWR : O_RDONLY, namebuf,
818 1.18 lukem sizeof(namebuf), 0);
819 1.18 lukem if (fd < 0) {
820 1.4 mycroft warn("%s", namebuf);
821 1.4 mycroft return (-1);
822 1.18 lukem }
823 1.4 mycroft disk = namebuf;
824 1.4 mycroft if (fstat(fd, &st) == -1) {
825 1.4 mycroft close(fd);
826 1.4 mycroft warn("%s", disk);
827 1.1 cgd return (-1);
828 1.7 mycroft }
829 1.4 mycroft if (!S_ISCHR(st.st_mode) && !S_ISREG(st.st_mode)) {
830 1.7 mycroft close(fd);
831 1.4 mycroft warnx("%s is not a character device or regular file", disk);
832 1.1 cgd return (-1);
833 1.4 mycroft }
834 1.4 mycroft if (get_params() == -1) {
835 1.4 mycroft close(fd);
836 1.1 cgd return (-1);
837 1.4 mycroft }
838 1.1 cgd return (0);
839 1.1 cgd }
840 1.4 mycroft
841 1.1 cgd int
842 1.4 mycroft read_disk(sector, buf)
843 1.4 mycroft int sector;
844 1.1 cgd void *buf;
845 1.4 mycroft {
846 1.4 mycroft
847 1.4 mycroft if (lseek(fd, (off_t)(sector * 512), 0) == -1)
848 1.4 mycroft return (-1);
849 1.1 cgd return (read(fd, buf, 512));
850 1.1 cgd }
851 1.4 mycroft
852 1.1 cgd int
853 1.4 mycroft write_disk(sector, buf)
854 1.4 mycroft int sector;
855 1.1 cgd void *buf;
856 1.4 mycroft {
857 1.4 mycroft
858 1.4 mycroft if (lseek(fd, (off_t)(sector * 512), 0) == -1)
859 1.4 mycroft return (-1);
860 1.1 cgd return (write(fd, buf, 512));
861 1.1 cgd }
862 1.4 mycroft
863 1.4 mycroft int
864 1.1 cgd get_params()
865 1.1 cgd {
866 1.4 mycroft
867 1.4 mycroft if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
868 1.4 mycroft warn("DIOCGDINFO");
869 1.4 mycroft return (-1);
870 1.1 cgd }
871 1.5 mycroft
872 1.4 mycroft dos_cylinders = cylinders = disklabel.d_ncylinders;
873 1.4 mycroft dos_heads = heads = disklabel.d_ntracks;
874 1.5 mycroft dos_sectors = sectors = disklabel.d_nsectors;
875 1.5 mycroft dos_cylindersectors = cylindersectors = heads * sectors;
876 1.1 cgd disksectors = cylinders * heads * sectors;
877 1.4 mycroft
878 1.1 cgd return (0);
879 1.1 cgd }
880 1.4 mycroft
881 1.1 cgd int
882 1.1 cgd read_s0()
883 1.4 mycroft {
884 1.4 mycroft
885 1.4 mycroft if (read_disk(0, mboot.bootinst) == -1) {
886 1.4 mycroft warn("can't read fdisk partition table");
887 1.1 cgd return (-1);
888 1.12 thorpej }
889 1.12 thorpej if (getshort(&mboot.signature) != BOOT_MAGIC) {
890 1.4 mycroft warnx("invalid fdisk partition table found");
891 1.4 mycroft /* So should we initialize things? */
892 1.1 cgd return (-1);
893 1.4 mycroft }
894 1.1 cgd return (0);
895 1.1 cgd }
896 1.4 mycroft
897 1.1 cgd int
898 1.1 cgd write_s0()
899 1.4 mycroft {
900 1.4 mycroft int flag;
901 1.1 cgd
902 1.1 cgd /*
903 1.1 cgd * write enable label sector before write (if necessary),
904 1.1 cgd * disable after writing.
905 1.1 cgd * needed if the disklabel protected area also protects
906 1.1 cgd * sector 0. (e.g. empty disk)
907 1.1 cgd */
908 1.1 cgd flag = 1;
909 1.4 mycroft if (ioctl(fd, DIOCWLABEL, &flag) < 0)
910 1.4 mycroft warn("DIOCWLABEL");
911 1.4 mycroft if (write_disk(0, mboot.bootinst) == -1) {
912 1.1 cgd warn("can't write fdisk partition table");
913 1.4 mycroft return -1;
914 1.1 cgd }
915 1.4 mycroft flag = 0;
916 1.4 mycroft if (ioctl(fd, DIOCWLABEL, &flag) < 0)
917 1.16 phil warn("DIOCWLABEL");
918 1.1 cgd return 0;
919 1.1 cgd }
920 1.4 mycroft
921 1.4 mycroft int
922 1.4 mycroft yesno(str)
923 1.4 mycroft char *str;
924 1.4 mycroft {
925 1.1 cgd int ch, first;
926 1.1 cgd
927 1.1 cgd printf("%s [n] ", str);
928 1.4 mycroft
929 1.4 mycroft first = ch = getchar();
930 1.4 mycroft while (ch != '\n' && ch != EOF)
931 1.4 mycroft ch = getchar();
932 1.1 cgd return (first == 'y' || first == 'Y');
933 1.1 cgd }
934 1.5 mycroft
935 1.5 mycroft void
936 1.4 mycroft decimal(str, num)
937 1.5 mycroft char *str;
938 1.1 cgd int *num;
939 1.8 cgd {
940 1.4 mycroft int acc = 0;
941 1.1 cgd char *cp;
942 1.5 mycroft
943 1.5 mycroft for (;; printf("%s is not a valid decimal number.\n", lbuf)) {
944 1.1 cgd printf("Supply a decimal value for \"%s\" [%d] ", str, *num);
945 1.1 cgd
946 1.7 mycroft fgets(lbuf, LBUF, stdin);
947 1.7 mycroft lbuf[strlen(lbuf)-1] = '\0';
948 1.7 mycroft cp = lbuf;
949 1.7 mycroft
950 1.7 mycroft cp += strspn(cp, " \t");
951 1.5 mycroft if (*cp == '\0')
952 1.1 cgd return;
953 1.7 mycroft
954 1.7 mycroft if (!isdigit(*cp))
955 1.5 mycroft continue;
956 1.7 mycroft acc = strtol(lbuf, &cp, 10);
957 1.5 mycroft
958 1.5 mycroft cp += strspn(cp, " \t");
959 1.5 mycroft if (*cp != '\0')
960 1.1 cgd continue;
961 1.5 mycroft
962 1.5 mycroft *num = acc;
963 1.1 cgd return;
964 1.1 cgd }
965 1.1 cgd
966 1.1 cgd }
967 1.4 mycroft
968 1.4 mycroft int
969 1.4 mycroft type_match(key, item)
970 1.4 mycroft const void *key, *item;
971 1.4 mycroft {
972 1.4 mycroft const int *typep = key;
973 1.4 mycroft const struct part_type *ptr = item;
974 1.4 mycroft
975 1.4 mycroft if (*typep < ptr->type)
976 1.4 mycroft return (-1);
977 1.4 mycroft if (*typep > ptr->type)
978 1.4 mycroft return (1);
979 1.4 mycroft return (0);
980 1.4 mycroft }
981 1.4 mycroft
982 1.4 mycroft char *
983 1.4 mycroft get_type(type)
984 1.4 mycroft int type;
985 1.4 mycroft {
986 1.4 mycroft struct part_type *ptr;
987 1.4 mycroft
988 1.4 mycroft ptr = bsearch(&type, part_types,
989 1.4 mycroft sizeof(part_types) / sizeof(struct part_type),
990 1.4 mycroft sizeof(struct part_type), type_match);
991 1.4 mycroft if (ptr == 0)
992 1.16 phil return ("unknown");
993 1.1 cgd return (ptr->name);
994 }
995