Skip to content

Commit e009194

Browse files
committed
Return -1 on error explicitly
1 parent d535124 commit e009194

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed

firmware/nand_programmer.c

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ static int np_send_error(uint8_t err_code)
195195
if (np_comm_cb)
196196
np_comm_cb->send((uint8_t *)&err_status, len);
197197

198-
return -1;
198+
return 0;
199199
}
200200

201201
static int np_send_bad_block_info(uint32_t addr)
@@ -275,16 +275,23 @@ static int _np_cmd_nand_erase(np_prog_t *prog)
275275
DEBUG_PRINT("Erase at 0x%lx %lx bytes command\r\n", addr, len);
276276

277277
if (addr & (prog->chip_info->block_size - 1))
278-
return np_send_error(NP_ERR_ADDR_NOT_ALIGN);
278+
{
279+
np_send_error(NP_ERR_ADDR_NOT_ALIGN);
280+
return -1;
281+
}
279282

280283
if (len & (prog->chip_info->block_size - 1))
281-
return np_send_error(NP_ERR_LEN_NOT_ALIGN);
284+
{
285+
np_send_error(NP_ERR_LEN_NOT_ALIGN);
286+
return -1;
287+
}
282288

283289
if (addr + len > prog->chip_info->size)
284290
{
285291
ERROR_PRINT("Erase address exceded 0x%lx+0x%lx is more then chip size "
286292
"0x%lx\r\n", addr, len, prog->chip_info->size);
287-
return np_send_error(NP_ERR_ADDR_EXCEEDED);
293+
np_send_error(NP_ERR_ADDR_EXCEEDED);
294+
return -1;
288295
}
289296

290297
page = addr / prog->chip_info->page_size;
@@ -293,7 +300,10 @@ static int _np_cmd_nand_erase(np_prog_t *prog)
293300
while (len)
294301
{
295302
if (np_nand_erase(prog, page))
296-
return np_send_error(NP_ERR_NAND_ERASE);
303+
{
304+
np_send_error(NP_ERR_NAND_ERASE);
305+
return -1;
306+
}
297307

298308
if (len >= prog->chip_info->block_size)
299309
len -= prog->chip_info->block_size;
@@ -337,7 +347,8 @@ static int np_cmd_nand_write_start(np_prog_t *prog)
337347
{
338348
ERROR_PRINT("Write address 0x%lx is more then chip size 0x%lx\r\n",
339349
write_start_cmd->addr, prog->chip_info->size);
340-
return np_send_error(NP_ERR_ADDR_EXCEEDED);
350+
np_send_error(NP_ERR_ADDR_EXCEEDED);
351+
return -1;
341352
}
342353

343354
prog->addr = write_start_cmd->addr;
@@ -418,13 +429,15 @@ static int np_cmd_nand_write_data(np_prog_t *prog)
418429
NP_PACKET_BUF_SIZE)
419430
{
420431
ERROR_PRINT("Data size is wrong %d\r\n", write_data_cmd->len);
421-
return np_send_error(NP_ERR_CMD_DATA_SIZE);
432+
np_send_error(NP_ERR_CMD_DATA_SIZE);
433+
return -1;
422434
}
423435

424436
if (!prog->addr_is_valid)
425437
{
426438
ERROR_PRINT("Write address is not set\r\n");
427-
return np_send_error(NP_ERR_ADDR_INVALID);
439+
np_send_error(NP_ERR_ADDR_INVALID);
440+
return -1;
428441
}
429442

