summaryrefslogtreecommitdiff
path: root/HisiPkg/HiKeyPkg/Drivers/HiKeyDxe/InstallBootMenu.c
blob: 84fdea184b8e43dad7e284771e7c16827300cf33 (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
/** @file
*
*  Copyright (c) 2015, Linaro Ltd. All rights reserved.
*  Copyright (c) 2015, Hisilicon Ltd. All rights reserved.
*
*  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/BaseMemoryLib.h>
#include <Library/BdsLib.h>
#include <Library/DevicePathLib.h>
#include <Library/IoLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/PrintLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>

#include <Protocol/DevicePathFromText.h>
#include <Protocol/DevicePathToText.h>
#include <Protocol/EmbeddedGpio.h>

#include <Guid/ArmGlobalVariableHob.h>
#include <Guid/EventGroup.h>
#include <Guid/GlobalVariable.h>
#include <Guid/VariableFormat.h>

#include "HiKeyDxeInternal.h"

#define MAX_BOOT_ENTRIES         16
// Jumper on pin5-6 of J15 determines whether boot to fastboot
#define DETECT_J15_FASTBOOT      24    // GPIO 3_0

#define USER_LED1                32    // GPIO 4_0
#define USER_LED2                33    // GPIO 4_1
#define USER_LED3                34    // GPIO 4_2
#define USER_LED4                35    // GPIO 4_3

struct HiKeyBootEntry {
  CHAR16    *Path;
  CHAR16    *Args;
  CHAR16    *Description;
  UINT16     LoadType;
};

STATIC CONST BOOLEAN mIsEndOfDxeEvent = TRUE;
STATIC UINT16 *mBootOrder = NULL;
STATIC UINT16 mBootCount = 0;
STATIC UINT16 mBootIndex = 0;

#define HIKEY_BOOT_ENTRY_FASTBOOT          0
#define HIKEY_BOOT_ENTRY_BOOT_EMMC         1    /* boot from eMMC */
#define HIKEY_BOOT_ENTRY_BOOT_SD           2    /* boot from SD card */

STATIC struct HiKeyBootEntry Entries[] = {
  [HIKEY_BOOT_ENTRY_FASTBOOT] = {
    L"VenHw(B549F005-4BD4-4020-A0CB-06F42BDA68C3)/HD(6,GPT,5C0F213C-17E1-4149-88C8-8B50FB4EC70E,0x7000,0x20000)/\\EFI\\BOOT\\FASTBOOT.EFI",
    NULL,
    L"fastboot",
    LOAD_OPTION_CATEGORY_APP
  },
  [HIKEY_BOOT_ENTRY_BOOT_EMMC] = {
    L"VenHw(B549F005-4BD4-4020-A0CB-06F42BDA68C3)/HD(6,GPT,5C0F213C-17E1-4149-88C8-8B50FB4EC70E,0x7000,0x20000)/\\EFI\\BOOT\\GRUBAA64.EFI",
    NULL,
    L"boot from eMMC",
    LOAD_OPTION_CATEGORY_APP
  },
  [HIKEY_BOOT_ENTRY_BOOT_SD] = {
    L"VenHw(594BFE73-5E18-4F12-8119-19DB8C5FC849)/HD(1,MBR,0x00000000,0x3F,0x21FC0)/Image",
    L"dtb=hi6220-hikey.dtb console=ttyAMA3,115200 earlycon=pl011,0xf7113000 root=/dev/mmcblk1p2 rw rootwait initrd=initrd.img efi=noruntime",
    L"boot from SD card",
    LOAD_OPTION_CATEGORY_BOOT
  }
};

STATIC
BOOLEAN
EFIAPI
HiKeyVerifyBootEntry (
  IN CHAR16          *BootVariableName,
  IN CHAR16          *BootDevicePathText,
  IN CHAR16          *BootArgs,
  IN CHAR16          *BootDescription,
  IN UINT16           LoadOptionAttr
  )
{
  EFI_DEVICE_PATH_TO_TEXT_PROTOCOL   *DevicePathToTextProtocol;
  CHAR16                             *DevicePathText;
  UINTN                               EfiLoadOptionSize;
  EFI_LOAD_OPTION                     EfiLoadOption;
  BDS_LOAD_OPTION                    *LoadOption;
  EFI_STATUS                          Status;
  UINTN                               DescriptionLength;

  Status = GetGlobalEnvironmentVariable (BootVariableName, NULL, &EfiLoadOptionSize, (VOID**)&EfiLoadOption);
  if (EFI_ERROR (Status)) {
    return FALSE;
  }
  if (EfiLoadOption == NULL) {
    return FALSE;
  }
  if (EfiLoadOptionSize < sizeof(UINT32) + sizeof(UINT16) + sizeof(CHAR16) + sizeof(EFI_DEVICE_PATH_PROTOCOL)) {
    return FALSE;
  }
  LoadOption = (BDS_LOAD_OPTION*)AllocateZeroPool (sizeof(BDS_LOAD_OPTION));
  if (LoadOption == NULL) {
    return FALSE;
  }

  LoadOption->LoadOption     = EfiLoadOption;
  LoadOption->Attributes         = *(UINT32*)EfiLoadOption;
  LoadOption->FilePathListLength = *(UINT16*)(EfiLoadOption + sizeof(UINT32));
  LoadOption->Description        = (CHAR16*)(EfiLoadOption + sizeof(UINT32) + sizeof(UINT16));
  DescriptionLength              = StrSize (LoadOption->Description);
  LoadOption->FilePathList       = (EFI_DEVICE_PATH_PROTOCOL*)(EfiLoadOption + sizeof(UINT32) + sizeof(UINT16) + DescriptionLength);
  if ((UINTN)((UINTN)LoadOption->FilePathList + LoadOption->FilePathListLength - (UINTN)EfiLoadOption) == EfiLoadOptionSize) {
    LoadOption->OptionalData     = NULL;
    LoadOption->OptionalDataSize = 0;
  } else {
    LoadOption->OptionalData     = (VOID*)((UINTN)(LoadOption->FilePathList) + LoadOption->FilePathListLength);
    LoadOption->OptionalDataSize = EfiLoadOptionSize - ((UINTN)LoadOption->OptionalData - (UINTN)EfiLoadOption);
  }

  if (((BootArgs == NULL) && (LoadOption->OptionalDataSize)) ||
      (BootArgs && (LoadOption->OptionalDataSize == 0))) {
    return FALSE;
  } else if (BootArgs && LoadOption->OptionalDataSize) {
    if (StrCmp (BootArgs, LoadOption->OptionalData) != 0)
      return FALSE;
  }
  if ((LoadOption->Description == NULL) || (BootDescription == NULL)) {
    return FALSE;
  }
  if (StrCmp (BootDescription, LoadOption->Description) != 0) {
    return FALSE;
  }
  if ((LoadOption->Attributes & LOAD_OPTION_CATEGORY) != (LoadOptionAttr & LOAD_OPTION_CATEGORY)) {
    return FALSE;
  }

  Status = gBS->LocateProtocol (&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);
  ASSERT_EFI_ERROR(Status);
  DevicePathText = DevicePathToTextProtocol->ConvertDevicePathToText(LoadOption->FilePathList, TRUE, TRUE);
  if (StrCmp (DevicePathText, BootDevicePathText) != 0) {
    return FALSE;
  }

  FreePool (LoadOption);
  return TRUE;
}

STATIC
EFI_STATUS
EFIAPI
HiKeyCreateBootEntry (
  IN CHAR16          *DevicePathText,
  IN CHAR16          *BootArgs,
  IN CHAR16          *BootDescription,
  IN UINT16           LoadOption
  )
{
  BDS_LOAD_OPTION                    *BdsLoadOption;
  EFI_STATUS                          Status;
  UINTN                               DescriptionSize;
  UINTN                               BootOrderSize;
  CHAR16                              BootVariableName[9];
  UINT8                              *EfiLoadOptionPtr;
  EFI_DEVICE_PATH_PROTOCOL           *DevicePathNode;
  UINTN                               NodeLength;
  EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *DevicePathFromTextProtocol;

  if ((DevicePathText == NULL) || (BootDescription == NULL)) {
    DEBUG ((EFI_D_ERROR, "%a: Invalid Parameters\n", __func__));
    return EFI_INVALID_PARAMETER;
  }

  UnicodeSPrint (BootVariableName, 9 * sizeof(CHAR16), L"Boot%04X", mBootCount);
  if (HiKeyVerifyBootEntry (BootVariableName, DevicePathText, BootArgs, BootDescription, LoadOption) == TRUE) {
    // The boot entry is already created.
    Status = EFI_SUCCESS;
    goto done;
  }

  BdsLoadOption = (BDS_LOAD_OPTION*)AllocateZeroPool (sizeof(BDS_LOAD_OPTION));
  ASSERT (BdsLoadOption != NULL);

  Status = gBS->LocateProtocol (
                  &gEfiDevicePathFromTextProtocolGuid,
                  NULL,
                  (VOID**)&DevicePathFromTextProtocol
                  );
  ASSERT_EFI_ERROR(Status);

  BdsLoadOption->FilePathList = DevicePathFromTextProtocol->ConvertTextToDevicePath (DevicePathText);
  ASSERT (BdsLoadOption->FilePathList != NULL);
  BdsLoadOption->FilePathListLength = GetDevicePathSize (BdsLoadOption->FilePathList);
  BdsLoadOption->Attributes = LOAD_OPTION_ACTIVE | (LoadOption & LOAD_OPTION_CATEGORY);

  if (BootArgs) {
    BdsLoadOption->OptionalDataSize = StrSize (BootArgs);
    BdsLoadOption->OptionalData = (CHAR16*)AllocateZeroPool (BdsLoadOption->OptionalDataSize);
    ASSERT (BdsLoadOption->OptionalData != NULL);
    StrCpy (BdsLoadOption->OptionalData, BootArgs);
  }

  BdsLoadOption->LoadOptionIndex = mBootCount;
  DescriptionSize = StrSize (BootDescription);
  BdsLoadOption->Description = (VOID*)AllocateZeroPool (DescriptionSize);
  StrCpy (BdsLoadOption->Description, BootDescription);

  BdsLoadOption->LoadOptionSize = sizeof(UINT32) + sizeof(UINT16) + DescriptionSize + BdsLoadOption->FilePathListLength + BdsLoadOption->OptionalDataSize;
  BdsLoadOption->LoadOption = (EFI_LOAD_OPTION)AllocateZeroPool (BdsLoadOption->LoadOptionSize);
  ASSERT (BdsLoadOption->LoadOption != NULL);

  EfiLoadOptionPtr = BdsLoadOption->LoadOption;

  //
  // Populate the EFI Load Option and BDS Boot Option structures
  //

  // Attributes fields
  *(UINT32*)EfiLoadOptionPtr = BdsLoadOption->Attributes;
  EfiLoadOptionPtr += sizeof(UINT32);

  // FilePath List fields
  *(UINT16*)EfiLoadOptionPtr = BdsLoadOption->FilePathListLength;
  EfiLoadOptionPtr += sizeof(UINT16);

  // Boot description fields
  CopyMem (EfiLoadOptionPtr, BdsLoadOption->Description, DescriptionSize);
  EfiLoadOptionPtr += DescriptionSize;

  // File path fields
  DevicePathNode = BdsLoadOption->FilePathList;
  while (!IsDevicePathEndType (DevicePathNode)) {
    NodeLength = DevicePathNodeLength(DevicePathNode);
    CopyMem (EfiLoadOptionPtr, DevicePathNode, NodeLength);
    EfiLoadOptionPtr += NodeLength;
    DevicePathNode = NextDevicePathNode (DevicePathNode);
  }

  // Set the End Device Path Type
  SetDevicePathEndNode (EfiLoadOptionPtr);
  EfiLoadOptionPtr += sizeof(EFI_DEVICE_PATH);

  // Fill the Optional Data
  if (BdsLoadOption->OptionalDataSize > 0) {
    CopyMem (EfiLoadOptionPtr, BdsLoadOption->OptionalData, BdsLoadOption->OptionalDataSize);
  }

  Status = gRT->SetVariable (
                  BootVariableName,
                  &gEfiGlobalVariableGuid,
                  EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
                  BdsLoadOption->LoadOptionSize,
                  BdsLoadOption->LoadOption
                  );
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_ERROR, "%a: failed to set BootVariable\n", __func__));
    return Status;
  }

