AKASHI Takahiro | efb9a9d | 2022-04-19 10:05:09 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Software partition device (UCLASS_PARTITION) |
| 4 | * |
| 5 | * Copyright (c) 2021 Linaro Limited |
| 6 | * Author: AKASHI Takahiro |
| 7 | */ |
| 8 | |
| 9 | #define LOG_CATEGORY UCLASS_PARTITION |
| 10 | |
| 11 | #include <blk.h> |
| 12 | #include <dm.h> |
| 13 | #include <log.h> |
| 14 | #include <part.h> |
| 15 | #include <vsprintf.h> |
| 16 | #include <dm/device-internal.h> |
| 17 | #include <dm/lists.h> |
| 18 | |
Marek Vasut | 9140559 | 2023-08-14 01:46:44 +0200 | [diff] [blame] | 19 | /** |
| 20 | * disk_blk_part_validate() - Check whether access to partition is within limits |
| 21 | * |
| 22 | * @dev: Device (partition udevice) |
| 23 | * @start: Start block for the access(from start of partition) |
| 24 | * @blkcnt: Number of blocks to access (within the partition) |
| 25 | * @return 0 on valid block range, or -ve on error. |
| 26 | */ |
| 27 | static int disk_blk_part_validate(struct udevice *dev, lbaint_t start, lbaint_t blkcnt) |
| 28 | { |
Marek Vasut | 9c00586 | 2023-08-14 01:46:45 +0200 | [diff] [blame] | 29 | struct disk_part *part = dev_get_uclass_plat(dev); |
| 30 | |
Marek Vasut | 9140559 | 2023-08-14 01:46:44 +0200 | [diff] [blame] | 31 | if (device_get_uclass_id(dev) != UCLASS_PARTITION) |
| 32 | return -ENOSYS; |
| 33 | |
Marek Vasut | 9c00586 | 2023-08-14 01:46:45 +0200 | [diff] [blame] | 34 | if (start >= part->gpt_part_info.size) |
| 35 | return -E2BIG; |
| 36 | |
| 37 | if ((start + blkcnt) > part->gpt_part_info.size) |
| 38 | return -ERANGE; |
| 39 | |
Marek Vasut | 9140559 | 2023-08-14 01:46:44 +0200 | [diff] [blame] | 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * disk_blk_part_offset() - Compute offset from start of block device |
| 45 | * |
| 46 | * @dev: Device (partition udevice) |
| 47 | * @start: Start block for the access (from start of partition) |
| 48 | * @return Start block for the access (from start of block device) |
| 49 | */ |
| 50 | static lbaint_t disk_blk_part_offset(struct udevice *dev, lbaint_t start) |
| 51 | { |
| 52 | struct disk_part *part = dev_get_uclass_plat(dev); |
| 53 | |
| 54 | return start + part->gpt_part_info.start; |
| 55 | } |
| 56 | |
AKASHI Takahiro | 8b57fe3 | 2022-04-19 10:05:16 +0900 | [diff] [blame] | 57 | /* |
| 58 | * BLOCK IO APIs |
| 59 | */ |
Marek Vasut | cc4d0f9 | 2023-08-14 01:46:42 +0200 | [diff] [blame] | 60 | /** |
| 61 | * disk_blk_read() - Read from a block device partition |
| 62 | * |
| 63 | * @dev: Device to read from (partition udevice) |
| 64 | * @start: Start block for the read (from start of partition) |
| 65 | * @blkcnt: Number of blocks to read (within the partition) |
| 66 | * @buffer: Place to put the data |
| 67 | * @return number of blocks read (which may be less than @blkcnt), |
| 68 | * or -ve on error. This never returns 0 unless @blkcnt is 0 |
| 69 | */ |
Simon Glass | 4e93eea | 2022-10-20 18:22:52 -0600 | [diff] [blame] | 70 | unsigned long disk_blk_read(struct udevice *dev, lbaint_t start, |
| 71 | lbaint_t blkcnt, void *buffer) |
AKASHI Takahiro | 8b57fe3 | 2022-04-19 10:05:16 +0900 | [diff] [blame] | 72 | { |
Marek Vasut | 9140559 | 2023-08-14 01:46:44 +0200 | [diff] [blame] | 73 | int ret = disk_blk_part_validate(dev, start, blkcnt); |
AKASHI Takahiro | 8b57fe3 | 2022-04-19 10:05:16 +0900 | [diff] [blame] | 74 | |
Marek Vasut | 9140559 | 2023-08-14 01:46:44 +0200 | [diff] [blame] | 75 | if (ret) |
| 76 | return ret; |
AKASHI Takahiro | 8b57fe3 | 2022-04-19 10:05:16 +0900 | [diff] [blame] | 77 | |
Marek Vasut | 9140559 | 2023-08-14 01:46:44 +0200 | [diff] [blame] | 78 | return blk_read(dev_get_parent(dev), disk_blk_part_offset(dev, start), |
Marek Vasut | cc4d0f9 | 2023-08-14 01:46:42 +0200 | [diff] [blame] | 79 | blkcnt, buffer); |
AKASHI Takahiro | 8b57fe3 | 2022-04-19 10:05:16 +0900 | [diff] [blame] | 80 | } |
| 81 | |
Marek Vasut | 2aee749 | 2023-08-14 01:46:43 +0200 | [diff] [blame] | 82 | /** |
| 83 | * disk_blk_write() - Write to a block device |
| 84 | * |
Marek Vasut | 9140559 | 2023-08-14 01:46:44 +0200 | [diff] [blame] | 85 | * @dev: Device to write to (partition udevice) |
| 86 | * @start: Start block for the write (from start of partition) |
| 87 | * @blkcnt: Number of blocks to write (within the partition) |
Marek Vasut | 2aee749 | 2023-08-14 01:46:43 +0200 | [diff] [blame] | 88 | * @buffer: Data to write |
| 89 | * @return number of blocks written (which may be less than @blkcnt), |
| 90 | * or -ve on error. This never returns 0 unless @blkcnt is 0 |
| 91 | */ |
Simon Glass | 4e93eea | 2022-10-20 18:22:52 -0600 | [diff] [blame] | 92 | unsigned long disk_blk_write(struct udevice *dev, lbaint_t start, |
| 93 | lbaint_t blkcnt, const void *buffer) |
AKASHI Takahiro | 8b57fe3 | 2022-04-19 10:05:16 +0900 | [diff] [blame] | 94 | { |
Marek Vasut | 9140559 | 2023-08-14 01:46:44 +0200 | [diff] [blame] | 95 | int ret = disk_blk_part_validate(dev, start, blkcnt); |
| 96 | |
| 97 | if (ret) |
| 98 | return ret; |
AKASHI Takahiro | 8b57fe3 | 2022-04-19 10:05:16 +0900 | [diff] [blame] | 99 | |
Marek Vasut | 9140559 | 2023-08-14 01:46:44 +0200 | [diff] [blame] | 100 | return blk_write(dev_get_parent(dev), disk_blk_part_offset(dev, start), |
| 101 | blkcnt, buffer); |
AKASHI Takahiro | 8b57fe3 | 2022-04-19 10:05:16 +0900 | [diff] [blame] | 102 | } |
| 103 | |
Marek Vasut | 2aee749 | 2023-08-14 01:46:43 +0200 | [diff] [blame] | 104 | /** |
| 105 | * disk_blk_erase() - Erase part of a block device |
| 106 | * |
Marek Vasut | 9140559 | 2023-08-14 01:46:44 +0200 | [diff] [blame] | 107 | * @dev: Device to erase (partition udevice) |
| 108 | * @start: Start block for the erase (from start of partition) |
| 109 | * @blkcnt: Number of blocks to erase (within the partition) |
Marek Vasut | 2aee749 | 2023-08-14 01:46:43 +0200 | [diff] [blame] | 110 | * @return number of blocks erased (which may be less than @blkcnt), |
| 111 | * or -ve on error. This never returns 0 unless @blkcnt is 0 |
| 112 | */ |
Simon Glass | 4e93eea | 2022-10-20 18:22:52 -0600 | [diff] [blame] | 113 | unsigned long disk_blk_erase(struct udevice *dev, lbaint_t start, |
| 114 | lbaint_t blkcnt) |
AKASHI Takahiro | 8b57fe3 | 2022-04-19 10:05:16 +0900 | [diff] [blame] | 115 | { |
Marek Vasut | 9140559 | 2023-08-14 01:46:44 +0200 | [diff] [blame] | 116 | int ret = disk_blk_part_validate(dev, start, blkcnt); |
| 117 | |
| 118 | if (ret) |
| 119 | return ret; |
AKASHI Takahiro | 8b57fe3 | 2022-04-19 10:05:16 +0900 | [diff] [blame] | 120 | |
Marek Vasut | 9140559 | 2023-08-14 01:46:44 +0200 | [diff] [blame] | 121 | return blk_erase(dev_get_parent(dev), disk_blk_part_offset(dev, start), |
| 122 | blkcnt); |
AKASHI Takahiro | 8b57fe3 | 2022-04-19 10:05:16 +0900 | [diff] [blame] | 123 | } |
| 124 | |
AKASHI Takahiro | efb9a9d | 2022-04-19 10:05:09 +0900 | [diff] [blame] | 125 | UCLASS_DRIVER(partition) = { |
| 126 | .id = UCLASS_PARTITION, |
| 127 | .per_device_plat_auto = sizeof(struct disk_part), |
| 128 | .name = "partition", |
| 129 | }; |
Marek Vasut | e18752b | 2023-08-14 01:46:46 +0200 | [diff] [blame] | 130 | |
| 131 | static const struct blk_ops blk_part_ops = { |
| 132 | .read = disk_blk_read, |
| 133 | .write = disk_blk_write, |
| 134 | .erase = disk_blk_erase, |
| 135 | }; |
| 136 | |
| 137 | U_BOOT_DRIVER(blk_partition) = { |
| 138 | .name = "blk_partition", |
| 139 | .id = UCLASS_PARTITION, |
| 140 | .ops = &blk_part_ops, |
| 141 | }; |