]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/fdisk-menu.c
ea12ac74ee2031ed146a0bf94e6366372bd0510d
[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 ('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('M', N_("enter protective/hybrid MBR")),
169
170 MENU_XSEP(""),
171 MENU_XENT('A', N_("toggle the legacy BIOS bootable flag")),
172 MENU_XENT('B', N_("toggle the no block IO protocol flag")),
173 MENU_XENT('R', N_("toggle the required partition flag")),
174 MENU_XENT('S', N_("toggle the GUID specific bits")),
175
176 { 0, NULL }
177 }
178 };
179
180 struct menu menu_sun = {
181 .callback = sun_menu_cb,
182 .label = FDISK_DISKLABEL_SUN,
183 .entries = {
184 MENU_BSEP(N_("Sun")),
185 MENU_ENT('a', N_("toggle the read-only flag")),
186 MENU_ENT('c', N_("toggle the mountable flag")),
187
188 MENU_XENT('a', N_("change number of alternate cylinders")),
189 MENU_XENT('e', N_("change number of extra sectors per cylinder")),
190 MENU_XENT('i', N_("change interleave factor")),
191 MENU_XENT('o', N_("change rotation speed (rpm)")),
192 MENU_XENT('y', N_("change number of physical cylinders")),
193 { 0, NULL }
194 }
195 };
196
197 struct menu menu_sgi = {
198 .callback = sgi_menu_cb,
199 .label = FDISK_DISKLABEL_SGI,
200 .entries = {
201 MENU_SEP(N_("SGI")),
202 MENU_ENT('a', N_("select bootable partition")),
203 MENU_ENT('b', N_("edit bootfile entry")),
204 MENU_ENT('c', N_("select sgi swap partition")),
205 MENU_ENT('i', N_("create SGI info")),
206 { 0, NULL }
207 }
208 };
209
210 struct menu menu_dos = {
211 .callback = dos_menu_cb,
212 .label = FDISK_DISKLABEL_DOS,
213 .entries = {
214 MENU_BSEP(N_("DOS (MBR)")),
215 MENU_ENT('a', N_("toggle a bootable flag")),
216 MENU_ENT('b', N_("edit nested BSD disklabel")),
217 MENU_ENT('c', N_("toggle the dos compatibility flag")),
218
219 MENU_XENT('b', N_("move beginning of data in a partition")),
220 MENU_XENT('i', N_("change the disk identifier")),
221
222 MENU_XENT_NEST('M', N_("return from protective/hybrid MBR to GPT"),
223 FDISK_DISKLABEL_DOS, FDISK_DISKLABEL_GPT),
224 { 0, NULL }
225 }
226 };
227
228 struct menu menu_bsd = {
229 .callback = bsd_menu_cb,
230 .label = FDISK_DISKLABEL_BSD,
231 .entries = {
232 MENU_SEP(N_("BSD")),
233 MENU_ENT('e', N_("edit drive data")),
234 MENU_ENT('i', N_("install bootstrap")),
235 MENU_ENT('s', N_("show complete disklabel")),
236 MENU_ENT('x', N_("link BSD partition to non-BSD partition")),
237 { 0, NULL }
238 }
239 };
240
241 static const struct menu *menus[] = {
242 &menu_gpt,
243 &menu_sun,
244 &menu_sgi,
245 &menu_dos,
246 &menu_bsd,
247 &menu_geo,
248 &menu_generic,
249 &menu_createlabel,
250 };
251
252 static const struct menu_entry *next_menu_entry(
253 struct fdisk_context *cxt,
254 struct menu_context *mc)
255 {
256 struct fdisk_label *lb = fdisk_get_label(cxt, NULL);
257 struct fdisk_context *parent = fdisk_get_parent(cxt);
258 unsigned int type = 0, pr_type = 0;
259
260 assert(cxt);
261
262 if (lb)
263 type = fdisk_label_get_type(lb);
264 if (parent)
265 pr_type = fdisk_label_get_type(fdisk_get_label(parent, NULL));
266
267 while (mc->menu_idx < ARRAY_SIZE(menus)) {
268 const struct menu *m = menus[mc->menu_idx];
269 const struct menu_entry *e = &(m->entries[mc->entry_idx]);
270
271 /*
272 * whole-menu filter
273 */
274
275 /* no more entries */
276 if (e->title == NULL ||
277 /* menu wanted for specified labels only */
278 (m->label && lb && !(m->label & type)) ||
279 /* unwanted for nested PT */
280 (m->nonested && parent) ||
281 /* menu excluded for specified labels */
282 (m->exclude && lb && (m->exclude & type))) {
283 mc->menu_idx++;
284 mc->entry_idx = 0;
285 continue;
286 }
287
288 /*
289 * per entry filter
290 */
291
292 /* excluded for the current label */
293 if ((e->exclude && lb && e->exclude & type) ||
294 /* entry wanted for specified labels only */
295 (e->label && lb && !(e->label & type)) ||
296 /* exclude non-expert entries in expect mode */
297 (e->expert == 0 && fdisk_is_details(cxt)) ||
298 /* nested only */
299 (e->parent && (!parent || pr_type != e->parent)) ||
300 /* exclude non-normal entries in normal mode */
301 (e->normal == 0 && !fdisk_is_details(cxt))) {
302 mc->entry_idx++;
303 continue;
304 }
305 mc->entry_idx++;
306 return e;
307
308 }
309 return NULL;
310 }
311
312 /* returns @menu and menu entry for then @key */
313 static const struct menu_entry *get_fdisk_menu_entry(
314 struct fdisk_context *cxt,
315 int key,
316 const struct menu **menu)
317 {
318 struct menu_context mc = MENU_CXT_EMPTY;
319 const struct menu_entry *e;
320
321 while ((e = next_menu_entry(cxt, &mc))) {
322 if (IS_MENU_SEP(e) || e->key != key)
323 continue;
324
325 if (menu)
326 *menu = menus[mc.menu_idx];
327 return e;
328 }
329
330 return NULL;
331 }
332
333 static int menu_detect_collisions(struct fdisk_context *cxt)
334 {
335 struct menu_context mc = MENU_CXT_EMPTY;
336 const struct menu_entry *e, *r;
337
338 while ((e = next_menu_entry(cxt, &mc))) {
339 if (IS_MENU_SEP(e))
340 continue;
341
342 r = get_fdisk_menu_entry(cxt, e->key, NULL);
343 if (!r) {
344 DBG(MENU, ul_debug("warning: not found "
345 "entry for %c", e->key));
346 return -1;
347 }
348 if (r != e) {
349 DBG(MENU, ul_debug("warning: duplicate key '%c'",
350 e->key));
351 DBG(MENU, ul_debug(" : %s", e->title));
352 DBG(MENU, ul_debug(" : %s", r->title));
353 abort();
354 }
355 }
356
357 return 0;
358 }
359
360 static int print_fdisk_menu(struct fdisk_context *cxt)
361 {
362 struct menu_context mc = MENU_CXT_EMPTY;
363 const struct menu_entry *e;
364
365 ON_DBG(MENU, menu_detect_collisions(cxt));
366
367 if (fdisk_is_details(cxt))
368 printf(_("\nHelp (expert commands):\n"));
369 else
370 printf(_("\nHelp:\n"));
371
372 while ((e = next_menu_entry(cxt, &mc))) {
373 if (IS_MENU_HID(e))
374 continue; /* hidden entry */
375 if (IS_MENU_SEP(e) && (!e->title || !*e->title))
376 printf("\n");
377 else if (IS_MENU_SEP(e)) {
378 color_scheme_enable("help-title", UL_COLOR_BOLD);
379 printf("\n %s\n", _(e->title));
380 color_disable();
381 } else
382 printf(" %c %s\n", e->key, _(e->title));
383 }
384 fputc('\n', stdout);
385
386 if (fdisk_get_parent(cxt)) {
387 struct fdisk_label *l = fdisk_get_label(cxt, NULL),
388 *p = fdisk_get_label(fdisk_get_parent(cxt), NULL);
389
390 fdisk_info(cxt, _("You're editing nested '%s' partition table, "
391 "primary partition table is '%s'."),
392 fdisk_label_get_name(l),
393 fdisk_label_get_name(p));
394 }
395
396 return 0;
397 }
398
399 /* Asks for command, verify the key and perform the command or
400 * returns the command key if no callback for the command is
401 * implemented.
402 *
403 * Note that this function might exchange the context pointer to
404 * switch to another (nested) context.
405 *
406 * Returns: <0 on error
407 * 0 on success (the command performed)
408 * >0 if no callback (then returns the key)
409 */
410 int process_fdisk_menu(struct fdisk_context **cxt0)
411 {
412 struct fdisk_context *cxt = *cxt0;
413 const struct menu_entry *ent;
414 const struct menu *menu;
415 int key, rc;
416 const char *prompt;
417 char buf[BUFSIZ];
418
419 if (fdisk_is_details(cxt))
420 prompt = _("Expert command (m for help): ");
421 else
422 prompt = _("Command (m for help): ");
423
424 fputc('\n',stdout);
425 rc = get_user_reply(cxt, prompt, buf, sizeof(buf));
426 if (rc)
427 return rc;
428
429 key = buf[0];
430 ent = get_fdisk_menu_entry(cxt, key, &menu);
431 if (!ent) {
432 fdisk_warnx(cxt, _("%c: unknown command"), key);
433 return -EINVAL;
434 }
435
436 rc = 0;
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 /*
521 * Basic fdisk actions
522 */
523 static int generic_menu_cb(struct fdisk_context **cxt0,
524 const struct menu *menu __attribute__((__unused__)),
525 const struct menu_entry *ent)
526 {
527 struct fdisk_context *cxt = *cxt0;
528 int rc = 0;
529 size_t n;
530
531 /* actions shared between expert and normal mode */
532 switch (ent->key) {
533 case 'p':
534 list_disk_geometry(cxt);
535 list_disklabel(cxt);
536 break;
537 case 'w':
538 if (fdisk_is_readonly(cxt)) {
539 fdisk_warnx(cxt, _("Device open in read-only mode."));
540 break;
541 }
542 rc = fdisk_write_disklabel(cxt);
543 if (rc)
544 err(EXIT_FAILURE, _("failed to write disklabel"));
545 if (fdisk_get_parent(cxt))
546 break; /* nested PT, don't leave */
547 fdisk_info(cxt, _("The partition table has been altered."));
548 rc = fdisk_reread_partition_table(cxt);
549 if (!rc)
550 rc = fdisk_deassign_device(cxt, 0);
551 /* fallthrough */
552 case 'q':
553 fdisk_unref_context(cxt);
554 fputc('\n', stdout);
555 exit(rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
556 case 'm':
557 rc = print_fdisk_menu(cxt);
558 break;
559 case 'v':
560 rc = fdisk_verify_disklabel(cxt);
561 break;
562 case 'i':
563 rc = print_partition_info(cxt);
564 break;
565 case 'F':
566 list_freespace(cxt);
567 break;
568 }
569
570 /* expert mode */
571 if (ent->expert) {
572 switch (ent->key) {
573 case 'd':
574 dump_firstsector(cxt);
575 break;
576 case 'D':
577 dump_disklabel(cxt);
578 break;
579 case 'f':
580 rc = fdisk_reorder_partitions(cxt);
581 break;
582 case 'r':
583 rc = fdisk_enable_details(cxt, 0);
584 break;
585 }
586 return rc;
587 }
588
589 /* normal mode */
590 switch (ent->key) {
591 case 'd':
592 rc = fdisk_ask_partnum(cxt, &n, FALSE);
593 if (!rc)
594 rc = fdisk_delete_partition(cxt, n);
595 if (rc)
596 fdisk_warnx(cxt, _("Could not delete partition %zu"), n + 1);
597 else
598 fdisk_info(cxt, _("Partition %zu has been deleted."), n + 1);
599 break;
600 case 'I':
601 script_read(cxt);
602 break;
603 case 'O':
604 script_write(cxt);
605 break;
606 case 'l':
607 list_partition_types(cxt);
608 break;
609 case 'n':
610 rc = fdisk_add_partition(cxt, NULL, NULL);
611 break;
612 case 't':
613 change_partition_type(cxt);
614 break;
615 case 'u':
616 fdisk_set_unit(cxt,
617 fdisk_use_cylinders(cxt) ? "sectors" :
618 "cylinders");
619 if (fdisk_use_cylinders(cxt))
620 fdisk_info(cxt, _("Changing display/entry units to cylinders (DEPRECATED!)."));
621 else
622 fdisk_info(cxt, _("Changing display/entry units to sectors."));
623 break;
624 case 'x':
625 fdisk_enable_details(cxt, 1);
626 break;
627 case 'r':
628 /* return from nested BSD to DOS */
629 if (fdisk_get_parent(cxt)) {
630 *cxt0 = fdisk_get_parent(cxt);
631
632 fdisk_info(cxt, _("Leaving nested disklabel."));
633 fdisk_unref_context(cxt);
634 cxt = *cxt0;
635 }
636 break;
637 }
638
639 return rc;
640 }
641
642
643 /*
644 * This is fdisk frontend for GPT specific libfdisk functions that
645 * are not expported by generic libfdisk API.
646 */
647 static int gpt_menu_cb(struct fdisk_context **cxt0,
648 const struct menu *menu __attribute__((__unused__)),
649 const struct menu_entry *ent)
650 {
651 struct fdisk_context *cxt = *cxt0;
652 struct fdisk_context *mbr;
653 struct fdisk_partition *pa = NULL;
654 size_t n;
655 int rc = 0;
656
657 assert(cxt);
658 assert(ent);
659 assert(fdisk_is_label(cxt, GPT));
660
661 DBG(MENU, ul_debug("enter GPT menu"));
662
663 if (ent->expert) {
664 switch (ent->key) {
665 case 'i':
666 return fdisk_set_disklabel_id(cxt);
667 case 'M':
668 mbr = fdisk_new_nested_context(cxt, "dos");
669 if (!mbr)
670 return -ENOMEM;
671 *cxt0 = cxt = mbr;
672 fdisk_enable_details(cxt, 1); /* keep us in expert mode */
673 fdisk_info(cxt, _("Entering protective/hybrid MBR disklabel."));
674 return 0;
675 }
676
677 /* actions where is necessary partnum */
678 rc = fdisk_ask_partnum(cxt, &n, FALSE);
679 if (rc)
680 return rc;
681
682 switch(ent->key) {
683 case 'u':
684 pa = fdisk_new_partition(); /* new template */
685 if (!pa)
686 rc = -ENOMEM;
687 else {
688 char *str = NULL;
689 rc = fdisk_ask_string(cxt, _("New UUID (in 8-4-4-4-12 format)"), &str);
690 if (!rc)
691 rc = fdisk_partition_set_uuid(pa, str);
692 if (!rc)
693 rc = fdisk_set_partition(cxt, n, pa);
694 free(str);
695 fdisk_unref_partition(pa);
696 }
697 break;
698 case 'n':
699 pa = fdisk_new_partition(); /* new template */
700 if (!pa)
701 rc = -ENOMEM;
702 else {
703 char *str = NULL;
704 rc = fdisk_ask_string(cxt, _("New name"), &str);
705 if (!rc)
706 rc = fdisk_partition_set_name(pa, str);
707 if (!rc)
708 rc = fdisk_set_partition(cxt, n, pa);
709 free(str);
710 fdisk_unref_partition(pa);
711 }
712 break;
713 case 'A':
714 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_LEGACYBOOT);
715 break;
716 case 'B':
717 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_NOBLOCK);
718 break;
719 case 'R':
720 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_REQUIRED);
721 break;
722 case 'S':
723 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_GUIDSPECIFIC);
724 break;
725 }
726 }
727
728 return rc;
729 }
730
731
732 /*
733 * This is fdisk frontend for MBR specific libfdisk functions that
734 * are not expported by generic libfdisk API.
735 */
736 static int dos_menu_cb(struct fdisk_context **cxt0,
737 const struct menu *menu __attribute__((__unused__)),
738 const struct menu_entry *ent)
739 {
740 struct fdisk_context *cxt = *cxt0;
741 int rc = 0;
742
743 DBG(MENU, ul_debug("enter DOS menu"));
744
745 if (!ent->expert) {
746 switch (ent->key) {
747 case 'a':
748 {
749 size_t n;
750 rc = fdisk_ask_partnum(cxt, &n, FALSE);
751 if (!rc)
752 rc = fdisk_toggle_partition_flag(cxt, n, DOS_FLAG_ACTIVE);
753 break;
754 }
755 case 'b':
756 {
757 struct fdisk_context *bsd
758 = fdisk_new_nested_context(cxt, "bsd");
759 if (!bsd)
760 return -ENOMEM;
761 if (!fdisk_has_label(bsd))
762 rc = fdisk_create_disklabel(bsd, "bsd");
763 if (rc)
764 fdisk_unref_context(bsd);
765 else {
766 *cxt0 = cxt = bsd;
767 fdisk_info(cxt, _("Entering nested BSD disklabel."));
768 }
769 break;
770 }
771 case 'c':
772 toggle_dos_compatibility_flag(cxt);
773 break;
774 }
775 return rc;
776 }
777
778 /* expert mode */
779 switch (ent->key) {
780 case 'b':
781 {
782 size_t n;
783 rc = fdisk_ask_partnum(cxt, &n, FALSE);
784 if (!rc)
785 rc = fdisk_dos_move_begin(cxt, n);
786 break;
787 }
788 case 'i':
789 rc = fdisk_set_disklabel_id(cxt);
790 break;
791 case 'M':
792 /* return from nested MBR to GPT */
793 if (fdisk_get_parent(cxt)) {
794 *cxt0 = fdisk_get_parent(cxt);
795
796 fdisk_info(cxt, _("Leaving nested disklabel."));
797 fdisk_unref_context(cxt);
798 cxt = *cxt0;
799 }
800 break;
801 }
802 return rc;
803 }
804
805 static int sun_menu_cb(struct fdisk_context **cxt0,
806 const struct menu *menu __attribute__((__unused__)),
807 const struct menu_entry *ent)
808 {
809 struct fdisk_context *cxt = *cxt0;
810 int rc = 0;
811
812 DBG(MENU, ul_debug("enter SUN menu"));
813
814 assert(cxt);
815 assert(ent);
816 assert(fdisk_is_label(cxt, SUN));
817
818 DBG(MENU, ul_debug("enter SUN menu"));
819
820 /* normal mode */
821 if (!ent->expert) {
822 size_t n;
823
824 rc = fdisk_ask_partnum(cxt, &n, FALSE);
825 if (rc)
826 return rc;
827 switch (ent->key) {
828 case 'a':
829 rc = fdisk_toggle_partition_flag(cxt, n, SUN_FLAG_RONLY);
830 break;
831 case 'c':
832 rc = fdisk_toggle_partition_flag(cxt, n, SUN_FLAG_UNMNT);
833 break;
834 }
835 return rc;
836 }
837
838 /* expert mode */
839 switch (ent->key) {
840 case 'a':
841 rc = fdisk_sun_set_alt_cyl(cxt);
842 break;
843 case 'e':
844 rc = fdisk_sun_set_xcyl(cxt);
845 break;
846 case 'i':
847 rc = fdisk_sun_set_ilfact(cxt);
848 break;
849 case 'o':
850 rc = fdisk_sun_set_rspeed(cxt);
851 break;
852 case 'y':
853 rc = fdisk_sun_set_pcylcount(cxt);
854 break;
855 }
856 return rc;
857 }
858
859 static int sgi_menu_cb(struct fdisk_context **cxt0,
860 const struct menu *menu __attribute__((__unused__)),
861 const struct menu_entry *ent)
862 {
863 struct fdisk_context *cxt = *cxt0;
864 int rc = -EINVAL;
865 size_t n = 0;
866
867 DBG(MENU, ul_debug("enter SGI menu"));
868
869 assert(cxt);
870 assert(ent);
871 assert(fdisk_is_label(cxt, SGI));
872
873 if (ent->expert)
874 return rc;
875
876 switch (ent->key) {
877 case 'a':
878 rc = fdisk_ask_partnum(cxt, &n, FALSE);
879 if (!rc)
880 rc = fdisk_toggle_partition_flag(cxt, n, SGI_FLAG_BOOT);
881 break;
882 case 'b':
883 fdisk_sgi_set_bootfile(cxt);
884 break;
885 case 'c':
886 rc = fdisk_ask_partnum(cxt, &n, FALSE);
887 if (!rc)
888 rc = fdisk_toggle_partition_flag(cxt, n, SGI_FLAG_SWAP);
889 break;
890 case 'i':
891 rc = fdisk_sgi_create_info(cxt);
892 break;
893 }
894
895 return rc;
896 }
897
898 /*
899 * This is fdisk frontend for BSD specific libfdisk functions that
900 * are not expported by generic libfdisk API.
901 */
902 static int bsd_menu_cb(struct fdisk_context **cxt0,
903 const struct menu *menu __attribute__((__unused__)),
904 const struct menu_entry *ent)
905 {
906 struct fdisk_context *cxt = *cxt0;
907 int rc = 0, org;
908
909 assert(cxt);
910 assert(ent);
911 assert(fdisk_is_label(cxt, BSD));
912
913 DBG(MENU, ul_debug("enter BSD menu"));
914
915 switch(ent->key) {
916 case 'e':
917 rc = fdisk_bsd_edit_disklabel(cxt);
918 break;
919 case 'i':
920 rc = fdisk_bsd_write_bootstrap(cxt);
921 break;
922 case 's':
923 org = fdisk_is_details(cxt);
924
925 fdisk_enable_details(cxt, 1);
926 list_disklabel(cxt);
927 fdisk_enable_details(cxt, org);
928 break;
929 case 'x':
930 rc = fdisk_bsd_link_partition(cxt);
931 break;
932 }
933 return rc;
934 }
935
936 /* C/H/S commands */
937 static int geo_menu_cb(struct fdisk_context **cxt0,
938 const struct menu *menu __attribute__((__unused__)),
939 const struct menu_entry *ent)
940 {
941 struct fdisk_context *cxt = *cxt0;
942 int rc = -EINVAL;
943 uintmax_t c = 0, h = 0, s = 0;
944
945 DBG(MENU, ul_debug("enter GEO menu"));
946
947 assert(cxt);
948 assert(ent);
949
950 switch (ent->key) {
951 case 'c':
952 rc = fdisk_ask_number(cxt, 1, fdisk_get_geom_cylinders(cxt),
953 1048576, _("Number of cylinders"), &c);
954 break;
955 case 'h':
956 rc = fdisk_ask_number(cxt, 1, fdisk_get_geom_heads(cxt),
957 256, _("Number of heads"), &h);
958 break;
959 case 's':
960 rc = fdisk_ask_number(cxt, 1, fdisk_get_geom_sectors(cxt),
961 63, _("Number of sectors"), &s);
962 break;
963 }
964
965 if (!rc)
966 fdisk_override_geometry(cxt, c, h, s);
967 return rc;
968 }
969
970 static int createlabel_menu_cb(struct fdisk_context **cxt0,
971 const struct menu *menu __attribute__((__unused__)),
972 const struct menu_entry *ent)
973 {
974 struct fdisk_context *cxt = *cxt0;
975 int rc = -EINVAL;
976
977 DBG(MENU, ul_debug("enter Create label menu"));
978
979 assert(cxt);
980 assert(ent);
981
982 if (ent->expert) {
983 switch (ent->key) {
984 case 'g':
985 /* Deprecated, use 'G' in main menu, just for backward
986 * compatibility only. */
987 rc = fdisk_create_disklabel(cxt, "sgi");
988 break;
989 }
990 return rc;
991 }
992
993 switch (ent->key) {
994 case 'g':
995 fdisk_create_disklabel(cxt, "gpt");
996 break;
997 case 'G':
998 fdisk_create_disklabel(cxt, "sgi");
999 break;
1000 case 'o':
1001 fdisk_create_disklabel(cxt, "dos");
1002 break;
1003 case 's':
1004 fdisk_create_disklabel(cxt, "sun");
1005 break;
1006 }
1007 return rc;
1008 }