dfu.h 14 KB

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