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
/*	$NetBSD: biosdisk.c,v 1.58 2022/05/03 10:09:40 jmcneill Exp $	*/

/*
 * Copyright (c) 1996, 1998
 *	Matthias Drochner.  All rights reserved.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 THE AUTHOR 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.
 *
 */

/*
 * raw BIOS disk device for libsa.
 * needs lowlevel parts from bios_disk.S and biosdisk_ll.c
 * partly from netbsd:sys/arch/i386/boot/disk.c
 * no bad144 handling!
 *
 * A lot of this must match sys/kern/subr_disk_mbr.c
 */

/*
 * Ported to boot 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
 *
 * Mach Operating System
 * Copyright (c) 1992, 1991 Carnegie Mellon University
 * All Rights Reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */

#if !defined(NO_DISKLABEL) || !defined(NO_GPT)
#define FSTYPENAMES
#endif

#include <lib/libkern/libkern.h>
#include <lib/libsa/stand.h>

#include <sys/types.h>
#include <sys/md5.h>
#include <sys/param.h>
#include <sys/disklabel.h>
#include <sys/disklabel_gpt.h>
#include <sys/uuid.h>

#include <fs/cd9660/iso.h>
#include <fs/unicode.h>

#include <lib/libsa/saerrno.h>
#include <machine/cpu.h>

#include "libi386.h"
#include "biosdisk_ll.h"
#include "biosdisk.h"
#ifdef _STANDALONE
#include "bootinfo.h"
#endif

#ifndef NO_GPT
#define MAXDEVNAME 39 /* "NAME=" + 34 char part_name */
#else
#define MAXDEVNAME 16
#endif

#ifndef BIOSDISK_BUFSIZE
#define BIOSDISK_BUFSIZE	2048	/* must be large enough for a CD sector */
#endif

#define BIOSDISKNPART 26

struct biosdisk {
	struct biosdisk_ll ll;
	daddr_t         boff;
	char            buf[BIOSDISK_BUFSIZE];
#if !defined(NO_DISKLABEL) || !defined(NO_GPT)
	struct biosdisk_partition part[BIOSDISKNPART];
#endif
};

#include <dev/raidframe/raidframevar.h>
#define RF_COMPONENT_INFO_OFFSET   16384   /* from sys/dev/raidframe/rf_netbsdkintf.c */
#define RF_COMPONENT_LABEL_VERSION     2   /* from <dev/raidframe/rf_raid.h> */

#define RAIDFRAME_NDEV 16 /* abitrary limit to 15 raidframe devices */
struct raidframe {
	int	last_unit;
	int	serial;
	int	biosdev;
	int	parent_part;
#ifndef NO_GPT
	char    parent_name[MAXDEVNAME + 1];
#endif
	daddr_t	offset;
	daddr_t	size;
};


#ifndef NO_GPT
const struct uuid GET_nbsd_raid = GPT_ENT_TYPE_NETBSD_RAIDFRAME;
const struct uuid GET_nbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS;
const struct uuid GET_nbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS;
const struct uuid GET_nbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP;
const struct uuid GET_nbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD;
const struct uuid GET_nbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD;

const struct uuid GET_efi = GPT_ENT_TYPE_EFI;
const struct uuid GET_mbr = GPT_ENT_TYPE_MBR;
const struct uuid GET_fbsd = GPT_ENT_TYPE_FREEBSD;
const struct uuid GET_fbsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
const struct uuid GET_fbsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
const struct uuid GET_fbsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
const struct uuid GET_fbsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
const struct uuid GET_ms_rsvd = GPT_ENT_TYPE_MS_RESERVED;
const struct uuid GET_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
const struct uuid GET_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA;
const struct uuid GET_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA;
const struct uuid GET_linux_data = GPT_ENT_TYPE_LINUX_DATA;
const struct uuid GET_linux_raid = GPT_ENT_TYPE_LINUX_RAID;
const struct uuid GET_linux_swap = GPT_ENT_TYPE_LINUX_SWAP;
const struct uuid GET_linux_lvm = GPT_ENT_TYPE_LINUX_LVM;
const struct uuid GET_apple_hfs = GPT_ENT_TYPE_APPLE_HFS;
const struct uuid GET_apple_ufs = GPT_ENT_TYPE_APPLE_UFS;
const struct uuid GET_bios = GPT_ENT_TYPE_BIOS;

const struct gpt_part gpt_parts[] = {
	{ &GET_nbsd_raid,	"NetBSD RAID" },
	{ &GET_nbsd_ffs,	"NetBSD FFS" },
	{ &GET_nbsd_lfs,	"NetBSD LFS" },
	{ &GET_nbsd_swap,	"NetBSD Swap" },
	{ &GET_nbsd_ccd,	"NetBSD ccd" },
	{ &GET_nbsd_cgd,	"NetBSD cgd" },
	{ &GET_efi,		"EFI System" },
	{ &GET_mbr,		"MBR" },
	{ &GET_fbsd,		"FreeBSD" },
	{ &GET_fbsd_swap,	"FreeBSD Swap" },
	{ &GET_fbsd_ufs,	"FreeBSD UFS" },
	{ &GET_fbsd_vinum,	"FreeBSD Vinum" },
	{ &GET_fbsd_zfs,	"FreeBSD ZFS" },
	{ &GET_ms_rsvd,		"Microsoft Reserved" },
	{ &GET_ms_basic_data,	"Microsoft Basic data" },
	{ &GET_ms_ldm_metadata,	"Microsoft LDM metadata" },
	{ &GET_ms_ldm_data,	"Microsoft LDM data" },
	{ &GET_linux_data,	"Linux data" },
	{ &GET_linux_raid,	"Linux RAID" },
	{ &GET_linux_swap,	"Linux Swap" },
	{ &GET_linux_lvm,	"Linux LVM" },
	{ &GET_apple_hfs,	"Apple HFS" },
	{ &GET_apple_ufs,	"Apple UFS" },
	{ &GET_bios,		"BIOS Boot (GRUB)" },
};
#endif /* NO_GPT */

