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> |
Sughosh Ganu | 1cafc2a | 2024-03-22 16:27:19 +0530 | [diff] [blame^] | 11 | #include <fwu_mdata.h> |
Masami Hiramatsu | 4898679 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 12 | #include <mtd.h> |
| 13 | #include <uuid.h> |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 14 | |
| 15 | #include <linux/types.h> |
| 16 | |
| 17 | struct fwu_mdata; |
| 18 | struct udevice; |
| 19 | |
Sughosh Ganu | 554b38f | 2022-10-21 18:15:56 +0530 | [diff] [blame] | 20 | struct fwu_mdata_gpt_blk_priv { |
| 21 | struct udevice *blk_dev; |
| 22 | }; |
| 23 | |
Masami Hiramatsu | 4898679 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 24 | struct fwu_mtd_image_info { |
| 25 | u32 start, size; |
| 26 | int bank_num, image_num; |
| 27 | char uuidbuf[UUID_STR_LEN + 1]; |
| 28 | }; |
| 29 | |
Sughosh Ganu | e67c0b7 | 2024-03-22 16:27:17 +0530 | [diff] [blame] | 30 | struct 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 Ganu | 1cafc2a | 2024-03-22 16:27:19 +0530 | [diff] [blame^] | 39 | struct 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 Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 56 | struct fwu_mdata_ops { |
| 57 | /** |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 58 | * 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 Ganu | f42a61f | 2024-03-22 16:27:16 +0530 | [diff] [blame] | 62 | * @size: Size in bytes of the metadata to be read |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 63 | * |
| 64 | * Return: 0 if OK, -ve on error |
| 65 | */ |
Sughosh Ganu | f42a61f | 2024-03-22 16:27:16 +0530 | [diff] [blame] | 66 | int (*read_mdata)(struct udevice *dev, struct fwu_mdata *mdata, |
| 67 | bool primary, uint32_t size); |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 68 | |
| 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 Ganu | f42a61f | 2024-03-22 16:27:16 +0530 | [diff] [blame] | 74 | * @size: Size in bytes of the metadata to be written |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 75 | * |
| 76 | * Return: 0 if OK, -ve on error |
| 77 | */ |
Sughosh Ganu | f42a61f | 2024-03-22 16:27:16 +0530 | [diff] [blame] | 78 | int (*write_mdata)(struct udevice *dev, struct fwu_mdata *mdata, |
| 79 | bool primary, uint32_t size); |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | #define FWU_MDATA_VERSION 0x1 |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 83 | #define FWU_IMAGE_ACCEPTED 0x1 |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 84 | |
| 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 Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 93 | /* |
| 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 Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 111 | /** |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 112 | * fwu_read_mdata() - Wrapper around fwu_mdata_ops.read_mdata() |
| 113 | */ |
Sughosh Ganu | f42a61f | 2024-03-22 16:27:16 +0530 | [diff] [blame] | 114 | int fwu_read_mdata(struct udevice *dev, struct fwu_mdata *mdata, |
| 115 | bool primary, uint32_t size); |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * fwu_write_mdata() - Wrapper around fwu_mdata_ops.write_mdata() |
| 119 | */ |
Sughosh Ganu | f42a61f | 2024-03-22 16:27:16 +0530 | [diff] [blame] | 120 | int fwu_write_mdata(struct udevice *dev, struct fwu_mdata *mdata, |
| 121 | bool primary, uint32_t size); |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 122 | |
| 123 | /** |
Jassi Brar | 1e917a6 | 2023-03-06 17:18:48 -0600 | [diff] [blame] | 124 | * fwu_get_mdata() - Read, verify and return the FWU metadata |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 125 | * |
| 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 Brar | 1e917a6 | 2023-03-06 17:18:48 -0600 | [diff] [blame] | 132 | int fwu_get_mdata(struct fwu_mdata *mdata); |
Jassi Brar | 167994f | 2023-03-06 17:18:28 -0600 | [diff] [blame] | 133 | |
| 134 | /** |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 135 | * 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 | */ |
| 144 | int 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 | */ |
| 155 | int fwu_set_active_index(uint active_idx); |
| 156 | |
| 157 | /** |
Masahisa Kojima | af7a34a | 2024-01-11 14:35:39 +0900 | [diff] [blame] | 158 | * 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 Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 161 | * |
| 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 Kojima | af7a34a | 2024-01-11 14:35:39 +0900 | [diff] [blame] | 164 | * be used for capsule update. |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 165 | * |
| 166 | * Return: 0 if OK, -ve on error |
| 167 | * |
| 168 | */ |
Masahisa Kojima | af7a34a | 2024-01-11 14:35:39 +0900 | [diff] [blame] | 169 | int fwu_get_dfu_alt_num(u8 image_index, u8 *alt_num); |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 170 | |
| 171 | /** |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 172 | * 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 | */ |
| 181 | int fwu_revert_boot_index(void); |
| 182 | |
| 183 | /** |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 184 | * 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 | */ |
| 196 | int 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 | */ |
| 212 | int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank); |
| 213 | |
Sughosh Ganu | 7d6e2c5 | 2022-10-21 18:15:59 +0530 | [diff] [blame] | 214 | /** |
| 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 | */ |
| 226 | int 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 | */ |
| 242 | int fwu_plat_get_update_index(uint *update_idx); |
Sughosh Ganu | 95b5a7d | 2022-10-21 18:16:00 +0530 | [diff] [blame] | 243 | |
| 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 | */ |
| 254 | void fwu_plat_get_bootidx(uint *boot_idx); |
Sughosh Ganu | 7e9814c | 2022-10-21 18:16:02 +0530 | [diff] [blame] | 255 | |
| 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 | */ |
| 266 | u8 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 | */ |
| 279 | u8 fwu_empty_capsule_checks_pass(void); |
| 280 | |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 281 | /** |
| 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 | */ |
| 290 | int fwu_trial_state_ctr_start(void); |
| 291 | |
Masami Hiramatsu | 4898679 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 292 | /** |
| 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 | */ |
| 302 | int 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 | */ |
| 314 | int fwu_mtd_get_alt_num(efi_guid_t *image_guid, u8 *alt_num, const char *mtd_dev); |
| 315 | |
Sughosh Ganu | 2eaedc9 | 2022-10-21 18:15:55 +0530 | [diff] [blame] | 316 | #endif /* _FWU_H_ */ |