X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=blobdiff_plain;f=src%2Fflash%2Fnand.c;h=da561f63f2f7e21e678ec027435932a8725bc37d;hp=4094effcb15b3beb557a520b6f81241f9c697190;hb=51cd370b396d19555158c1eb913e7c8386d92a0f;hpb=98723c4ecdbe06f90c66f3abec27b792c3b38e34 diff --git a/src/flash/nand.c b/src/flash/nand.c index 4094effcb1..da561f63f2 100644 --- a/src/flash/nand.c +++ b/src/flash/nand.c @@ -62,7 +62,7 @@ static struct nand_flash_controller *nand_flash_controllers[] = /* configured NAND devices and NAND Flash command handler */ static struct nand_device *nand_devices = NULL; -static command_t *nand_cmd; +static struct command *nand_cmd; /* Chip ID list * @@ -1285,286 +1285,300 @@ COMMAND_HANDLER(handle_nand_check_bad_blocks_command) return ERROR_OK; } -COMMAND_HANDLER(handle_nand_write_command) -{ - uint32_t offset; - uint32_t binary_size; - uint32_t buf_cnt; - enum oob_formats oob_format = NAND_OOB_NONE; - - struct fileio fileio; - - - if (argc < 3) - { - return ERROR_COMMAND_SYNTAX_ERROR; - } +struct nand_fileio_state { + uint32_t address; + uint32_t size; - struct nand_device *p; - int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p); - if (ERROR_OK != retval) - return retval; + uint8_t *page; + uint32_t page_size; - uint8_t *page = NULL; - uint32_t page_size = 0; - uint8_t *oob = NULL; - uint32_t oob_size = 0; - const int *eccpos = NULL; + enum oob_formats oob_format; + uint8_t *oob; + uint32_t oob_size; - COMMAND_PARSE_NUMBER(u32, args[2], offset); + const int *eccpos; - if (argc > 3) - { - for (unsigned i = 3; i < argc; i++) - { - if (!strcmp(args[i], "oob_raw")) - oob_format |= NAND_OOB_RAW; - else if (!strcmp(args[i], "oob_only")) - oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY; - else if (!strcmp(args[i], "oob_softecc")) - oob_format |= NAND_OOB_SW_ECC; - else if (!strcmp(args[i], "oob_softecc_kw")) - oob_format |= NAND_OOB_SW_ECC_KW; - else - { - command_print(cmd_ctx, "unknown option: %s", args[i]); - return ERROR_COMMAND_SYNTAX_ERROR; - } - } - } + bool file_opened; + struct fileio fileio; struct duration bench; - duration_start(&bench); - - if (fileio_open(&fileio, args[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK) - { - return ERROR_OK; - } +}; - buf_cnt = binary_size = fileio.size; +static void nand_fileio_init(struct nand_fileio_state *state) +{ + memset(state, 0, sizeof(*state)); + state->oob_format = NAND_OOB_NONE; +} - if (!(oob_format & NAND_OOB_ONLY)) +static int nand_fileio_start(struct command_context *cmd_ctx, + struct nand_device *nand, const char *filename, int filemode, + struct nand_fileio_state *state) +{ + if (state->address % nand->page_size) { - page_size = p->page_size; - page = malloc(p->page_size); + command_print(cmd_ctx, "only page-aligned addresses are supported"); + return ERROR_COMMAND_SYNTAX_ERROR; } - if (oob_format & (NAND_OOB_RAW | NAND_OOB_SW_ECC | NAND_OOB_SW_ECC_KW)) + duration_start(&state->bench); + + if (NULL != filename) { - if (p->page_size == 512) { - oob_size = 16; - eccpos = nand_oob_16.eccpos; - } else if (p->page_size == 2048) { - oob_size = 64; - eccpos = nand_oob_64.eccpos; + int retval = fileio_open(&state->fileio, filename, filemode, FILEIO_BINARY); + if (ERROR_OK != retval) + { + const char *msg = (FILEIO_READ == filemode) ? "read" : "write"; + command_print(cmd_ctx, "failed to open '%s' for %s access", + filename, msg); + return retval; } - oob = malloc(oob_size); + state->file_opened = true; } - if (offset % p->page_size) + if (!(state->oob_format & NAND_OOB_ONLY)) { - command_print(cmd_ctx, "only page size aligned offsets and sizes are supported"); - fileio_close(&fileio); - free(oob); - free(page); - return ERROR_OK; + state->page_size = nand->page_size; + state->page = malloc(nand->page_size); } - while (buf_cnt > 0) + if (state->oob_format & (NAND_OOB_RAW | NAND_OOB_SW_ECC | NAND_OOB_SW_ECC_KW)) { - uint32_t size_read; - - if (NULL != page) + if (nand->page_size == 512) { - fileio_read(&fileio, page_size, page, &size_read); - buf_cnt -= size_read; - if (size_read < page_size) - { - memset(page + size_read, 0xff, page_size - size_read); - } + state->oob_size = 16; + state->eccpos = nand_oob_16.eccpos; } - - if (oob_format & NAND_OOB_SW_ECC) - { - uint32_t i, j; - uint8_t ecc[3]; - memset(oob, 0xff, oob_size); - for (i = 0, j = 0; i < page_size; i += 256) { - nand_calculate_ecc(p, page + i, ecc); - oob[eccpos[j++]] = ecc[0]; - oob[eccpos[j++]] = ecc[1]; - oob[eccpos[j++]] = ecc[2]; - } - } else if (oob_format & NAND_OOB_SW_ECC_KW) - { - /* - * In this case eccpos is not used as - * the ECC data is always stored contigously - * at the end of the OOB area. It consists - * of 10 bytes per 512-byte data block. - */ - uint32_t i; - uint8_t *ecc = oob + oob_size - page_size/512 * 10; - memset(oob, 0xff, oob_size); - for (i = 0; i < page_size; i += 512) { - nand_calculate_ecc_kw(p, page + i, ecc); - ecc += 10; - } - } - else if (NULL != oob) + else if (nand->page_size == 2048) { - fileio_read(&fileio, oob_size, oob, &size_read); - buf_cnt -= size_read; - if (size_read < oob_size) - { - memset(oob + size_read, 0xff, oob_size - size_read); - } + state->oob_size = 64; + state->eccpos = nand_oob_64.eccpos; } + state->oob = malloc(state->oob_size); + } - if (nand_write_page(p, offset / p->page_size, page, page_size, oob, oob_size) != ERROR_OK) - { - command_print(cmd_ctx, "failed writing file %s to NAND flash %s at offset 0x%8.8" PRIx32 "", - args[1], args[0], offset); - - fileio_close(&fileio); - free(oob); - free(page); + return ERROR_OK; +} +static int nand_fileio_cleanup(struct nand_fileio_state *state) +{ + if (state->file_opened) + fileio_close(&state->fileio); - return ERROR_OK; - } - offset += page_size; + if (state->oob) + { + free(state->oob); + state->oob = NULL; } - - fileio_close(&fileio); - free(oob); - free(page); - oob = NULL; - page = NULL; - if (duration_measure(&bench) == ERROR_OK) + if (state->page) { - command_print(cmd_ctx, "wrote file %s to NAND flash %s " - "up to offset 0x%8.8" PRIx32 " in %fs (%0.3f kb/s)", - args[1], args[0], offset, duration_elapsed(&bench), - duration_kbps(&bench, fileio.size)); + free(state->page); + state->page = NULL; } - return ERROR_OK; } +static int nand_fileio_finish(struct nand_fileio_state *state) +{ + nand_fileio_cleanup(state); + return duration_measure(&state->bench); +} -COMMAND_HANDLER(handle_nand_dump_command) +static COMMAND_HELPER(nand_fileio_parse_args, struct nand_fileio_state *state, + struct nand_device **dev, enum fileio_access filemode, + bool need_size, bool sw_ecc) { - if (argc < 4) - { + nand_fileio_init(state); + + unsigned minargs = need_size ? 4 : 3; + if (argc < minargs) return ERROR_COMMAND_SYNTAX_ERROR; - } - struct nand_device *p; - int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p); + struct nand_device *nand; + int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &nand); if (ERROR_OK != retval) return retval; - if (NULL == p->device) + if (NULL == nand->device) { command_print(cmd_ctx, "#%s: not probed", args[0]); return ERROR_OK; } - struct fileio fileio; - - uint8_t *page = NULL; - uint32_t page_size = 0; - uint8_t *oob = NULL; - uint32_t oob_size = 0; - uint32_t address; - COMMAND_PARSE_NUMBER(u32, args[2], address); - uint32_t size; - COMMAND_PARSE_NUMBER(u32, args[3], size); - uint32_t bytes_done = 0; - enum oob_formats oob_format = NAND_OOB_NONE; + COMMAND_PARSE_NUMBER(u32, args[2], state->address); + if (need_size) + { + COMMAND_PARSE_NUMBER(u32, args[2], state->size); + if (state->size % nand->page_size) + { + command_print(cmd_ctx, "only page-aligned sizes are supported"); + return ERROR_COMMAND_SYNTAX_ERROR; + } + } - if (argc > 4) + if (argc > minargs) { - for (unsigned i = 4; i < argc; i++) + for (unsigned i = minargs; i < argc; i++) { if (!strcmp(args[i], "oob_raw")) - oob_format |= NAND_OOB_RAW; + state->oob_format |= NAND_OOB_RAW; else if (!strcmp(args[i], "oob_only")) - oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY; + state->oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY; + else if (sw_ecc && !strcmp(args[i], "oob_softecc")) + state->oob_format |= NAND_OOB_SW_ECC; + else if (sw_ecc && !strcmp(args[i], "oob_softecc_kw")) + state->oob_format |= NAND_OOB_SW_ECC_KW; else - command_print(cmd_ctx, "unknown option: '%s'", args[i]); + { + command_print(cmd_ctx, "unknown option: %s", args[i]); + return ERROR_COMMAND_SYNTAX_ERROR; + } } } - if ((address % p->page_size) || (size % p->page_size)) + retval = nand_fileio_start(cmd_ctx, nand, args[1], filemode, state); + if (ERROR_OK != retval) + return retval; + + if (!need_size) + state->size = state->fileio.size; + + *dev = nand; + + return ERROR_OK; +} + +/** + * @returns If no error occurred, returns number of bytes consumed; + * otherwise, returns a negative error code.) + */ +static int nand_fileio_read(struct nand_device *nand, + struct nand_fileio_state *s) +{ + uint32_t total_read = 0; + uint32_t one_read; + + if (NULL != s->page) { - command_print(cmd_ctx, "only page size aligned addresses and sizes are supported"); - return ERROR_OK; + fileio_read(&s->fileio, s->page_size, s->page, &one_read); + if (one_read < s->page_size) + memset(s->page + one_read, 0xff, s->page_size - one_read); + total_read += one_read; } - if (!(oob_format & NAND_OOB_ONLY)) + if (s->oob_format & NAND_OOB_SW_ECC) { - page_size = p->page_size; - page = malloc(p->page_size); + uint8_t ecc[3]; + memset(s->oob, 0xff, s->oob_size); + for (uint32_t i = 0, j = 0; i < s->page_size; i += 256) + { + nand_calculate_ecc(nand, s->page + i, ecc); + s->oob[s->eccpos[j++]] = ecc[0]; + s->oob[s->eccpos[j++]] = ecc[1]; + s->oob[s->eccpos[j++]] = ecc[2]; + } } - - if (oob_format & NAND_OOB_RAW) + else if (s->oob_format & NAND_OOB_SW_ECC_KW) { - if (p->page_size == 512) - oob_size = 16; - else if (p->page_size == 2048) - oob_size = 64; - oob = malloc(oob_size); + /* + * In this case eccpos is not used as + * the ECC data is always stored contigously + * at the end of the OOB area. It consists + * of 10 bytes per 512-byte data block. + */ + uint8_t *ecc = s->oob + s->oob_size - s->page_size / 512 * 10; + memset(s->oob, 0xff, s->oob_size); + for (uint32_t i = 0; i < s->page_size; i += 512) + { + nand_calculate_ecc_kw(nand, s->page + i, ecc); + ecc += 10; + } } - - if (fileio_open(&fileio, args[1], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK) + else if (NULL != s->oob) { - return ERROR_OK; + fileio_read(&s->fileio, s->oob_size, s->oob, &one_read); + if (one_read < s->oob_size) + memset(s->oob + one_read, 0xff, s->oob_size - one_read); + total_read += one_read; } + return total_read; +} - struct duration bench; - duration_start(&bench); +COMMAND_HANDLER(handle_nand_write_command) +{ + struct nand_device *nand = NULL; + struct nand_fileio_state s; + int retval = CALL_COMMAND_HANDLER(nand_fileio_parse_args, + &s, &nand, FILEIO_READ, false, true); + if (ERROR_OK != retval) + return retval; - while (size > 0) + uint32_t total_bytes = s.size; + while (s.size > 0) { - uint32_t size_written; - if ((retval = nand_read_page(p, address / p->page_size, page, page_size, oob, oob_size)) != ERROR_OK) + int bytes_read = nand_fileio_read(nand, &s); + if (bytes_read <= 0) { - command_print(cmd_ctx, "reading NAND flash page failed"); - free(page); - free(oob); - fileio_close(&fileio); - return ERROR_OK; + command_print(cmd_ctx, "error while reading file"); + return nand_fileio_cleanup(&s); } + s.size -= bytes_read; - if (NULL != page) + retval = nand_write_page(nand, s.address / nand->page_size, + s.page, s.page_size, s.oob, s.oob_size); + if (ERROR_OK != retval) { - fileio_write(&fileio, page_size, page, &size_written); - bytes_done += page_size; + command_print(cmd_ctx, "failed writing file %s " + "to NAND flash %s at offset 0x%8.8" PRIx32, + args[1], args[0], s.address); + return nand_fileio_cleanup(&s); } + s.address += s.page_size; + } - if (NULL != oob) + if (nand_fileio_finish(&s)) + { + command_print(cmd_ctx, "wrote file %s to NAND flash %s up to " + "offset 0x%8.8" PRIx32 " in %fs (%0.3f kb/s)", + args[1], args[0], s.address, duration_elapsed(&s.bench), + duration_kbps(&s.bench, total_bytes)); + } + return ERROR_OK; +} + +COMMAND_HANDLER(handle_nand_dump_command) +{ + struct nand_device *nand = NULL; + struct nand_fileio_state s; + int retval = CALL_COMMAND_HANDLER(nand_fileio_parse_args, + &s, &nand, FILEIO_WRITE, true, false); + if (ERROR_OK != retval) + return retval; + + while (s.size > 0) + { + uint32_t size_written; + int retval = nand_read_page(nand, s.address / nand->page_size, + s.page, s.page_size, s.oob, s.oob_size); + if (ERROR_OK != retval) { - fileio_write(&fileio, oob_size, oob, &size_written); - bytes_done += oob_size; + command_print(cmd_ctx, "reading NAND flash page failed"); + return nand_fileio_cleanup(&s); } - size -= p->page_size; - address += p->page_size; - } + if (NULL != s.page) + fileio_write(&s.fileio, s.page_size, s.page, &size_written); - free(page); - page = NULL; - free(oob); - oob = NULL; - fileio_close(&fileio); + if (NULL != s.oob) + fileio_write(&s.fileio, s.oob_size, s.oob, &size_written); - if (duration_measure(&bench) == ERROR_OK) - { - command_print(cmd_ctx, "dumped %lld byte in %fs (%0.3f kb/s)", - fileio.size, duration_elapsed(&bench), - duration_kbps(&bench, fileio.size)); + s.size -= nand->page_size; + s.address += nand->page_size; } + if (nand_fileio_finish(&s) == ERROR_OK) + { + command_print(cmd_ctx, "dumped %lld byte in %fs (%0.3f kb/s)", + s.fileio.size, duration_elapsed(&s.bench), + duration_kbps(&s.bench, s.fileio.size)); + } return ERROR_OK; }