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