summaryrefslogtreecommitdiffstats
path: root/FmpDevicePkg/FmpDxe/VariableSupport.c
blob: 86dd5b203b73159718128fbc11089fe10d4d09a1 (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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/** @file
  UEFI variable support functions for Firmware Management Protocol based
  firmware updates.

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

  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include "FmpDxe.h"
#include "VariableSupport.h"

/**
  Retrieve the value of a 32-bit UEFI Variable specified by VariableName and
  a GUID of gEfiCallerIdGuid.

  @param[in]  VariableName  Pointer to the UEFI Variable name to retrieve.
  @param[out] Valid         Set to TRUE if UEFI Variable is present and the size
                            of the UEFI Variable value is 32-bits.  Otherwise
                            FALSE.
  @param[out] Value         If Valid is set to TRUE, then the 32-bit value of
                            the UEFI Variable.  Otherwise 0.
**/
static
VOID
GetFmpVariable (
  IN  CHAR16   *VariableName,
  OUT BOOLEAN  *Valid,
  OUT UINT32   *Value
  )
{
  EFI_STATUS  Status;
  UINTN       Size;
  UINT32      *Buffer;

  *Valid = FALSE;
  *Value = 0;
  Size   = 0;
  Buffer = NULL;
  Status = GetVariable2 (
             VariableName,
             &gEfiCallerIdGuid,
             (VOID **)&Buffer,
             &Size
             );
  if (!EFI_ERROR (Status) && Size == sizeof (*Value) && Buffer != NULL) {
    *Valid = TRUE;
    *Value = *Buffer;
  }
  if (Buffer != NULL) {
    FreePool (Buffer);
  }
}

/**
  Delete the UEFI Variable with name specified by VariableName and GUID of
  gEfiCallerIdGuid.  If the variable can not be deleted, then print a
  DEBUG_ERROR message.

  @param[in] VariableName  Pointer to the UEFI Variable name to delete.
**/
static
VOID
DeleteFmpVariable (
  IN CHAR16  *VariableName
  )
{
  EFI_STATUS  Status;
  BOOLEAN     Valid;
  UINT32      Value;

  GetFmpVariable (VariableName, &Valid, &Value);
  if (Valid) {
    Status = gRT->SetVariable (VariableName, &gEfiCallerIdGuid, 0, 0, NULL);
    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to delete variable %s.  Status = %r\n", mImageIdName, VariableName, Status));
    } else {
      DEBUG ((DEBUG_INFO, "FmpDxe(%s): Deleted variable %s\n", mImageIdName, VariableName));
    }
  }
}

/**
  Retrieve the FMP Controller State UEFI Variable value.  Return NULL if
  the variable does not exist or if the size of the UEFI Variable is not the
  size of FMP_CONTROLLER_STATE.  The buffer for the UEFI Variable value
  if allocated using the UEFI Boot Service AllocatePool().

  @param[in] Private  Private context structure for the managed controller.

  @return  Pointer to the allocated FMP Controller State.  Returns NULL
           if the variable does not exist or is a different size than expected.
**/
static
FMP_CONTROLLER_STATE *
GetFmpControllerState (
  IN FIRMWARE_MANAGEMENT_PRIVATE_DATA  *Private
  )
{
  EFI_STATUS            Status;
  FMP_CONTROLLER_STATE  *FmpControllerState;
  UINTN                 Size;

  FmpControllerState = NULL;
  Size               = 0;
  Status = GetVariable2 (
             Private->FmpStateVariableName,
             &gEfiCallerIdGuid,
             (VOID **)&FmpControllerState,
             &Size
             );
  if (EFI_ERROR (Status) || FmpControllerState == NULL) {
    DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to get the controller state.  Status = %r\n", mImageIdName, Status));
  } else {
    if (Size == sizeof (*FmpControllerState)) {
      return FmpControllerState;
    }
    DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Getting controller state returned a size different than expected. Size = 0x%x\n", mImageIdName, Size));
  }
  if (FmpControllerState != NULL) {
    FreePool (FmpControllerState);
  }
  return NULL;
}

