956a0e3c076406b83d635174a201fd8761ee5133vboxsync/* $Id$ */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync/** @file
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * IPRT Disk Volume Management API (DVM) - MBR format backend.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync/*
c7814cf6e1240a519cbec0441e033d0e2470ed00vboxsync * Copyright (C) 2011-2012 Oracle Corporation
956a0e3c076406b83d635174a201fd8761ee5133vboxsync *
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * available from http://www.virtualbox.org. This file is free software;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * you can redistribute it and/or modify it under the terms of the GNU
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * General Public License (GPL) as published by the Free Software
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync *
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * The contents of this file may alternatively be used under the terms
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * of the Common Development and Distribution License Version 1.0
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * VirtualBox OSE distribution, in which case the provisions of the
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * CDDL are applicable instead of those of the GPL.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync *
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * You may elect to license modified versions of this file under the
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * terms and conditions of either the GPL or the CDDL or both.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync/*******************************************************************************
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync* Header Files *
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync*******************************************************************************/
956a0e3c076406b83d635174a201fd8761ee5133vboxsync#include <iprt/types.h>
956a0e3c076406b83d635174a201fd8761ee5133vboxsync#include <iprt/assert.h>
956a0e3c076406b83d635174a201fd8761ee5133vboxsync#include <iprt/mem.h>
956a0e3c076406b83d635174a201fd8761ee5133vboxsync#include <iprt/dvm.h>
956a0e3c076406b83d635174a201fd8761ee5133vboxsync#include <iprt/string.h>
956a0e3c076406b83d635174a201fd8761ee5133vboxsync#include "internal/dvm.h"
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync/*******************************************************************************
956a0e3c076406b83d635174a201fd8761ee5133vboxsync* Structures and Typedefs *
956a0e3c076406b83d635174a201fd8761ee5133vboxsync*******************************************************************************/
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync/**
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * MBR volume manager data.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync */
956a0e3c076406b83d635174a201fd8761ee5133vboxsynctypedef struct RTDVMFMTINTERNAL
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** Pointer to the underlying disk. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PCRTDVMDISK pDisk;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** Number of initialized partitions. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync uint32_t cPartitions;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** The raw MBR data. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync uint8_t abMbr[512];
956a0e3c076406b83d635174a201fd8761ee5133vboxsync} RTDVMFMTINTERNAL;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync/** Pointer to the MBR volume manager. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsynctypedef RTDVMFMTINTERNAL *PRTDVMFMTINTERNAL;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync/**
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * MBR volume data.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync */
956a0e3c076406b83d635174a201fd8761ee5133vboxsynctypedef struct RTDVMVOLUMEFMTINTERNAL
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** Pointer to the volume manager. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PRTDVMFMTINTERNAL pVolMgr;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** Partition table entry index. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync uint32_t idxEntry;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** Start offset of the volume. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync uint64_t offStart;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** Size of the volume. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync uint64_t cbVolume;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** Pointer to the raw partition table entry. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync uint8_t *pbMbrEntry;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync} RTDVMVOLUMEFMTINTERNAL;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync/** Pointer to an MBR volume. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsynctypedef RTDVMVOLUMEFMTINTERNAL *PRTDVMVOLUMEFMTINTERNAL;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync/**
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * MBR FS type to DVM volume type mapping entry.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsynctypedef struct RTDVMMBRFS2VOLTYPE
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** MBR FS Id. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync uint8_t bFsId;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** DVM volume type. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync RTDVMVOLTYPE enmVolType;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync} RTDVMMBRFS2VOLTYPE;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync/** Pointer to a MBR FS Type to volume type mapping entry. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsynctypedef RTDVMMBRFS2VOLTYPE *PRTDVMMBRFS2VOLTYPE;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync/*******************************************************************************
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync* Global Variables *
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync*******************************************************************************/
956a0e3c076406b83d635174a201fd8761ee5133vboxsync/**
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Mapping of FS types to DVM volume types.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync *
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * From http://www.win.tue.nl/~aeb/partitions/partition_types-1.html
956a0e3c076406b83d635174a201fd8761ee5133vboxsync */
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncstatic const RTDVMMBRFS2VOLTYPE g_aFs2DvmVolTypes[] =
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync { 0x06, RTDVMVOLTYPE_FAT16 },
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync { 0x07, RTDVMVOLTYPE_NTFS }, /* Simplification: Used for HPFS, exFAT, ++, too but NTFS is the more common one. */
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync { 0x0b, RTDVMVOLTYPE_FAT32 },
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync { 0x0c, RTDVMVOLTYPE_FAT32 },
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync { 0x82, RTDVMVOLTYPE_LINUX_SWAP },
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync { 0x83, RTDVMVOLTYPE_LINUX_NATIVE },
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync { 0x8e, RTDVMVOLTYPE_LINUX_LVM },
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync { 0xa5, RTDVMVOLTYPE_FREEBSD },
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync { 0xa9, RTDVMVOLTYPE_NETBSD },
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync { 0xa6, RTDVMVOLTYPE_OPENBSD },
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync { 0xaf, RTDVMVOLTYPE_MAC_OSX_HFS },
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync { 0xbf, RTDVMVOLTYPE_SOLARIS },
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync { 0xfd, RTDVMVOLTYPE_LINUX_SOFTRAID }
956a0e3c076406b83d635174a201fd8761ee5133vboxsync};
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrProbe(PCRTDVMDISK pDisk, uint32_t *puScore)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync int rc = VINF_SUCCESS;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync uint8_t abMbr[512];
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync *puScore = RTDVM_MATCH_SCORE_UNSUPPORTED;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync if (pDisk->cbDisk >= 512)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync {
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* Read from the disk and check for the 0x55aa signature at the end. */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rc = rtDvmDiskRead(pDisk, 0, &abMbr[0], sizeof(abMbr));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync if ( RT_SUCCESS(rc)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync && abMbr[510] == 0x55
956a0e3c076406b83d635174a201fd8761ee5133vboxsync && abMbr[511] == 0xaa)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync *puScore = RTDVM_MATCH_SCORE_SUPPORTED; /* Not perfect because GPTs have a protective MBR. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync }
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return rc;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrOpen(PCRTDVMDISK pDisk, PRTDVMFMT phVolMgrFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync int rc = VINF_SUCCESS;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PRTDVMFMTINTERNAL pThis = NULL;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pThis = (PRTDVMFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMFMTINTERNAL));
0cbb2b0b9388d33ac395656e849e4c4c7ddeb347vboxsync if (pThis)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync {
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pThis->pDisk = pDisk;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pThis->cPartitions = 0;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* Read the MBR and count the valid partition entries. */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rc = rtDvmDiskRead(pDisk, 0, &pThis->abMbr[0], sizeof(pThis->abMbr));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync if (RT_SUCCESS(rc))
956a0e3c076406b83d635174a201fd8761ee5133vboxsync {
956a0e3c076406b83d635174a201fd8761ee5133vboxsync uint8_t *pbMbrEntry = &pThis->abMbr[446];
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync Assert(pThis->abMbr[510] == 0x55 && pThis->abMbr[511] == 0xaa);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync for (unsigned i = 0; i < 4; i++)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync {
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* The entry is unused if the type contains 0x00. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync if (pbMbrEntry[4] != 0x00)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pThis->cPartitions++;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pbMbrEntry += 16;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync }
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync *phVolMgrFmt = pThis;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync }
956a0e3c076406b83d635174a201fd8761ee5133vboxsync }
956a0e3c076406b83d635174a201fd8761ee5133vboxsync else
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = VERR_NO_MEMORY;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return rc;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrInitialize(PCRTDVMDISK pDisk, PRTDVMFMT phVolMgrFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync int rc = VINF_SUCCESS;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PRTDVMFMTINTERNAL pThis = NULL;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pThis = (PRTDVMFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMFMTINTERNAL));
0cbb2b0b9388d33ac395656e849e4c4c7ddeb347vboxsync if (pThis)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync {
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* Setup a new MBR and write it to the disk. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync memset(&pThis->abMbr[0], 0, sizeof(pThis->abMbr));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pThis->abMbr[510] = 0x55;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pThis->abMbr[511] = 0xaa;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rc = rtDvmDiskWrite(pDisk, 0, &pThis->abMbr[0], sizeof(pThis->abMbr));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync if (RT_SUCCESS(rc))
956a0e3c076406b83d635174a201fd8761ee5133vboxsync {
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pThis->pDisk = pDisk;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pThis->cPartitions = 0;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync *phVolMgrFmt = pThis;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync }
956a0e3c076406b83d635174a201fd8761ee5133vboxsync else
956a0e3c076406b83d635174a201fd8761ee5133vboxsync RTMemFree(pThis);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync }
956a0e3c076406b83d635174a201fd8761ee5133vboxsync else
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = VERR_NO_MEMORY;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return rc;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(void) rtDvmFmtMbrClose(RTDVMFMT hVolMgrFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pThis->pDisk = NULL;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pThis->cPartitions = 0;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync memset(&pThis->abMbr[0], 0, sizeof(pThis->abMbr));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync RTMemFree(pThis);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrQueryRangeUse(RTDVMFMT hVolMgrFmt,
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync uint64_t off, uint64_t cbRange,
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync bool *pfUsed)
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync{
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync
6b8df80def0e731e860daade9dbd5b9432af4698vboxsync NOREF(cbRange);
6b8df80def0e731e860daade9dbd5b9432af4698vboxsync
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /* MBR uses the first sector only. */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync if (off < 512)
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync *pfUsed = true;
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync else
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync *pfUsed = false;
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync return VINF_SUCCESS;
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync}
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(uint32_t) rtDvmFmtMbrGetValidVolumes(RTDVMFMT hVolMgrFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return pThis->cPartitions;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(uint32_t) rtDvmFmtMbrGetMaxVolumes(RTDVMFMT hVolMgrFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync NOREF(hVolMgrFmt);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return 4; /** @todo: Add support for EBR? */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync/**
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Creates a new volume.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync *
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * @returns IPRT status code.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * @param pThis The MBR volume manager data.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * @param pbMbrEntry The raw MBR entry data.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * @param idx The index in the partition table.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * @param phVolFmt Where to store the volume data on success.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic int rtDvmFmtMbrVolumeCreate(PRTDVMFMTINTERNAL pThis, uint8_t *pbMbrEntry,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync uint32_t idx, PRTDVMVOLUMEFMT phVolFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync int rc = VINF_SUCCESS;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PRTDVMVOLUMEFMTINTERNAL pVol = (PRTDVMVOLUMEFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMVOLUMEFMTINTERNAL));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
0cbb2b0b9388d33ac395656e849e4c4c7ddeb347vboxsync if (pVol)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync {
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVol->pVolMgr = pThis;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVol->idxEntry = idx;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVol->pbMbrEntry = pbMbrEntry;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVol->offStart = *(uint32_t *)&pbMbrEntry[0x08] * pThis->pDisk->cbSector;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVol->cbVolume = *(uint32_t *)&pbMbrEntry[0x0c] * pThis->pDisk->cbSector;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync *phVolFmt = pVol;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync }
956a0e3c076406b83d635174a201fd8761ee5133vboxsync else
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = VERR_NO_MEMORY;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return rc;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrQueryFirstVolume(RTDVMFMT hVolMgrFmt, PRTDVMVOLUMEFMT phVolFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync int rc = VINF_SUCCESS;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync if (pThis->cPartitions != 0)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync {
956a0e3c076406b83d635174a201fd8761ee5133vboxsync uint8_t *pbMbrEntry = &pThis->abMbr[446];
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* Search for the first non empty entry. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync for (unsigned i = 0; i < 4; i++)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync {
956a0e3c076406b83d635174a201fd8761ee5133vboxsync if (pbMbrEntry[0x04] != 0x00)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync {
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rc = rtDvmFmtMbrVolumeCreate(pThis, pbMbrEntry, i, phVolFmt);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync break;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync }
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pbMbrEntry += 16;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync }
956a0e3c076406b83d635174a201fd8761ee5133vboxsync }
956a0e3c076406b83d635174a201fd8761ee5133vboxsync else
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = VERR_DVM_MAP_EMPTY;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return rc;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrQueryNextVolume(RTDVMFMT hVolMgrFmt, RTDVMVOLUMEFMT hVolFmt, PRTDVMVOLUMEFMT phVolFmtNext)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync int rc = VERR_DVM_MAP_NO_VOLUME;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync uint8_t *pbMbrEntry = pVol->pbMbrEntry + 16;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync for (unsigned i = pVol->idxEntry + 1; i < 4; i++)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync {
956a0e3c076406b83d635174a201fd8761ee5133vboxsync if (pbMbrEntry[0x04] != 0x00)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync {
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rc = rtDvmFmtMbrVolumeCreate(pThis, pbMbrEntry, i, phVolFmtNext);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync break;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync }
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pbMbrEntry += 16;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync }
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return rc;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(void) rtDvmFmtMbrVolumeClose(RTDVMVOLUMEFMT hVolFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVol->pVolMgr = NULL;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVol->offStart = 0;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVol->cbVolume = 0;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVol->pbMbrEntry = NULL;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync RTMemFree(pVol);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(uint64_t) rtDvmFmtMbrVolumeGetSize(RTDVMVOLUMEFMT hVolFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return pVol->cbVolume;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrVolumeQueryName(RTDVMVOLUMEFMT hVolFmt, char **ppszVolName)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
4e47bb772df0d04d1ded3e06354de547d52e2d06vboxsync NOREF(hVolFmt); NOREF(ppszVolName);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return VERR_NOT_SUPPORTED;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(RTDVMVOLTYPE) rtDvmFmtMbrVolumeGetType(RTDVMVOLUMEFMT hVolFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync RTDVMVOLTYPE enmVolType = RTDVMVOLTYPE_UNKNOWN;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync for (unsigned i = 0; i < RT_ELEMENTS(g_aFs2DvmVolTypes); i++)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync if (pVol->pbMbrEntry[0x04] == g_aFs2DvmVolTypes[i].bFsId)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync {
956a0e3c076406b83d635174a201fd8761ee5133vboxsync enmVolType = g_aFs2DvmVolTypes[i].enmVolType;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync break;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync }
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return enmVolType;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(uint64_t) rtDvmFmtMbrVolumeGetFlags(RTDVMVOLUMEFMT hVolFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync uint64_t fFlags = 0;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync if (pVol->pbMbrEntry[0x00] & 0x80)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync fFlags |= DVMVOLUME_FLAGS_BOOTABLE | DVMVOLUME_FLAGS_ACTIVE;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return fFlags;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsyncDECLCALLBACK(bool) rtDvmFmtMbrVolumeIsRangeIntersecting(RTDVMVOLUMEFMT hVolFmt,
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync uint64_t offStart, size_t cbRange,
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync uint64_t *poffVol,
8a63f697e3c0c9afe0dc326c3db61fd4217fa895vboxsync uint64_t *pcbIntersect)
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync{
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync bool fIntersect = false;
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync if (RTDVM_RANGE_IS_INTERSECTING(pVol->offStart, pVol->cbVolume, offStart))
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync {
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync fIntersect = true;
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync *poffVol = offStart - pVol->offStart;
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync *pcbIntersect = RT_MIN(cbRange, pVol->offStart + pVol->cbVolume - offStart);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync }
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync return fIntersect;
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync}
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrVolumeRead(RTDVMVOLUMEFMT hVolFmt, uint64_t off, void *pvBuf, size_t cbRead)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync AssertReturn(off + cbRead <= pVol->cbVolume, VERR_INVALID_PARAMETER);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync return rtDvmDiskRead(pVol->pVolMgr->pDisk, pVol->offStart + off, pvBuf, cbRead);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrVolumeWrite(RTDVMVOLUMEFMT hVolFmt, uint64_t off, const void *pvBuf, size_t cbWrite)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync AssertReturn(off + cbWrite <= pVol->cbVolume, VERR_INVALID_PARAMETER);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync return rtDvmDiskWrite(pVol->pVolMgr->pDisk, pVol->offStart + off, pvBuf, cbWrite);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncRTDVMFMTOPS g_rtDvmFmtMbr =
956a0e3c076406b83d635174a201fd8761ee5133vboxsync{
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pcszFmt */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync "MBR",
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnProbe */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rtDvmFmtMbrProbe,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnOpen */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rtDvmFmtMbrOpen,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnInitialize */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rtDvmFmtMbrInitialize,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnClose */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rtDvmFmtMbrClose,
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /* pfnQueryRangeUse */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync rtDvmFmtMbrQueryRangeUse,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnGetValidVolumes */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rtDvmFmtMbrGetValidVolumes,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnGetMaxVolumes */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rtDvmFmtMbrGetMaxVolumes,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnQueryFirstVolume */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rtDvmFmtMbrQueryFirstVolume,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnQueryNextVolume */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rtDvmFmtMbrQueryNextVolume,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeClose */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rtDvmFmtMbrVolumeClose,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeGetSize */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rtDvmFmtMbrVolumeGetSize,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeQueryName */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rtDvmFmtMbrVolumeQueryName,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeGetType */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rtDvmFmtMbrVolumeGetType,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeGetFlags */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rtDvmFmtMbrVolumeGetFlags,
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync /* pfnVOlumeIsRangeIntersecting */
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync rtDvmFmtMbrVolumeIsRangeIntersecting,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeRead */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rtDvmFmtMbrVolumeRead,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeWrite */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rtDvmFmtMbrVolumeWrite
956a0e3c076406b83d635174a201fd8761ee5133vboxsync};
956a0e3c076406b83d635174a201fd8761ee5133vboxsync