Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
/*	$NetBSD: bsddisklabel.c,v 1.23.2.11 2020/10/15 19:36:50 bouyer Exp $	*/

/*
 * Copyright 1997 Piermont Information Systems Inc.
 * All rights reserved.
 *
 * Based on code written by Philip A. Nelson for Piermont Information
 * Systems Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of Piermont Information Systems Inc. may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

/* bsddisklabel.c -- generate standard BSD disklabel */
/* Included by appropriate arch/XXXX/md.c */

#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/exec.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <machine/cpu.h>
#include <assert.h>
#include <stdio.h>
#include <stddef.h>
#include <util.h>
#include <dirent.h>
#include "defs.h"
#include "md.h"
#include "defsizes.h"
#include "endian.h"
#include "msg_defs.h"
#include "menu_defs.h"

static size_t fill_ptn_menu(struct partition_usage_set *pset);

/*
 * The default partition layout.
 */
static const struct part_usage_info
default_parts_init[] =
{
	/*
	 * Pretty complex setup for boot partitions.
	 * This is copy&pasted below, please keep in sync!
	 */
#ifdef PART_BOOT
	{ .size = PART_BOOT/512,	/* PART_BOOT is in BYTE, not MB! */
#ifdef PART_BOOT_MOUNT
	  .mount = PART_BOOT_MOUNT,
	  .instflags = PUIINST_MOUNT|PUIINST_BOOT,
#else
	  .instflags = PUIINST_BOOT,
#endif
#ifdef PART_BOOT_TYPE
	  .fs_type = PART_BOOT_TYPE,
#if (PART_BOOT_TYPE == FS_MSDOS) || (PART_BOOT_TYPE == FS_EX2FS)
	  .flags = PUIFLAG_ADD_OUTER,
#endif
#endif
#ifdef PART_BOOT_SUBT
	  .fs_version = PART_BOOT_SUBT,
#endif
	},
#endif

	/*
	 * Two more copies of above for _BOOT1 and _BOOT2, please
	 * keep in sync!
	 */
#ifdef PART_BOOT1
	{ .size = PART_BOOT1/512,	/* PART_BOOT1 is in BYTE, not MB! */
#ifdef PART_BOOT1_MOUNT
	  .mount = PART_BOOT1_MOUNT,
	  .instflags = PUIINST_MOUNT|PUIINST_BOOT,
#else
	  .instflags = PUIINST_MOUNT|PUIINST_BOOT,
#endif
#ifdef PART_BOOT1_TYPE
	  .fs_type = PART_BOOT1_TYPE,
#if (PART_BOOT1_TYPE == FS_MSDOS) || (PART_BOOT1_TYPE == FS_EX2FS)
	  .flags = PUIFLAG_ADD_OUTER,
#endif
#endif
#ifdef PART_BOOT1_SUBT
	  .fs_version = PART_BOOT1_SUBT,
#endif
	},
#endif
#ifdef PART_BOOT2
	{ .size = PART_BOOT2/512,	/* PART_BOOT2 is in BYTE, not MB! */
#ifdef PART_BOOT2_MOUNT
	  .mount = PART_BOOT2_MOUNT,
	  .instflags = PUIINST_MOUNT|PUIINST_BOOT,
#else
	  .instflags = PUIINST_MOUNT|PUIINST_BOOT,
#endif
#ifdef PART_BOOT2_TYPE
	  .fs_type = PART_BOOT2_TYPE,
#if (PART_BOOT2_TYPE == FS_MSDOS) || (PART_BOOT2_TYPE == FS_EX2FS)
	  .flags = PUIFLAG_ADD_OUTER,
#endif
#endif
#ifdef PART_BOOT2_SUBT
	  .fs_version = PART_BOOT1_SUBT,
#endif
	},
#endif

	{ .size = DEFROOTSIZE*(MEG/512), .mount = "/", .type = PT_root,
	  .flags = PUIFLAG_EXTEND },
	{
#if DEFSWAPSIZE > 0
	  .size = DEFSWAPSIZE*(MEG/512),
#endif
	  .type = PT_swap, .fs_type = FS_SWAP },
#ifdef HAVE_TMPFS
	{ .type = PT_root, .mount = "/tmp", .fs_type = FS_TMPFS,
	  .flags = PUIFLG_JUST_MOUNTPOINT },
#else
	{ .type = PT_root, .mount = "/tmp", .fs_type = FS_MFS,
	  .flags = PUIFLG_JUST_MOUNTPOINT },
#endif
	{ .def_size = DEFUSRSIZE*(MEG/512), .mount = "/usr", .type = PT_root,
	  .fs_type = FS_BSDFFS, .fs_version = 2 },
	{ .def_size = DEFVARSIZE*(MEG/512), .mount = "/var", .type = PT_root,
	  .fs_type = FS_BSDFFS, .fs_version = 2 },
};

static const char size_separator[] =
    "----------------------------------- - --------------------";
static char size_menu_title[STRSIZE];
static char size_menu_exit[MENUSTRSIZE];

static void
set_pset_exit_str(struct partition_usage_set *pset)
{
	char *str, num[25];
	const char *args[2];
	bool overrun;
	daddr_t free_space = pset->cur_free_space;

	/* format exit string */
	overrun = free_space < 0;
	if (overrun)
		free_space = -free_space;

	snprintf(num, sizeof(num), "%" PRIu64, free_space / sizemult);
	args[0] = num;
	args[1] = multname;
	str = str_arg_subst(
	    msg_string(overrun ? MSG_fssizesbad : MSG_fssizesok),
	    2, args);
	strlcpy(size_menu_exit, str, sizeof(size_menu_exit));
	free(str);
}

static void
draw_size_menu_header(menudesc *m, void *arg)
{
	struct partition_usage_set *pset = arg;
	size_t i;
	char col1[70], desc[MENUSTRSIZE];
	bool need_ext = false, need_existing = false;

	msg_display(MSG_ptnsizes);

	for (i = 0; i < pset->num; i++) {
		if (pset->infos[i].flags & PUIFLG_IS_OUTER)
			need_ext = true;
		else if (pset->infos[i].cur_part_id != NO_PART)
			need_existing = true;
	}
	if (need_ext && need_existing)
		snprintf(desc, sizeof desc, "%s, %s",
		    msg_string(MSG_ptnsizes_mark_existing),
		    msg_string(MSG_ptnsizes_mark_external));
	else if (need_existing)
		strlcpy(desc, msg_string(MSG_ptnsizes_mark_existing),
		    sizeof desc);
	else if (need_ext)
		strlcpy(desc, msg_string(MSG_ptnsizes_mark_external),
		    sizeof desc);
	if (need_ext || need_existing) {
		msg_printf("\n");
		msg_display_add_subst(msg_string(MSG_ptnsizes_markers),
		    1, &desc);
	}
	msg_printf("\n\n");

	/* update menu title */
	snprintf(col1, sizeof col1, "%s (%s)", msg_string(MSG_ptnheaders_size),
	    multname);
	snprintf(size_menu_title, sizeof size_menu_title,
	    "   %-37.37s %s\n   %s", col1,
	    msg_string(MSG_ptnheaders_filesystem), size_separator);
}