struct btinfo_bootdisk bi_disk;
struct btinfo_bootwedge bi_wedge;
struct btinfo_rootdevice bi_root;

#define MBR_PARTS(buf) ((char *)(buf) + offsetof(struct mbr_sector, mbr_parts))

#ifndef	devb2cdb
#define	devb2cdb(bno)	(((bno) * DEV_BSIZE) / ISO_DEFAULT_BLOCK_SIZE)
#endif

static void
dealloc_biosdisk(struct biosdisk *d)
{
#ifndef NO_GPT
	int i;

	for (i = 0; i < __arraycount(d->part); i++) {
		if (d->part[i].part_name != NULL)
			dealloc(d->part[i].part_name, BIOSDISK_PART_NAME_LEN);
	}
#endif

	dealloc(d, sizeof(*d));

	return;
}

static struct biosdisk_partition *
copy_biosdisk_part(struct biosdisk *d)
{
	struct biosdisk_partition *part;

	part = alloc(sizeof(d->part));
	if (part == NULL)
		goto out;

	memcpy(part, d->part, sizeof(d->part));

#ifndef NO_GPT
	int i;

	for (i = 0; i < __arraycount(d->part); i++) {
		if (d->part[i].part_name != NULL) {
			part[i].part_name = alloc(BIOSDISK_PART_NAME_LEN);
			memcpy(part[i].part_name, d->part[i].part_name,
			       BIOSDISK_PART_NAME_LEN);
		}
	}
#endif

out:
	return part;
}

int
biosdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
		  void *buf, size_t *rsize)
{
	struct biosdisk *d;
	int blks, frag;

	if (flag != F_READ)
		return EROFS;

	d = (struct biosdisk *) devdata;

	if (d->ll.type == BIOSDISK_TYPE_CD)
		dblk = devb2cdb(dblk);

	dblk += d->boff;

	blks = size / d->ll.secsize;
	if (blks && readsects(&d->ll, dblk, blks, buf, 0)) {
		if (rsize)
			*rsize = 0;
		return EIO;
	}

	/* needed for CD */
	frag = size % d->ll.secsize;
	if (frag) {
		if (readsects(&d->ll, dblk + blks, 1, d->buf, 0)) {
			if (rsize)
				*rsize = blks * d->ll.secsize;
			return EIO;
		}
		memcpy(buf + blks * d->ll.secsize, d->buf, frag);
	}

	if (rsize)
		*rsize = size;
	return 0;
}

static struct biosdisk *
alloc_biosdisk(int biosdev)
{
	struct biosdisk *d;

	d = alloc(sizeof(*d));
	if (d == NULL)
		return NULL;
	memset(d, 0, sizeof(*d));

	d->ll.dev = biosdev;
	if (set_geometry(&d->ll, NULL)) {
#ifdef DISK_DEBUG
		printf("no geometry information\n");
#endif
		dealloc_biosdisk(d);
		return NULL;
	}
	return d;
}

#if !defined(NO_DISKLABEL) || !defined(NO_GPT)
static void
md5(void *hash, const void *data, size_t len)
{
	MD5_CTX ctx;

	MD5Init(&ctx);
	MD5Update(&ctx, data, len);
	MD5Final(hash, &ctx);

	return;
}
#endif

#ifndef NO_GPT
bool
guid_is_nil(const struct uuid *u)
{
	static const struct uuid nil = { .time_low = 0 };
	return (memcmp(u, &nil, sizeof(*u)) == 0 ? true : false);
}

bool
guid_is_equal(const struct uuid *a, const struct uuid *b)
{
	return (memcmp(a, b, sizeof(*a)) == 0 ? true : false);
}

#ifndef NO_GPT
static void
part_name_utf8(const uint16_t *utf16_src, size_t utf16_srclen,
	       char *utf8_dst, size_t utf8_dstlen)
{
	char *c = utf8_dst;
	size_t r = utf8_dstlen - 1;
	size_t n;
	int j;

	if (utf8_dst == NULL)
		return;

	for (j = 0; j < utf16_srclen && utf16_src[j] != 0x0000; j++) {
		n = wput_utf8(c, r, le16toh(utf16_src[j]));
		if (n == 0)
			break;
		c += n; r -= n;
	}
	*c = '\0';

	return;
}
#endif

