1 /*
2 * Mach Operating System
3 * Copyright (c) 1992 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie Mellon
24 * the rights to redistribute these changes.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/disk.h>
31 #include <sys/disklabel.h>
32 #include <sys/diskmbr.h>
33 #include <sys/endian.h>
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <sys/mount.h>
37 #include <ctype.h>
38 #include <fcntl.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <libgeom.h>
42 #include <paths.h>
43 #include <regex.h>
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49
50 static int iotest;
51
52 #define NO_DISK_SECTORS ((u_int32_t)-1)
53 #define NO_TRACK_CYLINDERS 1023
54 #define NO_TRACK_HEADS 255
55 #define NO_TRACK_SECTORS 63
56 #define LBUF 100
57 static char lbuf[LBUF];
58
59 /*
60 *
61 * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992
62 *
63 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
64 * Copyright (c) 1989 Robert. V. Baron
65 * Created.
66 */
67
68 #define Decimal(str, ans, tmp, maxval) if (decimal(str, &tmp, ans, maxval)) ans = tmp
69
70 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
71
72 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
73 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
74 static int secsize = 0; /* the sensed sector size */
75
76 static char *disk;
77
78 static int cyls, sectors, heads, cylsecs, disksecs;
79
80 struct mboot {
81 unsigned char *bootinst; /* boot code */
82 off_t bootinst_size;
83 struct dos_partition parts[NDOSPART];
84 };
85
86 static struct mboot mboot;
87 static int fd;
88
89 #define ACTIVE 0x80
90
91 static uint dos_cyls;
92 static uint dos_heads;
93 static uint dos_sectors;
94 static uint dos_cylsecs;
95
96 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
97 #define DOSCYL(c) (c & 0xff)
98
99 #define MAX_ARGS 10
100
101 static int current_line_number;
102
103 static int geom_processed = 0;
104 static int part_processed = 0;
105 static int active_processed = 0;
106
107 typedef struct cmd {
108 char cmd;
109 int n_args;
110 struct arg {
111 char argtype;
112 unsigned long arg_val;
113 char * arg_str;
114 } args[MAX_ARGS];
115 } CMD;
116
117 static int B_flag = 0; /* replace boot code */
118 static int I_flag = 0; /* use entire disk for FreeBSD */
119 static int a_flag = 0; /* set active partition */
120 static char *b_flag = NULL; /* path to boot code */
121 static int i_flag = 0; /* replace partition data */
122 static int q_flag = 0; /* Be quiet */
123 static int u_flag = 0; /* update partition data */
124 static int s_flag = 0; /* Print a summary and exit */
125 static int t_flag = 0; /* test only */
126 static char *f_flag = NULL; /* Read config info from file */
127 static int v_flag = 0; /* Be verbose */
128 static int print_config_flag = 0;
129
130 /*
131 * A list of partition types, probably outdated.
132 */
133 static const char *const part_types[256] = {
134 [0x00] = "unused",
135 [0x01] = "Primary DOS with 12 bit FAT",
136 [0x02] = "XENIX / file system",
137 [0x03] = "XENIX /usr file system",
138 [0x04] = "Primary DOS with 16 bit FAT (< 32MB)",
139 [0x05] = "Extended DOS",
140 [0x06] = "Primary DOS, 16 bit FAT (>= 32MB)",
141 [0x07] = "NTFS, OS/2 HPFS, QNX-2 (16 bit) or Advanced UNIX",
142 [0x08] = "AIX file system or SplitDrive",
143 [0x09] = "AIX boot partition or Coherent",
144 [0x0A] = "OS/2 Boot Manager, OPUS or Coherent swap",
145 [0x0B] = "DOS or Windows 95 with 32 bit FAT",
146 [0x0C] = "DOS or Windows 95 with 32 bit FAT (LBA)",
147 [0x0E] = "Primary 'big' DOS (>= 32MB, LBA)",
148 [0x0F] = "Extended DOS (LBA)",
149 [0x10] = "OPUS",
150 [0x11] = "OS/2 BM: hidden DOS with 12-bit FAT",
151 [0x12] = "Compaq diagnostics",
152 [0x14] = "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)",
153 [0x16] = "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)",
154 [0x17] = "OS/2 BM: hidden IFS (e.g. HPFS)",
155 [0x18] = "AST Windows swapfile",
156 [0x1b] = "ASUS Recovery partition (NTFS)",
157 [0x24] = "NEC DOS",
158 [0x3C] = "PartitionMagic recovery",
159 [0x39] = "plan9",
160 [0x40] = "VENIX 286",
161 [0x41] = "Linux/MINIX (sharing disk with DRDOS)",
162 [0x42] = "SFS or Linux swap (sharing disk with DRDOS)",
163 [0x43] = "Linux native (sharing disk with DRDOS)",
164 [0x4D] = "QNX 4.2 Primary",
165 [0x4E] = "QNX 4.2 Secondary",
166 [0x4F] = "QNX 4.2 Tertiary",
167 [0x50] = "DM (disk manager)",
168 [0x51] = "DM6 Aux1 (or Novell)",
169 [0x52] = "CP/M or Microport SysV/AT",
170 [0x53] = "DM6 Aux3",
171 [0x54] = "DM6",
172 [0x55] = "EZ-Drive (disk manager)",
173 [0x56] = "Golden Bow (disk manager)",
174 [0x5c] = "Priam Edisk (disk manager)", /* according to S. Widlake */
175 [0x61] = "SpeedStor",
176 [0x63] = "System V/386 (such as ISC UNIX), GNU HURD or Mach",
177 [0x64] = "Novell Netware/286 2.xx",
178 [0x65] = "Novell Netware/386 3.xx",
179 [0x70] = "DiskSecure Multi-Boot",
180 [0x75] = "PCIX",
181 [0x77] = "QNX4.x",
182 [0x78] = "QNX4.x 2nd part",
183 [0x79] = "QNX4.x 3rd part",
184 [0x80] = "Minix until 1.4a",
185 [0x81] = "Minix since 1.4b, early Linux partition or Mitac disk manager",
186 [0x82] = "Linux swap or Solaris x86",
187 [0x83] = "Linux native",
188 [0x84] = "OS/2 hidden C: drive",
189 [0x85] = "Linux extended",
190 [0x86] = "NTFS volume set??",
191 [0x87] = "NTFS volume set??",
192 [0x93] = "Amoeba file system",
193 [0x94] = "Amoeba bad block table",
194 [0x9F] = "BSD/OS",
195 [0xA0] = "Suspend to Disk",
196 [0xA5] = "FreeBSD/NetBSD/386BSD",
197 [0xA6] = "OpenBSD",
198 [0xA7] = "NeXTSTEP",
199 [0xA9] = "NetBSD",
200 [0xAC] = "IBM JFS",
201 [0xAF] = "HFS+",
202 [0xB7] = "BSDI BSD/386 file system",
203 [0xB8] = "BSDI BSD/386 swap",
204 [0xBE] = "Solaris x86 boot",
205 [0xBF] = "Solaris x86 (new)",
206 [0xC1] = "DRDOS/sec with 12-bit FAT",
207 [0xC4] = "DRDOS/sec with 16-bit FAT (< 32MB)",
208 [0xC6] = "DRDOS/sec with 16-bit FAT (>= 32MB)",
209 [0xC7] = "Syrinx",
210 [0xDB] = "CP/M, Concurrent CP/M, Concurrent DOS or CTOS",
211 [0xDE] = "DELL Utilities - FAT filesystem",
212 [0xE1] = "DOS access or SpeedStor with 12-bit FAT extended partition",
213 [0xE3] = "DOS R/O or SpeedStor",
214 [0xE4] = "SpeedStor with 16-bit FAT extended partition < 1024 cyl.",
215 [0xEB] = "BeOS file system",
216 [0xEE] = "EFI GPT",
217 [0xEF] = "EFI System Partition",
218 [0xF1] = "SpeedStor",
219 [0xF2] = "DOS 3.3+ Secondary",
220 [0xF4] = "SpeedStor large partition",
221 [0xFB] = "VMware VMFS",
222 [0xFE] = "SpeedStor >1024 cyl. or LANstep",
223 [0xFF] = "Xenix bad blocks table",
224 };
225
226 static const char *
get_type(int t)227 get_type(int t)
228 {
229 const char *ret;
230
231 ret = (t >= 0 && t <= 255) ? part_types[t] : NULL;
232 return ret ? ret : "unknown";
233 }
234
235
236 static int geom_class_available(const char *);
237 static void print_s0(void);
238 static void print_part(const struct dos_partition *);
239 static void init_sector0(unsigned long start);
240 static void init_boot(void);
241 static void change_part(int i);
242 static void print_params(void);
243 static void change_active(int which);
244 static void change_code(void);
245 static void get_params_to_use(void);
246 static char *get_rootdisk(void);
247 static void dos(struct dos_partition *partp);
248 static int open_disk(int flag);
249 static ssize_t read_disk(off_t sector, void *buf);
250 static int write_disk(off_t sector, void *buf);
251 static int get_params(void);
252 static int read_s0(void);
253 static int write_s0(void);
254 static int ok(const char *str);
255 static int decimal(const char *str, int *num, int deflt, uint32_t maxval);
256 static int read_config(char *config_file);
257 static void reset_boot(void);
258 static int sanitize_partition(struct dos_partition *);
259 static void usage(void);
260
261 int
main(int argc,char * argv[])262 main(int argc, char *argv[])
263 {
264 int c, i;
265 int partition = -1;
266 struct dos_partition *partp;
267
268 while ((c = getopt(argc, argv, "BIab:f:ipqstuv1234")) != -1)
269 switch (c) {
270 case 'B':
271 B_flag = 1;
272 break;
273 case 'I':
274 I_flag = 1;
275 break;
276 case 'a':
277 a_flag = 1;
278 break;
279 case 'b':
280 b_flag = optarg;
281 break;
282 case 'f':
283 f_flag = optarg;
284 break;
285 case 'i':
286 i_flag = 1;
287 break;
288 case 'p':
289 print_config_flag = 1;
290 break;
291 case 'q':
292 q_flag = 1;
293 break;
294 case 's':
295 s_flag = 1;
296 break;
297 case 't':
298 t_flag = 1;
299 break;
300 case 'u':
301 u_flag = 1;
302 break;
303 case 'v':
304 v_flag = 1;
305 break;
306 case '1':
307 case '2':
308 case '3':
309 case '4':
310 partition = c - '0';
311 break;
312 default:
313 usage();
314 }
315 if (f_flag || i_flag)
316 u_flag = 1;
317 if (t_flag)
318 v_flag = 1;
319 argc -= optind;
320 argv += optind;
321
322 if (argc == 0) {
323 disk = get_rootdisk();
324 } else {
325 disk = g_device_path(argv[0]);
326 if (disk == NULL)
327 err(1, "unable to get correct path for %s", argv[0]);
328 }
329 if (open_disk(u_flag) < 0)
330 err(1, "cannot open disk %s", disk);
331
332 /* (abu)use mboot.bootinst to probe for the sector size */
333 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
334 err(1, "cannot allocate buffer to determine disk sector size");
335 if (read_disk(0, mboot.bootinst) == -1)
336 errx(1, "could not detect sector size");
337 free(mboot.bootinst);
338 mboot.bootinst = NULL;
339
340 if (print_config_flag) {
341 if (read_s0())
342 err(1, "read_s0");
343
344 printf("# %s\n", disk);
345 printf("g c%d h%d s%d\n", dos_cyls, dos_heads, dos_sectors);
346
347 for (i = 0; i < NDOSPART; i++) {
348 partp = &mboot.parts[i];
349
350 if (partp->dp_start == 0 && partp->dp_size == 0)
351 continue;
352
353 printf("p %d 0x%02x %lu %lu\n", i + 1, partp->dp_typ,
354 (u_long)partp->dp_start, (u_long)partp->dp_size);
355
356 /* Fill flags for the partition. */
357 if (partp->dp_flag & 0x80)
358 printf("a %d\n", i + 1);
359 }
360 exit(0);
361 }
362 if (s_flag) {
363 if (read_s0())
364 err(1, "read_s0");
365 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
366 dos_sectors);
367 printf("Part %11s %11s Type Flags\n", "Start", "Size");
368 for (i = 0; i < NDOSPART; i++) {
369 partp = &mboot.parts[i];
370 if (partp->dp_start == 0 && partp->dp_size == 0)
371 continue;
372 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
373 (u_long) partp->dp_start,
374 (u_long) partp->dp_size, partp->dp_typ,
375 partp->dp_flag);
376 }
377 exit(0);
378 }
379
380 printf("******* Working on device %s *******\n",disk);
381
382 if (I_flag) {
383 read_s0();
384 reset_boot();
385 partp = &mboot.parts[0];
386 partp->dp_typ = DOSPTYP_386BSD;
387 partp->dp_flag = ACTIVE;
388 partp->dp_start = dos_sectors;
389 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
390 dos_sectors;
391 dos(partp);
392 if (v_flag)
393 print_s0();
394 if (!t_flag)
395 write_s0();
396 exit(0);
397 }
398 if (f_flag) {
399 if (read_s0() || i_flag)
400 reset_boot();
401 if (!read_config(f_flag))
402 exit(1);
403 if (v_flag)
404 print_s0();
405 if (!t_flag)
406 write_s0();
407 } else {
408 if(u_flag)
409 get_params_to_use();
410 else
411 print_params();
412
413 if (read_s0())
414 init_sector0(dos_sectors);
415
416 printf("Media sector size is %d\n", secsize);
417 printf("Warning: BIOS sector numbering starts with sector 1\n");
418 printf("Information from DOS bootblock is:\n");
419 if (partition == -1)
420 for (i = 1; i <= NDOSPART; i++)
421 change_part(i);
422 else
423 change_part(partition);
424
425 if (u_flag || a_flag)
426 change_active(partition);
427
428 if (B_flag)
429 change_code();
430
431 if (u_flag || a_flag || B_flag) {
432 if (!t_flag) {
433 printf("\nWe haven't changed the partition table yet. ");
434 printf("This is your last chance.\n");
435 }
436 print_s0();
437 if (!t_flag) {
438 if (ok("Should we write new partition table?"))
439 write_s0();
440 } else {
441 printf("\n-t flag specified -- partition table not written.\n");
442 }
443 }
444 }
445
446 exit(0);
447 }
448
449 static void
usage()450 usage()
451 {
452 fprintf(stderr, "%s%s",
453 "usage: fdisk [-BIaipqstu] [-b bootcode] [-1234] [disk]\n",
454 " fdisk -f configfile [-itv] [disk]\n");
455 exit(1);
456 }
457
458 static void
print_s0(void)459 print_s0(void)
460 {
461 int i;
462
463 print_params();
464 printf("Information from DOS bootblock is:\n");
465 for (i = 1; i <= NDOSPART; i++) {
466 printf("%d: ", i);
467 print_part(&mboot.parts[i - 1]);
468 }
469 }
470
471 static struct dos_partition mtpart;
472
473 static void
print_part(const struct dos_partition * partp)474 print_part(const struct dos_partition *partp)
475 {
476 u_int64_t part_mb;
477
478 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
479 printf("<UNUSED>\n");
480 return;
481 }
482 /*
483 * Be careful not to overflow.
484 */
485 part_mb = partp->dp_size;
486 part_mb *= secsize;
487 part_mb /= (1024 * 1024);
488 printf("sysid %d (%#04x),(%s)\n", partp->dp_typ, partp->dp_typ,
489 get_type(partp->dp_typ));
490 printf(" start %lu, size %lu (%ju Meg), flag %x%s\n",
491 (u_long)partp->dp_start,
492 (u_long)partp->dp_size,
493 (uintmax_t)part_mb,
494 partp->dp_flag,
495 partp->dp_flag == ACTIVE ? " (active)" : "");
496 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
497 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
498 ,partp->dp_shd
499 ,DPSECT(partp->dp_ssect)
500 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
501 ,partp->dp_ehd
502 ,DPSECT(partp->dp_esect));
503 }
504
505
506 static void
init_boot(void)507 init_boot(void)
508 {
509 #ifndef __ia64__
510 const char *fname;
511 int fdesc, n;
512 struct stat sb;
513
514 fname = b_flag ? b_flag : "/boot/mbr";
515 if ((fdesc = open(fname, O_RDONLY)) == -1 ||
516 fstat(fdesc, &sb) == -1)
517 err(1, "%s", fname);
518 if (sb.st_size == 0)
519 errx(1, "%s is empty, must not be.", fname);
520 if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
521 errx(1, "%s: length must be a multiple of sector size", fname);
522 if (mboot.bootinst != NULL)
523 free(mboot.bootinst);
524 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
525 errx(1, "%s: unable to allocate read buffer", fname);
526 if ((n = read(fdesc, mboot.bootinst, mboot.bootinst_size)) == -1 ||
527 close(fdesc))
528 err(1, "%s", fname);
529 if (n != mboot.bootinst_size)
530 errx(1, "%s: short read", fname);
531 #else
532 if (mboot.bootinst != NULL)
533 free(mboot.bootinst);
534 mboot.bootinst_size = secsize;
535 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL)
536 errx(1, "unable to allocate boot block buffer");
537 memset(mboot.bootinst, 0, mboot.bootinst_size);
538 le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC);
539 #endif
540 }
541
542
543 static void
init_sector0(unsigned long start)544 init_sector0(unsigned long start)
545 {
546 struct dos_partition *partp = &mboot.parts[0];
547
548 init_boot();
549
550 partp->dp_typ = DOSPTYP_386BSD;
551 partp->dp_flag = ACTIVE;
552 start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
553 if(start == 0)
554 start = dos_sectors;
555 partp->dp_start = start;
556 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
557
558 dos(partp);
559 }
560
561 static void
change_part(int i)562 change_part(int i)
563 {
564 struct dos_partition *partp = &mboot.parts[i - 1];
565
566 printf("The data for partition %d is:\n", i);
567 print_part(partp);
568
569 if (u_flag && ok("Do you want to change it?")) {
570 int tmp;
571
572 if (i_flag) {
573 bzero(partp, sizeof (*partp));
574 if (i == 1) {
575 init_sector0(1);
576 printf("\nThe static data for the slice 1 has been reinitialized to:\n");
577 print_part(partp);
578 }
579 }
580
581 do {
582 Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp, 255);
583 Decimal("start", partp->dp_start, tmp, NO_DISK_SECTORS);
584 Decimal("size", partp->dp_size, tmp, NO_DISK_SECTORS);
585 if (!sanitize_partition(partp)) {
586 warnx("ERROR: failed to adjust; setting sysid to 0");
587 partp->dp_typ = 0;
588 }
589
590 if (ok("Explicitly specify beg/end address ?"))
591 {
592 int tsec,tcyl,thd;
593 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
594 thd = partp->dp_shd;
595 tsec = DPSECT(partp->dp_ssect);
596 Decimal("beginning cylinder", tcyl, tmp, NO_TRACK_CYLINDERS);
597 Decimal("beginning head", thd, tmp, NO_TRACK_HEADS);
598 Decimal("beginning sector", tsec, tmp, NO_TRACK_SECTORS);
599 partp->dp_scyl = DOSCYL(tcyl);
600 partp->dp_ssect = DOSSECT(tsec,tcyl);
601 partp->dp_shd = thd;
602
603 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
604 thd = partp->dp_ehd;
605 tsec = DPSECT(partp->dp_esect);
606 Decimal("ending cylinder", tcyl, tmp, NO_TRACK_CYLINDERS);
607 Decimal("ending head", thd, tmp, NO_TRACK_HEADS);
608 Decimal("ending sector", tsec, tmp, NO_TRACK_SECTORS);
609 partp->dp_ecyl = DOSCYL(tcyl);
610 partp->dp_esect = DOSSECT(tsec,tcyl);
611 partp->dp_ehd = thd;
612 } else
613 dos(partp);
614
615 print_part(partp);
616 } while (!ok("Are we happy with this entry?"));
617 }
618 }
619
620 static void
print_params()621 print_params()
622 {
623 printf("parameters extracted from in-core disklabel are:\n");
624 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
625 ,cyls,heads,sectors,cylsecs);
626 if (dos_cyls > 1023 || dos_heads > 255 || dos_sectors > 63)
627 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
628 printf("parameters to be used for BIOS calculations are:\n");
629 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
630 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
631 }
632
633 static void
change_active(int which)634 change_active(int which)
635 {
636 struct dos_partition *partp = &mboot.parts[0];
637 int active, i, new, tmp;
638
639 active = -1;
640 for (i = 0; i < NDOSPART; i++) {
641 if ((partp[i].dp_flag & ACTIVE) == 0)
642 continue;
643 printf("Partition %d is marked active\n", i + 1);
644 if (active == -1)
645 active = i + 1;
646 }
647 if (a_flag && which != -1)
648 active = which;
649 else if (active == -1)
650 active = 1;
651
652 if (!ok("Do you want to change the active partition?"))
653 return;
654 setactive:
655 do {
656 new = active;
657 Decimal("active partition", new, tmp, 0);
658 if (new < 1 || new > 4) {
659 printf("Active partition number must be in range 1-4."
660 " Try again.\n");
661 goto setactive;
662 }
663 active = new;
664 } while (!ok("Are you happy with this choice"));
665 for (i = 0; i < NDOSPART; i++)
666 partp[i].dp_flag = 0;
667 if (active > 0 && active <= NDOSPART)
668 partp[active-1].dp_flag = ACTIVE;
669 }
670
671 static void
change_code()672 change_code()
673 {
674 if (ok("Do you want to change the boot code?"))
675 init_boot();
676 }
677
678 void
get_params_to_use()679 get_params_to_use()
680 {
681 int tmp;
682 print_params();
683 if (ok("Do you want to change our idea of what BIOS thinks ?"))
684 {
685 do
686 {
687 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp, 0);
688 Decimal("BIOS's idea of #heads", dos_heads, tmp, 0);
689 Decimal("BIOS's idea of #sectors", dos_sectors, tmp, 0);
690 dos_cylsecs = dos_heads * dos_sectors;
691 print_params();
692 }
693 while(!ok("Are you happy with this choice"));
694 }
695 }
696
697
698 /***********************************************\
699 * Change real numbers into strange dos numbers *
700 \***********************************************/
701 static void
dos(struct dos_partition * partp)702 dos(struct dos_partition *partp)
703 {
704 int cy, sec;
705 u_int32_t end;
706
707 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
708 memcpy(partp, &mtpart, sizeof(*partp));
709 return;
710 }
711
712 /* Start c/h/s. */
713 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
714 cy = partp->dp_start / dos_cylsecs;
715 sec = partp->dp_start % dos_sectors + 1;
716 partp->dp_scyl = DOSCYL(cy);
717 partp->dp_ssect = DOSSECT(sec, cy);
718
719 /* End c/h/s. */
720 end = partp->dp_start + partp->dp_size - 1;
721 partp->dp_ehd = end % dos_cylsecs / dos_sectors;
722 cy = end / dos_cylsecs;
723 sec = end % dos_sectors + 1;
724 partp->dp_ecyl = DOSCYL(cy);
725 partp->dp_esect = DOSSECT(sec, cy);
726 }
727
728 static int
open_disk(int flag)729 open_disk(int flag)
730 {
731 int rwmode;
732
733 /* Write mode if one of these flags are set. */
734 rwmode = (a_flag || I_flag || B_flag || flag);
735 fd = g_open(disk, rwmode);
736 /* If the mode fails, try read-only if we didn't. */
737 if (fd == -1 && errno == EPERM && rwmode)
738 fd = g_open(disk, 0);
739 if (fd == -1 && errno == ENXIO)
740 return -2;
741 if (fd == -1) {
742 warnx("can't open device %s", disk);
743 return -1;
744 }
745 if (get_params() == -1) {
746 warnx("can't get disk parameters on %s", disk);
747 return -1;
748 }
749 return fd;
750 }
751
752 static ssize_t
read_disk(off_t sector,void * buf)753 read_disk(off_t sector, void *buf)
754 {
755
756 lseek(fd, (sector * 512), 0);
757 if (secsize == 0)
758 for (secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE;
759 secsize *= 2) {
760 /* try the read */
761 int size = read(fd, buf, secsize);
762 if (size == secsize)
763 /* it worked so return */
764 return secsize;
765 }
766 else
767 return read(fd, buf, secsize);
768
769 /* we failed to read at any of the sizes */
770 return -1;
771 }
772
773 static int
geom_class_available(const char * name)774 geom_class_available(const char *name)
775 {
776 struct gclass *class;
777 struct gmesh mesh;
778 int error;
779
780 error = geom_gettree(&mesh);
781 if (error != 0)
782 errc(1, error, "Cannot get GEOM tree");
783
784 LIST_FOREACH(class, &mesh.lg_class, lg_class) {
785 if (strcmp(class->lg_name, name) == 0) {
786 geom_deletetree(&mesh);
787 return (1);
788 }
789 }
790
791 geom_deletetree(&mesh);
792
793 return (0);
794 }
795
796 static int
write_disk(off_t sector,void * buf)797 write_disk(off_t sector, void *buf)
798 {
799 struct gctl_req *grq;
800 const char *errmsg;
801 char *pname;
802 int error;
803
804 /* Check that GEOM_MBR is available */
805 if (geom_class_available("MBR") != 0) {
806 grq = gctl_get_handle();
807 gctl_ro_param(grq, "verb", -1, "write MBR");
808 gctl_ro_param(grq, "class", -1, "MBR");
809 pname = g_providername(fd);
810 if (pname == NULL) {
811 warn("Error getting providername for %s", disk);
812 return (-1);
813 }
814 gctl_ro_param(grq, "geom", -1, pname);
815 gctl_ro_param(grq, "data", secsize, buf);
816 errmsg = gctl_issue(grq);
817 free(pname);
818 if (errmsg == NULL) {
819 gctl_free(grq);
820 return(0);
821 }
822 if (!q_flag)
823 warnx("GEOM_MBR: %s", errmsg);
824 gctl_free(grq);
825 } else {
826 /*
827 * Try to write MBR directly. This may help when disk
828 * is not in use.
829 * XXX: hardcoded sectorsize
830 */
831 error = pwrite(fd, buf, secsize, (sector * 512));
832 if (error == secsize)
833 return (0);
834 }
835
836 /*
837 * GEOM_MBR is not available or failed to write MBR.
838 * Now check that we have GEOM_PART and recommend to use gpart (8).
839 */
840 if (geom_class_available("PART") != 0)
841 warnx("Failed to write MBR. Try to use gpart(8).");
842 else
843 warnx("Failed to write sector zero");
844 return(EINVAL);
845 }
846
847 static int
get_params()848 get_params()
849 {
850 int error;
851 u_int u;
852 off_t o;
853
854 error = ioctl(fd, DIOCGFWSECTORS, &u);
855 if (error == 0)
856 sectors = dos_sectors = u;
857 else
858 sectors = dos_sectors = 63;
859
860 error = ioctl(fd, DIOCGFWHEADS, &u);
861 if (error == 0)
862 heads = dos_heads = u;
863 else
864 heads = dos_heads = 255;
865
866 dos_cylsecs = cylsecs = heads * sectors;
867 disksecs = cyls * heads * sectors;
868
869 u = g_sectorsize(fd);
870 if (u <= 0)
871 return (-1);
872
873 o = g_mediasize(fd);
874 if (o < 0)
875 return (-1);
876 disksecs = o / u;
877 cyls = dos_cyls = o / (u * dos_heads * dos_sectors);
878
879 return (disksecs);
880 }
881
882 static int
read_s0()883 read_s0()
884 {
885 int i;
886
887 mboot.bootinst_size = secsize;
888 if (mboot.bootinst != NULL)
889 free(mboot.bootinst);
890 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
891 warnx("unable to allocate buffer to read fdisk "
892 "partition table");
893 return -1;
894 }
895 if (read_disk(0, mboot.bootinst) == -1) {
896 warnx("can't read fdisk partition table");
897 return -1;
898 }
899 if (le16dec(&mboot.bootinst[DOSMAGICOFFSET]) != DOSMAGIC) {
900 warnx("invalid fdisk partition table found");
901 /* So should we initialize things */
902 return -1;
903 }
904 for (i = 0; i < NDOSPART; i++)
905 dos_partition_dec(
906 &mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE],
907 &mboot.parts[i]);
908 return 0;
909 }
910
911 static int
write_s0()912 write_s0()
913 {
914 int sector, i;
915
916 if (iotest) {
917 print_s0();
918 return 0;
919 }
920 for(i = 0; i < NDOSPART; i++)
921 dos_partition_enc(&mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE],
922 &mboot.parts[i]);
923 le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC);
924 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
925 if (write_disk(sector,
926 &mboot.bootinst[sector * secsize]) == -1) {
927 warn("can't write fdisk partition table");
928 return -1;
929 }
930 return(0);
931 }
932
933
934 static int
ok(const char * str)935 ok(const char *str)
936 {
937 printf("%s [n] ", str);
938 fflush(stdout);
939 if (fgets(lbuf, LBUF, stdin) == NULL)
940 exit(1);
941 lbuf[strlen(lbuf)-1] = 0;
942
943 if (*lbuf &&
944 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
945 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
946 return 1;
947 else
948 return 0;
949 }
950
951 static int
decimal(const char * str,int * num,int deflt,uint32_t maxval)952 decimal(const char *str, int *num, int deflt, uint32_t maxval)
953 {
954 long long acc;
955 int c;
956 char *cp;
957
958 while (1) {
959 acc = 0;
960 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
961 fflush(stdout);
962 if (fgets(lbuf, LBUF, stdin) == NULL)
963 exit(1);
964 lbuf[strlen(lbuf)-1] = 0;
965
966 if (!*lbuf)
967 return 0;
968
969 cp = lbuf;
970 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
971 if (!c)
972 return 0;
973 while ((c = *cp++)) {
974 if (c <= '9' && c >= '0') {
975 if (acc <= maxval || maxval == 0)
976 acc = acc * 10 + c - '0';
977 } else
978 break;
979 }
980 if (c == ' ' || c == '\t')
981 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
982 if (!c) {
983 if (maxval > 0 && acc > maxval) {
984 acc = maxval;
985 printf("%s exceeds maximum value allowed for "
986 "this field. The value has been reduced "
987 "to %lld\n", lbuf, acc);
988 }
989 *num = acc;
990 return 1;
991 } else
992 printf("%s is an invalid decimal number. Try again.\n",
993 lbuf);
994 }
995 }
996
997
998 static void
parse_config_line(char * line,CMD * command)999 parse_config_line(char *line, CMD *command)
1000 {
1001 char *cp, *end;
1002
1003 cp = line;
1004 while (1) {
1005 memset(command, 0, sizeof(*command));
1006
1007 while (isspace(*cp)) ++cp;
1008 if (*cp == '\0' || *cp == '#')
1009 break;
1010 command->cmd = *cp++;
1011
1012 /*
1013 * Parse args
1014 */
1015 while (1) {
1016 while (isspace(*cp)) ++cp;
1017 if (*cp == '\0')
1018 break; /* eol */
1019 if (*cp == '#')
1020 break; /* found comment */
1021 if (isalpha(*cp))
1022 command->args[command->n_args].argtype = *cp++;
1023 end = NULL;
1024 command->args[command->n_args].arg_val = strtoul(cp, &end, 0);
1025 if (cp == end || (!isspace(*end) && *end != '\0')) {
1026 char ch;
1027 end = cp;
1028 while (!isspace(*end) && *end != '\0') ++end;
1029 ch = *end; *end = '\0';
1030 command->args[command->n_args].arg_str = strdup(cp);
1031 *end = ch;
1032 } else
1033 command->args[command->n_args].arg_str = NULL;
1034 cp = end;
1035 command->n_args++;
1036 }
1037 break;
1038 }
1039 }
1040
1041
1042 static int
process_geometry(CMD * command)1043 process_geometry(CMD *command)
1044 {
1045 int status = 1, i;
1046
1047 while (1) {
1048 geom_processed = 1;
1049 if (part_processed) {
1050 warnx(
1051 "ERROR line %d: the geometry specification line must occur before\n\
1052 all partition specifications",
1053 current_line_number);
1054 status = 0;
1055 break;
1056 }
1057 if (command->n_args != 3) {
1058 warnx("ERROR line %d: incorrect number of geometry args",
1059 current_line_number);
1060 status = 0;
1061 break;
1062 }
1063 dos_cyls = 0;
1064 dos_heads = 0;
1065 dos_sectors = 0;
1066 for (i = 0; i < 3; ++i) {
1067 switch (command->args[i].argtype) {
1068 case 'c':
1069 dos_cyls = command->args[i].arg_val;
1070 break;
1071 case 'h':
1072 dos_heads = command->args[i].arg_val;
1073 break;
1074 case 's':
1075 dos_sectors = command->args[i].arg_val;
1076 break;
1077 default:
1078 warnx(
1079 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1080 current_line_number, command->args[i].argtype,
1081 command->args[i].argtype);
1082 status = 0;
1083 break;
1084 }
1085 }
1086 if (status == 0)
1087 break;
1088
1089 dos_cylsecs = dos_heads * dos_sectors;
1090
1091 /*
1092 * Do sanity checks on parameter values
1093 */
1094 if (dos_cyls == 0) {
1095 warnx("ERROR line %d: number of cylinders not specified",
1096 current_line_number);
1097 status = 0;
1098 }
1099 if (dos_cyls > 1024) {
1100 warnx(
1101 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1102 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1103 is dedicated to FreeBSD)",
1104 current_line_number, dos_cyls);
1105 }
1106
1107 if (dos_heads == 0) {
1108 warnx("ERROR line %d: number of heads not specified",
1109 current_line_number);
1110 status = 0;
1111 } else if (dos_heads > 256) {
1112 warnx("ERROR line %d: number of heads must be within (1-256)",
1113 current_line_number);
1114 status = 0;
1115 }
1116
1117 if (dos_sectors == 0) {
1118 warnx("ERROR line %d: number of sectors not specified",
1119 current_line_number);
1120 status = 0;
1121 } else if (dos_sectors > 63) {
1122 warnx("ERROR line %d: number of sectors must be within (1-63)",
1123 current_line_number);
1124 status = 0;
1125 }
1126
1127 break;
1128 }
1129 return (status);
1130 }
1131
1132 static u_int32_t
str2sectors(const char * str)1133 str2sectors(const char *str)
1134 {
1135 char *end;
1136 unsigned long val;
1137
1138 val = strtoul(str, &end, 0);
1139 if (str == end || *end == '\0') {
1140 warnx("ERROR line %d: unexpected size: \'%s\'",
1141 current_line_number, str);
1142 return NO_DISK_SECTORS;
1143 }
1144
1145 if (*end == 'K')
1146 val *= 1024UL / secsize;
1147 else if (*end == 'M')
1148 val *= 1024UL * 1024UL / secsize;
1149 else if (*end == 'G')
1150 val *= 1024UL * 1024UL * 1024UL / secsize;
1151 else {
1152 warnx("ERROR line %d: unexpected modifier: %c "
1153 "(not K/M/G)", current_line_number, *end);
1154 return NO_DISK_SECTORS;
1155 }
1156
1157 return val;
1158 }
1159
1160 static int
process_partition(CMD * command)1161 process_partition(CMD *command)
1162 {
1163 int status = 0, partition;
1164 u_int32_t prev_head_boundary, prev_cyl_boundary;
1165 u_int32_t adj_size, max_end;
1166 struct dos_partition *partp;
1167
1168 while (1) {
1169 part_processed = 1;
1170 if (command->n_args != 4) {
1171 warnx("ERROR line %d: incorrect number of partition args",
1172 current_line_number);
1173 break;
1174 }
1175 partition = command->args[0].arg_val;
1176 if (partition < 1 || partition > 4) {
1177 warnx("ERROR line %d: invalid partition number %d",
1178 current_line_number, partition);
1179 break;
1180 }
1181 partp = &mboot.parts[partition - 1];
1182 bzero(partp, sizeof (*partp));
1183 partp->dp_typ = command->args[1].arg_val;
1184 if (command->args[2].arg_str != NULL) {
1185 if (strcmp(command->args[2].arg_str, "*") == 0) {
1186 int i;
1187 partp->dp_start = dos_sectors;
1188 for (i = 1; i < partition; i++) {
1189 struct dos_partition *prev_partp;
1190 prev_partp = ((struct dos_partition *)
1191 &mboot.parts) + i - 1;
1192 if (prev_partp->dp_typ != 0)
1193 partp->dp_start = prev_partp->dp_start +
1194 prev_partp->dp_size;
1195 }
1196 if (partp->dp_start % dos_sectors != 0) {
1197 prev_head_boundary = partp->dp_start /
1198 dos_sectors * dos_sectors;
1199 partp->dp_start = prev_head_boundary +
1200 dos_sectors;
1201 }
1202 } else {
1203 partp->dp_start = str2sectors(command->args[2].arg_str);
1204 if (partp->dp_start == NO_DISK_SECTORS)
1205 break;
1206 }
1207 } else
1208 partp->dp_start = command->args[2].arg_val;
1209
1210 if (command->args[3].arg_str != NULL) {
1211 if (strcmp(command->args[3].arg_str, "*") == 0)
1212 partp->dp_size = ((disksecs / dos_cylsecs) *
1213 dos_cylsecs) - partp->dp_start;
1214 else {
1215 partp->dp_size = str2sectors(command->args[3].arg_str);
1216 if (partp->dp_size == NO_DISK_SECTORS)
1217 break;
1218 }
1219 prev_cyl_boundary = ((partp->dp_start + partp->dp_size) /
1220 dos_cylsecs) * dos_cylsecs;
1221 if (prev_cyl_boundary > partp->dp_start)
1222 partp->dp_size = prev_cyl_boundary - partp->dp_start;
1223 } else
1224 partp->dp_size = command->args[3].arg_val;
1225
1226 max_end = partp->dp_start + partp->dp_size;
1227
1228 if (partp->dp_typ == 0) {
1229 /*
1230 * Get out, the partition is marked as unused.
1231 */
1232 /*
1233 * Insure that it's unused.
1234 */
1235 bzero(partp, sizeof(*partp));
1236 status = 1;
1237 break;
1238 }
1239
1240 /*
1241 * Adjust start upwards, if necessary, to fall on a head boundary.
1242 */
1243 if (partp->dp_start % dos_sectors != 0) {
1244 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1245 if (max_end < dos_sectors ||
1246 prev_head_boundary > max_end - dos_sectors) {
1247 /*
1248 * Can't go past end of partition
1249 */
1250 warnx(
1251 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1252 a head boundary",
1253 current_line_number, partition);
1254 break;
1255 }
1256 warnx(
1257 "WARNING: adjusting start offset of partition %d\n\
1258 from %u to %u, to fall on a head boundary",
1259 partition, (u_int)partp->dp_start,
1260 (u_int)(prev_head_boundary + dos_sectors));
1261 partp->dp_start = prev_head_boundary + dos_sectors;
1262 }
1263
1264 /*
1265 * Adjust size downwards, if necessary, to fall on a cylinder
1266 * boundary.
1267 */
1268 prev_cyl_boundary =
1269 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1270 if (prev_cyl_boundary > partp->dp_start)
1271 adj_size = prev_cyl_boundary - partp->dp_start;
1272 else {
1273 warnx(
1274 "ERROR: could not adjust partition to start on a head boundary\n\
1275 and end on a cylinder boundary.");
1276 return (0);
1277 }
1278 if (adj_size != partp->dp_size) {
1279 warnx(
1280 "WARNING: adjusting size of partition %d from %u to %u\n\
1281 to end on a cylinder boundary",
1282 partition, (u_int)partp->dp_size, (u_int)adj_size);
1283 partp->dp_size = adj_size;
1284 }
1285 if (partp->dp_size == 0) {
1286 warnx("ERROR line %d: size of partition %d is zero",
1287 current_line_number, partition);
1288 break;
1289 }
1290
1291 dos(partp);
1292 status = 1;
1293 break;
1294 }
1295 return (status);
1296 }
1297
1298
1299 static int
process_active(CMD * command)1300 process_active(CMD *command)
1301 {
1302 int status = 0, partition, i;
1303 struct dos_partition *partp;
1304
1305 while (1) {
1306 active_processed = 1;
1307 if (command->n_args != 1) {
1308 warnx("ERROR line %d: incorrect number of active args",
1309 current_line_number);
1310 status = 0;
1311 break;
1312 }
1313 partition = command->args[0].arg_val;
1314 if (partition < 1 || partition > 4) {
1315 warnx("ERROR line %d: invalid partition number %d",
1316 current_line_number, partition);
1317 break;
1318 }
1319 /*
1320 * Reset active partition
1321 */
1322 partp = mboot.parts;
1323 for (i = 0; i < NDOSPART; i++)
1324 partp[i].dp_flag = 0;
1325 partp[partition-1].dp_flag = ACTIVE;
1326
1327 status = 1;
1328 break;
1329 }
1330 return (status);
1331 }
1332
1333
1334 static int
process_line(char * line)1335 process_line(char *line)
1336 {
1337 CMD command;
1338 int status = 1;
1339
1340 while (1) {
1341 parse_config_line(line, &command);
1342 switch (command.cmd) {
1343 case 0:
1344 /*
1345 * Comment or blank line
1346 */
1347 break;
1348 case 'g':
1349 /*
1350 * Set geometry
1351 */
1352 status = process_geometry(&command);
1353 break;
1354 case 'p':
1355 status = process_partition(&command);
1356 break;
1357 case 'a':
1358 status = process_active(&command);
1359 break;
1360 default:
1361 status = 0;
1362 break;
1363 }
1364 break;
1365 }
1366 return (status);
1367 }
1368
1369
1370 static int
read_config(char * config_file)1371 read_config(char *config_file)
1372 {
1373 FILE *fp = NULL;
1374 int status = 1;
1375 char buf[1010];
1376
1377 while (1) {
1378 if (strcmp(config_file, "-") != 0) {
1379 /*
1380 * We're not reading from stdin
1381 */
1382 if ((fp = fopen(config_file, "r")) == NULL) {
1383 status = 0;
1384 break;
1385 }
1386 } else {
1387 fp = stdin;
1388 }
1389 current_line_number = 0;
1390 while (!feof(fp)) {
1391 if (fgets(buf, sizeof(buf), fp) == NULL)
1392 break;
1393 ++current_line_number;
1394 status = process_line(buf);
1395 if (status == 0)
1396 break;
1397 }
1398 break;
1399 }
1400 if (fp) {
1401 /*
1402 * It doesn't matter if we're reading from stdin, as we've reached EOF
1403 */
1404 fclose(fp);
1405 }
1406 return (status);
1407 }
1408
1409
1410 static void
reset_boot(void)1411 reset_boot(void)
1412 {
1413 int i;
1414 struct dos_partition *partp;
1415
1416 init_boot();
1417 for (i = 0; i < 4; ++i) {
1418 partp = &mboot.parts[i];
1419 bzero(partp, sizeof(*partp));
1420 }
1421 }
1422
1423 static int
sanitize_partition(struct dos_partition * partp)1424 sanitize_partition(struct dos_partition *partp)
1425 {
1426 u_int32_t prev_head_boundary, prev_cyl_boundary;
1427 u_int32_t max_end, size, start;
1428
1429 start = partp->dp_start;
1430 size = partp->dp_size;
1431 max_end = start + size;
1432 /* Only allow a zero size if the partition is being marked unused. */
1433 if (size == 0) {
1434 if (start == 0 && partp->dp_typ == 0)
1435 return (1);
1436 warnx("ERROR: size of partition is zero");
1437 return (0);
1438 }
1439 /* Return if no adjustment is necessary. */
1440 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1441 return (1);
1442
1443 if (start == 0) {
1444 warnx("WARNING: partition overlaps with partition table");
1445 if (ok("Correct this automatically?"))
1446 start = dos_sectors;
1447 }
1448 if (start % dos_sectors != 0)
1449 warnx("WARNING: partition does not start on a head boundary");
1450 if ((start +size) % dos_sectors != 0)
1451 warnx("WARNING: partition does not end on a cylinder boundary");
1452 warnx("WARNING: this may confuse the BIOS or some operating systems");
1453 if (!ok("Correct this automatically?"))
1454 return (1);
1455
1456 /*
1457 * Adjust start upwards, if necessary, to fall on a head boundary.
1458 */
1459 if (start % dos_sectors != 0) {
1460 prev_head_boundary = start / dos_sectors * dos_sectors;
1461 if (max_end < dos_sectors ||
1462 prev_head_boundary >= max_end - dos_sectors) {
1463 /*
1464 * Can't go past end of partition
1465 */
1466 warnx(
1467 "ERROR: unable to adjust start of partition to fall on a head boundary");
1468 return (0);
1469 }
1470 start = prev_head_boundary + dos_sectors;
1471 }
1472
1473 /*
1474 * Adjust size downwards, if necessary, to fall on a cylinder
1475 * boundary.
1476 */
1477 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1478 if (prev_cyl_boundary > start)
1479 size = prev_cyl_boundary - start;
1480 else {
1481 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1482 and end on a cylinder boundary.");
1483 return (0);
1484 }
1485
1486 /* Finally, commit any changes to partp and return. */
1487 if (start != partp->dp_start) {
1488 warnx("WARNING: adjusting start offset of partition to %u",
1489 (u_int)start);
1490 partp->dp_start = start;
1491 }
1492 if (size != partp->dp_size) {
1493 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1494 partp->dp_size = size;
1495 }
1496
1497 return (1);
1498 }
1499
1500 /*
1501 * Try figuring out the root device's canonical disk name.
1502 * The following choices are considered:
1503 * /dev/ad0s1a => /dev/ad0
1504 * /dev/da0a => /dev/da0
1505 * /dev/vinum/root => /dev/vinum/root
1506 * A ".eli" part is removed if it exists (see geli(8)).
1507 * A ".journal" ending is removed if it exists (see gjournal(8)).
1508 */
1509 static char *
get_rootdisk(void)1510 get_rootdisk(void)
1511 {
1512 struct statfs rootfs;
1513 regex_t re;
1514 #define NMATCHES 2
1515 regmatch_t rm[NMATCHES];
1516 char dev[PATH_MAX], *s;
1517 int rv;
1518
1519 if (statfs("/", &rootfs) == -1)
1520 err(1, "statfs(\"/\")");
1521
1522 if ((rv = regcomp(&re, "^(/dev/[a-z/]+[0-9]*)([sp][0-9]+)?[a-h]?(\\.journal)?$",
1523 REG_EXTENDED)) != 0)
1524 errx(1, "regcomp() failed (%d)", rv);
1525 strlcpy(dev, rootfs.f_mntfromname, sizeof (dev));
1526 if ((s = strstr(dev, ".eli")) != NULL)
1527 memmove(s, s+4, strlen(s + 4) + 1);
1528
1529 if ((rv = regexec(&re, dev, NMATCHES, rm, 0)) != 0)
1530 errx(1,
1531 "mounted root fs resource doesn't match expectations (regexec returned %d)",
1532 rv);
1533 if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL)
1534 errx(1, "out of memory");
1535 memcpy(s, rootfs.f_mntfromname + rm[1].rm_so,
1536 rm[1].rm_eo - rm[1].rm_so);
1537 s[rm[1].rm_eo - rm[1].rm_so] = 0;
1538
1539 return s;
1540 }
1541