@@ -610,21 +610,36 @@ static int settings_zms_get_last_hash_ids(struct settings_zms *cf)
610610 rc = zms_read (& cf -> cf_zms , ll_last_hash_id , & settings_element ,
611611 sizeof (settings_element ));
612612 if (rc == - ENOENT ) {
613- /* header doesn't exist or linked list broken, reinitialize the header */
614- const struct settings_hash_linked_list settings_element = {
615- .previous_hash = 0 , .next_hash = 0 };
616- rc = zms_write (& cf -> cf_zms , ZMS_LL_HEAD_HASH_ID , & settings_element ,
617- sizeof (struct settings_hash_linked_list ));
618- if (rc < 0 ) {
619- return rc ;
620- }
621- cf -> last_hash_id = ZMS_LL_HEAD_HASH_ID ;
622- cf -> second_to_last_hash_id = 0 ;
613+ /* header doesn't exist or linked list broken, reinitialize the header
614+ * if it doesn't exist and recover it if it is broken
615+ */
616+ if (ll_last_hash_id == ZMS_LL_HEAD_HASH_ID ) {
617+ /* header doesn't exist */
618+ const struct settings_hash_linked_list settings_element = {
619+ .previous_hash = 0 , .next_hash = 0 };
620+ rc = zms_write (& cf -> cf_zms , ZMS_LL_HEAD_HASH_ID , & settings_element ,
621+ sizeof (struct settings_hash_linked_list ));
622+ if (rc < 0 ) {
623+ return rc ;
624+ }
625+ cf -> last_hash_id = ZMS_LL_HEAD_HASH_ID ;
626+ cf -> second_to_last_hash_id = 0 ;
623627#ifdef CONFIG_SETTINGS_ZMS_LL_CACHE
624- /* store the LL header in cache */
625- cf -> ll_cache_next = 0 ;
626- cf -> ll_cache [cf -> ll_cache_next ++ ] = settings_element ;
628+ /* store the LL header in cache */
629+ cf -> ll_cache_next = 0 ;
630+ cf -> ll_cache [cf -> ll_cache_next ++ ] = settings_element ;
627631#endif
632+ } else {
633+ /* let's recover it by keeping all nodes until the last one */
634+ const struct settings_hash_linked_list settings_element = {
635+ .previous_hash = cf -> second_to_last_hash_id ,
636+ .next_hash = 0 };
637+ rc = zms_write (& cf -> cf_zms , cf -> last_hash_id , & settings_element ,
638+ sizeof (struct settings_hash_linked_list ));
639+ if (rc < 0 ) {
640+ return rc ;
641+ }
642+ }
628643 return 0 ;
629644 } else if (rc < 0 ) {
630645 return rc ;
0 commit comments