Skip to content

Commit dc62946

Browse files
committed
Return error when write address/length is unaligned
1 parent 930af75 commit dc62946

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

firmware/nand_programmer.c

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -337,25 +337,42 @@ static int np_send_write_ack(uint32_t bytes_ack)
337337

338338
static int np_cmd_nand_write_start(np_prog_t *prog)
339339
{
340+
uint32_t addr, len;
341+
340342
np_write_start_cmd_t *write_start_cmd =
341343
(np_write_start_cmd_t *)prog->rx_buf;
342344

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);
344348

345-
if (write_start_cmd->addr >= prog->chip_info->size)
349+
if (addr + len > prog->chip_info->size)
346350
{
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);
349353
return NP_ERR_ADDR_EXCEEDED;
350354
}
351355

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;
354372
prog->addr_is_valid = 1;
355373

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;
359376

360377
prog->bytes_written = 0;
361378
prog->bytes_ack = 0;
@@ -419,14 +436,13 @@ static int np_nand_write(np_prog_t *prog, chip_info_t *chip_info)
419436

420437
static int np_cmd_nand_write_data(np_prog_t *prog)
421438
{
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;
425441

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)
428444
{
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);
430446
return NP_ERR_CMD_DATA_SIZE;
431447
}
432448

@@ -436,10 +452,10 @@ static int np_cmd_nand_write_data(np_prog_t *prog)
436452
return NP_ERR_ADDR_INVALID;
437453
}
438454

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)
440456
write_len = prog->chip_info->page_size - prog->page.offset;
441457
else
442-
write_len = write_data_cmd->len;
458+
write_len = len;
443459

444460
memcpy(prog->page.buf + prog->page.offset, write_data_cmd->data, write_len);
445461
prog->page.offset += write_len;
@@ -455,17 +471,16 @@ static int np_cmd_nand_write_data(np_prog_t *prog)
455471

456472
prog->page.page++;
457473
prog->page.offset = 0;
458-
memset(prog->page.buf, 0, prog->chip_info->page_size);
459474
}
460475

461-
bytes_left = write_data_cmd->len - write_len;
476+
bytes_left = len - write_len;
462477
if (bytes_left)
463478
{
464479
memcpy(prog->page.buf, write_data_cmd->data + write_len, bytes_left);
465480
prog->page.offset += bytes_left;
466481
}
467482

468-
prog->bytes_written += write_data_cmd->len;
483+
prog->bytes_written += len;
469484
if (prog->bytes_written - prog->bytes_ack >= prog->chip_info->page_size ||
470485
prog->bytes_written == prog->len)
471486
{
@@ -479,22 +494,15 @@ static int np_cmd_nand_write_data(np_prog_t *prog)
479494

480495
static int np_cmd_nand_write_end(np_prog_t *prog)
481496
{
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;
490499

491500
prog->addr_is_valid = 0;
492501

493-
if (np_nand_write(prog, prog->chip_info))
494-
return NP_ERR_NAND_WR;
495-
496-
Exit:
497502
return np_send_ok_status();
503+
504+
Error:
505+
return NP_ERR_NAND_WR;
498506
}
499507

500508
static int np_cmd_nand_write(np_prog_t *prog)

0 commit comments

Comments
 (0)