1fca180e3SMatthew Dillon /*
2fca180e3SMatthew Dillon * Mach Operating System
3fca180e3SMatthew Dillon * Copyright (c) 1992 Carnegie Mellon University
4fca180e3SMatthew Dillon * All Rights Reserved.
5fca180e3SMatthew Dillon *
6fca180e3SMatthew Dillon * Permission to use, copy, modify and distribute this software and its
7fca180e3SMatthew Dillon * documentation is hereby granted, provided that both the copyright
8fca180e3SMatthew Dillon * notice and this permission notice appear in all copies of the
9fca180e3SMatthew Dillon * software, derivative works or modified versions, and any portions
10fca180e3SMatthew Dillon * thereof, and that both notices appear in supporting documentation.
11fca180e3SMatthew Dillon *
12fca180e3SMatthew Dillon * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13fca180e3SMatthew Dillon * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14fca180e3SMatthew Dillon * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15fca180e3SMatthew Dillon *
16fca180e3SMatthew Dillon * Carnegie Mellon requests users of this software to return to
17fca180e3SMatthew Dillon *
18fca180e3SMatthew Dillon * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19fca180e3SMatthew Dillon * School of Computer Science
20fca180e3SMatthew Dillon * Carnegie Mellon University
21fca180e3SMatthew Dillon * Pittsburgh PA 15213-3890
22fca180e3SMatthew Dillon *
23fca180e3SMatthew Dillon * any improvements or extensions that they make and grant Carnegie Mellon
24fca180e3SMatthew Dillon * the rights to redistribute these changes.
25fca180e3SMatthew Dillon *
26fca180e3SMatthew Dillon * $FreeBSD: /repoman/r/ncvs/src/sbin/i386/fdisk/fdisk.c,v 1.36.2.14 2004/01/30 14:40:47 harti Exp $
27fca180e3SMatthew Dillon */
28fca180e3SMatthew Dillon
29a77a893aSSascha Wildner #include <sys/param.h>
30fca180e3SMatthew Dillon #include <sys/diskslice.h>
31fca180e3SMatthew Dillon #include <sys/diskmbr.h>
32e0fb398bSTim #include <sys/sysctl.h>
33fca180e3SMatthew Dillon #include <sys/stat.h>
34fca180e3SMatthew Dillon #include <ctype.h>
35fca180e3SMatthew Dillon #include <fcntl.h>
36fca180e3SMatthew Dillon #include <err.h>
37fca180e3SMatthew Dillon #include <fstab.h>
38fca180e3SMatthew Dillon #include <errno.h>
39fca180e3SMatthew Dillon #include <stdio.h>
40fca180e3SMatthew Dillon #include <stdlib.h>
41fca180e3SMatthew Dillon #include <string.h>
42fca180e3SMatthew Dillon #include <unistd.h>
43fca180e3SMatthew Dillon
4432506cfaSSascha Wildner #include <bus/cam/scsi/scsi_daio.h>
4532506cfaSSascha Wildner
46fca180e3SMatthew Dillon #define LBUF 100
47fca180e3SMatthew Dillon static char lbuf[LBUF];
48fca180e3SMatthew Dillon
49fca180e3SMatthew Dillon /*
50fca180e3SMatthew Dillon *
51fca180e3SMatthew Dillon * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992
52fca180e3SMatthew Dillon *
53fca180e3SMatthew Dillon * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
54fca180e3SMatthew Dillon * Copyright (c) 1989 Robert. V. Baron
55fca180e3SMatthew Dillon * Created.
56fca180e3SMatthew Dillon */
57fca180e3SMatthew Dillon
58fca180e3SMatthew Dillon #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
59fca180e3SMatthew Dillon #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
60fca180e3SMatthew Dillon #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
616a0b5de0SAlexander Kuleshov #define MAX_SECTORS_PER_TRACK 0x3f /* maximum number of sectors per track */
626a0b5de0SAlexander Kuleshov #define MIN_SECTORS_PER_TRACK 0x1 /* minimum number of sectors per track */
636a0b5de0SAlexander Kuleshov #define MAX_HEADS 0xff /* maximum number of head */
646a0b5de0SAlexander Kuleshov static int secsize = 0; /* the sensed sector size */
65fca180e3SMatthew Dillon
666a0b5de0SAlexander Kuleshov static int fd; /* file descriptor of the given disk */
676a0b5de0SAlexander Kuleshov static const char *disk;
686a0b5de0SAlexander Kuleshov static const char *disks[] =
69fca180e3SMatthew Dillon {
70fca180e3SMatthew Dillon "/dev/ad0", "/dev/da0", "/dev/vkd0", 0
71fca180e3SMatthew Dillon };
72fca180e3SMatthew Dillon
736a0b5de0SAlexander Kuleshov static int cyls, sectors, heads, cylsecs;
746a0b5de0SAlexander Kuleshov static int64_t disksecs;
75fca180e3SMatthew Dillon
76fca180e3SMatthew Dillon struct mboot
77fca180e3SMatthew Dillon {
78fca180e3SMatthew Dillon unsigned char padding[2]; /* force the longs to be long aligned */
79fca180e3SMatthew Dillon unsigned char *bootinst; /* boot code */
80fca180e3SMatthew Dillon off_t bootinst_size;
81073d5c5bSAlexander Kuleshov struct dos_partition parts[NDOSPART];
82fca180e3SMatthew Dillon };
836a0b5de0SAlexander Kuleshov static struct mboot mboot;
84fca180e3SMatthew Dillon
85fca180e3SMatthew Dillon #define ACTIVE 0x80
86fca180e3SMatthew Dillon
87fca180e3SMatthew Dillon int dos_cyls;
88fca180e3SMatthew Dillon int dos_heads;
89fca180e3SMatthew Dillon int dos_sectors;
90fca180e3SMatthew Dillon int dos_cylsecs;
91fca180e3SMatthew Dillon
926a0b5de0SAlexander Kuleshov #define DOSSECT(s,c) ((s & MAX_SECTORS_PER_TRACK) | ((c >> 2) & 0xc0))
93fca180e3SMatthew Dillon #define DOSCYL(c) (c & 0xff)
94fca180e3SMatthew Dillon #define MAXCYL 1023
95fca180e3SMatthew Dillon static int partition = -1;
96fca180e3SMatthew Dillon
97fca180e3SMatthew Dillon #define MAX_ARGS 10
98fca180e3SMatthew Dillon static int current_line_number;
99fca180e3SMatthew Dillon
100fca180e3SMatthew Dillon static int geom_processed = 0;
101fca180e3SMatthew Dillon static int part_processed = 0;
102fca180e3SMatthew Dillon static int active_processed = 0;
103fca180e3SMatthew Dillon
104fca180e3SMatthew Dillon typedef struct cmd {
105fca180e3SMatthew Dillon char cmd;
106fca180e3SMatthew Dillon int n_args;
107fca180e3SMatthew Dillon struct arg {
108fca180e3SMatthew Dillon char argtype;
10982675d28SSascha Wildner long long arg_val;
110fca180e3SMatthew Dillon } args[MAX_ARGS];
111fca180e3SMatthew Dillon } CMD;
112fca180e3SMatthew Dillon
113fca180e3SMatthew Dillon static int B_flag = 0; /* replace boot code */
114fca180e3SMatthew Dillon static int C_flag = 0; /* use wrapped values for CHS */
115e0fb398bSTim static int E_flag = 0; /* Erase through TRIM */
116fca180e3SMatthew Dillon static int I_flag = 0; /* use entire disk for DragonFly */
117fca180e3SMatthew Dillon static int a_flag = 0; /* set active partition */
118fca180e3SMatthew Dillon static char *b_flag = NULL; /* path to boot code */
119fca180e3SMatthew Dillon static int i_flag = 0; /* replace partition data */
120fca180e3SMatthew Dillon static int u_flag = 0; /* update partition data */
121fca180e3SMatthew Dillon static int p_flag = 0; /* operate on a disk image file */
122fca180e3SMatthew Dillon static int s_flag = 0; /* Print a summary and exit */
123fca180e3SMatthew Dillon static int t_flag = 0; /* test only */
1240b920486SMatthew Dillon static int x_flag = 0; /* Expand-to-fit device */
125fca180e3SMatthew Dillon static char *f_flag = NULL; /* Read config info from file */
126fca180e3SMatthew Dillon static int v_flag = 0; /* Be verbose */
127fca180e3SMatthew Dillon
128fca180e3SMatthew Dillon static void print_s0(int which);
129fca180e3SMatthew Dillon static void print_part(int i);
130fca180e3SMatthew Dillon static void init_sector0(unsigned long start);
131fca180e3SMatthew Dillon static void init_boot(void);
132fca180e3SMatthew Dillon static void change_part(int i);
1331b0b5697SSascha Wildner static void print_params(void);
134fca180e3SMatthew Dillon static void change_active(int which);
1351b0b5697SSascha Wildner static void change_code(void);
1361b0b5697SSascha Wildner static void get_params_to_use(void);
137fca180e3SMatthew Dillon static void dos(struct dos_partition *partp);
1381b0b5697SSascha Wildner static int open_disk(void);
139e0fb398bSTim static void erase_partition(int i);
140fca180e3SMatthew Dillon static ssize_t read_disk(off_t sector, void *buf);
141fca180e3SMatthew Dillon static ssize_t write_disk(off_t sector, void *buf);
1421b0b5697SSascha Wildner static int get_params(void);
1431b0b5697SSascha Wildner static int read_s0(void);
1441b0b5697SSascha Wildner static int write_s0(void);
1451b0b5697SSascha Wildner static int ok(const char *str);
1461b0b5697SSascha Wildner static int decimal(const char *str, int *num, int deflt);
1471b0b5697SSascha Wildner static const char *get_type(int type);
148fca180e3SMatthew Dillon static int read_config(char *config_file);
149fca180e3SMatthew Dillon static void reset_boot(void);
150fca180e3SMatthew Dillon static int sanitize_partition(struct dos_partition *);
151fca180e3SMatthew Dillon static void usage(void);
1520b920486SMatthew Dillon static int expand_table(void);
153fca180e3SMatthew Dillon
154fca180e3SMatthew Dillon int
main(int argc,char * argv[])155fca180e3SMatthew Dillon main(int argc, char *argv[])
156fca180e3SMatthew Dillon {
157fca180e3SMatthew Dillon int c, i;
158fca180e3SMatthew Dillon
1590b920486SMatthew Dillon while ((c = getopt(argc, argv, "BCEIab:f:p:istuvx1234")) != -1)
160fca180e3SMatthew Dillon switch (c) {
161fca180e3SMatthew Dillon case 'B':
162fca180e3SMatthew Dillon B_flag = 1;
163fca180e3SMatthew Dillon break;
164fca180e3SMatthew Dillon case 'C':
165fca180e3SMatthew Dillon C_flag = 1;
166fca180e3SMatthew Dillon break;
167e0fb398bSTim case 'E':
168e0fb398bSTim E_flag = 1;
169e0fb398bSTim break;
170fca180e3SMatthew Dillon case 'I':
171fca180e3SMatthew Dillon I_flag = 1;
172fca180e3SMatthew Dillon break;
173fca180e3SMatthew Dillon case 'a':
174fca180e3SMatthew Dillon a_flag = 1;
175fca180e3SMatthew Dillon break;
176fca180e3SMatthew Dillon case 'b':
177fca180e3SMatthew Dillon b_flag = optarg;
178fca180e3SMatthew Dillon break;
179fca180e3SMatthew Dillon case 'f':
180fca180e3SMatthew Dillon f_flag = optarg;
181fca180e3SMatthew Dillon break;
182fca180e3SMatthew Dillon case 'p':
183fca180e3SMatthew Dillon disk = optarg;
184fca180e3SMatthew Dillon p_flag = 1;
185fca180e3SMatthew Dillon break;
186fca180e3SMatthew Dillon case 'i':
187fca180e3SMatthew Dillon i_flag = 1;
188fca180e3SMatthew Dillon break;
189fca180e3SMatthew Dillon case 's':
190fca180e3SMatthew Dillon s_flag = 1;
191fca180e3SMatthew Dillon break;
192fca180e3SMatthew Dillon case 't':
193fca180e3SMatthew Dillon t_flag = 1;
194fca180e3SMatthew Dillon break;
195fca180e3SMatthew Dillon case 'u':
196fca180e3SMatthew Dillon u_flag = 1;
197fca180e3SMatthew Dillon break;
198fca180e3SMatthew Dillon case 'v':
199fca180e3SMatthew Dillon v_flag = 1;
200fca180e3SMatthew Dillon break;
2010b920486SMatthew Dillon case 'x':
2020b920486SMatthew Dillon ++x_flag;
2030b920486SMatthew Dillon break;
204fca180e3SMatthew Dillon case '1':
205fca180e3SMatthew Dillon case '2':
206fca180e3SMatthew Dillon case '3':
207fca180e3SMatthew Dillon case '4':
208fca180e3SMatthew Dillon partition = c - '0';
209fca180e3SMatthew Dillon break;
210fca180e3SMatthew Dillon default:
211fca180e3SMatthew Dillon usage();
212fca180e3SMatthew Dillon }
213fca180e3SMatthew Dillon if (f_flag || i_flag)
214fca180e3SMatthew Dillon u_flag = 1;
215fca180e3SMatthew Dillon if (t_flag)
216fca180e3SMatthew Dillon v_flag = 1;
217fca180e3SMatthew Dillon argc -= optind;
218fca180e3SMatthew Dillon argv += optind;
219fca180e3SMatthew Dillon
220fca180e3SMatthew Dillon if (argc > 0) {
221fca180e3SMatthew Dillon disk = getdevpath(argv[0], 0);
22201415650SAlexander Kuleshov if (!disk)
22301415650SAlexander Kuleshov err(1, "cannot open disk %s", disk);
2241b0b5697SSascha Wildner if (open_disk() < 0)
225fca180e3SMatthew Dillon err(1, "cannot open disk %s", disk);
226fca180e3SMatthew Dillon } else if (disk == NULL) {
227fca180e3SMatthew Dillon int rv = 0;
228fca180e3SMatthew Dillon
229fca180e3SMatthew Dillon for(i = 0; disks[i]; i++)
230fca180e3SMatthew Dillon {
231fca180e3SMatthew Dillon disk = disks[i];
2321b0b5697SSascha Wildner rv = open_disk();
233fca180e3SMatthew Dillon if (rv != -2) break;
234fca180e3SMatthew Dillon }
235fca180e3SMatthew Dillon if (rv < 0)
236fca180e3SMatthew Dillon err(1, "cannot open any disk");
237fca180e3SMatthew Dillon } else {
2381b0b5697SSascha Wildner if (open_disk() < 0)
239fca180e3SMatthew Dillon err(1, "cannot open disk %s", disk);
240fca180e3SMatthew Dillon }
241fca180e3SMatthew Dillon
242fca180e3SMatthew Dillon /* (abu)use mboot.bootinst to probe for the sector size */
243fca180e3SMatthew Dillon if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
244fca180e3SMatthew Dillon err(1, "cannot allocate buffer to determine disk sector size");
2459fe225c4SAlexander Kuleshov if (read_disk(0, mboot.bootinst) == -1)
2469fe225c4SAlexander Kuleshov {
2479fe225c4SAlexander Kuleshov free(mboot.bootinst);
2489fe225c4SAlexander Kuleshov errx(1, "could not detect sector size");
2499fe225c4SAlexander Kuleshov }
250fca180e3SMatthew Dillon free(mboot.bootinst);
251fca180e3SMatthew Dillon mboot.bootinst = NULL;
252fca180e3SMatthew Dillon
253fca180e3SMatthew Dillon if (s_flag)
254fca180e3SMatthew Dillon {
2551b0b5697SSascha Wildner int j;
256fca180e3SMatthew Dillon struct dos_partition *partp;
257fca180e3SMatthew Dillon
258fca180e3SMatthew Dillon if (read_s0())
259fca180e3SMatthew Dillon err(1, "read_s0");
260fca180e3SMatthew Dillon printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
261fca180e3SMatthew Dillon dos_sectors);
262fca180e3SMatthew Dillon printf("Part %11s %11s Type Flags\n", "Start", "Size");
2631b0b5697SSascha Wildner for (j = 0; j < NDOSPART; j++) {
2641b0b5697SSascha Wildner partp = ((struct dos_partition *) &mboot.parts) + j;
265fca180e3SMatthew Dillon if (partp->dp_start == 0 && partp->dp_size == 0)
266fca180e3SMatthew Dillon continue;
2671b0b5697SSascha Wildner printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", j + 1,
268fca180e3SMatthew Dillon (u_long) partp->dp_start,
269fca180e3SMatthew Dillon (u_long) partp->dp_size, partp->dp_typ,
270fca180e3SMatthew Dillon partp->dp_flag);
271fca180e3SMatthew Dillon }
272fca180e3SMatthew Dillon exit(0);
273fca180e3SMatthew Dillon }
274fca180e3SMatthew Dillon
275fca180e3SMatthew Dillon printf("******* Working on device %s *******\n",disk);
276fca180e3SMatthew Dillon
277fca180e3SMatthew Dillon if (I_flag)
278fca180e3SMatthew Dillon {
279fca180e3SMatthew Dillon struct dos_partition *partp;
280fca180e3SMatthew Dillon
281*1edc50a1SSascha Wildner read_s0();
282fca180e3SMatthew Dillon reset_boot();
283fca180e3SMatthew Dillon partp = (struct dos_partition *) (&mboot.parts[0]);
284794d80aaSMatthew Dillon partp->dp_typ = DOSPTYP_DFLYBSD;
285fca180e3SMatthew Dillon partp->dp_flag = ACTIVE;
286fca180e3SMatthew Dillon partp->dp_start = dos_sectors;
287fca180e3SMatthew Dillon if (disksecs - dos_sectors > 0xFFFFFFFFU) {
288fca180e3SMatthew Dillon printf("Warning: Ending logical block > 2TB, using max value\n");
289fca180e3SMatthew Dillon partp->dp_size = 0xFFFFFFFFU;
290fca180e3SMatthew Dillon } else {
291ed183f8cSSascha Wildner partp->dp_size =
292ed183f8cSSascha Wildner rounddown(disksecs, dos_cylsecs) - dos_sectors;
293fca180e3SMatthew Dillon }
294fca180e3SMatthew Dillon dos(partp);
295fca180e3SMatthew Dillon if (v_flag)
296fca180e3SMatthew Dillon print_s0(-1);
297e0fb398bSTim
298e0fb398bSTim if (E_flag) {
299e0fb398bSTim /* Trim now if we're using the entire device */
300e0fb398bSTim erase_partition(0);
301e0fb398bSTim }
302e0fb398bSTim
303fca180e3SMatthew Dillon if (!t_flag)
304ab9c9b79SAlexander Kuleshov if (write_s0() == -1)
305ab9c9b79SAlexander Kuleshov err(1, "can't write fdisk partition table");
306fca180e3SMatthew Dillon exit(0);
307fca180e3SMatthew Dillon }
308fca180e3SMatthew Dillon if (f_flag)
309fca180e3SMatthew Dillon {
310fca180e3SMatthew Dillon if (read_s0() || i_flag)
311fca180e3SMatthew Dillon {
312fca180e3SMatthew Dillon reset_boot();
313fca180e3SMatthew Dillon }
314fca180e3SMatthew Dillon
315fca180e3SMatthew Dillon if (!read_config(f_flag))
316fca180e3SMatthew Dillon {
317fca180e3SMatthew Dillon exit(1);
318fca180e3SMatthew Dillon }
3190b920486SMatthew Dillon if (x_flag)
3200b920486SMatthew Dillon x_flag = expand_table();
321fca180e3SMatthew Dillon if (v_flag)
322fca180e3SMatthew Dillon {
323fca180e3SMatthew Dillon print_s0(-1);
324fca180e3SMatthew Dillon }
325fca180e3SMatthew Dillon if (!t_flag)
326fca180e3SMatthew Dillon {
327ab9c9b79SAlexander Kuleshov if (write_s0() == -1)
328ab9c9b79SAlexander Kuleshov err(1, "can't write fdisk partition table");
329fca180e3SMatthew Dillon }
330fca180e3SMatthew Dillon }
331fca180e3SMatthew Dillon else
332fca180e3SMatthew Dillon {
333fca180e3SMatthew Dillon if (u_flag)
334fca180e3SMatthew Dillon {
335fca180e3SMatthew Dillon get_params_to_use();
336fca180e3SMatthew Dillon }
337fca180e3SMatthew Dillon else
338fca180e3SMatthew Dillon {
339fca180e3SMatthew Dillon print_params();
340fca180e3SMatthew Dillon }
341fca180e3SMatthew Dillon
342fca180e3SMatthew Dillon if (read_s0())
343fca180e3SMatthew Dillon init_sector0(dos_sectors);
344fca180e3SMatthew Dillon
345fca180e3SMatthew Dillon printf("Media sector size is %d\n", secsize);
346fca180e3SMatthew Dillon printf("Warning: BIOS sector numbering starts with sector 1\n");
347fca180e3SMatthew Dillon printf("Information from DOS bootblock is:\n");
348fca180e3SMatthew Dillon if (partition == -1)
349fca180e3SMatthew Dillon for (i = 1; i <= NDOSPART; i++)
350fca180e3SMatthew Dillon change_part(i);
351fca180e3SMatthew Dillon else
352fca180e3SMatthew Dillon change_part(partition);
353fca180e3SMatthew Dillon
354fca180e3SMatthew Dillon if (u_flag || a_flag)
355fca180e3SMatthew Dillon change_active(partition);
356fca180e3SMatthew Dillon
357fca180e3SMatthew Dillon if (B_flag)
358fca180e3SMatthew Dillon change_code();
359fca180e3SMatthew Dillon
3600b920486SMatthew Dillon if (x_flag)
3610b920486SMatthew Dillon x_flag = expand_table();
3620b920486SMatthew Dillon
3630b920486SMatthew Dillon if (x_flag || u_flag || a_flag || B_flag) {
364fca180e3SMatthew Dillon if (!t_flag) {
365fca180e3SMatthew Dillon printf("\nWe haven't changed the partition table yet. ");
366fca180e3SMatthew Dillon printf("This is your last chance.\n");
367fca180e3SMatthew Dillon }
368fca180e3SMatthew Dillon print_s0(-1);
369fca180e3SMatthew Dillon if (!t_flag) {
370e0fb398bSTim if (ok("Should we write new partition table?")) {
371e0fb398bSTim if (E_flag && u_flag) {
372e0fb398bSTim /*
373e0fb398bSTim * Trim now because we've committed to
374e0fb398bSTim * updating the partition.
375e0fb398bSTim */
376e0fb398bSTim if (partition == -1)
377e0fb398bSTim for (i = 0; i < NDOSPART; i++)
378e0fb398bSTim erase_partition(i);
379e0fb398bSTim else
380e0fb398bSTim erase_partition(partition);
381e0fb398bSTim }
382ab9c9b79SAlexander Kuleshov if (write_s0() == -1)
383ab9c9b79SAlexander Kuleshov err(1, "can't write fdisk partition table");
384fca180e3SMatthew Dillon }
385e0fb398bSTim }
386fca180e3SMatthew Dillon else
387fca180e3SMatthew Dillon {
388fca180e3SMatthew Dillon printf("\n-t flag specified -- partition table not written.\n");
389fca180e3SMatthew Dillon }
390fca180e3SMatthew Dillon }
391fca180e3SMatthew Dillon }
392fca180e3SMatthew Dillon
393fca180e3SMatthew Dillon exit(0);
394fca180e3SMatthew Dillon }
395fca180e3SMatthew Dillon
396fca180e3SMatthew Dillon static void
usage(void)397fca180e3SMatthew Dillon usage(void)
398fca180e3SMatthew Dillon {
399fca180e3SMatthew Dillon fprintf(stderr, "%s%s",
4007b8c2c90SSascha Wildner "usage: fdisk [-BCEIaistu] [-b bootcode] [-p diskimage] [-1234] [disk]\n",
401fca180e3SMatthew Dillon " fdisk -f configfile [-itv] [disk]\n");
402fca180e3SMatthew Dillon exit(1);
403fca180e3SMatthew Dillon }
404fca180e3SMatthew Dillon
4050b920486SMatthew Dillon static int
expand_table(void)4060b920486SMatthew Dillon expand_table(void)
4070b920486SMatthew Dillon {
4080b920486SMatthew Dillon struct dos_partition *part;
4090b920486SMatthew Dillon struct dos_partition *best;
4100b920486SMatthew Dillon int i;
4110b920486SMatthew Dillon
4120b920486SMatthew Dillon printf("\n");
4130b920486SMatthew Dillon
4140b920486SMatthew Dillon best = NULL;
4150b920486SMatthew Dillon for (i = 0; i < NDOSPART; ++i) {
4160b920486SMatthew Dillon part = ((struct dos_partition *) &mboot.parts) + i;
4170b920486SMatthew Dillon if (part->dp_start == 0 && part->dp_size == 0)
4180b920486SMatthew Dillon continue;
4190b920486SMatthew Dillon if (best == NULL || best->dp_start < part->dp_start)
4200b920486SMatthew Dillon best = part;
4210b920486SMatthew Dillon }
4220b920486SMatthew Dillon if (best) {
4230b920486SMatthew Dillon if (best->dp_typ == 0xEE || best->dp_typ == 0xEF) {
4240b920486SMatthew Dillon printf("Cannot use fdisk to resize a GPT label\n");
4250b920486SMatthew Dillon printf("use 'gpt expand <device>' instead\n");
4260b920486SMatthew Dillon best = NULL;
4270b920486SMatthew Dillon return 0;
4280b920486SMatthew Dillon }
4290b920486SMatthew Dillon if (disksecs - best->dp_start > 0xFFFFFFFFU) {
4300b920486SMatthew Dillon if (best->dp_size == 0xFFFFFFFFU) {
4310b920486SMatthew Dillon printf("Last slice already using max value, "
4320b920486SMatthew Dillon "no changes required\n");
4330b920486SMatthew Dillon best = NULL;
4340b920486SMatthew Dillon }
4350b920486SMatthew Dillon } else if (best->dp_size == rounddown(disksecs, dos_cylsecs) -
4360b920486SMatthew Dillon best->dp_start) {
4370b920486SMatthew Dillon printf("Last slice already properly sized, "
4380b920486SMatthew Dillon "no changes required\n");
4390b920486SMatthew Dillon best = NULL;
4400b920486SMatthew Dillon }
4410b920486SMatthew Dillon }
4420b920486SMatthew Dillon
4430b920486SMatthew Dillon if (best) {
4440b920486SMatthew Dillon printf("Changing size of last slice %u -> ", best->dp_start);
4450b920486SMatthew Dillon if (disksecs - best->dp_start > 0xFFFFFFFFU) {
4460b920486SMatthew Dillon printf("max-value\n");
4470b920486SMatthew Dillon best->dp_size = 0xFFFFFFFFU;
4480b920486SMatthew Dillon } else {
4490b920486SMatthew Dillon best->dp_size = rounddown(disksecs, dos_cylsecs) -
4500b920486SMatthew Dillon best->dp_start;
4510b920486SMatthew Dillon printf("%u\n", best->dp_size);
4520b920486SMatthew Dillon }
4530b920486SMatthew Dillon dos(best);
4540b920486SMatthew Dillon return 1;
4550b920486SMatthew Dillon }
4560b920486SMatthew Dillon return 0;
4570b920486SMatthew Dillon }
4580b920486SMatthew Dillon
459fca180e3SMatthew Dillon static void
print_s0(int which)460fca180e3SMatthew Dillon print_s0(int which)
461fca180e3SMatthew Dillon {
462fca180e3SMatthew Dillon int i;
463fca180e3SMatthew Dillon
464fca180e3SMatthew Dillon print_params();
465fca180e3SMatthew Dillon printf("Information from DOS bootblock is:\n");
466fca180e3SMatthew Dillon if (which == -1)
467fca180e3SMatthew Dillon for (i = 1; i <= NDOSPART; i++)
468fca180e3SMatthew Dillon printf("%d: ", i), print_part(i);
469fca180e3SMatthew Dillon else
470fca180e3SMatthew Dillon print_part(which);
471fca180e3SMatthew Dillon }
472fca180e3SMatthew Dillon
473fca180e3SMatthew Dillon static struct dos_partition mtpart = { 0 };
474fca180e3SMatthew Dillon
475fca180e3SMatthew Dillon static void
print_part(int i)476fca180e3SMatthew Dillon print_part(int i)
477fca180e3SMatthew Dillon {
478fca180e3SMatthew Dillon struct dos_partition *partp;
4791b0b5697SSascha Wildner uint64_t part_mb;
480fca180e3SMatthew Dillon
481fca180e3SMatthew Dillon partp = ((struct dos_partition *) &mboot.parts) + i - 1;
482fca180e3SMatthew Dillon
483fca180e3SMatthew Dillon if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
484fca180e3SMatthew Dillon printf("<UNUSED>\n");
485fca180e3SMatthew Dillon return;
486fca180e3SMatthew Dillon }
487fca180e3SMatthew Dillon /*
488fca180e3SMatthew Dillon * Be careful not to overflow.
489fca180e3SMatthew Dillon */
490fca180e3SMatthew Dillon part_mb = partp->dp_size;
491fca180e3SMatthew Dillon part_mb *= secsize;
492fca180e3SMatthew Dillon part_mb /= (1024 * 1024);
493fca180e3SMatthew Dillon printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
494a276dc6bSMatthew Dillon printf(" start %lu, size %lu (%jd Meg), flag %x%s\n",
495fca180e3SMatthew Dillon (u_long)partp->dp_start,
496fca180e3SMatthew Dillon (u_long)partp->dp_size,
497a276dc6bSMatthew Dillon (intmax_t)part_mb,
498fca180e3SMatthew Dillon partp->dp_flag,
499fca180e3SMatthew Dillon partp->dp_flag == ACTIVE ? " (active)" : "");
500fca180e3SMatthew Dillon printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
501fca180e3SMatthew Dillon ,DPCYL(partp->dp_scyl, partp->dp_ssect)
502fca180e3SMatthew Dillon ,partp->dp_shd
503fca180e3SMatthew Dillon ,DPSECT(partp->dp_ssect)
504fca180e3SMatthew Dillon ,DPCYL(partp->dp_ecyl, partp->dp_esect)
505fca180e3SMatthew Dillon ,partp->dp_ehd
506fca180e3SMatthew Dillon ,DPSECT(partp->dp_esect));
507fca180e3SMatthew Dillon }
508fca180e3SMatthew Dillon
509fca180e3SMatthew Dillon
510fca180e3SMatthew Dillon static void
init_boot(void)511fca180e3SMatthew Dillon init_boot(void)
512fca180e3SMatthew Dillon {
513fca180e3SMatthew Dillon const char *fname;
5146a0b5de0SAlexander Kuleshov int boot_fd, n;
515fca180e3SMatthew Dillon struct stat sb;
516fca180e3SMatthew Dillon
517fca180e3SMatthew Dillon fname = b_flag ? b_flag : "/boot/mbr";
5186a0b5de0SAlexander Kuleshov if ((boot_fd = open(fname, O_RDONLY)) == -1 ||
5196a0b5de0SAlexander Kuleshov fstat(boot_fd, &sb) == -1)
520fca180e3SMatthew Dillon err(1, "%s", fname);
521fca180e3SMatthew Dillon if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
522b59f5f3dSAlexander Kuleshov {
523b59f5f3dSAlexander Kuleshov close(boot_fd);
524fca180e3SMatthew Dillon errx(1, "%s: length must be a multiple of sector size", fname);
525b59f5f3dSAlexander Kuleshov }
526fca180e3SMatthew Dillon if (mboot.bootinst != NULL)
527fca180e3SMatthew Dillon free(mboot.bootinst);
528fca180e3SMatthew Dillon if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
529b59f5f3dSAlexander Kuleshov {
530b59f5f3dSAlexander Kuleshov close(boot_fd);
531fca180e3SMatthew Dillon errx(1, "%s: unable to allocate read buffer", fname);
532b59f5f3dSAlexander Kuleshov }
5336a0b5de0SAlexander Kuleshov if ((n = read(boot_fd, mboot.bootinst, mboot.bootinst_size)) == -1 ||
5346a0b5de0SAlexander Kuleshov close(boot_fd))
535fca180e3SMatthew Dillon err(1, "%s", fname);
536fca180e3SMatthew Dillon if (n != mboot.bootinst_size)
537b59f5f3dSAlexander Kuleshov {
538b59f5f3dSAlexander Kuleshov close(boot_fd);
539fca180e3SMatthew Dillon errx(1, "%s: short read", fname);
540fca180e3SMatthew Dillon }
541b59f5f3dSAlexander Kuleshov }
542fca180e3SMatthew Dillon
543fca180e3SMatthew Dillon
544fca180e3SMatthew Dillon static void
init_sector0(unsigned long start)545fca180e3SMatthew Dillon init_sector0(unsigned long start)
546fca180e3SMatthew Dillon {
547fca180e3SMatthew Dillon struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
548fca180e3SMatthew Dillon
549fca180e3SMatthew Dillon init_boot();
550fca180e3SMatthew Dillon
551794d80aaSMatthew Dillon partp->dp_typ = DOSPTYP_DFLYBSD;
552fca180e3SMatthew Dillon partp->dp_flag = ACTIVE;
553a77a893aSSascha Wildner start = roundup(start, dos_sectors);
554fca180e3SMatthew Dillon if (start == 0)
555fca180e3SMatthew Dillon start = dos_sectors;
556fca180e3SMatthew Dillon partp->dp_start = start;
557fca180e3SMatthew Dillon if (disksecs - start > 0xFFFFFFFFU) {
558fca180e3SMatthew Dillon printf("Warning: Ending logical block > 2TB, using max value\n");
559fca180e3SMatthew Dillon partp->dp_size = 0xFFFFFFFFU;
560fca180e3SMatthew Dillon } else {
561ed183f8cSSascha Wildner partp->dp_size = rounddown(disksecs, dos_cylsecs) - start;
562fca180e3SMatthew Dillon }
563fca180e3SMatthew Dillon
564fca180e3SMatthew Dillon dos(partp);
565fca180e3SMatthew Dillon }
566fca180e3SMatthew Dillon
567fca180e3SMatthew Dillon static void
change_part(int i)568fca180e3SMatthew Dillon change_part(int i)
569fca180e3SMatthew Dillon {
570fca180e3SMatthew Dillon struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
571fca180e3SMatthew Dillon
572fca180e3SMatthew Dillon printf("The data for partition %d is:\n", i);
573fca180e3SMatthew Dillon print_part(i);
574fca180e3SMatthew Dillon
575fca180e3SMatthew Dillon if (u_flag && ok("Do you want to change it?")) {
576fca180e3SMatthew Dillon int tmp;
577fca180e3SMatthew Dillon
578fca180e3SMatthew Dillon if (i_flag) {
579fca180e3SMatthew Dillon bzero((char *)partp, sizeof (struct dos_partition));
580fca180e3SMatthew Dillon if (i == 4) {
581fca180e3SMatthew Dillon init_sector0(1);
582fca180e3SMatthew Dillon printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
583fca180e3SMatthew Dillon print_part(i);
584fca180e3SMatthew Dillon }
585fca180e3SMatthew Dillon }
586fca180e3SMatthew Dillon
587fca180e3SMatthew Dillon do {
5887d89978dSSascha Wildner Decimal("sysid (108=DragonFly)", partp->dp_typ, tmp);
589fca180e3SMatthew Dillon Decimal("start", partp->dp_start, tmp);
590fca180e3SMatthew Dillon Decimal("size", partp->dp_size, tmp);
591fca180e3SMatthew Dillon if (!sanitize_partition(partp)) {
592fca180e3SMatthew Dillon warnx("ERROR: failed to adjust; setting sysid to 0");
593fca180e3SMatthew Dillon partp->dp_typ = 0;
594fca180e3SMatthew Dillon }
595fca180e3SMatthew Dillon
596fca180e3SMatthew Dillon if (ok("Explicitly specify beg/end address ?"))
597fca180e3SMatthew Dillon {
598fca180e3SMatthew Dillon int tsec,tcyl,thd;
599fca180e3SMatthew Dillon tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
600fca180e3SMatthew Dillon thd = partp->dp_shd;
601fca180e3SMatthew Dillon tsec = DPSECT(partp->dp_ssect);
602fca180e3SMatthew Dillon Decimal("beginning cylinder", tcyl, tmp);
603fca180e3SMatthew Dillon Decimal("beginning head", thd, tmp);
604fca180e3SMatthew Dillon Decimal("beginning sector", tsec, tmp);
605fca180e3SMatthew Dillon if (tcyl > MAXCYL && C_flag == 0) {
606fca180e3SMatthew Dillon printf("Warning: starting cylinder wraps, using all 1's\n");
607fca180e3SMatthew Dillon partp->dp_scyl = -1;
608fca180e3SMatthew Dillon partp->dp_ssect = -1;
609fca180e3SMatthew Dillon partp->dp_shd = -1;
610fca180e3SMatthew Dillon } else {
611fca180e3SMatthew Dillon partp->dp_scyl = DOSCYL(tcyl);
612fca180e3SMatthew Dillon partp->dp_ssect = DOSSECT(tsec,tcyl);
613fca180e3SMatthew Dillon partp->dp_shd = thd;
614fca180e3SMatthew Dillon }
615fca180e3SMatthew Dillon
616fca180e3SMatthew Dillon tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
617fca180e3SMatthew Dillon thd = partp->dp_ehd;
618fca180e3SMatthew Dillon tsec = DPSECT(partp->dp_esect);
619fca180e3SMatthew Dillon Decimal("ending cylinder", tcyl, tmp);
620fca180e3SMatthew Dillon Decimal("ending head", thd, tmp);
621fca180e3SMatthew Dillon Decimal("ending sector", tsec, tmp);
622fca180e3SMatthew Dillon if (tcyl > MAXCYL && C_flag == 0) {
623fca180e3SMatthew Dillon printf("Warning: ending cylinder wraps, using all 1's\n");
624fca180e3SMatthew Dillon partp->dp_ecyl = -1;
625fca180e3SMatthew Dillon partp->dp_esect = -1;
626fca180e3SMatthew Dillon partp->dp_ehd = -1;
627fca180e3SMatthew Dillon } else {
628fca180e3SMatthew Dillon partp->dp_ecyl = DOSCYL(tcyl);
629fca180e3SMatthew Dillon partp->dp_esect = DOSSECT(tsec,tcyl);
630fca180e3SMatthew Dillon partp->dp_ehd = thd;
631fca180e3SMatthew Dillon }
632fca180e3SMatthew Dillon } else
633fca180e3SMatthew Dillon dos(partp);
634fca180e3SMatthew Dillon
635fca180e3SMatthew Dillon print_part(i);
636fca180e3SMatthew Dillon } while (!ok("Are we happy with this entry?"));
637fca180e3SMatthew Dillon }
638fca180e3SMatthew Dillon }
639fca180e3SMatthew Dillon
640fca180e3SMatthew Dillon static void
print_params(void)641fca180e3SMatthew Dillon print_params(void)
642fca180e3SMatthew Dillon {
643fca180e3SMatthew Dillon printf("parameters extracted from device are:\n");
644fca180e3SMatthew Dillon printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
645fca180e3SMatthew Dillon ,cyls,heads,sectors,cylsecs);
6466a0b5de0SAlexander Kuleshov if ((dos_sectors > MAX_SECTORS_PER_TRACK) || (dos_cyls > MAXCYL) || (dos_heads > MAX_HEADS))
647fca180e3SMatthew Dillon printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
648fca180e3SMatthew Dillon printf("parameters to be used for BIOS calculations are:\n");
649fca180e3SMatthew Dillon printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
650fca180e3SMatthew Dillon ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
651fca180e3SMatthew Dillon }
652fca180e3SMatthew Dillon
653fca180e3SMatthew Dillon static void
change_active(int which)654fca180e3SMatthew Dillon change_active(int which)
655fca180e3SMatthew Dillon {
656fca180e3SMatthew Dillon struct dos_partition *partp = &mboot.parts[0];
657fca180e3SMatthew Dillon int active, i, new, tmp;
658fca180e3SMatthew Dillon
659fca180e3SMatthew Dillon active = -1;
660fca180e3SMatthew Dillon for (i = 0; i < NDOSPART; i++) {
661fca180e3SMatthew Dillon if ((partp[i].dp_flag & ACTIVE) == 0)
662fca180e3SMatthew Dillon continue;
663fca180e3SMatthew Dillon printf("Partition %d is marked active\n", i + 1);
664fca180e3SMatthew Dillon if (active == -1)
665fca180e3SMatthew Dillon active = i + 1;
666fca180e3SMatthew Dillon }
667fca180e3SMatthew Dillon if (a_flag && which != -1)
668fca180e3SMatthew Dillon active = which;
669fca180e3SMatthew Dillon else if (active == -1)
670fca180e3SMatthew Dillon active = 1;
671fca180e3SMatthew Dillon
672fca180e3SMatthew Dillon if (!ok("Do you want to change the active partition?"))
673fca180e3SMatthew Dillon return;
674fca180e3SMatthew Dillon setactive:
675fca180e3SMatthew Dillon do {
676fca180e3SMatthew Dillon new = active;
677fca180e3SMatthew Dillon Decimal("active partition", new, tmp);
678073d5c5bSAlexander Kuleshov if (new < 1 || new > NDOSPART) {
679fca180e3SMatthew Dillon printf("Active partition number must be in range 1-4."
680fca180e3SMatthew Dillon " Try again.\n");
681fca180e3SMatthew Dillon goto setactive;
682fca180e3SMatthew Dillon }
683fca180e3SMatthew Dillon active = new;
684fca180e3SMatthew Dillon } while (!ok("Are you happy with this choice"));
685fca180e3SMatthew Dillon for (i = 0; i < NDOSPART; i++)
686fca180e3SMatthew Dillon partp[i].dp_flag = 0;
687fca180e3SMatthew Dillon if (active > 0 && active <= NDOSPART)
688fca180e3SMatthew Dillon partp[active-1].dp_flag = ACTIVE;
689fca180e3SMatthew Dillon }
690fca180e3SMatthew Dillon
691fca180e3SMatthew Dillon static void
change_code(void)692fca180e3SMatthew Dillon change_code(void)
693fca180e3SMatthew Dillon {
694fca180e3SMatthew Dillon if (ok("Do you want to change the boot code?"))
695fca180e3SMatthew Dillon init_boot();
696fca180e3SMatthew Dillon }
697fca180e3SMatthew Dillon
698fca180e3SMatthew Dillon void
get_params_to_use(void)699fca180e3SMatthew Dillon get_params_to_use(void)
700fca180e3SMatthew Dillon {
701fca180e3SMatthew Dillon int tmp;
702fca180e3SMatthew Dillon print_params();
703fca180e3SMatthew Dillon if (ok("Do you want to change our idea of what BIOS thinks ?"))
704fca180e3SMatthew Dillon {
705fca180e3SMatthew Dillon do
706fca180e3SMatthew Dillon {
707fca180e3SMatthew Dillon Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
708fca180e3SMatthew Dillon Decimal("BIOS's idea of #heads", dos_heads, tmp);
709fca180e3SMatthew Dillon Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
710fca180e3SMatthew Dillon dos_cylsecs = dos_heads * dos_sectors;
711fca180e3SMatthew Dillon print_params();
712fca180e3SMatthew Dillon }
713fca180e3SMatthew Dillon while(!ok("Are you happy with this choice"));
714fca180e3SMatthew Dillon }
715fca180e3SMatthew Dillon }
716fca180e3SMatthew Dillon
717fca180e3SMatthew Dillon
718fca180e3SMatthew Dillon /***********************************************\
719fca180e3SMatthew Dillon * Change real numbers into strange dos numbers *
720fca180e3SMatthew Dillon \***********************************************/
721fca180e3SMatthew Dillon static void
dos(struct dos_partition * partp)722fca180e3SMatthew Dillon dos(struct dos_partition *partp)
723fca180e3SMatthew Dillon {
724fca180e3SMatthew Dillon int cy, sec;
7251b0b5697SSascha Wildner uint32_t end;
726fca180e3SMatthew Dillon
727fca180e3SMatthew Dillon if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
728fca180e3SMatthew Dillon memcpy(partp, &mtpart, sizeof(*partp));
729fca180e3SMatthew Dillon return;
730fca180e3SMatthew Dillon }
731fca180e3SMatthew Dillon
732fca180e3SMatthew Dillon /* Start c/h/s. */
733fca180e3SMatthew Dillon cy = partp->dp_start / dos_cylsecs;
734fca180e3SMatthew Dillon sec = partp->dp_start % dos_sectors + 1;
735fca180e3SMatthew Dillon if (cy > MAXCYL && C_flag == 0) {
736fca180e3SMatthew Dillon printf("Warning: starting cylinder wraps, using all 1's\n");
737fca180e3SMatthew Dillon partp->dp_shd = -1;
738fca180e3SMatthew Dillon partp->dp_scyl = -1;
739fca180e3SMatthew Dillon partp->dp_ssect = -1;
740fca180e3SMatthew Dillon } else {
741fca180e3SMatthew Dillon partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
742fca180e3SMatthew Dillon partp->dp_scyl = DOSCYL(cy);
743fca180e3SMatthew Dillon partp->dp_ssect = DOSSECT(sec, cy);
744fca180e3SMatthew Dillon }
745fca180e3SMatthew Dillon
746fca180e3SMatthew Dillon /* End c/h/s. */
747fca180e3SMatthew Dillon end = partp->dp_start + partp->dp_size - 1;
748fca180e3SMatthew Dillon cy = end / dos_cylsecs;
749fca180e3SMatthew Dillon sec = end % dos_sectors + 1;
750fca180e3SMatthew Dillon if (cy > MAXCYL && C_flag == 0) {
751fca180e3SMatthew Dillon printf("Warning: ending cylinder wraps, using all 1's\n");
752fca180e3SMatthew Dillon partp->dp_ehd = -1;
753fca180e3SMatthew Dillon partp->dp_ecyl = -1;
754fca180e3SMatthew Dillon partp->dp_esect = -1;
755fca180e3SMatthew Dillon } else {
756fca180e3SMatthew Dillon partp->dp_ehd = end % dos_cylsecs / dos_sectors;
757fca180e3SMatthew Dillon partp->dp_ecyl = DOSCYL(cy);
758fca180e3SMatthew Dillon partp->dp_esect = DOSSECT(sec, cy);
759fca180e3SMatthew Dillon }
760fca180e3SMatthew Dillon }
761fca180e3SMatthew Dillon
762e0fb398bSTim static void
erase_partition(int i)763e0fb398bSTim erase_partition(int i)
764e0fb398bSTim {
765e0fb398bSTim struct dos_partition *partp;
766e0fb398bSTim off_t ioarg[2];
767e0fb398bSTim
768e0fb398bSTim char *dev_name = strdup(disk);
769e0fb398bSTim
770e0fb398bSTim dev_name = strtok(dev_name + strlen("/dev/da"),"s");
771d9306807SMatthew Dillon #if 0
772d9306807SMatthew Dillon int trim_enabled = 0;
773d9306807SMatthew Dillon char sysctl_name[64];
774d9306807SMatthew Dillon size_t olen = sizeof(trim_enabled);
775d9306807SMatthew Dillon
776e0fb398bSTim sprintf(sysctl_name, "kern.cam.da.%s.trim_enabled", dev_name);
777088b2e68SMatthew Dillon if (sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0) < 0) {
778e0fb398bSTim printf("Device:%s does not support the TRIM command\n", disk);
779e0fb398bSTim usage();
780e0fb398bSTim }
781e0fb398bSTim if (!trim_enabled) {
782e0fb398bSTim printf("Erase device option selected, but sysctl (%s) "
783e0fb398bSTim "is not enabled\n",sysctl_name);
784e0fb398bSTim usage();
785e0fb398bSTim }
786d9306807SMatthew Dillon #endif
787e0fb398bSTim partp = ((struct dos_partition *) &mboot.parts) + i;
788e0fb398bSTim printf("erase sectors:%u %u\n",
789e0fb398bSTim partp->dp_start,
790e0fb398bSTim partp->dp_size);
791e0fb398bSTim
792e0fb398bSTim /* Trim the Device */
793e0fb398bSTim ioarg[0] = partp->dp_start;
794e0fb398bSTim ioarg[0] *=secsize;
795e0fb398bSTim ioarg[1] = partp->dp_size;
796e0fb398bSTim ioarg[1] *=secsize;
797e0fb398bSTim
79832506cfaSSascha Wildner if (ioctl(fd, DAIOCTRIM, ioarg) < 0) {
799e0fb398bSTim printf("Device trim failed\n");
800e0fb398bSTim usage ();
801e0fb398bSTim }
802e0fb398bSTim }
803e0fb398bSTim
804fca180e3SMatthew Dillon /* Getting device status */
805fca180e3SMatthew Dillon
806fca180e3SMatthew Dillon static int
open_disk(void)8071b0b5697SSascha Wildner open_disk(void)
808fca180e3SMatthew Dillon {
809fca180e3SMatthew Dillon struct stat st;
810fca180e3SMatthew Dillon
811fca180e3SMatthew Dillon if (stat(disk, &st) == -1) {
812fca180e3SMatthew Dillon if (errno == ENOENT)
813fca180e3SMatthew Dillon return -2;
814fca180e3SMatthew Dillon warnx("can't get file status of %s", disk);
815fca180e3SMatthew Dillon return -1;
816fca180e3SMatthew Dillon }
817fca180e3SMatthew Dillon if (!(st.st_mode & S_IFCHR) && p_flag == 0)
818fca180e3SMatthew Dillon warnx("device %s is not character special", disk);
819fca180e3SMatthew Dillon if ((fd = open(disk,
8200b920486SMatthew Dillon (x_flag || a_flag || I_flag ||
8210b920486SMatthew Dillon B_flag || u_flag) ? O_RDWR : O_RDONLY)) == -1) {
822fca180e3SMatthew Dillon if (errno == ENXIO)
823fca180e3SMatthew Dillon return -2;
824fca180e3SMatthew Dillon warnx("can't open device %s", disk);
825fca180e3SMatthew Dillon return -1;
826fca180e3SMatthew Dillon }
827fca180e3SMatthew Dillon if (get_params() == -1) {
828fca180e3SMatthew Dillon warnx("can't get disk parameters on %s", disk);
829fca180e3SMatthew Dillon return -1;
830fca180e3SMatthew Dillon }
831fca180e3SMatthew Dillon return fd;
832fca180e3SMatthew Dillon }
833fca180e3SMatthew Dillon
834fca180e3SMatthew Dillon static ssize_t
read_disk(off_t sector,void * buf)835fca180e3SMatthew Dillon read_disk(off_t sector, void *buf)
836fca180e3SMatthew Dillon {
837fca180e3SMatthew Dillon lseek(fd,(sector * 512), 0);
838fca180e3SMatthew Dillon if (secsize == 0)
839fca180e3SMatthew Dillon for(secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2)
840fca180e3SMatthew Dillon {
841fca180e3SMatthew Dillon /* try the read */
842fca180e3SMatthew Dillon int size = read(fd, buf, secsize);
843fca180e3SMatthew Dillon if (size == secsize)
844fca180e3SMatthew Dillon /* it worked so return */
845fca180e3SMatthew Dillon return secsize;
846fca180e3SMatthew Dillon }
847fca180e3SMatthew Dillon else
848fca180e3SMatthew Dillon return read(fd, buf, secsize);
849fca180e3SMatthew Dillon
850fca180e3SMatthew Dillon /* we failed to read at any of the sizes */
851fca180e3SMatthew Dillon return -1;
852fca180e3SMatthew Dillon }
853fca180e3SMatthew Dillon
854fca180e3SMatthew Dillon static ssize_t
write_disk(off_t sector,void * buf)855fca180e3SMatthew Dillon write_disk(off_t sector, void *buf)
856fca180e3SMatthew Dillon {
857fca180e3SMatthew Dillon lseek(fd,(sector * 512), 0);
858fca180e3SMatthew Dillon /* write out in the size that the read_disk found worked */
859fca180e3SMatthew Dillon return write(fd, buf, secsize);
860fca180e3SMatthew Dillon }
861fca180e3SMatthew Dillon
862fca180e3SMatthew Dillon static int
get_params(void)863fca180e3SMatthew Dillon get_params(void)
864fca180e3SMatthew Dillon {
865fca180e3SMatthew Dillon struct partinfo partinfo; /* disk parameters */
866fca180e3SMatthew Dillon struct stat st;
867fca180e3SMatthew Dillon
8688155d676SMatthew Dillon /*
8698155d676SMatthew Dillon * NOTE: When faking up the CHS for a file image (e.g. for USB),
8708155d676SMatthew Dillon * we must use max values. If we do not then an overflowed
8718155d676SMatthew Dillon * cylinder count will, by convention, set the CHS fields to
8728155d676SMatthew Dillon * all 1's. The heads and sectors in the CHS fields will then
8738155d676SMatthew Dillon * exceed the basic geometry which can cause BIOSes to brick.
8748155d676SMatthew Dillon */
875fca180e3SMatthew Dillon if (ioctl(fd, DIOCGPART, &partinfo) == -1) {
876fca180e3SMatthew Dillon if (p_flag && fstat(fd, &st) == 0 && st.st_size) {
8776a0b5de0SAlexander Kuleshov sectors = MAX_SECTORS_PER_TRACK;
8786a0b5de0SAlexander Kuleshov heads = MAX_HEADS;
879fca180e3SMatthew Dillon cylsecs = heads * sectors;
880fca180e3SMatthew Dillon cyls = st.st_size / 512 / cylsecs;
881fca180e3SMatthew Dillon } else {
882fca180e3SMatthew Dillon warnx("can't get disk parameters on %s; supplying dummy ones",
883fca180e3SMatthew Dillon disk);
884fca180e3SMatthew Dillon heads = 1;
885fca180e3SMatthew Dillon cylsecs = heads * sectors;
886fca180e3SMatthew Dillon }
887fca180e3SMatthew Dillon } else {
888fca180e3SMatthew Dillon cyls = partinfo.d_ncylinders;
889fca180e3SMatthew Dillon heads = partinfo.d_nheads;
890fca180e3SMatthew Dillon sectors = partinfo.d_secpertrack;
891fca180e3SMatthew Dillon cylsecs = heads * sectors;
892fca180e3SMatthew Dillon secsize = partinfo.media_blksize;
893fca180e3SMatthew Dillon }
894fca180e3SMatthew Dillon dos_cyls = cyls;
895fca180e3SMatthew Dillon dos_heads = heads;
896fca180e3SMatthew Dillon dos_sectors = sectors;
897fca180e3SMatthew Dillon dos_cylsecs = cylsecs;
898fca180e3SMatthew Dillon disksecs = (int64_t)cyls * heads * sectors;
899fca180e3SMatthew Dillon return (disksecs);
900fca180e3SMatthew Dillon }
901fca180e3SMatthew Dillon
902fca180e3SMatthew Dillon
903fca180e3SMatthew Dillon static int
read_s0(void)904fca180e3SMatthew Dillon read_s0(void)
905fca180e3SMatthew Dillon {
906fca180e3SMatthew Dillon mboot.bootinst_size = secsize;
907fca180e3SMatthew Dillon if (mboot.bootinst != NULL)
908fca180e3SMatthew Dillon free(mboot.bootinst);
909fca180e3SMatthew Dillon if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
910fca180e3SMatthew Dillon warnx("unable to allocate buffer to read fdisk "
911fca180e3SMatthew Dillon "partition table");
912fca180e3SMatthew Dillon return -1;
913fca180e3SMatthew Dillon }
914fca180e3SMatthew Dillon if (read_disk(0, mboot.bootinst) == -1) {
915fca180e3SMatthew Dillon warnx("can't read fdisk partition table");
916fca180e3SMatthew Dillon return -1;
917fca180e3SMatthew Dillon }
91834328c98SSascha Wildner if (*(uint16_t *)&mboot.bootinst[DOSMAGICOFF] != DOSMAGIC) {
919fca180e3SMatthew Dillon warnx("invalid fdisk partition table found");
920fca180e3SMatthew Dillon /* So should we initialize things */
921fca180e3SMatthew Dillon return -1;
922fca180e3SMatthew Dillon }
923fca180e3SMatthew Dillon memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
924fca180e3SMatthew Dillon return 0;
925fca180e3SMatthew Dillon }
926fca180e3SMatthew Dillon
927fca180e3SMatthew Dillon static int
write_s0(void)928fca180e3SMatthew Dillon write_s0(void)
929fca180e3SMatthew Dillon {
930fca180e3SMatthew Dillon #ifdef NOT_NOW
9316a0b5de0SAlexander Kuleshov int flag = 1;
932fca180e3SMatthew Dillon #endif
933fca180e3SMatthew Dillon int sector;
934fca180e3SMatthew Dillon
935fca180e3SMatthew Dillon memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
936fca180e3SMatthew Dillon /*
937fca180e3SMatthew Dillon * write enable label sector before write (if necessary),
938fca180e3SMatthew Dillon * disable after writing.
939fca180e3SMatthew Dillon * needed if the disklabel protected area also protects
940fca180e3SMatthew Dillon * sector 0. (e.g. empty disk)
941fca180e3SMatthew Dillon */
942fca180e3SMatthew Dillon #ifdef NOT_NOW
943fca180e3SMatthew Dillon if (ioctl(fd, DIOCWLABEL, &flag) < 0)
944fca180e3SMatthew Dillon warn("ioctl DIOCWLABEL");
945fca180e3SMatthew Dillon #endif
946fca180e3SMatthew Dillon for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
947fca180e3SMatthew Dillon if (write_disk(sector,
948fca180e3SMatthew Dillon &mboot.bootinst[sector * secsize]) == -1) {
949fca180e3SMatthew Dillon warn("can't write fdisk partition table");
950fca180e3SMatthew Dillon #ifdef NOT_NOW
951fca180e3SMatthew Dillon flag = 0;
952fca180e3SMatthew Dillon ioctl(fd, DIOCWLABEL, &flag);
953fca180e3SMatthew Dillon #endif
9546a0b5de0SAlexander Kuleshov return -1;
955fca180e3SMatthew Dillon }
956fca180e3SMatthew Dillon #ifdef NOT_NOW
957fca180e3SMatthew Dillon flag = 0;
958fca180e3SMatthew Dillon ioctl(fd, DIOCWLABEL, &flag);
959fca180e3SMatthew Dillon #endif
960fca180e3SMatthew Dillon return(0);
961fca180e3SMatthew Dillon }
962fca180e3SMatthew Dillon
963fca180e3SMatthew Dillon
964fca180e3SMatthew Dillon static int
ok(const char * str)9651b0b5697SSascha Wildner ok(const char *str)
966fca180e3SMatthew Dillon {
967fca180e3SMatthew Dillon printf("%s [n] ", str);
968fca180e3SMatthew Dillon fflush(stdout);
969fca180e3SMatthew Dillon if (fgets(lbuf, LBUF, stdin) == NULL)
970fca180e3SMatthew Dillon exit(1);
971fca180e3SMatthew Dillon lbuf[strlen(lbuf)-1] = 0;
972fca180e3SMatthew Dillon
973fca180e3SMatthew Dillon if (*lbuf &&
974fca180e3SMatthew Dillon (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
975fca180e3SMatthew Dillon !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
976fca180e3SMatthew Dillon return 1;
977fca180e3SMatthew Dillon else
978fca180e3SMatthew Dillon return 0;
979fca180e3SMatthew Dillon }
980fca180e3SMatthew Dillon
981fca180e3SMatthew Dillon static int
decimal(const char * str,int * num,int deflt)9821b0b5697SSascha Wildner decimal(const char *str, int *num, int deflt)
983fca180e3SMatthew Dillon {
984fca180e3SMatthew Dillon int acc = 0, c;
985fca180e3SMatthew Dillon char *cp;
986fca180e3SMatthew Dillon
987fca180e3SMatthew Dillon while (1) {
988fca180e3SMatthew Dillon printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
989fca180e3SMatthew Dillon fflush(stdout);
990fca180e3SMatthew Dillon if (fgets(lbuf, LBUF, stdin) == NULL)
991fca180e3SMatthew Dillon exit(1);
992fca180e3SMatthew Dillon lbuf[strlen(lbuf)-1] = 0;
993fca180e3SMatthew Dillon
994fca180e3SMatthew Dillon if (!*lbuf)
995fca180e3SMatthew Dillon return 0;
996fca180e3SMatthew Dillon
997fca180e3SMatthew Dillon cp = lbuf;
998fca180e3SMatthew Dillon while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
999fca180e3SMatthew Dillon if (!c)
1000fca180e3SMatthew Dillon return 0;
1001fca180e3SMatthew Dillon while ((c = *cp++)) {
1002fca180e3SMatthew Dillon if (c <= '9' && c >= '0')
1003fca180e3SMatthew Dillon acc = acc * 10 + c - '0';
1004fca180e3SMatthew Dillon else
1005fca180e3SMatthew Dillon break;
1006fca180e3SMatthew Dillon }
1007fca180e3SMatthew Dillon if (c == ' ' || c == '\t')
1008fca180e3SMatthew Dillon while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1009fca180e3SMatthew Dillon if (!c) {
1010fca180e3SMatthew Dillon *num = acc;
1011fca180e3SMatthew Dillon return 1;
1012fca180e3SMatthew Dillon } else
1013fca180e3SMatthew Dillon printf("%s is an invalid decimal number. Try again.\n",
1014fca180e3SMatthew Dillon lbuf);
1015fca180e3SMatthew Dillon }
1016fca180e3SMatthew Dillon
1017fca180e3SMatthew Dillon }
1018fca180e3SMatthew Dillon
10191b0b5697SSascha Wildner static const char *
get_type(int type)1020fca180e3SMatthew Dillon get_type(int type)
1021fca180e3SMatthew Dillon {
10222574e737SSascha Wildner int numentries = NELEM(dos_ptypes);
1023fca180e3SMatthew Dillon int counter = 0;
10242574e737SSascha Wildner const struct dos_ptype *ptr = dos_ptypes;
1025fca180e3SMatthew Dillon
1026fca180e3SMatthew Dillon
1027fca180e3SMatthew Dillon while(counter < numentries)
1028fca180e3SMatthew Dillon {
1029fca180e3SMatthew Dillon if (ptr->type == type)
1030fca180e3SMatthew Dillon {
1031fca180e3SMatthew Dillon return(ptr->name);
1032fca180e3SMatthew Dillon }
1033fca180e3SMatthew Dillon ptr++;
1034fca180e3SMatthew Dillon counter++;
1035fca180e3SMatthew Dillon }
1036fca180e3SMatthew Dillon return("unknown");
1037fca180e3SMatthew Dillon }
1038fca180e3SMatthew Dillon
1039fca180e3SMatthew Dillon
1040fca180e3SMatthew Dillon static void
parse_config_line(char * line,CMD * command)1041fca180e3SMatthew Dillon parse_config_line(char *line, CMD *command)
1042fca180e3SMatthew Dillon {
1043fca180e3SMatthew Dillon char *cp, *end;
1044fca180e3SMatthew Dillon
1045fca180e3SMatthew Dillon cp = line;
1046fca180e3SMatthew Dillon while (1) /* dirty trick used to insure one exit point for this
1047fca180e3SMatthew Dillon function */
1048fca180e3SMatthew Dillon {
1049fca180e3SMatthew Dillon memset(command, 0, sizeof(*command));
1050fca180e3SMatthew Dillon
1051fca180e3SMatthew Dillon while (isspace(*cp)) ++cp;
1052fca180e3SMatthew Dillon if (*cp == '\0' || *cp == '#')
1053fca180e3SMatthew Dillon {
1054fca180e3SMatthew Dillon break;
1055fca180e3SMatthew Dillon }
1056fca180e3SMatthew Dillon command->cmd = *cp++;
1057fca180e3SMatthew Dillon
1058fca180e3SMatthew Dillon /*
1059fca180e3SMatthew Dillon * Parse args
1060fca180e3SMatthew Dillon */
1061fca180e3SMatthew Dillon while (1)
1062fca180e3SMatthew Dillon {
1063fca180e3SMatthew Dillon while (isspace(*cp)) ++cp;
1064fca180e3SMatthew Dillon if (*cp == '#')
1065fca180e3SMatthew Dillon {
1066fca180e3SMatthew Dillon break; /* found comment */
1067fca180e3SMatthew Dillon }
1068fca180e3SMatthew Dillon if (isalpha(*cp))
1069fca180e3SMatthew Dillon {
1070fca180e3SMatthew Dillon command->args[command->n_args].argtype = *cp++;
1071fca180e3SMatthew Dillon }
1072fca180e3SMatthew Dillon if (!isdigit(*cp))
1073fca180e3SMatthew Dillon {
1074fca180e3SMatthew Dillon break; /* assume end of line */
1075fca180e3SMatthew Dillon }
1076fca180e3SMatthew Dillon end = NULL;
107782675d28SSascha Wildner command->args[command->n_args].arg_val = strtoll(cp, &end, 0);
1078fca180e3SMatthew Dillon if (cp == end)
1079fca180e3SMatthew Dillon {
1080fca180e3SMatthew Dillon break; /* couldn't parse number */
1081fca180e3SMatthew Dillon }
1082fca180e3SMatthew Dillon cp = end;
1083fca180e3SMatthew Dillon command->n_args++;
1084fca180e3SMatthew Dillon }
1085fca180e3SMatthew Dillon break;
1086fca180e3SMatthew Dillon }
1087fca180e3SMatthew Dillon }
1088fca180e3SMatthew Dillon
1089fca180e3SMatthew Dillon
1090fca180e3SMatthew Dillon static int
process_geometry(CMD * command)1091fca180e3SMatthew Dillon process_geometry(CMD *command)
1092fca180e3SMatthew Dillon {
1093fca180e3SMatthew Dillon int status = 1, i;
1094fca180e3SMatthew Dillon
1095fca180e3SMatthew Dillon while (1)
1096fca180e3SMatthew Dillon {
1097fca180e3SMatthew Dillon geom_processed = 1;
1098fca180e3SMatthew Dillon if (part_processed)
1099fca180e3SMatthew Dillon {
1100fca180e3SMatthew Dillon warnx(
1101fca180e3SMatthew Dillon "ERROR line %d: the geometry specification line must occur before\n\
1102fca180e3SMatthew Dillon all partition specifications",
1103fca180e3SMatthew Dillon current_line_number);
1104fca180e3SMatthew Dillon status = 0;
1105fca180e3SMatthew Dillon break;
1106fca180e3SMatthew Dillon }
1107fca180e3SMatthew Dillon if (command->n_args != 3)
1108fca180e3SMatthew Dillon {
1109fca180e3SMatthew Dillon warnx("ERROR line %d: incorrect number of geometry args",
1110fca180e3SMatthew Dillon current_line_number);
1111fca180e3SMatthew Dillon status = 0;
1112fca180e3SMatthew Dillon break;
1113fca180e3SMatthew Dillon }
1114fca180e3SMatthew Dillon dos_cyls = -1;
1115fca180e3SMatthew Dillon dos_heads = -1;
1116fca180e3SMatthew Dillon dos_sectors = -1;
1117fca180e3SMatthew Dillon for (i = 0; i < 3; ++i)
1118fca180e3SMatthew Dillon {
1119fca180e3SMatthew Dillon switch (command->args[i].argtype)
1120fca180e3SMatthew Dillon {
1121fca180e3SMatthew Dillon case 'c':
1122fca180e3SMatthew Dillon dos_cyls = command->args[i].arg_val;
1123fca180e3SMatthew Dillon break;
1124fca180e3SMatthew Dillon case 'h':
1125fca180e3SMatthew Dillon dos_heads = command->args[i].arg_val;
1126fca180e3SMatthew Dillon break;
1127fca180e3SMatthew Dillon case 's':
1128fca180e3SMatthew Dillon dos_sectors = command->args[i].arg_val;
1129fca180e3SMatthew Dillon break;
1130fca180e3SMatthew Dillon default:
1131fca180e3SMatthew Dillon warnx(
1132fca180e3SMatthew Dillon "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1133fca180e3SMatthew Dillon current_line_number, command->args[i].argtype,
1134fca180e3SMatthew Dillon command->args[i].argtype);
1135fca180e3SMatthew Dillon status = 0;
1136fca180e3SMatthew Dillon break;
1137fca180e3SMatthew Dillon }
1138fca180e3SMatthew Dillon }
1139fca180e3SMatthew Dillon if (status == 0)
1140fca180e3SMatthew Dillon {
1141fca180e3SMatthew Dillon break;
1142fca180e3SMatthew Dillon }
1143fca180e3SMatthew Dillon
1144fca180e3SMatthew Dillon dos_cylsecs = dos_heads * dos_sectors;
1145fca180e3SMatthew Dillon
1146fca180e3SMatthew Dillon /*
1147fca180e3SMatthew Dillon * Do sanity checks on parameter values
1148fca180e3SMatthew Dillon */
1149fca180e3SMatthew Dillon if (dos_cyls < 0)
1150fca180e3SMatthew Dillon {
1151fca180e3SMatthew Dillon warnx("ERROR line %d: number of cylinders not specified",
1152fca180e3SMatthew Dillon current_line_number);
1153fca180e3SMatthew Dillon status = 0;
1154fca180e3SMatthew Dillon }
1155fca180e3SMatthew Dillon if (dos_cyls == 0 || dos_cyls > 1024)
1156fca180e3SMatthew Dillon {
1157fca180e3SMatthew Dillon warnx(
1158fca180e3SMatthew Dillon "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1159fca180e3SMatthew Dillon (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1160fca180e3SMatthew Dillon is dedicated to DragonFly)",
1161fca180e3SMatthew Dillon current_line_number, dos_cyls);
1162fca180e3SMatthew Dillon }
1163fca180e3SMatthew Dillon
1164fca180e3SMatthew Dillon if (dos_heads < 0)
1165fca180e3SMatthew Dillon {
1166fca180e3SMatthew Dillon warnx("ERROR line %d: number of heads not specified",
1167fca180e3SMatthew Dillon current_line_number);
1168fca180e3SMatthew Dillon status = 0;
1169fca180e3SMatthew Dillon }
1170fca180e3SMatthew Dillon else if (dos_heads < 1 || dos_heads > 256)
1171fca180e3SMatthew Dillon {
1172fca180e3SMatthew Dillon warnx("ERROR line %d: number of heads must be within (1-256)",
1173fca180e3SMatthew Dillon current_line_number);
1174fca180e3SMatthew Dillon status = 0;
1175fca180e3SMatthew Dillon }
1176fca180e3SMatthew Dillon
1177fca180e3SMatthew Dillon if (dos_sectors < 0)
1178fca180e3SMatthew Dillon {
1179fca180e3SMatthew Dillon warnx("ERROR line %d: number of sectors not specified",
1180fca180e3SMatthew Dillon current_line_number);
1181fca180e3SMatthew Dillon status = 0;
1182fca180e3SMatthew Dillon }
11836a0b5de0SAlexander Kuleshov else if (dos_sectors < MIN_SECTORS_PER_TRACK || dos_sectors > MAX_SECTORS_PER_TRACK)
1184fca180e3SMatthew Dillon {
1185fca180e3SMatthew Dillon warnx("ERROR line %d: number of sectors must be within (1-63)",
1186fca180e3SMatthew Dillon current_line_number);
1187fca180e3SMatthew Dillon status = 0;
1188fca180e3SMatthew Dillon }
1189fca180e3SMatthew Dillon
1190fca180e3SMatthew Dillon break;
1191fca180e3SMatthew Dillon }
1192fca180e3SMatthew Dillon return (status);
1193fca180e3SMatthew Dillon }
1194fca180e3SMatthew Dillon
1195fca180e3SMatthew Dillon
1196fca180e3SMatthew Dillon static int
process_partition(CMD * command)1197fca180e3SMatthew Dillon process_partition(CMD *command)
1198fca180e3SMatthew Dillon {
11991b0b5697SSascha Wildner int status = 0, part;
12001b0b5697SSascha Wildner uint32_t prev_head_boundary, prev_cyl_boundary;
12011b0b5697SSascha Wildner uint32_t adj_size, max_end;
1202fca180e3SMatthew Dillon struct dos_partition *partp;
1203fca180e3SMatthew Dillon
1204fca180e3SMatthew Dillon while (1)
1205fca180e3SMatthew Dillon {
1206fca180e3SMatthew Dillon part_processed = 1;
1207fca180e3SMatthew Dillon if (command->n_args != 4)
1208fca180e3SMatthew Dillon {
1209fca180e3SMatthew Dillon warnx("ERROR line %d: incorrect number of partition args",
1210fca180e3SMatthew Dillon current_line_number);
1211fca180e3SMatthew Dillon break;
1212fca180e3SMatthew Dillon }
12131b0b5697SSascha Wildner part = command->args[0].arg_val;
1214073d5c5bSAlexander Kuleshov if (part < 1 || part > NDOSPART)
1215fca180e3SMatthew Dillon {
1216fca180e3SMatthew Dillon warnx("ERROR line %d: invalid partition number %d",
12171b0b5697SSascha Wildner current_line_number, part);
1218fca180e3SMatthew Dillon break;
1219fca180e3SMatthew Dillon }
12201b0b5697SSascha Wildner partp = ((struct dos_partition *) &mboot.parts) + part - 1;
1221fca180e3SMatthew Dillon bzero((char *)partp, sizeof (struct dos_partition));
1222fca180e3SMatthew Dillon partp->dp_typ = command->args[1].arg_val;
1223fca180e3SMatthew Dillon partp->dp_start = command->args[2].arg_val;
1224fca180e3SMatthew Dillon partp->dp_size = command->args[3].arg_val;
1225fca180e3SMatthew Dillon max_end = partp->dp_start + partp->dp_size;
1226fca180e3SMatthew Dillon
1227fca180e3SMatthew Dillon if (partp->dp_typ == 0)
1228fca180e3SMatthew Dillon {
1229fca180e3SMatthew Dillon /*
1230fca180e3SMatthew Dillon * Get out, the partition is marked as unused.
1231fca180e3SMatthew Dillon */
1232fca180e3SMatthew Dillon /*
1233fca180e3SMatthew Dillon * Insure that it's unused.
1234fca180e3SMatthew Dillon */
1235fca180e3SMatthew Dillon bzero((char *)partp, sizeof (struct dos_partition));
1236fca180e3SMatthew Dillon status = 1;
1237fca180e3SMatthew Dillon break;
1238fca180e3SMatthew Dillon }
1239fca180e3SMatthew Dillon
1240fca180e3SMatthew Dillon /*
1241fca180e3SMatthew Dillon * Adjust start upwards, if necessary, to fall on an head boundary.
1242fca180e3SMatthew Dillon */
1243fca180e3SMatthew Dillon if (partp->dp_start % dos_sectors != 0)
1244fca180e3SMatthew Dillon {
1245ed183f8cSSascha Wildner prev_head_boundary = rounddown(partp->dp_start, dos_sectors);
12461b0b5697SSascha Wildner if (max_end < (uint32_t)dos_sectors ||
1247fca180e3SMatthew Dillon prev_head_boundary > max_end - dos_sectors)
1248fca180e3SMatthew Dillon {
1249fca180e3SMatthew Dillon /*
1250fca180e3SMatthew Dillon * Can't go past end of partition
1251fca180e3SMatthew Dillon */
1252fca180e3SMatthew Dillon warnx(
1253fca180e3SMatthew Dillon "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1254fca180e3SMatthew Dillon a head boundary",
12551b0b5697SSascha Wildner current_line_number, part);
1256fca180e3SMatthew Dillon break;
1257fca180e3SMatthew Dillon }
1258fca180e3SMatthew Dillon warnx(
1259fca180e3SMatthew Dillon "WARNING: adjusting start offset of partition %d\n\
1260fca180e3SMatthew Dillon from %u to %u, to fall on a head boundary",
12611b0b5697SSascha Wildner part, (u_int)partp->dp_start,
1262fca180e3SMatthew Dillon (u_int)(prev_head_boundary + dos_sectors));
1263fca180e3SMatthew Dillon partp->dp_start = prev_head_boundary + dos_sectors;
1264fca180e3SMatthew Dillon }
1265fca180e3SMatthew Dillon
1266fca180e3SMatthew Dillon /*
1267fca180e3SMatthew Dillon * Adjust size downwards, if necessary, to fall on a cylinder
1268fca180e3SMatthew Dillon * boundary.
1269fca180e3SMatthew Dillon */
1270fca180e3SMatthew Dillon prev_cyl_boundary =
1271ed183f8cSSascha Wildner rounddown(partp->dp_start + partp->dp_size, dos_cylsecs);
1272fca180e3SMatthew Dillon if (prev_cyl_boundary > partp->dp_start)
1273fca180e3SMatthew Dillon adj_size = prev_cyl_boundary - partp->dp_start;
1274fca180e3SMatthew Dillon else
1275fca180e3SMatthew Dillon {
1276fca180e3SMatthew Dillon warnx(
1277fca180e3SMatthew Dillon "ERROR: could not adjust partition to start on a head boundary\n\
1278fca180e3SMatthew Dillon and end on a cylinder boundary.");
1279fca180e3SMatthew Dillon return (0);
1280fca180e3SMatthew Dillon }
1281fca180e3SMatthew Dillon if (adj_size != partp->dp_size)
1282fca180e3SMatthew Dillon {
1283fca180e3SMatthew Dillon warnx(
1284fca180e3SMatthew Dillon "WARNING: adjusting size of partition %d from %u to %u\n\
1285fca180e3SMatthew Dillon to end on a cylinder boundary",
12861b0b5697SSascha Wildner part, (u_int)partp->dp_size, (u_int)adj_size);
1287fca180e3SMatthew Dillon partp->dp_size = adj_size;
1288fca180e3SMatthew Dillon }
1289fca180e3SMatthew Dillon if (partp->dp_size == 0)
1290fca180e3SMatthew Dillon {
1291fca180e3SMatthew Dillon warnx("ERROR line %d: size of partition %d is zero",
12921b0b5697SSascha Wildner current_line_number, part);
1293fca180e3SMatthew Dillon break;
1294fca180e3SMatthew Dillon }
1295fca180e3SMatthew Dillon
1296fca180e3SMatthew Dillon dos(partp);
1297fca180e3SMatthew Dillon status = 1;
1298fca180e3SMatthew Dillon break;
1299fca180e3SMatthew Dillon }
1300fca180e3SMatthew Dillon return (status);
1301fca180e3SMatthew Dillon }
1302fca180e3SMatthew Dillon
1303fca180e3SMatthew Dillon
1304fca180e3SMatthew Dillon static int
process_active(CMD * command)1305fca180e3SMatthew Dillon process_active(CMD *command)
1306fca180e3SMatthew Dillon {
13071b0b5697SSascha Wildner int status = 0, part, i;
1308fca180e3SMatthew Dillon struct dos_partition *partp;
1309fca180e3SMatthew Dillon
1310fca180e3SMatthew Dillon while (1)
1311fca180e3SMatthew Dillon {
1312fca180e3SMatthew Dillon active_processed = 1;
1313fca180e3SMatthew Dillon if (command->n_args != 1)
1314fca180e3SMatthew Dillon {
1315fca180e3SMatthew Dillon warnx("ERROR line %d: incorrect number of active args",
1316fca180e3SMatthew Dillon current_line_number);
1317fca180e3SMatthew Dillon status = 0;
1318fca180e3SMatthew Dillon break;
1319fca180e3SMatthew Dillon }
13201b0b5697SSascha Wildner part = command->args[0].arg_val;
1321073d5c5bSAlexander Kuleshov if (part < 1 || part > NDOSPART)
1322fca180e3SMatthew Dillon {
1323fca180e3SMatthew Dillon warnx("ERROR line %d: invalid partition number %d",
13241b0b5697SSascha Wildner current_line_number, part);
1325fca180e3SMatthew Dillon break;
1326fca180e3SMatthew Dillon }
1327fca180e3SMatthew Dillon /*
1328fca180e3SMatthew Dillon * Reset active partition
1329fca180e3SMatthew Dillon */
1330fca180e3SMatthew Dillon partp = ((struct dos_partition *) &mboot.parts);
1331fca180e3SMatthew Dillon for (i = 0; i < NDOSPART; i++)
1332fca180e3SMatthew Dillon partp[i].dp_flag = 0;
13331b0b5697SSascha Wildner partp[part-1].dp_flag = ACTIVE;
1334fca180e3SMatthew Dillon
1335fca180e3SMatthew Dillon status = 1;
1336fca180e3SMatthew Dillon break;
1337fca180e3SMatthew Dillon }
1338fca180e3SMatthew Dillon return (status);
1339fca180e3SMatthew Dillon }
1340fca180e3SMatthew Dillon
1341fca180e3SMatthew Dillon
1342fca180e3SMatthew Dillon static int
process_line(char * line)1343fca180e3SMatthew Dillon process_line(char *line)
1344fca180e3SMatthew Dillon {
1345fca180e3SMatthew Dillon CMD command;
1346fca180e3SMatthew Dillon int status = 1;
1347fca180e3SMatthew Dillon
1348fca180e3SMatthew Dillon while (1)
1349fca180e3SMatthew Dillon {
1350fca180e3SMatthew Dillon parse_config_line(line, &command);
1351fca180e3SMatthew Dillon switch (command.cmd)
1352fca180e3SMatthew Dillon {
1353fca180e3SMatthew Dillon case 0:
1354fca180e3SMatthew Dillon /*
1355fca180e3SMatthew Dillon * Comment or blank line
1356fca180e3SMatthew Dillon */
1357fca180e3SMatthew Dillon break;
1358fca180e3SMatthew Dillon case 'g':
1359fca180e3SMatthew Dillon /*
1360fca180e3SMatthew Dillon * Set geometry
1361fca180e3SMatthew Dillon */
1362fca180e3SMatthew Dillon status = process_geometry(&command);
1363fca180e3SMatthew Dillon break;
1364fca180e3SMatthew Dillon case 'p':
1365fca180e3SMatthew Dillon status = process_partition(&command);
1366fca180e3SMatthew Dillon break;
1367fca180e3SMatthew Dillon case 'a':
1368fca180e3SMatthew Dillon status = process_active(&command);
1369fca180e3SMatthew Dillon break;
1370fca180e3SMatthew Dillon default:
1371fca180e3SMatthew Dillon status = 0;
1372fca180e3SMatthew Dillon break;
1373fca180e3SMatthew Dillon }
1374fca180e3SMatthew Dillon break;
1375fca180e3SMatthew Dillon }
1376fca180e3SMatthew Dillon return (status);
1377fca180e3SMatthew Dillon }
1378fca180e3SMatthew Dillon
1379fca180e3SMatthew Dillon
1380fca180e3SMatthew Dillon static int
read_config(char * config_file)1381fca180e3SMatthew Dillon read_config(char *config_file)
1382fca180e3SMatthew Dillon {
1383fca180e3SMatthew Dillon FILE *fp = NULL;
1384fca180e3SMatthew Dillon int status = 1;
1385fca180e3SMatthew Dillon char buf[1010];
1386fca180e3SMatthew Dillon
1387fca180e3SMatthew Dillon while (1) /* dirty trick used to insure one exit point for this
1388fca180e3SMatthew Dillon function */
1389fca180e3SMatthew Dillon {
1390fca180e3SMatthew Dillon if (strcmp(config_file, "-") != 0)
1391fca180e3SMatthew Dillon {
1392fca180e3SMatthew Dillon /*
1393fca180e3SMatthew Dillon * We're not reading from stdin
1394fca180e3SMatthew Dillon */
1395fca180e3SMatthew Dillon if ((fp = fopen(config_file, "r")) == NULL)
1396fca180e3SMatthew Dillon {
1397fca180e3SMatthew Dillon status = 0;
1398fca180e3SMatthew Dillon break;
1399fca180e3SMatthew Dillon }
1400fca180e3SMatthew Dillon }
1401fca180e3SMatthew Dillon else
1402fca180e3SMatthew Dillon {
1403fca180e3SMatthew Dillon fp = stdin;
1404fca180e3SMatthew Dillon }
1405fca180e3SMatthew Dillon current_line_number = 0;
1406fca180e3SMatthew Dillon while (!feof(fp))
1407fca180e3SMatthew Dillon {
1408fca180e3SMatthew Dillon if (fgets(buf, sizeof(buf), fp) == NULL)
1409fca180e3SMatthew Dillon {
1410fca180e3SMatthew Dillon break;
1411fca180e3SMatthew Dillon }
1412fca180e3SMatthew Dillon ++current_line_number;
1413fca180e3SMatthew Dillon status = process_line(buf);
1414fca180e3SMatthew Dillon if (status == 0)
1415fca180e3SMatthew Dillon {
1416fca180e3SMatthew Dillon break;
1417fca180e3SMatthew Dillon }
1418fca180e3SMatthew Dillon }
1419fca180e3SMatthew Dillon break;
1420fca180e3SMatthew Dillon }
1421fca180e3SMatthew Dillon if (fp)
1422fca180e3SMatthew Dillon {
1423fca180e3SMatthew Dillon /*
1424fca180e3SMatthew Dillon * It doesn't matter if we're reading from stdin, as we've reached EOF
1425fca180e3SMatthew Dillon */
1426fca180e3SMatthew Dillon fclose(fp);
1427fca180e3SMatthew Dillon }
1428fca180e3SMatthew Dillon return (status);
1429fca180e3SMatthew Dillon }
1430fca180e3SMatthew Dillon
1431fca180e3SMatthew Dillon
1432fca180e3SMatthew Dillon static void
reset_boot(void)1433fca180e3SMatthew Dillon reset_boot(void)
1434fca180e3SMatthew Dillon {
1435fca180e3SMatthew Dillon int i;
1436fca180e3SMatthew Dillon struct dos_partition *partp;
1437fca180e3SMatthew Dillon
1438fca180e3SMatthew Dillon init_boot();
1439073d5c5bSAlexander Kuleshov for (i = 0; i < NDOSPART; ++i)
1440fca180e3SMatthew Dillon {
1441fca180e3SMatthew Dillon partp = ((struct dos_partition *) &mboot.parts) + i;
1442fca180e3SMatthew Dillon bzero((char *)partp, sizeof (struct dos_partition));
1443fca180e3SMatthew Dillon }
1444fca180e3SMatthew Dillon }
1445fca180e3SMatthew Dillon
1446fca180e3SMatthew Dillon static int
sanitize_partition(struct dos_partition * partp)1447fca180e3SMatthew Dillon sanitize_partition(struct dos_partition *partp)
1448fca180e3SMatthew Dillon {
14491b0b5697SSascha Wildner uint32_t prev_head_boundary, prev_cyl_boundary;
14501b0b5697SSascha Wildner uint32_t max_end, size, start;
1451fca180e3SMatthew Dillon
1452fca180e3SMatthew Dillon start = partp->dp_start;
1453fca180e3SMatthew Dillon size = partp->dp_size;
1454fca180e3SMatthew Dillon max_end = start + size;
1455fca180e3SMatthew Dillon /* Only allow a zero size if the partition is being marked unused. */
1456fca180e3SMatthew Dillon if (size == 0) {
1457fca180e3SMatthew Dillon if (start == 0 && partp->dp_typ == 0)
1458fca180e3SMatthew Dillon return (1);
1459fca180e3SMatthew Dillon warnx("ERROR: size of partition is zero");
1460fca180e3SMatthew Dillon return (0);
1461fca180e3SMatthew Dillon }
1462fca180e3SMatthew Dillon /* Return if no adjustment is necessary. */
1463fca180e3SMatthew Dillon if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1464fca180e3SMatthew Dillon return (1);
1465fca180e3SMatthew Dillon
1466fca180e3SMatthew Dillon if (start == 0) {
1467fca180e3SMatthew Dillon warnx("WARNING: partition overlaps with partition table");
1468fca180e3SMatthew Dillon if (ok("Correct this automatically?"))
1469fca180e3SMatthew Dillon start = dos_sectors;
1470fca180e3SMatthew Dillon }
1471fca180e3SMatthew Dillon if (start % dos_sectors != 0)
1472fca180e3SMatthew Dillon warnx("WARNING: partition does not start on a head boundary");
1473fca180e3SMatthew Dillon if ((start +size) % dos_sectors != 0)
1474fca180e3SMatthew Dillon warnx("WARNING: partition does not end on a cylinder boundary");
1475fca180e3SMatthew Dillon warnx("WARNING: this may confuse the BIOS or some operating systems");
1476fca180e3SMatthew Dillon if (!ok("Correct this automatically?"))
1477fca180e3SMatthew Dillon return (1);
1478fca180e3SMatthew Dillon
1479fca180e3SMatthew Dillon /*
1480fca180e3SMatthew Dillon * Adjust start upwards, if necessary, to fall on an head boundary.
1481fca180e3SMatthew Dillon */
1482fca180e3SMatthew Dillon if (start % dos_sectors != 0) {
1483ed183f8cSSascha Wildner prev_head_boundary = rounddown(start, dos_sectors);
14841b0b5697SSascha Wildner if (max_end < (uint32_t)dos_sectors ||
1485fca180e3SMatthew Dillon prev_head_boundary >= max_end - dos_sectors) {
1486fca180e3SMatthew Dillon /*
1487fca180e3SMatthew Dillon * Can't go past end of partition
1488fca180e3SMatthew Dillon */
1489fca180e3SMatthew Dillon warnx(
1490fca180e3SMatthew Dillon "ERROR: unable to adjust start of partition to fall on a head boundary");
1491fca180e3SMatthew Dillon return (0);
1492fca180e3SMatthew Dillon }
1493fca180e3SMatthew Dillon start = prev_head_boundary + dos_sectors;
1494fca180e3SMatthew Dillon }
1495fca180e3SMatthew Dillon
1496fca180e3SMatthew Dillon /*
1497fca180e3SMatthew Dillon * Adjust size downwards, if necessary, to fall on a cylinder
1498fca180e3SMatthew Dillon * boundary.
1499fca180e3SMatthew Dillon */
1500ed183f8cSSascha Wildner prev_cyl_boundary = rounddown(start + size, dos_cylsecs);
1501fca180e3SMatthew Dillon if (prev_cyl_boundary > start)
1502fca180e3SMatthew Dillon size = prev_cyl_boundary - start;
1503fca180e3SMatthew Dillon else {
1504fca180e3SMatthew Dillon warnx("ERROR: could not adjust partition to start on a head boundary\n\
1505fca180e3SMatthew Dillon and end on a cylinder boundary.");
1506fca180e3SMatthew Dillon return (0);
1507fca180e3SMatthew Dillon }
1508fca180e3SMatthew Dillon
1509fca180e3SMatthew Dillon /* Finally, commit any changes to partp and return. */
1510fca180e3SMatthew Dillon if (start != partp->dp_start) {
1511fca180e3SMatthew Dillon warnx("WARNING: adjusting start offset of partition to %u",
1512fca180e3SMatthew Dillon (u_int)start);
1513fca180e3SMatthew Dillon partp->dp_start = start;
1514fca180e3SMatthew Dillon }
1515fca180e3SMatthew Dillon if (size != partp->dp_size) {
1516fca180e3SMatthew Dillon warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1517fca180e3SMatthew Dillon partp->dp_size = size;
1518fca180e3SMatthew Dillon }
1519fca180e3SMatthew Dillon
1520fca180e3SMatthew Dillon return (1);
1521fca180e3SMatthew Dillon }
1522