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
|
/** @file
*
* Shell command for launching AXF files.
*
* Copyright (c) 2014, ARM Limited. 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 <Guid/GlobalVariable.h>
#include <Library/PrintLib.h>
#include <Library/HandleParsingLib.h>
#include <Library/DevicePathLib.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/DebugLib.h>
#include <Library/ArmLib.h>
#include "ArmShellCmdRunAxf.h"
#include "ElfLoader.h"
#include "BootMonFsLoader.h"
// Provide arguments to AXF?
typedef VOID (*ELF_ENTRYPOINT)(UINTN arg0, UINTN arg1,
UINTN arg2, UINTN arg3);
STATIC
EFI_STATUS
ShutdownUefiBootServices (
VOID
)
{
EFI_STATUS Status;
UINTN MemoryMapSize;
EFI_MEMORY_DESCRIPTOR *MemoryMap;
UINTN MapKey;
UINTN DescriptorSize;
UINT32 DescriptorVersion;
UINTN Pages;
MemoryMap = NULL;
MemoryMapSize = 0;
Pages = 0;
do {
Status = gBS->GetMemoryMap (
&MemoryMapSize,
MemoryMap,
&MapKey,
&DescriptorSize,
&DescriptorVersion
);
if (Status == EFI_BUFFER_TOO_SMALL) {
Pages = EFI_SIZE_TO_PAGES (MemoryMapSize) + 1;
MemoryMap = AllocatePages (Pages);
//
// Get System MemoryMap
//
Status = gBS->GetMemoryMap (
&MemoryMapSize,
MemoryMap,
&MapKey,
&DescriptorSize,
&DescriptorVersion
);
}
// Don't do anything between the GetMemoryMap() and ExitBootServices()
if (!EFI_ERROR (Status)) {
Status = gBS->ExitBootServices (gImageHandle, MapKey);
if (EFI_ERROR (Status)) {
FreePages (MemoryMap, Pages);
MemoryMap = NULL;
MemoryMapSize = 0;
}
}
} while (EFI_ERROR (Status));
return Status;
}
STATIC
EFI_STATUS
PreparePlatformHardware (
VOID
)
{
//Note: Interrupts will be disabled by the GIC driver when ExitBootServices() will be called.
// Clean before Disable else the Stack gets corrupted with old data.
ArmCleanDataCache ();
ArmDisableDataCache ();
// Invalidate all the entries that might have snuck in.
ArmInvalidateDataCache ();
// Disable and invalidate the instruction cache
ArmDisableInstructionCache ();
ArmInvalidateInstructionCache ();
// Turn off MMU
ArmDisableMmu();
return EFI_SUCCESS;
}
// Process arguments to pass to AXF?
STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
{NULL, TypeMax}
};
/**
This is the shell command handler function pointer callback type. This
function handles the command when it is invoked in the shell.
@param[in] This The instance of the
EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL.
@param[in] SystemTable The pointer to the system table.
@param[in] ShellParameters The parameters associated with the command.
@param[in] Shell The instance of the shell protocol used in the
context of processing this command.
@return EFI_SUCCESS The operation was successful.
@return other The operation failed.
**/
SHELL_STATUS
EFIAPI
ShellDynCmdRunAxfHandler (
IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This,
IN EFI_SYSTEM_TABLE *SystemTable,
IN EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters,
IN EFI_SHELL_PROTOCOL *Shell
)
{
LIST_ENTRY *ParamPackage;
EFI_STATUS Status;
SHELL_STATUS ShellStatus;
SHELL_FILE_HANDLE FileHandle;
ELF_ENTRYPOINT StartElf;
CONST CHAR16 *FileName;
EFI_FILE_INFO *Info;
UINTN FileSize;
VOID *FileData;
VOID *Entrypoint;
LIST_ENTRY LoadList;
LIST_ENTRY *Node;
LIST_ENTRY *NextNode;
RUNAXF_LOAD_LIST *LoadNode;
CHAR16 *TmpFileName;
CHAR16 *TmpChar16;
ShellStatus = SHELL_SUCCESS;
FileHandle = NULL;
FileData = NULL;
InitializeListHead (&LoadList);
// Only install if they are not there yet? First time or every time?
// These can change if the shell exits and start again.
Status = gBS->InstallMultipleProtocolInterfaces (&gImageHandle,
&gEfiShellProtocolGuid, Shell,
&gEfiShellParametersProtocolGuid, ShellParameters,
NULL);
if (EFI_ERROR (Status)) {
return SHELL_DEVICE_ERROR;
}
// Update the protocols for the application library
Status = ShellInitialize ();
ASSERT_EFI_ERROR (Status);
// Add support to load AXF with optipnal args?
//
// Process Command Line arguments
//
Status = ShellCommandLineParse (ParamList, &ParamPackage, NULL, TRUE);
if (EFI_ERROR (Status)) {
ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_INVALID_ARG), gRunAxfHiiHandle);
ShellStatus = SHELL_INVALID_PARAMETER;
} else {
//
// Check for "-?"
//
if ((ShellCommandLineGetFlag (ParamPackage, L"-?")) ||
(ShellCommandLineGetRawValue (ParamPackage, 1) == NULL)) {
//
// We didn't get a file to load
//
ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_INVALID_ARG), gRunAxfHiiHandle);
ShellStatus = SHELL_INVALID_PARAMETER;
} else {
// For the moment we assume we only ever get one file to load with no arguments.
FileName = ShellCommandLineGetRawValue (ParamPackage, 1);
Status = ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_READ, 0);
if (EFI_ERROR (Status)) {
// BootMonFS supports file extensions, but they are stripped by default
// when the NOR is programmed.
// Remove the file extension and try to open again.
ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_FILE_NOT_FOUND),
gRunAxfHiiHandle, FileName);
// Go through the filename and remove file extension. Preserve the
// original name.
TmpFileName = AllocateCopyPool (StrSize (FileName), (VOID *)FileName);
if (TmpFileName != NULL) {
TmpChar16 = StrStr (TmpFileName, L".");
if (TmpChar16 != NULL) {
*TmpChar16 = '\0';
DEBUG((EFI_D_ERROR, "Trying to open file: %s\n", TmpFileName));
Status = ShellOpenFileByName (TmpFileName, &FileHandle,
EFI_FILE_MODE_READ, 0);
}
FreePool (TmpFileName);
}
// Do we now have an open file after trying again?
if (EFI_ERROR (Status)) {
ShellStatus = SHELL_INVALID_PARAMETER;
FileHandle = NULL;
}
}
if (FileHandle != NULL) {
Info = ShellGetFileInfo (FileHandle);
FileSize = (UINTN) Info->FileSize;
FreePool (Info);
//
// Allocate buffer to read file. 'Runtime' so we can access it after
// ExitBootServices().
//
FileData = AllocateRuntimeZeroPool (FileSize);
if (FileData == NULL) {
ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_NO_MEM), gRunAxfHiiHandle);
ShellStatus = SHELL_OUT_OF_RESOURCES;
} else {
//
// Read file into Buffer
//
Status = ShellReadFile (FileHandle, &FileSize, FileData);
if (EFI_ERROR (Status)) {
ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_READ_FAIL), gRunAxfHiiHandle);
SHELL_FREE_NON_NULL (FileData);
FileData = NULL;
ShellStatus = SHELL_DEVICE_ERROR;
}
}
}
}
//
// Free the command line package
//
ShellCommandLineFreeVarList (ParamPackage);
}
// We have a file in memory. Try to work out if we can use it.
// It can either be in ELF format or BootMonFS format.
if (FileData != NULL) {
// Do some validation on the file before we try to load it. The file can
// either be an proper ELF file or one processed by the FlashLoader.
// Since the data might need to go to various locations in memory we cannot
// load the data directly while UEFI is running. We use the file loaders to
// populate a linked list of data and load addresses. This is processed and
// data copied to where it needs to go after calling ExitBootServices. At
// that stage we've reached the point of no return, so overwriting UEFI code
// does not make a difference.
Status = ElfCheckFile (FileData);
if (!EFI_ERROR (Status)) {
// Load program into memory
Status = ElfLoadFile ((VOID*)FileData, &Entrypoint, &LoadList);
} else {
// Try to see if it is a BootMonFs executable
Status = BootMonFsCheckFile ((EFI_FILE_HANDLE)FileHandle);
if (!EFI_ERROR (Status)) {
// Load program into memory
Status = BootMonFsLoadFile ((EFI_FILE_HANDLE)FileHandle,
(VOID*)FileData, &Entrypoint, &LoadList);
} else {
ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_BAD_FILE),
gRunAxfHiiHandle);
SHELL_FREE_NON_NULL (FileData);
ShellStatus = SHELL_UNSUPPORTED;
}
}
}
// Program load list created.
// Shutdown UEFI, copy and jump to code.
if (!IsListEmpty (&LoadList) && !EFI_ERROR (Status)) {
// Exit boot services here. This means we cannot return and cannot assume to
// have access to UEFI functions.
Status = ShutdownUefiBootServices ();
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR,"Can not shutdown UEFI boot services. Status=0x%X\n",
Status));
} else {
// Process linked list. Copy data to Memory.
Node = GetFirstNode (&LoadList);
while (!IsNull (&LoadList, Node)) {
LoadNode = (RUNAXF_LOAD_LIST *)Node;
// Do we have data to copy or do we need to set Zeroes (.bss)?
if (LoadNode->Zeroes) {
ZeroMem ((VOID*)LoadNode->MemOffset, LoadNode->Length);
} else {
CopyMem ((VOID *)LoadNode->MemOffset, (VOID *)LoadNode->FileOffset,
LoadNode->Length);
}
Node = GetNextNode (&LoadList, Node);
}
//
// Switch off interrupts, caches, mmu, etc
//
Status = PreparePlatformHardware ();
ASSERT_EFI_ERROR (Status);
StartElf = (ELF_ENTRYPOINT)Entrypoint;
StartElf (0,0,0,0);
// We should never get here.. But if we do, spin..
ASSERT (FALSE);
while (1);
}
}
// Free file related information as we are returning to UEFI.
Node = GetFirstNode (&LoadList);
while (!IsNull (&LoadList, Node)) {
NextNode = RemoveEntryList (Node);
FreePool (Node);
Node = NextNode;
}
SHELL_FREE_NON_NULL (FileData);
if (FileHandle != NULL) {
ShellCloseFile (&FileHandle);
}
// Uninstall protocols as we don't know if they will change.
// If the shell exits and come in again these mappings may be different
// and cause a crash.
Status = gBS->UninstallMultipleProtocolInterfaces (gImageHandle,
&gEfiShellProtocolGuid, Shell,
&gEfiShellParametersProtocolGuid, ShellParameters,
NULL);
if (EFI_ERROR (Status) && ShellStatus == SHELL_SUCCESS) {
ShellStatus = SHELL_DEVICE_ERROR;
}
return ShellStatus;
}
/**
This is the command help handler function pointer callback type. This
function is responsible for displaying help information for the associated
command.
@param[in] This The instance of the
EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL.
@param[in] Language The pointer to the language string to use.
@return string Pool allocated help string, must be freed by
caller.
**/
CHAR16*
EFIAPI
ShellDynCmdRunAxfGetHelp (
IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This,
IN CONST CHAR8 *Language
)
{
CHAR16 *HelpText;
ASSERT (gRunAxfHiiHandle != NULL);
// This allocates memory. The caller is responsoible to free.
HelpText = HiiGetString (gRunAxfHiiHandle, STRING_TOKEN (STR_GET_HELP_RUNAXF),
Language);
return HelpText;
}
|