dfu_mtd.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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, lock_ofs, lock_len;
  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 = lock_ofs = 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 = lock_len = 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. debug("Unlocking the mtd device\n");
  56. ret = mtd_unlock(mtd, lock_ofs, lock_len);
  57. if (ret && ret != -EOPNOTSUPP) {
  58. printf("MTD device unlock failed\n");
  59. return 0;
  60. }
  61. while (remaining) {
  62. if (erase_op.addr + remaining > lim) {
  63. printf("Limit reached 0x%llx while erasing at offset 0x%llx\n",
  64. lim, off);
  65. return -EIO;
  66. }
  67. ret = mtd_erase(mtd, &erase_op);
  68. if (ret) {
  69. /* Abort if its not a bad block error */
  70. if (ret != -EIO) {
  71. printf("Failure while erasing at offset 0x%llx\n",
  72. erase_op.fail_addr);
  73. return 0;
  74. }
  75. printf("Skipping bad block at 0x%08llx\n",
  76. erase_op.addr);
  77. } else {
  78. remaining -= mtd->erasesize;
  79. }
  80. /* Continue erase behind bad block */
  81. erase_op.addr += mtd->erasesize;
  82. }
  83. }
  84. io_op.mode = MTD_OPS_AUTO_OOB;
  85. io_op.len = *len;
  86. if (has_pages && io_op.len > mtd->writesize)
  87. io_op.len = mtd->writesize;
  88. io_op.ooblen = 0;
  89. io_op.datbuf = buf;
  90. io_op.oobbuf = NULL;
  91. /* Loop over to do the actual read/write */
  92. remaining = *len;
  93. while (remaining) {
  94. if (off + remaining > lim) {
  95. printf("Limit reached 0x%llx while %s at offset 0x%llx\n",
  96. lim, op == DFU_OP_READ ? "reading" : "writing",
  97. off);
  98. if (op == DFU_OP_READ) {
  99. *len -= remaining;
  100. return 0;
  101. } else {
  102. return -EIO;
  103. }
  104. }
  105. /* Skip the block if it is bad */
  106. if (mtd_is_aligned_with_block_size(mtd, off) &&
  107. mtd_block_isbad(mtd, off)) {
  108. off += mtd->erasesize;
  109. dfu->bad_skip += mtd->erasesize;
  110. continue;
  111. }
  112. if (op == DFU_OP_READ)
  113. ret = mtd_read_oob(mtd, off, &io_op);
  114. else
  115. ret = mtd_write_oob(mtd, off, &io_op);
  116. if (ret) {
  117. printf("Failure while %s at offset 0x%llx\n",
  118. op == DFU_OP_READ ? "reading" : "writing", off);
  119. return -EIO;
  120. }
  121. off += io_op.retlen;
  122. remaining -= io_op.retlen;
  123. io_op.datbuf += io_op.retlen;
  124. io_op.len = remaining;
  125. if (has_pages && io_op.len > mtd->writesize)
  126. io_op.len = mtd->writesize;
  127. }
  128. if (op == DFU_OP_WRITE) {
  129. /* Write done, lock again */
  130. debug("Locking the mtd device\n");
  131. ret = mtd_lock(mtd, lock_ofs, lock_len);
  132. if (ret == -EOPNOTSUPP)
  133. ret = 0;
  134. else if (ret)
  135. printf("MTD device lock failed\n");
  136. }
  137. return ret;
  138. }
  139. static int dfu_get_medium_size_mtd(struct dfu_entity *dfu, u64 *size)
  140. {
  141. *size = dfu->data.mtd.info->size;
  142. return 0;
  143. }
  144. static int dfu_read_medium_mtd(struct dfu_entity *dfu, u64 offset, void *buf,
  145. long *len)
  146. {
  147. int ret = -1;
  148. switch (dfu->layout) {
  149. case DFU_RAW_ADDR:
  150. ret = mtd_block_op(DFU_OP_READ, dfu, offset, buf, len);
  151. break;
  152. default:
  153. printf("%s: Layout (%s) not (yet) supported!\n", __func__,
  154. dfu_get_layout(dfu->layout));
  155. }
  156. return ret;
  157. }
  158. static int dfu_write_medium_mtd(struct dfu_entity *dfu,
  159. u64 offset, void *buf, long *len)
  160. {
  161. int ret = -1;
  162. switch (dfu->layout) {
  163. case DFU_RAW_ADDR:
  164. ret = mtd_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
  165. break;
  166. default:
  167. printf("%s: Layout (%s) not (yet) supported!\n", __func__,
  168. dfu_get_layout(dfu->layout));
  169. }
  170. return ret;
  171. }
  172. static int dfu_flush_medium_mtd(struct dfu_entity *dfu)
  173. {
  174. struct mtd_info *mtd = dfu->data.mtd.info;
  175. u64 remaining;
  176. int ret;
  177. /* in case of ubi partition, erase rest of the partition */
  178. if (dfu->data.mtd.ubi) {
  179. struct erase_info erase_op = {};
  180. erase_op.mtd = dfu->data.mtd.info;
  181. erase_op.addr = round_up(dfu->data.mtd.start + dfu->offset +
  182. dfu->bad_skip, mtd->erasesize);
  183. erase_op.len = mtd->erasesize;
  184. erase_op.scrub = 0;
  185. remaining = dfu->data.mtd.start + dfu->data.mtd.size -
  186. erase_op.addr;
  187. while (remaining) {
  188. ret = mtd_erase(mtd, &erase_op);
  189. if (ret) {
  190. /* Abort if its not a bad block error */
  191. if (ret != -EIO)
  192. break;
  193. printf("Skipping bad block at 0x%08llx\n",
  194. erase_op.addr);
  195. }
  196. /* Skip bad block and continue behind it */
  197. erase_op.addr += mtd->erasesize;
  198. remaining -= mtd->erasesize;
  199. }
  200. }
  201. return 0;
  202. }
  203. static unsigned int dfu_polltimeout_mtd(struct dfu_entity *dfu)
  204. {
  205. /*
  206. * Currently, Poll Timeout != 0 is only needed on nand
  207. * ubi partition, as sectors which are not used need
  208. * to be erased
  209. */
  210. if (dfu->data.mtd.ubi)
  211. return DFU_MANIFEST_POLL_TIMEOUT;
  212. return DFU_DEFAULT_POLL_TIMEOUT;
  213. }
  214. int dfu_fill_entity_mtd(struct dfu_entity *dfu, char *devstr, char *s)
  215. {
  216. char *st;
  217. struct mtd_info *mtd;
  218. int ret, part;
  219. mtd = get_mtd_device_nm(devstr);
  220. if (IS_ERR_OR_NULL(mtd))
  221. return -ENODEV;
  222. put_mtd_device(mtd);
  223. dfu->dev_type = DFU_DEV_MTD;
  224. dfu->data.mtd.info = mtd;
  225. dfu->max_buf_size = mtd->erasesize;
  226. st = strsep(&s, " ");
  227. if (!strcmp(st, "raw")) {
  228. dfu->layout = DFU_RAW_ADDR;
  229. dfu->data.mtd.start = hextoul(s, &s);
  230. s++;
  231. dfu->data.mtd.size = hextoul(s, &s);
  232. } else if ((!strcmp(st, "part")) || (!strcmp(st, "partubi"))) {
  233. char mtd_id[32];
  234. struct mtd_device *mtd_dev;
  235. u8 part_num;
  236. struct part_info *pi;
  237. dfu->layout = DFU_RAW_ADDR;
  238. part = dectoul(s, &s);
  239. sprintf(mtd_id, "%s,%d", devstr, part - 1);
  240. printf("using id '%s'\n", mtd_id);
  241. mtdparts_init();
  242. ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
  243. if (ret != 0) {
  244. printf("Could not locate '%s'\n", mtd_id);
  245. return -1;
  246. }
  247. dfu->data.mtd.start = pi->offset;
  248. dfu->data.mtd.size = pi->size;
  249. if (!strcmp(st, "partubi"))
  250. dfu->data.mtd.ubi = 1;
  251. } else {
  252. printf("%s: Memory layout (%s) not supported!\n", __func__, st);
  253. return -1;
  254. }
  255. if (!mtd_is_aligned_with_block_size(mtd, dfu->data.mtd.start)) {
  256. printf("Offset not aligned with a block (0x%x)\n",
  257. mtd->erasesize);
  258. return -EINVAL;
  259. }
  260. if (!mtd_is_aligned_with_block_size(mtd, dfu->data.mtd.size)) {
  261. printf("Size not aligned with a block (0x%x)\n",
  262. mtd->erasesize);
  263. return -EINVAL;
  264. }
  265. dfu->get_medium_size = dfu_get_medium_size_mtd;
  266. dfu->read_medium = dfu_read_medium_mtd;
  267. dfu->write_medium = dfu_write_medium_mtd;
  268. dfu->flush_medium = dfu_flush_medium_mtd;
  269. dfu->poll_timeout = dfu_polltimeout_mtd;
  270. /* initial state */
  271. dfu->inited = 0;
  272. return 0;
  273. }