summaryrefslogtreecommitdiffstats
path: root/FmpDevicePkg/FmpDxe/VariableSupport.c
blob: b7e4cb98962881316b249f0c5c9be227d415a052 (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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/** @file
  UEFI variable support functions for Firmware Management Protocol based
  firmware updates.

  Copyright (c) 2016, Microsoft Corporation. All rights reserved.<BR>
  Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
  1. Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

**/

#include <PiDxe.h>
#include <Library/DebugLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/UefiLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Protocol/VariableLock.h>
#include "VariableSupport.h"

///
/// Array of UEFI variable names that are locked in LockAllFmpVariables().
///
const CHAR16  *mFmpVariableLockList[] = {
  VARNAME_VERSION,
  VARNAME_LSV,
  VARNAME_LASTATTEMPTSTATUS,
  VARNAME_LASTATTEMPTVERSION
};

/**
  Returns the value used to fill in the Version field of the
  EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the GetImageInfo()
  service of the Firmware Management Protocol.  The value is read from a UEFI
  variable.  If the UEFI variables does not exist, then a default version value
  is returned.

  UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"FmpVersion"

  @return  The version of the firmware image in the firmware device.

**/
UINT32
GetVersionFromVariable (
  VOID
  )
{
  EFI_STATUS  Status;
  UINT32      *Value;
  UINTN       Size;
  UINT32      Version;

  Value = NULL;
  Size = 0;
  Version = DEFAULT_VERSION;

  Status = GetVariable2 (VARNAME_VERSION, &gEfiCallerIdGuid, (VOID **)&Value, &Size);
  if (EFI_ERROR (Status) || (Value == NULL)) {
    DEBUG ((DEBUG_ERROR, "Failed to get the Version from variable.  Status = %r\n", Status));
    return Version;
  }

  //
  // No error from call
  //
  if (Size == sizeof (*Value)) {
    //
    // Successful read
    //
    Version = *Value;
  } else {
    //
    // Return default since size was unknown
    //
    DEBUG ((DEBUG_ERROR, "Getting version Variable returned a size different than expected. Size = 0x%x\n", Size));
  }

  FreePool (Value);

  return Version;
}

/**
  Returns the value used to fill in the LowestSupportedVersion field of the
  EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the GetImageInfo()
  service of the Firmware Management Protocol.  The value is read from a UEFI
  variable.  If the UEFI variables does not exist, then a default lowest
  supported version value is returned.

  UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"FmpLsv"

  @return  The lowest supported version of the firmware image in the firmware
           device.

**/
UINT32
GetLowestSupportedVersionFromVariable (
  VOID
  )
{
  EFI_STATUS  Status;
  UINT32      *Value;
  UINTN       Size;
  UINT32      Version;

  Value   = NULL;
  Size    = 0;
  Version = DEFAULT_LOWESTSUPPORTEDVERSION;

  Status = GetVariable2 (VARNAME_LSV, &gEfiCallerIdGuid, (VOID **)&Value, &Size);
  if (EFI_ERROR (Status) || (Value == NULL)) {
    DEBUG ((DEBUG_WARN, "Warning: Failed to get the Lowest Supported Version from variable.  Status = %r\n", Status));
    return Version;
  }

  //
  // No error from call
  //
  if (Size == sizeof (*Value)) {
    //
    // Successful read
    //
    Version = *Value;
  } else {
    //
    // Return default since size was unknown
    //
    DEBUG ((DEBUG_ERROR, "Getting LSV Variable returned a size different than expected. Size = 0x%x\n", Size));
  }

  FreePool (Value);

  return Version;
}

/**
  Returns the value used to fill in the LastAttemptStatus field of the
  EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the GetImageInfo()
  service of the Firmware Management Protocol.  The value is read from a UEFI
  variable.  If the UEFI variables does not exist, then a default last attempt
  status value is returned.

  UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"LastAttemptStatus"

  @return  The last attempt status value for the most recent capsule update.

**/
UINT32
GetLastAttemptStatusFromVariable (
  VOID
  )
{
  EFI_STATUS  Status;
  UINT32      *Value;
  UINTN       Size;
  UINT32      LastAttemptStatus;

  Value = NULL;
  Size  = 0;
  LastAttemptStatus     = DEFAULT_LASTATTEMPT;

  Status = GetVariable2 (VARNAME_LASTATTEMPTSTATUS, &gEfiCallerIdGuid, (VOID **)&Value, &Size);
  if (EFI_ERROR (Status) || (Value == NULL)) {
    DEBUG ((DEBUG_WARN, "Warning: Failed to get the Last Attempt Status from variable.  Status = %r\n", Status));
    return LastAttemptStatus;
  }

  //
  // No error from call
  //
  if (Size == sizeof (*Value)) {
    //
    // Successful read
    //
    LastAttemptStatus = *Value;
  } else {
    //
    // Return default since size was unknown
    //
    DEBUG (
      (DEBUG_ERROR,
      "Getting Last Attempt Status Variable returned a size different than expected. Size = 0x%x\n",
      Size)
      );
  }

  FreePool (Value);

  return LastAttemptStatus;
}

/**
  Returns the value used to fill in the LastAttemptVersion field of the
  EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the GetImageInfo()
  service of the Firmware Management Protocol.  The value is read from a UEFI
  variable.  If the UEFI variables does not exist, then a default last attempt
  version value is returned.

  UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"LastAttemptVersion"

  @return  The last attempt version value for the most recent capsule update.

**/
UINT32
GetLastAttemptVersionFromVariable (
  VOID
  )
{
  EFI_STATUS  Status;
  UINT32      *Value;
  UINTN       Size;
  UINT32      Version;

  Value   = NULL;
  Size    = 0;
  Version = DEFAULT_LASTATTEMPT;

  Status = GetVariable2 (VARNAME_LASTATTEMPTVERSION, &gEfiCallerIdGuid, (VOID **)&Value, &Size);
  if (EFI_ERROR (Status) || (Value == NULL)) {
    DEBUG ((DEBUG_WARN, "Warning: Failed to get the Last Attempt Version from variable.  Status = %r\n", Status));
    return Version;
  }

  //
  // No error from call
  //
  if (Size == sizeof (*Value)) {
    //
    // Successful read
    //
    Version = *Value;
  } else {
    //
    // Return default since size was unknown
    //
    DEBUG (
      (DEBUG_ERROR,
      "Getting Last Attempt Version variable returned a size different than expected. Size = 0x%x\n",
      Size)
      );
  }

  FreePool (Value);

  return Version;
}


/**
  Saves the version current of the firmware image in the firmware device to a
  UEFI variable.

  UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"FmpVersion"

  @param[in] Version  The version of the firmware image in the firmware device.

**/
VOID
SetVersionInVariable (
   UINT32  Version
  )
{
  EFI_STATUS  Status;
  UINT32      Current;

  Status = EFI_SUCCESS;

  Current = GetVersionFromVariable();
  if (Current != Version) {
    Status = gRT->SetVariable (
                    VARNAME_VERSION,
                    &gEfiCallerIdGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (Version),
                    &Version
                    );
    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "Failed to set the Version into a variable.  Status = %r\n", Status));
    }
  } else {
    DEBUG ((DEBUG_INFO, "Version variable doesn't need to update.  Same value as before.\n"));
  }
}

