blob: efe4bf1f9490ba8e972569a7ac8aa61b25c21de5 [file] [log] [blame]
AKASHI Takahiro43855fd2022-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
Heinrich Schuchardt054de212022-07-02 14:07:48 +020011#include <common.h>
AKASHI Takahiro43855fd2022-04-19 10:05:09 +090012#include <blk.h>
13#include <dm.h>
14#include <log.h>
15#include <part.h>
16#include <vsprintf.h>
17#include <dm/device-internal.h>
18#include <dm/lists.h>
19
Marek Vasut2bc0dfe2023-08-14 01:46:44 +020020/**
21 * disk_blk_part_validate() - Check whether access to partition is within limits
22 *
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.
27 */
28static int disk_blk_part_validate(struct udevice *dev, lbaint_t start, lbaint_t blkcnt)
29{
Marek Vasutbfd98b92023-08-14 01:46:45 +020030 struct disk_part *part = dev_get_uclass_plat(dev);
31
Marek Vasut2bc0dfe2023-08-14 01:46:44 +020032 if (device_get_uclass_id(dev) != UCLASS_PARTITION)
33 return -ENOSYS;
34
Marek Vasutbfd98b92023-08-14 01:46:45 +020035 if (start >= part->gpt_part_info.size)
36 return -E2BIG;
37
38 if ((start + blkcnt) > part->gpt_part_info.size)
39 return -ERANGE;
40
Marek Vasut2bc0dfe2023-08-14 01:46:44 +020041 return 0;
42}
43
44/**
45 * disk_blk_part_offset() - Compute offset from start of block device
46 *
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)
50 */
51static lbaint_t disk_blk_part_offset(struct udevice *dev, lbaint_t start)
52{
53 struct disk_part *part = dev_get_uclass_plat(dev);
54
55 return start + part->gpt_part_info.start;
56}
57
AKASHI Takahiro59da9d42022-04-19 10:05:16 +090058/*
59 * BLOCK IO APIs
60 */
Marek Vasut91d30662023-08-14 01:46:42 +020061/**
62 * disk_blk_read() - Read from a block device partition
63 *
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
70 */
Simon Glass76c839f2022-10-20 18:22:52 -060071unsigned long disk_blk_read(struct udevice *dev, lbaint_t start,
72 lbaint_t blkcnt, void *buffer)
AKASHI Takahiro59da9d42022-04-19 10:05:16 +090073{
Marek Vasut2bc0dfe2023-08-14 01:46:44 +020074 int ret = disk_blk_part_validate(dev, start, blkcnt);
AKASHI Takahiro59da9d42022-04-19 10:05:16 +090075
Marek Vasut2bc0dfe2023-08-14 01:46:44 +020076 if (ret)
77 return ret;
AKASHI Takahiro59da9d42022-04-19 10:05:16 +090078
Marek Vasut2bc0dfe2023-08-14 01:46:44 +020079 return blk_read(dev_get_parent(dev), disk_blk_part_offset(dev, start),
Marek Vasut91d30662023-08-14 01:46:42 +020080 blkcnt, buffer);
AKASHI Takahiro59da9d42022-04-19 10:05:16 +090081}
82
Marek Vasut9161c2c2023-08-14 01:46:43 +020083/**
84 * disk_blk_write() - Write to a block device
85 *
Marek Vasut2bc0dfe2023-08-14 01:46:44 +020086 * @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)
Marek Vasut9161c2c2023-08-14 01:46:43 +020089 * @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
92 */
Simon Glass76c839f2022-10-20 18:22:52 -060093unsigned long disk_blk_write(struct udevice *dev, lbaint_t start,
94 lbaint_t blkcnt, const void *buffer)
AKASHI Takahiro59da9d42022-04-19 10:05:16 +090095{
Marek Vasut2bc0dfe2023-08-14 01:46:44 +020096 int ret = disk_blk_part_validate(dev, start, blkcnt);
AKASHI Takahiro59da9d42022-04-19 10:05:16 +090097
Marek Vasut2bc0dfe2023-08-14 01:46:44 +020098 if (ret)
99 return ret;
100
101 return blk_write(dev_get_parent(dev), disk_blk_part_offset(dev, start),
102 blkcnt, buffer);
AKASHI Takahiro59da9d42022-04-19 10:05:16 +0900103}
104
Marek Vasut9161c2c2023-08-14 01:46:43 +0200105/**
106 * disk_blk_erase() - Erase part of a block device
107 *
Marek Vasut2bc0dfe2023-08-14 01:46:44 +0200108 * @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)
Marek Vasut9161c2c2023-08-14 01:46:43 +0200111 * @return number of blocks erased (which may be less than @blkcnt),
112 * or -ve on error. This never returns 0 unless @blkcnt is 0
113 */
Simon Glass76c839f2022-10-20 18:22:52 -0600114unsigned long disk_blk_erase(struct udevice *dev, lbaint_t start,
115 lbaint_t blkcnt)
AKASHI Takahiro59da9d42022-04-19 10:05:16 +0900116{
Marek Vasut2bc0dfe2023-08-14 01:46:44 +0200117 int ret = disk_blk_part_validate(dev, start, blkcnt);
AKASHI Takahiro59da9d42022-04-19 10:05:16 +0900118
Marek Vasut2bc0dfe2023-08-14 01:46:44 +0200119 if (ret)
120 return ret;
121
122 return blk_erase(dev_get_parent(dev), disk_blk_part_offset(dev, start),
123 blkcnt);
AKASHI Takahiro59da9d42022-04-19 10:05:16 +0900124}
125
AKASHI Takahiro43855fd2022-04-19 10:05:09 +0900126UCLASS_DRIVER(partition) = {
127 .id = UCLASS_PARTITION,
128 .per_device_plat_auto = sizeof(struct disk_part),
129 .name = "partition",
130};
Marek Vasut30a12e02023-08-14 01:46:46 +0200131
132static const struct blk_ops blk_part_ops = {
133 .read = disk_blk_read,
134 .write = disk_blk_write,
135 .erase = disk_blk_erase,
136};
137
138U_BOOT_DRIVER(blk_partition) = {
139 .name = "blk_partition",
140 .id = UCLASS_PARTITION,
141 .ops = &blk_part_ops,
142};