Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 1 | /* 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> |
Masami Hiramatsu | 4898679 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 11 | #include <mtd.h> |
| 12 | #include <uuid.h> |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 13 | |
| 14 | #include <linux/types.h> |
| 15 | |
| 16 | struct fwu_mdata; |
| 17 | struct udevice; |
| 18 | |
Sughosh Ganu | 554b38f | 2022-10-21 18:15:56 +0530 | [diff] [blame] | 19 | struct fwu_mdata_gpt_blk_priv { |
| 20 | struct udevice *blk_dev; |
| 21 | }; |
| 22 | |
Masami Hiramatsu | 4898679 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 23 | struct fwu_mtd_image_info { |
| 24 | u32 start, size; |
| 25 | int bank_num, image_num; |
| 26 | char uuidbuf[UUID_STR_LEN + 1]; |
| 27 | }; |
| 28 | |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 29 | struct fwu_mdata_ops { |
| 30 | /** |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 31 | * read_mdata() - Populate the asked FWU metadata copy |
| 32 | * @dev: FWU metadata device |
| 33 | * @mdata: Output FWU mdata read |
| 34 | * @primary: If primary or secondary copy of metadata is to be read |
Sughosh Ganu | f42a61f | 2024-03-22 16:27:16 +0530 | [diff] [blame^] | 35 | * @size: Size in bytes of the metadata to be read |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 36 | * |
| 37 | * Return: 0 if OK, -ve on error |
| 38 | */ |
Sughosh Ganu | f42a61f | 2024-03-22 16:27:16 +0530 | [diff] [blame^] | 39 | int (*read_mdata)(struct udevice *dev, struct fwu_mdata *mdata, |
| 40 | bool primary, uint32_t size); |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * write_mdata() - Write the given FWU metadata copy |
| 44 | * @dev: FWU metadata device |
| 45 | * @mdata: Copy of the FWU metadata to write |
| 46 | * @primary: If primary or secondary copy of metadata is to be written |
Sughosh Ganu | f42a61f | 2024-03-22 16:27:16 +0530 | [diff] [blame^] | 47 | * @size: Size in bytes of the metadata to be written |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 48 | * |
| 49 | * Return: 0 if OK, -ve on error |
| 50 | */ |
Sughosh Ganu | f42a61f | 2024-03-22 16:27:16 +0530 | [diff] [blame^] | 51 | int (*write_mdata)(struct udevice *dev, struct fwu_mdata *mdata, |
| 52 | bool primary, uint32_t size); |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | #define FWU_MDATA_VERSION 0x1 |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 56 | #define FWU_IMAGE_ACCEPTED 0x1 |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * GUID value defined in the FWU specification for identification |
| 60 | * of the FWU metadata partition. |
| 61 | */ |
| 62 | #define FWU_MDATA_GUID \ |
| 63 | EFI_GUID(0x8a7a84a0, 0x8387, 0x40f6, 0xab, 0x41, \ |
| 64 | 0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23) |
| 65 | |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 66 | /* |
| 67 | * GUID value defined in the Dependable Boot specification for |
| 68 | * identification of the revert capsule, used for reverting |
| 69 | * any image in the updated bank. |
| 70 | */ |
| 71 | #define FWU_OS_REQUEST_FW_REVERT_GUID \ |
| 72 | EFI_GUID(0xacd58b4b, 0xc0e8, 0x475f, 0x99, 0xb5, \ |
| 73 | 0x6b, 0x3f, 0x7e, 0x07, 0xaa, 0xf0) |
| 74 | |
| 75 | /* |
| 76 | * GUID value defined in the Dependable Boot specification for |
| 77 | * identification of the accept capsule, used for accepting |
| 78 | * an image in the updated bank. |
| 79 | */ |
| 80 | #define FWU_OS_REQUEST_FW_ACCEPT_GUID \ |
| 81 | EFI_GUID(0x0c996046, 0xbcc0, 0x4d04, 0x85, 0xec, \ |
| 82 | 0xe1, 0xfc, 0xed, 0xf1, 0xc6, 0xf8) |
| 83 | |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 84 | /** |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 85 | * fwu_read_mdata() - Wrapper around fwu_mdata_ops.read_mdata() |
| 86 | */ |
Sughosh Ganu | f42a61f | 2024-03-22 16:27:16 +0530 | [diff] [blame^] | 87 | int fwu_read_mdata(struct udevice *dev, struct fwu_mdata *mdata, |
| 88 | bool primary, uint32_t size); |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * fwu_write_mdata() - Wrapper around fwu_mdata_ops.write_mdata() |
| 92 | */ |
Sughosh Ganu | f42a61f | 2024-03-22 16:27:16 +0530 | [diff] [blame^] | 93 | int fwu_write_mdata(struct udevice *dev, struct fwu_mdata *mdata, |
| 94 | bool primary, uint32_t size); |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 95 | |
| 96 | /** |
Jassi Brar | 1e917a6 | 2023-03-06 17:18:48 -0600 | [diff] [blame] | 97 | * fwu_get_mdata() - Read, verify and return the FWU metadata |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 98 | * |
| 99 | * Read both the metadata copies from the storage media, verify their checksum, |
| 100 | * and ascertain that both copies match. If one of the copies has gone bad, |
| 101 | * restore it from the good copy. |
| 102 | * |
| 103 | * Return: 0 if OK, -ve on error |
| 104 | */ |
Jassi Brar | 1e917a6 | 2023-03-06 17:18:48 -0600 | [diff] [blame] | 105 | int fwu_get_mdata(struct fwu_mdata *mdata); |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 106 | |
| 107 | /** |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 108 | * fwu_get_active_index() - Get active_index from the FWU metadata |
| 109 | * @active_idxp: active_index value to be read |
| 110 | * |
| 111 | * Read the active_index field from the FWU metadata and place it in |
| 112 | * the variable pointed to be the function argument. |
| 113 | * |
| 114 | * Return: 0 if OK, -ve on error |
| 115 | * |
| 116 | */ |
| 117 | int fwu_get_active_index(uint *active_idxp); |
| 118 | |
| 119 | /** |
| 120 | * fwu_set_active_index() - Set active_index in the FWU metadata |
| 121 | * @active_idx: active_index value to be set |
| 122 | * |
| 123 | * Update the active_index field in the FWU metadata |
| 124 | * |
| 125 | * Return: 0 if OK, -ve on error |
| 126 | * |
| 127 | */ |
| 128 | int fwu_set_active_index(uint active_idx); |
| 129 | |
| 130 | /** |
Masahisa Kojima | af7a34a | 2024-01-11 14:35:39 +0900 | [diff] [blame] | 131 | * fwu_get_dfu_alt_num() - Get the dfu_alt_num to be used for capsule update |
| 132 | * @image_index: The Image Index for the image |
| 133 | * @alt_num: pointer to store dfu_alt_num |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 134 | * |
| 135 | * Currently, the capsule update driver uses the DFU framework for |
| 136 | * the updates. This function gets the DFU alt number which is to |
Masahisa Kojima | af7a34a | 2024-01-11 14:35:39 +0900 | [diff] [blame] | 137 | * be used for capsule update. |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 138 | * |
| 139 | * Return: 0 if OK, -ve on error |
| 140 | * |
| 141 | */ |
Masahisa Kojima | af7a34a | 2024-01-11 14:35:39 +0900 | [diff] [blame] | 142 | int fwu_get_dfu_alt_num(u8 image_index, u8 *alt_num); |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 143 | |
| 144 | /** |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 145 | * fwu_revert_boot_index() - Revert the active index in the FWU metadata |
| 146 | * |
| 147 | * Revert the active_index value in the FWU metadata, by swapping the values |
| 148 | * of active_index and previous_active_index in both copies of the |
| 149 | * FWU metadata. |
| 150 | * |
| 151 | * Return: 0 if OK, -ve on error |
| 152 | * |
| 153 | */ |
| 154 | int fwu_revert_boot_index(void); |
| 155 | |
| 156 | /** |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 157 | * fwu_accept_image() - Set the Acceptance bit for the image |
| 158 | * @img_type_id: GUID of the image type for which the accepted bit is to be |
| 159 | * cleared |
| 160 | * @bank: Bank of which the image's Accept bit is to be set |
| 161 | * |
| 162 | * Set the accepted bit for the image specified by the img_guid parameter. This |
| 163 | * indicates acceptance of image for subsequent boots by some governing component |
| 164 | * like OS(or firmware). |
| 165 | * |
| 166 | * Return: 0 if OK, -ve on error |
| 167 | * |
| 168 | */ |
| 169 | int fwu_accept_image(efi_guid_t *img_type_id, u32 bank); |
| 170 | |
| 171 | /** |
| 172 | * fwu_clear_accept_image() - Clear the Acceptance bit for the image |
| 173 | * @img_type_id: GUID of the image type for which the accepted bit is to be |
| 174 | * cleared |
| 175 | * @bank: Bank of which the image's Accept bit is to be cleared |
| 176 | * |
| 177 | * Clear the accepted bit for the image type specified by the img_type_id parameter. |
| 178 | * This function is called after the image has been updated. The accepted bit is |
| 179 | * cleared to be set subsequently after passing the image acceptance criteria, by |
| 180 | * either the OS(or firmware) |
| 181 | * |
| 182 | * Return: 0 if OK, -ve on error |
| 183 | * |
| 184 | */ |
| 185 | int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank); |
| 186 | |
Sughosh Ganu | 7d6e2c5 | 2022-10-21 18:15:59 +0530 | [diff] [blame] | 187 | /** |
| 188 | * fwu_plat_get_alt_num() - Get the DFU Alt Num for the image from the platform |
| 189 | * @dev: FWU device |
| 190 | * @image_guid: Image GUID for which DFU alt number needs to be retrieved |
| 191 | * @alt_num: Pointer to the alt_num |
| 192 | * |
| 193 | * Get the DFU alt number from the platform for the image specified by the |
| 194 | * image GUID. |
| 195 | * |
| 196 | * Return: 0 if OK, -ve on error |
| 197 | * |
| 198 | */ |
| 199 | int fwu_plat_get_alt_num(struct udevice *dev, efi_guid_t *image_guid, |
| 200 | u8 *alt_num); |
| 201 | |
| 202 | /** |
| 203 | * fwu_plat_get_update_index() - Get the value of the update bank |
| 204 | * @update_idx: Bank number to which images are to be updated |
| 205 | * |
| 206 | * Get the value of the bank(partition) to which the update needs to be |
| 207 | * made. |
| 208 | * |
| 209 | * Note: This is a weak function and platforms can override this with |
| 210 | * their own implementation for selection of the update bank. |
| 211 | * |
| 212 | * Return: 0 if OK, -ve on error |
| 213 | * |
| 214 | */ |
| 215 | int fwu_plat_get_update_index(uint *update_idx); |
Sughosh Ganu | 95b5a7d | 2022-10-21 18:16:00 +0530 | [diff] [blame] | 216 | |
| 217 | /** |
| 218 | * fwu_plat_get_bootidx() - Get the value of the boot index |
| 219 | * @boot_idx: Boot index value |
| 220 | * |
| 221 | * Get the value of the bank(partition) from which the platform |
| 222 | * has booted. This value is passed to U-Boot from the earlier |
| 223 | * stage bootloader which loads and boots all the relevant |
| 224 | * firmware images |
| 225 | * |
| 226 | */ |
| 227 | void fwu_plat_get_bootidx(uint *boot_idx); |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 228 | |
| 229 | /** |
| 230 | * fwu_update_checks_pass() - Check if FWU update can be done |
| 231 | * |
| 232 | * Check if the FWU update can be executed. The updates are |
| 233 | * allowed only when the platform is not in Trial State and |
| 234 | * the boot time checks have passed |
| 235 | * |
| 236 | * Return: 1 if OK, 0 if checks do not pass |
| 237 | * |
| 238 | */ |
| 239 | u8 fwu_update_checks_pass(void); |
| 240 | |
| 241 | /** |
| 242 | * fwu_empty_capsule_checks_pass() - Check if empty capsule can be processed |
| 243 | * |
| 244 | * Check if the empty capsule can be processed to either accept or revert |
| 245 | * an earlier executed update. The empty capsules need to be processed |
| 246 | * only when the platform is in Trial State and the boot time checks have |
| 247 | * passed |
| 248 | * |
| 249 | * Return: 1 if OK, 0 if not to be allowed |
| 250 | * |
| 251 | */ |
| 252 | u8 fwu_empty_capsule_checks_pass(void); |
| 253 | |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 254 | /** |
| 255 | * fwu_trial_state_ctr_start() - Start the Trial State counter |
| 256 | * |
| 257 | * Start the counter to identify the platform booting in the |
| 258 | * Trial State. The counter is implemented as an EFI variable. |
| 259 | * |
| 260 | * Return: 0 if OK, -ve on error |
| 261 | * |
| 262 | */ |
| 263 | int fwu_trial_state_ctr_start(void); |
| 264 | |
Masami Hiramatsu | 4898679 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 265 | /** |
| 266 | * fwu_gen_alt_info_from_mtd() - Parse dfu_alt_info from metadata in mtd |
| 267 | * @buf: Buffer into which the dfu_alt_info is filled |
| 268 | * @len: Maximum characters that can be written in buf |
| 269 | * @mtd: Pointer to underlying MTD device |
| 270 | * |
| 271 | * Parse dfu_alt_info from metadata in mtd. Used for setting the env. |
| 272 | * |
| 273 | * Return: 0 if OK, -ve on error |
| 274 | */ |
| 275 | int fwu_gen_alt_info_from_mtd(char *buf, size_t len, struct mtd_info *mtd); |
| 276 | |
| 277 | /** |
| 278 | * fwu_mtd_get_alt_num() - Mapping of fwu_plat_get_alt_num for MTD device |
| 279 | * @image_guid: Image GUID for which DFU alt number needs to be retrieved |
| 280 | * @alt_num: Pointer to the alt_num |
| 281 | * @mtd_dev: Name of mtd device instance |
| 282 | * |
| 283 | * To map fwu_plat_get_alt_num onto mtd based metadata implementation. |
| 284 | * |
| 285 | * Return: 0 if OK, -ve on error |
| 286 | */ |
| 287 | int fwu_mtd_get_alt_num(efi_guid_t *image_guid, u8 *alt_num, const char *mtd_dev); |
| 288 | |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 289 | #endif /* _FWU_H_ */ |