summaryrefslogtreecommitdiff
path: root/Silicon/BroxtonSoC/BroxtonSiPkg/Txe/Library/PeiSecLib/HeciMsgLib.c
blob: 390f0d71d397f5441e577c0eb8b8eb75fc641421 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/** @file
  Implementation file for Heci Message functionality.

  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>

  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 <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/HeciMsgLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/PciLib.h>
#include <Library/PerformanceLib.h>
#include <Library/PeiServicesLib.h>
#include <Library/TimerLib.h>
#include <Ppi/HeciPpi.h>
#include <CoreBiosMsg.h>
#include <HeciRegs.h>
#include <SeCAccess.h>
#include <IndustryStandard/Pci22.h>

/**
  Calculate if the circular buffer has overflowed.

  @param[in] ReadPointer       Location of the read pointer.
  @param[in] WritePointer      Location of the write pointer.

  @return    Number of filled slots.

**/
UINT8
FilledSlots (
  IN  UINT32 ReadPointer,
  IN  UINT32 WritePointer
  )
{
  UINT8    FilledSlots;

  FilledSlots = (((INT8) WritePointer) - ((INT8) ReadPointer));

  return FilledSlots;
}


/**
  Calculate if the circular buffer has overflowed

  @param[in] ReadPointer             Value read from host/me read pointer.
  @param[in] WritePointer            Value read from host/me write pointer.
  @param[in] BufferDepth             Value read from buffer depth register.

  @retval    EFI_DEVICE_ERROR        The circular buffer has overflowed.
  @retval    EFI_SUCCESS             The circular buffer does not overflowed.

**/
EFI_STATUS
OverflowCB (
  IN  UINT32 ReadPointer,
  IN  UINT32 WritePointer,
  IN  UINT32 BufferDepth
  )
{
  UINT8     FilledSlots;

  FilledSlots = (((INT8) WritePointer) - ((INT8) ReadPointer));

  //
  // test for overflow
  //
  if (FilledSlots > ((UINT8) BufferDepth)) {
    return EFI_DEVICE_ERROR;
  }

  return EFI_SUCCESS;
}


/**
  Send Get Firmware SKU Request to SEC.

  @param[in, out] FwCapsSku                 SEC Firmware Capability SKU.

  @retval         EFI_UNSUPPORTED           Current SEC mode doesn't support this function.
  @retval         EFI_SUCCESS               Command succeeded.
  @retval         EFI_DEVICE_ERROR          HECI Device error, command aborts abnormally.
  @retval         EFI_TIMEOUT               HECI does not return the buffer before timeout.
  @retval         EFI_BUFFER_TOO_SMALL      Message Buffer is too small for the Acknowledge.

**/
EFI_STATUS
PeiHeciGetFwCapsSkuMsg (
  IN OUT SECFWCAPS_SKU             *FwCapsSku
  )
{
  EFI_STATUS                      Status;
  GEN_GET_FW_CAPS_SKU_BUFFER      MsgGenGetFwCapsSku;
  UINT32                          Length;
  UINT32                          RecvLength;
  UINT32                          SeCMode;

  Status = HeciGetSeCMode (HECI1_DEVICE, &SeCMode);
  if (EFI_ERROR (Status) || (SeCMode != SEC_MODE_NORMAL)) {
    return EFI_UNSUPPORTED;
  }

  MsgGenGetFwCapsSku.Request.MKHIHeader.Data = 0;
  MsgGenGetFwCapsSku.Request.MKHIHeader.Fields.GroupId = MKHI_FWCAPS_GROUP_ID;
  MsgGenGetFwCapsSku.Request.MKHIHeader.Fields.Command = FWCAPS_GET_RULE_CMD;
  MsgGenGetFwCapsSku.Request.MKHIHeader.Fields.IsResponse = 0;
  MsgGenGetFwCapsSku.Request.Data.RuleId = 0;
  Length = sizeof (GEN_GET_FW_CAPSKU);
  RecvLength = sizeof (GEN_GET_FW_CAPS_SKU_ACK);

  //
  // Send Get FW SKU Request to SEC
  //
  Status = HeciSendwACK (
             HECI1_DEVICE,
             (UINT32 *) &MsgGenGetFwCapsSku,
             Length,
             &RecvLength,
             BIOS_FIXED_HOST_ADDR,
             HECI_CORE_MESSAGE_ADDR
             );

  if (!EFI_ERROR (Status) && ((MsgGenGetFwCapsSku.Response.MKHIHeader.Fields.Command) == FWCAPS_GET_RULE_CMD) &&
    ((MsgGenGetFwCapsSku.Response.MKHIHeader.Fields.IsResponse) == 1) &&
    (MsgGenGetFwCapsSku.Response.MKHIHeader.Fields.Result == 0)) {
    *FwCapsSku = MsgGenGetFwCapsSku.Response.Data.FWCapSku;
  }

  return Status;
}


