@@ -337,25 +337,42 @@ static int np_send_write_ack(uint32_t bytes_ack)
337
337
338
338
static int np_cmd_nand_write_start (np_prog_t * prog )
339
339
{
340
+ uint32_t addr , len ;
341
+
340
342
np_write_start_cmd_t * write_start_cmd =
341
343
(np_write_start_cmd_t * )prog -> rx_buf ;
342
344
343
- DEBUG_PRINT ("Write at 0x%lx command\r\n" , write_start_cmd -> addr );
345
+ addr = write_start_cmd -> addr ;
346
+ len = write_start_cmd -> len ;
347
+ DEBUG_PRINT ("Write at 0x%lx 0x%lx bytes command\r\n" , addr , len );
344
348
345
- if (write_start_cmd -> addr >= prog -> chip_info -> size )
349
+ if (addr + len > prog -> chip_info -> size )
346
350
{
347
- ERROR_PRINT ("Write address 0x%lx is more then chip size 0x%lx\r\n" ,
348
- write_start_cmd -> addr , prog -> chip_info -> size );
351
+ ERROR_PRINT ("Write address 0x%lx+0x%lx is more then chip size "
352
+ "0x%lx\r\n" , addr , len , prog -> chip_info -> size );
349
353
return NP_ERR_ADDR_EXCEEDED ;
350
354
}
351
355
352
- prog -> addr = write_start_cmd -> addr ;
353
- prog -> len = write_start_cmd -> len ;
356
+ if (addr & (prog -> chip_info -> page_size - 1 ))
357
+ {
358
+ ERROR_PRINT ("Address 0x%lx is not aligned to page size 0x%lx\r\n" ,
359
+ addr , prog -> chip_info -> page_size );
360
+ return NP_ERR_ADDR_NOT_ALIGN ;
361
+ }
362
+
363
+ if (len & (prog -> chip_info -> page_size - 1 ))
364
+ {
365
+ ERROR_PRINT ("Length 0x%lx is not aligned to page size 0x%lx\r\n" ,
366
+ len , prog -> chip_info -> page_size );
367
+ return NP_ERR_ADDR_NOT_ALIGN ;
368
+ }
369
+
370
+ prog -> addr = addr ;
371
+ prog -> len = len ;
354
372
prog -> addr_is_valid = 1 ;
355
373
356
- prog -> page .page = write_start_cmd -> addr / prog -> chip_info -> page_size ;
357
- prog -> page .offset = write_start_cmd -> addr % prog -> chip_info -> page_size ;
358
- memset (prog -> page .buf , 0 , sizeof (prog -> page .buf ));
374
+ prog -> page .page = addr / prog -> chip_info -> page_size ;
375
+ prog -> page .offset = 0 ;
359
376
360
377
prog -> bytes_written = 0 ;
361
378
prog -> bytes_ack = 0 ;
@@ -419,14 +436,13 @@ static int np_nand_write(np_prog_t *prog, chip_info_t *chip_info)
419
436
420
437
static int np_cmd_nand_write_data (np_prog_t * prog )
421
438
{
422
- uint32_t write_len , bytes_left ;
423
- np_write_data_cmd_t * write_data_cmd =
424
- (np_write_data_cmd_t * )prog -> rx_buf ;
439
+ uint32_t write_len , bytes_left , len ;
440
+ np_write_data_cmd_t * write_data_cmd = (np_write_data_cmd_t * )prog -> rx_buf ;
425
441
426
- if ( write_data_cmd -> len + offsetof( np_write_data_cmd_t , data ) >
427
- NP_PACKET_BUF_SIZE )
442
+ len = write_data_cmd -> len ;
443
+ if ( len + sizeof ( np_write_data_cmd_t ) > NP_PACKET_BUF_SIZE )
428
444
{
429
- ERROR_PRINT ("Data size is wrong %d \r\n" , write_data_cmd -> len );
445
+ ERROR_PRINT ("Data size is wrong 0x%lx \r\n" , len );
430
446
return NP_ERR_CMD_DATA_SIZE ;
431
447
}
432
448
@@ -436,10 +452,10 @@ static int np_cmd_nand_write_data(np_prog_t *prog)
436
452
return NP_ERR_ADDR_INVALID ;
437
453
}
438
454
439
- if (prog -> page .offset + write_data_cmd -> len > prog -> chip_info -> page_size )
455
+ if (prog -> page .offset + len > prog -> chip_info -> page_size )
440
456
write_len = prog -> chip_info -> page_size - prog -> page .offset ;
441
457
else
442
- write_len = write_data_cmd -> len ;
458
+ write_len = len ;
443
459
444
460
memcpy (prog -> page .buf + prog -> page .offset , write_data_cmd -> data , write_len );
445
461
prog -> page .offset += write_len ;
@@ -455,17 +471,16 @@ static int np_cmd_nand_write_data(np_prog_t *prog)
455
471
456
472
prog -> page .page ++ ;
457
473
prog -> page .offset = 0 ;
458
- memset (prog -> page .buf , 0 , prog -> chip_info -> page_size );
459
474
}
460
475
461
- bytes_left = write_data_cmd -> len - write_len ;
476
+ bytes_left = len - write_len ;
462
477
if (bytes_left )
463
478
{
464
479
memcpy (prog -> page .buf , write_data_cmd -> data + write_len , bytes_left );
465
480
prog -> page .offset += bytes_left ;
466
481
}
467
482
468
- prog -> bytes_written += write_data_cmd -> len ;
483
+ prog -> bytes_written += len ;
469
484
if (prog -> bytes_written - prog -> bytes_ack >= prog -> chip_info -> page_size ||
470
485
prog -> bytes_written == prog -> len )
471
486
{
@@ -479,22 +494,15 @@ static int np_cmd_nand_write_data(np_prog_t *prog)
479
494
480
495
static int np_cmd_nand_write_end (np_prog_t * prog )
481
496
{
482
- if (!prog -> page .offset )
483
- goto Exit ;
484
-
485
- if (!prog -> addr_is_valid )
486
- {
487
- ERROR_PRINT ("Write address is not set\r\n" );
488
- return NP_ERR_ADDR_INVALID ;
489
- }
497
+ if (prog -> page .offset )
498
+ goto Error ;
490
499
491
500
prog -> addr_is_valid = 0 ;
492
501
493
- if (np_nand_write (prog , prog -> chip_info ))
494
- return NP_ERR_NAND_WR ;
495
-
496
- Exit :
497
502
return np_send_ok_status ();
503
+
504
+ Error :
505
+ return NP_ERR_NAND_WR ;
498
506
}
499
507
500
508
static int np_cmd_nand_write (np_prog_t * prog )
0 commit comments