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