summaryrefslogtreecommitdiff
path: root/Board/EM/Platform/FirmwareVerInfo/FirmwareVerInfo.c
blob: 404a2cf68d061060cd691a9af076e0d1fddc66f2 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/** @file
**/

#include <Tiano.h>
#include <EfiDriverLib.h>

#include EFI_GUID_DEFINITION(DataHubRecords)
#include EFI_PROTOCOL_CONSUMER(DataHub)
#include EFI_PROTOCOL_CONSUMER(ExitPmAuth)
#include <RcFviDxeLib.h>

#include <Protocol\SMBios.h>


#pragma pack(1)

typedef struct {
    UINT8 Type;
    UINT8 Length;
    UINT16 Handle;
    UINT8 GroupName;
} SMBIOS_TYPE14_STRUCTURE_HDR;

typedef struct {
    UINT8 Type;
    UINT16 Handle;
} SMBIOS_TYPE14_STRUCTURE_ITEM;

#pragma pack()

VOID CreateFirmwareVerInfo(IN EFI_EVENT Event, IN VOID *Context);

EFI_SMBIOS_PROTOCOL *gSmbiosProtocol = NULL;

/**
  The user Entry Point for DXE driver. The user code starts with this function
  as the real entry point for the image goes into a library that calls this 
  function.

  @param[in] ImageHandle    The firmware allocated handle for the EFI image.  
  @param[in] SystemTable    A pointer to the EFI System Table.
  
  @retval EFI_SUCCESS       The entry point is executed successfully.
  @retval other             Some error occurs when executing this entry point.

**/
EFI_STATUS
EFIAPI
FirmwareVerInfoEntryPoint (
    IN EFI_HANDLE ImageHandle,
    IN EFI_SYSTEM_TABLE *SystemTable
)
{
    EFI_STATUS Status;
    EFI_GUID gEfiSmbiosProtocolGuid = EFI_SMBIOS_PROTOCOL_GUID;
    EFI_EVENT Event;

    DxeInitializeDriverLib (ImageHandle, SystemTable);

    Status = gBS->LocateProtocol(&gEfiSmbiosProtocolGuid, NULL, &gSmbiosProtocol);
    ASSERT_EFI_ERROR(Status);
    if (!EFI_ERROR(Status))
    {
        Status = EfiCreateEventReadyToBoot(
                        EFI_TPL_CALLBACK,
                        CreateFirmwareVerInfo,
                        NULL,
                        &Event
                        );
        ASSERT_EFI_ERROR (Status);
    }

    return EFI_SUCCESS;
}


