Skip to content

Commit d31932d

Browse files
frazer-armAnton-TF
authored andcommitted
plat: cs1k: Duplicate old images in FWU
When copying existing partitions during a partial firmware update, the GPT library is now used to duplicate the old partitions and then rename them accordingly. This streamlines the steps of 1. creating a new partition for the image to be copied into and 2. the copying itself into a single library call. Change-Id: Ibd169dcc14ed1c946bbd6c30b6962c89055d0e8e Signed-off-by: Frazer Carsley <frazer.carsley@arm.com>
1 parent 3cbf781 commit d31932d

1 file changed

Lines changed: 68 additions & 76 deletions

File tree

platform/ext/target/arm/corstone1000/bootloader/mcuboot/tfm_mcuboot_fwu.c

Lines changed: 68 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
* This is used when bank consistency is maintained during partial capsule update
4040
*/
4141
#define FLASH_CHUNK_SIZE 512
42-
static uint8_t flash_data_buf[FLASH_CHUNK_SIZE];
4342

4443
/* Possible states of the bank.
4544
* Naming convention here matches the implementation in U-Boot
@@ -2174,94 +2173,24 @@ static psa_status_t fwu_update_metadata(const psa_fwu_component_t *candidates, u
21742173
return ret;
21752174
}
21762175

2176+
#ifdef BL1_BUILD
21772177
static psa_status_t copy_image_from_other_bank(int image_index,
21782178
uint32_t active_index,
21792179
uint32_t previous_active_index)
21802180
{
21812181
FWU_LOG_FUNC_ENTER;
21822182

2183+
/* Use offsets directly */
21832184
uint32_t bank_offset[NR_OF_FW_BANKS] = {BANK_0_PARTITION_OFFSET, BANK_1_PARTITION_OFFSET};
21842185
psa_status_t ret;
21852186