static void
draw_size_menu_line(menudesc *m, int opt, void *arg)
{
	struct partition_usage_set *pset = arg;
	daddr_t size;
	char psize[38], inc_free[16], flag, swap[40];
	const char *mount;
	bool free_mount = false;

	if (opt < 0 || (size_t)opt >= pset->num)
		return;

	inc_free[0] = 0;
	if ((pset->infos[opt].flags & PUIFLAG_EXTEND) &&
	     pset->cur_free_space > 0) {
		size = pset->infos[opt].size + pset->cur_free_space;
		snprintf(inc_free, sizeof inc_free, " (%" PRIu64 ")",
		    size / sizemult);
	}
	size = pset->infos[opt].size;
	if (pset->infos[opt].fs_type == FS_TMPFS) {
		if (pset->infos[opt].size < 0)
			snprintf(psize, sizeof psize, "%" PRIu64 "%%", -size);
		else
			snprintf(psize, sizeof psize, "%" PRIu64 " %s", size,
			    msg_string(MSG_megname));
	} else {
		snprintf(psize, sizeof psize, "%" PRIu64 "%s",
		    size / sizemult, inc_free);
	}

	if (pset->infos[opt].type == PT_swap) {
		snprintf(swap, sizeof swap, "<%s>",
		    msg_string(MSG_swap_display));
		mount = swap;
	} else if (pset->infos[opt].flags & PUIFLG_JUST_MOUNTPOINT) {
		snprintf(swap, sizeof swap, "%s (%s)",
		    pset->infos[opt].mount,
		    getfslabelname(pset->infos[opt].fs_type,
		    pset->infos[opt].fs_version));
		mount = swap;
	} else if (pset->infos[opt].mount[0]) {
		if (pset->infos[opt].instflags & PUIINST_BOOT) {
			snprintf(swap, sizeof swap, "%s <%s>",
			    pset->infos[opt].mount, msg_string(MSG_ptn_boot));
			mount = swap;
		} else {
			mount = pset->infos[opt].mount;
		}
#ifndef NO_CLONES
	} else if (pset->infos[opt].flags & PUIFLG_CLONE_PARTS) {
		snprintf(swap, sizeof swap, "%zu %s",
		    pset->infos[opt].clone_src->num_sel,
		    msg_string(MSG_clone_target_disp));
		mount = swap;
#endif
	} else {
		mount = NULL;
		if (pset->infos[opt].parts->pscheme->other_partition_identifier
		    && pset->infos[opt].cur_part_id != NO_PART)
			mount = pset->infos[opt].parts->pscheme->
			    other_partition_identifier(pset->infos[opt].parts,
			    pset->infos[opt].cur_part_id);
		if (mount == NULL)
			mount = getfslabelname(pset->infos[opt].fs_type,
			    pset->infos[opt].fs_version);
		if (pset->infos[opt].instflags & PUIINST_BOOT) {
			snprintf(swap, sizeof swap, "%s <%s>",
			    mount, msg_string(MSG_ptn_boot));
			mount = swap;
		}
		mount = str_arg_subst(msg_string(MSG_size_ptn_not_mounted),
		    1, &mount);
		free_mount = true;
	}
	flag = ' ';
	if (pset->infos[opt].flags & PUIFLAG_EXTEND)
		flag = '+';
	else if (pset->infos[opt].flags & PUIFLG_IS_OUTER)
		flag = '@';
	else if (pset->infos[opt].cur_part_id != NO_PART)
		flag = '=';
	wprintw(m->mw, "%-35.35s %c %s", psize, flag, mount);
	if (free_mount)
		free(__UNCONST(mount));

	if (opt == 0)
		set_pset_exit_str(pset);
}

static int
add_other_ptn_size(menudesc *menu, void *arg)
{
	struct partition_usage_set *pset = arg;
	struct part_usage_info *p;
	struct menu_ent *m;
	char new_mp[MOUNTLEN], *err;
	const char *args;

	for (;;) {
		msg_prompt_win(partman_go?MSG_askfsmountadv:MSG_askfsmount,
		    -1, 18, 0, 0, NULL,  new_mp, sizeof(new_mp));
		if (new_mp[0] == 0)
			return 0;
		if (new_mp[0] != '/') {
			/* we need absolute mount paths */
			memmove(new_mp+1, new_mp, sizeof(new_mp)-1);
			new_mp[0] = '/';
		}

		/* duplicates? */
		bool duplicate = false;
		for (size_t i = 0; i < pset->num; i++) {
			if (strcmp(pset->infos[i].mount,
			    new_mp) == 0) {
			    	args = new_mp;
				err = str_arg_subst(
				    msg_string(MSG_mp_already_exists),
				    1, &args);
				err_msg_win(err);
				free(err);
				duplicate = true;
				break;
			}
		}
		if (!duplicate)
			break;
	}

	m = realloc(pset->menu_opts, (pset->num+5)*sizeof(*pset->menu_opts));
	if (m == NULL)
		return 0;
	p = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
	if (p == NULL)
		return 0;

	pset->infos = p;
	pset->menu_opts = m;
	menu->opts = m;
	menu->numopts = pset->num+4;
	m += pset->num;
	p += pset->num;
	memset(m, 0, sizeof(*m));
	memset(p, 0, sizeof(*p));
	p->parts = pset->parts;
	p->cur_part_id = NO_PART;
	p->type = PT_root;
	p->fs_type = FS_BSDFFS;
	p->fs_version = 2;
	strncpy(p->mount, new_mp, sizeof(p->mount));

	menu->cursel = pset->num;
	pset->num++;
	fill_ptn_menu(pset);

	return -1;
}

#ifndef NO_CLONES
static int
inst_ext_clone(menudesc *menu, void *arg)
{
	struct selected_partitions selected;
	struct clone_target_menu_data data;
	struct partition_usage_set *pset = arg;
	struct part_usage_info *p;
	menu_ent *men;
	int num_men, i;
 
	if (!select_partitions(&selected, pm->parts))
		return 0;

	num_men = pset->num+1;
	men = calloc(num_men, sizeof *men);
	if (men == NULL)
		return 0;
	for (i = 0; i < num_men; i++)
		men[i].opt_action = clone_target_select;
	men[num_men-1].opt_name = MSG_clone_target_end;

	memset(&data, 0, sizeof data);
	data.usage = *pset;
	data.res = -1;

	data.usage.menu = new_menu(MSG_clone_target_hdr,
	    men, num_men, 3, 2, 0, 65, MC_SCROLL,
	    NULL, draw_size_menu_line, NULL, NULL, MSG_cancel);
	process_menu(data.usage.menu, &data);
	free_menu(data.usage.menu);
	free(men);

	if (data.res < 0)
		goto err;

	/* insert clone record */
	men = realloc(pset->menu_opts, (pset->num+5)*sizeof(*pset->menu_opts));
	if (men == NULL)
		goto err;
	pset->menu_opts = men;
	menu->opts = men;
	menu->numopts = pset->num+4;

	p = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
	if (p == NULL)
		goto err;
	pset->infos = p;

	men += data.res;
	p += data.res;
	memmove(men+1, men, sizeof(*men)*((pset->num+4)-data.res));
	memmove(p+1, p, sizeof(*p)*((pset->num)-data.res));
	memset(men, 0, sizeof(*men));
	memset(p, 0, sizeof(*p));
	p->flags = PUIFLG_CLONE_PARTS;
	p->cur_part_id = NO_PART;
	p->clone_src = malloc(sizeof(selected));
	if (p->clone_src != NULL) {
		*p->clone_src = selected;
		p->clone_ndx = ~0U;
		p->size = selected_parts_size(&selected);
		p->parts = pset->parts;
	} else {
		p->clone_ndx = 0;
		free_selected_partitions(&selected);
	}

	menu->cursel = data.res == 0 ? 1 : 0;
	pset->num++;
	fill_ptn_menu(pset);

	return -1;

err:
	free_selected_partitions(&selected);
	return 0;
}
#endif

