Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / image / efi_image.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 <stdlib.h>
24 #include <wchar.h>
25 #include <ipxe/efi/efi.h>
26 #include <ipxe/efi/efi_snp.h>
27 #include <ipxe/efi/efi_download.h>
28 #include <ipxe/efi/efi_file.h>
29 #include <ipxe/efi/efi_utils.h>
30 #include <ipxe/efi/efi_strings.h>
31 #include <ipxe/efi/efi_wrap.h>
32 #include <ipxe/image.h>
33 #include <ipxe/init.h>
34 #include <ipxe/features.h>
35 #include <ipxe/uri.h>
36
37 FEATURE ( FEATURE_IMAGE, "EFI", DHCP_EB_FEATURE_EFI, 1 );
38
39 /* Disambiguate the various error causes */
40 #define EINFO_EEFI_LOAD                                                 \
41         __einfo_uniqify ( EINFO_EPLATFORM, 0x01,                        \
42                           "Could not load image" )
43 #define EINFO_EEFI_LOAD_PROHIBITED                                      \
44         __einfo_platformify ( EINFO_EEFI_LOAD, EFI_SECURITY_VIOLATION,  \
45                               "Image prohibited by security policy" )
46 #define EEFI_LOAD_PROHIBITED                                            \
47         __einfo_error ( EINFO_EEFI_LOAD_PROHIBITED )
48 #define EEFI_LOAD( efirc ) EPLATFORM ( EINFO_EEFI_LOAD, efirc,          \
49                                        EEFI_LOAD_PROHIBITED )
50 #define EINFO_EEFI_START                                                \
51         __einfo_uniqify ( EINFO_EPLATFORM, 0x02,                        \
52                           "Could not start image" )
53 #define EEFI_START( efirc ) EPLATFORM ( EINFO_EEFI_START, efirc )
54
55 /**
56  * Create device path for image
57  *
58  * @v image             EFI image
59  * @v parent            Parent device path
60  * @ret path            Device path, or NULL on failure
61  *
62  * The caller must eventually free() the device path.
63  */
64 static EFI_DEVICE_PATH_PROTOCOL *
65 efi_image_path ( struct image *image, EFI_DEVICE_PATH_PROTOCOL *parent ) {
66         EFI_DEVICE_PATH_PROTOCOL *path;
67         FILEPATH_DEVICE_PATH *filepath;
68         EFI_DEVICE_PATH_PROTOCOL *end;
69         size_t name_len;
70         size_t prefix_len;
71         size_t filepath_len;
72         size_t len;
73
74         /* Calculate device path lengths */
75         end = efi_devpath_end ( parent );
76         prefix_len = ( ( void * ) end - ( void * ) parent );
77         name_len = strlen ( image->name );
78         filepath_len = ( SIZE_OF_FILEPATH_DEVICE_PATH +
79                          ( name_len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
80         len = ( prefix_len + filepath_len + sizeof ( *end ) );
81
82         /* Allocate device path */
83         path = zalloc ( len );
84         if ( ! path )
85                 return NULL;
86
87         /* Construct device path */
88         memcpy ( path, parent, prefix_len );
89         filepath = ( ( ( void * ) path ) + prefix_len );
90         filepath->Header.Type = MEDIA_DEVICE_PATH;
91         filepath->Header.SubType = MEDIA_FILEPATH_DP;
92         filepath->Header.Length[0] = ( filepath_len & 0xff );
93         filepath->Header.Length[1] = ( filepath_len >> 8 );
94         efi_snprintf ( filepath->PathName, ( name_len + 1 /* NUL */ ),
95                        "%s", image->name );
96         end = ( ( ( void * ) filepath ) + filepath_len );
97         end->Type = END_DEVICE_PATH_TYPE;
98         end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
99         end->Length[0] = sizeof ( *end );
100
101         return path;
102 }
103
104 /**
105  * Create command line for image
106  *
107  * @v image             EFI image
108  * @ret cmdline         Command line, or NULL on failure
109  */
110 static wchar_t * efi_image_cmdline ( struct image *image ) {
111         wchar_t *cmdline;
112         size_t len;
113
114         len = ( strlen ( image->name ) +
115                 ( image->cmdline ?
116                   ( 1 /* " " */ + strlen ( image->cmdline ) ) : 0 ) );
117         cmdline = zalloc ( ( len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
118         if ( ! cmdline )
119                 return NULL;
120         efi_snprintf ( cmdline, ( len + 1 /* NUL */ ), "%s%s%s",
121                        image->name,
122                        ( image->cmdline ? " " : "" ),
123                        ( image->cmdline ? image->cmdline : "" ) );
124         return cmdline;
125 }
126
127 /**
128  * Execute EFI image
129  *
130  * @v image             EFI image
131  * @ret rc              Return status code
132  */
133 static int efi_image_exec ( struct image *image ) {
134         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
135         struct efi_snp_device *snpdev;
136         EFI_DEVICE_PATH_PROTOCOL *path;
137         union {
138                 EFI_LOADED_IMAGE_PROTOCOL *image;
139                 void *interface;
140         } loaded;
141         EFI_HANDLE handle;
142         wchar_t *cmdline;
143         EFI_STATUS efirc;
144         int rc;
145
146         /* Find an appropriate device handle to use */
147         snpdev = last_opened_snpdev();
148         if ( ! snpdev ) {
149                 DBGC ( image, "EFIIMAGE %p could not identify SNP device\n",
150                        image );
151                 rc = -ENODEV;
152                 goto err_no_snpdev;
153         }
154
155         /* Install file I/O protocols */
156         if ( ( rc = efi_file_install ( snpdev->handle ) ) != 0 ) {
157                 DBGC ( image, "EFIIMAGE %p could not install file protocol: "
158                        "%s\n", image, strerror ( rc ) );
159                 goto err_file_install;
160         }
161
162         /* Install iPXE download protocol */
163         if ( ( rc = efi_download_install ( snpdev->handle ) ) != 0 ) {
164                 DBGC ( image, "EFIIMAGE %p could not install iPXE download "
165                        "protocol: %s\n", image, strerror ( rc ) );
166                 goto err_download_install;
167         }
168
169         /* Create device path for image */
170         path = efi_image_path ( image, snpdev->path );
171         if ( ! path ) {
172                 DBGC ( image, "EFIIMAGE %p could not create device path\n",
173                        image );
174                 rc = -ENOMEM;
175                 goto err_image_path;
176         }
177
178         /* Create command line for image */
179         cmdline = efi_image_cmdline ( image );
180         if ( ! cmdline ) {
181                 DBGC ( image, "EFIIMAGE %p could not create command line\n",
182                        image );
183                 rc = -ENOMEM;
184                 goto err_cmdline;
185         }
186
187         /* Attempt loading image */
188         if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, path,
189                                        user_to_virt ( image->data, 0 ),
190                                        image->len, &handle ) ) != 0 ) {
191                 /* Not an EFI image */
192                 rc = -EEFI_LOAD ( efirc );
193                 DBGC ( image, "EFIIMAGE %p could not load: %s\n",
194                        image, strerror ( rc ) );
195                 goto err_load_image;
196         }
197
198         /* Get the loaded image protocol for the newly loaded image */
199         efirc = bs->OpenProtocol ( handle, &efi_loaded_image_protocol_guid,
200                                    &loaded.interface, efi_image_handle,
201                                    NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL );
202         if ( efirc ) {
203                 /* Should never happen */
204                 rc = -EEFI ( efirc );
205                 goto err_open_protocol;
206         }
207
208         /* Some EFI 1.10 implementations seem not to fill in DeviceHandle */
209         if ( loaded.image->DeviceHandle == NULL ) {
210                 DBGC ( image, "EFIIMAGE %p filling in missing DeviceHandle\n",
211                        image );
212                 loaded.image->DeviceHandle = snpdev->handle;
213         }
214
215         /* Sanity checks */
216         assert ( loaded.image->ParentHandle == efi_image_handle );
217         assert ( loaded.image->DeviceHandle == snpdev->handle );
218         assert ( loaded.image->LoadOptionsSize == 0 );
219         assert ( loaded.image->LoadOptions == NULL );
220
221         /* Set command line */
222         loaded.image->LoadOptions = cmdline;
223         loaded.image->LoadOptionsSize =
224                 ( ( wcslen ( cmdline ) + 1 /* NUL */ ) * sizeof ( wchar_t ) );
225
226         /* Release network devices for use via SNP */
227         efi_snp_release();
228
229         /* Wrap calls made by the loaded image (for debugging) */
230         efi_wrap ( handle );
231
232         /* Start the image */
233         if ( ( efirc = bs->StartImage ( handle, NULL, NULL ) ) != 0 ) {
234                 rc = -EEFI_START ( efirc );
235                 DBGC ( image, "EFIIMAGE %p could not start (or returned with "
236                        "error): %s\n", image, strerror ( rc ) );
237                 goto err_start_image;
238         }
239
240         /* Success */
241         rc = 0;
242
243  err_start_image:
244         efi_snp_claim();
245  err_open_protocol:
246         /* If there was no error, then the image must have been
247          * started and returned successfully.  It either unloaded
248          * itself, or it intended to remain loaded (e.g. it was a
249          * driver).  We therefore do not unload successful images.
250          *
251          * If there was an error, attempt to unload the image.  This
252          * may not work.  In particular, there is no way to tell
253          * whether an error returned from StartImage() was due to
254          * being unable to start the image (in which case we probably
255          * should call UnloadImage()), or due to the image itself
256          * returning an error (in which case we probably should not
257          * call UnloadImage()).  We therefore ignore any failures from
258          * the UnloadImage() call itself.
259          */
260         if ( rc != 0 )
261                 bs->UnloadImage ( handle );
262  err_load_image:
263         free ( cmdline );
264  err_cmdline:
265         free ( path );
266  err_image_path:
267         efi_download_uninstall ( snpdev->handle );
268  err_download_install:
269         efi_file_uninstall ( snpdev->handle );
270  err_file_install:
271  err_no_snpdev:
272         return rc;
273 }
274
275 /**
276  * Probe EFI image
277  *
278  * @v image             EFI file
279  * @ret rc              Return status code
280  */
281 static int efi_image_probe ( struct image *image ) {
282         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
283         static EFI_DEVICE_PATH_PROTOCOL empty_path = {
284                 .Type = END_DEVICE_PATH_TYPE,
285                 .SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE,
286                 .Length[0] = sizeof ( empty_path ),
287         };
288         EFI_HANDLE handle;
289         EFI_STATUS efirc;
290         int rc;
291
292         /* Attempt loading image */
293         if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, &empty_path,
294                                        user_to_virt ( image->data, 0 ),
295                                        image->len, &handle ) ) != 0 ) {
296                 /* Not an EFI image */
297                 rc = -EEFI_LOAD ( efirc );
298                 DBGC ( image, "EFIIMAGE %p could not load: %s\n",
299                        image, strerror ( rc ) );
300                 return rc;
301         }
302
303         /* Unload the image.  We can't leave it loaded, because we
304          * have no "unload" operation.
305          */
306         bs->UnloadImage ( handle );
307
308         return 0;
309 }
310
311 /** EFI image type */
312 struct image_type efi_image_type __image_type ( PROBE_NORMAL ) = {
313         .name = "EFI",
314         .probe = efi_image_probe,
315         .exec = efi_image_exec,
316 };