static int
check_gpt(struct biosdisk *d, daddr_t rf_offset, daddr_t sector)
{
	struct gpt_hdr gpth;
	const struct gpt_ent *ep;
	const struct uuid *u;
	daddr_t entblk;
	size_t size;
	uint32_t crc;
	int sectors;
	int entries;
	int entry;
	int i, j;

	/* read in gpt_hdr sector */
	if (readsects(&d->ll, sector, 1, d->buf, 1)) {
#ifdef DISK_DEBUG
		printf("Error reading GPT header at %"PRId64"\n", sector);
#endif
		return EIO;
	}

	memcpy(&gpth, d->buf, sizeof(gpth));

	if (memcmp(GPT_HDR_SIG, gpth.hdr_sig, sizeof(gpth.hdr_sig)))
		return -1;

	crc = gpth.hdr_crc_self;
	gpth.hdr_crc_self = 0;
	gpth.hdr_crc_self = crc32(0, (const void *)&gpth, GPT_HDR_SIZE);
	if (gpth.hdr_crc_self != crc) {
		return -1;
	}

	if (gpth.hdr_lba_self + rf_offset != sector)
		return -1;

#ifdef _STANDALONE
	bi_wedge.matchblk = sector;
	bi_wedge.matchnblks = 1;

	md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
#endif

	sectors = sizeof(d->buf)/d->ll.secsize; /* sectors per buffer */
	entries = sizeof(d->buf)/gpth.hdr_entsz; /* entries per buffer */
	entblk = gpth.hdr_lba_table + rf_offset;
	crc = crc32(0, NULL, 0);

	j = 0;
	ep = (const struct gpt_ent *)d->buf;

	for (entry = 0; entry < gpth.hdr_entries; entry += entries) {
		size = MIN(sizeof(d->buf),
		    (gpth.hdr_entries - entry) * gpth.hdr_entsz);
		entries = size / gpth.hdr_entsz;
		sectors = roundup(size, d->ll.secsize) / d->ll.secsize;
		if (readsects(&d->ll, entblk, sectors, d->buf, 1))
			return -1;
		entblk += sectors;
		crc = crc32(crc, (const void *)d->buf, size);

		for (i = 0; j < BIOSDISKNPART && i < entries; i++) {
			u = (const struct uuid *)ep[i].ent_type;
			if (!guid_is_nil(u)) {
				d->part[j].offset = ep[i].ent_lba_start;
				d->part[j].size = ep[i].ent_lba_end -
				    ep[i].ent_lba_start + 1;
				if (guid_is_equal(u, &GET_nbsd_ffs))
					d->part[j].fstype = FS_BSDFFS;
				else if (guid_is_equal(u, &GET_nbsd_lfs))
					d->part[j].fstype = FS_BSDLFS;
				else if (guid_is_equal(u, &GET_nbsd_raid))
					d->part[j].fstype = FS_RAID;
				else if (guid_is_equal(u, &GET_nbsd_swap))
					d->part[j].fstype = FS_SWAP;
				else if (guid_is_equal(u, &GET_nbsd_ccd))
					d->part[j].fstype = FS_CCD;
				else if (guid_is_equal(u, &GET_nbsd_cgd))
					d->part[j].fstype = FS_CGD;
				else
					d->part[j].fstype = FS_OTHER;
#ifndef NO_GPT
				for (int k = 0;
				     k < __arraycount(gpt_parts);
				     k++) {
					if (guid_is_equal(u, gpt_parts[k].guid))
						d->part[j].guid = &gpt_parts[k];
				}
				d->part[j].attr = ep[i].ent_attr;

				d->part[j].part_name =
				    alloc(BIOSDISK_PART_NAME_LEN);
				part_name_utf8(ep[i].ent_name,
					       sizeof(ep[i].ent_name),
					       d->part[j].part_name,
					       BIOSDISK_PART_NAME_LEN);
#endif
				j++;
			}
		}

	}

	if (crc != gpth.hdr_crc_table) {
#ifdef DISK_DEBUG	
		printf("GPT table CRC invalid\n");
#endif
		return -1;
	}

	return 0;
}

static int
read_gpt(struct biosdisk *d, daddr_t rf_offset, daddr_t rf_size)
{
	struct biosdisk_extinfo ed;
	daddr_t gptsector[2];
	int i, error;

	if (d->ll.type != BIOSDISK_TYPE_HD)
		/* No GPT on floppy and CD */
		return -1;

	if (rf_offset && rf_size) {
		gptsector[0] = rf_offset + GPT_HDR_BLKNO;
		gptsector[1] = rf_offset + rf_size - 1;
	} else {
		gptsector[0] = GPT_HDR_BLKNO;
		if (set_geometry(&d->ll, &ed) == 0 &&
		    d->ll.flags & BIOSDISK_INT13EXT) {
			gptsector[1] = ed.totsec - 1;
			/* Sanity check values returned from BIOS */
			if (ed.sbytes >= 512 &&
			    (ed.sbytes & (ed.sbytes - 1)) == 0)
				d->ll.secsize = ed.sbytes;
		} else {
#ifdef DISK_DEBUG
			printf("Unable to determine extended disk geometry - "
				"using CHS\n");
#endif
			/* at least try some other reasonable values then */
			gptsector[1] = d->ll.chs_sectors - 1;
		}
	}

	for (i = 0; i < __arraycount(gptsector); i++) {
		error = check_gpt(d, rf_offset, gptsector[i]);
		if (error == 0)
			break;
	}

	if (i >= __arraycount(gptsector)) {
		memset(d->part, 0, sizeof(d->part));
		return -1;
	}

#ifndef USE_SECONDARY_GPT
	if (i > 0) {
#ifdef DISK_DEBUG
		printf("ignoring valid secondary GPT\n");
#endif
		return -1;
	}
#endif

#ifdef DISK_DEBUG
	printf("using %s GPT\n", (i == 0) ? "primary" : "secondary");
#endif
	return 0;
}
#endif	/* !NO_GPT */

#ifndef NO_DISKLABEL
static void
ingest_label(struct biosdisk *d, struct disklabel *lp)
{
	int part;

	memset(d->part, 0, sizeof(d->part));

	for (part = 0; part < lp->d_npartitions; part++) {
		if (lp->d_partitions[part].p_size == 0)
			continue;
		if (lp->d_partitions[part].p_fstype == FS_UNUSED)
			continue;
		d->part[part].fstype = lp->d_partitions[part].p_fstype;
		d->part[part].offset = lp->d_partitions[part].p_offset;
		d->part[part].size = lp->d_partitions[part].p_size;
	}
}

static int
check_label(struct biosdisk *d, daddr_t sector)
{
	struct disklabel *lp;

	/* find partition in NetBSD disklabel */
	if (readsects(&d->ll, sector + LABELSECTOR, 1, d->buf, 0)) {
#ifdef DISK_DEBUG
		printf("Error reading disklabel\n");
#endif
		return EIO;
	}
	lp = (struct disklabel *) (d->buf + LABELOFFSET);
	if (lp->d_magic != DISKMAGIC || dkcksum(lp)) {
#ifdef DISK_DEBUG
		printf("warning: no disklabel in sector %"PRId64"\n", sector);
#endif
		return -1;
	}

	ingest_label(d, lp);

	bi_disk.labelsector = sector + LABELSECTOR;
	bi_disk.label.type = lp->d_type;
	memcpy(bi_disk.label.packname, lp->d_packname, 16);
	bi_disk.label.checksum = lp->d_checksum;

	bi_wedge.matchblk = sector + LABELSECTOR;
	bi_wedge.matchnblks = 1;

	md5(bi_wedge.matchhash, d->buf, d->ll.secsize);

	return 0;
}

