[PATCH 5/5] disk: Use a helper function to reduce duplication

Simon Glass sjg at chromium.org
Fri Mar 11 20:10:05 CET 2022


Reduce the duplicated code slightly by using a helper function to handle
the common code.

This reduces the code size very slightly.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 disk/disk-uclass.c | 46 +++++++++++++++++++++++++---------------------
 1 file changed, 25 insertions(+), 21 deletions(-)

diff --git a/disk/disk-uclass.c b/disk/disk-uclass.c
index 72ff62ebf58..7adfe6b2545 100644
--- a/disk/disk-uclass.c
+++ b/disk/disk-uclass.c
@@ -65,26 +65,38 @@ int part_create_block_devices(struct udevice *blk_dev)
 	return 0;
 }
 
+static int blk_part_setup(struct udevice *dev, lbaint_t *startp,
+			  lbaint_t blkcnt)
+{
+	struct disk_part *part;
+
+	part = dev_get_uclass_plat(dev);
+	if (*startp >= part->gpt_part_info.size)
+		return -E2BIG;
+
+	if (*startp + blkcnt > part->gpt_part_info.size)
+		blkcnt = part->gpt_part_info.size - *startp;
+	*startp += part->gpt_part_info.start;
+
+	return 0;
+}
+
 static ulong blk_part_read(struct udevice *dev, lbaint_t start,
 			   lbaint_t blkcnt, void *buffer)
 {
 	struct udevice *parent;
-	struct disk_part *part;
 	const struct blk_ops *ops;
+	int ret;
 
 	parent = dev_get_parent(dev);
 	ops = blk_get_ops(parent);
 	if (!ops->read)
 		return -ENOSYS;
 
-	part = dev_get_uclass_plat(dev);
-	if (start >= part->gpt_part_info.size)
+	ret = blk_part_setup(dev, &start, blkcnt);
+	if (ret)
 		return 0;
 
-	if ((start + blkcnt) > part->gpt_part_info.size)
-		blkcnt = part->gpt_part_info.size - start;
-	start += part->gpt_part_info.start;
-
 	return ops->read(parent, start, blkcnt, buffer);
 }
 
@@ -92,22 +104,18 @@ static ulong blk_part_write(struct udevice *dev, lbaint_t start,
 			    lbaint_t blkcnt, const void *buffer)
 {
 	struct udevice *parent;
-	struct disk_part *part;
 	const struct blk_ops *ops;
+	int ret;
 
 	parent = dev_get_parent(dev);
 	ops = blk_get_ops(parent);
 	if (!ops->write)
 		return -ENOSYS;
 
-	part = dev_get_uclass_plat(dev);
-	if (start >= part->gpt_part_info.size)
+	ret = blk_part_setup(dev, &start, blkcnt);
+	if (ret)
 		return 0;
 
-	if ((start + blkcnt) > part->gpt_part_info.size)
-		blkcnt = part->gpt_part_info.size - start;
-	start += part->gpt_part_info.start;
-
 	return ops->write(parent, start, blkcnt, buffer);
 }
 
@@ -115,22 +123,18 @@ static ulong blk_part_erase(struct udevice *dev, lbaint_t start,
 			    lbaint_t blkcnt)
 {
 	struct udevice *parent;
-	struct disk_part *part;
 	const struct blk_ops *ops;
+	int ret;
 
 	parent = dev_get_parent(dev);
 	ops = blk_get_ops(parent);
 	if (!ops->erase)
 		return -ENOSYS;
 
-	part = dev_get_uclass_plat(dev);
-	if (start >= part->gpt_part_info.size)
+	ret = blk_part_setup(dev, &start, blkcnt);
+	if (ret)
 		return 0;
 
-	if ((start + blkcnt) > part->gpt_part_info.size)
-		blkcnt = part->gpt_part_info.size - start;
-	start += part->gpt_part_info.start;
-
 	return ops->erase(parent, start, blkcnt);
 }
 
-- 
2.35.1.723.g4982287a31-goog



More information about the U-Boot mailing list