37
37
{
38
38
NP_ERR_ADDR_EXCEEDED = 0x00 ,
39
39
NP_ERR_ADDR_INVALID = 0x01 ,
40
- NP_ERR_NAND_WR = 0x02 ,
41
- NP_ERR_NAND_RD = 0x03 ,
42
- NP_ERR_NAND_ERASE = 0x04 ,
43
- NP_ERR_CHIP_NOT_SEL = 0x05 ,
44
- NP_ERR_CHIP_NOT_FOUND = 0x06 ,
45
- NP_ERR_CMD_DATA_SIZE = 0x07 ,
46
- NP_ERR_CMD_INVALID = 0x08 ,
47
- NP_ERR_BUF_OVERFLOW = 0x09 ,
40
+ NP_ERR_ADDR_NOT_ALIGN = 0x02 ,
41
+ NP_ERR_NAND_WR = 0x03 ,
42
+ NP_ERR_NAND_RD = 0x04 ,
43
+ NP_ERR_NAND_ERASE = 0x05 ,
44
+ NP_ERR_CHIP_NOT_SEL = 0x06 ,
45
+ NP_ERR_CHIP_NOT_FOUND = 0x07 ,
46
+ NP_ERR_CMD_DATA_SIZE = 0x08 ,
47
+ NP_ERR_CMD_INVALID = 0x09 ,
48
+ NP_ERR_BUF_OVERFLOW = 0x0a ,
49
+ NP_ERR_LEN_NOT_ALIGN = 0x0b ,
48
50
};
49
51
50
52
typedef struct __attribute__((__packed__ ))
@@ -228,9 +230,10 @@ static int np_cmd_nand_read_id(np_prog_t *prog)
228
230
return 0 ;
229
231
}
230
232
231
- static int np_nand_erase (uint32_t page , uint32_t addr )
233
+ static int np_nand_erase (np_prog_t * prog , uint32_t page )
232
234
{
233
235
uint32_t status ;
236
+ uint32_t addr = page * prog -> chip_info -> page_size ;
234
237
235
238
DEBUG_PRINT ("NAND erase at 0x%lx\r\n" , addr );
236
239
@@ -256,37 +259,40 @@ static int np_nand_erase(uint32_t page, uint32_t addr)
256
259
257
260
static int np_cmd_nand_erase (np_prog_t * prog )
258
261
{
259
- uint32_t addr , page , pages_in_block ;
262
+ uint32_t addr , page , pages_in_block , len ;
260
263
np_erase_cmd_t * erase_cmd = (np_erase_cmd_t * )prog -> rx_buf ;
261
264
265
+ len = erase_cmd -> len ;
266
+ addr = erase_cmd -> addr ;
267
+
268
+ DEBUG_PRINT ("Erase at 0x%lx %lx bytes command\r\n" , addr , len );
269
+
262
270
led_wr_set (true);
263
271
264
- DEBUG_PRINT ("Erase at 0x%lx %lx bytes command\r\n" , erase_cmd -> addr ,
265
- erase_cmd -> len );
272
+ if (addr & (prog -> chip_info -> block_size - 1 ))
273
+ return np_send_error (NP_ERR_ADDR_NOT_ALIGN );
274
+
275
+ if (len & (prog -> chip_info -> block_size - 1 ))
276
+ return np_send_error (NP_ERR_LEN_NOT_ALIGN );
266
277
267
- addr = erase_cmd -> addr & ~(prog -> chip_info -> block_size - 1 );
268
- erase_cmd -> len += erase_cmd -> addr - addr ;
278
+ if (addr + len > prog -> chip_info -> size )
279
+ {
280
+ ERROR_PRINT ("Erase address exceded 0x%lx+0x%lx is more then chip size "
281
+ "0x%lx\r\n" , addr , len , prog -> chip_info -> size );
282
+ return np_send_error (NP_ERR_ADDR_EXCEEDED );
283
+ }
269
284
270
285
page = addr / prog -> chip_info -> page_size ;
271
286
pages_in_block = prog -> chip_info -> block_size / prog -> chip_info -> page_size ;
272
287
273
- while (erase_cmd -> len )
288
+ while (len )
274
289
{
275
- if (addr >= prog -> chip_info -> size )
276
- {
277
- ERROR_PRINT ("Erase address 0x%lx is more then chip size 0x%lx\r\n" ,
278
- addr , prog -> chip_info -> size );
279
- return np_send_error (NP_ERR_ADDR_EXCEEDED );
280
- }
281
-
282
- if (np_nand_erase (page , addr ))
290
+ if (np_nand_erase (prog , page ))
283
291
return np_send_error (NP_ERR_NAND_ERASE );
284
292
285
- if (erase_cmd -> len >= prog -> chip_info -> block_size )
286
- erase_cmd -> len -= prog -> chip_info -> block_size ;
287
- else
288
- erase_cmd -> len = 0 ;
289
- addr += prog -> chip_info -> block_size ;
293
+ if (len >= prog -> chip_info -> block_size )
294
+ len -= prog -> chip_info -> block_size ;
295
+
290
296
page += pages_in_block ;
291
297
}
292
298
0 commit comments