summaryrefslogtreecommitdiff
path: root/ReferenceCode/Chipset/LynxPoint/Library/RcFviDxeLib/CreateFviLibrary.c
blob: e97d65b593c3c0b49daa7fcd65efb71dc5182945 (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
219
220
221
222
223
224
225
/** @file
  Firmware Version Info Interface Lib implementation.

@copyright
  Copyright (c) 2011 - 2012 Intel Corporation. All rights reserved
  This software and associated documentation (if any) is furnished
  under a license and may only be used or copied in accordance
  with the terms of the license. Except as permitted by such
  license, no part of this software or documentation may be
  reproduced, stored in a retrieval system, or transmitted in any
  form or by any means without the express written consent of
  Intel Corporation.

  This file contains an 'Intel Peripheral Driver' and uniquely
  identified as "Intel Reference Module" and is
  licensed for Intel CPUs and chipsets under the terms of your
  license agreement with Intel or your vendor.  This file may
  be modified by the user, subject to additional terms of the
  license agreement
**/
#include "RcFviLib.h"

EFI_DATA_HUB_PROTOCOL *mDataHub = NULL;

/**
  Initialize callback context for Firmware Version Info (FVI) Interface Spec v0.7
  implementation.

  @param[in] String               The pointer to the string for calculating length

  @retval None
**/
UINT32
GetStringLen (
  IN UINT8 *String
  )
{
  UINT8 Length;

  for (Length = 0; *String != 0; String++, Length++) {
    if (Length >= SMBIOS_STRING_MAX_LENGTH) {
      break;
    }
  }

  return (UINT32) (Length + 1);
}

/**
  Initialize callback context for Firmware Version Info (FVI) Interface Spec v0.7
  implementation.

  @param[in] Type                 Value is defined in SMBIOS Type 14 - Group Associaction structure - item type.
  @param[in] Count                Number of elements included by this SMBIOS table
  @param[in] FviContext           Context of FVI elements for data hub log

  @retval None
**/
VOID
InitFviDataHubCbContext (
  IN UINT8                         Type,
  IN UINT8                         Count,
  IN FVI_DATA_HUB_CALLBACK_CONTEXT *FviContext
  )
{
  ///
  /// Locate the Data hub protocol
  ///
  if (mDataHub == NULL) {
    gBS->LocateProtocol (&gEfiDataHubProtocolGuid, NULL, (VOID **)&mDataHub);
  }

  if (FviContext != NULL) {
    FviContext->FviHeader.FviHdr.Header.Type    = Type;
    FviContext->FviHeader.FviHdr.Count          = Count;
    FviContext->FviHeader.FviHdr.Header.Length  = sizeof (FVI_HEADER) + FVI_ELEMENTS_SIZE_NOSTRING * Count;
  } else {
    ASSERT (FALSE);
  }

  return ;
}

/**
  Create the Reference code version info as per Firmware Version Info (FVI) Interface Spec v0.7
  to Data Hub.

  @param[in] FviContext           Pointer to the notification functions context, which is context of FVI
                                  elements for data hub log

  @retval None
**/
VOID
CreateRcFviDatahub (
  IN FVI_DATA_HUB_CALLBACK_CONTEXT *FviContext
  )
{
  VOID  *Registration;

  if (mDataHub == NULL) {
    EfiCreateProtocolNotifyEvent (
      &gEfiDataHubProtocolGuid,
      EFI_TPL_CALLBACK,
      DataHubCallback,
      (VOID *) FviContext,
      &Registration
      );
  } else {
    DataHubCallback ((EFI_EVENT) NULL, (VOID *) FviContext);
  }
}

/**
  Publish the Reference code version info as per Firmware Version Info (FVI) Interface Spec v0.7
  using MiscSubClass Data Hub.

  @param[in] Event                Event whose notification function is being invoked.
  @param[in] Context              Pointer to the notification functions context, which is implementation dependent.

  @retval None
**/
VOID
EFIAPI
DataHubCallback (
  IN EFI_EVENT  Event,
  IN VOID       *Context
  )
{
  EFI_STATUS                    Status;
  FVI_DATA_HUB_CALLBACK_CONTEXT *FviContext;
  UINT8                         Index;
  UINT8                         StrIndex;
  UINT8                         *Record;
  UINT8                         *LastRecord;
  UINT8                         *String;
  UINT8                         Count;
  UINT32                        Length;
  FVI_ELEMENT_AND_FUNCTION      *NewElement;

  Status = EFI_SUCCESS;
  if (mDataHub == NULL) {
    Status = gBS->LocateProtocol (&gEfiDataHubProtocolGuid, NULL, (VOID **)&mDataHub);
  }

  if ((mDataHub != NULL) && (Context != NULL)) {

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

    FviContext = (FVI_DATA_HUB_CALLBACK_CONTEXT *) Context;
    Count = FviContext->FviHeader.FviHdr.Count;

    ///
    /// Allocate a buffer to record data sorted later
    ///
    Length  = sizeof (MISC_SUBCLASS_FVI_HEADER) + (sizeof (FVI_ELEMENTS) * Count);

    Status  = EFI_OUT_OF_RESOURCES;
    Record  = (UINT8 *) AllocateZeroPool (Length);
    if (Record != NULL) {
      LastRecord = Record;

      ///
      /// Copy the headers including Data Hub and SMBIOS FviSmbios OEM type
      ///
      CopyMem (LastRecord, &(FviContext->FviHeader), sizeof (MISC_SUBCLASS_FVI_HEADER));
      LastRecord += sizeof (MISC_SUBCLASS_FVI_HEADER);
      String      = LastRecord + FVI_ELEMENTS_SIZE_NOSTRING * Count;

      NewElement  = FviContext->Elements;

      ///
      /// Copy elements including strings
      ///
      for (Index = 0, StrIndex = 1; Index < Count; Index++) {
        if (NewElement->Function != NULL) {
          NewElement->Function (&(NewElement->Element));
        }

        ///
        /// If string is implemented for ComponentName or VersionString, and then string
        /// index of ComponentName or VersionString can't be zero. The string index of
        /// ComponentName and VersionString will be updated and calculated while analyse
        /// all elements here.String index must be non-zero if implemented.
        ///
        if (NewElement->Element.ComponentName != 0) {
          NewElement->Element.ComponentName = StrIndex;
          Length = GetStringLen (NewElement->Element.NameString);
          CopyMem (String, &(NewElement->Element.NameString), Length);
          String += Length;
          StrIndex++;
        }

        if (NewElement->Element.VersionString != 0) {
          NewElement->Element.VersionString = StrIndex;
          Length = GetStringLen (NewElement->Element.VerString);
          CopyMem (String, &(NewElement->Element.VerString), Length);
          String += Length;
          StrIndex++;
        }

        CopyMem (LastRecord, &(NewElement->Element), FVI_ELEMENTS_SIZE_NOSTRING);
        LastRecord += FVI_ELEMENTS_SIZE_NOSTRING;

        NewElement++;
      }

      Length = (UINT32) (String - Record) + 1;
      Status = mDataHub->LogData (
                          mDataHub,
                          &gMiscSubClassName,
                          &gMiscProducerGuid,
                          EFI_DATA_RECORD_CLASS_DATA,
                          (VOID *) Record,
                          Length
                          );
    }

    ASSERT (!EFI_ERROR (Status));
    if (Record != NULL) {
      FreePool (Record);
    }
  }
}