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