Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / image / elfboot.c
1 /*
2  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <errno.h>
23 #include <elf.h>
24 #include <ipxe/image.h>
25 #include <ipxe/elf.h>
26 #include <ipxe/features.h>
27 #include <ipxe/init.h>
28
29 /**
30  * @file
31  *
32  * ELF bootable image
33  *
34  */
35
36 FEATURE ( FEATURE_IMAGE, "ELF", DHCP_EB_FEATURE_ELF, 1 );
37
38 /**
39  * Execute ELF image
40  *
41  * @v image             ELF image
42  * @ret rc              Return status code
43  */
44 static int elfboot_exec ( struct image *image ) {
45         physaddr_t entry;
46         physaddr_t max;
47         int rc;
48
49         /* Load the image using core ELF support */
50         if ( ( rc = elf_load ( image, &entry, &max ) ) != 0 ) {
51                 DBGC ( image, "ELF %p could not load: %s\n",
52                        image, strerror ( rc ) );
53                 return rc;
54         }
55
56         /* An ELF image has no callback interface, so we need to shut
57          * down before invoking it.
58          */
59         shutdown_boot();
60
61         /* Jump to OS with flat physical addressing */
62         DBGC ( image, "ELF %p starting execution at %lx\n", image, entry );
63         __asm__ __volatile__ ( PHYS_CODE ( "pushl %%ebp\n\t" /* gcc bug */
64                                            "call *%%edi\n\t"
65                                            "popl %%ebp\n\t" /* gcc bug */ )
66                                : : "D" ( entry )
67                                : "eax", "ebx", "ecx", "edx", "esi", "memory" );
68
69         DBGC ( image, "ELF %p returned\n", image );
70
71         /* It isn't safe to continue after calling shutdown() */
72         while ( 1 ) {}
73
74         return -ECANCELED;  /* -EIMPOSSIBLE, anyone? */
75 }
76
77 /**
78  * Probe ELF image
79  *
80  * @v image             ELF file
81  * @ret rc              Return status code
82  */
83 static int elfboot_probe ( struct image *image ) {
84         Elf32_Ehdr ehdr;
85         static const uint8_t e_ident[] = {
86                 [EI_MAG0]       = ELFMAG0,
87                 [EI_MAG1]       = ELFMAG1,
88                 [EI_MAG2]       = ELFMAG2,
89                 [EI_MAG3]       = ELFMAG3,
90                 [EI_CLASS]      = ELFCLASS32,
91                 [EI_DATA]       = ELFDATA2LSB,
92                 [EI_VERSION]    = EV_CURRENT,
93         };
94
95         /* Read ELF header */
96         copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) );
97         if ( memcmp ( ehdr.e_ident, e_ident, sizeof ( e_ident ) ) != 0 ) {
98                 DBG ( "Invalid ELF identifier\n" );
99                 return -ENOEXEC;
100         }
101
102         return 0;
103 }
104
105 /** ELF image type */
106 struct image_type elfboot_image_type __image_type ( PROBE_NORMAL ) = {
107         .name = "ELF",
108         .probe = elfboot_probe,
109         .exec = elfboot_exec,
110 };