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

@ -1,277 +1,278 @@
/* /*
* Copyright (C) 2016 The Android Open Source Project * Copyright (C) 2016 The Android Open Source Project
* *
* Permission is hereby granted, free of charge, to any person * Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation * obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without * files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, * restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies * modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is * of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be * The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software. * included in all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
/* /*
#if !defined(AVB_INSIDE_LIBAVB_AB_H) && !defined(AVB_COMPILATION) #if !defined(AVB_INSIDE_LIBAVB_AB_H) && !defined(AVB_COMPILATION)
#error \ #error \
"Never include this file directly, include libavb_ab/libavb_ab.h instead." "Never include this file directly, include libavb_ab/libavb_ab.h instead."
#endif #endif
*/ */
#ifndef AVB_AB_FLOW_H_ #ifndef AVB_AB_FLOW_H_
#define AVB_AB_FLOW_H_ #define AVB_AB_FLOW_H_
#include <android_avb/avb_ab_ops.h> #include <android_avb/avb_ab_ops.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/* Magic for the A/B struct when serialized. */ /* Magic for the A/B struct when serialized. */
#define AVB_AB_MAGIC "\0AB0" #define AVB_AB_MAGIC "\0AB0"
#define AVB_AB_MAGIC_LEN 4 #define AVB_AB_MAGIC_LEN 4
/* Versioning for the on-disk A/B metadata - keep in sync with avbtool. */ /* Versioning for the on-disk A/B metadata - keep in sync with avbtool. */
#define AVB_AB_MAJOR_VERSION 1 #define AVB_AB_MAJOR_VERSION 1
#define AVB_AB_MINOR_VERSION 0 #define AVB_AB_MINOR_VERSION 0
/* Size of AvbABData struct. */ /* Size of AvbABData struct. */
#define AVB_AB_DATA_SIZE 32 #define AVB_AB_DATA_SIZE 32
/* Maximum values for slot data */ /* Maximum values for slot data */
#define AVB_AB_MAX_PRIORITY 15 #define AVB_AB_MAX_PRIORITY 15
#define AVB_AB_MAX_TRIES_REMAINING 7 #define AVB_AB_MAX_TRIES_REMAINING 7
/* Struct used for recording per-slot metadata. /* Struct used for recording per-slot metadata.
* *
* When serialized, data is stored in network byte-order. * When serialized, data is stored in network byte-order.
*/ */
typedef struct AvbABSlotData { typedef struct AvbABSlotData {
/* Slot priority. Valid values range from 0 to AVB_AB_MAX_PRIORITY, /* Slot priority. Valid values range from 0 to AVB_AB_MAX_PRIORITY,
* both inclusive with 1 being the lowest and AVB_AB_MAX_PRIORITY * both inclusive with 1 being the lowest and AVB_AB_MAX_PRIORITY
* being the highest. The special value 0 is used to indicate the * being the highest. The special value 0 is used to indicate the
* slot is unbootable. * slot is unbootable.
*/ */
uint8_t priority; uint8_t priority;
/* Number of times left attempting to boot this slot ranging from 0 /* Number of times left attempting to boot this slot ranging from 0
* to AVB_AB_MAX_TRIES_REMAINING. * to AVB_AB_MAX_TRIES_REMAINING.
*/ */
uint8_t tries_remaining; uint8_t tries_remaining;
/* Non-zero if this slot has booted successfully, 0 otherwise. */ /* Non-zero if this slot has booted successfully, 0 otherwise. */
uint8_t successful_boot; uint8_t successful_boot;
/* Reserved for future use. */ /* Reserved for future use. */
uint8_t reserved[1]; uint8_t reserved[1];
} AVB_ATTR_PACKED AvbABSlotData; } AVB_ATTR_PACKED AvbABSlotData;
/* Struct used for recording A/B metadata. /* Struct used for recording A/B metadata.
* *
* When serialized, data is stored in network byte-order. * When serialized, data is stored in network byte-order.
*/ */
typedef struct AvbABData { typedef struct AvbABData {
/* Magic number used for identification - see AVB_AB_MAGIC. */ /* Magic number used for identification - see AVB_AB_MAGIC. */
uint8_t magic[AVB_AB_MAGIC_LEN]; uint8_t magic[AVB_AB_MAGIC_LEN];
/* Version of on-disk struct - see AVB_AB_{MAJOR,MINOR}_VERSION. */ /* Version of on-disk struct - see AVB_AB_{MAJOR,MINOR}_VERSION. */
uint8_t version_major; uint8_t version_major;
uint8_t version_minor; uint8_t version_minor;
/* Padding to ensure |slots| field start eight bytes in. */ /* Padding to ensure |slots| field start eight bytes in. */
uint8_t reserved1[2]; uint8_t reserved1[2];
/* Per-slot metadata. */ /* Per-slot metadata. */
AvbABSlotData slots[2]; AvbABSlotData slots[2];
/* Reserved for future use. */ /* 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; /* CRC32 of all 28 bytes preceding this field. */
} AVB_ATTR_PACKED AvbABData; uint32_t crc32;
} AVB_ATTR_PACKED AvbABData;
/* Copies |src| to |dest|, byte-swapping fields in the
* process. Returns false if the data is invalid (e.g. wrong magic, /* Copies |src| to |dest|, byte-swapping fields in the
* wrong CRC32 etc.), true otherwise. * process. Returns false if the data is invalid (e.g. wrong magic,
*/ * wrong CRC32 etc.), true otherwise.
bool avb_ab_data_verify_and_byteswap(const AvbABData* src, AvbABData* dest); */
bool avb_ab_data_verify_and_byteswap(const AvbABData* src, AvbABData* dest);
/* Copies |src| to |dest|, byte-swapping fields in the process. Also
* updates the |crc32| field in |dest|. /* Copies |src| to |dest|, byte-swapping fields in the process. Also
*/ * updates the |crc32| field in |dest|.
void avb_ab_data_update_crc_and_byteswap(const AvbABData* src, AvbABData* dest); */
void avb_ab_data_update_crc_and_byteswap(const AvbABData* src, AvbABData* dest);
/* Initializes |data| such that it has two slots and both slots have
* maximum tries remaining. The CRC is not set. /* Initializes |data| such that it has two slots and both slots have
*/ * maximum tries remaining. The CRC is not set.
void avb_ab_data_init(AvbABData* data); */
void avb_ab_data_init(AvbABData* data);
/* Reads A/B metadata from the 'misc' partition using |ops|. Returned
* data is properly byteswapped. Returns AVB_IO_RESULT_OK on /* Reads A/B metadata from the 'misc' partition using |ops|. Returned
* success, error code otherwise. * data is properly byteswapped. Returns AVB_IO_RESULT_OK on
* * success, error code otherwise.
* If the data read from disk is invalid (e.g. wrong magic or CRC *
* checksum failure), the metadata will be reset using * If the data read from disk is invalid (e.g. wrong magic or CRC
* avb_ab_data_init() and then written to disk. * checksum failure), the metadata will be reset using
*/ * avb_ab_data_init() and then written to disk.
AvbIOResult avb_ab_data_read(AvbABOps* ab_ops, AvbABData* data); */
AvbIOResult avb_ab_data_read(AvbABOps* ab_ops, AvbABData* data);
/* Writes A/B metadata to the 'misc' partition using |ops|. This will
* byteswap and update the CRC as needed. Returns AVB_IO_RESULT_OK on /* Writes A/B metadata to the 'misc' partition using |ops|. This will
* success, error code otherwise. * byteswap and update the CRC as needed. Returns AVB_IO_RESULT_OK on
*/ * success, error code otherwise.
AvbIOResult avb_ab_data_write(AvbABOps* ab_ops, const AvbABData* data); */
AvbIOResult avb_ab_data_write(AvbABOps* ab_ops, const AvbABData* data);
/* Return codes used in avb_ab_flow(), see that function for
* documentation of each value. /* Return codes used in avb_ab_flow(), see that function for
*/ * documentation of each value.
typedef enum { */
AVB_AB_FLOW_RESULT_OK, typedef enum {
AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR, AVB_AB_FLOW_RESULT_OK,
AVB_AB_FLOW_RESULT_ERROR_OOM, AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR,
AVB_AB_FLOW_RESULT_ERROR_IO, AVB_AB_FLOW_RESULT_ERROR_OOM,
AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS, AVB_AB_FLOW_RESULT_ERROR_IO,
AVB_AB_FLOW_RESULT_ERROR_INVALID_ARGUMENT AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS,
} AvbABFlowResult; AVB_AB_FLOW_RESULT_ERROR_INVALID_ARGUMENT
} AvbABFlowResult;
/* Get a textual representation of |result|. */
const char* avb_ab_flow_result_to_string(AvbABFlowResult result); /* Get a textual representation of |result|. */
const char* avb_ab_flow_result_to_string(AvbABFlowResult result);
/* High-level function to select a slot to boot. The following
* algorithm is used: /* High-level function to select a slot to boot. The following
* * algorithm is used:
* 1. A/B metadata is loaded and validated using the *
* read_ab_metadata() operation. Typically this means it's read from * 1. A/B metadata is loaded and validated using the
* the 'misc' partition and if it's invalid then it's reset using * read_ab_metadata() operation. Typically this means it's read from
* avb_ab_data_init() and this reset metadata is returned. * the 'misc' partition and if it's invalid then it's reset using
* * avb_ab_data_init() and this reset metadata is returned.
* 2. All bootable slots listed in the A/B metadata are verified using *
* avb_slot_verify(). If a slot is invalid or if it fails verification * 2. All bootable slots listed in the A/B metadata are verified using
* (and AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is not set, see * avb_slot_verify(). If a slot is invalid or if it fails verification
* below), it will be marked as unbootable in the A/B metadata and the * (and AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is not set, see
* metadata will be saved to disk before returning. * below), it will be marked as unbootable in the A/B metadata and the
* * metadata will be saved to disk before returning.
* 3. If there are no bootable slots, the value *
* AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS is returned. * 3. If there are no bootable slots, the value
* * AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS is returned.
* 4. For each bootable slot, the Stored Rollback Indexes are updated *
* such that for each rollback index location, the Stored Rollback * 4. For each bootable slot, the Stored Rollback Indexes are updated
* Index is the largest number smaller than or equal to the Rollback * such that for each rollback index location, the Stored Rollback
* Index of each slot. * Index is the largest number smaller than or equal to the Rollback
* * Index of each slot.
* 5. The bootable slot with the highest priority is selected and *
* returned in |out_data|. If this slot is already marked as * 5. The bootable slot with the highest priority is selected and
* successful, the A/B metadata is not modified. However, if the slot * returned in |out_data|. If this slot is already marked as
* is not marked as bootable its |tries_remaining| count is * successful, the A/B metadata is not modified. However, if the slot
* decremented and the A/B metadata is saved to disk before returning. * is not marked as bootable its |tries_remaining| count is
* In either case the value AVB_AB_FLOW_RESULT_OK is returning. * decremented and the A/B metadata is saved to disk before returning.
* * In either case the value AVB_AB_FLOW_RESULT_OK is returning.
* The partitions to load is given in |requested_partitions| as a *
* NULL-terminated array of NUL-terminated strings. Typically the * The partitions to load is given in |requested_partitions| as a
* |requested_partitions| array only contains a single item for the * NULL-terminated array of NUL-terminated strings. Typically the
* boot partition, 'boot'. * |requested_partitions| array only contains a single item for the
* * boot partition, 'boot'.
* If the device is unlocked (and _only_ if it's unlocked), the *
* AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR flag should be set * If the device is unlocked (and _only_ if it's unlocked), the
* in the |flags| parameter. This will allow considering slots as * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR flag should be set
* verified even when avb_slot_verify() returns * in the |flags| parameter. This will allow considering slots as
* AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED, * verified even when avb_slot_verify() returns
* AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, or * AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED,
* AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX for the slot in * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, or
* question. * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX for the slot in
* * question.
* Note that neither androidboot.slot_suffix nor androidboot.slot are *
* set in the |cmdline| field in |AvbSlotVerifyData| - you will have * Note that neither androidboot.slot_suffix nor androidboot.slot are
* to pass these yourself. * set in the |cmdline| field in |AvbSlotVerifyData| - you will have
* * to pass these yourself.
* If a slot was selected and it verified then AVB_AB_FLOW_RESULT_OK *
* is returned. * If a slot was selected and it verified then AVB_AB_FLOW_RESULT_OK
* * is returned.
* If a slot was selected but it didn't verify then *
* AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR is returned. This can * If a slot was selected but it didn't verify then
* only happen when the AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR * AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR is returned. This can
* flag is set. * only happen when the AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR
* * flag is set.
* If an I/O operation - such as loading/saving metadata or checking *
* rollback indexes - fail, the value AVB_AB_FLOW_RESULT_ERROR_IO is * If an I/O operation - such as loading/saving metadata or checking
* returned. * rollback indexes - fail, the value AVB_AB_FLOW_RESULT_ERROR_IO is
* * returned.
* If memory allocation fails, AVB_AB_FLOW_RESULT_ERROR_OOM is *
* returned. * If memory allocation fails, AVB_AB_FLOW_RESULT_ERROR_OOM is
* * returned.
* If invalid arguments are passed, *
* AVB_AB_FLOW_RESULT_ERROR_INVALID_ARGUMENT is returned. For example * If invalid arguments are passed,
* this can happen if using AVB_HASHTREE_ERROR_MODE_LOGGING without * AVB_AB_FLOW_RESULT_ERROR_INVALID_ARGUMENT is returned. For example
* AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR. * this can happen if using AVB_HASHTREE_ERROR_MODE_LOGGING without
* * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR.
* Reasonable behavior for handling AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS *
* is to initiate device repair (which is device-dependent). * Reasonable behavior for handling AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS
*/ * is to initiate device repair (which is device-dependent).
AvbABFlowResult avb_ab_flow(AvbABOps* ab_ops, */
const char* const* requested_partitions, AvbABFlowResult avb_ab_flow(AvbABOps* ab_ops,
AvbSlotVerifyFlags flags, const char* const* requested_partitions,
AvbHashtreeErrorMode hashtree_error_mode, AvbSlotVerifyFlags flags,
AvbSlotVerifyData** out_data); AvbHashtreeErrorMode hashtree_error_mode,
AvbSlotVerifyData** out_data);
/* Marks the slot with the given slot number as active. Returns
* AVB_IO_RESULT_OK on success, error code otherwise. /* Marks the slot with the given slot number as active. Returns
* * AVB_IO_RESULT_OK on success, error code otherwise.
* This function is typically used by the OS updater when completing *
* an update. It can also used by the firmware for implementing the * This function is typically used by the OS updater when completing
* "set_active" command. * an update. It can also used by the firmware for implementing the
*/ * "set_active" command.
AvbIOResult avb_ab_mark_slot_active(AvbABOps* ab_ops, unsigned int slot_number); */
AvbIOResult avb_ab_mark_slot_active(AvbABOps* ab_ops, unsigned int slot_number);
/* Marks the slot with the given slot number as unbootable. Returns
* AVB_IO_RESULT_OK on success, error code otherwise. /* Marks the slot with the given slot number as unbootable. Returns
* * AVB_IO_RESULT_OK on success, error code otherwise.
* This function is typically used by the OS updater before writing to *
* a slot. * This function is typically used by the OS updater before writing to
*/ * a slot.
AvbIOResult avb_ab_mark_slot_unbootable(AvbABOps* ab_ops, */
unsigned int slot_number); AvbIOResult avb_ab_mark_slot_unbootable(AvbABOps* ab_ops,
unsigned int slot_number);
/* Marks the slot with the given slot number as having booted
* successfully. Returns AVB_IO_RESULT_OK on success, error code /* Marks the slot with the given slot number as having booted
* otherwise. * successfully. Returns AVB_IO_RESULT_OK on success, error code
* * otherwise.
* Calling this on an unbootable slot is an error - AVB_IO_RESULT_OK *
* will be returned yet the function will have no side-effects. * Calling this on an unbootable slot is an error - AVB_IO_RESULT_OK
* * will be returned yet the function will have no side-effects.
* This function is typically used by the OS updater after having *
* confirmed that the slot works as intended. * This function is typically used by the OS updater after having
*/ * confirmed that the slot works as intended.
AvbIOResult avb_ab_mark_slot_successful(AvbABOps* ab_ops, */
unsigned int slot_number); AvbIOResult avb_ab_mark_slot_successful(AvbABOps* ab_ops,
unsigned int slot_number);
/*
* Load metadata. /*
*/ * Load metadata.
AvbIOResult load_metadata(AvbABOps* ab_ops, */
AvbABData* ab_data, AvbIOResult load_metadata(AvbABOps* ab_ops,
AvbABData* ab_data_orig); AvbABData* ab_data,
AvbABData* ab_data_orig);
/* Writes A/B metadata to disk only if it has changed - returns
* AVB_IO_RESULT_OK on success, error code otherwise. /* Writes A/B metadata to disk only if it has changed - returns
*/ * AVB_IO_RESULT_OK on success, error code otherwise.
AvbIOResult save_metadata_if_changed(AvbABOps* ab_ops, */
AvbABData* ab_data, AvbIOResult save_metadata_if_changed(AvbABOps* ab_ops,
AvbABData* ab_data_orig); AvbABData* ab_data,
AvbABData* ab_data_orig);
#ifdef __cplusplus
} #ifdef __cplusplus
#endif }
#endif
#endif /* AVB_AB_FLOW_H_ */
#endif /* AVB_AB_FLOW_H_ */

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); 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 #ifdef __cplusplus
} }
#endif #endif

File diff suppressed because it is too large Load Diff

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) { if (rk_avb_ab_slot_select(ops->ab_ops, select_slot) != 0) {
printf("get_current_slot error!\n"); printf("###There is no bootable slot, bring up last_boot!###\n");
ret = -1; 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); avb_ops_user_free(ops);
@ -697,3 +703,30 @@ int rk_generate_unlock_challenge(void *buffer, uint32_t *challenge_len)
else else
return -1; 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;
}