label.c revision 1.10.2.9 1 1.10.2.9 snj /* $NetBSD: label.c,v 1.10.2.9 2022/12/31 05:03:14 snj Exp $ */
2 1.1 dholland
3 1.1 dholland /*
4 1.1 dholland * Copyright 1997 Jonathan Stone
5 1.1 dholland * All rights reserved.
6 1.1 dholland *
7 1.1 dholland * Redistribution and use in source and binary forms, with or without
8 1.1 dholland * modification, are permitted provided that the following conditions
9 1.1 dholland * are met:
10 1.1 dholland * 1. Redistributions of source code must retain the above copyright
11 1.1 dholland * notice, this list of conditions and the following disclaimer.
12 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 dholland * notice, this list of conditions and the following disclaimer in the
14 1.1 dholland * documentation and/or other materials provided with the distribution.
15 1.1 dholland * 3. All advertising materials mentioning features or use of this software
16 1.1 dholland * must display the following acknowledgement:
17 1.1 dholland * This product includes software developed for the NetBSD Project by
18 1.1 dholland * Jonathan Stone.
19 1.1 dholland * 4. The name of Jonathan Stone may not be used to endorse
20 1.1 dholland * or promote products derived from this software without specific prior
21 1.1 dholland * written permission.
22 1.1 dholland *
23 1.1 dholland * THIS SOFTWARE IS PROVIDED BY JONATHAN STONE ``AS IS''
24 1.1 dholland * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
27 1.1 dholland * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.1 dholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.1 dholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.1 dholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.1 dholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.1 dholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 1.1 dholland * THE POSSIBILITY OF SUCH DAMAGE.
34 1.1 dholland *
35 1.1 dholland */
36 1.1 dholland
37 1.1 dholland #include <sys/cdefs.h>
38 1.1 dholland #if defined(LIBC_SCCS) && !defined(lint)
39 1.10.2.9 snj __RCSID("$NetBSD: label.c,v 1.10.2.9 2022/12/31 05:03:14 snj Exp $");
40 1.1 dholland #endif
41 1.1 dholland
42 1.1 dholland #include <sys/types.h>
43 1.1 dholland #include <stddef.h>
44 1.7 martin #include <assert.h>
45 1.1 dholland #include <errno.h>
46 1.1 dholland #include <stdio.h>
47 1.1 dholland #include <fcntl.h>
48 1.1 dholland #include <util.h>
49 1.1 dholland #include <unistd.h>
50 1.1 dholland #include <sys/dkio.h>
51 1.1 dholland #include <sys/param.h>
52 1.7 martin #include <sys/bootblock.h>
53 1.10.2.6 bouyer #include <sys/bitops.h>
54 1.1 dholland #include <ufs/ffs/fs.h>
55 1.1 dholland
56 1.1 dholland #include "defs.h"
57 1.1 dholland #include "msg_defs.h"
58 1.1 dholland #include "menu_defs.h"
59 1.1 dholland
60 1.1 dholland /*
61 1.1 dholland * local prototypes
62 1.1 dholland */
63 1.7 martin static bool boringpart(const struct disk_part_info *info);
64 1.10.2.4 msaitoh static bool checklabel(struct disk_partitions*, char *, char *);
65 1.7 martin static void show_partition_adder(menudesc *, struct partition_usage_set*);
66 1.1 dholland
67 1.1 dholland /*
68 1.7 martin * Return 1 if a partition should be ignored when checking
69 1.1 dholland * for overlapping partitions.
70 1.1 dholland */
71 1.7 martin static bool
72 1.7 martin boringpart(const struct disk_part_info *info)
73 1.1 dholland {
74 1.1 dholland
75 1.7 martin if (info->size == 0)
76 1.7 martin return true;
77 1.7 martin if (info->flags &
78 1.7 martin (PTI_PSCHEME_INTERNAL|PTI_WHOLE_DISK|PTI_SEC_CONTAINER|
79 1.7 martin PTI_RAW_PART))
80 1.7 martin return true;
81 1.7 martin
82 1.7 martin return false;
83 1.1 dholland }
84 1.1 dholland
85 1.7 martin /*
86 1.7 martin * We have some partitions in our "wanted" list that we may not edit,
87 1.7 martin * like the RAW_PART in disklabel, some that just represent external
88 1.7 martin * mount entries for the final fstab or similar.
89 1.7 martin * We have previously sorted pset->parts and pset->infos to be in sync,
90 1.7 martin * but the former "array" may be shorter.
91 1.7 martin * Here are a few quick predicates to check for them.
92 1.7 martin */
93 1.7 martin static bool
94 1.7 martin real_partition(const struct partition_usage_set *pset, int index)
95 1.7 martin {
96 1.7 martin if (index < 0 || (size_t)index >= pset->num)
97 1.7 martin return false;
98 1.1 dholland
99 1.7 martin return pset->infos[index].cur_part_id != NO_PART;
100 1.7 martin }
101 1.1 dholland
102 1.1 dholland /*
103 1.7 martin * Check partitioning for overlapping partitions.
104 1.1 dholland * Returns 0 if no overlapping partition found, nonzero otherwise.
105 1.1 dholland * Sets reference arguments ovly1 and ovly2 to the indices of
106 1.1 dholland * overlapping partitions if any are found.
107 1.1 dholland */
108 1.7 martin static bool
109 1.7 martin checklabel(struct disk_partitions *parts,
110 1.10.2.4 msaitoh char *ovl1, char *ovl2)
111 1.7 martin {
112 1.7 martin part_id i, j;
113 1.7 martin struct disk_part_info info;
114 1.7 martin daddr_t istart, iend, jstart, jend;
115 1.7 martin unsigned int fs_type, fs_sub_type;
116 1.1 dholland
117 1.7 martin for (i = 0; i < parts->num_part - 1; i ++ ) {
118 1.7 martin if (!parts->pscheme->get_part_info(parts, i, &info))
119 1.7 martin continue;
120 1.1 dholland
121 1.1 dholland /* skip unused or reserved partitions */
122 1.7 martin if (boringpart(&info))
123 1.1 dholland continue;
124 1.1 dholland
125 1.1 dholland /*
126 1.1 dholland * check succeeding partitions for overlap.
127 1.10.2.4 msaitoh * O(n^2), but n is small.
128 1.1 dholland */
129 1.7 martin istart = info.start;
130 1.7 martin iend = istart + info.size;
131 1.7 martin fs_type = info.fs_type;
132 1.7 martin fs_sub_type = info.fs_sub_type;
133 1.1 dholland
134 1.7 martin for (j = i+1; j < parts->num_part; j++) {
135 1.7 martin
136 1.7 martin if (!parts->pscheme->get_part_info(parts, j, &info))
137 1.7 martin continue;
138 1.1 dholland
139 1.1 dholland /* skip unused or reserved partitions */
140 1.7 martin if (boringpart(&info))
141 1.1 dholland continue;
142 1.1 dholland
143 1.7 martin jstart = info.start;
144 1.7 martin jend = jstart + info.size;
145 1.1 dholland
146 1.1 dholland /* overlap? */
147 1.7 martin if ((istart <= jstart && jstart < iend) ||
148 1.7 martin (jstart <= istart && istart < jend)) {
149 1.10.2.4 msaitoh snprintf(ovl1, MENUSTRSIZE,
150 1.7 martin "%" PRIu64 " - %" PRIu64 " %s, %s",
151 1.7 martin istart / sizemult, iend / sizemult,
152 1.7 martin multname,
153 1.7 martin getfslabelname(fs_type, fs_sub_type));
154 1.10.2.4 msaitoh snprintf(ovl2, MENUSTRSIZE,
155 1.7 martin "%" PRIu64 " - %" PRIu64 " %s, %s",
156 1.7 martin jstart / sizemult, jend / sizemult,
157 1.7 martin multname,
158 1.7 martin getfslabelname(info.fs_type,
159 1.7 martin info.fs_sub_type));
160 1.7 martin return false;
161 1.1 dholland }
162 1.1 dholland }
163 1.1 dholland }
164 1.1 dholland
165 1.7 martin return true;
166 1.1 dholland }
167 1.1 dholland
168 1.2 martin int
169 1.7 martin checkoverlap(struct disk_partitions *parts)
170 1.2 martin {
171 1.7 martin char desc1[MENUSTRSIZE], desc2[MENUSTRSIZE];
172 1.7 martin if (!checklabel(parts, desc1, desc2)) {
173 1.7 martin msg_display_subst(MSG_partitions_overlap, 2, desc1, desc2);
174 1.2 martin return 1;
175 1.2 martin }
176 1.2 martin return 0;
177 1.2 martin }
178 1.2 martin
179 1.7 martin /*
180 1.7 martin * return (see post_edit_verify):
181 1.7 martin * 0 -> abort
182 1.7 martin * 1 -> re-edit
183 1.7 martin * 2 -> continue installation
184 1.7 martin */
185 1.1 dholland static int
186 1.10.2.5 msaitoh verify_parts(struct partition_usage_set *pset, bool install)
187 1.1 dholland {
188 1.7 martin struct part_usage_info *wanted;
189 1.7 martin struct disk_partitions *parts;
190 1.7 martin size_t i, num_root;
191 1.10.2.6 bouyer daddr_t first_bsdstart, inst_start;
192 1.7 martin int rv;
193 1.7 martin
194 1.10.2.6 bouyer first_bsdstart = inst_start = -1;
195 1.7 martin num_root = 0;
196 1.7 martin parts = pset->parts;
197 1.7 martin for (i = 0; i < pset->num; i++) {
198 1.7 martin wanted = &pset->infos[i];
199 1.1 dholland
200 1.7 martin if (wanted->flags & PUIFLG_JUST_MOUNTPOINT)
201 1.1 dholland continue;
202 1.7 martin if (wanted->cur_part_id == NO_PART)
203 1.7 martin continue;
204 1.7 martin if (!(wanted->instflags & PUIINST_MOUNT))
205 1.1 dholland continue;
206 1.7 martin if (strcmp(wanted->mount, "/") != 0)
207 1.1 dholland continue;
208 1.7 martin num_root++;
209 1.7 martin
210 1.10.2.6 bouyer if (first_bsdstart <= 0) {
211 1.7 martin first_bsdstart = wanted->cur_start;
212 1.7 martin }
213 1.10.2.6 bouyer if (inst_start < 0 &&
214 1.10.2.6 bouyer (wanted->cur_flags & PTI_INSTALL_TARGET)) {
215 1.7 martin inst_start = wanted->cur_start;
216 1.7 martin }
217 1.7 martin }
218 1.7 martin
219 1.10.2.5 msaitoh if ((num_root == 0 && install) ||
220 1.10.2.6 bouyer (num_root > 1 && inst_start < 0)) {
221 1.10.2.5 msaitoh if (num_root == 0 && install)
222 1.7 martin msg_display_subst(MSG_must_be_one_root, 2,
223 1.7 martin msg_string(parts->pscheme->name),
224 1.7 martin msg_string(parts->pscheme->short_name));
225 1.7 martin else
226 1.7 martin msg_display_subst(MSG_multbsdpart, 2,
227 1.7 martin msg_string(parts->pscheme->name),
228 1.7 martin msg_string(parts->pscheme->short_name));
229 1.7 martin rv = ask_reedit(parts);
230 1.7 martin if (rv != 2)
231 1.7 martin return rv;
232 1.7 martin }
233 1.7 martin
234 1.7 martin /* Check for overlaps */
235 1.7 martin if (checkoverlap(parts) != 0) {
236 1.7 martin rv = ask_reedit(parts);
237 1.7 martin if (rv != 2)
238 1.7 martin return rv;
239 1.1 dholland }
240 1.7 martin
241 1.7 martin /*
242 1.7 martin * post_edit_verify returns:
243 1.7 martin * 0 -> abort
244 1.7 martin * 1 -> re-edit
245 1.7 martin * 2 -> continue installation
246 1.7 martin */
247 1.7 martin if (parts->pscheme->post_edit_verify)
248 1.7 martin return parts->pscheme->post_edit_verify(parts, false);
249 1.7 martin
250 1.7 martin return 2;
251 1.1 dholland }
252 1.1 dholland
253 1.1 dholland static int
254 1.1 dholland edit_fs_start(menudesc *m, void *arg)
255 1.1 dholland {
256 1.7 martin struct single_part_fs_edit *edit = arg;
257 1.7 martin daddr_t start, end;
258 1.1 dholland
259 1.7 martin start = getpartoff(edit->pset->parts, edit->info.start);
260 1.7 martin if (edit->info.size != 0) {
261 1.10.2.8 msaitoh if (start < (edit->info.start+edit->info.size)) {
262 1.10.2.8 msaitoh /* Try to keep end in the same place */
263 1.10.2.8 msaitoh end = edit->info.start + edit->info.size;
264 1.10.2.8 msaitoh if (end < start)
265 1.10.2.8 msaitoh edit->info.size = edit->pset->parts->pscheme->
266 1.10.2.8 msaitoh max_free_space_at(edit->pset->parts,
267 1.10.2.8 msaitoh edit->info.start);
268 1.10.2.8 msaitoh else
269 1.10.2.8 msaitoh edit->info.size = end - start;
270 1.10.2.8 msaitoh } else {
271 1.10.2.8 msaitoh edit->info.size = 0;
272 1.10.2.8 msaitoh }
273 1.1 dholland }
274 1.7 martin edit->info.start = start;
275 1.1 dholland return 0;
276 1.1 dholland }
277 1.1 dholland
278 1.1 dholland static int
279 1.1 dholland edit_fs_size(menudesc *m, void *arg)
280 1.1 dholland {
281 1.7 martin struct single_part_fs_edit *edit = arg;
282 1.10.2.6 bouyer struct disk_part_info pinfo;
283 1.7 martin daddr_t size;
284 1.1 dholland
285 1.10.2.6 bouyer /* get original partition data, in case start moved already */
286 1.10.2.8 msaitoh if (!edit->pset->parts->pscheme->get_part_info(edit->pset->parts,
287 1.10.2.8 msaitoh edit->id, &pinfo))
288 1.10.2.8 msaitoh pinfo = edit->info;
289 1.10.2.6 bouyer /* ask for new size with old start and current values */
290 1.10.2.6 bouyer size = getpartsize(edit->pset->parts, pinfo.start,
291 1.10.2.6 bouyer edit->info.start, edit->info.size);
292 1.7 martin if (size < 0)
293 1.7 martin return 0;
294 1.7 martin if (size > edit->pset->parts->disk_size)
295 1.7 martin size = edit->pset->parts->disk_size - edit->info.start;
296 1.7 martin edit->info.size = size;
297 1.1 dholland return 0;
298 1.1 dholland }
299 1.1 dholland
300 1.7 martin static int
301 1.10.2.6 bouyer set_ffs_opt_pow2(menudesc *m, void *arg)
302 1.10.2.6 bouyer {
303 1.10.2.6 bouyer struct single_part_fs_edit *edit = arg;
304 1.10.2.6 bouyer size_t val = 1 << (edit->offset+m->cursel);
305 1.10.2.6 bouyer
306 1.10.2.6 bouyer if (edit->mode == 1) {
307 1.10.2.6 bouyer edit->info.fs_opt1 = val;
308 1.10.2.6 bouyer edit->wanted->fs_opt1 = val;
309 1.10.2.6 bouyer } else if (edit->mode == 2) {
310 1.10.2.6 bouyer edit->info.fs_opt2 = val;
311 1.10.2.6 bouyer edit->wanted->fs_opt2 = val;
312 1.10.2.6 bouyer }
313 1.10.2.6 bouyer return 0;
314 1.10.2.6 bouyer }
315 1.10.2.6 bouyer
316 1.10.2.6 bouyer static int
317 1.10.2.6 bouyer edit_fs_ffs_opt(menudesc *m, void *arg, msg head,
318 1.10.2.6 bouyer size_t min_val, size_t max_val)
319 1.10.2.6 bouyer {
320 1.10.2.6 bouyer struct single_part_fs_edit *edit = arg;
321 1.10.2.6 bouyer menu_ent opts[min(MAXPHYS/4096, 8)];
322 1.10.2.6 bouyer char names[min(MAXPHYS/4096, 8)][20];
323 1.10.2.6 bouyer size_t i, val;
324 1.10.2.6 bouyer int menu;
325 1.10.2.6 bouyer
326 1.10.2.6 bouyer edit->offset = ilog2(min_val);
327 1.10.2.6 bouyer memset(opts, 0, sizeof opts);
328 1.10.2.6 bouyer for (i = 0, val = min_val; val <= max_val; i++, val <<= 1) {
329 1.10.2.6 bouyer snprintf(names[i], sizeof names[i], "%zu", val);
330 1.10.2.6 bouyer opts[i].opt_name = names[i];
331 1.10.2.6 bouyer opts[i].opt_action = set_ffs_opt_pow2;
332 1.10.2.6 bouyer opts[i].opt_flags = OPT_EXIT;
333 1.10.2.6 bouyer }
334 1.10.2.6 bouyer menu = new_menu(head, opts, i, 40, 6, 0, 0, MC_NOEXITOPT,
335 1.10.2.6 bouyer NULL, NULL, NULL, NULL, NULL);
336 1.10.2.6 bouyer if (menu < 0)
337 1.10.2.6 bouyer return 1;
338 1.10.2.6 bouyer process_menu(menu, arg);
339 1.10.2.6 bouyer free_menu(menu);
340 1.10.2.6 bouyer return 0;
341 1.10.2.6 bouyer }
342 1.10.2.6 bouyer
343 1.10.2.6 bouyer static int
344 1.10.2.6 bouyer edit_fs_ffs_block(menudesc *m, void *arg)
345 1.10.2.6 bouyer {
346 1.10.2.6 bouyer struct single_part_fs_edit *edit = arg;
347 1.10.2.6 bouyer
348 1.10.2.6 bouyer edit->mode = 1; /* edit fs_opt1 */
349 1.10.2.6 bouyer return edit_fs_ffs_opt(m, arg, MSG_Select_file_system_block_size,
350 1.10.2.6 bouyer 4096, MAXPHYS);
351 1.10.2.6 bouyer }
352 1.10.2.6 bouyer
353 1.10.2.6 bouyer static int
354 1.10.2.6 bouyer edit_fs_ffs_frag(menudesc *m, void *arg)
355 1.10.2.6 bouyer {
356 1.10.2.6 bouyer struct single_part_fs_edit *edit = arg;
357 1.10.2.6 bouyer size_t bsize, sec_size;
358 1.10.2.6 bouyer
359 1.10.2.6 bouyer edit->mode = 2; /* edit fs_opt2 */
360 1.10.2.6 bouyer bsize = edit->info.fs_opt1;
361 1.10.2.6 bouyer if (bsize == 0) {
362 1.10.2.6 bouyer sec_size = edit->wanted->parts->bytes_per_sector;
363 1.10.2.6 bouyer if (edit->wanted->size >= (daddr_t)(128L*(GIG/sec_size)))
364 1.10.2.6 bouyer bsize = 32*1024;
365 1.10.2.6 bouyer else if (edit->wanted->size >= (daddr_t)(1000L*(MEG/sec_size)))
366 1.10.2.6 bouyer bsize = 16*1024;
367 1.10.2.6 bouyer else if (edit->wanted->size >= (daddr_t)(20L*(MEG/sec_size)))
368 1.10.2.6 bouyer bsize = 8*1024;
369 1.10.2.6 bouyer else
370 1.10.2.6 bouyer bsize = 4+1024;
371 1.10.2.6 bouyer }
372 1.10.2.6 bouyer return edit_fs_ffs_opt(m, arg, MSG_Select_file_system_fragment_size,
373 1.10.2.6 bouyer bsize / 8, bsize);
374 1.10.2.6 bouyer }
375 1.10.2.6 bouyer
376 1.10.2.6 bouyer static int
377 1.10.2.6 bouyer edit_fs_ffs_avg_size(menudesc *m, void *arg)
378 1.10.2.6 bouyer {
379 1.10.2.6 bouyer struct single_part_fs_edit *edit = arg;
380 1.10.2.6 bouyer char answer[12];
381 1.10.2.6 bouyer
382 1.10.2.6 bouyer snprintf(answer, sizeof answer, "%u", edit->info.fs_opt3);
383 1.10.2.6 bouyer msg_prompt_win(MSG_ptn_isize_prompt, -1, 18, 0, 0,
384 1.10.2.6 bouyer answer, answer, sizeof answer);
385 1.10.2.6 bouyer edit->info.fs_opt3 = atol(answer);
386 1.10.2.6 bouyer edit->wanted->fs_opt3 = edit->info.fs_opt3;
387 1.10.2.6 bouyer
388 1.10.2.6 bouyer return 0;
389 1.10.2.6 bouyer }
390 1.10.2.6 bouyer
391 1.10.2.6 bouyer static int
392 1.7 martin edit_fs_preserve(menudesc *m, void *arg)
393 1.1 dholland {
394 1.7 martin struct single_part_fs_edit *edit = arg;
395 1.1 dholland
396 1.7 martin edit->wanted->instflags ^= PUIINST_NEWFS;
397 1.7 martin return 0;
398 1.1 dholland }
399 1.1 dholland
400 1.7 martin static int
401 1.7 martin edit_install(menudesc *m, void *arg)
402 1.1 dholland {
403 1.7 martin struct single_part_fs_edit *edit = arg;
404 1.1 dholland
405 1.10.2.6 bouyer edit->info.flags ^= PTI_INSTALL_TARGET;
406 1.7 martin return 0;
407 1.1 dholland }
408 1.1 dholland
409 1.7 martin static int
410 1.7 martin edit_fs_mount(menudesc *m, void *arg)
411 1.1 dholland {
412 1.7 martin struct single_part_fs_edit *edit = arg;
413 1.1 dholland
414 1.7 martin edit->wanted->instflags ^= PUIINST_MOUNT;
415 1.7 martin return 0;
416 1.1 dholland }
417 1.1 dholland
418 1.1 dholland static int
419 1.7 martin edit_fs_mountpt(menudesc *m, void *arg)
420 1.1 dholland {
421 1.7 martin struct single_part_fs_edit *edit = arg;
422 1.7 martin char *p, *first, *last, buf[MOUNTLEN];
423 1.7 martin
424 1.7 martin strlcpy(buf, edit->wanted->mount, sizeof buf);
425 1.7 martin msg_prompt_win(MSG_mountpoint, -1, 18, 0, 0,
426 1.7 martin buf, buf, MOUNTLEN);
427 1.7 martin
428 1.7 martin /*
429 1.7 martin * Trim all leading and trailing whitespace
430 1.7 martin */
431 1.7 martin for (first = NULL, last = NULL, p = buf; *p; p++) {
432 1.7 martin if (isspace((unsigned char)*p))
433 1.7 martin continue;
434 1.7 martin if (first == NULL)
435 1.7 martin first = p;
436 1.7 martin last = p;
437 1.7 martin }
438 1.7 martin if (last != NULL)
439 1.7 martin last[1] = 0;
440 1.7 martin
441 1.10.2.6 bouyer if (first == NULL || *first == 0 || strcmp(first, "none") == 0) {
442 1.7 martin edit->wanted->mount[0] = 0;
443 1.7 martin edit->wanted->instflags &= ~PUIINST_MOUNT;
444 1.7 martin return 0;
445 1.7 martin }
446 1.7 martin
447 1.7 martin if (*first != '/') {
448 1.7 martin edit->wanted->mount[0] = '/';
449 1.7 martin strlcpy(&edit->wanted->mount[1], first,
450 1.7 martin sizeof(edit->wanted->mount)-1);
451 1.7 martin } else {
452 1.7 martin strlcpy(edit->wanted->mount, first, sizeof edit->wanted->mount);
453 1.7 martin }
454 1.1 dholland
455 1.1 dholland return 0;
456 1.7 martin }
457 1.1 dholland
458 1.1 dholland static int
459 1.7 martin edit_restore(menudesc *m, void *arg)
460 1.1 dholland {
461 1.7 martin struct single_part_fs_edit *edit = arg;
462 1.1 dholland
463 1.7 martin edit->info = edit->old_info;
464 1.7 martin *edit->wanted = edit->old_usage;
465 1.1 dholland return 0;
466 1.1 dholland }
467 1.1 dholland
468 1.1 dholland static int
469 1.7 martin edit_cancel(menudesc *m, void *arg)
470 1.1 dholland {
471 1.7 martin struct single_part_fs_edit *edit = arg;
472 1.1 dholland
473 1.7 martin edit->rv = -1;
474 1.7 martin return 1;
475 1.1 dholland }
476 1.1 dholland
477 1.1 dholland static int
478 1.7 martin edit_delete_ptn(menudesc *m, void *arg)
479 1.1 dholland {
480 1.7 martin struct single_part_fs_edit *edit = arg;
481 1.1 dholland
482 1.7 martin edit->rv = -2;
483 1.7 martin return 1;
484 1.7 martin }
485 1.7 martin
486 1.7 martin /*
487 1.7 martin * We have added/removed partitions, all cur_part_id values are
488 1.7 martin * out of sync. Re-fetch and reorder partitions accordingly.
489 1.7 martin */
490 1.7 martin static void
491 1.7 martin renumber_partitions(struct partition_usage_set *pset)
492 1.7 martin {
493 1.7 martin struct part_usage_info *ninfos;
494 1.7 martin struct disk_part_info info;
495 1.7 martin size_t i;
496 1.7 martin part_id pno;
497 1.7 martin
498 1.7 martin ninfos = calloc(pset->parts->num_part, sizeof(*ninfos));
499 1.7 martin if (ninfos == NULL) {
500 1.7 martin err_msg_win(err_outofmem);
501 1.7 martin return;
502 1.7 martin }
503 1.1 dholland
504 1.7 martin for (pno = 0; pno < pset->parts->num_part; pno++) {
505 1.7 martin if (!pset->parts->pscheme->get_part_info(pset->parts, pno,
506 1.7 martin &info))
507 1.7 martin continue;
508 1.10.2.9 snj for (i = 0; i < pset->num; i++) {
509 1.7 martin if (pset->infos[i].cur_start != info.start)
510 1.7 martin continue;
511 1.10.2.5 msaitoh if (pset->infos[i].cur_flags != info.flags)
512 1.10.2.5 msaitoh continue;
513 1.10.2.6 bouyer if ((info.fs_type != FS_UNUSED &&
514 1.10.2.6 bouyer info.fs_type == pset->infos[i].fs_type) ||
515 1.10.2.6 bouyer (pset->infos[i].type ==
516 1.10.2.6 bouyer info.nat_type->generic_ptype)) {
517 1.10.2.6 bouyer memcpy(&ninfos[pno], &pset->infos[i],
518 1.10.2.6 bouyer sizeof(ninfos[pno]));
519 1.10.2.6 bouyer ninfos[pno].cur_part_id = pno;
520 1.10.2.6 bouyer break;
521 1.10.2.6 bouyer }
522 1.7 martin }
523 1.1 dholland }
524 1.1 dholland
525 1.10.2.9 snj free(pset->infos);
526 1.10.2.9 snj pset->infos = ninfos;
527 1.10.2.9 snj pset->num = pset->parts->num_part;
528 1.7 martin }
529 1.7 martin
530 1.7 martin /*
531 1.7 martin * Most often used file system types, we offer them in a first level menu.
532 1.7 martin */
533 1.7 martin static const uint edit_fs_common_types[] =
534 1.7 martin { FS_BSDFFS, FS_SWAP, FS_MSDOS, FS_BSDLFS, FS_EX2FS };
535 1.7 martin
536 1.7 martin /*
537 1.7 martin * Functions for uncommon file system types - we offer the full list,
538 1.7 martin * but put FFSv2 and FFSv1 at the front.
539 1.7 martin */
540 1.7 martin static void
541 1.7 martin init_fs_type_ext(menudesc *menu, void *arg)
542 1.7 martin {
543 1.7 martin struct single_part_fs_edit *edit = arg;
544 1.7 martin uint t = edit->info.fs_type;
545 1.7 martin size_t i, ndx, max = menu->numopts;
546 1.7 martin
547 1.7 martin if (t == FS_BSDFFS) {
548 1.7 martin if (edit->info.fs_sub_type == 2)
549 1.7 martin menu->cursel = 0;
550 1.7 martin else
551 1.7 martin menu->cursel = 1;
552 1.7 martin return;
553 1.10.2.4 msaitoh } else if (t == FS_EX2FS && edit->info.fs_sub_type == 1) {
554 1.10.2.4 msaitoh menu->cursel = FSMAXTYPES;
555 1.10.2.4 msaitoh return;
556 1.1 dholland }
557 1.7 martin /* skip the two FFS entries, and do not add FFS later again */
558 1.7 martin for (ndx = 2, i = 0; i < FSMAXTYPES && ndx < max; i++) {
559 1.7 martin if (i == FS_UNUSED)
560 1.7 martin continue;
561 1.7 martin if (i == FS_BSDFFS)
562 1.7 martin continue;
563 1.7 martin if (fstypenames[i] == NULL)
564 1.7 martin continue;
565 1.1 dholland
566 1.7 martin if (i == t) {
567 1.7 martin menu->cursel = ndx;
568 1.7 martin break;
569 1.7 martin }
570 1.7 martin ndx++;
571 1.7 martin }
572 1.1 dholland }
573 1.1 dholland
574 1.1 dholland static int
575 1.7 martin set_fstype_ext(menudesc *menu, void *arg)
576 1.1 dholland {
577 1.7 martin struct single_part_fs_edit *edit = arg;
578 1.7 martin size_t i, ndx, max = menu->numopts;
579 1.10.2.4 msaitoh enum part_type pt;
580 1.7 martin
581 1.7 martin if (menu->cursel == 0 || menu->cursel == 1) {
582 1.7 martin edit->info.fs_type = FS_BSDFFS;
583 1.7 martin edit->info.fs_sub_type = menu->cursel == 0 ? 2 : 1;
584 1.10.2.4 msaitoh goto found_type;
585 1.10.2.4 msaitoh } else if (menu->cursel == FSMAXTYPES) {
586 1.10.2.4 msaitoh edit->info.fs_type = FS_EX2FS;
587 1.10.2.4 msaitoh edit->info.fs_sub_type = 1;
588 1.10.2.4 msaitoh goto found_type;
589 1.7 martin }
590 1.1 dholland
591 1.7 martin for (ndx = 2, i = 0; i < FSMAXTYPES && ndx < max; i++) {
592 1.7 martin if (i == FS_UNUSED)
593 1.7 martin continue;
594 1.7 martin if (i == FS_BSDFFS)
595 1.7 martin continue;
596 1.7 martin if (fstypenames[i] == NULL)
597 1.7 martin continue;
598 1.7 martin
599 1.7 martin if (ndx == (size_t)menu->cursel) {
600 1.7 martin edit->info.fs_type = i;
601 1.7 martin edit->info.fs_sub_type = 0;
602 1.10.2.4 msaitoh goto found_type;
603 1.7 martin }
604 1.7 martin ndx++;
605 1.7 martin }
606 1.1 dholland return 1;
607 1.10.2.4 msaitoh
608 1.10.2.4 msaitoh found_type:
609 1.10.2.4 msaitoh pt = edit->info.nat_type ? edit->info.nat_type->generic_ptype : PT_root;
610 1.10.2.4 msaitoh edit->info.nat_type = edit->pset->parts->pscheme->
611 1.10.2.4 msaitoh get_fs_part_type(pt, edit->info.fs_type, edit->info.fs_sub_type);
612 1.10.2.4 msaitoh if (edit->info.nat_type == NULL)
613 1.10.2.4 msaitoh edit->info.nat_type = edit->pset->parts->pscheme->
614 1.10.2.4 msaitoh get_generic_part_type(PT_root);
615 1.10.2.4 msaitoh edit->wanted->type = edit->info.nat_type->generic_ptype;
616 1.10.2.4 msaitoh edit->wanted->fs_type = edit->info.fs_type;
617 1.10.2.4 msaitoh edit->wanted->fs_version = edit->info.fs_sub_type;
618 1.10.2.4 msaitoh return 1;
619 1.1 dholland }
620 1.1 dholland
621 1.7 martin /*
622 1.7 martin * Offer a menu with "exotic" file system types, start with FFSv2 and FFSv1,
623 1.7 martin * skip later FFS entry in the generic list.
624 1.7 martin */
625 1.1 dholland static int
626 1.7 martin edit_fs_type_ext(menudesc *menu, void *arg)
627 1.1 dholland {
628 1.7 martin menu_ent *opts;
629 1.7 martin int m;
630 1.7 martin size_t i, ndx, cnt;
631 1.7 martin
632 1.10.2.4 msaitoh cnt = __arraycount(fstypenames);
633 1.7 martin opts = calloc(cnt, sizeof(*opts));
634 1.7 martin if (opts == NULL)
635 1.7 martin return 1;
636 1.7 martin
637 1.7 martin ndx = 0;
638 1.7 martin opts[ndx].opt_name = msg_string(MSG_fs_type_ffsv2);
639 1.7 martin opts[ndx].opt_action = set_fstype_ext;
640 1.7 martin ndx++;
641 1.7 martin opts[ndx].opt_name = msg_string(MSG_fs_type_ffs);
642 1.7 martin opts[ndx].opt_action = set_fstype_ext;
643 1.7 martin ndx++;
644 1.7 martin for (i = 0; i < FSMAXTYPES && ndx < cnt; i++) {
645 1.7 martin if (i == FS_UNUSED)
646 1.7 martin continue;
647 1.7 martin if (i == FS_BSDFFS)
648 1.7 martin continue;
649 1.7 martin if (fstypenames[i] == NULL)
650 1.7 martin continue;
651 1.7 martin opts[ndx].opt_name = fstypenames[i];
652 1.7 martin opts[ndx].opt_action = set_fstype_ext;
653 1.7 martin ndx++;
654 1.7 martin }
655 1.10.2.4 msaitoh opts[ndx].opt_name = msg_string(MSG_fs_type_ext2old);
656 1.10.2.4 msaitoh opts[ndx].opt_action = set_fstype_ext;
657 1.10.2.4 msaitoh ndx++;
658 1.7 martin assert(ndx == cnt);
659 1.7 martin m = new_menu(MSG_Select_the_type, opts, ndx,
660 1.7 martin 30, 6, 10, 0, MC_SUBMENU | MC_SCROLL,
661 1.7 martin init_fs_type_ext, NULL, NULL, NULL, MSG_unchanged);
662 1.7 martin
663 1.7 martin if (m < 0)
664 1.7 martin return 1;
665 1.7 martin process_menu(m, arg);
666 1.7 martin free_menu(m);
667 1.7 martin free(opts);
668 1.1 dholland
669 1.1 dholland return 1;
670 1.1 dholland }
671 1.1 dholland
672 1.1 dholland static void
673 1.7 martin init_fs_type(menudesc *menu, void *arg)
674 1.7 martin {
675 1.7 martin struct single_part_fs_edit *edit = arg;
676 1.7 martin size_t i;
677 1.7 martin
678 1.7 martin /* init menu->cursel from fs type in arg */
679 1.7 martin if (edit->info.fs_type == FS_BSDFFS) {
680 1.7 martin if (edit->info.fs_sub_type == 2)
681 1.7 martin menu->cursel = 0;
682 1.7 martin else
683 1.7 martin menu->cursel = 1;
684 1.7 martin }
685 1.7 martin for (i = 1; i < __arraycount(edit_fs_common_types); i++) {
686 1.7 martin if (edit->info.fs_type == edit_fs_common_types[i]) {
687 1.7 martin menu->cursel = i+1;
688 1.7 martin break;
689 1.7 martin }
690 1.7 martin }
691 1.7 martin }
692 1.7 martin
693 1.7 martin static int
694 1.7 martin set_fstype(menudesc *menu, void *arg)
695 1.7 martin {
696 1.7 martin struct single_part_fs_edit *edit = arg;
697 1.10.2.4 msaitoh enum part_type pt;
698 1.7 martin int ndx;
699 1.7 martin
700 1.10.2.4 msaitoh pt = edit->info.nat_type ? edit->info.nat_type->generic_ptype : PT_root;
701 1.7 martin if (menu->cursel < 2) {
702 1.7 martin edit->info.fs_type = FS_BSDFFS;
703 1.7 martin edit->info.fs_sub_type = menu->cursel == 0 ? 2 : 1;
704 1.7 martin edit->info.nat_type = edit->pset->parts->pscheme->
705 1.10.2.4 msaitoh get_fs_part_type(pt, FS_BSDFFS, 2);
706 1.7 martin if (edit->info.nat_type == NULL)
707 1.7 martin edit->info.nat_type = edit->pset->parts->
708 1.7 martin pscheme->get_generic_part_type(PT_root);
709 1.7 martin edit->wanted->type = edit->info.nat_type->generic_ptype;
710 1.7 martin edit->wanted->fs_type = edit->info.fs_type;
711 1.7 martin edit->wanted->fs_version = edit->info.fs_sub_type;
712 1.7 martin return 1;
713 1.7 martin }
714 1.7 martin ndx = menu->cursel-1;
715 1.7 martin
716 1.7 martin if (ndx < 0 ||
717 1.7 martin (size_t)ndx >= __arraycount(edit_fs_common_types))
718 1.7 martin return 1;
719 1.7 martin
720 1.7 martin edit->info.fs_type = edit_fs_common_types[ndx];
721 1.7 martin edit->info.fs_sub_type = 0;
722 1.7 martin edit->info.nat_type = edit->pset->parts->pscheme->
723 1.10.2.4 msaitoh get_fs_part_type(pt, edit->info.fs_type, 0);
724 1.7 martin if (edit->info.nat_type == NULL)
725 1.7 martin edit->info.nat_type = edit->pset->parts->
726 1.7 martin pscheme->get_generic_part_type(PT_root);
727 1.7 martin edit->wanted->type = edit->info.nat_type->generic_ptype;
728 1.7 martin edit->wanted->fs_type = edit->info.fs_type;
729 1.7 martin edit->wanted->fs_version = edit->info.fs_sub_type;
730 1.7 martin return 1;
731 1.7 martin }
732 1.7 martin
733 1.7 martin /*
734 1.7 martin * Offer a menu selecting the common file system types
735 1.7 martin */
736 1.7 martin static int
737 1.7 martin edit_fs_type(menudesc *menu, void *arg)
738 1.1 dholland {
739 1.7 martin struct single_part_fs_edit *edit = arg;
740 1.7 martin menu_ent *opts;
741 1.7 martin int m, cnt;
742 1.7 martin size_t i;
743 1.7 martin
744 1.7 martin /*
745 1.7 martin * Shortcut to full menu if we have an exotic value
746 1.7 martin */
747 1.10.2.4 msaitoh if (edit->info.fs_type == FS_EX2FS && edit->info.fs_sub_type == 1) {
748 1.10.2.4 msaitoh edit_fs_type_ext(menu, arg);
749 1.10.2.4 msaitoh return 0;
750 1.10.2.4 msaitoh }
751 1.7 martin for (i = 0; i < __arraycount(edit_fs_common_types); i++)
752 1.7 martin if (edit->info.fs_type == edit_fs_common_types[i])
753 1.7 martin break;
754 1.7 martin if (i >= __arraycount(edit_fs_common_types)) {
755 1.7 martin edit_fs_type_ext(menu, arg);
756 1.7 martin return 0;
757 1.7 martin }
758 1.1 dholland
759 1.7 martin /*
760 1.7 martin * Starting with a common type, show short menu first
761 1.7 martin */
762 1.7 martin cnt = __arraycount(edit_fs_common_types) + 2;
763 1.7 martin opts = calloc(cnt, sizeof(*opts));
764 1.7 martin if (opts == NULL)
765 1.7 martin return 0;
766 1.7 martin
767 1.7 martin /* special case entry 0: two FFS entries */
768 1.7 martin for (i = 0; i < __arraycount(edit_fs_common_types); i++) {
769 1.7 martin opts[i+1].opt_name = getfslabelname(edit_fs_common_types[i], 0);
770 1.7 martin opts[i+1].opt_action = set_fstype;
771 1.7 martin }
772 1.7 martin /* duplicate FFS (at offset 1) into first entry */
773 1.7 martin opts[0] = opts[1];
774 1.7 martin opts[0].opt_name = msg_string(MSG_fs_type_ffsv2);
775 1.7 martin opts[1].opt_name = msg_string(MSG_fs_type_ffs);
776 1.7 martin /* add secondary sub-menu */
777 1.7 martin assert(i+1 < (size_t)cnt);
778 1.7 martin opts[i+1].opt_name = msg_string(MSG_other_fs_type);
779 1.7 martin opts[i+1].opt_action = edit_fs_type_ext;
780 1.7 martin
781 1.7 martin m = new_menu(MSG_Select_the_type, opts, cnt,
782 1.7 martin 30, 6, 0, 0, MC_SUBMENU | MC_SCROLL,
783 1.7 martin init_fs_type, NULL, NULL, NULL, MSG_unchanged);
784 1.7 martin
785 1.7 martin if (m < 0)
786 1.7 martin return 0;
787 1.7 martin process_menu(m, arg);
788 1.7 martin free_menu(m);
789 1.7 martin free(opts);
790 1.7 martin
791 1.7 martin return 0;
792 1.1 dholland }
793 1.1 dholland
794 1.7 martin
795 1.7 martin static void update_edit_ptn_menu(menudesc *m, void *arg);
796 1.7 martin static void draw_edit_ptn_line(menudesc *m, int opt, void *arg);
797 1.7 martin static int edit_ptn_custom_type(menudesc *m, void *arg);
798 1.1 dholland
799 1.10.2.6 bouyer static void
800 1.10.2.6 bouyer remember_deleted(struct partition_usage_set *pset,
801 1.10.2.6 bouyer struct disk_partitions *parts)
802 1.10.2.6 bouyer {
803 1.10.2.6 bouyer size_t i, num;
804 1.10.2.6 bouyer struct disk_partitions **tab;
805 1.10.2.6 bouyer
806 1.10.2.6 bouyer /* do we have parts on record already? */
807 1.10.2.6 bouyer for (i = 0; i < pset->num_write_back; i++)
808 1.10.2.6 bouyer if (pset->write_back[i] == parts)
809 1.10.2.6 bouyer return;
810 1.10.2.6 bouyer /*
811 1.10.2.6 bouyer * Need to record this partition table for write back
812 1.10.2.6 bouyer */
813 1.10.2.6 bouyer num = pset->num_write_back + 1;
814 1.10.2.6 bouyer tab = realloc(pset->write_back, num*sizeof(*pset->write_back));
815 1.10.2.6 bouyer if (!tab)
816 1.10.2.6 bouyer return;
817 1.10.2.6 bouyer tab[pset->num_write_back] = parts;
818 1.10.2.6 bouyer pset->write_back = tab;
819 1.10.2.6 bouyer pset->num_write_back = num;
820 1.10.2.6 bouyer }
821 1.10.2.6 bouyer
822 1.2 martin int
823 1.1 dholland edit_ptn(menudesc *menu, void *arg)
824 1.1 dholland {
825 1.7 martin struct partition_usage_set *pset = arg;
826 1.7 martin struct single_part_fs_edit edit;
827 1.7 martin int fspart_menu, num_opts;
828 1.7 martin const char *err;
829 1.7 martin menu_ent *mopts, *popt;
830 1.7 martin bool is_new_part, with_inst_opt = pset->parts->parent == NULL;
831 1.7 martin
832 1.7 martin static const menu_ent edit_ptn_fields_head[] = {
833 1.8 christos { .opt_action=edit_fs_type },
834 1.8 christos { .opt_action=edit_fs_start },
835 1.8 christos { .opt_action=edit_fs_size },
836 1.8 christos { .opt_flags=OPT_IGNORE },
837 1.7 martin };
838 1.7 martin
839 1.7 martin static const menu_ent edit_ptn_fields_head_add[] = {
840 1.8 christos { .opt_action=edit_install },
841 1.7 martin };
842 1.7 martin
843 1.7 martin static const menu_ent edit_ptn_fields_head2[] = {
844 1.8 christos { .opt_action=edit_fs_preserve },
845 1.8 christos { .opt_action=edit_fs_mount },
846 1.7 martin { .opt_menu=MENU_mountoptions, .opt_flags=OPT_SUB },
847 1.8 christos { .opt_action=edit_fs_mountpt },
848 1.7 martin };
849 1.10.2.6 bouyer
850 1.10.2.6 bouyer static const menu_ent edit_ptn_fields_ffs[] = {
851 1.10.2.6 bouyer { .opt_action=edit_fs_ffs_avg_size },
852 1.10.2.6 bouyer { .opt_action=edit_fs_ffs_block },
853 1.10.2.6 bouyer { .opt_action=edit_fs_ffs_frag },
854 1.10.2.6 bouyer };
855 1.10.2.6 bouyer
856 1.7 martin static const menu_ent edit_ptn_fields_tail[] = {
857 1.7 martin { .opt_name=MSG_askunits, .opt_menu=MENU_sizechoice,
858 1.7 martin .opt_flags=OPT_SUB },
859 1.8 christos { .opt_name=MSG_restore,
860 1.7 martin .opt_action=edit_restore},
861 1.8 christos { .opt_name=MSG_Delete_partition,
862 1.7 martin .opt_action=edit_delete_ptn},
863 1.8 christos { .opt_name=MSG_cancel,
864 1.7 martin .opt_action=edit_cancel},
865 1.1 dholland };
866 1.7 martin
867 1.7 martin memset(&edit, 0, sizeof edit);
868 1.7 martin edit.pset = pset;
869 1.7 martin edit.index = menu->cursel;
870 1.7 martin edit.wanted = &pset->infos[edit.index];
871 1.7 martin
872 1.7 martin if (menu->cursel < 0 || (size_t)menu->cursel > pset->parts->num_part)
873 1.7 martin return 0;
874 1.7 martin is_new_part = (size_t)menu->cursel == pset->parts->num_part;
875 1.7 martin
876 1.7 martin num_opts = __arraycount(edit_ptn_fields_head) +
877 1.7 martin __arraycount(edit_ptn_fields_head2) +
878 1.7 martin __arraycount(edit_ptn_fields_tail);
879 1.10.2.6 bouyer if (edit.wanted->fs_type == FS_BSDFFS ||
880 1.10.2.6 bouyer edit.wanted->fs_type == FS_BSDLFS)
881 1.10.2.6 bouyer num_opts += __arraycount(edit_ptn_fields_ffs);
882 1.7 martin if (with_inst_opt)
883 1.7 martin num_opts += __arraycount(edit_ptn_fields_head_add);
884 1.7 martin if (is_new_part)
885 1.7 martin num_opts--;
886 1.7 martin else
887 1.7 martin num_opts += pset->parts->pscheme->custom_attribute_count;
888 1.7 martin
889 1.7 martin mopts = calloc(num_opts, sizeof(*mopts));
890 1.7 martin if (mopts == NULL) {
891 1.7 martin err_msg_win(err_outofmem);
892 1.7 martin return 0;
893 1.7 martin }
894 1.7 martin memcpy(mopts, edit_ptn_fields_head, sizeof(edit_ptn_fields_head));
895 1.7 martin popt = mopts + __arraycount(edit_ptn_fields_head);
896 1.7 martin if (with_inst_opt) {
897 1.7 martin memcpy(popt, edit_ptn_fields_head_add,
898 1.7 martin sizeof(edit_ptn_fields_head_add));
899 1.7 martin popt += __arraycount(edit_ptn_fields_head_add);
900 1.7 martin }
901 1.7 martin memcpy(popt, edit_ptn_fields_head2, sizeof(edit_ptn_fields_head2));
902 1.7 martin popt += __arraycount(edit_ptn_fields_head2);
903 1.10.2.6 bouyer if (edit.wanted->fs_type == FS_BSDFFS ||
904 1.10.2.6 bouyer edit.wanted->fs_type == FS_BSDLFS) {
905 1.10.2.6 bouyer memcpy(popt, edit_ptn_fields_ffs, sizeof(edit_ptn_fields_ffs));
906 1.10.2.6 bouyer popt += __arraycount(edit_ptn_fields_ffs);
907 1.10.2.6 bouyer }
908 1.7 martin edit.first_custom_attr = popt - mopts;
909 1.7 martin if (!is_new_part) {
910 1.7 martin for (size_t i = 0;
911 1.7 martin i < pset->parts->pscheme->custom_attribute_count;
912 1.7 martin i++, popt++) {
913 1.7 martin popt->opt_action = edit_ptn_custom_type;
914 1.7 martin }
915 1.7 martin }
916 1.7 martin memcpy(popt, edit_ptn_fields_tail, sizeof(edit_ptn_fields_tail));
917 1.7 martin popt += __arraycount(edit_ptn_fields_tail) - 1;
918 1.7 martin if (is_new_part)
919 1.7 martin memcpy(popt-1, popt, sizeof(*popt));
920 1.7 martin
921 1.7 martin if (is_new_part) {
922 1.7 martin struct disk_part_free_space space;
923 1.7 martin daddr_t align = pset->parts->pscheme->get_part_alignment(
924 1.7 martin pset->parts);
925 1.7 martin
926 1.7 martin edit.id = NO_PART;
927 1.7 martin if (pset->parts->pscheme->get_free_spaces(pset->parts,
928 1.7 martin &space, 1, align, align, -1, -1) == 1) {
929 1.7 martin edit.info.start = space.start;
930 1.7 martin edit.info.size = space.size;
931 1.7 martin edit.info.fs_type = FS_BSDFFS;
932 1.7 martin edit.info.fs_sub_type = 2;
933 1.7 martin edit.info.nat_type = pset->parts->pscheme->
934 1.10.2.4 msaitoh get_fs_part_type(PT_root, edit.info.fs_type,
935 1.7 martin edit.info.fs_sub_type);
936 1.7 martin edit.wanted->instflags = PUIINST_NEWFS;
937 1.7 martin }
938 1.7 martin } else {
939 1.7 martin edit.id = pset->infos[edit.index].cur_part_id;
940 1.7 martin if (!pset->parts->pscheme->get_part_info(pset->parts, edit.id,
941 1.7 martin &edit.info)) {
942 1.7 martin free(mopts);
943 1.7 martin return 0;
944 1.7 martin }
945 1.7 martin }
946 1.7 martin
947 1.7 martin edit.old_usage = *edit.wanted;
948 1.7 martin edit.old_info = edit.info;
949 1.7 martin
950 1.7 martin fspart_menu = new_menu(MSG_edfspart, mopts, num_opts,
951 1.7 martin 15, 2, 0, 55, MC_NOCLEAR | MC_SCROLL,
952 1.7 martin update_edit_ptn_menu, draw_edit_ptn_line, NULL,
953 1.7 martin NULL, MSG_OK);
954 1.7 martin
955 1.7 martin process_menu(fspart_menu, &edit);
956 1.7 martin free(mopts);
957 1.7 martin free_menu(fspart_menu);
958 1.7 martin
959 1.7 martin if (edit.rv == 0) { /* OK, set new data */
960 1.7 martin edit.info.last_mounted = edit.wanted->mount;
961 1.7 martin if (is_new_part) {
962 1.10.2.6 bouyer edit.wanted->parts = pset->parts;
963 1.7 martin edit.wanted->cur_part_id = pset->parts->pscheme->
964 1.7 martin add_partition(pset->parts, &edit.info, &err);
965 1.7 martin if (edit.wanted->cur_part_id == NO_PART)
966 1.7 martin err_msg_win(err);
967 1.7 martin else {
968 1.7 martin pset->parts->pscheme->get_part_info(
969 1.7 martin pset->parts, edit.wanted->cur_part_id,
970 1.7 martin &edit.info);
971 1.7 martin edit.wanted->cur_start = edit.info.start;
972 1.7 martin edit.wanted->size = edit.info.size;
973 1.7 martin edit.wanted->type =
974 1.7 martin edit.info.nat_type->generic_ptype;
975 1.7 martin edit.wanted->fs_type = edit.info.fs_type;
976 1.7 martin edit.wanted->fs_version = edit.info.fs_sub_type;
977 1.7 martin /* things have changed, re-sort */
978 1.7 martin renumber_partitions(pset);
979 1.7 martin }
980 1.7 martin } else {
981 1.7 martin if (!pset->parts->pscheme->set_part_info(pset->parts,
982 1.7 martin edit.id, &edit.info, &err))
983 1.7 martin err_msg_win(err);
984 1.10.2.9 snj else
985 1.10.2.9 snj pset->cur_free_space += edit.old_info.size -
986 1.10.2.9 snj edit.info.size;
987 1.7 martin }
988 1.7 martin
989 1.7 martin /*
990 1.7 martin * if size has changed, we may need to add or remove
991 1.7 martin * the option to add partitions
992 1.7 martin */
993 1.7 martin show_partition_adder(menu, pset);
994 1.7 martin } else if (edit.rv == -1) { /* cancel edit */
995 1.7 martin if (is_new_part) {
996 1.7 martin memmove(pset->infos+edit.index,
997 1.7 martin pset->infos+edit.index+1,
998 1.7 martin sizeof(*pset->infos)*(pset->num-edit.index));
999 1.7 martin memmove(menu->opts+edit.index,
1000 1.7 martin menu->opts+edit.index+1,
1001 1.7 martin sizeof(*menu->opts)*(menu->numopts-edit.index));
1002 1.7 martin menu->numopts--;
1003 1.7 martin menu->cursel = 0;
1004 1.7 martin pset->num--;
1005 1.7 martin return -1;
1006 1.7 martin }
1007 1.7 martin pset->infos[edit.index] = edit.old_usage;
1008 1.7 martin } else if (!is_new_part && edit.rv == -2) { /* delete partition */
1009 1.7 martin if (!pset->parts->pscheme->delete_partition(pset->parts,
1010 1.7 martin edit.id, &err)) {
1011 1.7 martin err_msg_win(err);
1012 1.7 martin return 0;
1013 1.7 martin }
1014 1.10.2.6 bouyer remember_deleted(pset,
1015 1.10.2.6 bouyer pset->infos[edit.index].parts);
1016 1.10.2.9 snj pset->cur_free_space += edit.info.size;
1017 1.7 martin memmove(pset->infos+edit.index,
1018 1.7 martin pset->infos+edit.index+1,
1019 1.7 martin sizeof(*pset->infos)*(pset->num-edit.index));
1020 1.7 martin memmove(menu->opts+edit.index,
1021 1.7 martin menu->opts+edit.index+1,
1022 1.7 martin sizeof(*menu->opts)*(menu->numopts-edit.index));
1023 1.7 martin menu->numopts--;
1024 1.7 martin menu->cursel = 0;
1025 1.10.2.6 bouyer if (pset->parts->num_part == 0)
1026 1.10.2.6 bouyer menu->cursel = 1; /* skip sentinel line */
1027 1.7 martin
1028 1.7 martin /* things have changed, re-sort */
1029 1.7 martin pset->num--;
1030 1.7 martin renumber_partitions(pset);
1031 1.7 martin
1032 1.7 martin /* we can likely add new partitions now */
1033 1.7 martin show_partition_adder(menu, pset);
1034 1.7 martin
1035 1.7 martin return -1;
1036 1.1 dholland }
1037 1.1 dholland
1038 1.1 dholland return 0;
1039 1.1 dholland }
1040 1.1 dholland
1041 1.1 dholland static void
1042 1.7 martin update_edit_ptn_menu(menudesc *m, void *arg)
1043 1.1 dholland {
1044 1.7 martin struct single_part_fs_edit *edit = arg;
1045 1.1 dholland int i;
1046 1.7 martin uint t = edit->info.fs_type;
1047 1.7 martin size_t attr_no;
1048 1.1 dholland
1049 1.7 martin /* Determine which of the properties can be changed */
1050 1.7 martin for (i = 0; i < m->numopts; i++) {
1051 1.7 martin if (m->opts[i].opt_action == NULL &&
1052 1.7 martin m->opts[i].opt_menu != MENU_mountoptions)
1053 1.7 martin continue;
1054 1.1 dholland
1055 1.1 dholland /* Default to disabled... */
1056 1.1 dholland m->opts[i].opt_flags |= OPT_IGNORE;
1057 1.7 martin if ((t == FS_UNUSED || t == FS_SWAP) &&
1058 1.7 martin (m->opts[i].opt_action == edit_fs_preserve ||
1059 1.7 martin m->opts[i].opt_action == edit_fs_mount ||
1060 1.7 martin m->opts[i].opt_action == edit_fs_mountpt ||
1061 1.7 martin m->opts[i].opt_menu == MENU_mountoptions))
1062 1.1 dholland continue;
1063 1.7 martin if (m->opts[i].opt_action == edit_install &&
1064 1.7 martin edit->info.nat_type &&
1065 1.7 martin edit->info.nat_type->generic_ptype != PT_root)
1066 1.7 martin /* can only install onto PT_root partitions */
1067 1.7 martin continue;
1068 1.7 martin if (m->opts[i].opt_action == edit_fs_preserve &&
1069 1.9 martin t != FS_BSDFFS && t != FS_BSDLFS && t != FS_APPLEUFS &&
1070 1.9 martin t != FS_MSDOS && t != FS_EX2FS) {
1071 1.9 martin /* Can not newfs this filesystem */
1072 1.7 martin edit->wanted->instflags &= ~PUIINST_NEWFS;
1073 1.1 dholland continue;
1074 1.1 dholland }
1075 1.7 martin if (m->opts[i].opt_action == edit_ptn_custom_type) {
1076 1.7 martin attr_no = (size_t)i - edit->first_custom_attr;
1077 1.7 martin if (!edit->pset->parts->pscheme->
1078 1.7 martin custom_attribute_writable(
1079 1.7 martin edit->pset->parts, edit->id, attr_no))
1080 1.1 dholland continue;
1081 1.1 dholland }
1082 1.10.2.7 msaitoh /*
1083 1.10.2.7 msaitoh * Do not allow editing of size/start/type when partition
1084 1.10.2.7 msaitoh * is defined in some outer partition table already
1085 1.10.2.7 msaitoh */
1086 1.10.2.7 msaitoh if ((edit->pset->infos[edit->index].flags & PUIFLG_IS_OUTER)
1087 1.10.2.7 msaitoh && (m->opts[i].opt_action == edit_fs_type
1088 1.10.2.7 msaitoh || m->opts[i].opt_action == edit_fs_start
1089 1.10.2.7 msaitoh || m->opts[i].opt_action == edit_fs_size))
1090 1.10.2.7 msaitoh continue;
1091 1.1 dholland /* Ok: we want this one */
1092 1.1 dholland m->opts[i].opt_flags &= ~OPT_IGNORE;
1093 1.1 dholland }
1094 1.10.2.7 msaitoh
1095 1.10.2.7 msaitoh /* Avoid starting at a (now) disabled menu item */
1096 1.10.2.7 msaitoh while (m->cursel >= 0 && m->cursel < m->numopts
1097 1.10.2.7 msaitoh && (m->opts[m->cursel].opt_flags & OPT_IGNORE))
1098 1.10.2.7 msaitoh m->cursel++;
1099 1.1 dholland }
1100 1.1 dholland
1101 1.1 dholland static void
1102 1.7 martin draw_edit_ptn_line(menudesc *m, int opt, void *arg)
1103 1.1 dholland {
1104 1.7 martin struct single_part_fs_edit *edit = arg;
1105 1.7 martin static int col_width;
1106 1.7 martin static const char *ptn_type, *ptn_start, *ptn_size, *ptn_end,
1107 1.7 martin *ptn_newfs, *ptn_mount, *ptn_mount_options, *ptn_mountpt,
1108 1.10.2.6 bouyer *ptn_install, *ptn_bsize, *ptn_fsize, *ptn_isize;
1109 1.7 martin const char *c;
1110 1.7 martin char val[MENUSTRSIZE];
1111 1.7 martin const char *attrname;
1112 1.7 martin size_t attr_no;
1113 1.7 martin
1114 1.7 martin if (col_width == 0) {
1115 1.7 martin int l;
1116 1.7 martin
1117 1.7 martin #define LOAD(STR) STR = msg_string(MSG_##STR); l = strlen(STR); \
1118 1.7 martin if (l > col_width) col_width = l
1119 1.7 martin
1120 1.7 martin LOAD(ptn_type);
1121 1.7 martin LOAD(ptn_start);
1122 1.7 martin LOAD(ptn_size);
1123 1.7 martin LOAD(ptn_end);
1124 1.7 martin LOAD(ptn_install);
1125 1.7 martin LOAD(ptn_newfs);
1126 1.7 martin LOAD(ptn_mount);
1127 1.7 martin LOAD(ptn_mount_options);
1128 1.7 martin LOAD(ptn_mountpt);
1129 1.10.2.6 bouyer LOAD(ptn_bsize);
1130 1.10.2.6 bouyer LOAD(ptn_fsize);
1131 1.10.2.6 bouyer LOAD(ptn_isize);
1132 1.7 martin #undef LOAD
1133 1.7 martin
1134 1.7 martin for (size_t i = 0;
1135 1.7 martin i < edit->pset->parts->pscheme->custom_attribute_count;
1136 1.7 martin i++) {
1137 1.7 martin attrname = msg_string(
1138 1.7 martin edit->pset->parts->pscheme->custom_attributes[i]
1139 1.7 martin .label);
1140 1.7 martin l = strlen(attrname);
1141 1.7 martin if (l > col_width) col_width = l;
1142 1.7 martin }
1143 1.1 dholland
1144 1.7 martin col_width += 3;
1145 1.7 martin }
1146 1.1 dholland
1147 1.1 dholland if (m->opts[opt].opt_flags & OPT_IGNORE
1148 1.7 martin && (opt != 3 || edit->info.fs_type == FS_UNUSED)
1149 1.10.2.7 msaitoh && m->opts[opt].opt_action != edit_ptn_custom_type
1150 1.10.2.7 msaitoh && m->opts[opt].opt_action != edit_fs_type
1151 1.10.2.7 msaitoh && m->opts[opt].opt_action != edit_fs_start
1152 1.10.2.7 msaitoh && m->opts[opt].opt_action != edit_fs_size) {
1153 1.7 martin wprintw(m->mw, "%*s -", col_width, "");
1154 1.1 dholland return;
1155 1.1 dholland }
1156 1.1 dholland
1157 1.7 martin if (opt < 4) {
1158 1.7 martin switch (opt) {
1159 1.7 martin case 0:
1160 1.7 martin if (edit->info.fs_type == FS_BSDFFS)
1161 1.7 martin if (edit->info.fs_sub_type == 2)
1162 1.7 martin c = msg_string(MSG_fs_type_ffsv2);
1163 1.7 martin else
1164 1.7 martin c = msg_string(MSG_fs_type_ffs);
1165 1.1 dholland else
1166 1.7 martin c = getfslabelname(edit->info.fs_type,
1167 1.7 martin edit->info.fs_sub_type);
1168 1.7 martin wprintw(m->mw, "%*s : %s", col_width, ptn_type, c);
1169 1.7 martin return;
1170 1.7 martin case 1:
1171 1.7 martin wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1172 1.7 martin ptn_start, edit->info.start / sizemult, multname);
1173 1.7 martin return;
1174 1.7 martin case 2:
1175 1.7 martin wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1176 1.7 martin ptn_size, edit->info.size / sizemult, multname);
1177 1.7 martin return;
1178 1.7 martin case 3:
1179 1.7 martin wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1180 1.7 martin ptn_end, (edit->info.start + edit->info.size)
1181 1.7 martin / sizemult, multname);
1182 1.7 martin return;
1183 1.7 martin }
1184 1.7 martin }
1185 1.7 martin if (m->opts[opt].opt_action == edit_install) {
1186 1.7 martin wprintw(m->mw, "%*s : %s", col_width, ptn_install,
1187 1.10.2.6 bouyer msg_string((edit->info.flags & PTI_INSTALL_TARGET)
1188 1.7 martin ? MSG_Yes : MSG_No));
1189 1.7 martin return;
1190 1.7 martin }
1191 1.7 martin if (m->opts[opt].opt_action == edit_fs_preserve) {
1192 1.7 martin wprintw(m->mw, "%*s : %s", col_width, ptn_newfs,
1193 1.7 martin msg_string(edit->wanted->instflags & PUIINST_NEWFS
1194 1.7 martin ? MSG_Yes : MSG_No));
1195 1.7 martin return;
1196 1.7 martin }
1197 1.7 martin if (m->opts[opt].opt_action == edit_fs_mount) {
1198 1.7 martin wprintw(m->mw, "%*s : %s", col_width, ptn_mount,
1199 1.7 martin msg_string(edit->wanted->instflags & PUIINST_MOUNT
1200 1.7 martin ? MSG_Yes : MSG_No));
1201 1.7 martin return;
1202 1.7 martin }
1203 1.10.2.6 bouyer if (m->opts[opt].opt_action == edit_fs_ffs_block) {
1204 1.10.2.6 bouyer wprintw(m->mw, "%*s : %u", col_width, ptn_bsize,
1205 1.10.2.6 bouyer edit->wanted->fs_opt1);
1206 1.10.2.6 bouyer return;
1207 1.10.2.6 bouyer }
1208 1.10.2.6 bouyer if (m->opts[opt].opt_action == edit_fs_ffs_frag) {
1209 1.10.2.6 bouyer wprintw(m->mw, "%*s : %u", col_width, ptn_fsize,
1210 1.10.2.6 bouyer edit->wanted->fs_opt2);
1211 1.10.2.6 bouyer return;
1212 1.10.2.6 bouyer }
1213 1.10.2.6 bouyer if (m->opts[opt].opt_action == edit_fs_ffs_avg_size) {
1214 1.10.2.6 bouyer if (edit->wanted->fs_opt3 == 0)
1215 1.10.2.6 bouyer wprintw(m->mw, "%*s : %s", col_width, ptn_isize,
1216 1.10.2.6 bouyer msg_string(MSG_ptn_isize_dflt));
1217 1.10.2.6 bouyer else {
1218 1.10.2.6 bouyer char buf[24], *line;
1219 1.10.2.6 bouyer const char *t = buf;
1220 1.10.2.6 bouyer
1221 1.10.2.6 bouyer snprintf(buf, sizeof buf, "%u", edit->wanted->fs_opt3);
1222 1.10.2.6 bouyer line = str_arg_subst(msg_string(MSG_ptn_isize_bytes),
1223 1.10.2.6 bouyer 1, &t);
1224 1.10.2.6 bouyer wprintw(m->mw, "%*s : %s", col_width, ptn_isize,
1225 1.10.2.6 bouyer line);
1226 1.10.2.6 bouyer free(line);
1227 1.10.2.6 bouyer }
1228 1.10.2.6 bouyer return;
1229 1.10.2.6 bouyer }
1230 1.7 martin if (m->opts[opt].opt_menu == MENU_mountoptions) {
1231 1.7 martin wprintw(m->mw, "%*s : ", col_width, ptn_mount_options);
1232 1.7 martin if (edit->wanted->mountflags & PUIMNT_ASYNC)
1233 1.1 dholland wprintw(m->mw, "async ");
1234 1.7 martin if (edit->wanted->mountflags & PUIMNT_NOATIME)
1235 1.1 dholland wprintw(m->mw, "noatime ");
1236 1.7 martin if (edit->wanted->mountflags & PUIMNT_NODEV)
1237 1.1 dholland wprintw(m->mw, "nodev ");
1238 1.7 martin if (edit->wanted->mountflags & PUIMNT_NODEVMTIME)
1239 1.1 dholland wprintw(m->mw, "nodevmtime ");
1240 1.7 martin if (edit->wanted->mountflags & PUIMNT_NOEXEC)
1241 1.1 dholland wprintw(m->mw, "noexec ");
1242 1.7 martin if (edit->wanted->mountflags & PUIMNT_NOSUID)
1243 1.1 dholland wprintw(m->mw, "nosuid ");
1244 1.7 martin if (edit->wanted->mountflags & PUIMNT_LOG)
1245 1.1 dholland wprintw(m->mw, "log ");
1246 1.7 martin if (edit->wanted->mountflags & PUIMNT_NOAUTO)
1247 1.7 martin wprintw(m->mw, "noauto ");
1248 1.7 martin return;
1249 1.7 martin }
1250 1.7 martin if (m->opts[opt].opt_action == edit_fs_mountpt) {
1251 1.7 martin wprintw(m->mw, "%*s : %s", col_width, ptn_mountpt,
1252 1.7 martin edit->wanted->mount);
1253 1.7 martin return;
1254 1.7 martin }
1255 1.7 martin
1256 1.7 martin attr_no = opt - edit->first_custom_attr;
1257 1.7 martin edit->pset->parts->pscheme->format_custom_attribute(
1258 1.7 martin edit->pset->parts, edit->id, attr_no, &edit->info,
1259 1.7 martin val, sizeof val);
1260 1.7 martin attrname = msg_string(edit->pset->parts->pscheme->
1261 1.7 martin custom_attributes[attr_no].label);
1262 1.7 martin wprintw(m->mw, "%*s : %s", col_width, attrname, val);
1263 1.7 martin }
1264 1.7 martin
1265 1.7 martin static int
1266 1.7 martin edit_ptn_custom_type(menudesc *m, void *arg)
1267 1.7 martin {
1268 1.7 martin struct single_part_fs_edit *edit = arg;
1269 1.7 martin size_t attr_no = m->cursel - edit->first_custom_attr;
1270 1.7 martin char line[STRSIZE];
1271 1.7 martin
1272 1.7 martin switch (edit->pset->parts->pscheme->custom_attributes[attr_no].type) {
1273 1.7 martin case pet_bool:
1274 1.7 martin edit->pset->parts->pscheme->custom_attribute_toggle(
1275 1.7 martin edit->pset->parts, edit->id, attr_no);
1276 1.1 dholland break;
1277 1.7 martin case pet_cardinal:
1278 1.7 martin case pet_str:
1279 1.7 martin edit->pset->parts->pscheme->format_custom_attribute(
1280 1.7 martin edit->pset->parts, edit->id, attr_no, &edit->info,
1281 1.7 martin line, sizeof(line));
1282 1.7 martin msg_prompt_win(
1283 1.7 martin edit->pset->parts->pscheme->custom_attributes[attr_no].
1284 1.7 martin label, -1, 18, 0, 0, line, line, sizeof(line));
1285 1.7 martin edit->pset->parts->pscheme->custom_attribute_set_str(
1286 1.7 martin edit->pset->parts, edit->id, attr_no, line);
1287 1.1 dholland break;
1288 1.1 dholland }
1289 1.7 martin
1290 1.7 martin return 0;
1291 1.7 martin }
1292 1.7 martin
1293 1.7 martin
1294 1.7 martin /*
1295 1.7 martin * Some column width depend on translation, we will set these in
1296 1.7 martin * fmt_fspart_header and later use it when formatting single entries
1297 1.7 martin * in fmt_fspart_row.
1298 1.7 martin * The table consist of 3 "size like" columns, all fixed width, then
1299 1.7 martin * ptnheaders_fstype, part_header_col_flag, and finally the mount point
1300 1.7 martin * (which is variable width).
1301 1.7 martin */
1302 1.7 martin static int fstype_width, flags_width;
1303 1.7 martin static char fspart_separator[MENUSTRSIZE];
1304 1.7 martin static char fspart_title[2*MENUSTRSIZE];
1305 1.7 martin
1306 1.7 martin /*
1307 1.7 martin * Format the header of the main partition editor menu.
1308 1.7 martin */
1309 1.7 martin static void
1310 1.7 martin fmt_fspart_header(menudesc *menu, void *arg)
1311 1.7 martin {
1312 1.7 martin struct partition_usage_set *pset = arg;
1313 1.7 martin char total[6], free_space[6], scol[13], ecol[13], szcol[13],
1314 1.10.2.3 msaitoh sepline[MENUSTRSIZE], *p, desc[MENUSTRSIZE];
1315 1.7 martin const char *fstype, *flags;
1316 1.7 martin int i;
1317 1.10.2.3 msaitoh size_t ptn;
1318 1.10.2.3 msaitoh bool with_clone, with_inst_flag = pset->parts->parent == NULL;
1319 1.7 martin
1320 1.10.2.3 msaitoh with_clone = false;
1321 1.10.2.3 msaitoh for (ptn = 0; ptn < pset->num && !with_clone; ptn++)
1322 1.10.2.3 msaitoh if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
1323 1.10.2.3 msaitoh with_clone = true;
1324 1.7 martin humanize_number(total, sizeof total,
1325 1.10.2.5 msaitoh pset->parts->disk_size * pset->parts->bytes_per_sector,
1326 1.7 martin "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1327 1.7 martin humanize_number(free_space, sizeof free_space,
1328 1.10.2.5 msaitoh pset->cur_free_space * pset->parts->bytes_per_sector,
1329 1.7 martin "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1330 1.7 martin
1331 1.10.2.3 msaitoh if (with_clone)
1332 1.10.2.3 msaitoh strlcpy(desc, msg_string(MSG_clone_flag_desc), sizeof desc);
1333 1.10.2.3 msaitoh else
1334 1.10.2.3 msaitoh desc[0] = 0;
1335 1.10.2.3 msaitoh if (pset->parts->pscheme->part_flag_desc)
1336 1.10.2.3 msaitoh strlcat(desc, msg_string(pset->parts->pscheme->part_flag_desc),
1337 1.10.2.3 msaitoh sizeof desc);
1338 1.10.2.3 msaitoh
1339 1.7 martin msg_display_subst(MSG_fspart, 7, pset->parts->disk,
1340 1.7 martin msg_string(pset->parts->pscheme->name),
1341 1.7 martin msg_string(pset->parts->pscheme->short_name),
1342 1.7 martin with_inst_flag ? msg_string(MSG_ptn_instflag_desc) : "",
1343 1.10.2.3 msaitoh desc, total, free_space);
1344 1.7 martin
1345 1.7 martin snprintf(scol, sizeof scol, "%s (%s)",
1346 1.7 martin msg_string(MSG_ptnheaders_start), multname);
1347 1.7 martin snprintf(ecol, sizeof ecol, "%s (%s)",
1348 1.7 martin msg_string(MSG_ptnheaders_end), multname);
1349 1.7 martin snprintf(szcol, sizeof szcol, "%s (%s)",
1350 1.7 martin msg_string(MSG_ptnheaders_size), multname);
1351 1.7 martin
1352 1.7 martin fstype = msg_string(MSG_ptnheaders_fstype);
1353 1.7 martin flags = msg_string(MSG_part_header_col_flag);
1354 1.7 martin fstype_width = max(strlen(fstype), 8);
1355 1.7 martin flags_width = strlen(flags);
1356 1.7 martin for (i = 0, p = sepline; i < fstype_width; i++)
1357 1.7 martin *p++ = '-';
1358 1.7 martin for (i = 0, *p++ = ' '; i < flags_width; i++)
1359 1.7 martin *p++ = '-';
1360 1.7 martin *p = 0;
1361 1.7 martin
1362 1.7 martin snprintf(fspart_separator, sizeof(fspart_separator),
1363 1.7 martin "------------ ------------ ------------ %s ----------------",
1364 1.7 martin sepline);
1365 1.7 martin
1366 1.7 martin snprintf(fspart_title, sizeof(fspart_title),
1367 1.7 martin " %12.12s %12.12s %12.12s %*s %*s %s\n"
1368 1.7 martin " %s", scol, ecol, szcol, fstype_width, fstype,
1369 1.7 martin flags_width, flags, msg_string(MSG_ptnheaders_filesystem),
1370 1.7 martin fspart_separator);
1371 1.7 martin
1372 1.7 martin msg_table_add("\n\n");
1373 1.7 martin }
1374 1.7 martin
1375 1.7 martin /*
1376 1.7 martin * Format one partition entry in the main partition editor.
1377 1.7 martin */
1378 1.7 martin static void
1379 1.7 martin fmt_fspart_row(menudesc *m, int ptn, void *arg)
1380 1.7 martin {
1381 1.7 martin struct partition_usage_set *pset = arg;
1382 1.7 martin struct disk_part_info info;
1383 1.7 martin daddr_t poffset, psize, pend;
1384 1.7 martin const char *desc;
1385 1.7 martin static const char *Yes;
1386 1.7 martin char flag_str[MENUSTRSIZE], *fp;
1387 1.7 martin unsigned inst_flags;
1388 1.10.2.3 msaitoh #ifndef NO_CLONES
1389 1.10.2.3 msaitoh size_t clone_cnt;
1390 1.10.2.3 msaitoh #endif
1391 1.7 martin bool with_inst_flag = pset->parts->parent == NULL;
1392 1.7 martin
1393 1.7 martin if (Yes == NULL)
1394 1.7 martin Yes = msg_string(MSG_Yes);
1395 1.7 martin
1396 1.10.2.3 msaitoh #ifndef NO_CLONES
1397 1.10.2.3 msaitoh if ((pset->infos[ptn].flags & PUIFLG_CLONE_PARTS) &&
1398 1.10.2.3 msaitoh pset->infos[ptn].cur_part_id == NO_PART) {
1399 1.10.2.3 msaitoh psize = pset->infos[ptn].size / sizemult;
1400 1.10.2.3 msaitoh if (pset->infos[ptn].clone_ndx <
1401 1.10.2.3 msaitoh pset->infos[ptn].clone_src->num_sel)
1402 1.10.2.3 msaitoh clone_cnt = 1;
1403 1.10.2.3 msaitoh else
1404 1.10.2.3 msaitoh clone_cnt = pset->infos[ptn].clone_src->num_sel;
1405 1.10.2.3 msaitoh if (pset->infos[ptn].cur_part_id == NO_PART)
1406 1.10.2.3 msaitoh wprintw(m->mw, " %12" PRIu64
1407 1.10.2.3 msaitoh " [%zu %s]", psize, clone_cnt,
1408 1.10.2.3 msaitoh msg_string(MSG_clone_target_disp));
1409 1.10.2.3 msaitoh else {
1410 1.10.2.3 msaitoh poffset = pset->infos[ptn].cur_start / sizemult;
1411 1.10.2.3 msaitoh pend = (pset->infos[ptn].cur_start +
1412 1.10.2.3 msaitoh pset->infos[ptn].size) / sizemult - 1;
1413 1.10.2.3 msaitoh wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
1414 1.10.2.3 msaitoh " [%zu %s]",
1415 1.10.2.3 msaitoh poffset, pend, psize, clone_cnt,
1416 1.10.2.3 msaitoh msg_string(MSG_clone_target_disp));
1417 1.10.2.3 msaitoh }
1418 1.10.2.3 msaitoh if (m->title == fspart_title)
1419 1.10.2.3 msaitoh m->opts[ptn].opt_flags |= OPT_IGNORE;
1420 1.10.2.3 msaitoh else
1421 1.10.2.3 msaitoh m->opts[ptn].opt_flags &= ~OPT_IGNORE;
1422 1.10.2.3 msaitoh return;
1423 1.10.2.3 msaitoh }
1424 1.10.2.3 msaitoh #endif
1425 1.10.2.3 msaitoh
1426 1.7 martin if (!real_partition(pset, ptn))
1427 1.7 martin return;
1428 1.7 martin
1429 1.7 martin if (!pset->parts->pscheme->get_part_info(pset->parts,
1430 1.7 martin pset->infos[ptn].cur_part_id, &info))
1431 1.7 martin return;
1432 1.7 martin
1433 1.10.2.3 msaitoh /*
1434 1.10.2.3 msaitoh * We use this function in multiple menus, but only want it
1435 1.10.2.3 msaitoh * to play with enable/disable in a single one:
1436 1.10.2.3 msaitoh */
1437 1.10.2.3 msaitoh if (m->title == fspart_title) {
1438 1.10.2.3 msaitoh /*
1439 1.10.2.3 msaitoh * Enable / disable this line if it is something
1440 1.10.2.3 msaitoh * like RAW_PART
1441 1.10.2.3 msaitoh */
1442 1.10.2.3 msaitoh if ((info.flags &
1443 1.10.2.3 msaitoh (PTI_WHOLE_DISK|PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
1444 1.10.2.3 msaitoh || (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS))
1445 1.10.2.3 msaitoh m->opts[ptn].opt_flags |= OPT_IGNORE;
1446 1.10.2.3 msaitoh else
1447 1.10.2.3 msaitoh m->opts[ptn].opt_flags &= ~OPT_IGNORE;
1448 1.10.2.3 msaitoh }
1449 1.7 martin
1450 1.7 martin poffset = info.start / sizemult;
1451 1.7 martin psize = info.size / sizemult;
1452 1.7 martin if (psize == 0)
1453 1.7 martin pend = 0;
1454 1.7 martin else
1455 1.7 martin pend = (info.start + info.size) / sizemult - 1;
1456 1.7 martin
1457 1.7 martin if (info.flags & PTI_WHOLE_DISK)
1458 1.7 martin desc = msg_string(MSG_NetBSD_partition_cant_change);
1459 1.7 martin else if (info.flags & PTI_RAW_PART)
1460 1.7 martin desc = msg_string(MSG_Whole_disk_cant_change);
1461 1.7 martin else if (info.flags & PTI_BOOT)
1462 1.7 martin desc = msg_string(MSG_Boot_partition_cant_change);
1463 1.7 martin else
1464 1.7 martin desc = getfslabelname(info.fs_type, info.fs_sub_type);
1465 1.7 martin
1466 1.7 martin fp = flag_str;
1467 1.7 martin inst_flags = pset->infos[ptn].instflags;
1468 1.10.2.6 bouyer if (with_inst_flag && (info.flags & PTI_INSTALL_TARGET) &&
1469 1.7 martin info.nat_type->generic_ptype == PT_root) {
1470 1.7 martin static char inst_flag;
1471 1.7 martin
1472 1.7 martin if (inst_flag == 0)
1473 1.7 martin inst_flag = msg_string(MSG_install_flag)[0];
1474 1.7 martin *fp++ = inst_flag;
1475 1.7 martin }
1476 1.7 martin if (inst_flags & PUIINST_NEWFS)
1477 1.7 martin *fp++ = msg_string(MSG_newfs_flag)[0];
1478 1.10.2.3 msaitoh if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
1479 1.10.2.3 msaitoh *fp++ = msg_string(MSG_clone_flag)[0];
1480 1.7 martin *fp = 0;
1481 1.7 martin if (pset->parts->pscheme->get_part_attr_str != NULL)
1482 1.7 martin pset->parts->pscheme->get_part_attr_str(pset->parts,
1483 1.7 martin pset->infos[ptn].cur_part_id, fp, sizeof(flag_str)-1);
1484 1.7 martin
1485 1.7 martin /* if the fstype description does not fit, check if we can overrun */
1486 1.7 martin if (strlen(desc) > (size_t)fstype_width &&
1487 1.7 martin flag_str[0] == 0 && (info.last_mounted == NULL ||
1488 1.7 martin info.last_mounted[0] == 0))
1489 1.7 martin wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
1490 1.7 martin " %s",
1491 1.7 martin poffset, pend, psize, desc);
1492 1.7 martin else
1493 1.7 martin wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
1494 1.7 martin " %*.*s %*s %s",
1495 1.7 martin poffset, pend, psize, fstype_width, fstype_width, desc,
1496 1.7 martin -flags_width, flag_str,
1497 1.7 martin (inst_flags & PUIINST_MOUNT) && info.last_mounted &&
1498 1.7 martin info.last_mounted[0] ? info.last_mounted : "");
1499 1.1 dholland }
1500 1.1 dholland
1501 1.10.2.3 msaitoh #ifndef NO_CLONES
1502 1.10.2.3 msaitoh static int
1503 1.10.2.3 msaitoh part_ext_clone(menudesc *m, void *arg)
1504 1.10.2.3 msaitoh {
1505 1.10.2.3 msaitoh struct selected_partitions selected, *clone_src;
1506 1.10.2.3 msaitoh struct clone_target_menu_data data;
1507 1.10.2.3 msaitoh struct partition_usage_set *pset = arg;
1508 1.10.2.3 msaitoh struct part_usage_info *p;
1509 1.10.2.3 msaitoh struct disk_part_info sinfo, cinfo;
1510 1.10.2.3 msaitoh struct disk_partitions *csrc;
1511 1.10.2.3 msaitoh struct disk_part_free_space space;
1512 1.10.2.3 msaitoh menu_ent *men;
1513 1.10.2.3 msaitoh daddr_t clone_size, free_size, offset, align;
1514 1.10.2.3 msaitoh int num_men, i;
1515 1.10.2.3 msaitoh size_t s, clone_cnt;
1516 1.10.2.3 msaitoh part_id cid;
1517 1.10.2.3 msaitoh struct clone_data {
1518 1.10.2.3 msaitoh struct disk_part_info info;
1519 1.10.2.3 msaitoh part_id new_id;
1520 1.10.2.3 msaitoh size_t ndx;
1521 1.10.2.3 msaitoh };
1522 1.10.2.3 msaitoh struct clone_data *clones = NULL;
1523 1.10.2.3 msaitoh
1524 1.10.2.3 msaitoh if (!select_partitions(&selected, pm->parts))
1525 1.10.2.3 msaitoh return 0;
1526 1.10.2.3 msaitoh
1527 1.10.2.3 msaitoh clone_size = selected_parts_size(&selected);
1528 1.10.2.3 msaitoh num_men = pset->num+1;
1529 1.10.2.3 msaitoh men = calloc(num_men, sizeof *men);
1530 1.10.2.3 msaitoh if (men == NULL)
1531 1.10.2.3 msaitoh return 0;
1532 1.10.2.3 msaitoh for (i = 0; i < num_men; i++) {
1533 1.10.2.3 msaitoh men[i].opt_action = clone_target_select;
1534 1.10.2.3 msaitoh if (i == 0)
1535 1.10.2.3 msaitoh free_size = pset->infos[i].cur_start;
1536 1.10.2.3 msaitoh else if (i > 0 && (size_t)i < pset->num)
1537 1.10.2.3 msaitoh free_size = pset->infos[i].cur_start -
1538 1.10.2.3 msaitoh pset->infos[i-1].cur_start - pset->infos[i-1].size;
1539 1.10.2.3 msaitoh else
1540 1.10.2.3 msaitoh free_size = pset->parts->free_space;
1541 1.10.2.3 msaitoh if (free_size < clone_size)
1542 1.10.2.3 msaitoh men[i].opt_flags = OPT_IGNORE;
1543 1.10.2.3 msaitoh }
1544 1.10.2.3 msaitoh men[num_men-1].opt_name = MSG_clone_target_end;
1545 1.10.2.3 msaitoh
1546 1.10.2.3 msaitoh memset(&data, 0, sizeof data);
1547 1.10.2.3 msaitoh data.usage = *pset;
1548 1.10.2.3 msaitoh data.res = -1;
1549 1.10.2.3 msaitoh
1550 1.10.2.3 msaitoh data.usage.menu = new_menu(MSG_clone_target_hdr,
1551 1.10.2.3 msaitoh men, num_men, 3, 2, 0, 65, MC_SCROLL,
1552 1.10.2.3 msaitoh NULL, fmt_fspart_row, NULL, NULL, MSG_cancel);
1553 1.10.2.3 msaitoh process_menu(data.usage.menu, &data);
1554 1.10.2.3 msaitoh free_menu(data.usage.menu);
1555 1.10.2.3 msaitoh free(men);
1556 1.10.2.3 msaitoh
1557 1.10.2.3 msaitoh if (data.res < 0)
1558 1.10.2.3 msaitoh goto err;
1559 1.10.2.3 msaitoh
1560 1.10.2.3 msaitoh /* create temporary infos for all clones that work out */
1561 1.10.2.3 msaitoh clone_cnt = 0;
1562 1.10.2.3 msaitoh clones = calloc(selected.num_sel, sizeof(*clones));
1563 1.10.2.3 msaitoh if (clones == NULL)
1564 1.10.2.3 msaitoh goto err;
1565 1.10.2.3 msaitoh
1566 1.10.2.3 msaitoh clone_src = malloc(sizeof(selected));
1567 1.10.2.3 msaitoh if (clone_src == NULL)
1568 1.10.2.3 msaitoh goto err;
1569 1.10.2.3 msaitoh *clone_src = selected;
1570 1.10.2.3 msaitoh
1571 1.10.2.3 msaitoh /* find selected offset from data.res and insert clones there */
1572 1.10.2.3 msaitoh align = pset->parts->pscheme->get_part_alignment(pset->parts);
1573 1.10.2.3 msaitoh offset = -1;
1574 1.10.2.3 msaitoh if (data.res > 0)
1575 1.10.2.3 msaitoh offset = pset->infos[data.res-1].cur_start
1576 1.10.2.3 msaitoh + pset->infos[data.res-1].size;
1577 1.10.2.3 msaitoh else
1578 1.10.2.3 msaitoh offset = 0;
1579 1.10.2.3 msaitoh for (s = 0; s < selected.num_sel; s++) {
1580 1.10.2.3 msaitoh csrc = selected.selection[s].parts;
1581 1.10.2.3 msaitoh cid = selected.selection[s].id;
1582 1.10.2.3 msaitoh csrc->pscheme->get_part_info(csrc, cid, &sinfo);
1583 1.10.2.3 msaitoh if (!pset->parts->pscheme->adapt_foreign_part_info(
1584 1.10.2.3 msaitoh pset->parts, &cinfo, csrc->pscheme, &sinfo))
1585 1.10.2.3 msaitoh continue;
1586 1.10.2.3 msaitoh size_t cnt = pset->parts->pscheme->get_free_spaces(
1587 1.10.2.3 msaitoh pset->parts, &space, 1, cinfo.size-align, align,
1588 1.10.2.3 msaitoh offset, -1);
1589 1.10.2.3 msaitoh if (cnt == 0)
1590 1.10.2.3 msaitoh continue;
1591 1.10.2.3 msaitoh cinfo.start = space.start;
1592 1.10.2.3 msaitoh cid = pset->parts->pscheme->add_partition(
1593 1.10.2.3 msaitoh pset->parts, &cinfo, NULL);
1594 1.10.2.3 msaitoh if (cid == NO_PART)
1595 1.10.2.3 msaitoh continue;
1596 1.10.2.3 msaitoh pset->parts->pscheme->get_part_info(pset->parts, cid, &cinfo);
1597 1.10.2.3 msaitoh clones[clone_cnt].info = cinfo;
1598 1.10.2.3 msaitoh clones[clone_cnt].new_id = cid;
1599 1.10.2.3 msaitoh clones[clone_cnt].ndx = s;
1600 1.10.2.3 msaitoh clone_cnt++;
1601 1.10.2.6 bouyer offset = roundup(cinfo.start+cinfo.size, align);
1602 1.10.2.3 msaitoh }
1603 1.10.2.3 msaitoh
1604 1.10.2.3 msaitoh /* insert new clone records at offset data.res */
1605 1.10.2.3 msaitoh men = realloc(m->opts, (m->numopts+clone_cnt)*sizeof(*m->opts));
1606 1.10.2.3 msaitoh if (men == NULL)
1607 1.10.2.3 msaitoh goto err;
1608 1.10.2.3 msaitoh pset->menu_opts = men;
1609 1.10.2.3 msaitoh m->opts = men;
1610 1.10.2.3 msaitoh m->numopts += clone_cnt;
1611 1.10.2.3 msaitoh
1612 1.10.2.3 msaitoh p = realloc(pset->infos, (pset->num+clone_cnt)*sizeof(*pset->infos));
1613 1.10.2.3 msaitoh if (p == NULL)
1614 1.10.2.3 msaitoh goto err;
1615 1.10.2.3 msaitoh pset->infos = p;
1616 1.10.2.3 msaitoh
1617 1.10.2.3 msaitoh men += data.res;
1618 1.10.2.3 msaitoh p += data.res;
1619 1.10.2.3 msaitoh memmove(men+clone_cnt, men,
1620 1.10.2.3 msaitoh sizeof(*men)*(m->numopts-data.res-clone_cnt));
1621 1.10.2.3 msaitoh if (pset->num > (size_t)data.res)
1622 1.10.2.3 msaitoh memmove(p+clone_cnt, p, sizeof(*p)*(pset->num-data.res));
1623 1.10.2.3 msaitoh memset(men, 0, sizeof(*men)*clone_cnt);
1624 1.10.2.3 msaitoh memset(p, 0, sizeof(*p)*clone_cnt);
1625 1.10.2.3 msaitoh for (s = 0; s < clone_cnt; s++) {
1626 1.10.2.3 msaitoh p[s].cur_part_id = clones[s].new_id;
1627 1.10.2.3 msaitoh p[s].cur_start = clones[s].info.start;
1628 1.10.2.3 msaitoh p[s].size = clones[s].info.size;
1629 1.10.2.3 msaitoh p[s].cur_flags = clones[s].info.flags;
1630 1.10.2.3 msaitoh p[s].flags = PUIFLG_CLONE_PARTS;
1631 1.10.2.3 msaitoh p[s].parts = pset->parts;
1632 1.10.2.3 msaitoh p[s].clone_src = clone_src;
1633 1.10.2.3 msaitoh p[s].clone_ndx = s;
1634 1.10.2.3 msaitoh }
1635 1.10.2.3 msaitoh free(clones);
1636 1.10.2.3 msaitoh m->cursel = ((size_t)data.res >= pset->num) ? 0 : data.res+clone_cnt;
1637 1.10.2.3 msaitoh pset->num += clone_cnt;
1638 1.10.2.3 msaitoh m->h = 0;
1639 1.10.2.3 msaitoh resize_menu_height(m);
1640 1.10.2.3 msaitoh
1641 1.10.2.3 msaitoh return -1;
1642 1.10.2.3 msaitoh
1643 1.10.2.3 msaitoh err:
1644 1.10.2.3 msaitoh free(clones);
1645 1.10.2.3 msaitoh free_selected_partitions(&selected);
1646 1.10.2.3 msaitoh return 0;
1647 1.10.2.3 msaitoh }
1648 1.10.2.3 msaitoh #endif
1649 1.10.2.3 msaitoh
1650 1.1 dholland static int
1651 1.7 martin edit_fspart_pack(menudesc *m, void *arg)
1652 1.1 dholland {
1653 1.7 martin struct partition_usage_set *pset = arg;
1654 1.7 martin char buf[STRSIZE];
1655 1.1 dholland
1656 1.7 martin if (!pset->parts->pscheme->get_disk_pack_name(pset->parts,
1657 1.7 martin buf, sizeof buf))
1658 1.7 martin return 0;
1659 1.7 martin
1660 1.7 martin msg_prompt_win(MSG_edit_disk_pack_hdr,
1661 1.7 martin -1, 18, 0, -1, buf, buf, sizeof(buf));
1662 1.7 martin
1663 1.7 martin pset->parts->pscheme->set_disk_pack_name(pset->parts, buf);
1664 1.1 dholland return 0;
1665 1.1 dholland }
1666 1.1 dholland
1667 1.7 martin static int
1668 1.7 martin edit_fspart_add(menudesc *m, void *arg)
1669 1.7 martin {
1670 1.7 martin struct partition_usage_set *pset = arg;
1671 1.7 martin struct part_usage_info *ninfo;
1672 1.7 martin menu_ent *nmenopts;
1673 1.7 martin size_t cnt, off;
1674 1.7 martin
1675 1.7 martin ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
1676 1.7 martin if (ninfo == NULL)
1677 1.7 martin return 0;
1678 1.7 martin pset->infos = ninfo;
1679 1.7 martin off = pset->parts->num_part;
1680 1.7 martin cnt = pset->num-pset->parts->num_part;
1681 1.7 martin if (cnt > 0)
1682 1.7 martin memmove(pset->infos+off+1,pset->infos+off,
1683 1.7 martin cnt*sizeof(*pset->infos));
1684 1.7 martin memset(pset->infos+off, 0, sizeof(*pset->infos));
1685 1.7 martin
1686 1.7 martin nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
1687 1.7 martin if (nmenopts == NULL)
1688 1.7 martin return 0;
1689 1.7 martin memmove(nmenopts+off+1, nmenopts+off,
1690 1.7 martin (m->numopts-off)*sizeof(*nmenopts));
1691 1.7 martin memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
1692 1.7 martin nmenopts[off].opt_action = edit_ptn;
1693 1.7 martin pset->menu_opts = m->opts = nmenopts;
1694 1.7 martin m->numopts++;
1695 1.7 martin m->cursel = off;
1696 1.7 martin pset->num++;
1697 1.7 martin
1698 1.7 martin /* now update edit menu to fit */
1699 1.7 martin m->h = 0;
1700 1.7 martin resize_menu_height(m);
1701 1.7 martin
1702 1.7 martin /* and directly invoke the partition editor for the new part */
1703 1.7 martin edit_ptn(m, arg);
1704 1.7 martin
1705 1.7 martin show_partition_adder(m, pset);
1706 1.7 martin
1707 1.7 martin return -1;
1708 1.7 martin }
1709 1.7 martin
1710 1.1 dholland static void
1711 1.7 martin add_partition_adder(menudesc *m, struct partition_usage_set *pset)
1712 1.1 dholland {
1713 1.7 martin struct part_usage_info *ninfo;
1714 1.7 martin menu_ent *nmenopts;
1715 1.7 martin size_t off;
1716 1.7 martin
1717 1.7 martin ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
1718 1.7 martin if (ninfo == NULL)
1719 1.7 martin return;
1720 1.7 martin pset->infos = ninfo;
1721 1.7 martin off = pset->parts->num_part+1;
1722 1.1 dholland
1723 1.7 martin nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
1724 1.7 martin if (nmenopts == NULL)
1725 1.7 martin return;
1726 1.7 martin memmove(nmenopts+off+1, nmenopts+off,
1727 1.7 martin (m->numopts-off)*sizeof(*nmenopts));
1728 1.7 martin memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
1729 1.7 martin
1730 1.7 martin nmenopts[off].opt_name = MSG_addpart;
1731 1.7 martin nmenopts[off].opt_flags = OPT_SUB;
1732 1.7 martin nmenopts[off].opt_action = edit_fspart_add;
1733 1.7 martin
1734 1.7 martin m->opts = nmenopts;
1735 1.7 martin m->numopts++;
1736 1.7 martin }
1737 1.1 dholland
1738 1.7 martin static void
1739 1.7 martin remove_partition_adder(menudesc *m, struct partition_usage_set *pset)
1740 1.7 martin {
1741 1.7 martin size_t off;
1742 1.1 dholland
1743 1.7 martin off = pset->parts->num_part+1;
1744 1.7 martin memmove(m->opts+off, m->opts+off+1,
1745 1.7 martin (m->numopts-off-1)*sizeof(*m->opts));
1746 1.7 martin m->numopts--;
1747 1.1 dholland }
1748 1.1 dholland
1749 1.1 dholland /*
1750 1.7 martin * Called whenever the "add a partition" option may need to be removed
1751 1.7 martin * or added
1752 1.1 dholland */
1753 1.7 martin static void
1754 1.7 martin show_partition_adder(menudesc *m, struct partition_usage_set *pset)
1755 1.1 dholland {
1756 1.10.2.5 msaitoh if (m->opts == NULL)
1757 1.10.2.5 msaitoh return;
1758 1.10.2.5 msaitoh
1759 1.7 martin bool can_add_partition = pset->parts->pscheme->can_add_partition(
1760 1.7 martin pset->parts);
1761 1.7 martin bool part_adder_present =
1762 1.7 martin (m->opts[pset->parts->num_part].opt_flags & OPT_IGNORE) &&
1763 1.7 martin (m->opts[pset->parts->num_part+1].opt_action == edit_fspart_add);
1764 1.1 dholland
1765 1.7 martin if (can_add_partition == part_adder_present)
1766 1.7 martin return;
1767 1.1 dholland
1768 1.7 martin if (can_add_partition)
1769 1.7 martin add_partition_adder(m, pset);
1770 1.7 martin else
1771 1.7 martin remove_partition_adder(m, pset);
1772 1.1 dholland
1773 1.7 martin /* now update edit menu to fit */
1774 1.7 martin m->h = 0;
1775 1.7 martin resize_menu_height(m);
1776 1.7 martin }
1777 1.1 dholland
1778 1.7 martin static int
1779 1.7 martin edit_fspart_abort(menudesc *m, void *arg)
1780 1.7 martin {
1781 1.7 martin struct partition_usage_set *pset = arg;
1782 1.1 dholland
1783 1.7 martin pset->ok = false;
1784 1.7 martin return 1;
1785 1.1 dholland }
1786 1.1 dholland
1787 1.1 dholland /*
1788 1.7 martin * Check a disklabel.
1789 1.7 martin * If there are overlapping active partitions,
1790 1.7 martin * Ask the user if they want to edit the partition or give up.
1791 1.1 dholland */
1792 1.1 dholland int
1793 1.10.2.5 msaitoh edit_and_check_label(struct pm_devs *p, struct partition_usage_set *pset,
1794 1.10.2.5 msaitoh bool install)
1795 1.1 dholland {
1796 1.7 martin menu_ent *op;
1797 1.7 martin size_t cnt, i;
1798 1.7 martin bool may_add = pset->parts->pscheme->can_add_partition(pset->parts);
1799 1.7 martin bool may_edit_pack =
1800 1.7 martin pset->parts->pscheme->get_disk_pack_name != NULL &&
1801 1.7 martin pset->parts->pscheme->set_disk_pack_name != NULL;
1802 1.7 martin
1803 1.10.2.3 msaitoh #ifdef NO_CLONES
1804 1.10.2.3 msaitoh #define C_M_ITEMS 0
1805 1.10.2.3 msaitoh #else
1806 1.10.2.3 msaitoh #define C_M_ITEMS 1
1807 1.10.2.3 msaitoh #endif
1808 1.7 martin pset->menu_opts = calloc(pset->parts->num_part
1809 1.10.2.3 msaitoh +3+C_M_ITEMS+may_add+may_edit_pack,
1810 1.7 martin sizeof *pset->menu_opts);
1811 1.7 martin if (pset->menu_opts == NULL)
1812 1.7 martin return 0;
1813 1.1 dholland
1814 1.7 martin op = pset->menu_opts;
1815 1.7 martin for (i = 0; i < pset->parts->num_part; i++) {
1816 1.7 martin op->opt_action = edit_ptn;
1817 1.7 martin op++;
1818 1.7 martin }
1819 1.7 martin /* separator line between partitions and actions */
1820 1.7 martin op->opt_name = fspart_separator;
1821 1.7 martin op->opt_flags = OPT_IGNORE|OPT_NOSHORT;
1822 1.7 martin op++;
1823 1.7 martin
1824 1.7 martin /* followed by new partition adder */
1825 1.7 martin if (may_add) {
1826 1.7 martin op->opt_name = MSG_addpart;
1827 1.7 martin op->opt_flags = OPT_SUB;
1828 1.7 martin op->opt_action = edit_fspart_add;
1829 1.7 martin op++;
1830 1.7 martin }
1831 1.7 martin
1832 1.7 martin /* and unit changer */
1833 1.7 martin op->opt_name = MSG_askunits;
1834 1.7 martin op->opt_menu = MENU_sizechoice;
1835 1.7 martin op->opt_flags = OPT_SUB;
1836 1.7 martin op->opt_action = NULL;
1837 1.7 martin op++;
1838 1.7 martin
1839 1.7 martin if (may_edit_pack) {
1840 1.7 martin op->opt_name = MSG_editpack;
1841 1.7 martin op->opt_flags = OPT_SUB;
1842 1.7 martin op->opt_action = edit_fspart_pack;
1843 1.7 martin op++;
1844 1.7 martin }
1845 1.10.2.3 msaitoh
1846 1.10.2.3 msaitoh #ifndef NO_CLONES
1847 1.10.2.3 msaitoh /* add a clone-from-elsewhere option */
1848 1.10.2.3 msaitoh op->opt_name = MSG_clone_from_elsewhere;
1849 1.10.2.3 msaitoh op->opt_action = part_ext_clone;
1850 1.10.2.3 msaitoh op++;
1851 1.10.2.3 msaitoh #endif
1852 1.7 martin
1853 1.7 martin /* and abort option */
1854 1.7 martin op->opt_name = MSG_cancel;
1855 1.7 martin op->opt_flags = OPT_EXIT;
1856 1.7 martin op->opt_action = edit_fspart_abort;
1857 1.7 martin op++;
1858 1.7 martin cnt = op - pset->menu_opts;
1859 1.10.2.3 msaitoh assert(cnt == pset->parts->num_part+3+C_M_ITEMS+may_add+may_edit_pack);
1860 1.7 martin
1861 1.7 martin pset->menu = new_menu(fspart_title, pset->menu_opts, cnt,
1862 1.7 martin 0, -1, 0, 74,
1863 1.7 martin MC_ALWAYS_SCROLL|MC_NOBOX|MC_DFLTEXIT|
1864 1.7 martin MC_NOCLEAR|MC_CONTINUOUS,
1865 1.7 martin fmt_fspart_header, fmt_fspart_row, NULL, NULL,
1866 1.7 martin MSG_partition_sizes_ok);
1867 1.7 martin
1868 1.7 martin if (pset->menu < 0) {
1869 1.7 martin free(pset->menu_opts);
1870 1.7 martin pset->menu_opts = NULL;
1871 1.7 martin return 0;
1872 1.7 martin }
1873 1.1 dholland
1874 1.7 martin p->current_cylsize = p->dlcylsize;
1875 1.1 dholland
1876 1.7 martin for (;;) {
1877 1.7 martin /* first give the user the option to edit the label... */
1878 1.7 martin pset->ok = true;
1879 1.7 martin process_menu(pset->menu, pset);
1880 1.7 martin if (!pset->ok) {
1881 1.7 martin i = 0;
1882 1.7 martin break;
1883 1.7 martin }
1884 1.1 dholland
1885 1.7 martin /* User thinks the label is OK. */
1886 1.10.2.5 msaitoh i = verify_parts(pset, install);
1887 1.7 martin if (i == 1)
1888 1.7 martin continue;
1889 1.7 martin break;
1890 1.1 dholland }
1891 1.7 martin free(pset->menu_opts);
1892 1.7 martin pset->menu_opts = NULL;
1893 1.7 martin free_menu(pset->menu);
1894 1.7 martin pset->menu = -1;
1895 1.1 dholland
1896 1.7 martin return i != 0;
1897 1.1 dholland }
1898 1.1 dholland
1899 1.1 dholland /*
1900 1.10 martin * strip trailing / to avoid confusion in path comparisions later
1901 1.10 martin */
1902 1.10 martin void
1903 1.10 martin canonicalize_last_mounted(char *path)
1904 1.10 martin {
1905 1.10 martin char *p;
1906 1.10 martin
1907 1.10.2.1 msaitoh if (path == NULL)
1908 1.10.2.1 msaitoh return;
1909 1.10.2.1 msaitoh
1910 1.10.2.1 msaitoh if (strcmp(path, "/") == 0)
1911 1.10.2.1 msaitoh return; /* in this case a "trailing" slash is allowed */
1912 1.10.2.1 msaitoh
1913 1.10 martin for (;;) {
1914 1.10 martin p = strrchr(path, '/');
1915 1.10 martin if (p == NULL)
1916 1.10 martin return;
1917 1.10 martin if (p[1] != 0)
1918 1.10 martin return;
1919 1.10 martin p[0] = 0;
1920 1.10 martin }
1921 1.10 martin }
1922 1.10 martin
1923 1.10 martin /*
1924 1.1 dholland * Try to get 'last mounted on' (or equiv) from fs superblock.
1925 1.1 dholland */
1926 1.1 dholland const char *
1927 1.7 martin get_last_mounted(int fd, daddr_t partstart, uint *fs_type, uint *fs_sub_type,
1928 1.7 martin uint flags)
1929 1.1 dholland {
1930 1.1 dholland static char sblk[SBLOCKSIZE]; /* is this enough? */
1931 1.1 dholland struct fs *SB = (struct fs *)sblk;
1932 1.7 martin static const off_t sblocks[] = SBLOCKSEARCH;
1933 1.7 martin const off_t *sbp;
1934 1.1 dholland const char *mnt = NULL;
1935 1.1 dholland int len;
1936 1.1 dholland
1937 1.1 dholland if (fd == -1)
1938 1.1 dholland return "";
1939 1.1 dholland
1940 1.7 martin if (fs_type)
1941 1.7 martin *fs_type = 0;
1942 1.7 martin if (fs_sub_type)
1943 1.7 martin *fs_sub_type = 0;
1944 1.7 martin
1945 1.1 dholland /* Check UFS1/2 (and hence LFS) superblock */
1946 1.1 dholland for (sbp = sblocks; mnt == NULL && *sbp != -1; sbp++) {
1947 1.1 dholland if (pread(fd, sblk, sizeof sblk,
1948 1.7 martin (off_t)partstart * (off_t)512 + *sbp) != sizeof sblk)
1949 1.7 martin continue;
1950 1.7 martin
1951 1.7 martin /*
1952 1.7 martin * If start of partition and allowed by flags check
1953 1.7 martin * for other fs types
1954 1.7 martin */
1955 1.7 martin if (*sbp == 0 && (flags & GLM_MAYBE_FAT32) &&
1956 1.7 martin sblk[0x42] == 0x29 && memcmp(sblk + 0x52, "FAT", 3) == 0) {
1957 1.7 martin /* Probably a FAT filesystem, report volume name */
1958 1.7 martin size_t i;
1959 1.7 martin for (i = 0x51; i >= 0x47; i--) {
1960 1.7 martin if (sblk[i] != ' ')
1961 1.7 martin break;
1962 1.7 martin sblk[i] = 0;
1963 1.7 martin }
1964 1.7 martin sblk[0x52] = 0;
1965 1.7 martin if (fs_type)
1966 1.7 martin *fs_type = FS_MSDOS;
1967 1.7 martin if (fs_sub_type)
1968 1.7 martin *fs_sub_type = sblk[0x53];
1969 1.7 martin return sblk + 0x47;
1970 1.7 martin } else if (*sbp == 0 && (flags & GLM_MAYBE_NTFS) &&
1971 1.7 martin memcmp(sblk+3, "NTFS ", 8) == 0) {
1972 1.7 martin if (fs_type)
1973 1.7 martin *fs_type = FS_NTFS;
1974 1.7 martin if (fs_sub_type)
1975 1.7 martin *fs_sub_type = MBR_PTYPE_NTFS;
1976 1.7 martin /* XXX dig for volume name attribute ? */
1977 1.7 martin return "";
1978 1.7 martin }
1979 1.7 martin
1980 1.7 martin if (!(flags & GLM_LIKELY_FFS))
1981 1.1 dholland continue;
1982 1.7 martin
1983 1.1 dholland /* Maybe we should validate the checksum??? */
1984 1.1 dholland switch (SB->fs_magic) {
1985 1.1 dholland case FS_UFS1_MAGIC:
1986 1.1 dholland case FS_UFS1_MAGIC_SWAPPED:
1987 1.1 dholland if (!(SB->fs_old_flags & FS_FLAGS_UPDATED)) {
1988 1.1 dholland if (*sbp == SBLOCK_UFS1)
1989 1.1 dholland mnt = (const char *)SB->fs_fsmnt;
1990 1.1 dholland } else {
1991 1.1 dholland /* Check we have the main superblock */
1992 1.1 dholland if (SB->fs_sblockloc == *sbp)
1993 1.1 dholland mnt = (const char *)SB->fs_fsmnt;
1994 1.1 dholland }
1995 1.7 martin if (fs_type)
1996 1.7 martin *fs_type = FS_BSDFFS;
1997 1.7 martin if (fs_sub_type)
1998 1.10.2.2 msaitoh *fs_sub_type = 1;
1999 1.1 dholland continue;
2000 1.1 dholland case FS_UFS2_MAGIC:
2001 1.1 dholland case FS_UFS2_MAGIC_SWAPPED:
2002 1.1 dholland /* Check we have the main superblock */
2003 1.1 dholland if (SB->fs_sblockloc == *sbp) {
2004 1.1 dholland mnt = (const char *)SB->fs_fsmnt;
2005 1.7 martin if (fs_type)
2006 1.7 martin *fs_type = FS_BSDFFS;
2007 1.7 martin if (fs_sub_type)
2008 1.7 martin *fs_sub_type = 2;
2009 1.1 dholland }
2010 1.1 dholland continue;
2011 1.1 dholland }
2012 1.1 dholland }
2013 1.1 dholland
2014 1.1 dholland if (mnt == NULL)
2015 1.1 dholland return "";
2016 1.1 dholland
2017 1.1 dholland /* If sysinst mounted this last then strip prefix */
2018 1.1 dholland len = strlen(targetroot_mnt);
2019 1.1 dholland if (memcmp(mnt, targetroot_mnt, len) == 0) {
2020 1.1 dholland if (mnt[len] == 0)
2021 1.1 dholland return "/";
2022 1.1 dholland if (mnt[len] == '/')
2023 1.1 dholland return mnt + len;
2024 1.1 dholland }
2025 1.1 dholland return mnt;
2026 1.1 dholland #undef SB
2027 1.1 dholland }
2028 1.1 dholland
2029 1.1 dholland /* Ask for a partition offset, check bounds and do the needed roundups */
2030 1.7 martin daddr_t
2031 1.7 martin getpartoff(struct disk_partitions *parts, daddr_t defpartstart)
2032 1.1 dholland {
2033 1.7 martin char defstart[24], isize[24], maxpart, minspace, maxspace,
2034 1.7 martin *prompt, *label_msg, valid_parts[4], valid_spaces[4],
2035 1.7 martin space_prompt[1024], *head, *hint_part, *hint_space, *tail;
2036 1.7 martin size_t num_freespace, spaces, ndx;
2037 1.7 martin struct disk_part_free_space *freespace;
2038 1.7 martin daddr_t i, localsizemult, ptn_alignment, min, max;
2039 1.7 martin part_id partn;
2040 1.7 martin struct disk_part_info info;
2041 1.7 martin const char *errmsg = NULL;
2042 1.7 martin
2043 1.7 martin min = parts->disk_start;
2044 1.7 martin max = min + parts->disk_size;
2045 1.7 martin
2046 1.7 martin /* upper bound on the number of free spaces, plus some slope */
2047 1.7 martin num_freespace = parts->num_part * 2 + 5;
2048 1.7 martin freespace = calloc(num_freespace, sizeof(*freespace));
2049 1.7 martin if (freespace == NULL)
2050 1.7 martin return -1;
2051 1.7 martin
2052 1.7 martin ptn_alignment = parts->pscheme->get_part_alignment(parts);
2053 1.7 martin spaces = parts->pscheme->get_free_spaces(parts, freespace,
2054 1.7 martin num_freespace, max(sizemult, ptn_alignment), ptn_alignment, -1,
2055 1.7 martin defpartstart);
2056 1.7 martin
2057 1.7 martin maxpart = 'a' + parts->num_part -1;
2058 1.7 martin if (parts->num_part > 1) {
2059 1.7 martin snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpart);
2060 1.7 martin } else if (parts->num_part == 1) {
2061 1.7 martin snprintf(valid_parts, sizeof valid_parts, " %c", maxpart);
2062 1.7 martin } else {
2063 1.7 martin strcpy(valid_parts, "---");
2064 1.7 martin }
2065 1.7 martin if (spaces > 1) {
2066 1.7 martin minspace = maxpart + 1;
2067 1.7 martin maxspace = minspace + spaces -1;
2068 1.7 martin snprintf(valid_spaces, sizeof valid_spaces, "%c-%c", minspace,
2069 1.7 martin maxspace);
2070 1.7 martin } else if (spaces == 1) {
2071 1.7 martin maxspace = minspace = maxpart + 1;
2072 1.7 martin snprintf(valid_spaces, sizeof valid_spaces, " %c", minspace);
2073 1.7 martin } else {
2074 1.7 martin minspace = 0;
2075 1.7 martin maxspace = 0;
2076 1.7 martin strcpy(valid_spaces, "---");
2077 1.7 martin }
2078 1.7 martin
2079 1.7 martin /* Add description of start/size to user prompt */
2080 1.7 martin const char *mstr = msg_string(MSG_free_space_line);
2081 1.7 martin space_prompt[0] = 0;
2082 1.7 martin for (ndx = 0; ndx < spaces; ndx++) {
2083 1.7 martin char str_start[40], str_end[40], str_size[40], str_tag[4];
2084 1.7 martin
2085 1.7 martin sprintf(str_tag, "%c: ", minspace+(int)ndx);
2086 1.7 martin sprintf(str_start, "%" PRIu64, freespace[ndx].start / sizemult);
2087 1.7 martin sprintf(str_end, "%" PRIu64,
2088 1.7 martin (freespace[ndx].start + freespace[ndx].size) / sizemult);
2089 1.7 martin sprintf(str_size, "%" PRIu64, freespace[ndx].size / sizemult);
2090 1.7 martin const char *args[4] = { str_start, str_end, str_size,
2091 1.7 martin multname };
2092 1.7 martin char *line = str_arg_subst(mstr, 4, args);
2093 1.7 martin strlcat(space_prompt, str_tag, sizeof(space_prompt));
2094 1.7 martin size_t len = strlcat(space_prompt, line, sizeof(space_prompt));
2095 1.7 martin free (line);
2096 1.7 martin if (len >= sizeof space_prompt)
2097 1.7 martin break;
2098 1.7 martin }
2099 1.7 martin
2100 1.7 martin const char *args[] = { valid_parts, valid_spaces, multname };
2101 1.7 martin hint_part = NULL;
2102 1.7 martin hint_space = NULL;
2103 1.7 martin head = str_arg_subst(msg_string(MSG_label_offset_head),
2104 1.7 martin __arraycount(args), args);
2105 1.7 martin if (parts->num_part)
2106 1.7 martin hint_part = str_arg_subst(msg_string(
2107 1.7 martin MSG_label_offset_part_hint), __arraycount(args), args);
2108 1.7 martin if (spaces)
2109 1.7 martin hint_space = str_arg_subst(msg_string(
2110 1.7 martin MSG_label_offset_space_hint), __arraycount(args), args);
2111 1.7 martin tail = str_arg_subst(msg_string(MSG_label_offset_tail),
2112 1.7 martin __arraycount(args), args);
2113 1.7 martin
2114 1.7 martin if (hint_part && hint_space)
2115 1.7 martin asprintf(&label_msg, "%s\n%s\n%s\n\n%s\n%s",
2116 1.7 martin head, hint_part, hint_space, space_prompt, tail);
2117 1.7 martin else if (hint_part)
2118 1.7 martin asprintf(&label_msg, "%s\n%s\n\n%s",
2119 1.7 martin head, hint_part, tail);
2120 1.7 martin else if (hint_space)
2121 1.7 martin asprintf(&label_msg, "%s\n%s\n\n%s\n%s",
2122 1.7 martin head, hint_space, space_prompt, tail);
2123 1.7 martin else
2124 1.7 martin asprintf(&label_msg, "%s\n\n%s",
2125 1.7 martin head, tail);
2126 1.7 martin free(head); free(hint_part); free(hint_space); free(tail);
2127 1.1 dholland
2128 1.7 martin localsizemult = sizemult;
2129 1.7 martin errmsg = NULL;
2130 1.1 dholland for (;;) {
2131 1.7 martin snprintf(defstart, sizeof defstart, "%" PRIu64,
2132 1.7 martin defpartstart/sizemult);
2133 1.7 martin if (errmsg != NULL && errmsg[0] != 0)
2134 1.7 martin asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
2135 1.7 martin else
2136 1.7 martin prompt = label_msg;
2137 1.7 martin msg_prompt_win(prompt, -1, 13, 70, -1,
2138 1.7 martin (defpartstart > 0) ? defstart : NULL, isize, sizeof isize);
2139 1.7 martin if (label_msg != prompt)
2140 1.7 martin free(prompt);
2141 1.7 martin if (strcmp(defstart, isize) == 0) {
2142 1.1 dholland /* Don't do rounding if default accepted */
2143 1.7 martin i = defpartstart;
2144 1.7 martin break;
2145 1.7 martin }
2146 1.1 dholland if (isize[1] == '\0' && isize[0] >= 'a' &&
2147 1.7 martin isize[0] <= maxpart) {
2148 1.1 dholland partn = isize[0] - 'a';
2149 1.7 martin if (parts->pscheme->get_part_info(parts, partn,
2150 1.7 martin &info)) {
2151 1.7 martin i = info.start + info.size;
2152 1.7 martin localsizemult = 1;
2153 1.7 martin } else {
2154 1.7 martin errmsg = msg_string(MSG_invalid_sector_number);
2155 1.7 martin continue;
2156 1.7 martin }
2157 1.7 martin } else if (isize[1] == '\0' && isize[0] >= minspace &&
2158 1.7 martin isize[0] <= maxspace) {
2159 1.7 martin ndx = isize[0] - minspace;
2160 1.7 martin i = freespace[ndx].start;
2161 1.1 dholland localsizemult = 1;
2162 1.1 dholland } else if (atoi(isize) == -1) {
2163 1.7 martin i = min;
2164 1.1 dholland localsizemult = 1;
2165 1.1 dholland } else {
2166 1.10.2.5 msaitoh i = parse_disk_pos(isize, &localsizemult,
2167 1.10.2.5 msaitoh parts->bytes_per_sector,
2168 1.10.2.5 msaitoh parts->pscheme->get_cylinder_size(parts), NULL);
2169 1.7 martin if (i < 0) {
2170 1.1 dholland errmsg = msg_string(MSG_invalid_sector_number);
2171 1.1 dholland continue;
2172 1.1 dholland }
2173 1.1 dholland }
2174 1.1 dholland /* round to cylinder size if localsizemult != 1 */
2175 1.10.2.5 msaitoh int cylsize = parts->pscheme->get_cylinder_size(parts);
2176 1.10.2.5 msaitoh i = NUMSEC(i, localsizemult, cylsize);
2177 1.1 dholland /* Adjust to start of slice if needed */
2178 1.7 martin if ((i < min && (min - i) < localsizemult) ||
2179 1.7 martin (i > min && (i - min) < localsizemult)) {
2180 1.7 martin i = min;
2181 1.1 dholland }
2182 1.7 martin if (max == 0 || i <= max)
2183 1.1 dholland break;
2184 1.1 dholland errmsg = msg_string(MSG_startoutsidedisk);
2185 1.1 dholland }
2186 1.7 martin free(label_msg);
2187 1.7 martin free(freespace);
2188 1.7 martin
2189 1.1 dholland return i;
2190 1.1 dholland }
2191 1.1 dholland
2192 1.1 dholland
2193 1.1 dholland /* Ask for a partition size, check bounds and do the needed roundups */
2194 1.7 martin daddr_t
2195 1.10.2.6 bouyer getpartsize(struct disk_partitions *parts, daddr_t orig_start,
2196 1.10.2.6 bouyer daddr_t partstart, daddr_t dflt)
2197 1.1 dholland {
2198 1.7 martin char dsize[24], isize[24], max_size[24], maxpartc, valid_parts[4],
2199 1.7 martin *label_msg, *prompt, *head, *hint, *tail;
2200 1.7 martin const char *errmsg = NULL;
2201 1.10.2.5 msaitoh daddr_t i, partend, diskend, localsizemult, max, max_r, dflt_r;
2202 1.7 martin struct disk_part_info info;
2203 1.7 martin part_id partn;
2204 1.7 martin
2205 1.10.2.5 msaitoh diskend = parts->disk_start + parts->disk_size;
2206 1.10.2.6 bouyer max = parts->pscheme->max_free_space_at(parts, orig_start);
2207 1.10.2.6 bouyer max += orig_start - partstart;
2208 1.10.2.6 bouyer if (sizemult == 1)
2209 1.10.2.6 bouyer max--; /* with hugher scale proper rounding later will be ok */
2210 1.7 martin
2211 1.7 martin /* We need to keep both the unrounded and rounded (_r) max and dflt */
2212 1.7 martin dflt_r = (partstart + dflt) / sizemult - partstart / sizemult;
2213 1.7 martin if (max == dflt)
2214 1.7 martin max_r = dflt_r;
2215 1.7 martin else
2216 1.7 martin max_r = max / sizemult;
2217 1.7 martin /* the partition may have been moved and now not fit any longer */
2218 1.7 martin if (dflt > max)
2219 1.7 martin dflt = max;
2220 1.7 martin if (dflt_r > max_r)
2221 1.7 martin dflt_r = max_r;
2222 1.7 martin
2223 1.7 martin snprintf(max_size, sizeof max_size, "%" PRIu64, max_r);
2224 1.7 martin
2225 1.7 martin maxpartc = 'a' + parts->num_part -1;
2226 1.7 martin if (parts->num_part > 1) {
2227 1.7 martin snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpartc);
2228 1.7 martin } else if (parts->num_part == 1) {
2229 1.7 martin snprintf(valid_parts, sizeof valid_parts, " %c", maxpartc);
2230 1.7 martin } else {
2231 1.7 martin strcpy(valid_parts, "---");
2232 1.7 martin }
2233 1.7 martin
2234 1.7 martin const char *args[] = { valid_parts, max_size, multname };
2235 1.7 martin hint = NULL;
2236 1.7 martin head = str_arg_subst(msg_string(MSG_label_size_head),
2237 1.7 martin __arraycount(args), args);
2238 1.7 martin if (parts->num_part)
2239 1.7 martin hint = str_arg_subst(msg_string(MSG_label_size_part_hint),
2240 1.7 martin __arraycount(args), args);
2241 1.7 martin tail = str_arg_subst(msg_string(MSG_label_size_tail),
2242 1.7 martin __arraycount(args), args);
2243 1.7 martin
2244 1.7 martin if (hint != NULL)
2245 1.7 martin asprintf(&label_msg, "%s\n%s\n\n%s", head, hint, tail);
2246 1.7 martin else
2247 1.7 martin asprintf(&label_msg, "%s\n\n%s", head, tail);
2248 1.7 martin free(head); free(hint); free(tail);
2249 1.1 dholland
2250 1.7 martin localsizemult = sizemult;
2251 1.7 martin i = -1;
2252 1.1 dholland for (;;) {
2253 1.7 martin snprintf(dsize, sizeof dsize, "%" PRIu64, dflt_r);
2254 1.7 martin
2255 1.7 martin if (errmsg != NULL && errmsg[0] != 0)
2256 1.7 martin asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
2257 1.7 martin else
2258 1.7 martin prompt = label_msg;
2259 1.7 martin msg_prompt_win(prompt, -1, 12, 70, -1,
2260 1.7 martin (dflt != 0) ? dsize : 0, isize, sizeof isize);
2261 1.7 martin if (prompt != label_msg)
2262 1.7 martin free(prompt);
2263 1.7 martin
2264 1.7 martin if (strcmp(isize, dsize) == 0) {
2265 1.7 martin free(label_msg);
2266 1.7 martin return dflt;
2267 1.7 martin }
2268 1.7 martin if (parts->num_part && isize[1] == '\0' && isize[0] >= 'a' &&
2269 1.1 dholland isize[0] <= maxpartc) {
2270 1.1 dholland partn = isize[0] - 'a';
2271 1.7 martin if (parts->pscheme->get_part_info(parts, partn,
2272 1.7 martin &info)) {
2273 1.7 martin i = info.start - partstart -1;
2274 1.7 martin localsizemult = 1;
2275 1.7 martin max_r = max;
2276 1.7 martin }
2277 1.1 dholland } else if (atoi(isize) == -1) {
2278 1.7 martin i = max;
2279 1.1 dholland localsizemult = 1;
2280 1.7 martin max_r = max;
2281 1.1 dholland } else {
2282 1.7 martin i = parse_disk_pos(isize, &localsizemult,
2283 1.10.2.5 msaitoh parts->bytes_per_sector,
2284 1.10.2.5 msaitoh parts->pscheme->get_cylinder_size(parts), NULL);
2285 1.7 martin if (localsizemult != sizemult)
2286 1.7 martin max_r = max;
2287 1.7 martin }
2288 1.7 martin if (i < 0) {
2289 1.7 martin errmsg = msg_string(MSG_Invalid_numeric);
2290 1.7 martin continue;
2291 1.7 martin } else if (i > max_r) {
2292 1.7 martin errmsg = msg_string(MSG_Too_large);
2293 1.7 martin continue;
2294 1.1 dholland }
2295 1.1 dholland /*
2296 1.1 dholland * partend is aligned to a cylinder if localsizemult
2297 1.1 dholland * is not 1 sector
2298 1.1 dholland */
2299 1.10.2.5 msaitoh int cylsize = parts->pscheme->get_cylinder_size(parts);
2300 1.7 martin partend = NUMSEC((partstart + i*localsizemult) / localsizemult,
2301 1.10.2.5 msaitoh localsizemult, cylsize);
2302 1.1 dholland /* Align to end-of-disk or end-of-slice if close enough */
2303 1.10.2.5 msaitoh if (partend > (diskend - sizemult)
2304 1.10.2.5 msaitoh && partend < (diskend + sizemult))
2305 1.10.2.5 msaitoh partend = diskend;
2306 1.7 martin if (partend > (partstart + max - sizemult)
2307 1.7 martin && partend < (partstart + max + sizemult))
2308 1.7 martin partend = partstart + max;
2309 1.1 dholland /* sanity checks */
2310 1.10.2.5 msaitoh if (partend > diskend) {
2311 1.10.2.5 msaitoh partend = diskend;
2312 1.7 martin errmsg = msg_string(MSG_endoutsidedisk);
2313 1.7 martin continue;
2314 1.1 dholland }
2315 1.7 martin free(label_msg);
2316 1.1 dholland if (partend < partstart)
2317 1.1 dholland return 0;
2318 1.1 dholland return (partend - partstart);
2319 1.1 dholland }
2320 1.1 dholland /* NOTREACHED */
2321 1.1 dholland }
2322 1.1 dholland
2323 1.1 dholland /*
2324 1.1 dholland * convert a string to a number of sectors, with a possible unit
2325 1.1 dholland * 150M = 150 Megabytes
2326 1.1 dholland * 2000c = 2000 cylinders
2327 1.1 dholland * 150256s = 150256 sectors
2328 1.7 martin * Without units, use the default (sizemult).
2329 1.7 martin * returns the raw input value, and the unit used. Caller needs to multiply!
2330 1.7 martin * On invalid inputs, returns -1.
2331 1.1 dholland */
2332 1.7 martin daddr_t
2333 1.7 martin parse_disk_pos(
2334 1.7 martin const char *str,
2335 1.7 martin daddr_t *localsizemult,
2336 1.10.2.5 msaitoh daddr_t bps,
2337 1.7 martin daddr_t cyl_size,
2338 1.7 martin bool *extend_this)
2339 1.1 dholland {
2340 1.7 martin daddr_t val;
2341 1.7 martin char *cp;
2342 1.7 martin bool mult_found;
2343 1.1 dholland
2344 1.1 dholland if (str[0] == '\0') {
2345 1.7 martin return -1;
2346 1.1 dholland }
2347 1.7 martin val = strtoull(str, &cp, 10);
2348 1.7 martin mult_found = false;
2349 1.7 martin if (extend_this)
2350 1.7 martin *extend_this = false;
2351 1.7 martin while (*cp != 0) {
2352 1.7 martin if (*cp == 'G' || *cp == 'g') {
2353 1.7 martin if (mult_found)
2354 1.7 martin return -1;
2355 1.10.2.5 msaitoh *localsizemult = GIG / bps;
2356 1.7 martin goto next;
2357 1.1 dholland }
2358 1.7 martin if (*cp == 'M' || *cp == 'm') {
2359 1.7 martin if (mult_found)
2360 1.7 martin return -1;
2361 1.10.2.5 msaitoh *localsizemult = MEG / bps;
2362 1.7 martin goto next;
2363 1.1 dholland }
2364 1.7 martin if (*cp == 'c' || *cp == 'C') {
2365 1.7 martin if (mult_found)
2366 1.7 martin return -1;
2367 1.10.2.5 msaitoh *localsizemult = cyl_size;
2368 1.7 martin goto next;
2369 1.1 dholland }
2370 1.7 martin if (*cp == 's' || *cp == 'S') {
2371 1.7 martin if (mult_found)
2372 1.7 martin return -1;
2373 1.1 dholland *localsizemult = 1;
2374 1.7 martin goto next;
2375 1.7 martin }
2376 1.7 martin if (*cp == '+' && extend_this) {
2377 1.7 martin *extend_this = true;
2378 1.7 martin cp++;
2379 1.1 dholland break;
2380 1.1 dholland }
2381 1.7 martin
2382 1.1 dholland /* not a known unit */
2383 1.7 martin return -1;
2384 1.7 martin
2385 1.7 martin next:
2386 1.7 martin mult_found = true;
2387 1.7 martin cp++;
2388 1.7 martin continue;
2389 1.1 dholland }
2390 1.7 martin if (*cp != 0)
2391 1.7 martin return -1;
2392 1.7 martin
2393 1.7 martin return val;
2394 1.1 dholland }
2395