Sat Aug 27 17:16:01 2011 UTC ()
staticfy. __dead.


(joerg)
diff -r1.131 -r1.132 src/sbin/fdisk/fdisk.c

cvs diff -r1.131 -r1.132 src/sbin/fdisk/fdisk.c (expand / switch to unified diff)

--- src/sbin/fdisk/fdisk.c 2011/05/08 14:22:16 1.131
+++ src/sbin/fdisk/fdisk.c 2011/08/27 17:16:01 1.132
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: fdisk.c,v 1.131 2011/05/08 14:22:16 pgoyette Exp $ */ 1/* $NetBSD: fdisk.c,v 1.132 2011/08/27 17:16:01 joerg Exp $ */
2 2
3/* 3/*
4 * Mach Operating System 4 * Mach Operating System
5 * Copyright (c) 1992 Carnegie Mellon University 5 * Copyright (c) 1992 Carnegie Mellon University
6 * All Rights Reserved. 6 * All Rights Reserved.
7 * 7 *
8 * Permission to use, copy, modify and distribute this software and its 8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright 9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the 10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions 11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation. 12 * thereof, and that both notices appear in supporting documentation.
13 * 13 *
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
@@ -29,27 +29,27 @@ @@ -29,27 +29,27 @@
29/* 29/*
30 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University 30 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
31 * Copyright (c) 1989 Robert. V. Baron 31 * Copyright (c) 1989 Robert. V. Baron
32 * Created. 32 * Created.
33 */ 33 */
34 34
35#if HAVE_NBTOOL_CONFIG_H 35#if HAVE_NBTOOL_CONFIG_H
36#include "nbtool_config.h" 36#include "nbtool_config.h"
37#endif 37#endif
38 38
39#include <sys/cdefs.h> 39#include <sys/cdefs.h>
40 40
41#ifndef lint 41#ifndef lint
42__RCSID("$NetBSD: fdisk.c,v 1.131 2011/05/08 14:22:16 pgoyette Exp $"); 42__RCSID("$NetBSD: fdisk.c,v 1.132 2011/08/27 17:16:01 joerg Exp $");
43#endif /* not lint */ 43#endif /* not lint */
44 44
45#define MBRPTYPENAMES 45#define MBRPTYPENAMES
46#include <sys/types.h> 46#include <sys/types.h>
47#include <sys/param.h> 47#include <sys/param.h>
48#include <sys/stat.h> 48#include <sys/stat.h>
49#include <ctype.h> 49#include <ctype.h>
50#include <err.h> 50#include <err.h>
51#include <errno.h> 51#include <errno.h>
52#include <fcntl.h> 52#include <fcntl.h>
53#include <paths.h> 53#include <paths.h>
54#include <stdarg.h> 54#include <stdarg.h>
55#include <stddef.h> 55#include <stddef.h>
@@ -130,34 +130,34 @@ __RCSID("$NetBSD: fdisk.c,v 1.131 2011/0 @@ -130,34 +130,34 @@ __RCSID("$NetBSD: fdisk.c,v 1.131 2011/0
130 130
131struct { 131struct {
132 struct mbr_sector *ptn; /* array of pbrs */ 132 struct mbr_sector *ptn; /* array of pbrs */
133 daddr_t base; /* first sector of ext. ptn */ 133 daddr_t base; /* first sector of ext. ptn */
134 daddr_t limit; /* last sector of ext. ptn */ 134 daddr_t limit; /* last sector of ext. ptn */
135 int num_ptn; /* number of contained partitions */ 135 int num_ptn; /* number of contained partitions */
136 int ptn_id; /* entry in mbr */ 136 int ptn_id; /* entry in mbr */
137 int is_corrupt; /* 1 if extended chain illegal */ 137 int is_corrupt; /* 1 if extended chain illegal */
138} ext; 138} ext;
139 139
140#define LBUF 100 140#define LBUF 100
141static char lbuf[LBUF]; 141static char lbuf[LBUF];
142 142
143const char *disk = _PATH_DEFDISK; 143static const char *disk = _PATH_DEFDISK;
144 144
145struct disklabel disklabel; /* disk parameters */ 145static struct disklabel disklabel; /* disk parameters */
146 146
147struct mbr_sector mboot; 147static struct mbr_sector mboot;
148 148
149const char *boot_dir = DEFAULT_BOOTDIR; 149static const char *boot_dir = DEFAULT_BOOTDIR;
150char *boot_path = 0; /* name of file we actually opened */ 150static char *boot_path = NULL; /* name of file we actually opened */
151 151
152#ifdef BOOTSEL 152#ifdef BOOTSEL
153#define BOOTSEL_OPTIONS "B" 153#define BOOTSEL_OPTIONS "B"
154#else 154#else
155#define BOOTSEL_OPTIONS  155#define BOOTSEL_OPTIONS
156#define change_part(e, p, id, st, sz, bm) change__part(e, p, id, st, sz) 156#define change_part(e, p, id, st, sz, bm) change__part(e, p, id, st, sz)
157#endif 157#endif
158#define OPTIONS BOOTSEL_OPTIONS "0123FSafiluvA:b:c:E:r:s:w:" 158#define OPTIONS BOOTSEL_OPTIONS "0123FSafiluvA:b:c:E:r:s:w:"
159 159
160/* 160/*
161 * Disk geometry and partition alignment. 161 * Disk geometry and partition alignment.
162 * 162 *
163 * Modern disks do not have a fixed geomery and will always give a 'faked' 163 * Modern disks do not have a fixed geomery and will always give a 'faked'
@@ -181,137 +181,137 @@ char *boot_path = 0; /* name of file w @@ -181,137 +181,137 @@ char *boot_path = 0; /* name of file w
181 * 181 *
182 * However non-magnetic media in particular has physical sectors that are 182 * However non-magnetic media in particular has physical sectors that are
183 * not the same size as those reported, so has to do read modify write 183 * not the same size as those reported, so has to do read modify write
184 * sequences for misaligned transfers. The alignment of partitions to 184 * sequences for misaligned transfers. The alignment of partitions to
185 * cylinder boundaries makes this happen all the time. 185 * cylinder boundaries makes this happen all the time.
186 * 186 *
187 * It is thus sensible to align partitions on a sensible sector boundary. 187 * It is thus sensible to align partitions on a sensible sector boundary.
188 * For instance 1MB (2048 sectors). 188 * For instance 1MB (2048 sectors).
189 * Common code can do this by using a geometry with 1 head and 2048 189 * Common code can do this by using a geometry with 1 head and 2048
190 * sectors per track. 190 * sectors per track.
191 */ 191 */
192 192
193/* Disks reported geometry and overall size from device driver */ 193/* Disks reported geometry and overall size from device driver */
194unsigned int cylinders, sectors, heads; 194static unsigned int cylinders, sectors, heads;
195daddr_t disksectors; 195static daddr_t disksectors;
196#define cylindersectors (heads * sectors) 196#define cylindersectors (heads * sectors)
197 197
198/* Geometry from the BIOS */ 198/* Geometry from the BIOS */
199unsigned int dos_cylinders; 199static unsigned int dos_cylinders;
200unsigned int dos_heads; 200static unsigned int dos_heads;
201unsigned int dos_sectors; 201static unsigned int dos_sectors;
202daddr_t dos_disksectors; 202static daddr_t dos_disksectors;
203#define dos_cylindersectors (dos_heads * dos_sectors) 203#define dos_cylindersectors (dos_heads * dos_sectors)
204#define dos_totalsectors (dos_heads * dos_sectors * dos_cylinders) 204#define dos_totalsectors (dos_heads * dos_sectors * dos_cylinders)
205 205
206#define DOSSECT(s,c) (((s) & 0x3f) | (((c) >> 2) & 0xc0)) 206#define DOSSECT(s,c) (((s) & 0x3f) | (((c) >> 2) & 0xc0))
207#define DOSCYL(c) ((c) & 0xff) 207#define DOSCYL(c) ((c) & 0xff)
208#define SEC_IN_1M (1024 * 1024 / 512) 208#define SEC_IN_1M (1024 * 1024 / 512)
209#define SEC_TO_MB(sec) ((unsigned int)(((sec) + SEC_IN_1M / 2) / SEC_IN_1M)) 209#define SEC_TO_MB(sec) ((unsigned int)(((sec) + SEC_IN_1M / 2) / SEC_IN_1M))
210#define SEC_TO_CYL(sec) (((sec) + dos_cylindersectors/2) / dos_cylindersectors) 210#define SEC_TO_CYL(sec) (((sec) + dos_cylindersectors/2) / dos_cylindersectors)
211 211
212#define MAXCYL 1024 /* Usual limit is 1023 */ 212#define MAXCYL 1024 /* Usual limit is 1023 */
213#define MAXHEAD 256 /* Usual limit is 255 */ 213#define MAXHEAD 256 /* Usual limit is 255 */
214#define MAXSECTOR 63 214#define MAXSECTOR 63
215int partition = -1; 215static int partition = -1;
216 216
217/* Alignment of partition, and offset if first sector unusable */ 217/* Alignment of partition, and offset if first sector unusable */
218unsigned int ptn_alignment; /* default dos_cylindersectors */ 218static unsigned int ptn_alignment; /* default dos_cylindersectors */
219unsigned int ptn_0_offset; /* default dos_sectors */ 219static unsigned int ptn_0_offset; /* default dos_sectors */
220 220
221int fd = -1, wfd = -1, *rfd = &fd; 221static int fd = -1, wfd = -1, *rfd = &fd;
222char *disk_file = NULL; 222static char *disk_file = NULL;
223char *disk_type = NULL; 223static char *disk_type = NULL;
224 224
225int a_flag; /* set active partition */ 225static int a_flag; /* set active partition */
226int i_flag; /* init bootcode */ 226static int i_flag; /* init bootcode */
227int u_flag; /* update partition data */ 227static int u_flag; /* update partition data */
228int v_flag; /* more verbose */ 228static int v_flag; /* more verbose */
229int sh_flag; /* Output data as shell defines */ 229static int sh_flag; /* Output data as shell defines */
230int f_flag; /* force --not interactive */ 230static int f_flag; /* force --not interactive */
231int s_flag; /* set id,offset,size */ 231static int s_flag; /* set id,offset,size */
232int b_flag; /* Set cyl, heads, secs (as c/h/s) */ 232static int b_flag; /* Set cyl, heads, secs (as c/h/s) */
233int B_flag; /* Edit/install bootselect code */ 233static int B_flag; /* Edit/install bootselect code */
234int E_flag; /* extended partition number */ 234static int E_flag; /* extended partition number */
235int b_cyl, b_head, b_sec; /* b_flag values. */ 235static int b_cyl, b_head, b_sec; /* b_flag values. */
236 236
237#if !HAVE_NBTOOL_CONFIG_H 237#if !HAVE_NBTOOL_CONFIG_H
238int F_flag = 0; 238static int F_flag = 0;
239#else 239#else
240/* Tool - force 'file' mode to avoid unsupported functions and ioctls */ 240/* Tool - force 'file' mode to avoid unsupported functions and ioctls */
241int F_flag = 1; 241static int F_flag = 1;
242#endif 242#endif
243 243
244struct gpt_hdr gpt1, gpt2; /* GUID partition tables */ 244static struct gpt_hdr gpt1, gpt2; /* GUID partition tables */
245 245
246struct mbr_sector bootcode[8192 / sizeof (struct mbr_sector)]; 246static struct mbr_sector bootcode[8192 / sizeof (struct mbr_sector)];
247int bootsize; /* actual size of bootcode */ 247static int bootsize; /* actual size of bootcode */
248int boot_installed; /* 1 if we've copied code into the mbr */ 248static int boot_installed; /* 1 if we've copied code into the mbr */
249 249
250#if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H 250#if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
251#define USE_DISKLIST 251#define USE_DISKLIST
252struct disklist *dl; 252static struct disklist *dl;
253#endif 253#endif
254 254
255 255
256#define KNOWN_SYSIDS (sizeof(mbr_ptypes)/sizeof(mbr_ptypes[0])) 256#define KNOWN_SYSIDS (sizeof(mbr_ptypes)/sizeof(mbr_ptypes[0]))
257 257
258void usage(void); 258__dead static void usage(void);
259void print_s0(int); 259static void print_s0(int);
260void print_part(struct mbr_sector *, int, daddr_t); 260static void print_part(struct mbr_sector *, int, daddr_t);
261void print_mbr_partition(struct mbr_sector *, int, daddr_t, daddr_t, int); 261static void print_mbr_partition(struct mbr_sector *, int, daddr_t, daddr_t, int);
262void print_pbr(daddr_t, int, uint8_t); 262static void print_pbr(daddr_t, int, uint8_t);
263int is_all_zero(const unsigned char *, size_t); 263static int is_all_zero(const unsigned char *, size_t);
264void printvis(int, const char *, const char *, size_t); 264static void printvis(int, const char *, const char *, size_t);
265int read_boot(const char *, void *, size_t, int); 265static int read_boot(const char *, void *, size_t, int);
266void init_sector0(int); 266static void init_sector0(int);
267void intuit_translated_geometry(void); 267static void intuit_translated_geometry(void);
268void get_bios_geometry(void); 268static void get_bios_geometry(void);
269void get_extended_ptn(void); 269static void get_extended_ptn(void);
270static void get_ptn_alignmemt(void); 270static static void get_ptn_alignmemt(void);
271#if defined(USE_DISKLIST) 271#if defined(USE_DISKLIST)
272void get_diskname(const char *, char *, size_t); 272static void get_diskname(const char *, char *, size_t);
273#endif 273#endif
274int change_part(int, int, int, daddr_t, daddr_t, char *); 274static int change_part(int, int, int, daddr_t, daddr_t, char *);
275void print_geometry(void); 275static void print_geometry(void);
276int first_active(void); 276static int first_active(void);
277void change_active(int); 277static void change_active(int);
278void change_bios_geometry(void); 278static void change_bios_geometry(void);
279void dos(int, unsigned char *, unsigned char *, unsigned char *); 279static void dos(int, unsigned char *, unsigned char *, unsigned char *);
280int open_disk(int); 280static int open_disk(int);
281int read_disk(daddr_t, void *); 281static int read_disk(daddr_t, void *);
282int write_disk(daddr_t, void *); 282static int write_disk(daddr_t, void *);
283int get_params(void); 283static int get_params(void);
284int read_s0(daddr_t, struct mbr_sector *); 284static int read_s0(daddr_t, struct mbr_sector *);
285int write_mbr(void); 285static int write_mbr(void);
286int read_gpt(daddr_t, struct gpt_hdr *); 286static int read_gpt(daddr_t, struct gpt_hdr *);
287int delete_gpt(struct gpt_hdr *); 287static int delete_gpt(struct gpt_hdr *);
288int yesno(const char *, ...); 288static int yesno(const char *, ...);
289int64_t decimal(const char *, int64_t, int, int64_t, int64_t); 289static int64_t decimal(const char *, int64_t, int, int64_t, int64_t);
290#define DEC_SEC 1 /* asking for a sector number */ 290#define DEC_SEC 1 /* asking for a sector number */
291#define DEC_RND 2 /* round to end of first track */ 291#define DEC_RND 2 /* round to end of first track */
292#define DEC_RND_0 4 /* convert 0 to size of a track */ 292#define DEC_RND_0 4 /* convert 0 to size of a track */
293#define DEC_RND_DOWN 8 /* subtract 1 track */ 293#define DEC_RND_DOWN 8 /* subtract 1 track */
294#define DEC_RND_DOWN_2 16 /* subtract 2 tracks */ 294#define DEC_RND_DOWN_2 16 /* subtract 2 tracks */
295void string(const char *, int, char *); 295static void string(const char *, int, char *);
296int ptn_id(const char *, int *); 296static int ptn_id(const char *, int *);
297int type_match(const void *, const void *); 297static int type_match(const void *, const void *);
298const char *get_type(int); 298static const char *get_type(int);
299int get_mapping(int, unsigned int *, unsigned int *, unsigned int *, unsigned long *); 299static int get_mapping(int, unsigned int *, unsigned int *, unsigned int *, unsigned long *);
300#ifdef BOOTSEL 300#ifdef BOOTSEL
301daddr_t configure_bootsel(daddr_t); 301static daddr_t configure_bootsel(daddr_t);
302void install_bootsel(int); 302static void install_bootsel(int);
303daddr_t get_default_boot(void); 303static daddr_t get_default_boot(void);
304void set_default_boot(daddr_t); 304static void set_default_boot(daddr_t);
305#endif 305#endif
306 306
307static void 307static void
308initvar_disk(const char **diskp) 308initvar_disk(const char **diskp)
309{ 309{
310#if !HAVE_NBTOOL_CONFIG_H 310#if !HAVE_NBTOOL_CONFIG_H
311 int mib[2]; 311 int mib[2];
312 size_t len; 312 size_t len;
313 char *root_device; 313 char *root_device;
314 314
315 mib[0] = CTL_KERN; 315 mib[0] = CTL_KERN;
316 mib[1] = KERN_ROOT_DEVICE; 316 mib[1] = KERN_ROOT_DEVICE;
317 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1 || 317 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1 ||
@@ -585,27 +585,27 @@ main(int argc, char *argv[]) @@ -585,27 +585,27 @@ main(int argc, char *argv[])
585 } 585 }
586 } else { 586 } else {
587 if (delete_gpt(&gpt1) > 0) 587 if (delete_gpt(&gpt1) > 0)
588 warnx("Primary GPT header was deleted"); 588 warnx("Primary GPT header was deleted");
589 if (delete_gpt(&gpt2) > 0) 589 if (delete_gpt(&gpt2) > 0)
590 warnx("Secondary GPT header was deleted"); 590 warnx("Secondary GPT header was deleted");
591 write_mbr(); 591 write_mbr();
592 } 592 }
593 } 593 }
594 594
595 exit(0); 595 exit(0);
596} 596}
597 597
598void 598static void
599usage(void) 599usage(void)
600{ 600{
601 int indent = 7 + (int)strlen(getprogname()) + 1; 601 int indent = 7 + (int)strlen(getprogname()) + 1;
602 602
603 (void)fprintf(stderr, "usage: %s [-afiluvBS] " 603 (void)fprintf(stderr, "usage: %s [-afiluvBS] "
604 "[-A ptn_alignment[/ptn_0_offset]] \\\n" 604 "[-A ptn_alignment[/ptn_0_offset]] \\\n"
605 "%*s[-b cylinders/heads/sectors] \\\n" 605 "%*s[-b cylinders/heads/sectors] \\\n"
606 "%*s[-0123 | -E num " 606 "%*s[-0123 | -E num "
607 "[-s id/start/size[/bootmenu]]] \\\n" 607 "[-s id/start/size[/bootmenu]]] \\\n"
608 "%*s[-t disktab] [-T disktype] \\\n" 608 "%*s[-t disktab] [-T disktype] \\\n"
609 "%*s[-c bootcode] " 609 "%*s[-c bootcode] "
610 "[-r|-w file] [device]\n" 610 "[-r|-w file] [device]\n"
611 "\t-a change active partition\n" 611 "\t-a change active partition\n"
@@ -622,27 +622,27 @@ usage(void) @@ -622,27 +622,27 @@ usage(void)
622 exit(1); 622 exit(1);
623} 623}
624 624
625static daddr_t 625static daddr_t
626ext_offset(int part) 626ext_offset(int part)
627{ 627{
628 daddr_t offset = ext.base; 628 daddr_t offset = ext.base;
629 629
630 if (part != 0) 630 if (part != 0)
631 offset += le32toh(ext.ptn[part - 1].mbr_parts[1].mbrp_start); 631 offset += le32toh(ext.ptn[part - 1].mbr_parts[1].mbrp_start);
632 return offset; 632 return offset;
633} 633}
634 634
635void 635static void
636print_s0(int which) 636print_s0(int which)
637{ 637{
638 int part; 638 int part;
639 639
640 if (which == -1) { 640 if (which == -1) {
641 if (!sh_flag) 641 if (!sh_flag)
642 printf("Partition table:\n"); 642 printf("Partition table:\n");
643 for (part = 0; part < MBR_PART_COUNT; part++) { 643 for (part = 0; part < MBR_PART_COUNT; part++) {
644 if (!sh_flag) 644 if (!sh_flag)
645 printf("%d: ", part); 645 printf("%d: ", part);
646 print_part(&mboot, part, 0); 646 print_part(&mboot, part, 0);
647 } 647 }
648 if (!sh_flag) { 648 if (!sh_flag) {
@@ -698,27 +698,27 @@ print_s0(int which) @@ -698,27 +698,27 @@ print_s0(int which)
698 if (!sh_flag) 698 if (!sh_flag)
699 printf("Extended partition E%d:\n", which); 699 printf("Extended partition E%d:\n", which);
700 if (which > ext.num_ptn) 700 if (which > ext.num_ptn)
701 printf("Undefined\n"); 701 printf("Undefined\n");
702 else 702 else
703 print_part(&ext.ptn[which], 0, ext_offset(which)); 703 print_part(&ext.ptn[which], 0, ext_offset(which));
704 } else { 704 } else {
705 if (!sh_flag) 705 if (!sh_flag)
706 printf("Partition %d:\n", which); 706 printf("Partition %d:\n", which);
707 print_part(&mboot, which, 0); 707 print_part(&mboot, which, 0);
708 } 708 }
709} 709}
710 710
711void 711static void
712print_part(struct mbr_sector *boot, int part, daddr_t offset) 712print_part(struct mbr_sector *boot, int part, daddr_t offset)
713{ 713{
714 struct mbr_partition *partp; 714 struct mbr_partition *partp;
715 const char *e; 715 const char *e;
716 716
717 if (!sh_flag) { 717 if (!sh_flag) {
718 print_mbr_partition(boot, part, offset, 0, 0); 718 print_mbr_partition(boot, part, offset, 0, 0);
719 return; 719 return;
720 } 720 }
721 721
722 partp = &boot->mbr_parts[part]; 722 partp = &boot->mbr_parts[part];
723 if (boot != &mboot) { 723 if (boot != &mboot) {
724 part = boot - ext.ptn; 724 part = boot - ext.ptn;
@@ -758,27 +758,27 @@ pr_cyls(daddr_t sector, int is_end) @@ -758,27 +758,27 @@ pr_cyls(daddr_t sector, int is_end)
758 printf("%lu", cyl); 758 printf("%lu", cyl);
759 759
760 if (is_end) { 760 if (is_end) {
761 if (head == dos_heads - 1 && sect == dos_sectors - 1) 761 if (head == dos_heads - 1 && sect == dos_sectors - 1)
762 return; 762 return;
763 } else { 763 } else {
764 if (head == 0 && sect == 0) 764 if (head == 0 && sect == 0)
765 return; 765 return;
766 } 766 }
767 767
768 printf("/%lu/%lu", head, sect + 1); 768 printf("/%lu/%lu", head, sect + 1);
769} 769}
770 770
771void 771static void
772print_mbr_partition(struct mbr_sector *boot, int part, 772print_mbr_partition(struct mbr_sector *boot, int part,
773 daddr_t offset, daddr_t exoffset, int indent) 773 daddr_t offset, daddr_t exoffset, int indent)
774{ 774{
775 daddr_t start; 775 daddr_t start;
776 daddr_t size; 776 daddr_t size;
777 struct mbr_partition *partp = &boot->mbr_parts[part]; 777 struct mbr_partition *partp = &boot->mbr_parts[part];
778 struct mbr_sector eboot; 778 struct mbr_sector eboot;
779 int p; 779 int p;
780 static int dumped = 0; 780 static int dumped = 0;
781 781
782 if (partp->mbrp_type == 0 && v_flag < 2) { 782 if (partp->mbrp_type == 0 && v_flag < 2) {
783 printf("<UNUSED>\n"); 783 printf("<UNUSED>\n");
784 return; 784 return;
@@ -858,69 +858,69 @@ print_mbr_partition(struct mbr_sector *b @@ -858,69 +858,69 @@ print_mbr_partition(struct mbr_sector *b
858 if (read_s0(start, &eboot) == -1) 858 if (read_s0(start, &eboot) == -1)
859 return; 859 return;
860 for (p = 0; p < MBR_PART_COUNT; p++) { 860 for (p = 0; p < MBR_PART_COUNT; p++) {
861 printf("%*s%d: ", indent, "", p); 861 printf("%*s%d: ", indent, "", p);
862 print_mbr_partition(&eboot, p, start, 862 print_mbr_partition(&eboot, p, start,
863 exoffset ? exoffset : start, indent); 863 exoffset ? exoffset : start, indent);
864 } 864 }
865 865
866 if (exoffset == 0) 866 if (exoffset == 0)
867 dumped = 1; 867 dumped = 1;
868} 868}
869 869
870/* Print a line with a label and a vis-encoded string */ 870/* Print a line with a label and a vis-encoded string */
871void 871static void
872printvis(int indent, const char *label, const char *buf, size_t size) 872printvis(int indent, const char *label, const char *buf, size_t size)
873{ 873{
874 char *visbuf; 874 char *visbuf;
875 875
876 if ((visbuf = malloc(size * 4 + 1)) == NULL) 876 if ((visbuf = malloc(size * 4 + 1)) == NULL)
877 err(1, "Malloc failed"); 877 err(1, "Malloc failed");
878 strsvisx(visbuf, buf, size, VIS_TAB|VIS_NL|VIS_OCTAL, "\""); 878 strsvisx(visbuf, buf, size, VIS_TAB|VIS_NL|VIS_OCTAL, "\"");
879 printf("%*s%s: \"%s\"\n", 879 printf("%*s%s: \"%s\"\n",
880 indent, "", 880 indent, "",
881 label, visbuf); 881 label, visbuf);
882 free(visbuf); 882 free(visbuf);
883} 883}
884 884
885/* Check whether a buffer contains all bytes zero */ 885/* Check whether a buffer contains all bytes zero */
886int 886static int
887is_all_zero(const unsigned char *p, size_t size) 887is_all_zero(const unsigned char *p, size_t size)
888{ 888{
889 889
890 while (size-- > 0) { 890 while (size-- > 0) {
891 if (*p++ != 0) 891 if (*p++ != 0)
892 return 0; 892 return 0;
893 } 893 }
894 return 1; 894 return 1;
895} 895}
896 896
897/* 897/*
898 * Report on the contents of a PBR sector. 898 * Report on the contents of a PBR sector.
899 * 899 *
900 * We first perform several sanity checks. If vflag >= 2, we report all 900 * We first perform several sanity checks. If vflag >= 2, we report all
901 * failing tests, but for smaller values of v_flag we stop after the 901 * failing tests, but for smaller values of v_flag we stop after the
902 * first failing test. Tests are ordered in an attempt to get the most 902 * first failing test. Tests are ordered in an attempt to get the most
903 * useful error message from the first failing test. 903 * useful error message from the first failing test.
904 * 904 *
905 * If v_flag >= 2, we also report some decoded values from the PBR. 905 * If v_flag >= 2, we also report some decoded values from the PBR.
906 * These results may be meaningless, if the PBR doesn't follow common 906 * These results may be meaningless, if the PBR doesn't follow common
907 * conventions. 907 * conventions.
908 * 908 *
909 * Trying to decode anything more than the magic number in the last 909 * Trying to decode anything more than the magic number in the last
910 * two bytes is a layering violation, but it can be very useful in 910 * two bytes is a layering violation, but it can be very useful in
911 * diagnosing boot failures. 911 * diagnosing boot failures.
912 */ 912 */
913void 913static void
914print_pbr(daddr_t sector, int indent, uint8_t part_type) 914print_pbr(daddr_t sector, int indent, uint8_t part_type)
915{ 915{
916 struct mbr_sector pboot; 916 struct mbr_sector pboot;
917 unsigned char *p, *endp; 917 unsigned char *p, *endp;
918 unsigned char val; 918 unsigned char val;
919 int ok; 919 int ok;
920 int errcount = 0; 920 int errcount = 0;
921 921
922#define PBR_ERROR(...) \ 922#define PBR_ERROR(...) \
923 do { \ 923 do { \
924 ++errcount; \ 924 ++errcount; \
925 printf("%*s%s: ", indent, "", \ 925 printf("%*s%s: ", indent, "", \
926 (v_flag < 2 ? "PBR is not bootable" : "Not bootable")); \ 926 (v_flag < 2 ? "PBR is not bootable" : "Not bootable")); \
@@ -981,27 +981,27 @@ print_pbr(daddr_t sector, int indent, ui @@ -981,27 +981,27 @@ print_pbr(daddr_t sector, int indent, ui
981 sizeof(pboot.mbr_oemname)); 981 sizeof(pboot.mbr_oemname));
982 } 982 }
983 983
984 if (pboot.mbr_bpb.bpb16.bsBootSig == 0x29) 984 if (pboot.mbr_bpb.bpb16.bsBootSig == 0x29)
985 printf("%*sBPB FAT16 boot signature found\n", 985 printf("%*sBPB FAT16 boot signature found\n",
986 indent, ""); 986 indent, "");
987 if (pboot.mbr_bpb.bpb32.bsBootSig == 0x29) 987 if (pboot.mbr_bpb.bpb32.bsBootSig == 0x29)
988 printf("%*sBPB FAT32 boot signature found\n", 988 printf("%*sBPB FAT32 boot signature found\n",
989 indent, ""); 989 indent, "");
990 990
991#undef PBR_ERROR 991#undef PBR_ERROR
992} 992}
993 993
994int 994static int
995read_boot(const char *name, void *buf, size_t len, int err_exit) 995read_boot(const char *name, void *buf, size_t len, int err_exit)
996{ 996{
997 int bfd, ret; 997 int bfd, ret;
998 struct stat st; 998 struct stat st;
999 999
1000 if (boot_path != NULL) 1000 if (boot_path != NULL)
1001 free(boot_path); 1001 free(boot_path);
1002 if (strchr(name, '/') == 0) 1002 if (strchr(name, '/') == 0)
1003 asprintf(&boot_path, "%s/%s", boot_dir, name); 1003 asprintf(&boot_path, "%s/%s", boot_dir, name);
1004 else 1004 else
1005 boot_path = strdup(name); 1005 boot_path = strdup(name);
1006 if (boot_path == NULL) 1006 if (boot_path == NULL)
1007 err(1, "Malloc failed"); 1007 err(1, "Malloc failed");
@@ -1035,27 +1035,27 @@ read_boot(const char *name, void *buf, s @@ -1035,27 +1035,27 @@ read_boot(const char *name, void *buf, s
1035 1035
1036 close(bfd); 1036 close(bfd);
1037 ret = (ret + 0x1ff) & ~0x1ff; 1037 ret = (ret + 0x1ff) & ~0x1ff;
1038 return ret; 1038 return ret;
1039 1039
1040 fail: 1040 fail:
1041 if (bfd >= 0) 1041 if (bfd >= 0)
1042 close(bfd); 1042 close(bfd);
1043 if (err_exit) 1043 if (err_exit)
1044 exit(1); 1044 exit(1);
1045 return 0; 1045 return 0;
1046} 1046}
1047 1047
1048void 1048static void
1049init_sector0(int zappart) 1049init_sector0(int zappart)
1050{ 1050{
1051 int i; 1051 int i;
1052 int copy_size = offsetof(struct mbr_sector, mbr_dsn); 1052 int copy_size = offsetof(struct mbr_sector, mbr_dsn);
1053 1053
1054#ifdef DEFAULT_BOOTCODE 1054#ifdef DEFAULT_BOOTCODE
1055 if (bootsize == 0) 1055 if (bootsize == 0)
1056 bootsize = read_boot(DEFAULT_BOOTCODE, bootcode, 1056 bootsize = read_boot(DEFAULT_BOOTCODE, bootcode,
1057 sizeof bootcode, 0); 1057 sizeof bootcode, 0);
1058#endif 1058#endif
1059#ifdef BOOTSEL 1059#ifdef BOOTSEL
1060 if (mboot.mbr_bootsel_magic == LE_MBR_BS_MAGIC 1060 if (mboot.mbr_bootsel_magic == LE_MBR_BS_MAGIC
1061 && bootcode[0].mbr_bootsel_magic == LE_MBR_BS_MAGIC) 1061 && bootcode[0].mbr_bootsel_magic == LE_MBR_BS_MAGIC)
@@ -1065,27 +1065,27 @@ init_sector0(int zappart) @@ -1065,27 +1065,27 @@ init_sector0(int zappart)
1065 if (bootsize != 0) { 1065 if (bootsize != 0) {
1066 boot_installed = 1; 1066 boot_installed = 1;
1067 memcpy(&mboot, bootcode, copy_size); 1067 memcpy(&mboot, bootcode, copy_size);
1068 mboot.mbr_bootsel_magic = bootcode[0].mbr_bootsel_magic; 1068 mboot.mbr_bootsel_magic = bootcode[0].mbr_bootsel_magic;
1069 } 1069 }
1070 mboot.mbr_magic = LE_MBR_MAGIC; 1070 mboot.mbr_magic = LE_MBR_MAGIC;
1071  1071
1072 if (!zappart) 1072 if (!zappart)
1073 return; 1073 return;
1074 for (i = 0; i < MBR_PART_COUNT; i++) 1074 for (i = 0; i < MBR_PART_COUNT; i++)
1075 memset(&mboot.mbr_parts[i], 0, sizeof(mboot.mbr_parts[i])); 1075 memset(&mboot.mbr_parts[i], 0, sizeof(mboot.mbr_parts[i]));
1076} 1076}
1077 1077
1078void 1078static void
1079get_extended_ptn(void) 1079get_extended_ptn(void)
1080{ 1080{
1081 struct mbr_partition *mp; 1081 struct mbr_partition *mp;
1082 struct mbr_sector *boot; 1082 struct mbr_sector *boot;
1083 daddr_t offset; 1083 daddr_t offset;
1084 struct mbr_sector *nptn; 1084 struct mbr_sector *nptn;
1085 1085
1086 /* find first (there should only be one) extended partition */ 1086 /* find first (there should only be one) extended partition */
1087 for (mp = mboot.mbr_parts; !MBR_IS_EXTENDED(mp->mbrp_type); mp++) 1087 for (mp = mboot.mbr_parts; !MBR_IS_EXTENDED(mp->mbrp_type); mp++)
1088 if (mp >= &mboot.mbr_parts[MBR_PART_COUNT]) 1088 if (mp >= &mboot.mbr_parts[MBR_PART_COUNT])
1089 return; 1089 return;
1090 1090
1091 /* 1091 /*
@@ -1125,29 +1125,29 @@ get_extended_ptn(void) @@ -1125,29 +1125,29 @@ get_extended_ptn(void)
1125 /* end of extended partition chain */ 1125 /* end of extended partition chain */
1126 return; 1126 return;
1127 /* must be in sector order */ 1127 /* must be in sector order */
1128 if (offset >= le32toh(boot->mbr_parts[1].mbrp_start)) 1128 if (offset >= le32toh(boot->mbr_parts[1].mbrp_start))
1129 break; 1129 break;
1130 } 1130 }
1131 1131
1132 warnx("Extended partition table is corrupt\n"); 1132 warnx("Extended partition table is corrupt\n");
1133 ext.is_corrupt = 1; 1133 ext.is_corrupt = 1;
1134 ext.num_ptn = 0; 1134 ext.num_ptn = 0;
1135} 1135}
1136 1136
1137#if defined(USE_DISKLIST) 1137#if defined(USE_DISKLIST)
1138void  1138static void
1139get_diskname(const char *fullname, char *diskname, size_t size) 1139get_diskname(const char *fullname, char *diskname, size_t size)
1140{  1140{
1141 const char *p, *p2; 1141 const char *p, *p2;
1142 size_t len; 1142 size_t len;
1143 1143
1144 p = strrchr(fullname, '/'); 1144 p = strrchr(fullname, '/');
1145 if (p == NULL) 1145 if (p == NULL)
1146 p = fullname; 1146 p = fullname;
1147 else 1147 else
1148 p++; 1148 p++;
1149 1149
1150 if (*p == 0) { 1150 if (*p == 0) {
1151 strlcpy(diskname, fullname, size); 1151 strlcpy(diskname, fullname, size);
1152 return; 1152 return;
1153 } 1153 }
@@ -1200,27 +1200,27 @@ get_ptn_alignmemt(void) @@ -1200,27 +1200,27 @@ get_ptn_alignmemt(void)
1200 /* ptn_base is a power of 2, use it */ 1200 /* ptn_base is a power of 2, use it */
1201 ptn_0_offset = ptn_0_base; 1201 ptn_0_offset = ptn_0_base;
1202 } 1202 }
1203 } 1203 }
1204 } else { 1204 } else {
1205 /* Use 1MB alignment for large disks */ 1205 /* Use 1MB alignment for large disks */
1206 if (disksectors > 2048 * 1024 * 128) { 1206 if (disksectors > 2048 * 1024 * 128) {
1207 ptn_alignment = 2048; 1207 ptn_alignment = 2048;
1208 ptn_0_offset = 2048; 1208 ptn_0_offset = 2048;
1209 } 1209 }
1210 } 1210 }
1211} 1211}
1212 1212
1213void 1213static void
1214get_bios_geometry(void) 1214get_bios_geometry(void)
1215{ 1215{
1216#if defined(USE_DISKLIST) 1216#if defined(USE_DISKLIST)
1217 int mib[2], i; 1217 int mib[2], i;
1218 size_t len; 1218 size_t len;
1219 struct biosdisk_info *bip; 1219 struct biosdisk_info *bip;
1220 struct nativedisk_info *nip; 1220 struct nativedisk_info *nip;
1221 char diskname[8]; 1221 char diskname[8];
1222 1222
1223 mib[0] = CTL_MACHDEP; 1223 mib[0] = CTL_MACHDEP;
1224 mib[1] = CPU_DISKINFO; 1224 mib[1] = CPU_DISKINFO;
1225 if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0) { 1225 if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0) {
1226 goto out; 1226 goto out;
@@ -1253,27 +1253,27 @@ get_bios_geometry(void) @@ -1253,27 +1253,27 @@ get_bios_geometry(void)
1253 dos_sectors = bip->bi_sec; 1253 dos_sectors = bip->bi_sec;
1254 if (bip->bi_lbasecs) 1254 if (bip->bi_lbasecs)
1255 dos_disksectors = bip->bi_lbasecs; 1255 dos_disksectors = bip->bi_lbasecs;
1256 return; 1256 return;
1257 } 1257 }
1258 } 1258 }
1259 out: 1259 out:
1260#endif 1260#endif
1261 /* Allright, allright, make a stupid guess.. */ 1261 /* Allright, allright, make a stupid guess.. */
1262 intuit_translated_geometry(); 1262 intuit_translated_geometry();
1263} 1263}
1264 1264
1265#ifdef BOOTSEL 1265#ifdef BOOTSEL
1266daddr_t 1266static daddr_t
1267get_default_boot(void) 1267get_default_boot(void)
1268{ 1268{
1269 unsigned int id; 1269 unsigned int id;
1270 int p; 1270 int p;
1271 1271
1272 if (mboot.mbr_bootsel_magic != LE_MBR_BS_MAGIC) 1272 if (mboot.mbr_bootsel_magic != LE_MBR_BS_MAGIC)
1273 /* default to first active partition */ 1273 /* default to first active partition */
1274 return DEFAULT_ACTIVE; 1274 return DEFAULT_ACTIVE;
1275 1275
1276 id = mboot.mbr_bootsel.mbrbs_defkey; 1276 id = mboot.mbr_bootsel.mbrbs_defkey;
1277 1277
1278 if (mboot.mbr_bootsel.mbrbs_flags & MBR_BS_ASCII) { 1278 if (mboot.mbr_bootsel.mbrbs_flags & MBR_BS_ASCII) {
1279 /* Keycode is ascii */ 1279 /* Keycode is ascii */
@@ -1307,27 +1307,27 @@ get_default_boot(void) @@ -1307,27 +1307,27 @@ get_default_boot(void)
1307 for (p = 0; p < ext.num_ptn; p++) { 1307 for (p = 0; p < ext.num_ptn; p++) {
1308 if (ext.ptn[p].mbr_parts[0].mbrp_type == 0) 1308 if (ext.ptn[p].mbr_parts[0].mbrp_type == 0)
1309 continue; 1309 continue;
1310 if (ext.ptn[p].mbr_bootsel.mbrbs_nametab[0][0] == 0) 1310 if (ext.ptn[p].mbr_bootsel.mbrbs_nametab[0][0] == 0)
1311 continue; 1311 continue;
1312 if (id-- == 0) 1312 if (id-- == 0)
1313 return ext_offset(p) 1313 return ext_offset(p)
1314 + le32toh(ext.ptn[p].mbr_parts[0].mbrp_start); 1314 + le32toh(ext.ptn[p].mbr_parts[0].mbrp_start);
1315 } 1315 }
1316 1316
1317 return DEFAULT_ACTIVE; 1317 return DEFAULT_ACTIVE;
1318} 1318}
1319 1319
1320void 1320static void
1321set_default_boot(daddr_t default_ptn) 1321set_default_boot(daddr_t default_ptn)
1322{ 1322{
1323 int p; 1323 int p;
1324 static const unsigned char key_list[] = { SCAN_ENTER, SCAN_F1, SCAN_1, 1324 static const unsigned char key_list[] = { SCAN_ENTER, SCAN_F1, SCAN_1,
1325 '\r', 'a', '1' }; 1325 '\r', 'a', '1' };
1326 const unsigned char *key = key_list; 1326 const unsigned char *key = key_list;
1327 1327
1328 if (mboot.mbr_bootsel_magic != LE_MBR_BS_MAGIC) 1328 if (mboot.mbr_bootsel_magic != LE_MBR_BS_MAGIC)
1329 /* sanity */ 1329 /* sanity */
1330 return; 1330 return;
1331 1331
1332 if (mboot.mbr_bootsel.mbrbs_flags & MBR_BS_ASCII) 1332 if (mboot.mbr_bootsel.mbrbs_flags & MBR_BS_ASCII)
1333 /* Use ascii values */ 1333 /* Use ascii values */
@@ -1363,27 +1363,27 @@ set_default_boot(daddr_t default_ptn) @@ -1363,27 +1363,27 @@ set_default_boot(daddr_t default_ptn)
1363 if (ext.ptn[p].mbr_bootsel.mbrbs_nametab[0][0] == 0) 1363 if (ext.ptn[p].mbr_bootsel.mbrbs_nametab[0][0] == 0)
1364 continue; 1364 continue;
1365 if (le32toh(ext.ptn[p].mbr_parts[0].mbrp_start) + 1365 if (le32toh(ext.ptn[p].mbr_parts[0].mbrp_start) +
1366 ext_offset(p) == default_ptn) 1366 ext_offset(p) == default_ptn)
1367 return; 1367 return;
1368 mboot.mbr_bootsel.mbrbs_defkey++; 1368 mboot.mbr_bootsel.mbrbs_defkey++;
1369 } 1369 }
1370 } 1370 }
1371 1371
1372 /* Default to first active partition */ 1372 /* Default to first active partition */
1373 mboot.mbr_bootsel.mbrbs_defkey = key[0]; 1373 mboot.mbr_bootsel.mbrbs_defkey = key[0];
1374} 1374}
1375 1375
1376void 1376static void
1377install_bootsel(int needed) 1377install_bootsel(int needed)
1378{ 1378{
1379 struct mbr_bootsel *mbs = &mboot.mbr_bootsel; 1379 struct mbr_bootsel *mbs = &mboot.mbr_bootsel;
1380 int p; 1380 int p;
1381 int ext13 = 0; 1381 int ext13 = 0;
1382 const char *code; 1382 const char *code;
1383 1383
1384 needed |= MBR_BS_NEWMBR; /* need new bootsel code */ 1384 needed |= MBR_BS_NEWMBR; /* need new bootsel code */
1385 1385
1386 /* Work out which boot code we need for this configuration */ 1386 /* Work out which boot code we need for this configuration */
1387 for (p = 0; p < MBR_PART_COUNT; p++) { 1387 for (p = 0; p < MBR_PART_COUNT; p++) {
1388 if (mboot.mbr_parts[p].mbrp_type == 0) 1388 if (mboot.mbr_parts[p].mbrp_type == 0)
1389 continue; 1389 continue;
@@ -1467,27 +1467,27 @@ install_bootsel(int needed) @@ -1467,27 +1467,27 @@ install_bootsel(int needed)
1467 boot_path, bootcode[0].mbr_bootsel.mbrbs_flags, 1467 boot_path, bootcode[0].mbr_bootsel.mbrbs_flags,
1468 needed); 1468 needed);
1469 } 1469 }
1470 1470
1471 if (!f_flag && !yesno("Update the bootcode from %s?", boot_path)) 1471 if (!f_flag && !yesno("Update the bootcode from %s?", boot_path))
1472 return; 1472 return;
1473 1473
1474 init_sector0(0); 1474 init_sector0(0);
1475 1475
1476 if (mboot.mbr_bootsel_magic == LE_MBR_BS_MAGIC) 1476 if (mboot.mbr_bootsel_magic == LE_MBR_BS_MAGIC)
1477 mbs->mbrbs_flags = bootcode[0].mbr_bootsel.mbrbs_flags | ext13; 1477 mbs->mbrbs_flags = bootcode[0].mbr_bootsel.mbrbs_flags | ext13;
1478} 1478}
1479 1479
1480daddr_t 1480static daddr_t
1481configure_bootsel(daddr_t default_ptn) 1481configure_bootsel(daddr_t default_ptn)
1482{ 1482{
1483 struct mbr_bootsel *mbs = &mboot.mbr_bootsel; 1483 struct mbr_bootsel *mbs = &mboot.mbr_bootsel;
1484 int i, item, opt; 1484 int i, item, opt;
1485 int tmo; 1485 int tmo;
1486 daddr_t *off; 1486 daddr_t *off;
1487 int num_bios_disks; 1487 int num_bios_disks;
1488 1488
1489#if defined(USE_DISKLIST) 1489#if defined(USE_DISKLIST)
1490 if (dl != NULL) { 1490 if (dl != NULL) {
1491 num_bios_disks = dl->dl_nbiosdisks; 1491 num_bios_disks = dl->dl_nbiosdisks;
1492 if (num_bios_disks > MAX_BIOS_DISKS) 1492 if (num_bios_disks > MAX_BIOS_DISKS)
1493 num_bios_disks = MAX_BIOS_DISKS; 1493 num_bios_disks = MAX_BIOS_DISKS;
@@ -1560,27 +1560,27 @@ configure_bootsel(daddr_t default_ptn) @@ -1560,27 +1560,27 @@ configure_bootsel(daddr_t default_ptn)
1560/* Prerequisite: the disklabel parameters and master boot record must 1560/* Prerequisite: the disklabel parameters and master boot record must
1561 * have been read (i.e. dos_* and mboot are meaningful). 1561 * have been read (i.e. dos_* and mboot are meaningful).
1562 * Specification: modifies dos_cylinders, dos_heads, dos_sectors, and 1562 * Specification: modifies dos_cylinders, dos_heads, dos_sectors, and
1563 * dos_cylindersectors to be consistent with what the 1563 * dos_cylindersectors to be consistent with what the
1564 * partition table is using, if we can find a geometry 1564 * partition table is using, if we can find a geometry
1565 * which is consistent with all partition table entries. 1565 * which is consistent with all partition table entries.
1566 * We may get the number of cylinders slightly wrong (in 1566 * We may get the number of cylinders slightly wrong (in
1567 * the conservative direction). The idea is to be able 1567 * the conservative direction). The idea is to be able
1568 * to create a NetBSD partition on a disk we don't know 1568 * to create a NetBSD partition on a disk we don't know
1569 * the translated geometry of. 1569 * the translated geometry of.
1570 * This routine is only used for non-x86 systems or when we fail to 1570 * This routine is only used for non-x86 systems or when we fail to
1571 * get the BIOS geometry from the kernel. 1571 * get the BIOS geometry from the kernel.
1572 */ 1572 */
1573void 1573static void
1574intuit_translated_geometry(void) 1574intuit_translated_geometry(void)
1575{ 1575{
1576 uint32_t xcylinders; 1576 uint32_t xcylinders;
1577 int xheads = -1, xsectors = -1, i, j; 1577 int xheads = -1, xsectors = -1, i, j;
1578 unsigned int c1, h1, s1, c2, h2, s2; 1578 unsigned int c1, h1, s1, c2, h2, s2;
1579 unsigned long a1, a2; 1579 unsigned long a1, a2;
1580 uint64_t num, denom; 1580 uint64_t num, denom;
1581 1581
1582 /* 1582 /*
1583 * The physical parameters may be invalid as bios geometry. 1583 * The physical parameters may be invalid as bios geometry.
1584 * If we cannot determine the actual bios geometry, we are 1584 * If we cannot determine the actual bios geometry, we are
1585 * better off picking a likely 'faked' geometry than leaving 1585 * better off picking a likely 'faked' geometry than leaving
1586 * the invalid physical one. 1586 * the invalid physical one.
@@ -1668,27 +1668,27 @@ intuit_translated_geometry(void) @@ -1668,27 +1668,27 @@ intuit_translated_geometry(void)
1668 else 1668 else
1669 dos_cylinders = xcylinders; 1669 dos_cylinders = xcylinders;
1670 dos_heads = xheads; 1670 dos_heads = xheads;
1671 dos_sectors = xsectors; 1671 dos_sectors = xsectors;
1672} 1672}
1673 1673
1674/* 1674/*
1675 * For the purposes of intuit_translated_geometry(), treat the partition 1675 * For the purposes of intuit_translated_geometry(), treat the partition
1676 * table as a list of eight mapping between (cylinder, head, sector) 1676 * table as a list of eight mapping between (cylinder, head, sector)
1677 * triplets and absolute sectors. Get the relevant geometry triplet and 1677 * triplets and absolute sectors. Get the relevant geometry triplet and
1678 * absolute sectors for a given entry, or return -1 if it isn't present. 1678 * absolute sectors for a given entry, or return -1 if it isn't present.
1679 * Note: for simplicity, the returned sector is 0-based. 1679 * Note: for simplicity, the returned sector is 0-based.
1680 */ 1680 */
1681int 1681static int
1682get_mapping(int i, unsigned int *cylinder, unsigned int *head, unsigned int *sector, 1682get_mapping(int i, unsigned int *cylinder, unsigned int *head, unsigned int *sector,
1683 unsigned long *absolute) 1683 unsigned long *absolute)
1684{ 1684{
1685 struct mbr_partition *part = &mboot.mbr_parts[i / 2]; 1685 struct mbr_partition *part = &mboot.mbr_parts[i / 2];
1686 1686
1687 if (part->mbrp_type == 0) 1687 if (part->mbrp_type == 0)
1688 return -1; 1688 return -1;
1689 if (i % 2 == 0) { 1689 if (i % 2 == 0) {
1690 *cylinder = MBR_PCYL(part->mbrp_scyl, part->mbrp_ssect); 1690 *cylinder = MBR_PCYL(part->mbrp_scyl, part->mbrp_ssect);
1691 *head = part->mbrp_shd; 1691 *head = part->mbrp_shd;
1692 *sector = MBR_PSECT(part->mbrp_ssect); 1692 *sector = MBR_PSECT(part->mbrp_ssect);
1693 *absolute = le32toh(part->mbrp_start); 1693 *absolute = le32toh(part->mbrp_start);
1694 } else { 1694 } else {
@@ -1942,27 +1942,27 @@ check_ext_overlap(int part, int sysid, d @@ -1942,27 +1942,27 @@ check_ext_overlap(int part, int sysid, d
1942 return "Overlaps another extended partition"; 1942 return "Overlaps another extended partition";
1943 if (fix) { 1943 if (fix) {
1944 if (part == -1) 1944 if (part == -1)
1945 delete_ext_ptn(p); 1945 delete_ext_ptn(p);
1946 else 1946 else
1947 /* must not change numbering yet */ 1947 /* must not change numbering yet */
1948 ext.ptn[p].mbr_parts[0].mbrp_type = 0; 1948 ext.ptn[p].mbr_parts[0].mbrp_type = 0;
1949 } 1949 }
1950 } 1950 }
1951 } 1951 }
1952 return 0; 1952 return 0;
1953} 1953}
1954 1954
1955int 1955static int
1956change_part(int extended, int part, int sysid, daddr_t start, daddr_t size, 1956change_part(int extended, int part, int sysid, daddr_t start, daddr_t size,
1957 char *bootmenu) 1957 char *bootmenu)
1958{ 1958{
1959 struct mbr_partition *partp; 1959 struct mbr_partition *partp;
1960 struct mbr_sector *boot; 1960 struct mbr_sector *boot;
1961 daddr_t offset; 1961 daddr_t offset;
1962 const char *e; 1962 const char *e;
1963 int upart = part; 1963 int upart = part;
1964 int p; 1964 int p;
1965 int fl; 1965 int fl;
1966 daddr_t n_s, n_e; 1966 daddr_t n_s, n_e;
1967 const char *errtext; 1967 const char *errtext;
1968#ifdef BOOTSEL 1968#ifdef BOOTSEL
@@ -2275,27 +2275,27 @@ change_part(int extended, int part, int  @@ -2275,27 +2275,27 @@ change_part(int extended, int part, int
2275 2275
2276 /* If we had to mark an extended partition as deleted because 2276 /* If we had to mark an extended partition as deleted because
2277 * another request would have overlapped it, now is the time 2277 * another request would have overlapped it, now is the time
2278 * to do the actual delete. 2278 * to do the actual delete.
2279 */ 2279 */
2280 if (extended && f_flag) { 2280 if (extended && f_flag) {
2281 for (p = ext.num_ptn; --p >= 0;) 2281 for (p = ext.num_ptn; --p >= 0;)
2282 if (ext.ptn[p].mbr_parts[0].mbrp_type == 0) 2282 if (ext.ptn[p].mbr_parts[0].mbrp_type == 0)
2283 delete_ext_ptn(p); 2283 delete_ext_ptn(p);
2284 } 2284 }
2285 return 1; 2285 return 1;
2286} 2286}
2287 2287
2288void 2288static void
2289print_geometry(void) 2289print_geometry(void)
2290{ 2290{
2291 2291
2292 if (sh_flag) { 2292 if (sh_flag) {
2293 printf("DISK=%s\n", disk); 2293 printf("DISK=%s\n", disk);
2294 printf("DLCYL=%d\nDLHEAD=%d\nDLSEC=%d\nDLSIZE=%"PRIdaddr"\n", 2294 printf("DLCYL=%d\nDLHEAD=%d\nDLSEC=%d\nDLSIZE=%"PRIdaddr"\n",
2295 cylinders, heads, sectors, disksectors); 2295 cylinders, heads, sectors, disksectors);
2296 printf("BCYL=%d\nBHEAD=%d\nBSEC=%d\nBDLSIZE=%"PRIdaddr"\n", 2296 printf("BCYL=%d\nBHEAD=%d\nBSEC=%d\nBDLSIZE=%"PRIdaddr"\n",
2297 dos_cylinders, dos_heads, dos_sectors, dos_disksectors); 2297 dos_cylinders, dos_heads, dos_sectors, dos_disksectors);
2298 printf("NUMEXTPTN=%d\n", ext.num_ptn); 2298 printf("NUMEXTPTN=%d\n", ext.num_ptn);
2299 return; 2299 return;
2300 } 2300 }
2301 2301
@@ -2305,39 +2305,39 @@ print_geometry(void) @@ -2305,39 +2305,39 @@ print_geometry(void)
2305 printf("cylinders: %d, heads: %d, sectors/track: %d " 2305 printf("cylinders: %d, heads: %d, sectors/track: %d "
2306 "(%d sectors/cylinder)\ntotal sectors: %"PRIdaddr"\n\n", 2306 "(%d sectors/cylinder)\ntotal sectors: %"PRIdaddr"\n\n",
2307 cylinders, heads, sectors, cylindersectors, disksectors); 2307 cylinders, heads, sectors, cylindersectors, disksectors);
2308 printf("BIOS disk geometry:\n"); 2308 printf("BIOS disk geometry:\n");
2309 printf("cylinders: %d, heads: %d, sectors/track: %d " 2309 printf("cylinders: %d, heads: %d, sectors/track: %d "
2310 "(%d sectors/cylinder)\ntotal sectors: %"PRIdaddr"\n\n", 2310 "(%d sectors/cylinder)\ntotal sectors: %"PRIdaddr"\n\n",
2311 dos_cylinders, dos_heads, dos_sectors, dos_cylindersectors, 2311 dos_cylinders, dos_heads, dos_sectors, dos_cylindersectors,
2312 dos_disksectors); 2312 dos_disksectors);
2313 printf("Partitions aligned to %d sector boundaries, offset %d\n\n", 2313 printf("Partitions aligned to %d sector boundaries, offset %d\n\n",
2314 ptn_alignment, ptn_0_offset); 2314 ptn_alignment, ptn_0_offset);
2315} 2315}
2316 2316
2317/* Find the first active partition, else return MBR_PART_COUNT */ 2317/* Find the first active partition, else return MBR_PART_COUNT */
2318int 2318static int
2319first_active(void) 2319first_active(void)
2320{ 2320{
2321 struct mbr_partition *partp = &mboot.mbr_parts[0]; 2321 struct mbr_partition *partp = &mboot.mbr_parts[0];
2322 int part; 2322 int part;
2323 2323
2324 for (part = 0; part < MBR_PART_COUNT; part++) 2324 for (part = 0; part < MBR_PART_COUNT; part++)
2325 if (partp[part].mbrp_flag & MBR_PFLAG_ACTIVE) 2325 if (partp[part].mbrp_flag & MBR_PFLAG_ACTIVE)
2326 return part; 2326 return part;
2327 return MBR_PART_COUNT; 2327 return MBR_PART_COUNT;
2328} 2328}
2329 2329
2330void 2330static void
2331change_active(int which) 2331change_active(int which)
2332{ 2332{
2333 struct mbr_partition *partp; 2333 struct mbr_partition *partp;
2334 int part; 2334 int part;
2335 int active = MBR_PART_COUNT; 2335 int active = MBR_PART_COUNT;
2336 2336
2337 partp = &mboot.mbr_parts[0]; 2337 partp = &mboot.mbr_parts[0];
2338 2338
2339 if (a_flag && which != -1) 2339 if (a_flag && which != -1)
2340 active = which; 2340 active = which;
2341 else 2341 else
2342 active = first_active(); 2342 active = first_active();
2343 if (!f_flag) { 2343 if (!f_flag) {
@@ -2350,27 +2350,27 @@ change_active(int which) @@ -2350,27 +2350,27 @@ change_active(int which)
2350 } while (!yesno("Are you happy with this choice?")); 2350 } while (!yesno("Are you happy with this choice?"));
2351 } else 2351 } else
2352 return; 2352 return;
2353 } else 2353 } else
2354 if (active != MBR_PART_COUNT) 2354 if (active != MBR_PART_COUNT)
2355 printf ("Making partition %d active.\n", active); 2355 printf ("Making partition %d active.\n", active);
2356 2356
2357 for (part = 0; part < MBR_PART_COUNT; part++) 2357 for (part = 0; part < MBR_PART_COUNT; part++)
2358 partp[part].mbrp_flag &= ~MBR_PFLAG_ACTIVE; 2358 partp[part].mbrp_flag &= ~MBR_PFLAG_ACTIVE;
2359 if (active < MBR_PART_COUNT) 2359 if (active < MBR_PART_COUNT)
2360 partp[active].mbrp_flag |= MBR_PFLAG_ACTIVE; 2360 partp[active].mbrp_flag |= MBR_PFLAG_ACTIVE;
2361} 2361}
2362 2362
2363void 2363static void
2364change_bios_geometry(void) 2364change_bios_geometry(void)
2365{ 2365{
2366 print_geometry(); 2366 print_geometry();
2367 if (!yesno("Do you want to change our idea of what BIOS thinks?")) 2367 if (!yesno("Do you want to change our idea of what BIOS thinks?"))
2368 return; 2368 return;
2369 2369
2370#if defined(USE_DISKLIST) 2370#if defined(USE_DISKLIST)
2371 if (dl != NULL) { 2371 if (dl != NULL) {
2372 struct biosdisk_info *bip; 2372 struct biosdisk_info *bip;
2373 int i; 2373 int i;
2374 2374
2375 for (i = 0; i < dl->dl_nbiosdisks; i++) { 2375 for (i = 0; i < dl->dl_nbiosdisks; i++) {
2376 if (i == 0) 2376 if (i == 0)
@@ -2390,46 +2390,46 @@ change_bios_geometry(void) @@ -2390,46 +2390,46 @@ change_bios_geometry(void)
2390 dos_cylinders, 0, 0, MAXCYL); 2390 dos_cylinders, 0, 0, MAXCYL);
2391 dos_heads = decimal("BIOS's idea of #heads", 2391 dos_heads = decimal("BIOS's idea of #heads",
2392 dos_heads, 0, 0, MAXHEAD); 2392 dos_heads, 0, 0, MAXHEAD);
2393 dos_sectors = decimal("BIOS's idea of #sectors", 2393 dos_sectors = decimal("BIOS's idea of #sectors",
2394 dos_sectors, 0, 1, MAXSECTOR); 2394 dos_sectors, 0, 1, MAXSECTOR);
2395 print_geometry(); 2395 print_geometry();
2396 } while (!yesno("Are you happy with this choice?")); 2396 } while (!yesno("Are you happy with this choice?"));
2397} 2397}
2398 2398
2399 2399
2400/***********************************************\ 2400/***********************************************\
2401* Change real numbers into strange dos numbers * 2401* Change real numbers into strange dos numbers *
2402\***********************************************/ 2402\***********************************************/
2403void 2403static void
2404dos(int sector, unsigned char *cylinderp, unsigned char *headp, 2404dos(int sector, unsigned char *cylinderp, unsigned char *headp,
2405 unsigned char *sectorp) 2405 unsigned char *sectorp)
2406{ 2406{
2407 int cylinder, head; 2407 int cylinder, head;
2408 2408
2409 cylinder = sector / dos_cylindersectors; 2409 cylinder = sector / dos_cylindersectors;
2410 sector -= cylinder * dos_cylindersectors; 2410 sector -= cylinder * dos_cylindersectors;
2411 2411
2412 head = sector / dos_sectors; 2412 head = sector / dos_sectors;
2413 sector -= head * dos_sectors; 2413 sector -= head * dos_sectors;
2414 if (cylinder > 1023) 2414 if (cylinder > 1023)
2415 cylinder = 1023; 2415 cylinder = 1023;
2416 2416
2417 *cylinderp = DOSCYL(cylinder); 2417 *cylinderp = DOSCYL(cylinder);
2418 *headp = head; 2418 *headp = head;
2419 *sectorp = DOSSECT(sector + 1, cylinder); 2419 *sectorp = DOSSECT(sector + 1, cylinder);
2420} 2420}
2421 2421
2422int 2422static int
2423open_disk(int update) 2423open_disk(int update)
2424{ 2424{
2425 static char namebuf[MAXPATHLEN + 1]; 2425 static char namebuf[MAXPATHLEN + 1];
2426 int flags = update && disk_file == NULL ? O_RDWR : O_RDONLY; 2426 int flags = update && disk_file == NULL ? O_RDWR : O_RDONLY;
2427 2427
2428 if (!F_flag) { 2428 if (!F_flag) {
2429 fd = opendisk(disk, flags, namebuf, sizeof(namebuf), 0); 2429 fd = opendisk(disk, flags, namebuf, sizeof(namebuf), 0);
2430 if (fd < 0) { 2430 if (fd < 0) {
2431 if (errno == ENODEV) 2431 if (errno == ENODEV)
2432 warnx("%s is not a character device", namebuf); 2432 warnx("%s is not a character device", namebuf);
2433 else 2433 else
2434 warn("cannot opendisk %s", namebuf); 2434 warn("cannot opendisk %s", namebuf);
2435 return (-1); 2435 return (-1);
@@ -2452,61 +2452,61 @@ open_disk(int update) @@ -2452,61 +2452,61 @@ open_disk(int update)
2452 /* for testing: read/write data from a disk file */ 2452 /* for testing: read/write data from a disk file */
2453 wfd = open(disk_file, update ? O_RDWR|O_CREAT : O_RDONLY, 0777); 2453 wfd = open(disk_file, update ? O_RDWR|O_CREAT : O_RDONLY, 0777);
2454 if (wfd == -1) { 2454 if (wfd == -1) {
2455 warn("%s", disk_file); 2455 warn("%s", disk_file);
2456 close(fd); 2456 close(fd);
2457 fd = -1; 2457 fd = -1;
2458 return -1; 2458 return -1;
2459 } 2459 }
2460 } else 2460 } else
2461 wfd = fd; 2461 wfd = fd;
2462 return (0); 2462 return (0);
2463} 2463}
2464 2464
2465int 2465static int
2466read_disk(daddr_t sector, void *buf) 2466read_disk(daddr_t sector, void *buf)
2467{ 2467{
2468 2468
2469 if (*rfd == -1) 2469 if (*rfd == -1)
2470 errx(1, "read_disk(); fd == -1"); 2470 errx(1, "read_disk(); fd == -1");
2471 if (lseek(*rfd, sector * (off_t)512, 0) == -1) 2471 if (lseek(*rfd, sector * (off_t)512, 0) == -1)
2472 return (-1); 2472 return (-1);
2473 return (read(*rfd, buf, 512)); 2473 return (read(*rfd, buf, 512));
2474} 2474}
2475 2475
2476int 2476static int
2477write_disk(daddr_t sector, void *buf) 2477write_disk(daddr_t sector, void *buf)
2478{ 2478{
2479 2479
2480 if (wfd == -1) 2480 if (wfd == -1)
2481 errx(1, "write_disk(); wfd == -1"); 2481 errx(1, "write_disk(); wfd == -1");
2482 if (lseek(wfd, sector * (off_t)512, 0) == -1) 2482 if (lseek(wfd, sector * (off_t)512, 0) == -1)
2483 return (-1); 2483 return (-1);
2484 return (write(wfd, buf, 512)); 2484 return (write(wfd, buf, 512));
2485} 2485}
2486 2486
2487static void 2487static void
2488guess_geometry(daddr_t _sectors) 2488guess_geometry(daddr_t _sectors)
2489{ 2489{
2490 dos_sectors = MAXSECTOR; 2490 dos_sectors = MAXSECTOR;
2491 dos_heads = MAXHEAD - 1; /* some BIOS might use 256 */ 2491 dos_heads = MAXHEAD - 1; /* some BIOS might use 256 */
2492 dos_cylinders = _sectors / (MAXSECTOR * (MAXHEAD - 1)); 2492 dos_cylinders = _sectors / (MAXSECTOR * (MAXHEAD - 1));
2493 if (dos_cylinders < 1) 2493 if (dos_cylinders < 1)
2494 dos_cylinders = 1; 2494 dos_cylinders = 1;
2495 else if (dos_cylinders > MAXCYL - 1) 2495 else if (dos_cylinders > MAXCYL - 1)
2496 dos_cylinders = MAXCYL - 1; 2496 dos_cylinders = MAXCYL - 1;
2497} 2497}
2498 2498
2499int 2499static int
2500get_params(void) 2500get_params(void)
2501{ 2501{
2502 if (disk_type != NULL) { 2502 if (disk_type != NULL) {
2503 struct disklabel *tmplabel; 2503 struct disklabel *tmplabel;
2504 2504
2505 if ((tmplabel = getdiskbyname(disk_type)) == NULL) { 2505 if ((tmplabel = getdiskbyname(disk_type)) == NULL) {
2506 warn("bad disktype"); 2506 warn("bad disktype");
2507 return (-1); 2507 return (-1);
2508 } 2508 }
2509 disklabel = *tmplabel; 2509 disklabel = *tmplabel;
2510 } else if (F_flag) { 2510 } else if (F_flag) {
2511 struct stat st; 2511 struct stat st;
2512 if (fstat(fd, &st) == -1) { 2512 if (fstat(fd, &st) == -1) {
@@ -2596,27 +2596,27 @@ validate_bootsel(struct mbr_bootsel *mbs @@ -2596,27 +2596,27 @@ validate_bootsel(struct mbr_bootsel *mbs
2596 /* Check the menu strings are printable */ 2596 /* Check the menu strings are printable */
2597 /* Unfortunately they aren't zero filled... */ 2597 /* Unfortunately they aren't zero filled... */
2598 for (i = 0; i < sizeof(mbs->mbrbs_nametab); i++) { 2598 for (i = 0; i < sizeof(mbs->mbrbs_nametab); i++) {
2599 int c = (uint8_t)mbs->mbrbs_nametab[0][i]; 2599 int c = (uint8_t)mbs->mbrbs_nametab[0][i];
2600 if (c == 0 || isprint(c)) 2600 if (c == 0 || isprint(c))
2601 continue; 2601 continue;
2602 return 3; 2602 return 3;
2603 } 2603 }
2604 2604
2605 return 0; 2605 return 0;
2606} 2606}
2607#endif 2607#endif
2608 2608
2609int 2609static int
2610read_s0(daddr_t offset, struct mbr_sector *boot) 2610read_s0(daddr_t offset, struct mbr_sector *boot)
2611{ 2611{
2612 const char *tabletype = offset ? "extended" : "primary"; 2612 const char *tabletype = offset ? "extended" : "primary";
2613#ifdef BOOTSEL 2613#ifdef BOOTSEL
2614 static int reported; 2614 static int reported;
2615#endif 2615#endif
2616 2616
2617 if (read_disk(offset, boot) == -1) { 2617 if (read_disk(offset, boot) == -1) {
2618 warn("Can't read %s partition table", tabletype); 2618 warn("Can't read %s partition table", tabletype);
2619 return -1; 2619 return -1;
2620 } 2620 }
2621 if (boot->mbr_magic != LE_MBR_MAGIC) { 2621 if (boot->mbr_magic != LE_MBR_MAGIC) {
2622 warnx("%s partition table invalid, " 2622 warnx("%s partition table invalid, "
@@ -2659,27 +2659,27 @@ read_s0(daddr_t offset, struct mbr_secto @@ -2659,27 +2659,27 @@ read_s0(daddr_t offset, struct mbr_secto
2659 else if (mboot.mbr_parts[id].mbrp_type != 0) 2659 else if (mboot.mbr_parts[id].mbrp_type != 0)
2660 id = le32toh(boot->mbr_parts[id].mbrp_start); 2660 id = le32toh(boot->mbr_parts[id].mbrp_start);
2661 else 2661 else
2662 id = DEFAULT_ACTIVE; 2662 id = DEFAULT_ACTIVE;
2663 boot->mbr_bootsel.mbrbs_defkey = id; 2663 boot->mbr_bootsel.mbrbs_defkey = id;
2664 } 2664 }
2665 boot->mbr_bootsel_magic = LE_MBR_BS_MAGIC; 2665 boot->mbr_bootsel_magic = LE_MBR_BS_MAGIC;
2666 /* highlight that new bootsel code is necessary */ 2666 /* highlight that new bootsel code is necessary */
2667 boot->mbr_bootsel.mbrbs_flags &= ~MBR_BS_NEWMBR; 2667 boot->mbr_bootsel.mbrbs_flags &= ~MBR_BS_NEWMBR;
2668#endif /* BOOTSEL */ 2668#endif /* BOOTSEL */
2669 return 0; 2669 return 0;
2670} 2670}
2671 2671
2672int 2672static int
2673write_mbr(void) 2673write_mbr(void)
2674{ 2674{
2675 int flag, i; 2675 int flag, i;
2676 daddr_t offset; 2676 daddr_t offset;
2677 int rval = -1; 2677 int rval = -1;
2678 2678
2679 /* 2679 /*
2680 * write enable label sector before write (if necessary), 2680 * write enable label sector before write (if necessary),
2681 * disable after writing. 2681 * disable after writing.
2682 * needed if the disklabel protected area also protects 2682 * needed if the disklabel protected area also protects
2683 * sector 0. (e.g. empty disk) 2683 * sector 0. (e.g. empty disk)
2684 */ 2684 */
2685 flag = 1; 2685 flag = 1;
@@ -2700,46 +2700,46 @@ write_mbr(void) @@ -2700,46 +2700,46 @@ write_mbr(void)
2700 warn("Can't write %dth extended partition", i); 2700 warn("Can't write %dth extended partition", i);
2701 goto protect_label; 2701 goto protect_label;
2702 } 2702 }
2703 offset = le32toh(ext.ptn[i].mbr_parts[1].mbrp_start); 2703 offset = le32toh(ext.ptn[i].mbr_parts[1].mbrp_start);
2704 } 2704 }
2705 rval = 0; 2705 rval = 0;
2706 protect_label: 2706 protect_label:
2707 flag = 0; 2707 flag = 0;
2708 if (wfd == fd && F_flag == 0 && ioctl(wfd, DIOCWLABEL, &flag) < 0) 2708 if (wfd == fd && F_flag == 0 && ioctl(wfd, DIOCWLABEL, &flag) < 0)
2709 warn("DIOCWLABEL"); 2709 warn("DIOCWLABEL");
2710 return rval; 2710 return rval;
2711} 2711}
2712 2712
2713int 2713static int
2714yesno(const char *str, ...) 2714yesno(const char *str, ...)
2715{ 2715{
2716 int ch, first; 2716 int ch, first;
2717 va_list ap; 2717 va_list ap;
2718 2718
2719 va_start(ap, str); 2719 va_start(ap, str);
2720 2720
2721 vprintf(str, ap); 2721 vprintf(str, ap);
2722 printf(" [n] "); 2722 printf(" [n] ");
2723 2723
2724 first = ch = getchar(); 2724 first = ch = getchar();
2725 while (ch != '\n' && ch != EOF) 2725 while (ch != '\n' && ch != EOF)
2726 ch = getchar(); 2726 ch = getchar();
2727 if (ch == EOF) 2727 if (ch == EOF)
2728 errx(1, "EOF"); 2728 errx(1, "EOF");
2729 return (first == 'y' || first == 'Y'); 2729 return (first == 'y' || first == 'Y');
2730} 2730}
2731 2731
2732int64_t 2732static int64_t
2733decimal(const char *prompt, int64_t dflt, int flags, int64_t minval, int64_t maxval) 2733decimal(const char *prompt, int64_t dflt, int flags, int64_t minval, int64_t maxval)
2734{ 2734{
2735 int64_t acc = 0; 2735 int64_t acc = 0;
2736 int valid; 2736 int valid;
2737 int len; 2737 int len;
2738 char *cp; 2738 char *cp;
2739 2739
2740 for (;;) { 2740 for (;;) {
2741 if (flags & DEC_SEC) { 2741 if (flags & DEC_SEC) {
2742 printf("%s: [%" PRId64 "..%" PRId64 "cyl default: %" PRId64 ", %" PRId64 "cyl, %uMB] ", 2742 printf("%s: [%" PRId64 "..%" PRId64 "cyl default: %" PRId64 ", %" PRId64 "cyl, %uMB] ",
2743 prompt, SEC_TO_CYL(minval), SEC_TO_CYL(maxval), 2743 prompt, SEC_TO_CYL(minval), SEC_TO_CYL(maxval),
2744 dflt, SEC_TO_CYL(dflt), SEC_TO_MB(dflt)); 2744 dflt, SEC_TO_CYL(dflt), SEC_TO_MB(dflt));
2745 } else 2745 } else
@@ -2793,27 +2793,27 @@ decimal(const char *prompt, int64_t dflt @@ -2793,27 +2793,27 @@ decimal(const char *prompt, int64_t dflt
2793 if (*cp != '\n') { 2793 if (*cp != '\n') {
2794 lbuf[strlen(lbuf) - 1] = 0; 2794 lbuf[strlen(lbuf) - 1] = 0;
2795 printf("%s is not a valid %s number.\n", lbuf, 2795 printf("%s is not a valid %s number.\n", lbuf,
2796 flags & DEC_SEC ? "sector" : "decimal"); 2796 flags & DEC_SEC ? "sector" : "decimal");
2797 continue; 2797 continue;
2798 } 2798 }
2799 2799
2800 if (acc >= minval && acc <= maxval) 2800 if (acc >= minval && acc <= maxval)
2801 return acc; 2801 return acc;
2802 printf("%" PRId64 " is not between %" PRId64 " and %" PRId64 ".\n", acc, minval, maxval); 2802 printf("%" PRId64 " is not between %" PRId64 " and %" PRId64 ".\n", acc, minval, maxval);
2803 } 2803 }
2804} 2804}
2805 2805
2806int 2806static int
2807ptn_id(const char *prompt, int *extended) 2807ptn_id(const char *prompt, int *extended)
2808{ 2808{
2809 unsigned int acc = 0; 2809 unsigned int acc = 0;
2810 char *cp; 2810 char *cp;
2811 2811
2812 for (;; printf("%s is not a valid partition number.\n", lbuf)) { 2812 for (;; printf("%s is not a valid partition number.\n", lbuf)) {
2813 printf("%s: [none] ", prompt); 2813 printf("%s: [none] ", prompt);
2814 2814
2815 if (!fgets(lbuf, LBUF, stdin)) 2815 if (!fgets(lbuf, LBUF, stdin))
2816 errx(1, "EOF"); 2816 errx(1, "EOF");
2817 lbuf[strlen(lbuf)-1] = '\0'; 2817 lbuf[strlen(lbuf)-1] = '\0';
2818 cp = lbuf; 2818 cp = lbuf;
2819 2819
@@ -2829,79 +2829,79 @@ ptn_id(const char *prompt, int *extended @@ -2829,79 +2829,79 @@ ptn_id(const char *prompt, int *extended
2829 2829
2830 acc = strtoul(cp, &cp, 10); 2830 acc = strtoul(cp, &cp, 10);
2831 2831
2832 cp += strspn(cp, " \t"); 2832 cp += strspn(cp, " \t");
2833 if (*cp != '\0') 2833 if (*cp != '\0')
2834 continue; 2834 continue;
2835 2835
2836 if (*extended || acc < MBR_PART_COUNT) 2836 if (*extended || acc < MBR_PART_COUNT)
2837 return acc; 2837 return acc;
2838 } 2838 }
2839} 2839}
2840 2840
2841#ifdef BOOTSEL 2841#ifdef BOOTSEL
2842void 2842static void
2843string(const char *prompt, int length, char *buf) 2843string(const char *prompt, int length, char *buf)
2844{ 2844{
2845 int len; 2845 int len;
2846 2846
2847 for (;;) { 2847 for (;;) {
2848 printf("%s: [%.*s] ", prompt, length, buf); 2848 printf("%s: [%.*s] ", prompt, length, buf);
2849 2849
2850 if (!fgets(lbuf, LBUF, stdin)) 2850 if (!fgets(lbuf, LBUF, stdin))
2851 errx(1, "EOF"); 2851 errx(1, "EOF");
2852 len = strlen(lbuf); 2852 len = strlen(lbuf);
2853 if (len <= 1) 2853 if (len <= 1)
2854 /* unchanged if just <enter> */ 2854 /* unchanged if just <enter> */
2855 return; 2855 return;
2856 /* now strip trailing spaces, <space><enter> deletes string */ 2856 /* now strip trailing spaces, <space><enter> deletes string */
2857 do 2857 do
2858 lbuf[--len] = 0; 2858 lbuf[--len] = 0;
2859 while (len != 0 && lbuf[len - 1] == ' '); 2859 while (len != 0 && lbuf[len - 1] == ' ');
2860 if (len < length) 2860 if (len < length)
2861 break; 2861 break;
2862 printf("'%s' is longer than %d characters.\n", 2862 printf("'%s' is longer than %d characters.\n",
2863 lbuf, length - 1); 2863 lbuf, length - 1);
2864 } 2864 }
2865 strncpy(buf, lbuf, length); 2865 strncpy(buf, lbuf, length);
2866} 2866}
2867#endif 2867#endif
2868 2868
2869int 2869static int
2870type_match(const void *key, const void *item) 2870type_match(const void *key, const void *item)
2871{ 2871{
2872 const int *idp = key; 2872 const int *idp = key;
2873 const struct mbr_ptype *ptr = item; 2873 const struct mbr_ptype *ptr = item;
2874 2874
2875 if (*idp < ptr->id) 2875 if (*idp < ptr->id)
2876 return (-1); 2876 return (-1);
2877 if (*idp > ptr->id) 2877 if (*idp > ptr->id)
2878 return (1); 2878 return (1);
2879 return (0); 2879 return (0);
2880} 2880}
2881 2881
2882const char * 2882static const char *
2883get_type(int type) 2883get_type(int type)
2884{ 2884{
2885 struct mbr_ptype *ptr; 2885 struct mbr_ptype *ptr;
2886 2886
2887 ptr = bsearch(&type, mbr_ptypes, KNOWN_SYSIDS, 2887 ptr = bsearch(&type, mbr_ptypes, KNOWN_SYSIDS,
2888 sizeof(mbr_ptypes[0]), type_match); 2888 sizeof(mbr_ptypes[0]), type_match);
2889 if (ptr == 0) 2889 if (ptr == 0)
2890 return ("unknown"); 2890 return ("unknown");
2891 return (ptr->name); 2891 return (ptr->name);
2892} 2892}
2893 2893
2894int 2894static int
2895read_gpt(daddr_t offset, struct gpt_hdr *gptp) 2895read_gpt(daddr_t offset, struct gpt_hdr *gptp)
2896{ 2896{
2897 char buf[512]; 2897 char buf[512];
2898 struct gpt_hdr *hdr = (void *)buf; 2898 struct gpt_hdr *hdr = (void *)buf;
2899 const char *tabletype = GPT_TYPE(offset); 2899 const char *tabletype = GPT_TYPE(offset);
2900 2900
2901 if (read_disk(offset, buf) == -1) { 2901 if (read_disk(offset, buf) == -1) {
2902 warn("Can't read %s GPT header", tabletype); 2902 warn("Can't read %s GPT header", tabletype);
2903 return -1; 2903 return -1;
2904 } 2904 }
2905 (void)memcpy(gptp, buf, GPT_HDR_SIZE); 2905 (void)memcpy(gptp, buf, GPT_HDR_SIZE);
2906 2906
2907 /* GPT CRC should be calculated with CRC field preset to zero */ 2907 /* GPT CRC should be calculated with CRC field preset to zero */
@@ -2913,27 +2913,27 @@ read_gpt(daddr_t offset, struct gpt_hdr  @@ -2913,27 +2913,27 @@ read_gpt(daddr_t offset, struct gpt_hdr
2913 /* not a GPT */ 2913 /* not a GPT */
2914 (void)memset(gptp, 0, GPT_HDR_SIZE); 2914 (void)memset(gptp, 0, GPT_HDR_SIZE);
2915 } 2915 }
2916 2916
2917 if (v_flag && gptp->hdr_size != 0) { 2917 if (v_flag && gptp->hdr_size != 0) {
2918 printf("Found %s GPT header CRC %"PRIu32" " 2918 printf("Found %s GPT header CRC %"PRIu32" "
2919 "at sector %"PRIdaddr", backup at %"PRIdaddr"\n", 2919 "at sector %"PRIdaddr", backup at %"PRIdaddr"\n",
2920 tabletype, gptp->hdr_crc_self, offset, gptp->hdr_lba_alt); 2920 tabletype, gptp->hdr_crc_self, offset, gptp->hdr_lba_alt);
2921 } 2921 }
2922 return gptp->hdr_size; 2922 return gptp->hdr_size;
2923 2923
2924} 2924}
2925 2925
2926int 2926static int
2927delete_gpt(struct gpt_hdr *gptp) 2927delete_gpt(struct gpt_hdr *gptp)
2928{ 2928{
2929 char buf[512]; 2929 char buf[512];
2930 struct gpt_hdr *hdr = (void *)buf; 2930 struct gpt_hdr *hdr = (void *)buf;
2931 2931
2932 if (gptp->hdr_size == 0) 2932 if (gptp->hdr_size == 0)
2933 return 0; 2933 return 0;
2934 2934
2935 /* don't accidently overwrite something important */ 2935 /* don't accidently overwrite something important */
2936 if (gptp->hdr_lba_self != GPT_HDR_BLKNO && 2936 if (gptp->hdr_lba_self != GPT_HDR_BLKNO &&
2937 gptp->hdr_lba_self != (uint64_t)disksectors - 1) { 2937 gptp->hdr_lba_self != (uint64_t)disksectors - 1) {
2938 warnx("given GPT header location doesn't seem correct"); 2938 warnx("given GPT header location doesn't seem correct");
2939 return -1; 2939 return -1;