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