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