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 | |
| 55 | static int fwu_trial_state_update(bool trial_state) |
| 56 | { |
| 57 | int ret; |
| 58 | struct fwu_data *data = fwu_get_data(); |
| 59 | |
| 60 | if (trial_state) { |
| 61 | ret = fwu_trial_state_ctr_start(); |
| 62 | if (ret) |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | data->trial_state = trial_state; |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * fwu_populate_mdata_image_info() - Populate the image information |
| 73 | * of the metadata |
| 74 | * @data: Version agnostic FWU metadata information |
| 75 | * |
| 76 | * Populate the image information in the FWU metadata by copying it |
| 77 | * from the version agnostic structure. This is done before the |
| 78 | * metadata gets written to the storage media. |
| 79 | * |
| 80 | * Return: None |
| 81 | */ |
| 82 | void fwu_populate_mdata_image_info(struct fwu_data *data) |
| 83 | { |
| 84 | size_t image_info_size; |
| 85 | void *dst_img_info, *src_img_info; |
| 86 | struct fwu_mdata *mdata = data->fwu_mdata; |
| 87 | |
| 88 | image_info_size = sizeof(data->fwu_images); |
| 89 | dst_img_info = &mdata->img_entry[0]; |
| 90 | src_img_info = &data->fwu_images[0]; |
| 91 | |
| 92 | memcpy(dst_img_info, src_img_info, image_info_size); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * fwu_state_machine_updates() - Update FWU state of the platform |
| 97 | * @trial_state: Is platform transitioning into Trial State |
| 98 | * @update_index: Bank number to which images have been updated |
| 99 | * |
| 100 | * On successful completion of updates, transition the platform to |
| 101 | * either Trial State or Regular State. |
| 102 | * |
| 103 | * To transition the platform to Trial State, start the |
| 104 | * TrialStateCtr counter, followed by setting the value of bank_state |
| 105 | * field of the metadata to Valid state(applicable only in version 2 |
| 106 | * of metadata). |
| 107 | * |
| 108 | * In case, the platform is to transition directly to Regular State, |
| 109 | * update the bank_state field of the metadata to Accepted |
| 110 | * state(applicable only in version 2 of metadata). |
| 111 | * |
| 112 | * Return: 0 if OK, -ve on error |
| 113 | */ |
| 114 | int fwu_state_machine_updates(bool trial_state, |
| 115 | __maybe_unused uint32_t update_index) |
| 116 | { |
| 117 | return fwu_trial_state_update(trial_state); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * fwu_get_mdata_size() - Get the FWU metadata size |
| 122 | * @mdata_size: Size of the metadata structure |
| 123 | * |
| 124 | * Get the size of the FWU metadata. |
| 125 | * |
| 126 | * Return: 0 if OK, -ve on error |
| 127 | */ |
| 128 | int fwu_get_mdata_size(uint32_t *mdata_size) |
| 129 | { |
| 130 | *mdata_size = sizeof(struct fwu_mdata); |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * fwu_init() - FWU specific initialisations |
| 137 | * |
| 138 | * Carry out some FWU specific initialisations including allocation |
| 139 | * of memory for the metadata copies, and reading the FWU metadata |
| 140 | * copies into the allocated memory. The metadata fields are then |
| 141 | * copied into a version agnostic structure. |
| 142 | * |
| 143 | * Return: 0 if OK, -ve on error |
| 144 | */ |
| 145 | int fwu_init(void) |
| 146 | { |
| 147 | int ret; |
| 148 | uint32_t mdata_size; |
| 149 | |
| 150 | fwu_get_mdata_size(&mdata_size); |
| 151 | |
| 152 | ret = fwu_mdata_copies_allocate(mdata_size); |
| 153 | if (ret) |
| 154 | return ret; |
| 155 | |
| 156 | /* |
| 157 | * Now read the entire structure, both copies, and |
| 158 | * validate that the copies. |
| 159 | */ |
| 160 | ret = fwu_get_mdata(NULL); |
| 161 | if (ret) |
| 162 | return ret; |
| 163 | |
| 164 | fwu_data_init(); |
| 165 | |
| 166 | return 0; |
| 167 | } |