/**
  Generates a Null-terminated Unicode string UEFI Variable name from a base name
  and a hardware instance.  If the hardware instance value is 0, then the base
  name is returned.  If the hardware instance value is non-zero, then the 64-bit
  hardware instance value is converted to a 16 character hex string and appended
  to base name.  The UEFI Variable name returned is allocated using the UEFI
  Boot Service AllocatePool().

  @param[in] HardwareInstance  64-bit hardware instance value.
  @param[in] BaseVariableName  Null-terminated Unicode string that is the base
                               name of the UEFI Variable.

  @return  Pointer to the allocated UEFI Variable name.  Returns NULL if the
           UEFI Variable can not be allocated.
**/
static
CHAR16 *
GenerateFmpVariableName (
  IN  UINT64  HardwareInstance,
  IN  CHAR16  *BaseVariableName
  )
{
  UINTN   Size;
  CHAR16  *VariableName;

  //
  // Allocate Unicode string with room for BaseVariableName and a 16 digit
  // hexadecimal value for the HardwareInstance value.
  //
  Size = StrSize (BaseVariableName) + 16 * sizeof (CHAR16);
  VariableName = AllocateCopyPool (Size, BaseVariableName);
  if (VariableName == NULL) {
    DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to generate variable name %s.\n", mImageIdName, BaseVariableName));
    return VariableName;
  }
  if (HardwareInstance == 0) {
    return VariableName;
  }
  UnicodeValueToStringS (
    &VariableName[StrLen(BaseVariableName)],
    Size,
    PREFIX_ZERO | RADIX_HEX,
    HardwareInstance,
    16
    );
  return VariableName;
}

