part_edit.c revision 1.4 1 /* $NetBSD: part_edit.c,v 1.4 2019/06/20 00:43:55 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_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 if (parts->pscheme->secondary_scheme != NULL) {
875 /* force empty secondary partitions */
876 parts->pscheme->secondary_partitions(parts, info.start, true);
877 }
878
879 pm->ptstart = info.start;
880 pm->ptsize = info.size;
881 return true;
882 }
883
884 static int
885 set_keep_existing(menudesc *m, void *arg)
886 {
887 ((arg_rep_int*)arg)->rv = LY_KEEPEXISTING;
888 return 0;
889 }
890
891 static int
892 set_use_only_part(menudesc *m, void *arg)
893 {
894 ((arg_rep_int*)arg)->rv = LY_SETSIZES;
895 return 0;
896 }
897
898 static int
899 set_use_entire_disk(menudesc *m, void *arg)
900 {
901 ((arg_rep_int*)arg)->rv = LY_USEFULL;
902 return 0;
903 }
904
905 static enum layout_type
906 ask_fullpart(struct disk_partitions *parts)
907 {
908 arg_rep_int ai;
909 const char *args[2];
910 int menu;
911 size_t num_opts;
912 menu_ent options[3], *opt;
913 daddr_t start, size;
914
915 args[0] = msg_string(pm->parts->pscheme->name);
916 args[1] = msg_string(pm->parts->pscheme->short_name);
917 ai.args.argv = args;
918 ai.args.argc = 2;
919 ai.rv = LY_SETSIZES;
920
921 memset(options, 0, sizeof(options));
922 num_opts = 0;
923 opt = &options[0];
924 if (parts->pscheme->guess_install_target != NULL &&
925 parts->pscheme->guess_install_target(parts, &start, &size)) {
926 opt->opt_name = MSG_Keep_existing_partitions;
927 opt->opt_exp_name = NULL;
928 opt->opt_menu = OPT_NOMENU;
929 opt->opt_flags = OPT_EXIT;
930 opt->opt_action = set_keep_existing;
931 opt++;
932 num_opts++;
933 }
934 opt->opt_name = MSG_Use_only_part_of_the_disk;
935 opt->opt_exp_name = NULL;
936 opt->opt_menu = OPT_NOMENU;
937 opt->opt_flags = OPT_EXIT;
938 opt->opt_action = set_use_only_part;
939 opt++;
940 num_opts++;
941
942 opt->opt_name = MSG_Use_the_entire_disk;
943 opt->opt_exp_name = NULL;
944 opt->opt_menu = OPT_NOMENU;
945 opt->opt_flags = OPT_EXIT;
946 opt->opt_action = set_use_entire_disk;
947 opt++;
948 num_opts++;
949
950 menu = new_menu(MSG_Select_your_choice, options, num_opts,
951 -1, -10, 0, 0, MC_NOEXITOPT, NULL, NULL, NULL, NULL, NULL);
952 if (menu != -1) {
953 get_menudesc(menu)->expand_act = expand_all_option_texts;
954 process_menu(menu, &ai);
955 free_menu(menu);
956 }
957
958 return ai.rv;
959 }
960
961 /*
962 * return (see post_edit_verify):
963 * 0 -> abort
964 * 1 -> re-edit
965 * 2 -> continue installation
966 */
967 static int
968 verify_outer_parts(struct disk_partitions *parts, bool quiet)
969 {
970 part_id i;
971 int num_bsdparts;
972 daddr_t first_bsdstart, first_bsdsize, inst_start, inst_size;
973
974 first_bsdstart = first_bsdsize = 0;
975 inst_start = inst_size = 0;
976 num_bsdparts = 0;
977 for (i = 0; i < parts->num_part; i++) {
978 struct disk_part_info info;
979 if (!parts->pscheme->get_part_info(parts, i, &info))
980 continue;
981 if (!(info.flags & PTI_SEC_CONTAINER))
982 continue;
983 if (info.nat_type->generic_ptype != PT_root)
984 continue;
985 num_bsdparts++;
986
987 if (first_bsdstart == 0) {
988 first_bsdstart = info.start;
989 first_bsdsize = info.size;
990 }
991 if (inst_start == 0 && info.start == pm->ptstart) {
992 inst_start = info.start;
993 inst_size = info.size;
994 }
995 }
996
997 if (num_bsdparts == 0 ||
998 (num_bsdparts > 1 && inst_start == 0)) {
999 if (quiet && num_bsdparts == 0)
1000 return 0;
1001 if (quiet && parts->pscheme->guess_install_target &&
1002 parts->pscheme->guess_install_target(parts,
1003 &inst_start, &inst_size)) {
1004 pm->ptstart = inst_start;
1005 pm->ptsize = inst_size;
1006 } else {
1007 if (num_bsdparts == 0)
1008 msg_display_subst(MSG_nobsdpart, 2,
1009 msg_string(parts->pscheme->name),
1010 msg_string(parts->pscheme->short_name));
1011 else
1012 msg_display_subst(MSG_multbsdpart, 2,
1013 msg_string(parts->pscheme->name),
1014 msg_string(parts->pscheme->short_name));
1015
1016 return ask_reedit(parts);
1017 }
1018 }
1019
1020 if (pm->ptstart == 0) {
1021 if (inst_start > 0) {
1022 pm->ptstart = inst_start;
1023 pm->ptsize = inst_size;
1024 } else if (first_bsdstart > 0) {
1025 pm->ptstart = first_bsdstart;
1026 pm->ptsize = first_bsdsize;
1027 } else if (parts->pscheme->guess_install_target &&
1028 parts->pscheme->guess_install_target(
1029 parts, &inst_start, &inst_size)) {
1030 pm->ptstart = inst_start;
1031 pm->ptsize = inst_size;
1032 }
1033 }
1034
1035 /*
1036 * post_edit_verify returns:
1037 * 0 -> abort
1038 * 1 -> re-edit
1039 * 2 -> continue installation
1040 */
1041 if (parts->pscheme->post_edit_verify)
1042 return parts->pscheme->post_edit_verify(parts, quiet);
1043
1044 return 2;
1045 }
1046
1047 static bool
1048 ask_outer_partsizes(struct disk_partitions *parts)
1049 {
1050 int j;
1051 int part_menu;
1052 size_t num_opts;
1053 arg_rv av;
1054
1055 part_menu_opts = outer_fill_part_menu_opts(parts, &num_opts);
1056 part_menu = new_menu(outer_part_title, part_menu_opts, num_opts,
1057 0, -1, 15, 70,
1058 MC_NOBOX|MC_ALWAYS_SCROLL|MC_NOCLEAR|MC_CONTINUOUS,
1059 draw_outer_part_header, draw_outer_part_line, NULL,
1060 NULL, MSG_Partition_table_ok);
1061 if (part_menu == -1) {
1062 free(part_menu_opts);
1063 return false;
1064 }
1065
1066 /* Default to MB, and use bios geometry for cylinder size */
1067 set_default_sizemult(MEG/512);
1068 if (pm->current_cylsize == 0)
1069 pm->current_cylsize = 16065; /* noone cares nowadays */
1070 pm->ptstart = 0;
1071 pm->ptsize = 0;
1072 av.rv = 0;
1073
1074 for (;;) {
1075 av.arg = parts;
1076 av.rv = 0;
1077 process_menu(part_menu, &av);
1078 if (av.rv < 0)
1079 break;
1080
1081 j = verify_outer_parts(parts, false);
1082 if (j == 0) {
1083 av.rv = -1;
1084 return false;
1085 } else if (j == 1) {
1086 continue;
1087 }
1088 break;
1089 }
1090
1091 free_menu(part_menu);
1092 free(part_menu_opts);
1093
1094 return av.rv == 0;
1095 }
1096
1097 bool
1098 edit_outer_parts(struct disk_partitions *parts)
1099 {
1100 part_id i;
1101 enum layout_type layout;
1102 int num_foreign_parts;
1103
1104 /* If targeting a wedge, do not ask for further partitioning */
1105 if (pm && (pm->no_part || pm->no_mbr))
1106 return true;
1107
1108 /* Make sure pm has been properly initialized */
1109 assert(pm->parts && pm->parts->pscheme);
1110
1111 if (partman_go)
1112 layout = LY_SETSIZES;
1113 else {
1114 /* Ask full/part */
1115 const struct disk_partitioning_scheme *sec =
1116 pm->parts->pscheme->secondary_scheme;
1117
1118 assert(sec != NULL);
1119
1120 uint64_t m_size =
1121 DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE + XNEEDMB;
1122 char min_size[5], build_size[5];
1123 const char
1124 *prim_name = msg_string(pm->parts->pscheme->name),
1125 *prim_short = msg_string(pm->parts->pscheme->short_name),
1126 *sec_name = msg_string(sec->name),
1127 *sec_short = msg_string(sec->short_name);
1128
1129 humanize_number(min_size, sizeof(min_size),
1130 m_size * MEG,
1131 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1132 humanize_number(build_size, sizeof(build_size),
1133 SYSTEM_BUILD_SIZE * MEG, "", HN_AUTOSCALE,
1134 HN_B | HN_NOSPACE | HN_DECIMAL);
1135
1136 msg_display_subst(MSG_fullpart, 7,
1137 pm->diskdev,
1138 prim_name, sec_name,
1139 prim_short, sec_short,
1140 min_size, build_size);
1141 msg_display_add("\n\n");
1142
1143 layout = ask_fullpart(pm->parts);
1144 }
1145
1146 if (layout == LY_USEFULL) {
1147 struct disk_part_info info;
1148
1149 /* Count nonempty, non-BSD partitions. */
1150 num_foreign_parts = 0;
1151 for (i = 0; i < parts->num_part; i++) {
1152 if (!parts->pscheme->get_part_info(parts, i, &info))
1153 continue;
1154 if (info.size == 0)
1155 continue;
1156 if (info.nat_type != NULL
1157 && info.nat_type->generic_ptype != PT_root
1158 && info.nat_type->generic_ptype != PT_swap)
1159 num_foreign_parts++;
1160 }
1161
1162 /* Ask if we really want to blow away non-NetBSD stuff */
1163 if (num_foreign_parts > 0) {
1164 msg_display(MSG_ovrwrite);
1165 if (!ask_noyes(NULL)) {
1166 if (logfp)
1167 (void)fprintf(logfp,
1168 "User answered no to destroy "
1169 "other data, aborting.\n");
1170 return false;
1171 }
1172 }
1173 if (!md_parts_use_wholedisk(parts))
1174 return false;
1175 if (parts->pscheme->post_edit_verify) {
1176 return
1177 parts->pscheme->post_edit_verify(parts, true) == 2;
1178 }
1179 return true;
1180 } else if (layout == LY_SETSIZES) {
1181 return ask_outer_partsizes(parts);
1182 } else {
1183 return verify_outer_parts(parts, true) == 2;
1184 }
1185 }
1186
1187 static int
1188 set_part_scheme(menudesc *m, void *arg)
1189 {
1190 size_t *res = arg;
1191
1192 *res = (size_t)m->cursel;
1193 return 1;
1194 }
1195
1196 const struct disk_partitioning_scheme *
1197 select_part_scheme(
1198 struct pm_devs *dev,
1199 const struct disk_partitioning_scheme *skip,
1200 bool bootable,
1201 const char *hdr)
1202 {
1203 int ps_menu = -1;
1204 menu_ent *opt;
1205 char **str, *ms = NULL;
1206 const struct disk_partitioning_scheme **options, *res;
1207 const char *title;
1208 size_t ndx, selected = ~0U, used;
1209 const struct disk_partitioning_scheme *p;
1210 bool showing_limit = false;
1211
1212 if (hdr == NULL)
1213 hdr = MSG_select_part_scheme;
1214
1215 opt = calloc(num_available_part_schemes, sizeof *opt);
1216 if (!opt)
1217 return NULL;
1218 str = calloc(num_available_part_schemes, sizeof *str);
1219 if (!str) {
1220 free(opt);
1221 return NULL;
1222 }
1223 options = calloc(num_available_part_schemes, sizeof *options);
1224 if (!options) {
1225 free(str);
1226 free(opt);
1227 return NULL;
1228 }
1229
1230 for (used = 0, ndx = 0; ndx < num_available_part_schemes; ndx++) {
1231 p = available_part_schemes[ndx];
1232 if (skip != NULL && p == skip)
1233 continue;
1234 if (bootable && p->have_boot_support != NULL &&
1235 !p->have_boot_support(dev->diskdev))
1236 continue;
1237 if (p->size_limit && dev->dlsize > p->size_limit) {
1238 char buf[255], hum_lim[5];
1239
1240 humanize_number(hum_lim, sizeof(hum_lim),
1241 (uint64_t)p->size_limit*512UL,
1242 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1243 sprintf(buf, "%s [%s %s]", msg_string(p->name),
1244 msg_string(MSG_size_limit), hum_lim);
1245 str[used] = strdup(buf);
1246 showing_limit = true;
1247 } else {
1248 str[used] = strdup(msg_string(p->name));
1249 }
1250 if (!str[used])
1251 goto out;
1252
1253 opt[used].opt_name = str[used];
1254 opt[used].opt_exp_name = NULL;
1255 opt[used].opt_menu = OPT_NOMENU;
1256 opt[used].opt_action = set_part_scheme;
1257 options[used] = p;
1258 used++;
1259 }
1260
1261 /* do not bother to ask if there are no options */
1262 if (used <= 1) {
1263 selected = (used == 1) ? 0 : ~0U;
1264 goto out;
1265 }
1266
1267 if (showing_limit) {
1268 char hum_lim[5], *tmp;
1269 size_t total;
1270
1271 const char *p1 = msg_string(hdr);
1272 const char *p2 = msg_string(MSG_select_part_limit);
1273
1274 humanize_number(hum_lim, sizeof(hum_lim),
1275 (uint64_t)dev->dlsize*512, "",
1276 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1277
1278 const char *args[] = { dev->diskdev, hum_lim };
1279 char *p3 = str_arg_subst(msg_string(MSG_part_limit_disksize),
1280 __arraycount(args), args);
1281
1282 total = strlen(p1) + strlen(p2) + strlen(p3)
1283 + sizeof(hum_lim) + 5;
1284 ms = tmp = malloc(total);
1285 title = tmp;
1286 strcpy(tmp, p1); tmp += strlen(p1);
1287 *tmp++ = '\n'; *tmp++ = '\n';
1288 strcpy(tmp, p2); tmp += strlen(p2);
1289 *tmp++ = '\n'; *tmp++ = '\n';
1290 strcpy(tmp, p3);
1291 free(p3);
1292 assert(strlen(ms) < total);
1293 } else {
1294 title = msg_string(hdr);
1295 }
1296 ps_menu = new_menu(title, opt, used,
1297 5, 5, 0, 0, 0, NULL, NULL, NULL, NULL, MSG_exit_menu_generic);
1298 if (ps_menu != -1)
1299 process_menu(ps_menu, &selected);
1300 out:
1301 res = selected >= used ? NULL : options[selected];
1302 for (ndx = 0; ndx < used; ndx++)
1303 free(str[ndx]);
1304 if (showing_limit && ms)
1305 free(ms);
1306 free(str);
1307 free(opt);
1308 free(options);
1309 if (ps_menu != -1)
1310 free_menu(ps_menu);
1311
1312 return res;
1313 }
1314