summaryrefslogtreecommitdiff
path: root/ArmPlatformPkg/Library/ArmShellCmdRunAxf/ElfLoader.c
blob: 6bb0d22de2fd7907cc8b83bfbeccd2547ea74fa3 (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
/** @file
*
*  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 <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/DebugLib.h>
#include <Library/UefiLib.h>

#include "ArmShellCmdRunAxf.h"
#include "ElfLoader.h"
#include "elf_common.h"
#include "elf32.h"
#include "elf64.h"


// Put the functions the #ifdef. We only use the appropriate one for the platform.
// This prevents 'defined but not used' compiler warning.
#ifdef MDE_CPU_ARM
STATIC
BOOLEAN
IsArmElf (
  IN  CONST VOID *Buf
  )
{
  Elf32_Ehdr *Hdr = (Elf32_Ehdr*)Buf;

  if (Hdr->e_ident[EI_CLASS] != ELFCLASS32) {
    ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_ELFWRONGCLASS_32), gRunAxfHiiHandle);
    return FALSE;
  }

  if (Hdr->e_machine != EM_ARM) {
    ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_ELFWRONGMACH_32), gRunAxfHiiHandle);
    return FALSE;
  }

  // We don't currently check endianness of ELF data (hdr->e_ident[EI_DATA])

  return TRUE;
}
#elif defined(MDE_CPU_AARCH64)
STATIC
BOOLEAN
IsAarch64Elf (
  IN  CONST VOID *Buf
  )
{
  Elf64_Ehdr *Hdr = (Elf64_Ehdr*)Buf;

  if (Hdr->e_ident[EI_CLASS] != ELFCLASS64) {
    ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_ELFWRONGCLASS_64), gRunAxfHiiHandle);
    return FALSE;
  }

  if (Hdr->e_machine != EM_AARCH64) {
    ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_ELFWRONGMACH_64), gRunAxfHiiHandle);
    return FALSE;
  }

  // We don't currently check endianness of ELF data (hdr->e_ident[EI_DATA])

  return TRUE;
}
#endif // MDE_CPU_ARM , MDE_CPU_AARCH64


/**
 Support checking 32 and 64bit as the header could be valid, we might just
 not support loading it.
**/
STATIC
EFI_STATUS
ElfCheckHeader (
  IN  CONST VOID *Buf
  )
{
  Elf32_Ehdr *Hdr32 = (Elf32_Ehdr*)Buf;

  if (!IS_ELF (*Hdr32)) {
    ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_ELFMAGIC), gRunAxfHiiHandle);
    return EFI_INVALID_PARAMETER;
  }

  if (Hdr32->e_type != ET_EXEC) {
    ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_ELFNOTEXEC), gRunAxfHiiHandle);
    return EFI_INVALID_PARAMETER;
  }

  if (Hdr32->e_ident[EI_CLASS] == ELFCLASS32) {
    if ((Hdr32->e_phoff == 0) || (Hdr32->e_phentsize == 0) || (Hdr32->e_phnum == 0)) {
      ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_ELFNOPROG), gRunAxfHiiHandle);
      return EFI_INVALID_PARAMETER;
    }

    if (Hdr32->e_flags != 0) {
      DEBUG ((EFI_D_INFO, "Warning: Wrong processor-specific flags, expected 0.\n"));
    }

    DEBUG ((EFI_D_INFO, "Entry point addr: 0x%lx\n", Hdr32->e_entry));
    DEBUG ((EFI_D_INFO, "Start of program headers: 0x%lx\n", Hdr32->e_phoff));
    DEBUG ((EFI_D_INFO, "Size of 1 program header: %d\n", Hdr32->e_phentsize));
    DEBUG ((EFI_D_INFO, "Number of program headers: %d\n", Hdr32->e_phnum));
  } else if (Hdr32->e_ident[EI_CLASS] == ELFCLASS64) {
      Elf64_Ehdr *Hdr64 = (Elf64_Ehdr*)Buf;

    if ((Hdr64->e_phoff == 0) || (Hdr64->e_phentsize == 0) || (Hdr64->e_phnum == 0)) {
      ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_ELFNOPROG), gRunAxfHiiHandle);
      return EFI_INVALID_PARAMETER;
    }

    if (Hdr64->e_flags != 0) {
      DEBUG ((EFI_D_INFO, "Warning: Wrong processor-specific flags, expected 0.\n"));
    }

    DEBUG ((EFI_D_INFO, "Entry point addr: 0x%lx\n", Hdr64->e_entry));
    DEBUG ((EFI_D_INFO, "Start of program headers: 0x%lx\n", Hdr64->e_phoff));
    DEBUG ((EFI_D_INFO, "Size of 1 program header: %d\n", Hdr64->e_phentsize));
    DEBUG ((EFI_D_INFO, "Number of program headers: %d\n", Hdr64->e_phnum));
  } else {
    ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_ELFWRONGCLASS), gRunAxfHiiHandle);
    return EFI_INVALID_PARAMETER;
  }

  return EFI_SUCCESS;
}


