iPXE
Functions
elfboot.c File Reference

ELF bootable image. More...

#include <errno.h>
#include <elf.h>
#include <ipxe/image.h>
#include <ipxe/elf.h>
#include <ipxe/features.h>
#include <ipxe/init.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
 FEATURE (FEATURE_IMAGE, "ELF", DHCP_EB_FEATURE_ELF, 1)
 
static int elfboot_exec (struct image *image)
 Execute ELF image. More...
 
static int elfboot_check_segment (struct image *image, Elf_Phdr *phdr, physaddr_t dest)
 Check that ELF segment uses flat physical addressing. More...
 
static int elfboot_probe (struct image *image)
 Probe ELF image. More...
 
struct image_type elfboot_image_type __image_type (PROBE_NORMAL)
 ELF image type. More...
 

Detailed Description

ELF bootable image.

Definition in file elfboot.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FEATURE()

FEATURE ( FEATURE_IMAGE  ,
"ELF"  ,
DHCP_EB_FEATURE_ELF  ,
 
)

◆ elfboot_exec()

static int elfboot_exec ( struct image image)
static

Execute ELF image.

Parameters
imageELF image
Return values
rcReturn status code

Definition at line 48 of file elfboot.c.

48  {
51  int rc;
52 
53  /* Load the image using core ELF support */
54  if ( ( rc = elf_load ( image, &entry, &max ) ) != 0 ) {
55  DBGC ( image, "ELF %p could not load: %s\n",
56  image, strerror ( rc ) );
57  return rc;
58  }
59 
60  /* An ELF image has no callback interface, so we need to shut
61  * down before invoking it.
62  */
63  shutdown_boot();
64 
65  /* Jump to OS with flat physical addressing */
66  DBGC ( image, "ELF %p starting execution at %lx\n", image, entry );
67  __asm__ __volatile__ ( PHYS_CODE ( "pushl %%ebp\n\t" /* gcc bug */
68  "call *%%edi\n\t"
69  "popl %%ebp\n\t" /* gcc bug */ )
70  : : "D" ( entry )
71  : "eax", "ebx", "ecx", "edx", "esi", "memory" );
72 
73  DBGC ( image, "ELF %p returned\n", image );
74 
75  /* It isn't safe to continue after calling shutdown() */
76  while ( 1 ) {}
77 
78  return -ECANCELED; /* -EIMPOSSIBLE, anyone? */
79 }
#define PHYS_CODE(asm_code_str)
Definition: librm.h:279
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define max(x, y)
Definition: ath.h:39
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
#define ECANCELED
Operation canceled.
Definition: errno.h:343
int elf_load(struct image *image, physaddr_t *entry, physaddr_t *max)
Load ELF image into memory.
Definition: elf.c:201
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
union aes_table_entry entry[256]
Table entries, indexed by S(N)
Definition: aes.c:26
__asm__ __volatile__("\n1:\n\t" "movb -1(%2,%1), %%al\n\t" "stosb\n\t" "loop 1b\n\t" "xorl %%eax, %%eax\n\t" "mov %3, %1\n\t" "rep stosb\n\t" :"=&D"(discard_D), "=&c"(discard_c) :"r"(data), "g"(pad_len), "0"(value0), "1"(len) :"eax")
unsigned long physaddr_t
Definition: stdint.h:20
__asm__(".section \".rodata\", \"a\", " PROGBITS "\n\t" "\nprivate_key_data:\n\t" ".size private_key_data, ( . - private_key_data )\n\t" ".equ private_key_len, ( . - private_key_data )\n\t" ".previous\n\t")
static void shutdown_boot(void)
Shut down system for OS boot.
Definition: init.h:76

References __asm__(), __volatile__(), DBGC, ECANCELED, elf_load(), entry, max, PHYS_CODE, rc, shutdown_boot(), and strerror().

◆ elfboot_check_segment()

static int elfboot_check_segment ( struct image image,
Elf_Phdr phdr,
physaddr_t  dest 
)
static

Check that ELF segment uses flat physical addressing.

Parameters
imageELF file
phdrELF program header
destDestination address
Return values
rcReturn status code

Definition at line 89 of file elfboot.c.

