[U-Boot] [PATCH v6 1/2] Loop block device for sandbox
Pavel Herrmann
morpheus.ibis at gmail.com
Fri Sep 28 11:21:29 CEST 2012
This driver uses files as block devices, can be used for testing disk
operations on sandbox.
A new command "sata_loop" is introduced to load files in runtime.
Signed-off-by: Pavel Herrmann <morpheus.ibis at gmail.com>
---
Changes for v6:
sync with new version of "Fix checkpatch warnings about externs in *.c"
set all constant disk properties even on not connected drivers
Changes for v5:
use common sata extern header - this requires "[PATCH] Fix checkpatch warnings
about externs in *.c" to be merged first
make sure we have valid callbacks before scanning partitions
Changes for v4:
checkpatch fixes
use NULLs instead of ""
extend sata_loop command
Changes for v3:
introduce sata_loop command
Changes for v2:
split sandbox config off into separate patch (2/2)
rename file to signify exported API
style fixes
show end of long filenames rather than beginning
check for lseek errors to indicate non-regular file
drivers/block/sata_loopback.c | 209 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 209 insertions(+)
create mode 100644 drivers/block/sata_loopback.c
diff --git a/drivers/block/sata_loopback.c b/drivers/block/sata_loopback.c
new file mode 100644
index 0000000..b55b270
--- /dev/null
+++ b/drivers/block/sata_loopback.c
@@ -0,0 +1,209 @@
+/*
+ * (C) Copyright 2012
+ * Pavel Herrmann <morpheus.ibis at gmail.com>
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <common.h>
+#include <part.h>
+#include <ata.h>
+#include <sata.h>
+#include <errno.h>
+#include <os.h>
+#include <command.h>
+#include <malloc.h>
+
+static const char revision[] = "0.0";
+static const char vendor[] = "SATA loopback";
+
+/* this will be auto-initialized to NULLs */
+static char *filenames[CONFIG_SYS_SATA_MAX_DEVICE];
+
+static inline int range_check(int dev)
+{
+ return (dev < 0) || (dev >= CONFIG_SYS_SATA_MAX_DEVICE);
+}
+
+int init_sata(int dev)
+{
+ block_dev_desc_t *pdev = &sata_dev_desc[dev];
+ int fd, old_fd;
+
+ if (range_check(dev)) {
+ printf("File index %d is out of range.\n", dev);
+ return -EINVAL;
+ }
+
+ if (filenames[dev])
+ fd = os_open(filenames[dev], OS_O_RDWR);
+ else
+ fd = -1;
+
+ old_fd = (long) pdev->priv;
+ /* This is ugly, but saves allocation for 1 int. */
+ pdev->priv = (void *) (long) fd;
+
+ if ((old_fd > 2) || (old_fd < 0))
+ os_close(old_fd);
+
+ return 0;
+}
+
+ulong sata_read(int dev, ulong start, lbaint_t blkcnt, void *buffer)
+{
+ block_dev_desc_t *pdev = &sata_dev_desc[dev];
+ int fd = (long) pdev->priv;
+ lbaint_t start_byte = ATA_SECT_SIZE * start;
+ lbaint_t length_byte = ATA_SECT_SIZE * blkcnt;
+ lbaint_t retval;
+
+ if (os_lseek(fd, start_byte, OS_SEEK_SET) != start_byte)
+ return -1;
+
+ retval = os_read(fd, buffer, length_byte);
+
+ return retval / ATA_SECT_SIZE;
+}
+
+ulong sata_write(int dev, ulong start, lbaint_t blkcnt, const void *buffer)
+{
+ block_dev_desc_t *pdev = &sata_dev_desc[dev];
+ int fd = (long) pdev->priv;
+ lbaint_t start_byte = ATA_SECT_SIZE * start;
+ lbaint_t length_byte = ATA_SECT_SIZE * blkcnt;
+ lbaint_t retval;
+
+ if (os_lseek(fd, start_byte, OS_SEEK_SET) != start_byte)
+ return -1;
+
+ retval = os_write(fd, buffer, length_byte);
+
+ return retval / ATA_SECT_SIZE;
+}
+
+int scan_sata(int dev)
+{
+ block_dev_desc_t *pdev = &sata_dev_desc[dev];
+ int fd = (long) pdev->priv;
+ int namelen;
+ char *filename;
+ lbaint_t bytes = 0;
+
+ if (range_check(dev)) {
+ printf("File index %d is out of range.\n", dev);
+ return -EINVAL;
+ }
+
+ if (filenames[dev])
+ filename = filenames[dev];
+ else
+ filename = "";
+
+ memcpy(pdev->vendor, vendor, sizeof(vendor));
+ memcpy(pdev->revision, revision, sizeof(revision));
+ namelen = strlen(filename);
+
+ if (namelen > 20) {
+ /* take the last 17 chars, prepend them with "..." */
+ memcpy(pdev->product, "...", 3);
+ memcpy(pdev->product+3, filename + (namelen - 17), 17);
+ } else
+ memcpy(pdev->product, filename, namelen);
+
+ pdev->product[20] = 0;
+ pdev->type = DEV_TYPE_HARDDISK;
+ pdev->blksz = ATA_SECT_SIZE;
+ pdev->lun = 0;
+ pdev->block_read = sata_read;
+ pdev->block_write = sata_write;
+
+ bytes = os_lseek(fd, 0, OS_SEEK_END);
+ if (bytes != -1) {
+ pdev->lba = bytes/ATA_SECT_SIZE;
+ printf("SATA loop %d:\nfilename: %s\nsize: %lu\nblock count:"
+ " %lu\n", dev, filename, bytes, pdev->lba);
+ } else {
+ pdev->lba = 0;
+ printf("SATA loop %d:\nfilename: %s\nFAILED TO OPEN\n",
+ dev, filename);
+ }
+
+ return 0;
+}
+
+
+int do_loop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ int dev = 0;
+
+ switch (argc) {
+ case 0:
+ case 1:
+ return CMD_RET_USAGE;
+ case 2:
+ if (!strncmp(argv[1], "inf", 3)) {
+ for (dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; dev++)
+ scan_sata(dev);
+ return 0;
+ }
+ return CMD_RET_USAGE;
+ case 3:
+ if (!strncmp(argv[1], "inf", 3)) {
+ dev = simple_strtoul(argv[2], NULL, 10);
+ return scan_sata(dev);
+ }
+ return CMD_RET_USAGE;
+ case 4:
+ if (!strncmp(argv[1], "load", 4)) {
+ dev = simple_strtoul(argv[2], NULL, 10);
+ /*
+ * init_sata() and scan_sata() do their own range
+ * check, however we need to explicitly do it here
+ * as well.
+ */
+ if (range_check(dev)) {
+ printf("File index %d is out of range.\n", dev);
+ return -EINVAL;
+ }
+ free(filenames[dev]);
+ filenames[dev] = strdup(argv[3]);
+ init_sata(dev);
+ scan_sata(dev);
+ /*
+ * Scan the partition table if we succeeded in loading
+ * the new loop file.
+ */
+ if (sata_dev_desc[dev].lba > 0)
+ init_part(&sata_dev_desc[dev]);
+
+ return 0;
+ }
+ return CMD_RET_USAGE;
+ }
+ return CMD_RET_USAGE;
+}
+
+U_BOOT_CMD(
+ sata_loop, 4, 1, do_loop,
+ "SATA loopback",
+ "info - show info about all loop devices\n"
+ "sata_loop info devnum - show info about loop devnum\n"
+ "sata_loop load devnum file - load file from host FS into loop devnum"
+);
--
1.7.12
More information about the U-Boot
mailing list