Chips/Hisilicon: Add FdtUpdateLib

The library will be used to update FDT per platform information gathered
by UEFI, like ethernet MAC address.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Heyi Guo <heyi.guo@linaro.org>
Reviewed-by: Leif Lindholm <leif.lindholm@linaro.org>
diff --git a/Chips/Hisilicon/Drivers/UpdateFdtDxe/UpdateFdtDxe.c b/Chips/Hisilicon/Drivers/UpdateFdtDxe/UpdateFdtDxe.c
new file mode 100644
index 0000000..8586e33
--- /dev/null
+++ b/Chips/Hisilicon/Drivers/UpdateFdtDxe/UpdateFdtDxe.c
@@ -0,0 +1,158 @@
+/** @file

+*

+*  Copyright (c) 2015, Hisilicon Limited. All rights reserved.

+*  Copyright (c) 2015, Linaro Limited. All rights reserved.

+*

+*  This program and the accompanying materials

+*  are licensed and made available under the terms and conditions of the BSD License

+*  which accompanies this distribution.  The full text of the license may be found at

+*  http://opensource.org/licenses/bsd-license.php

+*

+*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

+*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

+*

+**/

+

+

+#include <Uefi.h>

+#include <Pi/PiDxeCis.h>

+#include <Library/DebugLib.h>

+#include <libfdt.h>

+#include <Library/UefiBootServicesTableLib.h>

+#include <Library/MemoryAllocationLib.h>

+#include <Library/PrintLib.h>

+#include <Guid/Fdt.h>

+#include <Protocol/HisiBoardNicProtocol.h>

+#include <Library/DxeServicesTableLib.h>

+#include <Library/FdtUpdateLib.h>

+

+STATIC

+EFI_STATUS

+InstallFdtIntoConfigurationTable (

+  IN VOID* FdtBlob,

+  IN UINTN FdtSize

+  )

+{

+  EFI_STATUS Status;

+

+  // Check the FDT header is valid. We only make this check in DEBUG mode in case the FDT header change on

+  // production device and this ASSERT() becomes not valid.

+  if(!(fdt_check_header (FdtBlob) == 0))

+  {

+      DEBUG ((EFI_D_ERROR,"can not find FdtBlob \n"));

+      return EFI_INVALID_PARAMETER;

+  }

+

+  // Ensure the Size of the Device Tree is smaller than the size of the read file

+  if(!((UINTN)fdt_totalsize (FdtBlob) <= FdtSize))

+  {

+      DEBUG ((EFI_D_ERROR,"FdtBlob <= FdtSize \n"));

+      return EFI_INVALID_PARAMETER;

+  }

+

+  // Install the FDT into the Configuration Table

+  Status = gBS->InstallConfigurationTable (&gFdtTableGuid, FdtBlob);

+

+  return Status;

+}

+

+EFI_STATUS

+SetNvramSpace (VOID)

+{

+    EFI_STATUS          Status;

+    EFI_GCD_MEMORY_SPACE_DESCRIPTOR desp = {0};

+

+    if (PcdGet64(PcdReservedNvramSize) == 0) {

+      return EFI_SUCCESS;

+    }

+

+    Status = gDS->GetMemorySpaceDescriptor(PcdGet64(PcdReservedNvramBase),&desp);

+    if(EFI_ERROR(Status)){

+         DEBUG ((EFI_D_ERROR,"get memory space error:--------- \n"));

+        return Status;

+    }

+    desp.Attributes |= EFI_MEMORY_RUNTIME | EFI_MEMORY_WB;

+    Status = gDS->SetMemorySpaceAttributes(PcdGet64(PcdReservedNvramBase),PcdGet64(PcdReservedNvramSize), desp.Attributes);

+    if(EFI_ERROR(Status)){

+        DEBUG ((EFI_D_ERROR,"set memory space error:--------- \n"));

+        return Status;

+    }

+

+    return EFI_SUCCESS;

+}

+

+

+EFI_STATUS

+EFIAPI UpdateFdt (

+  IN EFI_HANDLE         ImageHandle,

+  IN EFI_SYSTEM_TABLE  *SystemTable)

