blob: e681e91027475a33ee3abeaa13120af619574a3d [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>
Sughosh Ganu1cafc2a2024-03-22 16:27:19 +053011#include <fwu_mdata.h>
Masami Hiramatsu48986792023-05-31 00:29:14 -050012#include <mtd.h>
13#include <uuid.h>
Sughosh Ganu2eaedc92022-10-21 18:15:55 +053014
15#include <linux/types.h>
16
17struct fwu_mdata;
18struct udevice;
19
Sughosh Ganu554b38f2022-10-21 18:15:56 +053020struct fwu_mdata_gpt_blk_priv {
21 struct udevice *blk_dev;
22};
23
Masami Hiramatsu48986792023-05-31 00:29:14 -050024struct fwu_mtd_image_info {
25 u32 start, size;
26 int bank_num, image_num;
27 char uuidbuf[UUID_STR_LEN + 1];
28};
29
Sughosh Ganue67c0b72024-03-22 16:27:17 +053030struct fwu_mdata_mtd_priv {
31 struct mtd_info *mtd;
32 char pri_label[50];
33 char sec_label[50];
34 u32 pri_offset;
35 u32 sec_offset;
36 struct fwu_mtd_image_info *fwu_mtd_images;
37};
38
Sughosh Ganu1cafc2a2024-03-22 16:27:19 +053039struct fwu_data {
40 uint32_t crc32;
41 uint32_t version;
42 uint32_t active_index;
43 uint32_t previous_active_index;
44 uint32_t metadata_size;
45 uint32_t boot_index;
46 uint32_t num_banks;
47 uint32_t num_images;
48 uint8_t bank_state[4];
49 bool trial_state;
50
51 struct fwu_mdata *fwu_mdata;
52
53 struct fwu_image_entry fwu_images[CONFIG_FWU_NUM_IMAGES_PER_BANK];
54};
55
Sughosh Ganu2eaedc92022-10-21 18:15:55 +053056struct fwu_mdata_ops {
57 /**
Jassi Brar167994f2023-03-06 17:18:28 -060058 * read_mdata() - Populate the asked FWU metadata copy
59 * @dev: FWU metadata device
60 * @mdata: Output FWU mdata read
61 * @primary: If primary or secondary copy of metadata is to be read
Sughosh Ganuf42a61f2024-03-22 16:27:16 +053062 * @size: Size in bytes of the metadata to be read
Jassi Brar167994f2023-03-06 17:18:28 -060063 *
64 * Return: 0 if OK, -ve on error
65 */
Sughosh Ganuf42a61f2024-03-22 16:27:16 +053066 int (*read_mdata)(struct udevice *dev, struct fwu_mdata *mdata,
67 bool primary, uint32_t size);
Jassi Brar167994f2023-03-06 17:18:28 -060068
69 /**
70 * write_mdata() - Write the given FWU metadata copy
71 * @dev: FWU metadata device
72 * @mdata: Copy of the FWU metadata to write
73 * @primary: If primary or secondary copy of metadata is to be written
Sughosh Ganuf42a61f2024-03-22 16:27:16 +053074 * @size: Size in bytes of the metadata to be written
Jassi Brar167994f2023-03-06 17:18:28 -060075 *
76 * Return: 0 if OK, -ve on error
77 */
Sughosh Ganuf42a61f2024-03-22 16:27:16 +053078 int (*write_mdata)(struct udevice *dev, struct fwu_mdata *mdata,
79 bool primary, uint32_t size);
Sughosh Ganu2eaedc92022-10-21 18:15:55 +053080};
81
82#define FWU_MDATA_VERSION 0x1
Sughosh Ganu86794052022-10-21 18:16:03 +053083#define FWU_IMAGE_ACCEPTED 0x1
Sughosh Ganu2eaedc92022-10-21 18:15:55 +053084
85/*
86* GUID value defined in the FWU specification for identification
87* of the FWU metadata partition.
88*/
89#define FWU_MDATA_GUID \
90 EFI_GUID(0x8a7a84a0, 0x8387, 0x40f6, 0xab, 0x41, \
91 0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23)
92
Sughosh Ganu86794052022-10-21 18:16:03 +053093/*
94* GUID value defined in the Dependable Boot specification for
95* identification of the revert capsule, used for reverting
96* any image in the updated bank.
97*/
98#define FWU_OS_REQUEST_FW_REVERT_GUID \
99 EFI_GUID(0xacd58b4b, 0xc0e8, 0x475f, 0x99, 0xb5, \
100 0x6b, 0x3f, 0x7e, 0x07, 0xaa, 0xf0)
101
102/*
103* GUID value defined in the Dependable Boot specification for
104* identification of the accept capsule, used for accepting
105* an image in the updated bank.
106*/
107#define FWU_OS_REQUEST_FW_ACCEPT_GUID \
108 EFI_GUID(0x0c996046, 0xbcc0, 0x4d04, 0x85, 0xec, \
109 0xe1, 0xfc, 0xed, 0xf1, 0xc6, 0xf8)
110
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530111/**
Jassi Brar167994f2023-03-06 17:18:28 -0600112 * fwu_read_mdata() - Wrapper around fwu_mdata_ops.read_mdata()
113 */
Sughosh Ganuf42a61f2024-03-22 16:27:16 +0530114int fwu_read_mdata(struct udevice *dev, struct fwu_mdata *mdata,
115 bool primary, uint32_t size);
Jassi Brar167994f2023-03-06 17:18:28 -0600116
117/**
118 * fwu_write_mdata() - Wrapper around fwu_mdata_ops.write_mdata()
119 */
Sughosh Ganuf42a61f2024-03-22 16:27:16 +0530120int fwu_write_mdata(struct udevice *dev, struct fwu_mdata *mdata,
121 bool primary, uint32_t size);
Jassi Brar167994f2023-03-06 17:18:28 -0600122
123/**
Jassi Brar1e917a62023-03-06 17:18:48 -0600124 * fwu_get_mdata() - Read, verify and return the FWU metadata
Jassi Brar167994f2023-03-06 17:18:28 -0600125 *
126 * Read both the metadata copies from the storage media, verify their checksum,
127 * and ascertain that both copies match. If one of the copies has gone bad,
128 * restore it from the good copy.
129 *
130 * Return: 0 if OK, -ve on error
131 */
Jassi Brar1e917a62023-03-06 17:18:48 -0600132int fwu_get_mdata(struct fwu_mdata *mdata);
Jassi Brar167994f2023-03-06 17:18:28 -0600133
134/**
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530135 * fwu_get_active_index() - Get active_index from the FWU metadata
136 * @active_idxp: active_index value to be read
137 *
138 * Read the active_index field from the FWU metadata and place it in
139 * the variable pointed to be the function argument.
140 *
141 * Return: 0 if OK, -ve on error
142 *
143 */
144int fwu_get_active_index(uint *active_idxp);
145
146/**
147 * fwu_set_active_index() - Set active_index in the FWU metadata
148 * @active_idx: active_index value to be set
149 *
150 * Update the active_index field in the FWU metadata
151 *
152 * Return: 0 if OK, -ve on error
153 *
154 */
155int fwu_set_active_index(uint active_idx);
156
157/**
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900158 * fwu_get_dfu_alt_num() - Get the dfu_alt_num to be used for capsule update
159 * @image_index: The Image Index for the image
160 * @alt_num: pointer to store dfu_alt_num
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530161 *
162 * Currently, the capsule update driver uses the DFU framework for
163 * the updates. This function gets the DFU alt number which is to
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900164 * be used for capsule update.
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530165 *
166 * Return: 0 if OK, -ve on error
167 *
168 */
Masahisa Kojimaaf7a34a2024-01-11 14:35:39 +0900169int fwu_get_dfu_alt_num(u8 image_index, u8 *alt_num);
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530170
171/**
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530172 * fwu_revert_boot_index() - Revert the active index in the FWU metadata
173 *
174 * Revert the active_index value in the FWU metadata, by swapping the values
175 * of active_index and previous_active_index in both copies of the
176 * FWU metadata.
177 *
178 * Return: 0 if OK, -ve on error
179 *
180 */
181int fwu_revert_boot_index(void);
182
183/**
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530184 * fwu_accept_image() - Set the Acceptance bit for the image
185 * @img_type_id: GUID of the image type for which the accepted bit is to be
186 * cleared
187 * @bank: Bank of which the image's Accept bit is to be set
188 *
189 * Set the accepted bit for the image specified by the img_guid parameter. This
190 * indicates acceptance of image for subsequent boots by some governing component
191 * like OS(or firmware).
192 *
193 * Return: 0 if OK, -ve on error
194 *
195 */
196int fwu_accept_image(efi_guid_t *img_type_id, u32 bank);
197
198/**
199 * fwu_clear_accept_image() - Clear the Acceptance bit for the image
200 * @img_type_id: GUID of the image type for which the accepted bit is to be
201 * cleared
202 * @bank: Bank of which the image's Accept bit is to be cleared
203 *
204 * Clear the accepted bit for the image type specified by the img_type_id parameter.
205 * This function is called after the image has been updated. The accepted bit is
206 * cleared to be set subsequently after passing the image acceptance criteria, by
207 * either the OS(or firmware)
208 *
209 * Return: 0 if OK, -ve on error
210 *
211 */
212int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank);
213
Sughosh Ganu7d6e2c52022-10-21 18:15:59 +0530214/**
215 * fwu_plat_get_alt_num() - Get the DFU Alt Num for the image from the platform
216 * @dev: FWU device
217 * @image_guid: Image GUID for which DFU alt number needs to be retrieved
218 * @alt_num: Pointer to the alt_num
219 *
220 * Get the DFU alt number from the platform for the image specified by the
221 * image GUID.
222 *
223 * Return: 0 if OK, -ve on error
224 *
225 */
226int fwu_plat_get_alt_num(struct udevice *dev, efi_guid_t *image_guid,
227 u8 *alt_num);
228
229/**
230 * fwu_plat_get_update_index() - Get the value of the update bank
231 * @update_idx: Bank number to which images are to be updated
232 *
233 * Get the value of the bank(partition) to which the update needs to be
234 * made.
235 *
236 * Note: This is a weak function and platforms can override this with
237 * their own implementation for selection of the update bank.
238 *
239 * Return: 0 if OK, -ve on error
240 *
241 */
242int fwu_plat_get_update_index(uint *update_idx);
Sughosh Ganu95b5a7d2022-10-21 18:16:00 +0530243
244/**
245 * fwu_plat_get_bootidx() - Get the value of the boot index
246 * @boot_idx: Boot index value
247 *
248 * Get the value of the bank(partition) from which the platform
249 * has booted. This value is passed to U-Boot from the earlier
250 * stage bootloader which loads and boots all the relevant
251 * firmware images
252 *
253 */
254void fwu_plat_get_bootidx(uint *boot_idx);
Sughosh Ganu7e9814c2022-10-21 18:16:02 +0530255
256/**
257 * fwu_update_checks_pass() - Check if FWU update can be done
258 *
259 * Check if the FWU update can be executed. The updates are
260 * allowed only when the platform is not in Trial State and
261 * the boot time checks have passed
262 *
263 * Return: 1 if OK, 0 if checks do not pass
264 *
265 */
266u8 fwu_update_checks_pass(void);
267
268/**
269 * fwu_empty_capsule_checks_pass() - Check if empty capsule can be processed
270 *
271 * Check if the empty capsule can be processed to either accept or revert
272 * an earlier executed update. The empty capsules need to be processed
273 * only when the platform is in Trial State and the boot time checks have
274 * passed
275 *
276 * Return: 1 if OK, 0 if not to be allowed
277 *
278 */
279u8 fwu_empty_capsule_checks_pass(void);
280
Sughosh Ganu86794052022-10-21 18:16:03 +0530281/**
282 * fwu_trial_state_ctr_start() - Start the Trial State counter
283 *
284 * Start the counter to identify the platform booting in the
285 * Trial State. The counter is implemented as an EFI variable.
286 *
287 * Return: 0 if OK, -ve on error
288 *
289 */
290int fwu_trial_state_ctr_start(void);
291
Masami Hiramatsu48986792023-05-31 00:29:14 -0500292/**
293 * fwu_gen_alt_info_from_mtd() - Parse dfu_alt_info from metadata in mtd
294 * @buf: Buffer into which the dfu_alt_info is filled
295 * @len: Maximum characters that can be written in buf
296 * @mtd: Pointer to underlying MTD device
297 *
298 * Parse dfu_alt_info from metadata in mtd. Used for setting the env.
299 *
300 * Return: 0 if OK, -ve on error
301 */
302int fwu_gen_alt_info_from_mtd(char *buf, size_t len, struct mtd_info *mtd);
303
304/**
305 * fwu_mtd_get_alt_num() - Mapping of fwu_plat_get_alt_num for MTD device
306 * @image_guid: Image GUID for which DFU alt number needs to be retrieved
307 * @alt_num: Pointer to the alt_num
308 * @mtd_dev: Name of mtd device instance
309 *
310 * To map fwu_plat_get_alt_num onto mtd based metadata implementation.
311 *
312 * Return: 0 if OK, -ve on error
313 */
314int fwu_mtd_get_alt_num(efi_guid_t *image_guid, u8 *alt_num, const char *mtd_dev);
315
Sughosh Ganu2eaedc92022-10-21 18:15:55 +0530316#endif /* _FWU_H_ */