/**
  Generate the names of the UEFI Variables used to store state information for
  a managed controller.  The UEFI Variables names are a combination of a base
  name and an optional hardware instance value as a 16 character hex value.  If
  the hardware instance value is 0, then the 16 character hex value is not
  included.  These storage for the UEFI Variable names are allocated using the
  UEFI Boot Service AllocatePool() and the pointers are stored in the Private.
  The following are examples of variable names produces for hardware instance
  value 0 and value 0x1234567812345678.

    FmpVersion
    FmpLsv
    LastAttemptStatus
    LastAttemptVersion
    FmpState

    FmpVersion1234567812345678
    FmpLsv1234567812345678
    LastAttemptStatus1234567812345678
    LastAttemptVersion1234567812345678
    FmpState1234567812345678

  @param[in,out] Private  Private context structure for the managed controller.
**/
VOID
GenerateFmpVariableNames (
  IN OUT FIRMWARE_MANAGEMENT_PRIVATE_DATA  *Private
  )
{
  EFI_STATUS                   Status;
  VOID                         *Buffer;
  FMP_CONTROLLER_STATE  FmpControllerState;

  if (Private->VersionVariableName != NULL) {
    FreePool (Private->VersionVariableName);
  }
  if (Private->LsvVariableName != NULL) {
    FreePool (Private->LsvVariableName);
  }
  if (Private->LastAttemptStatusVariableName != NULL) {
    FreePool (Private->LastAttemptStatusVariableName);
  }
  if (Private->LastAttemptVersionVariableName != NULL) {
    FreePool (Private->LastAttemptVersionVariableName);
  }
  if (Private->FmpStateVariableName != NULL) {
    FreePool (Private->FmpStateVariableName);
  }

  Private->VersionVariableName = GenerateFmpVariableName (
                                   Private->Descriptor.HardwareInstance,
                                   VARNAME_VERSION
                                   );
  Private->LsvVariableName = GenerateFmpVariableName (
                               Private->Descriptor.HardwareInstance,
                               VARNAME_LSV
                               );
  Private->LastAttemptStatusVariableName = GenerateFmpVariableName (
                                             Private->Descriptor.HardwareInstance,
                                             VARNAME_LASTATTEMPTSTATUS
                                             );
  Private->LastAttemptVersionVariableName = GenerateFmpVariableName (
                                              Private->Descriptor.HardwareInstance,
                                              VARNAME_LASTATTEMPTVERSION
                                              );
  Private->FmpStateVariableName = GenerateFmpVariableName (
                                    Private->Descriptor.HardwareInstance,
                                    VARNAME_FMPSTATE
                                    );

  DEBUG ((DEBUG_INFO, "FmpDxe(%s): Variable %g %s\n", mImageIdName, &gEfiCallerIdGuid, Private->VersionVariableName));
  DEBUG ((DEBUG_INFO, "FmpDxe(%s): Variable %g %s\n", mImageIdName, &gEfiCallerIdGuid, Private->LsvVariableName));
  DEBUG ((DEBUG_INFO, "FmpDxe(%s): Variable %g %s\n", mImageIdName, &gEfiCallerIdGuid, Private->LastAttemptStatusVariableName));
  DEBUG ((DEBUG_INFO, "FmpDxe(%s): Variable %g %s\n", mImageIdName, &gEfiCallerIdGuid, Private->LastAttemptVersionVariableName));
  DEBUG ((DEBUG_INFO, "FmpDxe(%s): Variable %g %s\n", mImageIdName, &gEfiCallerIdGuid, Private->FmpStateVariableName));

  Buffer = GetFmpControllerState (Private);
  if (Buffer != NULL) {
    //
    // FMP Controller State was found with correct size.
    // Delete old variables if they exist.
    //
    FreePool (Buffer);
    DeleteFmpVariable (Private->VersionVariableName);
    DeleteFmpVariable (Private->LsvVariableName);
    DeleteFmpVariable (Private->LastAttemptStatusVariableName);
    DeleteFmpVariable (Private->LastAttemptVersionVariableName);
    return;
  }

  //
  // FMP Controller State was either not found or is wrong size.
  // Create a new FMP Controller State variable with the correct size.
  //
  DEBUG ((DEBUG_INFO, "FmpDxe(%s): Create controller state\n", mImageIdName));
  GetFmpVariable (
    Private->VersionVariableName,
    &FmpControllerState.VersionValid,
    &FmpControllerState.Version
    );
  GetFmpVariable (
    Private->LsvVariableName,
    &FmpControllerState.LsvValid,
    &FmpControllerState.Lsv
    );
  GetFmpVariable (
    Private->LastAttemptStatusVariableName,
    &FmpControllerState.LastAttemptStatusValid,
    &FmpControllerState.LastAttemptStatus
    );
  GetFmpVariable (
    Private->LastAttemptVersionVariableName,
    &FmpControllerState.LastAttemptVersionValid,
    &FmpControllerState.LastAttemptVersion
    );
  Status = gRT->SetVariable (
                  Private->FmpStateVariableName,
                  &gEfiCallerIdGuid,
                  EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                  sizeof (FmpControllerState),
                  &FmpControllerState
                  );
  if (EFI_ERROR (Status)) {
    //
    // Failed to create FMP Controller State.  In this case, do not
    // delete the individual variables.  They can be used again on next boot
    // to create the FMP Controller State.
    //
    DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to create controller state.  Status = %r\n", mImageIdName, Status));
  } else {
    DeleteFmpVariable (Private->VersionVariableName);
    DeleteFmpVariable (Private->LsvVariableName);
    DeleteFmpVariable (Private->LastAttemptStatusVariableName);
    DeleteFmpVariable (Private->LastAttemptVersionVariableName);
  }
}

/**
  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"FmpState"

  @param[in] Private  Private context structure for the managed controller.

  @return  The version of the firmware image in the firmware device.
**/
UINT32
GetVersionFromVariable (
  IN FIRMWARE_MANAGEMENT_PRIVATE_DATA  *Private
  )
{
  FMP_CONTROLLER_STATE  *FmpControllerState;
  UINT32                Value;

  Value = DEFAULT_VERSION;
  FmpControllerState = GetFmpControllerState (Private);
  if (FmpControllerState != NULL) {
    if (FmpControllerState->VersionValid) {
      Value = FmpControllerState->Version;
      DEBUG ((DEBUG_INFO, "FmpDxe(%s): Get variable %g %s Version %08x\n",
        mImageIdName,
        &gEfiCallerIdGuid,
        Private->FmpStateVariableName,
        Value
        ));
    }
    FreePool (FmpControllerState);
  }
  return Value;
}

