blob: ee3cc4407d76bee65bb5b5ef48209ed8ce5c3f89 [file] [log] [blame]
AKASHI Takahiroefb9a9d2022-04-19 10:05:09 +09001// 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 Vasut91405592023-08-14 01:46:44 +020019/**
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 */
27static int disk_blk_part_validate(struct udevice *dev, lbaint_t start, lbaint_t blkcnt)
28{
Marek Vasut9c005862023-08-14 01:46:45 +020029 struct disk_part *part = dev_get_uclass_plat(dev);
30
Marek Vasut91405592023-08-14 01:46:44 +020031 if (device_get_uclass_id(dev) != UCLASS_PARTITION)
32 return -ENOSYS;
33
Marek Vasut9c005862023-08-14 01:46:45 +020034 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 Vasut91405592023-08-14 01:46:44 +020040 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 */
50static 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 Takahiro8b57fe32022-04-19 10:05:16 +090057/*
58 * BLOCK IO APIs
59 */
Marek Vasutcc4d0f92023-08-14 01:46:42 +020060/**
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 Glass4e93eea2022-10-20 18:22:52 -060070unsigned long disk_blk_read(struct udevice *dev, lbaint_t start,
71 lbaint_t blkcnt, void *buffer)
AKASHI Takahiro8b57fe32022-04-19 10:05:16 +090072{
Marek Vasut91405592023-08-14 01:46:44 +020073 int ret = disk_blk_part_validate(dev, start, blkcnt);
AKASHI Takahiro8b57fe32022-04-19 10:05:16 +090074
Marek Vasut91405592023-08-14 01:46:44 +020075 if (ret)
76 return ret;
AKASHI Takahiro8b57fe32022-04-19 10:05:16 +090077
Marek Vasut91405592023-08-14 01:46:44 +020078 return blk_read(dev_get_parent(dev), disk_blk_part_offset(dev, start),
Marek Vasutcc4d0f92023-08-14 01:46:42 +020079 blkcnt, buffer);
AKASHI Takahiro8b57fe32022-04-19 10:05:16 +090080}
81
Marek Vasut2aee7492023-08-14 01:46:43 +020082/**
83 * disk_blk_write() - Write to a block device
84 *
Marek Vasut91405592023-08-14 01:46:44 +020085 * @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 Vasut2aee7492023-08-14 01:46:43 +020088 * @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 Glass4e93eea2022-10-20 18:22:52 -060092unsigned long disk_blk_write(struct udevice *dev, lbaint_t start,
93 lbaint_t blkcnt, const void *buffer)
AKASHI Takahiro8b57fe32022-04-19 10:05:16 +090094{
Marek Vasut91405592023-08-14 01:46:44 +020095 int ret = disk_blk_part_validate(dev, start, blkcnt);
96
97 if (ret)
98 return ret;
AKASHI Takahiro8b57fe32022-04-19 10:05:16 +090099
Marek Vasut91405592023-08-14 01:46:44 +0200100 return blk_write(dev_get_parent(dev), disk_blk_part_offset(dev, start),
101 blkcnt, buffer);
AKASHI Takahiro8b57fe32022-04-19 10:05:16 +0900102}
103
Marek Vasut2aee7492023-08-14 01:46:43 +0200104/**
105 * disk_blk_erase() - Erase part of a block device
106 *
Marek Vasut91405592023-08-14 01:46:44 +0200107 * @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 Vasut2aee7492023-08-14 01:46:43 +0200110 * @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 Glass4e93eea2022-10-20 18:22:52 -0600113unsigned long disk_blk_erase(struct udevice *dev, lbaint_t start,
114 lbaint_t blkcnt)
AKASHI Takahiro8b57fe32022-04-19 10:05:16 +0900115{
Marek Vasut91405592023-08-14 01:46:44 +0200116 int ret = disk_blk_part_validate(dev, start, blkcnt);
117
118 if (ret)
119 return ret;
AKASHI Takahiro8b57fe32022-04-19 10:05:16 +0900120
Marek Vasut91405592023-08-14 01:46:44 +0200121 return blk_erase(dev_get_parent(dev), disk_blk_part_offset(dev, start),
122 blkcnt);
AKASHI Takahiro8b57fe32022-04-19 10:05:16 +0900123}
124
AKASHI Takahiroefb9a9d2022-04-19 10:05:09 +0900125UCLASS_DRIVER(partition) = {
126 .id = UCLASS_PARTITION,
127 .per_device_plat_auto = sizeof(struct disk_part),
128 .name = "partition",
129};
Marek Vasute18752b2023-08-14 01:46:46 +0200130
131static const struct blk_ops blk_part_ops = {
132 .read = disk_blk_read,
133 .write = disk_blk_write,
134 .erase = disk_blk_erase,
135};
136
137U_BOOT_DRIVER(blk_partition) = {
138 .name = "blk_partition",
139 .id = UCLASS_PARTITION,
140 .ops = &blk_part_ops,
141};