]> git.ipfire.org Git - thirdparty/util-linux.git/blame_incremental - disk-utils/fdisk-menu.c
lsns: fix clang compiler warning
[thirdparty/util-linux.git] / disk-utils / fdisk-menu.c
... / ...
CommitLineData
1
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <ctype.h>
6#include <stdint.h>
7
8#include "c.h"
9#include "rpmatch.h"
10#include "fdisk.h"
11#include "pt-sun.h"
12#include "pt-mbr.h"
13
14struct menu_entry {
15 const char key; /* command key */
16 const char *title; /* help string */
17 unsigned int normal : 1, /* normal mode */
18 expert : 1, /* expert mode */
19 hidden : 1; /* be sensitive for this key,
20 but don't print it in help */
21
22 enum fdisk_labeltype label; /* only for this label */
23 int exclude; /* all labels except these */
24 enum fdisk_labeltype parent; /* for nested PT */
25};
26
27#define IS_MENU_SEP(e) ((e)->key == '-')
28#define IS_MENU_HID(e) ((e)->hidden)
29
30struct menu {
31 enum fdisk_labeltype label; /* only for this label */
32 int exclude; /* all labels except these */
33
34 unsigned int nonested : 1; /* don't make this menu active in nested PT */
35
36 int (*callback)(struct fdisk_context **,
37 const struct menu *,
38 const struct menu_entry *);
39
40 struct menu_entry entries[]; /* NULL terminated array */
41};
42
43struct menu_context {
44 size_t menu_idx; /* the current menu */
45 size_t entry_idx; /* index with in the current menu */
46};
47
48#define MENU_CXT_EMPTY { 0, 0 }
49#define DECLARE_MENU_CB(x) \
50 static int x(struct fdisk_context **, \
51 const struct menu *, \
52 const struct menu_entry *)
53
54DECLARE_MENU_CB(gpt_menu_cb);
55DECLARE_MENU_CB(sun_menu_cb);
56DECLARE_MENU_CB(sgi_menu_cb);
57DECLARE_MENU_CB(geo_menu_cb);
58DECLARE_MENU_CB(dos_menu_cb);
59DECLARE_MENU_CB(bsd_menu_cb);
60DECLARE_MENU_CB(createlabel_menu_cb);
61DECLARE_MENU_CB(generic_menu_cb);
62
63/*
64 * Menu entry macros:
65 * MENU_X* expert mode only
66 * MENU_B* both -- expert + normal mode
67 *
68 * *_E exclude this label
69 * *_H hidden
70 * *_L only for this label
71 */
72
73/* separator */
74#define MENU_SEP(t) { .title = t, .key = '-', .normal = 1 }
75#define MENU_XSEP(t) { .title = t, .key = '-', .expert = 1 }
76#define MENU_BSEP(t) { .title = t, .key = '-', .expert = 1, .normal = 1 }
77
78/* entry */
79#define MENU_ENT(k, t) { .title = t, .key = k, .normal = 1 }
80#define MENU_ENT_E(k, t, l) { .title = t, .key = k, .normal = 1, .exclude = l }
81#define MENU_ENT_L(k, t, l) { .title = t, .key = k, .normal = 1, .label = l }
82
83#define MENU_XENT(k, t) { .title = t, .key = k, .expert = 1 }
84#define MENU_XENT_H(k, t) { .title = t, .key = k, .expert = 1, .hidden = 1 }
85
86#define MENU_BENT(k, t) { .title = t, .key = k, .expert = 1, .normal = 1 }
87#define MENU_BENT_E(k, t, l) { .title = t, .key = k, .expert = 1, .normal = 1, .exclude = l }
88
89#define MENU_ENT_NEST(k, t, l, p) { .title = t, .key = k, .normal = 1, .label = l, .parent = p }
90#define MENU_XENT_NEST(k, t, l, p) { .title = t, .key = k, .expert = 1, .label = l, .parent = p }
91#define MENU_BENT_NEST(k, t, l, p) { .title = t, .key = k, .expert = 1, .normal = 1, .label = l, .parent = p }
92
93/* Generic menu */
94static const struct menu menu_generic = {
95 .callback = generic_menu_cb,
96 .entries = {
97 MENU_BSEP(N_("Generic")),
98 MENU_ENT ('d', N_("delete a partition")),
99 MENU_ENT ('F', N_("list free unpartitioned space")),
100 MENU_ENT ('l', N_("list known partition types")),
101 MENU_ENT ('n', N_("add a new partition")),
102 MENU_BENT ('p', N_("print the partition table")),
103 MENU_ENT ('t', N_("change a partition type")),
104 MENU_BENT_E('v', N_("verify the partition table"), FDISK_DISKLABEL_BSD),
105 MENU_ENT ('i', N_("print information about a partition")),
106
107 MENU_XENT('d', N_("print the raw data of the first sector from the device")),
108 MENU_XENT('D', N_("print the raw data of the disklabel from the device")),
109 MENU_XENT('f', N_("fix partitions order")),
110
111 MENU_SEP(N_("Misc")),
112 MENU_BENT ('m', N_("print this menu")),
113 MENU_ENT_E('u', N_("change display/entry units"), FDISK_DISKLABEL_GPT),
114 MENU_ENT_E('x', N_("extra functionality (experts only)"), FDISK_DISKLABEL_BSD),
115
116 MENU_SEP(N_("Script")),
117 MENU_ENT ('I', N_("load disk layout from sfdisk script file")),
118 MENU_ENT ('O', N_("dump disk layout to sfdisk script file")),
119
120 MENU_BSEP(N_("Save & Exit")),
121 MENU_ENT_E('w', N_("write table to disk and exit"), FDISK_DISKLABEL_BSD),
122 MENU_ENT_L('w', N_("write table to disk"), FDISK_DISKLABEL_BSD),
123 MENU_BENT ('q', N_("quit without saving changes")),
124 MENU_XENT ('r', N_("return to main menu")),
125
126 MENU_ENT_NEST('r', N_("return from BSD to DOS"), FDISK_DISKLABEL_BSD, FDISK_DISKLABEL_DOS),
127
128 { 0, NULL }
129 }
130};
131
132static const struct menu menu_createlabel = {
133 .callback = createlabel_menu_cb,
134 .exclude = FDISK_DISKLABEL_BSD,
135 .nonested = 1,
136 .entries = {
137 MENU_SEP(N_("Create a new label")),
138 MENU_ENT('g', N_("create a new empty GPT partition table")),
139 MENU_ENT('G', N_("create a new empty SGI (IRIX) partition table")),
140 MENU_ENT('o', N_("create a new empty DOS partition table")),
141 MENU_ENT('s', N_("create a new empty Sun partition table")),
142
143 /* backward compatibility -- be sensitive to 'g', but don't
144 * print it in the expert menu */
145 MENU_XENT_H('g', N_("create an IRIX (SGI) partition table")),
146 { 0, NULL }
147 }
148};
149
150static const struct menu menu_geo = {
151 .callback = geo_menu_cb,
152 .exclude = FDISK_DISKLABEL_GPT | FDISK_DISKLABEL_BSD,
153 .entries = {
154 MENU_XSEP(N_("Geometry (for the current label)")),
155 MENU_XENT('c', N_("change number of cylinders")),
156 MENU_XENT('h', N_("change number of heads")),
157 MENU_XENT('s', N_("change number of sectors/track")),
158 { 0, NULL }
159 }
160};
161
162static const struct menu menu_gpt = {
163 .callback = gpt_menu_cb,
164 .label = FDISK_DISKLABEL_GPT,
165 .entries = {
166 MENU_BSEP(N_("GPT")),
167 MENU_XENT('i', N_("change disk GUID")),
168 MENU_XENT('n', N_("change partition name")),
169 MENU_XENT('u', N_("change partition UUID")),
170 MENU_XENT('l', N_("change table length")),
171 MENU_BENT('M', N_("enter protective/hybrid MBR")),
172
173 MENU_XSEP(""),
174 MENU_XENT('A', N_("toggle the legacy BIOS bootable flag")),
175 MENU_XENT('B', N_("toggle the no block IO protocol flag")),
176 MENU_XENT('R', N_("toggle the required partition flag")),
177 MENU_XENT('S', N_("toggle the GUID specific bits")),
178
179 { 0, NULL }
180 }
181};
182
183static const struct menu menu_sun = {
184 .callback = sun_menu_cb,
185 .label = FDISK_DISKLABEL_SUN,
186 .entries = {
187 MENU_BSEP(N_("Sun")),
188 MENU_ENT('a', N_("toggle the read-only flag")),
189 MENU_ENT('c', N_("toggle the mountable flag")),
190
191 MENU_XENT('a', N_("change number of alternate cylinders")),
192 MENU_XENT('e', N_("change number of extra sectors per cylinder")),
193 MENU_XENT('i', N_("change interleave factor")),
194 MENU_XENT('o', N_("change rotation speed (rpm)")),
195 MENU_XENT('y', N_("change number of physical cylinders")),
196 { 0, NULL }
197 }
198};
199
200static const struct menu menu_sgi = {
201 .callback = sgi_menu_cb,
202 .label = FDISK_DISKLABEL_SGI,
203 .entries = {
204 MENU_SEP(N_("SGI")),
205 MENU_ENT('a', N_("select bootable partition")),
206 MENU_ENT('b', N_("edit bootfile entry")),
207 MENU_ENT('c', N_("select sgi swap partition")),
208 MENU_ENT('i', N_("create SGI info")),
209 { 0, NULL }
210 }
211};
212
213static const struct menu menu_dos = {
214 .callback = dos_menu_cb,
215 .label = FDISK_DISKLABEL_DOS,
216 .entries = {
217 MENU_BSEP(N_("DOS (MBR)")),
218 MENU_ENT('a', N_("toggle a bootable flag")),
219 MENU_ENT('b', N_("edit nested BSD disklabel")),
220 MENU_ENT('c', N_("toggle the dos compatibility flag")),
221
222 MENU_XENT('b', N_("move beginning of data in a partition")),
223 MENU_XENT('i', N_("change the disk identifier")),
224
225 MENU_BENT_NEST('M', N_("return from protective/hybrid MBR to GPT"),
226 FDISK_DISKLABEL_DOS, FDISK_DISKLABEL_GPT),
227 { 0, NULL }
228 }
229};
230
231static const struct menu menu_bsd = {
232 .callback = bsd_menu_cb,
233 .label = FDISK_DISKLABEL_BSD,
234 .entries = {
235 MENU_SEP(N_("BSD")),
236 MENU_ENT('e', N_("edit drive data")),
237 MENU_ENT('i', N_("install bootstrap")),
238 MENU_ENT('s', N_("show complete disklabel")),
239 MENU_ENT('x', N_("link BSD partition to non-BSD partition")),
240 { 0, NULL }
241 }
242};
243
244static const struct menu *menus[] = {
245 &menu_gpt,
246 &menu_sun,
247 &menu_sgi,
248 &menu_dos,
249 &menu_bsd,
250 &menu_geo,
251 &menu_generic,
252 &menu_createlabel,
253};
254
255static const struct menu_entry *next_menu_entry(
256 struct fdisk_context *cxt,
257 struct menu_context *mc)
258{
259 struct fdisk_label *lb = fdisk_get_label(cxt, NULL);
260 struct fdisk_context *parent = fdisk_get_parent(cxt);
261 unsigned int type = 0, pr_type = 0;
262
263 assert(cxt);
264
265 if (lb)
266 type = fdisk_label_get_type(lb);
267 if (parent)
268 pr_type = fdisk_label_get_type(fdisk_get_label(parent, NULL));
269
270 while (mc->menu_idx < ARRAY_SIZE(menus)) {
271 const struct menu *m = menus[mc->menu_idx];
272 const struct menu_entry *e = &(m->entries[mc->entry_idx]);
273
274 /*
275 * whole-menu filter
276 */
277
278 /* no more entries */
279 if (e->title == NULL ||
280 /* menu wanted for specified labels only */
281 (m->label && lb && !(m->label & type)) ||
282 /* unwanted for nested PT */
283 (m->nonested && parent) ||
284 /* menu excluded for specified labels */
285 (m->exclude && lb && (m->exclude & type))) {
286 mc->menu_idx++;
287 mc->entry_idx = 0;
288 continue;
289 }
290
291 /*
292 * per entry filter
293 */
294
295 /* excluded for the current label */
296 if ((e->exclude && lb && e->exclude & type) ||
297 /* entry wanted for specified labels only */
298 (e->label && lb && !(e->label & type)) ||
299 /* exclude non-expert entries in expect mode */
300 (e->expert == 0 && fdisk_is_details(cxt)) ||
301 /* nested only */
302 (e->parent && (!parent || pr_type != e->parent)) ||
303 /* exclude non-normal entries in normal mode */
304 (e->normal == 0 && !fdisk_is_details(cxt))) {
305 mc->entry_idx++;
306 continue;
307 }
308 mc->entry_idx++;
309 return e;
310
311 }
312 return NULL;
313}
314
315/* returns @menu and menu entry for then @key */
316static const struct menu_entry *get_fdisk_menu_entry(
317 struct fdisk_context *cxt,
318 int key,
319 const struct menu **menu)
320{
321 struct menu_context mc = MENU_CXT_EMPTY;
322 const struct menu_entry *e;
323
324 while ((e = next_menu_entry(cxt, &mc))) {
325 if (IS_MENU_SEP(e) || e->key != key)
326 continue;
327
328 if (menu)
329 *menu = menus[mc.menu_idx];
330 return e;
331 }
332
333 return NULL;
334}
335
336static int menu_detect_collisions(struct fdisk_context *cxt)
337{
338 struct menu_context mc = MENU_CXT_EMPTY;
339 const struct menu_entry *e, *r;
340
341 while ((e = next_menu_entry(cxt, &mc))) {
342 if (IS_MENU_SEP(e))
343 continue;
344
345 r = get_fdisk_menu_entry(cxt, e->key, NULL);
346 if (!r) {
347 DBG(MENU, ul_debug("warning: not found "
348 "entry for %c", e->key));
349 return -1;
350 }
351 if (r != e) {
352 DBG(MENU, ul_debug("warning: duplicate key '%c'",
353 e->key));
354 DBG(MENU, ul_debug(" : %s", e->title));
355 DBG(MENU, ul_debug(" : %s", r->title));
356 abort();
357 }
358 }
359
360 return 0;
361}
362
363static int print_fdisk_menu(struct fdisk_context *cxt)
364{
365 struct menu_context mc = MENU_CXT_EMPTY;
366 const struct menu_entry *e;
367
368 ON_DBG(MENU, menu_detect_collisions(cxt));
369
370 if (fdisk_is_details(cxt))
371 printf(_("\nHelp (expert commands):\n"));
372 else
373 printf(_("\nHelp:\n"));
374
375 while ((e = next_menu_entry(cxt, &mc))) {
376 if (IS_MENU_HID(e))
377 continue; /* hidden entry */
378 if (IS_MENU_SEP(e) && (!e->title || !*e->title))
379 printf("\n");
380 else if (IS_MENU_SEP(e)) {
381 color_scheme_enable("help-title", UL_COLOR_BOLD);
382 printf("\n %s\n", _(e->title));
383 color_disable();
384 } else
385 printf(" %c %s\n", e->key, _(e->title));
386 }
387 fputc('\n', stdout);
388
389 if (fdisk_get_parent(cxt)) {
390 struct fdisk_label *l = fdisk_get_label(cxt, NULL),
391 *p = fdisk_get_label(fdisk_get_parent(cxt), NULL);
392
393 fdisk_info(cxt, _("You're editing nested '%s' partition table, "
394 "primary partition table is '%s'."),
395 fdisk_label_get_name(l),
396 fdisk_label_get_name(p));
397 }
398
399 return 0;
400}
401
402/* Asks for command, verify the key and perform the command or
403 * returns the command key if no callback for the command is
404 * implemented.
405 *
406 * Note that this function might exchange the context pointer to
407 * switch to another (nested) context.
408 *
409 * Returns: <0 on error
410 * 0 on success (the command performed)
411 * >0 if no callback (then returns the key)
412 */
413int process_fdisk_menu(struct fdisk_context **cxt0)
414{
415 struct fdisk_context *cxt = *cxt0;
416 const struct menu_entry *ent;
417 const struct menu *menu;
418 int key, rc;
419 const char *prompt;
420 char buf[BUFSIZ];
421
422 if (fdisk_is_details(cxt))
423 prompt = _("Expert command (m for help): ");
424 else
425 prompt = _("Command (m for help): ");
426
427 fputc('\n',stdout);
428 rc = get_user_reply(prompt, buf, sizeof(buf));
429
430 if (rc == -ECANCELED) {
431 /* Map ^C and ^D in main menu to 'q' */
432 if (is_interactive
433 && fdisk_label_is_changed(fdisk_get_label(cxt, NULL))) {
434 rc = get_user_reply(
435 _("\nDo you really want to quit? "),
436 buf, sizeof(buf));
437 if (rc || !rpmatch(buf))
438 return 0;
439 }
440 key = 'q';
441 } else if (rc) {
442 return rc;
443 } else
444 key = buf[0];
445
446 ent = get_fdisk_menu_entry(cxt, key, &menu);
447 if (!ent) {
448 fdisk_warnx(cxt, _("%c: unknown command"), key);
449 return -EINVAL;
450 }
451
452 DBG(MENU, ul_debug("selected: key=%c, entry='%s'",
453 key, ent->title));
454
455 /* menu has implemented callback, use it */
456 if (menu->callback)
457 rc = menu->callback(cxt0, menu, ent);
458 else {
459 DBG(MENU, ul_debug("no callback for key '%c'", key));
460 rc = -EINVAL;
461 }
462
463 DBG(MENU, ul_debug("process menu done [rc=%d]", rc));
464 return rc;
465}
466
467static int script_read(struct fdisk_context *cxt)
468{
469 struct fdisk_script *sc = NULL;
470 char *filename = NULL;
471 int rc;
472
473 rc = fdisk_ask_string(cxt, _("Enter script file name"), &filename);
474 if (rc)
475 return rc;
476
477 errno = 0;
478 sc = fdisk_new_script_from_file(cxt, filename);
479 if (!sc && errno)
480 fdisk_warn(cxt, _("Cannot open %s"), filename);
481 else if (!sc)
482 fdisk_warnx(cxt, _("Failed to parse script file %s"), filename);
483 else if (fdisk_apply_script(cxt, sc) != 0) {
484 fdisk_warnx(cxt, _("Failed to apply script %s"), filename);
485 fdisk_warnx(cxt, _("Resetting fdisk!"));
486 rc = fdisk_reassign_device(cxt);
487 if (rc == 0 && !fdisk_has_label(cxt)) {
488 fdisk_info(cxt, _("Device does not contain a recognized partition table."));
489 fdisk_create_disklabel(cxt, NULL);
490 }
491 } else
492 fdisk_info(cxt, _("Script successfully applied."));
493
494 fdisk_unref_script(sc);
495 free(filename);
496 return rc;
497}
498
499static int script_write(struct fdisk_context *cxt)
500{
501 struct fdisk_script *sc = NULL;
502 char *filename = NULL;
503 FILE *f = NULL;
504 int rc;
505
506 rc = fdisk_ask_string(cxt, _("Enter script file name"), &filename);
507 if (rc)
508 return rc;
509
510 sc = fdisk_new_script(cxt);
511 if (!sc) {
512 fdisk_warn(cxt, _("Failed to allocate script handler"));
513 goto done;
514 }
515
516 rc = fdisk_script_read_context(sc, NULL);
517 if (rc) {
518 fdisk_warnx(cxt, _("Failed to transform disk layout into script"));
519 goto done;
520 }
521
522 f = fopen(filename, "w");
523 if (!f) {
524 fdisk_warn(cxt, _("Cannot open %s"), filename);
525 goto done;
526 }
527
528 rc = fdisk_script_write_file(sc, f);
529 if (rc)
530 fdisk_warn(cxt, _("Failed to write script %s"), filename);
531 else
532 fdisk_info(cxt, _("Script successfully saved."));
533done:
534 if (f)
535 fclose(f);
536 fdisk_unref_script(sc);
537 free(filename);
538 return rc;
539}
540
541static int ask_for_wipe(struct fdisk_context *cxt, size_t partno)
542{
543 struct fdisk_partition *tmp = NULL;
544 char *fstype = NULL;
545 int rc, yes = 0;
546
547 rc = fdisk_get_partition(cxt, partno, &tmp);
548 if (rc)
549 goto done;
550
551 rc = fdisk_partition_to_string(tmp, cxt, FDISK_FIELD_FSTYPE, &fstype);
552 if (rc || fstype == NULL)
553 goto done;
554
555 fdisk_warnx(cxt, _("Partition #%zu contains a %s signature."), partno + 1, fstype);
556
557 if (pwipemode == WIPEMODE_AUTO && isatty(STDIN_FILENO))
558 fdisk_ask_yesno(cxt, _("Do you want to remove the signature?"), &yes);
559 else if (pwipemode == WIPEMODE_ALWAYS)
560 yes = 1;
561
562 if (yes) {
563 fdisk_info(cxt, _("The signature will be removed by a write command."));
564 rc = fdisk_wipe_partition(cxt, partno, TRUE);
565 }
566done:
567 fdisk_unref_partition(tmp);
568 free(fstype);
569 return rc;
570}
571
572/*
573 * Basic fdisk actions
574 */
575static int generic_menu_cb(struct fdisk_context **cxt0,
576 const struct menu *menu __attribute__((__unused__)),
577 const struct menu_entry *ent)
578{
579 struct fdisk_context *cxt = *cxt0;
580 int rc = 0;
581 size_t n;
582
583 /* actions shared between expert and normal mode */
584 switch (ent->key) {
585 case 'p':
586 list_disk_geometry(cxt);
587 list_disklabel(cxt);
588 break;
589 case 'w':
590 if (fdisk_is_readonly(cxt)) {
591 fdisk_warnx(cxt, _("Device is open in read-only mode."));
592 break;
593 }
594 rc = fdisk_write_disklabel(cxt);
595 if (rc)
596 err(EXIT_FAILURE, _("failed to write disklabel"));
597 if (fdisk_get_parent(cxt))
598 break; /* nested PT, don't leave */
599 fdisk_info(cxt, _("The partition table has been altered."));
600
601 if (device_is_used)
602 rc = fdisk_reread_changes(cxt, original_layout);
603 else
604 rc = fdisk_reread_partition_table(cxt);
605 if (!rc)
606 rc = fdisk_deassign_device(cxt, 0);
607 /* fallthrough */
608 case 'q':
609 fdisk_unref_context(cxt);
610 fputc('\n', stdout);
611 exit(rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
612 case 'm':
613 rc = print_fdisk_menu(cxt);
614 break;
615 case 'v':
616 rc = fdisk_verify_disklabel(cxt);
617 break;
618 case 'i':
619 rc = print_partition_info(cxt);
620 break;
621 case 'F':
622 list_freespace(cxt);
623 break;
624 }
625
626 /* expert mode */
627 if (ent->expert) {
628 switch (ent->key) {
629 case 'd':
630 dump_firstsector(cxt);
631 break;
632 case 'D':
633 dump_disklabel(cxt);
634 break;
635 case 'f':
636 rc = fdisk_reorder_partitions(cxt);
637 if (rc)
638 fdisk_warnx(cxt, _("Failed to fix partitions order."));
639 else
640 fdisk_info(cxt, _("Partitions order fixed."));
641 break;
642 case 'r':
643 rc = fdisk_enable_details(cxt, 0);
644 break;
645 }
646 return rc;
647 }
648
649 /* normal mode */
650 switch (ent->key) {
651 case 'd':
652 rc = fdisk_ask_partnum(cxt, &n, FALSE);
653 if (!rc)
654 rc = fdisk_delete_partition(cxt, n);
655 if (rc)
656 fdisk_warnx(cxt, _("Could not delete partition %zu"), n + 1);
657 else
658 fdisk_info(cxt, _("Partition %zu has been deleted."), n + 1);
659 break;
660 case 'I':
661 script_read(cxt);
662 break;
663 case 'O':
664 script_write(cxt);
665 break;
666 case 'l':
667 list_partition_types(cxt);
668 break;
669 case 'n':
670 {
671 size_t partno;
672 rc = fdisk_add_partition(cxt, NULL, &partno);
673 if (!rc)
674 rc = ask_for_wipe(cxt, partno);
675 break;
676 }
677 case 't':
678 change_partition_type(cxt);
679 break;
680 case 'u':
681 fdisk_set_unit(cxt,
682 fdisk_use_cylinders(cxt) ? "sectors" :
683 "cylinders");
684 if (fdisk_use_cylinders(cxt))
685 fdisk_info(cxt, _("Changing display/entry units to cylinders (DEPRECATED!)."));
686 else
687 fdisk_info(cxt, _("Changing display/entry units to sectors."));
688 break;
689 case 'x':
690 fdisk_enable_details(cxt, 1);
691 break;
692 case 'r':
693 /* return from nested BSD to DOS */
694 if (fdisk_get_parent(cxt)) {
695 *cxt0 = fdisk_get_parent(cxt);
696
697 fdisk_info(cxt, _("Leaving nested disklabel."));
698 fdisk_unref_context(cxt);
699 }
700 break;
701 }
702
703 return rc;
704}
705
706
707/*
708 * This is fdisk frontend for GPT specific libfdisk functions that
709 * are not exported by generic libfdisk API.
710 */
711static int gpt_menu_cb(struct fdisk_context **cxt0,
712 const struct menu *menu __attribute__((__unused__)),
713 const struct menu_entry *ent)
714{
715 struct fdisk_context *cxt = *cxt0;
716 struct fdisk_context *mbr;
717 struct fdisk_partition *pa = NULL;
718 size_t n;
719 int rc = 0;
720 uintmax_t length = 0;
721
722 assert(cxt);
723 assert(ent);
724 assert(fdisk_is_label(cxt, GPT));
725
726 DBG(MENU, ul_debug("enter GPT menu"));
727
728 if (ent->expert) {
729 switch (ent->key) {
730 case 'i':
731 return fdisk_set_disklabel_id(cxt);
732 case 'l':
733 rc = fdisk_ask_number(cxt, 1, fdisk_get_npartitions(cxt),
734 ~(uint32_t)0, _("New maximum entries"), &length);
735 if (rc)
736 return rc;
737 return fdisk_gpt_set_npartitions(cxt, (uint32_t) length);
738 case 'M':
739 mbr = fdisk_new_nested_context(cxt, "dos");
740 if (!mbr)
741 return -ENOMEM;
742 *cxt0 = cxt = mbr;
743 if (fdisk_is_details(cxt))
744 fdisk_enable_details(cxt, 1); /* keep us in expert mode */
745 fdisk_info(cxt, _("Entering protective/hybrid MBR disklabel."));
746 return 0;
747 }
748
749 /* actions where is necessary partnum */
750 rc = fdisk_ask_partnum(cxt, &n, FALSE);
751 if (rc)
752 return rc;
753
754 switch(ent->key) {
755 case 'u':
756 pa = fdisk_new_partition(); /* new template */
757 if (!pa)
758 rc = -ENOMEM;
759 else {
760 char *str = NULL;
761 rc = fdisk_ask_string(cxt, _("New UUID (in 8-4-4-4-12 format)"), &str);
762 if (!rc)
763 rc = fdisk_partition_set_uuid(pa, str);
764 if (!rc)
765 rc = fdisk_set_partition(cxt, n, pa);
766 free(str);
767 fdisk_unref_partition(pa);
768 }
769 break;
770 case 'n':
771 pa = fdisk_new_partition(); /* new template */
772 if (!pa)
773 rc = -ENOMEM;
774 else {
775 char *str = NULL;
776 rc = fdisk_ask_string(cxt, _("New name"), &str);
777 if (!rc)
778 rc = fdisk_partition_set_name(pa, str);
779 if (!rc)
780 rc = fdisk_set_partition(cxt, n, pa);
781 free(str);
782 fdisk_unref_partition(pa);
783 }
784 break;
785 case 'A':
786 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_LEGACYBOOT);
787 break;
788 case 'B':
789 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_NOBLOCK);
790 break;
791 case 'R':
792 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_REQUIRED);
793 break;
794 case 'S':
795 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_GUIDSPECIFIC);
796 break;
797 }
798 }
799
800 return rc;
801}
802
803
804/*
805 * This is fdisk frontend for MBR specific libfdisk functions that
806 * are not exported by generic libfdisk API.
807 */
808static int dos_menu_cb(struct fdisk_context **cxt0,
809 const struct menu *menu __attribute__((__unused__)),
810 const struct menu_entry *ent)
811{
812 struct fdisk_context *cxt = *cxt0;
813 int rc = 0;
814
815 DBG(MENU, ul_debug("enter DOS menu"));
816
817 if (!ent->expert) {
818 switch (ent->key) {
819 case 'a':
820 {
821 size_t n;
822 rc = fdisk_ask_partnum(cxt, &n, FALSE);
823 if (!rc)
824 rc = fdisk_toggle_partition_flag(cxt, n, DOS_FLAG_ACTIVE);
825 break;
826 }
827 case 'b':
828 {
829 struct fdisk_context *bsd
830 = fdisk_new_nested_context(cxt, "bsd");
831 if (!bsd)
832 return -ENOMEM;
833 if (!fdisk_has_label(bsd))
834 rc = fdisk_create_disklabel(bsd, "bsd");
835 if (rc)
836 fdisk_unref_context(bsd);
837 else {
838 *cxt0 = cxt = bsd;
839 fdisk_info(cxt, _("Entering nested BSD disklabel."));
840 }
841 break;
842 }
843 case 'c':
844 toggle_dos_compatibility_flag(cxt);
845 break;
846 }
847 return rc;
848 }
849
850 /* expert mode */
851 switch (ent->key) {
852 case 'b':
853 {
854 size_t n;
855 rc = fdisk_ask_partnum(cxt, &n, FALSE);
856 if (!rc)
857 rc = fdisk_dos_move_begin(cxt, n);
858 break;
859 }
860 case 'i':
861 rc = fdisk_set_disklabel_id(cxt);
862 break;
863 case 'M':
864 /* return from nested MBR to GPT */
865 if (fdisk_get_parent(cxt)) {
866 *cxt0 = fdisk_get_parent(cxt);
867
868 fdisk_info(cxt, _("Leaving nested disklabel."));
869 fdisk_unref_context(cxt);
870 }
871 break;
872 }
873 return rc;
874}
875
876static int sun_menu_cb(struct fdisk_context **cxt0,
877 const struct menu *menu __attribute__((__unused__)),
878 const struct menu_entry *ent)
879{
880 struct fdisk_context *cxt = *cxt0;
881 int rc = 0;
882
883 DBG(MENU, ul_debug("enter SUN menu"));
884
885 assert(cxt);
886 assert(ent);
887 assert(fdisk_is_label(cxt, SUN));
888
889 DBG(MENU, ul_debug("enter SUN menu"));
890
891 /* normal mode */
892 if (!ent->expert) {
893 size_t n;
894
895 rc = fdisk_ask_partnum(cxt, &n, FALSE);
896 if (rc)
897 return rc;
898 switch (ent->key) {
899 case 'a':
900 rc = fdisk_toggle_partition_flag(cxt, n, SUN_FLAG_RONLY);
901 break;
902 case 'c':
903 rc = fdisk_toggle_partition_flag(cxt, n, SUN_FLAG_UNMNT);
904 break;
905 }
906 return rc;
907 }
908
909 /* expert mode */
910 switch (ent->key) {
911 case 'a':
912 rc = fdisk_sun_set_alt_cyl(cxt);
913 break;
914 case 'e':
915 rc = fdisk_sun_set_xcyl(cxt);
916 break;
917 case 'i':
918 rc = fdisk_sun_set_ilfact(cxt);
919 break;
920 case 'o':
921 rc = fdisk_sun_set_rspeed(cxt);
922 break;
923 case 'y':
924 rc = fdisk_sun_set_pcylcount(cxt);
925 break;
926 }
927 return rc;
928}
929
930static int sgi_menu_cb(struct fdisk_context **cxt0,
931 const struct menu *menu __attribute__((__unused__)),
932 const struct menu_entry *ent)
933{
934 struct fdisk_context *cxt = *cxt0;
935 int rc = -EINVAL;
936 size_t n = 0;
937
938 DBG(MENU, ul_debug("enter SGI menu"));
939
940 assert(cxt);
941 assert(ent);
942 assert(fdisk_is_label(cxt, SGI));
943
944 if (ent->expert)
945 return rc;
946
947 switch (ent->key) {
948 case 'a':
949 rc = fdisk_ask_partnum(cxt, &n, FALSE);
950 if (!rc)
951 rc = fdisk_toggle_partition_flag(cxt, n, SGI_FLAG_BOOT);
952 break;
953 case 'b':
954 fdisk_sgi_set_bootfile(cxt);
955 break;
956 case 'c':
957 rc = fdisk_ask_partnum(cxt, &n, FALSE);
958 if (!rc)
959 rc = fdisk_toggle_partition_flag(cxt, n, SGI_FLAG_SWAP);
960 break;
961 case 'i':
962 rc = fdisk_sgi_create_info(cxt);
963 break;
964 }
965
966 return rc;
967}
968
969/*
970 * This is fdisk frontend for BSD specific libfdisk functions that
971 * are not exported by generic libfdisk API.
972 */
973static int bsd_menu_cb(struct fdisk_context **cxt0,
974 const struct menu *menu __attribute__((__unused__)),
975 const struct menu_entry *ent)
976{
977 struct fdisk_context *cxt = *cxt0;
978 int rc = 0, org;
979
980 assert(cxt);
981 assert(ent);
982 assert(fdisk_is_label(cxt, BSD));
983
984 DBG(MENU, ul_debug("enter BSD menu"));
985
986 switch(ent->key) {
987 case 'e':
988 rc = fdisk_bsd_edit_disklabel(cxt);
989 break;
990 case 'i':
991 rc = fdisk_bsd_write_bootstrap(cxt);
992 break;
993 case 's':
994 org = fdisk_is_details(cxt);
995
996 fdisk_enable_details(cxt, 1);
997 list_disklabel(cxt);
998 fdisk_enable_details(cxt, org);
999 break;
1000 case 'x':
1001 rc = fdisk_bsd_link_partition(cxt);
1002 break;
1003 }
1004 return rc;
1005}
1006
1007/* C/H/S commands
1008 *
1009 * The geometry setting from this dialog is not persistent and maybe reseted by
1010 * fdisk_reset_device_properties() (for example when you create a new disk
1011 * label). Note that on command line specified -C/-H/-S setting is persistent
1012 * as it's based on fdisk_save_user_geometry().
1013 */
1014static int geo_menu_cb(struct fdisk_context **cxt0,
1015 const struct menu *menu __attribute__((__unused__)),
1016 const struct menu_entry *ent)
1017{
1018 struct fdisk_context *cxt = *cxt0;
1019 struct fdisk_label *lb = fdisk_get_label(cxt, NULL);
1020 int rc = -EINVAL;
1021 uintmax_t c = 0, h = 0, s = 0;
1022 fdisk_sector_t mi, ma;
1023
1024 DBG(MENU, ul_debug("enter GEO menu"));
1025
1026 assert(cxt);
1027 assert(ent);
1028
1029 /* default */
1030 if (!lb)
1031 lb = fdisk_get_label(cxt, "dos");
1032
1033 switch (ent->key) {
1034 case 'c':
1035 fdisk_label_get_geomrange_cylinders(lb, &mi, &ma);
1036 rc = fdisk_ask_number(cxt, mi, fdisk_get_geom_cylinders(cxt),
1037 ma, _("Number of cylinders"), &c);
1038 break;
1039 case 'h':
1040 {
1041 unsigned int i, a;
1042 fdisk_label_get_geomrange_heads(lb, &i, &a);
1043 rc = fdisk_ask_number(cxt, i, fdisk_get_geom_heads(cxt),
1044 a, _("Number of heads"), &h);
1045 break;
1046 }
1047 case 's':
1048 fdisk_label_get_geomrange_sectors(lb, &mi, &ma);
1049 rc = fdisk_ask_number(cxt, mi, fdisk_get_geom_sectors(cxt),
1050 ma, _("Number of sectors"), &s);
1051 break;
1052 }
1053
1054 if (!rc)
1055 fdisk_override_geometry(cxt, c, h, s);
1056 return rc;
1057}
1058
1059static int createlabel_menu_cb(struct fdisk_context **cxt0,
1060 const struct menu *menu __attribute__((__unused__)),
1061 const struct menu_entry *ent)
1062{
1063 struct fdisk_context *cxt = *cxt0;
1064 int rc = -EINVAL;
1065
1066 DBG(MENU, ul_debug("enter Create label menu"));
1067
1068 assert(cxt);
1069 assert(ent);
1070
1071 if (ent->expert) {
1072 switch (ent->key) {
1073 case 'g':
1074 /* Deprecated, use 'G' in main menu, just for backward
1075 * compatibility only. */
1076 rc = fdisk_create_disklabel(cxt, "sgi");
1077 break;
1078 }
1079 } else {
1080 switch (ent->key) {
1081 case 'g':
1082 rc = fdisk_create_disklabel(cxt, "gpt");
1083 break;
1084 case 'G':
1085 rc = fdisk_create_disklabel(cxt, "sgi");
1086 break;
1087 case 'o':
1088 rc = fdisk_create_disklabel(cxt, "dos");
1089 break;
1090 case 's':
1091 rc = fdisk_create_disklabel(cxt, "sun");
1092 break;
1093 }
1094 }
1095
1096 if (rc == 0 && fdisk_get_collision(cxt))
1097 follow_wipe_mode(cxt);
1098
1099 return rc;
1100}