label.c revision 1.8 1 /* $NetBSD: label.c,v 1.8 2019/06/22 20:46:07 christos 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.8 2019/06/22 20:46:07 christos 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 <ufs/ffs/fs.h>
54
55 #include "defs.h"
56 #include "msg_defs.h"
57 #include "menu_defs.h"
58
59 /*
60 * local prototypes
61 */
62 static bool boringpart(const struct disk_part_info *info);
63 static bool checklabel(struct disk_partitions*, char[MENUSTRSIZE],
64 char[MENUSTRSIZE]);
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 0 if no overlapping partition found, nonzero otherwise.
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[MENUSTRSIZE], char ovl2[MENUSTRSIZE])
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 for (i = 0; i < parts->num_part - 1; i ++ ) {
118 if (!parts->pscheme->get_part_info(parts, i, &info))
119 continue;
120
121 /* skip unused or reserved partitions */
122 if (boringpart(&info))
123 continue;
124
125 /*
126 * check succeeding partitions for overlap.
127 * O(n^2), but n is small (currently <= 16).
128 */
129 istart = info.start;
130 iend = istart + info.size;
131 fs_type = info.fs_type;
132 fs_sub_type = info.fs_sub_type;
133
134 for (j = i+1; j < parts->num_part; j++) {
135
136 if (!parts->pscheme->get_part_info(parts, j, &info))
137 continue;
138
139 /* skip unused or reserved partitions */
140 if (boringpart(&info))
141 continue;
142
143 jstart = info.start;
144 jend = jstart + info.size;
145
146 /* overlap? */
147 if ((istart <= jstart && jstart < iend) ||
148 (jstart <= istart && istart < jend)) {
149 snprintf(ovl1, sizeof(*ovl1),
150 "%" PRIu64 " - %" PRIu64 " %s, %s",
151 istart / sizemult, iend / sizemult,
152 multname,
153 getfslabelname(fs_type, fs_sub_type));
154 snprintf(ovl2, sizeof(*ovl2),
155 "%" PRIu64 " - %" PRIu64 " %s, %s",
156 jstart / sizemult, jend / sizemult,
157 multname,
158 getfslabelname(info.fs_type,
159 info.fs_sub_type));
160 return false;
161 }
162 }
163 }
164
165 return true;
166 }
167
168 int
169 checkoverlap(struct disk_partitions *parts)
170 {
171 char desc1[MENUSTRSIZE], desc2[MENUSTRSIZE];
172 if (!checklabel(parts, desc1, desc2)) {
173 msg_display_subst(MSG_partitions_overlap, 2, desc1, desc2);
174 return 1;
175 }
176 return 0;
177 }
178
179 /*
180 * return (see post_edit_verify):
181 * 0 -> abort
182 * 1 -> re-edit
183 * 2 -> continue installation
184 */
185 static int
186 verify_parts(struct partition_usage_set *pset)
187 {
188 struct part_usage_info *wanted;
189 struct disk_partitions *parts;
190 size_t i, num_root;
191 daddr_t first_bsdstart, first_bsdsize, inst_start, inst_size;
192 int rv;
193
194 first_bsdstart = first_bsdsize = 0;
195 inst_start = inst_size = 0;
196 num_root = 0;
197 parts = pset->parts;
198 for (i = 0; i < pset->num; i++) {
199 wanted = &pset->infos[i];
200
201 if (wanted->flags & PUIFLG_JUST_MOUNTPOINT)
202 continue;
203 if (wanted->cur_part_id == NO_PART)
204 continue;
205 if (!(wanted->instflags & PUIINST_MOUNT))
206 continue;
207 if (strcmp(wanted->mount, "/") != 0)
208 continue;
209 num_root++;
210
211 if (first_bsdstart == 0) {
212 first_bsdstart = wanted->cur_start;
213 first_bsdsize = wanted->size;
214 }
215 if (inst_start == 0 && wanted->cur_start == pm->ptstart) {
216 inst_start = wanted->cur_start;
217 inst_size = wanted->size;
218 }
219 }
220
221 if (num_root == 0 ||
222 (num_root > 1 && inst_start == 0)) {
223 if (num_root == 0)
224 msg_display_subst(MSG_must_be_one_root, 2,
225 msg_string(parts->pscheme->name),
226 msg_string(parts->pscheme->short_name));
227 else
228 msg_display_subst(MSG_multbsdpart, 2,
229 msg_string(parts->pscheme->name),
230 msg_string(parts->pscheme->short_name));
231 rv = ask_reedit(parts);
232 if (rv != 2)
233 return rv;
234 }
235
236 if (pm->ptstart == 0) {
237 if (inst_start > 0) {
238 pm->ptstart = inst_start;
239 pm->ptsize = inst_size;
240 } else if (first_bsdstart > 0) {
241 pm->ptstart = first_bsdstart;
242 pm->ptsize = first_bsdsize;
243 } else if (parts->pscheme->guess_install_target &&
244 parts->pscheme->guess_install_target(
245 parts, &inst_start, &inst_size)) {
246 pm->ptstart = inst_start;
247 pm->ptsize = inst_size;
248 }
249 }
250
251 /* Check for overlaps */
252 if (checkoverlap(parts) != 0) {
253 rv = ask_reedit(parts);
254 if (rv != 2)
255 return rv;
256 }
257
258 /*
259 * post_edit_verify returns:
260 * 0 -> abort
261 * 1 -> re-edit
262 * 2 -> continue installation
263 */
264 if (parts->pscheme->post_edit_verify)
265 return parts->pscheme->post_edit_verify(parts, false);
266
267 return 2;
268 }
269
270 static int
271 edit_fs_start(menudesc *m, void *arg)
272 {
273 struct single_part_fs_edit *edit = arg;
274 daddr_t start, end;
275
276 start = getpartoff(edit->pset->parts, edit->info.start);
277 if (edit->info.size != 0) {
278 /* Try to keep end in the same place */
279 end = edit->info.start + edit->info.size;
280 if (end < start)
281 edit->info.size = edit->pset->parts->pscheme->
282 max_free_space_at(edit->pset->parts,
283 edit->info.start);
284 else
285 edit->info.size = end - start;
286 }
287 edit->info.start = start;
288 return 0;
289 }
290
291 static int
292 edit_fs_size(menudesc *m, void *arg)
293 {
294 struct single_part_fs_edit *edit = arg;
295 daddr_t size;
296
297 size = getpartsize(edit->pset->parts, edit->info.start,
298 edit->info.size);
299 if (size < 0)
300 return 0;
301 if (size > edit->pset->parts->disk_size)
302 size = edit->pset->parts->disk_size - edit->info.start;
303 edit->info.size = size;
304 return 0;
305 }
306
307 static int
308 edit_fs_preserve(menudesc *m, void *arg)
309 {
310 struct single_part_fs_edit *edit = arg;
311
312 edit->wanted->instflags ^= PUIINST_NEWFS;
313 return 0;
314 }
315
316 static int
317 edit_install(menudesc *m, void *arg)
318 {
319 struct single_part_fs_edit *edit = arg;
320
321 if (edit->info.start == pm->ptstart)
322 pm->ptstart = 0;
323 else
324 pm->ptstart = edit->info.start;
325 return 0;
326 }
327
328 static int
329 edit_fs_mount(menudesc *m, void *arg)
330 {
331 struct single_part_fs_edit *edit = arg;
332
333 edit->wanted->instflags ^= PUIINST_MOUNT;
334 return 0;
335 }
336
337 static int
338 edit_fs_mountpt(menudesc *m, void *arg)
339 {
340 struct single_part_fs_edit *edit = arg;
341 char *p, *first, *last, buf[MOUNTLEN];
342
343 strlcpy(buf, edit->wanted->mount, sizeof buf);
344 msg_prompt_win(MSG_mountpoint, -1, 18, 0, 0,
345 buf, buf, MOUNTLEN);
346
347 /*
348 * Trim all leading and trailing whitespace
349 */
350 for (first = NULL, last = NULL, p = buf; *p; p++) {
351 if (isspace((unsigned char)*p))
352 continue;
353 if (first == NULL)
354 first = p;
355 last = p;
356 }
357 if (last != NULL)
358 last[1] = 0;
359
360 if (*first == 0 || strcmp(first, "none") == 0) {
361 edit->wanted->mount[0] = 0;
362 edit->wanted->instflags &= ~PUIINST_MOUNT;
363 return 0;
364 }
365
366 if (*first != '/') {
367 edit->wanted->mount[0] = '/';
368 strlcpy(&edit->wanted->mount[1], first,
369 sizeof(edit->wanted->mount)-1);
370 } else {
371 strlcpy(edit->wanted->mount, first, sizeof edit->wanted->mount);
372 }
373
374 return 0;
375 }
376
377 static int
378 edit_restore(menudesc *m, void *arg)
379 {
380 struct single_part_fs_edit *edit = arg;
381
382 edit->info = edit->old_info;
383 *edit->wanted = edit->old_usage;
384 return 0;
385 }
386
387 static int
388 edit_cancel(menudesc *m, void *arg)
389 {
390 struct single_part_fs_edit *edit = arg;
391
392 edit->rv = -1;
393 return 1;
394 }
395
396 static int
397 edit_delete_ptn(menudesc *m, void *arg)
398 {
399 struct single_part_fs_edit *edit = arg;
400
401 edit->rv = -2;
402 return 1;
403 }
404
405 /*
406 * We have added/removed partitions, all cur_part_id values are
407 * out of sync. Re-fetch and reorder partitions accordingly.
408 */
409 static void
410 renumber_partitions(struct partition_usage_set *pset)
411 {
412 struct part_usage_info *ninfos;
413 struct disk_part_info info;
414 size_t i;
415 part_id pno;
416
417 ninfos = calloc(pset->parts->num_part, sizeof(*ninfos));
418 if (ninfos == NULL) {
419 err_msg_win(err_outofmem);
420 return;
421 }
422
423 for (pno = 0; pno < pset->parts->num_part; pno++) {
424 if (!pset->parts->pscheme->get_part_info(pset->parts, pno,
425 &info))
426 continue;
427 for (i = 0; i < pset->parts->num_part; i++) {
428 if (pset->infos[i].cur_start != info.start)
429 continue;
430 memcpy(&ninfos[pno], &pset->infos[i],
431 sizeof(ninfos[pno]));
432 ninfos[pno].cur_part_id = pno;
433 break;
434 }
435 }
436
437 memcpy(pset->infos, ninfos, sizeof(*pset->infos)*pset->parts->num_part);
438 free(ninfos);
439 }
440
441 /*
442 * Most often used file system types, we offer them in a first level menu.
443 */
444 static const uint edit_fs_common_types[] =
445 { FS_BSDFFS, FS_SWAP, FS_MSDOS, FS_BSDLFS, FS_EX2FS };
446
447 /*
448 * Functions for uncommon file system types - we offer the full list,
449 * but put FFSv2 and FFSv1 at the front.
450 */
451 static void
452 init_fs_type_ext(menudesc *menu, void *arg)
453 {
454 struct single_part_fs_edit *edit = arg;
455 uint t = edit->info.fs_type;
456 size_t i, ndx, max = menu->numopts;
457
458 if (t == FS_BSDFFS) {
459 if (edit->info.fs_sub_type == 2)
460 menu->cursel = 0;
461 else
462 menu->cursel = 1;
463 return;
464 }
465 /* skip the two FFS entries, and do not add FFS later again */
466 for (ndx = 2, i = 0; i < FSMAXTYPES && ndx < max; i++) {
467 if (i == FS_UNUSED)
468 continue;
469 if (i == FS_BSDFFS)
470 continue;
471 if (fstypenames[i] == NULL)
472 continue;
473
474 if (i == t) {
475 menu->cursel = ndx;
476 break;
477 }
478 ndx++;
479 }
480 }
481
482 static int
483 set_fstype_ext(menudesc *menu, void *arg)
484 {
485 struct single_part_fs_edit *edit = arg;
486 size_t i, ndx, max = menu->numopts;
487
488 if (menu->cursel == 0 || menu->cursel == 1) {
489 edit->info.fs_type = FS_BSDFFS;
490 edit->info.fs_sub_type = menu->cursel == 0 ? 2 : 1;
491 edit->info.nat_type = edit->pset->parts->pscheme->
492 get_fs_part_type(edit->info.fs_type,
493 edit->info.fs_sub_type);
494 edit->wanted->type = edit->info.nat_type->generic_ptype;
495 edit->wanted->fs_type = edit->info.fs_type;
496 edit->wanted->fs_version = edit->info.fs_sub_type;
497 return 1;
498 }
499
500 for (ndx = 2, i = 0; i < FSMAXTYPES && ndx < max; i++) {
501 if (i == FS_UNUSED)
502 continue;
503 if (i == FS_BSDFFS)
504 continue;
505 if (fstypenames[i] == NULL)
506 continue;
507
508 if (ndx == (size_t)menu->cursel) {
509 edit->info.fs_type = i;
510 edit->info.fs_sub_type = 0;
511 edit->info.nat_type = edit->pset->parts->pscheme->
512 get_fs_part_type(i, 0);
513 if (edit->info.nat_type == NULL)
514 edit->info.nat_type = edit->pset->parts->
515 pscheme->get_generic_part_type(PT_root);
516 edit->wanted->type = edit->info.nat_type->generic_ptype;
517 edit->wanted->fs_type = edit->info.fs_type;
518 edit->wanted->fs_version = edit->info.fs_sub_type;
519 break;
520 }
521 ndx++;
522 }
523 return 1;
524 }
525
526 /*
527 * Offer a menu with "exotic" file system types, start with FFSv2 and FFSv1,
528 * skip later FFS entry in the generic list.
529 */
530 static int
531 edit_fs_type_ext(menudesc *menu, void *arg)
532 {
533 menu_ent *opts;
534 int m;
535 size_t i, ndx, cnt;
536
537 cnt = __arraycount(fstypenames)-1;
538 opts = calloc(cnt, sizeof(*opts));
539 if (opts == NULL)
540 return 1;
541
542 ndx = 0;
543 opts[ndx].opt_name = msg_string(MSG_fs_type_ffsv2);
544 opts[ndx].opt_action = set_fstype_ext;
545 ndx++;
546 opts[ndx].opt_name = msg_string(MSG_fs_type_ffs);
547 opts[ndx].opt_action = set_fstype_ext;
548 ndx++;
549 for (i = 0; i < FSMAXTYPES && ndx < cnt; i++) {
550 if (i == FS_UNUSED)
551 continue;
552 if (i == FS_BSDFFS)
553 continue;
554 if (fstypenames[i] == NULL)
555 continue;
556 opts[ndx].opt_name = fstypenames[i];
557 opts[ndx].opt_action = set_fstype_ext;
558 ndx++;
559 }
560 assert(ndx == cnt);
561 m = new_menu(MSG_Select_the_type, opts, ndx,
562 30, 6, 10, 0, MC_SUBMENU | MC_SCROLL,
563 init_fs_type_ext, NULL, NULL, NULL, MSG_unchanged);
564
565 if (m < 0)
566 return 1;
567 process_menu(m, arg);
568 free_menu(m);
569 free(opts);
570
571 return 1;
572 }
573
574 static void
575 init_fs_type(menudesc *menu, void *arg)
576 {
577 struct single_part_fs_edit *edit = arg;
578 size_t i;
579
580 /* init menu->cursel from fs type in arg */
581 if (edit->info.fs_type == FS_BSDFFS) {
582 if (edit->info.fs_sub_type == 2)
583 menu->cursel = 0;
584 else
585 menu->cursel = 1;
586 }
587 for (i = 1; i < __arraycount(edit_fs_common_types); i++) {
588 if (edit->info.fs_type == edit_fs_common_types[i]) {
589 menu->cursel = i+1;
590 break;
591 }
592 }
593 }
594
595 static int
596 set_fstype(menudesc *menu, void *arg)
597 {
598 struct single_part_fs_edit *edit = arg;
599 int ndx;
600
601 if (menu->cursel < 2) {
602 edit->info.fs_type = FS_BSDFFS;
603 edit->info.fs_sub_type = menu->cursel == 0 ? 2 : 1;
604 edit->info.nat_type = edit->pset->parts->pscheme->
605 get_fs_part_type(FS_BSDFFS, 2);
606 if (edit->info.nat_type == NULL)
607 edit->info.nat_type = edit->pset->parts->
608 pscheme->get_generic_part_type(PT_root);
609 edit->wanted->type = edit->info.nat_type->generic_ptype;
610 edit->wanted->fs_type = edit->info.fs_type;
611 edit->wanted->fs_version = edit->info.fs_sub_type;
612 return 1;
613 }
614 ndx = menu->cursel-1;
615
616 if (ndx < 0 ||
617 (size_t)ndx >= __arraycount(edit_fs_common_types))
618 return 1;
619
620 edit->info.fs_type = edit_fs_common_types[ndx];
621 edit->info.fs_sub_type = 0;
622 edit->info.nat_type = edit->pset->parts->pscheme->
623 get_fs_part_type(edit->info.fs_type, 0);
624 if (edit->info.nat_type == NULL)
625 edit->info.nat_type = edit->pset->parts->
626 pscheme->get_generic_part_type(PT_root);
627 edit->wanted->type = edit->info.nat_type->generic_ptype;
628 edit->wanted->fs_type = edit->info.fs_type;
629 edit->wanted->fs_version = edit->info.fs_sub_type;
630 return 1;
631 }
632
633 /*
634 * Offer a menu selecting the common file system types
635 */
636 static int
637 edit_fs_type(menudesc *menu, void *arg)
638 {
639 struct single_part_fs_edit *edit = arg;
640 menu_ent *opts;
641 int m, cnt;
642 size_t i;
643
644 /*
645 * Shortcut to full menu if we have an exotic value
646 */
647 for (i = 0; i < __arraycount(edit_fs_common_types); i++)
648 if (edit->info.fs_type == edit_fs_common_types[i])
649 break;
650 if (i >= __arraycount(edit_fs_common_types)) {
651 edit_fs_type_ext(menu, arg);
652 return 0;
653 }
654
655 /*
656 * Starting with a common type, show short menu first
657 */
658 cnt = __arraycount(edit_fs_common_types) + 2;
659 opts = calloc(cnt, sizeof(*opts));
660 if (opts == NULL)
661 return 0;
662
663 /* special case entry 0: two FFS entries */
664 for (i = 0; i < __arraycount(edit_fs_common_types); i++) {
665 opts[i+1].opt_name = getfslabelname(edit_fs_common_types[i], 0);
666 opts[i+1].opt_action = set_fstype;
667 }
668 /* duplicate FFS (at offset 1) into first entry */
669 opts[0] = opts[1];
670 opts[0].opt_name = msg_string(MSG_fs_type_ffsv2);
671 opts[1].opt_name = msg_string(MSG_fs_type_ffs);
672 /* add secondary sub-menu */
673 assert(i+1 < (size_t)cnt);
674 opts[i+1].opt_name = msg_string(MSG_other_fs_type);
675 opts[i+1].opt_action = edit_fs_type_ext;
676
677 m = new_menu(MSG_Select_the_type, opts, cnt,
678 30, 6, 0, 0, MC_SUBMENU | MC_SCROLL,
679 init_fs_type, NULL, NULL, NULL, MSG_unchanged);
680
681 if (m < 0)
682 return 0;
683 process_menu(m, arg);
684 free_menu(m);
685 free(opts);
686
687 return 0;
688 }
689
690
691 static void update_edit_ptn_menu(menudesc *m, void *arg);
692 static void draw_edit_ptn_line(menudesc *m, int opt, void *arg);
693 static int edit_ptn_custom_type(menudesc *m, void *arg);
694
695 int
696 edit_ptn(menudesc *menu, void *arg)
697 {
698 struct partition_usage_set *pset = arg;
699 struct single_part_fs_edit edit;
700 int fspart_menu, num_opts;
701 const char *err;
702 menu_ent *mopts, *popt;
703 bool is_new_part, with_inst_opt = pset->parts->parent == NULL;
704
705 static const menu_ent edit_ptn_fields_head[] = {
706 { .opt_action=edit_fs_type },
707 { .opt_action=edit_fs_start },
708 { .opt_action=edit_fs_size },
709 { .opt_flags=OPT_IGNORE },
710 };
711
712 static const menu_ent edit_ptn_fields_head_add[] = {
713 { .opt_action=edit_install },
714 };
715
716 static const menu_ent edit_ptn_fields_head2[] = {
717 { .opt_action=edit_fs_preserve },
718 { .opt_action=edit_fs_mount },
719 { .opt_menu=MENU_mountoptions, .opt_flags=OPT_SUB },
720 { .opt_action=edit_fs_mountpt },
721 };
722 static const menu_ent edit_ptn_fields_tail[] = {
723 { .opt_name=MSG_askunits, .opt_menu=MENU_sizechoice,
724 .opt_flags=OPT_SUB },
725 { .opt_name=MSG_restore,
726 .opt_action=edit_restore},
727 { .opt_name=MSG_Delete_partition,
728 .opt_action=edit_delete_ptn},
729 { .opt_name=MSG_cancel,
730 .opt_action=edit_cancel},
731 };
732
733 memset(&edit, 0, sizeof edit);
734 edit.pset = pset;
735 edit.index = menu->cursel;
736 edit.wanted = &pset->infos[edit.index];
737
738 if (menu->cursel < 0 || (size_t)menu->cursel > pset->parts->num_part)
739 return 0;
740 is_new_part = (size_t)menu->cursel == pset->parts->num_part;
741
742 num_opts = __arraycount(edit_ptn_fields_head) +
743 __arraycount(edit_ptn_fields_head2) +
744 __arraycount(edit_ptn_fields_tail);
745 if (with_inst_opt)
746 num_opts += __arraycount(edit_ptn_fields_head_add);
747 if (is_new_part)
748 num_opts--;
749 else
750 num_opts += pset->parts->pscheme->custom_attribute_count;
751
752 mopts = calloc(num_opts, sizeof(*mopts));
753 if (mopts == NULL) {
754 err_msg_win(err_outofmem);
755 return 0;
756 }
757 memcpy(mopts, edit_ptn_fields_head, sizeof(edit_ptn_fields_head));
758 popt = mopts + __arraycount(edit_ptn_fields_head);
759 if (with_inst_opt) {
760 memcpy(popt, edit_ptn_fields_head_add,
761 sizeof(edit_ptn_fields_head_add));
762 popt += __arraycount(edit_ptn_fields_head_add);
763 }
764 memcpy(popt, edit_ptn_fields_head2, sizeof(edit_ptn_fields_head2));
765 popt += __arraycount(edit_ptn_fields_head2);
766 edit.first_custom_attr = popt - mopts;
767 if (!is_new_part) {
768 for (size_t i = 0;
769 i < pset->parts->pscheme->custom_attribute_count;
770 i++, popt++) {
771 popt->opt_action = edit_ptn_custom_type;
772 }
773 }
774 memcpy(popt, edit_ptn_fields_tail, sizeof(edit_ptn_fields_tail));
775 popt += __arraycount(edit_ptn_fields_tail) - 1;
776 if (is_new_part)
777 memcpy(popt-1, popt, sizeof(*popt));
778
779 if (is_new_part) {
780 struct disk_part_free_space space;
781 daddr_t align = pset->parts->pscheme->get_part_alignment(
782 pset->parts);
783
784 edit.id = NO_PART;
785 if (pset->parts->pscheme->get_free_spaces(pset->parts,
786 &space, 1, align, align, -1, -1) == 1) {
787 edit.info.start = space.start;
788 edit.info.size = space.size;
789 edit.info.fs_type = FS_BSDFFS;
790 edit.info.fs_sub_type = 2;
791 edit.info.nat_type = pset->parts->pscheme->
792 get_fs_part_type(edit.info.fs_type,
793 edit.info.fs_sub_type);
794 edit.wanted->instflags = PUIINST_NEWFS;
795 }
796 } else {
797 edit.id = pset->infos[edit.index].cur_part_id;
798 if (!pset->parts->pscheme->get_part_info(pset->parts, edit.id,
799 &edit.info)) {
800 free(mopts);
801 return 0;
802 }
803 }
804
805 edit.old_usage = *edit.wanted;
806 edit.old_info = edit.info;
807
808 fspart_menu = new_menu(MSG_edfspart, mopts, num_opts,
809 15, 2, 0, 55, MC_NOCLEAR | MC_SCROLL,
810 update_edit_ptn_menu, draw_edit_ptn_line, NULL,
811 NULL, MSG_OK);
812
813 process_menu(fspart_menu, &edit);
814 free(mopts);
815 free_menu(fspart_menu);
816
817 if (edit.rv == 0) { /* OK, set new data */
818 edit.info.last_mounted = edit.wanted->mount;
819 if (is_new_part) {
820 edit.wanted->cur_part_id = pset->parts->pscheme->
821 add_partition(pset->parts, &edit.info, &err);
822 if (edit.wanted->cur_part_id == NO_PART)
823 err_msg_win(err);
824 else {
825 pset->parts->pscheme->get_part_info(
826 pset->parts, edit.wanted->cur_part_id,
827 &edit.info);
828 edit.wanted->cur_start = edit.info.start;
829 edit.wanted->size = edit.info.size;
830 edit.wanted->type =
831 edit.info.nat_type->generic_ptype;
832 edit.wanted->fs_type = edit.info.fs_type;
833 edit.wanted->fs_version = edit.info.fs_sub_type;
834 /* things have changed, re-sort */
835 renumber_partitions(pset);
836 }
837 } else {
838 if (!pset->parts->pscheme->set_part_info(pset->parts,
839 edit.id, &edit.info, &err))
840 err_msg_win(err);
841 }
842
843 /*
844 * if size has changed, we may need to add or remove
845 * the option to add partitions
846 */
847 show_partition_adder(menu, pset);
848 } else if (edit.rv == -1) { /* cancel edit */
849 if (is_new_part) {
850 memmove(pset->infos+edit.index,
851 pset->infos+edit.index+1,
852 sizeof(*pset->infos)*(pset->num-edit.index));
853 memmove(menu->opts+edit.index,
854 menu->opts+edit.index+1,
855 sizeof(*menu->opts)*(menu->numopts-edit.index));
856 menu->numopts--;
857 menu->cursel = 0;
858 pset->num--;
859 return -1;
860 }
861 pset->infos[edit.index] = edit.old_usage;
862 } else if (!is_new_part && edit.rv == -2) { /* delete partition */
863 if (!pset->parts->pscheme->delete_partition(pset->parts,
864 edit.id, &err)) {
865 err_msg_win(err);
866 return 0;
867 }
868 memmove(pset->infos+edit.index,
869 pset->infos+edit.index+1,
870 sizeof(*pset->infos)*(pset->num-edit.index));
871 memmove(menu->opts+edit.index,
872 menu->opts+edit.index+1,
873 sizeof(*menu->opts)*(menu->numopts-edit.index));
874 menu->numopts--;
875 menu->cursel = 0;
876
877 /* things have changed, re-sort */
878 pset->num--;
879 renumber_partitions(pset);
880
881 /* we can likely add new partitions now */
882 show_partition_adder(menu, pset);
883
884 return -1;
885 }
886
887 return 0;
888 }
889
890 static void
891 update_edit_ptn_menu(menudesc *m, void *arg)
892 {
893 struct single_part_fs_edit *edit = arg;
894 int i;
895 uint t = edit->info.fs_type;
896 size_t attr_no;
897
898 /* Determine which of the properties can be changed */
899 for (i = 0; i < m->numopts; i++) {
900 if (m->opts[i].opt_action == NULL &&
901 m->opts[i].opt_menu != MENU_mountoptions)
902 continue;
903
904 /* Default to disabled... */
905 m->opts[i].opt_flags |= OPT_IGNORE;
906 if ((t == FS_UNUSED || t == FS_SWAP) &&
907 (m->opts[i].opt_action == edit_fs_preserve ||
908 m->opts[i].opt_action == edit_fs_mount ||
909 m->opts[i].opt_action == edit_fs_mountpt ||
910 m->opts[i].opt_menu == MENU_mountoptions))
911 continue;
912 if (m->opts[i].opt_action == edit_install &&
913 edit->info.nat_type &&
914 edit->info.nat_type->generic_ptype != PT_root)
915 /* can only install onto PT_root partitions */
916 continue;
917 if (m->opts[i].opt_action == edit_fs_preserve &&
918 t != FS_BSDFFS && t != FS_BSDLFS && t != FS_APPLEUFS) {
919 /* Can only newfs UFS and LFS filesystems */
920 edit->wanted->instflags &= ~PUIINST_NEWFS;
921 continue;
922 }
923 if (m->opts[i].opt_action == edit_ptn_custom_type) {
924 attr_no = (size_t)i - edit->first_custom_attr;
925 if (!edit->pset->parts->pscheme->
926 custom_attribute_writable(
927 edit->pset->parts, edit->id, attr_no))
928 continue;
929 }
930 /* Ok: we want this one */
931 m->opts[i].opt_flags &= ~OPT_IGNORE;
932 }
933 }
934
935 static void
936 draw_edit_ptn_line(menudesc *m, int opt, void *arg)
937 {
938 struct single_part_fs_edit *edit = arg;
939 static int col_width;
940 static const char *ptn_type, *ptn_start, *ptn_size, *ptn_end,
941 *ptn_newfs, *ptn_mount, *ptn_mount_options, *ptn_mountpt,
942 *ptn_install;
943 const char *c;
944 char val[MENUSTRSIZE];
945 const char *attrname;
946 size_t attr_no;
947
948 if (col_width == 0) {
949 int l;
950
951 #define LOAD(STR) STR = msg_string(MSG_##STR); l = strlen(STR); \
952 if (l > col_width) col_width = l
953
954 LOAD(ptn_type);
955 LOAD(ptn_start);
956 LOAD(ptn_size);
957 LOAD(ptn_end);
958 LOAD(ptn_install);
959 LOAD(ptn_newfs);
960 LOAD(ptn_mount);
961 LOAD(ptn_mount_options);
962 LOAD(ptn_mountpt);
963 #undef LOAD
964
965 for (size_t i = 0;
966 i < edit->pset->parts->pscheme->custom_attribute_count;
967 i++) {
968 attrname = msg_string(
969 edit->pset->parts->pscheme->custom_attributes[i]
970 .label);
971 l = strlen(attrname);
972 if (l > col_width) col_width = l;
973 }
974
975 col_width += 3;
976 }
977
978 if (m->opts[opt].opt_flags & OPT_IGNORE
979 && (opt != 3 || edit->info.fs_type == FS_UNUSED)
980 && m->opts[opt].opt_action != edit_ptn_custom_type) {
981 wprintw(m->mw, "%*s -", col_width, "");
982 return;
983 }
984
985 if (opt < 4) {
986 switch (opt) {
987 case 0:
988 if (edit->info.fs_type == FS_BSDFFS)
989 if (edit->info.fs_sub_type == 2)
990 c = msg_string(MSG_fs_type_ffsv2);
991 else
992 c = msg_string(MSG_fs_type_ffs);
993 else
994 c = getfslabelname(edit->info.fs_type,
995 edit->info.fs_sub_type);
996 wprintw(m->mw, "%*s : %s", col_width, ptn_type, c);
997 return;
998 case 1:
999 wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1000 ptn_start, edit->info.start / sizemult, multname);
1001 return;
1002 case 2:
1003 wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1004 ptn_size, edit->info.size / sizemult, multname);
1005 return;
1006 case 3:
1007 wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1008 ptn_end, (edit->info.start + edit->info.size)
1009 / sizemult, multname);
1010 return;
1011 }
1012 }
1013 if (m->opts[opt].opt_action == edit_install) {
1014 wprintw(m->mw, "%*s : %s", col_width, ptn_install,
1015 msg_string(edit->info.start == pm->ptstart
1016 ? MSG_Yes : MSG_No));
1017 return;
1018 }
1019 if (m->opts[opt].opt_action == edit_fs_preserve) {
1020 wprintw(m->mw, "%*s : %s", col_width, ptn_newfs,
1021 msg_string(edit->wanted->instflags & PUIINST_NEWFS
1022 ? MSG_Yes : MSG_No));
1023 return;
1024 }
1025 if (m->opts[opt].opt_action == edit_fs_mount) {
1026 wprintw(m->mw, "%*s : %s", col_width, ptn_mount,
1027 msg_string(edit->wanted->instflags & PUIINST_MOUNT
1028 ? MSG_Yes : MSG_No));
1029 return;
1030 }
1031 if (m->opts[opt].opt_menu == MENU_mountoptions) {
1032 wprintw(m->mw, "%*s : ", col_width, ptn_mount_options);
1033 if (edit->wanted->mountflags & PUIMNT_ASYNC)
1034 wprintw(m->mw, "async ");
1035 if (edit->wanted->mountflags & PUIMNT_NOATIME)
1036 wprintw(m->mw, "noatime ");
1037 if (edit->wanted->mountflags & PUIMNT_NODEV)
1038 wprintw(m->mw, "nodev ");
1039 if (edit->wanted->mountflags & PUIMNT_NODEVMTIME)
1040 wprintw(m->mw, "nodevmtime ");
1041 if (edit->wanted->mountflags & PUIMNT_NOEXEC)
1042 wprintw(m->mw, "noexec ");
1043 if (edit->wanted->mountflags & PUIMNT_NOSUID)
1044 wprintw(m->mw, "nosuid ");
1045 if (edit->wanted->mountflags & PUIMNT_LOG)
1046 wprintw(m->mw, "log ");
1047 if (edit->wanted->mountflags & PUIMNT_NOAUTO)
1048 wprintw(m->mw, "noauto ");
1049 return;
1050 }
1051 if (m->opts[opt].opt_action == edit_fs_mountpt) {
1052 wprintw(m->mw, "%*s : %s", col_width, ptn_mountpt,
1053 edit->wanted->mount);
1054 return;
1055 }
1056
1057 attr_no = opt - edit->first_custom_attr;
1058 edit->pset->parts->pscheme->format_custom_attribute(
1059 edit->pset->parts, edit->id, attr_no, &edit->info,
1060 val, sizeof val);
1061 attrname = msg_string(edit->pset->parts->pscheme->
1062 custom_attributes[attr_no].label);
1063 wprintw(m->mw, "%*s : %s", col_width, attrname, val);
1064 }
1065
1066 static int
1067 edit_ptn_custom_type(menudesc *m, void *arg)
1068 {
1069 struct single_part_fs_edit *edit = arg;
1070 size_t attr_no = m->cursel - edit->first_custom_attr;
1071 char line[STRSIZE];
1072
1073 switch (edit->pset->parts->pscheme->custom_attributes[attr_no].type) {
1074 case pet_bool:
1075 edit->pset->parts->pscheme->custom_attribute_toggle(
1076 edit->pset->parts, edit->id, attr_no);
1077 break;
1078 case pet_cardinal:
1079 case pet_str:
1080 edit->pset->parts->pscheme->format_custom_attribute(
1081 edit->pset->parts, edit->id, attr_no, &edit->info,
1082 line, sizeof(line));
1083 msg_prompt_win(
1084 edit->pset->parts->pscheme->custom_attributes[attr_no].
1085 label, -1, 18, 0, 0, line, line, sizeof(line));
1086 edit->pset->parts->pscheme->custom_attribute_set_str(
1087 edit->pset->parts, edit->id, attr_no, line);
1088 break;
1089 }
1090
1091 return 0;
1092 }
1093
1094
1095 /*
1096 * Some column width depend on translation, we will set these in
1097 * fmt_fspart_header and later use it when formatting single entries
1098 * in fmt_fspart_row.
1099 * The table consist of 3 "size like" columns, all fixed width, then
1100 * ptnheaders_fstype, part_header_col_flag, and finally the mount point
1101 * (which is variable width).
1102 */
1103 static int fstype_width, flags_width;
1104 static char fspart_separator[MENUSTRSIZE];
1105 static char fspart_title[2*MENUSTRSIZE];
1106
1107 /*
1108 * Format the header of the main partition editor menu.
1109 */
1110 static void
1111 fmt_fspart_header(menudesc *menu, void *arg)
1112 {
1113 struct partition_usage_set *pset = arg;
1114 char total[6], free_space[6], scol[13], ecol[13], szcol[13],
1115 sepline[MENUSTRSIZE], *p;
1116 const char *fstype, *flags;
1117 int i;
1118 bool with_inst_flag = pset->parts->parent == NULL;
1119
1120 humanize_number(total, sizeof total,
1121 pset->parts->disk_size * 512,
1122 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1123 humanize_number(free_space, sizeof free_space,
1124 pset->cur_free_space * 512,
1125 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1126
1127 msg_display_subst(MSG_fspart, 7, pset->parts->disk,
1128 msg_string(pset->parts->pscheme->name),
1129 msg_string(pset->parts->pscheme->short_name),
1130 with_inst_flag ? msg_string(MSG_ptn_instflag_desc) : "",
1131 pset->parts->pscheme->part_flag_desc ?
1132 msg_string(pset->parts->pscheme->part_flag_desc)
1133 : "",
1134 total, free_space);
1135
1136 snprintf(scol, sizeof scol, "%s (%s)",
1137 msg_string(MSG_ptnheaders_start), multname);
1138 snprintf(ecol, sizeof ecol, "%s (%s)",
1139 msg_string(MSG_ptnheaders_end), multname);
1140 snprintf(szcol, sizeof szcol, "%s (%s)",
1141 msg_string(MSG_ptnheaders_size), multname);
1142
1143 fstype = msg_string(MSG_ptnheaders_fstype);
1144 flags = msg_string(MSG_part_header_col_flag);
1145 fstype_width = max(strlen(fstype), 8);
1146 flags_width = strlen(flags);
1147 for (i = 0, p = sepline; i < fstype_width; i++)
1148 *p++ = '-';
1149 for (i = 0, *p++ = ' '; i < flags_width; i++)
1150 *p++ = '-';
1151 *p = 0;
1152
1153 snprintf(fspart_separator, sizeof(fspart_separator),
1154 "------------ ------------ ------------ %s ----------------",
1155 sepline);
1156
1157 snprintf(fspart_title, sizeof(fspart_title),
1158 " %12.12s %12.12s %12.12s %*s %*s %s\n"
1159 " %s", scol, ecol, szcol, fstype_width, fstype,
1160 flags_width, flags, msg_string(MSG_ptnheaders_filesystem),
1161 fspart_separator);
1162
1163 msg_table_add("\n\n");
1164 }
1165
1166 /*
1167 * Format one partition entry in the main partition editor.
1168 */
1169 static void
1170 fmt_fspart_row(menudesc *m, int ptn, void *arg)
1171 {
1172 struct partition_usage_set *pset = arg;
1173 struct disk_part_info info;
1174 daddr_t poffset, psize, pend;
1175 const char *desc;
1176 static const char *Yes;
1177 char flag_str[MENUSTRSIZE], *fp;
1178 unsigned inst_flags;
1179 bool with_inst_flag = pset->parts->parent == NULL;
1180
1181 if (Yes == NULL)
1182 Yes = msg_string(MSG_Yes);
1183
1184 if (!real_partition(pset, ptn))
1185 return;
1186
1187 if (!pset->parts->pscheme->get_part_info(pset->parts,
1188 pset->infos[ptn].cur_part_id, &info))
1189 return;
1190
1191 /* enable / disable this line if it is something like RAW_PART */
1192 if (info.flags & (PTI_WHOLE_DISK|PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
1193 m->opts[ptn].opt_flags |= OPT_IGNORE;
1194 else
1195 m->opts[ptn].opt_flags &= ~OPT_IGNORE;
1196
1197 poffset = info.start / sizemult;
1198 psize = info.size / sizemult;
1199 if (psize == 0)
1200 pend = 0;
1201 else
1202 pend = (info.start + info.size) / sizemult - 1;
1203
1204 if (info.flags & PTI_WHOLE_DISK)
1205 desc = msg_string(MSG_NetBSD_partition_cant_change);
1206 else if (info.flags & PTI_RAW_PART)
1207 desc = msg_string(MSG_Whole_disk_cant_change);
1208 else if (info.flags & PTI_BOOT)
1209 desc = msg_string(MSG_Boot_partition_cant_change);
1210 else
1211 desc = getfslabelname(info.fs_type, info.fs_sub_type);
1212
1213 fp = flag_str;
1214 inst_flags = pset->infos[ptn].instflags;
1215 if (with_inst_flag && info.start == pm->ptstart &&
1216 info.nat_type->generic_ptype == PT_root) {
1217 static char inst_flag;
1218
1219 if (inst_flag == 0)
1220 inst_flag = msg_string(MSG_install_flag)[0];
1221 *fp++ = inst_flag;
1222 }
1223 if (inst_flags & PUIINST_NEWFS)
1224 *fp++ = msg_string(MSG_newfs_flag)[0];
1225 *fp = 0;
1226 if (pset->parts->pscheme->get_part_attr_str != NULL)
1227 pset->parts->pscheme->get_part_attr_str(pset->parts,
1228 pset->infos[ptn].cur_part_id, fp, sizeof(flag_str)-1);
1229
1230 /* if the fstype description does not fit, check if we can overrun */
1231 if (strlen(desc) > (size_t)fstype_width &&
1232 flag_str[0] == 0 && (info.last_mounted == NULL ||
1233 info.last_mounted[0] == 0))
1234 wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
1235 " %s",
1236 poffset, pend, psize, desc);
1237 else
1238 wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
1239 " %*.*s %*s %s",
1240 poffset, pend, psize, fstype_width, fstype_width, desc,
1241 -flags_width, flag_str,
1242 (inst_flags & PUIINST_MOUNT) && info.last_mounted &&
1243 info.last_mounted[0] ? info.last_mounted : "");
1244 }
1245
1246 static int
1247 edit_fspart_pack(menudesc *m, void *arg)
1248 {
1249 struct partition_usage_set *pset = arg;
1250 char buf[STRSIZE];
1251
1252 if (!pset->parts->pscheme->get_disk_pack_name(pset->parts,
1253 buf, sizeof buf))
1254 return 0;
1255
1256 msg_prompt_win(MSG_edit_disk_pack_hdr,
1257 -1, 18, 0, -1, buf, buf, sizeof(buf));
1258
1259 pset->parts->pscheme->set_disk_pack_name(pset->parts, buf);
1260 return 0;
1261 }
1262
1263 static int
1264 edit_fspart_add(menudesc *m, void *arg)
1265 {
1266 struct partition_usage_set *pset = arg;
1267 struct part_usage_info *ninfo;
1268 menu_ent *nmenopts;
1269 size_t cnt, off;
1270
1271 ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
1272 if (ninfo == NULL)
1273 return 0;
1274 pset->infos = ninfo;
1275 off = pset->parts->num_part;
1276 cnt = pset->num-pset->parts->num_part;
1277 if (cnt > 0)
1278 memmove(pset->infos+off+1,pset->infos+off,
1279 cnt*sizeof(*pset->infos));
1280 memset(pset->infos+off, 0, sizeof(*pset->infos));
1281
1282 nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
1283 if (nmenopts == NULL)
1284 return 0;
1285 memmove(nmenopts+off+1, nmenopts+off,
1286 (m->numopts-off)*sizeof(*nmenopts));
1287 memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
1288 nmenopts[off].opt_action = edit_ptn;
1289 pset->menu_opts = m->opts = nmenopts;
1290 m->numopts++;
1291 m->cursel = off;
1292 pset->num++;
1293
1294 /* now update edit menu to fit */
1295 m->h = 0;
1296 resize_menu_height(m);
1297
1298 /* and directly invoke the partition editor for the new part */
1299 edit_ptn(m, arg);
1300
1301 show_partition_adder(m, pset);
1302
1303 return -1;
1304 }
1305
1306 static void
1307 add_partition_adder(menudesc *m, struct partition_usage_set *pset)
1308 {
1309 struct part_usage_info *ninfo;
1310 menu_ent *nmenopts;
1311 size_t off;
1312
1313 ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
1314 if (ninfo == NULL)
1315 return;
1316 pset->infos = ninfo;
1317 off = pset->parts->num_part+1;
1318
1319 nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
1320 if (nmenopts == NULL)
1321 return;
1322 memmove(nmenopts+off+1, nmenopts+off,
1323 (m->numopts-off)*sizeof(*nmenopts));
1324 memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
1325
1326 nmenopts[off].opt_name = MSG_addpart;
1327 nmenopts[off].opt_flags = OPT_SUB;
1328 nmenopts[off].opt_action = edit_fspart_add;
1329
1330 m->opts = nmenopts;
1331 m->numopts++;
1332 pset->num++;
1333 }
1334
1335 static void
1336 remove_partition_adder(menudesc *m, struct partition_usage_set *pset)
1337 {
1338 size_t off;
1339
1340 off = pset->parts->num_part+1;
1341 memmove(m->opts+off, m->opts+off+1,
1342 (m->numopts-off-1)*sizeof(*m->opts));
1343 m->numopts--;
1344 pset->num--;
1345 }
1346
1347 /*
1348 * Called whenever the "add a partition" option may need to be removed
1349 * or added
1350 */
1351 static void
1352 show_partition_adder(menudesc *m, struct partition_usage_set *pset)
1353 {
1354 bool can_add_partition = pset->parts->pscheme->can_add_partition(
1355 pset->parts);
1356 bool part_adder_present =
1357 (m->opts[pset->parts->num_part].opt_flags & OPT_IGNORE) &&
1358 (m->opts[pset->parts->num_part+1].opt_action == edit_fspart_add);
1359
1360 if (can_add_partition == part_adder_present)
1361 return;
1362
1363 if (can_add_partition)
1364 add_partition_adder(m, pset);
1365 else
1366 remove_partition_adder(m, pset);
1367
1368 /* now update edit menu to fit */
1369 m->h = 0;
1370 resize_menu_height(m);
1371 }
1372
1373 static int
1374 edit_fspart_abort(menudesc *m, void *arg)
1375 {
1376 struct partition_usage_set *pset = arg;
1377
1378 pset->ok = false;
1379 return 1;
1380 }
1381
1382 /*
1383 * Check a disklabel.
1384 * If there are overlapping active partitions,
1385 * Ask the user if they want to edit the partition or give up.
1386 */
1387 int
1388 edit_and_check_label(struct pm_devs *p, struct partition_usage_set *pset)
1389 {
1390 menu_ent *op;
1391 size_t cnt, i;
1392 bool may_add = pset->parts->pscheme->can_add_partition(pset->parts);
1393 bool may_edit_pack =
1394 pset->parts->pscheme->get_disk_pack_name != NULL &&
1395 pset->parts->pscheme->set_disk_pack_name != NULL;
1396
1397 pset->menu_opts = calloc(pset->parts->num_part
1398 +3+may_add+may_edit_pack,
1399 sizeof *pset->menu_opts);
1400 if (pset->menu_opts == NULL)
1401 return 0;
1402
1403 op = pset->menu_opts;
1404 for (i = 0; i < pset->parts->num_part; i++) {
1405 op->opt_action = edit_ptn;
1406 op++;
1407 }
1408 /* separator line between partitions and actions */
1409 op->opt_name = fspart_separator;
1410 op->opt_flags = OPT_IGNORE|OPT_NOSHORT;
1411 op++;
1412
1413 /* followed by new partition adder */
1414 if (may_add) {
1415 op->opt_name = MSG_addpart;
1416 op->opt_flags = OPT_SUB;
1417 op->opt_action = edit_fspart_add;
1418 op++;
1419 }
1420
1421 /* and unit changer */
1422 op->opt_name = MSG_askunits;
1423 op->opt_menu = MENU_sizechoice;
1424 op->opt_flags = OPT_SUB;
1425 op->opt_action = NULL;
1426 op++;
1427
1428 if (may_edit_pack) {
1429 op->opt_name = MSG_editpack;
1430 op->opt_flags = OPT_SUB;
1431 op->opt_action = edit_fspart_pack;
1432 op++;
1433 }
1434
1435 /* and abort option */
1436 op->opt_name = MSG_cancel;
1437 op->opt_flags = OPT_EXIT;
1438 op->opt_action = edit_fspart_abort;
1439 op++;
1440 cnt = op - pset->menu_opts;
1441 assert(cnt == pset->parts->num_part+3+may_add+may_edit_pack);
1442
1443 pset->menu = new_menu(fspart_title, pset->menu_opts, cnt,
1444 0, -1, 0, 74,
1445 MC_ALWAYS_SCROLL|MC_NOBOX|MC_DFLTEXIT|
1446 MC_NOCLEAR|MC_CONTINUOUS,
1447 fmt_fspart_header, fmt_fspart_row, NULL, NULL,
1448 MSG_partition_sizes_ok);
1449
1450 if (pset->menu < 0) {
1451 free(pset->menu_opts);
1452 pset->menu_opts = NULL;
1453 return 0;
1454 }
1455
1456 p->current_cylsize = p->dlcylsize;
1457
1458 for (;;) {
1459 /* first give the user the option to edit the label... */
1460 pset->ok = true;
1461 process_menu(pset->menu, pset);
1462 if (!pset->ok) {
1463 i = 0;
1464 break;
1465 }
1466
1467 /* User thinks the label is OK. */
1468 i = verify_parts(pset);
1469 if (i == 1)
1470 continue;
1471 break;
1472 }
1473 free(pset->menu_opts);
1474 pset->menu_opts = NULL;
1475 free_menu(pset->menu);
1476 pset->menu = -1;
1477
1478 return i != 0;
1479 }
1480
1481 /*
1482 * Try to get 'last mounted on' (or equiv) from fs superblock.
1483 */
1484 const char *
1485 get_last_mounted(int fd, daddr_t partstart, uint *fs_type, uint *fs_sub_type,
1486 uint flags)
1487 {
1488 static char sblk[SBLOCKSIZE]; /* is this enough? */
1489 struct fs *SB = (struct fs *)sblk;
1490 static const off_t sblocks[] = SBLOCKSEARCH;
1491 const off_t *sbp;
1492 const char *mnt = NULL;
1493 int len;
1494
1495 if (fd == -1)
1496 return "";
1497
1498 if (fs_type)
1499 *fs_type = 0;
1500 if (fs_sub_type)
1501 *fs_sub_type = 0;
1502
1503 /* Check UFS1/2 (and hence LFS) superblock */
1504 for (sbp = sblocks; mnt == NULL && *sbp != -1; sbp++) {
1505 if (pread(fd, sblk, sizeof sblk,
1506 (off_t)partstart * (off_t)512 + *sbp) != sizeof sblk)
1507 continue;
1508
1509 /*
1510 * If start of partition and allowed by flags check
1511 * for other fs types
1512 */
1513 if (*sbp == 0 && (flags & GLM_MAYBE_FAT32) &&
1514 sblk[0x42] == 0x29 && memcmp(sblk + 0x52, "FAT", 3) == 0) {
1515 /* Probably a FAT filesystem, report volume name */
1516 size_t i;
1517 for (i = 0x51; i >= 0x47; i--) {
1518 if (sblk[i] != ' ')
1519 break;
1520 sblk[i] = 0;
1521 }
1522 sblk[0x52] = 0;
1523 if (fs_type)
1524 *fs_type = FS_MSDOS;
1525 if (fs_sub_type)
1526 *fs_sub_type = sblk[0x53];
1527 return sblk + 0x47;
1528 } else if (*sbp == 0 && (flags & GLM_MAYBE_NTFS) &&
1529 memcmp(sblk+3, "NTFS ", 8) == 0) {
1530 if (fs_type)
1531 *fs_type = FS_NTFS;
1532 if (fs_sub_type)
1533 *fs_sub_type = MBR_PTYPE_NTFS;
1534 /* XXX dig for volume name attribute ? */
1535 return "";
1536 }
1537
1538 if (!(flags & GLM_LIKELY_FFS))
1539 continue;
1540
1541 /* Maybe we should validate the checksum??? */
1542 switch (SB->fs_magic) {
1543 case FS_UFS1_MAGIC:
1544 case FS_UFS1_MAGIC_SWAPPED:
1545 if (!(SB->fs_old_flags & FS_FLAGS_UPDATED)) {
1546 if (*sbp == SBLOCK_UFS1)
1547 mnt = (const char *)SB->fs_fsmnt;
1548 } else {
1549 /* Check we have the main superblock */
1550 if (SB->fs_sblockloc == *sbp)
1551 mnt = (const char *)SB->fs_fsmnt;
1552 }
1553 if (fs_type)
1554 *fs_type = FS_BSDFFS;
1555 if (fs_sub_type)
1556 *fs_sub_type = 2;
1557 continue;
1558 case FS_UFS2_MAGIC:
1559 case FS_UFS2_MAGIC_SWAPPED:
1560 /* Check we have the main superblock */
1561 if (SB->fs_sblockloc == *sbp) {
1562 mnt = (const char *)SB->fs_fsmnt;
1563 if (fs_type)
1564 *fs_type = FS_BSDFFS;
1565 if (fs_sub_type)
1566 *fs_sub_type = 2;
1567 }
1568 continue;
1569 }
1570 }
1571
1572 if (mnt == NULL)
1573 return "";
1574
1575 /* If sysinst mounted this last then strip prefix */
1576 len = strlen(targetroot_mnt);
1577 if (memcmp(mnt, targetroot_mnt, len) == 0) {
1578 if (mnt[len] == 0)
1579 return "/";
1580 if (mnt[len] == '/')
1581 return mnt + len;
1582 }
1583 return mnt;
1584 #undef SB
1585 }
1586
1587 /* Ask for a partition offset, check bounds and do the needed roundups */
1588 daddr_t
1589 getpartoff(struct disk_partitions *parts, daddr_t defpartstart)
1590 {
1591 char defstart[24], isize[24], maxpart, minspace, maxspace,
1592 *prompt, *label_msg, valid_parts[4], valid_spaces[4],
1593 space_prompt[1024], *head, *hint_part, *hint_space, *tail;
1594 size_t num_freespace, spaces, ndx;
1595 struct disk_part_free_space *freespace;
1596 daddr_t i, localsizemult, ptn_alignment, min, max;
1597 part_id partn;
1598 struct disk_part_info info;
1599 const char *errmsg = NULL;
1600
1601 min = parts->disk_start;
1602 max = min + parts->disk_size;
1603
1604 /* upper bound on the number of free spaces, plus some slope */
1605 num_freespace = parts->num_part * 2 + 5;
1606 freespace = calloc(num_freespace, sizeof(*freespace));
1607 if (freespace == NULL)
1608 return -1;
1609
1610 ptn_alignment = parts->pscheme->get_part_alignment(parts);
1611 spaces = parts->pscheme->get_free_spaces(parts, freespace,
1612 num_freespace, max(sizemult, ptn_alignment), ptn_alignment, -1,
1613 defpartstart);
1614
1615 maxpart = 'a' + parts->num_part -1;
1616 if (parts->num_part > 1) {
1617 snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpart);
1618 } else if (parts->num_part == 1) {
1619 snprintf(valid_parts, sizeof valid_parts, " %c", maxpart);
1620 } else {
1621 strcpy(valid_parts, "---");
1622 }
1623 if (spaces > 1) {
1624 minspace = maxpart + 1;
1625 maxspace = minspace + spaces -1;
1626 snprintf(valid_spaces, sizeof valid_spaces, "%c-%c", minspace,
1627 maxspace);
1628 } else if (spaces == 1) {
1629 maxspace = minspace = maxpart + 1;
1630 snprintf(valid_spaces, sizeof valid_spaces, " %c", minspace);
1631 } else {
1632 minspace = 0;
1633 maxspace = 0;
1634 strcpy(valid_spaces, "---");
1635 }
1636
1637 /* Add description of start/size to user prompt */
1638 const char *mstr = msg_string(MSG_free_space_line);
1639 space_prompt[0] = 0;
1640 for (ndx = 0; ndx < spaces; ndx++) {
1641 char str_start[40], str_end[40], str_size[40], str_tag[4];
1642
1643 sprintf(str_tag, "%c: ", minspace+(int)ndx);
1644 sprintf(str_start, "%" PRIu64, freespace[ndx].start / sizemult);
1645 sprintf(str_end, "%" PRIu64,
1646 (freespace[ndx].start + freespace[ndx].size) / sizemult);
1647 sprintf(str_size, "%" PRIu64, freespace[ndx].size / sizemult);
1648 const char *args[4] = { str_start, str_end, str_size,
1649 multname };
1650 char *line = str_arg_subst(mstr, 4, args);
1651 strlcat(space_prompt, str_tag, sizeof(space_prompt));
1652 size_t len = strlcat(space_prompt, line, sizeof(space_prompt));
1653 free (line);
1654 if (len >= sizeof space_prompt)
1655 break;
1656 }
1657
1658 const char *args[] = { valid_parts, valid_spaces, multname };
1659 hint_part = NULL;
1660 hint_space = NULL;
1661 head = str_arg_subst(msg_string(MSG_label_offset_head),
1662 __arraycount(args), args);
1663 if (parts->num_part)
1664 hint_part = str_arg_subst(msg_string(
1665 MSG_label_offset_part_hint), __arraycount(args), args);
1666 if (spaces)
1667 hint_space = str_arg_subst(msg_string(
1668 MSG_label_offset_space_hint), __arraycount(args), args);
1669 tail = str_arg_subst(msg_string(MSG_label_offset_tail),
1670 __arraycount(args), args);
1671
1672 if (hint_part && hint_space)
1673 asprintf(&label_msg, "%s\n%s\n%s\n\n%s\n%s",
1674 head, hint_part, hint_space, space_prompt, tail);
1675 else if (hint_part)
1676 asprintf(&label_msg, "%s\n%s\n\n%s",
1677 head, hint_part, tail);
1678 else if (hint_space)
1679 asprintf(&label_msg, "%s\n%s\n\n%s\n%s",
1680 head, hint_space, space_prompt, tail);
1681 else
1682 asprintf(&label_msg, "%s\n\n%s",
1683 head, tail);
1684 free(head); free(hint_part); free(hint_space); free(tail);
1685
1686 localsizemult = sizemult;
1687 errmsg = NULL;
1688 for (;;) {
1689 snprintf(defstart, sizeof defstart, "%" PRIu64,
1690 defpartstart/sizemult);
1691 if (errmsg != NULL && errmsg[0] != 0)
1692 asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
1693 else
1694 prompt = label_msg;
1695 msg_prompt_win(prompt, -1, 13, 70, -1,
1696 (defpartstart > 0) ? defstart : NULL, isize, sizeof isize);
1697 if (label_msg != prompt)
1698 free(prompt);
1699 if (strcmp(defstart, isize) == 0) {
1700 /* Don't do rounding if default accepted */
1701 i = defpartstart;
1702 break;
1703 }
1704 if (isize[1] == '\0' && isize[0] >= 'a' &&
1705 isize[0] <= maxpart) {
1706 partn = isize[0] - 'a';
1707 if (parts->pscheme->get_part_info(parts, partn,
1708 &info)) {
1709 i = info.start + info.size;
1710 localsizemult = 1;
1711 } else {
1712 errmsg = msg_string(MSG_invalid_sector_number);
1713 continue;
1714 }
1715 } else if (isize[1] == '\0' && isize[0] >= minspace &&
1716 isize[0] <= maxspace) {
1717 ndx = isize[0] - minspace;
1718 i = freespace[ndx].start;
1719 localsizemult = 1;
1720 } else if (atoi(isize) == -1) {
1721 i = min;
1722 localsizemult = 1;
1723 } else {
1724 i = parse_disk_pos(isize, &localsizemult, pm->dlcylsize, NULL);
1725 if (i < 0) {
1726 errmsg = msg_string(MSG_invalid_sector_number);
1727 continue;
1728 }
1729 }
1730 /* round to cylinder size if localsizemult != 1 */
1731 i = NUMSEC(i, localsizemult, pm->dlcylsize);
1732 /* Adjust to start of slice if needed */
1733 if ((i < min && (min - i) < localsizemult) ||
1734 (i > min && (i - min) < localsizemult)) {
1735 i = min;
1736 }
1737 if (max == 0 || i <= max)
1738 break;
1739 errmsg = msg_string(MSG_startoutsidedisk);
1740 }
1741 free(label_msg);
1742 free(freespace);
1743
1744 return i;
1745 }
1746
1747
1748 /* Ask for a partition size, check bounds and do the needed roundups */
1749 daddr_t
1750 getpartsize(struct disk_partitions *parts, daddr_t partstart, daddr_t dflt)
1751 {
1752 char dsize[24], isize[24], max_size[24], maxpartc, valid_parts[4],
1753 *label_msg, *prompt, *head, *hint, *tail;
1754 const char *errmsg = NULL;
1755 daddr_t i, partend, localsizemult, max, max_r, dflt_r;
1756 struct disk_part_info info;
1757 part_id partn;
1758
1759 max = parts->pscheme->max_free_space_at(parts, partstart);
1760
1761 /* We need to keep both the unrounded and rounded (_r) max and dflt */
1762 dflt_r = (partstart + dflt) / sizemult - partstart / sizemult;
1763 if (max == dflt)
1764 max_r = dflt_r;
1765 else
1766 max_r = max / sizemult;
1767 /* the partition may have been moved and now not fit any longer */
1768 if (dflt > max)
1769 dflt = max;
1770 if (dflt_r > max_r)
1771 dflt_r = max_r;
1772
1773 snprintf(max_size, sizeof max_size, "%" PRIu64, max_r);
1774
1775 maxpartc = 'a' + parts->num_part -1;
1776 if (parts->num_part > 1) {
1777 snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpartc);
1778 } else if (parts->num_part == 1) {
1779 snprintf(valid_parts, sizeof valid_parts, " %c", maxpartc);
1780 } else {
1781 strcpy(valid_parts, "---");
1782 }
1783
1784 const char *args[] = { valid_parts, max_size, multname };
1785 hint = NULL;
1786 head = str_arg_subst(msg_string(MSG_label_size_head),
1787 __arraycount(args), args);
1788 if (parts->num_part)
1789 hint = str_arg_subst(msg_string(MSG_label_size_part_hint),
1790 __arraycount(args), args);
1791 tail = str_arg_subst(msg_string(MSG_label_size_tail),
1792 __arraycount(args), args);
1793
1794 if (hint != NULL)
1795 asprintf(&label_msg, "%s\n%s\n\n%s", head, hint, tail);
1796 else
1797 asprintf(&label_msg, "%s\n\n%s", head, tail);
1798 free(head); free(hint); free(tail);
1799
1800 localsizemult = sizemult;
1801 i = -1;
1802 for (;;) {
1803 snprintf(dsize, sizeof dsize, "%" PRIu64, dflt_r);
1804
1805 if (errmsg != NULL && errmsg[0] != 0)
1806 asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
1807 else
1808 prompt = label_msg;
1809 msg_prompt_win(prompt, -1, 12, 70, -1,
1810 (dflt != 0) ? dsize : 0, isize, sizeof isize);
1811 if (prompt != label_msg)
1812 free(prompt);
1813
1814 if (strcmp(isize, dsize) == 0) {
1815 free(label_msg);
1816 return dflt;
1817 }
1818 if (parts->num_part && isize[1] == '\0' && isize[0] >= 'a' &&
1819 isize[0] <= maxpartc) {
1820 partn = isize[0] - 'a';
1821 if (parts->pscheme->get_part_info(parts, partn,
1822 &info)) {
1823 i = info.start - partstart -1;
1824 localsizemult = 1;
1825 max_r = max;
1826 }
1827 } else if (atoi(isize) == -1) {
1828 i = max;
1829 localsizemult = 1;
1830 max_r = max;
1831 } else {
1832 i = parse_disk_pos(isize, &localsizemult,
1833 pm->dlcylsize, NULL);
1834 if (localsizemult != sizemult)
1835 max_r = max;
1836 }
1837 if (i < 0) {
1838 errmsg = msg_string(MSG_Invalid_numeric);
1839 continue;
1840 } else if (i > max_r) {
1841 errmsg = msg_string(MSG_Too_large);
1842 continue;
1843 }
1844 /*
1845 * partend is aligned to a cylinder if localsizemult
1846 * is not 1 sector
1847 */
1848 partend = NUMSEC((partstart + i*localsizemult) / localsizemult,
1849 localsizemult, pm->dlcylsize);
1850 /* Align to end-of-disk or end-of-slice if close enough */
1851 if (partend > (pm->dlsize - sizemult)
1852 && partend < (pm->dlsize + sizemult))
1853 partend = pm->dlsize;
1854 if (partend > (partstart + max - sizemult)
1855 && partend < (partstart + max + sizemult))
1856 partend = partstart + max;
1857 /* sanity checks */
1858 if (partend > (partstart + pm->dlsize)) {
1859 partend = pm->dlsize;
1860 errmsg = msg_string(MSG_endoutsidedisk);
1861 continue;
1862 }
1863 free(label_msg);
1864 if (partend < partstart)
1865 return 0;
1866 return (partend - partstart);
1867 }
1868 /* NOTREACHED */
1869 }
1870
1871 /*
1872 * convert a string to a number of sectors, with a possible unit
1873 * 150M = 150 Megabytes
1874 * 2000c = 2000 cylinders
1875 * 150256s = 150256 sectors
1876 * Without units, use the default (sizemult).
1877 * returns the raw input value, and the unit used. Caller needs to multiply!
1878 * On invalid inputs, returns -1.
1879 */
1880 daddr_t
1881 parse_disk_pos(
1882 const char *str,
1883 daddr_t *localsizemult,
1884 daddr_t cyl_size,
1885 bool *extend_this)
1886 {
1887 daddr_t val;
1888 char *cp;
1889 bool mult_found;
1890
1891 if (str[0] == '\0') {
1892 return -1;
1893 }
1894 val = strtoull(str, &cp, 10);
1895 mult_found = false;
1896 if (extend_this)
1897 *extend_this = false;
1898 while (*cp != 0) {
1899 if (*cp == 'G' || *cp == 'g') {
1900 if (mult_found)
1901 return -1;
1902 *localsizemult = GIG / pm->sectorsize;
1903 goto next;
1904 }
1905 if (*cp == 'M' || *cp == 'm') {
1906 if (mult_found)
1907 return -1;
1908 *localsizemult = MEG / pm->sectorsize;
1909 goto next;
1910 }
1911 if (*cp == 'c' || *cp == 'C') {
1912 if (mult_found)
1913 return -1;
1914 *localsizemult = pm->dlcylsize;
1915 goto next;
1916 }
1917 if (*cp == 's' || *cp == 'S') {
1918 if (mult_found)
1919 return -1;
1920 *localsizemult = 1;
1921 goto next;
1922 }
1923 if (*cp == '+' && extend_this) {
1924 *extend_this = true;
1925 cp++;
1926 break;
1927 }
1928
1929 /* not a known unit */
1930 return -1;
1931
1932 next:
1933 mult_found = true;
1934 cp++;
1935 continue;
1936 }
1937 if (*cp != 0)
1938 return -1;
1939
1940 return val;
1941 }
1942