dfu.h 15 KB

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