/**
  This message is sent by the BIOS or IntelR MEBX. One of usages is to utilize
  this command to determine if the platform runs in Consumer or Corporate SKU
  size firmware.

  @param[out] RuleData                PlatformBrand,
                                      IntelMeFwImageType,
                                      SuperSku,
                                      PlatformTargetUsageType

  @retval     EFI_UNSUPPORTED         Current SEC mode doesn't support this function.
  @retval     EFI_SUCCESS             Command succeeded.
  @retval     EFI_DEVICE_ERROR        HECI Device error, command aborts abnormally.
  @retval     EFI_TIMEOUT             HECI does not return the buffer before timeout.
  @retval     EFI_BUFFER_TOO_SMALL    Message Buffer is too small for the Acknowledge.

**/
EFI_STATUS
PeiHeciGetPlatformTypeMsg (
  OUT PLATFORM_TYPE_RULE_DATA     *RuleData
  )
{
  EFI_STATUS                      Status;
  UINT32                          Length;
  UINT32                          RecvLength;
  GEN_GET_PLATFORM_TYPE_BUFFER    MsgGenGetPlatformType;
  UINT32                          SeCMode;

  Status = HeciGetSeCMode (HECI1_DEVICE, &SeCMode);
  if (EFI_ERROR(Status) || (MeMode != SEC_MODE_NORMAL)) {
    return EFI_UNSUPPORTED;
  }

  MsgGenGetPlatformType.Request.MKHIHeader.Data = 0;
  MsgGenGetPlatformType.Request.MKHIHeader.Fields.GroupId = MKHI_FWCAPS_GROUP_ID;
  MsgGenGetPlatformType.Request.MKHIHeader.Fields.Command = FWCAPS_GET_RULE_CMD;
  MsgGenGetPlatformType.Request.MKHIHeader.Fields.IsResponse = 0;
  MsgGenGetPlatformType.Request.Data.RuleId = 0x1D;
  Length = sizeof (GEN_GET_PLATFORM_TYPE);
  RecvLength = sizeof (GEN_GET_PLATFORM_TYPE_ACK);

  //
  // Send Get Platform Type Request to SEC
  //
  Status = HeciSendwACK (
             HECI1_DEVICE,
             (UINT32 *) &MsgGenGetPlatformType,
             Length,
             &RecvLength,
             BIOS_FIXED_HOST_ADDR,
             HECI_CORE_MESSAGE_ADDR
             );
  if (!EFI_ERROR (Status)) {
    *RuleData = MsgGenGetPlatformType.Response.Data.RuleData;
  }

  return Status;
}


/**
  This message is sent by the BIOS or IntelR MEBX. To Get Firmware Version Request to SEC.

  @param[in,out] MsgGenGetFwVersionAck    Return themessage of FW version.

  @retval        EFI_UNSUPPORTED          Current SEC mode doesn't support this function.
  @retval        EFI_SUCCESS              Command succeeded.
  @retval        EFI_DEVICE_ERROR         HECI Device error, command aborts abnormally.
  @retval        EFI_TIMEOUT              HECI does not return the buffer before timeout.
  @retval        EFI_BUFFER_TOO_SMALL     Message Buffer is too smallfor the Acknowledge.

**/
EFI_STATUS
PeiHeciGetFwVersionMsg (
  IN OUT GEN_GET_FW_VER_ACK     *MsgGenGetFwVersionAck
  )
{
  EFI_STATUS                      Status;
  UINT32                          Length;
  UINT32                          RecvLength;
  GEN_GET_FW_VER_ACK_BUFFER       *MsgGenGetFwVersion;
  UINT32                          SeCMode;

  Status = HeciGetSeCMode (HECI1_DEVICE, &SeCMode);
  if (EFI_ERROR(Status) || (MeMode != SEC_MODE_NORMAL)) {
    return EFI_UNSUPPORTED;
  }

  MsgGenGetFwVersion.Request.MKHIHeader.Data = 0;
  MsgGenGetFwVersion.Request.MKHIHeader.Fields.GroupId = MKHI_GEN_GROUP_ID;
  MsgGenGetFwVersion.Request.MKHIHeader.Fields.Command = GEN_GET_FW_VERSION_CMD;
  MsgGenGetFwVersion.Request.MKHIHeader.Fields.IsResponse = 0;
  Length = sizeof (GEN_GET_FW_VER);
  RecvLength = sizeof (GEN_GET_FW_VER_ACK);

  //
  // Send Get Firmware Version Request to SEC
  //
  Status = HeciSendwACK (
             HECI1_DEVICE,
             (UINT32 *) &MsgGenGetFwVersion,
             Length,
             &RecvLength,
             BIOS_FIXED_HOST_ADDR,
             HECI_CORE_MESSAGE_ADDR
             );

  if (!EFI_ERROR (Status)) {
    memcpy (MsgGenGetFwVersionAck, &(MsgGenGetFwVersion.Response), RecvLength);
  }

  return Status;
}