label.c revision 1.1 1 /* $NetBSD: label.c,v 1.1 2014/07/26 19:30:44 dholland Exp $ */
2
3 /*
4 * Copyright 1997 Jonathan Stone
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project by
18 * Jonathan Stone.
19 * 4. The name of Jonathan Stone may not be used to endorse
20 * or promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY JONATHAN STONE ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
39 __RCSID("$NetBSD: label.c,v 1.1 2014/07/26 19:30:44 dholland Exp $");
40 #endif
41
42 #include <sys/types.h>
43 #include <stddef.h>
44 #include <errno.h>
45 #include <stdio.h>
46 #include <fcntl.h>
47 #include <util.h>
48 #include <unistd.h>
49 #include <sys/dkio.h>
50 #include <sys/param.h>
51 #include <ufs/ffs/fs.h>
52
53 #include "defs.h"
54 #include "msg_defs.h"
55 #include "menu_defs.h"
56
57 struct ptn_menu_info {
58 int flags;
59 #define PIF_SHOW_UNUSED 1
60 };
61
62 /*
63 * local prototypes
64 */
65 static int boringpart(partinfo *, int, int, int);
66 static uint32_t getpartoff(uint32_t);
67 static uint32_t getpartsize(uint32_t, uint32_t);
68
69 static int checklabel(partinfo *, int, int, int, int *, int *);
70 static int atofsb(const char *, uint32_t *, uint32_t *);
71
72
73 /*
74 * Return 1 if partition i in lp should be ignored when checking
75 * for overlapping partitions.
76 */
77 static int
78 boringpart(partinfo *lp, int i, int rawpart, int bsdpart)
79 {
80
81 if (i == rawpart || i == bsdpart ||
82 lp[i].pi_fstype == FS_UNUSED || lp[i].pi_size == 0)
83 return 1;
84 return 0;
85 }
86
87
88
89 /*
90 * Check a sysinst label structure for overlapping partitions.
91 * Returns 0 if no overlapping partition found, nonzero otherwise.
92 * Sets reference arguments ovly1 and ovly2 to the indices of
93 * overlapping partitions if any are found.
94 */
95 static int
96 checklabel(partinfo *lp, int nparts, int rawpart, int bsdpart,
97 int *ovly1, int *ovly2)
98 {
99 int i;
100 int j;
101
102 *ovly1 = -1;
103 *ovly2 = -1;
104
105 for (i = 0; i < nparts - 1; i ++ ) {
106 partinfo *ip = &lp[i];
107 uint32_t istart, istop;
108
109 /* skip unused or reserved partitions */
110 if (boringpart(lp, i, rawpart, bsdpart))
111 continue;
112
113 /*
114 * check succeeding partitions for overlap.
115 * O(n^2), but n is small (currently <= 16).
116 */
117 istart = ip->pi_offset;
118 istop = istart + ip->pi_size;
119
120 for (j = i+1; j < nparts; j++) {
121 partinfo *jp = &lp[j];
122 uint32_t jstart, jstop;
123
124 /* skip unused or reserved partitions */
125 if (boringpart(lp, j, rawpart, bsdpart))
126 continue;
127
128 jstart = jp->pi_offset;
129 jstop = jstart + jp->pi_size;
130
131 /* overlap? */
132 if ((istart <= jstart && jstart < istop) ||
133 (jstart <= istart && istart < jstop)) {
134 *ovly1 = i;
135 *ovly2 = j;
136 return (1);
137 }
138 }
139 }
140
141 return (0);
142 }
143
144 static int
145 check_one_root(partinfo *lp, int nparts)
146 {
147 int part;
148 int foundroot = 0;
149
150 for (part = 0; part < nparts; lp++, part++) {
151 #if 0
152 if (!PI_ISBSDFS(lp))
153 continue;
154 #endif
155 if (!(lp->pi_flags & PIF_MOUNT))
156 continue;
157 if (strcmp(lp->pi_mount, "/") != 0)
158 continue;
159 if (foundroot)
160 /* Duplicate */
161 return 0;
162 foundroot = 1;
163 /* Save partition number, a few things need to know it */
164 rootpart = part;
165 }
166 return foundroot;
167 }
168
169 static int
170 edit_fs_start(menudesc *m, void *arg)
171 {
172 partinfo *p = arg;
173 uint32_t start, end;
174
175 start = getpartoff(p->pi_offset);
176 if (p->pi_size != 0) {
177 /* Try to keep end in the same place */
178 end = p->pi_offset + p->pi_size;
179 if (end < start)
180 p->pi_size = 0;
181 else
182 p->pi_size = end - start;
183 }
184 p->pi_offset = start;
185 return 0;
186 }
187
188 static int
189 edit_fs_size(menudesc *m, void *arg)
190 {
191 partinfo *p = arg;
192 uint32_t size;
193
194 size = getpartsize(p->pi_offset, p->pi_size);
195 if (size == ~0u)
196 size = dlsize - p->pi_offset;
197 p->pi_size = size;
198 if (size == 0) {
199 p->pi_offset = 0;
200 p->pi_fstype = FS_UNUSED;
201 }
202 return 0;
203 }
204
205 void
206 set_ptype(partinfo *p, int fstype, int flag)
207 {
208 p->pi_flags = (p->pi_flags & ~PIF_FFSv2) | flag;
209
210 if (p->pi_fstype == fstype)
211 return;
212
213 p->pi_fstype = fstype;
214 if (fstype == FS_BSDFFS || fstype == FS_BSDLFS) {
215 p->pi_frag = 8;
216 /*
217 * match newfs defaults for fragments size:
218 * fs size frag size
219 * < 20 MB 0.5 KB
220 * < 1000 MB 1 KB
221 * < 128 GB 2 KB
222 * >= 128 GB 4 KB
223 */
224 /* note pi_size is uint32_t so we have to avoid overflow */
225 if (p->pi_size < (20 * 1024 * (1024 / 512)))
226 p->pi_fsize = 512;
227 else if (p->pi_size < (1000 * 1024 * (1024 / 512)))
228 p->pi_fsize = 1024;
229 else if (p->pi_size < (128 * 1024 * 1024 * (1024 / 512)))
230 p->pi_fsize = 2048;
231 else
232 p->pi_fsize = 4096;
233 } else {
234 /* zero - fields not used */
235 p->pi_frag = 0;
236 p->pi_fsize = 0;
237 }
238 }
239
240 void
241 set_bsize(partinfo *p, int size)
242 {
243 int frags, sz;
244
245 sz = p->pi_fsize;
246 if (sz <= 0)
247 sz = 512;
248 frags = size / sz;
249 if (frags > 8)
250 frags = 8;
251 if (frags <= 0)
252 frags = 1;
253
254 p->pi_frag = frags;
255 p->pi_fsize = size / frags;
256 }
257
258 void
259 set_fsize(partinfo *p, int fsize)
260 {
261 int bsz = p->pi_fsize * p->pi_frag;
262 int frags = bsz / fsize;
263
264 if (frags > 8)
265 frags = 8;
266 if (frags <= 0)
267 frags = 1;
268
269 p->pi_fsize = fsize;
270 p->pi_frag = frags;
271 }
272
273 static int
274 edit_fs_isize(menudesc *m, void *arg)
275 {
276 partinfo *p = arg;
277 char answer[12];
278
279 snprintf(answer, sizeof answer, "%u", p->pi_isize);
280 msg_prompt_win(MSG_fs_isize, -1, 18, 0, 0,
281 answer, answer, sizeof answer);
282 p->pi_isize = atol(answer);
283 return 0;
284 }
285
286
287 static int
288 edit_fs_preserve(menudesc *m, void *arg)
289 {
290 partinfo *p = arg;
291
292 p->pi_flags ^= PIF_NEWFS;
293 return 0;
294 }
295
296 static int
297 edit_fs_mount(menudesc *m, void *arg)
298 {
299 partinfo *p = arg;
300
301 p->pi_flags ^= PIF_MOUNT;
302 return 0;
303 }
304
305 static int
306 edit_fs_mountpt(menudesc *m, void *arg)
307 {
308 partinfo *p = arg;
309
310 msg_prompt_win(MSG_mountpoint, -1, 18, 0, 0,
311 p->pi_mount, p->pi_mount, sizeof p->pi_mount);
312
313 if (p->pi_mount[0] == ' ' || strcmp(p->pi_mount, "none") == 0) {
314 p->pi_mount[0] = 0;
315 return 0;
316 }
317
318 if (p->pi_mount[0] != '/') {
319 memmove(p->pi_mount + 1, p->pi_mount,
320 sizeof p->pi_mount - 1);
321 p->pi_mount[sizeof p->pi_mount - 1] = 0;
322 p->pi_mount[0] = '/';
323 }
324
325 return 0;
326 }
327
328 static int
329 edit_restore(menudesc *m, void *arg)
330 {
331 partinfo *p = arg;
332
333 p->pi_flags |= PIF_RESET;
334 return 1;
335 }
336
337 static int
338 set_fstype(menudesc *m, void *arg)
339 {
340 partinfo *p = arg;
341
342 if (m->cursel == FS_UNUSED)
343 memset(p, 0, sizeof *p);
344 else
345 p->pi_fstype = m->cursel;
346 return 1;
347 }
348
349 static void
350 get_fstype(menudesc *m, void *arg)
351 {
352 partinfo *p = arg;
353
354 m->cursel = p->pi_fstype;
355 }
356
357 static void set_ptn_header(menudesc *m, void *arg);
358 static void set_ptn_label(menudesc *m, int opt, void *arg);
359 int all_fstype_menu = -1;
360
361 static int
362 edit_ptn(menudesc *menu, void *arg)
363 {
364 static menu_ent fs_fields[] = {
365 #define PTN_MENU_FSKIND 0
366 {NULL, MENU_selfskind, OPT_SUB, NULL},
367 #define PTN_MENU_START 1
368 {NULL, OPT_NOMENU, 0, edit_fs_start},
369 #define PTN_MENU_SIZE 2
370 {NULL, OPT_NOMENU, 0, edit_fs_size},
371 #define PTN_MENU_END 3
372 {NULL, OPT_NOMENU, OPT_IGNORE, NULL}, /* displays 'end' */
373 #define PTN_MENU_NEWFS 4
374 {NULL, OPT_NOMENU, 0, edit_fs_preserve},
375 #define PTN_MENU_ISIZE 5
376 {NULL, OPT_NOMENU, 0, edit_fs_isize},
377 #define PTN_MENU_BSIZE 6
378 {NULL, MENU_selbsize, OPT_SUB, NULL},
379 #define PTN_MENU_FSIZE 7
380 {NULL, MENU_selfsize, OPT_SUB, NULL},
381 #define PTN_MENU_MOUNT 8
382 {NULL, OPT_NOMENU, 0, edit_fs_mount},
383 #define PTN_MENU_MOUNTOPT 9
384 {NULL, MENU_mountoptions, OPT_SUB, NULL},
385 #define PTN_MENU_MOUNTPT 10
386 {NULL, OPT_NOMENU, 0, edit_fs_mountpt},
387 {MSG_askunits, MENU_sizechoice, OPT_SUB, NULL},
388 {MSG_restore, OPT_NOMENU, 0, edit_restore},
389 };
390 static int fspart_menu = -1;
391 static menu_ent all_fstypes[FSMAXTYPES];
392 partinfo *p, p_save;
393 unsigned int i;
394
395 if (fspart_menu == -1) {
396 fspart_menu = new_menu(NULL, fs_fields, nelem(fs_fields),
397 0, -1, 0, 70,
398 MC_NOBOX | MC_NOCLEAR | MC_SCROLL,
399 set_ptn_header, set_ptn_label, NULL,
400 NULL, MSG_partition_sizes_ok);
401 }
402
403 if (all_fstype_menu == -1) {
404 for (i = 0; i < nelem(all_fstypes); i++) {
405 all_fstypes[i].opt_name = getfslabelname(i);
406 all_fstypes[i].opt_menu = OPT_NOMENU;
407 all_fstypes[i].opt_flags = 0;
408 all_fstypes[i].opt_action = set_fstype;
409 }
410 all_fstype_menu = new_menu(MSG_Select_the_type,
411 all_fstypes, nelem(all_fstypes),
412 30, 6, 10, 0, MC_SUBMENU | MC_SCROLL,
413 get_fstype, NULL, NULL, NULL, MSG_unchanged);
414 }
415
416 p = bsdlabel + menu->cursel;
417 p->pi_flags &= ~PIF_RESET;
418 p_save = *p;
419 for (;;) {
420 process_menu(fspart_menu, p);
421 if (!(p->pi_flags & PIF_RESET))
422 break;
423 *p = p_save;
424 }
425
426 return 0;
427 }
428
429 static void
430 set_ptn_header(menudesc *m, void *arg)
431 {
432 partinfo *p = arg;
433 int i;
434 int t;
435
436 msg_clear();
437 msg_table_add(MSG_edfspart, 'a' + (p - bsdlabel));
438
439 /* Determine which of the properties can be changed */
440 for (i = PTN_MENU_START; i <= PTN_MENU_MOUNTPT; i++) {
441 /* Default to disabled... */
442 m->opts[i].opt_flags |= OPT_IGNORE;
443 t = p->pi_fstype;
444 if (i == PTN_MENU_END)
445 /* The 'end address' is calculated from the size */
446 continue;
447 if (t == FS_UNUSED || (t == FS_SWAP && i > PTN_MENU_END)) {
448 /* Nothing after 'size' can be set for swap/unused */
449 p->pi_flags &= ~(PIF_NEWFS | PIF_MOUNT);
450 p->pi_mount[0] = 0;
451 continue;
452 }
453 if (i == PTN_MENU_NEWFS && t != FS_BSDFFS && t != FS_BSDLFS
454 && t != FS_APPLEUFS) {
455 /* Can only newfs UFS and LFS filesystems */
456 p->pi_flags &= ~PIF_NEWFS;
457 continue;
458 }
459 if (i >= PTN_MENU_ISIZE && i <= PTN_MENU_FSIZE) {
460 /* Parameters for newfs... */
461 if (!(p->pi_flags & PIF_NEWFS))
462 /* Not if we aren't going to run newfs */
463 continue;
464 if (t == FS_APPLEUFS && i != PTN_MENU_ISIZE)
465 /* Can only set # inodes for appleufs */
466 continue;
467 if (t == FS_BSDLFS && i != PTN_MENU_BSIZE)
468 /* LFS doesn't have fragments */
469 continue;
470 }
471 /* Ok: we want this one */
472 m->opts[i].opt_flags &= ~OPT_IGNORE;
473 }
474 }
475
476 static void
477 disp_sector_count(menudesc *m, msg fmt, uint s)
478 {
479 uint ms = MEG / sectorsize;
480
481 wprintw(m->mw, msg_string(fmt),
482 s / ms, s / dlcylsize, s % dlcylsize ? '*' : ' ', s );
483 }
484
485 static void
486 set_ptn_label(menudesc *m, int opt, void *arg)
487 {
488 partinfo *p = arg;
489 const char *c;
490
491 if (m->opts[opt].opt_flags & OPT_IGNORE
492 && (opt != PTN_MENU_END || p->pi_fstype == FS_UNUSED)) {
493 wprintw(m->mw, " -");
494 return;
495 }
496
497 switch (opt) {
498 case PTN_MENU_FSKIND:
499 if (p->pi_fstype == FS_BSDFFS)
500 if (p->pi_flags & PIF_FFSv2)
501 c = "FFSv2";
502 else
503 c = "FFSv1";
504 else
505 c = getfslabelname(p->pi_fstype);
506 wprintw(m->mw, msg_string(MSG_fstype_fmt), c);
507 break;
508 case PTN_MENU_START:
509 disp_sector_count(m, MSG_start_fmt, p->pi_offset);
510 break;
511 case PTN_MENU_SIZE:
512 disp_sector_count(m, MSG_size_fmt, p->pi_size);
513 break;
514 case PTN_MENU_END:
515 disp_sector_count(m, MSG_end_fmt, p->pi_offset + p->pi_size);
516 break;
517 case PTN_MENU_NEWFS:
518 wprintw(m->mw, msg_string(MSG_newfs_fmt),
519 msg_string(p->pi_flags & PIF_NEWFS ? MSG_Yes : MSG_No));
520 break;
521 case PTN_MENU_ISIZE:
522 wprintw(m->mw, msg_string(p->pi_isize > 0 ?
523 MSG_isize_fmt : MSG_isize_fmt_dflt), p->pi_isize);
524 break;
525 case PTN_MENU_BSIZE:
526 wprintw(m->mw, msg_string(MSG_bsize_fmt),
527 p->pi_fsize * p->pi_frag);
528 break;
529 case PTN_MENU_FSIZE:
530 wprintw(m->mw, msg_string(MSG_fsize_fmt), p->pi_fsize);
531 break;
532 case PTN_MENU_MOUNT:
533 wprintw(m->mw, msg_string(MSG_mount_fmt),
534 msg_string(p->pi_flags & PIF_MOUNT ? MSG_Yes : MSG_No));
535 break;
536 case PTN_MENU_MOUNTOPT:
537 wprintw(m->mw, "%s", msg_string(MSG_mount_options_fmt));
538 if (p->pi_flags & PIF_ASYNC)
539 wprintw(m->mw, "async ");
540 if (p->pi_flags & PIF_NOATIME)
541 wprintw(m->mw, "noatime ");
542 if (p->pi_flags & PIF_NODEV)
543 wprintw(m->mw, "nodev ");
544 if (p->pi_flags & PIF_NODEVMTIME)
545 wprintw(m->mw, "nodevmtime ");
546 if (p->pi_flags & PIF_NOEXEC)
547 wprintw(m->mw, "noexec ");
548 if (p->pi_flags & PIF_NOSUID)
549 wprintw(m->mw, "nosuid ");
550 if (p->pi_flags & PIF_LOG)
551 wprintw(m->mw, "log ");
552 break;
553 case PTN_MENU_MOUNTPT:
554 wprintw(m->mw, msg_string(MSG_mountpt_fmt), p->pi_mount);
555 break;
556 }
557 }
558
559 static int
560 show_all_unused(menudesc *m, void *arg)
561 {
562 struct ptn_menu_info *pi = arg;
563
564 pi->flags |= PIF_SHOW_UNUSED;
565 return 0;
566 }
567
568 static void
569 set_label_texts(menudesc *menu, void *arg)
570 {
571 struct ptn_menu_info *pi = arg;
572 menu_ent *m;
573 int ptn, show_unused_ptn;
574 int rawptn = getrawpartition();
575 int maxpart = getmaxpartitions();
576
577 msg_display(MSG_fspart);
578 msg_table_add(MSG_fspart_header, multname, multname, multname);
579
580 for (show_unused_ptn = 0, ptn = 0; ptn < maxpart; ptn++) {
581 m = &menu->opts[ptn];
582 m->opt_menu = OPT_NOMENU;
583 m->opt_name = NULL;
584 m->opt_action = edit_ptn;
585 if (ptn == rawptn
586 #ifdef PART_BOOT
587 || ptn == PART_BOOT
588 #endif
589 || ptn == PART_C) {
590 m->opt_flags = OPT_IGNORE;
591 } else {
592 m->opt_flags = 0;
593 if (bsdlabel[ptn].pi_fstype == FS_UNUSED)
594 continue;
595 }
596 show_unused_ptn = ptn + 2;
597 }
598
599 if (!(pi->flags & PIF_SHOW_UNUSED) && ptn > show_unused_ptn) {
600 ptn = show_unused_ptn;
601 m = &menu->opts[ptn];
602 m->opt_name = MSG_show_all_unused_partitions;
603 m->opt_action = show_all_unused;
604 ptn++;
605 }
606
607 m = &menu->opts[ptn];
608 m->opt_menu = MENU_sizechoice;
609 m->opt_flags = OPT_SUB;
610 m->opt_action = NULL;
611 m->opt_name = MSG_askunits;
612
613 menu->numopts = ptn + 1;
614 }
615
616 /*
617 * Check a disklabel.
618 * If there are overlapping active partitions,
619 * Ask the user if they want to edit the partition or give up.
620 */
621 int
622 edit_and_check_label(partinfo *lp, int nparts, int rawpart, int bsdpart)
623 {
624 static struct menu_ent *menu;
625 static int menu_no = -1;
626 static struct ptn_menu_info pi;
627 int maxpart = getmaxpartitions();
628
629 if (menu == NULL) {
630 menu = malloc((maxpart + 1) * sizeof *menu);
631 if (!menu)
632 return 1;
633 }
634
635 if (menu_no == -1) {
636 menu_no = new_menu(NULL, menu, maxpart + 1,
637 0, -1, maxpart + 2, 74,
638 MC_ALWAYS_SCROLL | MC_NOBOX | MC_DFLTEXIT,
639 set_label_texts, fmt_fspart, NULL, NULL,
640 MSG_partition_sizes_ok);
641 }
642
643 if (menu_no < 0)
644 return 1;
645
646 pi.flags = 0;
647 current_cylsize = dlcylsize;
648
649 for (;;) {
650 int i, j;
651
652 /* first give the user the option to edit the label... */
653 process_menu(menu_no, &pi);
654
655 /* User thinks the label is OK. */
656 /* check we have a single root fs */
657 if (check_one_root(lp, nparts) == 0)
658 msg_display(MSG_must_be_one_root);
659 else
660 /* Check for overlaps */
661 if (checklabel(lp, nparts, rawpart, bsdpart, &i, &j))
662 /* partitions overlap */
663 msg_display(MSG_partitions_overlap,'a'+i,'a'+j);
664 else
665 return 1;
666
667 /*XXX ???*/
668 msg_display_add(MSG_edit_partitions_again);
669 process_menu(MENU_yesno, NULL);
670 if (!yesno)
671 return(0);
672 }
673
674 /*NOTREACHED*/
675 }
676
677 /*
678 * Read a label from disk into a sysinst label structure.
679 */
680 int
681 incorelabel(const char *dkname, partinfo *lp)
682 {
683 struct disklabel lab;
684 int i, maxpart;
685 struct partition *pp;
686 int fd;
687 char buf[64];
688
689 if (get_real_geom(dkname, &lab) == 0)
690 return -1;
691
692 touchwin(stdscr);
693 maxpart = getmaxpartitions();
694 if (maxpart > MAXPARTITIONS)
695 maxpart = MAXPARTITIONS;
696 if (maxpart > lab.d_npartitions)
697 maxpart = lab.d_npartitions;
698
699 /*
700 * Historically sysinst writes the name to d_typename rather
701 * than d_diskname. Who knows why, but pull the value back here.
702 */
703 if (lab.d_typename[0] != 0 && strcmp(lab.d_typename, "unknown") != 0)
704 strlcpy(bsddiskname, lab.d_typename, sizeof bsddiskname);
705
706 fd = opendisk(dkname, O_RDONLY, buf, sizeof buf, 0);
707 pp = &lab.d_partitions[0];
708 for (i = 0; i < maxpart; lp++, pp++, i++) {
709 lp->pi_partition = *pp;
710 if (lp->pi_fstype >= FSMAXTYPES)
711 lp->pi_fstype = FS_OTHER;
712 strlcpy(lp->pi_mount, get_last_mounted(fd, pp->p_offset, lp),
713 sizeof lp->pi_mount);
714 }
715 if (fd != -1)
716 close(fd);
717
718 return 0;
719 }
720
721 /*
722 * Try to get 'last mounted on' (or equiv) from fs superblock.
723 */
724 const char *
725 get_last_mounted(int fd, int partstart, partinfo *lp)
726 {
727 static char sblk[SBLOCKSIZE]; /* is this enough? */
728 struct fs *SB = (struct fs *)sblk;
729 static const int sblocks[] = SBLOCKSEARCH;
730 const int *sbp;
731 char *cp;
732 const char *mnt = NULL;
733 int len;
734
735 if (fd == -1)
736 return "";
737
738 /* Check UFS1/2 (and hence LFS) superblock */
739 for (sbp = sblocks; mnt == NULL && *sbp != -1; sbp++) {
740 if (pread(fd, sblk, sizeof sblk,
741 partstart * (off_t)512 + *sbp) != sizeof sblk)
742 continue;
743 /* Maybe we should validate the checksum??? */
744 switch (SB->fs_magic) {
745 case FS_UFS1_MAGIC:
746 case FS_UFS1_MAGIC_SWAPPED:
747 if (!(SB->fs_old_flags & FS_FLAGS_UPDATED)) {
748 if (*sbp == SBLOCK_UFS1)
749 mnt = (const char *)SB->fs_fsmnt;
750 } else {
751 /* Check we have the main superblock */
752 if (SB->fs_sblockloc == *sbp)
753 mnt = (const char *)SB->fs_fsmnt;
754 }
755 continue;
756 case FS_UFS2_MAGIC:
757 case FS_UFS2_MAGIC_SWAPPED:
758 /* Check we have the main superblock */
759 if (SB->fs_sblockloc == *sbp) {
760 mnt = (const char *)SB->fs_fsmnt;
761 if (lp != NULL)
762 lp->pi_flags |= PIF_FFSv2;
763 }
764 continue;
765 }
766
767 #if 0 /* Requires fs/ext2fs/ext2fs.h which collides badly... */
768 if ((struct ext2fs *)sblk)->e2fs_magic == E2FS_MAGIC) {
769 mnt = ((struct ext2fs *)sblk)->e2fs_fsmnt;
770 continue;
771 }
772 #endif
773 if (*sbp != 0)
774 continue;
775
776 /* If start of partition check for other fs types */
777 if (sblk[0x42] == 0x29 && memcmp(sblk + 0x52, "FAT", 3) == 0) {
778 /* Probably a FAT filesystem, report volume name */
779 cp = strchr(sblk + 0x47, ' ');
780 if (cp == NULL || cp - (sblk + 0x47) > 11)
781 cp = sblk + 0x47 + 11;
782 *cp = 0;
783 return sblk + 0x47;
784 }
785 }
786
787 if (mnt == NULL)
788 return "";
789
790 /* If sysinst mounted this last then strip prefix */
791 len = strlen(targetroot_mnt);
792 if (memcmp(mnt, targetroot_mnt, len) == 0) {
793 if (mnt[len] == 0)
794 return "/";
795 if (mnt[len] == '/')
796 return mnt + len;
797 }
798 return mnt;
799 #undef SB
800 }
801
802 /* Ask for a partition offset, check bounds and do the needed roundups */
803 static uint32_t
804 getpartoff(uint32_t defpartstart)
805 {
806 char defsize[20], isize[20], maxpartc;
807 uint32_t i;
808 uint32_t localsizemult;
809 int partn;
810 const char *errmsg = "\n";
811
812 maxpartc = 'a' + getmaxpartitions() - 1;
813 for (;;) {
814 snprintf(defsize, sizeof defsize, "%d", defpartstart/sizemult);
815 msg_prompt_win(MSG_label_offset, -1, 13, 70, 9,
816 (defpartstart > 0) ? defsize : NULL, isize, sizeof isize,
817 errmsg, maxpartc, maxpartc, multname);
818 if (strcmp(defsize, isize) == 0)
819 /* Don't do rounding if default accepted */
820 return defpartstart;
821 if (isize[1] == '\0' && isize[0] >= 'a' &&
822 isize[0] <= maxpartc) {
823 partn = isize[0] - 'a';
824 i = bsdlabel[partn].pi_size + bsdlabel[partn].pi_offset;
825 localsizemult = 1;
826 } else if (atoi(isize) == -1) {
827 i = ptstart;
828 localsizemult = 1;
829 } else {
830 if (atofsb(isize, &i, &localsizemult)) {
831 errmsg = msg_string(MSG_invalid_sector_number);
832 continue;
833 }
834 }
835 /* round to cylinder size if localsizemult != 1 */
836 i = NUMSEC(i/localsizemult, localsizemult, dlcylsize);
837 /* Adjust to start of slice if needed */
838 if ((i < ptstart && (ptstart - i) < localsizemult) ||
839 (i > ptstart && (i - ptstart) < localsizemult)) {
840 i = ptstart;
841 }
842 if (i <= dlsize)
843 break;
844 errmsg = msg_string(MSG_startoutsidedisk);
845 }
846 return i;
847 }
848
849
850 /* Ask for a partition size, check bounds and do the needed roundups */
851 static uint32_t
852 getpartsize(uint32_t partstart, uint32_t defpartsize)
853 {
854 char dsize[20], isize[20], maxpartc;
855 const char *errmsg = "\n";
856 uint32_t i, partend, localsizemult;
857 uint32_t fsptend = ptstart + ptsize;
858 int partn;
859
860 maxpartc = 'a' + getmaxpartitions() - 1;
861 for (;;) {
862 snprintf(dsize, sizeof dsize, "%d", defpartsize/sizemult);
863 msg_prompt_win(MSG_label_size, -1, 12, 70, 9,
864 (defpartsize != 0) ? dsize : 0, isize, sizeof isize,
865 errmsg, maxpartc, multname);
866 if (strcmp(isize, dsize) == 0)
867 return defpartsize;
868 if (isize[1] == '\0' && isize[0] >= 'a' &&
869 isize[0] <= maxpartc) {
870 partn = isize[0] - 'a';
871 i = bsdlabel[partn].pi_offset - partstart;
872 localsizemult = 1;
873 } else if (atoi(isize) == -1) {
874 i = fsptend - partstart;
875 localsizemult = 1;
876 } else {
877 if (atofsb(isize, &i, &localsizemult)) {
878 errmsg = msg_string(MSG_invalid_sector_number);
879 continue;
880 }
881 }
882 /*
883 * partend is aligned to a cylinder if localsizemult
884 * is not 1 sector
885 */
886 partend = NUMSEC((partstart + i) / localsizemult,
887 localsizemult, dlcylsize);
888 /* Align to end-of-disk or end-of-slice if close enough */
889 if (partend > (dlsize - localsizemult)
890 && partend < (dlsize + localsizemult))
891 partend = dlsize;
892 if (partend > (fsptend - localsizemult)
893 && partend < (fsptend + localsizemult))
894 partend = fsptend;
895 /* sanity checks */
896 if (partend > dlsize) {
897 partend = dlsize;
898 msg_prompt_win(MSG_endoutsidedisk, -1, 13, 70, 6,
899 NULL, isize, 1,
900 (partend - partstart) / sizemult, multname);
901 }
902 if (partend < partstart)
903 return 0;
904 return (partend - partstart);
905 }
906 /* NOTREACHED */
907 }
908
909 /*
910 * convert a string to a number of sectors, with a possible unit
911 * 150M = 150 Megabytes
912 * 2000c = 2000 cylinders
913 * 150256s = 150256 sectors
914 * Without units, use the default (sizemult)
915 * returns the number of sectors, and the unit used (for roundups).
916 */
917
918 static int
919 atofsb(const char *str, uint32_t *p_val, uint32_t *localsizemult)
920 {
921 int i;
922 uint32_t val;
923
924 *localsizemult = sizemult;
925 if (str[0] == '\0') {
926 return 1;
927 }
928 val = 0;
929 for (i = 0; str[i] != '\0'; i++) {
930 if (str[i] >= '0' && str[i] <= '9') {
931 val = val * 10 + str[i] - '0';
932 continue;
933 }
934 if (str[i + 1] != '\0') {
935 /* A non-digit caracter, not at the end */
936 return 1;
937 }
938 if (str[i] == 'G' || str[i] == 'g') {
939 val *= 1024;
940 *localsizemult = MEG / sectorsize;
941 break;
942 }
943 if (str[i] == 'M' || str[i] == 'm') {
944 *localsizemult = MEG / sectorsize;
945 break;
946 }
947 if (str[i] == 'c') {
948 *localsizemult = dlcylsize;
949 break;
950 }
951 if (str[i] == 's') {
952 *localsizemult = 1;
953 break;
954 }
955 /* not a known unit */
956 return 1;
957 }
958 *p_val = val * (*localsizemult);
959 return 0;
960 }
961