diff mbox series

[v2,3/6] cmd: ramdisk: add ramdisk control command

Message ID 20230714054406.761508-4-masahisa.kojima@linaro.org
State New
Headers show
Series introduce EFI_RAM_DISK_PROTOCOL | expand

Commit Message

Masahisa Kojima July 14, 2023, 5:44 a.m. UTC
This introcudes the ramdisk command, to control
the ramdisk devices.

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
---
Newly introcuded in v2

 cmd/Kconfig   |  7 ++++
 cmd/Makefile  |  1 +
 cmd/ramdisk.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 100 insertions(+)
 create mode 100644 cmd/ramdisk.c
diff mbox series

Patch

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 365371fb51..5f3022b0c9 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1592,6 +1592,13 @@  config CMD_AXI
 	  Interface) busses, a on-chip interconnect specification for managing
 	  functional blocks in SoC designs, which is also often used in designs
 	  involving FPGAs (e.g.  communication with IP cores in Xilinx FPGAs).
+
+config CMD_RAM_DISK
+	bool "ramdisk"
+	depends on RAM_DISK
+	help
+	  This provides commands to control the ramdisk devices.
+
 endmenu
 
 
diff --git a/cmd/Makefile b/cmd/Makefile
index 6c37521b4e..eb7d86a3c6 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -142,6 +142,7 @@  obj-$(CONFIG_CMD_PWM) += pwm.o
 obj-$(CONFIG_CMD_PXE) += pxe.o
 obj-$(CONFIG_CMD_WOL) += wol.o
 obj-$(CONFIG_CMD_QFW) += qfw.o
+obj-$(CONFIG_CMD_RAM_DISK) += ramdisk.o
 obj-$(CONFIG_CMD_READ) += read.o
 obj-$(CONFIG_CMD_WRITE) += read.o
 obj-$(CONFIG_CMD_REGINFO) += reginfo.o
diff --git a/cmd/ramdisk.c b/cmd/ramdisk.c
new file mode 100644
index 0000000000..8df29729aa
--- /dev/null
+++ b/cmd/ramdisk.c
@@ -0,0 +1,92 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ *  ramdisk command
+ *
+ *  Copyright (c) 2023, Linaro Limited
+ */
+
+#include <blk.h>
+#include <common.h>
+#include <command.h>
+#include <dm.h>
+#include <ramdisk.h>
+
+static int ramdisk_curr_dev; /* current device */
+
+static int get_desc(enum uclass_id uclass_id, int devnum, struct blk_desc **descp)
+{
+	struct udevice *dev;
+	struct uclass *uc;
+	int ret;
+
+	*descp = NULL;
+	ret = uclass_get(UCLASS_BLK, &uc);
+	if (ret)
+		return ret;
+
+	uclass_foreach_dev(dev, uc) {
+		struct blk_desc *desc = dev_get_uclass_plat(dev);
+
+		if (desc->uclass_id == uclass_id) {
+			if (desc->devnum == devnum) {
+				*descp = desc;
+				return 0;
+			}
+		}
+	}
+
+	return -1;
+}
+
+static int do_ramdisk(struct cmd_tbl *cmdtp, int flag, int argc,
+		      char *const argv[])
+{
+	int ret;
+	u64 addr, size;
+	struct udevice *dev;
+
+	if (strncmp(argv[1], "mount", 5) == 0) {
+		if (argc != 4)
+			return CMD_RET_USAGE;
+
+		addr = hextoul(argv[2], NULL);
+		size = hextoul(argv[3], NULL);
+		dev = ramdisk_mount(addr, size, NULL);
+		if (!dev)
+			return CMD_RET_FAILURE;
+
+		return CMD_RET_SUCCESS;
+	}
+
+	if (strncmp(argv[1], "umount", 6) == 0) {
+		struct blk_desc *desc;
+
+		ret = get_desc(UCLASS_RAM_DISK, ramdisk_curr_dev, &desc);
+		if (ret) {
+			printf("ramdisk device not found(dev %d)\n", ramdisk_curr_dev);
+			return CMD_RET_FAILURE;
+		}
+
+		ret = ramdisk_unmount(desc->bdev);
+		if (ret)
+			return CMD_RET_FAILURE;
+
+		return CMD_RET_SUCCESS;
+	}
+
+	return blk_common_cmd(argc, argv, UCLASS_RAM_DISK, &ramdisk_curr_dev);
+}
+
+U_BOOT_CMD(
+	ramdisk, 5, 1, do_ramdisk,
+	"ramdisk sub-system",
+	"ramdisk info  - show available ramdisk devices\n"
+	"ramdisk mount <address> <size>  - mount ramdisk\n"
+	"ramdisk umount - unmount ramdisk\n"
+	"ramdisk device [dev] - show or set current device\n"
+	"ramdisk part [dev] - print partition table of one or all ramdisk devices\n"
+	"ramdisk read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
+	"     to memory address `addr'\n"
+	"ramdisk write addr blk# cnt - write `cnt' blocks starting at block\n"
+	"     `blk#' from memory address `addr'"
+);