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