blob: 30cafd15caac00da55db063147d0bcf322768e4b [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 total_size = sizeof(*image_info) * num_image_type_guids;
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900134
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900135 if (*image_info_size < total_size) {
136 *image_info_size = total_size;
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900137
138 return EFI_BUFFER_TOO_SMALL;
139 }
140 *image_info_size = total_size;
141
Sughosh Ganu6a463bc2022-05-31 12:45:33 +0530142 fw_array = update_info.images;
143 *descriptor_count = num_image_type_guids;
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900144 *descriptor_version = EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION;
AKASHI Takahirof27c2012020-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 Ganu1ea06bc2022-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 Takahirof27c2012020-11-30 18:12:12 +0900153
Sughosh Ganu1ea06bc2022-04-15 11:29:35 +0530154 image_info[i].image_id_name = fw_array[i].fw_name;
AKASHI Takahirof27c2012020-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 Ganu88a2ef22020-12-30 19:27:10 +0530160 IMAGE_ATTRIBUTE_IMAGE_UPDATABLE |
161 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED;
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900162 image_info[i].attributes_setting =
163 IMAGE_ATTRIBUTE_IMAGE_UPDATABLE;
Sughosh Ganu88a2ef22020-12-30 19:27:10 +0530164
165 /* Check if the capsule authentication is enabled */
Sughosh Ganu6a2e26b2021-04-12 20:35:23 +0530166 if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE))
Sughosh Ganu88a2ef22020-12-30 19:27:10 +0530167 image_info[0].attributes_setting |=
168 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED;
169
AKASHI Takahirof27c2012020-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 Takahirof27c2012020-11-30 18:12:12 +0900175 }
176
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900177 return EFI_SUCCESS;
178}
179
Vincent Stehlé8645aef2022-05-31 09:55:34 +0200180/**
181 * efi_firmware_capsule_authenticate - authenticate the capsule if enabled
182 * @p_image: Pointer to new image
183 * @p_image_size: Pointer to size of new image
184 *
185 * Authenticate the capsule if authentication is enabled.
186 * The image pointer and the image size are updated in case of success.
187 *
188 * Return: status code
189 */
190static
191efi_status_t efi_firmware_capsule_authenticate(const void **p_image,
192 efi_uintn_t *p_image_size)
193{
194 const void *image = *p_image;
195 efi_uintn_t image_size = *p_image_size;
196 u32 fmp_hdr_signature;
197 struct fmp_payload_header *header;
198 void *capsule_payload;
199 efi_status_t status;
200 efi_uintn_t capsule_payload_size;
201
202 if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE)) {
203 capsule_payload = NULL;
204 capsule_payload_size = 0;
205 status = efi_capsule_authenticate(image, image_size,
206 &capsule_payload,
207 &capsule_payload_size);
208
209 if (status == EFI_SECURITY_VIOLATION) {
210 printf("Capsule authentication check failed. Aborting update\n");
211 return status;
212 } else if (status != EFI_SUCCESS) {
213 return status;
214 }
215
216 debug("Capsule authentication successful\n");
217 image = capsule_payload;
218 image_size = capsule_payload_size;
219 } else {
220 debug("Capsule authentication disabled. ");
221 debug("Updating capsule without authenticating.\n");
222 }
223
224 fmp_hdr_signature = FMP_PAYLOAD_HDR_SIGNATURE;
225 header = (void *)image;
226
227 if (!memcmp(&header->signature, &fmp_hdr_signature,
228 sizeof(fmp_hdr_signature))) {
229 /*
230 * When building the capsule with the scripts in
231 * edk2, a FMP header is inserted above the capsule
232 * payload. Compensate for this header to get the
233 * actual payload that is to be updated.
234 */
235 image += header->header_size;
236 image_size -= header->header_size;
237 }
238
239 *p_image = image;
240 *p_image_size = image_size;
241 return EFI_SUCCESS;
242}
243
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900244/**
Sughosh Ganu556a1262022-06-01 23:30:41 +0530245 * efi_firmware_get_image_info - return information about the current
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900246 * firmware image
247 * @this: Protocol instance
248 * @image_info_size: Size of @image_info
249 * @image_info: Image information
250 * @descriptor_version: Pointer to version number
251 * @descriptor_count: Pointer to number of descriptors
252 * @descriptor_size: Pointer to descriptor size
Vincent Stehlé7751d2e2022-05-25 11:20:22 +0200253 * @package_version: Package version
254 * @package_version_name: Package version's name
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900255 *
256 * Return information bout the current firmware image in @image_info.
257 * @image_info will consist of a number of descriptors.
258 * Each descriptor will be created based on "dfu_alt_info" variable.
259 *
260 * Return status code
261 */
262static
Sughosh Ganu556a1262022-06-01 23:30:41 +0530263efi_status_t EFIAPI efi_firmware_get_image_info(
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900264 struct efi_firmware_management_protocol *this,
265 efi_uintn_t *image_info_size,
266 struct efi_firmware_image_descriptor *image_info,
267 u32 *descriptor_version,
268 u8 *descriptor_count,
269 efi_uintn_t *descriptor_size,
270 u32 *package_version,
271 u16 **package_version_name)
272{
273 efi_status_t ret;
274
275 EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
276 image_info_size, image_info,
277 descriptor_version, descriptor_count, descriptor_size,
278 package_version, package_version_name);
279
280 if (!image_info_size)
281 return EFI_EXIT(EFI_INVALID_PARAMETER);
282
283 if (*image_info_size &&
284 (!image_info || !descriptor_version || !descriptor_count ||
285 !descriptor_size || !package_version || !package_version_name))
286 return EFI_EXIT(EFI_INVALID_PARAMETER);
287
Sughosh Ganu1ea06bc2022-04-15 11:29:35 +0530288 ret = efi_fill_image_desc_array(image_info_size, image_info,
289 descriptor_version, descriptor_count,
290 descriptor_size, package_version,
291 package_version_name);
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900292
293 return EFI_EXIT(ret);
294}
295
Sughosh Ganu556a1262022-06-01 23:30:41 +0530296#ifdef CONFIG_EFI_CAPSULE_FIRMWARE_FIT
297/*
298 * This FIRMWARE_MANAGEMENT_PROTOCOL driver provides a firmware update
299 * method with existing FIT image format, and handles
300 * - multiple regions of firmware via DFU
301 * but doesn't support
302 * - versioning of firmware image
303 * - package information
304 */
305
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900306/**
307 * efi_firmware_fit_set_image - update the firmware image
308 * @this: Protocol instance
309 * @image_index: Image index number
310 * @image: New image
311 * @image_size: Size of new image
312 * @vendor_code: Vendor-specific update policy
313 * @progress: Function to report the progress of update
314 * @abort_reason: Pointer to string of abort reason
315 *
316 * Update the firmware to new image, using dfu. The new image should
317 * have FIT image format commonly used in U-Boot.
318 * @vendor_code, @progress and @abort_reason are not supported.
319 *
320 * Return: status code
321 */
322static
323efi_status_t EFIAPI efi_firmware_fit_set_image(
324 struct efi_firmware_management_protocol *this,
325 u8 image_index,
326 const void *image,
327 efi_uintn_t image_size,
328 const void *vendor_code,
329 efi_status_t (*progress)(efi_uintn_t completion),
330 u16 **abort_reason)
331{
Vincent Stehlé8645aef2022-05-31 09:55:34 +0200332 efi_status_t status;
333
Heinrich Schuchardtb1193fa2022-02-03 20:13:17 +0100334 EFI_ENTRY("%p %d %p %zu %p %p %p\n", this, image_index, image,
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900335 image_size, vendor_code, progress, abort_reason);
336
337 if (!image || image_index != 1)
338 return EFI_EXIT(EFI_INVALID_PARAMETER);
339
Vincent Stehlé8645aef2022-05-31 09:55:34 +0200340 status = efi_firmware_capsule_authenticate(&image, &image_size);
341 if (status != EFI_SUCCESS)
342 return EFI_EXIT(status);
343
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900344 if (fit_update(image))
345 return EFI_EXIT(EFI_DEVICE_ERROR);
346
347 return EFI_EXIT(EFI_SUCCESS);
348}
349
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900350const struct efi_firmware_management_protocol efi_fmp_fit = {
Sughosh Ganu556a1262022-06-01 23:30:41 +0530351 .get_image_info = efi_firmware_get_image_info,
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900352 .get_image = efi_firmware_get_image_unsupported,
353 .set_image = efi_firmware_fit_set_image,
354 .check_image = efi_firmware_check_image_unsupported,
355 .get_package_info = efi_firmware_get_package_info_unsupported,
356 .set_package_info = efi_firmware_set_package_info_unsupported,
357};
AKASHI Takahirobb7e71d2020-11-17 09:28:00 +0900358#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_FIT */
359
360#ifdef CONFIG_EFI_CAPSULE_FIRMWARE_RAW
361/*
362 * This FIRMWARE_MANAGEMENT_PROTOCOL driver provides a firmware update
363 * method with raw data.
364 */
AKASHI Takahirobb7e71d2020-11-17 09:28:00 +0900365
366/**
AKASHI Takahirobb7e71d2020-11-17 09:28:00 +0900367 * efi_firmware_raw_set_image - update the firmware image
368 * @this: Protocol instance
369 * @image_index: Image index number
370 * @image: New image
371 * @image_size: Size of new image
372 * @vendor_code: Vendor-specific update policy
373 * @progress: Function to report the progress of update
374 * @abort_reason: Pointer to string of abort reason
375 *
376 * Update the firmware to new image, using dfu. The new image should
377 * be a single raw image.
378 * @vendor_code, @progress and @abort_reason are not supported.
379 *
380 * Return: status code
381 */
382static
383efi_status_t EFIAPI efi_firmware_raw_set_image(
384 struct efi_firmware_management_protocol *this,
385 u8 image_index,
386 const void *image,
387 efi_uintn_t image_size,
388 const void *vendor_code,
389 efi_status_t (*progress)(efi_uintn_t completion),
390 u16 **abort_reason)
391{
Sughosh Ganu88a2ef22020-12-30 19:27:10 +0530392 efi_status_t status;
Sughosh Ganu675b62e2020-12-30 19:27:05 +0530393
Heinrich Schuchardtb1193fa2022-02-03 20:13:17 +0100394 EFI_ENTRY("%p %d %p %zu %p %p %p\n", this, image_index, image,
AKASHI Takahirobb7e71d2020-11-17 09:28:00 +0900395 image_size, vendor_code, progress, abort_reason);
396
397 if (!image)
398 return EFI_EXIT(EFI_INVALID_PARAMETER);
399
Vincent Stehlé8645aef2022-05-31 09:55:34 +0200400 status = efi_firmware_capsule_authenticate(&image, &image_size);
401 if (status != EFI_SUCCESS)
402 return EFI_EXIT(status);
Sughosh Ganu675b62e2020-12-30 19:27:05 +0530403
AKASHI Takahirobb7e71d2020-11-17 09:28:00 +0900404 if (dfu_write_by_alt(image_index - 1, (void *)image, image_size,
405 NULL, NULL))
406 return EFI_EXIT(EFI_DEVICE_ERROR);
407
408 return EFI_EXIT(EFI_SUCCESS);
409}
410
411const struct efi_firmware_management_protocol efi_fmp_raw = {
Sughosh Ganu556a1262022-06-01 23:30:41 +0530412 .get_image_info = efi_firmware_get_image_info,
AKASHI Takahirobb7e71d2020-11-17 09:28:00 +0900413 .get_image = efi_firmware_get_image_unsupported,
414 .set_image = efi_firmware_raw_set_image,
415 .check_image = efi_firmware_check_image_unsupported,
416 .get_package_info = efi_firmware_get_package_info_unsupported,
417 .set_package_info = efi_firmware_set_package_info_unsupported,
418};
419#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_RAW */