summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/arch/i386/image/elfboot.c
blob: dc35689293e5df097655029b1d409a1c0d0ff3b7 (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
/*
 * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

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

/**
 * @file
 *
 * ELF bootable image
 *
 */

FEATURE ( FEATURE_IMAGE, "ELF", DHCP_EB_FEATURE_ELF, 1 );

/**
 * Execute ELF image
 *
 * @v image		ELF image
 * @ret rc		Return status code
 */
static int elfboot_exec ( struct image *image ) {
	physaddr_t entry;
	physaddr_t max;
	int rc;

	/* Load the image using core ELF support */
	if ( ( rc = elf_load ( image, &entry, &max ) ) != 0 ) {
		DBGC ( image, "ELF %p could not load: %s\n",
		       image, strerror ( rc ) );
		return rc;
	}

	/* An ELF image has no callback interface, so we need to shut
	 * down before invoking it.
	 */
	shutdown_boot();

	/* Jump to OS with flat physical addressing */
	DBGC ( image, "ELF %p starting execution at %lx\n", image, entry );
	__asm__ __volatile__ ( PHYS_CODE ( "pushl %%ebp\n\t" /* gcc bug */
					   "call *%%edi\n\t"
					   "popl %%ebp\n\t" /* gcc bug */ )
			       : : "D" ( entry )
			       : "eax", "ebx", "ecx", "edx", "esi", "memory" );

	DBGC ( image, "ELF %p returned\n", image );

	/* It isn't safe to continue after calling shutdown() */
	while ( 1 ) {}

	return -ECANCELED;  /* -EIMPOSSIBLE, anyone? */
}

/**
 * Check that ELF segment uses flat physical addressing
 *
 * @v image		ELF file
 * @v phdr		ELF program header
 * @v dest		Destination address
 * @ret rc		Return status code
 */
static int elfboot_check_segment ( struct image *image, Elf_Phdr *phdr,
				   physaddr_t dest ) {

	/* Check that ELF segment uses flat physical addressing */
	if ( phdr->p_vaddr != dest ) {
		DBGC ( image, "ELF %p uses virtual addressing (phys %x, "
		       "virt %x)\n", image, phdr->p_paddr, phdr->p_vaddr );
		return -ENOEXEC;
	}

	return 0;
}

/**
 * Probe ELF image
 *
 * @v image		ELF file
 * @ret rc		Return status code
 */
static int elfboot_probe ( struct image *image ) {
	Elf32_Ehdr ehdr;
	static const uint8_t e_ident[] = {
		[EI_MAG0]	= ELFMAG0,
		[EI_MAG1]	= ELFMAG1,
		[EI_MAG2]	= ELFMAG2,
		[EI_MAG3]	= ELFMAG3,
		[EI_CLASS]	= ELFCLASS32,
		[EI_DATA]	= ELFDATA2LSB,
		[EI_VERSION]	= EV_CURRENT,
	};
	physaddr_t entry;
	physaddr_t max;
	int rc;

	/* Read ELF header */
	copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) );
	if ( memcmp ( ehdr.e_ident, e_ident, sizeof ( e_ident ) ) != 0 ) {
		DBGC ( image, "Invalid ELF identifier\n" );
		return -ENOEXEC;
	}

	/* Check that this image uses flat physical addressing */
	if ( ( rc = elf_segments ( image, &ehdr, elfboot_check_segment,
				   &entry, &max ) ) != 0 ) {
		DBGC ( image, "Unloadable ELF image\n" );
		return rc;
	}

	return 0;
}

/** ELF image type */
struct image_type elfboot_image_type __image_type ( PROBE_NORMAL ) = {
	.name = "ELF",
	.probe = elfboot_probe,
	.exec = elfboot_exec,
};