-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlashLoader.c
More file actions
329 lines (286 loc) · 10.6 KB
/
FlashLoader.c
File metadata and controls
329 lines (286 loc) · 10.6 KB
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
/** @file
A simple OS Loader that load Dtb, Initrd and Linux Kernel from XIP flash memory.
Copyright (c) 2018 Chen Baozi <cbz@baozis.org>. All rights reserved.<BR>
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.
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 <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/DevicePathLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
#include <Library/UefiApplicationEntryPoint.h>
#include <Protocol/DevicePath.h>
#include <Protocol/LoadedImage.h>
#include <libfdt.h> // EmbeddedPkg/Library/FdtLib
#define FLASH_BASE 0x0 // 0
#define FLASH_FDT_OFFSET 0x200000 // 2M
#define FLASH_KERNEL_OFFSET 0x400000 // 4M
#define FLASH_INITRD_OFFSET 0x2000000 // 32M
#define FDT_ADDR (FLASH_BASE + FLASH_FDT_OFFSET)
#define INITRD_ADDR (FLASH_BASE + FLASH_INITRD_OFFSET)
#define KERNEL_ADDR (FLASH_BASE + FLASH_KERNEL_OFFSET)
#define FDT_BLOCK_SIZE 1048576 // <2M
#define KERNEL_BLOCK_SIZE 14516736 // <28M
#define INITRD_BLOCK_SIZE 16724866 // <32M
#define FDT_ADDITIONAL_ENTRIES_SIZE 0x400
#define KERNEL_ARGS_SIZE 512
#define KERNEL_CMDLINE L"console=tty0 console=ttyAMA0,115200 earlyprintk=ttyAMA0,115200 root=/dev/ram0" // EFI handle string in UTF-8
typedef struct {
MEMMAP_DEVICE_PATH Node1;
EFI_DEVICE_PATH_PROTOCOL End;
} MEMORY_DEVICE_PATH;
STATIC CONST MEMORY_DEVICE_PATH mMemoryDevicePathTemplate =
{
{
{
HARDWARE_DEVICE_PATH,
HW_MEMMAP_DP,
{
(UINT8)(sizeof(MEMMAP_DEVICE_PATH)),
(UINT8)((sizeof(MEMMAP_DEVICE_PATH)) >> 8),
},
}, // Header
0, // StartingAddress (set at runtime)
0 // EndingAddress (set at runtime)
}, // Node1
{
END_DEVICE_PATH_TYPE,
END_ENTIRE_DEVICE_PATH_SUBTYPE,
{ sizeof(EFI_DEVICE_PATH_PROTOCOL), 0 }
} // End
};
INTN
FlashLoaderGetChosenNode(
IN INTN UpdatedFdtBase
)
{
INTN ChosenNode;
ChosenNode = fdt_subnode_offset((CONST VOID *)UpdatedFdtBase, 0, "chosen");
if (ChosenNode < 0)
{
ChosenNode = fdt_add_subnode((VOID *)UpdatedFdtBase, 0, "chosen");
if (ChosenNode < 0)
{
DEBUG((DEBUG_ERROR, "Fail to find fdt node chosen!\n"));
return 0;
}
}
return ChosenNode;
}
EFI_STATUS
FlashLoaderSetProperty64 (
IN INTN UpdatedFdtBase,
IN INTN ChosenNode,
IN CHAR8 *PropertyName,
IN UINT64 Val
)
{
INTN Err;
struct fdt_property *Property;
int Len;
Property = fdt_get_property_w (
(VOID *)UpdatedFdtBase,
ChosenNode,
PropertyName,
&Len
);
if (NULL == Property && Len == -FDT_ERR_NOTFOUND) { // No chosen node in the FDT
Val = cpu_to_fdt64(Val);
Err = fdt_appendprop (
(VOID *)UpdatedFdtBase, // In Param
ChosenNode, // In Param
PropertyName, // In Param
&Val, // In Param
sizeof(UINT64));
} else if (Property != NULL) { // Already has chosen node
Err = fdt_setprop_u64 (
(VOID *)UpdatedFdtBase, // In Param
ChosenNode, // IN Param
PropertyName, // IN Param
Val // IN Param
);
} else {
return EFI_INVALID_PARAMETER;
}
return EFI_SUCCESS;
}
EFI_STATUS
FlashLoaderUpdateFdt (
IN VOID *FdtBase,
IN VOID *RamdiskData,
IN UINTN RamdiskSize
)
{
INTN ChosenNode, Err, NewFdtSize;
EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS UpdatedFdtBase;
// Extended and Allocate FDT memory region.
NewFdtSize = (UINTN)fdt_totalsize (FdtBase) + FDT_ADDITIONAL_ENTRIES_SIZE;
Print(L"Original FDT starts at 0x%lx with size %d bytes.\n",
FdtBase, fdt_totalsize(FdtBase));
Print(L"We will extended new FDT buffer to size 0x%x.\n", NewFdtSize);
Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData,
EFI_SIZE_TO_PAGES (NewFdtSize), &UpdatedFdtBase);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "Warning: Failed to reallocate FDT, err %d.\n",
Status));
return Status;
} else {
Print(L"Allocate new buffer for relocated FDT (@0x%lx).\n", UpdatedFdtBase);
}
// Load the Original FDT tree into the new region
Err = fdt_open_into(FdtBase, (VOID *)(INTN)UpdatedFdtBase, NewFdtSize);
if (Err) {
DEBUG ((DEBUG_ERROR, "fdt_open_into(): %a\n", fdt_strerror (Err)));
Status = EFI_INVALID_PARAMETER;
goto Fdt_Exit;
}
ChosenNode = FlashLoaderGetChosenNode(UpdatedFdtBase);
if (!ChosenNode) {
goto Fdt_Exit;
}
// Set "linux,initrd-start" and "linux,initrd-end"
Status = FlashLoaderSetProperty64 (UpdatedFdtBase, ChosenNode,
"linux,initrd-start",
(UINTN)RamdiskData);
if (EFI_ERROR(Status)) {
goto Fdt_Exit;
}
Status = FlashLoaderSetProperty64 (UpdatedFdtBase, ChosenNode,
"linux,initrd-end",
(UINTN)(RamdiskData + RamdiskSize));
if (EFI_ERROR (Status)) {
goto Fdt_Exit;
}
Status = gBS->InstallConfigurationTable (&gFdtTableGuid, (VOID *)(UINTN)UpdatedFdtBase);
if (!EFI_ERROR(Status)) {
Print(L"Successfully allocate new buffer for FDT and update the chosen node with initrd info.\n");
return EFI_SUCCESS;
}
Fdt_Exit:
gBS->FreePages(UpdatedFdtBase, EFI_SIZE_TO_PAGES(NewFdtSize));
return Status;
}
/***
The user Entry Point for Application. The user code starts with this function
as the real entry point for the application.
@param[in] ImageHandle The firmware allocated handle for the EFI image.
@param[in] SystemTable A pointer to the EFI System Table.
@retval EFI_SUCCESS The entry point is executed successfully.
@retval other Some error occurs when executing this entry point.
***/
EFI_STATUS
EFIAPI
UefiMain(
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
EFI_HANDLE KernelImageHandle;
MEMORY_DEVICE_PATH KernelDevicePath;
EFI_LOADED_IMAGE_PROTOCOL *KernelImageInfo;
VOID *KernelArg;
VOID *NewKernelArg;
VOID *OrigKernel;
UINTN KernelSize;
VOID *OrigFdt;
VOID *Ramdisk;
VOID *OrigRamdisk;
UINTN RamdiskSize;
Print(L"Enter FlashLoader.\n");
OrigFdt = (VOID *)FDT_ADDR;
Print(L"Device Tree Blob starts at 0x%lx\n", OrigFdt);
//
// Load the Initrd from a fixed address
//
OrigRamdisk = (VOID *)INITRD_ADDR;
Print(L"OrigRamdisk starts at 0x%lx .\n", OrigRamdisk);
RamdiskSize = INITRD_BLOCK_SIZE;
Print(L"Size of Ramdisk: 0x%lx\n", RamdiskSize);
Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData,
EFI_SIZE_TO_PAGES (RamdiskSize), (EFI_PHYSICAL_ADDRESS *)&Ramdisk);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "Warning: Failed to reallocate Ramdisk, err %d.\n",
Status));
return Status;
} else {
Print(L"Allocate %d pages (%d bytes) for Ramdisk, starts at 0x%lx\n",
EFI_SIZE_TO_PAGES (RamdiskSize), EFI_SIZE_TO_PAGES(RamdiskSize)*4096, Ramdisk);
}
gBS->CopyMem (Ramdisk, OrigRamdisk, RamdiskSize);
//
// Reallocate and extend FDT with chosen node region.
// Add "linux,initrd-start" and "linux,initrd-end" entries to the chosen node (of DTB).
// Install FDT as a configuration table.
//
FlashLoaderUpdateFdt (OrigFdt, Ramdisk, RamdiskSize);
//
// Load the Kernel (with EFI stub) from the fixed location with
// EFI_BOOT_SERVICES.LoadImage service using the SourceBuffer and SourceSize
// parameters.
//
OrigKernel = (VOID *)KERNEL_ADDR;
KernelSize = KERNEL_BLOCK_SIZE;
Print(L"Load the kernel from 0x%lx, which has the size of %d\n",
OrigKernel, KernelSize);
KernelDevicePath = mMemoryDevicePathTemplate;
KernelDevicePath.Node1.StartingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) OrigKernel;
Print(L" Kernel starts from 0x%lx.\n", KernelDevicePath.Node1.StartingAddress);
KernelDevicePath.Node1.EndingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) OrigKernel + KernelSize;
Print(L" Kernel ends at 0x%lx.\n", KernelDevicePath.Node1.EndingAddress);
Print(L"Before calling gBS->LoadImage.\n");
Status = gBS->LoadImage (
FALSE,
gImageHandle,
(EFI_DEVICE_PATH *)&KernelDevicePath,
(VOID *)(UINTN)OrigKernel,
KernelSize,
&KernelImageHandle);
if (EFI_ERROR (Status)) {
Print(L"Failed to LoadImage\n");
return Status;
} else {
Print(L"LoadImage looks successful.\n");
}
//
// Add the kernel command line in the LoadOptions string
//
KernelArg = KERNEL_CMDLINE;
Print(L"Static kernel args: %s\n", KernelArg);
Status = gBS->AllocatePool (EfiBootServicesData, StrLen(KernelArg), &NewKernelArg);
if (EFI_ERROR(Status)) {
DEBUG((DEBUG_WARN, "Warning: Failed to allocate kernel args buffer, err %d.\n",
Status));
return Status;
} else {
Print(L"AllocatePool succeeded for NewKernelArg.\n");
}
StrnCpyS(NewKernelArg, KERNEL_ARGS_SIZE, KernelArg, StrLen(KernelArg));
Print(L"Kernel cmdline: %s\n", NewKernelArg);
Status = gBS->HandleProtocol (
KernelImageHandle,
&gEfiLoadedImageProtocolGuid,
(VOID **)&KernelImageInfo);
if (EFI_ERROR (Status)) {
Print(L"Failed to HandleProtocol gEfiLoadedImageProtocolGuid.\n");
return Status;
}
KernelImageInfo->LoadOptions = NewKernelArg;
KernelImageInfo->LoadOptionsSize = StrLen (NewKernelArg) * sizeof (CHAR16);
//
// Start the Kernel with EFI_BOOT_SERVICES.StartImage boot service
//
Status = gBS->StartImage (KernelImageHandle, 0, NULL);
Print(L"Failed to StartImage.\n");
// When successful, not reach.
gBS->UnloadImage (KernelImageHandle);
// TODO: Free LoadOptions
return Status;
}