/**
 Load an ELF segment into memory.

 This function assumes the ELF file is valid.
 This function is meant to be called for PT_LOAD type segments only.
**/
STATIC
EFI_STATUS
ElfLoadSegment (
  IN  CONST VOID  *ElfImage,
  IN  CONST VOID  *PHdr,
  IN  LIST_ENTRY  *LoadList
  )
{
  VOID             *FileSegment;
  VOID             *MemSegment;
  UINTN             ExtraZeroes;
  UINTN             ExtraZeroesCount;
  RUNAXF_LOAD_LIST *LoadNode;

#ifdef MDE_CPU_ARM
  Elf32_Phdr  *ProgramHdr;
  ProgramHdr = (Elf32_Phdr *)PHdr;
#elif defined(MDE_CPU_AARCH64)
  Elf64_Phdr  *ProgramHdr;
  ProgramHdr = (Elf64_Phdr *)PHdr;
#endif

  ASSERT (ElfImage != NULL);
  ASSERT (ProgramHdr != NULL);

  FileSegment = (VOID *)((UINTN)ElfImage + ProgramHdr->p_offset);
  MemSegment = (VOID *)ProgramHdr->p_vaddr;

  // If the segment's memory size p_memsz is larger than the file size p_filesz,
  // the "extra" bytes are defined to hold the value 0 and to follow the
  // segment's initialised area.
  // This is typically the case for the .bss segment.
  // The file size may not be larger than the memory size.
  if (ProgramHdr->p_filesz > ProgramHdr->p_memsz) {
    ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_ELFBADFORMAT), gRunAxfHiiHandle);
    return EFI_INVALID_PARAMETER;
  }

  // Load the segment in memory.
  if (ProgramHdr->p_filesz != 0) {
    DEBUG ((EFI_D_INFO, "Loading segment from 0x%lx to 0x%lx (size = %ld)\n",
                 FileSegment, MemSegment, ProgramHdr->p_filesz));

    LoadNode = AllocateRuntimeZeroPool (sizeof (RUNAXF_LOAD_LIST));
    if (LoadNode == NULL) {
      return EFI_OUT_OF_RESOURCES;
    }
    LoadNode->MemOffset  = (UINTN)MemSegment;
    LoadNode->FileOffset = (UINTN)FileSegment;
    LoadNode->Length     = (UINTN)ProgramHdr->p_filesz;
    InsertTailList (LoadList, &LoadNode->Link);
  }

  ExtraZeroes = ((UINTN)MemSegment + ProgramHdr->p_filesz);
  ExtraZeroesCount = ProgramHdr->p_memsz - ProgramHdr->p_filesz;
  DEBUG ((EFI_D_INFO, "Completing segment with %d zero bytes.\n", ExtraZeroesCount));
  if (ExtraZeroesCount > 0) {
    // Extra Node to add the Zeroes.
    LoadNode = AllocateRuntimeZeroPool (sizeof (RUNAXF_LOAD_LIST));
    if (LoadNode == NULL) {
      return EFI_OUT_OF_RESOURCES;
    }
    LoadNode->MemOffset  = (UINTN)ExtraZeroes;
    LoadNode->Zeroes     = TRUE;
    LoadNode->Length     = ExtraZeroesCount;
    InsertTailList (LoadList, &LoadNode->Link);
  }

  return EFI_SUCCESS;
}