done:
  BootOrderSize = mBootCount * sizeof (UINT16);
  mBootOrder = ReallocatePool (BootOrderSize, BootOrderSize + sizeof (UINT16), mBootOrder);
  mBootOrder[mBootCount] = mBootCount;
  mBootCount++;
  return Status;
}

STATIC
EFI_STATUS
EFIAPI
HiKeyCreateBootOrder (
  IN    VOID
  )
{
  UINT16             *BootOrder;
  UINTN               BootOrderSize;
  UINTN               Index;
  EFI_STATUS          Status;

  Status = GetGlobalEnvironmentVariable (L"BootOrder", NULL, &BootOrderSize, (VOID**)&BootOrder);
  if (EFI_ERROR(Status) == 0) {
    if (BootOrderSize == mBootCount) {
      for (Index = 0; Index < mBootCount; Index++) {
        if (BootOrder[Index] != mBootOrder[Index]) {
          break;
        }
      }
      if (Index == mBootCount) {
        // Found BootOrder variable with expected value.
        return EFI_SUCCESS;
      }
    }
  }

  Status = gRT->SetVariable (
                  (CHAR16*)L"BootOrder",
                  &gEfiGlobalVariableGuid,
                  EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
                  mBootCount * sizeof(UINT16),
                  mBootOrder
                  );
  return Status;
}

