956a0e3c076406b83d635174a201fd8761ee5133vboxsync * IPRT Disk Volume Management API (DVM) - MBR format backend.
c7814cf6e1240a519cbec0441e033d0e2470ed00vboxsync * Copyright (C) 2011-2012 Oracle Corporation
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 * 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 * 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.
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync/*******************************************************************************
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync* Header Files *
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync*******************************************************************************/
956a0e3c076406b83d635174a201fd8761ee5133vboxsync/*******************************************************************************
956a0e3c076406b83d635174a201fd8761ee5133vboxsync* Structures and Typedefs *
956a0e3c076406b83d635174a201fd8761ee5133vboxsync*******************************************************************************/
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * MBR volume manager data.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** Pointer to the underlying disk. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** Number of initialized partitions. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** The raw MBR data. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync/** Pointer to the MBR volume manager. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * MBR volume data.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** Pointer to the volume manager. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** Partition table entry index. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** Start offset of the volume. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** Size of the volume. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** Pointer to the raw partition table entry. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync/** Pointer to an MBR volume. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsynctypedef RTDVMVOLUMEFMTINTERNAL *PRTDVMVOLUMEFMTINTERNAL;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * MBR FS type to DVM volume type mapping entry.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** MBR FS Id. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** DVM volume type. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync/** Pointer to a MBR FS Type to volume type mapping entry. */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync/*******************************************************************************
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync* Global Variables *
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync*******************************************************************************/
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Mapping of FS types to DVM volume types.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * From http://www.win.tue.nl/~aeb/partitions/partition_types-1.html
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncstatic const RTDVMMBRFS2VOLTYPE g_aFs2DvmVolTypes[] =
a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17vboxsync { 0x07, RTDVMVOLTYPE_NTFS }, /* Simplification: Used for HPFS, exFAT, ++, too but NTFS is the more common one. */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrProbe(PCRTDVMDISK pDisk, uint32_t *puScore)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* Read from the disk and check for the 0x55aa signature at the end. */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rc = rtDvmDiskRead(pDisk, 0, &abMbr[0], sizeof(abMbr));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync *puScore = RTDVM_MATCH_SCORE_SUPPORTED; /* Not perfect because GPTs have a protective MBR. */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrOpen(PCRTDVMDISK pDisk, PRTDVMFMT phVolMgrFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pThis = (PRTDVMFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMFMTINTERNAL));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* Read the MBR and count the valid partition entries. */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rc = rtDvmDiskRead(pDisk, 0, &pThis->abMbr[0], sizeof(pThis->abMbr));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync Assert(pThis->abMbr[510] == 0x55 && pThis->abMbr[511] == 0xaa);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync for (unsigned i = 0; i < 4; i++)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* The entry is unused if the type contains 0x00. */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrInitialize(PCRTDVMDISK pDisk, PRTDVMFMT phVolMgrFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pThis = (PRTDVMFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMFMTINTERNAL));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* Setup a new MBR and write it to the disk. */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rc = rtDvmDiskWrite(pDisk, 0, &pThis->abMbr[0], sizeof(pThis->abMbr));
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(void) rtDvmFmtMbrClose(RTDVMFMT hVolMgrFmt)
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrQueryRangeUse(RTDVMFMT hVolMgrFmt,
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /* MBR uses the first sector only. */
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(uint32_t) rtDvmFmtMbrGetValidVolumes(RTDVMFMT hVolMgrFmt)
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(uint32_t) rtDvmFmtMbrGetMaxVolumes(RTDVMFMT hVolMgrFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Creates a new volume.
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.
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic int rtDvmFmtMbrVolumeCreate(PRTDVMFMTINTERNAL pThis, uint8_t *pbMbrEntry,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PRTDVMVOLUMEFMTINTERNAL pVol = (PRTDVMVOLUMEFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMVOLUMEFMTINTERNAL));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVol->offStart = *(uint32_t *)&pbMbrEntry[0x08] * pThis->pDisk->cbSector;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVol->cbVolume = *(uint32_t *)&pbMbrEntry[0x0c] * pThis->pDisk->cbSector;
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrQueryFirstVolume(RTDVMFMT hVolMgrFmt, PRTDVMVOLUMEFMT phVolFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* Search for the first non empty entry. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync for (unsigned i = 0; i < 4; i++)
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rc = rtDvmFmtMbrVolumeCreate(pThis, pbMbrEntry, i, phVolFmt);
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrQueryNextVolume(RTDVMFMT hVolMgrFmt, RTDVMVOLUMEFMT hVolFmt, PRTDVMVOLUMEFMT phVolFmtNext)
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync rc = rtDvmFmtMbrVolumeCreate(pThis, pbMbrEntry, i, phVolFmtNext);
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(void) rtDvmFmtMbrVolumeClose(RTDVMVOLUMEFMT hVolFmt)
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(uint64_t) rtDvmFmtMbrVolumeGetSize(RTDVMVOLUMEFMT hVolFmt)
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrVolumeQueryName(RTDVMVOLUMEFMT hVolFmt, char **ppszVolName)
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(RTDVMVOLTYPE) rtDvmFmtMbrVolumeGetType(RTDVMVOLUMEFMT hVolFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync for (unsigned i = 0; i < RT_ELEMENTS(g_aFs2DvmVolTypes); i++)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync if (pVol->pbMbrEntry[0x04] == g_aFs2DvmVolTypes[i].bFsId)
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(uint64_t) rtDvmFmtMbrVolumeGetFlags(RTDVMVOLUMEFMT hVolFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync fFlags |= DVMVOLUME_FLAGS_BOOTABLE | DVMVOLUME_FLAGS_ACTIVE;
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsyncDECLCALLBACK(bool) rtDvmFmtMbrVolumeIsRangeIntersecting(RTDVMVOLUMEFMT hVolFmt,
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync if (RTDVM_RANGE_IS_INTERSECTING(pVol->offStart, pVol->cbVolume, offStart))
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync *pcbIntersect = RT_MIN(cbRange, pVol->offStart + pVol->cbVolume - offStart);
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrVolumeRead(RTDVMVOLUMEFMT hVolFmt, uint64_t off, void *pvBuf, size_t cbRead)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync AssertReturn(off + cbRead <= pVol->cbVolume, VERR_INVALID_PARAMETER);
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync return rtDvmDiskRead(pVol->pVolMgr->pDisk, pVol->offStart + off, pvBuf, cbRead);
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsyncstatic DECLCALLBACK(int) rtDvmFmtMbrVolumeWrite(RTDVMVOLUMEFMT hVolFmt, uint64_t off, const void *pvBuf, size_t cbWrite)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync AssertReturn(off + cbWrite <= pVol->cbVolume, VERR_INVALID_PARAMETER);
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync return rtDvmDiskWrite(pVol->pVolMgr->pDisk, pVol->offStart + off, pvBuf, cbWrite);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pcszFmt */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnProbe */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnOpen */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnInitialize */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnClose */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /* pfnQueryRangeUse */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnGetValidVolumes */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnGetMaxVolumes */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnQueryFirstVolume */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnQueryNextVolume */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeClose */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeGetSize */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeQueryName */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeGetType */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeGetFlags */
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync /* pfnVOlumeIsRangeIntersecting */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeRead */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeWrite */