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