]>
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" | |
ebfab541 | 9 | #include "rpmatch.h" |
161b0d1a | 10 | #include "fdisk.h" |
9f280903 | 11 | #include "pt-sun.h" |
f02fecd1 KZ |
12 | #include "pt-mbr.h" |
13 | ||
161b0d1a | 14 | struct menu_entry { |
7e937b77 KZ |
15 | const char key; /* command key */ |
16 | const char *title; /* help string */ | |
17 | unsigned int normal : 1, /* normal mode */ | |
18 | expert : 1, /* expert mode */ | |
19 | hidden : 1; /* be sensitive for this key, | |
20 | but don't print it in help */ | |
161b0d1a | 21 | |
7e937b77 | 22 | enum fdisk_labeltype label; /* only for this label */ |
58c87bd0 | 23 | int exclude; /* all labels except these */ |
f92e1810 | 24 | enum fdisk_labeltype parent; /* for nested PT */ |
161b0d1a KZ |
25 | }; |
26 | ||
27 | #define IS_MENU_SEP(e) ((e)->key == '-') | |
28 | #define IS_MENU_HID(e) ((e)->hidden) | |
29 | ||
30 | struct menu { | |
31 | enum fdisk_labeltype label; /* only for this label */ | |
58c87bd0 | 32 | int exclude; /* all labels except these */ |
161b0d1a | 33 | |
f92e1810 KZ |
34 | unsigned int nonested : 1; /* don't make this menu active in nested PT */ |
35 | ||
a47fec81 | 36 | int (*callback)(struct fdisk_context **, |
a410f8df KZ |
37 | const struct menu *, |
38 | const struct menu_entry *); | |
161b0d1a KZ |
39 | |
40 | struct menu_entry entries[]; /* NULL terminated array */ | |
41 | }; | |
42 | ||
43 | struct menu_context { | |
44 | size_t menu_idx; /* the current menu */ | |
45 | size_t entry_idx; /* index with in the current menu */ | |
46 | }; | |
47 | ||
48 | #define MENU_CXT_EMPTY { 0, 0 } | |
8e40a677 | 49 | #define DECLARE_MENU_CB(x) \ |
a47fec81 | 50 | static int x(struct fdisk_context **, \ |
8e40a677 KZ |
51 | const struct menu *, \ |
52 | const struct menu_entry *) | |
161b0d1a | 53 | |
8e40a677 | 54 | DECLARE_MENU_CB(gpt_menu_cb); |
9f280903 | 55 | DECLARE_MENU_CB(sun_menu_cb); |
aae727f2 | 56 | DECLARE_MENU_CB(sgi_menu_cb); |
8e40a677 | 57 | DECLARE_MENU_CB(geo_menu_cb); |
f02fecd1 | 58 | DECLARE_MENU_CB(dos_menu_cb); |
b529ea2a | 59 | DECLARE_MENU_CB(bsd_menu_cb); |
b3ac22ef | 60 | DECLARE_MENU_CB(createlabel_menu_cb); |
89309968 | 61 | DECLARE_MENU_CB(generic_menu_cb); |
b9e94cd7 | 62 | |
161b0d1a KZ |
63 | /* |
64 | * Menu entry macros: | |
65 | * MENU_X* expert mode only | |
66 | * MENU_B* both -- expert + normal mode | |
67 | * | |
7e937b77 KZ |
68 | * *_E exclude this label |
69 | * *_H hidden | |
70 | * *_L only for this label | |
161b0d1a KZ |
71 | */ |
72 | ||
73 | /* separator */ | |
74 | #define MENU_SEP(t) { .title = t, .key = '-', .normal = 1 } | |
75 | #define MENU_XSEP(t) { .title = t, .key = '-', .expert = 1 } | |
76 | #define MENU_BSEP(t) { .title = t, .key = '-', .expert = 1, .normal = 1 } | |
77 | ||
78 | /* entry */ | |
79 | #define MENU_ENT(k, t) { .title = t, .key = k, .normal = 1 } | |
80 | #define MENU_ENT_E(k, t, l) { .title = t, .key = k, .normal = 1, .exclude = l } | |
7e937b77 | 81 | #define MENU_ENT_L(k, t, l) { .title = t, .key = k, .normal = 1, .label = l } |
161b0d1a KZ |
82 | |
83 | #define MENU_XENT(k, t) { .title = t, .key = k, .expert = 1 } | |
84 | #define MENU_XENT_H(k, t) { .title = t, .key = k, .expert = 1, .hidden = 1 } | |
85 | ||
86 | #define MENU_BENT(k, t) { .title = t, .key = k, .expert = 1, .normal = 1 } | |
27ddd4f1 | 87 | #define MENU_BENT_E(k, t, l) { .title = t, .key = k, .expert = 1, .normal = 1, .exclude = l } |
161b0d1a | 88 | |
f92e1810 | 89 | #define MENU_ENT_NEST(k, t, l, p) { .title = t, .key = k, .normal = 1, .label = l, .parent = p } |
5c1c594e | 90 | #define MENU_BENT_NEST_H(k, t, l, p) { .title = t, .key = k, .expert = 1, .normal = 1, .label = l, .parent = p, .hidden = 1 } |
161b0d1a KZ |
91 | |
92 | /* Generic menu */ | |
2ba641e5 | 93 | static const struct menu menu_generic = { |
89309968 | 94 | .callback = generic_menu_cb, |
161b0d1a | 95 | .entries = { |
6ae5e1e0 | 96 | MENU_BSEP(N_("Generic")), |
161b0d1a | 97 | MENU_ENT ('d', N_("delete a partition")), |
0efd951c | 98 | MENU_ENT ('F', N_("list free unpartitioned space")), |
161b0d1a KZ |
99 | MENU_ENT ('l', N_("list known partition types")), |
100 | MENU_ENT ('n', N_("add a new partition")), | |
101 | MENU_BENT ('p', N_("print the partition table")), | |
6ae5e1e0 | 102 | MENU_ENT ('t', N_("change a partition type")), |
27ddd4f1 | 103 | MENU_BENT_E('v', N_("verify the partition table"), FDISK_DISKLABEL_BSD), |
3e3b51b3 | 104 | MENU_ENT ('i', N_("print information about a partition")), |
161b0d1a | 105 | |
e916600f KZ |
106 | MENU_XENT('d', N_("print the raw data of the first sector from the device")), |
107 | MENU_XENT('D', N_("print the raw data of the disklabel from the device")), | |
dd7ba604 | 108 | MENU_XENT('f', N_("fix partitions order")), |
6ae5e1e0 | 109 | |
161b0d1a KZ |
110 | MENU_SEP(N_("Misc")), |
111 | MENU_BENT ('m', N_("print this menu")), | |
112 | MENU_ENT_E('u', N_("change display/entry units"), FDISK_DISKLABEL_GPT), | |
22ddf547 | 113 | MENU_ENT_E('x', N_("extra functionality (experts only)"), FDISK_DISKLABEL_BSD), |
161b0d1a | 114 | |
a30e4ef4 | 115 | MENU_SEP(N_("Script")), |
5cb9918d KZ |
116 | MENU_ENT ('I', N_("load disk layout from sfdisk script file")), |
117 | MENU_ENT ('O', N_("dump disk layout to sfdisk script file")), | |
a30e4ef4 | 118 | |
2a1a67df | 119 | MENU_BSEP(N_("Save & Exit")), |
22ddf547 KZ |
120 | MENU_ENT_E('w', N_("write table to disk and exit"), FDISK_DISKLABEL_BSD), |
121 | MENU_ENT_L('w', N_("write table to disk"), FDISK_DISKLABEL_BSD), | |
161b0d1a KZ |
122 | MENU_BENT ('q', N_("quit without saving changes")), |
123 | MENU_XENT ('r', N_("return to main menu")), | |
f92e1810 | 124 | |
3e8228b2 | 125 | MENU_ENT_NEST('r', N_("return from BSD to DOS (MBR)"), FDISK_DISKLABEL_BSD, FDISK_DISKLABEL_DOS), |
161b0d1a | 126 | |
5c1c594e KZ |
127 | MENU_ENT_NEST('r', N_("return from protective/hybrid MBR to GPT"), FDISK_DISKLABEL_DOS, FDISK_DISKLABEL_GPT), |
128 | ||
161b0d1a KZ |
129 | { 0, NULL } |
130 | } | |
131 | }; | |
132 | ||
2ba641e5 | 133 | static const struct menu menu_createlabel = { |
b3ac22ef | 134 | .callback = createlabel_menu_cb, |
22ddf547 | 135 | .exclude = FDISK_DISKLABEL_BSD, |
f92e1810 | 136 | .nonested = 1, |
161b0d1a KZ |
137 | .entries = { |
138 | MENU_SEP(N_("Create a new label")), | |
139 | MENU_ENT('g', N_("create a new empty GPT partition table")), | |
140 | MENU_ENT('G', N_("create a new empty SGI (IRIX) partition table")), | |
3e8228b2 | 141 | MENU_ENT('o', N_("create a new empty MBR (DOS) partition table")), |
161b0d1a KZ |
142 | MENU_ENT('s', N_("create a new empty Sun partition table")), |
143 | ||
144 | /* backward compatibility -- be sensitive to 'g', but don't | |
145 | * print it in the expert menu */ | |
146 | MENU_XENT_H('g', N_("create an IRIX (SGI) partition table")), | |
147 | { 0, NULL } | |
148 | } | |
149 | }; | |
150 | ||
2ba641e5 | 151 | static const struct menu menu_geo = { |
8e40a677 | 152 | .callback = geo_menu_cb, |
22ddf547 | 153 | .exclude = FDISK_DISKLABEL_GPT | FDISK_DISKLABEL_BSD, |
be9ba6f3 | 154 | .entries = { |
2dd2880f | 155 | MENU_XSEP(N_("Geometry (for the current label)")), |
be9ba6f3 KZ |
156 | MENU_XENT('c', N_("change number of cylinders")), |
157 | MENU_XENT('h', N_("change number of heads")), | |
158 | MENU_XENT('s', N_("change number of sectors/track")), | |
159 | { 0, NULL } | |
160 | } | |
161 | }; | |
162 | ||
2ba641e5 | 163 | static const struct menu menu_gpt = { |
b9e94cd7 | 164 | .callback = gpt_menu_cb, |
161b0d1a KZ |
165 | .label = FDISK_DISKLABEL_GPT, |
166 | .entries = { | |
c48acf70 | 167 | MENU_BSEP(N_("GPT")), |
35b1f0a4 | 168 | MENU_XENT('i', N_("change disk GUID")), |
161b0d1a | 169 | MENU_XENT('n', N_("change partition name")), |
35b1f0a4 | 170 | MENU_XENT('u', N_("change partition UUID")), |
304cf949 | 171 | MENU_XENT('l', N_("change table length")), |
c48acf70 | 172 | MENU_BENT('M', N_("enter protective/hybrid MBR")), |
f362d863 KZ |
173 | |
174 | MENU_XSEP(""), | |
175 | MENU_XENT('A', N_("toggle the legacy BIOS bootable flag")), | |
176 | MENU_XENT('B', N_("toggle the no block IO protocol flag")), | |
177 | MENU_XENT('R', N_("toggle the required partition flag")), | |
178 | MENU_XENT('S', N_("toggle the GUID specific bits")), | |
179 | ||
161b0d1a KZ |
180 | { 0, NULL } |
181 | } | |
182 | }; | |
183 | ||
2ba641e5 | 184 | static const struct menu menu_sun = { |
9f280903 | 185 | .callback = sun_menu_cb, |
2a1a67df KZ |
186 | .label = FDISK_DISKLABEL_SUN, |
187 | .entries = { | |
188 | MENU_BSEP(N_("Sun")), | |
aaa43826 | 189 | MENU_ENT('a', N_("toggle the read-only flag")), |
2a1a67df KZ |
190 | MENU_ENT('c', N_("toggle the mountable flag")), |
191 | ||
192 | MENU_XENT('a', N_("change number of alternate cylinders")), | |
2a1a67df | 193 | MENU_XENT('e', N_("change number of extra sectors per cylinder")), |
2a1a67df KZ |
194 | MENU_XENT('i', N_("change interleave factor")), |
195 | MENU_XENT('o', N_("change rotation speed (rpm)")), | |
2a1a67df KZ |
196 | MENU_XENT('y', N_("change number of physical cylinders")), |
197 | { 0, NULL } | |
198 | } | |
199 | }; | |
200 | ||
2ba641e5 | 201 | static const struct menu menu_sgi = { |
aae727f2 | 202 | .callback = sgi_menu_cb, |
6ae5e1e0 KZ |
203 | .label = FDISK_DISKLABEL_SGI, |
204 | .entries = { | |
205 | MENU_SEP(N_("SGI")), | |
206 | MENU_ENT('a', N_("select bootable partition")), | |
207 | MENU_ENT('b', N_("edit bootfile entry")), | |
208 | MENU_ENT('c', N_("select sgi swap partition")), | |
aae727f2 | 209 | MENU_ENT('i', N_("create SGI info")), |
6ae5e1e0 KZ |
210 | { 0, NULL } |
211 | } | |
212 | }; | |
213 | ||
2ba641e5 | 214 | static const struct menu menu_dos = { |
f02fecd1 | 215 | .callback = dos_menu_cb, |
6ae5e1e0 KZ |
216 | .label = FDISK_DISKLABEL_DOS, |
217 | .entries = { | |
218 | MENU_BSEP(N_("DOS (MBR)")), | |
219 | MENU_ENT('a', N_("toggle a bootable flag")), | |
220 | MENU_ENT('b', N_("edit nested BSD disklabel")), | |
221 | MENU_ENT('c', N_("toggle the dos compatibility flag")), | |
222 | ||
223 | MENU_XENT('b', N_("move beginning of data in a partition")), | |
973c3fc7 | 224 | MENU_XENT('F', N_("fix partitions C/H/S values")), |
6ae5e1e0 | 225 | MENU_XENT('i', N_("change the disk identifier")), |
f92e1810 | 226 | |
5c1c594e KZ |
227 | MENU_BENT_NEST_H('M', N_("return from protective/hybrid MBR to GPT"), FDISK_DISKLABEL_DOS, FDISK_DISKLABEL_GPT), |
228 | ||
6ae5e1e0 KZ |
229 | { 0, NULL } |
230 | } | |
231 | }; | |
232 | ||
2ba641e5 | 233 | static const struct menu menu_bsd = { |
b529ea2a | 234 | .callback = bsd_menu_cb, |
22ddf547 | 235 | .label = FDISK_DISKLABEL_BSD, |
6ae5e1e0 KZ |
236 | .entries = { |
237 | MENU_SEP(N_("BSD")), | |
238 | MENU_ENT('e', N_("edit drive data")), | |
239 | MENU_ENT('i', N_("install bootstrap")), | |
240 | MENU_ENT('s', N_("show complete disklabel")), | |
6ae5e1e0 | 241 | MENU_ENT('x', N_("link BSD partition to non-BSD partition")), |
6ae5e1e0 KZ |
242 | { 0, NULL } |
243 | } | |
244 | }; | |
245 | ||
161b0d1a | 246 | static const struct menu *menus[] = { |
6ae5e1e0 KZ |
247 | &menu_gpt, |
248 | &menu_sun, | |
249 | &menu_sgi, | |
250 | &menu_dos, | |
251 | &menu_bsd, | |
be9ba6f3 | 252 | &menu_geo, |
161b0d1a KZ |
253 | &menu_generic, |
254 | &menu_createlabel, | |
161b0d1a KZ |
255 | }; |
256 | ||
257 | static const struct menu_entry *next_menu_entry( | |
258 | struct fdisk_context *cxt, | |
259 | struct menu_context *mc) | |
260 | { | |
8eccde20 KZ |
261 | struct fdisk_label *lb = fdisk_get_label(cxt, NULL); |
262 | struct fdisk_context *parent = fdisk_get_parent(cxt); | |
263 | unsigned int type = 0, pr_type = 0; | |
264 | ||
265 | assert(cxt); | |
266 | ||
68ddb136 KZ |
267 | if (lb) |
268 | type = fdisk_label_get_type(lb); | |
8eccde20 KZ |
269 | if (parent) |
270 | pr_type = fdisk_label_get_type(fdisk_get_label(parent, NULL)); | |
271 | ||
161b0d1a KZ |
272 | while (mc->menu_idx < ARRAY_SIZE(menus)) { |
273 | const struct menu *m = menus[mc->menu_idx]; | |
274 | const struct menu_entry *e = &(m->entries[mc->entry_idx]); | |
275 | ||
f92e1810 KZ |
276 | /* |
277 | * whole-menu filter | |
278 | */ | |
279 | ||
be9ba6f3 | 280 | /* no more entries */ |
161b0d1a | 281 | if (e->title == NULL || |
be9ba6f3 | 282 | /* menu wanted for specified labels only */ |
53a80856 | 283 | (m->label && (!lb || !(m->label & type))) || |
f92e1810 | 284 | /* unwanted for nested PT */ |
8eccde20 | 285 | (m->nonested && parent) || |
be9ba6f3 | 286 | /* menu excluded for specified labels */ |
8eccde20 | 287 | (m->exclude && lb && (m->exclude & type))) { |
161b0d1a KZ |
288 | mc->menu_idx++; |
289 | mc->entry_idx = 0; | |
290 | continue; | |
291 | } | |
292 | ||
f92e1810 KZ |
293 | /* |
294 | * per entry filter | |
295 | */ | |
296 | ||
7e937b77 | 297 | /* excluded for the current label */ |
8eccde20 | 298 | if ((e->exclude && lb && e->exclude & type) || |
7e937b77 | 299 | /* entry wanted for specified labels only */ |
53a80856 | 300 | (e->label && (!lb || !(e->label & type))) || |
161b0d1a | 301 | /* exclude non-expert entries in expect mode */ |
6a632136 | 302 | (e->expert == 0 && fdisk_is_details(cxt)) || |
f92e1810 | 303 | /* nested only */ |
8eccde20 | 304 | (e->parent && (!parent || pr_type != e->parent)) || |
161b0d1a | 305 | /* exclude non-normal entries in normal mode */ |
6a632136 | 306 | (e->normal == 0 && !fdisk_is_details(cxt))) { |
161b0d1a KZ |
307 | mc->entry_idx++; |
308 | continue; | |
309 | } | |
310 | mc->entry_idx++; | |
311 | return e; | |
312 | ||
313 | } | |
314 | return NULL; | |
315 | } | |
316 | ||
3b4f9f16 KZ |
317 | /* returns @menu and menu entry for then @key */ |
318 | static const struct menu_entry *get_fdisk_menu_entry( | |
319 | struct fdisk_context *cxt, | |
320 | int key, | |
321 | const struct menu **menu) | |
322 | { | |
323 | struct menu_context mc = MENU_CXT_EMPTY; | |
324 | const struct menu_entry *e; | |
325 | ||
326 | while ((e = next_menu_entry(cxt, &mc))) { | |
327 | if (IS_MENU_SEP(e) || e->key != key) | |
328 | continue; | |
329 | ||
330 | if (menu) | |
331 | *menu = menus[mc.menu_idx]; | |
332 | return e; | |
333 | } | |
334 | ||
335 | return NULL; | |
336 | } | |
337 | ||
338 | static int menu_detect_collisions(struct fdisk_context *cxt) | |
339 | { | |
340 | struct menu_context mc = MENU_CXT_EMPTY; | |
341 | const struct menu_entry *e, *r; | |
342 | ||
343 | while ((e = next_menu_entry(cxt, &mc))) { | |
344 | if (IS_MENU_SEP(e)) | |
345 | continue; | |
346 | ||
347 | r = get_fdisk_menu_entry(cxt, e->key, NULL); | |
348 | if (!r) { | |
e6d0c4c1 | 349 | DBG(MENU, ul_debug("warning: not found " |
3b4f9f16 KZ |
350 | "entry for %c", e->key)); |
351 | return -1; | |
352 | } | |
353 | if (r != e) { | |
e6d0c4c1 | 354 | DBG(MENU, ul_debug("warning: duplicate key '%c'", |
3b4f9f16 | 355 | e->key)); |
e6d0c4c1 KZ |
356 | DBG(MENU, ul_debug(" : %s", e->title)); |
357 | DBG(MENU, ul_debug(" : %s", r->title)); | |
3b4f9f16 KZ |
358 | abort(); |
359 | } | |
360 | } | |
361 | ||
362 | return 0; | |
363 | } | |
364 | ||
1f75d1ce | 365 | static int print_fdisk_menu(struct fdisk_context *cxt) |
161b0d1a KZ |
366 | { |
367 | struct menu_context mc = MENU_CXT_EMPTY; | |
368 | const struct menu_entry *e; | |
369 | ||
e6d0c4c1 | 370 | ON_DBG(MENU, menu_detect_collisions(cxt)); |
3b4f9f16 | 371 | |
6a632136 | 372 | if (fdisk_is_details(cxt)) |
39f01b7b | 373 | printf(_("\nHelp (expert commands):\n")); |
161b0d1a | 374 | else |
39f01b7b | 375 | printf(_("\nHelp:\n")); |
161b0d1a KZ |
376 | |
377 | while ((e = next_menu_entry(cxt, &mc))) { | |
378 | if (IS_MENU_HID(e)) | |
379 | continue; /* hidden entry */ | |
f362d863 KZ |
380 | if (IS_MENU_SEP(e) && (!e->title || !*e->title)) |
381 | printf("\n"); | |
382 | else if (IS_MENU_SEP(e)) { | |
496c979a | 383 | color_scheme_enable("help-title", UL_COLOR_BOLD); |
161b0d1a | 384 | printf("\n %s\n", _(e->title)); |
80a1712f KZ |
385 | color_disable(); |
386 | } else | |
161b0d1a KZ |
387 | printf(" %c %s\n", e->key, _(e->title)); |
388 | } | |
389 | fputc('\n', stdout); | |
390 | ||
8eccde20 KZ |
391 | if (fdisk_get_parent(cxt)) { |
392 | struct fdisk_label *l = fdisk_get_label(cxt, NULL), | |
393 | *p = fdisk_get_label(fdisk_get_parent(cxt), NULL); | |
394 | ||
f92e1810 KZ |
395 | fdisk_info(cxt, _("You're editing nested '%s' partition table, " |
396 | "primary partition table is '%s'."), | |
8eccde20 KZ |
397 | fdisk_label_get_name(l), |
398 | fdisk_label_get_name(p)); | |
399 | } | |
f92e1810 | 400 | |
161b0d1a KZ |
401 | return 0; |
402 | } | |
403 | ||
a410f8df KZ |
404 | /* Asks for command, verify the key and perform the command or |
405 | * returns the command key if no callback for the command is | |
406 | * implemented. | |
407 | * | |
a47fec81 KZ |
408 | * Note that this function might exchange the context pointer to |
409 | * switch to another (nested) context. | |
410 | * | |
a410f8df KZ |
411 | * Returns: <0 on error |
412 | * 0 on success (the command performed) | |
413 | * >0 if no callback (then returns the key) | |
414 | */ | |
a47fec81 | 415 | int process_fdisk_menu(struct fdisk_context **cxt0) |
a410f8df | 416 | { |
a47fec81 | 417 | struct fdisk_context *cxt = *cxt0; |
a410f8df KZ |
418 | const struct menu_entry *ent; |
419 | const struct menu *menu; | |
420 | int key, rc; | |
421 | const char *prompt; | |
89770db4 | 422 | char buf[BUFSIZ] = { '\0' }; |
a410f8df | 423 | |
6a632136 | 424 | if (fdisk_is_details(cxt)) |
a410f8df KZ |
425 | prompt = _("Expert command (m for help): "); |
426 | else | |
427 | prompt = _("Command (m for help): "); | |
428 | ||
429 | fputc('\n',stdout); | |
c1154128 KZ |
430 | rc = get_user_reply(prompt, buf, sizeof(buf)); |
431 | ||
432 | if (rc == -ECANCELED) { | |
433 | /* Map ^C and ^D in main menu to 'q' */ | |
434 | if (is_interactive | |
435 | && fdisk_label_is_changed(fdisk_get_label(cxt, NULL))) { | |
436 | rc = get_user_reply( | |
fff85eb5 | 437 | _("\nAll unwritten changes will be lost, do you really want to quit? "), |
c1154128 KZ |
438 | buf, sizeof(buf)); |
439 | if (rc || !rpmatch(buf)) | |
440 | return 0; | |
441 | } | |
442 | key = 'q'; | |
443 | } else if (rc) { | |
a410f8df | 444 | return rc; |
c1154128 KZ |
445 | } else |
446 | key = buf[0]; | |
a410f8df | 447 | |
a410f8df KZ |
448 | ent = get_fdisk_menu_entry(cxt, key, &menu); |
449 | if (!ent) { | |
450 | fdisk_warnx(cxt, _("%c: unknown command"), key); | |
451 | return -EINVAL; | |
452 | } | |
453 | ||
e6d0c4c1 | 454 | DBG(MENU, ul_debug("selected: key=%c, entry='%s'", |
a410f8df | 455 | key, ent->title)); |
a410f8df KZ |
456 | |
457 | /* menu has implemented callback, use it */ | |
27ddd4f1 | 458 | if (menu->callback) |
a47fec81 KZ |
459 | rc = menu->callback(cxt0, menu, ent); |
460 | else { | |
e6d0c4c1 | 461 | DBG(MENU, ul_debug("no callback for key '%c'", key)); |
27ddd4f1 | 462 | rc = -EINVAL; |
a47fec81 | 463 | } |
a410f8df | 464 | |
e6d0c4c1 | 465 | DBG(MENU, ul_debug("process menu done [rc=%d]", rc)); |
a47fec81 | 466 | return rc; |
a410f8df KZ |
467 | } |
468 | ||
a30e4ef4 KZ |
469 | static int script_read(struct fdisk_context *cxt) |
470 | { | |
471 | struct fdisk_script *sc = NULL; | |
472 | char *filename = NULL; | |
473 | int rc; | |
474 | ||
475 | rc = fdisk_ask_string(cxt, _("Enter script file name"), &filename); | |
476 | if (rc) | |
477 | return rc; | |
478 | ||
479 | errno = 0; | |
480 | sc = fdisk_new_script_from_file(cxt, filename); | |
481 | if (!sc && errno) | |
54fefa07 | 482 | fdisk_warn(cxt, _("Cannot open %s"), filename); |
a30e4ef4 KZ |
483 | else if (!sc) |
484 | fdisk_warnx(cxt, _("Failed to parse script file %s"), filename); | |
dda4743b | 485 | else if (fdisk_apply_script(cxt, sc) != 0) { |
a30e4ef4 | 486 | fdisk_warnx(cxt, _("Failed to apply script %s"), filename); |
dda4743b KZ |
487 | fdisk_warnx(cxt, _("Resetting fdisk!")); |
488 | rc = fdisk_reassign_device(cxt); | |
489 | if (rc == 0 && !fdisk_has_label(cxt)) { | |
490 | fdisk_info(cxt, _("Device does not contain a recognized partition table.")); | |
134b6296 | 491 | rc = fdisk_create_disklabel(cxt, NULL); |
dda4743b KZ |
492 | } |
493 | } else | |
a30e4ef4 KZ |
494 | fdisk_info(cxt, _("Script successfully applied.")); |
495 | ||
496 | fdisk_unref_script(sc); | |
497 | free(filename); | |
498 | return rc; | |
499 | } | |
500 | ||
501 | static int script_write(struct fdisk_context *cxt) | |
502 | { | |
503 | struct fdisk_script *sc = NULL; | |
504 | char *filename = NULL; | |
505 | FILE *f = NULL; | |
506 | int rc; | |
507 | ||
508 | rc = fdisk_ask_string(cxt, _("Enter script file name"), &filename); | |
509 | if (rc) | |
510 | return rc; | |
511 | ||
512 | sc = fdisk_new_script(cxt); | |
513 | if (!sc) { | |
514 | fdisk_warn(cxt, _("Failed to allocate script handler")); | |
515 | goto done; | |
516 | } | |
517 | ||
518 | rc = fdisk_script_read_context(sc, NULL); | |
519 | if (rc) { | |
54fefa07 | 520 | fdisk_warnx(cxt, _("Failed to transform disk layout into script")); |
a30e4ef4 KZ |
521 | goto done; |
522 | } | |
523 | ||
524 | f = fopen(filename, "w"); | |
525 | if (!f) { | |
54fefa07 | 526 | fdisk_warn(cxt, _("Cannot open %s"), filename); |
a30e4ef4 KZ |
527 | goto done; |
528 | } | |
529 | ||
530 | rc = fdisk_script_write_file(sc, f); | |
531 | if (rc) | |
532 | fdisk_warn(cxt, _("Failed to write script %s"), filename); | |
533 | else | |
534 | fdisk_info(cxt, _("Script successfully saved.")); | |
535 | done: | |
536 | if (f) | |
537 | fclose(f); | |
538 | fdisk_unref_script(sc); | |
539 | free(filename); | |
540 | return rc; | |
541 | } | |
542 | ||
ba465623 KZ |
543 | static int ask_for_wipe(struct fdisk_context *cxt, size_t partno) |
544 | { | |
545 | struct fdisk_partition *tmp = NULL; | |
546 | char *fstype = NULL; | |
547 | int rc, yes = 0; | |
548 | ||
549 | rc = fdisk_get_partition(cxt, partno, &tmp); | |
550 | if (rc) | |
551 | goto done; | |
552 | ||
553 | rc = fdisk_partition_to_string(tmp, cxt, FDISK_FIELD_FSTYPE, &fstype); | |
554 | if (rc || fstype == NULL) | |
555 | goto done; | |
556 | ||
557 | fdisk_warnx(cxt, _("Partition #%zu contains a %s signature."), partno + 1, fstype); | |
558 | ||
559 | if (pwipemode == WIPEMODE_AUTO && isatty(STDIN_FILENO)) | |
560 | fdisk_ask_yesno(cxt, _("Do you want to remove the signature?"), &yes); | |
561 | else if (pwipemode == WIPEMODE_ALWAYS) | |
562 | yes = 1; | |
563 | ||
564 | if (yes) { | |
565 | fdisk_info(cxt, _("The signature will be removed by a write command.")); | |
566 | rc = fdisk_wipe_partition(cxt, partno, TRUE); | |
567 | } | |
568 | done: | |
569 | fdisk_unref_partition(tmp); | |
570 | free(fstype); | |
571 | return rc; | |
572 | } | |
573 | ||
89309968 KZ |
574 | /* |
575 | * Basic fdisk actions | |
576 | */ | |
577 | static int generic_menu_cb(struct fdisk_context **cxt0, | |
578 | const struct menu *menu __attribute__((__unused__)), | |
579 | const struct menu_entry *ent) | |
580 | { | |
581 | struct fdisk_context *cxt = *cxt0; | |
582 | int rc = 0; | |
27ddd4f1 | 583 | size_t n; |
89309968 | 584 | |
27ddd4f1 | 585 | /* actions shared between expert and normal mode */ |
89309968 | 586 | switch (ent->key) { |
89309968 KZ |
587 | case 'p': |
588 | list_disk_geometry(cxt); | |
9f670072 | 589 | list_disklabel(cxt); |
89309968 | 590 | break; |
df4bfa97 | 591 | case 'w': |
6a632136 | 592 | if (fdisk_is_readonly(cxt)) { |
62eea9ce | 593 | fdisk_warnx(cxt, _("Device is open in read-only mode.")); |
e146ae4e KZ |
594 | break; |
595 | } | |
df4bfa97 KZ |
596 | rc = fdisk_write_disklabel(cxt); |
597 | if (rc) | |
fd56121a | 598 | err(EXIT_FAILURE, _("failed to write disklabel")); |
1d844703 KZ |
599 | |
600 | fdisk_info(cxt, _("The partition table has been altered.")); | |
8eccde20 | 601 | if (fdisk_get_parent(cxt)) |
df4bfa97 | 602 | break; /* nested PT, don't leave */ |
fadd8e08 KZ |
603 | |
604 | if (device_is_used) | |
605 | rc = fdisk_reread_changes(cxt, original_layout); | |
606 | else | |
607 | rc = fdisk_reread_partition_table(cxt); | |
a57639e1 | 608 | if (!rc) |
6a632136 | 609 | rc = fdisk_deassign_device(cxt, 0); |
a57639e1 | 610 | /* fallthrough */ |
89309968 | 611 | case 'q': |
c7119037 | 612 | fdisk_unref_context(cxt); |
a57639e1 KZ |
613 | fputc('\n', stdout); |
614 | exit(rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE); | |
27ddd4f1 KZ |
615 | case 'm': |
616 | rc = print_fdisk_menu(cxt); | |
617 | break; | |
89309968 KZ |
618 | case 'v': |
619 | rc = fdisk_verify_disklabel(cxt); | |
620 | break; | |
3e3b51b3 JLB |
621 | case 'i': |
622 | rc = print_partition_info(cxt); | |
623 | break; | |
0efd951c KZ |
624 | case 'F': |
625 | list_freespace(cxt); | |
626 | break; | |
27ddd4f1 KZ |
627 | } |
628 | ||
629 | /* expert mode */ | |
630 | if (ent->expert) { | |
631 | switch (ent->key) { | |
632 | case 'd': | |
e916600f KZ |
633 | dump_firstsector(cxt); |
634 | break; | |
635 | case 'D': | |
636 | dump_disklabel(cxt); | |
27ddd4f1 | 637 | break; |
dd7ba604 KZ |
638 | case 'f': |
639 | rc = fdisk_reorder_partitions(cxt); | |
640 | break; | |
27ddd4f1 | 641 | case 'r': |
6a632136 | 642 | rc = fdisk_enable_details(cxt, 0); |
27ddd4f1 KZ |
643 | break; |
644 | } | |
645 | return rc; | |
646 | } | |
647 | ||
648 | /* normal mode */ | |
649 | switch (ent->key) { | |
650 | case 'd': | |
651 | rc = fdisk_ask_partnum(cxt, &n, FALSE); | |
dbcd1a49 KZ |
652 | if (rc) |
653 | break; /* no partitions yet (or ENOMEM, ...) */ | |
654 | ||
655 | rc = fdisk_delete_partition(cxt, n); | |
27ddd4f1 | 656 | if (rc) |
48d4d931 | 657 | fdisk_warnx(cxt, _("Could not delete partition %zu"), n + 1); |
27ddd4f1 | 658 | else |
48d4d931 | 659 | fdisk_info(cxt, _("Partition %zu has been deleted."), n + 1); |
27ddd4f1 | 660 | break; |
a30e4ef4 KZ |
661 | case 'I': |
662 | script_read(cxt); | |
663 | break; | |
664 | case 'O': | |
665 | script_write(cxt); | |
666 | break; | |
27ddd4f1 KZ |
667 | case 'l': |
668 | list_partition_types(cxt); | |
669 | break; | |
670 | case 'n': | |
ba465623 KZ |
671 | { |
672 | size_t partno; | |
673 | rc = fdisk_add_partition(cxt, NULL, &partno); | |
674 | if (!rc) | |
675 | rc = ask_for_wipe(cxt, partno); | |
27ddd4f1 | 676 | break; |
ba465623 | 677 | } |
27ddd4f1 KZ |
678 | case 't': |
679 | change_partition_type(cxt); | |
680 | break; | |
681 | case 'u': | |
6a632136 KZ |
682 | fdisk_set_unit(cxt, |
683 | fdisk_use_cylinders(cxt) ? "sectors" : | |
27ddd4f1 | 684 | "cylinders"); |
6a632136 | 685 | if (fdisk_use_cylinders(cxt)) |
27ddd4f1 KZ |
686 | fdisk_info(cxt, _("Changing display/entry units to cylinders (DEPRECATED!).")); |
687 | else | |
688 | fdisk_info(cxt, _("Changing display/entry units to sectors.")); | |
689 | break; | |
690 | case 'x': | |
6a632136 | 691 | fdisk_enable_details(cxt, 1); |
27ddd4f1 KZ |
692 | break; |
693 | case 'r': | |
5c1c594e | 694 | /* return from nested BSD to DOS or MBR to GPT */ |
8eccde20 KZ |
695 | if (fdisk_get_parent(cxt)) { |
696 | *cxt0 = fdisk_get_parent(cxt); | |
27ddd4f1 | 697 | |
fd56121a | 698 | fdisk_info(cxt, _("Leaving nested disklabel.")); |
c7119037 | 699 | fdisk_unref_context(cxt); |
27ddd4f1 KZ |
700 | } |
701 | break; | |
89309968 | 702 | } |
27ddd4f1 | 703 | |
89309968 KZ |
704 | return rc; |
705 | } | |
706 | ||
8e40a677 | 707 | |
b9e94cd7 KZ |
708 | /* |
709 | * This is fdisk frontend for GPT specific libfdisk functions that | |
9e930041 | 710 | * are not exported by generic libfdisk API. |
b9e94cd7 | 711 | */ |
a47fec81 | 712 | static int gpt_menu_cb(struct fdisk_context **cxt0, |
b9e94cd7 KZ |
713 | const struct menu *menu __attribute__((__unused__)), |
714 | const struct menu_entry *ent) | |
715 | { | |
a47fec81 | 716 | struct fdisk_context *cxt = *cxt0; |
f92e1810 | 717 | struct fdisk_context *mbr; |
6936c081 | 718 | struct fdisk_partition *pa = NULL; |
b9e94cd7 | 719 | size_t n; |
f92e1810 | 720 | int rc = 0; |
e4015b34 | 721 | uintmax_t length = 0; |
b9e94cd7 KZ |
722 | |
723 | assert(cxt); | |
724 | assert(ent); | |
aa36c2cf | 725 | assert(fdisk_is_label(cxt, GPT)); |
b9e94cd7 | 726 | |
e6d0c4c1 | 727 | DBG(MENU, ul_debug("enter GPT menu")); |
b9e94cd7 | 728 | |
f92e1810 KZ |
729 | if (ent->expert) { |
730 | switch (ent->key) { | |
731 | case 'i': | |
732 | return fdisk_set_disklabel_id(cxt); | |
304cf949 SP |
733 | case 'l': |
734 | rc = fdisk_ask_number(cxt, 1, fdisk_get_npartitions(cxt), | |
735 | ~(uint32_t)0, _("New maximum entries"), &length); | |
736 | if (rc) | |
737 | return rc; | |
e4015b34 | 738 | return fdisk_gpt_set_npartitions(cxt, (uint32_t) length); |
f92e1810 KZ |
739 | case 'M': |
740 | mbr = fdisk_new_nested_context(cxt, "dos"); | |
741 | if (!mbr) | |
742 | return -ENOMEM; | |
743 | *cxt0 = cxt = mbr; | |
c48acf70 KZ |
744 | if (fdisk_is_details(cxt)) |
745 | fdisk_enable_details(cxt, 1); /* keep us in expert mode */ | |
0477369a | 746 | fdisk_info(cxt, _("Entering protective/hybrid MBR disklabel.")); |
f92e1810 KZ |
747 | return 0; |
748 | } | |
35b1f0a4 | 749 | |
f92e1810 KZ |
750 | /* actions where is necessary partnum */ |
751 | rc = fdisk_ask_partnum(cxt, &n, FALSE); | |
752 | if (rc) | |
753 | return rc; | |
b9e94cd7 | 754 | |
f92e1810 KZ |
755 | switch(ent->key) { |
756 | case 'u': | |
6936c081 KZ |
757 | pa = fdisk_new_partition(); /* new template */ |
758 | if (!pa) | |
759 | rc = -ENOMEM; | |
760 | else { | |
761 | char *str = NULL; | |
762 | rc = fdisk_ask_string(cxt, _("New UUID (in 8-4-4-4-12 format)"), &str); | |
763 | if (!rc) | |
764 | rc = fdisk_partition_set_uuid(pa, str); | |
765 | if (!rc) | |
766 | rc = fdisk_set_partition(cxt, n, pa); | |
767 | free(str); | |
768 | fdisk_unref_partition(pa); | |
769 | } | |
f92e1810 KZ |
770 | break; |
771 | case 'n': | |
6936c081 KZ |
772 | pa = fdisk_new_partition(); /* new template */ |
773 | if (!pa) | |
774 | rc = -ENOMEM; | |
775 | else { | |
776 | char *str = NULL; | |
777 | rc = fdisk_ask_string(cxt, _("New name"), &str); | |
778 | if (!rc) | |
779 | rc = fdisk_partition_set_name(pa, str); | |
780 | if (!rc) | |
781 | rc = fdisk_set_partition(cxt, n, pa); | |
782 | free(str); | |
783 | fdisk_unref_partition(pa); | |
784 | } | |
f92e1810 | 785 | break; |
f362d863 | 786 | case 'A': |
a1ef792f | 787 | rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_LEGACYBOOT); |
f362d863 KZ |
788 | break; |
789 | case 'B': | |
a1ef792f | 790 | rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_NOBLOCK); |
f362d863 KZ |
791 | break; |
792 | case 'R': | |
a1ef792f | 793 | rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_REQUIRED); |
f362d863 KZ |
794 | break; |
795 | case 'S': | |
a1ef792f | 796 | rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_GUIDSPECIFIC); |
f362d863 | 797 | break; |
f92e1810 | 798 | } |
b9e94cd7 | 799 | } |
6936c081 | 800 | |
b9e94cd7 KZ |
801 | return rc; |
802 | } | |
803 | ||
f02fecd1 KZ |
804 | |
805 | /* | |
806 | * This is fdisk frontend for MBR specific libfdisk functions that | |
9e930041 | 807 | * are not exported by generic libfdisk API. |
f02fecd1 | 808 | */ |
a47fec81 | 809 | static int dos_menu_cb(struct fdisk_context **cxt0, |
f02fecd1 KZ |
810 | const struct menu *menu __attribute__((__unused__)), |
811 | const struct menu_entry *ent) | |
812 | { | |
a47fec81 | 813 | struct fdisk_context *cxt = *cxt0; |
f02fecd1 KZ |
814 | int rc = 0; |
815 | ||
e6d0c4c1 | 816 | DBG(MENU, ul_debug("enter DOS menu")); |
a47fec81 | 817 | |
f02fecd1 KZ |
818 | if (!ent->expert) { |
819 | switch (ent->key) { | |
820 | case 'a': | |
821 | { | |
822 | size_t n; | |
823 | rc = fdisk_ask_partnum(cxt, &n, FALSE); | |
824 | if (!rc) | |
a1ef792f | 825 | rc = fdisk_toggle_partition_flag(cxt, n, DOS_FLAG_ACTIVE); |
f02fecd1 KZ |
826 | break; |
827 | } | |
828 | case 'b': | |
829 | { | |
830 | struct fdisk_context *bsd | |
831 | = fdisk_new_nested_context(cxt, "bsd"); | |
818d7924 KZ |
832 | if (!bsd) |
833 | return -ENOMEM; | |
aa36c2cf | 834 | if (!fdisk_has_label(bsd)) |
818d7924 | 835 | rc = fdisk_create_disklabel(bsd, "bsd"); |
a47fec81 | 836 | if (rc) |
c7119037 | 837 | fdisk_unref_context(bsd); |
27ddd4f1 | 838 | else { |
a47fec81 | 839 | *cxt0 = cxt = bsd; |
0477369a | 840 | fdisk_info(cxt, _("Entering nested BSD disklabel.")); |
27ddd4f1 | 841 | } |
f02fecd1 KZ |
842 | break; |
843 | } | |
844 | case 'c': | |
845 | toggle_dos_compatibility_flag(cxt); | |
846 | break; | |
847 | } | |
848 | return rc; | |
849 | } | |
850 | ||
851 | /* expert mode */ | |
852 | switch (ent->key) { | |
853 | case 'b': | |
854 | { | |
855 | size_t n; | |
856 | rc = fdisk_ask_partnum(cxt, &n, FALSE); | |
857 | if (!rc) | |
f8ad3899 | 858 | rc = fdisk_dos_move_begin(cxt, n); |
f02fecd1 KZ |
859 | break; |
860 | } | |
f02fecd1 KZ |
861 | case 'i': |
862 | rc = fdisk_set_disklabel_id(cxt); | |
863 | break; | |
f92e1810 | 864 | case 'M': |
5c1c594e | 865 | /* return from nested MBR to GPT (backward compatibility only) */ |
8eccde20 KZ |
866 | if (fdisk_get_parent(cxt)) { |
867 | *cxt0 = fdisk_get_parent(cxt); | |
f92e1810 KZ |
868 | |
869 | fdisk_info(cxt, _("Leaving nested disklabel.")); | |
c7119037 | 870 | fdisk_unref_context(cxt); |
f92e1810 KZ |
871 | } |
872 | break; | |
973c3fc7 PR |
873 | case 'F': |
874 | rc = fdisk_dos_fix_chs(cxt); | |
875 | if (rc) | |
876 | fdisk_info(cxt, _("C/H/S values fixed.")); | |
877 | else | |
878 | fdisk_info(cxt, _("Nothing to do. C/H/S values are correct already.")); | |
879 | break; | |
f02fecd1 KZ |
880 | } |
881 | return rc; | |
882 | } | |
883 | ||
a47fec81 | 884 | static int sun_menu_cb(struct fdisk_context **cxt0, |
9f280903 KZ |
885 | const struct menu *menu __attribute__((__unused__)), |
886 | const struct menu_entry *ent) | |
887 | { | |
a47fec81 | 888 | struct fdisk_context *cxt = *cxt0; |
9f280903 KZ |
889 | int rc = 0; |
890 | ||
e6d0c4c1 | 891 | DBG(MENU, ul_debug("enter SUN menu")); |
a47fec81 | 892 | |
9f280903 KZ |
893 | assert(cxt); |
894 | assert(ent); | |
aa36c2cf | 895 | assert(fdisk_is_label(cxt, SUN)); |
9f280903 | 896 | |
e6d0c4c1 | 897 | DBG(MENU, ul_debug("enter SUN menu")); |
9f280903 KZ |
898 | |
899 | /* normal mode */ | |
900 | if (!ent->expert) { | |
901 | size_t n; | |
902 | ||
903 | rc = fdisk_ask_partnum(cxt, &n, FALSE); | |
904 | if (rc) | |
905 | return rc; | |
906 | switch (ent->key) { | |
907 | case 'a': | |
a1ef792f | 908 | rc = fdisk_toggle_partition_flag(cxt, n, SUN_FLAG_RONLY); |
9f280903 KZ |
909 | break; |
910 | case 'c': | |
a1ef792f | 911 | rc = fdisk_toggle_partition_flag(cxt, n, SUN_FLAG_UNMNT); |
9f280903 KZ |
912 | break; |
913 | } | |
914 | return rc; | |
915 | } | |
916 | ||
917 | /* expert mode */ | |
918 | switch (ent->key) { | |
919 | case 'a': | |
920 | rc = fdisk_sun_set_alt_cyl(cxt); | |
921 | break; | |
922 | case 'e': | |
923 | rc = fdisk_sun_set_xcyl(cxt); | |
924 | break; | |
925 | case 'i': | |
926 | rc = fdisk_sun_set_ilfact(cxt); | |
927 | break; | |
928 | case 'o': | |
929 | rc = fdisk_sun_set_rspeed(cxt); | |
930 | break; | |
931 | case 'y': | |
932 | rc = fdisk_sun_set_pcylcount(cxt); | |
933 | break; | |
934 | } | |
935 | return rc; | |
936 | } | |
937 | ||
aae727f2 KZ |
938 | static int sgi_menu_cb(struct fdisk_context **cxt0, |
939 | const struct menu *menu __attribute__((__unused__)), | |
940 | const struct menu_entry *ent) | |
941 | { | |
942 | struct fdisk_context *cxt = *cxt0; | |
943 | int rc = -EINVAL; | |
944 | size_t n = 0; | |
945 | ||
e6d0c4c1 | 946 | DBG(MENU, ul_debug("enter SGI menu")); |
aae727f2 KZ |
947 | |
948 | assert(cxt); | |
949 | assert(ent); | |
aa36c2cf | 950 | assert(fdisk_is_label(cxt, SGI)); |
aae727f2 KZ |
951 | |
952 | if (ent->expert) | |
953 | return rc; | |
954 | ||
955 | switch (ent->key) { | |
956 | case 'a': | |
957 | rc = fdisk_ask_partnum(cxt, &n, FALSE); | |
958 | if (!rc) | |
a1ef792f | 959 | rc = fdisk_toggle_partition_flag(cxt, n, SGI_FLAG_BOOT); |
aae727f2 KZ |
960 | break; |
961 | case 'b': | |
ac84272d | 962 | fdisk_sgi_set_bootfile(cxt); |
aae727f2 KZ |
963 | break; |
964 | case 'c': | |
965 | rc = fdisk_ask_partnum(cxt, &n, FALSE); | |
966 | if (!rc) | |
a1ef792f | 967 | rc = fdisk_toggle_partition_flag(cxt, n, SGI_FLAG_SWAP); |
aae727f2 KZ |
968 | break; |
969 | case 'i': | |
ac84272d | 970 | rc = fdisk_sgi_create_info(cxt); |
aae727f2 KZ |
971 | break; |
972 | } | |
973 | ||
974 | return rc; | |
975 | } | |
976 | ||
b529ea2a KZ |
977 | /* |
978 | * This is fdisk frontend for BSD specific libfdisk functions that | |
9e930041 | 979 | * are not exported by generic libfdisk API. |
b529ea2a KZ |
980 | */ |
981 | static int bsd_menu_cb(struct fdisk_context **cxt0, | |
982 | const struct menu *menu __attribute__((__unused__)), | |
983 | const struct menu_entry *ent) | |
984 | { | |
985 | struct fdisk_context *cxt = *cxt0; | |
e563f055 | 986 | int rc = 0, org; |
b529ea2a KZ |
987 | |
988 | assert(cxt); | |
989 | assert(ent); | |
aa36c2cf | 990 | assert(fdisk_is_label(cxt, BSD)); |
b529ea2a | 991 | |
e6d0c4c1 | 992 | DBG(MENU, ul_debug("enter BSD menu")); |
b529ea2a KZ |
993 | |
994 | switch(ent->key) { | |
995 | case 'e': | |
996 | rc = fdisk_bsd_edit_disklabel(cxt); | |
997 | break; | |
998 | case 'i': | |
999 | rc = fdisk_bsd_write_bootstrap(cxt); | |
1000 | break; | |
1001 | case 's': | |
6a632136 | 1002 | org = fdisk_is_details(cxt); |
e563f055 | 1003 | |
6a632136 | 1004 | fdisk_enable_details(cxt, 1); |
9f670072 | 1005 | list_disklabel(cxt); |
6a632136 | 1006 | fdisk_enable_details(cxt, org); |
b529ea2a KZ |
1007 | break; |
1008 | case 'x': | |
1009 | rc = fdisk_bsd_link_partition(cxt); | |
1010 | break; | |
1011 | } | |
1012 | return rc; | |
1013 | } | |
1014 | ||
2dd2880f KZ |
1015 | /* C/H/S commands |
1016 | * | |
73afd3f8 | 1017 | * The geometry setting from this dialog is not persistent and maybe reset by |
2dd2880f KZ |
1018 | * fdisk_reset_device_properties() (for example when you create a new disk |
1019 | * label). Note that on command line specified -C/-H/-S setting is persistent | |
1020 | * as it's based on fdisk_save_user_geometry(). | |
1021 | */ | |
a47fec81 | 1022 | static int geo_menu_cb(struct fdisk_context **cxt0, |
8e40a677 KZ |
1023 | const struct menu *menu __attribute__((__unused__)), |
1024 | const struct menu_entry *ent) | |
1025 | { | |
a47fec81 | 1026 | struct fdisk_context *cxt = *cxt0; |
2dd2880f | 1027 | struct fdisk_label *lb = fdisk_get_label(cxt, NULL); |
8e40a677 KZ |
1028 | int rc = -EINVAL; |
1029 | uintmax_t c = 0, h = 0, s = 0; | |
2dd2880f | 1030 | fdisk_sector_t mi, ma; |
8e40a677 | 1031 | |
e6d0c4c1 | 1032 | DBG(MENU, ul_debug("enter GEO menu")); |
a47fec81 | 1033 | |
8e40a677 KZ |
1034 | assert(cxt); |
1035 | assert(ent); | |
1036 | ||
2dd2880f KZ |
1037 | /* default */ |
1038 | if (!lb) | |
1039 | lb = fdisk_get_label(cxt, "dos"); | |
1040 | ||
8e40a677 KZ |
1041 | switch (ent->key) { |
1042 | case 'c': | |
2dd2880f KZ |
1043 | fdisk_label_get_geomrange_cylinders(lb, &mi, &ma); |
1044 | rc = fdisk_ask_number(cxt, mi, fdisk_get_geom_cylinders(cxt), | |
1045 | ma, _("Number of cylinders"), &c); | |
8e40a677 KZ |
1046 | break; |
1047 | case 'h': | |
2dd2880f KZ |
1048 | { |
1049 | unsigned int i, a; | |
1050 | fdisk_label_get_geomrange_heads(lb, &i, &a); | |
1051 | rc = fdisk_ask_number(cxt, i, fdisk_get_geom_heads(cxt), | |
1052 | a, _("Number of heads"), &h); | |
8e40a677 | 1053 | break; |
2dd2880f | 1054 | } |
8e40a677 | 1055 | case 's': |
2dd2880f KZ |
1056 | fdisk_label_get_geomrange_sectors(lb, &mi, &ma); |
1057 | rc = fdisk_ask_number(cxt, mi, fdisk_get_geom_sectors(cxt), | |
1058 | ma, _("Number of sectors"), &s); | |
8e40a677 KZ |
1059 | break; |
1060 | } | |
1061 | ||
1062 | if (!rc) | |
1063 | fdisk_override_geometry(cxt, c, h, s); | |
1064 | return rc; | |
1065 | } | |
1066 | ||
b3ac22ef KZ |
1067 | static int createlabel_menu_cb(struct fdisk_context **cxt0, |
1068 | const struct menu *menu __attribute__((__unused__)), | |
1069 | const struct menu_entry *ent) | |
1070 | { | |
1071 | struct fdisk_context *cxt = *cxt0; | |
134b6296 | 1072 | const char *wanted = NULL; |
b3ac22ef KZ |
1073 | int rc = -EINVAL; |
1074 | ||
e6d0c4c1 | 1075 | DBG(MENU, ul_debug("enter Create label menu")); |
b3ac22ef KZ |
1076 | |
1077 | assert(cxt); | |
1078 | assert(ent); | |
1079 | ||
aae727f2 KZ |
1080 | if (ent->expert) { |
1081 | switch (ent->key) { | |
1082 | case 'g': | |
1083 | /* Deprecated, use 'G' in main menu, just for backward | |
1084 | * compatibility only. */ | |
134b6296 | 1085 | wanted = "sgi"; |
aae727f2 KZ |
1086 | break; |
1087 | } | |
5635d195 KZ |
1088 | } else { |
1089 | switch (ent->key) { | |
1090 | case 'g': | |
134b6296 | 1091 | wanted = "gpt"; |
5635d195 KZ |
1092 | break; |
1093 | case 'G': | |
134b6296 | 1094 | wanted = "sgi"; |
5635d195 KZ |
1095 | break; |
1096 | case 'o': | |
134b6296 | 1097 | wanted = "dos"; |
5635d195 KZ |
1098 | break; |
1099 | case 's': | |
134b6296 | 1100 | wanted = "sun"; |
5635d195 KZ |
1101 | break; |
1102 | } | |
aae727f2 KZ |
1103 | } |
1104 | ||
134b6296 KZ |
1105 | if (wanted) { |
1106 | rc = fdisk_create_disklabel(cxt, wanted); | |
1107 | if (rc) { | |
1108 | errno = -rc; | |
1109 | fdisk_warn(cxt, _("Failed to create '%s' disk label"), wanted); | |
1110 | } | |
1111 | } | |
5635d195 KZ |
1112 | if (rc == 0 && fdisk_get_collision(cxt)) |
1113 | follow_wipe_mode(cxt); | |
1114 | ||
b3ac22ef KZ |
1115 | return rc; |
1116 | } |