#include <sys/param.h>
#include <sys/bootblock.h>
#include <lib/libkern/libkern.h>
#include <lib/libsa/byteorder.h>
#include <lib/libsa/stand.h>
#include "mbr.h"
static int find_mbr_part(struct of_dev *, uint32_t, char *,
struct disklabel *, uint32_t, uint8_t, int);
static void make_dos_label(struct disklabel *, uint32_t);
int
search_mbr_label(struct of_dev *devp, u_long off, char *buf,
struct disklabel *lp, u_long off0)
{
static uint8_t fat_types[] = {
MBR_PTYPE_FAT12, MBR_PTYPE_FAT16S, MBR_PTYPE_FAT16B,
MBR_PTYPE_FAT32, MBR_PTYPE_FAT32L, MBR_PTYPE_FAT16L
};
size_t read;
uint32_t poff;
int i;
poff = find_mbr_part(devp, off, buf, lp, 0, MBR_PTYPE_NETBSD, 0);
#ifdef COMPAT_386BSD_MBRPART
if (poff == 0) {
poff = find_mbr_part(devp, off, buf, lp, 0,
MBR_PTYPE_386BSD, 0);
if (poff != 0)
printf("WARNING: old BSD partition ID!\n");
}
#endif
if (poff != 0) {
if (strategy(devp, F_READ, poff + LABELSECTOR, DEV_BSIZE,
buf, &read) == 0 && read == DEV_BSIZE)
if (getdisklabel(buf, lp) == NULL)
return 0;
}
for (i = 0; i < sizeof(fat_types); i++) {
poff = find_mbr_part(devp, off, buf, lp, 0, fat_types[i], 0);
if (poff != 0) {
make_dos_label(lp, poff);
return 0;
}
}
return ERDLAB;
}
static int
find_mbr_part(struct of_dev *devp, uint32_t off, char *buf,
struct disklabel *lp, uint32_t off0, uint8_t ptype, int recursion)
{
size_t read;
struct mbr_partition *p;
int i;
uint32_t poff;
if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
|| read != DEV_BSIZE)
return 0;
if (*(uint16_t *)&buf[MBR_MAGIC_OFFSET] != sa_htole16(MBR_MAGIC))
return 0;
if (recursion++ <= 1)
off0 += off;
for (p = (struct mbr_partition *)(buf + MBR_PART_OFFSET), i = 0;
i < MBR_PART_COUNT; i++, p++) {
if (p->mbrp_type == ptype) {
recursion--;
return sa_le32toh(p->mbrp_start) + off0;
}
else if (p->mbrp_type == MBR_PTYPE_EXT) {
poff = find_mbr_part(devp, sa_le32toh(p->mbrp_start),
buf, lp, off0, ptype, recursion);
if (poff != 0) {
recursion--;
return poff;
}
if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
|| read != DEV_BSIZE) {
recursion--;
return 0;
}
}
}
return 0;
}
static void
make_dos_label(struct disklabel *lp, uint32_t poff)
{
int i;
lp->d_npartitions = RAW_PART + 1;
for (i = 0; i < MAXPARTITIONS; i++) {
lp->d_partitions[i].p_size = 0;
lp->d_partitions[i].p_offset = 0;
lp->d_partitions[i].p_fstype = 0;
}
lp->d_partitions[0].p_offset = poff;
lp->d_partitions[0].p_fstype = FS_MSDOS;
lp->d_magic = lp->d_magic2 = DISKMAGIC;
lp->d_checksum = 0;
lp->d_checksum = dkcksum(lp);
}