Skip to content

Commit e262e60

Browse files
committed
Erase command does not handle unaligned address/length
1 parent fe33fae commit e262e60

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

firmware/nand_programmer.c

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ enum
3737
{
3838
NP_ERR_ADDR_EXCEEDED = 0x00,
3939
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,
4850
};
4951

5052
typedef struct __attribute__((__packed__))
@@ -228,9 +230,10 @@ static int np_cmd_nand_read_id(np_prog_t *prog)
228230
return 0;
229231
}
230232

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)
232234
{
233235
uint32_t status;
236+
uint32_t addr = page * prog->chip_info->page_size;
234237

235238
DEBUG_PRINT("NAND erase at 0x%lx\r\n", addr);
236239

@@ -256,37 +259,40 @@ static int np_nand_erase(uint32_t page, uint32_t addr)
256259

257260
static int np_cmd_nand_erase(np_prog_t *prog)
258261
{
259-
uint32_t addr, page, pages_in_block;
262+
uint32_t addr, page, pages_in_block, len;
260263
np_erase_cmd_t *erase_cmd = (np_erase_cmd_t *)prog->rx_buf;
261264

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+
262270
led_wr_set(true);
263271

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

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+
}
269284

270285
page = addr / prog->chip_info->page_size;
271286
pages_in_block = prog->chip_info->block_size / prog->chip_info->page_size;
272287

273-
while (erase_cmd->len)
288+
while (len)
274289
{
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))
283291
return np_send_error(NP_ERR_NAND_ERASE);
284292

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+
290296
page += pages_in_block;
291297
}
292298

0 commit comments

Comments
 (0)