]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - disk/disk-uclass.c
1 // SPDX-License-Identifier: GPL-2.0+
3 * Software partition device (UCLASS_PARTITION)
5 * Copyright (c) 2021 Linaro Limited
6 * Author: AKASHI Takahiro
9 #define LOG_CATEGORY UCLASS_PARTITION
17 #include <dm/device-internal.h>
21 * disk_blk_part_validate() - Check whether access to partition is within limits
23 * @dev: Device (partition udevice)
24 * @start: Start block for the access(from start of partition)
25 * @blkcnt: Number of blocks to access (within the partition)
26 * @return 0 on valid block range, or -ve on error.
28 static int disk_blk_part_validate(struct udevice
*dev
, lbaint_t start
, lbaint_t blkcnt
)
30 struct disk_part
*part
= dev_get_uclass_plat(dev
);
32 if (device_get_uclass_id(dev
) != UCLASS_PARTITION
)
35 if (start
>= part
->gpt_part_info
.size
)
38 if ((start
+ blkcnt
) > part
->gpt_part_info
.size
)
45 * disk_blk_part_offset() - Compute offset from start of block device
47 * @dev: Device (partition udevice)
48 * @start: Start block for the access (from start of partition)
49 * @return Start block for the access (from start of block device)
51 static lbaint_t
disk_blk_part_offset(struct udevice
*dev
, lbaint_t start
)
53 struct disk_part
*part
= dev_get_uclass_plat(dev
);
55 return start
+ part
->gpt_part_info
.start
;
62 * disk_blk_read() - Read from a block device partition
64 * @dev: Device to read from (partition udevice)
65 * @start: Start block for the read (from start of partition)
66 * @blkcnt: Number of blocks to read (within the partition)
67 * @buffer: Place to put the data
68 * @return number of blocks read (which may be less than @blkcnt),
69 * or -ve on error. This never returns 0 unless @blkcnt is 0
71 unsigned long disk_blk_read(struct udevice
*dev
, lbaint_t start
,
72 lbaint_t blkcnt
, void *buffer
)
74 int ret
= disk_blk_part_validate(dev
, start
, blkcnt
);
79 return blk_read(dev_get_parent(dev
), disk_blk_part_offset(dev
, start
),
84 * disk_blk_write() - Write to a block device
86 * @dev: Device to write to (partition udevice)
87 * @start: Start block for the write (from start of partition)
88 * @blkcnt: Number of blocks to write (within the partition)
89 * @buffer: Data to write
90 * @return number of blocks written (which may be less than @blkcnt),
91 * or -ve on error. This never returns 0 unless @blkcnt is 0
93 unsigned long disk_blk_write(struct udevice
*dev
, lbaint_t start
,
94 lbaint_t blkcnt
, const void *buffer
)
96 int ret
= disk_blk_part_validate(dev
, start
, blkcnt
);
101 return blk_write(dev_get_parent(dev
), disk_blk_part_offset(dev
, start
),
106 * disk_blk_erase() - Erase part of a block device
108 * @dev: Device to erase (partition udevice)
109 * @start: Start block for the erase (from start of partition)
110 * @blkcnt: Number of blocks to erase (within the partition)
111 * @return number of blocks erased (which may be less than @blkcnt),
112 * or -ve on error. This never returns 0 unless @blkcnt is 0
114 unsigned long disk_blk_erase(struct udevice
*dev
, lbaint_t start
,
117 int ret
= disk_blk_part_validate(dev
, start
, blkcnt
);
122 return blk_erase(dev_get_parent(dev
), disk_blk_part_offset(dev
, start
),
126 UCLASS_DRIVER(partition
) = {
127 .id
= UCLASS_PARTITION
,
128 .per_device_plat_auto
= sizeof(struct disk_part
),
132 static const struct blk_ops blk_part_ops
= {
133 .read
= disk_blk_read
,
134 .write
= disk_blk_write
,
135 .erase
= disk_blk_erase
,
138 U_BOOT_DRIVER(blk_partition
) = {
139 .name
= "blk_partition",
140 .id
= UCLASS_PARTITION
,
141 .ops
= &blk_part_ops
,