You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fileio.c 6.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /***************************************************************************
  2. * Copyright (C) 2007 by Dominic Rath <Dominic.Rath@gmx.de> *
  3. * Copyright (C) 2002 Thomas Gleixner <tglx@linutronix.de> *
  4. * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
  5. * *
  6. * Partially based on drivers/mtd/nand_ids.c from Linux. *
  7. * *
  8. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program; if not, write to the *
  20. * Free Software Foundation, Inc., *
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  22. ***************************************************************************/
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "core.h"
  27. #include "fileio.h"
  28. static struct nand_ecclayout nand_oob_16 = {
  29. .eccbytes = 6,
  30. .eccpos = {0, 1, 2, 3, 6, 7},
  31. .oobfree = {
  32. {.offset = 8,
  33. .length = 8}
  34. }
  35. };
  36. static struct nand_ecclayout nand_oob_64 = {
  37. .eccbytes = 24,
  38. .eccpos = {
  39. 40, 41, 42, 43, 44, 45, 46, 47,
  40. 48, 49, 50, 51, 52, 53, 54, 55,
  41. 56, 57, 58, 59, 60, 61, 62, 63
  42. },
  43. .oobfree = {
  44. {.offset = 2,
  45. .length = 38}
  46. }
  47. };
  48. void nand_fileio_init(struct nand_fileio_state *state)
  49. {
  50. memset(state, 0, sizeof(*state));
  51. state->oob_format = NAND_OOB_NONE;
  52. }
  53. int nand_fileio_start(struct command_context *cmd_ctx,
  54. struct nand_device *nand, const char *filename, int filemode,
  55. struct nand_fileio_state *state)
  56. {
  57. if (state->address % nand->page_size) {
  58. command_print(cmd_ctx, "only page-aligned addresses are supported");
  59. return ERROR_COMMAND_SYNTAX_ERROR;
  60. }
  61. duration_start(&state->bench);
  62. if (NULL != filename) {
  63. int retval = fileio_open(&state->fileio, filename, filemode, FILEIO_BINARY);
  64. if (ERROR_OK != retval) {
  65. const char *msg = (FILEIO_READ == filemode) ? "read" : "write";
  66. command_print(cmd_ctx, "failed to open '%s' for %s access",
  67. filename, msg);
  68. return retval;
  69. }
  70. state->file_opened = true;
  71. }
  72. if (!(state->oob_format & NAND_OOB_ONLY)) {
  73. state->page_size = nand->page_size;
  74. state->page = malloc(nand->page_size);
  75. }
  76. if (state->oob_format & (NAND_OOB_RAW | NAND_OOB_SW_ECC | NAND_OOB_SW_ECC_KW)) {
  77. if (nand->page_size == 512) {
  78. state->oob_size = 16;
  79. state->eccpos = nand_oob_16.eccpos;
  80. } else if (nand->page_size == 2048) {
  81. state->oob_size = 64;
  82. state->eccpos = nand_oob_64.eccpos;
  83. }
  84. state->oob = malloc(state->oob_size);
  85. }
  86. return ERROR_OK;
  87. }
  88. int nand_fileio_cleanup(struct nand_fileio_state *state)
  89. {
  90. if (state->file_opened)
  91. fileio_close(&state->fileio);
  92. if (state->oob) {
  93. free(state->oob);
  94. state->oob = NULL;
  95. }
  96. if (state->page) {
  97. free(state->page);
  98. state->page = NULL;
  99. }
  100. return ERROR_OK;
  101. }
  102. int nand_fileio_finish(struct nand_fileio_state *state)
  103. {
  104. nand_fileio_cleanup(state);
  105. return duration_measure(&state->bench);
  106. }
  107. COMMAND_HELPER(nand_fileio_parse_args, struct nand_fileio_state *state,
  108. struct nand_device **dev, enum fileio_access filemode,
  109. bool need_size, bool sw_ecc)
  110. {
  111. nand_fileio_init(state);
  112. unsigned minargs = need_size ? 4 : 3;
  113. if (CMD_ARGC < minargs)
  114. return ERROR_COMMAND_SYNTAX_ERROR;
  115. struct nand_device *nand;
  116. int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &nand);
  117. if (ERROR_OK != retval)
  118. return retval;
  119. if (NULL == nand->device) {
  120. command_print(CMD_CTX, "#%s: not probed", CMD_ARGV[0]);
  121. return ERROR_OK;
  122. }
  123. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], state->address);
  124. if (need_size) {
  125. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], state->size);
  126. if (state->size % nand->page_size) {
  127. command_print(CMD_CTX, "only page-aligned sizes are supported");
  128. return ERROR_COMMAND_SYNTAX_ERROR;
  129. }
  130. }
  131. if (CMD_ARGC > minargs) {
  132. for (unsigned i = minargs; i < CMD_ARGC; i++) {
  133. if (!strcmp(CMD_ARGV[i], "oob_raw"))
  134. state->oob_format |= NAND_OOB_RAW;
  135. else if (!strcmp(CMD_ARGV[i], "oob_only"))
  136. state->oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
  137. else if (sw_ecc && !strcmp(CMD_ARGV[i], "oob_softecc"))
  138. state->oob_format |= NAND_OOB_SW_ECC;
  139. else if (sw_ecc && !strcmp(CMD_ARGV[i], "oob_softecc_kw"))
  140. state->oob_format |= NAND_OOB_SW_ECC_KW;
  141. else {
  142. command_print(CMD_CTX, "unknown option: %s", CMD_ARGV[i]);
  143. return ERROR_COMMAND_SYNTAX_ERROR;
  144. }
  145. }
  146. }
  147. retval = nand_fileio_start(CMD_CTX, nand, CMD_ARGV[1], filemode, state);
  148. if (ERROR_OK != retval)
  149. return retval;
  150. if (!need_size) {
  151. int filesize;
  152. retval = fileio_size(&state->fileio, &filesize);
  153. if (retval != ERROR_OK)
  154. return retval;
  155. state->size = filesize;
  156. }
  157. *dev = nand;
  158. return ERROR_OK;
  159. }
  160. /**
  161. * @returns If no error occurred, returns number of bytes consumed;
  162. * otherwise, returns a negative error code.)
  163. */
  164. int nand_fileio_read(struct nand_device *nand, struct nand_fileio_state *s)
  165. {
  166. size_t total_read = 0;
  167. size_t one_read;
  168. if (NULL != s->page) {
  169. fileio_read(&s->fileio, s->page_size, s->page, &one_read);
  170. if (one_read < s->page_size)
  171. memset(s->page + one_read, 0xff, s->page_size - one_read);
  172. total_read += one_read;
  173. }
  174. if (s->oob_format & NAND_OOB_SW_ECC) {
  175. uint8_t ecc[3];
  176. memset(s->oob, 0xff, s->oob_size);
  177. for (uint32_t i = 0, j = 0; i < s->page_size; i += 256) {
  178. nand_calculate_ecc(nand, s->page + i, ecc);
  179. s->oob[s->eccpos[j++]] = ecc[0];
  180. s->oob[s->eccpos[j++]] = ecc[1];
  181. s->oob[s->eccpos[j++]] = ecc[2];
  182. }
  183. } else if (s->oob_format & NAND_OOB_SW_ECC_KW) {
  184. /*
  185. * In this case eccpos is not used as
  186. * the ECC data is always stored contigously
  187. * at the end of the OOB area. It consists
  188. * of 10 bytes per 512-byte data block.
  189. */
  190. uint8_t *ecc = s->oob + s->oob_size - s->page_size / 512 * 10;
  191. memset(s->oob, 0xff, s->oob_size);
  192. for (uint32_t i = 0; i < s->page_size; i += 512) {
  193. nand_calculate_ecc_kw(nand, s->page + i, ecc);
  194. ecc += 10;
  195. }
  196. } else if (NULL != s->oob) {
  197. fileio_read(&s->fileio, s->oob_size, s->oob, &one_read);
  198. if (one_read < s->oob_size)
  199. memset(s->oob + one_read, 0xff, s->oob_size - one_read);
  200. total_read += one_read;
  201. }
  202. return total_read;
  203. }