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