@@ -197,6 +197,7 @@ void _main(void)
197197 // get the nandfolder from /sneek/nandpath.bin file.
198198 char * path = (char * )heap_alloc_aligned ( 0 , 0x40 , 0x40 );
199199 char * npath = (char * )heap_alloc_aligned ( 0 , 0x40 , 0x40 );
200+ char * tempnroot = (char * )heap_alloc_aligned ( 0 , 0x40 , 0x40 );
200201 u8 * NInfo = (u8 * )heap_alloc_aligned ( 0 , 0xc0 , 32 );
201202 u8 * fpath = (u8 * )heap_alloc_aligned ( 0 , 0x80 , 32 );
202203
@@ -271,74 +272,86 @@ void _main(void)
271272
272273 strcpy ( path , PATHFILE );
273274 strcpy ( npath , NANDFOLDER );
274- if ( f_open ( & fil , path , FA_READ ) != FR_OK )
275+
276+ if ( f_open ( & fil , path , FA_READ ) == FR_OK )
277+ {
278+ if ( NandCFG )
279+ heap_free ( 0 , NandCFG );
280+
281+ NandCFG = (NandConfig * )heap_alloc_aligned ( 0 , fil .fsize , 32 );
282+ f_open ( & fil , path , FA_READ );
283+ f_read ( & fil , NandCFG , fil .fsize , & read );
284+ f_close (& fil );
285+ __sprintf ( tempnroot , "%.63s" , NandCFG -> NandInfo [NandCFG -> NandSel ] );
286+ f_unlink ( path );
287+ heap_free ( 0 , NandCFG );
288+ }
289+
290+ if ( f_opendir ( & dir , npath ) == FR_OK )
275291 {
276- if ( f_opendir ( & dir , npath ) == FR_OK )
292+ u32 ncnt = 0 ;
293+ f_open ( & fil , path , FA_WRITE |FA_CREATE_ALWAYS );
294+ NandCFG -> NandCnt = 0 ;
295+ NandCFG -> NandSel = 0 ;
296+ f_write ( & fil , NandCFG , NANDCFG_SIZE , & write );
297+ f_lseek ( & fil , 0x10 );
298+ usenfol = 1 ;
299+ while ( f_readdir ( & dir , & FInfo ) == FR_OK )
277300 {
278- u32 ncnt = 0 ;
279- f_open ( & fil , path , FA_WRITE |FA_CREATE_ALWAYS );
280- NandCFG -> NandCnt = 0 ;
281- NandCFG -> NandSel = 0 ;
282- f_write ( & fil , NandCFG , NANDCFG_SIZE , & write );
283- f_lseek ( & fil , 0x10 );
284- usenfol = 1 ;
285- while ( f_readdir ( & dir , & FInfo ) == FR_OK )
286- {
287- if ( FInfo .lfsize )
288- memcpy ( NInfo + NANDDESC_OFF , FInfo .lfname , NANDDESC_OFF );
289- else
290- memcpy ( NInfo + NANDDESC_OFF , FInfo .fname , NANDDESC_OFF );
301+ if ( FInfo .lfsize )
302+ memcpy ( NInfo + NANDDESC_OFF , FInfo .lfname , NANDDESC_OFF );
303+ else
304+ memcpy ( NInfo + NANDDESC_OFF , FInfo .fname , NANDDESC_OFF );
291305
292-
293- strcpy (fpath ,NANDFOLDER );
294- slen = strlen (fpath );
295- fpath [slen ] = '/' ;
296- strncpy (fpath + slen + 1 ,(char * )(NInfo + NANDDESC_OFF ),0x80 - slen - 1 );
297- memcpy (NInfo ,fpath ,0x40 );
306+ strcpy ( fpath , NANDFOLDER );
307+ slen = strlen (fpath );
308+ fpath [slen ] = '/' ;
309+ strncpy ( fpath + slen + 1 , (char * )(NInfo + NANDDESC_OFF ), 0x80 - slen - 1 );
310+ memcpy ( NInfo , fpath , 0x40 );
298311
299- memcpy ( NInfo + NANDDI_OFF , diroot , NANDDESC_OFF );
312+ memcpy ( NInfo + NANDDI_OFF , diroot , NANDDESC_OFF );
300313
301- f_write ( & fil , NInfo , NANDINFO_SIZE , & write );
314+ f_write ( & fil , NInfo , NANDINFO_SIZE , & write );
302315 //here we create fpath which is the full path to the nand
303316 //and we check if it is compatible with our nandpath
304317 //if it's the case, we simply consider the entry as ok
305318 //no need to do this if nandroot is empty
306- if (nandroot [0 ] != 0 )
307- {
308- slen = strlen (nandroot );
309- if (memcmp (nandroot ,fpath ,slen )== 0 )
310- {
311- NandCFG -> NandSel = ncnt ;
312- usenfol = 0 ;
313- }
314- }
315- ncnt ++ ;
316- }
317- //add the nandpath entry to the nandcfg entries.
318-
319- if ((nandroot [0 ]!= 0 )&& (usenfol == 1 ))
319+ if (nandroot [0 ] != 0 )
320320 {
321- if (ncnt < 40 )
321+ slen = strlen ( nandroot );
322+ if ( memcmp ( nandroot , fpath , slen ) == 0 )
322323 {
323- memcpy (NInfo ,nandroot ,NANDDESC_OFF );
324- memcpy (NInfo + NANDDESC_OFF ,nandroot ,NANDDESC_OFF );
325- memcpy ( NInfo + NANDDI_OFF , diroot , NANDDESC_OFF );
326- f_write ( & fil , NInfo , NANDINFO_SIZE , & write );
327- NandCFG -> NandSel = ncnt ;
328- ncnt ++ ;
324+ //NandCFG->NandSel = ncnt;
325+ usenfol = 0 ;
329326 }
330- }
331-
332- NandCFG -> NandCnt = ncnt ;
333- f_lseek ( & fil , 0 );
334- f_write ( & fil , NandCFG , NANDCFG_SIZE , & write );
335- f_close ( & fil );
327+ }
328+ if ( strcmp ( tempnroot , fpath ) == 0 )
329+ NandCFG -> NandSel = ncnt ;
330+
331+ ncnt ++ ;
336332 }
337- else
333+ //add the nandpath entry to the nandcfg entries.
334+
335+ if ( ( nandroot [0 ] != 0 ) && ( usenfol == 1 ) )
338336 {
339- usenfol = 0 ;
337+ memcpy ( NInfo , nandroot , NANDDESC_OFF );
338+ memcpy ( NInfo + NANDDESC_OFF , nandroot , NANDDESC_OFF );
339+ memcpy ( NInfo + NANDDI_OFF , diroot , NANDDESC_OFF );
340+ f_write ( & fil , NInfo , NANDINFO_SIZE , & write );
341+ NandCFG -> NandSel = ncnt ;
342+ ncnt ++ ;
340343 }
344+
345+ NandCFG -> NandCnt = ncnt ;
346+ f_lseek ( & fil , 0 );
347+ f_write ( & fil , NandCFG , NANDCFG_SIZE , & write );
348+ f_close ( & fil );
341349 }
350+ else
351+ {
352+ usenfol = 0 ;
353+ }
354+
342355
343356 if ( usenfol )
344357 {
@@ -364,6 +377,7 @@ void _main(void)
364377 }
365378
366379 heap_free ( 0 , fpath );
380+ heap_free ( 0 , tempnroot );
367381 heap_free ( 0 , NInfo );
368382 heap_free ( 0 , path );
369383 heap_free ( 0 , npath );
0 commit comments