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