@@ -604,21 +604,36 @@ static int settings_zms_get_last_hash_ids(struct settings_zms *cf)
604604		rc  =  zms_read (& cf -> cf_zms , ll_last_hash_id , & settings_element ,
605605			      sizeof (settings_element ));
606606		if  (rc  ==  - ENOENT ) {
607- 			/* header doesn't exist or linked list broken, reinitialize the header */ 
608- 			const  struct  settings_hash_linked_list  settings_element  =  {
609- 				.previous_hash  =  0 , .next_hash  =  0 };
610- 			rc  =  zms_write (& cf -> cf_zms , ZMS_LL_HEAD_HASH_ID , & settings_element ,
611- 				       sizeof (struct  settings_hash_linked_list ));
612- 			if  (rc  <  0 ) {
613- 				return  rc ;
614- 			}
615- 			cf -> last_hash_id  =  ZMS_LL_HEAD_HASH_ID ;
616- 			cf -> second_to_last_hash_id  =  0 ;
607+ 			/* header doesn't exist or linked list broken, reinitialize the header 
608+ 			 * if it doesn't exist and recover it if it is broken 
609+ 			 */ 
610+ 			if  (ll_last_hash_id  ==  ZMS_LL_HEAD_HASH_ID ) {
611+ 				/* header doesn't exist */ 
612+ 				const  struct  settings_hash_linked_list  settings_element  =  {
613+ 					.previous_hash  =  0 , .next_hash  =  0 };
614+ 				rc  =  zms_write (& cf -> cf_zms , ZMS_LL_HEAD_HASH_ID , & settings_element ,
615+ 					       sizeof (struct  settings_hash_linked_list ));
616+ 				if  (rc  <  0 ) {
617+ 					return  rc ;
618+ 				}
619+ 				cf -> last_hash_id  =  ZMS_LL_HEAD_HASH_ID ;
620+ 				cf -> second_to_last_hash_id  =  0 ;
617621#ifdef  CONFIG_SETTINGS_ZMS_LL_CACHE 
618- 			/* store the LL header in cache */ 
619- 			cf -> ll_cache_next  =  0 ;
620- 			cf -> ll_cache [cf -> ll_cache_next ++ ] =  settings_element ;
622+ 				 /* store the LL header in cache */ 
623+ 				 cf -> ll_cache_next  =  0 ;
624+ 				 cf -> ll_cache [cf -> ll_cache_next ++ ] =  settings_element ;
621625#endif 
626+ 			} else  {
627+ 				/* let's recover it by keeping all nodes until the last one */ 
628+ 				const  struct  settings_hash_linked_list  settings_element  =  {
629+ 					.previous_hash  =  cf -> second_to_last_hash_id ,
630+ 					.next_hash  =  0 };
631+ 				rc  =  zms_write (& cf -> cf_zms , cf -> last_hash_id , & settings_element ,
632+ 					       sizeof (struct  settings_hash_linked_list ));
633+ 				if  (rc  <  0 ) {
634+ 					return  rc ;
635+ 				}
636+ 			}
622637			return  0 ;
623638		} else  if  (rc  <  0 ) {
624639			return  rc ;
0 commit comments