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 1037a7bc39SJason Zhu #ifdef __cplusplus 1137a7bc39SJason Zhu extern "C" { 1237a7bc39SJason Zhu #endif 1337a7bc39SJason Zhu 1437a7bc39SJason Zhu /* rk used */ 1537a7bc39SJason Zhu #define PERM_ATTR_DIGEST_SIZE 32 1637a7bc39SJason Zhu #define PERM_ATTR_TOTAL_SIZE 1052 1737a7bc39SJason Zhu #define VBOOT_KEY_HASH_SIZE 32 1837a7bc39SJason Zhu #define ANDROID_VBOOT_LOCK 0 1937a7bc39SJason Zhu #define ANDROID_VBOOT_UNLOCK 1 2037a7bc39SJason Zhu #define SLOT_NUM 2 2137a7bc39SJason Zhu #define CURR_SYSTEM_SLOT_SUFFIX "ab" 2237a7bc39SJason Zhu #define VBMETA_MAX_SIZE 65536 2337a7bc39SJason Zhu #define ROLLBACK_MAX_SIZE 20 2437a7bc39SJason Zhu #define LOCK_MASK 0x1 2537a7bc39SJason Zhu #define UNLOCK_DISABLE_MASK 0x2 26615e0cdeSJason Zhu #define VBOOT_STATE_SIZE 1000 2737a7bc39SJason Zhu #define PERM_ATTR_SUCCESS_FLAG 1 2837a7bc39SJason Zhu /* soc-v use the rsa2048 */ 2937a7bc39SJason Zhu #define VBOOT_KEY_SIZE 256 30c3230cf0SJason Zhu #define RPMB_BASE_ADDR (64*1024/256) 31c3230cf0SJason Zhu #define UBOOT_RB_INDEX_OFFSET 24 32c3230cf0SJason Zhu #define TRUST_RB_INDEX_OFFSET 28 33*1f670f7cSJason Zhu #define ROCHCHIP_RSA_PARAMETER_SIZE 64 34*1f670f7cSJason Zhu 35*1f670f7cSJason Zhu struct rk_pub_key { 36*1f670f7cSJason Zhu u_int32_t rsa_n[ROCHCHIP_RSA_PARAMETER_SIZE]; 37*1f670f7cSJason Zhu u_int32_t rsa_e[ROCHCHIP_RSA_PARAMETER_SIZE]; 38*1f670f7cSJason Zhu u_int32_t rsa_c[ROCHCHIP_RSA_PARAMETER_SIZE]; 39*1f670f7cSJason Zhu }; 4037a7bc39SJason Zhu 4137a7bc39SJason Zhu /** 4237a7bc39SJason Zhu * Provided to fastboot to read how many slot in this system. 4337a7bc39SJason Zhu * 4437a7bc39SJason Zhu * @param slot_count We use parameter slot_count to obtain 4537a7bc39SJason Zhu * how many slots in the system. 4637a7bc39SJason Zhu * 4737a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 4837a7bc39SJason Zhu */ 4937a7bc39SJason Zhu int rk_avb_read_slot_count(char *slot_count); 5037a7bc39SJason Zhu 5137a7bc39SJason Zhu /** 5237a7bc39SJason Zhu * The android things supply many slots, their name like '_a', '_b'. 5337a7bc39SJason Zhu * We can use this function to read current slot is '_a' or '_b'. 5437a7bc39SJason Zhu * 5537a7bc39SJason Zhu * @slot_suffixes read value '_a' or '_b'. 5637a7bc39SJason Zhu * 5737a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 5837a7bc39SJason Zhu */ 5937a7bc39SJason Zhu int rk_avb_read_slot_suffixes(char *slot_suffixes); 6037a7bc39SJason Zhu 6137a7bc39SJason Zhu /** 6237a7bc39SJason Zhu * Use this function to set which slot boot first. 6337a7bc39SJason Zhu * 6437a7bc39SJason Zhu * @param slot_number set '0' or '1' 6537a7bc39SJason Zhu * 6637a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 6737a7bc39SJason Zhu */ 6837a7bc39SJason Zhu int rk_avb_set_slot_active(unsigned int *slot_number); 6937a7bc39SJason Zhu 7037a7bc39SJason Zhu /** 7137a7bc39SJason Zhu * Get current slot: '_a' or '_b'. 7237a7bc39SJason Zhu * 7337a7bc39SJason Zhu * @param select_slot obtain current slot. 7437a7bc39SJason Zhu * 7537a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 7637a7bc39SJason Zhu */ 7737a7bc39SJason Zhu int rk_avb_get_current_slot(char *select_slot); 7837a7bc39SJason Zhu 7937a7bc39SJason Zhu /** 8037a7bc39SJason Zhu * The android things defines permanent attributes to 8137a7bc39SJason Zhu * store PSK_public, product id. We can use this function 8237a7bc39SJason Zhu * to read them. 8337a7bc39SJason Zhu * 8437a7bc39SJason Zhu * @param attributes PSK_public, product id.... 8537a7bc39SJason Zhu * 8637a7bc39SJason Zhu * @param size The size of attributes. 8737a7bc39SJason Zhu * 8837a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 8937a7bc39SJason Zhu */ 9037a7bc39SJason Zhu int rk_avb_read_permanent_attributes(uint8_t *attributes, uint32_t size); 9137a7bc39SJason Zhu 9237a7bc39SJason Zhu /** 9337a7bc39SJason Zhu * The android things defines permanent attributes to 9437a7bc39SJason Zhu * store PSK_public, product id. We can use this function 9537a7bc39SJason Zhu * to write them. 9637a7bc39SJason Zhu * 9737a7bc39SJason Zhu * @param attributes PSK_public, product id.... 9837a7bc39SJason Zhu * 9937a7bc39SJason Zhu * @param size The size of attributes. 10037a7bc39SJason Zhu * 10137a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 10237a7bc39SJason Zhu */ 10337a7bc39SJason Zhu int rk_avb_write_permanent_attributes(uint8_t *attributes, uint32_t size); 10437a7bc39SJason Zhu 10537a7bc39SJason Zhu /** 10637a7bc39SJason Zhu * The funtion can be use to read the device state to judge 10737a7bc39SJason Zhu * whether the device can be flash. 10837a7bc39SJason Zhu * 10937a7bc39SJason Zhu * @param flash_lock_state A flag indicate the device flash state. 11037a7bc39SJason Zhu * 11137a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 11237a7bc39SJason Zhu */ 11337a7bc39SJason Zhu int rk_avb_read_flash_lock_state(uint8_t *flash_lock_state); 11437a7bc39SJason Zhu 11537a7bc39SJason Zhu /** 11637a7bc39SJason Zhu * The function is provided to write device flash state. 11737a7bc39SJason Zhu * 11837a7bc39SJason Zhu * @param flash_lock_state A flag indicate the device flash state. 11937a7bc39SJason Zhu * 12037a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 12137a7bc39SJason Zhu */ 12237a7bc39SJason Zhu int rk_avb_write_flash_lock_state(uint8_t flash_lock_state); 12337a7bc39SJason Zhu 12437a7bc39SJason Zhu /** 12537a7bc39SJason Zhu * The android things use the flag of lock state to indicate 12637a7bc39SJason Zhu * whether the device can be booted when verified error. 12737a7bc39SJason Zhu * 12837a7bc39SJason Zhu * @param lock_state A flag indicate the device lock state. 12937a7bc39SJason Zhu * 13037a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 13137a7bc39SJason Zhu */ 13237a7bc39SJason Zhu int rk_avb_read_lock_state(uint8_t *lock_state); 13337a7bc39SJason Zhu 13437a7bc39SJason Zhu /** 13537a7bc39SJason Zhu * The android things use the flag of lock state to indicate 13637a7bc39SJason Zhu * whether the device can be booted when verified error. 13737a7bc39SJason Zhu * 13837a7bc39SJason Zhu * @param lock_state A flag indicate the device lock state. 13937a7bc39SJason Zhu * 14037a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 14137a7bc39SJason Zhu */ 14237a7bc39SJason Zhu int rk_avb_write_lock_state(uint8_t lock_state); 14337a7bc39SJason Zhu 14437a7bc39SJason Zhu /** 14537a7bc39SJason Zhu * The android things uses fastboot to flash the permanent attributes. 14637a7bc39SJason Zhu * And if them were written, there must have a flag to indicate. 14737a7bc39SJason Zhu * 14837a7bc39SJason Zhu * @param flag indicate the permanent attributes have been written 14937a7bc39SJason Zhu * or not. 15037a7bc39SJason Zhu * 15137a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 15237a7bc39SJason Zhu */ 15337a7bc39SJason Zhu int rk_avb_read_perm_attr_flag(uint8_t *flag); 15437a7bc39SJason Zhu 15537a7bc39SJason Zhu /** 15637a7bc39SJason Zhu * The android things uses fastboot to flash the permanent attributes. 15737a7bc39SJason Zhu * And if them were written, there must have a flag to indicate. 15837a7bc39SJason Zhu * 15937a7bc39SJason Zhu * @param flag We can call this function to write the flag '1' 16037a7bc39SJason Zhu * to indicate the permanent attributes has been 16137a7bc39SJason Zhu * written. 16237a7bc39SJason Zhu * 16337a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 16437a7bc39SJason Zhu */ 16537a7bc39SJason Zhu int rk_avb_write_perm_attr_flag(uint8_t flag); 16637a7bc39SJason Zhu 16737a7bc39SJason Zhu /** 16837a7bc39SJason Zhu * The android things require the soc-v key hash to be flashed 16937a7bc39SJason Zhu * using the fastboot. So the function can be used in fastboot 17037a7bc39SJason Zhu * to flash the key hash. 17137a7bc39SJason Zhu * 17237a7bc39SJason Zhu * @param buf The vboot key hash data. 17337a7bc39SJason Zhu * 17437a7bc39SJason Zhu * @param length The length of key hash. 17537a7bc39SJason Zhu * 17637a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 17737a7bc39SJason Zhu */ 17837a7bc39SJason Zhu int rk_avb_read_vbootkey_hash(uint8_t *buf, uint8_t length); 17937a7bc39SJason Zhu 18037a7bc39SJason Zhu /** 18137a7bc39SJason Zhu * The android things require the soc-v key hash to be flashed 18237a7bc39SJason Zhu * using the fastboot. So the function can be used in fastboot 18337a7bc39SJason Zhu * to flash the key hash. 18437a7bc39SJason Zhu * 18537a7bc39SJason Zhu * @param buf The vboot key hash data. 18637a7bc39SJason Zhu * 18737a7bc39SJason Zhu * @param length The length of key hash. 18837a7bc39SJason Zhu * 18937a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 19037a7bc39SJason Zhu */ 19137a7bc39SJason Zhu int rk_avb_write_vbootkey_hash(uint8_t *buf, uint8_t length); 19237a7bc39SJason Zhu 19337a7bc39SJason Zhu /** 19437a7bc39SJason Zhu * U-boot close the optee client when start kernel 19537a7bc39SJason Zhu * to prevent the optee client being invoking by other 19637a7bc39SJason Zhu * program. 19737a7bc39SJason Zhu * 19837a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 19937a7bc39SJason Zhu */ 20037a7bc39SJason Zhu int rk_avb_close_optee_client(void); 20137a7bc39SJason Zhu 20237a7bc39SJason Zhu /** 20337a7bc39SJason Zhu * read the permanent attributes hash. 20437a7bc39SJason Zhu * 20537a7bc39SJason Zhu * @param buf The permanent attributes hash data. 20637a7bc39SJason Zhu * 20737a7bc39SJason Zhu * @param length The length of permanent attributes hash. 20837a7bc39SJason Zhu * 20937a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 21037a7bc39SJason Zhu */ 21137a7bc39SJason Zhu int rk_avb_read_attribute_hash(uint8_t *buf, uint8_t length); 21237a7bc39SJason Zhu 21337a7bc39SJason Zhu /** 21437a7bc39SJason Zhu * Write the permanent attributes hash. 21537a7bc39SJason Zhu * 21637a7bc39SJason Zhu * @param buf The permanent attributes hash data. 21737a7bc39SJason Zhu * 21837a7bc39SJason Zhu * @param length The length of permanent attributes hash. 21937a7bc39SJason Zhu * 22037a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 22137a7bc39SJason Zhu */ 22237a7bc39SJason Zhu int rk_avb_write_attribute_hash(uint8_t *buf, uint8_t length); 22337a7bc39SJason Zhu 22437a7bc39SJason Zhu /** 22537a7bc39SJason Zhu * Get the avb vboot state 22637a7bc39SJason Zhu * 22737a7bc39SJason Zhu * @param buf store the vboot state. 22837a7bc39SJason Zhu * 22937a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 23037a7bc39SJason Zhu */ 23137a7bc39SJason Zhu void rk_avb_get_at_vboot_state(char *buf); 23237a7bc39SJason Zhu 23337a7bc39SJason Zhu /** 23437a7bc39SJason Zhu * Get ab information from misc partition 23537a7bc39SJason Zhu * 23637a7bc39SJason Zhu * @param ab_data the struct of ab information 23737a7bc39SJason Zhu * 23837a7bc39SJason Zhu * @return 0 if the command succeeded, -1 if it failed 23937a7bc39SJason Zhu */ 24037a7bc39SJason Zhu int rk_avb_get_ab_info(AvbABData* ab_data); 24137a7bc39SJason Zhu 24237a7bc39SJason Zhu /** 24337a7bc39SJason Zhu * Get the information whether the partition has slot 24437a7bc39SJason Zhu * 24537a7bc39SJason Zhu * @param the partition name 24637a7bc39SJason Zhu * 24737a7bc39SJason Zhu * @return 0 if the partition has slot, -1 if not 24837a7bc39SJason Zhu */ 24937a7bc39SJason Zhu int rk_avb_get_part_has_slot_info(const char *base_name); 25037a7bc39SJason Zhu 25137a7bc39SJason Zhu AvbABFlowResult rk_avb_ab_slot_select(AvbABOps* ab_ops,char select_slot[]); 25237a7bc39SJason Zhu 25383ab7b49SJason Zhu /** 25483ab7b49SJason Zhu * authenticated unlock 25583ab7b49SJason Zhu * 25683ab7b49SJason Zhu * @param buffer: AvbAtxUnlockCredential 25783ab7b49SJason Zhu * 25883ab7b49SJason Zhu * @param out_is_trusted: true or false 25983ab7b49SJason Zhu * 26083ab7b49SJason Zhu * @return 0 if authenticated unlock OK, -1 if not 26183ab7b49SJason Zhu */ 26283ab7b49SJason Zhu int rk_auth_unlock(void *buffer, char *out_is_trusted); 26383ab7b49SJason Zhu 26457c7f8feSJason Zhu /** 26557c7f8feSJason Zhu * generate unlock challenge 26657c7f8feSJason Zhu * 26757c7f8feSJason Zhu * @param buffer: AvbAtxUnlockChallenge 26857c7f8feSJason Zhu * 26957c7f8feSJason Zhu * @param challenge_len: Challenge length 27057c7f8feSJason Zhu * 27157c7f8feSJason Zhu * @param out_is_trusted: true or false 27257c7f8feSJason Zhu * 27357c7f8feSJason Zhu * @return 0 if generate unlock challenge OK, -1 if not 27457c7f8feSJason Zhu */ 27557c7f8feSJason Zhu int rk_generate_unlock_challenge(void *buffer, uint32_t *challenge_len); 27657c7f8feSJason Zhu 2777cca3dd4SJason Zhu /** 2787cca3dd4SJason Zhu * Get last boot slot 2797cca3dd4SJason Zhu * 2807cca3dd4SJason Zhu * @return 0 is slot A; 1 is slot B; -1 is error 2817cca3dd4SJason Zhu */ 2827cca3dd4SJason Zhu int rk_get_lastboot(void); 2837cca3dd4SJason Zhu 2849b83ce70SJason Zhu /** 2859b83ce70SJason Zhu * Get permanent attribute certificate 2869b83ce70SJason Zhu * 2879b83ce70SJason Zhu * @param cer: certificate data 2889b83ce70SJason Zhu * 2899b83ce70SJason Zhu * @param size: certificate size 2909b83ce70SJason Zhu */ 2919b83ce70SJason Zhu int rk_avb_get_perm_attr_cer(uint8_t *cer, uint32_t size); 2929b83ce70SJason Zhu 2939b83ce70SJason Zhu /** 2949b83ce70SJason Zhu * Set permanent attribute certificate 2959b83ce70SJason Zhu * 2969b83ce70SJason Zhu * @param cer: certificate data 2979b83ce70SJason Zhu * 2989b83ce70SJason Zhu * @param size: certificate size 2999b83ce70SJason Zhu */ 3009b83ce70SJason Zhu int rk_avb_set_perm_attr_cer(uint8_t *cer, uint32_t size); 3019b83ce70SJason Zhu 302*1f670f7cSJason Zhu /** 303*1f670f7cSJason Zhu * Get public key 304*1f670f7cSJason Zhu * 305*1f670f7cSJason Zhu * @param pub_key: public key data 306*1f670f7cSJason Zhu */ 307*1f670f7cSJason Zhu int rk_avb_get_pub_key(struct rk_pub_key *pub_key); 308*1f670f7cSJason Zhu 30937a7bc39SJason Zhu #ifdef __cplusplus 31037a7bc39SJason Zhu } 31137a7bc39SJason Zhu #endif 31237a7bc39SJason Zhu 31337a7bc39SJason Zhu #endif /* RK_AVB_OPS_USER_H_ */ 314