dfu.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * dfu.h - DFU flashable area description
  4. *
  5. * Copyright (C) 2012 Samsung Electronics
  6. * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
  7. * Lukasz Majewski <l.majewski@samsung.com>
  8. */
  9. #ifndef __DFU_ENTITY_H_
  10. #define __DFU_ENTITY_H_
  11. #include <common.h>
  12. #include <linux/list.h>
  13. #include <mmc.h>
  14. #include <spi_flash.h>
  15. #include <linux/usb/composite.h>
  16. enum dfu_device_type {
  17. DFU_DEV_MMC = 1,
  18. DFU_DEV_ONENAND,
  19. DFU_DEV_NAND,
  20. DFU_DEV_RAM,
  21. DFU_DEV_SF,
  22. DFU_DEV_MTD,
  23. DFU_DEV_VIRT,
  24. };
  25. enum dfu_layout {
  26. DFU_RAW_ADDR = 1,
  27. DFU_FS_FAT,
  28. DFU_FS_EXT2,
  29. DFU_FS_EXT3,
  30. DFU_FS_EXT4,
  31. DFU_RAM_ADDR,
  32. };
  33. enum dfu_op {
  34. DFU_OP_READ = 1,
  35. DFU_OP_WRITE,
  36. DFU_OP_SIZE,
  37. };
  38. struct mmc_internal_data {
  39. int dev_num;
  40. /* RAW programming */
  41. unsigned int lba_start;
  42. unsigned int lba_size;
  43. unsigned int lba_blk_size;
  44. /* eMMC HW partition access */
  45. int hw_partition;
  46. /* FAT/EXT */
  47. unsigned int dev;
  48. unsigned int part;
  49. };
  50. struct mtd_internal_data {
  51. struct mtd_info *info;
  52. /* RAW programming */
  53. u64 start;
  54. u64 size;
  55. /* for ubi partition */
  56. unsigned int ubi;
  57. };
  58. struct nand_internal_data {
  59. /* RAW programming */
  60. u64 start;
  61. u64 size;
  62. unsigned int dev;
  63. unsigned int part;
  64. /* for nand/ubi use */
  65. unsigned int ubi;
  66. };
  67. struct ram_internal_data {
  68. unsigned long start;
  69. unsigned int size;
  70. };
  71. struct sf_internal_data {
  72. struct spi_flash *dev;
  73. /* RAW programming */
  74. u64 start;
  75. u64 size;
  76. /* for sf/ubi use */
  77. unsigned int ubi;
  78. };
  79. struct virt_internal_data {
  80. int dev_num;
  81. };
  82. #define DFU_NAME_SIZE 32
  83. #ifndef CONFIG_SYS_DFU_DATA_BUF_SIZE
  84. #define CONFIG_SYS_DFU_DATA_BUF_SIZE (1024*1024*8) /* 8 MiB */
  85. #endif
  86. #ifndef CONFIG_SYS_DFU_MAX_FILE_SIZE
  87. #define CONFIG_SYS_DFU_MAX_FILE_SIZE CONFIG_SYS_DFU_DATA_BUF_SIZE
  88. #endif
  89. #ifndef DFU_DEFAULT_POLL_TIMEOUT
  90. #define DFU_DEFAULT_POLL_TIMEOUT 0
  91. #endif
  92. #ifndef DFU_MANIFEST_POLL_TIMEOUT
  93. #define DFU_MANIFEST_POLL_TIMEOUT DFU_DEFAULT_POLL_TIMEOUT
  94. #endif
  95. struct dfu_entity {
  96. char name[DFU_NAME_SIZE];
  97. int alt;
  98. void *dev_private;
  99. enum dfu_device_type dev_type;
  100. enum dfu_layout layout;
  101. unsigned long max_buf_size;
  102. union {
  103. struct mmc_internal_data mmc;
  104. struct mtd_internal_data mtd;
  105. struct nand_internal_data nand;
  106. struct ram_internal_data ram;
  107. struct sf_internal_data sf;
  108. struct virt_internal_data virt;
  109. } data;
  110. int (*get_medium_size)(struct dfu_entity *dfu, u64 *size);
  111. int (*read_medium)(struct dfu_entity *dfu,
  112. u64 offset, void *buf, long *len);
  113. int (*write_medium)(struct dfu_entity *dfu,
  114. u64 offset, void *buf, long *len);
  115. int (*flush_medium)(struct dfu_entity *dfu);
  116. unsigned int (*poll_timeout)(struct dfu_entity *dfu);
  117. void (*free_entity)(struct dfu_entity *dfu);
  118. struct list_head list;
  119. /* on the fly state */
  120. u32 crc;
  121. u64 offset;
  122. int i_blk_seq_num;
  123. u8 *i_buf;
  124. u8 *i_buf_start;
  125. u8 *i_buf_end;
  126. u64 r_left;
  127. long b_left;
  128. u32 bad_skip; /* for nand use */
  129. unsigned int inited:1;
  130. };
  131. struct list_head;
  132. extern struct list_head dfu_list;
  133. #ifdef CONFIG_SET_DFU_ALT_INFO
  134. /**
  135. * set_dfu_alt_info() - set dfu_alt_info environment variable
  136. *
  137. * If CONFIG_SET_DFU_ALT_INFO=y, this board specific function is called to set
  138. * environment variable dfu_alt_info.
  139. *
  140. * @interface: dfu interface, e.g. "mmc" or "nand"
  141. * @devstr: device number as string
  142. */
  143. void set_dfu_alt_info(char *interface, char *devstr);
  144. #endif
  145. /**
  146. * dfu_alt_init() - initialize buffer for dfu entities
  147. *
  148. * @num: number of entities
  149. * @dfu: on return allocated buffer
  150. * Return: 0 on success
  151. */
  152. int dfu_alt_init(int num, struct dfu_entity **dfu);
  153. /**
  154. * dfu_alt_add() - add alternate to dfu entity buffer
  155. *
  156. * @dfu: dfu entity
  157. * @interface: dfu interface, e.g. "mmc" or "nand"
  158. * @devstr: device number as string
  159. * @s: string description of alternate
  160. * Return: 0 on success
  161. */
  162. int dfu_alt_add(struct dfu_entity *dfu, char *interface, char *devstr, char *s);
  163. /**
  164. * dfu_config_entities() - initialize dfu entitities from envirionment
  165. *
  166. * Initialize the list of dfu entities from environment variable dfu_alt_info.
  167. * The list must be freed by calling dfu_free_entities(). This function bypasses
  168. * set_dfu_alt_info(). So typically you should use dfu_init_env_entities()
  169. * instead.
  170. *
  171. * See function :c:func:`dfu_free_entities`
  172. * See function :c:func:`dfu_init_env_entities`
  173. *
  174. * @s: string with alternates
  175. * @interface: interface, e.g. "mmc" or "nand"
  176. * @devstr: device number as string
  177. * Return: 0 on success, a negative error code otherwise
  178. */
  179. int dfu_config_entities(char *s, char *interface, char *devstr);
  180. /**
  181. * dfu_free_entities() - free the list of dfu entities
  182. *
  183. * Free the internal list of dfu entities.
  184. *
  185. * See function :c:func:`dfu_init_env_entities`
  186. */
  187. void dfu_free_entities(void);
  188. /**
  189. * dfu_show_entities() - print DFU alt settings list
  190. */
  191. void dfu_show_entities(void);
  192. /**
  193. * dfu_get_alt_number() - get number of alternates
  194. *
  195. * Return: number of alternates in the dfu entities list
  196. */
  197. int dfu_get_alt_number(void);
  198. /**
  199. * dfu_get_dev_type() - get string representation for dfu device type
  200. *
  201. * @type: device type
  202. * Return: string representation for device type
  203. */
  204. const char *dfu_get_dev_type(enum dfu_device_type type);
  205. /**
  206. * dfu_get_layout() - get string describing layout
  207. *
  208. * Internally layouts are represented by enum dfu_device_type values. This
  209. * function translates an enum value to a human readable string, e.g. DFU_FS_FAT
  210. * is translated to "FAT".
  211. *
  212. * @layout: layout
  213. * Result: string representation for the layout
  214. */
  215. const char *dfu_get_layout(enum dfu_layout layout);
  216. /**
  217. * dfu_get_entity() - get dfu entity for an alternate id
  218. *
  219. * @alt: alternate id
  220. * Return: dfu entity
  221. */
  222. struct dfu_entity *dfu_get_entity(int alt);
  223. char *dfu_extract_token(char** e, int *n);
  224. /**
  225. * dfu_get_alt() - get alternate id for filename
  226. *
  227. * Environment variable dfu_alt_info defines the write destinations (alternates)
  228. * for different filenames. This function get the index of the alternate for
  229. * a filename. If an absolute filename is provided (starting with '/'), the
  230. * directory path is ignored.
  231. *
  232. * @name: filename
  233. * Return: id of the alternate or negative error number (-ENODEV)
  234. */
  235. int dfu_get_alt(char *name);
  236. /**
  237. * dfu_init_env_entities() - initialize dfu entitities from envirionment
  238. *
  239. * Initialize the list of dfu entities from environment variable dfu_alt_info.
  240. * The list must be freed by calling dfu_free_entities().
  241. * @interface and @devstr are used to select the relevant set of alternates
  242. * from environment variable dfu_alt_info.
  243. *
  244. * If environment variable dfu_alt_info specifies the interface and the device,
  245. * use NULL for @interface and @devstr.
  246. *
  247. * See function :c:func:`dfu_free_entities`
  248. *
  249. * @interface: interface, e.g. "mmc" or "nand"
  250. * @devstr: device number as string
  251. * Return: 0 on success, a negative error code otherwise
  252. */
  253. int dfu_init_env_entities(char *interface, char *devstr);
  254. unsigned char *dfu_get_buf(struct dfu_entity *dfu);
  255. unsigned char *dfu_free_buf(void);
  256. unsigned long dfu_get_buf_size(void);
  257. bool dfu_usb_get_reset(void);
  258. #ifdef CONFIG_DFU_TIMEOUT
  259. unsigned long dfu_get_timeout(void);
  260. void dfu_set_timeout(unsigned long);
  261. #endif
  262. /**
  263. * dfu_read() - read from dfu entity
  264. *
  265. * The block sequence number @blk_seq_num is a 16 bit counter that must be
  266. * incremented with each call for the same dfu entity @de.
  267. *
  268. * @de: dfu entity
  269. * @buf: buffer
  270. * @size: size of buffer
  271. * @blk_seq_num: block sequence number
  272. * Return: 0 for success, -1 for error
  273. */
  274. int dfu_read(struct dfu_entity *de, void *buf, int size, int blk_seq_num);
  275. /**
  276. * dfu_write() - write to dfu entity
  277. *
  278. * Write the contents of a buffer @buf to the dfu entity @de. After writing
  279. * the last block call dfu_flush(). If a file is already loaded completely
  280. * into memory it is preferable to use dfu_write_from_mem_addr() which takes
  281. * care of blockwise transfer and flushing.
  282. *
  283. * The block sequence number @blk_seq_num is a 16 bit counter that must be
  284. * incremented with each call for the same dfu entity @de.
  285. *
  286. * See function :c:func:`dfu_flush`
  287. * See function :c:func:`dfu_write_from_mem_addr`
  288. *
  289. * @de: dfu entity
  290. * @buf: buffer
  291. * @size: size of buffer
  292. * @blk_seq_num: block sequence number
  293. * Return: 0 for success, -1 for error
  294. */
  295. int dfu_write(struct dfu_entity *de, void *buf, int size, int blk_seq_num);
  296. /**
  297. * dfu_flush() - flush to dfu entity
  298. *
  299. * This function has to be called after writing the last block to the dfu
  300. * entity @de.
  301. *
  302. * The block sequence number @blk_seq_num is a 16 bit counter that must be
  303. * incremented with each call for the same dfu entity @de.
  304. *
  305. * See function :c:func:`dfu_write`
  306. *
  307. * @de: dfu entity
  308. * @buf: ignored
  309. * @size: ignored
  310. * @blk_seq_num: block sequence number of last write - ignored
  311. * Return: 0 for success, -1 for error
  312. */
  313. int dfu_flush(struct dfu_entity *de, void *buf, int size, int blk_seq_num);
  314. /**
  315. * dfu_initiated_callback() - weak callback called on DFU transaction start
  316. *
  317. * It is a callback function called by DFU stack when a DFU transaction is
  318. * initiated. This function allows to manage some board specific behavior on
  319. * DFU targets.
  320. *
  321. * @dfu: pointer to the dfu_entity, which should be initialized
  322. */
  323. void dfu_initiated_callback(struct dfu_entity *dfu);
  324. /**
  325. * dfu_flush_callback() - weak callback called at the end of the DFU write
  326. *
  327. * It is a callback function called by DFU stack after DFU manifestation.
  328. * This function allows to manage some board specific behavior on DFU targets
  329. *
  330. * @dfu: pointer to the dfu_entity, which should be flushed
  331. */
  332. void dfu_flush_callback(struct dfu_entity *dfu);
  333. int dfu_transaction_initiate(struct dfu_entity *dfu, bool read);
  334. void dfu_transaction_cleanup(struct dfu_entity *dfu);
  335. /*
  336. * dfu_defer_flush - pointer to store dfu_entity for deferred flashing.
  337. * It should be NULL when not used.
  338. */
  339. extern struct dfu_entity *dfu_defer_flush;
  340. /**
  341. * dfu_get_defer_flush() - get current value of dfu_defer_flush pointer
  342. *
  343. * Return: value of the dfu_defer_flush pointer
  344. */
  345. static inline struct dfu_entity *dfu_get_defer_flush(void)
  346. {
  347. return dfu_defer_flush;
  348. }
  349. /**
  350. * dfu_set_defer_flush() - set the dfu_defer_flush pointer
  351. *
  352. * @dfu: pointer to the dfu_entity, which should be written
  353. */
  354. static inline void dfu_set_defer_flush(struct dfu_entity *dfu)
  355. {
  356. dfu_defer_flush = dfu;
  357. }
  358. /**
  359. * dfu_write_from_mem_addr() - write data from memory to DFU managed medium
  360. *
  361. * This function adds support for writing data starting from fixed memory
  362. * address (like $loadaddr) to dfu managed medium (e.g. NAND, MMC, file system)
  363. *
  364. * @dfu: dfu entity to which we want to store data
  365. * @buf: fixed memory address from where data starts
  366. * @size: number of bytes to write
  367. *
  368. * Return: 0 on success, other value on failure
  369. */
  370. int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size);
  371. /* Device specific */
  372. #if CONFIG_IS_ENABLED(DFU_MMC)
  373. extern int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char *s);
  374. #else
  375. static inline int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr,
  376. char *s)
  377. {
  378. puts("MMC support not available!\n");
  379. return -1;
  380. }
  381. #endif
  382. #if CONFIG_IS_ENABLED(DFU_NAND)
  383. extern int dfu_fill_entity_nand(struct dfu_entity *dfu, char *devstr, char *s);
  384. #else
  385. static inline int dfu_fill_entity_nand(struct dfu_entity *dfu, char *devstr,
  386. char *s)
  387. {
  388. puts("NAND support not available!\n");
  389. return -1;
  390. }
  391. #endif
  392. #if CONFIG_IS_ENABLED(DFU_RAM)
  393. extern int dfu_fill_entity_ram(struct dfu_entity *dfu, char *devstr, char *s);
  394. #else
  395. static inline int dfu_fill_entity_ram(struct dfu_entity *dfu, char *devstr,
  396. char *s)
  397. {
  398. puts("RAM support not available!\n");
  399. return -1;
  400. }
  401. #endif
  402. #if CONFIG_IS_ENABLED(DFU_SF)
  403. extern int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char *s);
  404. #else
  405. static inline int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr,
  406. char *s)
  407. {
  408. puts("SF support not available!\n");
  409. return -1;
  410. }
  411. #endif
  412. #if CONFIG_IS_ENABLED(DFU_MTD)
  413. int dfu_fill_entity_mtd(struct dfu_entity *dfu, char *devstr, char *s);
  414. #else
  415. static inline int dfu_fill_entity_mtd(struct dfu_entity *dfu, char *devstr,
  416. char *s)
  417. {
  418. puts("MTD support not available!\n");
  419. return -1;
  420. }
  421. #endif
  422. #ifdef CONFIG_DFU_VIRT
  423. int dfu_fill_entity_virt(struct dfu_entity *dfu, char *devstr, char *s);
  424. int dfu_write_medium_virt(struct dfu_entity *dfu, u64 offset,
  425. void *buf, long *len);
  426. int dfu_get_medium_size_virt(struct dfu_entity *dfu, u64 *size);
  427. int dfu_read_medium_virt(struct dfu_entity *dfu, u64 offset,
  428. void *buf, long *len);
  429. #else
  430. static inline int dfu_fill_entity_virt(struct dfu_entity *dfu, char *devstr,
  431. char *s)
  432. {
  433. puts("VIRT support not available!\n");
  434. return -1;
  435. }
  436. #endif
  437. #if CONFIG_IS_ENABLED(DFU_WRITE_ALT)
  438. /**
  439. * dfu_write_by_name() - write data to DFU medium
  440. * @dfu_entity_name: Name of DFU entity to write
  441. * @addr: Address of data buffer to write
  442. * @len: Number of bytes
  443. * @interface: Destination DFU medium (e.g. "mmc")
  444. * @devstring: Instance number of destination DFU medium (e.g. "1")
  445. *
  446. * This function is storing data received on DFU supported medium which
  447. * is specified by @dfu_entity_name.
  448. *
  449. * Return: 0 - on success, error code - otherwise
  450. */
  451. int dfu_write_by_name(char *dfu_entity_name, void *addr,
  452. unsigned int len, char *interface, char *devstring);
  453. /**
  454. * dfu_write_by_alt() - write data to DFU medium
  455. * @dfu_alt_num: DFU alt setting number
  456. * @addr: Address of data buffer to write
  457. * @len: Number of bytes
  458. * @interface: Destination DFU medium (e.g. "mmc")
  459. * @devstring: Instance number of destination DFU medium (e.g. "1")
  460. *
  461. * This function is storing data received on DFU supported medium which
  462. * is specified by @dfu_alt_name.
  463. *
  464. * Return: 0 - on success, error code - otherwise
  465. */
  466. int dfu_write_by_alt(int dfu_alt_num, void *addr, unsigned int len,
  467. char *interface, char *devstring);
  468. #else
  469. static inline int dfu_write_by_name(char *dfu_entity_name, void *addr,
  470. unsigned int len, char *interface,
  471. char *devstring)
  472. {
  473. puts("write support for DFU not available!\n");
  474. return -ENOSYS;
  475. }
  476. static inline int dfu_write_by_alt(int dfu_alt_num, void *addr,
  477. unsigned int len, char *interface,
  478. char *devstring)
  479. {
  480. puts("write support for DFU not available!\n");
  481. return -ENOSYS;
  482. }
  483. #endif
  484. int dfu_add(struct usb_configuration *c);
  485. #endif /* __DFU_ENTITY_H_ */