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