/**
  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"FmpState"

  @param[in] Private  Private context structure for the managed controller.

  @return  The lowest supported version of the firmware image in the firmware
           device.
**/
UINT32
GetLowestSupportedVersionFromVariable (
  IN FIRMWARE_MANAGEMENT_PRIVATE_DATA  *Private
  )
{
  FMP_CONTROLLER_STATE  *FmpControllerState;
  UINT32                Value;

  Value = DEFAULT_LOWESTSUPPORTEDVERSION;
  FmpControllerState = GetFmpControllerState (Private);
  if (FmpControllerState != NULL) {
    if (FmpControllerState->LsvValid) {
      Value = FmpControllerState->Lsv;
      DEBUG ((DEBUG_INFO, "FmpDxe(%s): Get variable %g %s LowestSupportedVersion %08x\n",
        mImageIdName,
        &gEfiCallerIdGuid,
        Private->FmpStateVariableName,
        Value
        ));
    }
    FreePool (FmpControllerState);
  }
  return Value;
}

/**
  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"FmpState"

  @param[in] Private  Private context structure for the managed controller.

  @return  The last attempt status value for the most recent capsule update.
**/
UINT32
GetLastAttemptStatusFromVariable (
  IN FIRMWARE_MANAGEMENT_PRIVATE_DATA  *Private
  )
{
  FMP_CONTROLLER_STATE  *FmpControllerState;
  UINT32                Value;

  Value = DEFAULT_LASTATTEMPTSTATUS;
  FmpControllerState = GetFmpControllerState (Private);
  if (FmpControllerState != NULL) {
    if (FmpControllerState->LastAttemptStatusValid) {
      Value = FmpControllerState->LastAttemptStatus;
      DEBUG ((DEBUG_INFO, "FmpDxe(%s): Get variable %g %s LastAttemptStatus %08x\n",
        mImageIdName,
        &gEfiCallerIdGuid,
        Private->FmpStateVariableName,
        Value
        ));
    }
    FreePool (FmpControllerState);
  }
  return Value;
}

/**
  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"FmpState"

  @param[in] Private  Private context structure for the managed controller.

  @return  The last attempt version value for the most recent capsule update.
**/
UINT32
GetLastAttemptVersionFromVariable (
  IN FIRMWARE_MANAGEMENT_PRIVATE_DATA  *Private
  )
{
  FMP_CONTROLLER_STATE  *FmpControllerState;
  UINT32                Value;

  Value = DEFAULT_LASTATTEMPTVERSION;
  FmpControllerState = GetFmpControllerState (Private);
  if (FmpControllerState != NULL) {
    if (FmpControllerState->LastAttemptVersionValid) {
      Value = FmpControllerState->LastAttemptVersion;
      DEBUG ((DEBUG_INFO, "FmpDxe(%s): Get variable %g %s LastAttemptVersion %08x\n",
        mImageIdName,
        &gEfiCallerIdGuid,
        Private->FmpStateVariableName,
        Value
        ));
    }
    FreePool (FmpControllerState);
  }
  return Value;
}

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

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

  @param[in] Private  Private context structure for the managed controller.
  @param[in] Version  The version of the firmware image in the firmware device.
**/
VOID
SetVersionInVariable (
  IN FIRMWARE_MANAGEMENT_PRIVATE_DATA  *Private,
  IN UINT32                            Version
  )
{
  EFI_STATUS            Status;
  FMP_CONTROLLER_STATE  *FmpControllerState;
  BOOLEAN               Update;

  FmpControllerState = GetFmpControllerState (Private);
  if (FmpControllerState == NULL) {
    //
    // Can not update value if FMP Controller State does not exist.
    // This variable is guaranteed to be created by GenerateFmpVariableNames().
    //
    return;
  }

  Update = FALSE;
  if (!FmpControllerState->VersionValid) {
    Update = TRUE;
  }
  if (FmpControllerState->Version != Version) {
    Update = TRUE;
  }
  if (!Update) {
    DEBUG ((DEBUG_INFO, "FmpDxe(%s): No need to update controller state.  Same value as before.\n", mImageIdName));
  } else {
    FmpControllerState->VersionValid = TRUE;
    FmpControllerState->Version      = Version;
    Status = gRT->SetVariable (
                    Private->FmpStateVariableName,
                    &gEfiCallerIdGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (*FmpControllerState),
                    FmpControllerState
                    );
    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to update controller state.  Status = %r\n", mImageIdName, Status));
    } else {
      DEBUG ((DEBUG_INFO, "FmpDxe(%s): Set variable %g %s Version %08x\n",
        mImageIdName,
        &gEfiCallerIdGuid,
        Private->FmpStateVariableName,
        Version
        ));
    }
  }
  FreePool (FmpControllerState);
}

