fdisk.c revision 1.111 1 /* $NetBSD: fdisk.c,v 1.111 2007/12/23 08:14:28 apb Exp $ */
2
3 /*
4 * Mach Operating System
5 * Copyright (c) 1992 Carnegie Mellon University
6 * All Rights Reserved.
7 *
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
13 *
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 *
18 * Carnegie Mellon requests users of this software to return to
19 *
20 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
24 *
25 * any improvements or extensions that they make and grant Carnegie Mellon
26 * the rights to redistribute these changes.
27 */
28
29 /*
30 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
31 * Copyright (c) 1989 Robert. V. Baron
32 * Created.
33 */
34
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
38
39 #include <sys/cdefs.h>
40
41 #ifndef lint
42 __RCSID("$NetBSD: fdisk.c,v 1.111 2007/12/23 08:14:28 apb Exp $");
43 #endif /* not lint */
44
45 #define MBRPTYPENAMES
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/stat.h>
49 #include <ctype.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <paths.h>
54 #include <stdarg.h>
55 #include <stddef.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <vis.h>
61
62 #if !HAVE_NBTOOL_CONFIG_H
63 #include <sys/disklabel.h>
64 #include <sys/bootblock.h>
65 #include <sys/ioctl.h>
66 #include <sys/sysctl.h>
67 #include <disktab.h>
68 #include <util.h>
69 #else
70 #include <nbinclude/sys/disklabel.h>
71 #include <nbinclude/sys/bootblock.h>
72 #include "../../include/disktab.h"
73 /* We enforce -F, so none of these possibly undefined items can be needed */
74 #define opendisk(path, fl, buf, buflen, cooked) (-1)
75 #ifndef DIOCGDEFLABEL
76 #define DIOCGDEFLABEL 0
77 #endif
78 #ifndef DIOCGDINFO
79 #define DIOCGDINFO 0
80 #endif
81 #ifndef DIOCWLABEL
82 #define DIOCWLABEL 0
83 #endif
84 #endif /* HAVE_NBTOOL_CONFIG_H */
85
86 #define DEFAULT_BOOTDIR "/usr/mdec"
87
88 #define LE_MBR_MAGIC htole16(MBR_MAGIC)
89 #define LE_MBR_BS_MAGIC htole16(MBR_BS_MAGIC)
90
91 #if defined(__i386__) || defined(__x86_64__)
92 #if !HAVE_NBTOOL_CONFIG_H
93 #include <machine/cpu.h>
94 #endif /* !HAVE_NBTOOL_CONFIG_H */
95 #define BOOTSEL
96 #endif
97
98 #ifdef BOOTSEL
99
100 #define DEFAULT_BOOTCODE "mbr"
101 #define DEFAULT_BOOTSELCODE "mbr_bootsel"
102 #define DEFAULT_BOOTEXTCODE "mbr_ext"
103
104 /* Scan values for the various keys we use, as returned by the BIOS */
105 #define SCAN_ENTER 0x1c
106 #define SCAN_F1 0x3b
107 #define SCAN_1 0x2
108
109 #endif
110
111 #define LBUF 100
112 static char lbuf[LBUF];
113
114 #ifndef PRIdaddr
115 #define PRIdaddr PRId64
116 #endif
117
118 #ifndef PRId64
119 #if HAVE_LONG_LONG
120 #define PRId64 "lld"
121 #endif
122 #endif
123
124 #ifndef PRId32
125 #define PRId32 "ld"
126 #endif
127
128 #ifndef _PATH_DEFDISK
129 #define _PATH_DEFDISK "/dev/rwd0d"
130 #endif
131
132 const char *disk = _PATH_DEFDISK;
133
134 struct disklabel disklabel; /* disk parameters */
135
136 unsigned int cylinders, sectors, heads;
137 daddr_t disksectors;
138 #define cylindersectors (heads * sectors)
139
140 struct mbr_sector mboot;
141
142
143 struct {
144 struct mbr_sector *ptn; /* array of pbrs */
145 daddr_t base; /* first sector of ext. ptn */
146 daddr_t limit; /* last sector of ext. ptn */
147 int num_ptn; /* number of contained partitions */
148 int ptn_id; /* entry in mbr */
149 int is_corrupt; /* 1 if extended chain illegal */
150 } ext;
151
152 const char *boot_dir = DEFAULT_BOOTDIR;
153 char *boot_path = 0; /* name of file we actually opened */
154
155 #ifdef BOOTSEL
156
157 #define DEFAULT_ACTIVE (~(daddr_t)0)
158
159 #define OPTIONS "0123BFSafiluvs:b:c:E:r:w:t:T:"
160 #else
161 #define change_part(e, p, id, st, sz, bm) change__part(e, p, id, st, sz)
162 #define OPTIONS "0123FSafiluvs:b:c:E:r:w:"
163 #endif
164
165 unsigned int dos_cylinders;
166 unsigned int dos_heads;
167 unsigned int dos_sectors;
168 daddr_t dos_disksectors;
169 #define dos_cylindersectors (dos_heads * dos_sectors)
170 #define dos_totalsectors (dos_heads * dos_sectors * dos_cylinders)
171
172 #define DOSSECT(s,c) (((s) & 0x3f) | (((c) >> 2) & 0xc0))
173 #define DOSCYL(c) ((c) & 0xff)
174 #define SEC_IN_1M (1024 * 1024 / 512)
175 #define SEC_TO_MB(sec) ((unsigned int)(((sec) + SEC_IN_1M / 2) / SEC_IN_1M))
176 #define SEC_TO_CYL(sec) (((sec) + dos_cylindersectors/2) / dos_cylindersectors)
177
178 #define MAXCYL 1024 /* Usual limit is 1023 */
179 #define MAXHEAD 256 /* Usual limit is 255 */
180 #define MAXSECTOR 63
181 int partition = -1;
182
183 int fd = -1, wfd = -1, *rfd = &fd;
184 char *disk_file = NULL;
185 char *disk_type = NULL;
186
187 int a_flag; /* set active partition */
188 int i_flag; /* init bootcode */
189 int u_flag; /* update partition data */
190 int v_flag; /* more verbose */
191 int sh_flag; /* Output data as shell defines */
192 int f_flag; /* force --not interactive */
193 int s_flag; /* set id,offset,size */
194 int b_flag; /* Set cyl, heads, secs (as c/h/s) */
195 int B_flag; /* Edit/install bootselect code */
196 int E_flag; /* extended partition number */
197 int b_cyl, b_head, b_sec; /* b_flag values. */
198
199 #if !HAVE_NBTOOL_CONFIG_H
200 int F_flag = 0;
201 #else
202 /* Tool - force 'file' mode to avoid unsupported functions and ioctls */
203 int F_flag = 1;
204 #endif
205
206 struct mbr_sector bootcode[8192 / sizeof (struct mbr_sector)];
207 int bootsize; /* actual size of bootcode */
208 int boot_installed; /* 1 if we've copied code into the mbr */
209
210 #if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
211 struct disklist *dl;
212 #endif
213
214
215 #define KNOWN_SYSIDS (sizeof(mbr_ptypes)/sizeof(mbr_ptypes[0]))
216
217 void usage(void);
218 void print_s0(int);
219 void print_part(struct mbr_sector *, int, daddr_t);
220 void print_mbr_partition(struct mbr_sector *, int, daddr_t, daddr_t, int);
221 void print_pbr(daddr_t, int, uint8_t);
222 int is_all_zero(const unsigned char *, size_t);
223 void printvis(int, const char *, const char *, size_t);
224 int read_boot(const char *, void *, size_t, int);
225 void init_sector0(int);
226 void intuit_translated_geometry(void);
227 void get_geometry(void);
228 void get_extended_ptn(void);
229 #if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
230 void get_diskname(const char *, char *, size_t);
231 #endif /* (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H */
232 int change_part(int, int, int, daddr_t, daddr_t, char *);
233 void print_params(void);
234 void change_active(int);
235 void get_params_to_use(void);
236 void dos(int, unsigned char *, unsigned char *, unsigned char *);
237 int open_disk(int);
238 int read_disk(daddr_t, void *);
239 int write_disk(daddr_t, void *);
240 int get_params(void);
241 int read_s0(daddr_t, struct mbr_sector *);
242 int write_mbr(void);
243 int yesno(const char *, ...);
244 int decimal(const char *, int, int, int, int);
245 #define DEC_SEC 1 /* asking for a sector number */
246 #define DEC_RND 2 /* round to end of first track */
247 #define DEC_RND_0 4 /* round 0 to size of a track */
248 #define DEC_RND_DOWN 8 /* subtract 1 track */
249 #define DEC_RND_DOWN_2 16 /* subtract 2 tracks */
250 void string(const char *, int, char *);
251 int ptn_id(const char *, int *);
252 int type_match(const void *, const void *);
253 const char *get_type(int);
254 int get_mapping(int, unsigned int *, unsigned int *, unsigned int *, unsigned long *);
255 #ifdef BOOTSEL
256 daddr_t configure_bootsel(daddr_t);
257 void install_bootsel(int);
258 daddr_t get_default_boot(void);
259 void set_default_boot(daddr_t);
260 #endif
261
262 static void
263 initvar_disk(const char **diskp)
264 {
265 #if !HAVE_NBTOOL_CONFIG_H
266 int mib[2];
267 size_t len;
268 char *root_device;
269
270 mib[0] = CTL_KERN;
271 mib[1] = KERN_ROOT_DEVICE;
272 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1 ||
273 (root_device = malloc(len)) == NULL ||
274 sysctl(mib, 2, root_device, &len, NULL, 0) == -1)
275 return;
276
277 *diskp = root_device;
278 #endif /* HAVE_NBTOOL_CONFIG_H */
279 }
280
281 int
282 main(int argc, char *argv[])
283 {
284 struct stat sb;
285 int ch;
286 size_t len;
287 char *cp;
288 int n;
289 #ifdef BOOTSEL
290 daddr_t default_ptn; /* start sector of default ptn */
291 char *cbootmenu = 0;
292 #endif
293
294 int csysid, cstart, csize; /* For the b_flag. */
295
296 a_flag = i_flag = u_flag = sh_flag = f_flag = s_flag = b_flag = 0;
297 v_flag = 0;
298 E_flag = 0;
299 csysid = cstart = csize = 0;
300 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
301 switch (ch) {
302 case '0':
303 partition = 0;
304 break;
305 case '1':
306 partition = 1;
307 break;
308 case '2':
309 partition = 2;
310 break;
311 case '3':
312 partition = 3;
313 break;
314 case 'E': /* Extended partition number */
315 E_flag = 1;
316 partition = strtoul(optarg, &cp, 0);
317 if (*cp || partition < 0)
318 errx(1, "Bad partition number -E %s.", optarg);
319 break;
320 #ifdef BOOTSEL
321 case 'B': /* Bootselect parameters */
322 B_flag = 1;
323 break;
324 #endif
325 case 'F': /* device argument is really a file */
326 F_flag = 1;
327 break;
328 case 'S': /* Output as shell variables */
329 sh_flag = 1;
330 break;
331 case 'a': /* Set active partition */
332 a_flag = 1;
333 break;
334 case 'f': /* Non interactive */
335 f_flag = 1;
336 break;
337 case 'i': /* Always update bootcode */
338 i_flag = 1;
339 break;
340 case 'l': /* List known partition types */
341 for (len = 0; len < KNOWN_SYSIDS; len++)
342 printf("%03d %s\n", mbr_ptypes[len].id,
343 mbr_ptypes[len].name);
344 return 0;
345 case 'u': /* Update partition details */
346 u_flag = 1;
347 break;
348 case 'v': /* Be verbose */
349 v_flag++;
350 break;
351 case 's': /* Partition details */
352 s_flag = 1;
353 if (sscanf(optarg, "%d/%d/%d%n", &csysid, &cstart,
354 &csize, &n) == 3) {
355 if (optarg[n] == 0)
356 break;
357 #ifdef BOOTSEL
358 if (optarg[n] == '/') {
359 cbootmenu = optarg + n + 1;
360 break;
361 }
362 #endif
363 }
364 errx(1, "Bad argument to the -s flag.");
365 break;
366 case 'b': /* BIOS geometry */
367 b_flag = 1;
368 if (sscanf(optarg, "%d/%d/%d%n", &b_cyl, &b_head,
369 &b_sec, &n) != 3 || optarg[n] != 0)
370 errx(1, "Bad argument to the -b flag.");
371 if (b_cyl > MAXCYL)
372 b_cyl = MAXCYL;
373 break;
374 case 'c': /* file/directory containing boot code */
375 if (strchr(optarg, '/') != NULL &&
376 stat(optarg, &sb) == 0 &&
377 (sb.st_mode & S_IFMT) == S_IFDIR) {
378 boot_dir = optarg;
379 break;
380 }
381 bootsize = read_boot(optarg, bootcode,
382 sizeof bootcode, 1);
383 i_flag = 1;
384 break;
385 case 'r': /* read data from disk_file (not raw disk) */
386 rfd = &wfd;
387 /* FALLTHROUGH */
388 case 'w': /* write data to disk_file */
389 disk_file = optarg;
390 break;
391 case 't':
392 if (setdisktab(optarg) == -1)
393 errx(EXIT_FAILURE, "bad disktab");
394 break;
395 case 'T':
396 disk_type = optarg;
397 break;
398 default:
399 usage();
400 }
401 argc -= optind;
402 argv += optind;
403
404 if (disk_type != NULL && getdiskbyname(disk_type) == NULL)
405 errx(EXIT_FAILURE, "bad disktype");
406
407 if (sh_flag && (a_flag || i_flag || u_flag || f_flag || s_flag))
408 usage();
409
410 if (B_flag && f_flag) {
411 warnx("Bootselector may only be configured interactively");
412 usage();
413 }
414
415 if (f_flag && u_flag && !s_flag) {
416 warnx("Partition data not specified");
417 usage();
418 }
419
420 if (s_flag && partition == -1) {
421 warnx("-s flag requires a partition selected.");
422 usage();
423 }
424
425 if (argc > 1)
426 usage();
427
428 if (argc > 0)
429 disk = argv[0];
430 else if (!F_flag) {
431 /* Default to boot device */
432 initvar_disk(&disk);
433 }
434
435 if (open_disk(B_flag || a_flag || i_flag || u_flag) < 0)
436 exit(1);
437
438 if (read_s0(0, &mboot))
439 /* must have been a blank disk */
440 init_sector0(1);
441
442 #if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
443 get_geometry();
444 #else
445 intuit_translated_geometry();
446 #endif
447 get_extended_ptn();
448
449 #ifdef BOOTSEL
450 default_ptn = get_default_boot();
451 #endif
452
453 if (E_flag && !u_flag && partition >= ext.num_ptn)
454 errx(1, "Extended partition %d is not defined.", partition);
455
456 if (u_flag && (!f_flag || b_flag))
457 get_params_to_use();
458
459 /* Do the update stuff! */
460 if (u_flag) {
461 if (s_flag)
462 change_part(E_flag, partition, csysid, cstart, csize,
463 cbootmenu);
464 else {
465 int part = partition, chg_ext = E_flag, prompt = 1;
466 do {
467 if (prompt) {
468 printf("\n");
469 print_s0(partition);
470 }
471 if (partition == -1)
472 part = ptn_id(
473 "Which partition do you want to change?",
474 &chg_ext);
475 if (part < 0)
476 break;
477 prompt = change_part(chg_ext, part, 0, 0, 0, 0);
478 } while (partition == -1);
479 }
480 } else
481 if (!i_flag && !B_flag) {
482 print_params();
483 print_s0(partition);
484 }
485
486 if (a_flag && !E_flag)
487 change_active(partition);
488
489 #ifdef BOOTSEL
490 if (B_flag || u_flag || i_flag)
491 /* Ensure the mbr code supports this configuration */
492 install_bootsel(0);
493 if (B_flag)
494 default_ptn = configure_bootsel(default_ptn);
495 set_default_boot(default_ptn);
496 #else
497 if (i_flag)
498 init_sector0(0);
499 #endif
500
501 if (u_flag || a_flag || i_flag || B_flag) {
502 if (!f_flag) {
503 printf("\nWe haven't written the MBR back to disk "
504 "yet. This is your last chance.\n");
505 if (u_flag)
506 print_s0(-1);
507 if (yesno("Should we write new partition table?"))
508 write_mbr();
509 } else
510 write_mbr();
511 }
512
513 exit(0);
514 }
515
516 void
517 usage(void)
518 {
519 int indent = 7 + (int)strlen(getprogname()) + 1;
520
521 (void)fprintf(stderr, "usage: %s [-afiluvBS] "
522 "[-b cylinders/heads/sectors] \\\n"
523 "%*s[-0123 | -E num "
524 "[-s id/start/size[/bootmenu]]] \\\n"
525 "%*s[-t disktab] [-T disktype] \\\n"
526 "%*s[-c bootcode] "
527 "[-r|-w file] [device]\n"
528 "\t-a change active partition\n"
529 "\t-f force - not interactive\n"
530 "\t-i initialise MBR code\n"
531 "\t-l list partition types\n"
532 "\t-u update partition data\n"
533 "\t-v verbose output, -v -v more verbose still\n"
534 "\t-B update bootselect options\n"
535 "\t-F treat device as a regular file\n"
536 "\t-S output as shell defines\n"
537 "\t-r and -w access 'file' for non-destructive testing\n",
538 getprogname(), indent, "", indent, "", indent, "");
539 exit(1);
540 }
541
542 static daddr_t
543 ext_offset(int part)
544 {
545 daddr_t offset = ext.base;
546
547 if (part != 0)
548 offset += le32toh(ext.ptn[part - 1].mbr_parts[1].mbrp_start);
549 return offset;
550 }
551
552 void
553 print_s0(int which)
554 {
555 int part;
556
557 if (which == -1) {
558 if (!sh_flag)
559 printf("Partition table:\n");
560 for (part = 0; part < MBR_PART_COUNT; part++) {
561 if (!sh_flag)
562 printf("%d: ", part);
563 print_part(&mboot, part, 0);
564 }
565 if (!sh_flag) {
566 if (ext.is_corrupt)
567 printf("Extended partition table is corrupt\n");
568 else
569 if (ext.num_ptn != 0)
570 printf("Extended partition table:\n");
571 }
572 for (part = 0; part < ext.num_ptn; part++) {
573 if (!sh_flag)
574 printf("E%d: ", part);
575 print_part(&ext.ptn[part], 0, ext_offset(part));
576 if (!sh_flag && v_flag >= 2) {
577 printf("link: ");
578 print_mbr_partition(&ext.ptn[part], 1,
579 ext_offset(part), ext.base, 0);
580 }
581 }
582 #ifdef BOOTSEL
583 if (!sh_flag && mboot.mbr_bootsel_magic == LE_MBR_BS_MAGIC) {
584 int tmo;
585
586 printf("Bootselector ");
587 if (mboot.mbr_bootsel.mbrbs_flags & MBR_BS_ACTIVE) {
588 printf("enabled");
589 tmo = le16toh(mboot.mbr_bootsel.mbrbs_timeo);
590 if (tmo == 0xffff)
591 printf(", infinite timeout");
592 else
593 printf(", timeout %d seconds",
594 (10 * tmo + 9) / 182);
595 } else
596 printf("disabled");
597 printf(".\n");
598 }
599 #endif
600
601 if (!sh_flag && mboot.mbr_dsn != 0)
602 printf("Drive serial number: %"PRId32" (0x%08x)\n",
603 le32toh(mboot.mbr_dsn),
604 le32toh(mboot.mbr_dsn));
605 return;
606 }
607
608 if (E_flag) {
609 if (!sh_flag)
610 printf("Extended partition E%d:\n", which);
611 if (which > ext.num_ptn)
612 printf("Undefined\n");
613 else
614 print_part(&ext.ptn[which], 0, ext_offset(which));
615 } else {
616 if (!sh_flag)
617 printf("Partition %d:\n", which);
618 print_part(&mboot, which, 0);
619 }
620 }
621
622 void
623 print_part(struct mbr_sector *boot, int part, daddr_t offset)
624 {
625 struct mbr_partition *partp;
626 const char *e;
627
628 if (!sh_flag) {
629 print_mbr_partition(boot, part, offset, 0, 0);
630 return;
631 }
632
633 partp = &boot->mbr_parts[part];
634 if (boot != &mboot) {
635 part = boot - ext.ptn;
636 e = "E";
637 } else
638 e = "";
639
640 if (partp->mbrp_type == 0) {
641 printf("PART%s%dSIZE=0\n", e, part);
642 return;
643 }
644
645 printf("PART%s%dID=%d\n", e, part, partp->mbrp_type);
646 printf("PART%s%dSIZE=%u\n", e, part, le32toh(partp->mbrp_size));
647 printf("PART%s%dSTART=%"PRIdaddr"\n", e, part,
648 offset + le32toh(partp->mbrp_start));
649 printf("PART%s%dFLAG=0x%x\n", e, part, partp->mbrp_flag);
650 printf("PART%s%dBCYL=%d\n", e, part,
651 MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect));
652 printf("PART%s%dBHEAD=%d\n", e, part, partp->mbrp_shd);
653 printf("PART%s%dBSEC=%d\n", e, part, MBR_PSECT(partp->mbrp_ssect));
654 printf("PART%s%dECYL=%d\n", e, part,
655 MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect));
656 printf("PART%s%dEHEAD=%d\n", e, part, partp->mbrp_ehd);
657 printf("PART%s%dESEC=%d\n", e, part, MBR_PSECT(partp->mbrp_esect));
658 }
659
660 static void
661 pr_cyls(daddr_t sector, int is_end)
662 {
663 unsigned long cyl, head, sect;
664 cyl = sector / dos_cylindersectors;
665 sect = sector - cyl * dos_cylindersectors;
666 head = sect / dos_sectors;
667 sect -= head * dos_sectors;
668
669 printf("%lu", cyl);
670
671 if (is_end) {
672 if (head == dos_heads - 1 && sect == dos_sectors - 1)
673 return;
674 } else {
675 if (head == 0 && sect == 0)
676 return;
677 }
678
679 printf("/%lu/%lu", head, sect + 1);
680 }
681
682 void
683 print_mbr_partition(struct mbr_sector *boot, int part,
684 daddr_t offset, daddr_t exoffset, int indent)
685 {
686 daddr_t start;
687 daddr_t size;
688 struct mbr_partition *partp = &boot->mbr_parts[part];
689 struct mbr_sector eboot;
690 int p;
691 static int dumped = 0;
692
693 if (partp->mbrp_type == 0 && v_flag < 2) {
694 printf("<UNUSED>\n");
695 return;
696 }
697
698 start = le32toh(partp->mbrp_start);
699 size = le32toh(partp->mbrp_size);
700 if (MBR_IS_EXTENDED(partp->mbrp_type))
701 start += exoffset;
702 else
703 start += offset;
704
705 printf("%s (sysid %d)\n", get_type(partp->mbrp_type), partp->mbrp_type);
706 #ifdef BOOTSEL
707 if (boot->mbr_bootsel_magic == LE_MBR_BS_MAGIC &&
708 boot->mbr_bootsel.mbrbs_nametab[part][0])
709 printf("%*s bootmenu: %s\n", indent, "",
710 boot->mbr_bootsel.mbrbs_nametab[part]);
711 #endif
712
713 printf("%*s start %"PRIdaddr", size %"PRIdaddr,
714 indent, "", start, size);
715 if (size != 0) {
716 printf(" (%u MB, Cyls ", SEC_TO_MB(size));
717 if (v_flag == 0 && le32toh(partp->mbrp_start) == dos_sectors)
718 pr_cyls(start - dos_sectors, 0);
719 else
720 pr_cyls(start, 0);
721 printf("-");
722 pr_cyls(start + size - 1, 1);
723 printf(")");
724 }
725
726 switch (partp->mbrp_flag) {
727 case 0:
728 break;
729 case MBR_PFLAG_ACTIVE:
730 printf(", Active");
731 break;
732 default:
733 printf(", flag 0x%x", partp->mbrp_flag);
734 break;
735 }
736 printf("\n");
737
738 if (v_flag) {
739 printf("%*s beg: cylinder %4d, head %3d, sector %2d\n",
740 indent, "",
741 MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect),
742 partp->mbrp_shd, MBR_PSECT(partp->mbrp_ssect));
743 printf("%*s end: cylinder %4d, head %3d, sector %2d\n",
744 indent, "",
745 MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect),
746 partp->mbrp_ehd, MBR_PSECT(partp->mbrp_esect));
747 }
748
749 if (partp->mbrp_type == 0 && start == 0 && v_flag < 3)
750 return;
751
752 if (! MBR_IS_EXTENDED(partp->mbrp_type))
753 print_pbr(start, indent + 8, partp->mbrp_type);
754
755 if (!MBR_IS_EXTENDED(partp->mbrp_type) ||
756 (v_flag <= 2 && !ext.is_corrupt))
757 return;
758
759 /*
760 * Recursive dump extended table,
761 * This is read from the disk - so is wrong during editing.
762 * Just ensure we only show it once.
763 */
764 if (dumped)
765 return;
766
767 printf("%*s Extended partition table:\n", indent, "");
768 indent += 4;
769 if (read_s0(start, &eboot) == -1)
770 return;
771 for (p = 0; p < MBR_PART_COUNT; p++) {
772 printf("%*s%d: ", indent, "", p);
773 print_mbr_partition(&eboot, p, start,
774 exoffset ? exoffset : start, indent);
775 }
776
777 if (exoffset == 0)
778 dumped = 1;
779 }
780
781 /* Print a line with a label and a vis-encoded string */
782 void
783 printvis(int indent, const char *label, const char *buf, size_t size)
784 {
785 char *visbuf;
786
787 if ((visbuf = malloc(size * 4 + 1)) == NULL)
788 err(1, "Malloc failed");
789 strsvisx(visbuf, buf, size, VIS_TAB|VIS_NL|VIS_OCTAL, "\"");
790 printf("%*s%s: \"%s\"\n",
791 indent, "",
792 label, visbuf);
793 free(visbuf);
794 }
795
796 /* Check whether a buffer contains all bytes zero */
797 int
798 is_all_zero(const unsigned char *p, size_t size)
799 {
800
801 while (size-- > 0) {
802 if (*p++ != 0)
803 return 0;
804 }
805 return 1;
806 }
807
808 /*
809 * Report on the contents of a PBR sector.
810 *
811 * We first perform several sanity checks. If vflag >= 2, we report all
812 * failing tests, but for smaller values of v_flag we stop after the
813 * first failing test. Tests are ordered in an attempt to get the most
814 * useful error message from the first failing test.
815 *
816 * If v_flag >= 2, we also report some decoded values from the PBR.
817 * These results may be meaningless, if the PBR doesn't follow common
818 * conventions.
819 *
820 * Trying to decode anything more than the magic number in the last
821 * two bytes is a layering violation, but it can be very useful in
822 * diagnosing boot failures.
823 */
824 void
825 print_pbr(daddr_t sector, int indent, uint8_t part_type)
826 {
827 struct mbr_sector pboot;
828 unsigned char *p, *endp;
829 unsigned char val;
830 int ok;
831 int errcount = 0;
832
833 #define PBR_ERROR(...) \
834 do { \
835 ++errcount; \
836 printf("%*s%s: ", indent, "", \
837 (v_flag < 2 ? "PBR is not bootable" : "Not bootable")); \
838 printf(__VA_ARGS__); \
839 if (v_flag < 2) \
840 return; \
841 } while (/*CONSTCOND*/ 0)
842
843 if (v_flag >= 2) {
844 printf("%*sInformation from PBR:\n",
845 indent, "");
846 indent += 4;
847 }
848
849 if (read_disk(sector, &pboot) == -1) {
850 PBR_ERROR("Sector %"PRIdaddr" is unreadable (%s)\n",
851 sector, strerror(errno));
852 return;
853 }
854
855 /* all bytes identical? */
856 p = (unsigned char *)&pboot;
857 endp = p + sizeof(pboot);
858 val = *p;
859 ok = 0;
860 for (; p < endp; p++) {
861 if (*p != val) {
862 ok = 1;
863 break;
864 }
865 }
866 if (! ok)
867 PBR_ERROR("All bytes are identical (0x%02x)\n", val);
868
869 if (pboot.mbr_magic != LE_MBR_MAGIC)
870 PBR_ERROR("Bad magic number (0x%04x)\n",
871 le16toh(pboot.mbr_magic));
872
873 #if 0
874 /* Some i386 OS might fail this test. All non-i386 will fail. */
875 if (pboot.mbr_jmpboot[0] != 0xE9
876 && pboot.mbr_jmpboot[0] != 0xEB) {
877 PBR_ERROR("Does not begin with i386 JMP instruction"
878 " (0x%02x 0x%02x0 0x%02x)\n",
879 pboot.mbr_jmpboot[0], pboot.mbr_jmpboot[1],
880 pboot.mbr_jmpboot[2]);
881 }
882 #endif
883
884 if (v_flag > 0 && errcount == 0)
885 printf("%*sPBR appears to be bootable\n",
886 indent, "");
887 if (v_flag < 2)
888 return;
889
890 if (! is_all_zero(pboot.mbr_oemname, sizeof(pboot.mbr_oemname))) {
891 printvis(indent, "OEM name", (char *)pboot.mbr_oemname,
892 sizeof(pboot.mbr_oemname));
893 }
894
895 if (pboot.mbr_bpb.bpb16.bsBootSig == 0x29)
896 printf("%*sBPB FAT16 boot signature found\n",
897 indent, "");
898 if (pboot.mbr_bpb.bpb32.bsBootSig == 0x29)
899 printf("%*sBPB FAT32 boot signature found\n",
900 indent, "");
901
902 #undef PBR_ERROR
903 }
904
905 int
906 read_boot(const char *name, void *buf, size_t len, int err_exit)
907 {
908 int bfd, ret;
909 struct stat st;
910
911 if (boot_path != NULL)
912 free(boot_path);
913 if (strchr(name, '/') == 0)
914 asprintf(&boot_path, "%s/%s", boot_dir, name);
915 else
916 boot_path = strdup(name);
917 if (boot_path == NULL)
918 err(1, "Malloc failed");
919
920 if ((bfd = open(boot_path, O_RDONLY)) < 0 || fstat(bfd, &st) == -1) {
921 warn("%s", boot_path);
922 goto fail;
923 }
924
925 if (st.st_size > (off_t)len) {
926 warnx("%s: bootcode too large", boot_path);
927 goto fail;
928 }
929 ret = st.st_size;
930 if (ret < 0x200) {
931 warnx("%s: bootcode too small", boot_path);
932 goto fail;
933 }
934 if (read(bfd, buf, len) != ret) {
935 warn("%s", boot_path);
936 goto fail;
937 }
938
939 /*
940 * Do some sanity checking here
941 */
942 if (((struct mbr_sector *)buf)->mbr_magic != LE_MBR_MAGIC) {
943 warnx("%s: invalid magic", boot_path);
944 goto fail;
945 }
946
947 close(bfd);
948 ret = (ret + 0x1ff) & ~0x1ff;
949 return ret;
950
951 fail:
952 if (bfd >= 0)
953 close(bfd);
954 if (err_exit)
955 exit(1);
956 return 0;
957 }
958
959 void
960 init_sector0(int zappart)
961 {
962 int i;
963 int copy_size = offsetof(struct mbr_sector, mbr_dsn);
964
965 #ifdef DEFAULT_BOOTCODE
966 if (bootsize == 0)
967 bootsize = read_boot(DEFAULT_BOOTCODE, bootcode,
968 sizeof bootcode, 1);
969 #endif
970 #ifdef BOOTSEL
971 if (mboot.mbr_bootsel_magic == LE_MBR_BS_MAGIC
972 && bootcode[0].mbr_bootsel_magic == LE_MBR_BS_MAGIC)
973 copy_size = MBR_BS_OFFSET;
974 #endif
975
976 if (bootsize != 0) {
977 boot_installed = 1;
978 memcpy(&mboot, bootcode, copy_size);
979 mboot.mbr_bootsel_magic = bootcode[0].mbr_bootsel_magic;
980 }
981 mboot.mbr_magic = LE_MBR_MAGIC;
982
983 if (!zappart)
984 return;
985 for (i = 0; i < MBR_PART_COUNT; i++)
986 memset(&mboot.mbr_parts[i], 0, sizeof(mboot.mbr_parts[i]));
987 }
988
989 void
990 get_extended_ptn(void)
991 {
992 struct mbr_partition *mp;
993 struct mbr_sector *boot;
994 daddr_t offset;
995 struct mbr_sector *nptn;
996
997 /* find first (there should only be one) extended partition */
998 for (mp = mboot.mbr_parts; !MBR_IS_EXTENDED(mp->mbrp_type); mp++)
999 if (mp >= &mboot.mbr_parts[MBR_PART_COUNT])
1000 return;
1001
1002 /*
1003 * The extended partition should be structured as a linked list
1004 * (even though it appears, at first glance, to be a tree).
1005 */
1006 ext.base = le32toh(mp->mbrp_start);
1007 ext.limit = ext.base + le32toh(mp->mbrp_size);
1008 ext.ptn_id = mp - mboot.mbr_parts;
1009 for (offset = 0;; offset = le32toh(boot->mbr_parts[1].mbrp_start)) {
1010 nptn = realloc(ext.ptn, (ext.num_ptn + 1) * sizeof *ext.ptn);
1011 if (nptn == NULL)
1012 err(1, "Malloc failed");
1013 ext.ptn = nptn;
1014 boot = ext.ptn + ext.num_ptn;
1015 if (read_s0(offset + ext.base, boot) == -1)
1016 break;
1017 /* expect p0 to be valid and p1 to be another extended ptn */
1018 if (MBR_IS_EXTENDED(boot->mbr_parts[0].mbrp_type))
1019 break;
1020 if (boot->mbr_parts[1].mbrp_type != 0 &&
1021 !MBR_IS_EXTENDED(boot->mbr_parts[1].mbrp_type))
1022 break;
1023 /* p2 and p3 should be unallocated */
1024 if (boot->mbr_parts[2].mbrp_type != 0 ||
1025 boot->mbr_parts[3].mbrp_type != 0)
1026 break;
1027 /* data ptn inside extended one */
1028 if (boot->mbr_parts[0].mbrp_type != 0 &&
1029 offset + le32toh(boot->mbr_parts[0].mbrp_start)
1030 + le32toh(boot->mbr_parts[0].mbrp_size) > ext.limit)
1031 break;
1032
1033 ext.num_ptn++;
1034
1035 if (boot->mbr_parts[1].mbrp_type == 0)
1036 /* end of extended partition chain */
1037 return;
1038 /* must be in sector order */
1039 if (offset >= le32toh(boot->mbr_parts[1].mbrp_start))
1040 break;
1041 }
1042
1043 warnx("Extended partition table is corrupt\n");
1044 ext.is_corrupt = 1;
1045 ext.num_ptn = 0;
1046 }
1047
1048 #if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
1049 void
1050 get_diskname(const char *fullname, char *diskname, size_t size)
1051 {
1052 const char *p, *p2;
1053 size_t len;
1054
1055 p = strrchr(fullname, '/');
1056 if (p == NULL)
1057 p = fullname;
1058 else
1059 p++;
1060
1061 if (*p == 0) {
1062 strlcpy(diskname, fullname, size);
1063 return;
1064 }
1065
1066 if (*p == 'r')
1067 p++;
1068
1069 for (p2 = p; *p2 != 0; p2++)
1070 if (isdigit((unsigned char)*p2))
1071 break;
1072 if (*p2 == 0) {
1073 /* XXX invalid diskname? */
1074 strlcpy(diskname, fullname, size);
1075 return;
1076 }
1077 while (isdigit((unsigned char)*p2))
1078 p2++;
1079
1080 len = p2 - p;
1081 if (len > size) {
1082 /* XXX */
1083 strlcpy(diskname, fullname, size);
1084 return;
1085 }
1086
1087 memcpy(diskname, p, len);
1088 diskname[len] = 0;
1089 }
1090
1091 void
1092 get_geometry(void)
1093 {
1094 int mib[2], i;
1095 size_t len;
1096 struct biosdisk_info *bip;
1097 struct nativedisk_info *nip;
1098 char diskname[8];
1099
1100 mib[0] = CTL_MACHDEP;
1101 mib[1] = CPU_DISKINFO;
1102 if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0) {
1103 goto out;
1104 }
1105 dl = (struct disklist *) malloc(len);
1106 if (dl == NULL)
1107 err(1, "Malloc failed");
1108 if (sysctl(mib, 2, dl, &len, NULL, 0) < 0) {
1109 free(dl);
1110 dl = 0;
1111 goto out;
1112 }
1113
1114 get_diskname(disk, diskname, sizeof diskname);
1115
1116 for (i = 0; i < dl->dl_nnativedisks; i++) {
1117 nip = &dl->dl_nativedisks[i];
1118 if (strcmp(diskname, nip->ni_devname))
1119 continue;
1120 /*
1121 * XXX listing possible matches is better. This is ok for
1122 * now because the user has a chance to change it later.
1123 * Also, if all the disks have the same parameters then we can
1124 * just use them, we don't need to know which disk is which.
1125 */
1126 if (nip->ni_nmatches != 0) {
1127 bip = &dl->dl_biosdisks[nip->ni_biosmatches[0]];
1128 dos_cylinders = bip->bi_cyl;
1129 dos_heads = bip->bi_head;
1130 dos_sectors = bip->bi_sec;
1131 if (bip->bi_lbasecs)
1132 dos_disksectors = bip->bi_lbasecs;
1133 return;
1134 }
1135 }
1136 out:
1137 /* Allright, allright, make a stupid guess.. */
1138 intuit_translated_geometry();
1139 }
1140 #endif /* (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H */
1141
1142 #ifdef BOOTSEL
1143 daddr_t
1144 get_default_boot(void)
1145 {
1146 unsigned int id;
1147 int p;
1148
1149 if (mboot.mbr_bootsel_magic != LE_MBR_BS_MAGIC)
1150 /* default to first active partition */
1151 return DEFAULT_ACTIVE;
1152
1153 if (mboot.mbr_bootsel.mbrbs_defkey == SCAN_ENTER)
1154 return DEFAULT_ACTIVE;
1155
1156 id = mboot.mbr_bootsel.mbrbs_defkey;
1157
1158 /* 1+ => allocated partition id, F1+ => disk 0+ */
1159 if (id >= SCAN_F1)
1160 return id - SCAN_F1;
1161 id -= SCAN_1;
1162
1163 for (p = 0; p < MBR_PART_COUNT; p++) {
1164 if (mboot.mbr_parts[p].mbrp_type == 0)
1165 continue;
1166 if (mboot.mbr_bootsel.mbrbs_nametab[p][0] == 0)
1167 continue;
1168 if (id-- == 0)
1169 return le32toh(mboot.mbr_parts[p].mbrp_start);
1170 }
1171
1172 for (p = 0; p < ext.num_ptn; p++) {
1173 if (ext.ptn[p].mbr_parts[0].mbrp_type == 0)
1174 continue;
1175 if (ext.ptn[p].mbr_bootsel.mbrbs_nametab[0][0] == 0)
1176 continue;
1177 if (id-- == 0)
1178 return ext_offset(p)
1179 + le32toh(ext.ptn[p].mbr_parts[0].mbrp_start);
1180 }
1181
1182 return DEFAULT_ACTIVE;
1183 }
1184
1185 void
1186 set_default_boot(daddr_t default_ptn)
1187 {
1188 int p;
1189 int key = SCAN_1;
1190
1191 if (mboot.mbr_bootsel_magic != LE_MBR_BS_MAGIC)
1192 /* sanity */
1193 return;
1194
1195 if (default_ptn == DEFAULT_ACTIVE) {
1196 mboot.mbr_bootsel.mbrbs_defkey = SCAN_ENTER;
1197 return;
1198 }
1199
1200 for (p = 0; p < MBR_PART_COUNT; p++) {
1201 if (mboot.mbr_parts[p].mbrp_type == 0)
1202 continue;
1203 if (mboot.mbr_bootsel.mbrbs_nametab[p][0] == 0)
1204 continue;
1205 if (le32toh(mboot.mbr_parts[p].mbrp_start) == default_ptn) {
1206 mboot.mbr_bootsel.mbrbs_defkey = key;
1207 return;
1208 }
1209 key++;
1210 }
1211
1212 if (mboot.mbr_bootsel.mbrbs_flags & MBR_BS_EXTLBA) {
1213 for (p = 0; p < ext.num_ptn; p++) {
1214 if (ext.ptn[p].mbr_parts[0].mbrp_type == 0)
1215 continue;
1216 if (ext.ptn[p].mbr_bootsel.mbrbs_nametab[0][0] == 0)
1217 continue;
1218 if (le32toh(ext.ptn[p].mbr_parts[0].mbrp_start) +
1219 ext_offset(p) == default_ptn) {
1220 mboot.mbr_bootsel.mbrbs_defkey = key;
1221 return;
1222 }
1223 key++;
1224 }
1225 }
1226
1227 if (default_ptn < 8) {
1228 key = SCAN_F1;
1229 mboot.mbr_bootsel.mbrbs_defkey = key + default_ptn;
1230 return;
1231 }
1232
1233 /* Default to first active partition */
1234 mboot.mbr_bootsel.mbrbs_defkey = SCAN_ENTER;
1235 }
1236
1237 void
1238 install_bootsel(int needed)
1239 {
1240 struct mbr_bootsel *mbs = &mboot.mbr_bootsel;
1241 int p;
1242 int ext13 = 0;
1243 const char *code;
1244
1245 needed |= MBR_BS_NEWMBR; /* need new bootsel code */
1246
1247 /* Work out which boot code we need for this configuration */
1248 for (p = 0; p < MBR_PART_COUNT; p++) {
1249 if (mboot.mbr_parts[p].mbrp_type == 0)
1250 continue;
1251 if (mboot.mbr_bootsel_magic != LE_MBR_BS_MAGIC)
1252 break;
1253 if (mbs->mbrbs_nametab[p][0] == 0)
1254 continue;
1255 needed |= MBR_BS_ACTIVE;
1256 if (le32toh(mboot.mbr_parts[p].mbrp_start) >= dos_totalsectors)
1257 ext13 = MBR_BS_EXTINT13;
1258 }
1259
1260 for (p = 0; p < ext.num_ptn; p++) {
1261 if (ext.ptn[p].mbr_bootsel_magic != LE_MBR_BS_MAGIC)
1262 continue;
1263 if (ext.ptn[p].mbr_parts[0].mbrp_type == 0)
1264 continue;
1265 if (ext.ptn[p].mbr_bootsel.mbrbs_nametab[p][0] == 0)
1266 continue;
1267 needed |= MBR_BS_EXTLBA | MBR_BS_ACTIVE;
1268 }
1269
1270 if (B_flag)
1271 needed |= MBR_BS_ACTIVE;
1272
1273 /* Is the installed code good enough ? */
1274 if (!i_flag && (needed == 0 ||
1275 (mboot.mbr_bootsel_magic == LE_MBR_BS_MAGIC
1276 && (mbs->mbrbs_flags & needed) == needed))) {
1277 /* yes - just set flags */
1278 mbs->mbrbs_flags |= ext13;
1279 return;
1280 }
1281
1282 /* ok - we need to replace the bootcode */
1283
1284 if (f_flag && !(i_flag || B_flag)) {
1285 warnx("Installed bootfile doesn't support required options.");
1286 return;
1287 }
1288
1289 if (!f_flag && bootsize == 0 && !i_flag)
1290 /* Output an explanation for the 'update bootcode' prompt. */
1291 printf("\n%s\n",
1292 "Installed bootfile doesn't support required options.");
1293
1294 /* Were we told a specific file ? (which we have already read) */
1295 /* If so check that it supports what we need. */
1296 if (bootsize != 0 && needed != 0
1297 && (bootcode[0].mbr_bootsel_magic != LE_MBR_BS_MAGIC
1298 || ((bootcode[0].mbr_bootsel.mbrbs_flags & needed) != needed))) {
1299 /* No it doesn't... */
1300 if (f_flag)
1301 warnx("Bootfile %s doesn't support "
1302 "required bootsel options", boot_path );
1303 /* But install it anyway */
1304 else
1305 if (yesno("Bootfile %s doesn't support the required "
1306 "options,\ninstall default bootfile instead?",
1307 boot_path))
1308 bootsize = 0;
1309 }
1310
1311 if (bootsize == 0) {
1312 /* Get name of bootfile that supports the required facilities */
1313 code = DEFAULT_BOOTCODE;
1314 if (needed & MBR_BS_ACTIVE)
1315 code = DEFAULT_BOOTSELCODE;
1316 #ifdef DEFAULT_BOOTEXTCODE
1317 if (needed & MBR_BS_EXTLBA)
1318 code = DEFAULT_BOOTEXTCODE;
1319 #endif
1320
1321 bootsize = read_boot(code, bootcode, sizeof bootcode, 0);
1322 if (bootsize == 0)
1323 /* The old bootcode is better than no bootcode at all */
1324 return;
1325 if ((bootcode[0].mbr_bootsel.mbrbs_flags & needed) != needed)
1326 warnx("Default bootfile %s doesn't support required "
1327 "options. Got flags 0x%x, wanted 0x%x\n",
1328 boot_path, bootcode[0].mbr_bootsel.mbrbs_flags,
1329 needed);
1330 }
1331
1332 if (!f_flag && !yesno("Update the bootcode from %s?", boot_path))
1333 return;
1334
1335 init_sector0(0);
1336
1337 if (mboot.mbr_bootsel_magic == LE_MBR_BS_MAGIC)
1338 mbs->mbrbs_flags = bootcode[0].mbr_bootsel.mbrbs_flags | ext13;
1339 }
1340
1341 daddr_t
1342 configure_bootsel(daddr_t default_ptn)
1343 {
1344 struct mbr_bootsel *mbs = &mboot.mbr_bootsel;
1345 int i, item, opt;
1346 int tmo;
1347 daddr_t *off;
1348 int num_bios_disks;
1349
1350 #if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
1351 if (dl != NULL) {
1352 num_bios_disks = dl->dl_nbiosdisks;
1353 if (num_bios_disks > 8)
1354 num_bios_disks = 8;
1355 } else
1356 #endif
1357 num_bios_disks = 8;
1358
1359 printf("\nBoot selector configuration:\n");
1360
1361 /* The timeout value is in ticks, ~18.2 Hz. Avoid using floats.
1362 * Ticks are nearly 64k/3600 - so our long timers are sligtly out!
1363 * Newer bootcode always waits for 1 tick, so treats 0xffff
1364 * as wait forever.
1365 */
1366 tmo = le16toh(mbs->mbrbs_timeo);
1367 tmo = tmo == 0xffff ? -1 : (10 * tmo + 9) / 182;
1368 tmo = decimal("Timeout value (0 to 3600 seconds, -1 => never)",
1369 tmo, 0, -1, 3600);
1370 mbs->mbrbs_timeo = htole16(tmo == -1 ? 0xffff : (tmo * 182) / 10);
1371
1372 off = calloc(1 + MBR_PART_COUNT + ext.num_ptn + num_bios_disks, sizeof *off);
1373 if (off == NULL)
1374 err(1, "Malloc failed");
1375
1376 printf("Select the default boot option. Options are:\n\n");
1377 item = 0;
1378 opt = 0;
1379 off[opt] = DEFAULT_ACTIVE;
1380 printf("%d: The first active partition\n", opt);
1381 for (i = 0; i < MBR_PART_COUNT; i++) {
1382 if (mboot.mbr_parts[i].mbrp_type == 0)
1383 continue;
1384 if (mbs->mbrbs_nametab[i][0] == 0)
1385 continue;
1386 printf("%d: %s\n", ++opt, &mbs->mbrbs_nametab[i][0]);
1387 off[opt] = le32toh(mboot.mbr_parts[i].mbrp_start);
1388 if (off[opt] == default_ptn)
1389 item = opt;
1390 }
1391 if (mbs->mbrbs_flags & MBR_BS_EXTLBA) {
1392 for (i = 0; i < ext.num_ptn; i++) {
1393 if (ext.ptn[i].mbr_parts[0].mbrp_type == 0)
1394 continue;
1395 if (ext.ptn[i].mbr_bootsel.mbrbs_nametab[0][0] == 0)
1396 continue;
1397 printf("%d: %s\n",
1398 ++opt, ext.ptn[i].mbr_bootsel.mbrbs_nametab[0]);
1399 off[opt] = ext_offset(i) +
1400 le32toh(ext.ptn[i].mbr_parts[0].mbrp_start);
1401 if (off[opt] == default_ptn)
1402 item = opt;
1403 }
1404 }
1405 for (i = 0; i < num_bios_disks; i++) {
1406 printf("%d: Harddisk %d\n", ++opt, i);
1407 off[opt] = i;
1408 if (i == default_ptn)
1409 item = opt;
1410 }
1411
1412 item = decimal("Default boot option", item, 0, 0, opt);
1413
1414 default_ptn = off[item];
1415 free(off);
1416 return default_ptn;
1417 }
1418 #endif /* BOOTSEL */
1419
1420
1421 /* Prerequisite: the disklabel parameters and master boot record must
1422 * have been read (i.e. dos_* and mboot are meaningful).
1423 * Specification: modifies dos_cylinders, dos_heads, dos_sectors, and
1424 * dos_cylindersectors to be consistent with what the
1425 * partition table is using, if we can find a geometry
1426 * which is consistent with all partition table entries.
1427 * We may get the number of cylinders slightly wrong (in
1428 * the conservative direction). The idea is to be able
1429 * to create a NetBSD partition on a disk we don't know
1430 * the translated geometry of.
1431 * This routine is only used for non-x86 systems or when we fail to
1432 * get the BIOS geometry from the kernel.
1433 */
1434 void
1435 intuit_translated_geometry(void)
1436 {
1437 int xcylinders = -1, xheads = -1, xsectors = -1, i, j;
1438 unsigned int c1, h1, s1, c2, h2, s2;
1439 unsigned long a1, a2;
1440 uint64_t num, denom;
1441
1442 /*
1443 * The physical parameters may be invalid as bios geometry.
1444 * If we cannot determine the actual bios geometry, we are
1445 * better off picking a likely 'faked' geometry than leaving
1446 * the invalid physical one.
1447 */
1448
1449 if (dos_cylinders > MAXCYL || dos_heads > MAXHEAD ||
1450 dos_sectors > MAXSECTOR) {
1451 h1 = MAXHEAD - 1;
1452 c1 = MAXCYL - 1;
1453 #if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
1454 if (dl != NULL) {
1455 /* BIOS may use 256 heads or 1024 cylinders */
1456 for (i = 0; i < dl->dl_nbiosdisks; i++) {
1457 if (h1 < dl->dl_biosdisks[i].bi_head)
1458 h1 = dl->dl_biosdisks[i].bi_head;
1459 if (c1 < dl->dl_biosdisks[i].bi_cyl)
1460 c1 = dl->dl_biosdisks[i].bi_cyl;
1461 }
1462 }
1463 #endif
1464 dos_sectors = MAXSECTOR;
1465 dos_heads = h1;
1466 dos_cylinders = disklabel.d_secperunit / (MAXSECTOR * h1);
1467 if (dos_cylinders > c1)
1468 dos_cylinders = c1;
1469 }
1470
1471 /* Try to deduce the number of heads from two different mappings. */
1472 for (i = 0; i < MBR_PART_COUNT * 2 - 1; i++) {
1473 if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
1474 continue;
1475 a1 -= s1;
1476 for (j = i + 1; j < MBR_PART_COUNT * 2; j++) {
1477 if (get_mapping(j, &c2, &h2, &s2, &a2) < 0)
1478 continue;
1479 a2 -= s2;
1480 num = (uint64_t)h1 * a2 - (uint64_t)h2 * a1;
1481 denom = (uint64_t)c2 * a1 - (uint64_t)c1 * a2;
1482 if (denom != 0 && num % denom == 0) {
1483 xheads = num / denom;
1484 xsectors = a1 / (c1 * xheads + h1);
1485 break;
1486 }
1487 }
1488 if (xheads != -1)
1489 break;
1490 }
1491
1492 if (xheads == -1) {
1493 warnx("Cannot determine the number of heads");
1494 return;
1495 }
1496
1497 /* Estimate the number of cylinders. */
1498 xcylinders = disklabel.d_secperunit / xheads / xsectors;
1499 if (disklabel.d_secperunit > xcylinders * xheads * xsectors)
1500 xcylinders++;
1501
1502 /*
1503 * Now verify consistency with each of the partition table entries.
1504 * Be willing to shove cylinders up a little bit to make things work,
1505 * but translation mismatches are fatal.
1506 */
1507 for (i = 0; i < MBR_PART_COUNT * 2; i++) {
1508 if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
1509 continue;
1510 if (c1 >= MAXCYL - 2)
1511 continue;
1512 if (xsectors * (c1 * xheads + h1) + s1 != a1)
1513 return;
1514 }
1515
1516
1517 /* Everything checks out.
1518 * Reset the geometry to use for further calculations.
1519 * But cylinders cannot be > 1024.
1520 */
1521 if (xcylinders > MAXCYL)
1522 dos_cylinders = MAXCYL;
1523 else
1524 dos_cylinders = xcylinders;
1525 dos_heads = xheads;
1526 dos_sectors = xsectors;
1527 }
1528
1529 /*
1530 * For the purposes of intuit_translated_geometry(), treat the partition
1531 * table as a list of eight mapping between (cylinder, head, sector)
1532 * triplets and absolute sectors. Get the relevant geometry triplet and
1533 * absolute sectors for a given entry, or return -1 if it isn't present.
1534 * Note: for simplicity, the returned sector is 0-based.
1535 */
1536 int
1537 get_mapping(int i, unsigned int *cylinder, unsigned int *head, unsigned int *sector,
1538 unsigned long *absolute)
1539 {
1540 struct mbr_partition *part = &mboot.mbr_parts[i / 2];
1541
1542 if (part->mbrp_type == 0)
1543 return -1;
1544 if (i % 2 == 0) {
1545 *cylinder = MBR_PCYL(part->mbrp_scyl, part->mbrp_ssect);
1546 *head = part->mbrp_shd;
1547 *sector = MBR_PSECT(part->mbrp_ssect) - 1;
1548 *absolute = le32toh(part->mbrp_start);
1549 } else {
1550 *cylinder = MBR_PCYL(part->mbrp_ecyl, part->mbrp_esect);
1551 *head = part->mbrp_ehd;
1552 *sector = MBR_PSECT(part->mbrp_esect) - 1;
1553 *absolute = le32toh(part->mbrp_start)
1554 + le32toh(part->mbrp_size) - 1;
1555 }
1556 /* Sanity check the data against all zeroes */
1557 if ((*cylinder == 0) && (*sector == 0) && (*head == 0))
1558 return -1;
1559 /* Sanity check the data against max values */
1560 if ((((*cylinder * MAXHEAD) + *head) * MAXSECTOR + *sector) < *absolute)
1561 /* cannot be a CHS mapping */
1562 return -1;
1563 return 0;
1564 }
1565
1566 static void
1567 delete_ptn(int part)
1568 {
1569 if (part == ext.ptn_id) {
1570 /* forget all about the extended partition */
1571 free(ext.ptn);
1572 memset(&ext, 0, sizeof ext);
1573 }
1574
1575 mboot.mbr_parts[part].mbrp_type = 0;
1576 }
1577
1578 static void
1579 delete_ext_ptn(int part)
1580 {
1581
1582 if (part == 0) {
1583 ext.ptn[0].mbr_parts[0].mbrp_type = 0;
1584 return;
1585 }
1586 ext.ptn[part - 1].mbr_parts[1] = ext.ptn[part].mbr_parts[1];
1587 memmove(&ext.ptn[part], &ext.ptn[part + 1],
1588 (ext.num_ptn - part - 1) * sizeof ext.ptn[0]);
1589 ext.num_ptn--;
1590 }
1591
1592 static int
1593 add_ext_ptn(daddr_t start, daddr_t size)
1594 {
1595 int part;
1596 struct mbr_partition *partp;
1597 struct mbr_sector *nptn;
1598
1599 nptn = realloc(ext.ptn, (ext.num_ptn + 1) * sizeof *ext.ptn);
1600 if (!nptn)
1601 err(1, "realloc");
1602 ext.ptn = nptn;
1603 for (part = 0; part < ext.num_ptn; part++)
1604 if (ext_offset(part) > start)
1605 break;
1606 /* insert before 'part' - make space... */
1607 memmove(&ext.ptn[part + 1], &ext.ptn[part],
1608 (ext.num_ptn - part) * sizeof ext.ptn[0]);
1609 memset(&ext.ptn[part], 0, sizeof ext.ptn[0]);
1610 ext.ptn[part].mbr_magic = LE_MBR_MAGIC;
1611 /* we will be 'part' */
1612 if (part == 0) {
1613 /* link us to 'next' */
1614 partp = &ext.ptn[0].mbr_parts[1];
1615 /* offset will be fixed by caller */
1616 partp->mbrp_size = htole32(
1617 le32toh(ext.ptn[1].mbr_parts[0].mbrp_start) +
1618 le32toh(ext.ptn[1].mbr_parts[0].mbrp_size));
1619 } else {
1620 /* link us to prev's next */
1621 partp = &ext.ptn[part - 1].mbr_parts[1];
1622 ext.ptn[part].mbr_parts[1] = *partp;
1623 /* and prev onto us */
1624 partp->mbrp_start = htole32(start - dos_sectors - ext.base);
1625 partp->mbrp_size = htole32(size + dos_sectors);
1626 }
1627 partp->mbrp_type = 5; /* as used by win98 */
1628 partp->mbrp_flag = 0;
1629 /* wallop in some CHS values - win98 doesn't saturate them */
1630 dos(le32toh(partp->mbrp_start),
1631 &partp->mbrp_scyl, &partp->mbrp_shd, &partp->mbrp_ssect);
1632 dos(le32toh(partp->mbrp_start) + le32toh(partp->mbrp_size) - 1,
1633 &partp->mbrp_ecyl, &partp->mbrp_ehd, &partp->mbrp_esect);
1634 ext.num_ptn++;
1635
1636 return part;
1637 }
1638
1639 static const char *
1640 check_overlap(int part, int sysid, daddr_t start, daddr_t size, int fix)
1641 {
1642 int p;
1643 unsigned int p_s, p_e;
1644
1645 if (sysid != 0) {
1646 if (start < dos_sectors)
1647 return "Track zero is reserved for the BIOS";
1648 if (start + size > disksectors)
1649 return "Partition exceeds size of disk";
1650 for (p = 0; p < MBR_PART_COUNT; p++) {
1651 if (p == part || mboot.mbr_parts[p].mbrp_type == 0)
1652 continue;
1653 p_s = le32toh(mboot.mbr_parts[p].mbrp_start);
1654 p_e = p_s + le32toh(mboot.mbr_parts[p].mbrp_size);
1655 if (start + size <= p_s || start >= p_e)
1656 continue;
1657 if (f_flag) {
1658 if (fix)
1659 delete_ptn(p);
1660 return 0;
1661 }
1662 return "Overlaps another partition";
1663 }
1664 }
1665
1666 /* Are we trying to create an extended partition */
1667 if (!MBR_IS_EXTENDED(mboot.mbr_parts[part].mbrp_type)) {
1668 /* this wasn't the extended partition */
1669 if (!MBR_IS_EXTENDED(sysid))
1670 return 0;
1671 /* making an extended partition */
1672 if (ext.base != 0) {
1673 if (!f_flag)
1674 return "There cannot be 2 extended partitions";
1675 if (fix)
1676 delete_ptn(ext.ptn_id);
1677 }
1678 if (fix) {
1679 /* allocate a new extended partition */
1680 ext.ptn = calloc(1, sizeof ext.ptn[0]);
1681 if (ext.ptn == NULL)
1682 err(1, "Malloc failed");
1683 ext.ptn[0].mbr_magic = LE_MBR_MAGIC;
1684 ext.ptn_id = part;
1685 ext.base = start;
1686 ext.limit = start + size;
1687 ext.num_ptn = 1;
1688 }
1689 return 0;
1690 }
1691
1692 /* Check we haven't cut space allocated to an extended ptn */
1693
1694 if (!MBR_IS_EXTENDED(sysid)) {
1695 /* no longer an extended partition */
1696 if (fix) {
1697 /* Kill all memory of the extended partitions */
1698 delete_ptn(part);
1699 return 0;
1700 }
1701 if (ext.num_ptn == 0 ||
1702 (ext.num_ptn == 1 && ext.ptn[0].mbr_parts[0].mbrp_type == 0))
1703 /* nothing in extended partition */
1704 return 0;
1705 if (f_flag)
1706 return 0;
1707 if (yesno("Do you really want to delete all the extended partitions?"))
1708 return 0;
1709 return "Extended partition busy";
1710 }
1711
1712 if (le32toh(mboot.mbr_parts[part].mbrp_start) != ext.base)
1713 /* maybe impossible, but an extra sanity check */
1714 return 0;
1715
1716 for (p = ext.num_ptn; --p >= 0;) {
1717 if (ext.ptn[p].mbr_parts[0].mbrp_type == 0)
1718 continue;
1719 p_s = ext_offset(p);
1720 p_e = p_s + le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
1721 + le32toh(ext.ptn[p].mbr_parts[0].mbrp_size);
1722 if (p_s >= start && p_e <= start + size)
1723 continue;
1724 if (!f_flag)
1725 return "Extended partition outside main partition";
1726 if (fix)
1727 delete_ext_ptn(p);
1728 }
1729
1730 if (fix && start != ext.base) {
1731 /* The internal offsets need to be fixed up */
1732 for (p = 0; p < ext.num_ptn - 1; p++)
1733 ext.ptn[p].mbr_parts[1].mbrp_start = htole32(
1734 le32toh(ext.ptn[p].mbr_parts[1].mbrp_start)
1735 + ext.base - start);
1736 /* and maybe an empty partition at the start */
1737 if (ext.ptn[0].mbr_parts[0].mbrp_type == 0) {
1738 if (le32toh(ext.ptn[0].mbr_parts[1].mbrp_start) == 0) {
1739 /* don't need the empty slot */
1740 memmove(&ext.ptn[0], &ext.ptn[1],
1741 (ext.num_ptn - 1) * sizeof ext.ptn[0]);
1742 ext.num_ptn--;
1743 }
1744 } else {
1745 /* must create an empty slot */
1746 add_ext_ptn(start, dos_sectors);
1747 ext.ptn[0].mbr_parts[1].mbrp_start = htole32(ext.base
1748 - start);
1749 }
1750 }
1751 if (fix) {
1752 ext.base = start;
1753 ext.limit = start + size;
1754 }
1755 return 0;
1756 }
1757
1758 static const char *
1759 check_ext_overlap(int part, int sysid, daddr_t start, daddr_t size, int fix)
1760 {
1761 int p;
1762 unsigned int p_s, p_e;
1763
1764 if (sysid == 0)
1765 return 0;
1766
1767 if (MBR_IS_EXTENDED(sysid))
1768 return "Nested extended partitions are not allowed";
1769
1770 /* allow one track at start for extended partition header */
1771 start -= dos_sectors;
1772 size += dos_sectors;
1773 if (start < ext.base || start + size > ext.limit)
1774 return "Outside bounds of extended partition";
1775
1776 if (f_flag && !fix)
1777 return 0;
1778
1779 for (p = ext.num_ptn; --p >= 0;) {
1780 if (p == part || ext.ptn[p].mbr_parts[0].mbrp_type == 0)
1781 continue;
1782 p_s = ext_offset(p);
1783 p_e = p_s + le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
1784 + le32toh(ext.ptn[p].mbr_parts[0].mbrp_size);
1785 if (p == 0)
1786 p_s += le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
1787 - dos_sectors;
1788 if (start < p_e && start + size > p_s) {
1789 if (!f_flag)
1790 return "Overlaps another extended partition";
1791 if (fix) {
1792 if (part == -1)
1793 delete_ext_ptn(p);
1794 else
1795 /* must not change numbering yet */
1796 ext.ptn[p].mbr_parts[0].mbrp_type = 0;
1797 }
1798 }
1799 }
1800 return 0;
1801 }
1802
1803 int
1804 change_part(int extended, int part, int sysid, daddr_t start, daddr_t size,
1805 char *bootmenu)
1806 {
1807 struct mbr_partition *partp;
1808 struct mbr_sector *boot;
1809 daddr_t offset;
1810 const char *e;
1811 int upart = part;
1812 int p;
1813 int fl;
1814 daddr_t n_s, n_e;
1815 const char *errtext;
1816 #ifdef BOOTSEL
1817 char tmp_bootmenu[MBR_PART_COUNT * (MBR_BS_PARTNAMESIZE + 1)];
1818 int bootmenu_len = (extended ? MBR_PART_COUNT : 1) * (MBR_BS_PARTNAMESIZE + 1);
1819 #endif
1820
1821 if (extended) {
1822 if (part != -1 && part < ext.num_ptn) {
1823 boot = &ext.ptn[part];
1824 partp = &boot->mbr_parts[0];
1825 offset = ext_offset(part);
1826 } else {
1827 part = -1;
1828 boot = 0;
1829 partp = 0;
1830 offset = 0;
1831 }
1832 upart = 0;
1833 e = "E";
1834 } else {
1835 boot = &mboot;
1836 partp = &boot->mbr_parts[part];
1837 offset = 0;
1838 e = "";
1839 }
1840
1841 if (!f_flag && part != -1) {
1842 printf("The data for partition %s%d is:\n", e, part);
1843 print_part(boot, upart, offset);
1844 }
1845
1846 #ifdef BOOTSEL
1847 if (bootmenu != NULL)
1848 strlcpy(tmp_bootmenu, bootmenu, bootmenu_len);
1849 else
1850 if (boot != NULL && boot->mbr_bootsel_magic == LE_MBR_BS_MAGIC)
1851 strlcpy(tmp_bootmenu,
1852 boot->mbr_bootsel.mbrbs_nametab[upart],
1853 bootmenu_len);
1854 else
1855 tmp_bootmenu[0] = 0;
1856 #endif
1857
1858 if (!s_flag && partp != NULL) {
1859 /* values not specified, default to current ones */
1860 sysid = partp->mbrp_type;
1861 start = offset + le32toh(partp->mbrp_start);
1862 size = le32toh(partp->mbrp_size);
1863 }
1864
1865 /* creating a new partition, default to free space */
1866 if (!s_flag && sysid == 0 && extended) {
1867 /* non-extended partition */
1868 start = ext.base;
1869 for (p = 0; p < ext.num_ptn; p++) {
1870 if (ext.ptn[p].mbr_parts[0].mbrp_type == 0)
1871 continue;
1872 n_s = ext_offset(p);
1873 if (n_s > start + dos_sectors)
1874 break;
1875 start = ext_offset(p)
1876 + le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
1877 + le32toh(ext.ptn[p].mbr_parts[0].mbrp_size);
1878 }
1879 if (ext.limit - start <= dos_sectors) {
1880 printf("No space in extended partition\n");
1881 return 0;
1882 }
1883 start += dos_sectors;
1884 }
1885
1886 if (!s_flag && sysid == 0 && !extended) {
1887 /* same for non-extended partition */
1888 /* first see if old start is free */
1889 if (start < dos_sectors)
1890 start = 0;
1891 for (p = 0; start != 0 && p < MBR_PART_COUNT; p++) {
1892 if (mboot.mbr_parts[p].mbrp_type == 0)
1893 continue;
1894 n_s = le32toh(mboot.mbr_parts[p].mbrp_start);
1895 if (start >= n_s &&
1896 start < n_s + le32toh(mboot.mbr_parts[p].mbrp_size))
1897 start = 0;
1898 }
1899 if (start == 0) {
1900 /* Look for first gap */
1901 start = dos_sectors;
1902 for (p = 0; p < MBR_PART_COUNT; p++) {
1903 if (mboot.mbr_parts[p].mbrp_type == 0)
1904 continue;
1905 n_s = le32toh(mboot.mbr_parts[p].mbrp_start);
1906 n_e = n_s + le32toh(mboot.mbr_parts[p].mbrp_size);
1907 if (start >= n_s && start < n_e) {
1908 start = n_e;
1909 p = -1;
1910 }
1911 }
1912 if (start >= disksectors) {
1913 printf("No free space\n");
1914 return 0;
1915 }
1916 }
1917 }
1918
1919 if (!f_flag) {
1920 /* request new values from user */
1921 if (sysid == 0)
1922 sysid = 169;
1923 sysid = decimal("sysid", sysid, 0, 0, 255);
1924 if (sysid == 0 && !v_flag) {
1925 start = 0;
1926 size = 0;
1927 #ifdef BOOTSEL
1928 tmp_bootmenu[0] = 0;
1929 #endif
1930 } else {
1931 daddr_t old = start;
1932 daddr_t lim = extended ? ext.limit : disksectors;
1933 start = decimal("start", start,
1934 DEC_SEC | DEC_RND_0 | (extended ? DEC_RND : 0),
1935 extended ? ext.base : 0, lim);
1936 /* Adjust 'size' so that end doesn't move when 'start'
1937 * is only changed slightly.
1938 */
1939 if (size > start - old)
1940 size -= start - old;
1941 else
1942 size = 0;
1943 /* Find end of available space from this start point */
1944 if (extended) {
1945 for (p = 0; p < ext.num_ptn; p++) {
1946 if (p == part)
1947 continue;
1948 if (ext.ptn[p].mbr_parts[0].mbrp_type == 0)
1949 continue;
1950 n_s = ext_offset(p);
1951 if (n_s > start && n_s < lim)
1952 lim = n_s;
1953 if (start >= n_s && start < n_s
1954 + le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
1955 + le32toh(ext.ptn[p].mbr_parts[0].mbrp_size)) {
1956 lim = start;
1957 break;
1958 }
1959 }
1960 } else {
1961 for (p = 0; p < MBR_PART_COUNT; p++) {
1962 if (p == part)
1963 continue;
1964 if (mboot.mbr_parts[p].mbrp_type == 0)
1965 continue;
1966 n_s = le32toh(mboot.mbr_parts[p].mbrp_start);
1967 if (n_s > start && n_s < lim)
1968 lim = n_s;
1969 if (start >= n_s && start < n_s
1970 + le32toh(mboot.mbr_parts[p].mbrp_size)) {
1971 lim = start;
1972 break;
1973 }
1974 }
1975 }
1976 lim -= start;
1977 if (lim == 0) {
1978 printf("Start sector already allocated\n");
1979 return 0;
1980 }
1981 if (size == 0 || size > lim)
1982 size = lim;
1983 fl = DEC_SEC;
1984 if (start % dos_cylindersectors == dos_sectors)
1985 fl |= DEC_RND_DOWN;
1986 if (start == 2 * dos_sectors)
1987 fl |= DEC_RND_DOWN | DEC_RND_DOWN_2;
1988 size = decimal("size", size, fl, 0, lim);
1989 #ifdef BOOTSEL
1990 #ifndef DEFAULT_BOOTEXTCODE
1991 if (!extended)
1992 #endif
1993 string("bootmenu", bootmenu_len, tmp_bootmenu);
1994 #endif
1995 }
1996 }
1997
1998 /*
1999 * Before we write these away, we must verify that nothing
2000 * untoward has been requested.
2001 */
2002
2003 if (extended)
2004 errtext = check_ext_overlap(part, sysid, start, size, 0);
2005 else
2006 errtext = check_overlap(part, sysid, start, size, 0);
2007 if (errtext != NULL) {
2008 if (f_flag)
2009 errx(2, "%s\n", errtext);
2010 printf("%s\n", errtext);
2011 return 0;
2012 }
2013
2014 /*
2015 * Before proceeding, delete any overlapped partitions.
2016 * This can only happen if '-f' was supplied on the command line.
2017 * Just hope the caller knows what they are doing.
2018 * This also fixes the base of each extended partition if the
2019 * partition itself has moved.
2020 */
2021
2022 if (extended)
2023 errtext = check_ext_overlap(part, sysid, start, size, 1);
2024 else
2025 errtext = check_overlap(part, sysid, start, size, 1);
2026
2027 if (errtext)
2028 errx(1, "%s\n", errtext);
2029
2030 if (sysid == 0) {
2031 /* delete this partition - save info though */
2032 if (partp == NULL)
2033 /* must have been trying to create an extended ptn */
2034 return 0;
2035 if (start == 0 && size == 0)
2036 memset(partp, 0, sizeof *partp);
2037 #ifdef BOOTSEL
2038 if (boot->mbr_bootsel_magic == LE_MBR_BS_MAGIC)
2039 memset(boot->mbr_bootsel.mbrbs_nametab[upart], 0,
2040 sizeof boot->mbr_bootsel.mbrbs_nametab[0]);
2041 #endif
2042 if (extended)
2043 delete_ext_ptn(part);
2044 else
2045 delete_ptn(part);
2046 return 1;
2047 }
2048
2049
2050 if (extended) {
2051 if (part != -1)
2052 delete_ext_ptn(part);
2053 if (start == ext.base + dos_sectors)
2054 /* First one must have been free */
2055 part = 0;
2056 else
2057 part = add_ext_ptn(start, size);
2058
2059 /* These must be re-calculated because of the realloc */
2060 boot = &ext.ptn[part];
2061 partp = &boot->mbr_parts[0];
2062 offset = ext_offset(part);
2063 }
2064
2065 partp->mbrp_type = sysid;
2066 partp->mbrp_start = htole32( start - offset);
2067 partp->mbrp_size = htole32( size);
2068 dos(start, &partp->mbrp_scyl, &partp->mbrp_shd, &partp->mbrp_ssect);
2069 dos(start + size - 1,
2070 &partp->mbrp_ecyl, &partp->mbrp_ehd, &partp->mbrp_esect);
2071 #ifdef BOOTSEL
2072 if (extended) {
2073 boot->mbr_bootsel_magic = LE_MBR_BS_MAGIC;
2074 strncpy(boot->mbr_bootsel.mbrbs_nametab[upart], tmp_bootmenu,
2075 bootmenu_len);
2076 } else {
2077 /* We need to bootselect code installed in order to have
2078 * somewhere to safely write the menu tag.
2079 */
2080 if (boot->mbr_bootsel_magic != LE_MBR_BS_MAGIC) {
2081 if (f_flag ||
2082 yesno("The bootselect code is not installed, "
2083 "do you want to install it now?"))
2084 install_bootsel(MBR_BS_ACTIVE);
2085 }
2086 if (boot->mbr_bootsel_magic == LE_MBR_BS_MAGIC) {
2087 strncpy(boot->mbr_bootsel.mbrbs_nametab[upart],
2088 tmp_bootmenu, bootmenu_len);
2089 }
2090 }
2091 #endif
2092
2093 if (v_flag && !f_flag && yesno("Explicitly specify beg/end address?")) {
2094 /* this really isn't a good idea.... */
2095 int tsector, tcylinder, thead;
2096
2097 tcylinder = MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect);
2098 thead = partp->mbrp_shd;
2099 tsector = MBR_PSECT(partp->mbrp_ssect);
2100 tcylinder = decimal("beginning cylinder",
2101 tcylinder, 0, 0, dos_cylinders - 1);
2102 thead = decimal("beginning head",
2103 thead, 0, 0, dos_heads - 1);
2104 tsector = decimal("beginning sector",
2105 tsector, 0, 1, dos_sectors);
2106 partp->mbrp_scyl = DOSCYL(tcylinder);
2107 partp->mbrp_shd = thead;
2108 partp->mbrp_ssect = DOSSECT(tsector, tcylinder);
2109
2110 tcylinder = MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect);
2111 thead = partp->mbrp_ehd;
2112 tsector = MBR_PSECT(partp->mbrp_esect);
2113 tcylinder = decimal("ending cylinder",
2114 tcylinder, 0, 0, dos_cylinders - 1);
2115 thead = decimal("ending head",
2116 thead, 0, 0, dos_heads - 1);
2117 tsector = decimal("ending sector",
2118 tsector, 0, 1, dos_sectors);
2119 partp->mbrp_ecyl = DOSCYL(tcylinder);
2120 partp->mbrp_ehd = thead;
2121 partp->mbrp_esect = DOSSECT(tsector, tcylinder);
2122 }
2123
2124 /* If we had to mark an extended partition as deleted because
2125 * another request would have overlapped it, now is the time
2126 * to do the actual delete.
2127 */
2128 if (extended && f_flag) {
2129 for (p = ext.num_ptn; --p >= 0;)
2130 if (ext.ptn[p].mbr_parts[0].mbrp_type == 0)
2131 delete_ext_ptn(p);
2132 }
2133 return 1;
2134 }
2135
2136 void
2137 print_params(void)
2138 {
2139
2140 if (sh_flag) {
2141 printf("DISK=%s\n", disk);
2142 printf("DLCYL=%d\nDLHEAD=%d\nDLSEC=%d\nDLSIZE=%"PRIdaddr"\n",
2143 cylinders, heads, sectors, disksectors);
2144 printf("BCYL=%d\nBHEAD=%d\nBSEC=%d\nBDLSIZE=%"PRIdaddr"\n",
2145 dos_cylinders, dos_heads, dos_sectors, dos_disksectors);
2146 printf("NUMEXTPTN=%d\n", ext.num_ptn);
2147 return;
2148 }
2149
2150 /* Not sh_flag */
2151 printf("Disk: %s\n", disk);
2152 printf("NetBSD disklabel disk geometry:\n");
2153 printf("cylinders: %d, heads: %d, sectors/track: %d "
2154 "(%d sectors/cylinder)\ntotal sectors: %"PRIdaddr"\n\n",
2155 cylinders, heads, sectors, cylindersectors, disksectors);
2156 printf("BIOS disk geometry:\n");
2157 printf("cylinders: %d, heads: %d, sectors/track: %d "
2158 "(%d sectors/cylinder)\ntotal sectors: %"PRIdaddr"\n\n",
2159 dos_cylinders, dos_heads, dos_sectors, dos_cylindersectors,
2160 dos_disksectors);
2161 }
2162
2163 void
2164 change_active(int which)
2165 {
2166 struct mbr_partition *partp;
2167 int part;
2168 int active = MBR_PART_COUNT;
2169
2170 partp = &mboot.mbr_parts[0];
2171
2172 if (a_flag && which != -1)
2173 active = which;
2174 else {
2175 for (part = 0; part < MBR_PART_COUNT; part++)
2176 if (partp[part].mbrp_flag & MBR_PFLAG_ACTIVE)
2177 active = part;
2178 }
2179 if (!f_flag) {
2180 if (yesno("Do you want to change the active partition?")) {
2181 printf ("Choosing %d will make no partition active.\n",
2182 MBR_PART_COUNT);
2183 do {
2184 active = decimal("active partition",
2185 active, 0, 0, MBR_PART_COUNT);
2186 } while (!yesno("Are you happy with this choice?"));
2187 } else
2188 return;
2189 } else
2190 if (active != MBR_PART_COUNT)
2191 printf ("Making partition %d active.\n", active);
2192
2193 for (part = 0; part < MBR_PART_COUNT; part++)
2194 partp[part].mbrp_flag &= ~MBR_PFLAG_ACTIVE;
2195 if (active < MBR_PART_COUNT)
2196 partp[active].mbrp_flag |= MBR_PFLAG_ACTIVE;
2197 }
2198
2199 void
2200 get_params_to_use(void)
2201 {
2202 #if defined(__i386__) || defined(__x86_64__)
2203 struct biosdisk_info *bip;
2204 int i;
2205 #endif
2206
2207 if (b_flag) {
2208 dos_cylinders = b_cyl;
2209 dos_heads = b_head;
2210 dos_sectors = b_sec;
2211 return;
2212 }
2213
2214 print_params();
2215 if (!yesno("Do you want to change our idea of what BIOS thinks?"))
2216 return;
2217
2218 #if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
2219 if (dl != NULL) {
2220 for (i = 0; i < dl->dl_nbiosdisks; i++) {
2221 if (i == 0)
2222 printf("\nGeometries of known disks:\n");
2223 bip = &dl->dl_biosdisks[i];
2224 printf("Disk %d: cylinders %u, heads %u, sectors %u"
2225 " (%"PRIdaddr" sectors, %dMB)\n",
2226 i, bip->bi_cyl, bip->bi_head, bip->bi_sec,
2227 bip->bi_lbasecs, SEC_TO_MB(bip->bi_lbasecs));
2228
2229 }
2230 printf("\n");
2231 }
2232 #endif
2233 do {
2234 dos_cylinders = decimal("BIOS's idea of #cylinders",
2235 dos_cylinders, 0, 0, MAXCYL);
2236 dos_heads = decimal("BIOS's idea of #heads",
2237 dos_heads, 0, 0, MAXHEAD);
2238 dos_sectors = decimal("BIOS's idea of #sectors",
2239 dos_sectors, 0, 1, MAXSECTOR);
2240 print_params();
2241 } while (!yesno("Are you happy with this choice?"));
2242 }
2243
2244
2245 /***********************************************\
2246 * Change real numbers into strange dos numbers *
2247 \***********************************************/
2248 void
2249 dos(int sector, unsigned char *cylinderp, unsigned char *headp,
2250 unsigned char *sectorp)
2251 {
2252 int cylinder, head;
2253
2254 cylinder = sector / dos_cylindersectors;
2255 sector -= cylinder * dos_cylindersectors;
2256
2257 head = sector / dos_sectors;
2258 sector -= head * dos_sectors;
2259 if (cylinder > 1023)
2260 cylinder = 1023;
2261
2262 *cylinderp = DOSCYL(cylinder);
2263 *headp = head;
2264 *sectorp = DOSSECT(sector + 1, cylinder);
2265 }
2266
2267 int
2268 open_disk(int update)
2269 {
2270 static char namebuf[MAXPATHLEN + 1];
2271 int flags = update && disk_file == NULL ? O_RDWR : O_RDONLY;
2272
2273 if (!F_flag) {
2274 fd = opendisk(disk, flags, namebuf, sizeof(namebuf), 0);
2275 if (fd < 0) {
2276 if (errno == ENODEV)
2277 warnx("%s is not a character device", namebuf);
2278 else
2279 warn("cannot opendisk %s", namebuf);
2280 return (-1);
2281 }
2282 disk = namebuf;
2283 } else {
2284 fd = open(disk, flags, 0);
2285 if (fd == -1) {
2286 warn("cannot open %s", disk);
2287 return -1;
2288 }
2289 }
2290
2291 if (get_params() == -1) {
2292 close(fd);
2293 fd = -1;
2294 return (-1);
2295 }
2296 if (disk_file != NULL) {
2297 /* for testing: read/write data from a disk file */
2298 wfd = open(disk_file, update ? O_RDWR|O_CREAT : O_RDONLY, 0777);
2299 if (wfd == -1) {
2300 warn("%s", disk_file);
2301 close(fd);
2302 fd = -1;
2303 return -1;
2304 }
2305 } else
2306 wfd = fd;
2307 return (0);
2308 }
2309
2310 int
2311 read_disk(daddr_t sector, void *buf)
2312 {
2313
2314 if (*rfd == -1)
2315 errx(1, "read_disk(); fd == -1");
2316 if (lseek(*rfd, sector * (off_t)512, 0) == -1)
2317 return (-1);
2318 return (read(*rfd, buf, 512));
2319 }
2320
2321 int
2322 write_disk(daddr_t sector, void *buf)
2323 {
2324
2325 if (wfd == -1)
2326 errx(1, "write_disk(); wfd == -1");
2327 if (lseek(wfd, sector * (off_t)512, 0) == -1)
2328 return (-1);
2329 return (write(wfd, buf, 512));
2330 }
2331
2332 static void
2333 guess_geometry(daddr_t _sectors)
2334 {
2335 dos_sectors = MAXSECTOR;
2336 dos_heads = MAXHEAD - 1; /* some BIOS might use 256 */
2337 dos_cylinders = _sectors / (MAXSECTOR * (MAXHEAD - 1));
2338 if (dos_cylinders < 1)
2339 dos_cylinders = 1;
2340 else if (dos_cylinders > MAXCYL - 1)
2341 dos_cylinders = MAXCYL - 1;
2342 }
2343
2344 int
2345 get_params(void)
2346 {
2347 if (disk_type != NULL) {
2348 struct disklabel *tmplabel;
2349
2350 if ((tmplabel = getdiskbyname(disk_type)) == NULL) {
2351 warn("bad disktype");
2352 return (-1);
2353 }
2354 disklabel = *tmplabel;
2355 } else if (F_flag) {
2356 struct stat st;
2357 if (fstat(fd, &st) == -1) {
2358 warn("fstat");
2359 return (-1);
2360 }
2361 if (st.st_size % 512 != 0) {
2362 warnx("%s size (%lld) is not divisible "
2363 "by sector size (%d)", disk, (long long)st.st_size,
2364 512);
2365 }
2366 disklabel.d_secperunit = st.st_size / 512;
2367 guess_geometry(disklabel.d_secperunit);
2368 disklabel.d_ncylinders = dos_cylinders;
2369 disklabel.d_ntracks = dos_heads;
2370 disklabel.d_nsectors = dos_sectors;
2371 } else if (ioctl(fd, DIOCGDEFLABEL, &disklabel) == -1) {
2372 warn("DIOCGDEFLABEL");
2373 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
2374 warn("DIOCGDINFO");
2375 return (-1);
2376 }
2377 }
2378
2379 disksectors = disklabel.d_secperunit;
2380 cylinders = disklabel.d_ncylinders;
2381 heads = disklabel.d_ntracks;
2382 sectors = disklabel.d_nsectors;
2383
2384 /* pick up some defaults for the BIOS sizes */
2385 if (sectors <= MAXSECTOR) {
2386 dos_cylinders = cylinders;
2387 dos_heads = heads;
2388 dos_sectors = sectors;
2389 } else {
2390 /* guess - has to better than the above */
2391 guess_geometry(disksectors);
2392 }
2393 dos_disksectors = disksectors;
2394
2395 return (0);
2396 }
2397
2398 #ifdef BOOTSEL
2399 /*
2400 * Rather unfortunately the bootsel 'magic' number is at the end of the
2401 * the structure, and there is no checksum. So when other operating
2402 * systems install mbr code by only writing the length of their code they
2403 * can overwrite part of the structure but keeping the magic number intact.
2404 * This code attempts to empirically detect this problem.
2405 */
2406 static int
2407 validate_bootsel(struct mbr_bootsel *mbs)
2408 {
2409 unsigned int key = mbs->mbrbs_defkey;
2410 unsigned int tmo;
2411 int i;
2412
2413 if (v_flag)
2414 return 0;
2415
2416 /*
2417 * Check default key is sane
2418 * - this is the most likely field to be stuffed
2419 * 12 disks and 12 bootable partitions seems enough!
2420 * (the keymap decode starts falling apart at that point)
2421 */
2422 if (key != 0 && !(key == SCAN_ENTER
2423 || (key >= SCAN_1 && key < SCAN_1 + 12)
2424 || (key >= SCAN_F1 && key < SCAN_F1 + 12)))
2425 return 1;
2426
2427 /* Checking the flags will lead to breakage... */
2428
2429 /* Timeout value is expecyed to be a multiple of a second */
2430 tmo = htole16(mbs->mbrbs_timeo);
2431 if (tmo != 0 && tmo != 0xffff && tmo != (10 * tmo + 9) / 182 * 182 / 10)
2432 return 2;
2433
2434 /* Check the menu strings are printable */
2435 /* Unfortunately they aren't zero filled... */
2436 for (i = 0; i < sizeof(mbs->mbrbs_nametab); i++) {
2437 int c = (uint8_t)mbs->mbrbs_nametab[0][i];
2438 if (c == 0 || isprint(c))
2439 continue;
2440 return 3;
2441 }
2442
2443 return 0;
2444 }
2445 #endif
2446
2447 int
2448 read_s0(daddr_t offset, struct mbr_sector *boot)
2449 {
2450 const char *tabletype = offset ? "extended" : "primary";
2451 #ifdef BOOTSEL
2452 static int reported;
2453 #endif
2454
2455 if (read_disk(offset, boot) == -1) {
2456 warn("Can't read %s partition table", tabletype);
2457 return -1;
2458 }
2459 if (boot->mbr_magic != LE_MBR_MAGIC) {
2460 warnx("%s partition table invalid, "
2461 "no magic in sector %"PRIdaddr, tabletype, offset);
2462 return -1;
2463
2464 }
2465 #ifdef BOOTSEL
2466 if (boot->mbr_bootsel_magic == LE_MBR_BS_MAGIC) {
2467 /* mbr_bootsel in new location */
2468 if (validate_bootsel(&boot->mbr_bootsel)) {
2469 warnx("removing corrupt bootsel information");
2470 boot->mbr_bootsel_magic = 0;
2471 }
2472 return 0;
2473 }
2474 if (boot->mbr_bootsel_magic != LE_MBR_MAGIC)
2475 return 0;
2476
2477 /* mbr_bootsel in old location */
2478 if (!reported)
2479 warnx("%s partition table: using old-style bootsel information",
2480 tabletype);
2481 reported = 1;
2482 if (validate_bootsel((void *)((uint8_t *)boot + MBR_BS_OFFSET + 4))) {
2483 warnx("%s bootsel information corrupt - ignoring", tabletype);
2484 return 0;
2485 }
2486 memmove((uint8_t *)boot + MBR_BS_OFFSET,
2487 (uint8_t *)boot + MBR_BS_OFFSET + 4,
2488 sizeof(struct mbr_bootsel));
2489 if ( ! (boot->mbr_bootsel.mbrbs_flags & MBR_BS_NEWMBR)) {
2490 /* old style default key */
2491 int id;
2492 /* F1..F4 => ptn 0..3, F5+ => disk 0+ */
2493 id = boot->mbr_bootsel.mbrbs_defkey;
2494 id -= SCAN_F1;
2495 if (id >= MBR_PART_COUNT)
2496 id -= MBR_PART_COUNT; /* Use number of disk */
2497 else if (mboot.mbr_parts[id].mbrp_type != 0)
2498 id = le32toh(boot->mbr_parts[id].mbrp_start);
2499 else
2500 id = DEFAULT_ACTIVE;
2501 boot->mbr_bootsel.mbrbs_defkey = id;
2502 }
2503 boot->mbr_bootsel_magic = LE_MBR_BS_MAGIC;
2504 /* highlight that new bootsel code is necessary */
2505 boot->mbr_bootsel.mbrbs_flags &= ~MBR_BS_NEWMBR;
2506 #endif /* BOOTSEL */
2507 return 0;
2508 }
2509
2510 int
2511 write_mbr(void)
2512 {
2513 int flag, i;
2514 daddr_t offset;
2515 int rval = -1;
2516
2517 /*
2518 * write enable label sector before write (if necessary),
2519 * disable after writing.
2520 * needed if the disklabel protected area also protects
2521 * sector 0. (e.g. empty disk)
2522 */
2523 flag = 1;
2524 if (wfd == fd && F_flag == 0 && ioctl(wfd, DIOCWLABEL, &flag) < 0)
2525 warn("DIOCWLABEL");
2526 if (write_disk(0, &mboot) == -1) {
2527 warn("Can't write fdisk partition table");
2528 goto protect_label;
2529 }
2530 if (boot_installed)
2531 for (i = bootsize; (i -= 0x200) > 0;)
2532 if (write_disk(i / 0x200, &bootcode[i / 0x200]) == -1) {
2533 warn("Can't write bootcode");
2534 goto protect_label;
2535 }
2536 for (offset = 0, i = 0; i < ext.num_ptn; i++) {
2537 if (write_disk(ext.base + offset, ext.ptn + i) == -1) {
2538 warn("Can't write %dth extended partition", i);
2539 goto protect_label;
2540 }
2541 offset = le32toh(ext.ptn[i].mbr_parts[1].mbrp_start);
2542 }
2543 rval = 0;
2544 protect_label:
2545 flag = 0;
2546 if (wfd == fd && F_flag == 0 && ioctl(wfd, DIOCWLABEL, &flag) < 0)
2547 warn("DIOCWLABEL");
2548 return rval;
2549 }
2550
2551 int
2552 yesno(const char *str, ...)
2553 {
2554 int ch, first;
2555 va_list ap;
2556
2557 va_start(ap, str);
2558
2559 vprintf(str, ap);
2560 printf(" [n] ");
2561
2562 first = ch = getchar();
2563 while (ch != '\n' && ch != EOF)
2564 ch = getchar();
2565 if (ch == EOF)
2566 errx(1, "EOF");
2567 return (first == 'y' || first == 'Y');
2568 }
2569
2570 int
2571 decimal(const char *prompt, int dflt, int flags, int minval, int maxval)
2572 {
2573 int acc = 0;
2574 char *cp;
2575 char ch;
2576
2577 for (;;) {
2578 if (flags & DEC_SEC) {
2579 printf("%s: [%d..%dcyl default: %d, %dcyl, %uMB] ",
2580 prompt, SEC_TO_CYL(minval), SEC_TO_CYL(maxval),
2581 dflt, SEC_TO_CYL(dflt), SEC_TO_MB(dflt));
2582 } else
2583 printf("%s: [%d..%d default: %d] ",
2584 prompt, minval, maxval, dflt);
2585
2586 if (!fgets(lbuf, LBUF, stdin))
2587 errx(1, "EOF");
2588 lbuf[strlen(lbuf)-1] = '\0';
2589 cp = lbuf;
2590
2591 cp += strspn(cp, " \t");
2592 if (*cp == '\0')
2593 return dflt;
2594
2595 if (cp[0] == '$' && cp[1] == 0)
2596 return maxval;
2597
2598 if (isdigit((unsigned char)*cp) || *cp == '-') {
2599 acc = strtol(lbuf, &cp, 10);
2600 if (flags & DEC_SEC) {
2601 ch = *cp;
2602 if (ch == 'g' || ch == 'G') {
2603 acc *= 1024;
2604 ch = 'm';
2605 }
2606 if (ch == 'm' || ch == 'M') {
2607 acc *= SEC_IN_1M;
2608 /* round to whole number of cylinders */
2609 acc += dos_cylindersectors / 2;
2610 acc /= dos_cylindersectors;
2611 ch = 'c';
2612 }
2613 if (ch == 'c' || ch == 'C') {
2614 cp++;
2615 acc *= dos_cylindersectors;
2616 /* adjustments for cylinder boundary */
2617 if (acc == 0 && flags & DEC_RND_0)
2618 acc += dos_sectors;
2619 if (flags & DEC_RND)
2620 acc += dos_sectors;
2621 if (flags & DEC_RND_DOWN)
2622 acc -= dos_sectors;
2623 if (flags & DEC_RND_DOWN_2)
2624 acc -= dos_sectors;
2625 }
2626 }
2627 }
2628
2629 cp += strspn(cp, " \t");
2630 if (*cp != '\0') {
2631 printf("%s is not a valid %s number.\n", lbuf,
2632 flags & DEC_SEC ? "sector" : "decimal");
2633 continue;
2634 }
2635
2636 if (acc >= minval && acc <= maxval)
2637 return acc;
2638 printf("%d is not between %d and %d.\n", acc, minval, maxval);
2639 }
2640 }
2641
2642 int
2643 ptn_id(const char *prompt, int *extended)
2644 {
2645 unsigned int acc = 0;
2646 char *cp;
2647
2648 for (;; printf("%s is not a valid partition number.\n", lbuf)) {
2649 printf("%s: [none] ", prompt);
2650
2651 if (!fgets(lbuf, LBUF, stdin))
2652 errx(1, "EOF");
2653 lbuf[strlen(lbuf)-1] = '\0';
2654 cp = lbuf;
2655
2656 cp += strspn(cp, " \t");
2657 *extended = 0;
2658 if (*cp == 0)
2659 return -1;
2660
2661 if (*cp == 'E' || *cp == 'e') {
2662 cp++;
2663 *extended = 1;
2664 }
2665
2666 acc = strtoul(cp, &cp, 10);
2667
2668 cp += strspn(cp, " \t");
2669 if (*cp != '\0')
2670 continue;
2671
2672 if (*extended || acc < MBR_PART_COUNT)
2673 return acc;
2674 }
2675 }
2676
2677 #ifdef BOOTSEL
2678 void
2679 string(const char *prompt, int length, char *buf)
2680 {
2681 int len;
2682
2683 for (;;) {
2684 printf("%s: [%.*s] ", prompt, length, buf);
2685
2686 if (!fgets(lbuf, LBUF, stdin))
2687 errx(1, "EOF");
2688 len = strlen(lbuf);
2689 if (len <= 1)
2690 /* unchanged if just <enter> */
2691 return;
2692 /* now strip trailing spaces, <space><enter> deletes string */
2693 do
2694 lbuf[--len] = 0;
2695 while (len != 0 && lbuf[len - 1] == ' ');
2696 if (len < length)
2697 break;
2698 printf("'%s' is longer than %d characters.\n",
2699 lbuf, length - 1);
2700 }
2701 strncpy(buf, lbuf, length);
2702 }
2703 #endif
2704
2705 int
2706 type_match(const void *key, const void *item)
2707 {
2708 const int *idp = key;
2709 const struct mbr_ptype *ptr = item;
2710
2711 if (*idp < ptr->id)
2712 return (-1);
2713 if (*idp > ptr->id)
2714 return (1);
2715 return (0);
2716 }
2717
2718 const char *
2719 get_type(int type)
2720 {
2721 struct mbr_ptype *ptr;
2722
2723 ptr = bsearch(&type, mbr_ptypes, KNOWN_SYSIDS,
2724 sizeof(mbr_ptypes[0]), type_match);
2725 if (ptr == 0)
2726 return ("unknown");
2727 return (ptr->name);
2728 }
2729