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
|
/** @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/CacheMaintenanceLib.h>
#include <Library/DevicePathLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Guid/ArmGlobalVariableHob.h>
#include <Protocol/BlockIo.h>
#include "HiKeyDxeInternal.h"
#define SERIAL_NUMBER_LENGTH 16
#define SERIAL_NUMBER_LBA 1024
#define SERIAL_NUMBER_BLOCK_SIZE 512
#define RANDOM_MAGIC 0x9a4dbeaf
struct RandomSerialNo {
UINTN Magic;
UINTN Data;
CHAR8 SerialNo[32];
};
STATIC
UINTN
EFIAPI
HiKeyInitSerialNo (
IN VOID
)
{
EFI_STATUS Status;
UINTN VariableSize;
EFI_DEVICE_PATH_PROTOCOL *BlockDevicePath;
EFI_BLOCK_IO_PROTOCOL *BlockIoProtocol;
EFI_HANDLE Handle;
VOID *DataPtr;
struct RandomSerialNo *Random;
CHAR16 DefaultSerialNo[] = L"0123456789abcdef";
CHAR16 SerialNoUnicode[32], DataUnicode[32];
BlockDevicePath = ConvertTextToDevicePath ((CHAR16*)FixedPcdGetPtr (PcdAndroidFastbootNvmDevicePath));
Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &BlockDevicePath, &Handle);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "Warning: Couldn't locate block device (status: %r)\n", Status));
return EFI_INVALID_PARAMETER;
}
Status = gBS->OpenProtocol (
Handle,
&gEfiBlockIoProtocolGuid,
(VOID **) &BlockIoProtocol,
gImageHandle,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "Warning: Couldn't open block device (status: %r)\n", Status));
return EFI_DEVICE_ERROR;
}
DataPtr = AllocateZeroPool (SERIAL_NUMBER_BLOCK_SIZE);
WriteBackDataCacheRange (DataPtr, SERIAL_NUMBER_BLOCK_SIZE);
Status = BlockIoProtocol->ReadBlocks (BlockIoProtocol, BlockIoProtocol->Media->MediaId,
SERIAL_NUMBER_LBA, SERIAL_NUMBER_BLOCK_SIZE,
DataPtr);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "Warning: failed on reading blocks.\n"));
goto exit;
}
InvalidateDataCacheRange (DataPtr, SERIAL_NUMBER_BLOCK_SIZE);
Random = (struct RandomSerialNo *)DataPtr;
if (Random->Magic != RANDOM_MAGIC) {
VariableSize = SERIAL_NUMBER_LENGTH * sizeof (CHAR16);
Status = gRT->GetVariable (
(CHAR16 *)L"SerialNo",
&gArmGlobalVariableGuid,
NULL,
&VariableSize,
&DefaultSerialNo
);
if (Status == EFI_NOT_FOUND) {
Status = gRT->SetVariable (
(CHAR16*)L"SerialNo",
&gArmGlobalVariableGuid,
EFI_VARIABLE_NON_VOLATILE |
EFI_VARIABLE_BOOTSERVICE_ACCESS |
EFI_VARIABLE_RUNTIME_ACCESS,
VariableSize,
DefaultSerialNo
);
}
} else {
AsciiStrToUnicodeStr (Random->SerialNo, SerialNoUnicode);
VariableSize = SERIAL_NUMBER_LENGTH * sizeof (CHAR16);
Status = gRT->GetVariable (
(CHAR16 *)L"SerialNo",
&gArmGlobalVariableGuid,
NULL,
&VariableSize,
&DataUnicode
);
if ((Status == EFI_NOT_FOUND) || StrCmp (DataUnicode, SerialNoUnicode)) {
Status = gRT->SetVariable (
(CHAR16*)L"SerialNo",
&gArmGlobalVariableGuid,
EFI_VARIABLE_NON_VOLATILE |
EFI_VARIABLE_BOOTSERVICE_ACCESS |
EFI_VARIABLE_RUNTIME_ACCESS,
VariableSize,
SerialNoUnicode
);
}
}
exit:
FreePool (DataPtr);
return Status;
}
STATIC
VOID
EFIAPI
HiKeyInitBootDevice (
IN VOID
)
{
EFI_STATUS Status;
UINTN VariableSize;
CHAR16 DefaultBootDevice[] = L"emmc";
VariableSize = BOOT_DEVICE_LENGTH * sizeof (CHAR16);
Status = gRT->GetVariable (
(CHAR16 *)L"HiKeyBootDevice",
&gArmGlobalVariableGuid,
NULL,
&VariableSize,
&DefaultBootDevice
);
if (Status == EFI_NOT_FOUND) {
Status = gRT->SetVariable (
(CHAR16*)L"HiKeyBootDevice",
&gArmGlobalVariableGuid,
EFI_VARIABLE_NON_VOLATILE |
EFI_VARIABLE_BOOTSERVICE_ACCESS |
EFI_VARIABLE_RUNTIME_ACCESS,
VariableSize,
DefaultBootDevice
);
}
}
EFI_STATUS
EFIAPI
HiKeyEntryPoint (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
HiKeyInitSerialNo ();
HiKeyInitBootDevice ();
HiKeyInitPeripherals ();
// Try to install the Flat Device Tree (FDT). This function actually installs the
// UEFI Driver Binding Protocol.
Status = HiKeyFdtInstall (ImageHandle);
if (EFI_ERROR (Status))
return Status;
Status = HiKeyBootMenuInstall ();
return Status;
}
|