+{

+    INTN                Error;

+    VOID*               Fdt;

+    UINT32              Size;

+    UINTN               NewFdtBlobSize;

+    UINTN               NewFdtBlobBase;

+    EFI_STATUS          Status = EFI_SUCCESS;

+    UINT32              Index = 0;

+    UINTN               FDTConfigTable;

+

+    (VOID) SetNvramSpace ();

+

+    Fdt = (VOID*)(PcdGet64(FdtFileAddress));

+

+

+    Error = fdt_check_header ((VOID*)(PcdGet64(FdtFileAddress)));

+    DEBUG ((EFI_D_ERROR,"fdtfileaddress:--------- 0x%lx\n",PcdGet64(FdtFileAddress)));

+    if (Error != 0)

+    {

+        DEBUG ((EFI_D_ERROR,"ERROR: Device Tree header not valid (%a)\n", fdt_strerror(Error)));

+        return EFI_INVALID_PARAMETER;

+    }

+

+    Size = (UINTN)fdt_totalsize ((VOID*)(PcdGet64(FdtFileAddress)));

+    NewFdtBlobSize = Size + ADD_FILE_LENGTH;

+

+    Status = gBS->AllocatePages (AllocateAnyPages, EfiRuntimeServicesData, EFI_SIZE_TO_PAGES(Size), &NewFdtBlobBase);

+    if (EFI_ERROR (Status))

+    {

+        return EFI_OUT_OF_RESOURCES;

+    }

+

+    (VOID) CopyMem((VOID*)NewFdtBlobBase, Fdt, Size);

+

+    Status = EFIFdtUpdate(NewFdtBlobBase);

+    if (EFI_ERROR (Status))

+    {

+        DEBUG((EFI_D_ERROR, "%a(%d):EFIFdtUpdate Fail!\n", __FUNCTION__,__LINE__));

+        goto EXIT;

+    }

+

+

+    Status = InstallFdtIntoConfigurationTable ((VOID*)(UINTN)NewFdtBlobBase, NewFdtBlobSize);

+    DEBUG ((EFI_D_ERROR, "NewFdtBlobBase: 0x%lx  NewFdtBlobSize:0x%lx\n",NewFdtBlobBase,NewFdtBlobSize));

+    if (EFI_ERROR (Status))

+    {

+        DEBUG ((EFI_D_ERROR, "installfdtconfiguration table fail():\n"));

+        goto EXIT;

+    }

+

+

+    for (Index = 0; Index < gST->NumberOfTableEntries; Index ++)

+    {

+        if (CompareGuid (&gFdtTableGuid, &(gST->ConfigurationTable[Index].VendorGuid)))

+        {

+            FDTConfigTable = (UINTN)gST->ConfigurationTable[Index].VendorTable;

+            DEBUG ((EFI_D_ERROR, "FDTConfigTable Address: 0x%lx\n",FDTConfigTable));

+            break;

+        }

+    }

+

+    return Status;

+

+    EXIT:

+

+         gBS->FreePages(NewFdtBlobBase,EFI_SIZE_TO_PAGES(NewFdtBlobSize));

+

+    return Status;

+

+}

diff --git a/Chips/Hisilicon/Drivers/UpdateFdtDxe/UpdateFdtDxe.inf b/Chips/Hisilicon/Drivers/UpdateFdtDxe/UpdateFdtDxe.inf
new file mode 100644
index 0000000..a0f0a9d
--- /dev/null
+++ b/Chips/Hisilicon/Drivers/UpdateFdtDxe/UpdateFdtDxe.inf
@@ -0,0 +1,62 @@
+#/** @file

+#

+#    Copyright (c) 2015, Hisilicon Limited. All rights reserved.

+#    Copyright (c) 2015, Linaro Limited. All rights reserved.

+#

+#    This program and the accompanying materials

+#    are licensed and made available under the terms and conditions of the BSD License

+#    which accompanies this distribution. The full text of the license may be found at

+#    http://opensource.org/licenses/bsd-license.php

+#

+#    THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

+#    WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

+#

+#**/

+

+[Defines]

+  INF_VERSION                    = 0x00010005

+  BASE_NAME                      = UpdateFdtDxe

+  FILE_GUID                      = E29977F9-20A4-4551-B0EC-BCE246592E76

+  MODULE_TYPE                    = DXE_DRIVER

+  VERSION_STRING                 = 1.0

+

+  ENTRY_POINT                    = UpdateFdt

+

+[Sources.common]

+  UpdateFdtDxe.c

+

+

+[Packages]