/**
  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"FmpState"

  @param[in] Private                 Private context structure for the managed
                                     controller.
  @param[in] LowestSupportedVersion  The lowest supported version of the
                                     firmware image in the firmware device.
**/
VOID
SetLowestSupportedVersionInVariable (
  IN FIRMWARE_MANAGEMENT_PRIVATE_DATA  *Private,
  IN UINT32                            LowestSupportedVersion
  )
{
  EFI_STATUS            Status;
  FMP_CONTROLLER_STATE  *FmpControllerState;
  BOOLEAN               Update;

  FmpControllerState = GetFmpControllerState (Private);
  if (FmpControllerState == NULL) {
    //
    // Can not update value if FMP Controller State does not exist.
    // This variable is guaranteed to be created by GenerateFmpVariableNames().
    //
    return;
  }

  Update = FALSE;
  if (!FmpControllerState->LsvValid) {
    Update = TRUE;
  }
  if (FmpControllerState->Lsv < LowestSupportedVersion) {
    Update = TRUE;
  }
  if (!Update) {
    DEBUG ((DEBUG_INFO, "FmpDxe(%s): No need to update controller state.  Same value as before.\n", mImageIdName));
  } else {
    FmpControllerState->LsvValid = TRUE;
    FmpControllerState->Lsv      = LowestSupportedVersion;
    Status = gRT->SetVariable (
                    Private->FmpStateVariableName,
                    &gEfiCallerIdGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (*FmpControllerState),
                    FmpControllerState
                    );
    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to update controller state.  Status = %r\n", mImageIdName, Status));
    } else {
      DEBUG ((DEBUG_INFO, "FmpDxe(%s): Set variable %g %s LowestSupportedVersion %08x\n",
        mImageIdName,
        &gEfiCallerIdGuid,
        Private->FmpStateVariableName,
        LowestSupportedVersion
        ));
    }
  }
  FreePool (FmpControllerState);
}

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

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

  @param[in] Private            Private context structure for the managed
                                controller.
  @param[in] LastAttemptStatus  The last attempt status of the most recent FMP
                                capsule update.
**/
VOID
SetLastAttemptStatusInVariable (
  IN FIRMWARE_MANAGEMENT_PRIVATE_DATA  *Private,
  IN UINT32                            LastAttemptStatus
  )
{
  EFI_STATUS            Status;
  FMP_CONTROLLER_STATE  *FmpControllerState;
  BOOLEAN               Update;

  FmpControllerState = GetFmpControllerState (Private);
  if (FmpControllerState == NULL) {
    //
    // Can not update value if FMP Controller State does not exist.
    // This variable is guaranteed to be created by GenerateFmpVariableNames().
    //
    return;
  }

  Update = FALSE;
  if (!FmpControllerState->LastAttemptStatusValid) {
    Update = TRUE;
  }
  if (FmpControllerState->LastAttemptStatus != LastAttemptStatus) {
    Update = TRUE;
  }
  if (!Update) {
    DEBUG ((DEBUG_INFO, "FmpDxe(%s): No need to update controller state.  Same value as before.\n", mImageIdName));
  } else {
    FmpControllerState->LastAttemptStatusValid = TRUE;
    FmpControllerState->LastAttemptStatus      = LastAttemptStatus;
    Status = gRT->SetVariable (
                    Private->FmpStateVariableName,
                    &gEfiCallerIdGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (*FmpControllerState),
                    FmpControllerState
                    );
    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to update controller state.  Status = %r\n", mImageIdName, Status));
    } else {
      DEBUG ((DEBUG_INFO, "FmpDxe(%s): Set variable %g %s LastAttemptStatus %08x\n",
        mImageIdName,
        &gEfiCallerIdGuid,
        Private->FmpStateVariableName,
        LastAttemptStatus
        ));
    }
  }
  FreePool (FmpControllerState);
}

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

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

  @param[in] Private             Private context structure for the managed
                                 controller.
  @param[in] LastAttemptVersion  The last attempt version value of the most
                                 recent FMP capsule update.
