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