Sughosh Ganu | a56a10b | 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 | |
| 9 | #include <linux/types.h> |
| 10 | |
| 11 | #define FWU_MDATA_VERSION 0x1U |
| 12 | |
| 13 | static uint32_t fwu_check_trial_state(struct fwu_mdata *mdata, uint32_t bank) |
| 14 | { |
| 15 | u32 i; |
| 16 | struct fwu_image_entry *img_entry; |
| 17 | struct fwu_image_bank_info *img_bank_info; |
| 18 | |
| 19 | img_entry = &mdata->img_entry[0]; |
| 20 | for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) { |
| 21 | img_bank_info = &img_entry[i].img_bank_info[bank]; |
| 22 | if (!img_bank_info->accepted) { |
| 23 | return 1; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | static void fwu_data_init(void) |
| 31 | { |
| 32 | size_t image_info_size; |
| 33 | void *dst_img_info, *src_img_info; |
| 34 | struct fwu_data *data = fwu_get_data(); |
| 35 | struct fwu_mdata *mdata = data->fwu_mdata; |
| 36 | |
| 37 | data->crc32 = mdata->crc32; |
| 38 | data->version = mdata->version; |
| 39 | data->active_index = mdata->active_index; |
| 40 | data->previous_active_index = mdata->previous_active_index; |
| 41 | |
| 42 | data->metadata_size = sizeof(struct fwu_mdata); |
| 43 | data->num_banks = CONFIG_FWU_NUM_BANKS; |
| 44 | data->num_images = CONFIG_FWU_NUM_IMAGES_PER_BANK; |
| 45 | fwu_plat_get_bootidx(&data->boot_index); |
| 46 | data->trial_state = fwu_check_trial_state(mdata, data->boot_index); |
| 47 | |
| 48 | src_img_info = &mdata->img_entry[0]; |
| 49 | dst_img_info = &data->fwu_images[0]; |
| 50 | image_info_size = sizeof(data->fwu_images); |
| 51 | |
| 52 | memcpy(dst_img_info, src_img_info, image_info_size); |
| 53 | } |
| 54 | |
Sughosh Ganu | 4c63b7e | 2024-09-09 16:50:18 +0530 | [diff] [blame] | 55 | static int fwu_trial_state_update(bool trial_state, uint32_t bank) |
Sughosh Ganu | a56a10b | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 56 | { |
| 57 | int ret; |
| 58 | struct fwu_data *data = fwu_get_data(); |
| 59 | |
Sughosh Ganu | 4c63b7e | 2024-09-09 16:50:18 +0530 | [diff] [blame] | 60 | if (!trial_state && !fwu_bank_accepted(data, bank)) |
| 61 | return 0; |
| 62 | |
Sughosh Ganu | a56a10b | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 63 | if (trial_state) { |
| 64 | ret = fwu_trial_state_ctr_start(); |
| 65 | if (ret) |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | data->trial_state = trial_state; |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * fwu_populate_mdata_image_info() - Populate the image information |
| 76 | * of the metadata |
| 77 | * @data: Version agnostic FWU metadata information |
| 78 | * |
| 79 | * Populate the image information in the FWU metadata by copying it |
| 80 | * from the version agnostic structure. This is done before the |
| 81 | * metadata gets written to the storage media. |
| 82 | * |
| 83 | * Return: None |
| 84 | */ |
| 85 | void fwu_populate_mdata_image_info(struct fwu_data *data) |
| 86 | { |
| 87 | size_t image_info_size; |
| 88 | void *dst_img_info, *src_img_info; |
| 89 | struct fwu_mdata *mdata = data->fwu_mdata; |
| 90 | |
| 91 | image_info_size = sizeof(data->fwu_images); |
| 92 | dst_img_info = &mdata->img_entry[0]; |
| 93 | src_img_info = &data->fwu_images[0]; |
| 94 | |
| 95 | memcpy(dst_img_info, src_img_info, image_info_size); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * fwu_state_machine_updates() - Update FWU state of the platform |
| 100 | * @trial_state: Is platform transitioning into Trial State |
| 101 | * @update_index: Bank number to which images have been updated |
| 102 | * |
| 103 | * On successful completion of updates, transition the platform to |
| 104 | * either Trial State or Regular State. |
| 105 | * |
| 106 | * To transition the platform to Trial State, start the |
| 107 | * TrialStateCtr counter, followed by setting the value of bank_state |
| 108 | * field of the metadata to Valid state(applicable only in version 2 |
| 109 | * of metadata). |
| 110 | * |
| 111 | * In case, the platform is to transition directly to Regular State, |
| 112 | * update the bank_state field of the metadata to Accepted |
| 113 | * state(applicable only in version 2 of metadata). |
| 114 | * |
| 115 | * Return: 0 if OK, -ve on error |
| 116 | */ |
| 117 | int fwu_state_machine_updates(bool trial_state, |
Sughosh Ganu | 4c63b7e | 2024-09-09 16:50:18 +0530 | [diff] [blame] | 118 | uint32_t update_index) |
Sughosh Ganu | a56a10b | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 119 | { |
Sughosh Ganu | 4c63b7e | 2024-09-09 16:50:18 +0530 | [diff] [blame] | 120 | return fwu_trial_state_update(trial_state, update_index); |
Sughosh Ganu | a56a10b | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * fwu_get_mdata_size() - Get the FWU metadata size |
| 125 | * @mdata_size: Size of the metadata structure |
| 126 | * |
| 127 | * Get the size of the FWU metadata. |
| 128 | * |
| 129 | * Return: 0 if OK, -ve on error |
| 130 | */ |
| 131 | int fwu_get_mdata_size(uint32_t *mdata_size) |
| 132 | { |
| 133 | *mdata_size = sizeof(struct fwu_mdata); |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * fwu_init() - FWU specific initialisations |
| 140 | * |
| 141 | * Carry out some FWU specific initialisations including allocation |
| 142 | * of memory for the metadata copies, and reading the FWU metadata |
| 143 | * copies into the allocated memory. The metadata fields are then |
| 144 | * copied into a version agnostic structure. |
| 145 | * |
| 146 | * Return: 0 if OK, -ve on error |
| 147 | */ |
| 148 | int fwu_init(void) |
| 149 | { |
| 150 | int ret; |
| 151 | uint32_t mdata_size; |
Sughosh Ganu | 97c9fb3 | 2024-09-09 16:50:17 +0530 | [diff] [blame] | 152 | struct fwu_mdata mdata = {0}; |
Sughosh Ganu | a56a10b | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 153 | |
| 154 | fwu_get_mdata_size(&mdata_size); |
| 155 | |
| 156 | ret = fwu_mdata_copies_allocate(mdata_size); |
| 157 | if (ret) |
| 158 | return ret; |
| 159 | |
| 160 | /* |
| 161 | * Now read the entire structure, both copies, and |
| 162 | * validate that the copies. |
| 163 | */ |
Sughosh Ganu | 97c9fb3 | 2024-09-09 16:50:17 +0530 | [diff] [blame] | 164 | ret = fwu_get_mdata(&mdata); |
Sughosh Ganu | a56a10b | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 165 | if (ret) |
| 166 | return ret; |
| 167 | |
Sughosh Ganu | 97c9fb3 | 2024-09-09 16:50:17 +0530 | [diff] [blame] | 168 | if (mdata.version != 0x1) { |
| 169 | log_err("FWU metadata version %u. Expected value of %u\n", |
| 170 | mdata.version, FWU_MDATA_VERSION); |
| 171 | return -EINVAL; |
| 172 | } |
| 173 | |
Sughosh Ganu | a56a10b | 2024-03-22 16:27:20 +0530 | [diff] [blame] | 174 | fwu_data_init(); |
| 175 | |
| 176 | return 0; |
| 177 | } |