@@ -198,6 +198,7 @@ typedef struct
198
198
typedef struct
199
199
{
200
200
uint8_t * rx_buf ;
201
+ uint32_t rx_buf_len ;
201
202
uint32_t addr ;
202
203
uint32_t len ;
203
204
int addr_is_set ;
@@ -398,13 +399,21 @@ static int _np_cmd_nand_erase(np_prog_t *prog)
398
399
{
399
400
int ret ;
400
401
uint32_t addr , page , pages_in_block , len , total_len ;
401
- np_erase_cmd_t * erase_cmd = ( np_erase_cmd_t * ) prog -> rx_buf ;
402
- bool is_bad = false, skip_bb = erase_cmd -> flags . skip_bb ;
402
+ np_erase_cmd_t * erase_cmd ;
403
+ bool skip_bb , is_bad = false ;
403
404
405
+ DEBUG_PRINT ("Erase at 0x%lx %lx bytes command\r\n" , addr , len );
406
+
407
+ if (prog -> rx_buf_len < sizeof (np_erase_cmd_t ))
408
+ {
409
+ ERROR_PRINT ("Wrong buffer length for erase command %lu\r\n" ,
410
+ prog -> rx_buf_len );
411
+ return NP_ERR_LEN_INVALID ;
412
+ }
413
+ erase_cmd = (np_erase_cmd_t * )prog -> rx_buf ;
404
414
total_len = len = erase_cmd -> len ;
405
415
addr = erase_cmd -> addr ;
406
-
407
- DEBUG_PRINT ("Erase at 0x%lx %lx bytes command\r\n" , addr , len );
416
+ skip_bb = erase_cmd -> flags .skip_bb ;
408
417
409
418
if (skip_bb && !prog -> bb_is_read && (ret = _np_cmd_read_bad_blocks (prog )))
410
419
return ret ;
@@ -496,13 +505,20 @@ static int np_cmd_nand_write_start(np_prog_t *prog)
496
505
{
497
506
int ret ;
498
507
uint32_t addr , len ;
508
+ np_write_start_cmd_t * write_start_cmd ;
499
509
500
- np_write_start_cmd_t * write_start_cmd =
501
- (np_write_start_cmd_t * )prog -> rx_buf ;
510
+ DEBUG_PRINT ("Write at 0x%lx 0x%lx bytes command\r\n" , addr , len );
511
+
512
+ if (prog -> rx_buf_len < sizeof (np_write_start_cmd_t ))
513
+ {
514
+ ERROR_PRINT ("Wrong buffer length for write start command %lu\r\n" ,
515
+ prog -> rx_buf_len );
516
+ return NP_ERR_LEN_INVALID ;
517
+ }
502
518
519
+ write_start_cmd = (np_write_start_cmd_t * )prog -> rx_buf ;
503
520
addr = write_start_cmd -> addr ;
504
521
len = write_start_cmd -> len ;
505
- DEBUG_PRINT ("Write at 0x%lx 0x%lx bytes command\r\n" , addr , len );
506
522
507
523
if (addr + len > prog -> chip_info .size )
508
524
{
@@ -611,15 +627,30 @@ static int np_nand_write(np_prog_t *prog)
611
627
static int np_cmd_nand_write_data (np_prog_t * prog )
612
628
{
613
629
uint32_t write_len , bytes_left , len ;
614
- np_write_data_cmd_t * write_data_cmd = (np_write_data_cmd_t * )prog -> rx_buf ;
630
+ np_write_data_cmd_t * write_data_cmd ;
631
+
632
+ if (prog -> rx_buf_len < sizeof (np_write_data_cmd_t ))
633
+ {
634
+ ERROR_PRINT ("Wrong buffer length for write data command %lu\r\n" ,
635
+ prog -> rx_buf_len );
636
+ return NP_ERR_LEN_INVALID ;
637
+ }
615
638
639
+ write_data_cmd = (np_write_data_cmd_t * )prog -> rx_buf ;
616
640
len = write_data_cmd -> len ;
617
641
if (len + sizeof (np_write_data_cmd_t ) > NP_PACKET_BUF_SIZE )
618
642
{
619
643
ERROR_PRINT ("Data size is wrong 0x%lx\r\n" , len );
620
644
return NP_ERR_CMD_DATA_SIZE ;
621
645
}
622
646
647
+ if (len + sizeof (np_write_data_cmd_t ) != prog -> rx_buf_len )
648
+ {
649
+ ERROR_PRINT ("Buffer len 0x%lx is bigger then command 0x%lx\r\n" ,
650
+ prog -> rx_buf_len , len + sizeof (np_write_data_cmd_t ));
651
+ return NP_ERR_CMD_DATA_SIZE ;
652
+ }
653
+
623
654
if (!prog -> addr_is_set )
624
655
{
625
656
ERROR_PRINT ("Write address is not set\r\n" );
@@ -763,15 +794,25 @@ static int _np_cmd_nand_read(np_prog_t *prog)
763
794
int ret ;
764
795
uint32_t addr , len , send_len ;
765
796
static np_page_t page ;
797
+ np_read_cmd_t * read_cmd ;
798
+ bool skip_bb ;
766
799
uint32_t resp_header_size = offsetof(np_resp_t , data );
767
800
uint32_t tx_data_len = sizeof (np_packet_send_buf ) - resp_header_size ;
768
- np_read_cmd_t * read_cmd = (np_read_cmd_t * )prog -> rx_buf ;
769
- bool skip_bb = read_cmd -> flags .skip_bb ;
770
801
np_resp_t * resp = (np_resp_t * )np_packet_send_buf ;
771
802
803
+ DEBUG_PRINT ("Read at 0x%lx 0x%lx bytes command\r\n" , addr , len );
804
+
805
+ if (prog -> rx_buf_len < sizeof (np_read_cmd_t ))
806
+ {
807
+ ERROR_PRINT ("Wrong buffer length for read command %lu\r\n" ,
808
+ prog -> rx_buf_len );
809
+ return NP_ERR_LEN_INVALID ;
810
+ }
811
+
812
+ read_cmd = (np_read_cmd_t * )prog -> rx_buf ;
772
813
addr = read_cmd -> addr ;
773
814
len = read_cmd -> len ;
774
- DEBUG_PRINT ( "Read at 0x%lx 0x%lx bytes command\r\n" , addr , len ) ;
815
+ skip_bb = read_cmd -> flags . skip_bb ;
775
816
776
817
if (addr + len > prog -> chip_info .size )
777
818
{
@@ -881,10 +922,19 @@ static int np_cmd_nand_read(np_prog_t *prog)
881
922
882
923
static int np_cmd_nand_conf (np_prog_t * prog )
883
924
{
884
- np_conf_cmd_t * conf_cmd = ( np_conf_cmd_t * ) prog -> rx_buf ;
925
+ np_conf_cmd_t * conf_cmd ;
885
926
886
927
DEBUG_PRINT ("Chip configure command\r\n" );
887
928
929
+ if (prog -> rx_buf_len < sizeof (np_conf_cmd_t ))
930
+ {
931
+ ERROR_PRINT ("Wrong buffer length for configuration command %lu\r\n" ,
932
+ prog -> rx_buf_len );
933
+ return NP_ERR_LEN_INVALID ;
934
+ }
935
+
936
+ conf_cmd = (np_conf_cmd_t * )prog -> rx_buf ;
937
+
888
938
prog -> chip_info .page_size = conf_cmd -> page_size ;
889
939
prog -> chip_info .block_size = conf_cmd -> block_size ;
890
940
prog -> chip_info .size = conf_cmd -> size ;
@@ -983,7 +1033,15 @@ static bool np_cmd_is_valid(np_cmd_code_t code)
983
1033
984
1034
static int np_cmd_handler (np_prog_t * prog )
985
1035
{
986
- np_cmd_t * cmd = (np_cmd_t * )prog -> rx_buf ;
1036
+ np_cmd_t * cmd ;
1037
+
1038
+ if (prog -> rx_buf_len < sizeof (np_cmd_t ))
1039
+ {
1040
+ ERROR_PRINT ("Wrong buffer length for command %lu\r\n" ,
1041
+ prog -> rx_buf_len );
1042
+ return NP_ERR_LEN_INVALID ;
1043
+ }
1044
+ cmd = (np_cmd_t * )prog -> rx_buf ;
987
1045
988
1046
if (!prog -> chip_is_conf && cmd -> code != NP_CMD_NAND_CONF &&
989
1047
cmd -> code != NP_CMD_VERSION_GET )
@@ -1007,9 +1065,8 @@ static void np_packet_handler(np_prog_t *prog)
1007
1065
1008
1066
do
1009
1067
{
1010
- np_comm_cb -> peek (& prog -> rx_buf );
1011
-
1012
- if (!prog -> rx_buf )
1068
+ prog -> rx_buf_len = np_comm_cb -> peek (& prog -> rx_buf );
1069
+ if (!prog -> rx_buf_len )
1013
1070
break ;
1014
1071
1015
1072
ret = np_cmd_handler (prog );
0 commit comments