**/
VOID
SetLastAttemptVersionInVariable (
  IN FIRMWARE_MANAGEMENT_PRIVATE_DATA  *Private,
  IN UINT32                            LastAttemptVersion
  )
{
  EFI_STATUS            Status;
  FMP_CONTROLLER_STATE  *FmpControllerState;
  BOOLEAN               Update;

  FmpControllerState = GetFmpControllerState (Private);
  if (FmpControllerState == NULL) {
    //
    // Can not update value if FMP Controller State does not exist.
    // This variable is guaranteed to be created by GenerateFmpVariableNames().
    //
    return;
  }

  Update = FALSE;
  if (!FmpControllerState->LastAttemptVersionValid) {
    Update = TRUE;
  }
  if (FmpControllerState->LastAttemptVersion != LastAttemptVersion) {
    Update = TRUE;
  }
  if (!Update) {
    DEBUG ((DEBUG_INFO, "FmpDxe(%s): No need to update controller state.  Same value as before.\n", mImageIdName));
  } else {
    FmpControllerState->LastAttemptVersionValid = TRUE;
    FmpControllerState->LastAttemptVersion      = LastAttemptVersion;
    Status = gRT->SetVariable (
                    Private->FmpStateVariableName,
                    &gEfiCallerIdGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (*FmpControllerState),
                    FmpControllerState
                    );
    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to update controller state.  Status = %r\n", mImageIdName, Status));
    } else {
      DEBUG ((DEBUG_INFO, "FmpDxe(%s): Set variable %g %s LastAttemptVersion %08x\n",
        mImageIdName,
        &gEfiCallerIdGuid,
        Private->FmpStateVariableName,
        LastAttemptVersion
        ));
    }
  }
  FreePool (FmpControllerState);
}

/**
  Attempts to lock a single UEFI Variable propagating the error state of the
  first lock attempt that fails.  Uses gEfiCallerIdGuid as the variable GUID.

  @param[in] PreviousStatus  The previous UEFI Variable lock attempt status.
  @param[in] VariableLock    The EDK II Variable Lock Protocol instance.
  @param[in] VariableName    The name of the UEFI Variable to lock.

  @retval  EFI_SUCCESS  The UEFI Variable was locked and the previous variable
                        lock attempt also succeeded.
  @retval  Other        The UEFI Variable could not be locked or the previous
                        variable lock attempt failed.
**/
static
EFI_STATUS
LockFmpVariable (
  IN EFI_STATUS                    PreviousStatus,
  IN EDKII_VARIABLE_LOCK_PROTOCOL  *VariableLock,
  IN CHAR16                        *VariableName
  )
{
  EFI_STATUS  Status;

  Status = VariableLock->RequestToLock (
                           VariableLock,
                           VariableName,
                           &gEfiCallerIdGuid
                           );
  if (!EFI_ERROR (Status)) {
    return PreviousStatus;
  }

  DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to lock variable %g %s.  Status = %r\n",
    mImageIdName,
    &gEfiCallerIdGuid,
    VariableName,
    Status
    ));

  if (EFI_ERROR (PreviousStatus)) {
    return PreviousStatus;
  }
  return Status;
}

/**
  Locks all the UEFI Variables that use gEfiCallerIdGuid of the currently
  executing module.

  @param[in] Private  Private context structure for the managed controller.

  @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 (
  FIRMWARE_MANAGEMENT_PRIVATE_DATA  *Private
  )
{
  EFI_STATUS                    Status;
  EDKII_VARIABLE_LOCK_PROTOCOL  *VariableLock;

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

  Status = EFI_SUCCESS;
  Status = LockFmpVariable (Status, VariableLock, Private->VersionVariableName);
  Status = LockFmpVariable (Status, VariableLock, Private->LsvVariableName);
  Status = LockFmpVariable (Status, VariableLock, Private->LastAttemptStatusVariableName);
  Status = LockFmpVariable (Status, VariableLock, Private->LastAttemptVersionVariableName);
  Status = LockFmpVariable (Status, VariableLock, Private->FmpStateVariableName);

  return Status;
}