label.c revision 1.13 1 /* $NetBSD: label.c,v 1.13 2019/11/12 16:33:14 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.13 2019/11/12 16:33:14 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[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 t != FS_MSDOS && t != FS_EX2FS) {
920 /* Can not newfs this filesystem */
921 edit->wanted->instflags &= ~PUIINST_NEWFS;
922 continue;
923 }
924 if (m->opts[i].opt_action == edit_ptn_custom_type) {
925 attr_no = (size_t)i - edit->first_custom_attr;
926 if (!edit->pset->parts->pscheme->
927 custom_attribute_writable(
928 edit->pset->parts, edit->id, attr_no))
929 continue;
930 }
931 /* Ok: we want this one */
932 m->opts[i].opt_flags &= ~OPT_IGNORE;
933 }
934 }
935
936 static void
937 draw_edit_ptn_line(menudesc *m, int opt, void *arg)
938 {
939 struct single_part_fs_edit *edit = arg;
940 static int col_width;
941 static const char *ptn_type, *ptn_start, *ptn_size, *ptn_end,
942 *ptn_newfs, *ptn_mount, *ptn_mount_options, *ptn_mountpt,
943 *ptn_install;
944 const char *c;
945 char val[MENUSTRSIZE];
946 const char *attrname;
947 size_t attr_no;
948
949 if (col_width == 0) {
950 int l;
951
952 #define LOAD(STR) STR = msg_string(MSG_##STR); l = strlen(STR); \
953 if (l > col_width) col_width = l
954
955 LOAD(ptn_type);
956 LOAD(ptn_start);
957 LOAD(ptn_size);
958 LOAD(ptn_end);
959 LOAD(ptn_install);
960 LOAD(ptn_newfs);
961 LOAD(ptn_mount);
962 LOAD(ptn_mount_options);
963 LOAD(ptn_mountpt);
964 #undef LOAD
965
966 for (size_t i = 0;
967 i < edit->pset->parts->pscheme->custom_attribute_count;
968 i++) {
969 attrname = msg_string(
970 edit->pset->parts->pscheme->custom_attributes[i]
971 .label);
972 l = strlen(attrname);
973 if (l > col_width) col_width = l;
974 }
975
976 col_width += 3;
977 }
978
979 if (m->opts[opt].opt_flags & OPT_IGNORE
980 && (opt != 3 || edit->info.fs_type == FS_UNUSED)
981 && m->opts[opt].opt_action != edit_ptn_custom_type) {
982 wprintw(m->mw, "%*s -", col_width, "");
983 return;
984 }
985
986 if (opt < 4) {
987 switch (opt) {
988 case 0:
989 if (edit->info.fs_type == FS_BSDFFS)
990 if (edit->info.fs_sub_type == 2)
991 c = msg_string(MSG_fs_type_ffsv2);
992 else
993 c = msg_string(MSG_fs_type_ffs);
994 else
995 c = getfslabelname(edit->info.fs_type,
996 edit->info.fs_sub_type);
997 wprintw(m->mw, "%*s : %s", col_width, ptn_type, c);
998 return;
999 case 1:
1000 wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1001 ptn_start, edit->info.start / sizemult, multname);
1002 return;
1003 case 2:
1004 wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1005 ptn_size, edit->info.size / sizemult, multname);
1006 return;
1007 case 3:
1008 wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1009 ptn_end, (edit->info.start + edit->info.size)
1010 / sizemult, multname);
1011 return;
1012 }
1013 }
1014 if (m->opts[opt].opt_action == edit_install) {
1015 wprintw(m->mw, "%*s : %s", col_width, ptn_install,
1016 msg_string(edit->info.start == pm->ptstart
1017 ? MSG_Yes : MSG_No));
1018 return;
1019 }
1020 if (m->opts[opt].opt_action == edit_fs_preserve) {
1021 wprintw(m->mw, "%*s : %s", col_width, ptn_newfs,
1022 msg_string(edit->wanted->instflags & PUIINST_NEWFS
1023 ? MSG_Yes : MSG_No));
1024 return;
1025 }
1026 if (m->opts[opt].opt_action == edit_fs_mount) {
1027 wprintw(m->mw, "%*s : %s", col_width, ptn_mount,
1028 msg_string(edit->wanted->instflags & PUIINST_MOUNT
1029 ? MSG_Yes : MSG_No));
1030 return;
1031 }
1032 if (m->opts[opt].opt_menu == MENU_mountoptions) {
1033 wprintw(m->mw, "%*s : ", col_width, ptn_mount_options);
1034 if (edit->wanted->mountflags & PUIMNT_ASYNC)
1035 wprintw(m->mw, "async ");
1036 if (edit->wanted->mountflags & PUIMNT_NOATIME)
1037 wprintw(m->mw, "noatime ");
1038 if (edit->wanted->mountflags & PUIMNT_NODEV)
1039 wprintw(m->mw, "nodev ");
1040 if (edit->wanted->mountflags & PUIMNT_NODEVMTIME)
1041 wprintw(m->mw, "nodevmtime ");
1042 if (edit->wanted->mountflags & PUIMNT_NOEXEC)
1043 wprintw(m->mw, "noexec ");
1044 if (edit->wanted->mountflags & PUIMNT_NOSUID)
1045 wprintw(m->mw, "nosuid ");
1046 if (edit->wanted->mountflags & PUIMNT_LOG)
1047 wprintw(m->mw, "log ");
1048 if (edit->wanted->mountflags & PUIMNT_NOAUTO)
1049 wprintw(m->mw, "noauto ");
1050 return;
1051 }
1052 if (m->opts[opt].opt_action == edit_fs_mountpt) {
1053 wprintw(m->mw, "%*s : %s", col_width, ptn_mountpt,
1054 edit->wanted->mount);
1055 return;
1056 }
1057
1058 attr_no = opt - edit->first_custom_attr;
1059 edit->pset->parts->pscheme->format_custom_attribute(
1060 edit->pset->parts, edit->id, attr_no, &edit->info,
1061 val, sizeof val);
1062 attrname = msg_string(edit->pset->parts->pscheme->
1063 custom_attributes[attr_no].label);
1064 wprintw(m->mw, "%*s : %s", col_width, attrname, val);
1065 }
1066
1067 static int
1068 edit_ptn_custom_type(menudesc *m, void *arg)
1069 {
1070 struct single_part_fs_edit *edit = arg;
1071 size_t attr_no = m->cursel - edit->first_custom_attr;
1072 char line[STRSIZE];
1073
1074 switch (edit->pset->parts->pscheme->custom_attributes[attr_no].type) {
1075 case pet_bool:
1076 edit->pset->parts->pscheme->custom_attribute_toggle(
1077 edit->pset->parts, edit->id, attr_no);
1078 break;
1079 case pet_cardinal:
1080 case pet_str:
1081 edit->pset->parts->pscheme->format_custom_attribute(
1082 edit->pset->parts, edit->id, attr_no, &edit->info,
1083 line, sizeof(line));
1084 msg_prompt_win(
1085 edit->pset->parts->pscheme->custom_attributes[attr_no].
1086 label, -1, 18, 0, 0, line, line, sizeof(line));
1087 edit->pset->parts->pscheme->custom_attribute_set_str(
1088 edit->pset->parts, edit->id, attr_no, line);
1089 break;
1090 }
1091
1092 return 0;
1093 }
1094
1095
1096 /*
1097 * Some column width depend on translation, we will set these in
1098 * fmt_fspart_header and later use it when formatting single entries
1099 * in fmt_fspart_row.
1100 * The table consist of 3 "size like" columns, all fixed width, then
1101 * ptnheaders_fstype, part_header_col_flag, and finally the mount point
1102 * (which is variable width).
1103 */
1104 static int fstype_width, flags_width;
1105 static char fspart_separator[MENUSTRSIZE];
1106 static char fspart_title[2*MENUSTRSIZE];
1107
1108 /*
1109 * Format the header of the main partition editor menu.
1110 */
1111 static void
1112 fmt_fspart_header(menudesc *menu, void *arg)
1113 {
1114 struct partition_usage_set *pset = arg;
1115 char total[6], free_space[6], scol[13], ecol[13], szcol[13],
1116 sepline[MENUSTRSIZE], *p, desc[MENUSTRSIZE];
1117 const char *fstype, *flags;
1118 int i;
1119 size_t ptn;
1120 bool with_clone, with_inst_flag = pset->parts->parent == NULL;
1121
1122 with_clone = false;
1123 for (ptn = 0; ptn < pset->num && !with_clone; ptn++)
1124 if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
1125 with_clone = true;
1126 humanize_number(total, sizeof total,
1127 pset->parts->disk_size * 512,
1128 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1129 humanize_number(free_space, sizeof free_space,
1130 pset->cur_free_space * 512,
1131 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1132
1133 if (with_clone)
1134 strlcpy(desc, msg_string(MSG_clone_flag_desc), sizeof desc);
1135 else
1136 desc[0] = 0;
1137 if (pset->parts->pscheme->part_flag_desc)
1138 strlcat(desc, msg_string(pset->parts->pscheme->part_flag_desc),
1139 sizeof desc);
1140
1141 msg_display_subst(MSG_fspart, 7, pset->parts->disk,
1142 msg_string(pset->parts->pscheme->name),
1143 msg_string(pset->parts->pscheme->short_name),
1144 with_inst_flag ? msg_string(MSG_ptn_instflag_desc) : "",
1145 desc, total, free_space);
1146
1147 snprintf(scol, sizeof scol, "%s (%s)",
1148 msg_string(MSG_ptnheaders_start), multname);
1149 snprintf(ecol, sizeof ecol, "%s (%s)",
1150 msg_string(MSG_ptnheaders_end), multname);
1151 snprintf(szcol, sizeof szcol, "%s (%s)",
1152 msg_string(MSG_ptnheaders_size), multname);
1153
1154 fstype = msg_string(MSG_ptnheaders_fstype);
1155 flags = msg_string(MSG_part_header_col_flag);
1156 fstype_width = max(strlen(fstype), 8);
1157 flags_width = strlen(flags);
1158 for (i = 0, p = sepline; i < fstype_width; i++)
1159 *p++ = '-';
1160 for (i = 0, *p++ = ' '; i < flags_width; i++)
1161 *p++ = '-';
1162 *p = 0;
1163
1164 snprintf(fspart_separator, sizeof(fspart_separator),
1165 "------------ ------------ ------------ %s ----------------",
1166 sepline);
1167
1168 snprintf(fspart_title, sizeof(fspart_title),
1169 " %12.12s %12.12s %12.12s %*s %*s %s\n"
1170 " %s", scol, ecol, szcol, fstype_width, fstype,
1171 flags_width, flags, msg_string(MSG_ptnheaders_filesystem),
1172 fspart_separator);
1173
1174 msg_table_add("\n\n");
1175 }
1176
1177 /*
1178 * Format one partition entry in the main partition editor.
1179 */
1180 static void
1181 fmt_fspart_row(menudesc *m, int ptn, void *arg)
1182 {
1183 struct partition_usage_set *pset = arg;
1184 struct disk_part_info info;
1185 daddr_t poffset, psize, pend;
1186 const char *desc;
1187 static const char *Yes;
1188 char flag_str[MENUSTRSIZE], *fp;
1189 unsigned inst_flags;
1190 size_t clone_cnt;
1191 bool with_inst_flag = pset->parts->parent == NULL;
1192
1193 if (Yes == NULL)
1194 Yes = msg_string(MSG_Yes);
1195
1196 if ((pset->infos[ptn].flags & PUIFLG_CLONE_PARTS) &&
1197 pset->infos[ptn].cur_part_id == NO_PART) {
1198 psize = pset->infos[ptn].size / sizemult;
1199 if (pset->infos[ptn].clone_ndx <
1200 pset->infos[ptn].clone_src->num_sel)
1201 clone_cnt = 1;
1202 else
1203 clone_cnt = pset->infos[ptn].clone_src->num_sel;
1204 if (pset->infos[ptn].cur_part_id == NO_PART)
1205 wprintw(m->mw, " %12" PRIu64
1206 " [%zu %s]", psize, clone_cnt,
1207 msg_string(MSG_clone_target_disp));
1208 else {
1209 poffset = pset->infos[ptn].cur_start / sizemult;
1210 pend = (pset->infos[ptn].cur_start +
1211 pset->infos[ptn].size) / sizemult - 1;
1212 wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
1213 " [%zu %s]",
1214 poffset, pend, psize, clone_cnt,
1215 msg_string(MSG_clone_target_disp));
1216 }
1217 if (m->title == fspart_title)
1218 m->opts[ptn].opt_flags |= OPT_IGNORE;
1219 else
1220 m->opts[ptn].opt_flags &= ~OPT_IGNORE;
1221 return;
1222 }
1223
1224 if (!real_partition(pset, ptn))
1225 return;
1226
1227 if (!pset->parts->pscheme->get_part_info(pset->parts,
1228 pset->infos[ptn].cur_part_id, &info))
1229 return;
1230
1231 /*
1232 * We use this function in multiple menus, but only want it
1233 * to play with enable/disable in a single one:
1234 */
1235 if (m->title == fspart_title) {
1236 /*
1237 * Enable / disable this line if it is something
1238 * like RAW_PART
1239 */
1240 if ((info.flags &
1241 (PTI_WHOLE_DISK|PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
1242 || (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS))
1243 m->opts[ptn].opt_flags |= OPT_IGNORE;
1244 else
1245 m->opts[ptn].opt_flags &= ~OPT_IGNORE;
1246 }
1247
1248 poffset = info.start / sizemult;
1249 psize = info.size / sizemult;
1250 if (psize == 0)
1251 pend = 0;
1252 else
1253 pend = (info.start + info.size) / sizemult - 1;
1254
1255 if (info.flags & PTI_WHOLE_DISK)
1256 desc = msg_string(MSG_NetBSD_partition_cant_change);
1257 else if (info.flags & PTI_RAW_PART)
1258 desc = msg_string(MSG_Whole_disk_cant_change);
1259 else if (info.flags & PTI_BOOT)
1260 desc = msg_string(MSG_Boot_partition_cant_change);
1261 else
1262 desc = getfslabelname(info.fs_type, info.fs_sub_type);
1263
1264 fp = flag_str;
1265 inst_flags = pset->infos[ptn].instflags;
1266 if (with_inst_flag && info.start == pm->ptstart &&
1267 info.nat_type->generic_ptype == PT_root) {
1268 static char inst_flag;
1269
1270 if (inst_flag == 0)
1271 inst_flag = msg_string(MSG_install_flag)[0];
1272 *fp++ = inst_flag;
1273 }
1274 if (inst_flags & PUIINST_NEWFS)
1275 *fp++ = msg_string(MSG_newfs_flag)[0];
1276 if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
1277 *fp++ = msg_string(MSG_clone_flag)[0];
1278 *fp = 0;
1279 if (pset->parts->pscheme->get_part_attr_str != NULL)
1280 pset->parts->pscheme->get_part_attr_str(pset->parts,
1281 pset->infos[ptn].cur_part_id, fp, sizeof(flag_str)-1);
1282
1283 /* if the fstype description does not fit, check if we can overrun */
1284 if (strlen(desc) > (size_t)fstype_width &&
1285 flag_str[0] == 0 && (info.last_mounted == NULL ||
1286 info.last_mounted[0] == 0))
1287 wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
1288 " %s",
1289 poffset, pend, psize, desc);
1290 else
1291 wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
1292 " %*.*s %*s %s",
1293 poffset, pend, psize, fstype_width, fstype_width, desc,
1294 -flags_width, flag_str,
1295 (inst_flags & PUIINST_MOUNT) && info.last_mounted &&
1296 info.last_mounted[0] ? info.last_mounted : "");
1297 }
1298
1299 static int
1300 part_ext_clone(menudesc *m, void *arg)
1301 {
1302 struct selected_partitions selected, *clone_src;
1303 struct clone_target_menu_data data;
1304 struct partition_usage_set *pset = arg;
1305 struct part_usage_info *p;
1306 struct disk_part_info sinfo, cinfo;
1307 struct disk_partitions *csrc;
1308 struct disk_part_free_space space;
1309 menu_ent *men;
1310 daddr_t clone_size, free_size, offset, align;
1311 int num_men, i;
1312 size_t s, clone_cnt;
1313 part_id cid;
1314 struct clone_data {
1315 struct disk_part_info info;
1316 part_id new_id;
1317 size_t ndx;
1318 };
1319 struct clone_data *clones = NULL;
1320
1321 if (!select_partitions(&selected, pm->parts))
1322 return 0;
1323
1324 clone_size = selected_parts_size(&selected);
1325 num_men = pset->num+1;
1326 men = calloc(num_men, sizeof *men);
1327 if (men == NULL)
1328 return 0;
1329 for (i = 0; i < num_men; i++) {
1330 men[i].opt_action = clone_target_select;
1331 if (i == 0)
1332 free_size = pset->infos[i].cur_start;
1333 else if (i > 0 && (size_t)i < pset->num)
1334 free_size = pset->infos[i].cur_start -
1335 pset->infos[i-1].cur_start - pset->infos[i-1].size;
1336 else
1337 free_size = pset->parts->free_space;
1338 if (free_size < clone_size)
1339 men[i].opt_flags = OPT_IGNORE;
1340 }
1341 men[num_men-1].opt_name = MSG_clone_target_end;
1342
1343 memset(&data, 0, sizeof data);
1344 data.usage = *pset;
1345 data.res = -1;
1346
1347 data.usage.menu = new_menu(MSG_clone_target_hdr,
1348 men, num_men, 3, 2, 0, 65, MC_SCROLL,
1349 NULL, fmt_fspart_row, NULL, NULL, MSG_cancel);
1350 process_menu(data.usage.menu, &data);
1351 free_menu(data.usage.menu);
1352 free(men);
1353
1354 if (data.res < 0)
1355 goto err;
1356
1357 /* create temporary infos for all clones that work out */
1358 clone_cnt = 0;
1359 clones = calloc(selected.num_sel, sizeof(*clones));
1360 if (clones == NULL)
1361 goto err;
1362
1363 clone_src = malloc(sizeof(selected));
1364 if (clone_src == NULL)
1365 goto err;
1366 *clone_src = selected;
1367
1368 /* find selected offset from data.res and insert clones there */
1369 align = pset->parts->pscheme->get_part_alignment(pset->parts);
1370 offset = -1;
1371 if (data.res > 0)
1372 offset = pset->infos[data.res-1].cur_start
1373 + pset->infos[data.res-1].size;
1374 else
1375 offset = 0;
1376 for (s = 0; s < selected.num_sel; s++) {
1377 csrc = selected.selection[s].parts;
1378 cid = selected.selection[s].id;
1379 csrc->pscheme->get_part_info(csrc, cid, &sinfo);
1380 if (!pset->parts->pscheme->adapt_foreign_part_info(
1381 pset->parts, &cinfo, csrc->pscheme, &sinfo))
1382 continue;
1383 size_t cnt = pset->parts->pscheme->get_free_spaces(
1384 pset->parts, &space, 1, cinfo.size-align, align,
1385 offset, -1);
1386 if (cnt == 0)
1387 continue;
1388 cinfo.start = space.start;
1389 cid = pset->parts->pscheme->add_partition(
1390 pset->parts, &cinfo, NULL);
1391 if (cid == NO_PART)
1392 continue;
1393 pset->parts->pscheme->get_part_info(pset->parts, cid, &cinfo);
1394 clones[clone_cnt].info = cinfo;
1395 clones[clone_cnt].new_id = cid;
1396 clones[clone_cnt].ndx = s;
1397 clone_cnt++;
1398 offset = rounddown(cinfo.start+cinfo.size+align, align);
1399 }
1400
1401 /* insert new clone records at offset data.res */
1402 men = realloc(m->opts, (m->numopts+clone_cnt)*sizeof(*m->opts));
1403 if (men == NULL)
1404 goto err;
1405 pset->menu_opts = men;
1406 m->opts = men;
1407 m->numopts += clone_cnt;
1408
1409 p = realloc(pset->infos, (pset->num+clone_cnt)*sizeof(*pset->infos));
1410 if (p == NULL)
1411 goto err;
1412 pset->infos = p;
1413
1414 men += data.res;
1415 p += data.res;
1416 memmove(men+clone_cnt, men,
1417 sizeof(*men)*(m->numopts-data.res-clone_cnt));
1418 if (pset->num > (size_t)data.res)
1419 memmove(p+clone_cnt, p, sizeof(*p)*(pset->num-data.res));
1420 memset(men, 0, sizeof(*men)*clone_cnt);
1421 memset(p, 0, sizeof(*p)*clone_cnt);
1422 for (s = 0; s < clone_cnt; s++) {
1423 p[s].cur_part_id = clones[s].new_id;
1424 p[s].cur_start = clones[s].info.start;
1425 p[s].size = clones[s].info.size;
1426 p[s].cur_flags = clones[s].info.flags;
1427 p[s].flags = PUIFLG_CLONE_PARTS;
1428 p[s].parts = pset->parts;
1429 p[s].clone_src = clone_src;
1430 p[s].clone_ndx = s;
1431 }
1432 free(clones);
1433 m->cursel = ((size_t)data.res >= pset->num) ? 0 : data.res+clone_cnt;
1434 pset->num += clone_cnt;
1435 m->h = 0;
1436 resize_menu_height(m);
1437
1438 return -1;
1439
1440 err:
1441 free(clones);
1442 free_selected_partitions(&selected);
1443 return 0;
1444 }
1445
1446 static int
1447 edit_fspart_pack(menudesc *m, void *arg)
1448 {
1449 struct partition_usage_set *pset = arg;
1450 char buf[STRSIZE];
1451
1452 if (!pset->parts->pscheme->get_disk_pack_name(pset->parts,
1453 buf, sizeof buf))
1454 return 0;
1455
1456 msg_prompt_win(MSG_edit_disk_pack_hdr,
1457 -1, 18, 0, -1, buf, buf, sizeof(buf));
1458
1459 pset->parts->pscheme->set_disk_pack_name(pset->parts, buf);
1460 return 0;
1461 }
1462
1463 static int
1464 edit_fspart_add(menudesc *m, void *arg)
1465 {
1466 struct partition_usage_set *pset = arg;
1467 struct part_usage_info *ninfo;
1468 menu_ent *nmenopts;
1469 size_t cnt, off;
1470
1471 ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
1472 if (ninfo == NULL)
1473 return 0;
1474 pset->infos = ninfo;
1475 off = pset->parts->num_part;
1476 cnt = pset->num-pset->parts->num_part;
1477 if (cnt > 0)
1478 memmove(pset->infos+off+1,pset->infos+off,
1479 cnt*sizeof(*pset->infos));
1480 memset(pset->infos+off, 0, sizeof(*pset->infos));
1481
1482 nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
1483 if (nmenopts == NULL)
1484 return 0;
1485 memmove(nmenopts+off+1, nmenopts+off,
1486 (m->numopts-off)*sizeof(*nmenopts));
1487 memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
1488 nmenopts[off].opt_action = edit_ptn;
1489 pset->menu_opts = m->opts = nmenopts;
1490 m->numopts++;
1491 m->cursel = off;
1492 pset->num++;
1493
1494 /* now update edit menu to fit */
1495 m->h = 0;
1496 resize_menu_height(m);
1497
1498 /* and directly invoke the partition editor for the new part */
1499 edit_ptn(m, arg);
1500
1501 show_partition_adder(m, pset);
1502
1503 return -1;
1504 }
1505
1506 static void
1507 add_partition_adder(menudesc *m, struct partition_usage_set *pset)
1508 {
1509 struct part_usage_info *ninfo;
1510 menu_ent *nmenopts;
1511 size_t off;
1512
1513 ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
1514 if (ninfo == NULL)
1515 return;
1516 pset->infos = ninfo;
1517 off = pset->parts->num_part+1;
1518
1519 nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
1520 if (nmenopts == NULL)
1521 return;
1522 memmove(nmenopts+off+1, nmenopts+off,
1523 (m->numopts-off)*sizeof(*nmenopts));
1524 memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
1525
1526 nmenopts[off].opt_name = MSG_addpart;
1527 nmenopts[off].opt_flags = OPT_SUB;
1528 nmenopts[off].opt_action = edit_fspart_add;
1529
1530 m->opts = nmenopts;
1531 m->numopts++;
1532 pset->num++;
1533 }
1534
1535 static void
1536 remove_partition_adder(menudesc *m, struct partition_usage_set *pset)
1537 {
1538 size_t off;
1539
1540 off = pset->parts->num_part+1;
1541 memmove(m->opts+off, m->opts+off+1,
1542 (m->numopts-off-1)*sizeof(*m->opts));
1543 m->numopts--;
1544 pset->num--;
1545 }
1546
1547 /*
1548 * Called whenever the "add a partition" option may need to be removed
1549 * or added
1550 */
1551 static void
1552 show_partition_adder(menudesc *m, struct partition_usage_set *pset)
1553 {
1554 bool can_add_partition = pset->parts->pscheme->can_add_partition(
1555 pset->parts);
1556 bool part_adder_present =
1557 (m->opts[pset->parts->num_part].opt_flags & OPT_IGNORE) &&
1558 (m->opts[pset->parts->num_part+1].opt_action == edit_fspart_add);
1559
1560 if (can_add_partition == part_adder_present)
1561 return;
1562
1563 if (can_add_partition)
1564 add_partition_adder(m, pset);
1565 else
1566 remove_partition_adder(m, pset);
1567
1568 /* now update edit menu to fit */
1569 m->h = 0;
1570 resize_menu_height(m);
1571 }
1572
1573 static int
1574 edit_fspart_abort(menudesc *m, void *arg)
1575 {
1576 struct partition_usage_set *pset = arg;
1577
1578 pset->ok = false;
1579 return 1;
1580 }
1581
1582 /*
1583 * Check a disklabel.
1584 * If there are overlapping active partitions,
1585 * Ask the user if they want to edit the partition or give up.
1586 */
1587 int
1588 edit_and_check_label(struct pm_devs *p, struct partition_usage_set *pset)
1589 {
1590 menu_ent *op;
1591 size_t cnt, i;
1592 bool may_add = pset->parts->pscheme->can_add_partition(pset->parts);
1593 bool may_edit_pack =
1594 pset->parts->pscheme->get_disk_pack_name != NULL &&
1595 pset->parts->pscheme->set_disk_pack_name != NULL;
1596
1597 pset->menu_opts = calloc(pset->parts->num_part
1598 +4+may_add+may_edit_pack,
1599 sizeof *pset->menu_opts);
1600 if (pset->menu_opts == NULL)
1601 return 0;
1602
1603 op = pset->menu_opts;
1604 for (i = 0; i < pset->parts->num_part; i++) {
1605 op->opt_action = edit_ptn;
1606 op++;
1607 }
1608 /* separator line between partitions and actions */
1609 op->opt_name = fspart_separator;
1610 op->opt_flags = OPT_IGNORE|OPT_NOSHORT;
1611 op++;
1612
1613 /* followed by new partition adder */
1614 if (may_add) {
1615 op->opt_name = MSG_addpart;
1616 op->opt_flags = OPT_SUB;
1617 op->opt_action = edit_fspart_add;
1618 op++;
1619 }
1620
1621 /* and unit changer */
1622 op->opt_name = MSG_askunits;
1623 op->opt_menu = MENU_sizechoice;
1624 op->opt_flags = OPT_SUB;
1625 op->opt_action = NULL;
1626 op++;
1627
1628 if (may_edit_pack) {
1629 op->opt_name = MSG_editpack;
1630 op->opt_flags = OPT_SUB;
1631 op->opt_action = edit_fspart_pack;
1632 op++;
1633 }
1634
1635 /* add a clone-from-elsewhere option */
1636 op->opt_name = MSG_clone_from_elsewhere;
1637 op->opt_action = part_ext_clone;
1638 op++;
1639
1640 /* and abort option */
1641 op->opt_name = MSG_cancel;
1642 op->opt_flags = OPT_EXIT;
1643 op->opt_action = edit_fspart_abort;
1644 op++;
1645 cnt = op - pset->menu_opts;
1646 assert(cnt == pset->parts->num_part+4+may_add+may_edit_pack);
1647
1648 pset->menu = new_menu(fspart_title, pset->menu_opts, cnt,
1649 0, -1, 0, 74,
1650 MC_ALWAYS_SCROLL|MC_NOBOX|MC_DFLTEXIT|
1651 MC_NOCLEAR|MC_CONTINUOUS,
1652 fmt_fspart_header, fmt_fspart_row, NULL, NULL,
1653 MSG_partition_sizes_ok);
1654
1655 if (pset->menu < 0) {
1656 free(pset->menu_opts);
1657 pset->menu_opts = NULL;
1658 return 0;
1659 }
1660
1661 p->current_cylsize = p->dlcylsize;
1662
1663 for (;;) {
1664 /* first give the user the option to edit the label... */
1665 pset->ok = true;
1666 process_menu(pset->menu, pset);
1667 if (!pset->ok) {
1668 i = 0;
1669 break;
1670 }
1671
1672 /* User thinks the label is OK. */
1673 i = verify_parts(pset);
1674 if (i == 1)
1675 continue;
1676 break;
1677 }
1678 free(pset->menu_opts);
1679 pset->menu_opts = NULL;
1680 free_menu(pset->menu);
1681 pset->menu = -1;
1682
1683 return i != 0;
1684 }
1685
1686 /*
1687 * strip trailing / to avoid confusion in path comparisions later
1688 */
1689 void
1690 canonicalize_last_mounted(char *path)
1691 {
1692 char *p;
1693
1694 if (path == NULL)
1695 return;
1696
1697 if (strcmp(path, "/") == 0)
1698 return; /* in this case a "trailing" slash is allowed */
1699
1700 for (;;) {
1701 p = strrchr(path, '/');
1702 if (p == NULL)
1703 return;
1704 if (p[1] != 0)
1705 return;
1706 p[0] = 0;
1707 }
1708 }
1709
1710 /*
1711 * Try to get 'last mounted on' (or equiv) from fs superblock.
1712 */
1713 const char *
1714 get_last_mounted(int fd, daddr_t partstart, uint *fs_type, uint *fs_sub_type,
1715 uint flags)
1716 {
1717 static char sblk[SBLOCKSIZE]; /* is this enough? */
1718 struct fs *SB = (struct fs *)sblk;
1719 static const off_t sblocks[] = SBLOCKSEARCH;
1720 const off_t *sbp;
1721 const char *mnt = NULL;
1722 int len;
1723
1724 if (fd == -1)
1725 return "";
1726
1727 if (fs_type)
1728 *fs_type = 0;
1729 if (fs_sub_type)
1730 *fs_sub_type = 0;
1731
1732 /* Check UFS1/2 (and hence LFS) superblock */
1733 for (sbp = sblocks; mnt == NULL && *sbp != -1; sbp++) {
1734 if (pread(fd, sblk, sizeof sblk,
1735 (off_t)partstart * (off_t)512 + *sbp) != sizeof sblk)
1736 continue;
1737
1738 /*
1739 * If start of partition and allowed by flags check
1740 * for other fs types
1741 */
1742 if (*sbp == 0 && (flags & GLM_MAYBE_FAT32) &&
1743 sblk[0x42] == 0x29 && memcmp(sblk + 0x52, "FAT", 3) == 0) {
1744 /* Probably a FAT filesystem, report volume name */
1745 size_t i;
1746 for (i = 0x51; i >= 0x47; i--) {
1747 if (sblk[i] != ' ')
1748 break;
1749 sblk[i] = 0;
1750 }
1751 sblk[0x52] = 0;
1752 if (fs_type)
1753 *fs_type = FS_MSDOS;
1754 if (fs_sub_type)
1755 *fs_sub_type = sblk[0x53];
1756 return sblk + 0x47;
1757 } else if (*sbp == 0 && (flags & GLM_MAYBE_NTFS) &&
1758 memcmp(sblk+3, "NTFS ", 8) == 0) {
1759 if (fs_type)
1760 *fs_type = FS_NTFS;
1761 if (fs_sub_type)
1762 *fs_sub_type = MBR_PTYPE_NTFS;
1763 /* XXX dig for volume name attribute ? */
1764 return "";
1765 }
1766
1767 if (!(flags & GLM_LIKELY_FFS))
1768 continue;
1769
1770 /* Maybe we should validate the checksum??? */
1771 switch (SB->fs_magic) {
1772 case FS_UFS1_MAGIC:
1773 case FS_UFS1_MAGIC_SWAPPED:
1774 if (!(SB->fs_old_flags & FS_FLAGS_UPDATED)) {
1775 if (*sbp == SBLOCK_UFS1)
1776 mnt = (const char *)SB->fs_fsmnt;
1777 } else {
1778 /* Check we have the main superblock */
1779 if (SB->fs_sblockloc == *sbp)
1780 mnt = (const char *)SB->fs_fsmnt;
1781 }
1782 if (fs_type)
1783 *fs_type = FS_BSDFFS;
1784 if (fs_sub_type)
1785 *fs_sub_type = 1;
1786 continue;
1787 case FS_UFS2_MAGIC:
1788 case FS_UFS2_MAGIC_SWAPPED:
1789 /* Check we have the main superblock */
1790 if (SB->fs_sblockloc == *sbp) {
1791 mnt = (const char *)SB->fs_fsmnt;
1792 if (fs_type)
1793 *fs_type = FS_BSDFFS;
1794 if (fs_sub_type)
1795 *fs_sub_type = 2;
1796 }
1797 continue;
1798 }
1799 }
1800
1801 if (mnt == NULL)
1802 return "";
1803
1804 /* If sysinst mounted this last then strip prefix */
1805 len = strlen(targetroot_mnt);
1806 if (memcmp(mnt, targetroot_mnt, len) == 0) {
1807 if (mnt[len] == 0)
1808 return "/";
1809 if (mnt[len] == '/')
1810 return mnt + len;
1811 }
1812 return mnt;
1813 #undef SB
1814 }
1815
1816 /* Ask for a partition offset, check bounds and do the needed roundups */
1817 daddr_t
1818 getpartoff(struct disk_partitions *parts, daddr_t defpartstart)
1819 {
1820 char defstart[24], isize[24], maxpart, minspace, maxspace,
1821 *prompt, *label_msg, valid_parts[4], valid_spaces[4],
1822 space_prompt[1024], *head, *hint_part, *hint_space, *tail;
1823 size_t num_freespace, spaces, ndx;
1824 struct disk_part_free_space *freespace;
1825 daddr_t i, localsizemult, ptn_alignment, min, max;
1826 part_id partn;
1827 struct disk_part_info info;
1828 const char *errmsg = NULL;
1829
1830 min = parts->disk_start;
1831 max = min + parts->disk_size;
1832
1833 /* upper bound on the number of free spaces, plus some slope */
1834 num_freespace = parts->num_part * 2 + 5;
1835 freespace = calloc(num_freespace, sizeof(*freespace));
1836 if (freespace == NULL)
1837 return -1;
1838
1839 ptn_alignment = parts->pscheme->get_part_alignment(parts);
1840 spaces = parts->pscheme->get_free_spaces(parts, freespace,
1841 num_freespace, max(sizemult, ptn_alignment), ptn_alignment, -1,
1842 defpartstart);
1843
1844 maxpart = 'a' + parts->num_part -1;
1845 if (parts->num_part > 1) {
1846 snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpart);
1847 } else if (parts->num_part == 1) {
1848 snprintf(valid_parts, sizeof valid_parts, " %c", maxpart);
1849 } else {
1850 strcpy(valid_parts, "---");
1851 }
1852 if (spaces > 1) {
1853 minspace = maxpart + 1;
1854 maxspace = minspace + spaces -1;
1855 snprintf(valid_spaces, sizeof valid_spaces, "%c-%c", minspace,
1856 maxspace);
1857 } else if (spaces == 1) {
1858 maxspace = minspace = maxpart + 1;
1859 snprintf(valid_spaces, sizeof valid_spaces, " %c", minspace);
1860 } else {
1861 minspace = 0;
1862 maxspace = 0;
1863 strcpy(valid_spaces, "---");
1864 }
1865
1866 /* Add description of start/size to user prompt */
1867 const char *mstr = msg_string(MSG_free_space_line);
1868 space_prompt[0] = 0;
1869 for (ndx = 0; ndx < spaces; ndx++) {
1870 char str_start[40], str_end[40], str_size[40], str_tag[4];
1871
1872 sprintf(str_tag, "%c: ", minspace+(int)ndx);
1873 sprintf(str_start, "%" PRIu64, freespace[ndx].start / sizemult);
1874 sprintf(str_end, "%" PRIu64,
1875 (freespace[ndx].start + freespace[ndx].size) / sizemult);
1876 sprintf(str_size, "%" PRIu64, freespace[ndx].size / sizemult);
1877 const char *args[4] = { str_start, str_end, str_size,
1878 multname };
1879 char *line = str_arg_subst(mstr, 4, args);
1880 strlcat(space_prompt, str_tag, sizeof(space_prompt));
1881 size_t len = strlcat(space_prompt, line, sizeof(space_prompt));
1882 free (line);
1883 if (len >= sizeof space_prompt)
1884 break;
1885 }
1886
1887 const char *args[] = { valid_parts, valid_spaces, multname };
1888 hint_part = NULL;
1889 hint_space = NULL;
1890 head = str_arg_subst(msg_string(MSG_label_offset_head),
1891 __arraycount(args), args);
1892 if (parts->num_part)
1893 hint_part = str_arg_subst(msg_string(
1894 MSG_label_offset_part_hint), __arraycount(args), args);
1895 if (spaces)
1896 hint_space = str_arg_subst(msg_string(
1897 MSG_label_offset_space_hint), __arraycount(args), args);
1898 tail = str_arg_subst(msg_string(MSG_label_offset_tail),
1899 __arraycount(args), args);
1900
1901 if (hint_part && hint_space)
1902 asprintf(&label_msg, "%s\n%s\n%s\n\n%s\n%s",
1903 head, hint_part, hint_space, space_prompt, tail);
1904 else if (hint_part)
1905 asprintf(&label_msg, "%s\n%s\n\n%s",
1906 head, hint_part, tail);
1907 else if (hint_space)
1908 asprintf(&label_msg, "%s\n%s\n\n%s\n%s",
1909 head, hint_space, space_prompt, tail);
1910 else
1911 asprintf(&label_msg, "%s\n\n%s",
1912 head, tail);
1913 free(head); free(hint_part); free(hint_space); free(tail);
1914
1915 localsizemult = sizemult;
1916 errmsg = NULL;
1917 for (;;) {
1918 snprintf(defstart, sizeof defstart, "%" PRIu64,
1919 defpartstart/sizemult);
1920 if (errmsg != NULL && errmsg[0] != 0)
1921 asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
1922 else
1923 prompt = label_msg;
1924 msg_prompt_win(prompt, -1, 13, 70, -1,
1925 (defpartstart > 0) ? defstart : NULL, isize, sizeof isize);
1926 if (label_msg != prompt)
1927 free(prompt);
1928 if (strcmp(defstart, isize) == 0) {
1929 /* Don't do rounding if default accepted */
1930 i = defpartstart;
1931 break;
1932 }
1933 if (isize[1] == '\0' && isize[0] >= 'a' &&
1934 isize[0] <= maxpart) {
1935 partn = isize[0] - 'a';
1936 if (parts->pscheme->get_part_info(parts, partn,
1937 &info)) {
1938 i = info.start + info.size;
1939 localsizemult = 1;
1940 } else {
1941 errmsg = msg_string(MSG_invalid_sector_number);
1942 continue;
1943 }
1944 } else if (isize[1] == '\0' && isize[0] >= minspace &&
1945 isize[0] <= maxspace) {
1946 ndx = isize[0] - minspace;
1947 i = freespace[ndx].start;
1948 localsizemult = 1;
1949 } else if (atoi(isize) == -1) {
1950 i = min;
1951 localsizemult = 1;
1952 } else {
1953 i = parse_disk_pos(isize, &localsizemult, pm->dlcylsize, NULL);
1954 if (i < 0) {
1955 errmsg = msg_string(MSG_invalid_sector_number);
1956 continue;
1957 }
1958 }
1959 /* round to cylinder size if localsizemult != 1 */
1960 i = NUMSEC(i, localsizemult, pm->dlcylsize);
1961 /* Adjust to start of slice if needed */
1962 if ((i < min && (min - i) < localsizemult) ||
1963 (i > min && (i - min) < localsizemult)) {
1964 i = min;
1965 }
1966 if (max == 0 || i <= max)
1967 break;
1968 errmsg = msg_string(MSG_startoutsidedisk);
1969 }
1970 free(label_msg);
1971 free(freespace);
1972
1973 return i;
1974 }
1975
1976
1977 /* Ask for a partition size, check bounds and do the needed roundups */
1978 daddr_t
1979 getpartsize(struct disk_partitions *parts, daddr_t partstart, daddr_t dflt)
1980 {
1981 char dsize[24], isize[24], max_size[24], maxpartc, valid_parts[4],
1982 *label_msg, *prompt, *head, *hint, *tail;
1983 const char *errmsg = NULL;
1984 daddr_t i, partend, localsizemult, max, max_r, dflt_r;
1985 struct disk_part_info info;
1986 part_id partn;
1987
1988 max = parts->pscheme->max_free_space_at(parts, partstart);
1989
1990 /* We need to keep both the unrounded and rounded (_r) max and dflt */
1991 dflt_r = (partstart + dflt) / sizemult - partstart / sizemult;
1992 if (max == dflt)
1993 max_r = dflt_r;
1994 else
1995 max_r = max / sizemult;
1996 /* the partition may have been moved and now not fit any longer */
1997 if (dflt > max)
1998 dflt = max;
1999 if (dflt_r > max_r)
2000 dflt_r = max_r;
2001
2002 snprintf(max_size, sizeof max_size, "%" PRIu64, max_r);
2003
2004 maxpartc = 'a' + parts->num_part -1;
2005 if (parts->num_part > 1) {
2006 snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpartc);
2007 } else if (parts->num_part == 1) {
2008 snprintf(valid_parts, sizeof valid_parts, " %c", maxpartc);
2009 } else {
2010 strcpy(valid_parts, "---");
2011 }
2012
2013 const char *args[] = { valid_parts, max_size, multname };
2014 hint = NULL;
2015 head = str_arg_subst(msg_string(MSG_label_size_head),
2016 __arraycount(args), args);
2017 if (parts->num_part)
2018 hint = str_arg_subst(msg_string(MSG_label_size_part_hint),
2019 __arraycount(args), args);
2020 tail = str_arg_subst(msg_string(MSG_label_size_tail),
2021 __arraycount(args), args);
2022
2023 if (hint != NULL)
2024 asprintf(&label_msg, "%s\n%s\n\n%s", head, hint, tail);
2025 else
2026 asprintf(&label_msg, "%s\n\n%s", head, tail);
2027 free(head); free(hint); free(tail);
2028
2029 localsizemult = sizemult;
2030 i = -1;
2031 for (;;) {
2032 snprintf(dsize, sizeof dsize, "%" PRIu64, dflt_r);
2033
2034 if (errmsg != NULL && errmsg[0] != 0)
2035 asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
2036 else
2037 prompt = label_msg;
2038 msg_prompt_win(prompt, -1, 12, 70, -1,
2039 (dflt != 0) ? dsize : 0, isize, sizeof isize);
2040 if (prompt != label_msg)
2041 free(prompt);
2042
2043 if (strcmp(isize, dsize) == 0) {
2044 free(label_msg);
2045 return dflt;
2046 }
2047 if (parts->num_part && isize[1] == '\0' && isize[0] >= 'a' &&
2048 isize[0] <= maxpartc) {
2049 partn = isize[0] - 'a';
2050 if (parts->pscheme->get_part_info(parts, partn,
2051 &info)) {
2052 i = info.start - partstart -1;
2053 localsizemult = 1;
2054 max_r = max;
2055 }
2056 } else if (atoi(isize) == -1) {
2057 i = max;
2058 localsizemult = 1;
2059 max_r = max;
2060 } else {
2061 i = parse_disk_pos(isize, &localsizemult,
2062 pm->dlcylsize, NULL);
2063 if (localsizemult != sizemult)
2064 max_r = max;
2065 }
2066 if (i < 0) {
2067 errmsg = msg_string(MSG_Invalid_numeric);
2068 continue;
2069 } else if (i > max_r) {
2070 errmsg = msg_string(MSG_Too_large);
2071 continue;
2072 }
2073 /*
2074 * partend is aligned to a cylinder if localsizemult
2075 * is not 1 sector
2076 */
2077 partend = NUMSEC((partstart + i*localsizemult) / localsizemult,
2078 localsizemult, pm->dlcylsize);
2079 /* Align to end-of-disk or end-of-slice if close enough */
2080 if (partend > (pm->dlsize - sizemult)
2081 && partend < (pm->dlsize + sizemult))
2082 partend = pm->dlsize;
2083 if (partend > (partstart + max - sizemult)
2084 && partend < (partstart + max + sizemult))
2085 partend = partstart + max;
2086 /* sanity checks */
2087 if (partend > (partstart + pm->dlsize)) {
2088 partend = pm->dlsize;
2089 errmsg = msg_string(MSG_endoutsidedisk);
2090 continue;
2091 }
2092 free(label_msg);
2093 if (partend < partstart)
2094 return 0;
2095 return (partend - partstart);
2096 }
2097 /* NOTREACHED */
2098 }
2099
2100 /*
2101 * convert a string to a number of sectors, with a possible unit
2102 * 150M = 150 Megabytes
2103 * 2000c = 2000 cylinders
2104 * 150256s = 150256 sectors
2105 * Without units, use the default (sizemult).
2106 * returns the raw input value, and the unit used. Caller needs to multiply!
2107 * On invalid inputs, returns -1.
2108 */
2109 daddr_t
2110 parse_disk_pos(
2111 const char *str,
2112 daddr_t *localsizemult,
2113 daddr_t cyl_size,
2114 bool *extend_this)
2115 {
2116 daddr_t val;
2117 char *cp;
2118 bool mult_found;
2119
2120 if (str[0] == '\0') {
2121 return -1;
2122 }
2123 val = strtoull(str, &cp, 10);
2124 mult_found = false;
2125 if (extend_this)
2126 *extend_this = false;
2127 while (*cp != 0) {
2128 if (*cp == 'G' || *cp == 'g') {
2129 if (mult_found)
2130 return -1;
2131 *localsizemult = GIG / pm->sectorsize;
2132 goto next;
2133 }
2134 if (*cp == 'M' || *cp == 'm') {
2135 if (mult_found)
2136 return -1;
2137 *localsizemult = MEG / pm->sectorsize;
2138 goto next;
2139 }
2140 if (*cp == 'c' || *cp == 'C') {
2141 if (mult_found)
2142 return -1;
2143 *localsizemult = pm->dlcylsize;
2144 goto next;
2145 }
2146 if (*cp == 's' || *cp == 'S') {
2147 if (mult_found)
2148 return -1;
2149 *localsizemult = 1;
2150 goto next;
2151 }
2152 if (*cp == '+' && extend_this) {
2153 *extend_this = true;
2154 cp++;
2155 break;
2156 }
2157
2158 /* not a known unit */
2159 return -1;
2160
2161 next:
2162 mult_found = true;
2163 cp++;
2164 continue;
2165 }
2166 if (*cp != 0)
2167 return -1;
2168
2169 return val;
2170 }
2171