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