/**
 Check that the ELF File Header is valid and Machine type supported.

 Not all information is checked in the ELF header, only the stuff that
 matters to us in our simplified ELF loader.

 @param[in] ElfImage  Address of the ELF file to check.

 @retval EFI_SUCCESS on success.
 @retval EFI_INVALID_PARAMETER if the header is invalid.
 @retval EFI_UNSUPPORTED if the file type/platform is not supported.
**/
EFI_STATUS
ElfCheckFile (
  IN  CONST VOID *ElfImage
  )
{
  EFI_STATUS Status;

  ASSERT (ElfImage != NULL);

  // Check that the ELF header is valid.
  Status = ElfCheckHeader (ElfImage);
  if (EFI_ERROR(Status)) {
    ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_ELFBADHEADER), gRunAxfHiiHandle);
    return EFI_INVALID_PARAMETER;
  }

#ifdef MDE_CPU_ARM
  if (IsArmElf (ElfImage)) {
    return EFI_SUCCESS;
  }
#elif defined(MDE_CPU_AARCH64)
  if (IsAarch64Elf (ElfImage)) {
    return EFI_SUCCESS;
  }
#endif

  ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_BAD_ARCH), gRunAxfHiiHandle);
  return EFI_UNSUPPORTED;
}


/**
  Load a ELF file.

  @param[in] ElfImage       Address of the ELF file in memory.

  @param[out] EntryPoint    Will be filled with the ELF entry point address.

  @param[out] ImageSize     Will be filled with the ELF size in memory. This will
                            effectively be equal to the sum of the segments sizes.

  This functon assumes the header is valid and supported as checked with
  ElfCheckFile().

  @retval EFI_SUCCESS on success.
  @retval EFI_INVALID_PARAMETER if the ELF file is invalid.
**/
EFI_STATUS
ElfLoadFile (
  IN  CONST VOID   *ElfImage,
  OUT VOID        **EntryPoint,
  OUT LIST_ENTRY   *LoadList
  )
{
  EFI_STATUS    Status;
  UINT8        *ProgramHdr;
  UINTN         Index;
  UINTN         ImageSize;

#ifdef MDE_CPU_ARM
  Elf32_Ehdr   *ElfHdr;
  Elf32_Phdr   *ProgramHdrPtr;

  ElfHdr = (Elf32_Ehdr*)ElfImage;
#elif defined(MDE_CPU_AARCH64)
  Elf64_Ehdr   *ElfHdr;
  Elf64_Phdr   *ProgramHdrPtr;

  ElfHdr = (Elf64_Ehdr*)ElfImage;
#endif

  ASSERT (ElfImage   != NULL);
  ASSERT (EntryPoint != NULL);
  ASSERT (LoadList   != NULL);

  ProgramHdr = (UINT8*)ElfImage + ElfHdr->e_phoff;
  DEBUG ((EFI_D_INFO, "ELF program header entry : 0x%lx\n", ProgramHdr));

  ImageSize = 0;

  // Load every loadable ELF segment into memory.
  for (Index = 0; Index < ElfHdr->e_phnum; ++Index) {

#ifdef MDE_CPU_ARM
    ProgramHdrPtr = (Elf32_Phdr*)ProgramHdr;
#elif defined(MDE_CPU_AARCH64)
    ProgramHdrPtr = (Elf64_Phdr*)ProgramHdr;
#endif

    // Only consider PT_LOAD type segments, ignore others.
    if (ProgramHdrPtr->p_type == PT_LOAD) {
      Status = ElfLoadSegment (ElfImage, (VOID *)ProgramHdrPtr, LoadList);
      if (EFI_ERROR (Status)) {
        ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_ELFFAILSEG), gRunAxfHiiHandle);
        return EFI_INVALID_PARAMETER;
      }
      ImageSize += ProgramHdrPtr->p_memsz;
    }
    ProgramHdr += ElfHdr->e_phentsize;
  }

  if (ImageSize == 0) {
    ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RUNAXF_ELFNOSEG), gRunAxfHiiHandle);
    return EFI_INVALID_PARAMETER;
  }

  // Return the entry point specified in the ELF header.
  *EntryPoint = (void*)ElfHdr->e_entry;

  return EFI_SUCCESS;
}