Index: trunk/src/VBox/Runtime/common/misc/fdt.cpp
===================================================================
--- trunk/src/VBox/Runtime/common/misc/fdt.cpp	(revision 100032)
+++ trunk/src/VBox/Runtime/common/misc/fdt.cpp	(revision 100036)
@@ -45,4 +45,5 @@
 #include <iprt/assert.h>
 #include <iprt/err.h>
+#include <iprt/file.h>
 #include <iprt/log.h>
 #include <iprt/mem.h>
@@ -93,4 +94,6 @@
     /** Current tree depth in the structure block. */
     uint32_t                cTreeDepth;
+    /** The next free phandle value. */
+    uint32_t                idPHandle;
 } RTFDTINT;
 /** Pointer to the internal Flattened Devicetree instance. */
@@ -266,4 +269,5 @@
     pThis->u32CpuIdPhysBoot = 0;
     pThis->cTreeDepth       = 0;
+    pThis->idPHandle        = UINT32_MAX;
     RTMemFree(pThis);
 }
@@ -415,4 +419,5 @@
                         pThis->cbStruct         = DtbHdr.cbDtStruct;
                         pThis->u32CpuIdPhysBoot = DtbHdr.u32CpuIdPhysBoot;
+                        pThis->idPHandle        = UINT32_MAX; /** @todo Would need to parse the whole tree to find the next free handle. */
 
                         /* Load the memory reservation block. */
@@ -1193,4 +1198,5 @@
 
     uint32_t cbNew = RT_ALIGN_32(pThis->cbStruct + cbSpaceRequired, _1K);
+    Assert(cbNew > pThis->cbStructMax);
     void *pvStructNew = RTMemReallocZ(pThis->pbStruct, pThis->cbStructMax, cbNew);
     if (!pvStructNew)
@@ -1301,4 +1307,6 @@
     if (pThis)
     {
+        pThis->idPHandle = 1; /* 0 is invalid */
+
         /* Add the root node. */
         rc = RTFdtNodeAdd(pThis, "");
@@ -1332,6 +1340,13 @@
 RTDECL(int) RTFdtCreateFromFile(PRTFDT phFdt, const char *pszFilename, RTFDTTYPE enmInType, PRTERRINFO pErrInfo)
 {
-    RT_NOREF(phFdt, pszFilename, enmInType, pErrInfo);
-    return VERR_NOT_IMPLEMENTED;
+    RTVFSIOSTREAM hVfsIos = NIL_RTVFSIOSTREAM;
+    int rc = RTVfsChainOpenIoStream(pszFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
+                                    &hVfsIos, NULL /*poffError*/, pErrInfo);
+    if (RT_FAILURE(rc))
+        return rc;
+
+    rc = RTFdtCreateFromVfsIoStrm(phFdt, hVfsIos, enmInType, pErrInfo);
+    RTVfsIoStrmRelease(hVfsIos);
+    return rc;
 }
 
@@ -1360,11 +1375,23 @@
         return rc;
 
-    uint32_t *pu32 = (uint32_t *)pThis->pbStruct;
-    while (pThis->cTreeDepth--)
+    uint32_t *pu32 = (uint32_t *)(pThis->pbStruct + pThis->cbStruct);
+    while (pThis->cTreeDepth)
+    {
         *pu32++ = DTB_FDT_TOKEN_END_NODE_BE;
+        pThis->cTreeDepth--;
+    }
 
     *pu32 = DTB_FDT_TOKEN_END_BE;
     pThis->cbStruct += cbStructSpace;
     return VINF_SUCCESS;
+}
+
+
+RTDECL(uint32_t) RTFdtPHandleAllocate(RTFDT hFdt)
+{
+    PRTFDTINT pThis = hFdt;
+    AssertPtrReturn(pThis, UINT32_MAX);
+
+    return pThis->idPHandle++;
 }
 
@@ -1388,6 +1415,13 @@
 RTDECL(int) RTFdtDumpToFile(RTFDT hFdt, RTFDTTYPE enmOutType, uint32_t fFlags, const char *pszFilename, PRTERRINFO pErrInfo)
 {
-    RT_NOREF(hFdt, enmOutType, fFlags, pszFilename, pErrInfo);
-    return VERR_NOT_IMPLEMENTED;
+    RTVFSIOSTREAM hVfsIos = NIL_RTVFSIOSTREAM;
+    int rc = RTVfsChainOpenIoStream(pszFilename, RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_DENY_NONE,
+                                    &hVfsIos, NULL /*poffError*/, pErrInfo);
+    if (RT_FAILURE(rc))
+        return rc;
+
+    rc = RTFdtDumpToVfsIoStrm(hFdt, enmOutType, fFlags, hVfsIos, pErrInfo);
+    RTVfsIoStrmRelease(hVfsIos);
+    return rc;
 }
 
@@ -1493,5 +1527,5 @@
     AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
 
-    RT_H2BE_U32(u32);
+    u32 = RT_H2BE_U32(u32);
     return rtFdtStructAppendProperty(pThis, pszProperty, &u32, sizeof(u32));
 }
@@ -1529,7 +1563,7 @@
         return rc;
 
-    uint32_t cbPropAligned = RT_ALIGN_32(cCells * sizeof(uint32_t) + 3 * sizeof(uint32_t), sizeof(uint32_t));
-
-    rc = rtFdtStructEnsureSpace(pThis, cbPropAligned);
+    uint32_t cbProp = cCells * sizeof(uint32_t) + 3 * sizeof(uint32_t);
+
+    rc = rtFdtStructEnsureSpace(pThis, cbProp);
     if (RT_FAILURE(rc))
         return rc;
@@ -1540,7 +1574,10 @@
     *pu32++ = RT_H2BE_U32(offStr);
     for (uint32_t i = 0; i < cCells; i++)
-        *pu32++ = va_arg(va, uint32_t);
-
-    pThis->cbStruct += cbPropAligned;
-    return VINF_SUCCESS;
-}
+    {
+        uint32_t u32 = va_arg(va, uint32_t);
+        *pu32++ = RT_H2BE_U32(u32);
+    }
+
+    pThis->cbStruct += cbProp;
+    return VINF_SUCCESS;
+}
