dfu.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
  41. {
  42. static unsigned char *i_buf;
  43. static int i_blk_seq_num;
  44. long w_size = 0;
  45. int ret = 0;
  46. debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
  47. __func__, dfu->name, buf, size, blk_seq_num, i_buf);
  48. if (blk_seq_num == 0) {
  49. i_buf = dfu_buf;
  50. i_blk_seq_num = 0;
  51. }
  52. if (i_blk_seq_num++ != blk_seq_num) {
  53. printf("%s: Wrong sequence number! [%d] [%d]\n",
  54. __func__, i_blk_seq_num, blk_seq_num);
  55. return -1;
  56. }
  57. memcpy(i_buf, buf, size);
  58. i_buf += size;
  59. if (size == 0) {
  60. /* Integrity check (if needed) */
  61. debug("%s: %s %d [B] CRC32: 0x%x\n", __func__, dfu->name,
  62. i_buf - dfu_buf, crc32(0, dfu_buf, i_buf - dfu_buf));
  63. w_size = i_buf - dfu_buf;
  64. ret = dfu->write_medium(dfu, dfu_buf, &w_size);
  65. if (ret)
  66. debug("%s: Write error!\n", __func__);
  67. i_blk_seq_num = 0;
  68. i_buf = NULL;
  69. return ret;
  70. }
  71. return ret;
  72. }
  73. int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
  74. {
  75. static unsigned char *i_buf;
  76. static int i_blk_seq_num;
  77. static long r_size;
  78. static u32 crc;
  79. int ret = 0;
  80. debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
  81. __func__, dfu->name, buf, size, blk_seq_num, i_buf);
  82. if (blk_seq_num == 0) {
  83. i_buf = dfu_buf;
  84. ret = dfu->read_medium(dfu, i_buf, &r_size);
  85. debug("%s: %s %ld [B]\n", __func__, dfu->name, r_size);
  86. i_blk_seq_num = 0;
  87. /* Integrity check (if needed) */
  88. crc = crc32(0, dfu_buf, r_size);
  89. }
  90. if (i_blk_seq_num++ != blk_seq_num) {
  91. printf("%s: Wrong sequence number! [%d] [%d]\n",
  92. __func__, i_blk_seq_num, blk_seq_num);
  93. return -1;
  94. }
  95. if (r_size >= size) {
  96. memcpy(buf, i_buf, size);
  97. i_buf += size;
  98. r_size -= size;
  99. return size;
  100. } else {
  101. memcpy(buf, i_buf, r_size);
  102. i_buf += r_size;
  103. debug("%s: %s CRC32: 0x%x\n", __func__, dfu->name, crc);
  104. puts("UPLOAD ... done\nCtrl+C to exit ...\n");
  105. i_buf = NULL;
  106. i_blk_seq_num = 0;
  107. crc = 0;
  108. return r_size;
  109. }
  110. return ret;
  111. }
  112. static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
  113. char *interface, int num)
  114. {
  115. char *st;
  116. debug("%s: %s interface: %s num: %d\n", __func__, s, interface, num);
  117. st = strsep(&s, " ");
  118. strcpy(dfu->name, st);
  119. dfu->dev_num = num;
  120. dfu->alt = alt;
  121. /* Specific for mmc device */
  122. if (strcmp(interface, "mmc") == 0) {
  123. if (dfu_fill_entity_mmc(dfu, s))
  124. return -1;
  125. } else {
  126. printf("%s: Device %s not (yet) supported!\n",
  127. __func__, interface);
  128. return -1;
  129. }
  130. return 0;
  131. }
  132. void dfu_free_entities(void)
  133. {
  134. struct dfu_entity *dfu, *p, *t = NULL;
  135. list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
  136. list_del(&dfu->list);
  137. t = dfu;
  138. }
  139. if (t)
  140. free(t);
  141. INIT_LIST_HEAD(&dfu_list);
  142. }
  143. int dfu_config_entities(char *env, char *interface, int num)
  144. {
  145. struct dfu_entity *dfu;
  146. int i, ret;
  147. char *s;
  148. dfu_alt_num = dfu_find_alt_num(env);
  149. debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
  150. dfu = calloc(sizeof(*dfu), dfu_alt_num);
  151. if (!dfu)
  152. return -1;
  153. for (i = 0; i < dfu_alt_num; i++) {
  154. s = strsep(&env, ";");
  155. ret = dfu_fill_entity(&dfu[i], s, i, interface, num);
  156. if (ret)
  157. return -1;
  158. list_add_tail(&dfu[i].list, &dfu_list);
  159. }
  160. return 0;
  161. }
  162. const char *dfu_get_dev_type(enum dfu_device_type t)
  163. {
  164. const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND" };
  165. return dev_t[t];
  166. }
  167. const char *dfu_get_layout(enum dfu_layout l)
  168. {
  169. const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
  170. "EXT3", "EXT4" };
  171. return dfu_layout[l];
  172. }
  173. void dfu_show_entities(void)
  174. {
  175. struct dfu_entity *dfu;
  176. puts("DFU alt settings list:\n");
  177. list_for_each_entry(dfu, &dfu_list, list) {
  178. printf("dev: %s alt: %d name: %s layout: %s\n",
  179. dfu_get_dev_type(dfu->dev_type), dfu->alt,
  180. dfu->name, dfu_get_layout(dfu->layout));
  181. }
  182. }
  183. int dfu_get_alt_number(void)
  184. {
  185. return dfu_alt_num;
  186. }
  187. struct dfu_entity *dfu_get_entity(int alt)
  188. {
  189. struct dfu_entity *dfu;
  190. list_for_each_entry(dfu, &dfu_list, list) {
  191. if (dfu->alt == alt)
  192. return dfu;
  193. }
  194. return NULL;
  195. }