dvmmbr.cpp revision a15d881e9ec9bffe9b27ae4174efb8d4dc4a0e17
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * IPRT Disk Volume Management API (DVM) - MBR format backend.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Copyright (C) 2011 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. */
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. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncDECLCALLBACK(int) dvmFmtMbrProbe(PCRTDVMDISK pDisk, uint32_t *puScore)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* Read from the disk and check for the 0x55aa signature at the end. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = dvmDiskRead(pDisk, 0, &abMbr[0], sizeof(abMbr));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync *puScore = RTDVM_MATCH_SCORE_SUPPORTED; /* Not perfect because GPTs have a protective MBR. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncDECLCALLBACK(int) dvmFmtMbrOpen(PCRTDVMDISK pDisk, PRTDVMFMT phVolMgrFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pThis = (PRTDVMFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMFMTINTERNAL));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* Read the MBR and count the valid partition entries. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = dvmDiskRead(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. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncDECLCALLBACK(int) dvmFmtMbrInitialize(PCRTDVMDISK pDisk, PRTDVMFMT phVolMgrFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pThis = (PRTDVMFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMFMTINTERNAL));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* Setup a new MBR and write it to the disk. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = dvmDiskWrite(pDisk, 0, &pThis->abMbr[0], sizeof(pThis->abMbr));
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncDECLCALLBACK(void) dvmFmtMbrClose(RTDVMFMT hVolMgrFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncDECLCALLBACK(uint32_t) dvmFmtMbrGetValidVolumes(RTDVMFMT hVolMgrFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncDECLCALLBACK(uint32_t) dvmFmtMbrGetMaxVolumes(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.
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncstatic int dvmFmtMbrVolumeCreate(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;
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncDECLCALLBACK(int) dvmFmtMbrQueryFirstVolume(RTDVMFMT hVolMgrFmt, PRTDVMVOLUMEFMT phVolFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* Search for the first non empty entry. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync for (unsigned i = 0; i < 4; i++)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = dvmFmtMbrVolumeCreate(pThis, pbMbrEntry, i, phVolFmt);
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncDECLCALLBACK(int) dvmFmtMbrQueryNextVolume(RTDVMFMT hVolMgrFmt, RTDVMVOLUMEFMT hVolFmt, PRTDVMVOLUMEFMT phVolFmtNext)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = dvmFmtMbrVolumeCreate(pThis, pbMbrEntry, i, phVolFmtNext);
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncDECLCALLBACK(void) dvmFmtMbrVolumeClose(RTDVMVOLUMEFMT hVolFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncDECLCALLBACK(uint64_t) dvmFmtMbrVolumeGetSize(RTDVMVOLUMEFMT hVolFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncDECLCALLBACK(int) dvmFmtMbrVolumeQueryName(RTDVMVOLUMEFMT hVolFmt, char **ppszVolName)
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncDECLCALLBACK(RTDVMVOLTYPE) dvmFmtMbrVolumeGetType(RTDVMVOLUMEFMT hVolFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync for (unsigned i = 0; i < RT_ELEMENTS(g_aFs2DvmVolTypes); i++)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync if (pVol->pbMbrEntry[0x04] == g_aFs2DvmVolTypes[i].bFsId)
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncDECLCALLBACK(uint64_t) dvmFmtMbrVolumeGetFlags(RTDVMVOLUMEFMT hVolFmt)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync fFlags |= DVMVOLUME_FLAGS_BOOTABLE | DVMVOLUME_FLAGS_ACTIVE;
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncDECLCALLBACK(int) dvmFmtMbrVolumeRead(RTDVMVOLUMEFMT hVolFmt, uint64_t off, void *pvBuf, size_t cbRead)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync AssertReturn(off + cbRead <= pVol->cbVolume, VERR_INVALID_PARAMETER);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return dvmDiskRead(pVol->pVolMgr->pDisk, pVol->offStart + off, pvBuf, cbRead);
956a0e3c076406b83d635174a201fd8761ee5133vboxsyncDECLCALLBACK(int) dvmFmtMbrVolumeWrite(RTDVMVOLUMEFMT hVolFmt, uint64_t off, const void *pvBuf, size_t cbWrite)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync AssertReturn(off + cbWrite <= pVol->cbVolume, VERR_INVALID_PARAMETER);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return dvmDiskWrite(pVol->pVolMgr->pDisk, pVol->offStart + off, pvBuf, cbWrite);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pcszFmt */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnProbe */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnOpen */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnInitialize */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnClose */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnGetValidVolumes */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnGetMaxVolumes */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnQueryFirstVolume */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnQueryNextVolume */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeClose */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeGetSize */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeQueryName */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeGetType */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeGetFlags */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeRead */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* pfnVolumeWrite */