]> git.ipfire.org Git - thirdparty/util-linux.git/blame - disk-utils/fdisk-menu.c
libfdisk: fix range checking for fdisk_set_last_lba
[thirdparty/util-linux.git] / disk-utils / fdisk-menu.c
CommitLineData
161b0d1a
KZ
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"
9f280903 10#include "pt-sun.h"
f02fecd1
KZ
11#include "pt-mbr.h"
12
161b0d1a 13struct menu_entry {
7e937b77
KZ
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 */
161b0d1a 20
7e937b77 21 enum fdisk_labeltype label; /* only for this label */
58c87bd0 22 int exclude; /* all labels except these */
f92e1810 23 enum fdisk_labeltype parent; /* for nested PT */
161b0d1a
KZ
24};
25
26#define IS_MENU_SEP(e) ((e)->key == '-')
27#define IS_MENU_HID(e) ((e)->hidden)
28
29struct menu {
30 enum fdisk_labeltype label; /* only for this label */
58c87bd0 31 int exclude; /* all labels except these */
161b0d1a 32
f92e1810
KZ
33 unsigned int nonested : 1; /* don't make this menu active in nested PT */
34
a47fec81 35 int (*callback)(struct fdisk_context **,
a410f8df
KZ
36 const struct menu *,
37 const struct menu_entry *);
161b0d1a
KZ
38
39 struct menu_entry entries[]; /* NULL terminated array */
40};
41
42struct 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 }
8e40a677 48#define DECLARE_MENU_CB(x) \
a47fec81 49 static int x(struct fdisk_context **, \
8e40a677
KZ
50 const struct menu *, \
51 const struct menu_entry *)
161b0d1a 52
8e40a677 53DECLARE_MENU_CB(gpt_menu_cb);
9f280903 54DECLARE_MENU_CB(sun_menu_cb);
aae727f2 55DECLARE_MENU_CB(sgi_menu_cb);
8e40a677 56DECLARE_MENU_CB(geo_menu_cb);
f02fecd1 57DECLARE_MENU_CB(dos_menu_cb);
b529ea2a 58DECLARE_MENU_CB(bsd_menu_cb);
b3ac22ef 59DECLARE_MENU_CB(createlabel_menu_cb);
89309968 60DECLARE_MENU_CB(generic_menu_cb);
b9e94cd7 61
161b0d1a
KZ
62/*
63 * Menu entry macros:
64 * MENU_X* expert mode only
65 * MENU_B* both -- expert + normal mode
66 *
7e937b77
KZ
67 * *_E exclude this label
68 * *_H hidden
69 * *_L only for this label
161b0d1a
KZ
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 }
7e937b77 80#define MENU_ENT_L(k, t, l) { .title = t, .key = k, .normal = 1, .label = l }
161b0d1a
KZ
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 }
27ddd4f1 86#define MENU_BENT_E(k, t, l) { .title = t, .key = k, .expert = 1, .normal = 1, .exclude = l }
161b0d1a 87
f92e1810
KZ
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 }
161b0d1a
KZ
90
91/* Generic menu */
92struct menu menu_generic = {
89309968 93 .callback = generic_menu_cb,
161b0d1a 94 .entries = {
6ae5e1e0 95 MENU_BSEP(N_("Generic")),
161b0d1a 96 MENU_ENT ('d', N_("delete a partition")),
0efd951c 97 MENU_ENT ('F', N_("list free unpartitioned space")),
161b0d1a
KZ
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")),
6ae5e1e0 101 MENU_ENT ('t', N_("change a partition type")),
27ddd4f1 102 MENU_BENT_E('v', N_("verify the partition table"), FDISK_DISKLABEL_BSD),
3e3b51b3 103 MENU_ENT ('i', N_("print information about a partition")),
161b0d1a 104
e916600f
KZ
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")),
dd7ba604 107 MENU_XENT('f', N_("fix partitions order")),
6ae5e1e0 108
161b0d1a
KZ
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),
22ddf547 112 MENU_ENT_E('x', N_("extra functionality (experts only)"), FDISK_DISKLABEL_BSD),
161b0d1a 113
a30e4ef4 114 MENU_SEP(N_("Script")),
5cb9918d
KZ
115 MENU_ENT ('I', N_("load disk layout from sfdisk script file")),
116 MENU_ENT ('O', N_("dump disk layout to sfdisk script file")),
a30e4ef4 117
2a1a67df 118 MENU_BSEP(N_("Save & Exit")),
22ddf547
KZ
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),
161b0d1a
KZ
121 MENU_BENT ('q', N_("quit without saving changes")),
122 MENU_XENT ('r', N_("return to main menu")),
f92e1810
KZ
123
124 MENU_ENT_NEST('r', N_("return from BSD to DOS"), FDISK_DISKLABEL_BSD, FDISK_DISKLABEL_DOS),
161b0d1a
KZ
125
126 { 0, NULL }
127 }
128};
129
130struct menu menu_createlabel = {
b3ac22ef 131 .callback = createlabel_menu_cb,
22ddf547 132 .exclude = FDISK_DISKLABEL_BSD,
f92e1810 133 .nonested = 1,
161b0d1a
KZ
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
be9ba6f3 148struct menu menu_geo = {
8e40a677 149 .callback = geo_menu_cb,
22ddf547 150 .exclude = FDISK_DISKLABEL_GPT | FDISK_DISKLABEL_BSD,
be9ba6f3
KZ
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
161b0d1a 160struct menu menu_gpt = {
b9e94cd7 161 .callback = gpt_menu_cb,
161b0d1a
KZ
162 .label = FDISK_DISKLABEL_GPT,
163 .entries = {
164 MENU_XSEP(N_("GPT")),
35b1f0a4 165 MENU_XENT('i', N_("change disk GUID")),
161b0d1a 166 MENU_XENT('n', N_("change partition name")),
35b1f0a4 167 MENU_XENT('u', N_("change partition UUID")),
f92e1810 168 MENU_XENT('M', N_("enter protective/hybrid MBR")),
f362d863
KZ
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
161b0d1a
KZ
176 { 0, NULL }
177 }
178};
179
2a1a67df 180struct menu menu_sun = {
9f280903 181 .callback = sun_menu_cb,
2a1a67df
KZ
182 .label = FDISK_DISKLABEL_SUN,
183 .entries = {
184 MENU_BSEP(N_("Sun")),
aaa43826 185 MENU_ENT('a', N_("toggle the read-only flag")),
2a1a67df
KZ
186 MENU_ENT('c', N_("toggle the mountable flag")),
187
188 MENU_XENT('a', N_("change number of alternate cylinders")),
2a1a67df 189 MENU_XENT('e', N_("change number of extra sectors per cylinder")),
2a1a67df
KZ
190 MENU_XENT('i', N_("change interleave factor")),
191 MENU_XENT('o', N_("change rotation speed (rpm)")),
2a1a67df
KZ
192 MENU_XENT('y', N_("change number of physical cylinders")),
193 { 0, NULL }
194 }
195};
196
6ae5e1e0 197struct menu menu_sgi = {
aae727f2 198 .callback = sgi_menu_cb,
6ae5e1e0
KZ
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")),
aae727f2 205 MENU_ENT('i', N_("create SGI info")),
6ae5e1e0
KZ
206 { 0, NULL }
207 }
208};
209
210struct menu menu_dos = {
f02fecd1 211 .callback = dos_menu_cb,
6ae5e1e0
KZ
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")),
6ae5e1e0 220 MENU_XENT('i', N_("change the disk identifier")),
f92e1810
KZ
221
222 MENU_XENT_NEST('M', N_("return from protective/hybrid MBR to GPT"),
223 FDISK_DISKLABEL_DOS, FDISK_DISKLABEL_GPT),
6ae5e1e0
KZ
224 { 0, NULL }
225 }
226};
227
228struct menu menu_bsd = {
b529ea2a 229 .callback = bsd_menu_cb,
22ddf547 230 .label = FDISK_DISKLABEL_BSD,
6ae5e1e0
KZ
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")),
6ae5e1e0 236 MENU_ENT('x', N_("link BSD partition to non-BSD partition")),
6ae5e1e0
KZ
237 { 0, NULL }
238 }
239};
240
161b0d1a 241static const struct menu *menus[] = {
6ae5e1e0
KZ
242 &menu_gpt,
243 &menu_sun,
244 &menu_sgi,
245 &menu_dos,
246 &menu_bsd,
be9ba6f3 247 &menu_geo,
161b0d1a
KZ
248 &menu_generic,
249 &menu_createlabel,
161b0d1a
KZ
250};
251
252static const struct menu_entry *next_menu_entry(
253 struct fdisk_context *cxt,
254 struct menu_context *mc)
255{
8eccde20
KZ
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
68ddb136
KZ
262 if (lb)
263 type = fdisk_label_get_type(lb);
8eccde20
KZ
264 if (parent)
265 pr_type = fdisk_label_get_type(fdisk_get_label(parent, NULL));
266
161b0d1a
KZ
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
f92e1810
KZ
271 /*
272 * whole-menu filter
273 */
274
be9ba6f3 275 /* no more entries */
161b0d1a 276 if (e->title == NULL ||
be9ba6f3 277 /* menu wanted for specified labels only */
8eccde20 278 (m->label && lb && !(m->label & type)) ||
f92e1810 279 /* unwanted for nested PT */
8eccde20 280 (m->nonested && parent) ||
be9ba6f3 281 /* menu excluded for specified labels */
8eccde20 282 (m->exclude && lb && (m->exclude & type))) {
161b0d1a
KZ
283 mc->menu_idx++;
284 mc->entry_idx = 0;
285 continue;
286 }
287
f92e1810
KZ
288 /*
289 * per entry filter
290 */
291
7e937b77 292 /* excluded for the current label */
8eccde20 293 if ((e->exclude && lb && e->exclude & type) ||
7e937b77 294 /* entry wanted for specified labels only */
8eccde20 295 (e->label && lb && !(e->label & type)) ||
161b0d1a 296 /* exclude non-expert entries in expect mode */
6a632136 297 (e->expert == 0 && fdisk_is_details(cxt)) ||
f92e1810 298 /* nested only */
8eccde20 299 (e->parent && (!parent || pr_type != e->parent)) ||
161b0d1a 300 /* exclude non-normal entries in normal mode */
6a632136 301 (e->normal == 0 && !fdisk_is_details(cxt))) {
161b0d1a
KZ
302 mc->entry_idx++;
303 continue;
304 }
305 mc->entry_idx++;
306 return e;
307
308 }
309 return NULL;
310}
311
3b4f9f16
KZ
312/* returns @menu and menu entry for then @key */
313static 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
333static 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) {
e6d0c4c1 344 DBG(MENU, ul_debug("warning: not found "
3b4f9f16
KZ
345 "entry for %c", e->key));
346 return -1;
347 }
348 if (r != e) {
e6d0c4c1 349 DBG(MENU, ul_debug("warning: duplicate key '%c'",
3b4f9f16 350 e->key));
e6d0c4c1
KZ
351 DBG(MENU, ul_debug(" : %s", e->title));
352 DBG(MENU, ul_debug(" : %s", r->title));
3b4f9f16
KZ
353 abort();
354 }
355 }
356
357 return 0;
358}
359
1f75d1ce 360static int print_fdisk_menu(struct fdisk_context *cxt)
161b0d1a
KZ
361{
362 struct menu_context mc = MENU_CXT_EMPTY;
363 const struct menu_entry *e;
364
e6d0c4c1 365 ON_DBG(MENU, menu_detect_collisions(cxt));
3b4f9f16 366
6a632136 367 if (fdisk_is_details(cxt))
39f01b7b 368 printf(_("\nHelp (expert commands):\n"));
161b0d1a 369 else
39f01b7b 370 printf(_("\nHelp:\n"));
161b0d1a
KZ
371
372 while ((e = next_menu_entry(cxt, &mc))) {
373 if (IS_MENU_HID(e))
374 continue; /* hidden entry */
f362d863
KZ
375 if (IS_MENU_SEP(e) && (!e->title || !*e->title))
376 printf("\n");
377 else if (IS_MENU_SEP(e)) {
496c979a 378 color_scheme_enable("help-title", UL_COLOR_BOLD);
161b0d1a 379 printf("\n %s\n", _(e->title));
80a1712f
KZ
380 color_disable();
381 } else
161b0d1a
KZ
382 printf(" %c %s\n", e->key, _(e->title));
383 }
384 fputc('\n', stdout);
385
8eccde20
KZ
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
f92e1810
KZ
390 fdisk_info(cxt, _("You're editing nested '%s' partition table, "
391 "primary partition table is '%s'."),
8eccde20
KZ
392 fdisk_label_get_name(l),
393 fdisk_label_get_name(p));
394 }
f92e1810 395
161b0d1a
KZ
396 return 0;
397}
398
a410f8df
KZ
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 *
a47fec81
KZ
403 * Note that this function might exchange the context pointer to
404 * switch to another (nested) context.
405 *
a410f8df
KZ
406 * Returns: <0 on error
407 * 0 on success (the command performed)
408 * >0 if no callback (then returns the key)
409 */
a47fec81 410int process_fdisk_menu(struct fdisk_context **cxt0)
a410f8df 411{
a47fec81 412 struct fdisk_context *cxt = *cxt0;
a410f8df
KZ
413 const struct menu_entry *ent;
414 const struct menu *menu;
415 int key, rc;
416 const char *prompt;
417 char buf[BUFSIZ];
418
6a632136 419 if (fdisk_is_details(cxt))
a410f8df
KZ
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
e6d0c4c1 436 DBG(MENU, ul_debug("selected: key=%c, entry='%s'",
a410f8df 437 key, ent->title));
a410f8df
KZ
438
439 /* menu has implemented callback, use it */
27ddd4f1 440 if (menu->callback)
a47fec81
KZ
441 rc = menu->callback(cxt0, menu, ent);
442 else {
e6d0c4c1 443 DBG(MENU, ul_debug("no callback for key '%c'", key));
27ddd4f1 444 rc = -EINVAL;
a47fec81 445 }
a410f8df 446
e6d0c4c1 447 DBG(MENU, ul_debug("process menu done [rc=%d]", rc));
a47fec81 448 return rc;
a410f8df
KZ
449}
450
a30e4ef4
KZ
451static int script_read(struct fdisk_context *cxt)
452{
453 struct fdisk_script *sc = NULL;
454 char *filename = NULL;
455 int rc;
456
457 rc = fdisk_ask_string(cxt, _("Enter script file name"), &filename);
458 if (rc)
459 return rc;
460
461 errno = 0;
462 sc = fdisk_new_script_from_file(cxt, filename);
463 if (!sc && errno)
54fefa07 464 fdisk_warn(cxt, _("Cannot open %s"), filename);
a30e4ef4
KZ
465 else if (!sc)
466 fdisk_warnx(cxt, _("Failed to parse script file %s"), filename);
467 else if (fdisk_apply_script(cxt, sc) != 0)
468 fdisk_warnx(cxt, _("Failed to apply script %s"), filename);
469 else
470 fdisk_info(cxt, _("Script successfully applied."));
471
472 fdisk_unref_script(sc);
473 free(filename);
474 return rc;
475}
476
477static int script_write(struct fdisk_context *cxt)
478{
479 struct fdisk_script *sc = NULL;
480 char *filename = NULL;
481 FILE *f = NULL;
482 int rc;
483
484 rc = fdisk_ask_string(cxt, _("Enter script file name"), &filename);
485 if (rc)
486 return rc;
487
488 sc = fdisk_new_script(cxt);
489 if (!sc) {
490 fdisk_warn(cxt, _("Failed to allocate script handler"));
491 goto done;
492 }
493
494 rc = fdisk_script_read_context(sc, NULL);
495 if (rc) {
54fefa07 496 fdisk_warnx(cxt, _("Failed to transform disk layout into script"));
a30e4ef4
KZ
497 goto done;
498 }
499
500 f = fopen(filename, "w");
501 if (!f) {
54fefa07 502 fdisk_warn(cxt, _("Cannot open %s"), filename);
a30e4ef4
KZ
503 goto done;
504 }
505
506 rc = fdisk_script_write_file(sc, f);
507 if (rc)
508 fdisk_warn(cxt, _("Failed to write script %s"), filename);
509 else
510 fdisk_info(cxt, _("Script successfully saved."));
511done:
512 if (f)
513 fclose(f);
514 fdisk_unref_script(sc);
515 free(filename);
516 return rc;
517}
518
ba465623
KZ
519static int ask_for_wipe(struct fdisk_context *cxt, size_t partno)
520{
521 struct fdisk_partition *tmp = NULL;
522 char *fstype = NULL;
523 int rc, yes = 0;
524
525 rc = fdisk_get_partition(cxt, partno, &tmp);
526 if (rc)
527 goto done;
528
529 rc = fdisk_partition_to_string(tmp, cxt, FDISK_FIELD_FSTYPE, &fstype);
530 if (rc || fstype == NULL)
531 goto done;
532
533 fdisk_warnx(cxt, _("Partition #%zu contains a %s signature."), partno + 1, fstype);
534
535 if (pwipemode == WIPEMODE_AUTO && isatty(STDIN_FILENO))
536 fdisk_ask_yesno(cxt, _("Do you want to remove the signature?"), &yes);
537 else if (pwipemode == WIPEMODE_ALWAYS)
538 yes = 1;
539
540 if (yes) {
541 fdisk_info(cxt, _("The signature will be removed by a write command."));
542 rc = fdisk_wipe_partition(cxt, partno, TRUE);
543 }
544done:
545 fdisk_unref_partition(tmp);
546 free(fstype);
547 return rc;
548}
549
89309968
KZ
550/*
551 * Basic fdisk actions
552 */
553static int generic_menu_cb(struct fdisk_context **cxt0,
554 const struct menu *menu __attribute__((__unused__)),
555 const struct menu_entry *ent)
556{
557 struct fdisk_context *cxt = *cxt0;
558 int rc = 0;
27ddd4f1 559 size_t n;
89309968 560
27ddd4f1 561 /* actions shared between expert and normal mode */
89309968 562 switch (ent->key) {
89309968
KZ
563 case 'p':
564 list_disk_geometry(cxt);
9f670072 565 list_disklabel(cxt);
89309968 566 break;
df4bfa97 567 case 'w':
6a632136 568 if (fdisk_is_readonly(cxt)) {
62eea9ce 569 fdisk_warnx(cxt, _("Device is open in read-only mode."));
e146ae4e
KZ
570 break;
571 }
df4bfa97
KZ
572 rc = fdisk_write_disklabel(cxt);
573 if (rc)
fd56121a 574 err(EXIT_FAILURE, _("failed to write disklabel"));
8eccde20 575 if (fdisk_get_parent(cxt))
df4bfa97
KZ
576 break; /* nested PT, don't leave */
577 fdisk_info(cxt, _("The partition table has been altered."));
a57639e1
KZ
578 rc = fdisk_reread_partition_table(cxt);
579 if (!rc)
6a632136 580 rc = fdisk_deassign_device(cxt, 0);
a57639e1 581 /* fallthrough */
89309968 582 case 'q':
c7119037 583 fdisk_unref_context(cxt);
a57639e1
KZ
584 fputc('\n', stdout);
585 exit(rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
27ddd4f1
KZ
586 case 'm':
587 rc = print_fdisk_menu(cxt);
588 break;
89309968
KZ
589 case 'v':
590 rc = fdisk_verify_disklabel(cxt);
591 break;
3e3b51b3
JLB
592 case 'i':
593 rc = print_partition_info(cxt);
594 break;
0efd951c
KZ
595 case 'F':
596 list_freespace(cxt);
597 break;
27ddd4f1
KZ
598 }
599
600 /* expert mode */
601 if (ent->expert) {
602 switch (ent->key) {
603 case 'd':
e916600f
KZ
604 dump_firstsector(cxt);
605 break;
606 case 'D':
607 dump_disklabel(cxt);
27ddd4f1 608 break;
dd7ba604
KZ
609 case 'f':
610 rc = fdisk_reorder_partitions(cxt);
4afb337e 611 if (rc)
056e622c 612 fdisk_warnx(cxt, _("Failed to fix partitions order."));
4afb337e
KZ
613 else
614 fdisk_info(cxt, _("Partitions order fixed."));
dd7ba604 615 break;
27ddd4f1 616 case 'r':
6a632136 617 rc = fdisk_enable_details(cxt, 0);
27ddd4f1
KZ
618 break;
619 }
620 return rc;
621 }
622
623 /* normal mode */
624 switch (ent->key) {
625 case 'd':
626 rc = fdisk_ask_partnum(cxt, &n, FALSE);
627 if (!rc)
628 rc = fdisk_delete_partition(cxt, n);
629 if (rc)
48d4d931 630 fdisk_warnx(cxt, _("Could not delete partition %zu"), n + 1);
27ddd4f1 631 else
48d4d931 632 fdisk_info(cxt, _("Partition %zu has been deleted."), n + 1);
27ddd4f1 633 break;
a30e4ef4
KZ
634 case 'I':
635 script_read(cxt);
636 break;
637 case 'O':
638 script_write(cxt);
639 break;
27ddd4f1
KZ
640 case 'l':
641 list_partition_types(cxt);
642 break;
643 case 'n':
ba465623
KZ
644 {
645 size_t partno;
646 rc = fdisk_add_partition(cxt, NULL, &partno);
647 if (!rc)
648 rc = ask_for_wipe(cxt, partno);
27ddd4f1 649 break;
ba465623 650 }
27ddd4f1
KZ
651 case 't':
652 change_partition_type(cxt);
653 break;
654 case 'u':
6a632136
KZ
655 fdisk_set_unit(cxt,
656 fdisk_use_cylinders(cxt) ? "sectors" :
27ddd4f1 657 "cylinders");
6a632136 658 if (fdisk_use_cylinders(cxt))
27ddd4f1
KZ
659 fdisk_info(cxt, _("Changing display/entry units to cylinders (DEPRECATED!)."));
660 else
661 fdisk_info(cxt, _("Changing display/entry units to sectors."));
662 break;
663 case 'x':
6a632136 664 fdisk_enable_details(cxt, 1);
27ddd4f1
KZ
665 break;
666 case 'r':
f92e1810 667 /* return from nested BSD to DOS */
8eccde20
KZ
668 if (fdisk_get_parent(cxt)) {
669 *cxt0 = fdisk_get_parent(cxt);
27ddd4f1 670
fd56121a 671 fdisk_info(cxt, _("Leaving nested disklabel."));
c7119037 672 fdisk_unref_context(cxt);
27ddd4f1
KZ
673 }
674 break;
89309968 675 }
27ddd4f1 676
89309968
KZ
677 return rc;
678}
679
8e40a677 680
b9e94cd7
KZ
681/*
682 * This is fdisk frontend for GPT specific libfdisk functions that
683 * are not expported by generic libfdisk API.
684 */
a47fec81 685static int gpt_menu_cb(struct fdisk_context **cxt0,
b9e94cd7
KZ
686 const struct menu *menu __attribute__((__unused__)),
687 const struct menu_entry *ent)
688{
a47fec81 689 struct fdisk_context *cxt = *cxt0;
f92e1810 690 struct fdisk_context *mbr;
6936c081 691 struct fdisk_partition *pa = NULL;
b9e94cd7 692 size_t n;
f92e1810 693 int rc = 0;
b9e94cd7
KZ
694
695 assert(cxt);
696 assert(ent);
aa36c2cf 697 assert(fdisk_is_label(cxt, GPT));
b9e94cd7 698
e6d0c4c1 699 DBG(MENU, ul_debug("enter GPT menu"));
b9e94cd7 700
f92e1810
KZ
701 if (ent->expert) {
702 switch (ent->key) {
703 case 'i':
704 return fdisk_set_disklabel_id(cxt);
705 case 'M':
706 mbr = fdisk_new_nested_context(cxt, "dos");
707 if (!mbr)
708 return -ENOMEM;
709 *cxt0 = cxt = mbr;
6a632136 710 fdisk_enable_details(cxt, 1); /* keep us in expert mode */
0477369a 711 fdisk_info(cxt, _("Entering protective/hybrid MBR disklabel."));
f92e1810
KZ
712 return 0;
713 }
35b1f0a4 714
f92e1810
KZ
715 /* actions where is necessary partnum */
716 rc = fdisk_ask_partnum(cxt, &n, FALSE);
717 if (rc)
718 return rc;
b9e94cd7 719
f92e1810
KZ
720 switch(ent->key) {
721 case 'u':
6936c081
KZ
722 pa = fdisk_new_partition(); /* new template */
723 if (!pa)
724 rc = -ENOMEM;
725 else {
726 char *str = NULL;
727 rc = fdisk_ask_string(cxt, _("New UUID (in 8-4-4-4-12 format)"), &str);
728 if (!rc)
729 rc = fdisk_partition_set_uuid(pa, str);
730 if (!rc)
731 rc = fdisk_set_partition(cxt, n, pa);
732 free(str);
733 fdisk_unref_partition(pa);
734 }
f92e1810
KZ
735 break;
736 case 'n':
6936c081
KZ
737 pa = fdisk_new_partition(); /* new template */
738 if (!pa)
739 rc = -ENOMEM;
740 else {
741 char *str = NULL;
742 rc = fdisk_ask_string(cxt, _("New name"), &str);
743 if (!rc)
744 rc = fdisk_partition_set_name(pa, str);
745 if (!rc)
746 rc = fdisk_set_partition(cxt, n, pa);
747 free(str);
748 fdisk_unref_partition(pa);
749 }
f92e1810 750 break;
f362d863 751 case 'A':
a1ef792f 752 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_LEGACYBOOT);
f362d863
KZ
753 break;
754 case 'B':
a1ef792f 755 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_NOBLOCK);
f362d863
KZ
756 break;
757 case 'R':
a1ef792f 758 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_REQUIRED);
f362d863
KZ
759 break;
760 case 'S':
a1ef792f 761 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_GUIDSPECIFIC);
f362d863 762 break;
f92e1810 763 }
b9e94cd7 764 }
6936c081 765
b9e94cd7
KZ
766 return rc;
767}
768
f02fecd1
KZ
769
770/*
771 * This is fdisk frontend for MBR specific libfdisk functions that
772 * are not expported by generic libfdisk API.
773 */
a47fec81 774static int dos_menu_cb(struct fdisk_context **cxt0,
f02fecd1
KZ
775 const struct menu *menu __attribute__((__unused__)),
776 const struct menu_entry *ent)
777{
a47fec81 778 struct fdisk_context *cxt = *cxt0;
f02fecd1
KZ
779 int rc = 0;
780
e6d0c4c1 781 DBG(MENU, ul_debug("enter DOS menu"));
a47fec81 782
f02fecd1
KZ
783 if (!ent->expert) {
784 switch (ent->key) {
785 case 'a':
786 {
787 size_t n;
788 rc = fdisk_ask_partnum(cxt, &n, FALSE);
789 if (!rc)
a1ef792f 790 rc = fdisk_toggle_partition_flag(cxt, n, DOS_FLAG_ACTIVE);
f02fecd1
KZ
791 break;
792 }
793 case 'b':
794 {
795 struct fdisk_context *bsd
796 = fdisk_new_nested_context(cxt, "bsd");
818d7924
KZ
797 if (!bsd)
798 return -ENOMEM;
aa36c2cf 799 if (!fdisk_has_label(bsd))
818d7924 800 rc = fdisk_create_disklabel(bsd, "bsd");
a47fec81 801 if (rc)
c7119037 802 fdisk_unref_context(bsd);
27ddd4f1 803 else {
a47fec81 804 *cxt0 = cxt = bsd;
0477369a 805 fdisk_info(cxt, _("Entering nested BSD disklabel."));
27ddd4f1 806 }
f02fecd1
KZ
807 break;
808 }
809 case 'c':
810 toggle_dos_compatibility_flag(cxt);
811 break;
812 }
813 return rc;
814 }
815
816 /* expert mode */
817 switch (ent->key) {
818 case 'b':
819 {
820 size_t n;
821 rc = fdisk_ask_partnum(cxt, &n, FALSE);
822 if (!rc)
f8ad3899 823 rc = fdisk_dos_move_begin(cxt, n);
f02fecd1
KZ
824 break;
825 }
f02fecd1
KZ
826 case 'i':
827 rc = fdisk_set_disklabel_id(cxt);
828 break;
f92e1810
KZ
829 case 'M':
830 /* return from nested MBR to GPT */
8eccde20
KZ
831 if (fdisk_get_parent(cxt)) {
832 *cxt0 = fdisk_get_parent(cxt);
f92e1810
KZ
833
834 fdisk_info(cxt, _("Leaving nested disklabel."));
c7119037 835 fdisk_unref_context(cxt);
f92e1810
KZ
836 }
837 break;
f02fecd1
KZ
838 }
839 return rc;
840}
841
a47fec81 842static int sun_menu_cb(struct fdisk_context **cxt0,
9f280903
KZ
843 const struct menu *menu __attribute__((__unused__)),
844 const struct menu_entry *ent)
845{
a47fec81 846 struct fdisk_context *cxt = *cxt0;
9f280903
KZ
847 int rc = 0;
848
e6d0c4c1 849 DBG(MENU, ul_debug("enter SUN menu"));
a47fec81 850
9f280903
KZ
851 assert(cxt);
852 assert(ent);
aa36c2cf 853 assert(fdisk_is_label(cxt, SUN));
9f280903 854
e6d0c4c1 855 DBG(MENU, ul_debug("enter SUN menu"));
9f280903
KZ
856
857 /* normal mode */
858 if (!ent->expert) {
859 size_t n;
860
861 rc = fdisk_ask_partnum(cxt, &n, FALSE);
862 if (rc)
863 return rc;
864 switch (ent->key) {
865 case 'a':
a1ef792f 866 rc = fdisk_toggle_partition_flag(cxt, n, SUN_FLAG_RONLY);
9f280903
KZ
867 break;
868 case 'c':
a1ef792f 869 rc = fdisk_toggle_partition_flag(cxt, n, SUN_FLAG_UNMNT);
9f280903
KZ
870 break;
871 }
872 return rc;
873 }
874
875 /* expert mode */
876 switch (ent->key) {
877 case 'a':
878 rc = fdisk_sun_set_alt_cyl(cxt);
879 break;
880 case 'e':
881 rc = fdisk_sun_set_xcyl(cxt);
882 break;
883 case 'i':
884 rc = fdisk_sun_set_ilfact(cxt);
885 break;
886 case 'o':
887 rc = fdisk_sun_set_rspeed(cxt);
888 break;
889 case 'y':
890 rc = fdisk_sun_set_pcylcount(cxt);
891 break;
892 }
893 return rc;
894}
895
aae727f2
KZ
896static int sgi_menu_cb(struct fdisk_context **cxt0,
897 const struct menu *menu __attribute__((__unused__)),
898 const struct menu_entry *ent)
899{
900 struct fdisk_context *cxt = *cxt0;
901 int rc = -EINVAL;
902 size_t n = 0;
903
e6d0c4c1 904 DBG(MENU, ul_debug("enter SGI menu"));
aae727f2
KZ
905
906 assert(cxt);
907 assert(ent);
aa36c2cf 908 assert(fdisk_is_label(cxt, SGI));
aae727f2
KZ
909
910 if (ent->expert)
911 return rc;
912
913 switch (ent->key) {
914 case 'a':
915 rc = fdisk_ask_partnum(cxt, &n, FALSE);
916 if (!rc)
a1ef792f 917 rc = fdisk_toggle_partition_flag(cxt, n, SGI_FLAG_BOOT);
aae727f2
KZ
918 break;
919 case 'b':
ac84272d 920 fdisk_sgi_set_bootfile(cxt);
aae727f2
KZ
921 break;
922 case 'c':
923 rc = fdisk_ask_partnum(cxt, &n, FALSE);
924 if (!rc)
a1ef792f 925 rc = fdisk_toggle_partition_flag(cxt, n, SGI_FLAG_SWAP);
aae727f2
KZ
926 break;
927 case 'i':
ac84272d 928 rc = fdisk_sgi_create_info(cxt);
aae727f2
KZ
929 break;
930 }
931
932 return rc;
933}
934
b529ea2a
KZ
935/*
936 * This is fdisk frontend for BSD specific libfdisk functions that
937 * are not expported by generic libfdisk API.
938 */
939static int bsd_menu_cb(struct fdisk_context **cxt0,
940 const struct menu *menu __attribute__((__unused__)),
941 const struct menu_entry *ent)
942{
943 struct fdisk_context *cxt = *cxt0;
e563f055 944 int rc = 0, org;
b529ea2a
KZ
945
946 assert(cxt);
947 assert(ent);
aa36c2cf 948 assert(fdisk_is_label(cxt, BSD));
b529ea2a 949
e6d0c4c1 950 DBG(MENU, ul_debug("enter BSD menu"));
b529ea2a
KZ
951
952 switch(ent->key) {
953 case 'e':
954 rc = fdisk_bsd_edit_disklabel(cxt);
955 break;
956 case 'i':
957 rc = fdisk_bsd_write_bootstrap(cxt);
958 break;
959 case 's':
6a632136 960 org = fdisk_is_details(cxt);
e563f055 961
6a632136 962 fdisk_enable_details(cxt, 1);
9f670072 963 list_disklabel(cxt);
6a632136 964 fdisk_enable_details(cxt, org);
b529ea2a
KZ
965 break;
966 case 'x':
967 rc = fdisk_bsd_link_partition(cxt);
968 break;
969 }
970 return rc;
971}
972
8e40a677 973/* C/H/S commands */
a47fec81 974static int geo_menu_cb(struct fdisk_context **cxt0,
8e40a677
KZ
975 const struct menu *menu __attribute__((__unused__)),
976 const struct menu_entry *ent)
977{
a47fec81 978 struct fdisk_context *cxt = *cxt0;
8e40a677
KZ
979 int rc = -EINVAL;
980 uintmax_t c = 0, h = 0, s = 0;
981
e6d0c4c1 982 DBG(MENU, ul_debug("enter GEO menu"));
a47fec81 983
8e40a677
KZ
984 assert(cxt);
985 assert(ent);
986
987 switch (ent->key) {
988 case 'c':
8eccde20 989 rc = fdisk_ask_number(cxt, 1, fdisk_get_geom_cylinders(cxt),
8e40a677
KZ
990 1048576, _("Number of cylinders"), &c);
991 break;
992 case 'h':
8eccde20 993 rc = fdisk_ask_number(cxt, 1, fdisk_get_geom_heads(cxt),
8e40a677
KZ
994 256, _("Number of heads"), &h);
995 break;
996 case 's':
8eccde20 997 rc = fdisk_ask_number(cxt, 1, fdisk_get_geom_sectors(cxt),
8e40a677
KZ
998 63, _("Number of sectors"), &s);
999 break;
1000 }
1001
1002 if (!rc)
1003 fdisk_override_geometry(cxt, c, h, s);
1004 return rc;
1005}
1006
b3ac22ef
KZ
1007static int createlabel_menu_cb(struct fdisk_context **cxt0,
1008 const struct menu *menu __attribute__((__unused__)),
1009 const struct menu_entry *ent)
1010{
1011 struct fdisk_context *cxt = *cxt0;
1012 int rc = -EINVAL;
1013
e6d0c4c1 1014 DBG(MENU, ul_debug("enter Create label menu"));
b3ac22ef
KZ
1015
1016 assert(cxt);
1017 assert(ent);
1018
aae727f2
KZ
1019 if (ent->expert) {
1020 switch (ent->key) {
1021 case 'g':
1022 /* Deprecated, use 'G' in main menu, just for backward
1023 * compatibility only. */
1024 rc = fdisk_create_disklabel(cxt, "sgi");
1025 break;
1026 }
1027 return rc;
1028 }
1029
b3ac22ef
KZ
1030 switch (ent->key) {
1031 case 'g':
1032 fdisk_create_disklabel(cxt, "gpt");
1033 break;
1034 case 'G':
1035 fdisk_create_disklabel(cxt, "sgi");
1036 break;
1037 case 'o':
1038 fdisk_create_disklabel(cxt, "dos");
1039 break;
1040 case 's':
1041 fdisk_create_disklabel(cxt, "sun");
1042 break;
1043 }
1044 return rc;
1045}