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