blk.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2000-2004
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #ifndef BLK_H
  7. #define BLK_H
  8. #include <efi.h>
  9. #ifdef CONFIG_SYS_64BIT_LBA
  10. typedef uint64_t lbaint_t;
  11. #define LBAFlength "ll"
  12. #else
  13. typedef ulong lbaint_t;
  14. #define LBAFlength "l"
  15. #endif
  16. #define LBAF "%" LBAFlength "x"
  17. #define LBAFU "%" LBAFlength "u"
  18. struct udevice;
  19. /* Interface types: */
  20. enum if_type {
  21. IF_TYPE_UNKNOWN = 0,
  22. IF_TYPE_IDE,
  23. IF_TYPE_SCSI,
  24. IF_TYPE_ATAPI,
  25. IF_TYPE_USB,
  26. IF_TYPE_DOC,
  27. IF_TYPE_MMC,
  28. IF_TYPE_SD,
  29. IF_TYPE_SATA,
  30. IF_TYPE_HOST,
  31. IF_TYPE_NVME,
  32. IF_TYPE_EFI,
  33. IF_TYPE_PVBLOCK,
  34. IF_TYPE_VIRTIO,
  35. IF_TYPE_COUNT, /* Number of interface types */
  36. };
  37. #define BLK_VEN_SIZE 40
  38. #define BLK_PRD_SIZE 20
  39. #define BLK_REV_SIZE 8
  40. /*
  41. * Identifies the partition table type (ie. MBR vs GPT GUID) signature
  42. */
  43. enum sig_type {
  44. SIG_TYPE_NONE,
  45. SIG_TYPE_MBR,
  46. SIG_TYPE_GUID,
  47. SIG_TYPE_COUNT /* Number of signature types */
  48. };
  49. /*
  50. * With driver model (CONFIG_BLK) this is uclass platform data, accessible
  51. * with dev_get_uclass_plat(dev)
  52. */
  53. struct blk_desc {
  54. /*
  55. * TODO: With driver model we should be able to use the parent
  56. * device's uclass instead.
  57. */
  58. enum if_type if_type; /* type of the interface */
  59. int devnum; /* device number */
  60. unsigned char part_type; /* partition type */
  61. unsigned char target; /* target SCSI ID */
  62. unsigned char lun; /* target LUN */
  63. unsigned char hwpart; /* HW partition, e.g. for eMMC */
  64. unsigned char type; /* device type */
  65. unsigned char removable; /* removable device */
  66. #ifdef CONFIG_LBA48
  67. /* device can use 48bit addr (ATA/ATAPI v7) */
  68. unsigned char lba48;
  69. #endif
  70. lbaint_t lba; /* number of blocks */
  71. unsigned long blksz; /* block size */
  72. int log2blksz; /* for convenience: log2(blksz) */
  73. char vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
  74. char product[BLK_PRD_SIZE + 1]; /* device product number */
  75. char revision[BLK_REV_SIZE + 1]; /* firmware revision */
  76. enum sig_type sig_type; /* Partition table signature type */
  77. union {
  78. uint32_t mbr_sig; /* MBR integer signature */
  79. efi_guid_t guid_sig; /* GPT GUID Signature */
  80. };
  81. #if CONFIG_IS_ENABLED(BLK)
  82. /*
  83. * For now we have a few functions which take struct blk_desc as a
  84. * parameter. This field allows them to look up the associated
  85. * device. Once these functions are removed we can drop this field.
  86. */
  87. struct udevice *bdev;
  88. #else
  89. unsigned long (*block_read)(struct blk_desc *block_dev,
  90. lbaint_t start,
  91. lbaint_t blkcnt,
  92. void *buffer);
  93. unsigned long (*block_write)(struct blk_desc *block_dev,
  94. lbaint_t start,
  95. lbaint_t blkcnt,
  96. const void *buffer);
  97. unsigned long (*block_erase)(struct blk_desc *block_dev,
  98. lbaint_t start,
  99. lbaint_t blkcnt);
  100. void *priv; /* driver private struct pointer */
  101. #endif
  102. };
  103. #define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
  104. #define PAD_TO_BLOCKSIZE(size, blk_desc) \
  105. (PAD_SIZE(size, blk_desc->blksz))
  106. #if CONFIG_IS_ENABLED(BLOCK_CACHE)
  107. /**
  108. * blkcache_init() - initialize the block cache list pointers
  109. */
  110. int blkcache_init(void);
  111. /**
  112. * blkcache_read() - attempt to read a set of blocks from cache
  113. *
  114. * @param iftype - IF_TYPE_x for type of device
  115. * @param dev - device index of particular type
  116. * @param start - starting block number
  117. * @param blkcnt - number of blocks to read
  118. * @param blksz - size in bytes of each block
  119. * @param buf - buffer to contain cached data
  120. *
  121. * @return - 1 if block returned from cache, 0 otherwise.
  122. */
  123. int blkcache_read(int iftype, int dev,
  124. lbaint_t start, lbaint_t blkcnt,
  125. unsigned long blksz, void *buffer);
  126. /**
  127. * blkcache_fill() - make data read from a block device available
  128. * to the block cache
  129. *
  130. * @param iftype - IF_TYPE_x for type of device
  131. * @param dev - device index of particular type
  132. * @param start - starting block number
  133. * @param blkcnt - number of blocks available
  134. * @param blksz - size in bytes of each block
  135. * @param buf - buffer containing data to cache
  136. *
  137. */
  138. void blkcache_fill(int iftype, int dev,
  139. lbaint_t start, lbaint_t blkcnt,
  140. unsigned long blksz, void const *buffer);
  141. /**
  142. * blkcache_invalidate() - discard the cache for a set of blocks
  143. * because of a write or device (re)initialization.
  144. *
  145. * @param iftype - IF_TYPE_x for type of device
  146. * @param dev - device index of particular type
  147. */
  148. void blkcache_invalidate(int iftype, int dev);
  149. /**
  150. * blkcache_configure() - configure block cache
  151. *
  152. * @param blocks - maximum blocks per entry
  153. * @param entries - maximum entries in cache
  154. */
  155. void blkcache_configure(unsigned blocks, unsigned entries);
  156. /*
  157. * statistics of the block cache
  158. */
  159. struct block_cache_stats {
  160. unsigned hits;
  161. unsigned misses;
  162. unsigned entries; /* current entry count */
  163. unsigned max_blocks_per_entry;
  164. unsigned max_entries;
  165. };
  166. /**
  167. * get_blkcache_stats() - return statistics and reset
  168. *
  169. * @param stats - statistics are copied here
  170. */
  171. void blkcache_stats(struct block_cache_stats *stats);
  172. #else
  173. static inline int blkcache_read(int iftype, int dev,
  174. lbaint_t start, lbaint_t blkcnt,
  175. unsigned long blksz, void *buffer)
  176. {
  177. return 0;
  178. }
  179. static inline void blkcache_fill(int iftype, int dev,
  180. lbaint_t start, lbaint_t blkcnt,
  181. unsigned long blksz, void const *buffer) {}
  182. static inline void blkcache_invalidate(int iftype, int dev) {}
  183. #endif
  184. #if CONFIG_IS_ENABLED(BLK)
  185. struct udevice;
  186. /* Operations on block devices */
  187. struct blk_ops {
  188. /**
  189. * read() - read from a block device
  190. *
  191. * @dev: Device to read from
  192. * @start: Start block number to read (0=first)
  193. * @blkcnt: Number of blocks to read
  194. * @buffer: Destination buffer for data read
  195. * @return number of blocks read, or -ve error number (see the
  196. * IS_ERR_VALUE() macro
  197. */
  198. unsigned long (*read)(struct udevice *dev, lbaint_t start,
  199. lbaint_t blkcnt, void *buffer);
  200. /**
  201. * write() - write to a block device
  202. *
  203. * @dev: Device to write to
  204. * @start: Start block number to write (0=first)
  205. * @blkcnt: Number of blocks to write
  206. * @buffer: Source buffer for data to write
  207. * @return number of blocks written, or -ve error number (see the
  208. * IS_ERR_VALUE() macro
  209. */
  210. unsigned long (*write)(struct udevice *dev, lbaint_t start,
  211. lbaint_t blkcnt, const void *buffer);
  212. /**
  213. * erase() - erase a section of a block device
  214. *
  215. * @dev: Device to (partially) erase
  216. * @start: Start block number to erase (0=first)
  217. * @blkcnt: Number of blocks to erase
  218. * @return number of blocks erased, or -ve error number (see the
  219. * IS_ERR_VALUE() macro
  220. */
  221. unsigned long (*erase)(struct udevice *dev, lbaint_t start,
  222. lbaint_t blkcnt);
  223. /**
  224. * select_hwpart() - select a particular hardware partition
  225. *
  226. * Some devices (e.g. MMC) can support partitioning at the hardware
  227. * level. This is quite separate from the normal idea of
  228. * software-based partitions. MMC hardware partitions must be
  229. * explicitly selected. Once selected only the region of the device
  230. * covered by that partition is accessible.
  231. *
  232. * The MMC standard provides for two boot partitions (numbered 1 and 2),
  233. * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
  234. *
  235. * @desc: Block device to update
  236. * @hwpart: Hardware partition number to select. 0 means the raw
  237. * device, 1 is the first partition, 2 is the second, etc.
  238. * @return 0 if OK, -ve on error
  239. */
  240. int (*select_hwpart)(struct udevice *dev, int hwpart);
  241. };
  242. #define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
  243. /*
  244. * These functions should take struct udevice instead of struct blk_desc,
  245. * but this is convenient for migration to driver model. Add a 'd' prefix
  246. * to the function operations, so that blk_read(), etc. can be reserved for
  247. * functions with the correct arguments.
  248. */
  249. unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
  250. lbaint_t blkcnt, void *buffer);
  251. unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
  252. lbaint_t blkcnt, const void *buffer);
  253. unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
  254. lbaint_t blkcnt);
  255. /**
  256. * blk_find_device() - Find a block device
  257. *
  258. * This function does not activate the device. The device will be returned
  259. * whether or not it is activated.
  260. *
  261. * @if_type: Interface type (enum if_type_t)
  262. * @devnum: Device number (specific to each interface type)
  263. * @devp: the device, if found
  264. * @return 0 if found, -ENODEV if no device found, or other -ve error value
  265. */
  266. int blk_find_device(int if_type, int devnum, struct udevice **devp);
  267. /**
  268. * blk_get_device() - Find and probe a block device ready for use
  269. *
  270. * @if_type: Interface type (enum if_type_t)
  271. * @devnum: Device number (specific to each interface type)
  272. * @devp: the device, if found
  273. * @return 0 if found, -ENODEV if no device found, or other -ve error value
  274. */
  275. int blk_get_device(int if_type, int devnum, struct udevice **devp);
  276. /**
  277. * blk_first_device() - Find the first device for a given interface
  278. *
  279. * The device is probed ready for use
  280. *
  281. * @devnum: Device number (specific to each interface type)
  282. * @devp: the device, if found
  283. * @return 0 if found, -ENODEV if no device, or other -ve error value
  284. */
  285. int blk_first_device(int if_type, struct udevice **devp);
  286. /**
  287. * blk_next_device() - Find the next device for a given interface
  288. *
  289. * This can be called repeatedly after blk_first_device() to iterate through
  290. * all devices of the given interface type.
  291. *
  292. * The device is probed ready for use
  293. *
  294. * @devp: On entry, the previous device returned. On exit, the next
  295. * device, if found
  296. * @return 0 if found, -ENODEV if no device, or other -ve error value
  297. */
  298. int blk_next_device(struct udevice **devp);
  299. /**
  300. * blk_create_device() - Create a new block device
  301. *
  302. * @parent: Parent of the new device
  303. * @drv_name: Driver name to use for the block device
  304. * @name: Name for the device
  305. * @if_type: Interface type (enum if_type_t)
  306. * @devnum: Device number, specific to the interface type, or -1 to
  307. * allocate the next available number
  308. * @blksz: Block size of the device in bytes (typically 512)
  309. * @lba: Total number of blocks of the device
  310. * @devp: the new device (which has not been probed)
  311. */
  312. int blk_create_device(struct udevice *parent, const char *drv_name,
  313. const char *name, int if_type, int devnum, int blksz,
  314. lbaint_t lba, struct udevice **devp);
  315. /**
  316. * blk_create_devicef() - Create a new named block device
  317. *
  318. * @parent: Parent of the new device
  319. * @drv_name: Driver name to use for the block device
  320. * @name: Name for the device (parent name is prepended)
  321. * @if_type: Interface type (enum if_type_t)
  322. * @devnum: Device number, specific to the interface type, or -1 to
  323. * allocate the next available number
  324. * @blksz: Block size of the device in bytes (typically 512)
  325. * @lba: Total number of blocks of the device
  326. * @devp: the new device (which has not been probed)
  327. */
  328. int blk_create_devicef(struct udevice *parent, const char *drv_name,
  329. const char *name, int if_type, int devnum, int blksz,
  330. lbaint_t lba, struct udevice **devp);
  331. /**
  332. * blk_unbind_all() - Unbind all device of the given interface type
  333. *
  334. * The devices are removed and then unbound.
  335. *
  336. * @if_type: Interface type to unbind
  337. * @return 0 if OK, -ve on error
  338. */
  339. int blk_unbind_all(int if_type);
  340. /**
  341. * blk_find_max_devnum() - find the maximum device number for an interface type
  342. *
  343. * Finds the last allocated device number for an interface type @if_type. The
  344. * next number is safe to use for a newly allocated device.
  345. *
  346. * @if_type: Interface type to scan
  347. * @return maximum device number found, or -ENODEV if none, or other -ve on
  348. * error
  349. */
  350. int blk_find_max_devnum(enum if_type if_type);
  351. /**
  352. * blk_next_free_devnum() - get the next device number for an interface type
  353. *
  354. * Finds the next number that is safe to use for a newly allocated device for
  355. * an interface type @if_type.
  356. *
  357. * @if_type: Interface type to scan
  358. * @return next device number safe to use, or -ve on error
  359. */
  360. int blk_next_free_devnum(enum if_type if_type);
  361. /**
  362. * blk_select_hwpart() - select a hardware partition
  363. *
  364. * Select a hardware partition if the device supports it (typically MMC does)
  365. *
  366. * @dev: Device to update
  367. * @hwpart: Partition number to select
  368. * @return 0 if OK, -ve on error
  369. */
  370. int blk_select_hwpart(struct udevice *dev, int hwpart);
  371. /**
  372. * blk_get_from_parent() - obtain a block device by looking up its parent
  373. *
  374. * All devices with
  375. */
  376. int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
  377. /**
  378. * blk_get_by_device() - Get the block device descriptor for the given device
  379. * @dev: Instance of a storage device
  380. *
  381. * Return: With block device descriptor on success , NULL if there is no such
  382. * block device.
  383. */
  384. struct blk_desc *blk_get_by_device(struct udevice *dev);
  385. #else
  386. #include <errno.h>
  387. /*
  388. * These functions should take struct udevice instead of struct blk_desc,
  389. * but this is convenient for migration to driver model. Add a 'd' prefix
  390. * to the function operations, so that blk_read(), etc. can be reserved for
  391. * functions with the correct arguments.
  392. */
  393. static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
  394. lbaint_t blkcnt, void *buffer)
  395. {
  396. ulong blks_read;
  397. if (blkcache_read(block_dev->if_type, block_dev->devnum,
  398. start, blkcnt, block_dev->blksz, buffer))
  399. return blkcnt;
  400. /*
  401. * We could check if block_read is NULL and return -ENOSYS. But this
  402. * bloats the code slightly (cause some board to fail to build), and
  403. * it would be an error to try an operation that does not exist.
  404. */
  405. blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
  406. if (blks_read == blkcnt)
  407. blkcache_fill(block_dev->if_type, block_dev->devnum,
  408. start, blkcnt, block_dev->blksz, buffer);
  409. return blks_read;
  410. }
  411. static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
  412. lbaint_t blkcnt, const void *buffer)
  413. {
  414. blkcache_invalidate(block_dev->if_type, block_dev->devnum);
  415. return block_dev->block_write(block_dev, start, blkcnt, buffer);
  416. }
  417. static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
  418. lbaint_t blkcnt)
  419. {
  420. blkcache_invalidate(block_dev->if_type, block_dev->devnum);
  421. return block_dev->block_erase(block_dev, start, blkcnt);
  422. }
  423. /**
  424. * struct blk_driver - Driver for block interface types
  425. *
  426. * This provides access to the block devices for each interface type. One
  427. * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
  428. * type that is to be supported.
  429. *
  430. * @if_typename: Interface type name
  431. * @if_type: Interface type
  432. * @max_devs: Maximum number of devices supported
  433. * @desc: Pointer to list of devices for this interface type,
  434. * or NULL to use @get_dev() instead
  435. */
  436. struct blk_driver {
  437. const char *if_typename;
  438. enum if_type if_type;
  439. int max_devs;
  440. struct blk_desc *desc;
  441. /**
  442. * get_dev() - get a pointer to a block device given its number
  443. *
  444. * Each interface allocates its own devices and typically
  445. * struct blk_desc is contained with the interface's data structure.
  446. * There is no global numbering for block devices. This method allows
  447. * the device for an interface type to be obtained when @desc is NULL.
  448. *
  449. * @devnum: Device number (0 for first device on that interface,
  450. * 1 for second, etc.
  451. * @descp: Returns pointer to the block device on success
  452. * @return 0 if OK, -ve on error
  453. */
  454. int (*get_dev)(int devnum, struct blk_desc **descp);
  455. /**
  456. * select_hwpart() - Select a hardware partition
  457. *
  458. * Some devices (e.g. MMC) can support partitioning at the hardware
  459. * level. This is quite separate from the normal idea of
  460. * software-based partitions. MMC hardware partitions must be
  461. * explicitly selected. Once selected only the region of the device
  462. * covered by that partition is accessible.
  463. *
  464. * The MMC standard provides for two boot partitions (numbered 1 and 2),
  465. * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
  466. * Partition 0 is the main user-data partition.
  467. *
  468. * @desc: Block device descriptor
  469. * @hwpart: Hardware partition number to select. 0 means the main
  470. * user-data partition, 1 is the first partition, 2 is
  471. * the second, etc.
  472. * @return 0 if OK, other value for an error
  473. */
  474. int (*select_hwpart)(struct blk_desc *desc, int hwpart);
  475. };
  476. /*
  477. * Declare a new U-Boot legacy block driver. New drivers should use driver
  478. * model (UCLASS_BLK).
  479. */
  480. #define U_BOOT_LEGACY_BLK(__name) \
  481. ll_entry_declare(struct blk_driver, __name, blk_driver)
  482. struct blk_driver *blk_driver_lookup_type(int if_type);
  483. #endif /* !CONFIG_BLK */
  484. /**
  485. * blk_get_devnum_by_typename() - Get a block device by type and number
  486. *
  487. * This looks through the available block devices of the given type, returning
  488. * the one with the given @devnum.
  489. *
  490. * @if_type: Block device type
  491. * @devnum: Device number
  492. * @return point to block device descriptor, or NULL if not found
  493. */
  494. struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum);
  495. /**
  496. * blk_get_devnum_by_type() - Get a block device by type name, and number
  497. *
  498. * This looks up the block device type based on @if_typename, then calls
  499. * blk_get_devnum_by_type().
  500. *
  501. * @if_typename: Block device type name
  502. * @devnum: Device number
  503. * @return point to block device descriptor, or NULL if not found
  504. */
  505. struct blk_desc *blk_get_devnum_by_typename(const char *if_typename,
  506. int devnum);
  507. /**
  508. * blk_dselect_hwpart() - select a hardware partition
  509. *
  510. * This selects a hardware partition (such as is supported by MMC). The block
  511. * device size may change as this effectively points the block device to a
  512. * partition at the hardware level. See the select_hwpart() method above.
  513. *
  514. * @desc: Block device descriptor for the device to select
  515. * @hwpart: Partition number to select
  516. * @return 0 if OK, -ve on error
  517. */
  518. int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
  519. /**
  520. * blk_list_part() - list the partitions for block devices of a given type
  521. *
  522. * This looks up the partition type for each block device of type @if_type,
  523. * then displays a list of partitions.
  524. *
  525. * @if_type: Block device type
  526. * @return 0 if OK, -ENODEV if there is none of that type
  527. */
  528. int blk_list_part(enum if_type if_type);
  529. /**
  530. * blk_list_devices() - list the block devices of a given type
  531. *
  532. * This lists each block device of the type @if_type, showing the capacity
  533. * as well as type-specific information.
  534. *
  535. * @if_type: Block device type
  536. */
  537. void blk_list_devices(enum if_type if_type);
  538. /**
  539. * blk_show_device() - show information about a given block device
  540. *
  541. * This shows the block device capacity as well as type-specific information.
  542. *
  543. * @if_type: Block device type
  544. * @devnum: Device number
  545. * @return 0 if OK, -ENODEV for invalid device number
  546. */
  547. int blk_show_device(enum if_type if_type, int devnum);
  548. /**
  549. * blk_print_device_num() - show information about a given block device
  550. *
  551. * This is similar to blk_show_device() but returns an error if the block
  552. * device type is unknown.
  553. *
  554. * @if_type: Block device type
  555. * @devnum: Device number
  556. * @return 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
  557. * device is not connected
  558. */
  559. int blk_print_device_num(enum if_type if_type, int devnum);
  560. /**
  561. * blk_print_part_devnum() - print the partition information for a device
  562. *
  563. * @if_type: Block device type
  564. * @devnum: Device number
  565. * @return 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
  566. * the interface type is not supported, other -ve on other error
  567. */
  568. int blk_print_part_devnum(enum if_type if_type, int devnum);
  569. /**
  570. * blk_read_devnum() - read blocks from a device
  571. *
  572. * @if_type: Block device type
  573. * @devnum: Device number
  574. * @blkcnt: Number of blocks to read
  575. * @buffer: Address to write data to
  576. * @return number of blocks read, or -ve error number on error
  577. */
  578. ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
  579. lbaint_t blkcnt, void *buffer);
  580. /**
  581. * blk_write_devnum() - write blocks to a device
  582. *
  583. * @if_type: Block device type
  584. * @devnum: Device number
  585. * @blkcnt: Number of blocks to write
  586. * @buffer: Address to read data from
  587. * @return number of blocks written, or -ve error number on error
  588. */
  589. ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
  590. lbaint_t blkcnt, const void *buffer);
  591. /**
  592. * blk_select_hwpart_devnum() - select a hardware partition
  593. *
  594. * This is similar to blk_dselect_hwpart() but it looks up the interface and
  595. * device number.
  596. *
  597. * @if_type: Block device type
  598. * @devnum: Device number
  599. * @hwpart: Partition number to select
  600. * @return 0 if OK, -ve on error
  601. */
  602. int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart);
  603. /**
  604. * blk_get_if_type_name() - Get the name of an interface type
  605. *
  606. * @if_type: Interface type to check
  607. * @return name of interface, or NULL if none
  608. */
  609. const char *blk_get_if_type_name(enum if_type if_type);
  610. /**
  611. * blk_common_cmd() - handle common commands with block devices
  612. *
  613. * @args: Number of arguments to the command (argv[0] is the command itself)
  614. * @argv: Command arguments
  615. * @if_type: Interface type
  616. * @cur_devnump: Current device number for this interface type
  617. * @return 0 if OK, CMD_RET_ERROR on error
  618. */
  619. int blk_common_cmd(int argc, char *const argv[], enum if_type if_type,
  620. int *cur_devnump);
  621. enum blk_flag_t {
  622. BLKF_FIXED = 1 << 0,
  623. BLKF_REMOVABLE = 1 << 1,
  624. BLKF_BOTH = BLKF_FIXED | BLKF_REMOVABLE,
  625. };
  626. /**
  627. * blk_first_device_err() - Get the first block device
  628. *
  629. * The device returned is probed if necessary, and ready for use
  630. *
  631. * @flags: Indicates type of device to return
  632. * @devp: Returns pointer to the first device in that uclass, or NULL if none
  633. * @return 0 if found, -ENODEV if not found, other -ve on error
  634. */
  635. int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp);
  636. /**
  637. * blk_next_device_err() - Get the next block device
  638. *
  639. * The device returned is probed if necessary, and ready for use
  640. *
  641. * @flags: Indicates type of device to return
  642. * @devp: On entry, pointer to device to lookup. On exit, returns pointer
  643. * to the next device in the uclass if no error occurred, or -ENODEV if
  644. * there is no next device.
  645. * @return 0 if found, -ENODEV if not found, other -ve on error
  646. */
  647. int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp);
  648. /**
  649. * blk_foreach_probe() - Helper function to iteration through block devices
  650. *
  651. * This creates a for() loop which works through the available devices in
  652. * a uclass in order from start to end. Devices are probed if necessary,
  653. * and ready for use.
  654. *
  655. * @flags: Indicates type of device to return
  656. * @dev: struct udevice * to hold the current device. Set to NULL when there
  657. * are no more devices.
  658. */
  659. #define blk_foreach_probe(flags, pos) \
  660. for (int _ret = blk_first_device_err(flags, &(pos)); \
  661. !_ret && pos; \
  662. _ret = blk_next_device_err(flags, &(pos)))
  663. /**
  664. * blk_count_devices() - count the number of devices of a particular type
  665. *
  666. * @flags: Indicates type of device to find
  667. * @return number of devices matching those flags
  668. */
  669. int blk_count_devices(enum blk_flag_t flag);
  670. #endif