2186-
#ifdef BL1_BUILD
21872187
/* Use offsets directly */
2188+
uint8_t data[FLASH_CHUNK_SIZE];
21882189
size_t remaining_size = fwu_image[image_index].image_size;
21892190
size_t data_size;
21902191
size_t offset_read = bank_offset[active_index] + fwu_image[image_index].image_offset;
21912192
size_t offset_write = bank_offset[previous_active_index] + fwu_image[image_index].image_offset;
21922193
int data_transferred_count;
2193-
#else
2194-
/* Use GPT to find the correct image */
2195-
struct partition_entry_t active_part;
2196-
ret = gpt_entry_read_by_type(
2197-
&(fwu_image[image_index].image_type),
2198-
0,
2199-
&active_part);
2200-
if (ret == PSA_ERROR_DOES_NOT_EXIST) {
2201-
FWU_LOG_MSG("%s: Unable to find partition '%s'\r\n",
2202-
__func__, fwu_image[image_index].image_names[active_index]);
2203-
return ret;
2204-
} else if (ret == PSA_ERROR_STORAGE_FAILURE) {
2205-
FWU_LOG_MSG("%s: Flash error whilst reading GPT partition '%s'\r\n",
2206-
__func__, fwu_image[image_index].image_names[active_index]);
2207-
return ret;
2208-
} else if (ret < 0) {
2209-
FWU_LOG_MSG("%s: Unable to read partition '%s'\r\n",
2210-
__func__, fwu_image[image_index].image_names[active_index]);
2211-
return ret;
2212-
}
2213-
2214-
struct partition_entry_t prev_active_part;
2215-
ret = gpt_entry_read_by_type(
2216-
&(fwu_image[image_index].image_type),
2217-
1,
2218-
&prev_active_part);
2219-
2220-
if (ret == PSA_ERROR_DOES_NOT_EXIST) {
2221-
/* Create the partition in the expected space */
2222-
struct efi_guid_t new_guid = {0};
2223-
char unicode_name[GPT_ENTRY_NAME_LENGTH] = {'\0'};
2224-
ascii_to_unicode(fwu_image[image_index].image_names[previous_active_index], unicode_name);
2225-
2226-
ret = gpt_entry_create(&(fwu_image[image_index].image_type),
2227-
(bank_offset[previous_active_index] + fwu_image[image_index].image_offset) / TFM_GPT_BLOCK_SIZE,
2228-
1 + ((fwu_image[image_index].image_size - 1) / TFM_GPT_BLOCK_SIZE),
2229-
0,
2230-
unicode_name,
2231-
&new_guid);
2232-
if (ret == PSA_ERROR_INSUFFICIENT_STORAGE) {
2233-
FWU_LOG_MSG("%s: No space left on device!\r\n", __func__);
2234-
return ret;
2235-
} else if (ret == PSA_ERROR_STORAGE_FAILURE) {
2236-
FWU_LOG_MSG("%s: Flash error whilst creating GPT partition '%s'!\r\n",
2237-
__func__, fwu_image[image_index].image_names[previous_active_index]);
2238-
return ret;
2239-
} else if (ret < 0) {
2240-
return ret;
2241-
}
2242-
2243-
ret = gpt_entry_read(&new_guid, &prev_active_part);
2244-
if (ret == PSA_ERROR_STORAGE_FAILURE) {
2245-
FWU_LOG_MSG("%s: Flash error whilst reading GPT partition '%s'\r\n",
2246-
__func__, fwu_image[image_index].image_names[previous_active_index]);
2247-
return ret;
2248-
} else if (ret < 0) {
2249-
return ret;
2250-
}
2251-
} else if (ret == PSA_ERROR_STORAGE_FAILURE) {
2252-
FWU_LOG_MSG("%s: Flash error whilst reading GPT partition '%s'\r\n",
2253-
__func__, fwu_image[image_index].image_names[previous_active_index]);
2254-
return ret;
2255-
} else if (ret < 0) {
2256-
return ret;
2257-
}
2258-
2259-
size_t remaining_size = prev_active_part.size * TFM_GPT_BLOCK_SIZE;
2260-
size_t data_size;
2261-
size_t offset_read = active_part.start * TFM_GPT_BLOCK_SIZE;
2262-
size_t offset_write = prev_active_part.start * TFM_GPT_BLOCK_SIZE;
2263-
int data_transferred_count;
2264-
#endif /* BL1_BUILD */
22652194

