part_edit.c revision 1.20 1 /* $NetBSD: part_edit.c,v 1.20 2020/10/10 18:48:32 martin Exp $ */
2
3 /*
4 * Copyright (c) 2019 The NetBSD Foundation, Inc.
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 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29
30 /* part_edit.c -- generic partition editing code */
31
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <assert.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <util.h>
39 #include "defs.h"
40 #include "md.h"
41 #include "msg_defs.h"
42 #include "menu_defs.h"
43 #include "defsizes.h"
44 #include "endian.h"
45
46
47 /*
48 * A structure passed to various menu functions for partition editing
49 */
50 struct part_edit_info {
51 struct disk_partitions *parts; /* the partitions we edit */
52 struct disk_part_info cur; /* current value (maybe incomplete) */
53 part_id cur_id; /* which partition is it? */
54 int first_custom_opt; /* scheme specific menu options
55 * start here */
56 bool cancelled; /* do not apply changes */
57 bool num_changed; /* number of partitions has changed */
58 };
59
60 #ifndef NO_CLONES
61 struct single_clone_data {
62 struct selected_partitions clone_src;
63 part_id *clone_ids; /* partition IDs in target */
64 };
65 #endif
66 struct outer_parts_data {
67 struct arg_rv av;
68 #ifndef NO_CLONES
69 struct single_clone_data *clones;
70 size_t num_clone_entries;
71 #endif
72 };
73
74 static menu_ent *part_menu_opts; /* the currently edited partitions */
75 static menu_ent *outer_fill_part_menu_opts(const struct disk_partitions *parts, size_t *cnt);
76 static void draw_outer_part_line(menudesc *m, int opt, void *arg);
77
78 static char outer_part_sep_line[MENUSTRSIZE],
79 outer_part_title[2*MENUSTRSIZE];
80
81 static int
82 maxline(const char *p, int *count)
83 {
84 int m = 0, i = 0;
85
86 for (;; p++) {
87 if (*p == '\n' || *p == 0) {
88 if (i > m)
89 m = i;
90 (*count)++;
91 if (*p == 0)
92 return m;
93 i = 0;
94 } else {
95 i++;
96 }
97 }
98 }
99
100 int
101 err_msg_win(const char *errmsg)
102 {
103 const char *cont;
104 int l, l1, lines;
105
106 errmsg = msg_string(errmsg);
107 cont = msg_string(MSG_Hit_enter_to_continue);
108
109 lines = 0;
110 l = maxline(errmsg, &lines);
111 l1 = maxline(cont, &lines);
112 if (l < l1)
113 l = l1;
114
115 msg_fmt_prompt_win("%s.\n%s", -1, 18, l + 5, 2+lines,
116 NULL, NULL, 1, "%s%s", errmsg, cont);
117 return 0;
118 }
119
120 static int
121 set_part_type(menudesc *m, void *arg)
122 {
123 struct part_edit_info *info = arg;
124 const struct part_type_desc *desc;
125 char buf[STRSIZE];
126 const char *err;
127
128 if (m->cursel == 0)
129 return 1; /* no change */
130
131 desc = info->parts->pscheme->get_part_type(m->cursel-1);
132 if (desc == NULL) {
133 /* Create custom type */
134 if (info->cur.nat_type != NULL)
135 strlcpy(buf, info->cur.nat_type->short_desc,
136 sizeof(buf));
137 else
138 buf[0] = 0;
139 for (;;) {
140 msg_prompt_win(info->parts->pscheme->new_type_prompt,
141 -1, 18, 0, 0,
142 buf, buf, sizeof(buf));
143 if (buf[0] == 0)
144 break;
145 desc = info->parts->pscheme->create_custom_part_type(
146 buf, &err);
147 if (desc != NULL)
148 break;
149 err_msg_win(err);
150 }
151 }
152
153 info->cur.nat_type = desc;
154 return 1;
155 }
156
157 static void
158 set_type_label(menudesc *m, int opt, void *arg)
159 {
160 struct part_edit_info *info = arg;
161 const struct part_type_desc *desc;
162
163 if (opt == 0) {
164 wprintw(m->mw, "%s", msg_string(MSG_Dont_change));
165 return;
166 }
167
168 desc = info->parts->pscheme->get_part_type(opt-1);
169 if (desc == NULL) {
170 wprintw(m->mw, "%s", msg_string(MSG_Other_kind));
171 return;
172 }
173 wprintw(m->mw, "%s", desc->description);
174 }
175
176 static int
177 edit_part_type(menudesc *m, void *arg)
178 {
179 struct part_edit_info *info = arg;
180 menu_ent *type_opts;
181 int type_menu = -1;
182 size_t popt_cnt, i;
183
184 /*
185 * We add one line at the start of the menu, and one at the
186 * bottom, see "set_type_label" above.
187 */
188 popt_cnt = info->parts->pscheme->get_part_types_count() + 2;
189 type_opts = calloc(popt_cnt, sizeof(*type_opts));
190 for (i = 0; i < popt_cnt; i++) {
191 type_opts[i].opt_action = set_part_type;
192 }
193 type_menu = new_menu(NULL, type_opts, popt_cnt,
194 13, 12, 0, 30,
195 MC_SUBMENU | MC_SCROLL | MC_NOEXITOPT | MC_NOCLEAR,
196 NULL, set_type_label, NULL,
197 NULL, NULL);
198
199 if (type_menu != -1) {
200 process_menu(type_menu, arg);
201 info->num_changed = true; /* force reload of menu */
202 }
203
204 free_menu(type_menu);
205 free(type_opts);
206
207 return -1;
208 }
209
210 static int
211 edit_part_start(menudesc *m, void *arg)
212 {
213 struct part_edit_info *marg = arg;
214 struct disk_part_info pinfo;
215 daddr_t max_size;
216
217 marg->parts->pscheme->get_part_info(marg->parts, marg->cur_id, &pinfo);
218 marg->cur.start = getpartoff(marg->parts, marg->cur.start);
219 max_size = marg->parts->pscheme->max_free_space_at(marg->parts,
220 pinfo.start);
221 max_size += pinfo.start - marg->cur.start;
222 if (marg->cur.size > max_size)
223 marg->cur.size = max_size;
224
225 return 0;
226 }
227
228 static int
229 edit_part_size(menudesc *m, void *arg)
230 {
231 struct part_edit_info *marg = arg;
232 struct disk_part_info pinfo;
233
234 marg->parts->pscheme->get_part_info(marg->parts, marg->cur_id, &pinfo);
235 marg->cur.size = getpartsize(marg->parts, pinfo.start,
236 marg->cur.start, marg->cur.size);
237
238 return 0;
239 }
240
241 static int
242 edit_part_install(menudesc *m, void *arg)
243 {
244 struct part_edit_info *marg = arg;
245
246 marg->cur.flags ^= PTI_INSTALL_TARGET;
247 return 0;
248 }
249
250 static void
251 menu_opts_reload(menudesc *m, const struct disk_partitions *parts)
252 {
253 size_t new_num;
254
255 free(part_menu_opts);
256 part_menu_opts = outer_fill_part_menu_opts(parts, &new_num);
257 m->opts = part_menu_opts;
258 m->numopts = new_num;
259 }
260
261 static int
262 delete_part(menudesc *m, void *arg)
263 {
264 struct part_edit_info *marg = arg;
265 const char *err_msg = NULL;
266
267 if (marg->cur_id == NO_PART)
268 return 0;
269
270 if (!marg->parts->pscheme->delete_partition(marg->parts, marg->cur_id,
271 &err_msg))
272 err_msg_win(err_msg);
273
274 marg->num_changed = true; /* reload list of partitions */
275 marg->cancelled = true; /* do not write back cur data */
276
277 return 0;
278 }
279
280 static void draw_outer_ptn_line(menudesc *m, int line, void *arg);
281 static void draw_outer_ptn_header(menudesc *m, void *arg);
282
283 static int
284 part_rollback(menudesc *m, void *arg)
285 {
286 struct part_edit_info *marg = arg;
287
288 marg->cancelled = true;
289 return 0;
290 }
291
292 static menu_ent common_ptn_edit_opts[] = {
293 #define PTN_OPT_TYPE 0
294 { .opt_action=edit_part_type },
295 #define PTN_OPT_START 1
296 { .opt_action=edit_part_start },
297 #define PTN_OPT_SIZE 2
298 { .opt_action=edit_part_size },
299 #define PTN_OPT_END 3
300 { .opt_flags=OPT_IGNORE }, /* read only "end" */
301
302 /*
303 * Only the part upto here will be used when adding a new partition
304 */
305
306 #define PTN_OPT_INSTALL 4
307 { .opt_action=edit_part_install },
308
309 #define PTN_OPTS_COMMON PTN_OPT_INSTALL /* cut off from here for add */
310 };
311
312 static int
313 edit_custom_opt(menudesc *m, void *arg)
314 {
315 struct part_edit_info *marg = arg;
316 size_t attr_no = m->cursel - marg->first_custom_opt;
317 char line[STRSIZE];
318
319 switch (marg->parts->pscheme->custom_attributes[attr_no].type) {
320 case pet_bool:
321 marg->parts->pscheme->custom_attribute_toggle(
322 marg->parts, marg->cur_id, attr_no);
323 break;
324 case pet_cardinal:
325 case pet_str:
326 marg->parts->pscheme->format_custom_attribute(
327 marg->parts, marg->cur_id, attr_no, &marg->cur,
328 line, sizeof(line));
329 msg_prompt_win(
330 marg->parts->pscheme->custom_attributes[attr_no].label,
331 -1, 18, 0, 0, line, line, sizeof(line));
332 marg->parts->pscheme->custom_attribute_set_str(
333 marg->parts, marg->cur_id, attr_no, line);
334 break;
335 }
336
337 return 0;
338 }
339
340 static menu_ent ptn_edit_opts[] = {
341 { .opt_name=MSG_askunits, .opt_menu=MENU_sizechoice,
342 .opt_flags=OPT_SUB },
343
344 { .opt_name=MSG_Delete_partition,
345 .opt_action = delete_part, .opt_flags = OPT_EXIT },
346
347 { .opt_name=MSG_cancel,
348 .opt_action = part_rollback, .opt_flags = OPT_EXIT },
349 };
350
351 static menu_ent ptn_add_opts[] = {
352 { .opt_name=MSG_askunits, .opt_menu=MENU_sizechoice,
353 .opt_flags=OPT_SUB },
354
355 { .opt_name=MSG_cancel,
356 .opt_action = part_rollback, .opt_flags = OPT_EXIT },
357 };
358
359 /*
360 * Concatenate common_ptn_edit_opts, the partitioning scheme specific
361 * custom options and the given suffix to a single menu options array.
362 */
363 static menu_ent *
364 fill_part_edit_menu_opts(struct disk_partitions *parts,
365 bool with_custom_attrs,
366 const menu_ent *suffix, size_t suffix_count, size_t *res_cnt)
367 {
368 size_t i;
369 menu_ent *opts, *p;
370 size_t count, hdr_cnt;
371
372 if (with_custom_attrs) {
373 hdr_cnt = __arraycount(common_ptn_edit_opts);
374 count = hdr_cnt + parts->pscheme->custom_attribute_count
375 + suffix_count;
376 } else {
377 hdr_cnt = PTN_OPTS_COMMON;
378 count = hdr_cnt + suffix_count;
379 }
380
381 opts = calloc(count, sizeof(*opts));
382 if (opts == NULL) {
383 *res_cnt = 0;
384 return NULL;
385 }
386
387 memcpy(opts, common_ptn_edit_opts,
388 sizeof(*opts)*hdr_cnt);
389 p = opts + hdr_cnt;
390 if (with_custom_attrs) {
391 for (i = 0; i < parts->pscheme->custom_attribute_count; i++) {
392 p->opt_action = edit_custom_opt;
393 p++;
394 }
395 }
396 memcpy(p, suffix, sizeof(*opts)*suffix_count);
397
398 *res_cnt = count;
399 return opts;
400 }
401
402 static int
403 edit_part_entry(menudesc *m, void *arg)
404 {
405 struct outer_parts_data *pdata = arg;
406 struct part_edit_info data = { .parts = pdata->av.arg,
407 .cur_id = m->cursel,
408 .first_custom_opt = __arraycount(common_ptn_edit_opts) };
409 int ptn_menu;
410 const char *err;
411 menu_ent *opts;
412 size_t num_opts;
413
414 opts = fill_part_edit_menu_opts(data.parts, true, ptn_edit_opts,
415 __arraycount(ptn_edit_opts), &num_opts);
416 if (opts == NULL)
417 return 1;
418
419 if (data.cur_id < data.parts->num_part)
420 data.parts->pscheme->get_part_info(data.parts, data.cur_id,
421 &data.cur);
422
423 ptn_menu = new_menu(NULL, opts, num_opts,
424 15, 2, 0, 54,
425 MC_SUBMENU | MC_SCROLL | MC_NOCLEAR,
426 draw_outer_ptn_header, draw_outer_ptn_line, NULL,
427 NULL, MSG_Partition_OK);
428 if (ptn_menu == -1) {
429 free(opts);
430 return 1;
431 }
432
433 process_menu(ptn_menu, &data);
434 free_menu(ptn_menu);
435 free(opts);
436
437 if (!data.cancelled && data.cur_id < data.parts->num_part)
438 if (!data.parts->pscheme->set_part_info(data.parts,
439 data.cur_id, &data.cur, &err))
440 err_msg_win(err);
441
442 if (data.num_changed) {
443 menu_opts_reload(m, data.parts);
444 m->cursel = data.parts->num_part > 0 ? 0 : 2;
445 return -1;
446 }
447
448 return 0;
449 }
450
451 #ifndef NO_CLONES
452 static int
453 add_part_clone(menudesc *menu, void *arg)
454 {
455 struct outer_parts_data *pdata = arg;
456 struct disk_partitions *parts = pdata->av.arg;
457 struct clone_target_menu_data data;
458 menu_ent *men;
459 int num_men, i;
460 struct disk_part_info sinfo, cinfo;
461 struct disk_partitions *csrc;
462 struct disk_part_free_space space;
463 daddr_t offset, align;
464 size_t s;
465 part_id cid;
466 struct selected_partitions selected;
467 struct single_clone_data *new_clones;
468
469 if (!select_partitions(&selected, parts))
470 return 0;
471
472 new_clones = realloc(pdata->clones,
473 sizeof(*pdata->clones)*(pdata->num_clone_entries+1));
474 if (new_clones == NULL)
475 return 0;
476 pdata->num_clone_entries++;
477 pdata->clones = new_clones;
478 new_clones += (pdata->num_clone_entries-1);
479 memset(new_clones, 0, sizeof *new_clones);
480 new_clones->clone_src = selected;
481
482 memset(&data, 0, sizeof data);
483 data.usage.parts = parts;
484
485 /* if we already have partitions, ask for the target position */
486 if (parts->num_part > 0) {
487 data.res = -1;
488 num_men = parts->num_part+1;
489 men = calloc(num_men, sizeof *men);
490 if (men == NULL)
491 return 0;
492 for (i = 0; i < num_men; i++)
493 men[i].opt_action = clone_target_select;
494 men[num_men-1].opt_name = MSG_clone_target_end;
495
496 data.usage.menu = new_menu(MSG_clone_target_hdr,
497 men, num_men, 3, 2, 0, 65, MC_SCROLL,
498 NULL, draw_outer_part_line, NULL, NULL, MSG_cancel);
499 process_menu(data.usage.menu, &data);
500 free_menu(data.usage.menu);
501 free(men);
502
503 if (data.res < 0)
504 goto err;
505 } else {
506 data.res = 0;
507 }
508
509 /* find selected offset from data.res and insert clones there */
510 align = parts->pscheme->get_part_alignment(parts);
511 offset = -1;
512 if (data.res > 0) {
513 for (cid = 0; cid < (size_t)data.res; cid++) {
514 if (!parts->pscheme->get_part_info(parts, cid, &sinfo))
515 continue;
516 offset = sinfo.start + sinfo.size;
517 }
518 } else {
519 offset = 0;
520 }
521
522 new_clones->clone_ids = calloc(selected.num_sel,
523 sizeof(*new_clones->clone_ids));
524 if (new_clones->clone_ids == NULL)
525 goto err;
526 for (s = 0; s < selected.num_sel; s++) {
527 csrc = selected.selection[s].parts;
528 cid = selected.selection[s].id;
529 csrc->pscheme->get_part_info(csrc, cid, &sinfo);
530 if (!parts->pscheme->adapt_foreign_part_info(
531 parts, &cinfo, csrc->pscheme, &sinfo))
532 continue;
533 size_t cnt = parts->pscheme->get_free_spaces(
534 parts, &space, 1, cinfo.size-align, align,
535 offset, -1);
536 if (cnt == 0)
537 continue;
538 cinfo.start = space.start;
539 cid = parts->pscheme->add_partition(
540 parts, &cinfo, NULL);
541 new_clones->clone_ids[s] = cid;
542 if (cid == NO_PART)
543 continue;
544 parts->pscheme->get_part_info(parts, cid, &cinfo);
545 offset = roundup(cinfo.start+cinfo.size, align);
546 }
547
548 /* reload menu and start again */
549 menu_opts_reload(menu, parts);
550 menu->cursel = parts->num_part+1;
551 if (parts->num_part == 0)
552 menu->cursel++;
553 return -1;
554
555 err:
556 free_selected_partitions(&selected);
557 return -1;
558 }
559 #endif
560
561 static int
562 add_part_entry(menudesc *m, void *arg)
563 {
564 struct outer_parts_data *pdata = arg;
565 struct part_edit_info data = { .parts = pdata->av.arg,
566 .first_custom_opt = PTN_OPTS_COMMON };
567 int ptn_menu;
568 daddr_t ptn_alignment, start;
569 menu_ent *opts;
570 size_t num_opts;
571 struct disk_part_free_space space;
572 const char *err;
573
574 opts = fill_part_edit_menu_opts(data.parts, false, ptn_add_opts,
575 __arraycount(ptn_add_opts), &num_opts);
576 if (opts == NULL)
577 return 1;
578
579 ptn_alignment = data.parts->pscheme->get_part_alignment(data.parts);
580 start = pm->ptstart > 0 ? pm->ptstart : -1;
581 data.cur_id = NO_PART;
582 memset(&data.cur, 0, sizeof(data.cur));
583 data.cur.nat_type = data.parts->pscheme->
584 get_generic_part_type(PT_root);
585 if (data.parts->pscheme->get_free_spaces(data.parts, &space, 1,
586 max(sizemult, ptn_alignment), ptn_alignment, start, -1) > 0) {
587 data.cur.start = space.start;
588 data.cur.size = space.size;
589 } else {
590 return 0;
591 }
592
593 ptn_menu = new_menu(NULL, opts, num_opts,
594 15, -1, 0, 54,
595 MC_SUBMENU | MC_SCROLL | MC_NOCLEAR,
596 draw_outer_ptn_header, draw_outer_ptn_line, NULL,
597 NULL, MSG_Partition_OK);
598 if (ptn_menu == -1) {
599 free(opts);
600 return 1;
601 }
602
603 process_menu(ptn_menu, &data);
604 free_menu(ptn_menu);
605 free(opts);
606
607 if (!data.cancelled &&
608 data.parts->pscheme->add_partition(data.parts, &data.cur, &err)
609 == NO_PART)
610 err_msg_win(err);
611
612 menu_opts_reload(m, data.parts);
613 m->cursel = data.parts->num_part+1;
614 if (data.parts->num_part == 0)
615 m->cursel++;
616 return -1;
617 }
618
619 static void
620 draw_outer_ptn_line(menudesc *m, int line, void *arg)
621 {
622 struct part_edit_info *marg = arg;
623 char value[STRSIZE];
624 static int col_width;
625 static const char *yes, *no, *ptn_type, *ptn_start, *ptn_size,
626 *ptn_end, *ptn_install;
627
628 if (yes == NULL) {
629 int i;
630
631 #define CHECK(str) i = strlen(str); if (i > col_width) col_width = i;
632
633 col_width = 0;
634 yes = msg_string(MSG_Yes); CHECK(yes);
635 no = msg_string(MSG_No); CHECK(no);
636 ptn_type = msg_string(MSG_ptn_type); CHECK(ptn_type);
637 ptn_start = msg_string(MSG_ptn_start); CHECK(ptn_start);
638 ptn_size = msg_string(MSG_ptn_size); CHECK(ptn_size);
639 ptn_end = msg_string(MSG_ptn_end); CHECK(ptn_end);
640 ptn_install = msg_string(MSG_ptn_install); CHECK(ptn_install);
641
642 #undef CHECK
643
644 for (size_t n = 0;
645 n < marg->parts->pscheme->custom_attribute_count; n++) {
646 i = strlen(msg_string(
647 marg->parts->pscheme->custom_attributes[n].label));
648 if (i > col_width)
649 col_width = i;
650 }
651 col_width += 3;
652 }
653
654 if (line >= marg->first_custom_opt) {
655 size_t attr_no = line-marg->first_custom_opt;
656 marg->parts->pscheme->format_custom_attribute(
657 marg->parts, marg->cur_id, attr_no, &marg->cur,
658 value, sizeof(value));
659 wprintw(m->mw, "%*s : %s", col_width,
660 msg_string(
661 marg->parts->pscheme->custom_attributes[attr_no].label),
662 value);
663 return;
664 }
665
666 switch (line) {
667 case PTN_OPT_TYPE:
668 wprintw(m->mw, "%*s : %s", col_width, ptn_type,
669 marg->cur.nat_type != NULL
670 ? marg->cur.nat_type->description
671 : "-");
672 break;
673 case PTN_OPT_START:
674 wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width, ptn_start,
675 marg->cur.start / (daddr_t)sizemult, multname);
676 break;
677 case PTN_OPT_SIZE:
678 wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width, ptn_size,
679 marg->cur.size / (daddr_t)sizemult, multname);
680 break;
681 case PTN_OPT_END:
682 wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width, ptn_end,
683 (marg->cur.start + marg->cur.size - 1) / (daddr_t)sizemult,
684 multname);
685 break;
686 case PTN_OPT_INSTALL:
687 wprintw(m->mw, "%*s : %s", col_width, ptn_install,
688 (marg->cur.nat_type->generic_ptype == PT_root &&
689 (marg->cur.flags & PTI_INSTALL_TARGET)) ? yes : no);
690 break;
691 }
692
693 }
694
695 static void
696 draw_outer_ptn_header(menudesc *m, void *arg)
697 {
698 struct part_edit_info *marg = arg;
699 size_t attr_no;
700 bool may_change_type;
701
702 #define DISABLE(opt,cond) \
703 if (cond) \
704 m->opts[opt].opt_flags |= OPT_IGNORE; \
705 else \
706 m->opts[opt].opt_flags &= ~OPT_IGNORE;
707
708 /* e.g. MBR extended partitions can only change if empty */
709 may_change_type = marg->cur_id == NO_PART
710 || marg->parts->pscheme->part_type_can_change == NULL
711 || marg->parts->pscheme->part_type_can_change(
712 marg->parts, marg->cur_id);
713
714 DISABLE(PTN_OPT_TYPE, !may_change_type);
715 if (!may_change_type && m->cursel == PTN_OPT_TYPE)
716 m->cursel++;
717 if (marg->cur_id != NO_PART) {
718 for (int i = 0; i < m->numopts; i++) {
719 if (m->opts[i].opt_action == delete_part) {
720 DISABLE(i, !may_change_type);
721 }
722 }
723 }
724
725 /* Can only install into NetBSD partition */
726 if (marg->cur_id != NO_PART) {
727 DISABLE(PTN_OPT_INSTALL, marg->cur.nat_type == NULL
728 || marg->cur.nat_type->generic_ptype != PT_root);
729 }
730
731 if (marg->cur_id == NO_PART)
732 return;
733
734 for (attr_no = 0; attr_no <
735 marg->parts->pscheme->custom_attribute_count; attr_no++) {
736 bool writable =
737 marg->parts->pscheme->custom_attribute_writable(
738 marg->parts, marg->cur_id, attr_no);
739 DISABLE(attr_no+marg->first_custom_opt, !writable);
740 }
741 }
742
743 static void
744 draw_outer_part_line(menudesc *m, int opt, void *arg)
745 {
746 struct outer_parts_data *pdata = arg;
747 struct disk_partitions *parts = pdata->av.arg;
748 int len;
749 part_id pno = opt;
750 struct disk_part_info info;
751 char buf[SSTRSIZE], *astr, colval[STRSIZE], line[STRSIZE];
752 size_t astr_avail, x;
753 static char install_flag = 0;
754
755 #define PART_ROW_USED_FMT "%13" PRIu64 " %13" PRIu64 " %-4s"
756
757 len = snprintf(0, 0, PART_ROW_USED_FMT, (daddr_t)0, (daddr_t)0, "");
758
759 if (pno >= parts->num_part ||
760 !parts->pscheme->get_part_info(parts, pno, &info)) {
761 wprintw(m->mw, "%*s", len, "");
762 // XXX
763 return;
764 }
765
766 if ((info.flags & PTI_INSTALL_TARGET) &&
767 info.nat_type->generic_ptype == PT_root) {
768 if (install_flag == 0)
769 install_flag = msg_string(MSG_install_flag)[0];
770 astr_avail = sizeof(buf)-1;
771 buf[0] = install_flag;
772 buf[1] = 0;
773 astr = buf+1;
774 } else {
775 buf[0] = 0;
776 astr = buf;
777 astr_avail = sizeof(buf);
778 }
779 if (parts->pscheme->get_part_attr_str != NULL)
780 parts->pscheme->get_part_attr_str(parts, pno, astr,
781 astr_avail);
782
783 daddr_t start = info.start / sizemult;
784 daddr_t size = info.size / sizemult;
785 wprintw(m->mw, PART_ROW_USED_FMT,
786 start, size, buf);
787
788 line[0] = 0; x = 0;
789 for (size_t col = 0; col < parts->pscheme->edit_columns_count; col++) {
790 if (parts->pscheme->format_partition_table_str(parts, pno,
791 col, colval, sizeof(colval)) && colval[0] != 0
792 && x < sizeof(line)-2) {
793 for (size_t i = strlen(line); i < x; i++)
794 line[i] = ' ';
795 line[x] = ' ';
796 strlcpy(line+x+1, colval, sizeof(line)-x-1);
797 }
798 x += parts->pscheme->edit_columns[col].width + 1;
799 }
800 wprintw(m->mw, "%s", line);
801 }
802
803 static int
804 part_edit_abort(menudesc *m, void *arg)
805 {
806 struct outer_parts_data *pdata = arg;
807
808 pdata->av.rv = -1;
809 return 0;
810 }
811
812 static menu_ent *
813 outer_fill_part_menu_opts(const struct disk_partitions *parts, size_t *cnt)
814 {
815 menu_ent *opts, *op;
816 size_t num_opts;
817 size_t i;
818 bool may_add;
819
820 may_add = parts->pscheme->can_add_partition(parts);
821 num_opts = 3 + parts->num_part;
822 #ifndef NO_CLONES
823 num_opts++;
824 #endif
825 if (parts->num_part == 0)
826 num_opts++;
827 if (may_add)
828 num_opts++;
829 opts = calloc(num_opts, sizeof *opts);
830 if (opts == NULL) {
831 *cnt = 0;
832 return NULL;
833 }
834
835 /* add all exisiting partitions */
836 for (op = opts, i = 0; i < parts->num_part && i < (num_opts-2);
837 op++, i++) {
838 op->opt_flags = OPT_SUB;
839 op->opt_action = edit_part_entry;
840 }
841
842 /* if empty, hint that partitions are missing */
843 if (parts->num_part == 0) {
844 op->opt_name = MSG_nopart;
845 op->opt_flags = OPT_IGNORE|OPT_NOSHORT;
846 op++;
847 }
848
849 /* separator line between partitions and actions */
850 op->opt_name = outer_part_sep_line;
851 op->opt_flags = OPT_IGNORE|OPT_NOSHORT;
852 op++;
853
854 /* followed by new partition adder */
855 if (may_add) {
856 op->opt_name = MSG_addpart;
857 op->opt_flags = OPT_SUB;
858 op->opt_action = add_part_entry;
859 op++;
860 }
861
862 #ifndef NO_CLONES
863 /* and a partition cloner */
864 op->opt_name = MSG_clone_from_elsewhere;
865 op->opt_action = add_part_clone;
866 op++;
867 #endif
868
869 /* and unit changer */
870 op->opt_name = MSG_askunits;
871 op->opt_menu = MENU_sizechoice;
872 op->opt_flags = OPT_SUB;
873 op->opt_action = NULL;
874 op++;
875
876 /* and abort option */
877 op->opt_name = MSG_cancel;
878 op->opt_flags = OPT_EXIT;
879 op->opt_action = part_edit_abort;
880 op++;
881
882 /* counts are consistent? */
883 assert((op - opts) >= 0 && (size_t)(op - opts) == num_opts);
884
885 *cnt = num_opts;
886 return opts;
887 }
888
889 static void
890 draw_outer_part_header(menudesc *m, void *arg)
891 {
892 struct outer_parts_data *pdata = arg;
893 struct disk_partitions *parts = pdata->av.arg;
894 char start[SSTRSIZE], size[SSTRSIZE], col[SSTRSIZE],
895 *disk_info, total[SSTRSIZE], avail[SSTRSIZE];
896 size_t sep;
897 const char *args[3];
898
899 msg_display_subst(MSG_editparttable, 4,
900 parts->disk,
901 msg_string(parts->pscheme->name),
902 msg_string(parts->pscheme->short_name),
903 parts->pscheme->part_flag_desc ?
904 msg_string(parts->pscheme->part_flag_desc)
905 : "");
906
907 snprintf(total, sizeof(total), "%" PRIu64 " %s",
908 parts->disk_size / sizemult, multname);
909 snprintf(avail, sizeof(total), "%" PRIu64 " %s",
910 parts->free_space / sizemult, multname);
911 args[0] = parts->disk;
912 args[1] = total;
913 args[2] = avail;
914 disk_info = str_arg_subst(msg_string(MSG_part_header), 3, args);
915 msg_table_add(disk_info);
916 free(disk_info);
917
918
919 strcpy(outer_part_sep_line, "------------- ------------- ----");
920 sep = strlen(outer_part_sep_line);
921 snprintf(start, sizeof(start), "%s(%s)",
922 msg_string(MSG_part_header_col_start), multname);
923 snprintf(size, sizeof(size), "%s(%s)",
924 msg_string(MSG_part_header_col_size), multname);
925 snprintf(outer_part_title, sizeof(outer_part_title),
926 " %13s %13s %-4s", start, size,
927 msg_string(MSG_part_header_col_flag));
928
929 for (size_t i = 0; i < parts->pscheme->edit_columns_count; i++) {
930 char *np = outer_part_sep_line+sep;
931 unsigned int w = parts->pscheme->edit_columns[i].width;
932 snprintf(col, sizeof(col), " %*s", -w,
933 msg_string(parts->pscheme->edit_columns[i].title));
934 strlcat(outer_part_title, col, sizeof(outer_part_title));
935 if (sep < sizeof(outer_part_sep_line)-1) {
936 *np++ = ' ';
937 sep++;
938 }
939 for (unsigned int p = 0; p < w &&
940 sep < sizeof(outer_part_sep_line)-1; p++)
941 *np++ = '-', sep++;
942 *np = 0;
943 }
944
945 strlcat(outer_part_title, "\n ", sizeof(outer_part_title));
946 strlcat(outer_part_title, outer_part_sep_line,
947 sizeof(outer_part_title));
948
949 msg_table_add("\n\n");
950 }
951
952 /*
953 * Use the whole disk for NetBSD, but (if any) create required helper
954 * partitions (usually for booting and stuff).
955 */
956 bool
957 parts_use_wholedisk(struct disk_partitions *parts,
958 size_t add_ext_parts, const struct disk_part_info *ext_parts)
959 {
960 part_id nbsd;
961 struct disk_part_info info;
962 struct disk_part_free_space space;
963 daddr_t align, start;
964 size_t i;
965
966 parts->pscheme->delete_all_partitions(parts);
967 align = parts->pscheme->get_part_alignment(parts);
968 start = pm->ptstart > 0 ? pm->ptstart : -1;
969
970 if (ext_parts != NULL) {
971 for (i = 0; i < add_ext_parts; i++) {
972 info = ext_parts[i];
973 if (parts->pscheme->get_free_spaces(parts, &space,
974 1, info.size, align, start, -1) != 1)
975 return false;
976 info.start = space.start;
977 if (info.nat_type == NULL)
978 info.nat_type = parts->pscheme->
979 get_fs_part_type(PT_undef, info.fs_type,
980 info.fs_sub_type);
981 if (parts->pscheme->add_partition(parts, &info, NULL)
982 == NO_PART)
983 return false;
984 }
985 }
986
987 if (parts->pscheme->get_free_spaces(parts, &space, 1, 3*align,
988 align, start, -1) != 1)
989 return false;
990
991 memset(&info, 0, sizeof(info));
992 info.start = space.start;
993 info.size = space.size;
994 info.flags = PTI_INSTALL_TARGET;
995 if (parts->pscheme->secondary_scheme != NULL)
996 info.flags |= PTI_SEC_CONTAINER;
997 info.nat_type = parts->pscheme->get_generic_part_type(PT_root);
998 nbsd = parts->pscheme->add_partition(parts, &info, NULL);
999
1000 if (nbsd == NO_PART)
1001 return false;
1002
1003 if (!parts->pscheme->get_part_info(parts, nbsd, &info))
1004 return false;
1005
1006 if (parts->pscheme->secondary_scheme != NULL) {
1007 /* force empty secondary partitions */
1008 parts->pscheme->secondary_partitions(parts, info.start, true);
1009 }
1010
1011 return true;
1012 }
1013
1014 static int
1015 set_keep_existing(menudesc *m, void *arg)
1016 {
1017 ((arg_rep_int*)arg)->rv = LY_KEEPEXISTING;
1018 return 0;
1019 }
1020
1021 static int
1022 set_use_only_part(menudesc *m, void *arg)
1023 {
1024 ((arg_rep_int*)arg)->rv = LY_SETSIZES;
1025 return 0;
1026 }
1027
1028 static int
1029 set_use_entire_disk(menudesc *m, void *arg)
1030 {
1031 ((arg_rep_int*)arg)->rv = LY_USEFULL;
1032 return 0;
1033 }
1034
1035 static enum layout_type
1036 ask_fullpart(struct disk_partitions *parts)
1037 {
1038 arg_rep_int ai;
1039 const char *args[2];
1040 int menu;
1041 size_t num_opts;
1042 menu_ent options[3], *opt;
1043 daddr_t start, size;
1044
1045 args[0] = msg_string(pm->parts->pscheme->name);
1046 args[1] = msg_string(pm->parts->pscheme->short_name);
1047 ai.args.argv = args;
1048 ai.args.argc = 2;
1049 ai.rv = LY_SETSIZES;
1050
1051 memset(options, 0, sizeof(options));
1052 num_opts = 0;
1053 opt = &options[0];
1054 if (parts->pscheme->guess_install_target != NULL &&
1055 parts->pscheme->guess_install_target(parts, &start, &size)) {
1056 opt->opt_name = MSG_Keep_existing_partitions;
1057 opt->opt_flags = OPT_EXIT;
1058 opt->opt_action = set_keep_existing;
1059 opt++;
1060 num_opts++;
1061 }
1062 opt->opt_name = MSG_Use_only_part_of_the_disk;
1063 opt->opt_flags = OPT_EXIT;
1064 opt->opt_action = set_use_only_part;
1065 opt++;
1066 num_opts++;
1067
1068 opt->opt_name = MSG_Use_the_entire_disk;
1069 opt->opt_flags = OPT_EXIT;
1070 opt->opt_action = set_use_entire_disk;
1071 opt++;
1072 num_opts++;
1073
1074 menu = new_menu(MSG_Select_your_choice, options, num_opts,
1075 -1, -10, 0, 0, MC_NOEXITOPT, NULL, NULL, NULL, NULL, NULL);
1076 if (menu != -1) {
1077 get_menudesc(menu)->expand_act = expand_all_option_texts;
1078 process_menu(menu, &ai);
1079 free_menu(menu);
1080 }
1081
1082 return ai.rv;
1083 }
1084
1085 /*
1086 * return (see post_edit_verify):
1087 * 0 -> abort
1088 * 1 -> re-edit
1089 * 2 -> continue installation
1090 */
1091 static int
1092 verify_outer_parts(struct disk_partitions *parts, bool quiet)
1093 {
1094 part_id i;
1095 int num_bsdparts;
1096 daddr_t first_bsdstart, first_bsdsize, inst_start, inst_size;
1097
1098 first_bsdstart = inst_start = -1;
1099 first_bsdsize = inst_size = 0;
1100 num_bsdparts = 0;
1101 for (i = 0; i < parts->num_part; i++) {
1102 struct disk_part_info info;
1103 if (!parts->pscheme->get_part_info(parts, i, &info))
1104 continue;
1105 if (!(info.flags & PTI_SEC_CONTAINER))
1106 continue;
1107 if (info.nat_type->generic_ptype != PT_root)
1108 continue;
1109 num_bsdparts++;
1110
1111 if (first_bsdstart < 0) {
1112 first_bsdstart = info.start;
1113 first_bsdsize = info.size;
1114 }
1115 if (inst_start< 0 && (info.flags & PTI_INSTALL_TARGET)) {
1116 inst_start = info.start;
1117 inst_size = info.size;
1118 }
1119 }
1120
1121 if (num_bsdparts == 0 ||
1122 (num_bsdparts > 1 && inst_start < 0)) {
1123 if (quiet && num_bsdparts == 0)
1124 return 0;
1125 if (quiet && parts->pscheme->guess_install_target &&
1126 parts->pscheme->guess_install_target(parts,
1127 &inst_start, &inst_size)) {
1128 pm->ptstart = inst_start;
1129 pm->ptsize = inst_size;
1130 } else {
1131 if (num_bsdparts == 0)
1132 msg_display_subst(MSG_nobsdpart, 2,
1133 msg_string(parts->pscheme->name),
1134 msg_string(parts->pscheme->short_name));
1135 else
1136 msg_display_subst(MSG_multbsdpart, 2,
1137 msg_string(parts->pscheme->name),
1138 msg_string(parts->pscheme->short_name));
1139
1140 return ask_reedit(parts);
1141 }
1142 }
1143
1144 if (pm->ptstart == 0) {
1145 if (inst_start > 0) {
1146 pm->ptstart = inst_start;
1147 pm->ptsize = inst_size;
1148 } else if (first_bsdstart > 0) {
1149 pm->ptstart = first_bsdstart;
1150 pm->ptsize = first_bsdsize;
1151 } else if (parts->pscheme->guess_install_target &&
1152 parts->pscheme->guess_install_target(
1153 parts, &inst_start, &inst_size)) {
1154 pm->ptstart = inst_start;
1155 pm->ptsize = inst_size;
1156 }
1157 }
1158
1159 /*
1160 * post_edit_verify returns:
1161 * 0 -> abort
1162 * 1 -> re-edit
1163 * 2 -> continue installation
1164 */
1165 if (parts->pscheme->post_edit_verify)
1166 return parts->pscheme->post_edit_verify(parts, quiet);
1167
1168 return 2;
1169 }
1170
1171 static bool
1172 ask_outer_partsizes(struct disk_partitions *parts)
1173 {
1174 int j;
1175 int part_menu;
1176 size_t num_opts;
1177 #ifndef NO_CLONES
1178 size_t i, ci;
1179 #endif
1180 struct outer_parts_data data;
1181
1182 part_menu_opts = outer_fill_part_menu_opts(parts, &num_opts);
1183 part_menu = new_menu(outer_part_title, part_menu_opts, num_opts,
1184 0, -1, 15, 70,
1185 MC_NOBOX|MC_ALWAYS_SCROLL|MC_NOCLEAR|MC_CONTINUOUS,
1186 draw_outer_part_header, draw_outer_part_line, NULL,
1187 NULL, MSG_Partition_table_ok);
1188 if (part_menu == -1) {
1189 free(part_menu_opts);
1190 return false;
1191 }
1192
1193 /* Default to MB, and use bios geometry for cylinder size */
1194 set_default_sizemult(parts->disk, MEG, parts->bytes_per_sector);
1195 if (pm->current_cylsize == 0)
1196 pm->current_cylsize = 16065; /* noone cares nowadays */
1197 memset(&data, 0, sizeof data);
1198 data.av.arg = parts;
1199
1200 for (;;) {
1201 data.av.rv = 0;
1202 process_menu(part_menu, &data);
1203 if (data.av.rv < 0)
1204 break;
1205
1206 j = verify_outer_parts(parts, false);
1207 if (j == 0) {
1208 data.av.rv = -1;
1209 return false;
1210 } else if (j == 1) {
1211 continue;
1212 }
1213 break;
1214 }
1215
1216 #ifndef NO_CLONES
1217 /* handle cloned partitions content copies now */
1218 for (i = 0; i < data.num_clone_entries; i++) {
1219 for (ci = 0; ci < data.clones[i].clone_src.num_sel; ci++) {
1220 if (data.clones[i].clone_src.with_data)
1221 clone_partition_data(parts,
1222 data.clones[i].clone_ids[ci],
1223 data.clones[i].clone_src.selection[ci].
1224 parts,
1225 data.clones[i].clone_src.selection[ci].id);
1226 }
1227 }
1228
1229 /* free clone data */
1230 if (data.clones) {
1231 for (i = 0; i < data.num_clone_entries; i++)
1232 free_selected_partitions(&data.clones[i].clone_src);
1233 free(data.clones);
1234 }
1235 #endif
1236
1237 free_menu(part_menu);
1238 free(part_menu_opts);
1239
1240 return data.av.rv == 0;
1241 }
1242
1243 bool
1244 edit_outer_parts(struct disk_partitions *parts)
1245 {
1246 part_id i;
1247 enum layout_type layout;
1248 int num_foreign_parts;
1249
1250 /* If targeting a wedge, do not ask for further partitioning */
1251 if (pm && (pm->no_part || pm->no_mbr))
1252 return true;
1253
1254 /* Make sure parts has been properly initialized */
1255 assert(parts && parts->pscheme);
1256
1257 if (parts->pscheme->secondary_scheme == NULL)
1258 return true; /* no outer parts */
1259
1260 if (partman_go) {
1261 layout = LY_SETSIZES;
1262 } else {
1263 /* Ask full/part */
1264 const struct disk_partitioning_scheme *sec =
1265 parts->pscheme->secondary_scheme;
1266
1267 uint64_t m_size =
1268 DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE + XNEEDMB;
1269 char min_size[5], build_size[5];
1270 const char
1271 *prim_name = msg_string(parts->pscheme->name),
1272 *prim_short = msg_string(parts->pscheme->short_name),
1273 *sec_name = msg_string(sec->name),
1274 *sec_short = msg_string(sec->short_name);
1275
1276 humanize_number(min_size, sizeof(min_size),
1277 m_size * MEG,
1278 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1279 humanize_number(build_size, sizeof(build_size),
1280 SYSTEM_BUILD_SIZE * MEG, "", HN_AUTOSCALE,
1281 HN_B | HN_NOSPACE | HN_DECIMAL);
1282
1283 msg_display_subst(MSG_fullpart, 7,
1284 pm->diskdev,
1285 prim_name, sec_name,
1286 prim_short, sec_short,
1287 min_size, build_size);
1288 msg_display_add("\n\n");
1289
1290 layout = ask_fullpart(parts);
1291 }
1292
1293 if (layout == LY_USEFULL) {
1294 struct disk_part_info info;
1295
1296 /* Count nonempty, non-BSD partitions. */
1297 num_foreign_parts = 0;
1298 for (i = 0; i < parts->num_part; i++) {
1299 if (!parts->pscheme->get_part_info(parts, i, &info))
1300 continue;
1301 if (info.size == 0)
1302 continue;
1303 if (info.flags & (PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
1304 continue;
1305 if (info.nat_type != NULL
1306 && info.nat_type->generic_ptype != PT_root
1307 && info.nat_type->generic_ptype != PT_swap)
1308 num_foreign_parts++;
1309 }
1310
1311 /* Ask if we really want to blow away non-NetBSD stuff */
1312 if (num_foreign_parts > 0) {
1313 msg_display(MSG_ovrwrite);
1314 if (!ask_noyes(NULL)) {
1315 if (logfp)
1316 (void)fprintf(logfp,
1317 "User answered no to destroy "
1318 "other data, aborting.\n");
1319 return false;
1320 }
1321 }
1322 if (!md_parts_use_wholedisk(parts)) {
1323 hit_enter_to_continue(MSG_No_free_space, NULL);
1324 return false;
1325 }
1326 if (parts->pscheme->post_edit_verify) {
1327 return
1328 parts->pscheme->post_edit_verify(parts, true) == 2;
1329 }
1330 return true;
1331 } else if (layout == LY_SETSIZES) {
1332 return ask_outer_partsizes(parts);
1333 } else {
1334 return verify_outer_parts(parts, true) == 2;
1335 }
1336 }
1337
1338 static int
1339 set_part_scheme(menudesc *m, void *arg)
1340 {
1341 size_t *res = arg;
1342
1343 *res = (size_t)m->cursel;
1344 return 1;
1345 }
1346
1347 const struct disk_partitioning_scheme *
1348 select_part_scheme(
1349 struct pm_devs *dev,
1350 const struct disk_partitioning_scheme *skip,
1351 bool bootable,
1352 const char *hdr)
1353 {
1354 int ps_menu = -1;
1355 menu_ent *opt;
1356 char **str, *ms = NULL;
1357 const struct disk_partitioning_scheme **options, *res;
1358 const char *title;
1359 size_t ndx, selected = ~0U, used;
1360 const struct disk_partitioning_scheme *p;
1361 bool showing_limit = false;
1362
1363 if (hdr == NULL)
1364 hdr = MSG_select_part_scheme;
1365
1366 opt = calloc(num_available_part_schemes, sizeof *opt);
1367 if (!opt)
1368 return NULL;
1369 str = calloc(num_available_part_schemes, sizeof *str);
1370 if (!str) {
1371 free(opt);
1372 return NULL;
1373 }
1374 options = calloc(num_available_part_schemes, sizeof *options);
1375 if (!options) {
1376 free(str);
1377 free(opt);
1378 return NULL;
1379 }
1380
1381 for (used = 0, ndx = 0; ndx < num_available_part_schemes; ndx++) {
1382 p = available_part_schemes[ndx];
1383 /*
1384 * Do not match exactly, we want to skip all lookalikes
1385 * too (only_disklabel_parts vs. disklabel_parts)
1386 */
1387 if (skip != NULL &&
1388 p->create_new_for_disk == skip->create_new_for_disk)
1389 continue;
1390 if (bootable && p->have_boot_support != NULL &&
1391 !p->have_boot_support(dev->diskdev))
1392 continue;
1393 #ifdef HAVE_MBR
1394 if (dev->no_mbr && p->name == MSG_parttype_mbr)
1395 continue;
1396 #endif
1397 if (p->size_limit && dev->dlsize*(dev->sectorsize/512) >
1398 p->size_limit) {
1399 char buf[255], hum_lim[5];
1400
1401 humanize_number(hum_lim, sizeof(hum_lim),
1402 (uint64_t)p->size_limit*512UL,
1403 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1404 sprintf(buf, "%s [%s %s]", msg_string(p->name),
1405 msg_string(MSG_size_limit), hum_lim);
1406 str[used] = strdup(buf);
1407 showing_limit = true;
1408 } else {
1409 str[used] = strdup(msg_string(p->name));
1410 }
1411 if (!str[used])
1412 goto out;
1413
1414 opt[used].opt_name = str[used];
1415 opt[used].opt_action = set_part_scheme;
1416 options[used] = p;
1417 used++;
1418 }
1419
1420 /* do not bother to ask if there are no options */
1421 if (used <= 1) {
1422 selected = (used == 1) ? 0 : ~0U;
1423 goto out;
1424 }
1425
1426 if (showing_limit) {
1427 char hum_lim[5], *tmp;
1428 size_t total;
1429
1430 const char *p1 = msg_string(hdr);
1431 const char *p2 = msg_string(MSG_select_part_limit);
1432
1433 humanize_number(hum_lim, sizeof(hum_lim),
1434 (uint64_t)dev->dlsize*dev->sectorsize, "",
1435 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1436
1437 const char *args[] = { dev->diskdev, hum_lim };
1438 char *p3 = str_arg_subst(msg_string(MSG_part_limit_disksize),
1439 __arraycount(args), args);
1440
1441 total = strlen(p1) + strlen(p2) + strlen(p3)
1442 + sizeof(hum_lim) + 5;
1443 ms = tmp = malloc(total);
1444 title = tmp;
1445 strcpy(tmp, p1); tmp += strlen(p1);
1446 *tmp++ = '\n'; *tmp++ = '\n';
1447 strcpy(tmp, p2); tmp += strlen(p2);
1448 *tmp++ = '\n'; *tmp++ = '\n';
1449 strcpy(tmp, p3);
1450 free(p3);
1451 assert(strlen(ms) < total);
1452 } else {
1453 title = msg_string(hdr);
1454 }
1455 ps_menu = new_menu(title, opt, used,
1456 5, 5, 0, 0, 0, NULL, NULL, NULL, NULL, MSG_exit_menu_generic);
1457 if (ps_menu != -1)
1458 process_menu(ps_menu, &selected);
1459 out:
1460 res = selected >= used ? NULL : options[selected];
1461 for (ndx = 0; ndx < used; ndx++)
1462 free(str[ndx]);
1463 if (showing_limit && ms)
1464 free(ms);
1465 free(str);
1466 free(opt);
1467 free(options);
1468 if (ps_menu != -1)
1469 free_menu(ps_menu);
1470
1471 return res;
1472 }
1473