EFI_STATUS BuildFviStruct(BOOLEAN GroupAssociation, EFI_DATA_RECORD_HEADER *Record)
{
    static VOID *FviStructBuffer = NULL;
    static UINTN FviStructBufferSize = 0;
    static UINTN FviStructSize = 0;
    static UINTN ElementCount = 0;

    EFI_STATUS Status;
    EFI_SUBCLASS_TYPE1_HEADER *DataHeader = NULL;
    FVI_HEADER *FviHeader = NULL;
    UINT16 BufferSize = 0;
    SMBIOS_TYPE14_STRUCTURE_HDR *SmbiosHdr = NULL;
    SMBIOS_TYPE14_STRUCTURE_ITEM *ItemPtr = NULL;
    CHAR8 GroupName[] = "Firmware Version Info\0";

    if (GroupAssociation)
    {
        if ((FviStructBuffer == NULL) && (FviStructSize > sizeof(SMBIOS_TYPE14_STRUCTURE_HDR)))
        {
            return EFI_UNSUPPORTED;
        }

        SmbiosHdr = (SMBIOS_TYPE14_STRUCTURE_HDR *)FviStructBuffer;

        SmbiosHdr->Type = 14;
        SmbiosHdr->Length = (UINT8)FviStructSize;
        SmbiosHdr->Handle = 0xFFFF;

        SmbiosHdr->GroupName = 0x01;
        EfiAsciiStrCpy((CHAR8 *)((UINT8 *)FviStructBuffer+FviStructSize), GroupName);
        FviStructSize += sizeof(GroupName);

        Status = gSmbiosProtocol->SmbiosAddStructure((UINT8 *)SmbiosHdr, (UINT16)(FviStructSize));
        if (!EFI_ERROR(Status))
        {
            gBS->FreePool(FviStructBuffer);
            FviStructBuffer = NULL;
        }

        return Status;
    }

    if (Record == NULL)
    {
        return EFI_INVALID_PARAMETER;
    }

    if (FviStructBuffer == NULL)
    {
        FviStructBufferSize = 0x2000;
        FviStructBuffer = EfiLibAllocateZeroPool(FviStructBufferSize);
        FviStructSize = sizeof(SMBIOS_TYPE14_STRUCTURE_HDR);
    }

    SmbiosHdr = (SMBIOS_TYPE14_STRUCTURE_HDR *)FviStructBuffer;
    ItemPtr = (SMBIOS_TYPE14_STRUCTURE_ITEM *)(SmbiosHdr + 1);
    ItemPtr += ElementCount;

    if (EfiCompareGuid(&Record->DataRecordGuid, &gMiscSubClassName))
    {
        DataHeader = (EFI_SUBCLASS_TYPE1_HEADER *)(Record + 1);
        FviHeader = (FVI_HEADER *)(UINT8 *)(DataHeader + 1);

        if (FviHeader->Header.Type == FVI_SMBIOS_TYPE)
        {
            FviHeader->Header.Handle = 0xFFFF;
            BufferSize = (UINT16)(Record->RecordSize - sizeof(EFI_DATA_RECORD_HEADER) - sizeof(EFI_SUBCLASS_TYPE1_HEADER));
            Status = gSmbiosProtocol->SmbiosAddStructure((UINT8 *)FviHeader, BufferSize);
            if (!EFI_ERROR(Status))
            {
                ItemPtr->Type = FviHeader->Header.Type;
                ItemPtr->Handle = FviHeader->Header.Handle;
                FviStructSize += sizeof(SMBIOS_TYPE14_STRUCTURE_ITEM);
                ElementCount++;
            }
        }
    }

    return EFI_SUCCESS;
}


VOID CreateFirmwareVerInfo(IN EFI_EVENT Event, IN VOID *Context)
{
    EFI_STATUS Status;
    EFI_HANDLE DataHubHandle;
    UINTN HandleSize;
    EFI_DATA_HUB_PROTOCOL *DataHub = NULL;
    EFI_DATA_RECORD_HEADER *Record = NULL;
    UINT64 MonotonicCount = 0;

    HandleSize = sizeof (EFI_HANDLE);

    Status = gBS->LocateHandle (
                     ByProtocol,
                     &gEfiDataHubProtocolGuid,
                     NULL,
                     &HandleSize,
                     &DataHubHandle
                 );
    if (EFI_ERROR (Status))
    {
        return;
    }

    Status = gBS->HandleProtocol (
                     DataHubHandle,
                     &gEfiDataHubProtocolGuid,
                     &DataHub
                 );
    if (EFI_ERROR (Status))
    {
        return;
    }

    do {
        Status = DataHub->GetNextRecord (
                             DataHub,
                             &MonotonicCount,
                             NULL,
                             &Record);
        if (!EFI_ERROR (Status))
        {
            if (Record->DataRecordClass == EFI_DATA_RECORD_CLASS_DATA)
            {
                BuildFviStruct(FALSE, Record);
            }
        }
    } while (!EFI_ERROR (Status) && (MonotonicCount != 0));

    Status = BuildFviStruct(TRUE, NULL);
    if (EFI_ERROR(Status))
    {
        DEBUG((EFI_D_ERROR, "!!!Firmware Version Information Error!!!\n"));
        DEBUG((EFI_D_ERROR, "!!!Type 14 ¡V Group Associations Indicator!!!\n"));
    }

    if (Event != NULL)
    {
        gBS->CloseEvent(Event);
    }
}