+  ArmPlatformPkg/ArmPlatformPkg.dec

+  MdePkg/MdePkg.dec

+  ArmPkg/ArmPkg.dec

+  OpenPlatformPkg/Chips/Hisilicon/HisiPkg.dec

+  EmbeddedPkg/EmbeddedPkg.dec

+

+[LibraryClasses]

+  UefiBootServicesTableLib

+  MemoryAllocationLib

+  UefiDriverEntryPoint

+  DebugLib

+  BaseLib

+  FdtLib

+  PcdLib

+  FdtUpdateLib

+  DxeServicesTableLib

+

+[Guids]

+ gFdtTableGuid

+[Protocols]

+

+ gHisiBoardNicProtocolGuid

+

+[Pcd]

+

+ gHisiTokenSpaceGuid.FdtFileAddress

+ gHisiTokenSpaceGuid.PcdReservedNvramSize

+ gHisiTokenSpaceGuid.PcdReservedNvramBase

+

+

+[Depex]

+TRUE

+

diff --git a/Chips/Hisilicon/HisiPkg.dec b/Chips/Hisilicon/HisiPkg.dec
index 5a93597..492f195 100644
--- a/Chips/Hisilicon/HisiPkg.dec
+++ b/Chips/Hisilicon/HisiPkg.dec
@@ -50,6 +50,7 @@
   OemMiscLib|Include/Library/OemMiscLib.h

   I2CLib|Include/Library/I2CLib.h

   PlatformPciLib|Include/Library/PlatformPciLib.h

+  FdtUpdateLib|Include/Library/FdtUpdateLib.h

 

 [PcdsFixedAtBuild]

   gHisiTokenSpaceGuid.PcdNORFlashBase|0x00000000|UINT64|0x01000008

@@ -90,6 +91,14 @@
   gHisiTokenSpaceGuid.PcdBottomOfHighMemory|0x0|UINT64|0x40000003

 

   gHisiTokenSpaceGuid.PcdSlotPerChannelNum|0x0|UINT32|0x40000004

+

+  #FDT File Address

+  gHisiTokenSpaceGuid.FdtFileAddress|0x0|UINT64|0x40000005

+

+  #Reserved for NVRAM

+  gHisiTokenSpaceGuid.PcdReservedNvramBase|0x0|UINT64|0x40000006

+  gHisiTokenSpaceGuid.PcdReservedNvramSize|0x0|UINT64|0x40000007

+

   gHisiTokenSpaceGuid.PcdTrustedFirmwareEnable|0x0|UINT64|0x40000008

   gHisiTokenSpaceGuid.PcdTrustedFirmwareBL1Base|0x0|UINT64|0x40000009

   gHisiTokenSpaceGuid.PcdTrustedFirmwareMagicNum|0x5A5A5A5A|UINT32|0x4000000a

diff --git a/Chips/Hisilicon/Include/Library/FdtUpdateLib.h b/Chips/Hisilicon/Include/Library/FdtUpdateLib.h
new file mode 100644
index 0000000..94fc3d3
--- /dev/null
+++ b/Chips/Hisilicon/Include/Library/FdtUpdateLib.h
@@ -0,0 +1,45 @@
+/** @file

+*

+*  Copyright (c) 2015, Hisilicon Limited. All rights reserved.

+*  Copyright (c) 2015, Linaro Limited. All rights reserved.

+*

+*  This program and the accompanying materials

+*  are licensed and made available under the terms and conditions of the BSD License

+*  which accompanies this distribution.  The full text of the license may be found at

+*  http://opensource.org/licenses/bsd-license.php

+*

+*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

+*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

+*

+**/

+

+

+

+#ifndef _FDTUPDATELIB_H_

+#define _FDTUPDATELIB_H_

+

+#define ADD_FILE_LENGTH  0x400

+

+typedef struct

+{

+  UINT32 BaseHigh;

+  UINT32 BaseLow;

+  UINT32 LengthHigh;

+  UINT32 LengthLow;

+}PHY_MEM_REGION;

+

+typedef struct

+{

+  UINT8 data0;

+  UINT8 data1;

+  UINT8 data2;

+  UINT8 data3;

+  UINT8 data4;

+  UINT8 data5;

+}MAC_ADDRESS;

+

+extern  EFI_STATUS EFIFdtUpdate(UINTN FdtFileAddr);

+

