]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
blk: blkmap: add ramdisk creation utility function
authorMasahisa Kojima <masahisa.kojima@linaro.org>
Fri, 10 Nov 2023 04:25:36 +0000 (13:25 +0900)
committerIlias Apalodimas <ilias.apalodimas@linaro.org>
Sat, 18 Nov 2023 08:08:08 +0000 (10:08 +0200)
User needs to call several functions to create the ramdisk
with blkmap.
This adds the utility function to create blkmap device and
mount the ramdisk.

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
drivers/block/Makefile
drivers/block/blkmap.c
drivers/block/blkmap_helper.c [new file with mode: 0644]
include/blkmap.h

index fdcba5c8318f3f59ee07862df3a7434f826732df..fe6a1fcf486b69f67593533d1339616ba2da8ecd 100644 (file)
@@ -15,7 +15,8 @@ obj-$(CONFIG_RKMTD) += rkmtd.o
 endif
 obj-$(CONFIG_SANDBOX) += sandbox.o host-uclass.o host_dev.o
 obj-$(CONFIG_$(SPL_TPL_)BLOCK_CACHE) += blkcache.o
-obj-$(CONFIG_BLKMAP) += blkmap.o
+obj-$(CONFIG_$(SPL_TPL_)BLKMAP) += blkmap.o
+obj-$(CONFIG_$(SPL_TPL_)BLKMAP) += blkmap_helper.o
 
 obj-$(CONFIG_EFI_MEDIA) += efi-media-uclass.o
 obj-$(CONFIG_EFI_MEDIA_SANDBOX) += sb_efi_media.o
index 149a4cac3eaa314dacc9bb367cf4de11532b2e0a..21201409ed4b22c85c9f788875399a6d10830552 100644 (file)
@@ -66,21 +66,6 @@ struct blkmap_slice {
        void (*destroy)(struct blkmap *bm, struct blkmap_slice *bms);
 };
 
-/**
- * struct blkmap - Block map
- *
- * Data associated with a blkmap.
- *
- * @label: Human readable name of this blkmap
- * @blk: Underlying block device
- * @slices: List of slices associated with this blkmap
- */
-struct blkmap {
-       char *label;
-       struct udevice *blk;
-       struct list_head slices;
-};
-
 static bool blkmap_slice_contains(struct blkmap_slice *bms, lbaint_t blknr)
 {
        return (blknr >= bms->blknr) && (blknr < (bms->blknr + bms->blkcnt));
diff --git a/drivers/block/blkmap_helper.c b/drivers/block/blkmap_helper.c
new file mode 100644 (file)
index 0000000..bfba141
--- /dev/null
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * blkmap helper function
+ *
+ * Copyright (c) 2023, Linaro Limited
+ */
+
+#include <blk.h>
+#include <blkmap.h>
+#include <dm/device.h>
+#include <dm/device-internal.h>
+
+int blkmap_create_ramdisk(const char *label, ulong image_addr, ulong image_size,
+                         struct udevice **devp)
+{
+       int ret;
+       lbaint_t blknum;
+       struct blkmap *bm;
+       struct blk_desc *desc;
+       struct udevice *bm_dev;
+
+       ret = blkmap_create(label, &bm_dev);
+       if (ret) {
+               log_err("failed to create blkmap\n");
+               return ret;
+       }
+
+       bm = dev_get_plat(bm_dev);
+       desc = dev_get_uclass_plat(bm->blk);
+       blknum = image_size >> desc->log2blksz;
+       ret = blkmap_map_pmem(bm_dev, 0, blknum, image_addr);
+       if (ret) {
+               log_err("Unable to map %#llx at block %d : %d\n",
+                       (unsigned long long)image_addr, 0, ret);
+               goto err;
+       }
+       log_info("Block %d+0x" LBAF " mapped to %#llx\n", 0, blknum,
+                (unsigned long long)image_addr);
+
+       ret = device_probe(bm->blk);
+       if (ret)
+               goto err;
+
+       if (devp)
+               *devp = bm_dev;
+
+       return 0;
+
+err:
+       blkmap_destroy(bm_dev);
+
+       return ret;
+}
index af54583c7dd3051a7266f5cfe4fe9e1a0eb26fd5..30dc84a7da8fd5445e5d65023c9f7d6f9010348d 100644 (file)
@@ -7,6 +7,23 @@
 #ifndef _BLKMAP_H
 #define _BLKMAP_H
 
+#include <dm/lists.h>
+
+/**
+ * struct blkmap - Block map
+ *
+ * Data associated with a blkmap.
+ *
+ * @label: Human readable name of this blkmap
+ * @blk: Underlying block device
+ * @slices: List of slices associated with this blkmap
+ */
+struct blkmap {
+       char *label;
+       struct udevice *blk;
+       struct list_head slices;
+};
+
 /**
  * blkmap_map_linear() - Map region of other block device
  *
@@ -74,4 +91,16 @@ int blkmap_create(const char *label, struct udevice **devp);
  */
 int blkmap_destroy(struct udevice *dev);
 
+/**
+ * blkmap_create_ramdisk() - Create new ramdisk with blkmap
+ *
+ * @label: Label of the new blkmap
+ * @image_addr: Target memory start address of this mapping
+ * @image_size: Target memory size of this mapping
+ * @devp: Updated with the address of the created blkmap device
+ * Returns: 0 on success, negative error code on failure
+ */
+int blkmap_create_ramdisk(const char *label, ulong image_addr, ulong image_size,
+                         struct udevice **devp);
+
 #endif /* _BLKMAP_H */