STATIC
EFI_STATUS
EFIAPI
HiKeyCreateBootNext (
  IN     VOID
  )
{
  EFI_STATUS          Status;
  UINT16             *BootNext;
  UINTN               BootNextSize;

  BootNextSize = sizeof(UINT16);
  Status = GetGlobalEnvironmentVariable (L"BootNext", NULL, &BootNextSize, (VOID**)&BootNext);
  if (EFI_ERROR(Status) == 0) {
    if (BootNextSize == sizeof (UINT16)) {
      if (*BootNext == mBootOrder[mBootIndex]) {
        // Found the BootNext variable with expected value.
        return EFI_SUCCESS;
      }
    }
  }
  BootNext = &mBootOrder[mBootIndex];
  Status = gRT->SetVariable (
                  (CHAR16*)L"BootNext",
                  &gEfiGlobalVariableGuid,
                  EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
                  sizeof (UINT16),
                  BootNext
                  );
  return Status;
}

STATIC
VOID
EFIAPI
HiKeyTestLed (
  IN     EMBEDDED_GPIO   *Gpio
  )
{
  EFI_STATUS             Status;

  Status = Gpio->Set (Gpio, USER_LED1, GPIO_MODE_OUTPUT_0);
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_ERROR, "%a: failed to set LED1\n", __func__));
    return;
  }
  Status = Gpio->Set (Gpio, USER_LED2, GPIO_MODE_OUTPUT_1);
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_ERROR, "%a: failed to set LED2\n", __func__));
    return;
  }
  Status = Gpio->Set (Gpio, USER_LED3, GPIO_MODE_OUTPUT_0);
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_ERROR, "%a: failed to set LED3\n", __func__));
    return;
  }
  Status = Gpio->Set (Gpio, USER_LED4, GPIO_MODE_OUTPUT_1);
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_ERROR, "%a: failed to set LED4\n", __func__));
    return;
  }
}