430443
if (prog->page.offset + write_data_cmd->len > prog->chip_info->page_size)
@@ -438,7 +451,10 @@ static int np_cmd_nand_write_data(np_prog_t *prog)
438451
if (prog->page.offset == prog->chip_info->page_size)
439452
{
440453
if (np_nand_write(prog, prog->chip_info))
441-
return np_send_error(NP_ERR_NAND_WR);
454+
{
455+
np_send_error(NP_ERR_NAND_WR);
456+
return -1;
457+
}
442458

443459
prog->addr += prog->chip_info->page_size;
444460
if (prog->addr >= prog->chip_info->size)
@@ -476,13 +492,17 @@ static int np_cmd_nand_write_end(np_prog_t *prog)
476492
if (!prog->addr_is_valid)
477493
{
478494
ERROR_PRINT("Write address is not set\r\n");
479-
return np_send_error(NP_ERR_ADDR_INVALID);
495+
np_send_error(NP_ERR_ADDR_INVALID);
496+
return -1;
480497
}
481498

482499
prog->addr_is_valid = 0;
483500

484501
if (np_nand_write(prog, prog->chip_info))
485-
return np_send_error(NP_ERR_NAND_WR);
502+
{
503+
np_send_error(NP_ERR_NAND_WR);
504+
return -1;
505+
}
486506

487507
Exit:
488508
return np_send_ok_status();
@@ -557,7 +577,8 @@ static int _np_cmd_nand_read(np_prog_t *prog)
557577
{
558578
ERROR_PRINT("Read address 0x%lx is more then chip size 0x%lx\r\n",
559579
read_cmd->addr, prog->chip_info->size);
560-
return np_send_error(NP_ERR_ADDR_EXCEEDED);
580+
np_send_error(NP_ERR_ADDR_EXCEEDED);
581+
return -1;
561582
}
562583

563584
addr = read_cmd->addr;
@@ -569,7 +590,10 @@ static int _np_cmd_nand_read(np_prog_t *prog)
569590
while (read_cmd->len)
570591
{
571592
if (np_nand_read(addr, &page, prog->chip_info))
572-
return np_send_error(NP_ERR_NAND_RD);
593+
{
594+
np_send_error(NP_ERR_NAND_RD);
595+
return -1;
596+
}
573597

574598
while (page.offset < prog->chip_info->page_size && read_cmd->len)
575599
{
@@ -603,7 +627,8 @@ static int _np_cmd_nand_read(np_prog_t *prog)
603627
{
604628
ERROR_PRINT("Read address 0x%lx is more then chip size 0x%lx",
605629
addr, prog->chip_info->page_size);
606-
return np_send_error(NP_ERR_ADDR_EXCEEDED);
630+
np_send_error(NP_ERR_ADDR_EXCEEDED);
631+
return -1;
607632
}
608633
page.page++;
609634
page.offset = 0;
@@ -640,7 +665,8 @@ static int np_cmd_nand_select(np_prog_t *prog)
640665
prog->chip_info = NULL;
641666

642667
ERROR_PRINT("Chip ID %lu not found\r\n", select_cmd->chip_num);
643-
return np_send_error(NP_ERR_CHIP_NOT_FOUND);
668+
np_send_error(NP_ERR_CHIP_NOT_FOUND);
669+
return -1;
644670
}
645671

646672
return np_send_ok_status();
@@ -660,13 +686,16 @@ static int np_read_bad_block_info_from_page(np_prog_t *prog, uint32_t block,
660686
break;
661687
case NAND_ERROR:
662688
ERROR_PRINT("NAND read bad block info error at 0x%lx\r\n", addr);
663-
return np_send_error(NP_ERR_NAND_RD);
689+
np_send_error(NP_ERR_NAND_RD);
690+
return -1;
664691
case NAND_TIMEOUT_ERROR:
665692
ERROR_PRINT("NAND read timeout at 0x%lx\r\n", addr);
666-
return np_send_error(NP_ERR_NAND_RD);
693+
np_send_error(NP_ERR_NAND_RD);
694+
return -1;
667695
default:
668696
ERROR_PRINT("Unknown NAND status\r\n");
669-
return np_send_error(NP_ERR_NAND_RD);
697+
np_send_error(NP_ERR_NAND_RD);
698+
return -1;
670699
}
671700

672701
if (bad_block_data != NP_NAND_GOOD_BLOCK_MARK)
@@ -746,13 +775,15 @@ static int np_cmd_handler(np_prog_t *prog)
746775
if (!prog->chip_info && cmd->code != NP_CMD_NAND_SELECT)
747776
{
748777
ERROR_PRINT("Chip is not selected\r\n");
749-
return np_send_error(NP_ERR_CHIP_NOT_SEL);
778+
np_send_error(NP_ERR_CHIP_NOT_SEL);
779+
return -1;
750780
}
751781

752782
if (!np_cmd_is_valid(cmd->code))
753783
{
754784
ERROR_PRINT("Invalid cmd code %d\r\n", cmd->code);
755-
return np_send_error(NP_ERR_CMD_INVALID);
785+
np_send_error(NP_ERR_CMD_INVALID);
786+
return -1;
756787
}
757788

758789
if (cmd_handler[cmd->code].exec(prog))

0 commit comments

Comments
 (0)