lib: avb: support last boot

Change-Id: I803e3deda068be34061a302c27009db5e444f290
Signed-off-by: Jason Zhu <jason.zhu@rock-chips.com>
This commit is contained in:
Jason Zhu 2019-02-28 20:56:46 +08:00 committed by Jianhong Chen
parent 4454e90b43
commit 7cca3dd4d7
4 changed files with 853 additions and 810 deletions

View File

@ -96,7 +96,8 @@ typedef struct AvbABData {
AvbABSlotData slots[2];
/* Reserved for future use. */
uint8_t reserved2[12];
uint8_t last_boot;
uint8_t reserved2[11];
/* CRC32 of all 28 bytes preceding this field. */
uint32_t crc32;

View File

@ -267,6 +267,14 @@ int rk_auth_unlock(void *buffer, char *out_is_trusted);
*/
int rk_generate_unlock_challenge(void *buffer, uint32_t *challenge_len);
/**
* Get last boot slot
*
* @return 0 is slot A; 1 is slot B; -1 is error
*/
int rk_get_lastboot(void);
#ifdef __cplusplus
}
#endif

View File

@ -64,6 +64,7 @@ void avb_ab_data_init(AvbABData* data) {
avb_memcpy(data->magic, AVB_AB_MAGIC, AVB_AB_MAGIC_LEN);
data->version_major = AVB_AB_MAJOR_VERSION;
data->version_minor = AVB_AB_MINOR_VERSION;
data->last_boot = 0;
data->slots[0].priority = AVB_AB_MAX_PRIORITY;
data->slots[0].tries_remaining = AVB_AB_MAX_TRIES_REMAINING;
data->slots[0].successful_boot = 0;

View File

@ -117,8 +117,14 @@ int rk_avb_get_current_slot(char *select_slot)
}
if (rk_avb_ab_slot_select(ops->ab_ops, select_slot) != 0) {
printf("get_current_slot error!\n");
ret = -1;
printf("###There is no bootable slot, bring up last_boot!###\n");
if (rk_get_lastboot() == 1)
memcpy(select_slot, "_b", 2);
else if(rk_get_lastboot() == 0)
memcpy(select_slot, "_a", 2);
else
return -1;
ret = 0;
}
avb_ops_user_free(ops);
@ -697,3 +703,30 @@ int rk_generate_unlock_challenge(void *buffer, uint32_t *challenge_len)
else
return -1;
}
int rk_get_lastboot(void)
{
AvbIOResult io_ret = AVB_IO_RESULT_OK;
AvbABData ab_data;
int lastboot = -1;
AvbOps* ops;
ops = avb_ops_user_new();
if (ops == NULL) {
printf("avb_ops_user_new() failed!\n");
return -1;
}
io_ret = ops->ab_ops->read_ab_metadata(ops->ab_ops, &ab_data);
if (io_ret != AVB_IO_RESULT_OK) {
avb_error("I/O error while loading A/B metadata.\n");
goto out;
}
lastboot = ab_data.last_boot;
out:
avb_ops_user_free(ops);
return lastboot;
}