STATIC
VOID
EFIAPI
HiKeyDetectJumper (
  IN     VOID
  )
{
  EMBEDDED_GPIO         *Gpio;
  EFI_STATUS             Status;
  UINTN                  Value;

  Status = gBS->LocateProtocol (&gEmbeddedGpioProtocolGuid, NULL, (VOID **)&Gpio);
  ASSERT_EFI_ERROR (Status);

  Status = Gpio->Set (Gpio, DETECT_J15_FASTBOOT, GPIO_MODE_INPUT);
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_ERROR, "%a: failed to set jumper as gpio input\n", __func__));
    return;
  }
  Status = Gpio->Get (Gpio, DETECT_J15_FASTBOOT, &Value);
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_ERROR, "%a: failed to get value from jumper\n", __func__));
    return;
  }
  if (Value == 1) {
    // Jump not connected on pin5-6 of J15
    mBootIndex = 1;
  } else {
    mBootIndex = 0;
  }

  HiKeyTestLed (Gpio);
}

STATIC
BOOLEAN
EFIAPI
HiKeySDCardIsPresent (
  IN      VOID
  )
{
  UINT32    Value;

  /*
   * FIXME
   * At first, reading GPIO pin shouldn't exist in SD driver. We need to
   * add some callbacks to handle settings for hardware platform.
   * In the second, reading GPIO pin should be based on GPIO driver. Now
   * GPIO driver could only be used for one PL061 gpio controller. And it's
   * used to detect jumper setting. As a workaround, we have to read the gpio
   * register instead at here.
   *
   */
  Value = MmioRead32 (0xf8012000 + (1 << 2));
  if (Value)
    return FALSE;
  return TRUE;
}

