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