2 * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER
);
23 #include <gpxe/image.h>
25 #include <gpxe/features.h>
26 #include <gpxe/init.h>
35 FEATURE ( FEATURE_IMAGE
, "ELF", DHCP_EB_FEATURE_ELF
, 1 );
37 struct image_type elfboot_image_type
__image_type ( PROBE_NORMAL
);
43 * @ret rc Return status code
45 static int elfboot_exec ( struct image
*image
) {
46 physaddr_t entry
= image
->priv
.phys
;
48 /* An ELF image has no callback interface, so we need to shut
49 * down before invoking it.
51 shutdown ( SHUTDOWN_BOOT
);
53 /* Jump to OS with flat physical addressing */
54 DBGC ( image
, "ELF %p starting execution at %lx\n", image
, entry
);
55 __asm__
__volatile__ ( PHYS_CODE ( "call *%%edi\n\t" )
57 : "eax", "ebx", "ecx", "edx", "esi", "ebp",
60 DBGC ( image
, "ELF %p returned\n", image
);
62 /* It isn't safe to continue after calling shutdown() */
65 return -ECANCELED
; /* -EIMPOSSIBLE, anyone? */
69 * Load ELF image into memory
72 * @ret rc Return status code
74 static int elfboot_load ( struct image
*image
) {
76 static const uint8_t e_ident
[] = {
81 [EI_CLASS
] = ELFCLASS32
,
82 [EI_DATA
] = ELFDATA2LSB
,
83 [EI_VERSION
] = EV_CURRENT
,
88 copy_from_user ( &ehdr
, image
->data
, 0, sizeof ( ehdr
) );
89 if ( memcmp ( ehdr
.e_ident
, e_ident
, sizeof ( e_ident
) ) != 0 ) {
90 DBG ( "Invalid ELF identifier\n" );
94 /* This is an ELF image, valid or otherwise */
96 image
->type
= &elfboot_image_type
;
98 /* Load the image using core ELF support */
99 if ( ( rc
= elf_load ( image
) ) != 0 ) {
100 DBGC ( image
, "ELF %p could not load: %s\n",
101 image
, strerror ( rc
) );
108 /** ELF image type */
109 struct image_type elfboot_image_type
__image_type ( PROBE_NORMAL
) = {
111 .load
= elfboot_load
,
112 .exec
= elfboot_exec
,