xref: /rk3399_rockchip-uboot/include/android_avb/rk_avb_ops_user.h (revision 6e2db7c2c8eb14e0a5f8852aae9ff54c059a3817)
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
331f670f7cSJason Zhu #define ROCHCHIP_RSA_PARAMETER_SIZE 64
341f670f7cSJason Zhu 
351f670f7cSJason Zhu struct rk_pub_key {
361f670f7cSJason Zhu 	u_int32_t rsa_n[ROCHCHIP_RSA_PARAMETER_SIZE];
371f670f7cSJason Zhu 	u_int32_t rsa_e[ROCHCHIP_RSA_PARAMETER_SIZE];
381f670f7cSJason Zhu 	u_int32_t rsa_c[ROCHCHIP_RSA_PARAMETER_SIZE];
391f670f7cSJason 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 /**
80eb89f0a8SJoseph Chen  * Append current slot to given partition name
81eb89f0a8SJoseph Chen  *
82eb89f0a8SJoseph Chen  * @param part_name	partition name
83eb89f0a8SJoseph Chen  * @param slot		given slot suffix, auto append current slot if NULL
84eb89f0a8SJoseph Chen  * @param new_name	partition name with slot suffix appended
85eb89f0a8SJoseph Chen  *
86eb89f0a8SJoseph Chen  * @return 0 if the command succeeded, -1 if it failed
87eb89f0a8SJoseph Chen  */
88eb89f0a8SJoseph Chen int rk_avb_append_part_slot(const char *part_name, char *new_name);
89eb89f0a8SJoseph Chen 
90eb89f0a8SJoseph Chen /**
9137a7bc39SJason Zhu  * The android things defines permanent attributes to
9237a7bc39SJason Zhu  * store PSK_public, product id. We can use this function
9337a7bc39SJason Zhu  * to read them.
9437a7bc39SJason Zhu  *
9537a7bc39SJason Zhu  * @param attributes  PSK_public, product id....
9637a7bc39SJason Zhu  *
9737a7bc39SJason Zhu  * @param size        The size of attributes.
9837a7bc39SJason Zhu  *
9937a7bc39SJason Zhu  * @return 0 if the command succeeded, -1 if it failed
10037a7bc39SJason Zhu  */
10137a7bc39SJason Zhu int rk_avb_read_permanent_attributes(uint8_t *attributes, uint32_t size);
10237a7bc39SJason Zhu 
10337a7bc39SJason Zhu /**
10437a7bc39SJason Zhu  * The android things defines permanent attributes to
10537a7bc39SJason Zhu  * store PSK_public, product id. We can use this function
10637a7bc39SJason Zhu  * to write them.
10737a7bc39SJason Zhu  *
10837a7bc39SJason Zhu  * @param attributes  PSK_public, product id....
10937a7bc39SJason Zhu  *
11037a7bc39SJason Zhu  * @param size        The size of attributes.
11137a7bc39SJason Zhu  *
11237a7bc39SJason Zhu  * @return 0 if the command succeeded, -1 if it failed
11337a7bc39SJason Zhu  */
11437a7bc39SJason Zhu int rk_avb_write_permanent_attributes(uint8_t *attributes, uint32_t size);
11537a7bc39SJason Zhu 
11637a7bc39SJason Zhu /**
11737a7bc39SJason Zhu  * The funtion can be use to read the device state to judge
11837a7bc39SJason Zhu  * whether the device can be flash.
11937a7bc39SJason Zhu  *
12037a7bc39SJason Zhu  * @param flash_lock_state  A flag indicate the device flash state.
12137a7bc39SJason Zhu  *
12237a7bc39SJason Zhu  * @return 0 if the command succeeded, -1 if it failed
12337a7bc39SJason Zhu  */
12437a7bc39SJason Zhu int rk_avb_read_flash_lock_state(uint8_t *flash_lock_state);
12537a7bc39SJason Zhu 
12637a7bc39SJason Zhu /**
12737a7bc39SJason Zhu  * The function is provided to write device flash state.
12837a7bc39SJason Zhu  *
12937a7bc39SJason Zhu  * @param flash_lock_state   A flag indicate the device flash state.
13037a7bc39SJason Zhu  *
13137a7bc39SJason Zhu  * @return 0 if the command succeeded, -1 if it failed
13237a7bc39SJason Zhu  */
13337a7bc39SJason Zhu int rk_avb_write_flash_lock_state(uint8_t flash_lock_state);
13437a7bc39SJason Zhu 
13537a7bc39SJason Zhu /**
13637a7bc39SJason Zhu  * The android things use the flag of lock state to indicate
13737a7bc39SJason Zhu  * whether the device can be booted when verified error.
13837a7bc39SJason Zhu  *
13937a7bc39SJason Zhu  * @param lock_state  A flag indicate the device lock state.
14037a7bc39SJason Zhu  *
14137a7bc39SJason Zhu  * @return 0 if the command succeeded, -1 if it failed
14237a7bc39SJason Zhu  */
14337a7bc39SJason Zhu int rk_avb_read_lock_state(uint8_t *lock_state);
14437a7bc39SJason Zhu 
14537a7bc39SJason Zhu /**
14637a7bc39SJason Zhu  * The android things use the flag of lock state to indicate
14737a7bc39SJason Zhu  * whether the device can be booted when verified error.
14837a7bc39SJason Zhu  *
14937a7bc39SJason Zhu  * @param lock_state   A flag indicate the device lock state.
15037a7bc39SJason Zhu  *
15137a7bc39SJason Zhu  * @return 0 if the command succeeded, -1 if it failed
15237a7bc39SJason Zhu  */
15337a7bc39SJason Zhu int rk_avb_write_lock_state(uint8_t lock_state);
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   indicate the permanent attributes have been written
16037a7bc39SJason Zhu  *               or not.
16137a7bc39SJason Zhu  *
16237a7bc39SJason Zhu  * @return 0 if the command succeeded, -1 if it failed
16337a7bc39SJason Zhu  */
16437a7bc39SJason Zhu int rk_avb_read_perm_attr_flag(uint8_t *flag);
16537a7bc39SJason Zhu 
16637a7bc39SJason Zhu /**
16737a7bc39SJason Zhu  * The android things uses fastboot to flash the permanent attributes.
16837a7bc39SJason Zhu  * And if them were written, there must have a flag to indicate.
16937a7bc39SJason Zhu  *
17037a7bc39SJason Zhu  * @param flag   We can call this function to write the flag '1'
17137a7bc39SJason Zhu  *               to indicate the permanent attributes has been
17237a7bc39SJason Zhu  *               written.
17337a7bc39SJason Zhu  *
17437a7bc39SJason Zhu  * @return 0 if the command succeeded, -1 if it failed
17537a7bc39SJason Zhu  */
17637a7bc39SJason Zhu int rk_avb_write_perm_attr_flag(uint8_t flag);
17737a7bc39SJason Zhu 
17837a7bc39SJason Zhu /**
17937a7bc39SJason Zhu  * The android things require the soc-v key hash to be flashed
18037a7bc39SJason Zhu  * using the fastboot. So the function can be used in fastboot
18137a7bc39SJason Zhu  * to flash the key hash.
18237a7bc39SJason Zhu  *
18337a7bc39SJason Zhu  * @param buf    The vboot key hash data.
18437a7bc39SJason Zhu  *
18537a7bc39SJason Zhu  * @param length The length of key hash.
18637a7bc39SJason Zhu  *
18737a7bc39SJason Zhu  * @return 0 if the command succeeded, -1 if it failed
18837a7bc39SJason Zhu  */
18937a7bc39SJason Zhu int rk_avb_read_vbootkey_hash(uint8_t *buf, uint8_t length);
19037a7bc39SJason Zhu 
19137a7bc39SJason Zhu /**
19237a7bc39SJason Zhu  * The android things require the soc-v key hash to be flashed
19337a7bc39SJason Zhu  * using the fastboot. So the function can be used in fastboot
19437a7bc39SJason Zhu  * to flash the key hash.
19537a7bc39SJason Zhu  *
19637a7bc39SJason Zhu  * @param buf    The vboot key hash data.
19737a7bc39SJason Zhu  *
19837a7bc39SJason Zhu  * @param length The length of key hash.
19937a7bc39SJason Zhu  *
20037a7bc39SJason Zhu  * @return 0 if the command succeeded, -1 if it failed
20137a7bc39SJason Zhu  */
20237a7bc39SJason Zhu int rk_avb_write_vbootkey_hash(uint8_t *buf, uint8_t length);
20337a7bc39SJason Zhu 
20437a7bc39SJason Zhu /**
20537a7bc39SJason Zhu  * U-boot close the optee client when start kernel
20637a7bc39SJason Zhu  * to prevent the optee client being invoking by other
20737a7bc39SJason Zhu  * program.
20837a7bc39SJason Zhu  *
20937a7bc39SJason Zhu  * @return 0 if the command succeeded, -1 if it failed
21037a7bc39SJason Zhu  */
21137a7bc39SJason Zhu int rk_avb_close_optee_client(void);
21237a7bc39SJason Zhu 
21337a7bc39SJason Zhu /**
21437a7bc39SJason Zhu  * read 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_read_attribute_hash(uint8_t *buf, uint8_t length);
22337a7bc39SJason Zhu 
22437a7bc39SJason Zhu /**
22537a7bc39SJason Zhu  * Write the permanent attributes hash.
22637a7bc39SJason Zhu  *
22737a7bc39SJason Zhu  * @param buf    The permanent attributes hash data.
22837a7bc39SJason Zhu  *
22937a7bc39SJason Zhu  * @param length The length of permanent attributes hash.
23037a7bc39SJason Zhu  *
23137a7bc39SJason Zhu  * @return 0 if the command succeeded, -1 if it failed
23237a7bc39SJason Zhu  */
23337a7bc39SJason Zhu int rk_avb_write_attribute_hash(uint8_t *buf, uint8_t length);
23437a7bc39SJason Zhu 
23537a7bc39SJason Zhu /**
23637a7bc39SJason Zhu  * Get the avb vboot state
23737a7bc39SJason Zhu  *
23837a7bc39SJason Zhu  * @param buf    store the vboot state.
23937a7bc39SJason Zhu  *
24037a7bc39SJason Zhu  * @return 0 if the command succeeded, -1 if it failed
24137a7bc39SJason Zhu  */
24237a7bc39SJason Zhu void rk_avb_get_at_vboot_state(char *buf);
24337a7bc39SJason Zhu 
24437a7bc39SJason Zhu /**
24537a7bc39SJason Zhu  * Get ab information from misc partition
24637a7bc39SJason Zhu  *
24737a7bc39SJason Zhu  * @param ab_data    the struct of ab information
24837a7bc39SJason Zhu  *
24937a7bc39SJason Zhu  * @return 0 if the command succeeded, -1 if it failed
25037a7bc39SJason Zhu  */
25137a7bc39SJason Zhu int rk_avb_get_ab_info(AvbABData* ab_data);
25237a7bc39SJason Zhu 
25337a7bc39SJason Zhu /**
25437a7bc39SJason Zhu  * Get the information whether the partition has slot
25537a7bc39SJason Zhu  *
25637a7bc39SJason Zhu  * @param the partition name
25737a7bc39SJason Zhu  *
25837a7bc39SJason Zhu  * @return 0 if the partition has slot, -1 if not
25937a7bc39SJason Zhu  */
26037a7bc39SJason Zhu int rk_avb_get_part_has_slot_info(const char *base_name);
26137a7bc39SJason Zhu 
26237a7bc39SJason Zhu AvbABFlowResult rk_avb_ab_slot_select(AvbABOps* ab_ops,char select_slot[]);
26337a7bc39SJason Zhu 
26483ab7b49SJason Zhu /**
26583ab7b49SJason Zhu  * authenticated unlock
26683ab7b49SJason Zhu  *
26783ab7b49SJason Zhu  * @param buffer: AvbAtxUnlockCredential
26883ab7b49SJason Zhu  *
26983ab7b49SJason Zhu  * @param out_is_trusted: true or false
27083ab7b49SJason Zhu  *
27183ab7b49SJason Zhu  * @return 0 if authenticated unlock OK, -1 if not
27283ab7b49SJason Zhu  */
27383ab7b49SJason Zhu int rk_auth_unlock(void *buffer, char *out_is_trusted);
27483ab7b49SJason Zhu 
27557c7f8feSJason Zhu /**
27657c7f8feSJason Zhu  * generate unlock challenge
27757c7f8feSJason Zhu  *
27857c7f8feSJason Zhu  * @param buffer: AvbAtxUnlockChallenge
27957c7f8feSJason Zhu  *
28057c7f8feSJason Zhu  * @param challenge_len: Challenge length
28157c7f8feSJason Zhu  *
28257c7f8feSJason Zhu  * @param out_is_trusted: true or false
28357c7f8feSJason Zhu  *
28457c7f8feSJason Zhu  * @return 0 if generate unlock challenge OK, -1 if not
28557c7f8feSJason Zhu  */
28657c7f8feSJason Zhu int rk_generate_unlock_challenge(void *buffer, uint32_t *challenge_len);
28757c7f8feSJason Zhu 
2887cca3dd4SJason Zhu /**
2897cca3dd4SJason Zhu  * Get last boot slot
2907cca3dd4SJason Zhu  *
2917cca3dd4SJason Zhu  * @return 0 is slot A; 1 is slot B; -1 is error
2927cca3dd4SJason Zhu  */
2937cca3dd4SJason Zhu int rk_get_lastboot(void);
2947cca3dd4SJason Zhu 
2959b83ce70SJason Zhu /**
2969b83ce70SJason Zhu  * Get permanent attribute certificate
2979b83ce70SJason Zhu  *
2989b83ce70SJason Zhu  * @param cer: certificate data
2999b83ce70SJason Zhu  *
3009b83ce70SJason Zhu  * @param size: certificate size
3019b83ce70SJason Zhu  */
3029b83ce70SJason Zhu int rk_avb_get_perm_attr_cer(uint8_t *cer, uint32_t size);
3039b83ce70SJason Zhu 
3049b83ce70SJason Zhu /**
3059b83ce70SJason Zhu  * Set permanent attribute certificate
3069b83ce70SJason Zhu  *
3079b83ce70SJason Zhu  * @param cer: certificate data
3089b83ce70SJason Zhu  *
3099b83ce70SJason Zhu  * @param size: certificate size
3109b83ce70SJason Zhu  */
3119b83ce70SJason Zhu int rk_avb_set_perm_attr_cer(uint8_t *cer, uint32_t size);
3129b83ce70SJason Zhu 
3131f670f7cSJason Zhu /**
3141f670f7cSJason Zhu  * Get public key
3151f670f7cSJason Zhu  *
3161f670f7cSJason Zhu  * @param pub_key: public key data
3171f670f7cSJason Zhu  */
3181f670f7cSJason Zhu int rk_avb_get_pub_key(struct rk_pub_key *pub_key);
3191f670f7cSJason Zhu 
320813227adSJason Zhu /**
321813227adSJason Zhu  * init ab metadata
322813227adSJason Zhu  */
323813227adSJason Zhu int rk_avb_init_ab_metadata(void);
324813227adSJason Zhu 
325*6e2db7c2SWu Liangqing /**
326*6e2db7c2SWu Liangqing  * rockchip avb commands
327*6e2db7c2SWu Liangqing  *
328*6e2db7c2SWu Liangqing  */
329*6e2db7c2SWu Liangqing int rk_avb_write_perm_attr(uint16_t id, void *pbuf, uint16_t size);
330*6e2db7c2SWu Liangqing int rk_avb_read_perm_attr(uint16_t id, void *pbuf, uint16_t size);
331*6e2db7c2SWu Liangqing 
33237a7bc39SJason Zhu #ifdef __cplusplus
33337a7bc39SJason Zhu }
33437a7bc39SJason Zhu #endif
33537a7bc39SJason Zhu 
33637a7bc39SJason Zhu #endif /* RK_AVB_OPS_USER_H_ */
337