+#endif

+

+

diff --git a/Platforms/Hisilicon/D02/FdtUpdateLibD02/FdtUpdateLib.c b/Platforms/Hisilicon/D02/FdtUpdateLibD02/FdtUpdateLib.c
new file mode 100644
index 0000000..429306b
--- /dev/null
+++ b/Platforms/Hisilicon/D02/FdtUpdateLibD02/FdtUpdateLib.c
@@ -0,0 +1,341 @@
+/** @file

+*

+*  Copyright (c) 2015, Hisilicon Limited. All rights reserved.

+*  Copyright (c) 2015, Linaro Limited. All rights reserved.

+*

+*  This program and the accompanying materials

+*  are licensed and made available under the terms and conditions of the BSD License

+*  which accompanies this distribution.  The full text of the license may be found at

+*  http://opensource.org/licenses/bsd-license.php

+*

+*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

+*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

+*

+**/

+

+#include <Uefi.h>

+#include <Library/BaseLib.h>

+#include <libfdt.h>

+#include <Library/IoLib.h>

+#include <Library/DebugLib.h>

+#include <Library/UefiBootServicesTableLib.h>

+#include <Library/FdtUpdateLib.h>

+#include <Protocol/HisiBoardNicProtocol.h>

+#include <Library/MemoryAllocationLib.h>

+

+MAC_ADDRESS gMacAddress[1];

+

+CHAR8  *EthName[8]=

+{

+ "ethernet@0","ethernet@1",

+ "ethernet@2","ethernet@3",

+ "ethernet@4","ethernet@5",

+ "ethernet@6","ethernet@7"

+};

+

+CHAR8  *MacName[4]=

+{

+ "ethernet-mac@c7040000",

+ "ethernet-mac@c7044000",

+ "ethernet-mac@c7048000",

+ "ethernet-mac@c704c000"

+};

+

+STATIC

+BOOLEAN

+IsMemMapRegion (

+  IN EFI_MEMORY_TYPE MemoryType

+  )

+{

+  switch(MemoryType)

+    {

+        case EfiRuntimeServicesCode:

+        case EfiRuntimeServicesData:

+        case EfiConventionalMemory:

+        case EfiACPIReclaimMemory:

+        case EfiACPIMemoryNVS:

+        case EfiLoaderCode:

+        case EfiLoaderData:

+        case EfiBootServicesCode:

+        case EfiBootServicesData:

+        case EfiPalCode:

+                return TRUE;

+        default:

+                return FALSE;

+  }

+}

+

+EFI_STATUS

+GetMacAddress (UINT32 Port)

+{

+    EFI_MAC_ADDRESS Mac;

+    EFI_STATUS Status;

+    HISI_BOARD_NIC_PROTOCOL *OemNic = NULL;

+

+    Status = gBS->LocateProtocol(&gHisiBoardNicProtocolGuid, NULL, (VOID **)&OemNic);

+    if(EFI_ERROR(Status))

+    {

+        DEBUG((EFI_D_ERROR, "[%a]:[%dL] LocateProtocol failed %r\n", __FUNCTION__, __LINE__, Status));

+        return Status;

+    }

+

+    Status = OemNic->GetMac(&Mac, Port);

+    if(EFI_ERROR(Status))

+    {

+        DEBUG((EFI_D_ERROR, "[%a]:[%dL] GetMac failed %r\n", __FUNCTION__, __LINE__, Status));

+        return Status;

+    }

+

+    gMacAddress[0].data0=Mac.Addr[0];

+    gMacAddress[0].data1=Mac.Addr[1];

+    gMacAddress[0].data2=Mac.Addr[2];

+    gMacAddress[0].data3=Mac.Addr[3];

+    gMacAddress[0].data4=Mac.Addr[4];

+    gMacAddress[0].data5=Mac.Addr[5];

+    DEBUG((EFI_D_ERROR, "Port%d:0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",

+        Port,gMacAddress[0].data0,gMacAddress[0].data1,gMacAddress[0].data2,

+        gMacAddress[0].data3,gMacAddress[0].data4,gMacAddress[0].data5));

+

+    return EFI_SUCCESS;

+}

+

+STATIC

+EFI_STATUS

+DelPhyhandleUpdateMacAddress(IN VOID* Fdt)

