blob: a83ea19534c935c3459588d6139049b2942373e2 [file] [log] [blame]
Sughosh Ganu2eaedc92022-10-21 18:15:55 +05301/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (c) 2022, Linaro Limited
4 */
5
6#if !defined _FWU_H_
7#define _FWU_H_
8
9#include <blk.h>
10#include <efi.h>
11
12#include <linux/types.h>
13
14struct fwu_mdata;
15struct udevice;
16
17/**
18 * @mdata_check: check the validity of the FWU metadata partitions
19 * @get_mdata() - Get a FWU metadata copy
20 * @update_mdata() - Update the FWU metadata copy
21 */
22struct fwu_mdata_ops {
23 /**
24 * check_mdata() - Check if the FWU metadata is valid
25 * @dev: FWU device
26 *
27 * Validate both copies of the FWU metadata. If one of the copies
28 * has gone bad, restore it from the other copy.
29 *
30 * Return: 0 if OK, -ve on error
31 */
32 int (*check_mdata)(struct udevice *dev);
33
34 /**
35 * get_mdata() - Get a FWU metadata copy
36 * @dev: FWU device
37 * @mdata: Pointer to FWU metadata
38 *
39 * Get a valid copy of the FWU metadata.
40 *
41 * Return: 0 if OK, -ve on error
42 */
43 int (*get_mdata)(struct udevice *dev, struct fwu_mdata *mdata);
44
45 /**
46 * update_mdata() - Update the FWU metadata
47 * @dev: FWU device
48 * @mdata: Copy of the FWU metadata
49 *
50 * Update the FWU metadata structure by writing to the
51 * FWU metadata partitions.
52 *
53 * Return: 0 if OK, -ve on error
54 */
55 int (*update_mdata)(struct udevice *dev, struct fwu_mdata *mdata);
56
57 /**
58 * get_mdata_part_num() - Get the FWU metadata partition numbers
59 * @dev: FWU metadata device
60 * @mdata_parts: array for storing the metadata partition numbers
61 *
62 * Get the partition numbers on the storage device on which the
63 * FWU metadata is stored. Two partition numbers will be returned.
64 *
65 * Return: 0 if OK, -ve on error
66 */
67 int (*get_mdata_part_num)(struct udevice *dev, uint *mdata_parts);
68
69 /**
70 * read_mdata_partition() - Read the FWU metadata from a partition
71 * @dev: FWU metadata device
72 * @mdata: Copy of the FWU metadata
73 * @part_num: Partition number from which FWU metadata is to be read
74 *
75 * Read the FWU metadata from the specified partition number
76 *
77 * Return: 0 if OK, -ve on error
78 */
79 int (*read_mdata_partition)(struct udevice *dev,
80 struct fwu_mdata *mdata, uint part_num);
81
82 /**
83 * write_mdata_partition() - Write the FWU metadata to a partition
84 * @dev: FWU metadata device
85 * @mdata: Copy of the FWU metadata
86 * @part_num: Partition number to which FWU metadata is to be written
87 *
88 * Write the FWU metadata to the specified partition number
89 *
90 * Return: 0 if OK, -ve on error
91 */
92 int (*write_mdata_partition)(struct udevice *dev,
93 struct fwu_mdata *mdata, uint part_num);
94};
95
96#define FWU_MDATA_VERSION 0x1
97
98/*
99* GUID value defined in the FWU specification for identification
100* of the FWU metadata partition.
101*/
102#define FWU_MDATA_GUID \
103 EFI_GUID(0x8a7a84a0, 0x8387, 0x40f6, 0xab, 0x41, \
104 0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23)
105
106/**
107 * fwu_check_mdata_validity() - Check for validity of the FWU metadata copies
108 *
109 * Read both the metadata copies from the storage media, verify their
110 * checksum, and ascertain that both copies match. If one of the copies
111 * has gone bad, restore it from the good copy.
112 *
113 * Return: 0 if OK, -ve on error
114 *
115 */
116int fwu_check_mdata_validity(void);
117
118/**
119 * fwu_get_mdata_part_num() - Get the FWU metadata partition numbers
120 * @dev: FWU metadata device
121 * @mdata_parts: array for storing the metadata partition numbers
122 *
123 * Get the partition numbers on the storage device on which the
124 * FWU metadata is stored. Two partition numbers will be returned
125 * through the array.
126 *
127 * Return: 0 if OK, -ve on error
128 *
129 */
130int fwu_get_mdata_part_num(struct udevice *dev, uint *mdata_parts);
131
132/**
133 * fwu_read_mdata_partition() - Read the FWU metadata from a partition
134 * @dev: FWU metadata device
135 * @mdata: Copy of the FWU metadata
136 * @part_num: Partition number from which FWU metadata is to be read
137 *
138 * Read the FWU metadata from the specified partition number
139 *
140 * Return: 0 if OK, -ve on error
141 *
142 */
143int fwu_read_mdata_partition(struct udevice *dev, struct fwu_mdata *mdata,
144 uint part_num);
145
146/**
147 * fwu_write_mdata_partition() - Write the FWU metadata to a partition
148 * @dev: FWU metadata device
149 * @mdata: Copy of the FWU metadata
150 * @part_num: Partition number to which FWU metadata is to be written
151 *
152 * Write the FWU metadata to the specified partition number
153 *
154 * Return: 0 if OK, -ve on error
155 *
156 */
157int fwu_write_mdata_partition(struct udevice *dev, struct fwu_mdata *mdata,
158 uint part_num);
159
160/**
161 * fwu_get_mdata() - Get a FWU metadata copy
162 * @dev: FWU metadata device
163 * @mdata: Copy of the FWU metadata
164 *
165 * Get a valid copy of the FWU metadata.
166 *
167 * Note: This function is to be called first when modifying any fields
168 * in the metadata. The sequence of calls to modify any field in the
169 * metadata would be 1) fwu_get_mdata 2) Modify metadata, followed by
170 * 3) fwu_update_mdata
171 *
172 * Return: 0 if OK, -ve on error
173 *
174 */
175int fwu_get_mdata(struct udevice *dev, struct fwu_mdata *mdata);
176
177/**
178 * fwu_update_mdata() - Update the FWU metadata
179 * @dev: FWU metadata device
180 * @mdata: Copy of the FWU metadata
181 *
182 * Update the FWU metadata structure by writing to the
183 * FWU metadata partitions.
184 *
185 * Note: This function is not to be called directly to update the
186 * metadata fields. The sequence of function calls should be
187 * 1) fwu_get_mdata() 2) Modify the medata fields 3) fwu_update_mdata()
188 *
189 * The sequence of updating the partitions should be, update the
190 * primary metadata partition (first partition encountered), followed
191 * by updating the secondary partition. With this update sequence, in
192 * the rare scenario that the two metadata partitions are valid but do
193 * not match, maybe due to power outage at the time of updating the
194 * metadata copies, the secondary partition can be updated from the
195 * primary.
196 *
197 * Return: 0 if OK, -ve on error
198 *
199 */
200int fwu_update_mdata(struct udevice *dev, struct fwu_mdata *mdata);
201
202/**
203 * fwu_get_active_index() - Get active_index from the FWU metadata
204 * @active_idxp: active_index value to be read
205 *
206 * Read the active_index field from the FWU metadata and place it in
207 * the variable pointed to be the function argument.
208 *
209 * Return: 0 if OK, -ve on error
210 *
211 */
212int fwu_get_active_index(uint *active_idxp);
213
214/**
215 * fwu_set_active_index() - Set active_index in the FWU metadata
216 * @active_idx: active_index value to be set
217 *
218 * Update the active_index field in the FWU metadata
219 *
220 * Return: 0 if OK, -ve on error
221 *
222 */
223int fwu_set_active_index(uint active_idx);
224
225/**
226 * fwu_get_image_index() - Get the Image Index to be used for capsule update
227 * @image_index: The Image Index for the image
228 *
229 * The FWU multi bank update feature computes the value of image_index at
230 * runtime, based on the bank to which the image needs to be written to.
231 * Derive the image_index value for the image.
232 *
233 * Currently, the capsule update driver uses the DFU framework for
234 * the updates. This function gets the DFU alt number which is to
235 * be used as the Image Index
236 *
237 * Return: 0 if OK, -ve on error
238 *
239 */
240int fwu_get_image_index(u8 *image_index);
241
242/**
243 * fwu_mdata_check() - Check if the FWU metadata is valid
244 * @dev: FWU metadata device
245 *
246 * Validate both copies of the FWU metadata. If one of the copies
247 * has gone bad, restore it from the other copy.
248 *
249 * Return: 0 if OK, -ve on error
250 *
251 */
252int fwu_mdata_check(struct udevice *dev);
253
254/**
255 * fwu_revert_boot_index() - Revert the active index in the FWU metadata
256 *
257 * Revert the active_index value in the FWU metadata, by swapping the values
258 * of active_index and previous_active_index in both copies of the
259 * FWU metadata.
260 *
261 * Return: 0 if OK, -ve on error
262 *
263 */
264int fwu_revert_boot_index(void);
265
266/**
267 * fwu_verify_mdata() - Verify the FWU metadata
268 * @mdata: FWU metadata structure
269 * @pri_part: FWU metadata partition is primary or secondary
270 *
271 * Verify the FWU metadata by computing the CRC32 for the metadata
272 * structure and comparing it against the CRC32 value stored as part
273 * of the structure.
274 *
275 * Return: 0 if OK, -ve on error
276 *
277 */
278int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part);
279
280/**
281 * fwu_accept_image() - Set the Acceptance bit for the image
282 * @img_type_id: GUID of the image type for which the accepted bit is to be
283 * cleared
284 * @bank: Bank of which the image's Accept bit is to be set
285 *
286 * Set the accepted bit for the image specified by the img_guid parameter. This
287 * indicates acceptance of image for subsequent boots by some governing component
288 * like OS(or firmware).
289 *
290 * Return: 0 if OK, -ve on error
291 *
292 */
293int fwu_accept_image(efi_guid_t *img_type_id, u32 bank);
294
295/**
296 * fwu_clear_accept_image() - Clear the Acceptance bit for the image
297 * @img_type_id: GUID of the image type for which the accepted bit is to be
298 * cleared
299 * @bank: Bank of which the image's Accept bit is to be cleared
300 *
301 * Clear the accepted bit for the image type specified by the img_type_id parameter.
302 * This function is called after the image has been updated. The accepted bit is
303 * cleared to be set subsequently after passing the image acceptance criteria, by
304 * either the OS(or firmware)
305 *
306 * Return: 0 if OK, -ve on error
307 *
308 */
309int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank);
310
311#endif /* _FWU_H_ */