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