+{

+    UINT8               port;

+    INTN                ethernetnode;

+    INTN                node;

+    INTN                Error;

+    struct              fdt_property *m_prop;

+    int                 m_oldlen;

+    EFI_STATUS          Status = EFI_SUCCESS;

+

+    node = fdt_subnode_offset(Fdt, 0, "soc");

+    if (node < 0)

+    {

+        DEBUG ((EFI_D_ERROR, "can not find soc root node\n"));

+        return EFI_INVALID_PARAMETER;

+    }

+    else

+    {

+        for( port=0; port<8; port++ )

+        {

+            (VOID) GetMacAddress(port);

+            ethernetnode=fdt_subnode_offset(Fdt, node,EthName[port]);

+            if (ethernetnode < 0)

+            {

+                DEBUG ((EFI_D_ERROR, "can not find ethernet@ %d node\n",port));

+            }

+            m_prop = fdt_get_property_w(Fdt, ethernetnode, "local-mac-address", &m_oldlen);

+            if(m_prop)

+            {

+                Error = fdt_delprop(Fdt, ethernetnode, "local-mac-address");

+                if (Error)

+                {

+                    DEBUG ((EFI_D_ERROR, "ERROR:fdt_delprop() Local-mac-address: %a\n", fdt_strerror (Error)));

+                    Status = EFI_INVALID_PARAMETER;

+                }

+                Error = fdt_setprop(Fdt, ethernetnode, "local-mac-address",gMacAddress,sizeof(MAC_ADDRESS));

+                if (Error)

+                {

+                    DEBUG ((EFI_D_ERROR, "ERROR:fdt_setprop():local-mac-address %a\n", fdt_strerror (Error)));

+                    Status = EFI_INVALID_PARAMETER;

+                }

+            }

+        }

+    }

+    return Status;

+}

+

+EFI_STATUS UpdateMemoryNode(VOID* Fdt)

