1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
12 #ifdef CONFIG_SYS_64BIT_LBA
13 typedef uint64_t lbaint_t;
14 #define LBAFlength "ll"
16 typedef ulong lbaint_t;
17 #define LBAFlength "l"
19 #define LBAF "%" LBAFlength "x"
20 #define LBAFU "%" LBAFlength "u"
22 /* Interface types: */
37 IF_TYPE_COUNT, /* Number of interface types */
40 #define BLK_VEN_SIZE 40
41 #define BLK_PRD_SIZE 20
42 #define BLK_REV_SIZE 8
45 * Identifies the partition table type (ie. MBR vs GPT GUID) signature
52 SIG_TYPE_COUNT /* Number of signature types */
56 * With driver model (CONFIG_BLK) this is uclass platform data, accessible
57 * with dev_get_uclass_platdata(dev)
61 * TODO: With driver model we should be able to use the parent
62 * device's uclass instead.
64 enum if_type if_type; /* type of the interface */
65 int devnum; /* device number */
66 unsigned char part_type; /* partition type */
67 unsigned char target; /* target SCSI ID */
68 unsigned char lun; /* target LUN */
69 unsigned char hwpart; /* HW partition, e.g. for eMMC */
70 unsigned char type; /* device type */
71 unsigned char removable; /* removable device */
73 /* device can use 48bit addr (ATA/ATAPI v7) */
76 lbaint_t lba; /* number of blocks */
77 unsigned long blksz; /* block size */
78 int log2blksz; /* for convenience: log2(blksz) */
79 char vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
80 char product[BLK_PRD_SIZE + 1]; /* device product number */
81 char revision[BLK_REV_SIZE + 1]; /* firmware revision */
82 enum sig_type sig_type; /* Partition table signature type */
84 uint32_t mbr_sig; /* MBR integer signature */
85 efi_guid_t guid_sig; /* GPT GUID Signature */
87 #if CONFIG_IS_ENABLED(BLK)
89 * For now we have a few functions which take struct blk_desc as a
90 * parameter. This field allows them to look up the associated
91 * device. Once these functions are removed we can drop this field.
95 unsigned long (*block_read)(struct blk_desc *block_dev,
99 unsigned long (*block_write)(struct blk_desc *block_dev,
103 unsigned long (*block_erase)(struct blk_desc *block_dev,
106 void *priv; /* driver private struct pointer */
110 #define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
111 #define PAD_TO_BLOCKSIZE(size, blk_desc) \
112 (PAD_SIZE(size, blk_desc->blksz))
114 #if CONFIG_IS_ENABLED(BLOCK_CACHE)
116 * blkcache_read() - attempt to read a set of blocks from cache
118 * @param iftype - IF_TYPE_x for type of device
119 * @param dev - device index of particular type
120 * @param start - starting block number
121 * @param blkcnt - number of blocks to read
122 * @param blksz - size in bytes of each block
123 * @param buf - buffer to contain cached data
125 * @return - '1' if block returned from cache, '0' otherwise.
127 int blkcache_read(int iftype, int dev,
128 lbaint_t start, lbaint_t blkcnt,
129 unsigned long blksz, void *buffer);
132 * blkcache_fill() - make data read from a block device available
135 * @param iftype - IF_TYPE_x for type of device
136 * @param dev - device index of particular type
137 * @param start - starting block number
138 * @param blkcnt - number of blocks available
139 * @param blksz - size in bytes of each block
140 * @param buf - buffer containing data to cache
143 void blkcache_fill(int iftype, int dev,
144 lbaint_t start, lbaint_t blkcnt,
145 unsigned long blksz, void const *buffer);
148 * blkcache_invalidate() - discard the cache for a set of blocks
149 * because of a write or device (re)initialization.
151 * @param iftype - IF_TYPE_x for type of device
152 * @param dev - device index of particular type
154 void blkcache_invalidate(int iftype, int dev);
157 * blkcache_configure() - configure block cache
159 * @param blocks - maximum blocks per entry
160 * @param entries - maximum entries in cache
162 void blkcache_configure(unsigned blocks, unsigned entries);
165 * statistics of the block cache
167 struct block_cache_stats {
170 unsigned entries; /* current entry count */
171 unsigned max_blocks_per_entry;
172 unsigned max_entries;
176 * get_blkcache_stats() - return statistics and reset
178 * @param stats - statistics are copied here
180 void blkcache_stats(struct block_cache_stats *stats);
184 static inline int blkcache_read(int iftype, int dev,
185 lbaint_t start, lbaint_t blkcnt,
186 unsigned long blksz, void *buffer)
191 static inline void blkcache_fill(int iftype, int dev,
192 lbaint_t start, lbaint_t blkcnt,
193 unsigned long blksz, void const *buffer) {}
195 static inline void blkcache_invalidate(int iftype, int dev) {}
199 #if CONFIG_IS_ENABLED(BLK)
202 /* Operations on block devices */
205 * read() - read from a block device
207 * @dev: Device to read from
208 * @start: Start block number to read (0=first)
209 * @blkcnt: Number of blocks to read
210 * @buffer: Destination buffer for data read
211 * @return number of blocks read, or -ve error number (see the
212 * IS_ERR_VALUE() macro
214 unsigned long (*read)(struct udevice *dev, lbaint_t start,
215 lbaint_t blkcnt, void *buffer);
218 * write() - write to a block device
220 * @dev: Device to write to
221 * @start: Start block number to write (0=first)
222 * @blkcnt: Number of blocks to write
223 * @buffer: Source buffer for data to write
224 * @return number of blocks written, or -ve error number (see the
225 * IS_ERR_VALUE() macro
227 unsigned long (*write)(struct udevice *dev, lbaint_t start,
228 lbaint_t blkcnt, const void *buffer);
231 * erase() - erase a section of a block device
233 * @dev: Device to (partially) erase
234 * @start: Start block number to erase (0=first)
235 * @blkcnt: Number of blocks to erase
236 * @return number of blocks erased, or -ve error number (see the
237 * IS_ERR_VALUE() macro
239 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
243 * select_hwpart() - select a particular hardware partition
245 * Some devices (e.g. MMC) can support partitioning at the hardware
246 * level. This is quite separate from the normal idea of
247 * software-based partitions. MMC hardware partitions must be
248 * explicitly selected. Once selected only the region of the device
249 * covered by that partition is accessible.
251 * The MMC standard provides for two boot partitions (numbered 1 and 2),
252 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
254 * @desc: Block device to update
255 * @hwpart: Hardware partition number to select. 0 means the raw
256 * device, 1 is the first partition, 2 is the second, etc.
257 * @return 0 if OK, -ve on error
259 int (*select_hwpart)(struct udevice *dev, int hwpart);
262 #define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
265 * These functions should take struct udevice instead of struct blk_desc,
266 * but this is convenient for migration to driver model. Add a 'd' prefix
267 * to the function operations, so that blk_read(), etc. can be reserved for
268 * functions with the correct arguments.
270 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
271 lbaint_t blkcnt, void *buffer);
272 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
273 lbaint_t blkcnt, const void *buffer);
274 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
278 * blk_find_device() - Find a block device
280 * This function does not activate the device. The device will be returned
281 * whether or not it is activated.
283 * @if_type: Interface type (enum if_type_t)
284 * @devnum: Device number (specific to each interface type)
285 * @devp: the device, if found
286 * @return 0 if found, -ENODEV if no device found, or other -ve error value
288 int blk_find_device(int if_type, int devnum, struct udevice **devp);
291 * blk_get_device() - Find and probe a block device ready for use
293 * @if_type: Interface type (enum if_type_t)
294 * @devnum: Device number (specific to each interface type)
295 * @devp: the device, if found
296 * @return 0 if found, -ENODEV if no device found, or other -ve error value
298 int blk_get_device(int if_type, int devnum, struct udevice **devp);
301 * blk_first_device() - Find the first device for a given interface
303 * The device is probed ready for use
305 * @devnum: Device number (specific to each interface type)
306 * @devp: the device, if found
307 * @return 0 if found, -ENODEV if no device, or other -ve error value
309 int blk_first_device(int if_type, struct udevice **devp);
312 * blk_next_device() - Find the next device for a given interface
314 * This can be called repeatedly after blk_first_device() to iterate through
315 * all devices of the given interface type.
317 * The device is probed ready for use
319 * @devp: On entry, the previous device returned. On exit, the next
321 * @return 0 if found, -ENODEV if no device, or other -ve error value
323 int blk_next_device(struct udevice **devp);
326 * blk_create_device() - Create a new block device
328 * @parent: Parent of the new device
329 * @drv_name: Driver name to use for the block device
330 * @name: Name for the device
331 * @if_type: Interface type (enum if_type_t)
332 * @devnum: Device number, specific to the interface type, or -1 to
333 * allocate the next available number
334 * @blksz: Block size of the device in bytes (typically 512)
335 * @lba: Total number of blocks of the device
336 * @devp: the new device (which has not been probed)
338 int blk_create_device(struct udevice *parent, const char *drv_name,
339 const char *name, int if_type, int devnum, int blksz,
340 lbaint_t lba, struct udevice **devp);
343 * blk_create_devicef() - Create a new named block device
345 * @parent: Parent of the new device
346 * @drv_name: Driver name to use for the block device
347 * @name: Name for the device (parent name is prepended)
348 * @if_type: Interface type (enum if_type_t)
349 * @devnum: Device number, specific to the interface type, or -1 to
350 * allocate the next available number
351 * @blksz: Block size of the device in bytes (typically 512)
352 * @lba: Total number of blocks of the device
353 * @devp: the new device (which has not been probed)
355 int blk_create_devicef(struct udevice *parent, const char *drv_name,
356 const char *name, int if_type, int devnum, int blksz,
357 lbaint_t lba, struct udevice **devp);
360 * blk_prepare_device() - Prepare a block device for use
362 * This reads partition information from the device if supported.
364 * @dev: Device to prepare
365 * @return 0 if ok, -ve on error
367 int blk_prepare_device(struct udevice *dev);
370 * blk_unbind_all() - Unbind all device of the given interface type
372 * The devices are removed and then unbound.
374 * @if_type: Interface type to unbind
375 * @return 0 if OK, -ve on error
377 int blk_unbind_all(int if_type);
380 * blk_find_max_devnum() - find the maximum device number for an interface type
382 * Finds the last allocated device number for an interface type @if_type. The
383 * next number is safe to use for a newly allocated device.
385 * @if_type: Interface type to scan
386 * @return maximum device number found, or -ENODEV if none, or other -ve on
389 int blk_find_max_devnum(enum if_type if_type);
392 * blk_select_hwpart() - select a hardware partition
394 * Select a hardware partition if the device supports it (typically MMC does)
396 * @dev: Device to update
397 * @hwpart: Partition number to select
398 * @return 0 if OK, -ve on error
400 int blk_select_hwpart(struct udevice *dev, int hwpart);
403 * blk_get_from_parent() - obtain a block device by looking up its parent
407 int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
410 * blk_get_by_device() - Get the block device descriptor for the given device
411 * @dev: Instance of a storage device
413 * Return: With block device descriptor on success , NULL if there is no such
416 struct blk_desc *blk_get_by_device(struct udevice *dev);
421 * These functions should take struct udevice instead of struct blk_desc,
422 * but this is convenient for migration to driver model. Add a 'd' prefix
423 * to the function operations, so that blk_read(), etc. can be reserved for
424 * functions with the correct arguments.
426 static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
427 lbaint_t blkcnt, void *buffer)
430 if (blkcache_read(block_dev->if_type, block_dev->devnum,
431 start, blkcnt, block_dev->blksz, buffer))
435 * We could check if block_read is NULL and return -ENOSYS. But this
436 * bloats the code slightly (cause some board to fail to build), and
437 * it would be an error to try an operation that does not exist.
439 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
440 if (blks_read == blkcnt)
441 blkcache_fill(block_dev->if_type, block_dev->devnum,
442 start, blkcnt, block_dev->blksz, buffer);
447 static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
448 lbaint_t blkcnt, const void *buffer)
450 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
451 return block_dev->block_write(block_dev, start, blkcnt, buffer);
454 static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
457 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
458 return block_dev->block_erase(block_dev, start, blkcnt);
462 * struct blk_driver - Driver for block interface types
464 * This provides access to the block devices for each interface type. One
465 * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
466 * type that is to be supported.
468 * @if_typename: Interface type name
469 * @if_type: Interface type
470 * @max_devs: Maximum number of devices supported
471 * @desc: Pointer to list of devices for this interface type,
472 * or NULL to use @get_dev() instead
475 const char *if_typename;
476 enum if_type if_type;
478 struct blk_desc *desc;
480 * get_dev() - get a pointer to a block device given its number
482 * Each interface allocates its own devices and typically
483 * struct blk_desc is contained with the interface's data structure.
484 * There is no global numbering for block devices. This method allows
485 * the device for an interface type to be obtained when @desc is NULL.
487 * @devnum: Device number (0 for first device on that interface,
489 * @descp: Returns pointer to the block device on success
490 * @return 0 if OK, -ve on error
492 int (*get_dev)(int devnum, struct blk_desc **descp);
495 * select_hwpart() - Select a hardware partition
497 * Some devices (e.g. MMC) can support partitioning at the hardware
498 * level. This is quite separate from the normal idea of
499 * software-based partitions. MMC hardware partitions must be
500 * explicitly selected. Once selected only the region of the device
501 * covered by that partition is accessible.
503 * The MMC standard provides for two boot partitions (numbered 1 and 2),
504 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
505 * Partition 0 is the main user-data partition.
507 * @desc: Block device descriptor
508 * @hwpart: Hardware partition number to select. 0 means the main
509 * user-data partition, 1 is the first partition, 2 is
511 * @return 0 if OK, other value for an error
513 int (*select_hwpart)(struct blk_desc *desc, int hwpart);
517 * Declare a new U-Boot legacy block driver. New drivers should use driver
518 * model (UCLASS_BLK).
520 #define U_BOOT_LEGACY_BLK(__name) \
521 ll_entry_declare(struct blk_driver, __name, blk_driver)
523 struct blk_driver *blk_driver_lookup_type(int if_type);
525 #endif /* !CONFIG_BLK */
528 * blk_get_devnum_by_typename() - Get a block device by type and number
530 * This looks through the available block devices of the given type, returning
531 * the one with the given @devnum.
533 * @if_type: Block device type
534 * @devnum: Device number
535 * @return point to block device descriptor, or NULL if not found
537 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum);
540 * blk_get_devnum_by_type() - Get a block device by type name, and number
542 * This looks up the block device type based on @if_typename, then calls
543 * blk_get_devnum_by_type().
545 * @if_typename: Block device type name
546 * @devnum: Device number
547 * @return point to block device descriptor, or NULL if not found
549 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename,
553 * blk_dselect_hwpart() - select a hardware partition
555 * This selects a hardware partition (such as is supported by MMC). The block
556 * device size may change as this effectively points the block device to a
557 * partition at the hardware level. See the select_hwpart() method above.
559 * @desc: Block device descriptor for the device to select
560 * @hwpart: Partition number to select
561 * @return 0 if OK, -ve on error
563 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
566 * blk_list_part() - list the partitions for block devices of a given type
568 * This looks up the partition type for each block device of type @if_type,
569 * then displays a list of partitions.
571 * @if_type: Block device type
572 * @return 0 if OK, -ENODEV if there is none of that type
574 int blk_list_part(enum if_type if_type);
577 * blk_list_devices() - list the block devices of a given type
579 * This lists each block device of the type @if_type, showing the capacity
580 * as well as type-specific information.
582 * @if_type: Block device type
584 void blk_list_devices(enum if_type if_type);
587 * blk_show_device() - show information about a given block device
589 * This shows the block device capacity as well as type-specific information.
591 * @if_type: Block device type
592 * @devnum: Device number
593 * @return 0 if OK, -ENODEV for invalid device number
595 int blk_show_device(enum if_type if_type, int devnum);
598 * blk_print_device_num() - show information about a given block device
600 * This is similar to blk_show_device() but returns an error if the block
601 * device type is unknown.
603 * @if_type: Block device type
604 * @devnum: Device number
605 * @return 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
606 * device is not connected
608 int blk_print_device_num(enum if_type if_type, int devnum);
611 * blk_print_part_devnum() - print the partition information for a device
613 * @if_type: Block device type
614 * @devnum: Device number
615 * @return 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
616 * the interface type is not supported, other -ve on other error
618 int blk_print_part_devnum(enum if_type if_type, int devnum);
621 * blk_read_devnum() - read blocks from a device
623 * @if_type: Block device type
624 * @devnum: Device number
625 * @blkcnt: Number of blocks to read
626 * @buffer: Address to write data to
627 * @return number of blocks read, or -ve error number on error
629 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
630 lbaint_t blkcnt, void *buffer);
633 * blk_write_devnum() - write blocks to a device
635 * @if_type: Block device type
636 * @devnum: Device number
637 * @blkcnt: Number of blocks to write
638 * @buffer: Address to read data from
639 * @return number of blocks written, or -ve error number on error
641 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
642 lbaint_t blkcnt, const void *buffer);
645 * blk_select_hwpart_devnum() - select a hardware partition
647 * This is similar to blk_dselect_hwpart() but it looks up the interface and
650 * @if_type: Block device type
651 * @devnum: Device number
652 * @hwpart: Partition number to select
653 * @return 0 if OK, -ve on error
655 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart);
658 * blk_get_if_type_name() - Get the name of an interface type
660 * @if_type: Interface type to check
661 * @return name of interface, or NULL if none
663 const char *blk_get_if_type_name(enum if_type if_type);
666 * blk_common_cmd() - handle common commands with block devices
668 * @args: Number of arguments to the command (argv[0] is the command itself)
669 * @argv: Command arguments
670 * @if_type: Interface type
671 * @cur_devnump: Current device number for this interface type
672 * @return 0 if OK, CMD_RET_ERROR on error
674 int blk_common_cmd(int argc, char * const argv[], enum if_type if_type,