44 */
55
66#include "nand_programmer.h"
7+ #include "nand_bad_block.h"
78#include "fsmc_nand.h"
89#include "chip_db.h"
910#include "led.h"
4950 NP_ERR_BUF_OVERFLOW = -110 ,
5051 NP_ERR_LEN_NOT_ALIGN = -111 ,
5152 NP_ERR_LEN_EXCEEDED = -112 ,
53+ NP_ERR_LEN_INVALID = -113 ,
5254};
5355
5456typedef struct __attribute__((__packed__ ))
@@ -268,6 +270,7 @@ static int np_nand_erase(np_prog_t *prog, uint32_t page)
268270
269271static int _np_cmd_nand_erase (np_prog_t * prog )
270272{
273+ int is_bad ;
271274 uint32_t addr , page , pages_in_block , len ;
272275 np_erase_cmd_t * erase_cmd = (np_erase_cmd_t * )prog -> rx_buf ;
273276
@@ -283,6 +286,12 @@ static int _np_cmd_nand_erase(np_prog_t *prog)
283286 return NP_ERR_ADDR_NOT_ALIGN ;
284287 }
285288
289+ if (!len )
290+ {
291+ ERROR_PRINT ("Length is 0\r\n" );
292+ return NP_ERR_LEN_INVALID ;
293+ }
294+
286295 if (len & (prog -> chip_info -> block_size - 1 ))
287296 {
288297 ERROR_PRINT ("Length 0x%lx is not aligned to block size 0x%lx\r\n" ,
@@ -302,13 +311,28 @@ static int _np_cmd_nand_erase(np_prog_t *prog)
302311
303312 while (len )
304313 {
305- if (np_nand_erase (prog , page ))
306- return NP_ERR_NAND_ERASE ;
314+ if (addr >= prog -> chip_info -> size )
315+ {
316+ ERROR_PRINT ("Erase address 0x%lx is more then chip size 0x%lx\r\n" ,
317+ addr , prog -> chip_info -> size );
318+ return NP_ERR_ADDR_EXCEEDED ;
319+ }
307320
308- if (len >= prog -> chip_info -> block_size )
309- len -= prog -> chip_info -> block_size ;
321+ if ((is_bad = nand_bad_block_table_lookup (addr )))
322+ {
323+ DEBUG_PRINT ("Skipped bad block at 0x%lx\r\n" , addr );
324+ if (np_send_bad_block_info (addr ))
325+ return -1 ;
326+ }
310327
328+ if (!is_bad && np_nand_erase (prog , page ))
329+ return NP_ERR_NAND_ERASE ;
330+
331+ addr += prog -> chip_info -> block_size ;
311332 page += pages_in_block ;
333+ /* On partial erase do not count bad blocks */
334+ if (!is_bad || (is_bad && erase_cmd -> len == prog -> chip_info -> size ))
335+ len -= prog -> chip_info -> block_size ;
312336 }
313337
314338 return np_send_ok_status ();
@@ -676,6 +700,7 @@ static int np_cmd_nand_select(np_prog_t *prog)
676700 if (!chip_select (select_cmd -> chip_num ))
677701 {
678702 nand_init ();
703+ nand_bad_block_table_init ();
679704 prog -> chip_info = chip_info_selected_get ();
680705 }
681706 else
@@ -717,6 +742,8 @@ static int np_read_bad_block_info_from_page(np_prog_t *prog, uint32_t block,
717742 * is_bad = true;
718743 if (np_send_bad_block_info (addr ))
719744 return -1 ;
745+ if (nand_bad_block_table_add (addr ))
746+ return -1 ;
720747 }
721748 else
722749 * is_bad = false;
0 commit comments