dfu_mtd.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * dfu_mtd.c -- DFU for MTD device.
  4. *
  5. * Copyright (C) 2019,STMicroelectronics - All Rights Reserved
  6. *
  7. * Based on dfu_nand.c
  8. */
  9. #include <common.h>
  10. #include <dfu.h>
  11. #include <mtd.h>
  12. #include <jffs2/load_kernel.h>
  13. #include <linux/err.h>
  14. static bool mtd_is_aligned_with_block_size(struct mtd_info *mtd, u64 size)
  15. {
  16. return !do_div(size, mtd->erasesize);
  17. }
  18. static int mtd_block_op(enum dfu_op op, struct dfu_entity *dfu,
  19. u64 offset, void *buf, long *len)
  20. {
  21. u64 off, lim, remaining;
  22. struct mtd_info *mtd = dfu->data.mtd.info;
  23. struct mtd_oob_ops io_op = {};
  24. int ret = 0;
  25. bool has_pages = mtd->type == MTD_NANDFLASH ||
  26. mtd->type == MTD_MLCNANDFLASH;
  27. /* if buf == NULL return total size of the area */
  28. if (!buf) {
  29. *len = dfu->data.mtd.size;
  30. return 0;
  31. }
  32. off = dfu->data.mtd.start + offset + dfu->bad_skip;
  33. lim = dfu->data.mtd.start + dfu->data.mtd.size;
  34. if (off >= lim) {
  35. printf("Limit reached 0x%llx\n", lim);
  36. *len = 0;
  37. return op == DFU_OP_READ ? 0 : -EIO;
  38. }
  39. /* limit request with the available size */
  40. if (off + *len >= lim)
  41. *len = lim - off;
  42. if (!mtd_is_aligned_with_block_size(mtd, off)) {
  43. printf("Offset not aligned with a block (0x%x)\n",
  44. mtd->erasesize);
  45. return 0;
  46. }
  47. /* first erase */
  48. if (op == DFU_OP_WRITE) {
  49. struct erase_info erase_op = {};
  50. remaining = round_up(*len, mtd->erasesize);
  51. erase_op.mtd = mtd;
  52. erase_op.addr = off;
  53. erase_op.len = mtd->erasesize;
  54. erase_op.scrub = 0;
  55. while (remaining) {
  56. if (erase_op.addr + remaining > lim) {
  57. printf("Limit reached 0x%llx while erasing at offset 0x%llx\n",
  58. lim, off);
  59. return -EIO;
  60. }
  61. ret = mtd_erase(mtd, &erase_op);
  62. if (ret) {
  63. /* Abort if its not a bad block error */
  64. if (ret != -EIO) {
  65. printf("Failure while erasing at offset 0x%llx\n",
  66. erase_op.fail_addr);
  67. return 0;
  68. }
  69. printf("Skipping bad block at 0x%08llx\n",
  70. erase_op.addr);
  71. } else {
  72. remaining -= mtd->erasesize;
  73. }
  74. /* Continue erase behind bad block */
  75. erase_op.addr += mtd->erasesize;
  76. }
  77. }
  78. io_op.mode = MTD_OPS_AUTO_OOB;
  79. io_op.len = *len;
  80. if (has_pages && io_op.len > mtd->writesize)
  81. io_op.len = mtd->writesize;
  82. io_op.ooblen = 0;
  83. io_op.datbuf = buf;
  84. io_op.oobbuf = NULL;
  85. /* Loop over to do the actual read/write */
  86. remaining = *len;
  87. while (remaining) {
  88. if (off + remaining > lim) {
  89. printf("Limit reached 0x%llx while %s at offset 0x%llx\n",
  90. lim, op == DFU_OP_READ ? "reading" : "writing",
  91. off);
  92. if (op == DFU_OP_READ) {
  93. *len -= remaining;
  94. return 0;
  95. } else {
  96. return -EIO;
  97. }
  98. }
  99. /* Skip the block if it is bad */
  100. if (mtd_is_aligned_with_block_size(mtd, off) &&
  101. mtd_block_isbad(mtd, off)) {
  102. off += mtd->erasesize;
  103. dfu->bad_skip += mtd->erasesize;
  104. continue;
  105. }
  106. if (op == DFU_OP_READ)
  107. ret = mtd_read_oob(mtd, off, &io_op);
  108. else
  109. ret = mtd_write_oob(mtd, off, &io_op);
  110. if (ret) {
  111. printf("Failure while %s at offset 0x%llx\n",
  112. op == DFU_OP_READ ? "reading" : "writing", off);
  113. return -EIO;
  114. }
  115. off += io_op.retlen;
  116. remaining -= io_op.retlen;
  117. io_op.datbuf += io_op.retlen;
  118. io_op.len = remaining;
  119. if (has_pages && io_op.len > mtd->writesize)
  120. io_op.len = mtd->writesize;
  121. }
  122. return ret;
  123. }
  124. static int dfu_get_medium_size_mtd(struct dfu_entity *dfu, u64 *size)
  125. {
  126. *size = dfu->data.mtd.info->size;
  127. return 0;
  128. }
  129. static int dfu_read_medium_mtd(struct dfu_entity *dfu, u64 offset, void *buf,
  130. long *len)
  131. {
  132. int ret = -1;
  133. switch (dfu->layout) {
  134. case DFU_RAW_ADDR:
  135. ret = mtd_block_op(DFU_OP_READ, dfu, offset, buf, len);
  136. break;
  137. default:
  138. printf("%s: Layout (%s) not (yet) supported!\n", __func__,
  139. dfu_get_layout(dfu->layout));
  140. }
  141. return ret;
  142. }
  143. static int dfu_write_medium_mtd(struct dfu_entity *dfu,
  144. u64 offset, void *buf, long *len)
  145. {
  146. int ret = -1;
  147. switch (dfu->layout) {
  148. case DFU_RAW_ADDR:
  149. ret = mtd_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
  150. break;
  151. default:
  152. printf("%s: Layout (%s) not (yet) supported!\n", __func__,
  153. dfu_get_layout(dfu->layout));
  154. }
  155. return ret;
  156. }
  157. static int dfu_flush_medium_mtd(struct dfu_entity *dfu)
  158. {
  159. struct mtd_info *mtd = dfu->data.mtd.info;
  160. u64 remaining;
  161. int ret;
  162. /* in case of ubi partition, erase rest of the partition */
  163. if (dfu->data.nand.ubi) {
  164. struct erase_info erase_op = {};
  165. erase_op.mtd = dfu->data.mtd.info;
  166. erase_op.addr = round_up(dfu->data.mtd.start + dfu->offset +
  167. dfu->bad_skip, mtd->erasesize);
  168. erase_op.len = mtd->erasesize;
  169. erase_op.scrub = 0;
  170. remaining = dfu->data.mtd.start + dfu->data.mtd.size -
  171. erase_op.addr;
  172. while (remaining) {
  173. ret = mtd_erase(mtd, &erase_op);
  174. if (ret) {
  175. /* Abort if its not a bad block error */
  176. if (ret != -EIO)
  177. break;
  178. printf("Skipping bad block at 0x%08llx\n",
  179. erase_op.addr);
  180. }
  181. /* Skip bad block and continue behind it */
  182. erase_op.addr += mtd->erasesize;
  183. remaining -= mtd->erasesize;
  184. }
  185. }
  186. return 0;
  187. }
  188. static unsigned int dfu_polltimeout_mtd(struct dfu_entity *dfu)
  189. {
  190. /*
  191. * Currently, Poll Timeout != 0 is only needed on nand
  192. * ubi partition, as sectors which are not used need
  193. * to be erased
  194. */
  195. if (dfu->data.nand.ubi)
  196. return DFU_MANIFEST_POLL_TIMEOUT;
  197. return DFU_DEFAULT_POLL_TIMEOUT;
  198. }
  199. int dfu_fill_entity_mtd(struct dfu_entity *dfu, char *devstr, char *s)
  200. {
  201. char *st;
  202. struct mtd_info *mtd;
  203. bool has_pages;
  204. int ret, part;
  205. mtd = get_mtd_device_nm(devstr);
  206. if (IS_ERR_OR_NULL(mtd))
  207. return -ENODEV;
  208. put_mtd_device(mtd);
  209. dfu->dev_type = DFU_DEV_MTD;
  210. dfu->data.mtd.info = mtd;
  211. has_pages = mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
  212. dfu->max_buf_size = has_pages ? mtd->erasesize : 0;
  213. st = strsep(&s, " ");
  214. if (!strcmp(st, "raw")) {
  215. dfu->layout = DFU_RAW_ADDR;
  216. dfu->data.mtd.start = simple_strtoul(s, &s, 16);
  217. s++;
  218. dfu->data.mtd.size = simple_strtoul(s, &s, 16);
  219. } else if ((!strcmp(st, "part")) || (!strcmp(st, "partubi"))) {
  220. char mtd_id[32];
  221. struct mtd_device *mtd_dev;
  222. u8 part_num;
  223. struct part_info *pi;
  224. dfu->layout = DFU_RAW_ADDR;
  225. part = simple_strtoul(s, &s, 10);
  226. sprintf(mtd_id, "%s,%d", devstr, part - 1);
  227. printf("using id '%s'\n", mtd_id);
  228. mtdparts_init();
  229. ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
  230. if (ret != 0) {
  231. printf("Could not locate '%s'\n", mtd_id);
  232. return -1;
  233. }
  234. dfu->data.mtd.start = pi->offset;
  235. dfu->data.mtd.size = pi->size;
  236. if (!strcmp(st, "partubi"))
  237. dfu->data.mtd.ubi = 1;
  238. } else {
  239. printf("%s: Memory layout (%s) not supported!\n", __func__, st);
  240. return -1;
  241. }
  242. if (!mtd_is_aligned_with_block_size(mtd, dfu->data.mtd.start)) {
  243. printf("Offset not aligned with a block (0x%x)\n",
  244. mtd->erasesize);
  245. return -EINVAL;
  246. }
  247. if (!mtd_is_aligned_with_block_size(mtd, dfu->data.mtd.size)) {
  248. printf("Size not aligned with a block (0x%x)\n",
  249. mtd->erasesize);
  250. return -EINVAL;
  251. }
  252. dfu->get_medium_size = dfu_get_medium_size_mtd;
  253. dfu->read_medium = dfu_read_medium_mtd;
  254. dfu->write_medium = dfu_write_medium_mtd;
  255. dfu->flush_medium = dfu_flush_medium_mtd;
  256. dfu->poll_timeout = dfu_polltimeout_mtd;
  257. /* initial state */
  258. dfu->inited = 0;
  259. return 0;
  260. }