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