label.c revision 1.46.2.2 1 /* $NetBSD: label.c,v 1.46.2.2 2023/11/26 12:40:50 bouyer 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.46.2.2 2023/11/26 12:40:50 bouyer Exp $");
40 #endif
41
42 #include <sys/types.h>
43 #include <stddef.h>
44 #include <assert.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <fcntl.h>
48 #include <util.h>
49 #include <unistd.h>
50 #include <sys/dkio.h>
51 #include <sys/param.h>
52 #include <sys/bootblock.h>
53 #include <sys/bitops.h>
54 #include <ufs/ffs/fs.h>
55
56 #include "defs.h"
57 #include "msg_defs.h"
58 #include "menu_defs.h"
59
60 /*
61 * local prototypes
62 */
63 static bool boringpart(const struct disk_part_info *info);
64 static bool checklabel(struct disk_partitions*, char *, char *);
65 static void show_partition_adder(menudesc *, struct partition_usage_set*);
66
67 /*
68 * Return 1 if a partition should be ignored when checking
69 * for overlapping partitions.
70 */
71 static bool
72 boringpart(const struct disk_part_info *info)
73 {
74
75 if (info->size == 0)
76 return true;
77 if (info->flags &
78 (PTI_PSCHEME_INTERNAL|PTI_WHOLE_DISK|PTI_SEC_CONTAINER|
79 PTI_RAW_PART))
80 return true;
81
82 return false;
83 }
84
85 /*
86 * We have some partitions in our "wanted" list that we may not edit,
87 * like the RAW_PART in disklabel, some that just represent external
88 * mount entries for the final fstab or similar.
89 * We have previously sorted pset->parts and pset->infos to be in sync,
90 * but the former "array" may be shorter.
91 * Here are a few quick predicates to check for them.
92 */
93 static bool
94 real_partition(const struct partition_usage_set *pset, int index)
95 {
96 if (index < 0 || (size_t)index >= pset->num)
97 return false;
98
99 return pset->infos[index].cur_part_id != NO_PART;
100 }
101
102 /*
103 * Check partitioning for overlapping partitions.
104 * Returns true if no overlapping partition found.
105 * Sets reference arguments ovly1 and ovly2 to the indices of
106 * overlapping partitions if any are found.
107 */
108 static bool
109 checklabel(struct disk_partitions *parts,
110 char *ovl1, char *ovl2)
111 {
112 part_id i, j;
113 struct disk_part_info info;
114 daddr_t istart, iend, jstart, jend;
115 unsigned int fs_type, fs_sub_type;
116
117 if (parts->num_part == 0)
118 return true;
119
120 for (i = 0; i < parts->num_part - 1; i ++ ) {
121 if (!parts->pscheme->get_part_info(parts, i, &info))
122 continue;
123
124 /* skip unused or reserved partitions */
125 if (boringpart(&info))
126 continue;
127
128 /*
129 * check succeeding partitions for overlap.
130 * O(n^2), but n is small.
131 */
132 istart = info.start;
133 iend = istart + info.size;
134 fs_type = info.fs_type;
135 fs_sub_type = info.fs_sub_type;
136
137 for (j = i+1; j < parts->num_part; j++) {
138
139 if (!parts->pscheme->get_part_info(parts, j, &info))
140 continue;
141
142 /* skip unused or reserved partitions */
143 if (boringpart(&info))
144 continue;
145
146 jstart = info.start;
147 jend = jstart + info.size;
148
149 /* overlap? */
150 if ((istart <= jstart && jstart < iend) ||
151 (jstart <= istart && istart < jend)) {
152 snprintf(ovl1, MENUSTRSIZE,
153 "%" PRIu64 " - %" PRIu64 " %s, %s",
154 istart / sizemult, iend / sizemult,
155 multname,
156 getfslabelname(fs_type, fs_sub_type));
157 snprintf(ovl2, MENUSTRSIZE,
158 "%" PRIu64 " - %" PRIu64 " %s, %s",
159 jstart / sizemult, jend / sizemult,
160 multname,
161 getfslabelname(info.fs_type,
162 info.fs_sub_type));
163 return false;
164 }
165 }
166 }
167
168 return true;
169 }
170
171 int
172 checkoverlap(struct disk_partitions *parts)
173 {
174 char desc1[MENUSTRSIZE], desc2[MENUSTRSIZE];
175 if (!checklabel(parts, desc1, desc2)) {
176 msg_display_subst(MSG_partitions_overlap, 2, desc1, desc2);
177 return 1;
178 }
179 return 0;
180 }
181
182 /*
183 * return (see post_edit_verify):
184 * 0 -> abort
185 * 1 -> re-edit
186 * 2 -> continue installation
187 */
188 static int
189 verify_parts(struct partition_usage_set *pset, bool install)
190 {
191 struct part_usage_info *wanted;
192 struct disk_partitions *parts;
193 size_t i, num_root;
194 daddr_t first_bsdstart, inst_start;
195 int rv;
196
197 first_bsdstart = inst_start = -1;
198 num_root = 0;
199 parts = pset->parts;
200 for (i = 0; i < pset->num; i++) {
201 wanted = &pset->infos[i];
202
203 if (wanted->flags & PUIFLG_JUST_MOUNTPOINT)
204 continue;
205 if (wanted->cur_part_id == NO_PART)
206 continue;
207 if (!(wanted->instflags & PUIINST_MOUNT))
208 continue;
209 if (strcmp(wanted->mount, "/") != 0)
210 continue;
211 num_root++;
212
213 if (first_bsdstart <= 0) {
214 first_bsdstart = wanted->cur_start;
215 }
216 if (inst_start < 0 &&
217 (wanted->cur_flags & PTI_INSTALL_TARGET)) {
218 inst_start = wanted->cur_start;
219 }
220 }
221
222 if ((num_root == 0 && install) ||
223 (num_root > 1 && inst_start < 0)) {
224 if (num_root == 0 && install)
225 msg_display_subst(MSG_must_be_one_root, 2,
226 msg_string(parts->pscheme->name),
227 msg_string(parts->pscheme->short_name));
228 else
229 msg_display_subst(MSG_multbsdpart, 2,
230 msg_string(parts->pscheme->name),
231 msg_string(parts->pscheme->short_name));
232 rv = ask_reedit(parts);
233 if (rv != 2)
234 return rv;
235 }
236
237 /* Check for overlaps */
238 if (checkoverlap(parts) != 0) {
239 rv = ask_reedit(parts);
240 if (rv != 2)
241 return rv;
242 }
243
244 /*
245 * post_edit_verify returns:
246 * 0 -> abort
247 * 1 -> re-edit
248 * 2 -> continue installation
249 */
250 if (parts->pscheme->post_edit_verify)
251 return parts->pscheme->post_edit_verify(parts, false);
252
253 return 2;
254 }
255
256 static int
257 edit_fs_start(menudesc *m, void *arg)
258 {
259 struct single_part_fs_edit *edit = arg;
260 daddr_t start, end;
261
262 start = getpartoff(edit->pset->parts, edit->info.start);
263 if (edit->info.size != 0) {
264 if (start < (edit->info.start+edit->info.size)) {
265 /* Try to keep end in the same place */
266 end = edit->info.start + edit->info.size;
267 if (end < start)
268 edit->info.size = edit->pset->parts->pscheme->
269 max_free_space_at(edit->pset->parts,
270 edit->info.start);
271 else
272 edit->info.size = end - start;
273 } else {
274 edit->info.size = 0;
275 }
276 }
277 edit->info.start = start;
278 return 0;
279 }
280
281 static int
282 edit_fs_size(menudesc *m, void *arg)
283 {
284 struct single_part_fs_edit *edit = arg;
285 struct disk_part_info pinfo;
286 daddr_t size;
287
288 /* get original partition data, in case start moved already */
289 if (!edit->pset->parts->pscheme->get_part_info(edit->pset->parts,
290 edit->id, &pinfo))
291 pinfo = edit->info;
292 /* ask for new size with old start and current values */
293 size = getpartsize(edit->pset->parts, pinfo.start,
294 edit->info.start, edit->info.size);
295 if (size < 0)
296 return 0;
297 if (size > edit->pset->parts->disk_size)
298 size = edit->pset->parts->disk_size - edit->info.start;
299 edit->info.size = size;
300 return 0;
301 }
302
303 static int
304 set_ffs_opt_pow2(menudesc *m, void *arg)
305 {
306 struct single_part_fs_edit *edit = arg;
307 size_t val = 1 << (edit->offset+m->cursel);
308
309 if (edit->mode == 1) {
310 edit->info.fs_opt1 = val;
311 edit->wanted->fs_opt1 = val;
312 } else if (edit->mode == 2) {
313 edit->info.fs_opt2 = val;
314 edit->wanted->fs_opt2 = val;
315 }
316 return 0;
317 }
318
319 static int
320 edit_fs_ffs_opt(menudesc *m, void *arg, msg head,
321 size_t min_val, size_t max_val)
322 {
323 struct single_part_fs_edit *edit = arg;
324 menu_ent opts[min(MAXPHYS/4096, 8)];
325 char names[min(MAXPHYS/4096, 8)][20];
326 size_t i, val;
327 int menu;
328
329 edit->offset = ilog2(min_val);
330 memset(opts, 0, sizeof opts);
331 for (i = 0, val = min_val; val <= max_val; i++, val <<= 1) {
332 snprintf(names[i], sizeof names[i], "%zu", val);
333 opts[i].opt_name = names[i];
334 opts[i].opt_action = set_ffs_opt_pow2;
335 opts[i].opt_flags = OPT_EXIT;
336 }
337 menu = new_menu(head, opts, i, 40, 6, 0, 0, MC_NOEXITOPT,
338 NULL, NULL, NULL, NULL, NULL);
339 if (menu < 0)
340 return 1;
341 process_menu(menu, arg);
342 free_menu(menu);
343 return 0;
344 }
345
346 static int
347 edit_fs_ffs_block(menudesc *m, void *arg)
348 {
349 struct single_part_fs_edit *edit = arg;
350
351 edit->mode = 1; /* edit fs_opt1 */
352 return edit_fs_ffs_opt(m, arg, MSG_Select_file_system_block_size,
353 4096, MAXPHYS);
354 }
355
356 static int
357 edit_fs_ffs_frag(menudesc *m, void *arg)
358 {
359 struct single_part_fs_edit *edit = arg;
360 size_t bsize, sec_size;
361
362 edit->mode = 2; /* edit fs_opt2 */
363 bsize = edit->info.fs_opt1;
364 if (bsize == 0) {
365 sec_size = edit->wanted->parts->bytes_per_sector;
366 if (edit->wanted->size >= (daddr_t)(128L*(GIG/sec_size)))
367 bsize = 32*1024;
368 else if (edit->wanted->size >= (daddr_t)(1000L*(MEG/sec_size)))
369 bsize = 16*1024;
370 else if (edit->wanted->size >= (daddr_t)(20L*(MEG/sec_size)))
371 bsize = 8*1024;
372 else
373 bsize = 4+1024;
374 }
375 return edit_fs_ffs_opt(m, arg, MSG_Select_file_system_fragment_size,
376 bsize / 8, bsize);
377 }
378
379 static int
380 edit_fs_ffs_avg_size(menudesc *m, void *arg)
381 {
382 struct single_part_fs_edit *edit = arg;
383 char answer[12];
384
385 snprintf(answer, sizeof answer, "%u", edit->info.fs_opt3);
386 msg_prompt_win(MSG_ptn_isize_prompt, -1, 18, 0, 0,
387 answer, answer, sizeof answer);
388 edit->info.fs_opt3 = atol(answer);
389 edit->wanted->fs_opt3 = edit->info.fs_opt3;
390
391 return 0;
392 }
393
394 static int
395 edit_fs_preserve(menudesc *m, void *arg)
396 {
397 struct single_part_fs_edit *edit = arg;
398
399 edit->wanted->instflags ^= PUIINST_NEWFS;
400 return 0;
401 }
402
403 static int
404 edit_install(menudesc *m, void *arg)
405 {
406 struct single_part_fs_edit *edit = arg;
407
408 edit->info.flags ^= PTI_INSTALL_TARGET;
409 return 0;
410 }
411
412 static int
413 edit_fs_mount(menudesc *m, void *arg)
414 {
415 struct single_part_fs_edit *edit = arg;
416
417 edit->wanted->instflags ^= PUIINST_MOUNT;
418 return 0;
419 }
420
421 static int
422 edit_fs_mountpt(menudesc *m, void *arg)
423 {
424 struct single_part_fs_edit *edit = arg;
425 char *p, *first, *last, buf[MOUNTLEN];
426
427 strlcpy(buf, edit->wanted->mount, sizeof buf);
428 msg_prompt_win(MSG_mountpoint, -1, 18, 0, 0,
429 buf, buf, MOUNTLEN);
430
431 /*
432 * Trim all leading and trailing whitespace
433 */
434 for (first = NULL, last = NULL, p = buf; *p; p++) {
435 if (isspace((unsigned char)*p))
436 continue;
437 if (first == NULL)
438 first = p;
439 last = p;
440 }
441 if (last != NULL)
442 last[1] = 0;
443
444 if (first == NULL || *first == 0 || strcmp(first, "-") == 0) {
445 edit->wanted->mount[0] = 0;
446 edit->wanted->instflags &= ~PUIINST_MOUNT;
447 return 0;
448 }
449
450 if (*first != '/') {
451 edit->wanted->mount[0] = '/';
452 strlcpy(&edit->wanted->mount[1], first,
453 sizeof(edit->wanted->mount)-1);
454 } else {
455 strlcpy(edit->wanted->mount, first, sizeof edit->wanted->mount);
456 }
457 edit->wanted->instflags |= PUIINST_MOUNT;
458
459 return 0;
460 }
461
462 static int
463 edit_restore(menudesc *m, void *arg)
464 {
465 struct single_part_fs_edit *edit = arg;
466
467 edit->info = edit->old_info;
468 *edit->wanted = edit->old_usage;
469 return 0;
470 }
471
472 static int
473 edit_cancel(menudesc *m, void *arg)
474 {
475 struct single_part_fs_edit *edit = arg;
476
477 edit->rv = -1;
478 return 1;
479 }
480
481 static int
482 edit_delete_ptn(menudesc *m, void *arg)
483 {
484 struct single_part_fs_edit *edit = arg;
485
486 edit->rv = -2;
487 return 1;
488 }
489
490 /*
491 * We have added/removed partitions, all cur_part_id values are
492 * out of sync. Re-fetch and reorder partitions accordingly.
493 */
494 static void
495 renumber_partitions(struct partition_usage_set *pset)
496 {
497 struct part_usage_info *ninfos;
498 struct disk_part_info info;
499 size_t i;
500 part_id pno;
501
502 ninfos = calloc(pset->parts->num_part, sizeof(*ninfos));
503 if (ninfos == NULL) {
504 err_msg_win(err_outofmem);
505 return;
506 }
507
508 for (pno = 0; pno < pset->parts->num_part; pno++) {
509 if (!pset->parts->pscheme->get_part_info(pset->parts, pno,
510 &info))
511 continue;
512 for (i = 0; i < pset->num; i++) {
513 if (pset->infos[i].cur_start != info.start)
514 continue;
515 if (pset->infos[i].cur_flags != info.flags)
516 continue;
517 if ((info.fs_type != FS_UNUSED &&
518 info.fs_type == pset->infos[i].fs_type) ||
519 (pset->infos[i].type ==
520 info.nat_type->generic_ptype)) {
521 memcpy(&ninfos[pno], &pset->infos[i],
522 sizeof(ninfos[pno]));
523 ninfos[pno].cur_part_id = pno;
524 break;
525 }
526 }
527 }
528
529 free(pset->infos);
530 pset->infos = ninfos;
531 pset->num = pset->parts->num_part;
532 }
533
534 /*
535 * Most often used file system types, we offer them in a first level menu.
536 */
537 static const uint edit_fs_common_types[] =
538 { FS_BSDFFS, FS_SWAP, FS_MSDOS, FS_EFI_SP, FS_BSDLFS, FS_EX2FS };
539
540 /*
541 * Functions for uncommon file system types - we offer the full list,
542 * but put FFSv2 and FFSv1 at the front and duplicate FS_MSDOS as
543 * EFI system partition.
544 */
545 static void
546 init_fs_type_ext(menudesc *menu, void *arg)
547 {
548 struct single_part_fs_edit *edit = arg;
549 uint t = edit->info.fs_type;
550 size_t i, ndx, max = menu->numopts;
551
552 if (t == FS_BSDFFS) {
553 if (edit->info.fs_sub_type == 3)
554 menu->cursel = 0;
555 else if (edit->info.fs_sub_type == 2)
556 menu->cursel = 1;
557 else
558 menu->cursel = 2;
559 return;
560 } else if (t == FS_EX2FS && edit->info.fs_sub_type == 1) {
561 menu->cursel = FSMAXTYPES+2;
562 return;
563 }
564 /* skip the two FFS entries, and do not add FFS later again */
565 for (ndx = 3, i = 0; i < FSMAXTYPES && ndx < max; i++) {
566 if (i == FS_UNUSED)
567 continue;
568 if (i == FS_BSDFFS)
569 continue;
570 if (fstypenames[i] == NULL)
571 continue;
572
573 if (i == t) {
574 menu->cursel = ndx;
575 break;
576 }
577 if (i == FS_MSDOS) {
578 ndx++;
579 if (t == FS_EFI_SP) {
580 menu->cursel = ndx;
581 break;
582 }
583 }
584 ndx++;
585 }
586 }
587
588 static int
589 set_fstype_ext(menudesc *menu, void *arg)
590 {
591 struct single_part_fs_edit *edit = arg;
592 size_t i, ndx, max = menu->numopts;
593 enum part_type pt;
594
595 if (menu->cursel >= 0 && menu->cursel <= 2) {
596 edit->info.fs_type = FS_BSDFFS;
597 edit->info.fs_sub_type = 3-menu->cursel;
598 goto found_type;
599 } else if (menu->cursel == FSMAXTYPES+2) {
600 edit->info.fs_type = FS_EX2FS;
601 edit->info.fs_sub_type = 1;
602 goto found_type;
603 }
604
605 for (ndx = 3, i = 0; i < FSMAXTYPES && ndx < max; i++) {
606 if (i == FS_UNUSED)
607 continue;
608 if (i == FS_BSDFFS)
609 continue;
610 if (fstypenames[i] == NULL)
611 continue;
612
613 if (ndx == (size_t)menu->cursel) {
614 edit->info.fs_type = i;
615 edit->info.fs_sub_type = 0;
616 goto found_type;
617 }
618 ndx++;
619 if (i == FS_MSDOS) {
620 if (ndx == (size_t)menu->cursel) {
621 edit->info.fs_type = FS_EFI_SP;
622 edit->info.fs_sub_type = 0;
623 goto found_type;
624 }
625 ndx++;
626 }
627 }
628 return 1;
629
630 found_type:
631 pt = edit->info.nat_type ? edit->info.nat_type->generic_ptype : PT_root;
632 edit->info.nat_type = edit->pset->parts->pscheme->
633 get_fs_part_type(pt, edit->info.fs_type, edit->info.fs_sub_type);
634 if (edit->info.nat_type == NULL)
635 edit->info.nat_type = edit->pset->parts->pscheme->
636 get_generic_part_type(PT_root);
637 edit->wanted->type = edit->info.nat_type->generic_ptype;
638 edit->wanted->fs_type = edit->info.fs_type;
639 edit->wanted->fs_version = edit->info.fs_sub_type;
640 return 1;
641 }
642
643 /*
644 * Offer a menu with "exotic" file system types, start with FFSv2 and FFSv1,
645 * skip later FFS entry in the generic list.
646 */
647 static int
648 edit_fs_type_ext(menudesc *menu, void *arg)
649 {
650 menu_ent *opts;
651 int m;
652 size_t i, ndx, cnt;
653
654 cnt = __arraycount(fstypenames)+2;
655 opts = calloc(cnt, sizeof(*opts));
656 if (opts == NULL)
657 return 1;
658
659 ndx = 0;
660 opts[ndx].opt_name = msg_string(MSG_fs_type_ffsv2ea);
661 opts[ndx].opt_action = set_fstype_ext;
662 ndx++;
663 opts[ndx].opt_name = msg_string(MSG_fs_type_ffsv2);
664 opts[ndx].opt_action = set_fstype_ext;
665 ndx++;
666 opts[ndx].opt_name = msg_string(MSG_fs_type_ffs);
667 opts[ndx].opt_action = set_fstype_ext;
668 ndx++;
669 for (i = 0; i < FSMAXTYPES && ndx < cnt; i++) {
670 if (i == FS_UNUSED)
671 continue;
672 if (i == FS_BSDFFS)
673 continue;
674 if (fstypenames[i] == NULL)
675 continue;
676 opts[ndx].opt_name = fstypenames[i];
677 opts[ndx].opt_action = set_fstype_ext;
678 ndx++;
679 if (i == FS_MSDOS) {
680 opts[ndx] = opts[ndx-1];
681 opts[ndx].opt_name = getfslabelname(FS_EFI_SP, 0);
682 ndx++;
683 }
684 }
685 opts[ndx].opt_name = msg_string(MSG_fs_type_ext2old);
686 opts[ndx].opt_action = set_fstype_ext;
687 ndx++;
688 assert(ndx == cnt);
689 m = new_menu(MSG_Select_the_type, opts, ndx,
690 30, 6, 10, 0, MC_SUBMENU | MC_SCROLL,
691 init_fs_type_ext, NULL, NULL, NULL, MSG_unchanged);
692
693 if (m < 0)
694 return 1;
695 process_menu(m, arg);
696 free_menu(m);
697 free(opts);
698
699 return 1;
700 }
701
702 static void
703 init_fs_type(menudesc *menu, void *arg)
704 {
705 struct single_part_fs_edit *edit = arg;
706 size_t i;
707
708 /* init menu->cursel from fs type in arg */
709 if (edit->info.fs_type == FS_BSDFFS) {
710 if (edit->info.fs_sub_type == 3)
711 menu->cursel = 0;
712 else if (edit->info.fs_sub_type == 2)
713 menu->cursel = 1;
714 else
715 menu->cursel = 2;
716 }
717 for (i = 1; i < __arraycount(edit_fs_common_types); i++) {
718 if (edit->info.fs_type == edit_fs_common_types[i]) {
719 menu->cursel = i+2;
720 break;
721 }
722 }
723 }
724
725 static int
726 set_fstype(menudesc *menu, void *arg)
727 {
728 struct single_part_fs_edit *edit = arg;
729 enum part_type pt;
730 int ndx;
731
732 pt = edit->info.nat_type ? edit->info.nat_type->generic_ptype : PT_root;
733 if (menu->cursel < 3) {
734 edit->info.fs_type = FS_BSDFFS;
735 edit->info.fs_sub_type = 3-menu->cursel;
736 edit->info.nat_type = edit->pset->parts->pscheme->
737 get_fs_part_type(pt, FS_BSDFFS, edit->info.fs_sub_type);
738 if (edit->info.nat_type == NULL)
739 edit->info.nat_type = edit->pset->parts->
740 pscheme->get_generic_part_type(PT_root);
741 edit->wanted->type = edit->info.nat_type->generic_ptype;
742 edit->wanted->fs_type = edit->info.fs_type;
743 edit->wanted->fs_version = edit->info.fs_sub_type;
744 return 1;
745 }
746 ndx = menu->cursel-2;
747
748 if (ndx < 0 ||
749 (size_t)ndx >= __arraycount(edit_fs_common_types))
750 return 1;
751
752 edit->info.fs_type = edit_fs_common_types[ndx];
753 edit->info.fs_sub_type = 0;
754 edit->info.nat_type = edit->pset->parts->pscheme->
755 get_fs_part_type(pt, edit->info.fs_type, 0);
756 if (edit->info.nat_type == NULL)
757 edit->info.nat_type = edit->pset->parts->
758 pscheme->get_generic_part_type(PT_root);
759 edit->wanted->type = edit->info.nat_type->generic_ptype;
760 edit->wanted->fs_type = edit->info.fs_type;
761 edit->wanted->fs_version = edit->info.fs_sub_type;
762 return 1;
763 }
764
765 /*
766 * Offer a menu selecting the common file system types
767 */
768 static int
769 edit_fs_type(menudesc *menu, void *arg)
770 {
771 struct single_part_fs_edit *edit = arg;
772 menu_ent *opts;
773 int m, cnt;
774 size_t i;
775
776 /*
777 * Shortcut to full menu if we have an exotic value
778 */
779 if (edit->info.fs_type == FS_EX2FS && edit->info.fs_sub_type == 1) {
780 edit_fs_type_ext(menu, arg);
781 return 0;
782 }
783 for (i = 0; i < __arraycount(edit_fs_common_types); i++)
784 if (edit->info.fs_type == edit_fs_common_types[i])
785 break;
786 if (i >= __arraycount(edit_fs_common_types)) {
787 edit_fs_type_ext(menu, arg);
788 return 0;
789 }
790
791 /*
792 * Starting with a common type, show short menu first
793 */
794 cnt = __arraycount(edit_fs_common_types) + 3;
795 opts = calloc(cnt, sizeof(*opts));
796 if (opts == NULL)
797 return 0;
798
799 /* special case entry 0 - 2: three FFS entries */
800 for (i = 0; i < __arraycount(edit_fs_common_types); i++) {
801 opts[i+2].opt_name = getfslabelname(edit_fs_common_types[i], 0);
802 opts[i+2].opt_action = set_fstype;
803 }
804 /* duplicate FFS (at offset 2) into first two entries */
805 opts[0] = opts[1] = opts[2];
806 opts[0].opt_name = msg_string(MSG_fs_type_ffsv2ea);
807 opts[1].opt_name = msg_string(MSG_fs_type_ffsv2);
808 opts[2].opt_name = msg_string(MSG_fs_type_ffs);
809 /* add secondary sub-menu */
810 assert(i+2 < (size_t)cnt);
811 opts[i+2].opt_name = msg_string(MSG_other_fs_type);
812 opts[i+2].opt_action = edit_fs_type_ext;
813
814 m = new_menu(MSG_Select_the_type, opts, cnt,
815 30, 6, 0, 0, MC_SUBMENU | MC_SCROLL,
816 init_fs_type, NULL, NULL, NULL, MSG_unchanged);
817
818 if (m < 0)
819 return 0;
820 process_menu(m, arg);
821 free_menu(m);
822 free(opts);
823
824 return 0;
825 }
826
827
828 static void update_edit_ptn_menu(menudesc *m, void *arg);
829 static void draw_edit_ptn_line(menudesc *m, int opt, void *arg);
830 static int edit_ptn_custom_type(menudesc *m, void *arg);
831
832 static void
833 remember_deleted(struct partition_usage_set *pset,
834 struct disk_partitions *parts)
835 {
836 size_t i, num;
837 struct disk_partitions **tab;
838
839 /* do we have parts on record already? */
840 for (i = 0; i < pset->num_write_back; i++)
841 if (pset->write_back[i] == parts)
842 return;
843 /*
844 * Need to record this partition table for write back
845 */
846 num = pset->num_write_back + 1;
847 tab = realloc(pset->write_back, num*sizeof(*pset->write_back));
848 if (!tab)
849 return;
850 tab[pset->num_write_back] = parts;
851 pset->write_back = tab;
852 pset->num_write_back = num;
853 }
854
855 int
856 edit_ptn(menudesc *menu, void *arg)
857 {
858 struct partition_usage_set *pset = arg;
859 struct single_part_fs_edit edit;
860 int fspart_menu, num_opts;
861 const char *err;
862 menu_ent *mopts, *popt;
863 bool is_new_part, with_inst_opt = pset->parts->parent == NULL;
864
865 static const menu_ent edit_ptn_fields_head[] = {
866 { .opt_action=edit_fs_type },
867 { .opt_action=edit_fs_start },
868 { .opt_action=edit_fs_size },
869 { .opt_flags=OPT_IGNORE },
870 };
871
872 static const menu_ent edit_ptn_fields_head_add[] = {
873 { .opt_action=edit_install },
874 };
875
876 static const menu_ent edit_ptn_fields_head2[] = {
877 { .opt_action=edit_fs_preserve },
878 { .opt_action=edit_fs_mount },
879 { .opt_menu=MENU_mountoptions, .opt_flags=OPT_SUB },
880 { .opt_action=edit_fs_mountpt },
881 };
882
883 static const menu_ent edit_ptn_fields_ffs[] = {
884 { .opt_action=edit_fs_ffs_avg_size },
885 { .opt_action=edit_fs_ffs_block },
886 { .opt_action=edit_fs_ffs_frag },
887 };
888
889 static const menu_ent edit_ptn_fields_tail[] = {
890 { .opt_name=MSG_askunits, .opt_menu=MENU_sizechoice,
891 .opt_flags=OPT_SUB },
892 { .opt_name=MSG_restore,
893 .opt_action=edit_restore},
894 { .opt_name=MSG_Delete_partition,
895 .opt_action=edit_delete_ptn},
896 { .opt_name=MSG_cancel,
897 .opt_action=edit_cancel},
898 };
899
900 memset(&edit, 0, sizeof edit);
901 edit.pset = pset;
902 edit.index = menu->cursel;
903 edit.wanted = &pset->infos[edit.index];
904
905 if (menu->cursel < 0 || (size_t)menu->cursel > pset->parts->num_part)
906 return 0;
907 is_new_part = (size_t)menu->cursel == pset->parts->num_part;
908
909 num_opts = __arraycount(edit_ptn_fields_head) +
910 __arraycount(edit_ptn_fields_head2) +
911 __arraycount(edit_ptn_fields_tail);
912 if (edit.wanted->fs_type == FS_BSDFFS ||
913 edit.wanted->fs_type == FS_BSDLFS)
914 num_opts += __arraycount(edit_ptn_fields_ffs);
915 if (with_inst_opt)
916 num_opts += __arraycount(edit_ptn_fields_head_add);
917 if (is_new_part)
918 num_opts--;
919 else
920 num_opts += pset->parts->pscheme->custom_attribute_count;
921
922 mopts = calloc(num_opts, sizeof(*mopts));
923 if (mopts == NULL) {
924 err_msg_win(err_outofmem);
925 return 0;
926 }
927 memcpy(mopts, edit_ptn_fields_head, sizeof(edit_ptn_fields_head));
928 popt = mopts + __arraycount(edit_ptn_fields_head);
929 if (with_inst_opt) {
930 memcpy(popt, edit_ptn_fields_head_add,
931 sizeof(edit_ptn_fields_head_add));
932 popt += __arraycount(edit_ptn_fields_head_add);
933 }
934 memcpy(popt, edit_ptn_fields_head2, sizeof(edit_ptn_fields_head2));
935 popt += __arraycount(edit_ptn_fields_head2);
936 if (edit.wanted->fs_type == FS_BSDFFS ||
937 edit.wanted->fs_type == FS_BSDLFS) {
938 memcpy(popt, edit_ptn_fields_ffs, sizeof(edit_ptn_fields_ffs));
939 popt += __arraycount(edit_ptn_fields_ffs);
940 }
941 edit.first_custom_attr = popt - mopts;
942 if (!is_new_part) {
943 for (size_t i = 0;
944 i < pset->parts->pscheme->custom_attribute_count;
945 i++, popt++) {
946 popt->opt_action = edit_ptn_custom_type;
947 }
948 }
949 memcpy(popt, edit_ptn_fields_tail, sizeof(edit_ptn_fields_tail));
950 popt += __arraycount(edit_ptn_fields_tail) - 1;
951 if (is_new_part)
952 memcpy(popt-1, popt, sizeof(*popt));
953
954 if (is_new_part) {
955 struct disk_part_free_space space;
956 daddr_t align = pset->parts->pscheme->get_part_alignment(
957 pset->parts);
958
959 edit.id = NO_PART;
960 if (pset->parts->pscheme->get_free_spaces(pset->parts,
961 &space, 1, align, align, -1, -1) == 1) {
962 edit.info.start = space.start;
963 edit.info.size = space.size;
964 edit.info.fs_type = FS_BSDFFS;
965 edit.info.fs_sub_type = 2;
966 edit.info.nat_type = pset->parts->pscheme->
967 get_fs_part_type(PT_root, edit.info.fs_type,
968 edit.info.fs_sub_type);
969 edit.wanted->instflags = PUIINST_NEWFS;
970 }
971 } else {
972 edit.id = pset->infos[edit.index].cur_part_id;
973 if (!pset->parts->pscheme->get_part_info(pset->parts, edit.id,
974 &edit.info)) {
975 free(mopts);
976 return 0;
977 }
978 }
979
980 edit.old_usage = *edit.wanted;
981 edit.old_info = edit.info;
982
983 fspart_menu = new_menu(MSG_edfspart, mopts, num_opts,
984 15, 2, 0, 55, MC_NOCLEAR | MC_SCROLL,
985 update_edit_ptn_menu, draw_edit_ptn_line, NULL,
986 NULL, MSG_OK);
987
988 process_menu(fspart_menu, &edit);
989 free(mopts);
990 free_menu(fspart_menu);
991
992 if (edit.rv == 0) { /* OK, set new data */
993 edit.info.last_mounted = edit.wanted->mount;
994 if (is_new_part) {
995 edit.wanted->parts = pset->parts;
996 if (!can_newfs_fstype(edit.info.fs_type))
997 edit.wanted->instflags &= ~PUIINST_NEWFS;
998 edit.wanted->cur_part_id = pset->parts->pscheme->
999 add_partition(pset->parts, &edit.info, &err);
1000 if (edit.wanted->cur_part_id == NO_PART)
1001 err_msg_win(err);
1002 else {
1003 pset->parts->pscheme->get_part_info(
1004 pset->parts, edit.wanted->cur_part_id,
1005 &edit.info);
1006 edit.wanted->cur_start = edit.info.start;
1007 edit.wanted->size = edit.info.size;
1008 edit.wanted->type =
1009 edit.info.nat_type->generic_ptype;
1010 edit.wanted->fs_type = edit.info.fs_type;
1011 edit.wanted->fs_version = edit.info.fs_sub_type;
1012 edit.wanted->cur_flags = edit.info.flags;
1013 /* things have changed, re-sort */
1014 renumber_partitions(pset);
1015 }
1016 } else {
1017 if (!pset->parts->pscheme->set_part_info(pset->parts,
1018 edit.id, &edit.info, &err))
1019 err_msg_win(err);
1020 else
1021 pset->cur_free_space += edit.old_info.size -
1022 edit.info.size;
1023 }
1024
1025 /*
1026 * if size has changed, we may need to add or remove
1027 * the option to add partitions
1028 */
1029 show_partition_adder(menu, pset);
1030 } else if (edit.rv == -1) { /* cancel edit */
1031 if (is_new_part) {
1032 memmove(pset->infos+edit.index,
1033 pset->infos+edit.index+1,
1034 sizeof(*pset->infos)*(pset->num-edit.index));
1035 memmove(menu->opts+edit.index,
1036 menu->opts+edit.index+1,
1037 sizeof(*menu->opts)*(menu->numopts-edit.index));
1038 menu->numopts--;
1039 menu->cursel = 0;
1040 pset->num--;
1041 return -1;
1042 }
1043 pset->infos[edit.index] = edit.old_usage;
1044 } else if (!is_new_part && edit.rv == -2) { /* delete partition */
1045 if (!pset->parts->pscheme->delete_partition(pset->parts,
1046 edit.id, &err)) {
1047 err_msg_win(err);
1048 return 0;
1049 }
1050 remember_deleted(pset,
1051 pset->infos[edit.index].parts);
1052 pset->cur_free_space += edit.info.size;
1053 memmove(pset->infos+edit.index,
1054 pset->infos+edit.index+1,
1055 sizeof(*pset->infos)*(pset->num-edit.index));
1056 memmove(menu->opts+edit.index,
1057 menu->opts+edit.index+1,
1058 sizeof(*menu->opts)*(menu->numopts-edit.index));
1059 menu->numopts--;
1060 menu->cursel = 0;
1061 if (pset->parts->num_part == 0)
1062 menu->cursel = 1; /* skip sentinel line */
1063
1064 /* things have changed, re-sort */
1065 pset->num--;
1066 renumber_partitions(pset);
1067
1068 /* we can likely add new partitions now */
1069 show_partition_adder(menu, pset);
1070
1071 return -1;
1072 }
1073
1074 return 0;
1075 }
1076
1077 static void
1078 update_edit_ptn_menu(menudesc *m, void *arg)
1079 {
1080 struct single_part_fs_edit *edit = arg;
1081 int i;
1082 uint t = edit->info.fs_type;
1083 size_t attr_no;
1084
1085 /* Determine which of the properties can be changed */
1086 for (i = 0; i < m->numopts; i++) {
1087 if (m->opts[i].opt_action == NULL &&
1088 m->opts[i].opt_menu != MENU_mountoptions)
1089 continue;
1090
1091 /* Default to disabled... */
1092 m->opts[i].opt_flags |= OPT_IGNORE;
1093 if ((t == FS_UNUSED || t == FS_SWAP) &&
1094 (m->opts[i].opt_action == edit_fs_preserve ||
1095 m->opts[i].opt_action == edit_fs_mount ||
1096 m->opts[i].opt_action == edit_fs_mountpt ||
1097 m->opts[i].opt_menu == MENU_mountoptions))
1098 continue;
1099 if (m->opts[i].opt_action == edit_install &&
1100 edit->info.nat_type &&
1101 edit->info.nat_type->generic_ptype != PT_root)
1102 /* can only install onto PT_root partitions */
1103 continue;
1104 if (m->opts[i].opt_action == edit_fs_preserve &&
1105 !can_newfs_fstype(t)) {
1106 /* Can not newfs this filesystem */
1107 edit->wanted->instflags &= ~PUIINST_NEWFS;
1108 continue;
1109 }
1110 if (m->opts[i].opt_action == edit_ptn_custom_type) {
1111 attr_no = (size_t)i - edit->first_custom_attr;
1112 if (!edit->pset->parts->pscheme->
1113 custom_attribute_writable(
1114 edit->pset->parts, edit->id, attr_no))
1115 continue;
1116 }
1117 /*
1118 * Do not allow editing of size/start/type when partition
1119 * is defined in some outer partition table already
1120 */
1121 if ((edit->pset->infos[edit->index].flags & PUIFLG_IS_OUTER)
1122 && (m->opts[i].opt_action == edit_fs_type
1123 || m->opts[i].opt_action == edit_fs_start
1124 || m->opts[i].opt_action == edit_fs_size))
1125 continue;
1126 /* Ok: we want this one */
1127 m->opts[i].opt_flags &= ~OPT_IGNORE;
1128 }
1129
1130 /* Avoid starting at a (now) disabled menu item */
1131 while (m->cursel >= 0 && m->cursel < m->numopts
1132 && (m->opts[m->cursel].opt_flags & OPT_IGNORE))
1133 m->cursel++;
1134 }
1135
1136 static void
1137 draw_edit_ptn_line(menudesc *m, int opt, void *arg)
1138 {
1139 struct single_part_fs_edit *edit = arg;
1140 static int col_width;
1141 static const char *ptn_type, *ptn_start, *ptn_size, *ptn_end,
1142 *ptn_newfs, *ptn_mount, *ptn_mount_options, *ptn_mountpt,
1143 *ptn_install, *ptn_bsize, *ptn_fsize, *ptn_isize;
1144 const char *c;
1145 char val[MENUSTRSIZE];
1146 const char *attrname;
1147 size_t attr_no;
1148
1149 if (col_width == 0) {
1150 int l;
1151
1152 #define LOAD(STR) STR = msg_string(MSG_##STR); l = strlen(STR); \
1153 if (l > col_width) col_width = l
1154
1155 LOAD(ptn_type);
1156 LOAD(ptn_start);
1157 LOAD(ptn_size);
1158 LOAD(ptn_end);
1159 LOAD(ptn_install);
1160 LOAD(ptn_newfs);
1161 LOAD(ptn_mount);
1162 LOAD(ptn_mount_options);
1163 LOAD(ptn_mountpt);
1164 LOAD(ptn_bsize);
1165 LOAD(ptn_fsize);
1166 LOAD(ptn_isize);
1167 #undef LOAD
1168
1169 for (size_t i = 0;
1170 i < edit->pset->parts->pscheme->custom_attribute_count;
1171 i++) {
1172 attrname = msg_string(
1173 edit->pset->parts->pscheme->custom_attributes[i]
1174 .label);
1175 l = strlen(attrname);
1176 if (l > col_width) col_width = l;
1177 }
1178
1179 col_width += 3;
1180 }
1181
1182 if (m->opts[opt].opt_flags & OPT_IGNORE
1183 && (opt != 3 || edit->info.fs_type == FS_UNUSED)
1184 && m->opts[opt].opt_action != edit_ptn_custom_type
1185 && m->opts[opt].opt_action != edit_fs_type
1186 && m->opts[opt].opt_action != edit_fs_start
1187 && m->opts[opt].opt_action != edit_fs_size) {
1188 wprintw(m->mw, "%*s -", col_width, "");
1189 return;
1190 }
1191
1192 if (opt < 4) {
1193 switch (opt) {
1194 case 0:
1195 if (edit->info.fs_type == FS_BSDFFS) {
1196 if (edit->info.fs_sub_type == 3)
1197 c = msg_string(MSG_fs_type_ffsv2ea);
1198 else if (edit->info.fs_sub_type == 2)
1199 c = msg_string(MSG_fs_type_ffsv2);
1200 else
1201 c = msg_string(MSG_fs_type_ffs);
1202 } else {
1203 c = getfslabelname(edit->info.fs_type,
1204 edit->info.fs_sub_type);
1205 }
1206 wprintw(m->mw, "%*s : %s", col_width, ptn_type, c);
1207 return;
1208 case 1:
1209 wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1210 ptn_start, edit->info.start / sizemult, multname);
1211 return;
1212 case 2:
1213 wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1214 ptn_size, edit->info.size / sizemult, multname);
1215 return;
1216 case 3:
1217 wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1218 ptn_end, (edit->info.start + edit->info.size)
1219 / sizemult, multname);
1220 return;
1221 }
1222 }
1223 if (m->opts[opt].opt_action == edit_install) {
1224 wprintw(m->mw, "%*s : %s", col_width, ptn_install,
1225 msg_string((edit->info.flags & PTI_INSTALL_TARGET)
1226 ? MSG_Yes : MSG_No));
1227 return;
1228 }
1229 if (m->opts[opt].opt_action == edit_fs_preserve) {
1230 wprintw(m->mw, "%*s : %s", col_width, ptn_newfs,
1231 msg_string(edit->wanted->instflags & PUIINST_NEWFS
1232 ? MSG_Yes : MSG_No));
1233 return;
1234 }
1235 if (m->opts[opt].opt_action == edit_fs_mount) {
1236 wprintw(m->mw, "%*s : %s", col_width, ptn_mount,
1237 msg_string(edit->wanted->instflags & PUIINST_MOUNT
1238 ? MSG_Yes : MSG_No));
1239 return;
1240 }
1241 if (m->opts[opt].opt_action == edit_fs_ffs_block) {
1242 wprintw(m->mw, "%*s : %u", col_width, ptn_bsize,
1243 edit->wanted->fs_opt1);
1244 return;
1245 }
1246 if (m->opts[opt].opt_action == edit_fs_ffs_frag) {
1247 wprintw(m->mw, "%*s : %u", col_width, ptn_fsize,
1248 edit->wanted->fs_opt2);
1249 return;
1250 }
1251 if (m->opts[opt].opt_action == edit_fs_ffs_avg_size) {
1252 if (edit->wanted->fs_opt3 == 0)
1253 wprintw(m->mw, "%*s : %s", col_width, ptn_isize,
1254 msg_string(MSG_ptn_isize_dflt));
1255 else {
1256 char buf[24], *line;
1257 const char *t = buf;
1258
1259 snprintf(buf, sizeof buf, "%u", edit->wanted->fs_opt3);
1260 line = str_arg_subst(msg_string(MSG_ptn_isize_bytes),
1261 1, &t);
1262 wprintw(m->mw, "%*s : %s", col_width, ptn_isize,
1263 line);
1264 free(line);
1265 }
1266 return;
1267 }
1268 if (m->opts[opt].opt_menu == MENU_mountoptions) {
1269 wprintw(m->mw, "%*s : ", col_width, ptn_mount_options);
1270 if (edit->wanted->mountflags & PUIMNT_ASYNC)
1271 wprintw(m->mw, "async ");
1272 if (edit->wanted->mountflags & PUIMNT_NOATIME)
1273 wprintw(m->mw, "noatime ");
1274 if (edit->wanted->mountflags & PUIMNT_NODEV)
1275 wprintw(m->mw, "nodev ");
1276 if (edit->wanted->mountflags & PUIMNT_NODEVMTIME)
1277 wprintw(m->mw, "nodevmtime ");
1278 if (edit->wanted->mountflags & PUIMNT_NOEXEC)
1279 wprintw(m->mw, "noexec ");
1280 if (edit->wanted->mountflags & PUIMNT_NOSUID)
1281 wprintw(m->mw, "nosuid ");
1282 if (edit->wanted->mountflags & PUIMNT_LOG)
1283 wprintw(m->mw, "log ");
1284 if (edit->wanted->mountflags & PUIMNT_NOAUTO)
1285 wprintw(m->mw, "noauto ");
1286 return;
1287 }
1288 if (m->opts[opt].opt_action == edit_fs_mountpt) {
1289 wprintw(m->mw, "%*s : %s", col_width, ptn_mountpt,
1290 edit->wanted->mount);
1291 return;
1292 }
1293
1294 attr_no = opt - edit->first_custom_attr;
1295 edit->pset->parts->pscheme->format_custom_attribute(
1296 edit->pset->parts, edit->id, attr_no, &edit->info,
1297 val, sizeof val);
1298 attrname = msg_string(edit->pset->parts->pscheme->
1299 custom_attributes[attr_no].label);
1300 wprintw(m->mw, "%*s : %s", col_width, attrname, val);
1301 }
1302
1303 static int
1304 edit_ptn_custom_type(menudesc *m, void *arg)
1305 {
1306 struct single_part_fs_edit *edit = arg;
1307 size_t attr_no = m->cursel - edit->first_custom_attr;
1308 char line[STRSIZE];
1309
1310 switch (edit->pset->parts->pscheme->custom_attributes[attr_no].type) {
1311 case pet_bool:
1312 edit->pset->parts->pscheme->custom_attribute_toggle(
1313 edit->pset->parts, edit->id, attr_no);
1314 break;
1315 case pet_cardinal:
1316 case pet_str:
1317 edit->pset->parts->pscheme->format_custom_attribute(
1318 edit->pset->parts, edit->id, attr_no, &edit->info,
1319 line, sizeof(line));
1320 msg_prompt_win(
1321 edit->pset->parts->pscheme->custom_attributes[attr_no].
1322 label, -1, 18, 0, 0, line, line, sizeof(line));
1323 edit->pset->parts->pscheme->custom_attribute_set_str(
1324 edit->pset->parts, edit->id, attr_no, line);
1325 break;
1326 }
1327
1328 return 0;
1329 }
1330
1331
1332 /*
1333 * Some column width depend on translation, we will set these in
1334 * fmt_fspart_header and later use it when formatting single entries
1335 * in fmt_fspart_row.
1336 * The table consist of 3 "size like" columns, all fixed width, then
1337 * ptnheaders_fstype, part_header_col_flag, and finally the mount point
1338 * (which is variable width).
1339 */
1340 static int fstype_width, flags_width;
1341 static char fspart_separator[MENUSTRSIZE];
1342 static char fspart_title[2*MENUSTRSIZE];
1343
1344 /*
1345 * Format the header of the main partition editor menu.
1346 */
1347 static void
1348 fmt_fspart_header(menudesc *menu, void *arg)
1349 {
1350 struct partition_usage_set *pset = arg;
1351 char total[6], free_space[6], scol[13], ecol[13], szcol[13],
1352 sepline[MENUSTRSIZE], *p, desc[MENUSTRSIZE];
1353 const char *fstype, *flags;
1354 int i;
1355 size_t ptn;
1356 bool with_clone, with_inst_flag = pset->parts->parent == NULL;
1357
1358 with_clone = false;
1359 for (ptn = 0; ptn < pset->num && !with_clone; ptn++)
1360 if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
1361 with_clone = true;
1362 humanize_number(total, sizeof total,
1363 pset->parts->disk_size * pset->parts->bytes_per_sector,
1364 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1365 humanize_number(free_space, sizeof free_space,
1366 pset->cur_free_space * pset->parts->bytes_per_sector,
1367 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1368
1369 if (with_clone)
1370 strlcpy(desc, msg_string(MSG_clone_flag_desc), sizeof desc);
1371 else
1372 desc[0] = 0;
1373 if (pset->parts->pscheme->part_flag_desc)
1374 strlcat(desc, msg_string(pset->parts->pscheme->part_flag_desc),
1375 sizeof desc);
1376
1377 msg_display_subst(MSG_fspart, 7, pset->parts->disk,
1378 msg_string(pset->parts->pscheme->name),
1379 msg_string(pset->parts->pscheme->short_name),
1380 with_inst_flag ? msg_string(MSG_ptn_instflag_desc) : "",
1381 desc, total, free_space);
1382
1383 snprintf(scol, sizeof scol, "%s (%s)",
1384 msg_string(MSG_ptnheaders_start), multname);
1385 snprintf(ecol, sizeof ecol, "%s (%s)",
1386 msg_string(MSG_ptnheaders_end), multname);
1387 snprintf(szcol, sizeof szcol, "%s (%s)",
1388 msg_string(MSG_ptnheaders_size), multname);
1389
1390 fstype = msg_string(MSG_ptnheaders_fstype);
1391 flags = msg_string(MSG_part_header_col_flag);
1392 fstype_width = max(strlen(fstype), 8);
1393 flags_width = strlen(flags);
1394 for (i = 0, p = sepline; i < fstype_width; i++)
1395 *p++ = '-';
1396 for (i = 0, *p++ = ' '; i < flags_width; i++)
1397 *p++ = '-';
1398 *p = 0;
1399
1400 snprintf(fspart_separator, sizeof(fspart_separator),
1401 "------------ ------------ ------------ %s ----------------",
1402 sepline);
1403
1404 snprintf(fspart_title, sizeof(fspart_title),
1405 " %12.12s %12.12s %12.12s %*s %*s %s\n"
1406 " %s", scol, ecol, szcol, fstype_width, fstype,
1407 flags_width, flags, msg_string(MSG_ptnheaders_filesystem),
1408 fspart_separator);
1409
1410 msg_table_add("\n\n");
1411 }
1412
1413 /*
1414 * Format one partition entry in the main partition editor.
1415 */
1416 static void
1417 fmt_fspart_row(menudesc *m, int ptn, void *arg)
1418 {
1419 struct partition_usage_set *pset = arg;
1420 struct disk_part_info info;
1421 daddr_t poffset, psize, pend;
1422 const char *desc;
1423 static const char *Yes;
1424 char flag_str[MENUSTRSIZE], *fp;
1425 unsigned inst_flags;
1426 #ifndef NO_CLONES
1427 size_t clone_cnt;
1428 #endif
1429 bool with_inst_flag = pset->parts->parent == NULL;
1430
1431 if (Yes == NULL)
1432 Yes = msg_string(MSG_Yes);
1433
1434 #ifndef NO_CLONES
1435 if ((pset->infos[ptn].flags & PUIFLG_CLONE_PARTS) &&
1436 pset->infos[ptn].cur_part_id == NO_PART) {
1437 psize = pset->infos[ptn].size / sizemult;
1438 if (pset->infos[ptn].clone_ndx <
1439 pset->infos[ptn].clone_src->num_sel)
1440 clone_cnt = 1;
1441 else
1442 clone_cnt = pset->infos[ptn].clone_src->num_sel;
1443 if (pset->infos[ptn].cur_part_id == NO_PART)
1444 wprintw(m->mw, " %12" PRIu64
1445 " [%zu %s]", psize, clone_cnt,
1446 msg_string(MSG_clone_target_disp));
1447 else {
1448 poffset = pset->infos[ptn].cur_start / sizemult;
1449 pend = (pset->infos[ptn].cur_start +
1450 pset->infos[ptn].size) / sizemult - 1;
1451 wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
1452 " [%zu %s]",
1453 poffset, pend, psize, clone_cnt,
1454 msg_string(MSG_clone_target_disp));
1455 }
1456 if (m->title == fspart_title)
1457 m->opts[ptn].opt_flags |= OPT_IGNORE;
1458 else
1459 m->opts[ptn].opt_flags &= ~OPT_IGNORE;
1460 return;
1461 }
1462 #endif
1463
1464 if (!real_partition(pset, ptn))
1465 return;
1466
1467 if (!pset->parts->pscheme->get_part_info(pset->parts,
1468 pset->infos[ptn].cur_part_id, &info))
1469 return;
1470
1471 /*
1472 * We use this function in multiple menus, but only want it
1473 * to play with enable/disable in a single one:
1474 */
1475 if (m->title == fspart_title) {
1476 /*
1477 * Enable / disable this line if it is something
1478 * like RAW_PART
1479 */
1480 if ((info.flags &
1481 (PTI_WHOLE_DISK|PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
1482 || (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS))
1483 m->opts[ptn].opt_flags |= OPT_IGNORE;
1484 else
1485 m->opts[ptn].opt_flags &= ~OPT_IGNORE;
1486 }
1487
1488 poffset = info.start / sizemult;
1489 psize = info.size / sizemult;
1490 if (psize == 0)
1491 pend = 0;
1492 else
1493 pend = (info.start + info.size) / sizemult - 1;
1494
1495 if (info.flags & PTI_WHOLE_DISK)
1496 desc = msg_string(MSG_NetBSD_partition_cant_change);
1497 else if (info.flags & PTI_RAW_PART)
1498 desc = msg_string(MSG_Whole_disk_cant_change);
1499 else if (info.flags & PTI_BOOT)
1500 desc = msg_string(MSG_Boot_partition_cant_change);
1501 else
1502 desc = getfslabelname(info.fs_type, info.fs_sub_type);
1503
1504 fp = flag_str;
1505 inst_flags = pset->infos[ptn].instflags;
1506 if (with_inst_flag && (info.flags & PTI_INSTALL_TARGET) &&
1507 info.nat_type->generic_ptype == PT_root) {
1508 static char inst_flag;
1509
1510 if (inst_flag == 0)
1511 inst_flag = msg_string(MSG_install_flag)[0];
1512 *fp++ = inst_flag;
1513 }
1514 if (inst_flags & PUIINST_NEWFS)
1515 *fp++ = msg_string(MSG_newfs_flag)[0];
1516 if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
1517 *fp++ = msg_string(MSG_clone_flag)[0];
1518 *fp = 0;
1519 if (pset->parts->pscheme->get_part_attr_str != NULL)
1520 pset->parts->pscheme->get_part_attr_str(pset->parts,
1521 pset->infos[ptn].cur_part_id, fp, sizeof(flag_str)-1);
1522
1523 /* if the fstype description does not fit, check if we can overrun */
1524 if (strlen(desc) > (size_t)fstype_width &&
1525 flag_str[0] == 0 && (info.last_mounted == NULL ||
1526 info.last_mounted[0] == 0))
1527 wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
1528 " %s",
1529 poffset, pend, psize, desc);
1530 else
1531 wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
1532 " %*.*s %*s %s",
1533 poffset, pend, psize, fstype_width, fstype_width, desc,
1534 -flags_width, flag_str,
1535 (inst_flags & PUIINST_MOUNT) && info.last_mounted &&
1536 info.last_mounted[0] ? info.last_mounted : "");
1537 }
1538
1539 #ifndef NO_CLONES
1540 static int
1541 part_ext_clone(menudesc *m, void *arg)
1542 {
1543 struct selected_partitions selected, *clone_src;
1544 struct clone_target_menu_data data;
1545 struct partition_usage_set *pset = arg;
1546 struct part_usage_info *p;
1547 struct disk_part_info sinfo, cinfo;
1548 struct disk_partitions *csrc;
1549 struct disk_part_free_space space;
1550 menu_ent *men;
1551 daddr_t clone_size, free_size, offset, align;
1552 int num_men, i;
1553 size_t s, clone_cnt;
1554 part_id cid;
1555 struct clone_data {
1556 struct disk_part_info info;
1557 part_id new_id;
1558 size_t ndx;
1559 };
1560 struct clone_data *clones = NULL;
1561
1562 if (!select_partitions(&selected, pm->parts))
1563 return 0;
1564
1565 clone_size = selected_parts_size(&selected);
1566 num_men = pset->num+1;
1567 men = calloc(num_men, sizeof *men);
1568 if (men == NULL)
1569 return 0;
1570 for (i = 0; i < num_men; i++) {
1571 men[i].opt_action = clone_target_select;
1572 if (i == 0)
1573 free_size = pset->infos[i].cur_start;
1574 else if (i > 0 && (size_t)i < pset->num)
1575 free_size = pset->infos[i].cur_start -
1576 pset->infos[i-1].cur_start - pset->infos[i-1].size;
1577 else
1578 free_size = pset->parts->free_space;
1579 if (free_size < clone_size)
1580 men[i].opt_flags = OPT_IGNORE;
1581 }
1582 men[num_men-1].opt_name = MSG_clone_target_end;
1583
1584 memset(&data, 0, sizeof data);
1585 data.usage = *pset;
1586 data.res = -1;
1587
1588 data.usage.menu = new_menu(MSG_clone_target_hdr,
1589 men, num_men, 3, 2, 0, 65, MC_SCROLL,
1590 NULL, fmt_fspart_row, NULL, NULL, MSG_cancel);
1591 process_menu(data.usage.menu, &data);
1592 free_menu(data.usage.menu);
1593 free(men);
1594
1595 if (data.res < 0)
1596 goto err;
1597
1598 /* create temporary infos for all clones that work out */
1599 clone_cnt = 0;
1600 clones = calloc(selected.num_sel, sizeof(*clones));
1601 if (clones == NULL)
1602 goto err;
1603
1604 clone_src = malloc(sizeof(selected));
1605 if (clone_src == NULL)
1606 goto err;
1607 *clone_src = selected;
1608
1609 /* find selected offset from data.res and insert clones there */
1610 align = pset->parts->pscheme->get_part_alignment(pset->parts);
1611 offset = -1;
1612 if (data.res > 0)
1613 offset = pset->infos[data.res-1].cur_start
1614 + pset->infos[data.res-1].size;
1615 else
1616 offset = 0;
1617 for (s = 0; s < selected.num_sel; s++) {
1618 csrc = selected.selection[s].parts;
1619 cid = selected.selection[s].id;
1620 csrc->pscheme->get_part_info(csrc, cid, &sinfo);
1621 if (!pset->parts->pscheme->adapt_foreign_part_info(
1622 pset->parts, &cinfo, csrc->pscheme, &sinfo))
1623 continue;
1624 size_t cnt = pset->parts->pscheme->get_free_spaces(
1625 pset->parts, &space, 1, cinfo.size-align, align,
1626 offset, -1);
1627 if (cnt == 0)
1628 continue;
1629 cinfo.start = space.start;
1630 cid = pset->parts->pscheme->add_partition(
1631 pset->parts, &cinfo, NULL);
1632 if (cid == NO_PART)
1633 continue;
1634 pset->parts->pscheme->get_part_info(pset->parts, cid, &cinfo);
1635 clones[clone_cnt].info = cinfo;
1636 clones[clone_cnt].new_id = cid;
1637 clones[clone_cnt].ndx = s;
1638 clone_cnt++;
1639 offset = roundup(cinfo.start+cinfo.size, align);
1640 }
1641
1642 /* insert new clone records at offset data.res */
1643 men = realloc(m->opts, (m->numopts+clone_cnt)*sizeof(*m->opts));
1644 if (men == NULL)
1645 goto err;
1646 pset->menu_opts = men;
1647 m->opts = men;
1648 m->numopts += clone_cnt;
1649
1650 p = realloc(pset->infos, (pset->num+clone_cnt)*sizeof(*pset->infos));
1651 if (p == NULL)
1652 goto err;
1653 pset->infos = p;
1654
1655 men += data.res;
1656 p += data.res;
1657 memmove(men+clone_cnt, men,
1658 sizeof(*men)*(m->numopts-data.res-clone_cnt));
1659 if (pset->num > (size_t)data.res)
1660 memmove(p+clone_cnt, p, sizeof(*p)*(pset->num-data.res));
1661 memset(men, 0, sizeof(*men)*clone_cnt);
1662 memset(p, 0, sizeof(*p)*clone_cnt);
1663 for (s = 0; s < clone_cnt; s++) {
1664 p[s].cur_part_id = clones[s].new_id;
1665 p[s].cur_start = clones[s].info.start;
1666 p[s].size = clones[s].info.size;
1667 p[s].cur_flags = clones[s].info.flags;
1668 p[s].flags = PUIFLG_CLONE_PARTS;
1669 p[s].parts = pset->parts;
1670 p[s].clone_src = clone_src;
1671 p[s].clone_ndx = s;
1672 }
1673 free(clones);
1674 m->cursel = ((size_t)data.res >= pset->num) ? 0 : data.res+clone_cnt;
1675 pset->num += clone_cnt;
1676 m->h = 0;
1677 resize_menu_height(m);
1678
1679 return -1;
1680
1681 err:
1682 free(clones);
1683 free_selected_partitions(&selected);
1684 return 0;
1685 }
1686 #endif
1687
1688 static int
1689 edit_fspart_pack(menudesc *m, void *arg)
1690 {
1691 struct partition_usage_set *pset = arg;
1692 char buf[STRSIZE];
1693
1694 if (!pset->parts->pscheme->get_disk_pack_name(pset->parts,
1695 buf, sizeof buf))
1696 return 0;
1697
1698 msg_prompt_win(MSG_edit_disk_pack_hdr,
1699 -1, 18, 0, -1, buf, buf, sizeof(buf));
1700
1701 pset->parts->pscheme->set_disk_pack_name(pset->parts, buf);
1702 return 0;
1703 }
1704
1705 static int
1706 edit_fspart_add(menudesc *m, void *arg)
1707 {
1708 struct partition_usage_set *pset = arg;
1709 struct part_usage_info *ninfo;
1710 menu_ent *nmenopts;
1711 size_t cnt, off;
1712
1713 ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
1714 if (ninfo == NULL)
1715 return 0;
1716 pset->infos = ninfo;
1717 off = pset->parts->num_part;
1718 cnt = pset->num-pset->parts->num_part;
1719 if (cnt > 0)
1720 memmove(pset->infos+off+1,pset->infos+off,
1721 cnt*sizeof(*pset->infos));
1722 memset(pset->infos+off, 0, sizeof(*pset->infos));
1723
1724 nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
1725 if (nmenopts == NULL)
1726 return 0;
1727 memmove(nmenopts+off+1, nmenopts+off,
1728 (m->numopts-off)*sizeof(*nmenopts));
1729 memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
1730 nmenopts[off].opt_action = edit_ptn;
1731 pset->menu_opts = m->opts = nmenopts;
1732 m->numopts++;
1733 m->cursel = off;
1734 pset->num++;
1735
1736 /* now update edit menu to fit */
1737 m->h = 0;
1738 resize_menu_height(m);
1739
1740 /* and directly invoke the partition editor for the new part */
1741 edit_ptn(m, arg);
1742
1743 show_partition_adder(m, pset);
1744
1745 return -1;
1746 }
1747
1748 static void
1749 add_partition_adder(menudesc *m, struct partition_usage_set *pset)
1750 {
1751 struct part_usage_info *ninfo;
1752 menu_ent *nmenopts;
1753 size_t off;
1754
1755 ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
1756 if (ninfo == NULL)
1757 return;
1758 pset->infos = ninfo;
1759 off = pset->parts->num_part+1;
1760
1761 nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
1762 if (nmenopts == NULL)
1763 return;
1764 memmove(nmenopts+off+1, nmenopts+off,
1765 (m->numopts-off)*sizeof(*nmenopts));
1766 memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
1767
1768 nmenopts[off].opt_name = MSG_addpart;
1769 nmenopts[off].opt_flags = OPT_SUB;
1770 nmenopts[off].opt_action = edit_fspart_add;
1771
1772 m->opts = nmenopts;
1773 m->numopts++;
1774 }
1775
1776 static void
1777 remove_partition_adder(menudesc *m, struct partition_usage_set *pset)
1778 {
1779 size_t off;
1780
1781 off = pset->parts->num_part+1;
1782 memmove(m->opts+off, m->opts+off+1,
1783 (m->numopts-off-1)*sizeof(*m->opts));
1784 m->numopts--;
1785 }
1786
1787 /*
1788 * Called whenever the "add a partition" option may need to be removed
1789 * or added
1790 */
1791 static void
1792 show_partition_adder(menudesc *m, struct partition_usage_set *pset)
1793 {
1794 if (m->opts == NULL)
1795 return;
1796
1797 bool can_add_partition = pset->parts->pscheme->can_add_partition(
1798 pset->parts);
1799 bool part_adder_present =
1800 (m->opts[pset->parts->num_part].opt_flags & OPT_IGNORE) &&
1801 (m->opts[pset->parts->num_part+1].opt_action == edit_fspart_add);
1802
1803 if (can_add_partition == part_adder_present)
1804 return;
1805
1806 if (can_add_partition)
1807 add_partition_adder(m, pset);
1808 else
1809 remove_partition_adder(m, pset);
1810
1811 /* now update edit menu to fit */
1812 m->h = 0;
1813 resize_menu_height(m);
1814 }
1815
1816 static int
1817 edit_fspart_abort(menudesc *m, void *arg)
1818 {
1819 struct partition_usage_set *pset = arg;
1820
1821 pset->ok = false;
1822 return 1;
1823 }
1824
1825 /*
1826 * Check a disklabel.
1827 * If there are overlapping active partitions,
1828 * Ask the user if they want to edit the partition or give up.
1829 */
1830 int
1831 edit_and_check_label(struct pm_devs *p, struct partition_usage_set *pset,
1832 bool install)
1833 {
1834 menu_ent *op;
1835 size_t cnt, i;
1836 bool may_add = pset->parts->pscheme->can_add_partition(pset->parts);
1837 bool may_edit_pack =
1838 pset->parts->pscheme->get_disk_pack_name != NULL &&
1839 pset->parts->pscheme->set_disk_pack_name != NULL;
1840
1841 #ifdef NO_CLONES
1842 #define C_M_ITEMS 0
1843 #else
1844 #define C_M_ITEMS 1
1845 #endif
1846 pset->menu_opts = calloc(pset->parts->num_part
1847 +3+C_M_ITEMS+may_add+may_edit_pack,
1848 sizeof *pset->menu_opts);
1849 if (pset->menu_opts == NULL)
1850 return 0;
1851
1852 op = pset->menu_opts;
1853 for (i = 0; i < pset->parts->num_part; i++) {
1854 op->opt_action = edit_ptn;
1855 op++;
1856 }
1857 /* separator line between partitions and actions */
1858 op->opt_name = fspart_separator;
1859 op->opt_flags = OPT_IGNORE|OPT_NOSHORT;
1860 op++;
1861
1862 /* followed by new partition adder */
1863 if (may_add) {
1864 op->opt_name = MSG_addpart;
1865 op->opt_flags = OPT_SUB;
1866 op->opt_action = edit_fspart_add;
1867 op++;
1868 }
1869
1870 /* and unit changer */
1871 op->opt_name = MSG_askunits;
1872 op->opt_menu = MENU_sizechoice;
1873 op->opt_flags = OPT_SUB;
1874 op->opt_action = NULL;
1875 op++;
1876
1877 if (may_edit_pack) {
1878 op->opt_name = MSG_editpack;
1879 op->opt_flags = OPT_SUB;
1880 op->opt_action = edit_fspart_pack;
1881 op++;
1882 }
1883
1884 #ifndef NO_CLONES
1885 /* add a clone-from-elsewhere option */
1886 op->opt_name = MSG_clone_from_elsewhere;
1887 op->opt_action = part_ext_clone;
1888 op++;
1889 #endif
1890
1891 /* and abort option */
1892 op->opt_name = MSG_cancel;
1893 op->opt_flags = OPT_EXIT;
1894 op->opt_action = edit_fspart_abort;
1895 op++;
1896 cnt = op - pset->menu_opts;
1897 assert(cnt == pset->parts->num_part+3+C_M_ITEMS+may_add+may_edit_pack);
1898
1899 pset->menu = new_menu(fspart_title, pset->menu_opts, cnt,
1900 0, -1, 0, 74,
1901 MC_ALWAYS_SCROLL|MC_NOBOX|MC_DFLTEXIT|
1902 MC_NOCLEAR|MC_CONTINUOUS,
1903 fmt_fspart_header, fmt_fspart_row, NULL, NULL,
1904 MSG_partition_sizes_ok);
1905
1906 if (pset->menu < 0) {
1907 free(pset->menu_opts);
1908 pset->menu_opts = NULL;
1909 return 0;
1910 }
1911
1912 p->current_cylsize = p->dlcylsize;
1913
1914 for (;;) {
1915 /* first give the user the option to edit the label... */
1916 pset->ok = true;
1917 process_menu(pset->menu, pset);
1918 if (!pset->ok) {
1919 i = 0;
1920 break;
1921 }
1922
1923 /* User thinks the label is OK. */
1924 i = verify_parts(pset, install);
1925 if (i == 1)
1926 continue;
1927 break;
1928 }
1929 free(pset->menu_opts);
1930 pset->menu_opts = NULL;
1931 free_menu(pset->menu);
1932 pset->menu = -1;
1933
1934 return i != 0;
1935 }
1936
1937 /*
1938 * strip trailing / to avoid confusion in path comparisons later
1939 */
1940 void
1941 canonicalize_last_mounted(char *path)
1942 {
1943 char *p;
1944
1945 if (path == NULL)
1946 return;
1947
1948 if (strcmp(path, "/") == 0)
1949 return; /* in this case a "trailing" slash is allowed */
1950
1951 for (;;) {
1952 p = strrchr(path, '/');
1953 if (p == NULL)
1954 return;
1955 if (p[1] != 0)
1956 return;
1957 p[0] = 0;
1958 }
1959 }
1960
1961 /*
1962 * Try to get 'last mounted on' (or equiv) from fs superblock.
1963 */
1964 const char *
1965 get_last_mounted(int fd, daddr_t partstart, uint *fs_type, uint *fs_sub_type,
1966 uint flags)
1967 {
1968 static char sblk[SBLOCKSIZE] __aligned(8); /* is this enough? */
1969 struct fs *SB = (struct fs *)sblk;
1970 static const off_t sblocks[] = SBLOCKSEARCH;
1971 const off_t *sbp;
1972 const char *mnt = NULL;
1973 int len;
1974
1975 if (fd == -1)
1976 return "";
1977
1978 if (fs_type)
1979 *fs_type = 0;
1980 if (fs_sub_type)
1981 *fs_sub_type = 0;
1982
1983 /* Check UFS1/2 (and hence LFS) superblock */
1984 for (sbp = sblocks; mnt == NULL && *sbp != -1; sbp++) {
1985 if (pread(fd, sblk, sizeof sblk,
1986 (off_t)partstart * (off_t)512 + *sbp) != sizeof sblk)
1987 continue;
1988
1989 /*
1990 * If start of partition and allowed by flags check
1991 * for other fs types
1992 */
1993 if (*sbp == 0 && (flags & GLM_MAYBE_FAT32) &&
1994 sblk[0x42] == 0x29 && memcmp(sblk + 0x52, "FAT", 3) == 0) {
1995 /* Probably a FAT filesystem, report volume name */
1996 size_t i;
1997 for (i = 0x51; i >= 0x47; i--) {
1998 if (sblk[i] != ' ')
1999 break;
2000 sblk[i] = 0;
2001 }
2002 sblk[0x52] = 0;
2003 if (fs_type)
2004 *fs_type = FS_MSDOS;
2005 if (fs_sub_type)
2006 *fs_sub_type = sblk[0x53];
2007 return sblk + 0x47;
2008 } else if (*sbp == 0 && (flags & GLM_MAYBE_NTFS) &&
2009 memcmp(sblk+3, "NTFS ", 8) == 0) {
2010 if (fs_type)
2011 *fs_type = FS_NTFS;
2012 if (fs_sub_type)
2013 *fs_sub_type = MBR_PTYPE_NTFS;
2014 /* XXX dig for volume name attribute ? */
2015 return "";
2016 }
2017
2018 if (!(flags & GLM_LIKELY_FFS))
2019 continue;
2020
2021 /* Maybe we should validate the checksum??? */
2022 switch (SB->fs_magic) {
2023 case FS_UFS1_MAGIC:
2024 case FS_UFS1_MAGIC_SWAPPED:
2025 if (!(SB->fs_old_flags & FS_FLAGS_UPDATED)) {
2026 if (*sbp == SBLOCK_UFS1)
2027 mnt = (const char *)SB->fs_fsmnt;
2028 } else {
2029 /* Check we have the main superblock */
2030 if (SB->fs_sblockloc == *sbp)
2031 mnt = (const char *)SB->fs_fsmnt;
2032 }
2033 if (fs_type)
2034 *fs_type = FS_BSDFFS;
2035 if (fs_sub_type)
2036 *fs_sub_type = 1;
2037 continue;
2038 case FS_UFS2_MAGIC:
2039 case FS_UFS2EA_MAGIC:
2040 case FS_UFS2_MAGIC_SWAPPED:
2041 case FS_UFS2EA_MAGIC_SWAPPED:
2042 /* Check we have the main superblock */
2043 if (SB->fs_sblockloc == *sbp) {
2044 mnt = (const char *)SB->fs_fsmnt;
2045 if (fs_type)
2046 *fs_type = FS_BSDFFS;
2047 if (fs_sub_type)
2048 *fs_sub_type = 2;
2049 }
2050 continue;
2051 }
2052 }
2053
2054 if (mnt == NULL)
2055 return "";
2056
2057 /* If sysinst mounted this last then strip prefix */
2058 len = strlen(targetroot_mnt);
2059 if (memcmp(mnt, targetroot_mnt, len) == 0) {
2060 if (mnt[len] == 0)
2061 return "/";
2062 if (mnt[len] == '/')
2063 return mnt + len;
2064 }
2065 return mnt;
2066 #undef SB
2067 }
2068
2069 /* Ask for a partition offset, check bounds and do the needed roundups */
2070 daddr_t
2071 getpartoff(struct disk_partitions *parts, daddr_t defpartstart)
2072 {
2073 char defstart[24], isize[24], maxpart, minspace, maxspace,
2074 *prompt, *label_msg, valid_parts[4], valid_spaces[4],
2075 space_prompt[1024], *head, *hint_part, *hint_space, *tail;
2076 size_t num_freespace, spaces, ndx;
2077 struct disk_part_free_space *freespace;
2078 daddr_t i, localsizemult, ptn_alignment, min, max;
2079 part_id partn;
2080 struct disk_part_info info;
2081 const char *errmsg = NULL;
2082
2083 min = parts->disk_start;
2084 max = min + parts->disk_size;
2085
2086 /* upper bound on the number of free spaces, plus some slope */
2087 num_freespace = parts->num_part * 2 + 5;
2088 freespace = calloc(num_freespace, sizeof(*freespace));
2089 if (freespace == NULL)
2090 return -1;
2091
2092 ptn_alignment = parts->pscheme->get_part_alignment(parts);
2093 spaces = parts->pscheme->get_free_spaces(parts, freespace,
2094 num_freespace, max(sizemult, ptn_alignment), ptn_alignment, -1,
2095 defpartstart);
2096
2097 maxpart = 'a' + parts->num_part -1;
2098 if (parts->num_part > 1) {
2099 snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpart);
2100 } else if (parts->num_part == 1) {
2101 snprintf(valid_parts, sizeof valid_parts, " %c", maxpart);
2102 } else {
2103 strcpy(valid_parts, "---");
2104 }
2105 if (spaces > 1) {
2106 minspace = maxpart + 1;
2107 maxspace = minspace + spaces -1;
2108 snprintf(valid_spaces, sizeof valid_spaces, "%c-%c", minspace,
2109 maxspace);
2110 } else if (spaces == 1) {
2111 maxspace = minspace = maxpart + 1;
2112 snprintf(valid_spaces, sizeof valid_spaces, " %c", minspace);
2113 } else {
2114 minspace = 0;
2115 maxspace = 0;
2116 strcpy(valid_spaces, "---");
2117 }
2118
2119 /* Add description of start/size to user prompt */
2120 const char *mstr = msg_string(MSG_free_space_line);
2121 space_prompt[0] = 0;
2122 for (ndx = 0; ndx < spaces; ndx++) {
2123 char str_start[40], str_end[40], str_size[40], str_tag[4];
2124
2125 sprintf(str_tag, "%c: ", minspace+(int)ndx);
2126 sprintf(str_start, "%" PRIu64, freespace[ndx].start / sizemult);
2127 sprintf(str_end, "%" PRIu64,
2128 (freespace[ndx].start + freespace[ndx].size) / sizemult);
2129 sprintf(str_size, "%" PRIu64, freespace[ndx].size / sizemult);
2130 const char *args[4] = { str_start, str_end, str_size,
2131 multname };
2132 char *line = str_arg_subst(mstr, 4, args);
2133 strlcat(space_prompt, str_tag, sizeof(space_prompt));
2134 size_t len = strlcat(space_prompt, line, sizeof(space_prompt));
2135 free (line);
2136 if (len >= sizeof space_prompt)
2137 break;
2138 }
2139
2140 const char *args[] = { valid_parts, valid_spaces, multname };
2141 hint_part = NULL;
2142 hint_space = NULL;
2143 head = str_arg_subst(msg_string(MSG_label_offset_head),
2144 __arraycount(args), args);
2145 if (parts->num_part)
2146 hint_part = str_arg_subst(msg_string(
2147 MSG_label_offset_part_hint), __arraycount(args), args);
2148 if (spaces)
2149 hint_space = str_arg_subst(msg_string(
2150 MSG_label_offset_space_hint), __arraycount(args), args);
2151 tail = str_arg_subst(msg_string(MSG_label_offset_tail),
2152 __arraycount(args), args);
2153
2154 if (hint_part && hint_space)
2155 asprintf(&label_msg, "%s\n%s\n%s\n\n%s\n%s",
2156 head, hint_part, hint_space, space_prompt, tail);
2157 else if (hint_part)
2158 asprintf(&label_msg, "%s\n%s\n\n%s",
2159 head, hint_part, tail);
2160 else if (hint_space)
2161 asprintf(&label_msg, "%s\n%s\n\n%s\n%s",
2162 head, hint_space, space_prompt, tail);
2163 else
2164 asprintf(&label_msg, "%s\n\n%s",
2165 head, tail);
2166 free(head); free(hint_part); free(hint_space); free(tail);
2167
2168 localsizemult = sizemult;
2169 errmsg = NULL;
2170 for (;;) {
2171 snprintf(defstart, sizeof defstart, "%" PRIu64,
2172 defpartstart/sizemult);
2173 if (errmsg != NULL && errmsg[0] != 0)
2174 asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
2175 else
2176 prompt = label_msg;
2177 msg_prompt_win(prompt, -1, 13, 70, -1,
2178 (defpartstart > 0) ? defstart : NULL, isize, sizeof isize);
2179 if (label_msg != prompt)
2180 free(prompt);
2181 if (strcmp(defstart, isize) == 0) {
2182 /* Don't do rounding if default accepted */
2183 i = defpartstart;
2184 break;
2185 }
2186 if (isize[1] == '\0' && isize[0] >= 'a' &&
2187 isize[0] <= maxpart) {
2188 partn = isize[0] - 'a';
2189 if (parts->pscheme->get_part_info(parts, partn,
2190 &info)) {
2191 i = info.start + info.size;
2192 localsizemult = 1;
2193 } else {
2194 errmsg = msg_string(MSG_invalid_sector_number);
2195 continue;
2196 }
2197 } else if (isize[1] == '\0' && isize[0] >= minspace &&
2198 isize[0] <= maxspace) {
2199 ndx = isize[0] - minspace;
2200 i = freespace[ndx].start;
2201 localsizemult = 1;
2202 } else if (atoi(isize) == -1) {
2203 i = min;
2204 localsizemult = 1;
2205 } else {
2206 i = parse_disk_pos(isize, &localsizemult,
2207 parts->bytes_per_sector,
2208 parts->pscheme->get_cylinder_size(parts), NULL);
2209 if (i < 0) {
2210 errmsg = msg_string(MSG_invalid_sector_number);
2211 continue;
2212 }
2213 }
2214 /* round to cylinder size if localsizemult != 1 */
2215 int cylsize = parts->pscheme->get_cylinder_size(parts);
2216 i = NUMSEC(i, localsizemult, cylsize);
2217 /* Adjust to start of slice if needed */
2218 if ((i < min && (min - i) < localsizemult) ||
2219 (i > min && (i - min) < localsizemult)) {
2220 i = min;
2221 }
2222 if (max == 0 || i <= max)
2223 break;
2224 errmsg = msg_string(MSG_startoutsidedisk);
2225 }
2226 free(label_msg);
2227 free(freespace);
2228
2229 return i;
2230 }
2231
2232
2233 /* Ask for a partition size, check bounds and do the needed roundups */
2234 daddr_t
2235 getpartsize(struct disk_partitions *parts, daddr_t orig_start,
2236 daddr_t partstart, daddr_t dflt)
2237 {
2238 char dsize[24], isize[24], max_size[24], maxpartc, valid_parts[4],
2239 *label_msg, *prompt, *head, *hint, *tail;
2240 const char *errmsg = NULL;
2241 daddr_t i, partend, diskend, localsizemult, max, max_r, dflt_r;
2242 struct disk_part_info info;
2243 part_id partn;
2244
2245 diskend = parts->disk_start + parts->disk_size;
2246 max = parts->pscheme->max_free_space_at(parts, orig_start);
2247 max += orig_start - partstart;
2248 if (sizemult == 1)
2249 max--; /* with hugher scale proper rounding later will be ok */
2250
2251 /* We need to keep both the unrounded and rounded (_r) max and dflt */
2252 dflt_r = (partstart + dflt) / sizemult - partstart / sizemult;
2253 if (max == dflt)
2254 max_r = dflt_r;
2255 else
2256 max_r = max / sizemult;
2257 /* the partition may have been moved and now not fit any longer */
2258 if (dflt > max)
2259 dflt = max;
2260 if (dflt_r > max_r)
2261 dflt_r = max_r;
2262
2263 snprintf(max_size, sizeof max_size, "%" PRIu64, max_r);
2264
2265 maxpartc = 'a' + parts->num_part -1;
2266 if (parts->num_part > 1) {
2267 snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpartc);
2268 } else if (parts->num_part == 1) {
2269 snprintf(valid_parts, sizeof valid_parts, " %c", maxpartc);
2270 } else {
2271 strcpy(valid_parts, "---");
2272 }
2273
2274 const char *args[] = { valid_parts, max_size, multname };
2275 hint = NULL;
2276 head = str_arg_subst(msg_string(MSG_label_size_head),
2277 __arraycount(args), args);
2278 if (parts->num_part)
2279 hint = str_arg_subst(msg_string(MSG_label_size_part_hint),
2280 __arraycount(args), args);
2281 tail = str_arg_subst(msg_string(MSG_label_size_tail),
2282 __arraycount(args), args);
2283
2284 if (hint != NULL)
2285 asprintf(&label_msg, "%s\n%s\n\n%s", head, hint, tail);
2286 else
2287 asprintf(&label_msg, "%s\n\n%s", head, tail);
2288 free(head); free(hint); free(tail);
2289
2290 localsizemult = sizemult;
2291 i = -1;
2292 for (;;) {
2293 snprintf(dsize, sizeof dsize, "%" PRIu64, dflt_r);
2294
2295 if (errmsg != NULL && errmsg[0] != 0)
2296 asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
2297 else
2298 prompt = label_msg;
2299 msg_prompt_win(prompt, -1, 12, 70, -1,
2300 (dflt != 0) ? dsize : 0, isize, sizeof isize);
2301 if (prompt != label_msg)
2302 free(prompt);
2303
2304 if (strcmp(isize, dsize) == 0) {
2305 free(label_msg);
2306 return dflt;
2307 }
2308 if (parts->num_part && isize[1] == '\0' && isize[0] >= 'a' &&
2309 isize[0] <= maxpartc) {
2310 partn = isize[0] - 'a';
2311 if (parts->pscheme->get_part_info(parts, partn,
2312 &info)) {
2313 i = info.start - partstart -1;
2314 localsizemult = 1;
2315 max_r = max;
2316 }
2317 } else if (atoi(isize) == -1) {
2318 i = max;
2319 localsizemult = 1;
2320 max_r = max;
2321 } else {
2322 i = parse_disk_pos(isize, &localsizemult,
2323 parts->bytes_per_sector,
2324 parts->pscheme->get_cylinder_size(parts), NULL);
2325 if (localsizemult != sizemult)
2326 max_r = max;
2327 }
2328 if (i < 0) {
2329 errmsg = msg_string(MSG_Invalid_numeric);
2330 continue;
2331 } else if (i > max_r) {
2332 errmsg = msg_string(MSG_Too_large);
2333 continue;
2334 }
2335 /*
2336 * partend is aligned to a cylinder if localsizemult
2337 * is not 1 sector
2338 */
2339 int cylsize = parts->pscheme->get_cylinder_size(parts);
2340 partend = NUMSEC((partstart + i*localsizemult) / localsizemult,
2341 localsizemult, cylsize);
2342 /* Align to end-of-disk or end-of-slice if close enough */
2343 if (partend > (diskend - sizemult)
2344 && partend < (diskend + sizemult))
2345 partend = diskend;
2346 if (partend > (partstart + max - sizemult)
2347 && partend < (partstart + max + sizemult))
2348 partend = partstart + max;
2349 /* sanity checks */
2350 if (partend > diskend) {
2351 partend = diskend;
2352 errmsg = msg_string(MSG_endoutsidedisk);
2353 continue;
2354 }
2355 free(label_msg);
2356 if (partend < partstart)
2357 return 0;
2358 return (partend - partstart);
2359 }
2360 /* NOTREACHED */
2361 }
2362
2363 /*
2364 * convert a string to a number of sectors, with a possible unit
2365 * 150M = 150 Megabytes
2366 * 2000c = 2000 cylinders
2367 * 150256s = 150256 sectors
2368 * Without units, use the default (sizemult).
2369 * returns the raw input value, and the unit used. Caller needs to multiply!
2370 * On invalid inputs, returns -1.
2371 */
2372 daddr_t
2373 parse_disk_pos(
2374 const char *str,
2375 daddr_t *localsizemult,
2376 daddr_t bps,
2377 daddr_t cyl_size,
2378 bool *extend_this)
2379 {
2380 daddr_t val;
2381 char *cp;
2382 bool mult_found;
2383
2384 if (str[0] == '\0') {
2385 return -1;
2386 }
2387 val = strtoull(str, &cp, 10);
2388 mult_found = false;
2389 if (extend_this)
2390 *extend_this = false;
2391 while (*cp != 0) {
2392 if (*cp == 'G' || *cp == 'g') {
2393 if (mult_found)
2394 return -1;
2395 *localsizemult = GIG / bps;
2396 goto next;
2397 }
2398 if (*cp == 'M' || *cp == 'm') {
2399 if (mult_found)
2400 return -1;
2401 *localsizemult = MEG / bps;
2402 goto next;
2403 }
2404 if (*cp == 'c' || *cp == 'C') {
2405 if (mult_found)
2406 return -1;
2407 *localsizemult = cyl_size;
2408 goto next;
2409 }
2410 if (*cp == 's' || *cp == 'S') {
2411 if (mult_found)
2412 return -1;
2413 *localsizemult = 1;
2414 goto next;
2415 }
2416 if (*cp == '+' && extend_this) {
2417 *extend_this = true;
2418 cp++;
2419 break;
2420 }
2421
2422 /* not a known unit */
2423 return -1;
2424
2425 next:
2426 mult_found = true;
2427 cp++;
2428 continue;
2429 }
2430 if (*cp != 0)
2431 return -1;
2432
2433 return val;
2434 }
2435