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., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
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>
37 FEATURE ( FEATURE_IMAGE, "EFI", DHCP_EB_FEATURE_EFI, 1 );
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 )
56 * Create device path for image
59 * @v parent Parent device path
60 * @ret path Device path, or NULL on failure
62 * The caller must eventually free() the device path.
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;
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 ) );
82 /* Allocate device path */
83 path = zalloc ( len );
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 */ ),
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 );
105 * Create command line for image
108 * @ret cmdline Command line, or NULL on failure
110 static wchar_t * efi_image_cmdline ( struct image *image ) {
114 len = ( strlen ( image->name ) +
116 ( 1 /* " " */ + strlen ( image->cmdline ) ) : 0 ) );
117 cmdline = zalloc ( ( len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
120 efi_snprintf ( cmdline, ( len + 1 /* NUL */ ), "%s%s%s",
122 ( image->cmdline ? " " : "" ),
123 ( image->cmdline ? image->cmdline : "" ) );
131 * @ret rc Return status code
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;
138 EFI_LOADED_IMAGE_PROTOCOL *image;
146 /* Find an appropriate device handle to use */
147 snpdev = last_opened_snpdev();
149 DBGC ( image, "EFIIMAGE %p could not identify SNP device\n",
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;
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;
169 /* Create device path for image */
170 path = efi_image_path ( image, snpdev->path );
172 DBGC ( image, "EFIIMAGE %p could not create device path\n",
178 /* Create command line for image */
179 cmdline = efi_image_cmdline ( image );
181 DBGC ( image, "EFIIMAGE %p could not create command line\n",
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 ) );
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 );
203 /* Should never happen */
204 rc = -EEFI ( efirc );
205 goto err_open_protocol;
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",
212 loaded.image->DeviceHandle = snpdev->handle;
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 );
221 /* Set command line */
222 loaded.image->LoadOptions = cmdline;
223 loaded.image->LoadOptionsSize =
224 ( ( wcslen ( cmdline ) + 1 /* NUL */ ) * sizeof ( wchar_t ) );
226 /* Release network devices for use via SNP */
229 /* Wrap calls made by the loaded image (for debugging) */
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;
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.
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.
261 bs->UnloadImage ( handle );
267 efi_download_uninstall ( snpdev->handle );
268 err_download_install:
269 efi_file_uninstall ( snpdev->handle );
279 * @ret rc Return status code
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 ),
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 ) );
303 /* Unload the image. We can't leave it loaded, because we
304 * have no "unload" operation.
306 bs->UnloadImage ( handle );
311 /** EFI image type */
312 struct image_type efi_image_type __image_type ( PROBE_NORMAL ) = {
314 .probe = efi_image_probe,
315 .exec = efi_image_exec,