static int
read_minix_subp(struct biosdisk *d, struct disklabel* dflt_lbl,
			int this_ext, daddr_t sector)
{
	struct mbr_partition mbr[MBR_PART_COUNT];
	int i;
	int typ;
	struct partition *p;

	if (readsects(&d->ll, sector, 1, d->buf, 0)) {
#ifdef DISK_DEBUG
		printf("Error reading MFS sector %"PRId64"\n", sector);
#endif
		return EIO;
	}
	if ((uint8_t)d->buf[510] != 0x55 || (uint8_t)d->buf[511] != 0xAA) {
		return -1;
	}
	memcpy(&mbr, MBR_PARTS(d->buf), sizeof(mbr));
	for (i = 0; i < MBR_PART_COUNT; i++) {
		typ = mbr[i].mbrp_type;
		if (typ == 0)
			continue;
		sector = this_ext + mbr[i].mbrp_start;
		if (dflt_lbl->d_npartitions >= MAXPARTITIONS)
			continue;
		p = &dflt_lbl->d_partitions[dflt_lbl->d_npartitions++];
		p->p_offset = sector;
		p->p_size = mbr[i].mbrp_size;
		p->p_fstype = xlat_mbr_fstype(typ);
	}
	return 0;
}

#if defined(EFIBOOT) && defined(SUPPORT_CD9660)
static int
check_cd9660(struct biosdisk *d)
{
	struct biosdisk_extinfo ed;
	struct iso_primary_descriptor *vd;
	daddr_t bno;

	for (bno = 16;; bno++) {
		if (readsects(&d->ll, bno, 1, d->buf, 0))
			return -1;
		vd = (struct iso_primary_descriptor *)d->buf;
		if (memcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
			return -1;
		if (isonum_711(vd->type) == ISO_VD_END)
			return -1;
		if (isonum_711(vd->type) == ISO_VD_PRIMARY)
			break;
	}
	if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
		return -1;

	if (set_geometry(&d->ll, &ed))
		return -1;

	memset(d->part, 0, sizeof(d->part));
	d->part[0].fstype = FS_ISO9660;
	d->part[0].offset = 0;
	d->part[0].size = ed.totsec;
	return 0;
}
#endif

static int
read_label(struct biosdisk *d, daddr_t offset)
{
	struct disklabel dflt_lbl;
	struct mbr_partition mbr[MBR_PART_COUNT];
	struct partition *p;
	uint32_t sector;
	int i;
	int error;
	int typ;
	uint32_t ext_base, this_ext, next_ext;
#ifdef COMPAT_386BSD_MBRPART
	int sector_386bsd = -1;
#endif

	memset(&dflt_lbl, 0, sizeof(dflt_lbl));
	dflt_lbl.d_npartitions = 8;

	d->boff = 0;

	if (d->ll.type != BIOSDISK_TYPE_HD)
		/* No label on floppy and CD */
		return -1;

	/*
	 * find NetBSD Partition in DOS partition table
	 * XXX check magic???
	 */
	ext_base = offset;
	next_ext = offset;
	for (;;) {
		this_ext = ext_base + next_ext;
		next_ext = offset;
		if (readsects(&d->ll, this_ext, 1, d->buf, 0)) {
#ifdef DISK_DEBUG
			printf("error reading MBR sector %u\n", this_ext);
#endif
			return EIO;
		}
		memcpy(&mbr, MBR_PARTS(d->buf), sizeof(mbr));
		/* Look for NetBSD partition ID */
		for (i = 0; i < MBR_PART_COUNT; i++) {
			typ = mbr[i].mbrp_type;
			if (typ == 0)
				continue;
			sector = this_ext + mbr[i].mbrp_start;
#ifdef DISK_DEBUG
			printf("ptn type %d in sector %u\n", typ, sector);
#endif
                        if (typ == MBR_PTYPE_MINIX_14B) {
				if (!read_minix_subp(d, &dflt_lbl,
						   this_ext, sector)) {
					/* Don't add "container" partition */
					continue;
				}
			}
			if (typ == MBR_PTYPE_NETBSD) {
				error = check_label(d, sector);
				if (error >= 0)
					return error;
			}
			if (MBR_IS_EXTENDED(typ)) {
				next_ext = mbr[i].mbrp_start + offset;
				continue;
			}
#ifdef COMPAT_386BSD_MBRPART
			if (this_ext == offset && typ == MBR_PTYPE_386BSD)
				sector_386bsd = sector;
#endif
			if (this_ext != offset) {
				if (dflt_lbl.d_npartitions >= MAXPARTITIONS)
					continue;
				p = &dflt_lbl.d_partitions[dflt_lbl.d_npartitions++];
			} else
				p = &dflt_lbl.d_partitions[i];
			p->p_offset = sector;
			p->p_size = mbr[i].mbrp_size;
			p->p_fstype = xlat_mbr_fstype(typ);
		}
		if (next_ext == offset)
			break;
		if (ext_base == offset) {
			ext_base = next_ext;
			next_ext = offset;
		}
	}

	sector = offset;
#ifdef COMPAT_386BSD_MBRPART
	if (sector_386bsd != -1) {
		printf("old BSD partition ID!\n");
		sector = sector_386bsd;
	}
#endif

	/*
	 * One of two things:
	 * 	1. no MBR
	 *	2. no NetBSD partition in MBR
	 *
	 * We simply default to "start of disk" in this case and
	 * press on.
	 */
	error = check_label(d, sector);
	if (error >= 0)
		return error;

#if defined(EFIBOOT) && defined(SUPPORT_CD9660)
	/* Check CD/DVD */
	error = check_cd9660(d);
	if (error >= 0)
		return error;
#endif

	/*
	 * Nothing at start of disk, return info from mbr partitions.
	 */
	/* XXX fill it to make checksum match kernel one */
	dflt_lbl.d_checksum = dkcksum(&dflt_lbl);
	ingest_label(d, &dflt_lbl);
	return 0;
}
#endif /* NO_DISKLABEL */

#if !defined(NO_DISKLABEL) || !defined(NO_GPT)
static int
read_partitions(struct biosdisk *d, daddr_t offset, daddr_t size)
{
	int error;

	error = -1;

#ifndef NO_GPT
	error = read_gpt(d, offset, size);
	if (error == 0)
		return 0;

#endif
#ifndef NO_DISKLABEL
	error = read_label(d, offset);
#endif
	return error;
}
#endif

#ifndef NO_RAIDFRAME
static void
raidframe_probe(struct raidframe *raidframe, int *raidframe_count,
		struct biosdisk *d, int part)
{
	int i = *raidframe_count;
	struct RF_ComponentLabel_s label;
	daddr_t offset;

	if (i + 1 > RAIDFRAME_NDEV)
		return;

	offset = d->part[part].offset;
	if ((biosdisk_read_raidframe(d->ll.dev, offset, &label)) != 0)
		return;

	if (label.version != RF_COMPONENT_LABEL_VERSION)
		printf("Unexpected raidframe label version\n");

	raidframe[i].last_unit = label.last_unit;
	raidframe[i].serial = label.serial_number;
	raidframe[i].biosdev = d->ll.dev;
	raidframe[i].parent_part = part;
#ifndef NO_GPT
	if (d->part[part].part_name)
		strlcpy(raidframe[i].parent_name,
			d->part[part].part_name, MAXDEVNAME);
	else
		raidframe[i].parent_name[0] = '\0';
#endif
	raidframe[i].offset = offset;
	raidframe[i].size = label.__numBlocks;

	(*raidframe_count)++;

	return;
}
#endif

void
biosdisk_probe(void)
{
	struct biosdisk *d;
	struct biosdisk_extinfo ed;
#ifndef NO_RAIDFRAME
	struct raidframe raidframe[RAIDFRAME_NDEV];
	int raidframe_count = 0;
#endif
	uint64_t size;
	int first;
	int i;
#if !defined(NO_DISKLABEL) || !defined(NO_GPT)
	int part;
#endif

	for (i = 0; i < MAX_BIOSDISKS + 2; i++) {
		first = 1;
		d = alloc(sizeof(*d));
		if (d == NULL) {
			printf("Out of memory\n");
			return;
		}
		memset(d, 0, sizeof(*d));
		memset(&ed, 0, sizeof(ed));
		if (i >= MAX_BIOSDISKS)
			d->ll.dev = 0x00 + i - MAX_BIOSDISKS;	/* fd */
		else
			d->ll.dev = 0x80 + i;			/* hd/cd */
		if (set_geometry(&d->ll, &ed))
			goto next_disk;
		printf("disk ");
		switch (d->ll.type) {
		case BIOSDISK_TYPE_CD:
			printf("cd0\n  cd0a\n");
			break;
		case BIOSDISK_TYPE_FD:
			printf("fd%d\n", d->ll.dev & 0x7f);
			printf("  fd%da\n", d->ll.dev & 0x7f);
			break;
		case BIOSDISK_TYPE_HD:
			printf("hd%d", d->ll.dev & 0x7f);
			if (d->ll.flags & BIOSDISK_INT13EXT) {
				printf(" size ");
				size = ed.totsec * ed.sbytes;
				if (size >= (10ULL * 1024 * 1024 * 1024))
					printf("%"PRIu64" GB",
					    size / (1024 * 1024 * 1024));
				else
					printf("%"PRIu64" MB",
					    size / (1024 * 1024));
			}
			printf("\n");
			break;
		}
#if !defined(NO_DISKLABEL) || !defined(NO_GPT)
		if (d->ll.type != BIOSDISK_TYPE_HD)
			goto next_disk;

		if (read_partitions(d, 0, 0) != 0)
			goto next_disk;

		for (part = 0; part < BIOSDISKNPART; part++) {
			if (d->part[part].size == 0)
				continue;
			if (d->part[part].fstype == FS_UNUSED)
				continue;
#ifndef NO_RAIDFRAME
			if (d->part[part].fstype == FS_RAID)
				raidframe_probe(raidframe,
						&raidframe_count, d, part);
#endif
			if (first) {
				printf(" ");
				first = 0;
			}
#ifndef NO_GPT
			if (d->part[part].part_name &&
			    d->part[part].part_name[0])
				printf(" NAME=%s(", d->part[part].part_name);
			else
#endif
				printf(" hd%d%c(", d->ll.dev & 0x7f, part + 'a');

#ifndef NO_GPT
			if (d->part[part].guid != NULL)
				printf("%s", d->part[part].guid->name);
			else
#endif

			if (d->part[part].fstype < FSMAXTYPES)
				printf("%s",
				  fstypenames[d->part[part].fstype]);
			else
				printf("%d", d->part[part].fstype);
			printf(")");
		}
#endif
		if (first == 0)
			printf("\n");

next_disk:
		dealloc_biosdisk(d);
	}

#ifndef NO_RAIDFRAME
	for (i = 0; i < raidframe_count; i++) {
		size_t secsize;

		if ((d = alloc_biosdisk(raidframe[i].biosdev)) == NULL) {
			printf("Out of memory\n");
			return;
		}

		secsize = d->ll.secsize;

		printf("raidframe raid%d serial %d in ",
		       raidframe[i].last_unit, raidframe[i].serial);
#ifndef NO_GPT
		if (raidframe[i].parent_name[0])
			printf("NAME=%s size ", raidframe[i].parent_name);
		else
#endif
		printf("hd%d%c size ", d->ll.dev & 0x7f,
		       raidframe[i].parent_part + 'a');
		if (raidframe[i].size >= (10ULL * 1024 * 1024 * 1024 / secsize))
			printf("%"PRIu64" GB",
			    raidframe[i].size / (1024 * 1024 * 1024 / secsize));
		else
			printf("%"PRIu64" MB",
			    raidframe[i].size / (1024 * 1024 / secsize));
		printf("\n");

		if (read_partitions(d,
		    raidframe[i].offset + RF_PROTECTED_SECTORS,
		    raidframe[i].size) != 0)
			goto next_raidrame;

		first = 1;
		for (part = 0; part < BIOSDISKNPART; part++) {
#ifndef NO_GPT
			bool bootme = d->part[part].attr & GPT_ENT_ATTR_BOOTME;
#else
			bool bootme = 0;
#endif

			if (d->part[part].size == 0)
				continue;
			if (d->part[part].fstype == FS_UNUSED)
				continue;
			if (d->part[part].fstype == FS_RAID)
				continue;
			if (first) {
				printf(" ");
				first = 0;
			}
#ifndef NO_GPT
			if (d->part[part].part_name &&
			    d->part[part].part_name[0])
				printf(" NAME=%s(", d->part[part].part_name);
			else
#endif
				printf(" raid%d%c(", raidframe[i].last_unit,
				       part + 'a');
#ifndef NO_GPT
			if (d->part[part].guid != NULL)
				printf("%s", d->part[part].guid->name);
			else
#endif
			if (d->part[part].fstype < FSMAXTYPES)
				printf("%s",
				  fstypenames[d->part[part].fstype]);
			else
				printf("%d", d->part[part].fstype);
			printf("%s)", bootme ? ", bootme" : "");
		}

next_raidrame:
		if (first == 0)
			printf("\n");

		dealloc_biosdisk(d);
	}
#endif
}

/* Determine likely partition for possible sector number of dos
 * partition.
 */

int
biosdisk_findpartition(int biosdev, daddr_t sector,
		       int *partition, const char **part_name)
{
#if defined(NO_DISKLABEL) && defined(NO_GPT)
	*partition = 0;
	if (part_name)
		*part_name = NULL;
	return 0;
#else
	int i;
	struct biosdisk *d;
	int biosboot_sector_part = -1;
	int bootable_fs_part = -1;
	int boot_part = 0;
#ifndef NO_GPT
	int gpt_bootme_part = -1;
	static char namebuf[MAXDEVNAME + 1];
#endif

#ifdef DISK_DEBUG
	printf("looking for partition device %x, sector %"PRId64"\n", biosdev, sector);
#endif

	/* default to first partition */
	*partition = 0;
	if (part_name)
		*part_name = NULL;

	/* Look for netbsd partition that is the dos boot one */
	d = alloc_biosdisk(biosdev);
	if (d == NULL)
		return -1;

	if (read_partitions(d, 0, 0) == 0) {
		for (i = 0; i < BIOSDISKNPART; i++) {
			if (d->part[i].fstype == FS_UNUSED)
				continue;

			if (d->part[i].offset == sector &&
			    biosboot_sector_part == -1)
				biosboot_sector_part = i;

#ifndef NO_GPT
			if (d->part[i].attr & GPT_ENT_ATTR_BOOTME &&
			    gpt_bootme_part == -1)
				gpt_bootme_part = i;
#endif
			switch (d->part[i].fstype) {
			case FS_BSDFFS:
			case FS_BSDLFS:
			case FS_RAID:
			case FS_CCD:
			case FS_CGD:
			case FS_ISO9660:
				if (bootable_fs_part == -1)
					bootable_fs_part = i;
				break;

			default:
				break;
			}
		}

#ifndef NO_GPT
		if (gpt_bootme_part != -1)
			boot_part = gpt_bootme_part;
		else
#endif
		if (biosboot_sector_part != -1)
			boot_part = biosboot_sector_part;
		else if (bootable_fs_part != -1)
			boot_part = bootable_fs_part;
		else
			boot_part = 0;

		*partition = boot_part;
#ifndef NO_GPT
		if (part_name &&
		    d->part[boot_part].part_name &&
		    d->part[boot_part].part_name[0]) {
			strlcpy(namebuf, d->part[boot_part].part_name,
				BIOSDISK_PART_NAME_LEN);
			*part_name = namebuf;
		}
#endif
	}

	dealloc_biosdisk(d);
	return 0;
#endif /* NO_DISKLABEL && NO_GPT */
}

int
biosdisk_readpartition(int biosdev, daddr_t offset, daddr_t size,
    struct biosdisk_partition **partpp, int *rnum)
{
#if defined(NO_DISKLABEL) && defined(NO_GPT)
	return ENOTSUP;
#else
	struct biosdisk *d;
	struct biosdisk_partition *part;
	int rv;

	/* Look for netbsd partition that is the dos boot one */
	d = alloc_biosdisk(biosdev);
	if (d == NULL)
		return ENOMEM;

	if (read_partitions(d, offset, size)) {
		rv = EINVAL;
		goto out;
	}

	part = copy_biosdisk_part(d);
	if (part == NULL) {
		rv = ENOMEM;
		goto out;
	}

	*partpp = part;
	*rnum = (int)__arraycount(d->part);
	rv = 0;
out:
	dealloc_biosdisk(d);
	return rv;
#endif /* NO_DISKLABEL && NO_GPT */
}

#ifndef NO_RAIDFRAME
int
biosdisk_read_raidframe(int biosdev, daddr_t offset,
			struct RF_ComponentLabel_s *label)
{
#if defined(NO_DISKLABEL) && defined(NO_GPT)
	return ENOTSUP;
#else
	struct biosdisk *d;
	struct biosdisk_extinfo ed;
	daddr_t size;
	int rv = -1;

	/* Look for netbsd partition that is the dos boot one */
	d = alloc_biosdisk(biosdev);
	if (d == NULL)
		goto out;

	if (d->ll.type != BIOSDISK_TYPE_HD)
		/* No raidframe on floppy and CD */
		goto out;

	if (set_geometry(&d->ll, &ed) != 0)
		goto out;

	/* Sanity check values returned from BIOS */
	if (ed.sbytes >= 512 &&
	    (ed.sbytes & (ed.sbytes - 1)) == 0)
		d->ll.secsize = ed.sbytes;

	offset += (RF_COMPONENT_INFO_OFFSET / d->ll.secsize);
	size = roundup(sizeof(*label), d->ll.secsize) / d->ll.secsize;
	if (readsects(&d->ll, offset, size, d->buf, 0))
		goto out;
	memcpy(label, d->buf, sizeof(*label));
	rv = 0;
out:
	if (d != NULL)
		dealloc_biosdisk(d);
	return rv;
#endif /* NO_DISKLABEL && NO_GPT */
}
#endif /* NO_RAIDFRAME */

#ifdef _STANDALONE
static void
add_biosdisk_bootinfo(void)
{
	if (bootinfo == NULL) {
		return;
	}
	BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
	BI_ADD(&bi_wedge, BTINFO_BOOTWEDGE, sizeof(bi_wedge));
	return;
}
#endif

#ifndef NO_GPT
static daddr_t
raidframe_part_offset(struct biosdisk *d, int part)
{
	struct biosdisk raidframe;
	daddr_t rf_offset;
	daddr_t rf_size;
	int i, candidate;

	memset(&raidframe, 0, sizeof(raidframe));
	raidframe.ll = d->ll;

	rf_offset = d->part[part].offset + RF_PROTECTED_SECTORS;
	rf_size = d->part[part].size;
	if (read_gpt(&raidframe, rf_offset, rf_size) != 0)
		return RF_PROTECTED_SECTORS;

	candidate = 0;
	for (i = 0; i < BIOSDISKNPART; i++) {
		if (raidframe.part[i].size == 0)
			continue;
		if (raidframe.part[i].fstype == FS_UNUSED)
			continue;
#ifndef NO_GPT
		if (raidframe.part[i].attr & GPT_ENT_ATTR_BOOTME)
			candidate = i;
#endif
	}

	return RF_PROTECTED_SECTORS + raidframe.part[candidate].offset;
}
#endif

int
biosdisk_open(struct open_file *f, ...)
/* struct open_file *f, int biosdev, int partition */
{
	va_list ap;
	struct biosdisk *d;
	int biosdev;
	int partition;
	int error = 0;

	va_start(ap, f);
	biosdev = va_arg(ap, int);
	d = alloc_biosdisk(biosdev);
	if (d == NULL) {
		error = ENXIO;
		goto out;
	}

	partition = va_arg(ap, int);
	bi_disk.biosdev = d->ll.dev;
	bi_disk.partition = partition;
	bi_disk.labelsector = -1;

	bi_wedge.biosdev = d->ll.dev;
	bi_wedge.matchblk = -1;

#if !defined(NO_DISKLABEL) || !defined(NO_GPT)
	error = read_partitions(d, 0, 0);
	if (error == -1) {
		error = 0;
		goto nolabel;
	}
	if (error)
		goto out;

	if (partition >= BIOSDISKNPART ||
	    d->part[partition].fstype == FS_UNUSED) {
#ifdef DISK_DEBUG
		printf("illegal partition\n");
#endif
		error = EPART;
		goto out;
	}

	d->boff = d->part[partition].offset;

	if (d->part[partition].fstype == FS_RAID)
#ifndef NO_GPT
		d->boff += raidframe_part_offset(d, partition);
#else
		d->boff += RF_PROTECTED_SECTORS;
#endif

#ifdef _STANDALONE
	bi_wedge.startblk = d->part[partition].offset;
	bi_wedge.nblks = d->part[partition].size;
#endif

nolabel:
#endif
#ifdef DISK_DEBUG
	printf("partition @%"PRId64"\n", d->boff);
#endif

#ifdef _STANDALONE
	add_biosdisk_bootinfo();
#endif

	f->f_devdata = d;
out:
        va_end(ap);
	if (error)
		dealloc_biosdisk(d);
	return error;
}

#ifndef NO_GPT
static int
biosdisk_find_name(const char *fname, int *biosdev,
		   daddr_t *offset, daddr_t *size)
{
	struct biosdisk *d;
	char name[MAXDEVNAME + 1];
	char *sep;
#ifndef NO_RAIDFRAME
	struct raidframe raidframe[RAIDFRAME_NDEV];
	int raidframe_count = 0;
#endif
	int i;
	int part;
	int ret = -1;

	/* Strip leadinf NAME= and cut after the coloon included */
	strlcpy(name, fname + 5, MAXDEVNAME);
	sep = strchr(name, ':');
	if (sep)
		*sep = '\0';

	for (i = 0; i < MAX_BIOSDISKS; i++) {
		d = alloc(sizeof(*d));
		if (d == NULL) {
			printf("Out of memory\n");
			goto out;
		}

		memset(d, 0, sizeof(*d));
		d->ll.dev = 0x80 + i;			/* hd/cd */
		if (set_geometry(&d->ll, NULL))
			goto next_disk;

		if (d->ll.type != BIOSDISK_TYPE_HD)
			goto next_disk;

		if (read_partitions(d, 0, 0) != 0)
			goto next_disk;

		for (part = 0; part < BIOSDISKNPART; part++) {
			if (d->part[part].size == 0)
				continue;
			if (d->part[part].fstype == FS_UNUSED)
				continue;
#ifndef NO_RAIDFRAME
			if (d->part[part].fstype == FS_RAID) {
				raidframe_probe(raidframe,
						&raidframe_count, d, part);
				/*
				 * Do not match RAID partition for a name,
				 * we want to report an inner partition.
				 */
				continue;
			}
#endif
			if (d->part[part].part_name != NULL &&
			    strcmp(d->part[part].part_name, name) == 0) {
				*biosdev = d->ll.dev;
				*offset = d->part[part].offset;
				*size = d->part[part].size;
				ret = 0;
				goto out;
			}

		}
next_disk:
		dealloc_biosdisk(d);
		d = NULL;
	}

#ifndef NO_RAIDFRAME
	for (i = 0; i < raidframe_count; i++) {
		int candidate = -1;

		if ((d = alloc_biosdisk(raidframe[i].biosdev)) == NULL) {
			printf("Out of memory\n");
			goto out;
		}

		if (read_partitions(d,
		    raidframe[i].offset + RF_PROTECTED_SECTORS,
		    raidframe[i].size) != 0)
			goto next_raidframe;

		for (part = 0; part < BIOSDISKNPART; part++) {
			bool bootme = d->part[part].attr & GPT_ENT_ATTR_BOOTME;
			if (d->part[part].size == 0)
				continue;
			if (d->part[part].fstype == FS_UNUSED)
				continue;

			if (d->part[part].part_name != NULL &&
			    strcmp(d->part[part].part_name, name) == 0) {
				*biosdev = raidframe[i].biosdev;
				*offset = raidframe[i].offset
					+ RF_PROTECTED_SECTORS
					+ d->part[part].offset;
				*size = d->part[part].size;
				ret = 0;
				goto out;
			}
			if (strcmp(raidframe[i].parent_name, name) == 0) {
				if (candidate == -1 || bootme)
					candidate = part;
				continue;
			}
		}

		if (candidate != -1) {
			*biosdev = raidframe[i].biosdev;
			*offset = raidframe[i].offset
				+ RF_PROTECTED_SECTORS
				+ d->part[candidate].offset;
			*size = d->part[candidate].size;
			ret = 0;
			goto out;
		}

next_raidframe:
		dealloc_biosdisk(d);
		d = NULL;
	}
#endif

out:
	if (d != NULL)
		dealloc_biosdisk(d);

	return ret;
}
#endif

#ifndef NO_RAIDFRAME
static int
biosdisk_find_raid(const char *name, int *biosdev,
		   daddr_t *offset, daddr_t *size)
{
	struct biosdisk *d = NULL;
	struct raidframe raidframe[RAIDFRAME_NDEV];
	int raidframe_count = 0;
	int i;
	int target_unit = 0;
	int target_part;
	int part;
	int ret = -1;

	if (strstr(name, "raid") != name)
		goto out;

#define isnum(c) ((c) >= '0' && (c) <= '9')
	i = 4; /* skip leading "raid" */
	if (!isnum(name[i]))
		goto out;
	do {
		target_unit *= 10;
		target_unit += name[i++] - '0';
	} while (isnum(name[i]));

#define isvalidpart(c) ((c) >= 'a' && (c) <= 'z')

	if (!isvalidpart(name[i]))
		goto out;
	target_part = name[i] - 'a';

	for (i = 0; i < MAX_BIOSDISKS; i++) {
		d = alloc(sizeof(*d));
		if (d == NULL) {
			printf("Out of memory\n");
			goto out;
		}

		memset(d, 0, sizeof(*d));
		d->ll.dev = 0x80 + i;			/* hd/cd */
		if (set_geometry(&d->ll, NULL))
			goto next_disk;

		if (d->ll.type != BIOSDISK_TYPE_HD)
			goto next_disk;

		if (read_partitions(d, 0, 0) != 0)
			goto next_disk;

		for (part = 0; part < BIOSDISKNPART; part++) {
			if (d->part[part].size == 0)
				continue;
			if (d->part[part].fstype != FS_RAID)
				continue;
			raidframe_probe(raidframe,
					&raidframe_count, d, part);

		}
next_disk:
		dealloc_biosdisk(d);
		d = NULL;
	}

	for (i = 0; i < raidframe_count; i++) {
		if (raidframe[i].last_unit != target_unit)
			continue;

		if ((d = alloc_biosdisk(raidframe[i].biosdev)) == NULL) {
			printf("Out of memory\n");
			goto out;
		}

		if (read_partitions(d,
		    raidframe[i].offset + RF_PROTECTED_SECTORS,
		    raidframe[i].size) != 0)
			goto next_raidframe;

		for (part = 0; part < BIOSDISKNPART; part++) {
			if (d->part[part].size == 0)
				continue;
			if (d->part[part].fstype == FS_UNUSED)
				continue;
			if (part == target_part) {
				*biosdev = raidframe[i].biosdev;
				*offset = raidframe[i].offset
					+ RF_PROTECTED_SECTORS
					+ d->part[part].offset;
				*size = d->part[part].size;
				ret = 0;
				goto out;
			}
		}
next_raidframe:
		dealloc_biosdisk(d);
		d = NULL;
	}
out:
	if (d != NULL)
		dealloc_biosdisk(d);

	return ret;
}
#endif

int
biosdisk_open_name(struct open_file *f, const char *name)
{
#if defined(NO_GPT) && defined(NO_RAIDFRAME)
	return ENXIO;
#else
	struct biosdisk *d = NULL;
	int biosdev;
	daddr_t offset;
	daddr_t size;
	int error = -1;

#ifndef NO_GPT
	if (strstr(name, "NAME=") == name)
		error = biosdisk_find_name(name, &biosdev, &offset, &size);
#endif
#ifndef NO_RAIDFRAME
	if (strstr(name, "raid") == name)
		error = biosdisk_find_raid(name, &biosdev, &offset, &size);
#endif

	if (error != 0) {
		printf("%s not found\n", name);
		error = ENXIO;
		goto out;
	}

	d = alloc_biosdisk(biosdev);
	if (d == NULL) {
		error = ENXIO;
		goto out;
	}

	bi_disk.biosdev = d->ll.dev;
	bi_disk.partition = 0;
	bi_disk.labelsector = -1;

	bi_wedge.biosdev = d->ll.dev;

	/*
	 * If we did not get wedge match info from check_gpt()
	 * compute it now.
	 */
	if (bi_wedge.matchblk == -1) {
		if (readsects(&d->ll, offset, 1, d->buf, 1)) {
#ifdef DISK_DEBUG
       			printf("Error reading sector at %"PRId64"\n", offset);
#endif
			error =  EIO;
			goto out;
		}

		bi_wedge.matchblk = offset;
		bi_wedge.matchnblks = 1;

		md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
	}

	d->boff = offset;

	bi_wedge.startblk = offset;
	bi_wedge.nblks = size;

#ifdef _STANDALONE
	add_biosdisk_bootinfo();
#endif

	f->f_devdata = d;
out:
	if (error && d != NULL)
		dealloc_biosdisk(d);
	return error;
#endif
}



#ifndef LIBSA_NO_FS_CLOSE
int
biosdisk_close(struct open_file *f)
{
	struct biosdisk *d = f->f_devdata;

	/* let the floppy drive go off */
	if (d->ll.type == BIOSDISK_TYPE_FD)
		wait_sec(3);	/* 2s is enough on all PCs I found */

	dealloc_biosdisk(d);
	f->f_devdata = NULL;
	return 0;
}
#endif

int
biosdisk_ioctl(struct open_file *f, u_long cmd, void *arg)
{
	return EIO;
}