STATIC
VOID
EFIAPI
HiKeyCreateFdtVariable (
  IN CHAR16          *FdtPathText
  )
{
  UINTN                     FdtDevicePathSize;
  EFI_DEVICE_PATH_PROTOCOL *FdtDevicePath;
  EFI_STATUS                Status;
  EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *DevicePathFromTextProtocol;

  Status = gBS->LocateProtocol (
                  &gEfiDevicePathFromTextProtocolGuid,
                  NULL,
                  (VOID**)&DevicePathFromTextProtocol
                  );
  ASSERT_EFI_ERROR(Status);

  FdtDevicePath = DevicePathFromTextProtocol->ConvertTextToDevicePath (FdtPathText);
  ASSERT (FdtDevicePath != NULL);

  FdtDevicePathSize = GetDevicePathSize (FdtDevicePath);
  Status = gRT->SetVariable (
                  (CHAR16*)L"Fdt",
                  &gArmGlobalVariableGuid,
                  EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                  FdtDevicePathSize,
                  FdtDevicePath
                  );
  ASSERT_EFI_ERROR(Status);
}

STATIC
VOID
EFIAPI
HiKeyOnEndOfDxe (
  EFI_EVENT                               Event,
  VOID                                    *Context
  )
{
  EFI_STATUS          Status;
  UINTN               VariableSize;
  UINT16              AutoBoot, Count, Index;
  CHAR16              BootDevice[BOOT_DEVICE_LENGTH];

  VariableSize = sizeof (UINT16);
  Status = gRT->GetVariable (
                  (CHAR16 *)L"HiKeyAutoBoot",
                  &gArmGlobalVariableGuid,
                  NULL,
                  &VariableSize,
                  (VOID*)&AutoBoot
                  );
  if (Status == EFI_NOT_FOUND) {
    AutoBoot = 1;
    Status = gRT->SetVariable (
                    (CHAR16*)L"HiKeyAutoBoot",
                    &gArmGlobalVariableGuid,
                    EFI_VARIABLE_NON_VOLATILE       |
                    EFI_VARIABLE_BOOTSERVICE_ACCESS |
                    EFI_VARIABLE_RUNTIME_ACCESS,
                    sizeof (UINT16),
                    &AutoBoot
                    );
    ASSERT_EFI_ERROR (Status);
  } else if (EFI_ERROR (Status) == 0) {
    if (AutoBoot == 0) {
      // Select boot entry by manual.
      // Delete the BootNext environment variable
      gRT->SetVariable (L"BootNext",
             &gEfiGlobalVariableGuid,
             EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
             0,
             NULL);
      return;
    }
  }

  Count = sizeof (Entries) / sizeof (struct HiKeyBootEntry);

  mBootCount = 0;
  mBootOrder = NULL;

  for (Index = 0; Index < Count; Index++) {
    Status = HiKeyCreateBootEntry (
               Entries[Index].Path,
               Entries[Index].Args,
               Entries[Index].Description,
               Entries[Index].LoadType
               );
    ASSERT_EFI_ERROR (Status);
  }

  if ((mBootCount == 0) || (mBootCount >= MAX_BOOT_ENTRIES)) {
    DEBUG ((EFI_D_ERROR, "%a: can't create boot entries\n", __func__));
    return;
  }

  Status = HiKeyCreateBootOrder ();
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_ERROR, "%a: failed to set BootOrder variable\n", __func__));
    return;
  }

  HiKeyDetectJumper ();

  // Check boot device.
  // If boot device is eMMC, it's always higher priority.
  // If boot device is SD card, SD card is higher priority.
  //    If SD card is present, boot SD. Otherwise, still boot eMMC.
  VariableSize = BOOT_DEVICE_LENGTH * sizeof (UINT16);
  Status = gRT->GetVariable (
                  (CHAR16 *)L"HiKeyBootDevice",
                  &gArmGlobalVariableGuid,
                  NULL,
                  &VariableSize,
                  &BootDevice
                  );
  if (EFI_ERROR (Status) == 0) {
    if (StrnCmp (BootDevice, L"emmc", StrLen (L"emmc")) == 0) {
      if (mBootIndex > 0) {
        mBootIndex = HIKEY_BOOT_ENTRY_BOOT_EMMC;
      }
    } else if (StrnCmp (BootDevice, L"sd", StrLen (L"sd")) == 0) {
      if (mBootIndex > 0) {
         // If SD card is present, boot from SD card directly.
         if (HiKeySDCardIsPresent () == TRUE) {
           mBootIndex = HIKEY_BOOT_ENTRY_BOOT_SD;
         } else {
           mBootIndex = HIKEY_BOOT_ENTRY_BOOT_EMMC;
         }
      }
    } else {
      DEBUG ((EFI_D_ERROR, "%a: invalid boot device (%a) is specified\n", __func__, BootDevice));
      mBootIndex = HIKEY_BOOT_ENTRY_BOOT_EMMC;
    }
  } else {
    DEBUG ((EFI_D_ERROR, "failed to get HiKeyBootDevice variable, %r\n", Status));
  }

  // Fdt variable should be aligned with Image path.
  // In another word, Fdt and Image file should be located in the same path.
  // Since grub is used for eMMC boot, don't need to assign Fdt and Image path.
  switch (mBootIndex) {
  case HIKEY_BOOT_ENTRY_BOOT_SD:
    HiKeyCreateFdtVariable (L"VenHw(594BFE73-5E18-4F12-8119-19DB8C5FC849)/HD(1,MBR,0x00000000,0x3F,0x21FC0)/hi6220-hikey.dtb");
    break;
  }


  Status = HiKeyCreateBootNext ();
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_ERROR, "%a: failed to set BootNext variable\n", __func__));
    return;
  }
}

EFI_STATUS
HiKeyBootMenuInstall (
  IN VOID
  )
{
  EFI_STATUS          Status;
  EFI_EVENT           EndOfDxeEvent;

  Status = gBS->CreateEventEx (
                  EVT_NOTIFY_SIGNAL,
                  TPL_CALLBACK,
                  HiKeyOnEndOfDxe,
                  &mIsEndOfDxeEvent,
                  &gEfiEndOfDxeEventGroupGuid,
                  &EndOfDxeEvent
                  );
  ASSERT_EFI_ERROR (Status);
  return Status;
}