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