@@ -41,30 +41,33 @@ static uint16_t _max_rk_amnt;
4141static bool _flash_is_erased (int page , int offset , size_t len );
4242
4343/**
44- * @brief Get amount of flashpages
44+ * @brief Get available amount of flashpages to store resident keys
4545 */
46- static unsigned _amount_of_flashpages (void );
46+ static unsigned _amount_flashpages_rk (void );
4747
4848int fido2_ctap_mem_init (void )
4949{
5050 int ret ;
51+ unsigned start = fido2_ctap_mem_get_rk_start_page ();
52+ unsigned amount = _amount_flashpages_rk ();
5153
5254 ret = mtd_init (& _mtd_dev );
5355
5456 if (ret < 0 ) {
5557 return ret ;
5658 }
5759
58- for (unsigned i = CTAP_FLASH_RK_START_PAGE ; i < _amount_of_flashpages () ; i ++ ) {
60+ for (unsigned i = start ; i < start + amount ; i ++ ) {
5961 _max_rk_amnt += flashpage_size (i ) / CTAP_FLASH_RK_SZ ;
6062 }
6163
6264 return CTAP2_OK ;
6365}
6466
65- static unsigned _amount_of_flashpages (void )
67+ static unsigned _amount_flashpages_rk (void )
6668{
67- return _mtd_dev .sector_count * _mtd_dev .pages_per_sector ;
69+ /* -1 because first page holds authenticator state information */
70+ return CONFIG_FIDO2_CTAP_NUM_FLASHPAGES - 1 ;
6871}
6972
7073int fido2_ctap_mem_read (void * buf , uint32_t page , uint32_t offset , uint32_t len )
@@ -127,8 +130,10 @@ uint16_t fido2_ctap_mem_get_max_rk_amount(void)
127130int fido2_ctap_mem_get_flashpage_number_of_rk (uint16_t rk_idx )
128131{
129132 uint16_t idx = 0 ;
133+ unsigned start = fido2_ctap_mem_get_rk_start_page ();
134+ unsigned amount = _amount_flashpages_rk ();
130135
131- for (unsigned i = CTAP_FLASH_RK_START_PAGE ; i < _amount_of_flashpages () ; i ++ ) {
136+ for (unsigned i = start ; i < start + amount ; i ++ ) {
132137 idx += flashpage_size (i ) / CTAP_FLASH_RK_SZ ;
133138
134139 if (idx >= rk_idx ) {
@@ -142,8 +147,10 @@ int fido2_ctap_mem_get_flashpage_number_of_rk(uint16_t rk_idx)
142147int fido2_ctap_mem_get_offset_of_rk_into_flashpage (uint16_t rk_idx )
143148{
144149 uint16_t idx = 0 ;
150+ unsigned start = fido2_ctap_mem_get_rk_start_page ();
151+ unsigned amount = _amount_flashpages_rk ();
145152
146- for (unsigned i = CTAP_FLASH_RK_START_PAGE ; i < _amount_of_flashpages () ; i ++ ) {
153+ for (unsigned i = start ; i < start + amount ; i ++ ) {
147154 uint16_t old_idx = idx ;
148155 idx += flashpage_size (i ) / CTAP_FLASH_RK_SZ ;
149156
@@ -154,3 +161,13 @@ int fido2_ctap_mem_get_offset_of_rk_into_flashpage(uint16_t rk_idx)
154161
155162 return -1 ;
156163}
164+
165+ unsigned fido2_ctap_mem_get_state_flashpage_number (void )
166+ {
167+ return flashpage_last_free () - CONFIG_FIDO2_CTAP_NUM_FLASHPAGES + 1 ;
168+ }
169+
170+ unsigned fido2_ctap_mem_get_rk_start_page (void )
171+ {
172+ return fido2_ctap_mem_get_state_flashpage_number () + 1 ;
173+ }
0 commit comments