dfu.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * dfu.h - DFU flashable area description
  3. *
  4. * Copyright (C) 2012 Samsung Electronics
  5. * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
  6. * Lukasz Majewski <l.majewski@samsung.com>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #ifndef __DFU_ENTITY_H_
  11. #define __DFU_ENTITY_H_
  12. #include <common.h>
  13. #include <linux/list.h>
  14. #include <mmc.h>
  15. #include <spi_flash.h>
  16. #include <linux/usb/composite.h>
  17. enum dfu_device_type {
  18. DFU_DEV_MMC = 1,
  19. DFU_DEV_ONENAND,
  20. DFU_DEV_NAND,
  21. DFU_DEV_RAM,
  22. DFU_DEV_SF,
  23. };
  24. enum dfu_layout {
  25. DFU_RAW_ADDR = 1,
  26. DFU_FS_FAT,
  27. DFU_FS_EXT2,
  28. DFU_FS_EXT3,
  29. DFU_FS_EXT4,
  30. DFU_RAM_ADDR,
  31. };
  32. enum dfu_op {
  33. DFU_OP_READ = 1,
  34. DFU_OP_WRITE,
  35. DFU_OP_SIZE,
  36. };
  37. struct mmc_internal_data {
  38. int dev_num;
  39. /* RAW programming */
  40. unsigned int lba_start;
  41. unsigned int lba_size;
  42. unsigned int lba_blk_size;
  43. /* eMMC HW partition access */
  44. int hw_partition;
  45. /* FAT/EXT */
  46. unsigned int dev;
  47. unsigned int part;
  48. };
  49. struct nand_internal_data {
  50. /* RAW programming */
  51. u64 start;
  52. u64 size;
  53. unsigned int dev;
  54. unsigned int part;
  55. /* for nand/ubi use */
  56. unsigned int ubi;
  57. };
  58. struct ram_internal_data {
  59. void *start;
  60. unsigned int size;
  61. };
  62. struct sf_internal_data {
  63. struct spi_flash *dev;
  64. /* RAW programming */
  65. u64 start;
  66. u64 size;
  67. };
  68. #define DFU_NAME_SIZE 32
  69. #define DFU_CMD_BUF_SIZE 128
  70. #ifndef CONFIG_SYS_DFU_DATA_BUF_SIZE
  71. #define CONFIG_SYS_DFU_DATA_BUF_SIZE (1024*1024*8) /* 8 MiB */
  72. #endif
  73. #ifndef CONFIG_SYS_DFU_MAX_FILE_SIZE
  74. #define CONFIG_SYS_DFU_MAX_FILE_SIZE CONFIG_SYS_DFU_DATA_BUF_SIZE
  75. #endif
  76. #ifndef DFU_DEFAULT_POLL_TIMEOUT
  77. #define DFU_DEFAULT_POLL_TIMEOUT 0
  78. #endif
  79. #ifndef DFU_MANIFEST_POLL_TIMEOUT
  80. #define DFU_MANIFEST_POLL_TIMEOUT DFU_DEFAULT_POLL_TIMEOUT
  81. #endif
  82. struct dfu_entity {
  83. char name[DFU_NAME_SIZE];
  84. int alt;
  85. void *dev_private;
  86. enum dfu_device_type dev_type;
  87. enum dfu_layout layout;
  88. unsigned long max_buf_size;
  89. union {
  90. struct mmc_internal_data mmc;
  91. struct nand_internal_data nand;
  92. struct ram_internal_data ram;
  93. struct sf_internal_data sf;
  94. } data;
  95. long (*get_medium_size)(struct dfu_entity *dfu);
  96. int (*read_medium)(struct dfu_entity *dfu,
  97. u64 offset, void *buf, long *len);
  98. int (*write_medium)(struct dfu_entity *dfu,
  99. u64 offset, void *buf, long *len);
  100. int (*flush_medium)(struct dfu_entity *dfu);
  101. unsigned int (*poll_timeout)(struct dfu_entity *dfu);
  102. void (*free_entity)(struct dfu_entity *dfu);
  103. struct list_head list;
  104. /* on the fly state */
  105. u32 crc;
  106. u64 offset;
  107. int i_blk_seq_num;
  108. u8 *i_buf;
  109. u8 *i_buf_start;
  110. u8 *i_buf_end;
  111. long r_left;
  112. long b_left;
  113. u32 bad_skip; /* for nand use */
  114. unsigned int inited:1;
  115. };
  116. #ifdef CONFIG_SET_DFU_ALT_INFO
  117. void set_dfu_alt_info(char *interface, char *devstr);
  118. #endif
  119. int dfu_config_entities(char *s, char *interface, char *devstr);
  120. void dfu_free_entities(void);
  121. void dfu_show_entities(void);
  122. int dfu_get_alt_number(void);
  123. const char *dfu_get_dev_type(enum dfu_device_type t);
  124. const char *dfu_get_layout(enum dfu_layout l);
  125. struct dfu_entity *dfu_get_entity(int alt);
  126. char *dfu_extract_token(char** e, int *n);
  127. void dfu_trigger_reset(void);
  128. int dfu_get_alt(char *name);
  129. int dfu_init_env_entities(char *interface, char *devstr);
  130. unsigned char *dfu_get_buf(struct dfu_entity *dfu);
  131. unsigned char *dfu_free_buf(void);
  132. unsigned long dfu_get_buf_size(void);
  133. bool dfu_usb_get_reset(void);
  134. int dfu_read(struct dfu_entity *de, void *buf, int size, int blk_seq_num);
  135. int dfu_write(struct dfu_entity *de, void *buf, int size, int blk_seq_num);
  136. int dfu_flush(struct dfu_entity *de, void *buf, int size, int blk_seq_num);
  137. /*
  138. * dfu_defer_flush - pointer to store dfu_entity for deferred flashing.
  139. * It should be NULL when not used.
  140. */
  141. extern struct dfu_entity *dfu_defer_flush;
  142. /**
  143. * dfu_get_defer_flush - get current value of dfu_defer_flush pointer
  144. *
  145. * @return - value of the dfu_defer_flush pointer
  146. */
  147. static inline struct dfu_entity *dfu_get_defer_flush(void)
  148. {
  149. return dfu_defer_flush;
  150. }
  151. /**
  152. * dfu_set_defer_flush - set the dfu_defer_flush pointer
  153. *
  154. * @param dfu - pointer to the dfu_entity, which should be written
  155. */
  156. static inline void dfu_set_defer_flush(struct dfu_entity *dfu)
  157. {
  158. dfu_defer_flush = dfu;
  159. }
  160. /**
  161. * dfu_write_from_mem_addr - write data from memory to DFU managed medium
  162. *
  163. * This function adds support for writing data starting from fixed memory
  164. * address (like $loadaddr) to dfu managed medium (e.g. NAND, MMC, file system)
  165. *
  166. * @param dfu - dfu entity to which we want to store data
  167. * @param buf - fixed memory addres from where data starts
  168. * @param size - number of bytes to write
  169. *
  170. * @return - 0 on success, other value on failure
  171. */
  172. int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size);
  173. /* Device specific */
  174. #if CONFIG_IS_ENABLED(DFU_MMC)
  175. extern int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char *s);
  176. #else
  177. static inline int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr,
  178. char *s)
  179. {
  180. puts("MMC support not available!\n");
  181. return -1;
  182. }
  183. #endif
  184. #ifdef CONFIG_DFU_NAND
  185. extern int dfu_fill_entity_nand(struct dfu_entity *dfu, char *devstr, char *s);
  186. #else
  187. static inline int dfu_fill_entity_nand(struct dfu_entity *dfu, char *devstr,
  188. char *s)
  189. {
  190. puts("NAND support not available!\n");
  191. return -1;
  192. }
  193. #endif
  194. #ifdef CONFIG_DFU_RAM
  195. extern int dfu_fill_entity_ram(struct dfu_entity *dfu, char *devstr, char *s);
  196. #else
  197. static inline int dfu_fill_entity_ram(struct dfu_entity *dfu, char *devstr,
  198. char *s)
  199. {
  200. puts("RAM support not available!\n");
  201. return -1;
  202. }
  203. #endif
  204. #ifdef CONFIG_DFU_SF
  205. extern int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char *s);
  206. #else
  207. static inline int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr,
  208. char *s)
  209. {
  210. puts("SF support not available!\n");
  211. return -1;
  212. }
  213. #endif
  214. /**
  215. * dfu_tftp_write - Write TFTP data to DFU medium
  216. *
  217. * This function is storing data received via TFTP on DFU supported medium.
  218. *
  219. * @param dfu_entity_name - name of DFU entity to write
  220. * @param addr - address of data buffer to write
  221. * @param len - number of bytes
  222. * @param interface - destination DFU medium (e.g. "mmc")
  223. * @param devstring - instance number of destination DFU medium (e.g. "1")
  224. *
  225. * @return 0 on success, otherwise error code
  226. */
  227. #ifdef CONFIG_DFU_TFTP
  228. int dfu_tftp_write(char *dfu_entity_name, unsigned int addr, unsigned int len,
  229. char *interface, char *devstring);
  230. #else
  231. static inline int dfu_tftp_write(char *dfu_entity_name, unsigned int addr,
  232. unsigned int len, char *interface,
  233. char *devstring)
  234. {
  235. puts("TFTP write support for DFU not available!\n");
  236. return -ENOSYS;
  237. }
  238. #endif
  239. int dfu_add(struct usb_configuration *c);
  240. #endif /* __DFU_ENTITY_H_ */