/**
  Saves the lowest supported version current of the firmware image in the
  firmware device to a UEFI variable.

  UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"FmpLsv"

  @param[in] LowestSupportedVersion The lowest supported version of the firmware image
                                    in the firmware device.

**/
VOID
SetLowestSupportedVersionInVariable (
   UINT32  LowestSupportedVersion
  )
{
  EFI_STATUS  Status;
  UINT32      Current;

  Status = EFI_SUCCESS;

  Current = GetLowestSupportedVersionFromVariable();
  if (LowestSupportedVersion > Current) {
    Status = gRT->SetVariable (
                    VARNAME_LSV,
                    &gEfiCallerIdGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (LowestSupportedVersion), &LowestSupportedVersion
                    );
    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "Failed to set the LSV into a variable.  Status = %r\n", Status));
    }
  } else {
    DEBUG ((DEBUG_INFO, "LSV variable doesn't need to update.  Same value as before.\n"));
  }
}

/**
  Saves the last attempt status value of the most recent FMP capsule update to a
  UEFI variable.

  UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"LastAttemptStatus"

  @param[in] LastAttemptStatus  The last attempt status of the most recent FMP
                                capsule update.

**/
VOID
SetLastAttemptStatusInVariable (
   UINT32  LastAttemptStatus
  )
{
  EFI_STATUS  Status;
  UINT32      Current;

  Status = EFI_SUCCESS;

  Current = GetLastAttemptStatusFromVariable();
  if (Current != LastAttemptStatus) {
    Status = gRT->SetVariable (
                    VARNAME_LASTATTEMPTSTATUS,
                    &gEfiCallerIdGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (LastAttemptStatus),
                    &LastAttemptStatus
                    );
    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "Failed to set the LastAttemptStatus into a variable.  Status = %r\n", Status));
    }
  } else {
    DEBUG ((DEBUG_INFO, "LastAttemptStatus variable doesn't need to update.  Same value as before.\n"));
  }
}