+{

+    INTN                Error = 0;

+    EFI_STATUS          Status = EFI_SUCCESS;

+    UINT32              Index = 0;

+    UINT32              MemIndex;

+    INTN                node;

+    struct              fdt_property *m_prop;

+    int                 m_oldlen;

+    EFI_MEMORY_DESCRIPTOR *MemoryMap;

+    EFI_MEMORY_DESCRIPTOR *MemoryMapPtr;

+    EFI_MEMORY_DESCRIPTOR *MemoryMapPtrCurrent;

+    UINTN                 MemoryMapSize;

+    UINTN                 Pages0 = 0;

+    UINTN                 Pages1 = 0;

+    UINTN                 MapKey;

+    UINTN                 DescriptorSize;

+    UINT32                DescriptorVersion;

+    PHY_MEM_REGION        *mRegion;

+    UINTN                 MemoryMapLastEndAddress ;

+    UINTN                 MemoryMapcontinuousStartAddress ;

+    UINTN                 MemoryMapCurrentStartAddress;

+    BOOLEAN               FindMemoryRegionFlag = FALSE;

+    node = fdt_subnode_offset(Fdt, 0, "memory");

+    if (node < 0)

+    {

+        // Create the memory node

+        node = fdt_add_subnode(Fdt, 0, "memory");

+        if(node < 0)

+        {

+          DEBUG((EFI_D_INFO, "[%a]:[%dL] fdt add subnode error\n", __FUNCTION__, __LINE__));

+        }

+    }

+    //find the memory node property

+    m_prop = fdt_get_property_w(Fdt, node, "memory", &m_oldlen);

+    if(m_prop)

+        Error=fdt_delprop(Fdt, node, "reg");

+    if (Error)

+    {

+        DEBUG ((EFI_D_ERROR, "ERROR:fdt_delprop(): %a\n", fdt_strerror (Error)));

+        Status = EFI_INVALID_PARAMETER;

+        return Status;

+    }

+

+    MemoryMap = NULL;

+    MemoryMapSize = 0;

+    MemIndex = 0;

+

+    Status = gBS->GetMemoryMap (&MemoryMapSize, MemoryMap, &MapKey, &DescriptorSize, &DescriptorVersion);

+    if (Status == EFI_BUFFER_TOO_SMALL)

+    {

+        // The UEFI specification advises to allocate more memory for the MemoryMap buffer between successive

+        // calls to GetMemoryMap(), since allocation of the new buffer may potentially increase memory map size.

+        //DEBUG ((EFI_D_ERROR, "MemoryMapsize: 0x%lx\n",MemoryMapSize));

+        Pages0 = EFI_SIZE_TO_PAGES (MemoryMapSize) + 1;

+        MemoryMap = AllocatePages (Pages0);

+        if (MemoryMap == NULL)

+        {

+            Status = EFI_OUT_OF_RESOURCES;

+            return Status;

+        }

+        Status = gBS->GetMemoryMap (&MemoryMapSize, MemoryMap, &MapKey, &DescriptorSize, &DescriptorVersion);

+    }

+

+    if(MemoryMap == NULL)

+    {

+        Status = EFI_OUT_OF_RESOURCES;

+        //goto EXIT;

+        return Status;

+    }

+

+    mRegion = NULL;

+    Pages1 = EFI_SIZE_TO_PAGES (sizeof(PHY_MEM_REGION) *( MemoryMapSize / DescriptorSize));

+    mRegion = (PHY_MEM_REGION*)AllocatePages(Pages1);

+    if (mRegion == NULL)

+    {

+      Status = EFI_OUT_OF_RESOURCES;

+      return Status;

+    }

+

+    if (!EFI_ERROR(Status))

+    {

+        MemoryMapPtr = MemoryMap;

+        MemoryMapPtrCurrent = MemoryMapPtr;

+        MemoryMapLastEndAddress = 0;

+        MemoryMapcontinuousStartAddress = 0;

+        MemoryMapCurrentStartAddress = 0;

+        for ( Index = 0; Index < (MemoryMapSize / DescriptorSize); Index++)

+        {

+            MemoryMapPtrCurrent = (EFI_MEMORY_DESCRIPTOR*)((UINTN)MemoryMapPtr + Index*DescriptorSize);

+            MemoryMapCurrentStartAddress = (UINTN)MemoryMapPtrCurrent->PhysicalStart;

+

+            if (!IsMemMapRegion ((EFI_MEMORY_TYPE)MemoryMapPtrCurrent->Type))

+            {

+                continue;

+            }

+            else

+            {

+                FindMemoryRegionFlag = TRUE;

+                if(MemoryMapCurrentStartAddress != MemoryMapLastEndAddress)

+                {

+                    mRegion[MemIndex].BaseHigh= cpu_to_fdt32(MemoryMapcontinuousStartAddress>>32);

+                    mRegion[MemIndex].BaseLow=cpu_to_fdt32(MemoryMapcontinuousStartAddress);

+                    mRegion[MemIndex].LengthHigh= cpu_to_fdt32((MemoryMapLastEndAddress-MemoryMapcontinuousStartAddress)>>32);

+                    mRegion[MemIndex].LengthLow=cpu_to_fdt32(MemoryMapLastEndAddress-MemoryMapcontinuousStartAddress);

+                    MemIndex+=1;

+                    MemoryMapcontinuousStartAddress=MemoryMapCurrentStartAddress;

+                }

+            }

+            MemoryMapLastEndAddress = (UINTN)(MemoryMapPtrCurrent->PhysicalStart + MemoryMapPtrCurrent->NumberOfPages * EFI_PAGE_SIZE);

+        }

+        if (FindMemoryRegionFlag)

+        {

+            mRegion[MemIndex].BaseHigh = cpu_to_fdt32(MemoryMapcontinuousStartAddress>>32);

+            mRegion[MemIndex].BaseLow = cpu_to_fdt32(MemoryMapcontinuousStartAddress);

+            mRegion[MemIndex].LengthHigh = cpu_to_fdt32((MemoryMapLastEndAddress-MemoryMapcontinuousStartAddress)>>32);

+            mRegion[MemIndex].LengthLow = cpu_to_fdt32(MemoryMapLastEndAddress-MemoryMapcontinuousStartAddress);

+        }

+    }

+    Error = fdt_setprop(Fdt, node, "reg",mRegion,sizeof(PHY_MEM_REGION) *(MemIndex+1));

+    FreePages (mRegion, Pages1);

+    FreePages (MemoryMap, Pages0);

+    if (Error)

+    {

+        DEBUG ((EFI_D_ERROR, "ERROR:fdt_setprop(): %a\n", fdt_strerror (Error)));

+        Status = EFI_INVALID_PARAMETER;

+        return Status;

+    }

+  return Status;

+}

+

