dvmmbr.cpp revision 956a0e3c076406b83d635174a201fd8761ee5133
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync/* $Id$ */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync/** @file
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT Disk Volume Management API (DVM) - MBR format backend.
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync/*
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2011 Oracle Corporation
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync *
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * available from http://www.virtualbox.org. This file is free software;
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * you can redistribute it and/or modify it under the terms of the GNU
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * General Public License (GPL) as published by the Free Software
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync *
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * The contents of this file may alternatively be used under the terms
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * of the Common Development and Distribution License Version 1.0
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * VirtualBox OSE distribution, in which case the provisions of the
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * CDDL are applicable instead of those of the GPL.
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync *
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * You may elect to license modified versions of this file under the
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * terms and conditions of either the GPL or the CDDL or both.
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync#include <iprt/types.h>
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync#include <iprt/assert.h>
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync#include <iprt/mem.h>
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync#include <iprt/dvm.h>
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync#include <iprt/string.h>
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync#include "internal/dvm.h"
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync/*******************************************************************************
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync* Structures and Typedefs *
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync*******************************************************************************/
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync/**
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync * MBR volume manager data.
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsynctypedef struct RTDVMFMTINTERNAL
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync{
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** Pointer to the underlying disk. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync PCRTDVMDISK pDisk;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** Number of initialized partitions. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync uint32_t cPartitions;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** The raw MBR data. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync uint8_t abMbr[512];
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync} RTDVMFMTINTERNAL;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync/** Pointer to the MBR volume manager. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsynctypedef RTDVMFMTINTERNAL *PRTDVMFMTINTERNAL;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync/**
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync * MBR volume data.
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsynctypedef struct RTDVMVOLUMEFMTINTERNAL
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync{
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** Pointer to the volume manager. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync PRTDVMFMTINTERNAL pVolMgr;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** Partition table entry index. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync uint32_t idxEntry;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** Start offset of the volume. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync uint64_t offStart;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** Size of the volume. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync uint64_t cbVolume;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** Pointer to the raw partition table entry. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync uint8_t *pbMbrEntry;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync} RTDVMVOLUMEFMTINTERNAL;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync/** Pointer to an MBR volume. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsynctypedef RTDVMVOLUMEFMTINTERNAL *PRTDVMVOLUMEFMTINTERNAL;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync/**
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync * MBR FS type to DVM volume type mapping entry.
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsynctypedef struct RTDVMMBRFS2VOLTYPE
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync{
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** MBR FS Id. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync uint8_t bFsId;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** DVM volume type. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync RTDVMVOLTYPE enmVolType;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync} RTDVMMBRFS2VOLTYPE;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync/** Pointer to a MBR FS Type to volume type mapping entry. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsynctypedef RTDVMMBRFS2VOLTYPE *PRTDVMMBRFS2VOLTYPE;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync/**
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync * Mapping of FS types to DVM volume types.
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync *
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync * From http://www.win.tue.nl/~aeb/partitions/partition_types-1.html
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsyncstatic const RTDVMMBRFS2VOLTYPE g_aFs2DvmVolTypes[] =
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync{
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {0x06, RTDVMVOLTYPE_FAT16},
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {0x07, RTDVMVOLTYPE_NTFS}, /* Used for exFAT too but NTFS is more common. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {0x0b, RTDVMVOLTYPE_FAT32},
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {0x0c, RTDVMVOLTYPE_FAT32},
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {0x82, RTDVMVOLTYPE_LINUX_SWAP},
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {0x83, RTDVMVOLTYPE_LINUX_NATIVE},
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {0x8e, RTDVMVOLTYPE_LINUX_LVM},
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {0xa5, RTDVMVOLTYPE_FREEBSD},
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {0xa9, RTDVMVOLTYPE_NETBSD},
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {0xa6, RTDVMVOLTYPE_OPENBSD},
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {0xaf, RTDVMVOLTYPE_MAC_OSX_HFS},
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {0xbf, RTDVMVOLTYPE_SOLARIS},
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {0xfd, RTDVMVOLTYPE_LINUX_SOFTRAID}
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync};
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsyncDECLCALLBACK(int) dvmFmtMbrProbe(PCRTDVMDISK pDisk, uint32_t *puScore)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync{
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync int rc = VINF_SUCCESS;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync uint8_t abMbr[512];
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync *puScore = RTDVM_MATCH_SCORE_UNSUPPORTED;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync if (pDisk->cbDisk >= 512)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /* Read from the disk and check for the 0x55aa signature at the end. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync rc = dvmDiskRead(pDisk, 0, &abMbr[0], sizeof(abMbr));
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync if ( RT_SUCCESS(rc)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync && abMbr[510] == 0x55
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync && abMbr[511] == 0xaa)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync *puScore = RTDVM_MATCH_SCORE_SUPPORTED; /* Not perfect because GPTs have a protective MBR. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync }
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync return rc;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync}
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsyncDECLCALLBACK(int) dvmFmtMbrOpen(PCRTDVMDISK pDisk, PRTDVMFMT phVolMgrFmt)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync{
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync int rc = VINF_SUCCESS;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync PRTDVMFMTINTERNAL pThis = NULL;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pThis = (PRTDVMFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMFMTINTERNAL));
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync if (VALID_PTR(pThis))
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pThis->pDisk = pDisk;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pThis->cPartitions = 0;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /* Read the MBR and count the valid partition entries. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync rc = dvmDiskRead(pDisk, 0, &pThis->abMbr[0], sizeof(pThis->abMbr));
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync if (RT_SUCCESS(rc))
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync uint8_t *pbMbrEntry = &pThis->abMbr[446];
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync Assert(pThis->abMbr[510] == 0x55 && pThis->abMbr[511] == 0xaa);
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync for (unsigned i = 0; i < 4; i++)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /* The entry is unused if the type contains 0x00. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync if (pbMbrEntry[4] != 0x00)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pThis->cPartitions++;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pbMbrEntry += 16;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync }
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync *phVolMgrFmt = pThis;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync }
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync }
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync else
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync rc = VERR_NO_MEMORY;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync return rc;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync}
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsyncDECLCALLBACK(int) dvmFmtMbrInitialize(PCRTDVMDISK pDisk, PRTDVMFMT phVolMgrFmt)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync{
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync int rc = VINF_SUCCESS;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync PRTDVMFMTINTERNAL pThis = NULL;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pThis = (PRTDVMFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMFMTINTERNAL));
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync if (VALID_PTR(pThis))
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /* Setup a new MBR and write it to the disk. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync memset(&pThis->abMbr[0], 0, sizeof(pThis->abMbr));
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pThis->abMbr[510] = 0x55;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pThis->abMbr[511] = 0xaa;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync rc = dvmDiskWrite(pDisk, 0, &pThis->abMbr[0], sizeof(pThis->abMbr));
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync if (RT_SUCCESS(rc))
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pThis->pDisk = pDisk;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pThis->cPartitions = 0;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync *phVolMgrFmt = pThis;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync }
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync else
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync RTMemFree(pThis);
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync }
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync else
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync rc = VERR_NO_MEMORY;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync return rc;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync}
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsyncDECLCALLBACK(void) dvmFmtMbrClose(RTDVMFMT hVolMgrFmt)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync{
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pThis->pDisk = NULL;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pThis->cPartitions = 0;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync memset(&pThis->abMbr[0], 0, sizeof(pThis->abMbr));
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync RTMemFree(pThis);
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync}
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsyncDECLCALLBACK(uint32_t) dvmFmtMbrGetValidVolumes(RTDVMFMT hVolMgrFmt)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync{
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync return pThis->cPartitions;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync}
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsyncDECLCALLBACK(uint32_t) dvmFmtMbrGetMaxVolumes(RTDVMFMT hVolMgrFmt)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync{
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync NOREF(hVolMgrFmt);
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync return 4; /** @todo: Add support for EBR? */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync}
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync/**
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync * Creates a new volume.
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync *
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync * @returns IPRT status code.
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync * @param pThis The MBR volume manager data.
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync * @param pbMbrEntry The raw MBR entry data.
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync * @param idx The index in the partition table.
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync * @param phVolFmt Where to store the volume data on success.
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync */
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsyncstatic int dvmFmtMbrVolumeCreate(PRTDVMFMTINTERNAL pThis, uint8_t *pbMbrEntry,
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync uint32_t idx, PRTDVMVOLUMEFMT phVolFmt)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync{
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync int rc = VINF_SUCCESS;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync PRTDVMVOLUMEFMTINTERNAL pVol = (PRTDVMVOLUMEFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMVOLUMEFMTINTERNAL));
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync if (VALID_PTR(pVol))
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pVol->pVolMgr = pThis;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pVol->idxEntry = idx;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pVol->pbMbrEntry = pbMbrEntry;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pVol->offStart = *(uint32_t *)&pbMbrEntry[0x08] * pThis->pDisk->cbSector;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pVol->cbVolume = *(uint32_t *)&pbMbrEntry[0x0c] * pThis->pDisk->cbSector;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync *phVolFmt = pVol;
}
else
rc = VERR_NO_MEMORY;
return rc;
}
DECLCALLBACK(int) dvmFmtMbrQueryFirstVolume(RTDVMFMT hVolMgrFmt, PRTDVMVOLUMEFMT phVolFmt)
{
int rc = VINF_SUCCESS;
PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
if (pThis->cPartitions != 0)
{
uint8_t *pbMbrEntry = &pThis->abMbr[446];
/* Search for the first non empty entry. */
for (unsigned i = 0; i < 4; i++)
{
if (pbMbrEntry[0x04] != 0x00)
{
rc = dvmFmtMbrVolumeCreate(pThis, pbMbrEntry, i, phVolFmt);
break;
}
pbMbrEntry += 16;
}
}
else
rc = VERR_DVM_MAP_EMPTY;
return rc;
}
DECLCALLBACK(int) dvmFmtMbrQueryNextVolume(RTDVMFMT hVolMgrFmt, RTDVMVOLUMEFMT hVolFmt, PRTDVMVOLUMEFMT phVolFmtNext)
{
int rc = VERR_DVM_MAP_NO_VOLUME;
PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
uint8_t *pbMbrEntry = pVol->pbMbrEntry + 16;
for (unsigned i = pVol->idxEntry + 1; i < 4; i++)
{
if (pbMbrEntry[0x04] != 0x00)
{
rc = dvmFmtMbrVolumeCreate(pThis, pbMbrEntry, i, phVolFmtNext);
break;
}
pbMbrEntry += 16;
}
return rc;
}
DECLCALLBACK(void) dvmFmtMbrVolumeClose(RTDVMVOLUMEFMT hVolFmt)
{
PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
pVol->pVolMgr = NULL;
pVol->offStart = 0;
pVol->cbVolume = 0;
pVol->pbMbrEntry = NULL;
RTMemFree(pVol);
}
DECLCALLBACK(uint64_t) dvmFmtMbrVolumeGetSize(RTDVMVOLUMEFMT hVolFmt)
{
PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
return pVol->cbVolume;
}
DECLCALLBACK(int) dvmFmtMbrVolumeQueryName(RTDVMVOLUMEFMT hVolFmt, char **ppszVolName)
{
NOREF(hVolFmt);
return VERR_NOT_SUPPORTED;
}
DECLCALLBACK(RTDVMVOLTYPE) dvmFmtMbrVolumeGetType(RTDVMVOLUMEFMT hVolFmt)
{
RTDVMVOLTYPE enmVolType = RTDVMVOLTYPE_UNKNOWN;
PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
for (unsigned i = 0; i < RT_ELEMENTS(g_aFs2DvmVolTypes); i++)
if (pVol->pbMbrEntry[0x04] == g_aFs2DvmVolTypes[i].bFsId)
{
enmVolType = g_aFs2DvmVolTypes[i].enmVolType;
break;
}
return enmVolType;
}
DECLCALLBACK(uint64_t) dvmFmtMbrVolumeGetFlags(RTDVMVOLUMEFMT hVolFmt)
{
uint64_t fFlags = 0;
PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
if (pVol->pbMbrEntry[0x00] & 0x80)
fFlags |= DVMVOLUME_FLAGS_BOOTABLE | DVMVOLUME_FLAGS_ACTIVE;
return fFlags;
}
DECLCALLBACK(int) dvmFmtMbrVolumeRead(RTDVMVOLUMEFMT hVolFmt, uint64_t off, void *pvBuf, size_t cbRead)
{
PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
AssertReturn(off + cbRead <= pVol->cbVolume, VERR_INVALID_PARAMETER);
return dvmDiskRead(pVol->pVolMgr->pDisk, pVol->offStart + off, pvBuf, cbRead);
}
DECLCALLBACK(int) dvmFmtMbrVolumeWrite(RTDVMVOLUMEFMT hVolFmt, uint64_t off, const void *pvBuf, size_t cbWrite)
{
PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
AssertReturn(off + cbWrite <= pVol->cbVolume, VERR_INVALID_PARAMETER);
return dvmDiskWrite(pVol->pVolMgr->pDisk, pVol->offStart + off, pvBuf, cbWrite);
}
RTDVMFMTOPS g_DvmFmtMbr =
{
/* pcszFmt */
"MBR",
/* pfnProbe */
dvmFmtMbrProbe,
/* pfnOpen */
dvmFmtMbrOpen,
/* pfnInitialize */
dvmFmtMbrInitialize,
/* pfnClose */
dvmFmtMbrClose,
/* pfnGetValidVolumes */
dvmFmtMbrGetValidVolumes,
/* pfnGetMaxVolumes */
dvmFmtMbrGetMaxVolumes,
/* pfnQueryFirstVolume */
dvmFmtMbrQueryFirstVolume,
/* pfnQueryNextVolume */
dvmFmtMbrQueryNextVolume,
/* pfnVolumeClose */
dvmFmtMbrVolumeClose,
/* pfnVolumeGetSize */
dvmFmtMbrVolumeGetSize,
/* pfnVolumeQueryName */
dvmFmtMbrVolumeQueryName,
/* pfnVolumeGetType */
dvmFmtMbrVolumeGetType,
/* pfnVolumeGetFlags */
dvmFmtMbrVolumeGetFlags,
/* pfnVolumeRead */
dvmFmtMbrVolumeRead,
/* pfnVolumeWrite */
dvmFmtMbrVolumeWrite
};