blk.h 23 KB

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