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
103 * GUID value defined in the FWU specification for identification
104 * of the FWU metadata partition.
106 #define FWU_MDATA_GUID \
107 EFI_GUID(0x8a7a84a0, 0x8387, 0x40f6, 0xab, 0x41, \
108 0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23)
111 * fwu_check_mdata_validity() - Check for validity of the FWU metadata copies
113 * Read both the metadata copies from the storage media, verify their
114 * checksum, and ascertain that both copies match. If one of the copies
115 * has gone bad, restore it from the good copy.
117 * Return: 0 if OK, -ve on error
120 int fwu_check_mdata_validity(void);
123 * fwu_get_mdata_part_num() - Get the FWU metadata partition numbers
124 * @dev: FWU metadata device
125 * @mdata_parts: array for storing the metadata partition numbers
127 * Get the partition numbers on the storage device on which the
128 * FWU metadata is stored. Two partition numbers will be returned
131 * Return: 0 if OK, -ve on error
134 int fwu_get_mdata_part_num(struct udevice *dev, uint *mdata_parts);
137 * fwu_read_mdata_partition() - Read the FWU metadata from a partition
138 * @dev: FWU metadata device
139 * @mdata: Copy of the FWU metadata
140 * @part_num: Partition number from which FWU metadata is to be read
142 * Read the FWU metadata from the specified partition number
144 * Return: 0 if OK, -ve on error
147 int fwu_read_mdata_partition(struct udevice *dev, struct fwu_mdata *mdata,
151 * fwu_write_mdata_partition() - Write the FWU metadata to a partition
152 * @dev: FWU metadata device
153 * @mdata: Copy of the FWU metadata
154 * @part_num: Partition number to which FWU metadata is to be written
156 * Write the FWU metadata to the specified partition number
158 * Return: 0 if OK, -ve on error
161 int fwu_write_mdata_partition(struct udevice *dev, struct fwu_mdata *mdata,
165 * fwu_get_mdata() - Get a FWU metadata copy
166 * @dev: FWU metadata device
167 * @mdata: Copy of the FWU metadata
169 * Get a valid copy of the FWU metadata.
171 * Note: This function is to be called first when modifying any fields
172 * in the metadata. The sequence of calls to modify any field in the
173 * metadata would be 1) fwu_get_mdata 2) Modify metadata, followed by
174 * 3) fwu_update_mdata
176 * Return: 0 if OK, -ve on error
179 int fwu_get_mdata(struct udevice *dev, struct fwu_mdata *mdata);
182 * fwu_update_mdata() - Update the FWU metadata
183 * @dev: FWU metadata device
184 * @mdata: Copy of the FWU metadata
186 * Update the FWU metadata structure by writing to the
187 * FWU metadata partitions.
189 * Note: This function is not to be called directly to update the
190 * metadata fields. The sequence of function calls should be
191 * 1) fwu_get_mdata() 2) Modify the medata fields 3) fwu_update_mdata()
193 * The sequence of updating the partitions should be, update the
194 * primary metadata partition (first partition encountered), followed
195 * by updating the secondary partition. With this update sequence, in
196 * the rare scenario that the two metadata partitions are valid but do
197 * not match, maybe due to power outage at the time of updating the
198 * metadata copies, the secondary partition can be updated from the
201 * Return: 0 if OK, -ve on error
204 int fwu_update_mdata(struct udevice *dev, struct fwu_mdata *mdata);
207 * fwu_get_active_index() - Get active_index from the FWU metadata
208 * @active_idxp: active_index value to be read
210 * Read the active_index field from the FWU metadata and place it in
211 * the variable pointed to be the function argument.
213 * Return: 0 if OK, -ve on error
216 int fwu_get_active_index(uint *active_idxp);
219 * fwu_set_active_index() - Set active_index in the FWU metadata
220 * @active_idx: active_index value to be set
222 * Update the active_index field in the FWU metadata
224 * Return: 0 if OK, -ve on error
227 int fwu_set_active_index(uint active_idx);
230 * fwu_get_image_index() - Get the Image Index to be used for capsule update
231 * @image_index: The Image Index for the image
233 * The FWU multi bank update feature computes the value of image_index at
234 * runtime, based on the bank to which the image needs to be written to.
235 * Derive the image_index value for the image.
237 * Currently, the capsule update driver uses the DFU framework for
238 * the updates. This function gets the DFU alt number which is to
239 * be used as the Image Index
241 * Return: 0 if OK, -ve on error
244 int fwu_get_image_index(u8 *image_index);
247 * fwu_mdata_check() - Check if the FWU metadata is valid
248 * @dev: FWU metadata device
250 * Validate both copies of the FWU metadata. If one of the copies
251 * has gone bad, restore it from the other copy.
253 * Return: 0 if OK, -ve on error
256 int fwu_mdata_check(struct udevice *dev);
259 * fwu_revert_boot_index() - Revert the active index in the FWU metadata
261 * Revert the active_index value in the FWU metadata, by swapping the values
262 * of active_index and previous_active_index in both copies of the
265 * Return: 0 if OK, -ve on error
268 int fwu_revert_boot_index(void);
271 * fwu_verify_mdata() - Verify the FWU metadata
272 * @mdata: FWU metadata structure
273 * @pri_part: FWU metadata partition is primary or secondary
275 * Verify the FWU metadata by computing the CRC32 for the metadata
276 * structure and comparing it against the CRC32 value stored as part
279 * Return: 0 if OK, -ve on error
282 int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part);
285 * fwu_accept_image() - Set the Acceptance bit for the image
286 * @img_type_id: GUID of the image type for which the accepted bit is to be
288 * @bank: Bank of which the image's Accept bit is to be set
290 * Set the accepted bit for the image specified by the img_guid parameter. This
291 * indicates acceptance of image for subsequent boots by some governing component
292 * like OS(or firmware).
294 * Return: 0 if OK, -ve on error
297 int fwu_accept_image(efi_guid_t *img_type_id, u32 bank);
300 * fwu_clear_accept_image() - Clear the Acceptance bit for the image
301 * @img_type_id: GUID of the image type for which the accepted bit is to be
303 * @bank: Bank of which the image's Accept bit is to be cleared
305 * Clear the accepted bit for the image type specified by the img_type_id parameter.
306 * This function is called after the image has been updated. The accepted bit is
307 * cleared to be set subsequently after passing the image acceptance criteria, by
308 * either the OS(or firmware)
310 * Return: 0 if OK, -ve on error
313 int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank);
316 * fwu_plat_get_alt_num() - Get the DFU Alt Num for the image from the platform
318 * @image_guid: Image GUID for which DFU alt number needs to be retrieved
319 * @alt_num: Pointer to the alt_num
321 * Get the DFU alt number from the platform for the image specified by the
324 * Return: 0 if OK, -ve on error
327 int fwu_plat_get_alt_num(struct udevice *dev, efi_guid_t *image_guid,
331 * fwu_plat_get_update_index() - Get the value of the update bank
332 * @update_idx: Bank number to which images are to be updated
334 * Get the value of the bank(partition) to which the update needs to be
337 * Note: This is a weak function and platforms can override this with
338 * their own implementation for selection of the update bank.
340 * Return: 0 if OK, -ve on error
343 int fwu_plat_get_update_index(uint *update_idx);
346 * fwu_plat_get_bootidx() - Get the value of the boot index
347 * @boot_idx: Boot index value
349 * Get the value of the bank(partition) from which the platform
350 * has booted. This value is passed to U-Boot from the earlier
351 * stage bootloader which loads and boots all the relevant
355 void fwu_plat_get_bootidx(uint *boot_idx);
358 * fwu_update_checks_pass() - Check if FWU update can be done
360 * Check if the FWU update can be executed. The updates are
361 * allowed only when the platform is not in Trial State and
362 * the boot time checks have passed
364 * Return: 1 if OK, 0 if checks do not pass
367 u8 fwu_update_checks_pass(void);
370 * fwu_empty_capsule_checks_pass() - Check if empty capsule can be processed
372 * Check if the empty capsule can be processed to either accept or revert
373 * an earlier executed update. The empty capsules need to be processed
374 * only when the platform is in Trial State and the boot time checks have
377 * Return: 1 if OK, 0 if not to be allowed
380 u8 fwu_empty_capsule_checks_pass(void);