#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: vtoc.c,v 1.4 2008/04/28 20:23:18 martin Exp $");
#include <sys/types.h>
#include <sys/systm.h>
#if !defined(_KERNEL) && defined(_STANDALONE)
#include <lib/libsa/stand.h>
#include <lib/libkern/libkern.h>
#endif
#ifdef VTOC_DEBUG
#define DPRINTF(fmt, args...) printf(fmt, ##args)
#else
#define DPRINTF(arg...) ((void)0)
#endif
#include <machine/sector.h>
#include <machine/vtoc.h>
#ifdef VTOC_DEBUG
void vtoc_print_partition_table(const struct ux_partition *);
#endif
bool
vtoc_sector(void *rwops, struct vtoc_sector *vtoc, int start)
{
if (!sector_read(rwops, (void *)vtoc, start + VTOC_SECTOR))
return false;
if (!vtoc_sanity(vtoc))
return false;
return true;
}
const struct ux_partition *
vtoc_find_bfs(const struct vtoc_sector *vtoc)
{
int i;
if (!vtoc_valid(vtoc)) {
printf("invalid VTOC\n");
return 0;
}
for (i = 0; i < vtoc->npartitions; i++)
if (vtoc->partition[i].tag == VTOC_TAG_STAND)
break;
if (i == vtoc->npartitions) {
printf("no bfs partition found.\n");
return 0;
}
return &vtoc->partition[i];
}
bool
vtoc_valid(const struct vtoc_sector *vtoc)
{
return (vtoc->magic == VTOC_MAGIC) && (vtoc->version == VTOC_VERSION);
}
bool
vtoc_sanity(const struct vtoc_sector *vtoc)
{
if (!vtoc_valid(vtoc)) {
DPRINTF("Invalid VTOC.\n");
return false;
}
DPRINTF("[VTOC] (%d byte)\n", sizeof *vtoc);
DPRINTF("Bootinfo = %08x %08x %08x\n",
vtoc->bootinfo[0], vtoc->bootinfo[1], vtoc->bootinfo[2]);
DPRINTF("Magic = %08x\n", vtoc->magic);
DPRINTF("Version = %d\n", vtoc->version);
DPRINTF("Volume = %s\n", vtoc->volume);
DPRINTF("Sector size = %d\n", vtoc->sector_size_byte);
DPRINTF("# of partitions = %d\n", vtoc->npartitions);
#ifdef VTOC_DEBUG
vtoc_print_partition_table(vtoc->partition);
#endif
return true;
}
#ifdef VTOC_DEBUG
void
vtoc_print_partition_table(const struct ux_partition *p)
{
int i;
DPRINTF(" Partition Tag Flags Start Count Last\n");
for (i = 0; i < 16; i++, p++) {
DPRINTF(" %x %d %02x %8d %8d %8d\n",
i, p->tag, p->flags, p->start_sector, p->nsectors,
p->start_sector + p->nsectors - 1);
}
}
#endif