blob: dde2173257242f46bce15de0cbff697fb0a9b575 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass1a736612016-02-29 15:25:39 -07002/*
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass1a736612016-02-29 15:25:39 -07005 */
6
7#ifndef BLK_H
8#define BLK_H
9
Peter Jonesff98cb92017-09-13 18:05:25 -040010#include <efi.h>
11
Simon Glass1a736612016-02-29 15:25:39 -070012#ifdef CONFIG_SYS_64BIT_LBA
13typedef uint64_t lbaint_t;
14#define LBAFlength "ll"
15#else
16typedef ulong lbaint_t;
17#define LBAFlength "l"
18#endif
19#define LBAF "%" LBAFlength "x"
20#define LBAFU "%" LBAFlength "u"
21
Simon Glass96f37b02021-07-05 16:32:59 -060022struct udevice;
23
Simon Glass1a736612016-02-29 15:25:39 -070024/* Interface types: */
Simon Glass5ec4f1a2016-02-29 15:25:40 -070025enum if_type {
26 IF_TYPE_UNKNOWN = 0,
27 IF_TYPE_IDE,
28 IF_TYPE_SCSI,
29 IF_TYPE_ATAPI,
30 IF_TYPE_USB,
31 IF_TYPE_DOC,
32 IF_TYPE_MMC,
33 IF_TYPE_SD,
34 IF_TYPE_SATA,
35 IF_TYPE_HOST,
Zhikang Zhangffab6942017-08-03 02:30:56 -070036 IF_TYPE_NVME,
Simon Glass2abd8d12021-12-04 08:56:30 -070037 IF_TYPE_EFI_LOADER,
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +030038 IF_TYPE_PVBLOCK,
Tuomas Tynkkynen4ad54ec2018-10-15 02:21:10 -070039 IF_TYPE_VIRTIO,
Simon Glass42b7f422021-12-04 08:56:31 -070040 IF_TYPE_EFI_MEDIA,
Simon Glass5ec4f1a2016-02-29 15:25:40 -070041
42 IF_TYPE_COUNT, /* Number of interface types */
43};
Simon Glass1a736612016-02-29 15:25:39 -070044
Bin Mengeb81b1a2017-09-10 05:12:50 -070045#define BLK_VEN_SIZE 40
46#define BLK_PRD_SIZE 20
47#define BLK_REV_SIZE 8
48
Masahisa Kojimace3dbc52021-10-26 17:27:25 +090049#define PART_FORMAT_PCAT 0x1
50#define PART_FORMAT_GPT 0x2
51
Simon Glass09d71aa2016-02-29 15:25:55 -070052/*
Peter Jonesff98cb92017-09-13 18:05:25 -040053 * Identifies the partition table type (ie. MBR vs GPT GUID) signature
54 */
55enum sig_type {
56 SIG_TYPE_NONE,
57 SIG_TYPE_MBR,
58 SIG_TYPE_GUID,
59
60 SIG_TYPE_COUNT /* Number of signature types */
61};
62
63/*
Simon Glass09d71aa2016-02-29 15:25:55 -070064 * With driver model (CONFIG_BLK) this is uclass platform data, accessible
Simon Glasscaa4daa2020-12-03 16:55:18 -070065 * with dev_get_uclass_plat(dev)
Simon Glass09d71aa2016-02-29 15:25:55 -070066 */
Simon Glass1a736612016-02-29 15:25:39 -070067struct blk_desc {
Simon Glass09d71aa2016-02-29 15:25:55 -070068 /*
69 * TODO: With driver model we should be able to use the parent
70 * device's uclass instead.
71 */
Simon Glass5ec4f1a2016-02-29 15:25:40 -070072 enum if_type if_type; /* type of the interface */
Simon Glassbcce53d2016-02-29 15:25:51 -070073 int devnum; /* device number */
Simon Glass1a736612016-02-29 15:25:39 -070074 unsigned char part_type; /* partition type */
75 unsigned char target; /* target SCSI ID */
76 unsigned char lun; /* target LUN */
77 unsigned char hwpart; /* HW partition, e.g. for eMMC */
78 unsigned char type; /* device type */
79 unsigned char removable; /* removable device */
80#ifdef CONFIG_LBA48
81 /* device can use 48bit addr (ATA/ATAPI v7) */
82 unsigned char lba48;
83#endif
84 lbaint_t lba; /* number of blocks */
85 unsigned long blksz; /* block size */
86 int log2blksz; /* for convenience: log2(blksz) */
Bin Mengeb81b1a2017-09-10 05:12:50 -070087 char vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
88 char product[BLK_PRD_SIZE + 1]; /* device product number */
89 char revision[BLK_REV_SIZE + 1]; /* firmware revision */
Peter Jonesff98cb92017-09-13 18:05:25 -040090 enum sig_type sig_type; /* Partition table signature type */
91 union {
92 uint32_t mbr_sig; /* MBR integer signature */
93 efi_guid_t guid_sig; /* GPT GUID Signature */
94 };
Simon Glassc4d660d2017-07-04 13:31:19 -060095#if CONFIG_IS_ENABLED(BLK)
Simon Glassb6694a32016-05-01 13:52:33 -060096 /*
97 * For now we have a few functions which take struct blk_desc as a
98 * parameter. This field allows them to look up the associated
99 * device. Once these functions are removed we can drop this field.
100 */
Simon Glass09d71aa2016-02-29 15:25:55 -0700101 struct udevice *bdev;
102#else
Simon Glass1a736612016-02-29 15:25:39 -0700103 unsigned long (*block_read)(struct blk_desc *block_dev,
104 lbaint_t start,
105 lbaint_t blkcnt,
106 void *buffer);
107 unsigned long (*block_write)(struct blk_desc *block_dev,
108 lbaint_t start,
109 lbaint_t blkcnt,
110 const void *buffer);
111 unsigned long (*block_erase)(struct blk_desc *block_dev,
112 lbaint_t start,
113 lbaint_t blkcnt);
114 void *priv; /* driver private struct pointer */
Simon Glass09d71aa2016-02-29 15:25:55 -0700115#endif
Simon Glass1a736612016-02-29 15:25:39 -0700116};
117
118#define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
119#define PAD_TO_BLOCKSIZE(size, blk_desc) \
120 (PAD_SIZE(size, blk_desc->blksz))
121
Adam Ford6fef62c2018-06-11 17:17:48 -0500122#if CONFIG_IS_ENABLED(BLOCK_CACHE)
Angelo Durgehello1526bcc2020-01-21 10:37:27 +0100123
124/**
125 * blkcache_init() - initialize the block cache list pointers
126 */
127int blkcache_init(void);
128
Eric Nelsone40cf342016-03-28 10:05:44 -0700129/**
130 * blkcache_read() - attempt to read a set of blocks from cache
131 *
132 * @param iftype - IF_TYPE_x for type of device
133 * @param dev - device index of particular type
134 * @param start - starting block number
135 * @param blkcnt - number of blocks to read
136 * @param blksz - size in bytes of each block
137 * @param buf - buffer to contain cached data
138 *
Eric Nelson50fe8df2020-01-22 16:59:55 -0700139 * @return - 1 if block returned from cache, 0 otherwise.
Eric Nelsone40cf342016-03-28 10:05:44 -0700140 */
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700141int blkcache_read(int iftype, int dev,
142 lbaint_t start, lbaint_t blkcnt,
143 unsigned long blksz, void *buffer);
Eric Nelsone40cf342016-03-28 10:05:44 -0700144
145/**
146 * blkcache_fill() - make data read from a block device available
147 * to the block cache
148 *
149 * @param iftype - IF_TYPE_x for type of device
150 * @param dev - device index of particular type
151 * @param start - starting block number
152 * @param blkcnt - number of blocks available
153 * @param blksz - size in bytes of each block
154 * @param buf - buffer containing data to cache
155 *
156 */
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700157void blkcache_fill(int iftype, int dev,
158 lbaint_t start, lbaint_t blkcnt,
159 unsigned long blksz, void const *buffer);
Eric Nelsone40cf342016-03-28 10:05:44 -0700160
161/**
162 * blkcache_invalidate() - discard the cache for a set of blocks
163 * because of a write or device (re)initialization.
164 *
165 * @param iftype - IF_TYPE_x for type of device
166 * @param dev - device index of particular type
167 */
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700168void blkcache_invalidate(int iftype, int dev);
Eric Nelsone40cf342016-03-28 10:05:44 -0700169
170/**
171 * blkcache_configure() - configure block cache
172 *
173 * @param blocks - maximum blocks per entry
174 * @param entries - maximum entries in cache
175 */
176void blkcache_configure(unsigned blocks, unsigned entries);
177
178/*
179 * statistics of the block cache
180 */
181struct block_cache_stats {
182 unsigned hits;
183 unsigned misses;
184 unsigned entries; /* current entry count */
185 unsigned max_blocks_per_entry;
186 unsigned max_entries;
187};
188
189/**
190 * get_blkcache_stats() - return statistics and reset
191 *
192 * @param stats - statistics are copied here
193 */
194void blkcache_stats(struct block_cache_stats *stats);
195
196#else
197
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700198static inline int blkcache_read(int iftype, int dev,
199 lbaint_t start, lbaint_t blkcnt,
200 unsigned long blksz, void *buffer)
Eric Nelsone40cf342016-03-28 10:05:44 -0700201{
202 return 0;
203}
204
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700205static inline void blkcache_fill(int iftype, int dev,
206 lbaint_t start, lbaint_t blkcnt,
207 unsigned long blksz, void const *buffer) {}
Eric Nelsone40cf342016-03-28 10:05:44 -0700208
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700209static inline void blkcache_invalidate(int iftype, int dev) {}
Eric Nelsone40cf342016-03-28 10:05:44 -0700210
211#endif
212
Simon Glassc4d660d2017-07-04 13:31:19 -0600213#if CONFIG_IS_ENABLED(BLK)
Simon Glass09d71aa2016-02-29 15:25:55 -0700214struct udevice;
215
216/* Operations on block devices */
217struct blk_ops {
218 /**
219 * read() - read from a block device
220 *
221 * @dev: Device to read from
222 * @start: Start block number to read (0=first)
223 * @blkcnt: Number of blocks to read
224 * @buffer: Destination buffer for data read
225 * @return number of blocks read, or -ve error number (see the
226 * IS_ERR_VALUE() macro
227 */
228 unsigned long (*read)(struct udevice *dev, lbaint_t start,
229 lbaint_t blkcnt, void *buffer);
230
231 /**
232 * write() - write to a block device
233 *
234 * @dev: Device to write to
235 * @start: Start block number to write (0=first)
236 * @blkcnt: Number of blocks to write
237 * @buffer: Source buffer for data to write
238 * @return number of blocks written, or -ve error number (see the
239 * IS_ERR_VALUE() macro
240 */
241 unsigned long (*write)(struct udevice *dev, lbaint_t start,
242 lbaint_t blkcnt, const void *buffer);
243
244 /**
245 * erase() - erase a section of a block device
246 *
247 * @dev: Device to (partially) erase
248 * @start: Start block number to erase (0=first)
249 * @blkcnt: Number of blocks to erase
250 * @return number of blocks erased, or -ve error number (see the
251 * IS_ERR_VALUE() macro
252 */
253 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
254 lbaint_t blkcnt);
Simon Glasscd0fb552016-05-01 13:52:30 -0600255
256 /**
257 * select_hwpart() - select a particular hardware partition
258 *
259 * Some devices (e.g. MMC) can support partitioning at the hardware
260 * level. This is quite separate from the normal idea of
261 * software-based partitions. MMC hardware partitions must be
262 * explicitly selected. Once selected only the region of the device
263 * covered by that partition is accessible.
264 *
265 * The MMC standard provides for two boot partitions (numbered 1 and 2),
266 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
267 *
268 * @desc: Block device to update
269 * @hwpart: Hardware partition number to select. 0 means the raw
270 * device, 1 is the first partition, 2 is the second, etc.
271 * @return 0 if OK, -ve on error
272 */
273 int (*select_hwpart)(struct udevice *dev, int hwpart);
Simon Glass09d71aa2016-02-29 15:25:55 -0700274};
275
276#define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
277
278/*
279 * These functions should take struct udevice instead of struct blk_desc,
280 * but this is convenient for migration to driver model. Add a 'd' prefix
281 * to the function operations, so that blk_read(), etc. can be reserved for
282 * functions with the correct arguments.
283 */
284unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
285 lbaint_t blkcnt, void *buffer);
286unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
287 lbaint_t blkcnt, const void *buffer);
288unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
289 lbaint_t blkcnt);
290
291/**
Simon Glass61392812017-04-23 20:02:05 -0600292 * blk_find_device() - Find a block device
293 *
294 * This function does not activate the device. The device will be returned
295 * whether or not it is activated.
296 *
297 * @if_type: Interface type (enum if_type_t)
298 * @devnum: Device number (specific to each interface type)
299 * @devp: the device, if found
300 * @return 0 if found, -ENODEV if no device found, or other -ve error value
301 */
302int blk_find_device(int if_type, int devnum, struct udevice **devp);
303
304/**
Simon Glass09d71aa2016-02-29 15:25:55 -0700305 * blk_get_device() - Find and probe a block device ready for use
306 *
307 * @if_type: Interface type (enum if_type_t)
308 * @devnum: Device number (specific to each interface type)
309 * @devp: the device, if found
Simon Glass61392812017-04-23 20:02:05 -0600310 * @return 0 if found, -ENODEV if no device found, or other -ve error value
Simon Glass09d71aa2016-02-29 15:25:55 -0700311 */
312int blk_get_device(int if_type, int devnum, struct udevice **devp);
313
314/**
315 * blk_first_device() - Find the first device for a given interface
316 *
317 * The device is probed ready for use
318 *
319 * @devnum: Device number (specific to each interface type)
320 * @devp: the device, if found
321 * @return 0 if found, -ENODEV if no device, or other -ve error value
322 */
323int blk_first_device(int if_type, struct udevice **devp);
324
325/**
326 * blk_next_device() - Find the next device for a given interface
327 *
328 * This can be called repeatedly after blk_first_device() to iterate through
329 * all devices of the given interface type.
330 *
331 * The device is probed ready for use
332 *
333 * @devp: On entry, the previous device returned. On exit, the next
334 * device, if found
335 * @return 0 if found, -ENODEV if no device, or other -ve error value
336 */
337int blk_next_device(struct udevice **devp);
338
339/**
340 * blk_create_device() - Create a new block device
341 *
342 * @parent: Parent of the new device
343 * @drv_name: Driver name to use for the block device
344 * @name: Name for the device
345 * @if_type: Interface type (enum if_type_t)
Simon Glass52138fd2016-05-01 11:36:28 -0600346 * @devnum: Device number, specific to the interface type, or -1 to
347 * allocate the next available number
Simon Glass09d71aa2016-02-29 15:25:55 -0700348 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200349 * @lba: Total number of blocks of the device
Simon Glass09d71aa2016-02-29 15:25:55 -0700350 * @devp: the new device (which has not been probed)
351 */
352int blk_create_device(struct udevice *parent, const char *drv_name,
353 const char *name, int if_type, int devnum, int blksz,
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200354 lbaint_t lba, struct udevice **devp);
Simon Glass09d71aa2016-02-29 15:25:55 -0700355
356/**
Simon Glass9107c972016-05-01 11:36:29 -0600357 * blk_create_devicef() - Create a new named block device
358 *
359 * @parent: Parent of the new device
360 * @drv_name: Driver name to use for the block device
361 * @name: Name for the device (parent name is prepended)
362 * @if_type: Interface type (enum if_type_t)
363 * @devnum: Device number, specific to the interface type, or -1 to
364 * allocate the next available number
365 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200366 * @lba: Total number of blocks of the device
Simon Glass9107c972016-05-01 11:36:29 -0600367 * @devp: the new device (which has not been probed)
368 */
369int blk_create_devicef(struct udevice *parent, const char *drv_name,
370 const char *name, int if_type, int devnum, int blksz,
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200371 lbaint_t lba, struct udevice **devp);
Simon Glass9107c972016-05-01 11:36:29 -0600372
373/**
Simon Glass09d71aa2016-02-29 15:25:55 -0700374 * blk_unbind_all() - Unbind all device of the given interface type
375 *
376 * The devices are removed and then unbound.
377 *
378 * @if_type: Interface type to unbind
379 * @return 0 if OK, -ve on error
380 */
381int blk_unbind_all(int if_type);
382
Simon Glass52138fd2016-05-01 11:36:28 -0600383/**
384 * blk_find_max_devnum() - find the maximum device number for an interface type
385 *
386 * Finds the last allocated device number for an interface type @if_type. The
387 * next number is safe to use for a newly allocated device.
388 *
389 * @if_type: Interface type to scan
390 * @return maximum device number found, or -ENODEV if none, or other -ve on
391 * error
392 */
393int blk_find_max_devnum(enum if_type if_type);
394
Simon Glasscd0fb552016-05-01 13:52:30 -0600395/**
Bin Mengc879eeb2018-10-15 02:21:09 -0700396 * blk_next_free_devnum() - get the next device number for an interface type
397 *
398 * Finds the next number that is safe to use for a newly allocated device for
399 * an interface type @if_type.
400 *
401 * @if_type: Interface type to scan
402 * @return next device number safe to use, or -ve on error
403 */
404int blk_next_free_devnum(enum if_type if_type);
405
406/**
Simon Glasscd0fb552016-05-01 13:52:30 -0600407 * blk_select_hwpart() - select a hardware partition
408 *
409 * Select a hardware partition if the device supports it (typically MMC does)
410 *
411 * @dev: Device to update
412 * @hwpart: Partition number to select
413 * @return 0 if OK, -ve on error
414 */
415int blk_select_hwpart(struct udevice *dev, int hwpart);
416
Tom Rini4bdb49a2017-06-10 10:01:05 -0400417/**
418 * blk_get_from_parent() - obtain a block device by looking up its parent
419 *
420 * All devices with
421 */
422int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
423
Tien Fong Cheebc53d262018-07-06 16:26:36 +0800424/**
425 * blk_get_by_device() - Get the block device descriptor for the given device
426 * @dev: Instance of a storage device
427 *
428 * Return: With block device descriptor on success , NULL if there is no such
429 * block device.
430 */
431struct blk_desc *blk_get_by_device(struct udevice *dev);
432
Simon Glass09d71aa2016-02-29 15:25:55 -0700433#else
434#include <errno.h>
Simon Glass2a981dc2016-02-29 15:25:52 -0700435/*
436 * These functions should take struct udevice instead of struct blk_desc,
437 * but this is convenient for migration to driver model. Add a 'd' prefix
438 * to the function operations, so that blk_read(), etc. can be reserved for
439 * functions with the correct arguments.
440 */
441static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
442 lbaint_t blkcnt, void *buffer)
443{
Eric Nelsone40cf342016-03-28 10:05:44 -0700444 ulong blks_read;
445 if (blkcache_read(block_dev->if_type, block_dev->devnum,
446 start, blkcnt, block_dev->blksz, buffer))
447 return blkcnt;
448
Simon Glass2a981dc2016-02-29 15:25:52 -0700449 /*
450 * We could check if block_read is NULL and return -ENOSYS. But this
451 * bloats the code slightly (cause some board to fail to build), and
452 * it would be an error to try an operation that does not exist.
453 */
Eric Nelsone40cf342016-03-28 10:05:44 -0700454 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
455 if (blks_read == blkcnt)
456 blkcache_fill(block_dev->if_type, block_dev->devnum,
457 start, blkcnt, block_dev->blksz, buffer);
458
459 return blks_read;
Simon Glass2a981dc2016-02-29 15:25:52 -0700460}
461
462static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
463 lbaint_t blkcnt, const void *buffer)
464{
Eric Nelsone40cf342016-03-28 10:05:44 -0700465 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
Simon Glass2a981dc2016-02-29 15:25:52 -0700466 return block_dev->block_write(block_dev, start, blkcnt, buffer);
467}
468
469static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
470 lbaint_t blkcnt)
471{
Eric Nelsone40cf342016-03-28 10:05:44 -0700472 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
Simon Glass2a981dc2016-02-29 15:25:52 -0700473 return block_dev->block_erase(block_dev, start, blkcnt);
474}
Simon Glass6eef6ea2016-05-01 11:36:03 -0600475
476/**
477 * struct blk_driver - Driver for block interface types
478 *
479 * This provides access to the block devices for each interface type. One
480 * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
481 * type that is to be supported.
482 *
483 * @if_typename: Interface type name
484 * @if_type: Interface type
485 * @max_devs: Maximum number of devices supported
486 * @desc: Pointer to list of devices for this interface type,
487 * or NULL to use @get_dev() instead
488 */
489struct blk_driver {
490 const char *if_typename;
491 enum if_type if_type;
492 int max_devs;
493 struct blk_desc *desc;
494 /**
495 * get_dev() - get a pointer to a block device given its number
496 *
497 * Each interface allocates its own devices and typically
498 * struct blk_desc is contained with the interface's data structure.
499 * There is no global numbering for block devices. This method allows
500 * the device for an interface type to be obtained when @desc is NULL.
501 *
502 * @devnum: Device number (0 for first device on that interface,
503 * 1 for second, etc.
504 * @descp: Returns pointer to the block device on success
505 * @return 0 if OK, -ve on error
506 */
507 int (*get_dev)(int devnum, struct blk_desc **descp);
508
509 /**
510 * select_hwpart() - Select a hardware partition
511 *
512 * Some devices (e.g. MMC) can support partitioning at the hardware
513 * level. This is quite separate from the normal idea of
514 * software-based partitions. MMC hardware partitions must be
515 * explicitly selected. Once selected only the region of the device
516 * covered by that partition is accessible.
517 *
518 * The MMC standard provides for two boot partitions (numbered 1 and 2),
519 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
520 * Partition 0 is the main user-data partition.
521 *
522 * @desc: Block device descriptor
523 * @hwpart: Hardware partition number to select. 0 means the main
524 * user-data partition, 1 is the first partition, 2 is
525 * the second, etc.
526 * @return 0 if OK, other value for an error
527 */
528 int (*select_hwpart)(struct blk_desc *desc, int hwpart);
529};
530
531/*
532 * Declare a new U-Boot legacy block driver. New drivers should use driver
533 * model (UCLASS_BLK).
534 */
535#define U_BOOT_LEGACY_BLK(__name) \
536 ll_entry_declare(struct blk_driver, __name, blk_driver)
537
538struct blk_driver *blk_driver_lookup_type(int if_type);
539
Simon Glass09d71aa2016-02-29 15:25:55 -0700540#endif /* !CONFIG_BLK */
Simon Glass2a981dc2016-02-29 15:25:52 -0700541
Simon Glass6eef6ea2016-05-01 11:36:03 -0600542/**
543 * blk_get_devnum_by_typename() - Get a block device by type and number
544 *
545 * This looks through the available block devices of the given type, returning
546 * the one with the given @devnum.
547 *
548 * @if_type: Block device type
549 * @devnum: Device number
550 * @return point to block device descriptor, or NULL if not found
551 */
552struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum);
553
554/**
555 * blk_get_devnum_by_type() - Get a block device by type name, and number
556 *
557 * This looks up the block device type based on @if_typename, then calls
558 * blk_get_devnum_by_type().
559 *
560 * @if_typename: Block device type name
561 * @devnum: Device number
562 * @return point to block device descriptor, or NULL if not found
563 */
564struct blk_desc *blk_get_devnum_by_typename(const char *if_typename,
565 int devnum);
566
567/**
568 * blk_dselect_hwpart() - select a hardware partition
569 *
570 * This selects a hardware partition (such as is supported by MMC). The block
571 * device size may change as this effectively points the block device to a
572 * partition at the hardware level. See the select_hwpart() method above.
573 *
574 * @desc: Block device descriptor for the device to select
575 * @hwpart: Partition number to select
576 * @return 0 if OK, -ve on error
577 */
578int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
579
580/**
581 * blk_list_part() - list the partitions for block devices of a given type
582 *
583 * This looks up the partition type for each block device of type @if_type,
584 * then displays a list of partitions.
585 *
586 * @if_type: Block device type
587 * @return 0 if OK, -ENODEV if there is none of that type
588 */
589int blk_list_part(enum if_type if_type);
590
591/**
592 * blk_list_devices() - list the block devices of a given type
593 *
594 * This lists each block device of the type @if_type, showing the capacity
595 * as well as type-specific information.
596 *
597 * @if_type: Block device type
598 */
599void blk_list_devices(enum if_type if_type);
600
601/**
602 * blk_show_device() - show information about a given block device
603 *
604 * This shows the block device capacity as well as type-specific information.
605 *
606 * @if_type: Block device type
607 * @devnum: Device number
608 * @return 0 if OK, -ENODEV for invalid device number
609 */
610int blk_show_device(enum if_type if_type, int devnum);
611
612/**
613 * blk_print_device_num() - show information about a given block device
614 *
615 * This is similar to blk_show_device() but returns an error if the block
616 * device type is unknown.
617 *
618 * @if_type: Block device type
619 * @devnum: Device number
620 * @return 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
621 * device is not connected
622 */
623int blk_print_device_num(enum if_type if_type, int devnum);
624
625/**
626 * blk_print_part_devnum() - print the partition information for a device
627 *
628 * @if_type: Block device type
629 * @devnum: Device number
630 * @return 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
631 * the interface type is not supported, other -ve on other error
632 */
633int blk_print_part_devnum(enum if_type if_type, int devnum);
634
635/**
636 * blk_read_devnum() - read blocks from a device
637 *
638 * @if_type: Block device type
639 * @devnum: Device number
640 * @blkcnt: Number of blocks to read
641 * @buffer: Address to write data to
642 * @return number of blocks read, or -ve error number on error
643 */
644ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
645 lbaint_t blkcnt, void *buffer);
646
647/**
648 * blk_write_devnum() - write blocks to a device
649 *
650 * @if_type: Block device type
651 * @devnum: Device number
652 * @blkcnt: Number of blocks to write
653 * @buffer: Address to read data from
654 * @return number of blocks written, or -ve error number on error
655 */
656ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
657 lbaint_t blkcnt, const void *buffer);
658
659/**
660 * blk_select_hwpart_devnum() - select a hardware partition
661 *
662 * This is similar to blk_dselect_hwpart() but it looks up the interface and
663 * device number.
664 *
665 * @if_type: Block device type
666 * @devnum: Device number
667 * @hwpart: Partition number to select
668 * @return 0 if OK, -ve on error
669 */
670int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart);
671
Simon Glass6faa4ed2017-07-29 11:34:53 -0600672/**
673 * blk_get_if_type_name() - Get the name of an interface type
674 *
675 * @if_type: Interface type to check
676 * @return name of interface, or NULL if none
677 */
678const char *blk_get_if_type_name(enum if_type if_type);
679
Simon Glass4395f662017-07-29 11:34:54 -0600680/**
681 * blk_common_cmd() - handle common commands with block devices
682 *
683 * @args: Number of arguments to the command (argv[0] is the command itself)
684 * @argv: Command arguments
685 * @if_type: Interface type
686 * @cur_devnump: Current device number for this interface type
687 * @return 0 if OK, CMD_RET_ERROR on error
688 */
Simon Glass09140112020-05-10 11:40:03 -0600689int blk_common_cmd(int argc, char *const argv[], enum if_type if_type,
Simon Glass4395f662017-07-29 11:34:54 -0600690 int *cur_devnump);
691
Simon Glass96f37b02021-07-05 16:32:59 -0600692enum blk_flag_t {
693 BLKF_FIXED = 1 << 0,
694 BLKF_REMOVABLE = 1 << 1,
695 BLKF_BOTH = BLKF_FIXED | BLKF_REMOVABLE,
696};
697
698/**
699 * blk_first_device_err() - Get the first block device
700 *
701 * The device returned is probed if necessary, and ready for use
702 *
703 * @flags: Indicates type of device to return
704 * @devp: Returns pointer to the first device in that uclass, or NULL if none
705 * @return 0 if found, -ENODEV if not found, other -ve on error
706 */
707int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp);
708
709/**
710 * blk_next_device_err() - Get the next block device
711 *
712 * The device returned is probed if necessary, and ready for use
713 *
714 * @flags: Indicates type of device to return
715 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
716 * to the next device in the uclass if no error occurred, or -ENODEV if
717 * there is no next device.
718 * @return 0 if found, -ENODEV if not found, other -ve on error
719 */
720int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp);
721
722/**
723 * blk_foreach_probe() - Helper function to iteration through block devices
724 *
725 * This creates a for() loop which works through the available devices in
726 * a uclass in order from start to end. Devices are probed if necessary,
727 * and ready for use.
728 *
729 * @flags: Indicates type of device to return
730 * @dev: struct udevice * to hold the current device. Set to NULL when there
731 * are no more devices.
732 */
733#define blk_foreach_probe(flags, pos) \
734 for (int _ret = blk_first_device_err(flags, &(pos)); \
735 !_ret && pos; \
736 _ret = blk_next_device_err(flags, &(pos)))
737
738/**
739 * blk_count_devices() - count the number of devices of a particular type
740 *
741 * @flags: Indicates type of device to find
742 * @return number of devices matching those flags
743 */
744int blk_count_devices(enum blk_flag_t flag);
745
Simon Glass1a736612016-02-29 15:25:39 -0700746#endif