dfu.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * dfu.c -- DFU back-end routines
  3. *
  4. * Copyright (C) 2012 Samsung Electronics
  5. * author: Lukasz Majewski <l.majewski@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <common.h>
  22. #include <malloc.h>
  23. #include <mmc.h>
  24. #include <fat.h>
  25. #include <dfu.h>
  26. #include <linux/list.h>
  27. #include <linux/compiler.h>
  28. static LIST_HEAD(dfu_list);
  29. static int dfu_alt_num;
  30. static int dfu_find_alt_num(const char *s)
  31. {
  32. int i = 0;
  33. for (; *s; s++)
  34. if (*s == ';')
  35. i++;
  36. return ++i;
  37. }
  38. static unsigned char __aligned(CONFIG_SYS_CACHELINE_SIZE)
  39. dfu_buf[DFU_DATA_BUF_SIZE];
  40. static int dfu_write_buffer_drain(struct dfu_entity *dfu)
  41. {
  42. long w_size;
  43. int ret;
  44. /* flush size? */
  45. w_size = dfu->i_buf - dfu->i_buf_start;
  46. if (w_size == 0)
  47. return 0;
  48. /* update CRC32 */
  49. dfu->crc = crc32(dfu->crc, dfu->i_buf_start, w_size);
  50. ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
  51. if (ret)
  52. debug("%s: Write error!\n", __func__);
  53. /* point back */
  54. dfu->i_buf = dfu->i_buf_start;
  55. /* update offset */
  56. dfu->offset += w_size;
  57. puts("#");
  58. return ret;
  59. }
  60. int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
  61. {
  62. int ret = 0;
  63. int tret;
  64. debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%x\n",
  65. __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
  66. dfu->i_buf - dfu->i_buf_start);
  67. if (!dfu->inited) {
  68. /* initial state */
  69. dfu->crc = 0;
  70. dfu->offset = 0;
  71. dfu->bad_skip = 0;
  72. dfu->i_blk_seq_num = 0;
  73. dfu->i_buf_start = dfu_buf;
  74. dfu->i_buf_end = dfu_buf + sizeof(dfu_buf);
  75. dfu->i_buf = dfu->i_buf_start;
  76. dfu->inited = 1;
  77. }
  78. if (dfu->i_blk_seq_num != blk_seq_num) {
  79. printf("%s: Wrong sequence number! [%d] [%d]\n",
  80. __func__, dfu->i_blk_seq_num, blk_seq_num);
  81. return -1;
  82. }
  83. /* DFU 1.1 standard says:
  84. * The wBlockNum field is a block sequence number. It increments each
  85. * time a block is transferred, wrapping to zero from 65,535. It is used
  86. * to provide useful context to the DFU loader in the device."
  87. *
  88. * This means that it's a 16 bit counter that roll-overs at
  89. * 0xffff -> 0x0000. By having a typical 4K transfer block
  90. * we roll-over at exactly 256MB. Not very fun to debug.
  91. *
  92. * Handling rollover, and having an inited variable,
  93. * makes things work.
  94. */
  95. /* handle rollover */
  96. dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
  97. /* flush buffer if overflow */
  98. if ((dfu->i_buf + size) > dfu->i_buf_end) {
  99. tret = dfu_write_buffer_drain(dfu);
  100. if (ret == 0)
  101. ret = tret;
  102. }
  103. /* we should be in buffer now (if not then size too large) */
  104. if ((dfu->i_buf + size) > dfu->i_buf_end) {
  105. printf("%s: Wrong size! [%d] [%d] - %d\n",
  106. __func__, dfu->i_blk_seq_num, blk_seq_num, size);
  107. return -1;
  108. }
  109. memcpy(dfu->i_buf, buf, size);
  110. dfu->i_buf += size;
  111. /* if end or if buffer full flush */
  112. if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
  113. tret = dfu_write_buffer_drain(dfu);
  114. if (ret == 0)
  115. ret = tret;
  116. }
  117. /* end? */
  118. if (size == 0) {
  119. /* Now try and flush to the medium if needed. */
  120. if (dfu->flush_medium)
  121. ret = dfu->flush_medium(dfu);
  122. printf("\nDFU complete CRC32: 0x%08x\n", dfu->crc);
  123. /* clear everything */
  124. dfu->crc = 0;
  125. dfu->offset = 0;
  126. dfu->i_blk_seq_num = 0;
  127. dfu->i_buf_start = dfu_buf;
  128. dfu->i_buf_end = dfu_buf + sizeof(dfu_buf);
  129. dfu->i_buf = dfu->i_buf_start;
  130. dfu->inited = 0;
  131. }
  132. return ret = 0 ? size : ret;
  133. }
  134. static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
  135. {
  136. long chunk;
  137. int ret, readn;
  138. readn = 0;
  139. while (size > 0) {
  140. /* get chunk that can be read */
  141. chunk = min(size, dfu->b_left);
  142. /* consume */
  143. if (chunk > 0) {
  144. memcpy(buf, dfu->i_buf, chunk);
  145. dfu->crc = crc32(dfu->crc, buf, chunk);
  146. dfu->i_buf += chunk;
  147. dfu->b_left -= chunk;
  148. size -= chunk;
  149. buf += chunk;
  150. readn += chunk;
  151. }
  152. /* all done */
  153. if (size > 0) {
  154. /* no more to read */
  155. if (dfu->r_left == 0)
  156. break;
  157. dfu->i_buf = dfu->i_buf_start;
  158. dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
  159. /* got to read, but buffer is empty */
  160. if (dfu->b_left > dfu->r_left)
  161. dfu->b_left = dfu->r_left;
  162. ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
  163. &dfu->b_left);
  164. if (ret != 0) {
  165. debug("%s: Read error!\n", __func__);
  166. return ret;
  167. }
  168. dfu->offset += dfu->b_left;
  169. dfu->r_left -= dfu->b_left;
  170. puts("#");
  171. }
  172. }
  173. return readn;
  174. }
  175. int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
  176. {
  177. int ret = 0;
  178. debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
  179. __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
  180. if (!dfu->inited) {
  181. ret = dfu->read_medium(dfu, 0, buf, &dfu->r_left);
  182. if (ret != 0) {
  183. debug("%s: failed to get r_left\n", __func__);
  184. return ret;
  185. }
  186. debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left);
  187. dfu->i_blk_seq_num = 0;
  188. dfu->crc = 0;
  189. dfu->offset = 0;
  190. dfu->i_buf_start = dfu_buf;
  191. dfu->i_buf_end = dfu_buf + sizeof(dfu_buf);
  192. dfu->i_buf = dfu->i_buf_start;
  193. dfu->b_left = 0;
  194. dfu->bad_skip = 0;
  195. dfu->inited = 1;
  196. }
  197. if (dfu->i_blk_seq_num != blk_seq_num) {
  198. printf("%s: Wrong sequence number! [%d] [%d]\n",
  199. __func__, dfu->i_blk_seq_num, blk_seq_num);
  200. return -1;
  201. }
  202. /* handle rollover */
  203. dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
  204. ret = dfu_read_buffer_fill(dfu, buf, size);
  205. if (ret < 0) {
  206. printf("%s: Failed to fill buffer\n", __func__);
  207. return -1;
  208. }
  209. if (ret < size) {
  210. debug("%s: %s CRC32: 0x%x\n", __func__, dfu->name, dfu->crc);
  211. puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
  212. dfu->i_blk_seq_num = 0;
  213. dfu->crc = 0;
  214. dfu->offset = 0;
  215. dfu->i_buf_start = dfu_buf;
  216. dfu->i_buf_end = dfu_buf + sizeof(dfu_buf);
  217. dfu->i_buf = dfu->i_buf_start;
  218. dfu->b_left = 0;
  219. dfu->bad_skip = 0;
  220. dfu->inited = 0;
  221. }
  222. return ret;
  223. }
  224. static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
  225. char *interface, int num)
  226. {
  227. char *st;
  228. debug("%s: %s interface: %s num: %d\n", __func__, s, interface, num);
  229. st = strsep(&s, " ");
  230. strcpy(dfu->name, st);
  231. dfu->dev_num = num;
  232. dfu->alt = alt;
  233. /* Specific for mmc device */
  234. if (strcmp(interface, "mmc") == 0) {
  235. if (dfu_fill_entity_mmc(dfu, s))
  236. return -1;
  237. } else if (strcmp(interface, "nand") == 0) {
  238. if (dfu_fill_entity_nand(dfu, s))
  239. return -1;
  240. } else {
  241. printf("%s: Device %s not (yet) supported!\n",
  242. __func__, interface);
  243. return -1;
  244. }
  245. return 0;
  246. }
  247. void dfu_free_entities(void)
  248. {
  249. struct dfu_entity *dfu, *p, *t = NULL;
  250. list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
  251. list_del(&dfu->list);
  252. t = dfu;
  253. }
  254. if (t)
  255. free(t);
  256. INIT_LIST_HEAD(&dfu_list);
  257. }
  258. int dfu_config_entities(char *env, char *interface, int num)
  259. {
  260. struct dfu_entity *dfu;
  261. int i, ret;
  262. char *s;
  263. dfu_alt_num = dfu_find_alt_num(env);
  264. debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
  265. dfu = calloc(sizeof(*dfu), dfu_alt_num);
  266. if (!dfu)
  267. return -1;
  268. for (i = 0; i < dfu_alt_num; i++) {
  269. s = strsep(&env, ";");
  270. ret = dfu_fill_entity(&dfu[i], s, i, interface, num);
  271. if (ret)
  272. return -1;
  273. list_add_tail(&dfu[i].list, &dfu_list);
  274. }
  275. return 0;
  276. }
  277. const char *dfu_get_dev_type(enum dfu_device_type t)
  278. {
  279. const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND" };
  280. return dev_t[t];
  281. }
  282. const char *dfu_get_layout(enum dfu_layout l)
  283. {
  284. const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
  285. "EXT3", "EXT4" };
  286. return dfu_layout[l];
  287. }
  288. void dfu_show_entities(void)
  289. {
  290. struct dfu_entity *dfu;
  291. puts("DFU alt settings list:\n");
  292. list_for_each_entry(dfu, &dfu_list, list) {
  293. printf("dev: %s alt: %d name: %s layout: %s\n",
  294. dfu_get_dev_type(dfu->dev_type), dfu->alt,
  295. dfu->name, dfu_get_layout(dfu->layout));
  296. }
  297. }
  298. int dfu_get_alt_number(void)
  299. {
  300. return dfu_alt_num;
  301. }
  302. struct dfu_entity *dfu_get_entity(int alt)
  303. {
  304. struct dfu_entity *dfu;
  305. list_for_each_entry(dfu, &dfu_list, list) {
  306. if (dfu->alt == alt)
  307. return dfu;
  308. }
  309. return NULL;
  310. }