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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
/** @file
An instance of the NorFlashPlatformLib for Kvmtool platform.
Copyright (c) 2020, ARM Ltd. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/VirtNorFlashPlatformLib.h>
#include <Protocol/FdtClient.h>
/** Macro defining the NOR block size configured in Kvmtool.
*/
#define KVMTOOL_NOR_BLOCK_SIZE SIZE_64KB
/** Macro defining the maximum number of Flash devices.
*/
#define MAX_FLASH_DEVICES 4
/** Macro defining the cfi-flash label describing the UEFI variable store.
*/
#define LABEL_UEFI_VAR_STORE "System-firmware"
STATIC VIRT_NOR_FLASH_DESCRIPTION mNorFlashDevices[MAX_FLASH_DEVICES];
STATIC UINTN mNorFlashDeviceCount = 0;
STATIC INT32 mUefiVarStoreNode = MAX_INT32;
STATIC FDT_CLIENT_PROTOCOL *mFdtClient;
/** This function performs platform specific actions to initialise
the NOR flash, if required.
@retval EFI_SUCCESS Success.
**/
EFI_STATUS
VirtNorFlashPlatformInitialization (
VOID
)
{
EFI_STATUS Status;
DEBUG ((DEBUG_INFO, "NorFlashPlatformInitialization\n"));
if ((mNorFlashDeviceCount > 0) && (mUefiVarStoreNode != MAX_INT32)) {
//
// UEFI takes ownership of the cfi-flash hardware, and exposes its
// functionality through the UEFI Runtime Variable Service. This means we
// need to disable it in the device tree to prevent the OS from attaching
// its device driver as well.
// Note: This library is loaded twice. First by FaultTolerantWriteDxe to
// setup the PcdFlashNvStorageFtw* and later by NorFlashDxe to provide the
// NorFlashPlatformLib interfaces. If the node is disabled when the library
// is first loaded, then during the subsequent loading of the library the
// call to FindNextCompatibleNode() from the library constructor skips the
// FDT node used for UEFI storage variable. Due to this we cannot setup the
// NOR flash device description i.e. mNorFlashDevices[].
// Since NorFlashPlatformInitialization() is called only by NorFlashDxe,
// we know it is safe to disable the node here.
//
Status = mFdtClient->SetNodeProperty (
mFdtClient,
mUefiVarStoreNode,
"status",
"disabled",
sizeof ("disabled")
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "Failed to set cfi-flash status to 'disabled'\n"));
}
} else {
Status = EFI_NOT_FOUND;
DEBUG ((DEBUG_ERROR, "Flash device for UEFI variable storage not found\n"));
}
return Status;
}
/** Initialise Non volatile Flash storage variables.
@param [in] FlashDevice Pointer to the NOR Flash device.
@retval EFI_SUCCESS Success.
@retval EFI_INVALID_PARAMETER A parameter is invalid.
@retval EFI_OUT_OF_RESOURCES Insufficient flash storage space.
**/
STATIC
EFI_STATUS
SetupVariableStore (
IN VIRT_NOR_FLASH_DESCRIPTION *FlashDevice
)
{
UINTN FlashRegion;
UINTN FlashNvStorageVariableBase;
UINTN FlashNvStorageFtwWorkingBase;
UINTN FlashNvStorageFtwSpareBase;
UINTN FlashNvStorageVariableSize;
UINTN FlashNvStorageFtwWorkingSize;
UINTN FlashNvStorageFtwSpareSize;
FlashNvStorageVariableSize = PcdGet32 (PcdFlashNvStorageVariableSize);
FlashNvStorageFtwWorkingSize = PcdGet32 (PcdFlashNvStorageFtwWorkingSize);
FlashNvStorageFtwSpareSize = PcdGet32 (PcdFlashNvStorageFtwSpareSize);
if ((FlashNvStorageVariableSize == 0) ||
(FlashNvStorageFtwWorkingSize == 0) ||
(FlashNvStorageFtwSpareSize == 0))
{
DEBUG ((DEBUG_ERROR, "FlashNvStorage size not defined\n"));
return EFI_INVALID_PARAMETER;
}
// Setup the variable store
FlashRegion = FlashDevice->DeviceBaseAddress;
FlashNvStorageVariableBase = FlashRegion;
FlashRegion += PcdGet32 (PcdFlashNvStorageVariableSize);
FlashNvStorageFtwWorkingBase = FlashRegion;
FlashRegion += PcdGet32 (PcdFlashNvStorageFtwWorkingSize);
FlashNvStorageFtwSpareBase = FlashRegion;
FlashRegion += PcdGet32 (PcdFlashNvStorageFtwSpareSize);
if (FlashRegion > (FlashDevice->DeviceBaseAddress + FlashDevice->Size)) {
DEBUG ((DEBUG_ERROR, "Insufficient flash storage size\n"));
return EFI_OUT_OF_RESOURCES;
}
PcdSet32S (
PcdFlashNvStorageVariableBase,
FlashNvStorageVariableBase
);
PcdSet32S (
PcdFlashNvStorageFtwWorkingBase,
FlashNvStorageFtwWorkingBase
);
PcdSet32S (
PcdFlashNvStorageFtwSpareBase,
FlashNvStorageFtwSpareBase
);
DEBUG ((
DEBUG_INFO,
"PcdFlashNvStorageVariableBase = 0x%x\n",
FlashNvStorageVariableBase
));
DEBUG ((
DEBUG_INFO,
"PcdFlashNvStorageVariableSize = 0x%x\n",
FlashNvStorageVariableSize
));
DEBUG ((
DEBUG_INFO,
"PcdFlashNvStorageFtwWorkingBase = 0x%x\n",
FlashNvStorageFtwWorkingBase
));
DEBUG ((
DEBUG_INFO,
"PcdFlashNvStorageFtwWorkingSize = 0x%x\n",
FlashNvStorageFtwWorkingSize
));
DEBUG ((
DEBUG_INFO,
"PcdFlashNvStorageFtwSpareBase = 0x%x\n",
FlashNvStorageFtwSpareBase
));
DEBUG ((
DEBUG_INFO,
"PcdFlashNvStorageFtwSpareSize = 0x%x\n",
FlashNvStorageFtwSpareSize
));
return EFI_SUCCESS;
}
/** Return the Flash devices on the platform.
@param [out] NorFlashDescriptions Pointer to the Flash device description.
@param [out] Count Number of Flash devices.
@retval EFI_SUCCESS Success.
@retval EFI_NOT_FOUND Flash device not found.
**/
EFI_STATUS
VirtNorFlashPlatformGetDevices (
OUT VIRT_NOR_FLASH_DESCRIPTION **NorFlashDescriptions,
OUT UINT32 *Count
)
{
if (mNorFlashDeviceCount > 0) {
*NorFlashDescriptions = mNorFlashDevices;
*Count = mNorFlashDeviceCount;
return EFI_SUCCESS;
}
return EFI_NOT_FOUND;
}
/** Entrypoint for NorFlashPlatformLib.
@param [in] ImageHandle The handle to the image.
@param [in] SystemTable Pointer to the System Table.
@retval EFI_SUCCESS Success.
@retval EFI_INVALID_PARAMETER A parameter is invalid.
@retval EFI_NOT_FOUND Flash device not found.
**/
EFI_STATUS
EFIAPI
NorFlashPlatformLibConstructor (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
INT32 Node;
EFI_STATUS Status;
EFI_STATUS FindNodeStatus;
CONST UINT32 *Reg;
UINT32 PropSize;
UINT64 Base;
UINT64 Size;
UINTN UefiVarStoreIndex;
CONST CHAR8 *Label;
UINT32 LabelLen;
if (mNorFlashDeviceCount != 0) {
return EFI_SUCCESS;
}
Status = gBS->LocateProtocol (
&gFdtClientProtocolGuid,
NULL,
(VOID **)&mFdtClient
);
ASSERT_EFI_ERROR (Status);
UefiVarStoreIndex = MAX_UINTN;
for (FindNodeStatus = mFdtClient->FindCompatibleNode (
mFdtClient,
"cfi-flash",
&Node
);
!EFI_ERROR (FindNodeStatus) &&
(mNorFlashDeviceCount < MAX_FLASH_DEVICES);
FindNodeStatus = mFdtClient->FindNextCompatibleNode (
mFdtClient,
"cfi-flash",
Node,
&Node
))
{
Status = mFdtClient->GetNodeProperty (
mFdtClient,
Node,
"label",
(CONST VOID **)&Label,
&LabelLen
);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_ERROR,
"%a: GetNodeProperty ('label') failed (Status == %r)\n",
__FUNCTION__,
Status
));
} else if (AsciiStrCmp (Label, LABEL_UEFI_VAR_STORE) == 0) {
UefiVarStoreIndex = mNorFlashDeviceCount;
mUefiVarStoreNode = Node;
}
Status = mFdtClient->GetNodeProperty (
mFdtClient,
Node,
"reg",
(CONST VOID **)&Reg,
&PropSize
);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_ERROR,
"%a: GetNodeProperty () failed (Status == %r)\n",
__FUNCTION__,
Status
));
continue;
}
ASSERT ((PropSize % (4 * sizeof (UINT32))) == 0);
while ((PropSize >= (4 * sizeof (UINT32))) &&
(mNorFlashDeviceCount < MAX_FLASH_DEVICES))
{
Base = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[0]));
Size = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[2]));
Reg += 4;
PropSize -= 4 * sizeof (UINT32);
//
// Disregard any flash devices that overlap with the primary FV.
// The firmware is not updatable from inside the guest anyway.
//
if ((PcdGet64 (PcdFvBaseAddress) + PcdGet32 (PcdFvSize) > Base) &&
((Base + Size) > PcdGet64 (PcdFvBaseAddress)))
{
continue;
}
DEBUG ((
DEBUG_INFO,
"NOR%d : Base = 0x%lx, Size = 0x%lx\n",
mNorFlashDeviceCount,
Base,
Size
));
mNorFlashDevices[mNorFlashDeviceCount].DeviceBaseAddress = (UINTN)Base;
mNorFlashDevices[mNorFlashDeviceCount].RegionBaseAddress = (UINTN)Base;
mNorFlashDevices[mNorFlashDeviceCount].Size = (UINTN)Size;
mNorFlashDevices[mNorFlashDeviceCount].BlockSize = KVMTOOL_NOR_BLOCK_SIZE;
mNorFlashDeviceCount++;
}
} // for
// Setup the variable store in the last device
if (mNorFlashDeviceCount > 0) {
if (UefiVarStoreIndex == MAX_UINTN) {
// We did not find a label matching the UEFI Variable store. Default to
// using the last cfi-flash device as the variable store.
UefiVarStoreIndex = mNorFlashDeviceCount - 1;
mUefiVarStoreNode = Node;
}
if (mNorFlashDevices[UefiVarStoreIndex].DeviceBaseAddress != 0) {
return SetupVariableStore (&mNorFlashDevices[UefiVarStoreIndex]);
}
}
return EFI_NOT_FOUND;
}
|