blob: 27953fe76996ee1632e93742dfbb63b9036acea2 [file] [log] [blame]
AKASHI Takahirof27c2012020-11-30 18:12:12 +09001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * EFI Firmware management protocol
4 *
5 * Copyright (c) 2020 Linaro Limited
6 * Author: AKASHI Takahiro
7 */
8
9#include <common.h>
10#include <charset.h>
11#include <dfu.h>
12#include <efi_loader.h>
13#include <image.h>
Sughosh Ganu675b62e2020-12-30 19:27:05 +053014#include <signatures.h>
15
AKASHI Takahirof27c2012020-11-30 18:12:12 +090016#include <linux/list.h>
17
Sughosh Ganu675b62e2020-12-30 19:27:05 +053018#define FMP_PAYLOAD_HDR_SIGNATURE SIGNATURE_32('M', 'S', 'S', '1')
19
20/**
21 * struct fmp_payload_header - EDK2 header for the FMP payload
22 *
23 * This structure describes the header which is preprended to the
24 * FMP payload by the edk2 capsule generation scripts.
25 *
26 * @signature: Header signature used to identify the header
27 * @header_size: Size of the structure
28 * @fw_version: Firmware versions used
29 * @lowest_supported_version: Lowest supported version
30 */
31struct fmp_payload_header {
32 u32 signature;
33 u32 header_size;
34 u32 fw_version;
35 u32 lowest_supported_version;
36};
37
Sughosh Ganua9e6f012022-04-15 11:29:37 +053038__weak void set_dfu_alt_info(char *interface, char *devstr)
39{
40 env_set("dfu_alt_info", update_info.dfu_string);
41}
42
AKASHI Takahirobb7e71d2020-11-17 09:28:00 +090043/* Place holder; not supported */
44static
45efi_status_t EFIAPI efi_firmware_get_image_unsupported(
46 struct efi_firmware_management_protocol *this,
47 u8 image_index,
48 void *image,
49 efi_uintn_t *image_size)
50{
51 EFI_ENTRY("%p %d %p %p\n", this, image_index, image, image_size);
52
53 return EFI_EXIT(EFI_UNSUPPORTED);
54}
55
56/* Place holder; not supported */
57static
58efi_status_t EFIAPI efi_firmware_check_image_unsupported(
59 struct efi_firmware_management_protocol *this,
60 u8 image_index,
61 const void *image,
62 efi_uintn_t *image_size,
63 u32 *image_updatable)
64{
65 EFI_ENTRY("%p %d %p %p %p\n", this, image_index, image, image_size,
66 image_updatable);
67
68 return EFI_EXIT(EFI_UNSUPPORTED);
69}
70
71/* Place holder; not supported */
72static
73efi_status_t EFIAPI efi_firmware_get_package_info_unsupported(
74 struct efi_firmware_management_protocol *this,
75 u32 *package_version,
76 u16 **package_version_name,
77 u32 *package_version_name_maxlen,
78 u64 *attributes_supported,
79 u64 *attributes_setting)
80{
81 EFI_ENTRY("%p %p %p %p %p %p\n", this, package_version,
82 package_version_name, package_version_name_maxlen,
83 attributes_supported, attributes_setting);
84
85 return EFI_EXIT(EFI_UNSUPPORTED);
86}
87
88/* Place holder; not supported */
89static
90efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
91 struct efi_firmware_management_protocol *this,
92 const void *image,
93 efi_uintn_t *image_size,
94 const void *vendor_code,
95 u32 package_version,
96 const u16 *package_version_name)
97{
98 EFI_ENTRY("%p %p %p %p %x %p\n", this, image, image_size, vendor_code,
99 package_version, package_version_name);
100
101 return EFI_EXIT(EFI_UNSUPPORTED);
102}
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900103
104/**
Sughosh Ganu1ea06bc2022-04-15 11:29:35 +0530105 * efi_fill_image_desc_array - populate image descriptor array
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900106 * @image_info_size: Size of @image_info
107 * @image_info: Image information
108 * @descriptor_version: Pointer to version number
Sughosh Ganu1ea06bc2022-04-15 11:29:35 +0530109 * @descriptor_count: Image count
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900110 * @descriptor_size: Pointer to descriptor size
Sughosh Ganu1ea06bc2022-04-15 11:29:35 +0530111 * @package_version: Package version
112 * @package_version_name: Package version's name
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900113 *
Sughosh Ganu1ea06bc2022-04-15 11:29:35 +0530114 * Return information about the current firmware image in @image_info.
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900115 * @image_info will consist of a number of descriptors.
Sughosh Ganu1ea06bc2022-04-15 11:29:35 +0530116 * Each descriptor will be created based on efi_fw_image array.
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900117 *
118 * Return status code
119 */
Sughosh Ganu1ea06bc2022-04-15 11:29:35 +0530120static efi_status_t efi_fill_image_desc_array(
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900121 efi_uintn_t *image_info_size,
122 struct efi_firmware_image_descriptor *image_info,
123 u32 *descriptor_version,
124 u8 *descriptor_count,
125 efi_uintn_t *descriptor_size,
126 u32 *package_version,
Sughosh Ganu1ea06bc2022-04-15 11:29:35 +0530127 u16 **package_version_name)
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900128{
Sughosh Ganu1ea06bc2022-04-15 11:29:35 +0530129 size_t total_size;
130 struct efi_fw_image *fw_array;
131 int i;
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900132
Sughosh Ganu1ea06bc2022-04-15 11:29:35 +0530133 fw_array = update_info.images;
134 *descriptor_count = num_image_type_guids;
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900135
Sughosh Ganu1ea06bc2022-04-15 11:29:35 +0530136 total_size = sizeof(*image_info) * num_image_type_guids;
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900137
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900138 if (*image_info_size < total_size) {
139 *image_info_size = total_size;
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900140
141 return EFI_BUFFER_TOO_SMALL;
142 }
143 *image_info_size = total_size;
144
145 *descriptor_version = EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION;
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900146 *descriptor_size = sizeof(*image_info);
147 *package_version = 0xffffffff; /* not supported */
148 *package_version_name = NULL; /* not supported */
149
Sughosh Ganu1ea06bc2022-04-15 11:29:35 +0530150 for (i = 0; i < num_image_type_guids; i++) {
151 image_info[i].image_index = fw_array[i].image_index;
152 image_info[i].image_type_id = fw_array[i].image_type_id;
153 image_info[i].image_id = fw_array[i].image_index;
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900154
Sughosh Ganu1ea06bc2022-04-15 11:29:35 +0530155 image_info[i].image_id_name = fw_array[i].fw_name;
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900156
157 image_info[i].version = 0; /* not supported */
158 image_info[i].version_name = NULL; /* not supported */
159 image_info[i].size = 0;
160 image_info[i].attributes_supported =
Sughosh Ganu88a2ef22020-12-30 19:27:10 +0530161 IMAGE_ATTRIBUTE_IMAGE_UPDATABLE |
162 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED;
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900163 image_info[i].attributes_setting =
164 IMAGE_ATTRIBUTE_IMAGE_UPDATABLE;
Sughosh Ganu88a2ef22020-12-30 19:27:10 +0530165
166 /* Check if the capsule authentication is enabled */
Sughosh Ganu6a2e26b2021-04-12 20:35:23 +0530167 if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE))
Sughosh Ganu88a2ef22020-12-30 19:27:10 +0530168 image_info[0].attributes_setting |=
169 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED;
170
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900171 image_info[i].lowest_supported_image_version = 0;
172 image_info[i].last_attempt_version = 0;
173 image_info[i].last_attempt_status = LAST_ATTEMPT_STATUS_SUCCESS;
174 image_info[i].hardware_instance = 1;
175 image_info[i].dependencies = NULL;
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900176 }
177
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900178 return EFI_SUCCESS;
179}
180
AKASHI Takahirobb7e71d2020-11-17 09:28:00 +0900181#ifdef CONFIG_EFI_CAPSULE_FIRMWARE_FIT
182/*
183 * This FIRMWARE_MANAGEMENT_PROTOCOL driver provides a firmware update
184 * method with existing FIT image format, and handles
185 * - multiple regions of firmware via DFU
186 * but doesn't support
187 * - versioning of firmware image
188 * - package information
189 */
AKASHI Takahirobb7e71d2020-11-17 09:28:00 +0900190
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900191/**
192 * efi_firmware_fit_get_image_info - return information about the current
193 * firmware image
194 * @this: Protocol instance
195 * @image_info_size: Size of @image_info
196 * @image_info: Image information
197 * @descriptor_version: Pointer to version number
198 * @descriptor_count: Pointer to number of descriptors
199 * @descriptor_size: Pointer to descriptor size
200 * package_version: Package version
201 * package_version_name: Package version's name
202 *
203 * Return information bout the current firmware image in @image_info.
204 * @image_info will consist of a number of descriptors.
205 * Each descriptor will be created based on "dfu_alt_info" variable.
206 *
207 * Return status code
208 */
209static
210efi_status_t EFIAPI efi_firmware_fit_get_image_info(
211 struct efi_firmware_management_protocol *this,
212 efi_uintn_t *image_info_size,
213 struct efi_firmware_image_descriptor *image_info,
214 u32 *descriptor_version,
215 u8 *descriptor_count,
216 efi_uintn_t *descriptor_size,
217 u32 *package_version,
218 u16 **package_version_name)
219{
220 efi_status_t ret;
221
222 EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
223 image_info_size, image_info,
224 descriptor_version, descriptor_count, descriptor_size,
225 package_version, package_version_name);
226
227 if (!image_info_size)
228 return EFI_EXIT(EFI_INVALID_PARAMETER);
229
230 if (*image_info_size &&
231 (!image_info || !descriptor_version || !descriptor_count ||
232 !descriptor_size || !package_version || !package_version_name))
233 return EFI_EXIT(EFI_INVALID_PARAMETER);
234
Sughosh Ganu1ea06bc2022-04-15 11:29:35 +0530235 ret = efi_fill_image_desc_array(image_info_size, image_info,
236 descriptor_version, descriptor_count,
237 descriptor_size, package_version,
238 package_version_name);
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900239
240 return EFI_EXIT(ret);
241}
242
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900243/**
244 * efi_firmware_fit_set_image - update the firmware image
245 * @this: Protocol instance
246 * @image_index: Image index number
247 * @image: New image
248 * @image_size: Size of new image
249 * @vendor_code: Vendor-specific update policy
250 * @progress: Function to report the progress of update
251 * @abort_reason: Pointer to string of abort reason
252 *
253 * Update the firmware to new image, using dfu. The new image should
254 * have FIT image format commonly used in U-Boot.
255 * @vendor_code, @progress and @abort_reason are not supported.
256 *
257 * Return: status code
258 */
259static
260efi_status_t EFIAPI efi_firmware_fit_set_image(
261 struct efi_firmware_management_protocol *this,
262 u8 image_index,
263 const void *image,
264 efi_uintn_t image_size,
265 const void *vendor_code,
266 efi_status_t (*progress)(efi_uintn_t completion),
267 u16 **abort_reason)
268{
Heinrich Schuchardtb1193fa2022-02-03 20:13:17 +0100269 EFI_ENTRY("%p %d %p %zu %p %p %p\n", this, image_index, image,
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900270 image_size, vendor_code, progress, abort_reason);
271
272 if (!image || image_index != 1)
273 return EFI_EXIT(EFI_INVALID_PARAMETER);
274
275 if (fit_update(image))
276 return EFI_EXIT(EFI_DEVICE_ERROR);
277
278 return EFI_EXIT(EFI_SUCCESS);
279}
280
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900281const struct efi_firmware_management_protocol efi_fmp_fit = {
282 .get_image_info = efi_firmware_fit_get_image_info,
283 .get_image = efi_firmware_get_image_unsupported,
284 .set_image = efi_firmware_fit_set_image,
285 .check_image = efi_firmware_check_image_unsupported,
286 .get_package_info = efi_firmware_get_package_info_unsupported,
287 .set_package_info = efi_firmware_set_package_info_unsupported,
288};
AKASHI Takahirobb7e71d2020-11-17 09:28:00 +0900289#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_FIT */
290
291#ifdef CONFIG_EFI_CAPSULE_FIRMWARE_RAW
292/*
293 * This FIRMWARE_MANAGEMENT_PROTOCOL driver provides a firmware update
294 * method with raw data.
295 */
AKASHI Takahirobb7e71d2020-11-17 09:28:00 +0900296
297/**
298 * efi_firmware_raw_get_image_info - return information about the current
299 firmware image
300 * @this: Protocol instance
301 * @image_info_size: Size of @image_info
302 * @image_info: Image information
303 * @descriptor_version: Pointer to version number
304 * @descriptor_count: Pointer to number of descriptors
305 * @descriptor_size: Pointer to descriptor size
306 * package_version: Package version
307 * package_version_name: Package version's name
308 *
309 * Return information bout the current firmware image in @image_info.
310 * @image_info will consist of a number of descriptors.
311 * Each descriptor will be created based on "dfu_alt_info" variable.
312 *
313 * Return status code
314 */
315static
316efi_status_t EFIAPI efi_firmware_raw_get_image_info(
317 struct efi_firmware_management_protocol *this,
318 efi_uintn_t *image_info_size,
319 struct efi_firmware_image_descriptor *image_info,
320 u32 *descriptor_version,
321 u8 *descriptor_count,
322 efi_uintn_t *descriptor_size,
323 u32 *package_version,
324 u16 **package_version_name)
325{
326 efi_status_t ret = EFI_SUCCESS;
327
328 EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
329 image_info_size, image_info,
330 descriptor_version, descriptor_count, descriptor_size,
331 package_version, package_version_name);
332
333 if (!image_info_size)
334 return EFI_EXIT(EFI_INVALID_PARAMETER);
335
336 if (*image_info_size &&
337 (!image_info || !descriptor_version || !descriptor_count ||
338 !descriptor_size || !package_version || !package_version_name))
339 return EFI_EXIT(EFI_INVALID_PARAMETER);
340
Sughosh Ganu1ea06bc2022-04-15 11:29:35 +0530341 ret = efi_fill_image_desc_array(image_info_size, image_info,
342 descriptor_version, descriptor_count,
343 descriptor_size, package_version,
344 package_version_name);
AKASHI Takahirobb7e71d2020-11-17 09:28:00 +0900345
346 return EFI_EXIT(ret);
347}
348
349/**
350 * efi_firmware_raw_set_image - update the firmware image
351 * @this: Protocol instance
352 * @image_index: Image index number
353 * @image: New image
354 * @image_size: Size of new image
355 * @vendor_code: Vendor-specific update policy
356 * @progress: Function to report the progress of update
357 * @abort_reason: Pointer to string of abort reason
358 *
359 * Update the firmware to new image, using dfu. The new image should
360 * be a single raw image.
361 * @vendor_code, @progress and @abort_reason are not supported.
362 *
363 * Return: status code
364 */
365static
366efi_status_t EFIAPI efi_firmware_raw_set_image(
367 struct efi_firmware_management_protocol *this,
368 u8 image_index,
369 const void *image,
370 efi_uintn_t image_size,
371 const void *vendor_code,
372 efi_status_t (*progress)(efi_uintn_t completion),
373 u16 **abort_reason)
374{
Sughosh Ganu675b62e2020-12-30 19:27:05 +0530375 u32 fmp_hdr_signature;
376 struct fmp_payload_header *header;
Sughosh Ganu88a2ef22020-12-30 19:27:10 +0530377 void *capsule_payload;
378 efi_status_t status;
379 efi_uintn_t capsule_payload_size;
Sughosh Ganu675b62e2020-12-30 19:27:05 +0530380
Heinrich Schuchardtb1193fa2022-02-03 20:13:17 +0100381 EFI_ENTRY("%p %d %p %zu %p %p %p\n", this, image_index, image,
AKASHI Takahirobb7e71d2020-11-17 09:28:00 +0900382 image_size, vendor_code, progress, abort_reason);
383
384 if (!image)
385 return EFI_EXIT(EFI_INVALID_PARAMETER);
386
Sughosh Ganu88a2ef22020-12-30 19:27:10 +0530387 /* Authenticate the capsule if authentication enabled */
Sughosh Ganu6a2e26b2021-04-12 20:35:23 +0530388 if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE)) {
Sughosh Ganu88a2ef22020-12-30 19:27:10 +0530389 capsule_payload = NULL;
390 capsule_payload_size = 0;
391 status = efi_capsule_authenticate(image, image_size,
392 &capsule_payload,
393 &capsule_payload_size);
394
395 if (status == EFI_SECURITY_VIOLATION) {
396 printf("Capsule authentication check failed. Aborting update\n");
397 return EFI_EXIT(status);
398 } else if (status != EFI_SUCCESS) {
399 return EFI_EXIT(status);
400 }
401
402 debug("Capsule authentication successfull\n");
403 image = capsule_payload;
404 image_size = capsule_payload_size;
405 } else {
406 debug("Capsule authentication disabled. ");
407 debug("Updating capsule without authenticating.\n");
408 }
409
Sughosh Ganu675b62e2020-12-30 19:27:05 +0530410 fmp_hdr_signature = FMP_PAYLOAD_HDR_SIGNATURE;
411 header = (void *)image;
412
413 if (!memcmp(&header->signature, &fmp_hdr_signature,
414 sizeof(fmp_hdr_signature))) {
415 /*
416 * When building the capsule with the scripts in
417 * edk2, a FMP header is inserted above the capsule
418 * payload. Compensate for this header to get the
419 * actual payload that is to be updated.
420 */
421 image += header->header_size;
422 image_size -= header->header_size;
423
424 }
425
AKASHI Takahirobb7e71d2020-11-17 09:28:00 +0900426 if (dfu_write_by_alt(image_index - 1, (void *)image, image_size,
427 NULL, NULL))
428 return EFI_EXIT(EFI_DEVICE_ERROR);
429
430 return EFI_EXIT(EFI_SUCCESS);
431}
432
433const struct efi_firmware_management_protocol efi_fmp_raw = {
434 .get_image_info = efi_firmware_raw_get_image_info,
435 .get_image = efi_firmware_get_image_unsupported,
436 .set_image = efi_firmware_raw_set_image,
437 .check_image = efi_firmware_check_image_unsupported,
438 .get_package_info = efi_firmware_get_package_info_unsupported,
439 .set_package_info = efi_firmware_set_package_info_unsupported,
440};
441#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_RAW */