137a7bc39SJason Zhu /* 237a7bc39SJason Zhu * (C) Copyright 2017 Rockchip Electronics Co., Ltd 337a7bc39SJason Zhu * 437a7bc39SJason Zhu * SPDX-License-Identifier: GPL-2.0+ 537a7bc39SJason Zhu */ 637a7bc39SJason Zhu 737a7bc39SJason Zhu #ifndef RK_AVB_OPS_USER_H_ 837a7bc39SJason Zhu #define RK_AVB_OPS_USER_H_ 937a7bc39SJason Zhu 1017e05a56SJason Zhu #include <android_avb/avb_ops_user.h> 1117e05a56SJason Zhu #include <android_avb/libavb_ab.h> 1217e05a56SJason Zhu 1337a7bc39SJason Zhu #ifdef __cplusplus 1437a7bc39SJason Zhu extern "C" { 1537a7bc39SJason Zhu #endif 1637a7bc39SJason Zhu 1737a7bc39SJason Zhu /* rk used */ 1837a7bc39SJason Zhu #define PERM_ATTR_DIGEST_SIZE 32 1937a7bc39SJason Zhu #define PERM_ATTR_TOTAL_SIZE 1052 2037a7bc39SJason Zhu #define VBOOT_KEY_HASH_SIZE 32 2137a7bc39SJason Zhu #define ANDROID_VBOOT_LOCK 0 2237a7bc39SJason Zhu #define ANDROID_VBOOT_UNLOCK 1 2337a7bc39SJason Zhu #define SLOT_NUM 2 2437a7bc39SJason Zhu #define CURR_SYSTEM_SLOT_SUFFIX "ab" 2537a7bc39SJason Zhu #define VBMETA_MAX_SIZE 65536 2637a7bc39SJason Zhu #define ROLLBACK_MAX_SIZE 20 27*334b1f31SJason Zhu #define LOCK_MASK (1 << 0) 28*334b1f31SJason Zhu #define UNLOCK_DISABLE_MASK (1 << 1) 29615e0cdeSJason Zhu #define VBOOT_STATE_SIZE 1000 3037a7bc39SJason Zhu #define PERM_ATTR_SUCCESS_FLAG 1 3137a7bc39SJason Zhu /* soc-v use the rsa2048 */ 3237a7bc39SJason Zhu #define VBOOT_KEY_SIZE 256 33c3230cf0SJason Zhu #define RPMB_BASE_ADDR (64*1024/256) 34c3230cf0SJason Zhu #define UBOOT_RB_INDEX_OFFSET 24 35c3230cf0SJason Zhu #define TRUST_RB_INDEX_OFFSET 28 361f670f7cSJason Zhu #define ROCHCHIP_RSA_PARAMETER_SIZE 64 371f670f7cSJason Zhu 381f670f7cSJason Zhu struct rk_pub_key { 391f670f7cSJason Zhu u_int32_t rsa_n[ROCHCHIP_RSA_PARAMETER_SIZE]; 401f670f7cSJason Zhu u_int32_t rsa_e[ROCHCHIP_RSA_PARAMETER_SIZE]; 411f670f7cSJason Zhu u_int32_t rsa_c[ROCHCHIP_RSA_PARAMETER_SIZE]; 421f670f7cSJason Zhu }; 4337a7bc39SJason Zhu 4437a7bc39SJason Zhu /** 4537a7bc39SJason Zhu * Provided to fastboot to read how many slot in this system. 4637a7bc39SJason Zhu * 4737a7bc39SJason Zhu * @param slot_count We use parameter slot_count to obtain 4837a7bc39SJason Zhu * how many slots in the system. 4937a7bc39SJason Zhu * 5037a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 5137a7bc39SJason Zhu */ 5237a7bc39SJason Zhu int rk_avb_read_slot_count(char *slot_count); 5337a7bc39SJason Zhu 5437a7bc39SJason Zhu /** 5537a7bc39SJason Zhu * The android things supply many slots, their name like '_a', '_b'. 5637a7bc39SJason Zhu * We can use this function to read current slot is '_a' or '_b'. 5737a7bc39SJason Zhu * 5837a7bc39SJason Zhu * @slot_suffixes read value '_a' or '_b'. 5937a7bc39SJason Zhu * 6037a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 6137a7bc39SJason Zhu */ 6237a7bc39SJason Zhu int rk_avb_read_slot_suffixes(char *slot_suffixes); 6337a7bc39SJason Zhu 6437a7bc39SJason Zhu /** 6537a7bc39SJason Zhu * Use this function to set which slot boot first. 6637a7bc39SJason Zhu * 6737a7bc39SJason Zhu * @param slot_number set '0' or '1' 6837a7bc39SJason Zhu * 6937a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 7037a7bc39SJason Zhu */ 7137a7bc39SJason Zhu int rk_avb_set_slot_active(unsigned int *slot_number); 7237a7bc39SJason Zhu 7337a7bc39SJason Zhu /** 7437a7bc39SJason Zhu * Get current slot: '_a' or '_b'. 7537a7bc39SJason Zhu * 7637a7bc39SJason Zhu * @param select_slot obtain current slot. 7737a7bc39SJason Zhu * 7837a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 7937a7bc39SJason Zhu */ 8037a7bc39SJason Zhu int rk_avb_get_current_slot(char *select_slot); 8137a7bc39SJason Zhu 8237a7bc39SJason Zhu /** 83eb89f0a8SJoseph Chen * Append current slot to given partition name 84eb89f0a8SJoseph Chen * 85eb89f0a8SJoseph Chen * @param part_name partition name 86eb89f0a8SJoseph Chen * @param slot given slot suffix, auto append current slot if NULL 87eb89f0a8SJoseph Chen * @param new_name partition name with slot suffix appended 88eb89f0a8SJoseph Chen * 89eb89f0a8SJoseph Chen * @return 0 if the command succeeded, -1 if it failed 90eb89f0a8SJoseph Chen */ 91eb89f0a8SJoseph Chen int rk_avb_append_part_slot(const char *part_name, char *new_name); 92eb89f0a8SJoseph Chen 93eb89f0a8SJoseph Chen /** 9437a7bc39SJason Zhu * The android things defines permanent attributes to 9537a7bc39SJason Zhu * store PSK_public, product id. We can use this function 9637a7bc39SJason Zhu * to read them. 9737a7bc39SJason Zhu * 9837a7bc39SJason Zhu * @param attributes PSK_public, product id.... 9937a7bc39SJason Zhu * 10037a7bc39SJason Zhu * @param size The size of attributes. 10137a7bc39SJason Zhu * 10237a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 10337a7bc39SJason Zhu */ 10437a7bc39SJason Zhu int rk_avb_read_permanent_attributes(uint8_t *attributes, uint32_t size); 10537a7bc39SJason Zhu 10637a7bc39SJason Zhu /** 10737a7bc39SJason Zhu * The android things defines permanent attributes to 10837a7bc39SJason Zhu * store PSK_public, product id. We can use this function 10937a7bc39SJason Zhu * to write them. 11037a7bc39SJason Zhu * 11137a7bc39SJason Zhu * @param attributes PSK_public, product id.... 11237a7bc39SJason Zhu * 11337a7bc39SJason Zhu * @param size The size of attributes. 11437a7bc39SJason Zhu * 11537a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 11637a7bc39SJason Zhu */ 11737a7bc39SJason Zhu int rk_avb_write_permanent_attributes(uint8_t *attributes, uint32_t size); 11837a7bc39SJason Zhu 11937a7bc39SJason Zhu /** 12037a7bc39SJason Zhu * The funtion can be use to read the device state to judge 12137a7bc39SJason Zhu * whether the device can be flash. 12237a7bc39SJason Zhu * 12337a7bc39SJason Zhu * @param flash_lock_state A flag indicate the device flash state. 12437a7bc39SJason Zhu * 12537a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 12637a7bc39SJason Zhu */ 12737a7bc39SJason Zhu int rk_avb_read_flash_lock_state(uint8_t *flash_lock_state); 12837a7bc39SJason Zhu 12937a7bc39SJason Zhu /** 13037a7bc39SJason Zhu * The function is provided to write device flash state. 13137a7bc39SJason Zhu * 13237a7bc39SJason Zhu * @param flash_lock_state A flag indicate the device flash state. 13337a7bc39SJason Zhu * 13437a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 13537a7bc39SJason Zhu */ 13637a7bc39SJason Zhu int rk_avb_write_flash_lock_state(uint8_t flash_lock_state); 13737a7bc39SJason Zhu 13837a7bc39SJason Zhu /** 13937a7bc39SJason Zhu * The android things use the flag of lock state to indicate 14037a7bc39SJason Zhu * whether the device can be booted when verified error. 14137a7bc39SJason Zhu * 14237a7bc39SJason Zhu * @param lock_state A flag indicate the device lock state. 14337a7bc39SJason Zhu * 14437a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 14537a7bc39SJason Zhu */ 14637a7bc39SJason Zhu int rk_avb_read_lock_state(uint8_t *lock_state); 14737a7bc39SJason Zhu 14837a7bc39SJason Zhu /** 14937a7bc39SJason Zhu * The android things use the flag of lock state to indicate 15037a7bc39SJason Zhu * whether the device can be booted when verified error. 15137a7bc39SJason Zhu * 15237a7bc39SJason Zhu * @param lock_state A flag indicate the device lock state. 15337a7bc39SJason Zhu * 15437a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 15537a7bc39SJason Zhu */ 15637a7bc39SJason Zhu int rk_avb_write_lock_state(uint8_t lock_state); 15737a7bc39SJason Zhu 15837a7bc39SJason Zhu /** 15937a7bc39SJason Zhu * The android things uses fastboot to flash the permanent attributes. 16037a7bc39SJason Zhu * And if them were written, there must have a flag to indicate. 16137a7bc39SJason Zhu * 16237a7bc39SJason Zhu * @param flag indicate the permanent attributes have been written 16337a7bc39SJason Zhu * or not. 16437a7bc39SJason Zhu * 16537a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 16637a7bc39SJason Zhu */ 16737a7bc39SJason Zhu int rk_avb_read_perm_attr_flag(uint8_t *flag); 16837a7bc39SJason Zhu 16937a7bc39SJason Zhu /** 17037a7bc39SJason Zhu * The android things uses fastboot to flash the permanent attributes. 17137a7bc39SJason Zhu * And if them were written, there must have a flag to indicate. 17237a7bc39SJason Zhu * 17337a7bc39SJason Zhu * @param flag We can call this function to write the flag '1' 17437a7bc39SJason Zhu * to indicate the permanent attributes has been 17537a7bc39SJason Zhu * written. 17637a7bc39SJason Zhu * 17737a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 17837a7bc39SJason Zhu */ 17937a7bc39SJason Zhu int rk_avb_write_perm_attr_flag(uint8_t flag); 18037a7bc39SJason Zhu 18137a7bc39SJason Zhu /** 18237a7bc39SJason Zhu * The android things require the soc-v key hash to be flashed 18337a7bc39SJason Zhu * using the fastboot. So the function can be used in fastboot 18437a7bc39SJason Zhu * to flash the key hash. 18537a7bc39SJason Zhu * 18637a7bc39SJason Zhu * @param buf The vboot key hash data. 18737a7bc39SJason Zhu * 18837a7bc39SJason Zhu * @param length The length of key hash. 18937a7bc39SJason Zhu * 19037a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 19137a7bc39SJason Zhu */ 19237a7bc39SJason Zhu int rk_avb_read_vbootkey_hash(uint8_t *buf, uint8_t length); 19337a7bc39SJason Zhu 19437a7bc39SJason Zhu /** 19537a7bc39SJason Zhu * The android things require the soc-v key hash to be flashed 19637a7bc39SJason Zhu * using the fastboot. So the function can be used in fastboot 19737a7bc39SJason Zhu * to flash the key hash. 19837a7bc39SJason Zhu * 19937a7bc39SJason Zhu * @param buf The vboot key hash data. 20037a7bc39SJason Zhu * 20137a7bc39SJason Zhu * @param length The length of key hash. 20237a7bc39SJason Zhu * 20337a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 20437a7bc39SJason Zhu */ 20537a7bc39SJason Zhu int rk_avb_write_vbootkey_hash(uint8_t *buf, uint8_t length); 20637a7bc39SJason Zhu 20737a7bc39SJason Zhu /** 20837a7bc39SJason Zhu * U-boot close the optee client when start kernel 20937a7bc39SJason Zhu * to prevent the optee client being invoking by other 21037a7bc39SJason Zhu * program. 21137a7bc39SJason Zhu * 21237a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 21337a7bc39SJason Zhu */ 21437a7bc39SJason Zhu int rk_avb_close_optee_client(void); 21537a7bc39SJason Zhu 21637a7bc39SJason Zhu /** 21737a7bc39SJason Zhu * read the permanent attributes hash. 21837a7bc39SJason Zhu * 21937a7bc39SJason Zhu * @param buf The permanent attributes hash data. 22037a7bc39SJason Zhu * 22137a7bc39SJason Zhu * @param length The length of permanent attributes hash. 22237a7bc39SJason Zhu * 22337a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 22437a7bc39SJason Zhu */ 22537a7bc39SJason Zhu int rk_avb_read_attribute_hash(uint8_t *buf, uint8_t length); 22637a7bc39SJason Zhu 22737a7bc39SJason Zhu /** 22837a7bc39SJason Zhu * Write the permanent attributes hash. 22937a7bc39SJason Zhu * 23037a7bc39SJason Zhu * @param buf The permanent attributes hash data. 23137a7bc39SJason Zhu * 23237a7bc39SJason Zhu * @param length The length of permanent attributes hash. 23337a7bc39SJason Zhu * 23437a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 23537a7bc39SJason Zhu */ 23637a7bc39SJason Zhu int rk_avb_write_attribute_hash(uint8_t *buf, uint8_t length); 23737a7bc39SJason Zhu 23837a7bc39SJason Zhu /** 23937a7bc39SJason Zhu * Get the avb vboot state 24037a7bc39SJason Zhu * 24137a7bc39SJason Zhu * @param buf store the vboot state. 24237a7bc39SJason Zhu * 24337a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 24437a7bc39SJason Zhu */ 24537a7bc39SJason Zhu void rk_avb_get_at_vboot_state(char *buf); 24637a7bc39SJason Zhu 24737a7bc39SJason Zhu /** 24837a7bc39SJason Zhu * Get ab information from misc partition 24937a7bc39SJason Zhu * 25037a7bc39SJason Zhu * @param ab_data the struct of ab information 25137a7bc39SJason Zhu * 25237a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 25337a7bc39SJason Zhu */ 25437a7bc39SJason Zhu int rk_avb_get_ab_info(AvbABData* ab_data); 25537a7bc39SJason Zhu 25637a7bc39SJason Zhu /** 25737a7bc39SJason Zhu * Get the information whether the partition has slot 25837a7bc39SJason Zhu * 25937a7bc39SJason Zhu * @param the partition name 26037a7bc39SJason Zhu * 26137a7bc39SJason Zhu * @return 0 if the partition has slot, -1 if not 26237a7bc39SJason Zhu */ 26337a7bc39SJason Zhu int rk_avb_get_part_has_slot_info(const char *base_name); 26437a7bc39SJason Zhu 26537a7bc39SJason Zhu AvbABFlowResult rk_avb_ab_slot_select(AvbABOps* ab_ops,char select_slot[]); 26637a7bc39SJason Zhu 26783ab7b49SJason Zhu /** 26883ab7b49SJason Zhu * authenticated unlock 26983ab7b49SJason Zhu * 27083ab7b49SJason Zhu * @param buffer: AvbAtxUnlockCredential 27183ab7b49SJason Zhu * 27283ab7b49SJason Zhu * @param out_is_trusted: true or false 27383ab7b49SJason Zhu * 27483ab7b49SJason Zhu * @return 0 if authenticated unlock OK, -1 if not 27583ab7b49SJason Zhu */ 27683ab7b49SJason Zhu int rk_auth_unlock(void *buffer, char *out_is_trusted); 27783ab7b49SJason Zhu 27857c7f8feSJason Zhu /** 27957c7f8feSJason Zhu * generate unlock challenge 28057c7f8feSJason Zhu * 28157c7f8feSJason Zhu * @param buffer: AvbAtxUnlockChallenge 28257c7f8feSJason Zhu * 28357c7f8feSJason Zhu * @param challenge_len: Challenge length 28457c7f8feSJason Zhu * 28557c7f8feSJason Zhu * @param out_is_trusted: true or false 28657c7f8feSJason Zhu * 28757c7f8feSJason Zhu * @return 0 if generate unlock challenge OK, -1 if not 28857c7f8feSJason Zhu */ 28957c7f8feSJason Zhu int rk_generate_unlock_challenge(void *buffer, uint32_t *challenge_len); 29057c7f8feSJason Zhu 2917cca3dd4SJason Zhu /** 2927cca3dd4SJason Zhu * Get last boot slot 2937cca3dd4SJason Zhu * 2947cca3dd4SJason Zhu * @return 0 is slot A; 1 is slot B; -1 is error 2957cca3dd4SJason Zhu */ 2967cca3dd4SJason Zhu int rk_get_lastboot(void); 2977cca3dd4SJason Zhu 2989b83ce70SJason Zhu /** 2999b83ce70SJason Zhu * Get permanent attribute certificate 3009b83ce70SJason Zhu * 3019b83ce70SJason Zhu * @param cer: certificate data 3029b83ce70SJason Zhu * 3039b83ce70SJason Zhu * @param size: certificate size 3049b83ce70SJason Zhu */ 3059b83ce70SJason Zhu int rk_avb_get_perm_attr_cer(uint8_t *cer, uint32_t size); 3069b83ce70SJason Zhu 3079b83ce70SJason Zhu /** 3089b83ce70SJason Zhu * Set permanent attribute certificate 3099b83ce70SJason Zhu * 3109b83ce70SJason Zhu * @param cer: certificate data 3119b83ce70SJason Zhu * 3129b83ce70SJason Zhu * @param size: certificate size 3139b83ce70SJason Zhu */ 3149b83ce70SJason Zhu int rk_avb_set_perm_attr_cer(uint8_t *cer, uint32_t size); 3159b83ce70SJason Zhu 3161f670f7cSJason Zhu /** 3171f670f7cSJason Zhu * Get public key 3181f670f7cSJason Zhu * 3191f670f7cSJason Zhu * @param pub_key: public key data 3201f670f7cSJason Zhu */ 3211f670f7cSJason Zhu int rk_avb_get_pub_key(struct rk_pub_key *pub_key); 3221f670f7cSJason Zhu 323813227adSJason Zhu /** 324813227adSJason Zhu * init ab metadata 325813227adSJason Zhu */ 326813227adSJason Zhu int rk_avb_init_ab_metadata(void); 327813227adSJason Zhu 3286e2db7c2SWu Liangqing /** 3296e2db7c2SWu Liangqing * rockchip avb commands 3306e2db7c2SWu Liangqing */ 3316e2db7c2SWu Liangqing int rk_avb_write_perm_attr(uint16_t id, void *pbuf, uint16_t size); 3326e2db7c2SWu Liangqing int rk_avb_read_perm_attr(uint16_t id, void *pbuf, uint16_t size); 3336e2db7c2SWu Liangqing 33417e05a56SJason Zhu /** 33517e05a56SJason Zhu * Do the device have boot slot 33617e05a56SJason Zhu */ 33717e05a56SJason Zhu bool rk_avb_ab_have_bootable_slot(void); 33817e05a56SJason Zhu 3392d62cca0SJason Zhu /** 3402d62cca0SJason Zhu * update rollback index 3412d62cca0SJason Zhu */ 3422d62cca0SJason Zhu int rk_avb_update_stored_rollback_indexes_for_slot(AvbOps* ops, AvbSlotVerifyData* slot_data); 3432d62cca0SJason Zhu 34437a7bc39SJason Zhu #ifdef __cplusplus 34537a7bc39SJason Zhu } 34637a7bc39SJason Zhu #endif 34737a7bc39SJason Zhu 34837a7bc39SJason Zhu #endif /* RK_AVB_OPS_USER_H_ */ 349