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