dvmmbr.cpp revision 956a0e3c076406b83d635174a201fd8761ee5133
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT Disk Volume Management API (DVM) - MBR format backend.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2011 Oracle Corporation
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 * 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 * 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* Structures and Typedefs *
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync*******************************************************************************/
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync * MBR volume manager data.
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** Pointer to the underlying disk. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** Number of initialized partitions. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** The raw MBR data. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync/** Pointer to the MBR volume manager. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync * MBR volume data.
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** Pointer to the volume manager. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** Partition table entry index. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** Start offset of the volume. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** Size of the volume. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** Pointer to the raw partition table entry. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync/** Pointer to an MBR volume. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsynctypedef RTDVMVOLUMEFMTINTERNAL *PRTDVMVOLUMEFMTINTERNAL;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync * MBR FS type to DVM volume type mapping entry.
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** MBR FS Id. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /** DVM volume type. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync/** Pointer to a MBR FS Type to volume type mapping entry. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync * Mapping of FS types to DVM volume types.
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync * From http://www.win.tue.nl/~aeb/partitions/partition_types-1.html
b81824898d989ddac97abd4544d38447a8eea0f7vboxsyncstatic const RTDVMMBRFS2VOLTYPE g_aFs2DvmVolTypes[] =
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync {0x07, RTDVMVOLTYPE_NTFS}, /* Used for exFAT too but NTFS is more common. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsyncDECLCALLBACK(int) dvmFmtMbrProbe(PCRTDVMDISK pDisk, uint32_t *puScore)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /* Read from the disk and check for the 0x55aa signature at the end. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync rc = dvmDiskRead(pDisk, 0, &abMbr[0], sizeof(abMbr));
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync *puScore = RTDVM_MATCH_SCORE_SUPPORTED; /* Not perfect because GPTs have a protective MBR. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsyncDECLCALLBACK(int) dvmFmtMbrOpen(PCRTDVMDISK pDisk, PRTDVMFMT phVolMgrFmt)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pThis = (PRTDVMFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMFMTINTERNAL));
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /* Read the MBR and count the valid partition entries. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync rc = dvmDiskRead(pDisk, 0, &pThis->abMbr[0], sizeof(pThis->abMbr));
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync Assert(pThis->abMbr[510] == 0x55 && pThis->abMbr[511] == 0xaa);
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync for (unsigned i = 0; i < 4; i++)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /* The entry is unused if the type contains 0x00. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsyncDECLCALLBACK(int) dvmFmtMbrInitialize(PCRTDVMDISK pDisk, PRTDVMFMT phVolMgrFmt)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pThis = (PRTDVMFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMFMTINTERNAL));
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync /* Setup a new MBR and write it to the disk. */
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync rc = dvmDiskWrite(pDisk, 0, &pThis->abMbr[0], sizeof(pThis->abMbr));
b81824898d989ddac97abd4544d38447a8eea0f7vboxsyncDECLCALLBACK(void) dvmFmtMbrClose(RTDVMFMT hVolMgrFmt)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsyncDECLCALLBACK(uint32_t) dvmFmtMbrGetValidVolumes(RTDVMFMT hVolMgrFmt)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsyncDECLCALLBACK(uint32_t) dvmFmtMbrGetMaxVolumes(RTDVMFMT hVolMgrFmt)
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync * Creates a new volume.
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.
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsyncstatic int dvmFmtMbrVolumeCreate(PRTDVMFMTINTERNAL pThis, uint8_t *pbMbrEntry,
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync PRTDVMVOLUMEFMTINTERNAL pVol = (PRTDVMVOLUMEFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMVOLUMEFMTINTERNAL));
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pVol->offStart = *(uint32_t *)&pbMbrEntry[0x08] * pThis->pDisk->cbSector;
b81824898d989ddac97abd4544d38447a8eea0f7vboxsync pVol->cbVolume = *(uint32_t *)&pbMbrEntry[0x0c] * pThis->pDisk->cbSector;
return rc;
return rc;
DECLCALLBACK(int) dvmFmtMbrQueryNextVolume(RTDVMFMT hVolMgrFmt, RTDVMVOLUMEFMT hVolFmt, PRTDVMVOLUMEFMT phVolFmtNext)
return rc;
return VERR_NOT_SUPPORTED;
return enmVolType;
return fFlags;
DECLCALLBACK(int) dvmFmtMbrVolumeRead(RTDVMVOLUMEFMT hVolFmt, uint64_t off, void *pvBuf, size_t cbRead)
DECLCALLBACK(int) dvmFmtMbrVolumeWrite(RTDVMVOLUMEFMT hVolFmt, uint64_t off, const void *pvBuf, size_t cbWrite)