90  {
91 
92  /* Check that ELF segment uses flat physical addressing */
93  if ( phdr->p_vaddr != dest ) {
94  DBGC ( image, "ELF %p uses virtual addressing (phys %x, "
95  "virt %x)\n", image, phdr->p_paddr, phdr->p_vaddr );
96  return -ENOEXEC;
97  }
98 
99  return 0;
100 }
Elf32_Addr p_paddr
Definition: elf.h:71
#define ENOEXEC
Exec format error.
Definition: errno.h:519
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
Elf32_Addr p_vaddr
Definition: elf.h:70
static void * dest
Definition: strings.h:176

References DBGC, dest, ENOEXEC, Elf32_Phdr::p_paddr, and Elf32_Phdr::p_vaddr.

Referenced by elfboot_probe().

◆ elfboot_probe()

static int elfboot_probe ( struct image image)
static

Probe ELF image.

Parameters
imageELF file
Return values
rcReturn status code

Definition at line 108 of file elfboot.c.

108  {
109  Elf32_Ehdr ehdr;
110  static const uint8_t e_ident[] = {
111  [EI_MAG0] = ELFMAG0,
112  [EI_MAG1] = ELFMAG1,
113  [EI_MAG2] = ELFMAG2,
114  [EI_MAG3] = ELFMAG3,
115  [EI_CLASS] = ELFCLASS32,
116  [EI_DATA] = ELFDATA2LSB,
118  };
120  physaddr_t max;
121  int rc;
122 
123  /* Read ELF header */
124  copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) );
125  if ( memcmp ( ehdr.e_ident, e_ident, sizeof ( e_ident ) ) != 0 ) {
126  DBGC ( image, "Invalid ELF identifier\n" );
127  return -ENOEXEC;
128  }
129 
130  /* Check that this image uses flat physical addressing */
131  if ( ( rc = elf_segments ( image, &ehdr, elfboot_check_segment,
132  &entry, &max ) ) != 0 ) {
133  DBGC ( image, "Unloadable ELF image\n" );
134  return rc;
135  }
136 
137  return 0;
138 }
#define EI_DATA
Definition: elf.h:48
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
ELF header.
Definition: elf.h:25
userptr_t data
Raw file image.
Definition: image.h:41
#define max(x, y)
Definition: ath.h:39
#define EI_VERSION
Definition: elf.h:49
#define ELFMAG1
Definition: elf.h:53
#define ENOEXEC
Exec format error.
Definition: errno.h:519
static __always_inline void copy_from_user(void *dest, userptr_t src, off_t src_off, size_t len)
Copy data from user buffer.
Definition: uaccess.h:337
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
#define ELFCLASS32
Definition: elf.h:58
#define ELFMAG2
Definition: elf.h:54
unsigned char e_ident[EI_NIDENT]
Definition: elf.h:26
#define EI_MAG2
Definition: elf.h:45
union aes_table_entry entry[256]
Table entries, indexed by S(N)
Definition: aes.c:26
unsigned char uint8_t
Definition: stdint.h:10
int elf_segments(struct image *image, Elf_Ehdr *ehdr, int(*process)(struct image *image, Elf_Phdr *phdr, physaddr_t dest), physaddr_t *entry, physaddr_t *max)
Process ELF segments.
Definition: elf.c:154
static int elfboot_check_segment(struct image *image, Elf_Phdr *phdr, physaddr_t dest)
Check that ELF segment uses flat physical addressing.
Definition: elfboot.c:89
#define EI_CLASS
Definition: elf.h:47
unsigned long physaddr_t
Definition: stdint.h:20
#define ELFMAG3
Definition: elf.h:55
#define EV_CURRENT
Definition: elf.h:64
#define ELFDATA2LSB
Definition: elf.h:61
#define EI_MAG3
Definition: elf.h:46
#define ELFMAG0
Definition: elf.h:52
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:98
#define EI_MAG1
Definition: elf.h:44
#define EI_MAG0
Definition: elf.h:43

References copy_from_user(), image::data, DBGC, Elf32_Ehdr::e_ident, EI_CLASS, EI_DATA, EI_MAG0, EI_MAG1, EI_MAG2, EI_MAG3, EI_VERSION, elf_segments(), elfboot_check_segment(), ELFCLASS32, ELFDATA2LSB, ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, ENOEXEC, entry, EV_CURRENT, max, memcmp(), and rc.

◆ __image_type()

struct image_type elfboot_image_type __image_type ( PROBE_NORMAL  )

ELF image type.