summaryrefslogtreecommitdiff
path: root/Silicon/Hisilicon/Drivers/UpdateFdtDxe/UpdateFdtDxe.c
blob: 699a820108e7c04d35fa6990a84c53c06bf5e9df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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(NewFdtBlobSize), &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;

}