blob: 9cdefab41fa18cfebb76f11ad8aedb1325294f4f [file] [log] [blame]
AKASHI Takahirof4818e62020-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 Ganu7221c6e2020-12-30 19:27:05 +053014#include <signatures.h>
15
AKASHI Takahirof4818e62020-11-30 18:12:12 +090016#include <linux/list.h>
17
Sughosh Ganu7221c6e2020-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 Ganua1d9f672022-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 Takahiro7ff3f3c2020-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 Takahirof4818e62020-11-30 18:12:12 +0900103
104/**
Sughosh Ganu2a9fd7d2022-04-15 11:29:35 +0530105 * efi_fill_image_desc_array - populate image descriptor array
AKASHI Takahirof4818e62020-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 Ganu2a9fd7d2022-04-15 11:29:35 +0530109 * @descriptor_count: Image count
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900110 * @descriptor_size: Pointer to descriptor size
Sughosh Ganu2a9fd7d2022-04-15 11:29:35 +0530111 * @package_version: Package version
112 * @package_version_name: Package version's name
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900113 *
Sughosh Ganu2a9fd7d2022-04-15 11:29:35 +0530114 * Return information about the current firmware image in @image_info.
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900115 * @image_info will consist of a number of descriptors.
Sughosh Ganu2a9fd7d2022-04-15 11:29:35 +0530116 * Each descriptor will be created based on efi_fw_image array.
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900117 *
118 * Return status code
119 */
Sughosh Ganu2a9fd7d2022-04-15 11:29:35 +0530120static efi_status_t efi_fill_image_desc_array(
AKASHI Takahirof4818e62020-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 Ganu2a9fd7d2022-04-15 11:29:35 +0530127 u16 **package_version_name)
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900128{
Sughosh Ganu2a9fd7d2022-04-15 11:29:35 +0530129 size_t total_size;
130 struct efi_fw_image *fw_array;
131 int i;
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900132
Sughosh Ganu2a9fd7d2022-04-15 11:29:35 +0530133 total_size = sizeof(*image_info) * num_image_type_guids;
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900134
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900135 if (*image_info_size < total_size) {
136 *image_info_size = total_size;
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900137
138 return EFI_BUFFER_TOO_SMALL;
139 }
140 *image_info_size = total_size;
141
Sughosh Ganuc212fc72022-05-31 12:45:33 +0530142 fw_array = update_info.images;
143 *descriptor_count = num_image_type_guids;
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900144 *descriptor_version = EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION;
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900145 *descriptor_size = sizeof(*image_info);
146 *package_version = 0xffffffff; /* not supported */
147 *package_version_name = NULL; /* not supported */
148
Sughosh Ganu2a9fd7d2022-04-15 11:29:35 +0530149 for (i = 0; i < num_image_type_guids; i++) {
150 image_info[i].image_index = fw_array[i].image_index;
151 image_info[i].image_type_id = fw_array[i].image_type_id;
152 image_info[i].image_id = fw_array[i].image_index;
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900153
Sughosh Ganu2a9fd7d2022-04-15 11:29:35 +0530154 image_info[i].image_id_name = fw_array[i].fw_name;
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900155
156 image_info[i].version = 0; /* not supported */
157 image_info[i].version_name = NULL; /* not supported */
158 image_info[i].size = 0;
159 image_info[i].attributes_supported =
Sughosh Ganuc9a821b2020-12-30 19:27:10 +0530160 IMAGE_ATTRIBUTE_IMAGE_UPDATABLE |
161 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED;
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900162 image_info[i].attributes_setting =
163 IMAGE_ATTRIBUTE_IMAGE_UPDATABLE;
Sughosh Ganuc9a821b2020-12-30 19:27:10 +0530164
165 /* Check if the capsule authentication is enabled */
Sughosh Ganud1bd8492021-04-12 20:35:23 +0530166 if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE))
Sughosh Ganuc9a821b2020-12-30 19:27:10 +0530167 image_info[0].attributes_setting |=
168 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED;
169
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900170 image_info[i].lowest_supported_image_version = 0;
171 image_info[i].last_attempt_version = 0;
172 image_info[i].last_attempt_status = LAST_ATTEMPT_STATUS_SUCCESS;
173 image_info[i].hardware_instance = 1;
174 image_info[i].dependencies = NULL;
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900175 }
176
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900177 return EFI_SUCCESS;
178}
179
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900180#ifdef CONFIG_EFI_CAPSULE_FIRMWARE_FIT
181/*
182 * This FIRMWARE_MANAGEMENT_PROTOCOL driver provides a firmware update
183 * method with existing FIT image format, and handles
184 * - multiple regions of firmware via DFU
185 * but doesn't support
186 * - versioning of firmware image
187 * - package information
188 */
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900189
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900190/**
191 * efi_firmware_fit_get_image_info - return information about the current
192 * firmware image
193 * @this: Protocol instance
194 * @image_info_size: Size of @image_info
195 * @image_info: Image information
196 * @descriptor_version: Pointer to version number
197 * @descriptor_count: Pointer to number of descriptors
198 * @descriptor_size: Pointer to descriptor size
Vincent Stehlé0f3c9222022-05-25 11:20:22 +0200199 * @package_version: Package version
200 * @package_version_name: Package version's name
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900201 *
202 * Return information bout the current firmware image in @image_info.
203 * @image_info will consist of a number of descriptors.
204 * Each descriptor will be created based on "dfu_alt_info" variable.
205 *
206 * Return status code
207 */
208static
209efi_status_t EFIAPI efi_firmware_fit_get_image_info(
210 struct efi_firmware_management_protocol *this,
211 efi_uintn_t *image_info_size,
212 struct efi_firmware_image_descriptor *image_info,
213 u32 *descriptor_version,
214 u8 *descriptor_count,
215 efi_uintn_t *descriptor_size,
216 u32 *package_version,
217 u16 **package_version_name)
218{
219 efi_status_t ret;
220
221 EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
222 image_info_size, image_info,
223 descriptor_version, descriptor_count, descriptor_size,
224 package_version, package_version_name);
225
226 if (!image_info_size)
227 return EFI_EXIT(EFI_INVALID_PARAMETER);
228
229 if (*image_info_size &&
230 (!image_info || !descriptor_version || !descriptor_count ||
231 !descriptor_size || !package_version || !package_version_name))
232 return EFI_EXIT(EFI_INVALID_PARAMETER);
233
Sughosh Ganu2a9fd7d2022-04-15 11:29:35 +0530234 ret = efi_fill_image_desc_array(image_info_size, image_info,
235 descriptor_version, descriptor_count,
236 descriptor_size, package_version,
237 package_version_name);
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900238
239 return EFI_EXIT(ret);
240}
241
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900242/**
243 * efi_firmware_fit_set_image - update the firmware image
244 * @this: Protocol instance
245 * @image_index: Image index number
246 * @image: New image
247 * @image_size: Size of new image
248 * @vendor_code: Vendor-specific update policy
249 * @progress: Function to report the progress of update
250 * @abort_reason: Pointer to string of abort reason
251 *
252 * Update the firmware to new image, using dfu. The new image should
253 * have FIT image format commonly used in U-Boot.
254 * @vendor_code, @progress and @abort_reason are not supported.
255 *
256 * Return: status code
257 */
258static
259efi_status_t EFIAPI efi_firmware_fit_set_image(
260 struct efi_firmware_management_protocol *this,
261 u8 image_index,
262 const void *image,
263 efi_uintn_t image_size,
264 const void *vendor_code,
265 efi_status_t (*progress)(efi_uintn_t completion),
266 u16 **abort_reason)
267{
Heinrich Schuchardt94f09e02022-02-03 20:13:17 +0100268 EFI_ENTRY("%p %d %p %zu %p %p %p\n", this, image_index, image,
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900269 image_size, vendor_code, progress, abort_reason);
270
271 if (!image || image_index != 1)
272 return EFI_EXIT(EFI_INVALID_PARAMETER);
273
274 if (fit_update(image))
275 return EFI_EXIT(EFI_DEVICE_ERROR);
276
277 return EFI_EXIT(EFI_SUCCESS);
278}
279
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900280const struct efi_firmware_management_protocol efi_fmp_fit = {
281 .get_image_info = efi_firmware_fit_get_image_info,
282 .get_image = efi_firmware_get_image_unsupported,
283 .set_image = efi_firmware_fit_set_image,
284 .check_image = efi_firmware_check_image_unsupported,
285 .get_package_info = efi_firmware_get_package_info_unsupported,
286 .set_package_info = efi_firmware_set_package_info_unsupported,
287};
288#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_FIT */
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900289
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900290#ifdef CONFIG_EFI_CAPSULE_FIRMWARE_RAW
291/*
292 * This FIRMWARE_MANAGEMENT_PROTOCOL driver provides a firmware update
293 * method with raw data.
294 */
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900295
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900296/**
297 * efi_firmware_raw_get_image_info - return information about the current
Vincent Stehlé0f3c9222022-05-25 11:20:22 +0200298 * firmware image
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900299 * @this: Protocol instance
300 * @image_info_size: Size of @image_info
301 * @image_info: Image information
302 * @descriptor_version: Pointer to version number
303 * @descriptor_count: Pointer to number of descriptors
304 * @descriptor_size: Pointer to descriptor size
Vincent Stehlé0f3c9222022-05-25 11:20:22 +0200305 * @package_version: Package version
306 * @package_version_name: Package version's name
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900307 *
308 * Return information bout the current firmware image in @image_info.
309 * @image_info will consist of a number of descriptors.
310 * Each descriptor will be created based on "dfu_alt_info" variable.
311 *
312 * Return status code
313 */
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900314static
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900315efi_status_t EFIAPI efi_firmware_raw_get_image_info(
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900316 struct efi_firmware_management_protocol *this,
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900317 efi_uintn_t *image_info_size,
318 struct efi_firmware_image_descriptor *image_info,
319 u32 *descriptor_version,
320 u8 *descriptor_count,
321 efi_uintn_t *descriptor_size,
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900322 u32 *package_version,
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900323 u16 **package_version_name)
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900324{
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900325 efi_status_t ret = EFI_SUCCESS;
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900326
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900327 EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
328 image_info_size, image_info,
329 descriptor_version, descriptor_count, descriptor_size,
330 package_version, package_version_name);
331
332 if (!image_info_size)
333 return EFI_EXIT(EFI_INVALID_PARAMETER);
334
335 if (*image_info_size &&
336 (!image_info || !descriptor_version || !descriptor_count ||
337 !descriptor_size || !package_version || !package_version_name))
338 return EFI_EXIT(EFI_INVALID_PARAMETER);
339
Sughosh Ganu2a9fd7d2022-04-15 11:29:35 +0530340 ret = efi_fill_image_desc_array(image_info_size, image_info,
341 descriptor_version, descriptor_count,
342 descriptor_size, package_version,
343 package_version_name);
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900344
345 return EFI_EXIT(ret);
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900346}
347
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900348/**
349 * efi_firmware_raw_set_image - update the firmware image
350 * @this: Protocol instance
351 * @image_index: Image index number
352 * @image: New image
353 * @image_size: Size of new image
354 * @vendor_code: Vendor-specific update policy
355 * @progress: Function to report the progress of update
356 * @abort_reason: Pointer to string of abort reason
357 *
358 * Update the firmware to new image, using dfu. The new image should
359 * be a single raw image.
360 * @vendor_code, @progress and @abort_reason are not supported.
361 *
362 * Return: status code
363 */
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900364static
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900365efi_status_t EFIAPI efi_firmware_raw_set_image(
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900366 struct efi_firmware_management_protocol *this,
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900367 u8 image_index,
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900368 const void *image,
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900369 efi_uintn_t image_size,
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900370 const void *vendor_code,
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900371 efi_status_t (*progress)(efi_uintn_t completion),
372 u16 **abort_reason)
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900373{
Sughosh Ganu7221c6e2020-12-30 19:27:05 +0530374 u32 fmp_hdr_signature;
375 struct fmp_payload_header *header;
Sughosh Ganuc9a821b2020-12-30 19:27:10 +0530376 void *capsule_payload;
377 efi_status_t status;
378 efi_uintn_t capsule_payload_size;
Sughosh Ganu7221c6e2020-12-30 19:27:05 +0530379
Heinrich Schuchardt94f09e02022-02-03 20:13:17 +0100380 EFI_ENTRY("%p %d %p %zu %p %p %p\n", this, image_index, image,
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900381 image_size, vendor_code, progress, abort_reason);
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900382
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900383 if (!image)
384 return EFI_EXIT(EFI_INVALID_PARAMETER);
385
Sughosh Ganuc9a821b2020-12-30 19:27:10 +0530386 /* Authenticate the capsule if authentication enabled */
Sughosh Ganud1bd8492021-04-12 20:35:23 +0530387 if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE)) {
Sughosh Ganuc9a821b2020-12-30 19:27:10 +0530388 capsule_payload = NULL;
389 capsule_payload_size = 0;
390 status = efi_capsule_authenticate(image, image_size,
391 &capsule_payload,
392 &capsule_payload_size);
393
394 if (status == EFI_SECURITY_VIOLATION) {
395 printf("Capsule authentication check failed. Aborting update\n");
396 return EFI_EXIT(status);
397 } else if (status != EFI_SUCCESS) {
398 return EFI_EXIT(status);
399 }
400
401 debug("Capsule authentication successfull\n");
402 image = capsule_payload;
403 image_size = capsule_payload_size;
404 } else {
405 debug("Capsule authentication disabled. ");
406 debug("Updating capsule without authenticating.\n");
407 }
408
Sughosh Ganu7221c6e2020-12-30 19:27:05 +0530409 fmp_hdr_signature = FMP_PAYLOAD_HDR_SIGNATURE;
410 header = (void *)image;
411
412 if (!memcmp(&header->signature, &fmp_hdr_signature,
413 sizeof(fmp_hdr_signature))) {
414 /*
415 * When building the capsule with the scripts in
416 * edk2, a FMP header is inserted above the capsule
417 * payload. Compensate for this header to get the
418 * actual payload that is to be updated.
419 */
420 image += header->header_size;
421 image_size -= header->header_size;
422
423 }
424
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900425 if (dfu_write_by_alt(image_index - 1, (void *)image, image_size,
426 NULL, NULL))
427 return EFI_EXIT(EFI_DEVICE_ERROR);
428
429 return EFI_EXIT(EFI_SUCCESS);
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900430}
431
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900432const struct efi_firmware_management_protocol efi_fmp_raw = {
433 .get_image_info = efi_firmware_raw_get_image_info,
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900434 .get_image = efi_firmware_get_image_unsupported,
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900435 .set_image = efi_firmware_raw_set_image,
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900436 .check_image = efi_firmware_check_image_unsupported,
437 .get_package_info = efi_firmware_get_package_info_unsupported,
438 .set_package_info = efi_firmware_set_package_info_unsupported,
439};
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900440#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_RAW */