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