1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (c) 2022, Linaro Limited
12 #include <linux/types.h>
17 struct fwu_mdata_gpt_blk_priv {
18 struct udevice *blk_dev;
22 * @mdata_check: check the validity of the FWU metadata partitions
23 * @get_mdata() - Get a FWU metadata copy
24 * @update_mdata() - Update the FWU metadata copy
26 struct fwu_mdata_ops {
28 * check_mdata() - Check if the FWU metadata is valid
31 * Validate both copies of the FWU metadata. If one of the copies
32 * has gone bad, restore it from the other copy.
34 * Return: 0 if OK, -ve on error
36 int (*check_mdata)(struct udevice *dev);
39 * get_mdata() - Get a FWU metadata copy
41 * @mdata: Pointer to FWU metadata
43 * Get a valid copy of the FWU metadata.
45 * Return: 0 if OK, -ve on error
47 int (*get_mdata)(struct udevice *dev, struct fwu_mdata *mdata);
50 * update_mdata() - Update the FWU metadata
52 * @mdata: Copy of the FWU metadata
54 * Update the FWU metadata structure by writing to the
55 * FWU metadata partitions.
57 * Return: 0 if OK, -ve on error
59 int (*update_mdata)(struct udevice *dev, struct fwu_mdata *mdata);
62 * get_mdata_part_num() - Get the FWU metadata partition numbers
63 * @dev: FWU metadata device
64 * @mdata_parts: array for storing the metadata partition numbers
66 * Get the partition numbers on the storage device on which the
67 * FWU metadata is stored. Two partition numbers will be returned.
69 * Return: 0 if OK, -ve on error
71 int (*get_mdata_part_num)(struct udevice *dev, uint *mdata_parts);
74 * read_mdata_partition() - Read the FWU metadata from a partition
75 * @dev: FWU metadata device
76 * @mdata: Copy of the FWU metadata
77 * @part_num: Partition number from which FWU metadata is to be read
79 * Read the FWU metadata from the specified partition number
81 * Return: 0 if OK, -ve on error
83 int (*read_mdata_partition)(struct udevice *dev,
84 struct fwu_mdata *mdata, uint part_num);
87 * write_mdata_partition() - Write the FWU metadata to a partition
88 * @dev: FWU metadata device
89 * @mdata: Copy of the FWU metadata
90 * @part_num: Partition number to which FWU metadata is to be written
92 * Write the FWU metadata to the specified partition number
94 * Return: 0 if OK, -ve on error
96 int (*write_mdata_partition)(struct udevice *dev,
97 struct fwu_mdata *mdata, uint part_num);
100 #define FWU_MDATA_VERSION 0x1
101 #define FWU_IMAGE_ACCEPTED 0x1
104 * GUID value defined in the FWU specification for identification
105 * of the FWU metadata partition.
107 #define FWU_MDATA_GUID \
108 EFI_GUID(0x8a7a84a0, 0x8387, 0x40f6, 0xab, 0x41, \
109 0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23)
112 * GUID value defined in the Dependable Boot specification for
113 * identification of the revert capsule, used for reverting
114 * any image in the updated bank.
116 #define FWU_OS_REQUEST_FW_REVERT_GUID \
117 EFI_GUID(0xacd58b4b, 0xc0e8, 0x475f, 0x99, 0xb5, \
118 0x6b, 0x3f, 0x7e, 0x07, 0xaa, 0xf0)
121 * GUID value defined in the Dependable Boot specification for
122 * identification of the accept capsule, used for accepting
123 * an image in the updated bank.
125 #define FWU_OS_REQUEST_FW_ACCEPT_GUID \
126 EFI_GUID(0x0c996046, 0xbcc0, 0x4d04, 0x85, 0xec, \
127 0xe1, 0xfc, 0xed, 0xf1, 0xc6, 0xf8)
130 * fwu_check_mdata_validity() - Check for validity of the FWU metadata copies
132 * Read both the metadata copies from the storage media, verify their
133 * checksum, and ascertain that both copies match. If one of the copies
134 * has gone bad, restore it from the good copy.
136 * Return: 0 if OK, -ve on error
139 int fwu_check_mdata_validity(void);
142 * fwu_get_mdata_part_num() - Get the FWU metadata partition numbers
143 * @dev: FWU metadata device
144 * @mdata_parts: array for storing the metadata partition numbers
146 * Get the partition numbers on the storage device on which the
147 * FWU metadata is stored. Two partition numbers will be returned
150 * Return: 0 if OK, -ve on error
153 int fwu_get_mdata_part_num(struct udevice *dev, uint *mdata_parts);
156 * fwu_read_mdata_partition() - Read the FWU metadata from a partition
157 * @dev: FWU metadata device
158 * @mdata: Copy of the FWU metadata
159 * @part_num: Partition number from which FWU metadata is to be read
161 * Read the FWU metadata from the specified partition number
163 * Return: 0 if OK, -ve on error
166 int fwu_read_mdata_partition(struct udevice *dev, struct fwu_mdata *mdata,
170 * fwu_write_mdata_partition() - Write the FWU metadata to a partition
171 * @dev: FWU metadata device
172 * @mdata: Copy of the FWU metadata
173 * @part_num: Partition number to which FWU metadata is to be written
175 * Write the FWU metadata to the specified partition number
177 * Return: 0 if OK, -ve on error
180 int fwu_write_mdata_partition(struct udevice *dev, struct fwu_mdata *mdata,
184 * fwu_get_mdata() - Get a FWU metadata copy
185 * @dev: FWU metadata device
186 * @mdata: Copy of the FWU metadata
188 * Get a valid copy of the FWU metadata.
190 * Note: This function is to be called first when modifying any fields
191 * in the metadata. The sequence of calls to modify any field in the
192 * metadata would be 1) fwu_get_mdata 2) Modify metadata, followed by
193 * 3) fwu_update_mdata
195 * Return: 0 if OK, -ve on error
198 int fwu_get_mdata(struct udevice *dev, struct fwu_mdata *mdata);
201 * fwu_update_mdata() - Update the FWU metadata
202 * @dev: FWU metadata device
203 * @mdata: Copy of the FWU metadata
205 * Update the FWU metadata structure by writing to the
206 * FWU metadata partitions.
208 * Note: This function is not to be called directly to update the
209 * metadata fields. The sequence of function calls should be
210 * 1) fwu_get_mdata() 2) Modify the medata fields 3) fwu_update_mdata()
212 * The sequence of updating the partitions should be, update the
213 * primary metadata partition (first partition encountered), followed
214 * by updating the secondary partition. With this update sequence, in
215 * the rare scenario that the two metadata partitions are valid but do
216 * not match, maybe due to power outage at the time of updating the
217 * metadata copies, the secondary partition can be updated from the
220 * Return: 0 if OK, -ve on error
223 int fwu_update_mdata(struct udevice *dev, struct fwu_mdata *mdata);
226 * fwu_get_active_index() - Get active_index from the FWU metadata
227 * @active_idxp: active_index value to be read
229 * Read the active_index field from the FWU metadata and place it in
230 * the variable pointed to be the function argument.
232 * Return: 0 if OK, -ve on error
235 int fwu_get_active_index(uint *active_idxp);
238 * fwu_set_active_index() - Set active_index in the FWU metadata
239 * @active_idx: active_index value to be set
241 * Update the active_index field in the FWU metadata
243 * Return: 0 if OK, -ve on error
246 int fwu_set_active_index(uint active_idx);
249 * fwu_get_image_index() - Get the Image Index to be used for capsule update
250 * @image_index: The Image Index for the image
252 * The FWU multi bank update feature computes the value of image_index at
253 * runtime, based on the bank to which the image needs to be written to.
254 * Derive the image_index value for the image.
256 * Currently, the capsule update driver uses the DFU framework for
257 * the updates. This function gets the DFU alt number which is to
258 * be used as the Image Index
260 * Return: 0 if OK, -ve on error
263 int fwu_get_image_index(u8 *image_index);
266 * fwu_mdata_check() - Check if the FWU metadata is valid
267 * @dev: FWU metadata device
269 * Validate both copies of the FWU metadata. If one of the copies
270 * has gone bad, restore it from the other copy.
272 * Return: 0 if OK, -ve on error
275 int fwu_mdata_check(struct udevice *dev);
278 * fwu_revert_boot_index() - Revert the active index in the FWU metadata
280 * Revert the active_index value in the FWU metadata, by swapping the values
281 * of active_index and previous_active_index in both copies of the
284 * Return: 0 if OK, -ve on error
287 int fwu_revert_boot_index(void);
290 * fwu_verify_mdata() - Verify the FWU metadata
291 * @mdata: FWU metadata structure
292 * @pri_part: FWU metadata partition is primary or secondary
294 * Verify the FWU metadata by computing the CRC32 for the metadata
295 * structure and comparing it against the CRC32 value stored as part
298 * Return: 0 if OK, -ve on error
301 int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part);
304 * fwu_accept_image() - Set the Acceptance bit for the image
305 * @img_type_id: GUID of the image type for which the accepted bit is to be
307 * @bank: Bank of which the image's Accept bit is to be set
309 * Set the accepted bit for the image specified by the img_guid parameter. This
310 * indicates acceptance of image for subsequent boots by some governing component
311 * like OS(or firmware).
313 * Return: 0 if OK, -ve on error
316 int fwu_accept_image(efi_guid_t *img_type_id, u32 bank);
319 * fwu_clear_accept_image() - Clear the Acceptance bit for the image
320 * @img_type_id: GUID of the image type for which the accepted bit is to be
322 * @bank: Bank of which the image's Accept bit is to be cleared
324 * Clear the accepted bit for the image type specified by the img_type_id parameter.
325 * This function is called after the image has been updated. The accepted bit is
326 * cleared to be set subsequently after passing the image acceptance criteria, by
327 * either the OS(or firmware)
329 * Return: 0 if OK, -ve on error
332 int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank);
335 * fwu_plat_get_alt_num() - Get the DFU Alt Num for the image from the platform
337 * @image_guid: Image GUID for which DFU alt number needs to be retrieved
338 * @alt_num: Pointer to the alt_num
340 * Get the DFU alt number from the platform for the image specified by the
343 * Return: 0 if OK, -ve on error
346 int fwu_plat_get_alt_num(struct udevice *dev, efi_guid_t *image_guid,
350 * fwu_plat_get_update_index() - Get the value of the update bank
351 * @update_idx: Bank number to which images are to be updated
353 * Get the value of the bank(partition) to which the update needs to be
356 * Note: This is a weak function and platforms can override this with
357 * their own implementation for selection of the update bank.
359 * Return: 0 if OK, -ve on error
362 int fwu_plat_get_update_index(uint *update_idx);
365 * fwu_plat_get_bootidx() - Get the value of the boot index
366 * @boot_idx: Boot index value
368 * Get the value of the bank(partition) from which the platform
369 * has booted. This value is passed to U-Boot from the earlier
370 * stage bootloader which loads and boots all the relevant
374 void fwu_plat_get_bootidx(uint *boot_idx);
377 * fwu_update_checks_pass() - Check if FWU update can be done
379 * Check if the FWU update can be executed. The updates are
380 * allowed only when the platform is not in Trial State and
381 * the boot time checks have passed
383 * Return: 1 if OK, 0 if checks do not pass
386 u8 fwu_update_checks_pass(void);
389 * fwu_empty_capsule_checks_pass() - Check if empty capsule can be processed
391 * Check if the empty capsule can be processed to either accept or revert
392 * an earlier executed update. The empty capsules need to be processed
393 * only when the platform is in Trial State and the boot time checks have
396 * Return: 1 if OK, 0 if not to be allowed
399 u8 fwu_empty_capsule_checks_pass(void);
402 * fwu_trial_state_ctr_start() - Start the Trial State counter
404 * Start the counter to identify the platform booting in the
405 * Trial State. The counter is implemented as an EFI variable.
407 * Return: 0 if OK, -ve on error
410 int fwu_trial_state_ctr_start(void);