bootloader: Don't unnecessarily test OTA app slots more than once per boot

Would loop more than once through each OTA possibility, but really only
needs to try each slot once.
This commit is contained in:
Angus Gratton 2017-08-18 12:08:38 +10:00 committed by Angus Gratton
parent 2861f3e88e
commit 43e231c916

View file

@ -262,8 +262,8 @@ static esp_partition_pos_t index_to_partition(const bootloader_state_t *bs, int
return bs->test; return bs->test;
} }
if (index >= 0 && index < MAX_OTA_SLOTS) { if (index >= 0 && index < MAX_OTA_SLOTS && index < bs->app_count) {
return bs->ota[index % bs->app_count]; return bs->ota[index];
} }
esp_partition_pos_t invalid = { 0 }; esp_partition_pos_t invalid = { 0 };
@ -272,15 +272,16 @@ static esp_partition_pos_t index_to_partition(const bootloader_state_t *bs, int
static void log_invalid_app_partition(int index) static void log_invalid_app_partition(int index)
{ {
const char *not_bootable = " is not bootable"; /* save a few string literal bytes */
switch(index) { switch(index) {
case FACTORY_INDEX: case FACTORY_INDEX:
ESP_LOGE(TAG, "Factory app partition is not bootable"); ESP_LOGE(TAG, "Factory app partition%s", not_bootable);
break; break;
case TEST_APP_INDEX: case TEST_APP_INDEX:
ESP_LOGE(TAG, "Factory test app partition is not bootable"); ESP_LOGE(TAG, "Factory test app partition%s", not_bootable);
break; break;
default: default:
ESP_LOGE(TAG, "OTA app partition slot %d is not bootable", index); ESP_LOGE(TAG, "OTA app partition slot %d%s", index, not_bootable);
break; break;
} }
} }
@ -367,6 +368,8 @@ static bool try_load_partition(const esp_partition_pos_t *partition, esp_image_m
return false; return false;
} }
#define TRY_LOG_FORMAT "Trying partition index %d offs 0x%x size 0x%x"
/* Load the app for booting. Start from partition 'start_index', if not bootable then work backwards to FACTORY_INDEX /* Load the app for booting. Start from partition 'start_index', if not bootable then work backwards to FACTORY_INDEX
* (ie try any OTA slots in descending order and then the factory partition). * (ie try any OTA slots in descending order and then the factory partition).
* *
@ -382,29 +385,29 @@ static bool load_boot_image(const bootloader_state_t *bs, int start_index, esp_i
esp_partition_pos_t part; esp_partition_pos_t part;
/* work backwards from start_index, down to the factory app */ /* work backwards from start_index, down to the factory app */
do { for(index = start_index; index >= FACTORY_INDEX; index--) {
ESP_LOGD(TAG, "Trying partition index %d...", index);
part = index_to_partition(bs, index); part = index_to_partition(bs, index);
ESP_LOGD(TAG, "part offs 0x%x size 0x%x", part.offset, part.size); if (part.size == 0) {
if (try_load_partition(&part, result)) { continue;
return true;
} }
if (part.size > 0) { ESP_LOGD(TAG, TRY_LOG_FORMAT, index, part.offset, part.size);
log_invalid_app_partition(index); if (try_load_partition(&part, result)) {
} return true;
index--; }
} while(index >= FACTORY_INDEX); log_invalid_app_partition(index);
}
/* failing that work forwards from start_index, try valid OTA slots */
index = start_index + 1; /* failing that work forwards from start_index, try valid OTA slots */
while (index < bs->app_count) { for(index = start_index + 1; index < bs->app_count; index++) {
ESP_LOGD(TAG, "Trying partition index %d...", index); part = index_to_partition(bs, index);
part = index_to_partition(bs, index); if (part.size == 0) {
continue;
}
ESP_LOGD(TAG, TRY_LOG_FORMAT, index, part.offset, part.size);
if (try_load_partition(&part, result)) { if (try_load_partition(&part, result)) {
return true; return true;
} }
log_invalid_app_partition(index); log_invalid_app_partition(index);
index++;
} }
if (try_load_partition(&bs->test, result)) { if (try_load_partition(&bs->test, result)) {