/**
  Saves the last attempt version value of the most recent FMP capsule update to
  a UEFI variable.

  UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"LastAttemptVersion"

  @param[in] LastAttemptVersion  The last attempt version value of the most
                                 recent FMP capsule update.

**/
VOID
SetLastAttemptVersionInVariable (
   UINT32  LastAttemptVersion
  )
{
  EFI_STATUS  Status;
  UINT32      Current;

  Status = EFI_SUCCESS;

  Current = GetLastAttemptVersionFromVariable();
  if (Current != LastAttemptVersion) {
    Status = gRT->SetVariable (
                    VARNAME_LASTATTEMPTVERSION,
                    &gEfiCallerIdGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (LastAttemptVersion),
                    &LastAttemptVersion
                    );
    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "Failed to set the LastAttemptVersion into a variable.  Status = %r\n", Status));
    }
  } else {
    DEBUG ((DEBUG_INFO, "LastAttemptVersion variable doesn't need to update.  Same value as before.\n"));
  }
}

/**
  Locks all the UEFI Variables used by this module.

  @retval  EFI_SUCCESS      All UEFI variables are locked.
  @retval  EFI_UNSUPPORTED  Variable Lock Protocol not found.
  @retval  Other            One of the UEFI variables could not be locked.

**/
EFI_STATUS
LockAllFmpVariables (
  VOID
  )
{
  EFI_STATUS                    Status;
  EDKII_VARIABLE_LOCK_PROTOCOL  *VariableLock;
  EFI_STATUS                    ReturnStatus;
  UINTN                         Index;

  VariableLock = NULL;
  Status = gBS->LocateProtocol (
                  &gEdkiiVariableLockProtocolGuid,
                  NULL,
                  (VOID **)&VariableLock
                  );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to locate Variable Lock Protocol (%r).\n", Status));
    return EFI_UNSUPPORTED;
  }

  ReturnStatus = EFI_SUCCESS;
  for (Index = 0; Index < ARRAY_SIZE (mFmpVariableLockList); Index++) {
    Status = VariableLock->RequestToLock (
                             VariableLock,
                             (CHAR16 *)mFmpVariableLockList[Index],
                             &gEfiCallerIdGuid
                             );
    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to lock variable %g %s.  Status = %r\n",
        &gEfiCallerIdGuid,
        mFmpVariableLockList[Index],
        Status
        ));
      if (!EFI_ERROR (ReturnStatus)) {
        ReturnStatus = Status;
      }
    }
  }

  return ReturnStatus;
}