22662195
ret = erase_image(offset_write, remaining_size);
22672196
if (ret != PSA_SUCCESS) {
@@ -2273,7 +2202,7 @@ static psa_status_t copy_image_from_other_bank(int image_index,
22732202
data_size = (remaining_size > FLASH_CHUNK_SIZE) ? FLASH_CHUNK_SIZE : remaining_size;
22742203

22752204
/* read image data from flash */
2276-
data_transferred_count = FWU_METADATA_FLASH_DEV.ReadData(offset_read, flash_data_buf, data_size);
2205+
data_transferred_count = FWU_METADATA_FLASH_DEV.ReadData(offset_read, data, data_size);
22772206
if (data_transferred_count < 0) {
22782207
FWU_LOG_MSG("%s: ERROR - Flash read failed (ret = %d)\n\r", __func__, data_transferred_count);
22792208
return PSA_ERROR_STORAGE_FAILURE;
@@ -2288,7 +2217,7 @@ static psa_status_t copy_image_from_other_bank(int image_index,
22882217
offset_read += data_size;
22892218

22902219
/* write image data to flash */
2291-
data_transferred_count = FWU_METADATA_FLASH_DEV.ProgramData(offset_write, flash_data_buf, data_size);
2220+
data_transferred_count = FWU_METADATA_FLASH_DEV.ProgramData(offset_write, data, data_size);
22922221
if (data_transferred_count < 0) {
22932222
FWU_LOG_MSG("%s: ERROR - Flash read failed (ret = %d)\n\r", __func__, data_transferred_count);
22942223
return PSA_ERROR_STORAGE_FAILURE;
@@ -2307,6 +2236,69 @@ static psa_status_t copy_image_from_other_bank(int image_index,
23072236
FWU_LOG_MSG("%s: exit \n\r", __func__);
23082237
return PSA_SUCCESS;
23092238
}
2239+
#else
2240+
static psa_status_t copy_image_from_other_bank(int image_index,
2241+
uint32_t active_index,
2242+
uint32_t previous_active_index)
2243+
{
2244+
FWU_LOG_FUNC_ENTER;
2245+
2246+
/* Use GPT to find and copy the correct image */
2247+
uint32_t bank_offset[NR_OF_FW_BANKS] = {BANK_0_PARTITION_OFFSET, BANK_1_PARTITION_OFFSET};
2248+
uint64_t new_lba =
2249+
(bank_offset[previous_active_index] + fwu_image[image_index].image_offset) / TFM_GPT_BLOCK_SIZE;
2250+
2251+
struct partition_entry_t active_part;
2252+
psa_status_t ret = gpt_entry_read_by_type(
2253+
&(fwu_image[image_index].image_type),
2254+
0,
2255+
&active_part);
2256+
if (ret == PSA_ERROR_DOES_NOT_EXIST) {
2257+
FWU_LOG_MSG("%s: Unable to find partition '%s'\r\n",
2258+
__func__, fwu_image[image_index].image_names[active_index]);
2259+
return ret;
2260+
} else if (ret == PSA_ERROR_STORAGE_FAILURE) {
2261+
FWU_LOG_MSG("%s: Flash error whilst reading GPT partition '%s'\r\n",
2262+
__func__, fwu_image[image_index].image_names[active_index]);
2263+
return ret;
2264+
} else if (ret < 0) {
2265+
FWU_LOG_MSG("%s: Unable to read partition '%s'\r\n",
2266+
__func__, fwu_image[image_index].image_names[active_index]);
2267+
return ret;
2268+
}
2269+
2270+
struct efi_guid_t new_guid;
2271+
ret = gpt_entry_duplicate(&(active_part.partition_guid), new_lba, &new_guid);
2272+
if (ret == PSA_ERROR_STORAGE_FAILURE) {
2273+
FWU_LOG_MSG("%s: Flash error whilst creating GPT partition '%s'\r\n",
2274+
__func__, fwu_image[image_index].image_names[previous_active_index]);
2275+
return ret;
2276+
} else if (ret < 0) {
2277+
FWU_LOG_MSG("%s: Unable to create partition '%s'\r\n",
2278+
__func__, fwu_image[image_index].image_names[previous_active_index]);
2279+
return ret;
2280+
}
2281+
2282+
char unicode_name[GPT_ENTRY_NAME_LENGTH] = {'\0'};
2283+
ascii_to_unicode(fwu_image[image_index].image_names[previous_active_index], unicode_name);
2284+
ret = gpt_entry_rename(&new_guid, unicode_name);
2285+
if (ret != PSA_SUCCESS) {
2286+
FWU_LOG_MSG("%s: Unable to rename partition to '%s'\r\n",
2287+
__func__, fwu_image[image_index].image_names[previous_active_index]);
2288+
2289+
/* Delete the newly created partition as there is code that relies on the naming */
2290+
ret = gpt_entry_remove(&new_guid);
2291+
if (ret != PSA_SUCCESS) {
2292+
FWU_LOG_MSG("%s: Catastrophic failure: unable to remove duplicate partition '%s'\r\n",
2293+
__func__, fwu_image[image_index].image_names[active_index]);
2294+
}
2295+
return ret;
2296+
}
2297+
2298+
FWU_LOG_MSG("%s: exit \n\r", __func__);
2299+
return PSA_SUCCESS;
2300+
}
2301+
#endif /* BL1_BUILD */
23102302

23112303
static psa_status_t maintain_bank_consistency(void)
23122304
{

0 commit comments

Comments
 (0)