+/*

+ * Entry point for fdtupdate lib.

+ */

+

+EFI_STATUS EFIFdtUpdate(UINTN FdtFileAddr)

+{

+    INTN                Error;

+    VOID*               Fdt;

+    UINT32              Size;

+    UINTN               NewFdtBlobSize;

+    UINTN               NewFdtBlobBase;

+    EFI_STATUS          Status = EFI_SUCCESS;

+

+    Error = fdt_check_header ((VOID*)(UINTN)(FdtFileAddr));

+    if (Error != 0)

+    {

+        DEBUG ((EFI_D_ERROR,"ERROR: Device Tree header not valid (%a)\n", fdt_strerror(Error)));

+        return EFI_INVALID_PARAMETER;

+    }

+

+    Size = (UINTN)fdt_totalsize ((VOID*)(UINTN)(FdtFileAddr));

+    NewFdtBlobSize = Size + ADD_FILE_LENGTH;

+    Fdt = (VOID*)(UINTN)FdtFileAddr;

+

+    Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData, EFI_SIZE_TO_PAGES(NewFdtBlobSize), &NewFdtBlobBase);

+    if (EFI_ERROR (Status))

+    {

+        return EFI_OUT_OF_RESOURCES;

+    }

+

+

+    Error = fdt_open_into(Fdt,(VOID*)(UINTN)(NewFdtBlobBase), (NewFdtBlobSize));

+    if (Error) {

+        DEBUG ((EFI_D_ERROR, "ERROR:fdt_open_into(): %a\n", fdt_strerror (Error)));

+        Status = EFI_INVALID_PARAMETER;

+        goto EXIT;

+    }

+

+    Fdt = (VOID*)(UINTN)NewFdtBlobBase;

+

+    Status = DelPhyhandleUpdateMacAddress(Fdt);

+    if (EFI_ERROR (Status))

+    {

+        DEBUG ((EFI_D_ERROR, "DelPhyhandleUpdateMacAddress fail:\n"));

+        Status = EFI_SUCCESS;

+    }

+

+    Status = UpdateMemoryNode(Fdt);

+    if (EFI_ERROR (Status))

+    {

+        goto EXIT;

+    }

+

+    gBS->CopyMem(((VOID*)(UINTN)(FdtFileAddr)),((VOID*)(UINTN)(NewFdtBlobBase)),NewFdtBlobSize);

+

+EXIT:

+    gBS->FreePages(NewFdtBlobBase,EFI_SIZE_TO_PAGES(NewFdtBlobSize));

+

+    return Status;

+}

diff --git a/Platforms/Hisilicon/D02/FdtUpdateLibD02/FdtUpdateLib.inf b/Platforms/Hisilicon/D02/FdtUpdateLibD02/FdtUpdateLib.inf
new file mode 100644
index 0000000..c952414
--- /dev/null
+++ b/Platforms/Hisilicon/D02/FdtUpdateLibD02/FdtUpdateLib.inf
@@ -0,0 +1,43 @@
+#/** @file

+#

+#    Copyright (c) 2015, Hisilicon Limited. All rights reserved.

+#    Copyright (c) 2015, Linaro Limited. All rights reserved.

+#

+#    This program and the accompanying materials

+#    are licensed and made available under the terms and conditions of the BSD License

+#    which accompanies this distribution. The full text of the license may be found at

+#    http://opensource.org/licenses/bsd-license.php

+#

+#    THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

+#    WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

+#

+#**/

+

+[Defines]

+  INF_VERSION                    = 0x00010005

+  BASE_NAME                      = FdtUpdateLib

+  FILE_GUID                      = 02CF1727-E697-47fc-8CC2-5DCB81B26DD9

+  MODULE_TYPE                    = BASE

+  VERSION_STRING                 = 1.0

+  LIBRARY_CLASS                  = FdtUpdateLib

+

+

+[Sources.common]

+  FdtUpdateLib.c

+

+

+[Packages]

+  MdePkg/MdePkg.dec

+  MdeModulePkg/MdeModulePkg.dec

+  EmbeddedPkg/EmbeddedPkg.dec

+  OpenPlatformPkg/Chips/Hisilicon/HisiPkg.dec

+

+[LibraryClasses]

+  FdtLib

+  OemAddressMapLib

+

+[Protocols]

+  gHisiBoardNicProtocolGuid

+

+[Pcd]

+