static size_t
fill_ptn_menu(struct partition_usage_set *pset)
{
	struct part_usage_info *p;
	struct disk_part_info info;
	menu_ent *m;
	size_t i;
	daddr_t free_space;

#ifdef NO_CLONES
#define	ADD_ITEMS	3
#else
#define	ADD_ITEMS	4
#endif

	memset(pset->menu_opts, 0, (pset->num+ADD_ITEMS)
	    *sizeof(*pset->menu_opts));
	for (m = pset->menu_opts, p = pset->infos, i = 0; i < pset->num;
	    m++, p++, i++) {
		if (p->flags & PUIFLG_CLONE_PARTS)
			m->opt_flags = OPT_IGNORE|OPT_NOSHORT;
		else
		m->opt_action = set_ptn_size;
	}

	m->opt_name = size_separator;
	m->opt_flags = OPT_IGNORE|OPT_NOSHORT;
	m++;

	m->opt_name = MSG_add_another_ptn;
	m->opt_action = add_other_ptn_size;
	m++;

#ifndef NO_CLONES
	m->opt_name = MSG_clone_from_elsewhere;
	m->opt_action = inst_ext_clone;
	m++;
#endif

	m->opt_name = MSG_askunits;
	m->opt_menu = MENU_sizechoice;
	m->opt_flags = OPT_SUB;
	m++;

	/* calculate free space */
	free_space = pset->parts->free_space - pset->reserved_space;
	for (i = 0; i < pset->parts->num_part; i++) {
		if (!pset->parts->pscheme->get_part_info(pset->parts, i,
		    &info))
			continue;
		if (info.flags & (PTI_SEC_CONTAINER|PTI_WHOLE_DISK|
		    PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
			continue;
		free_space += info.size;
	}
	for (i = 0; i < pset->num; i++) {
		if (pset->infos[i].flags &
		    (PUIFLG_IS_OUTER|PUIFLG_JUST_MOUNTPOINT))
			continue;
		free_space -= pset->infos[i].size;
	}
	pset->cur_free_space = free_space;
	set_pset_exit_str(pset);

	if (pset->menu >= 0)
		set_menu_numopts(pset->menu, m - pset->menu_opts);

	return m - pset->menu_opts;
}

static part_id
find_part_at(struct disk_partitions *parts, daddr_t start)
{
	size_t i;
	struct disk_part_info info;

	for (i = 0; i < parts->num_part; i++) {
		if (!parts->pscheme->get_part_info(parts, i, &info))
			continue;
		if (info.start == start)
			return i;
	}

	return NO_PART;
}

static daddr_t
parse_ram_size(const char *str, bool *is_percent)
{
	daddr_t val;
	char *cp;

	val = strtoull(str, &cp, 10);
	while (*cp && isspace((unsigned char)*cp))
		cp++;

	*is_percent = *cp == '%';
	return val;
}

int
set_ptn_size(menudesc *m, void *arg)
{
	struct partition_usage_set *pset = arg;
	struct part_usage_info *p = &pset->infos[m->cursel];
	char answer[16], dflt[16];
	const char *err_msg;
	size_t i, root = ~0U;
	daddr_t size, old_size, new_size_val, mult;
	int rv;
	bool non_zero, extend, is_ram_size, is_percent = false;

	if (pset->cur_free_space == 0 && p->size == 0 &&
	    !(p->flags & PUIFLG_JUST_MOUNTPOINT))
		/* Don't allow 'free_parts' to go negative */
		return 0;

	if (p->cur_part_id != NO_PART) {
		rv = 0;
		process_menu(MENU_ptnsize_replace_existing_partition, &rv);
		if (rv == 0)
			return 0;
		if (!pset->parts->pscheme->delete_partition(pset->parts,
		    p->cur_part_id, &err_msg)) {
			if (err_msg)
				err_msg_win(err_msg);
			return 0;
		}
		p->cur_part_id = NO_PART;
		/*
		 * All other part ids are invalid now too - update them!
		 */
		for (i = 0; i < pset->num; i++) {
			if (pset->infos[i].cur_part_id == NO_PART)
				continue;
			pset->infos[i].cur_part_id =
			    find_part_at(pset->parts, pset->infos[i].cur_start);
		}
	}

	is_ram_size = (p->flags & PUIFLG_JUST_MOUNTPOINT)
	    && p->fs_type == FS_TMPFS;

	size = p->size;
	if (is_ram_size && size < 0) {
		is_percent = true;
		size = -size;
	}
	old_size = size;
	if (size == 0)
		size = p->def_size;
	if (!is_ram_size)
		size /= sizemult;

	if (is_ram_size) {
		snprintf(dflt, sizeof dflt, "%" PRIu64 "%s",
		    size, is_percent ? "%" : "");
	} else {
		snprintf(dflt, sizeof dflt, "%" PRIu64 "%s",
		    size, p->flags & PUIFLAG_EXTEND ? "+" : "");
	}

	for (;;) {
		msg_fmt_prompt_win(MSG_askfssize, -1, 18, 0, 0,
		    dflt, answer, sizeof answer, "%s%s", p->mount,
		    is_ram_size ? msg_string(MSG_megname) : multname);

		if (is_ram_size) {
			new_size_val = parse_ram_size(answer, &is_percent);
			if (is_percent && 
			    (new_size_val < 0 || new_size_val > 100))
				continue;
			if (!is_percent && new_size_val < 0)
				continue;
			size = new_size_val;
			extend = false;
			break;
		}
		mult = sizemult;
		new_size_val = parse_disk_pos(answer, &mult, pm->sectorsize,
		    pm->dlcylsize, &extend);

		if (strcmp(answer, dflt) == 0)
			non_zero = p->def_size > 0;
		else
			non_zero = new_size_val > 0;

		/* Some special cases when /usr is first given a size */
		if (old_size == 0 && non_zero &&
		    strcmp(p->mount, "/usr") == 0) {
			for (i = 0; i < pset->num; i++) {
				if (strcmp(pset->infos[i].mount, "/") == 0) {
					root = i;
					break;
				}
			}
			/* Remove space for /usr from / */
			if (root < pset->num && pset->infos[i].cur_part_id ==
			    NO_PART) {
			    	pset->infos[root].size -= p->def_size;
				pset->cur_free_space += p->def_size;
			}
			/* hack to add free space to default sized /usr */
			if (strcmp(answer, dflt) == 0) {
				size = p->def_size;
				pset->infos[root].flags &= ~PUIFLAG_EXTEND;
				p->flags |= PUIFLAG_EXTEND;
				goto adjust_free;
			}
		}
		if (new_size_val < 0)
			continue;
		size = new_size_val;
		break;
	}

	daddr_t align = pset->parts->pscheme->get_part_alignment(pset->parts);
	if (!is_ram_size) {
		size = NUMSEC(size, mult, align);
	}
	if (p->flags & PUIFLAG_EXTEND)
		p->flags &= ~PUIFLAG_EXTEND;
	if (extend && (p->limit == 0 || p->limit > p->size)) {
		for (size_t k = 0; k < pset->num; k++)
			pset->infos[k].flags &= ~PUIFLAG_EXTEND;
		p->flags |= PUIFLAG_EXTEND;
		if (size == 0)
			size = align;
	}
	if (p->limit != 0 && size > p->limit)
		size = p->limit;
    adjust_free:
	if ((p->flags & (PUIFLG_IS_OUTER|PUIFLG_JUST_MOUNTPOINT)) == 0)
		pset->cur_free_space += p->size - size;
	p->size = is_percent ? -size : size;
	set_pset_exit_str(pset);

	return 0;
}

/*
 * User interface to edit a "wanted" partition layout "pset" as first
 * abstract phase (not concrete partitions).
 * Make sure to have everything (at least theoretically) fit the
 * available space.
 * During editing we keep the part_usage_info and the menu_opts
 * in pset in sync, that is: we always allocate just enough entries
 * in pset->infos as we have usage infos in the list (pset->num),
 * and two additional menu entries ("add a partition" and "select units").
 * The menu exit string changes depending on content, and implies
 * abort while the partition set is not valid (does not fit).
 * Return true when the user wants to continue (by editing the concrete
 * partitions), return false to abort.
 */
bool
get_ptn_sizes(struct partition_usage_set *pset)
{
	size_t num;

	wclear(stdscr);
	wrefresh(stdscr);

	if (pset->menu_opts == NULL)
		pset->menu_opts = calloc(pset->num+4, sizeof(*pset->menu_opts));

	pset->menu = -1;
	num = fill_ptn_menu(pset);

	pset->menu = new_menu(size_menu_title, pset->menu_opts, num,
			3, -1, 12, 70,
			MC_ALWAYS_SCROLL|MC_NOBOX|MC_NOCLEAR|MC_CONTINUOUS,
			draw_size_menu_header, draw_size_menu_line, NULL,
			NULL, size_menu_exit);

	if (pset->menu < 0) {
		free(pset->menu_opts);
		pset->menu_opts = NULL;
		return false;
	}

	pset->ok = true;
	process_menu(pset->menu, pset);

	free_menu(pset->menu);
	free(pset->menu_opts);
	pset->menu = -1;
	pset->menu_opts = NULL;

	return pset->ok;
}

static int
set_keep_existing(menudesc *m, void *arg)
{
	((arg_rep_int*)arg)->rv = LY_KEEPEXISTING;
	return 0;
}

static int
set_switch_scheme(menudesc *m, void *arg)
{
	((arg_rep_int*)arg)->rv = LY_OTHERSCHEME;
	return 0;
}

static int
set_edit_part_sizes(menudesc *m, void *arg)
{
	((arg_rep_int*)arg)->rv = LY_SETSIZES;
	return 0;
}

static int
set_use_default_sizes(menudesc *m, void *arg)
{
	((arg_rep_int*)arg)->rv = LY_USEDEFAULT;
	return 0;
}

/*
 * Check if there is a reasonable pre-existing partition for
 * NetBSD.
 */
static bool
check_existing_netbsd(struct disk_partitions *parts)
{
	size_t nbsd_parts;
	struct disk_part_info info;

	nbsd_parts = 0;
	for (part_id p = 0; p < parts->num_part; p++) {
		if (!parts->pscheme->get_part_info(parts, p, &info))
			continue;
		if (info.flags & (PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
			continue;
		if (info.nat_type && info.nat_type->generic_ptype == PT_root)
			nbsd_parts++;
	}

	return nbsd_parts > 0;
}

/*
 * Query a partition layout type (with available options depending on
 * pre-existing partitions).
 */
static enum layout_type
ask_layout(struct disk_partitions *parts, bool have_existing)
{
	arg_rep_int ai;
	const char *args[2];
	int menu;
	size_t num_opts;
	menu_ent options[4], *opt;

	args[0] = msg_string(parts->pscheme->name);
	args[1] = msg_string(parts->pscheme->short_name);
	ai.args.argv = args;
	ai.args.argc = 2;
	ai.rv = LY_ERROR;

	memset(options, 0, sizeof(options));
	num_opts = 0;
	opt = &options[0];

	if (have_existing) {
		opt->opt_name = MSG_Keep_existing_partitions;
		opt->opt_flags = OPT_EXIT;
		opt->opt_action = set_keep_existing;
		opt++;
		num_opts++;
	}
	opt->opt_name = MSG_Set_Sizes;
	opt->opt_flags = OPT_EXIT;
	opt->opt_action = set_edit_part_sizes;
	opt++;
	num_opts++;

	opt->opt_name = MSG_Use_Default_Parts;
	opt->opt_flags = OPT_EXIT;
	opt->opt_action = set_use_default_sizes;
	opt++;
	num_opts++;

	if (num_available_part_schemes > 1 &&
	    parts->parent == NULL) {
		opt->opt_name = MSG_Use_Different_Part_Scheme;
		opt->opt_flags = OPT_EXIT;
		opt->opt_action = set_switch_scheme;
		opt++;
		num_opts++;
	}

	menu = new_menu(MSG_Select_your_choice, options, num_opts,
	    -1, -10, 0, 0, 0, NULL, NULL, NULL, NULL, MSG_cancel);
	if (menu != -1) {
		get_menudesc(menu)->expand_act = expand_all_option_texts;
		process_menu(menu, &ai);
		free_menu(menu);
	}

	return ai.rv;
}

static void
merge_part_with_wanted(struct disk_partitions *parts, part_id pno,
    const struct disk_part_info *info, struct partition_usage_set *wanted,
    size_t wanted_num, bool is_outer)
{
	struct part_usage_info *infos;

	/*
	 * does this partition match something in the wanted set?
	 */
	for (size_t i = 0; i < wanted_num; i++) {
		if (wanted->infos[i].type != info->nat_type->generic_ptype)
			continue;
		if (wanted->infos[i].type == PT_root &&
		    info->last_mounted != NULL && info->last_mounted[0] != 0 &&
		    strcmp(info->last_mounted, wanted->infos[i].mount) != 0)
			continue;
		if (wanted->infos[i].cur_part_id != NO_PART)
			continue;
		wanted->infos[i].cur_part_id = pno;
		wanted->infos[i].parts = parts;
		wanted->infos[i].size = info->size;
		wanted->infos[i].cur_start = info->start;
		wanted->infos[i].flags &= ~PUIFLAG_EXTEND;
		if (wanted->infos[i].fs_type != FS_UNUSED &&
		    wanted->infos[i].type != PT_swap &&
		    info->last_mounted != NULL &&
		    info->last_mounted[0] != 0)
			wanted->infos[i].instflags |= PUIINST_MOUNT;
		if (is_outer)
			wanted->infos[i].flags |= PUIFLG_IS_OUTER;
		else
			wanted->infos[i].flags &= ~PUIFLG_IS_OUTER;
		return;
	}

	/*
	 * no match - if this is fromt the outer scheme, we are done.
	 * otherwise it must be inserted into the wanted set.
	 */
	if (is_outer)
		return;

	/*
	 * create a new entry for this
	 */
	infos = realloc(wanted->infos, sizeof(*infos)*(wanted->num+1));
	if (infos == NULL)
		return;
	wanted->infos = infos;
	infos += wanted->num;
	wanted->num++;
	memset(infos, 0, sizeof(*infos));
	if (info->last_mounted != NULL && info->last_mounted[0] != 0)
		strlcpy(infos->mount, info->last_mounted,
		    sizeof(infos->mount));
	infos->type = info->nat_type->generic_ptype;
	infos->cur_part_id = pno;
	infos->parts = parts;
	infos->size = info->size;
	infos->cur_start = info->start;
	infos->fs_type = info->fs_type;
	infos->fs_version = info->fs_sub_type;
	if (is_outer)
		infos->flags |= PUIFLG_IS_OUTER;
}

static bool
have_x11_by_default(void)
{
	static const uint8_t def_sets[] = { MD_SETS_SELECTED };

	for (size_t i = 0; i < __arraycount(def_sets); i++)
		if (def_sets[i] >= SET_X11_FIRST &&
		    def_sets[i] <= SET_X11_LAST)
			return true;

	return false;
}

static void
fill_defaults(struct partition_usage_set *wanted, struct disk_partitions *parts,
    daddr_t ptstart, daddr_t ptsize)
{
	size_t i, root = ~0U, usr = ~0U, swap = ~0U, def_usr = ~0U;
	daddr_t free_space, dump_space, required;
#if defined(DEFAULT_UFS2) && !defined(HAVE_UFS2_BOOT)
	size_t boot = ~0U;
#endif

	memset(wanted, 0, sizeof(*wanted));
	wanted->parts = parts;
	if (ptstart > parts->disk_start)
		wanted->reserved_space = ptstart - parts->disk_start;
	if ((ptstart + ptsize) < (parts->disk_start+parts->disk_size))
		wanted->reserved_space +=
		    (parts->disk_start+parts->disk_size) -
		    (ptstart + ptsize);
	wanted->num = __arraycount(default_parts_init);
	wanted->infos = calloc(wanted->num, sizeof(*wanted->infos));
	if (wanted->infos == NULL) {
		err_msg_win(err_outofmem);
		return;
	}

	memcpy(wanted->infos, default_parts_init, sizeof(default_parts_init));

#ifdef HAVE_TMPFS
	if (get_ramsize() >= SMALL_RAM_SIZE) {
		for (i = 0; i < wanted->num; i++) {
			if (wanted->infos[i].type != PT_root ||
			    wanted->infos[i].fs_type != FS_TMPFS)
				continue;
			/* default tmpfs to 1/4 RAM */
			wanted->infos[i].size = -25;
			wanted->infos[i].def_size = -25;
			break;
		}
	}
#endif

#ifdef MD_PART_DEFAULTS
	MD_PART_DEFAULTS(pm, wanted->infos, wanted->num);
#endif

	for (i = 0; i < wanted->num; i++) {
		wanted->infos[i].parts = parts;
		wanted->infos[i].cur_part_id = NO_PART;
		if (wanted->infos[i].type == PT_undef &&
		    wanted->infos[i].fs_type != FS_UNUSED) {
			const struct part_type_desc *pt =
			    parts->pscheme->get_fs_part_type(PT_undef,
			    wanted->infos[i].fs_type,
			    wanted->infos[i].fs_version);
			if (pt != NULL)
				wanted->infos[i].type = pt->generic_ptype;
		}
		if (wanted->parts->parent != NULL &&
		    (wanted->infos[i].fs_type == FS_MSDOS ||
		     wanted->infos[i].fs_type == FS_EX2FS))
			wanted->infos[i].flags |=
			    PUIFLG_ADD_INNER|PUIFLAG_ADD_OUTER;

#if DEFSWAPSIZE == -1
		if (wanted->infos[i].type == PT_swap) {
#ifdef	MD_MAY_SWAP_TO
			if (MD_MAY_SWAP_TO(wanted->parts->disk))
#endif
				wanted->infos[i].size =
				    get_ramsize() * (MEG / 512);
		}
#endif
		if (wanted->infos[i].type == PT_swap && swap > wanted->num)
			swap = i;
#if defined(DEFAULT_UFS2) && !defined(HAVE_UFS2_BOOT)
		if (wanted->infos[i].instflags & PUIINST_BOOT)
			boot = i;
#endif
		if (wanted->infos[i].type == PT_root) {
			if (strcmp(wanted->infos[i].mount, "/") == 0) {
				root = i;
			} else if (
			    strcmp(wanted->infos[i].mount, "/usr") == 0) {
				if (wanted->infos[i].size > 0)
					usr = i;
				else
					def_usr = i;
			}
			if (wanted->infos[i].fs_type == FS_UNUSED)
				wanted->infos[i].fs_type = FS_BSDFFS;
			if (wanted->infos[i].fs_type == FS_BSDFFS) {
#ifdef DEFAULT_UFS2
#ifndef HAVE_UFS2_BOOT
				if (boot < wanted->num || i != root)
#endif
					wanted->infos[i].fs_version = 2;
#endif
			}
		}
	}

	/*
	 * Now we have the defaults as if we were installing to an
	 * empty disk. Merge the partitions in target range that are already
	 * there (match with wanted) or are there additionaly.
	 * The only thing outside of target range that we care for
	 * are FAT partitions, EXT2FS partitions, and a potential
	 * swap partition - we assume one is enough.
	 */
	size_t num = wanted->num;
	if (parts->parent) {
		for (part_id pno = 0; pno < parts->parent->num_part; pno++) {
			struct disk_part_info info;

			if (!parts->parent->pscheme->get_part_info(
			    parts->parent, pno, &info))
				continue;
			if (info.nat_type->generic_ptype != PT_swap &&
			    info.fs_type != FS_MSDOS &&
			    info.fs_type != FS_EX2FS)
				continue;
			merge_part_with_wanted(parts->parent, pno, &info,
			    wanted, num, true);
			break;
		}
	}
	for (part_id pno = 0; pno < parts->num_part; pno++) {
		struct disk_part_info info;

		if (!parts->pscheme->get_part_info(parts, pno, &info))
			continue;

		if (info.flags & PTI_PSCHEME_INTERNAL)
			continue;

		if (info.nat_type->generic_ptype != PT_swap &&
		    (info.start < ptstart ||
		    (info.start + info.size) > (ptstart+ptsize)))
			continue;

		merge_part_with_wanted(parts, pno, &info,
		    wanted, num, false);
	}

	daddr_t align = parts->pscheme->get_part_alignment(parts);

	if (root < wanted->num && wanted->infos[root].cur_part_id == NO_PART) {
		daddr_t max_root_size = parts->disk_start + parts->disk_size;
		if (root_limit > 0) {
			/* Bah - bios can not read all the disk, limit root */
			max_root_size = root_limit - parts->disk_start;
		}
		wanted->infos[root].limit = max_root_size;
	}

	if (have_x11_by_default()) {
		daddr_t xsize = XNEEDMB * (MEG / 512);
		if (usr < wanted->num) {
			if (wanted->infos[usr].cur_part_id == NO_PART) {
				wanted->infos[usr].size += xsize;
				wanted->infos[usr].def_size += xsize;
			}
		} else if (root < wanted->num &&
		    wanted->infos[root].cur_part_id == NO_PART &&
		    (wanted->infos[root].limit == 0 ||
		    (wanted->infos[root].size + xsize) <=
		    wanted->infos[root].limit)) {
			wanted->infos[root].size += xsize;
		}
	}
	if (wanted->infos[root].limit > 0 &&
	    wanted->infos[root].size > wanted->infos[root].limit) {
		if (usr < wanted->num) {
			/* move space from root to usr */
			daddr_t spill = wanted->infos[root].size -
			    wanted->infos[root].limit;
			spill = roundup(spill, align);
			wanted->infos[root].size =
			    wanted->infos[root].limit;
			wanted->infos[usr].size = spill;
		} else {
			wanted->infos[root].size =
			    wanted->infos[root].limit;
		}
	}

	/*
	 * Preliminary calc additional space to allocate and how much
	 * we likely will have left over. Use that to do further
	 * adjustments, so we don't present the user inherently
	 * impossible defaults.
	 */
	free_space = parts->free_space - wanted->reserved_space;
	required = 0;
	if (root < wanted->num)
		required += wanted->infos[root].size;
	if (usr < wanted->num)
		required += wanted->infos[usr].size;
	else if (def_usr < wanted->num)
		required += wanted->infos[def_usr].def_size;
	free_space -= required;
	for (i = 0; i < wanted->num; i++) {
		if (i == root || i == usr)
			continue;	/* already accounted above */
		if (wanted->infos[i].cur_part_id != NO_PART)
			continue;
		if (wanted->infos[i].size == 0)
			continue;
		if (wanted->infos[i].flags
		    & (PUIFLG_IS_OUTER|PUIFLG_JUST_MOUNTPOINT))
			continue;
		free_space -= wanted->infos[i].size;
	}
	if (free_space < 0 && swap < wanted->num) {
		/* steel from swap partition */
		daddr_t d = wanted->infos[swap].size;
		daddr_t inc = roundup(-free_space, align);
		if (inc > d)
			inc = d;
		free_space += inc;
		wanted->infos[swap].size -= inc;
	}
	if (root < wanted->num) {
		/* Add space for 2 system dumps to / (traditional) */
		dump_space = get_ramsize() * (MEG/512);
		dump_space = roundup(dump_space, align);
		if (free_space > dump_space*2)
			dump_space *= 2;
		if (free_space > dump_space) {
			wanted->infos[root].size += dump_space;
			free_space -= dump_space;
		}
	}
	if (wanted->infos[root].limit > 0 &&
	    (wanted->infos[root].cur_start + wanted->infos[root].size >
		wanted->infos[root].limit ||
	    (wanted->infos[root].flags & PUIFLAG_EXTEND &&
	    (wanted->infos[root].cur_start + wanted->infos[root].size
	     + free_space > wanted->infos[root].limit)))) {
		if (usr >= wanted->num && def_usr < wanted->num) {
			usr = def_usr;
			wanted->infos[usr].size = wanted->infos[root].size
			    - wanted->infos[root].limit;
			if (wanted->infos[usr].size <= 0)
				wanted->infos[usr].size = max(1,
				    wanted->infos[usr].def_size);
			wanted->infos[root].size =
			    wanted->infos[root].limit;
			if (wanted->infos[root].flags & PUIFLAG_EXTEND) {
				wanted->infos[root].flags &= ~PUIFLAG_EXTEND;
				wanted->infos[usr].flags |= PUIFLAG_EXTEND;
			}
		} else if (usr < wanted->num) {
			/* move space from root to usr */
			daddr_t spill = wanted->infos[root].size -
			    wanted->infos[root].limit;
			spill = roundup(spill, align);
			wanted->infos[root].size =
			    wanted->infos[root].limit;
			wanted->infos[usr].size = spill;
		} else {
			wanted->infos[root].size =
			    wanted->infos[root].limit;
		}
	}
}

/*
 * We sort pset->infos to sync with pset->parts and
 * the cur_part_id, to allow using the same index into both
 * "array" in later phases. This may include inserting
 * dummy  entries (when we do not actually want the
 * partition, but it is forced upon us, like RAW_PART in
 * disklabel).
 */
static void
sort_and_sync_parts(struct partition_usage_set *pset)
{
	struct part_usage_info *infos;
	size_t i, j, no;
	part_id pno;

	pset->cur_free_space = pset->parts->free_space - pset->reserved_space;

	/* count non-empty entries that are not in pset->parts */
	no = pset->parts->num_part;
	for (i = 0; i < pset->num; i++) {
		if (pset->infos[i].size == 0)
			continue;
		if (pset->infos[i].cur_part_id != NO_PART)
			continue;
		no++;
	}

	/* allocate new infos */
	infos = calloc(no, sizeof *infos);
	if (infos == NULL)
		return;

	/* pre-initialize the first entires as dummy entries */
	for (i = 0; i < pset->parts->num_part; i++) {
		infos[i].cur_part_id = NO_PART;
		infos[i].cur_flags = PTI_PSCHEME_INTERNAL;
	}
	/*
	 * Now copy over eveything from our old entries that points to
	 * a real partition.
	 */
	for (i = 0; i < pset->num; i++) {
		pno = pset->infos[i].cur_part_id;
		if (pno == NO_PART)
			continue;
		if (pset->parts != pset->infos[i].parts)
			continue;
		if (pset->infos[i].flags & PUIFLG_JUST_MOUNTPOINT)
			continue;
		if ((pset->infos[i].flags & (PUIFLG_IS_OUTER|PUIFLG_ADD_INNER))
		    == PUIFLG_IS_OUTER)
			continue;
		if (pno >= pset->parts->num_part)
			continue;
		memcpy(infos+pno, pset->infos+i, sizeof(*infos));
	}
	/* Fill in the infos for real partitions where we had no data */
	for (pno = 0; pno < pset->parts->num_part; pno++) {
		struct disk_part_info info;

		if (infos[pno].cur_part_id != NO_PART)
			continue;

		if (!pset->parts->pscheme->get_part_info(pset->parts, pno,
		    &info))
			continue;

		infos[pno].parts = pset->parts;
		infos[pno].cur_part_id = pno;
		infos[pno].cur_flags = info.flags;
		infos[pno].size = info.size;
		infos[pno].type = info.nat_type->generic_ptype;
		infos[pno].cur_start = info.start;
		infos[pno].fs_type = info.fs_type;
		infos[pno].fs_version = info.fs_sub_type;
	}
	/* Add the non-partition entires after that */
	j = pset->parts->num_part;
	for (i = 0; i < pset->num; i++) {
		if (j >= no)
			break;
		if (pset->infos[i].size == 0)
			continue;
		if (pset->infos[i].cur_part_id != NO_PART)
			continue;
		memcpy(infos+j, pset->infos+i, sizeof(*infos));
		j++;
	}

	/* done, replace infos */
	free(pset->infos);
	pset->num = no;
	pset->infos = infos;
}

#ifndef NO_CLONES
/*
 * Convert clone entries with more than one source into
 * several entries with a single source each.
 */
static void
normalize_clones(struct part_usage_info **infos, size_t *num)
{
	size_t i, j, add_clones;
	struct part_usage_info *ui, *src, *target;
	struct disk_part_info info;
	struct selected_partition *clone;

	for (add_clones = 0, i = 0; i < *num; i++) {
		if ((*infos)[i].clone_src != NULL &&
		    (*infos)[i].flags & PUIFLG_CLONE_PARTS &&
		    (*infos)[i].cur_part_id == NO_PART)
			add_clones += (*infos)[i].clone_src->num_sel-1;
	}
	if (add_clones == 0)
		return;

	ui = calloc(*num+add_clones, sizeof(**infos));
	if (ui == NULL)
		return;	/* can not handle this well here, drop some clones */

	/* walk the list and dedup clones */
	for (src = *infos, target = ui, i = 0; i < *num; i++) {
		if (src != target)
			*target = *src;
		if (target->clone_src != NULL &&
		    (target->flags & PUIFLG_CLONE_PARTS) &&
		    target->cur_part_id == NO_PART) {
			for (j = 0; j < src->clone_src->num_sel; j++) {
				if (j > 0) {
					target++;
					*target = *src;
				}
				target->clone_ndx = j;
				clone = &target->clone_src->selection[j];
				clone->parts->pscheme->get_part_info(
				    clone->parts, clone->id, &info);
				target->size = info.size;
			}
		}
		target++;
		src++;
	}
	*num += add_clones;
	assert((target-ui) >= 0 && (size_t)(target-ui) == *num);
	free(*infos);
	*infos = ui;
}
#endif

static void
apply_settings_to_partitions(struct disk_partitions *parts,
    struct partition_usage_set *wanted, daddr_t start, daddr_t xsize)
{
	size_t i, exp_ndx = ~0U;
	daddr_t planned_space = 0, nsp, from, align;
	struct disk_part_info *infos;
#ifndef NO_CLONES
	struct disk_part_info cinfo, srcinfo;
	struct selected_partition *sp;
#endif
	struct disk_part_free_space space;
	struct disk_partitions *ps = NULL;
	part_id pno, new_part_id;

#ifndef NO_CLONES
	normalize_clones(&wanted->infos, &wanted->num);
#endif

	infos = calloc(wanted->num, sizeof(*infos));
	if (infos == NULL) {
		err_msg_win(err_outofmem);
		return;
	}

	align = wanted->parts->pscheme->get_part_alignment(wanted->parts);
	/*
	 * Pass one: calculate space available for expanding
	 * the marked partition.
	 */
	for (i = 0; i < wanted->num; i++) {
		if ((wanted->infos[i].flags & PUIFLAG_EXTEND) &&
		    exp_ndx == ~0U)
			exp_ndx = i;
		if (wanted->infos[i].flags & PUIFLG_JUST_MOUNTPOINT)
			continue;
		nsp = wanted->infos[i].size;
		if (wanted->infos[i].cur_part_id != NO_PART) {
			ps = wanted->infos[i].flags & PUIFLG_IS_OUTER ?
			    parts->parent : parts;

			ps->pscheme->get_part_info(ps,
			     wanted->infos[i].cur_part_id, &infos[i]);
			if (!(wanted->infos[i].flags & PUIFLG_IS_OUTER))
				nsp -= infos[i].size;
		}
		if (nsp > 0)
			planned_space += roundup(nsp, align);
	}

	/*
	 * Expand the pool partition (or shrink, if we overran),
	 * but check size limits.
	 */
	if (exp_ndx < wanted->num) {
		daddr_t free_space =
		    parts->free_space - roundup(wanted->reserved_space, align);
		free_space -= planned_space;
		daddr_t new_size = wanted->infos[exp_ndx].size;
		if (free_space > 0)
			new_size += free_space;

		if (wanted->infos[exp_ndx].limit > 0 &&
		    (new_size + wanted->infos[exp_ndx].cur_start)
		     > wanted->infos[exp_ndx].limit) {
			wanted->infos[exp_ndx].size =
			    wanted->infos[exp_ndx].limit
			    - wanted->infos[exp_ndx].cur_start;
		} else {
			wanted->infos[exp_ndx].size = new_size;
		}
	}

	/*
	 * Now it gets tricky: we want the wanted partitions in order
	 * as defined, but any already existing partitions should not
	 * be moved. We allow them to change size though.
	 * To keep it simple, we just assign in order and skip blocked
	 * spaces. This may shuffle the order of the resulting partitions
	 * compared to the wanted list.
	 */

	/* Adjust sizes of existing partitions */
	for (i = 0; i < wanted->num; i++) {
		ps = wanted->infos[i].flags & PUIFLG_IS_OUTER ?
		    parts->parent : parts;
		const struct part_usage_info *want = &wanted->infos[i];

		if (want->cur_part_id == NO_PART)
			continue;
		if (i == exp_ndx)	/* the exp. part. can not exist yet */
			continue;
		daddr_t free_size = ps->pscheme->max_free_space_at(ps,
		    infos[i].start);
		if (free_size < wanted->infos[i].size)
			continue;
		if (infos[i].size != wanted->infos[i].size) {
			infos[i].size = wanted->infos[i].size;
			ps->pscheme->set_part_info(ps, want->cur_part_id,
			    &infos[i], NULL);
		}
	}

	from = start > 0 ? start : -1;
	/*
	 * First add all outer partitions - we need to align those exactly
	 * with the inner counterpart later.
	 */
	if (parts->parent) {
		ps = parts->parent;
		daddr_t outer_align = ps->pscheme->get_part_alignment(ps);

		for (i = 0; i < wanted->num; i++) {
			struct part_usage_info *want = &wanted->infos[i];

			if (want->cur_part_id != NO_PART)
				continue;
			if (!(want->flags & PUIFLAG_ADD_OUTER))
				continue;
			if (want->size <= 0)
				continue;

			size_t cnt = ps->pscheme->get_free_spaces(ps,
			    &space, 1, want->size-2*outer_align,
			    outer_align, from, -1);

			if (cnt == 0)	/* no free space for this partition */
				continue;

			infos[i].start = space.start;
			infos[i].size = min(want->size, space.size);
			infos[i].nat_type =
			    ps->pscheme->get_fs_part_type(
			        want->type, want->fs_type, want->fs_version);
			infos[i].last_mounted = want->mount;
			infos[i].fs_type = want->fs_type;
			infos[i].fs_sub_type = want->fs_version;
			infos[i].fs_opt1 = want->fs_opt1;
			infos[i].fs_opt2 = want->fs_opt2;
			infos[i].fs_opt3 = want->fs_opt3;
			new_part_id = ps->pscheme->add_partition(ps,
			    &infos[i], NULL);
			if (new_part_id == NO_PART)
				continue;	/* failed to add, skip */

			ps->pscheme->get_part_info(ps,
			    new_part_id, &infos[i]);
			want->cur_part_id = new_part_id;

			want->flags |= PUIFLG_ADD_INNER|PUIFLG_IS_OUTER;
			from = roundup(infos[i].start + 
			    infos[i].size, outer_align);
		}
	}

	/*
	 * Now add new inner partitions (and cloned partitions)
	 */
	for (i = 0; i < wanted->num; i++) {

		daddr_t limit = wanted->parts->disk_size + wanted->parts->disk_start;
		if (from >= limit)
			break;

		struct part_usage_info *want = &wanted->infos[i];

		if (want->cur_part_id != NO_PART)
			continue;
		if (want->flags & (PUIFLG_JUST_MOUNTPOINT|PUIFLG_IS_OUTER))
			continue;
#ifndef NO_CLONES
		if ((want->flags & PUIFLG_CLONE_PARTS) &&
		    want->clone_src != NULL &&
		    want->clone_ndx < want->clone_src->num_sel) {
			sp = &want->clone_src->selection[want->clone_ndx];
			if (!sp->parts->pscheme->get_part_info(
			    sp->parts, sp->id, &srcinfo))
				continue;
			if (!wanted->parts->pscheme->
			    adapt_foreign_part_info(wanted->parts,
			    &cinfo, sp->parts->pscheme, &srcinfo))
				continue;

			/* find space for cinfo and add a partition */
			size_t cnt = wanted->parts->pscheme->get_free_spaces(
			    wanted->parts, &space, 1, want->size-align, align,
			    from, -1);
			if (cnt == 0)
				cnt = wanted->parts->pscheme->get_free_spaces(
				    wanted->parts, &space, 1,
				    want->size-5*align, align, from, -1);

			if (cnt == 0)
				continue; /* no free space for this clone */

			infos[i] = cinfo;
			infos[i].start = space.start;
			new_part_id = wanted->parts->pscheme->add_partition(
			    wanted->parts, &infos[i], NULL);
		} else {
#else
		{
#endif
			if (want->size <= 0)
				continue;
			size_t cnt = wanted->parts->pscheme->get_free_spaces(
			    wanted->parts, &space, 1, want->size-align, align,
			    from, -1);
			if (cnt == 0)
				cnt = wanted->parts->pscheme->get_free_spaces(
				    wanted->parts, &space, 1,
				    want->size-5*align, align, from, -1);

			if (cnt == 0)
				continue; /* no free space for this partition */

			infos[i].start = space.start;
			infos[i].size = min(want->size, space.size);
			infos[i].nat_type =
			    wanted->parts->pscheme->get_fs_part_type(
			    want->type, want->fs_type, want->fs_version);
			infos[i].last_mounted = want->mount;
			infos[i].fs_type = want->fs_type;
			infos[i].fs_sub_type = want->fs_version;
			infos[i].fs_opt1 = want->fs_opt1;
			infos[i].fs_opt2 = want->fs_opt2;
			infos[i].fs_opt3 = want->fs_opt3;
			if (want->fs_type != FS_UNUSED &&
			    want->type != PT_swap) {
				want->instflags |= PUIINST_NEWFS;
				if (want->mount[0] != 0)
					want->instflags |= PUIINST_MOUNT;
			}
			new_part_id = wanted->parts->pscheme->add_partition(
			    wanted->parts, &infos[i], NULL);
		}

		if (new_part_id == NO_PART)
			continue;	/* failed to add, skip */

		wanted->parts->pscheme->get_part_info(
		    wanted->parts, new_part_id, &infos[i]);
		from = roundup(infos[i].start+infos[i].size, align);
	}


	/*
	* If there are any outer partitions that we need as inner ones
	 * too, add them to the inner partitioning scheme.
	 */
	for (i = 0; i < wanted->num; i++) {
		struct part_usage_info *want = &wanted->infos[i];

		if (want->cur_part_id == NO_PART)
			continue;
		if (want->flags & PUIFLG_JUST_MOUNTPOINT)
			continue;
		if (want->size <= 0)
			continue;

		if ((want->flags & (PUIFLG_ADD_INNER|PUIFLG_IS_OUTER)) !=
		    (PUIFLG_ADD_INNER|PUIFLG_IS_OUTER))
			continue;

		new_part_id = NO_PART;
		for (part_id j = 0; new_part_id == NO_PART &&
		    j < wanted->parts->num_part; j++) {
			struct disk_part_info test;

			if (!wanted->parts->pscheme->get_part_info(
			    wanted->parts, j, &test))
				continue;
			if (test.start == want->cur_start &&
			    test.size == want->size)
				new_part_id = j;
		}

		if (new_part_id == NO_PART) {
			infos[i].start = want->cur_start;
			infos[i].size = want->size;
			infos[i].nat_type = wanted->parts->pscheme->
			    get_fs_part_type(want->type, want->fs_type,
			    want->fs_version);
			infos[i].last_mounted = want->mount;
			infos[i].fs_type = want->fs_type;
			infos[i].fs_sub_type = want->fs_version;
			infos[i].fs_opt1 = want->fs_opt1;
			infos[i].fs_opt2 = want->fs_opt2;
			infos[i].fs_opt3 = want->fs_opt3;

			if (wanted->parts->pscheme->add_outer_partition
			    != NULL)
				new_part_id = wanted->parts->pscheme->
				    add_outer_partition(
				    wanted->parts, &infos[i], NULL);
			else
				new_part_id = wanted->parts->pscheme->
				    add_partition(
				    wanted->parts, &infos[i], NULL);
		
			if (new_part_id == NO_PART)
				continue;	/* failed to add, skip */
		}

		wanted->parts->pscheme->get_part_info(
		    wanted->parts, new_part_id, &infos[i]);
		want->parts = wanted->parts;
		if (want->fs_type != FS_UNUSED &&
		    want->type != PT_swap) {
			want->instflags |= PUIINST_NEWFS;
			if (want->mount[0] != 0)
				want->instflags |= PUIINST_MOUNT;
		}
	}

	/*
	 * Note: all part_ids are invalid now, as we have added things!
	 */
	for (i = 0; i < wanted->num; i++)
		wanted->infos[i].cur_part_id = NO_PART;
	for (pno = 0; pno < parts->num_part; pno++) {
		struct disk_part_info t;

		if (!parts->pscheme->get_part_info(parts, pno, &t))
			continue;

		for (i = 0; i < wanted->num; i++) {
			if (wanted->infos[i].cur_part_id != NO_PART)
				continue;
			if (wanted->infos[i].size <= 0)
				continue;
			if (t.start == infos[i].start) {
				wanted->infos[i].cur_part_id = pno;
				wanted->infos[i].cur_start = infos[i].start;
				wanted->infos[i].cur_flags = infos[i].flags;
				break;
			}
		}
	}
	free(infos);

	/* sort, and sync part ids and wanted->infos[] indices */
	sort_and_sync_parts(wanted);
}

static void
replace_by_default(struct disk_partitions *parts,
    daddr_t start, daddr_t size, struct partition_usage_set *wanted)
{

	if (start == 0 && size == parts->disk_size)
		parts->pscheme->delete_all_partitions(parts);
	else if (parts->pscheme->delete_partitions_in_range != NULL)
		parts->pscheme->delete_partitions_in_range(parts, start, size);
	else
		assert(parts->num_part == 0);

	fill_defaults(wanted, parts, start, size);
	apply_settings_to_partitions(parts, wanted, start, size);
}

static bool
edit_with_defaults(struct disk_partitions *parts,
    daddr_t start, daddr_t size, struct partition_usage_set *wanted)
{
	bool ok;

	fill_defaults(wanted, parts, start, size);
	ok = get_ptn_sizes(wanted);
	if (ok)
		apply_settings_to_partitions(parts, wanted, start, size);
	return ok;
}

/*
 * md back-end code for menu-driven BSD disklabel editor.
 * returns 0 on failure, 1 on success, -1 for restart.
 * fills the install target with a list for newfs/fstab.
 */
int
make_bsd_partitions(struct install_partition_desc *install)
{
	struct disk_partitions *parts = pm->parts;
	const struct disk_partitioning_scheme *pscheme;
	struct partition_usage_set wanted;
	daddr_t p_start, p_size;
	enum layout_type layoutkind = LY_SETSIZES;
	bool have_existing;

	if (pm && pm->no_part && parts == NULL)
		return 1;
	if (parts == NULL) {
		pscheme = select_part_scheme(pm, NULL, !pm->no_mbr, NULL);
		if (pscheme == NULL)
			return 0;
		parts = pscheme->create_new_for_disk(pm->diskdev,
		    0, pm->dlsize, true, NULL);
		if (parts == NULL)
			return 0;
		pm->parts = parts;
	} else {
		pscheme = parts->pscheme;
	}

	if (pscheme->secondary_partitions) {
		struct disk_partitions *p;

		p = pscheme->secondary_partitions(parts, pm->ptstart, false);
		if (p) {
			parts = p;
			pscheme = parts->pscheme;
		}
	}

	have_existing = check_existing_netbsd(parts);

	/*
	 * Make sure the cylinder size multiplier/divisor and disk sieze are
	 * valid
	 */
	if (pm->current_cylsize == 0)
		pm->current_cylsize = pm->dlcylsize;
	if (pm->ptsize == 0)
		pm->ptsize = pm->dlsize;

	/* Ask for layout type -- standard or special */
	if (partman_go == 0) {
		char bsd_size[6], min_size[6], x_size[6];

		humanize_number(bsd_size, sizeof(bsd_size),
		    (uint64_t)pm->ptsize*pm->sectorsize,
		    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
		humanize_number(min_size, sizeof(min_size),
		    (uint64_t)(DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE)*MEG,
		    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
		humanize_number(x_size, sizeof(x_size),
		    (uint64_t)(DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE
		    + XNEEDMB)*MEG,
		    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);

		msg_display_subst(
		    have_existing ? MSG_layout_prologue_existing
		    : MSG_layout_prologue_none, 6, pm->diskdev,
		    msg_string(parts->pscheme->name),
		    msg_string(parts->pscheme->short_name),
		    bsd_size, min_size, x_size);
		msg_display_add_subst(MSG_layout_main, 6,
		    pm->diskdev,
		    msg_string(parts->pscheme->name),
		    msg_string(parts->pscheme->short_name),
		    bsd_size, min_size, x_size);
		msg_display_add("\n\n");
		layoutkind = ask_layout(parts, have_existing);
		if (layoutkind == LY_ERROR)
			return 0;
	}

	if (layoutkind == LY_USEDEFAULT || layoutkind == LY_SETSIZES) {
		/* calc available disk area for the NetBSD partitions */
		p_start = pm->ptstart;
		p_size = pm->ptsize;
		if (parts->parent != NULL && 
		    parts->parent->pscheme->guess_install_target != NULL)
			parts->parent->pscheme->guess_install_target(
			    parts->parent, &p_start, &p_size);
	}
	if (layoutkind == LY_OTHERSCHEME) {
		parts->pscheme->destroy_part_scheme(parts);
		return -1;
	} else if (layoutkind == LY_USEDEFAULT) { 
		replace_by_default(parts, p_start, p_size,
		    &wanted);
	} else if (layoutkind == LY_SETSIZES) {
		if (!edit_with_defaults(parts, p_start, p_size,
		    &wanted)) {
			free_usage_set(&wanted);
			return 0;
		}
	} else {
		usage_set_from_parts(&wanted, parts);
	}

	/*
	 * Make sure the target root partition is properly marked,
	 * check for existing EFI boot partition.
	 */
	bool have_inst_target = false;
#ifdef HAVE_EFI_BOOT
	daddr_t target_start = -1;
#endif
	for (size_t i = 0; i < wanted.num; i++) {
		if (wanted.infos[i].cur_flags & PTI_INSTALL_TARGET) {
			have_inst_target = true;
#ifdef HAVE_EFI_BOOT
			target_start = wanted.infos[i].cur_start;
#endif
			break;
		 }
	}
	if (!have_inst_target) {
		for (size_t i = 0; i < wanted.num; i++) {
			struct disk_part_info info;

			if (wanted.infos[i].type != PT_root ||
			    strcmp(wanted.infos[i].mount, "/") != 0) 
				continue;
			wanted.infos[i].cur_flags |= PTI_INSTALL_TARGET;

			if (!wanted.parts->pscheme->get_part_info(wanted.parts,
			    wanted.infos[i].cur_part_id, &info))
				break;
			info.flags |= PTI_INSTALL_TARGET;
			wanted.parts->pscheme->set_part_info(wanted.parts,
			    wanted.infos[i].cur_part_id, &info, NULL);
#ifdef HAVE_EFI_BOOT
			target_start = wanted.infos[i].cur_start;
#endif
			break;
		}
	}
#ifdef HAVE_EFI_BOOT
	size_t boot_part = ~0U;
	for (part_id i = 0; i < wanted.num; i++) {
		if ((wanted.infos[i].cur_flags & PTI_BOOT) != 0 ||
		    wanted.infos[i].type ==  PT_EFI_SYSTEM) {
			boot_part = i;
			break;
		}
	}
	if (boot_part == ~0U) {
		for (part_id i = 0; i < wanted.num; i++) {
			/*
			 * heuristic to recognize existing MBR FAT
			 * partitions as EFI without looking for
			 * details
			 */
			if ((wanted.infos[i].type != PT_FAT &&
			    wanted.infos[i].type != PT_EFI_SYSTEM) ||
			    wanted.infos[i].fs_type != FS_MSDOS)
				continue;
			daddr_t ps = wanted.infos[i].cur_start;
			daddr_t pe = ps + wanted.infos[i].size;
			if (target_start >= 0 &&
			   (ps >= target_start || pe >= target_start)) 
				continue;
			boot_part = i;
			break;
		}
	}
	if (boot_part != ~0U) {
		struct disk_part_info info;

		if (wanted.parts->pscheme->get_part_info(wanted.parts,
		    wanted.infos[boot_part].cur_part_id, &info)) {
			info.flags |= PTI_BOOT;
			wanted.parts->pscheme->set_part_info(wanted.parts,
			    wanted.infos[boot_part].cur_part_id, &info, NULL);
		}
		wanted.infos[boot_part].instflags |= PUIINST_BOOT;
	}
#endif

	/*
	 * OK, we have a partition table. Give the user the chance to
	 * edit it and verify it's OK, or abort altogether.
	 */
 	for (;;) {
		int rv = edit_and_check_label(pm, &wanted, true);
		if (rv == 0) {
			msg_display(MSG_abort_part);
			free_usage_set(&wanted);
			return 0;
		}
		/* update install infos */
		install->num = wanted.num;
		install->infos = wanted.infos;
		install->write_back = wanted.write_back;
		install->num_write_back = wanted.num_write_back;
		/* and check them */
		if (check_partitions(install))
			break;
	}

	/* we moved infos from wanted to install target */
	wanted.infos = NULL;
	wanted.write_back = NULL;
	free_usage_set(&wanted);

	/* Everything looks OK. */
	return 1;
}

#ifndef MD_NEED_BOOTBLOCK
#define MD_NEED_BOOTBLOCK(A)	true
#endif

/*
 * check that there is at least a / somewhere.
 */
bool
check_partitions(struct install_partition_desc *install)
{
#ifdef HAVE_BOOTXX_xFS
	int rv = 1;
	char *bootxx;
#endif
#ifndef HAVE_UFS2_BOOT
	size_t i;
#endif

#ifdef HAVE_BOOTXX_xFS
	if (MD_NEED_BOOTBLOCK(install)) {
		/* check if we have boot code for the root partition type */
		bootxx = bootxx_name(install);
		if (bootxx != NULL) {
			rv = access(bootxx, R_OK);
			free(bootxx);
		} else
			rv = -1;
		if (rv != 0) {
			hit_enter_to_continue(NULL, MSG_No_Bootcode);
			return false;
		}
	}
#endif
#ifndef HAVE_UFS2_BOOT
	if (MD_NEED_BOOTBLOCK(install)) {
		for (i = 0; i < install->num; i++) {
			if (install->infos[i].type != PT_root)
				continue;
			if (strcmp(install->infos[i].mount, "/") != 0)
				continue;
			if (install->infos[i].fs_type != FS_BSDFFS)
				continue;
			if (install->infos[i].fs_version != 2)
				continue;
			hit_enter_to_continue(NULL, MSG_cannot_ufs2_root);
			return false;
		}
	}
#endif

	return md_check_partitions(install);
}