Sughosh Ganu | fba5b3f | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (c) 2024, Linaro Limited |
| 4 | */ |
| 5 | |
| 6 | #include <fwu.h> |
| 7 | #include <fwu_mdata.h> |
| 8 | #include <log.h> |
| 9 | |
| 10 | #include <linux/types.h> |
| 11 | |
| 12 | #define FWU_MDATA_VERSION 0x2U |
Sughosh Ganu | 5e9feee | 2024-09-09 16:50:16 +0530 | [diff] [blame^] | 13 | #define FWU_IMG_DESC_OFFSET 0x20U |
| 14 | |
| 15 | static struct fwu_mdata g_mdata; |
Sughosh Ganu | fba5b3f | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 16 | |
| 17 | static inline struct fwu_fw_store_desc *fwu_get_fw_desc(struct fwu_mdata *mdata) |
| 18 | { |
| 19 | return (struct fwu_fw_store_desc *)((u8 *)mdata + sizeof(*mdata)); |
| 20 | } |
| 21 | |
| 22 | static uint32_t fwu_check_trial_state(struct fwu_mdata *mdata, uint32_t bank) |
| 23 | { |
| 24 | return mdata->bank_state[bank] == FWU_BANK_VALID ? 1 : 0; |
| 25 | } |
| 26 | |
| 27 | static void fwu_data_init(void) |
| 28 | { |
| 29 | int i; |
| 30 | size_t image_info_size; |
| 31 | void *dst_img_info, *src_img_info; |
| 32 | struct fwu_data *data = fwu_get_data(); |
| 33 | struct fwu_mdata *mdata = data->fwu_mdata; |
| 34 | |
| 35 | data->crc32 = mdata->crc32; |
| 36 | data->version = mdata->version; |
| 37 | data->active_index = mdata->active_index; |
| 38 | data->previous_active_index = mdata->previous_active_index; |
| 39 | data->metadata_size = mdata->metadata_size; |
| 40 | fwu_plat_get_bootidx(&data->boot_index); |
| 41 | data->trial_state = fwu_check_trial_state(mdata, data->boot_index); |
| 42 | |
| 43 | data->num_banks = fwu_get_fw_desc(mdata)->num_banks; |
| 44 | data->num_images = fwu_get_fw_desc(mdata)->num_images; |
| 45 | |
| 46 | for (i = 0; i < 4; i++) { |
| 47 | data->bank_state[i] = mdata->bank_state[i]; |
| 48 | } |
| 49 | |
| 50 | image_info_size = sizeof(data->fwu_images); |
| 51 | src_img_info = &fwu_get_fw_desc(mdata)->img_entry[0]; |
| 52 | dst_img_info = &data->fwu_images[0]; |
| 53 | |
| 54 | memcpy(dst_img_info, src_img_info, image_info_size); |
| 55 | } |
| 56 | |
| 57 | static int fwu_mdata_sanity_checks(void) |
| 58 | { |
| 59 | uint8_t num_banks; |
| 60 | uint16_t num_images; |
| 61 | struct fwu_data *data = fwu_get_data(); |
| 62 | struct fwu_mdata *mdata = data->fwu_mdata; |
| 63 | |
Sughosh Ganu | fba5b3f | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 64 | num_banks = fwu_get_fw_desc(mdata)->num_banks; |
| 65 | num_images = fwu_get_fw_desc(mdata)->num_images; |
| 66 | |
| 67 | if (num_banks != CONFIG_FWU_NUM_BANKS) { |
| 68 | log_err("Number of Banks(%u) in FWU Metadata different from the configured value(%d)", |
| 69 | num_banks, CONFIG_FWU_NUM_BANKS); |
| 70 | return -EINVAL; |
| 71 | } |
| 72 | |
| 73 | if (num_images != CONFIG_FWU_NUM_IMAGES_PER_BANK) { |
| 74 | log_err("Number of Images(%u) in FWU Metadata different from the configured value(%d)", |
| 75 | num_images, CONFIG_FWU_NUM_IMAGES_PER_BANK); |
| 76 | return -EINVAL; |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static int fwu_bank_state_update(bool trial_state, uint32_t bank) |
| 83 | { |
| 84 | int ret; |
| 85 | struct fwu_data *data = fwu_get_data(); |
| 86 | struct fwu_mdata *mdata = data->fwu_mdata; |
| 87 | |
| 88 | mdata->bank_state[bank] = data->bank_state[bank] = trial_state ? |
| 89 | FWU_BANK_VALID : FWU_BANK_ACCEPTED; |
| 90 | |
| 91 | ret = fwu_sync_mdata(mdata, BOTH_PARTS); |
| 92 | if (ret) |
| 93 | log_err("Unable to set bank_state for bank %u\n", bank); |
| 94 | else |
| 95 | data->trial_state = trial_state; |
| 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | static int fwu_trial_state_start(uint update_index) |
| 101 | { |
| 102 | int ret; |
| 103 | |
| 104 | ret = fwu_trial_state_ctr_start(); |
| 105 | if (ret) |
| 106 | return ret; |
| 107 | |
| 108 | ret = fwu_bank_state_update(1, update_index); |
| 109 | if (ret) |
| 110 | return ret; |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
Sughosh Ganu | 5e9feee | 2024-09-09 16:50:16 +0530 | [diff] [blame^] | 115 | static bool fwu_get_mdata_mandatory(uint part) |
| 116 | { |
| 117 | int ret = 0; |
| 118 | struct udevice *fwu_dev = fwu_get_dev(); |
| 119 | |
| 120 | memset(&g_mdata, 0, sizeof(struct fwu_mdata)); |
| 121 | |
| 122 | ret = fwu_read_mdata(fwu_dev, &g_mdata, |
| 123 | part == PRIMARY_PART ? true : false, |
| 124 | sizeof(struct fwu_mdata)); |
| 125 | if (ret) |
| 126 | return false; |
| 127 | |
| 128 | if (g_mdata.version != FWU_MDATA_VERSION) { |
| 129 | log_err("FWU partition %u has metadata version %u. Expected value of %u\n", |
| 130 | part, g_mdata.version, FWU_MDATA_VERSION); |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | if (g_mdata.desc_offset != FWU_IMG_DESC_OFFSET) { |
| 135 | log_err("Descriptor Offset(0x%x) in the FWU Metadata partition %u not equal to 0x20\n", |
| 136 | g_mdata.desc_offset, part); |
| 137 | log_err("Image information expected in the metadata\n"); |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | return true; |
| 142 | } |
| 143 | |
Sughosh Ganu | fba5b3f | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 144 | /** |
| 145 | * fwu_populate_mdata_image_info() - Populate the image information |
| 146 | * of the metadata |
| 147 | * @data: Version agnostic FWU metadata information |
| 148 | * |
| 149 | * Populate the image information in the FWU metadata by copying it |
| 150 | * from the version agnostic structure. This is done before the |
| 151 | * metadata gets written to the storage media. |
| 152 | * |
| 153 | * Return: None |
| 154 | */ |
| 155 | void fwu_populate_mdata_image_info(struct fwu_data *data) |
| 156 | { |
| 157 | size_t image_info_size; |
| 158 | struct fwu_mdata *mdata = data->fwu_mdata; |
| 159 | void *dst_img_info, *src_img_info; |
| 160 | |
| 161 | image_info_size = sizeof(data->fwu_images); |
| 162 | dst_img_info = &fwu_get_fw_desc(mdata)->img_entry[0]; |
| 163 | src_img_info = &data->fwu_images[0]; |
| 164 | |
| 165 | memcpy(dst_img_info, src_img_info, image_info_size); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * fwu_state_machine_updates() - Update FWU state of the platform |
| 170 | * @trial_state: Is platform transitioning into Trial State |
| 171 | * @update_index: Bank number to which images have been updated |
| 172 | * |
| 173 | * On successful completion of updates, transition the platform to |
| 174 | * either Trial State or Regular State. |
| 175 | * |
| 176 | * To transition the platform to Trial State, start the |
| 177 | * TrialStateCtr counter, followed by setting the value of bank_state |
| 178 | * field of the metadata to Valid state(applicable only in version 2 |
| 179 | * of metadata). |
| 180 | * |
| 181 | * In case, the platform is to transition directly to Regular State, |
| 182 | * update the bank_state field of the metadata to Accepted |
| 183 | * state(applicable only in version 2 of metadata). |
| 184 | * |
| 185 | * Return: 0 if OK, -ve on error |
| 186 | */ |
| 187 | int fwu_state_machine_updates(bool trial_state, uint32_t update_index) |
| 188 | { |
| 189 | return trial_state ? fwu_trial_state_start(update_index) : |
| 190 | fwu_bank_state_update(0, update_index); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * fwu_get_mdata_size() - Get the FWU metadata size |
| 195 | * @mdata_size: Size of the metadata structure |
| 196 | * |
| 197 | * Get the size of the FWU metadata from the structure. This is later used |
| 198 | * to allocate memory for the structure. |
| 199 | * |
| 200 | * Return: 0 if OK, -ve on error |
| 201 | */ |
| 202 | int fwu_get_mdata_size(uint32_t *mdata_size) |
| 203 | { |
Sughosh Ganu | fba5b3f | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 204 | struct fwu_data *data = fwu_get_data(); |
Sughosh Ganu | fba5b3f | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 205 | |
| 206 | if (data->metadata_size) { |
| 207 | *mdata_size = data->metadata_size; |
| 208 | return 0; |
| 209 | } |
| 210 | |
Sughosh Ganu | 5e9feee | 2024-09-09 16:50:16 +0530 | [diff] [blame^] | 211 | *mdata_size = g_mdata.metadata_size; |
Sughosh Ganu | fba5b3f | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 212 | if (!*mdata_size) |
| 213 | return -EINVAL; |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * fwu_init() - FWU specific initialisations |
| 220 | * |
| 221 | * Carry out some FWU specific initialisations including allocation |
| 222 | * of memory for the metadata copies, and reading the FWU metadata |
| 223 | * copies into the allocated memory. The metadata fields are then |
| 224 | * copied into a version agnostic structure. |
| 225 | * |
| 226 | * Return: 0 if OK, -ve on error |
| 227 | */ |
| 228 | int fwu_init(void) |
| 229 | { |
| 230 | int ret; |
Sughosh Ganu | fba5b3f | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 231 | |
| 232 | /* |
| 233 | * First we read only the top level structure |
| 234 | * and get the size of the complete structure. |
Sughosh Ganu | 5e9feee | 2024-09-09 16:50:16 +0530 | [diff] [blame^] | 235 | * Try reading the first partition first, if |
| 236 | * that does not work, try the secondary |
| 237 | * partition. The idea is, if one of the |
| 238 | * partitions is corrupted, it should be restored |
| 239 | * from the intact partition. |
Sughosh Ganu | fba5b3f | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 240 | */ |
Sughosh Ganu | 5e9feee | 2024-09-09 16:50:16 +0530 | [diff] [blame^] | 241 | if (!fwu_get_mdata_mandatory(PRIMARY_PART) && |
| 242 | !fwu_get_mdata_mandatory(SECONDARY_PART)) { |
Sughosh Ganu | fba5b3f | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 243 | log_err("FWU metadata read failed\n"); |
Sughosh Ganu | 5e9feee | 2024-09-09 16:50:16 +0530 | [diff] [blame^] | 244 | return -1; |
Sughosh Ganu | fba5b3f | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 245 | } |
| 246 | |
Sughosh Ganu | 5e9feee | 2024-09-09 16:50:16 +0530 | [diff] [blame^] | 247 | ret = fwu_mdata_copies_allocate(g_mdata.metadata_size); |
Sughosh Ganu | fba5b3f | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 248 | if (ret) |
| 249 | return ret; |
| 250 | |
| 251 | /* |
| 252 | * Now read the entire structure, both copies, and |
| 253 | * validate that the copies. |
| 254 | */ |
| 255 | ret = fwu_get_mdata(NULL); |
| 256 | if (ret) |
| 257 | return ret; |
| 258 | |
| 259 | ret = fwu_mdata_sanity_checks(); |
| 260 | if (ret) |
| 261 | return ret; |
| 262 | |
| 263 | fwu_data_init(); |
| 264 | |
| 265 | return 0; |
| 266 | } |