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