blob: 27953fe76996ee1632e93742dfbb63b9036acea2 [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 fw_array = update_info.images;
134 *descriptor_count = num_image_type_guids;
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900135
Sughosh Ganu2a9fd7d2022-04-15 11:29:35 +0530136 total_size = sizeof(*image_info) * num_image_type_guids;
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900137
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900138 if (*image_info_size < total_size) {
139 *image_info_size = total_size;
AKASHI Takahirof4818e62020-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 Takahirof4818e62020-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 Ganu2a9fd7d2022-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 Takahirof4818e62020-11-30 18:12:12 +0900154
Sughosh Ganu2a9fd7d2022-04-15 11:29:35 +0530155 image_info[i].image_id_name = fw_array[i].fw_name;
AKASHI Takahirof4818e62020-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 Ganuc9a821b2020-12-30 19:27:10 +0530161 IMAGE_ATTRIBUTE_IMAGE_UPDATABLE |
162 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED;
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900163 image_info[i].attributes_setting =
164 IMAGE_ATTRIBUTE_IMAGE_UPDATABLE;
Sughosh Ganuc9a821b2020-12-30 19:27:10 +0530165
166 /* Check if the capsule authentication is enabled */
Sughosh Ganud1bd8492021-04-12 20:35:23 +0530167 if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE))
Sughosh Ganuc9a821b2020-12-30 19:27:10 +0530168 image_info[0].attributes_setting |=
169 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED;
170
AKASHI Takahirof4818e62020-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 Takahirof4818e62020-11-30 18:12:12 +0900176 }
177
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900178 return EFI_SUCCESS;
179}
180
AKASHI Takahiro7ff3f3c2020-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 Takahiro7ff3f3c2020-11-17 09:28:00 +0900190
AKASHI Takahirof4818e62020-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 Ganu2a9fd7d2022-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 Takahirof4818e62020-11-30 18:12:12 +0900239
240 return EFI_EXIT(ret);
241}
242
AKASHI Takahirof4818e62020-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 Schuchardt94f09e02022-02-03 20:13:17 +0100269 EFI_ENTRY("%p %d %p %zu %p %p %p\n", this, image_index, image,
AKASHI Takahirof4818e62020-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 Takahiro7ff3f3c2020-11-17 09:28:00 +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};
289#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_FIT */
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900290
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900291#ifdef CONFIG_EFI_CAPSULE_FIRMWARE_RAW
292/*
293 * This FIRMWARE_MANAGEMENT_PROTOCOL driver provides a firmware update
294 * method with raw data.
295 */
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900296
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900297/**
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 */
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900315static
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900316efi_status_t EFIAPI efi_firmware_raw_get_image_info(
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900317 struct efi_firmware_management_protocol *this,
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900318 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,
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900323 u32 *package_version,
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900324 u16 **package_version_name)
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900325{
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900326 efi_status_t ret = EFI_SUCCESS;
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900327
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900328 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 Ganu2a9fd7d2022-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 Takahiro7ff3f3c2020-11-17 09:28:00 +0900345
346 return EFI_EXIT(ret);
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900347}
348
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900349/**
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 */
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900365static
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900366efi_status_t EFIAPI efi_firmware_raw_set_image(
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900367 struct efi_firmware_management_protocol *this,
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900368 u8 image_index,
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900369 const void *image,
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900370 efi_uintn_t image_size,
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900371 const void *vendor_code,
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900372 efi_status_t (*progress)(efi_uintn_t completion),
373 u16 **abort_reason)
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900374{
Sughosh Ganu7221c6e2020-12-30 19:27:05 +0530375 u32 fmp_hdr_signature;
376 struct fmp_payload_header *header;
Sughosh Ganuc9a821b2020-12-30 19:27:10 +0530377 void *capsule_payload;
378 efi_status_t status;
379 efi_uintn_t capsule_payload_size;
Sughosh Ganu7221c6e2020-12-30 19:27:05 +0530380
Heinrich Schuchardt94f09e02022-02-03 20:13:17 +0100381 EFI_ENTRY("%p %d %p %zu %p %p %p\n", this, image_index, image,
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900382 image_size, vendor_code, progress, abort_reason);
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900383
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900384 if (!image)
385 return EFI_EXIT(EFI_INVALID_PARAMETER);
386
Sughosh Ganuc9a821b2020-12-30 19:27:10 +0530387 /* Authenticate the capsule if authentication enabled */
Sughosh Ganud1bd8492021-04-12 20:35:23 +0530388 if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE)) {
Sughosh Ganuc9a821b2020-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 Ganu7221c6e2020-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 Takahiro7ff3f3c2020-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);
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900431}
432
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900433const struct efi_firmware_management_protocol efi_fmp_raw = {
434 .get_image_info = efi_firmware_raw_get_image_info,
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900435 .get_image = efi_firmware_get_image_unsupported,
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900436 .set_image = efi_firmware_raw_set_image,
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900437 .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};
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900441#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_RAW */