Commit f4d8de48 authored by Henrik Nordström's avatar Henrik Nordström Committed by Simon Glass

sandbox: block driver using host file/device as backing store

Provide a way to use any host file or device as a block device in U-Boot.
This can be used to provide filesystem access within U-Boot to an ext2
image file on the host, for example.

The support is plumbed into the filesystem and partition interfaces.

We don't want to print a message in the driver every time we find a missing
device. Pass the information back to the caller where a message can be printed
if desired.
Signed-off-by: default avatarHenrik Nordström <henrik@henriknordstrom.net>
Signed-off-by: default avatarSimon Glass <sjg@chromium.org>
- Removed change to part.c get_device_and_partition()
Signed-off-by: default avatarSimon Glass <sjg@chromium.org>
Reviewed-by: default avatarSimon Glass <sjg@chromium.org>
parent 60d18d3f
...@@ -6,6 +6,9 @@ ...@@ -6,6 +6,9 @@
#include <common.h> #include <common.h>
#include <fs.h> #include <fs.h>
#include <part.h>
#include <sandboxblockdev.h>
#include <asm/errno.h>
static int do_sandbox_load(cmd_tbl_t *cmdtp, int flag, int argc, static int do_sandbox_load(cmd_tbl_t *cmdtp, int flag, int argc,
char * const argv[]) char * const argv[])
...@@ -25,10 +28,69 @@ static int do_sandbox_save(cmd_tbl_t *cmdtp, int flag, int argc, ...@@ -25,10 +28,69 @@ static int do_sandbox_save(cmd_tbl_t *cmdtp, int flag, int argc,
return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX); return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
} }
static int do_sandbox_bind(cmd_tbl_t *cmdtp, int flag, int argc,
char * const argv[])
{
if (argc < 2 || argc > 3)
return CMD_RET_USAGE;
char *ep;
char *dev_str = argv[1];
char *file = argc >= 3 ? argv[2] : NULL;
int dev = simple_strtoul(dev_str, &ep, 16);
if (*ep) {
printf("** Bad device specification %s **\n", dev_str);
return CMD_RET_USAGE;
}
return host_dev_bind(dev, file);
}
static int do_sandbox_info(cmd_tbl_t *cmdtp, int flag, int argc,
char * const argv[])
{
if (argc < 1 || argc > 2)
return CMD_RET_USAGE;
int min_dev = 0;
int max_dev = CONFIG_HOST_MAX_DEVICES - 1;
if (argc >= 2) {
char *ep;
char *dev_str = argv[1];
int dev = simple_strtoul(dev_str, &ep, 16);
if (*ep) {
printf("** Bad device specification %s **\n", dev_str);
return CMD_RET_USAGE;
}
min_dev = dev;
max_dev = dev;
}
int dev;
printf("%3s %12s %s\n", "dev", "blocks", "path");
for (dev = min_dev; dev <= max_dev; dev++) {
block_dev_desc_t *blk_dev;
int ret;
printf("%3d ", dev);
ret = host_get_dev_err(dev, &blk_dev);
if (ret) {
if (ret == -ENOENT)
puts("Not bound to a backing file\n");
else if (ret == -ENODEV)
puts("Invalid host device number\n");
continue;
}
struct host_block_dev *host_dev = blk_dev->priv;
printf("%12lu %s\n", (unsigned long)blk_dev->lba,
host_dev->filename);
}
return 0;
}
static cmd_tbl_t cmd_sandbox_sub[] = { static cmd_tbl_t cmd_sandbox_sub[] = {
U_BOOT_CMD_MKENT(load, 7, 0, do_sandbox_load, "", ""), U_BOOT_CMD_MKENT(load, 7, 0, do_sandbox_load, "", ""),
U_BOOT_CMD_MKENT(ls, 3, 0, do_sandbox_ls, "", ""), U_BOOT_CMD_MKENT(ls, 3, 0, do_sandbox_ls, "", ""),
U_BOOT_CMD_MKENT(save, 6, 0, do_sandbox_save, "", ""), U_BOOT_CMD_MKENT(save, 6, 0, do_sandbox_save, "", ""),
U_BOOT_CMD_MKENT(bind, 3, 0, do_sandbox_bind, "", ""),
U_BOOT_CMD_MKENT(info, 3, 0, do_sandbox_info, "", ""),
}; };
static int do_sandbox(cmd_tbl_t *cmdtp, int flag, int argc, static int do_sandbox(cmd_tbl_t *cmdtp, int flag, int argc,
...@@ -57,4 +119,6 @@ U_BOOT_CMD( ...@@ -57,4 +119,6 @@ U_BOOT_CMD(
"sb ls host <filename> - list files on host\n" "sb ls host <filename> - list files on host\n"
"sb save host <dev> <filename> <addr> <bytes> [<offset>] - " "sb save host <dev> <filename> <addr> <bytes> [<offset>] - "
"save a file to host\n" "save a file to host\n"
"sb bind <dev> [<filename>] - bind \"host\" device to file\n"
"sb info [<dev>] - show device binding & info"
); );
...@@ -42,6 +42,9 @@ static const struct block_drvr block_drvr[] = { ...@@ -42,6 +42,9 @@ static const struct block_drvr block_drvr[] = {
#endif #endif
#if defined(CONFIG_SYSTEMACE) #if defined(CONFIG_SYSTEMACE)
{ .name = "ace", .get_dev = systemace_get_dev, }, { .name = "ace", .get_dev = systemace_get_dev, },
#endif
#if defined(CONFIG_SANDBOX)
{ .name = "host", .get_dev = host_get_dev, },
#endif #endif
{ }, { },
}; };
...@@ -286,6 +289,9 @@ static void print_part_header (const char *type, block_dev_desc_t * dev_desc) ...@@ -286,6 +289,9 @@ static void print_part_header (const char *type, block_dev_desc_t * dev_desc)
case IF_TYPE_MMC: case IF_TYPE_MMC:
puts ("MMC"); puts ("MMC");
break; break;
case IF_TYPE_HOST:
puts("HOST");
break;
default: default:
puts ("UNKNOWN"); puts ("UNKNOWN");
break; break;
......
...@@ -18,5 +18,6 @@ obj-$(CONFIG_SATA_DWC) += sata_dwc.o ...@@ -18,5 +18,6 @@ obj-$(CONFIG_SATA_DWC) += sata_dwc.o
obj-$(CONFIG_SATA_SIL3114) += sata_sil3114.o obj-$(CONFIG_SATA_SIL3114) += sata_sil3114.o
obj-$(CONFIG_SATA_SIL) += sata_sil.o obj-$(CONFIG_SATA_SIL) += sata_sil.o
obj-$(CONFIG_IDE_SIL680) += sil680.o obj-$(CONFIG_IDE_SIL680) += sil680.o
obj-$(CONFIG_SANDBOX) += sandbox.o
obj-$(CONFIG_SCSI_SYM53C8XX) += sym53c8xx.o obj-$(CONFIG_SCSI_SYM53C8XX) += sym53c8xx.o
obj-$(CONFIG_SYSTEMACE) += systemace.o obj-$(CONFIG_SYSTEMACE) += systemace.o
/*
* Copyright (C) 2013 Henrik Nordstrom <henrik@henriknordstrom.net>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <config.h>
#include <common.h>
#include <part.h>
#include <os.h>
#include <malloc.h>
#include <sandboxblockdev.h>
#include <asm/errno.h>
static struct host_block_dev host_devices[CONFIG_HOST_MAX_DEVICES];
static struct host_block_dev *find_host_device(int dev)
{
if (dev >= 0 && dev < CONFIG_HOST_MAX_DEVICES)
return &host_devices[dev];
return NULL;
}
static unsigned long host_block_read(int dev, unsigned long start,
lbaint_t blkcnt, void *buffer)
{
struct host_block_dev *host_dev = find_host_device(dev);
if (!host_dev)
return -1;
if (os_lseek(host_dev->fd,
start * host_dev->blk_dev.blksz,
OS_SEEK_SET) == -1) {
printf("ERROR: Invalid position\n");
return -1;
}
ssize_t len = os_read(host_dev->fd, buffer,
blkcnt * host_dev->blk_dev.blksz);
if (len >= 0)
return len / host_dev->blk_dev.blksz;
return -1;
}
static unsigned long host_block_write(int dev, unsigned long start,
lbaint_t blkcnt, const void *buffer)
{
struct host_block_dev *host_dev = find_host_device(dev);
if (os_lseek(host_dev->fd,
start * host_dev->blk_dev.blksz,
OS_SEEK_SET) == -1) {
printf("ERROR: Invalid position\n");
return -1;
}
ssize_t len = os_write(host_dev->fd, buffer, blkcnt *
host_dev->blk_dev.blksz);
if (len >= 0)
return len / host_dev->blk_dev.blksz;
return -1;
}
int host_dev_bind(int dev, char *filename)
{
struct host_block_dev *host_dev = find_host_device(dev);
if (!host_dev)
return -1;
if (host_dev->blk_dev.priv) {
os_close(host_dev->fd);
host_dev->blk_dev.priv = NULL;
}
if (host_dev->filename)
free(host_dev->filename);
if (filename && *filename) {
host_dev->filename = strdup(filename);
} else {
host_dev->filename = NULL;
return 0;
}
host_dev->fd = os_open(host_dev->filename, OS_O_RDWR);
if (host_dev->fd == -1) {
printf("Failed to access host backing file '%s'\n",
host_dev->filename);
return 1;
}
block_dev_desc_t *blk_dev = &host_dev->blk_dev;
blk_dev->if_type = IF_TYPE_HOST;
blk_dev->priv = host_dev;
blk_dev->blksz = 512;
blk_dev->lba = os_lseek(host_dev->fd, 0, OS_SEEK_END) / blk_dev->blksz;
blk_dev->block_read = host_block_read;
blk_dev->block_write = host_block_write;
blk_dev->dev = dev;
blk_dev->part_type = PART_TYPE_UNKNOWN;
init_part(blk_dev);
return 0;
}
int host_get_dev_err(int dev, block_dev_desc_t **blk_devp)
{
struct host_block_dev *host_dev = find_host_device(dev);
if (!host_dev)
return -ENODEV;
if (!host_dev->blk_dev.priv)
return -ENOENT;
*blk_devp = &host_dev->blk_dev;
return 0;
}
block_dev_desc_t *host_get_dev(int dev)
{
block_dev_desc_t *blk_dev;
if (host_get_dev_err(dev, &blk_dev))
return NULL;
return blk_dev;
}
...@@ -50,7 +50,8 @@ ...@@ -50,7 +50,8 @@
defined(CONFIG_CMD_PART) || \ defined(CONFIG_CMD_PART) || \
defined(CONFIG_CMD_GPT) || \ defined(CONFIG_CMD_GPT) || \
defined(CONFIG_MMC) || \ defined(CONFIG_MMC) || \
defined(CONFIG_SYSTEMACE) defined(CONFIG_SYSTEMACE) || \
defined(CONFIG_SANDBOX)
#define HAVE_BLOCK_DEVICE #define HAVE_BLOCK_DEVICE
#endif #endif
......
...@@ -39,6 +39,9 @@ ...@@ -39,6 +39,9 @@
#define CONFIG_CMD_FAT #define CONFIG_CMD_FAT
#define CONFIG_CMD_EXT4 #define CONFIG_CMD_EXT4
#define CONFIG_CMD_EXT4_WRITE #define CONFIG_CMD_EXT4_WRITE
#define CONFIG_CMD_PART
#define CONFIG_DOS_PARTITION
#define CONFIG_HOST_MAX_DEVICES 4
#define CONFIG_SYS_VSNPRINTF #define CONFIG_SYS_VSNPRINTF
......
...@@ -58,6 +58,8 @@ typedef struct block_dev_desc { ...@@ -58,6 +58,8 @@ typedef struct block_dev_desc {
#define IF_TYPE_MMC 6 #define IF_TYPE_MMC 6
#define IF_TYPE_SD 7 #define IF_TYPE_SD 7
#define IF_TYPE_SATA 8 #define IF_TYPE_SATA 8
#define IF_TYPE_HOST 9
#define IF_TYPE_MAX 10 /* Max number of IF_TYPE_* supported */
/* Part types */ /* Part types */
#define PART_TYPE_UNKNOWN 0x00 #define PART_TYPE_UNKNOWN 0x00
...@@ -102,6 +104,8 @@ block_dev_desc_t* usb_stor_get_dev(int dev); ...@@ -102,6 +104,8 @@ block_dev_desc_t* usb_stor_get_dev(int dev);
block_dev_desc_t* mmc_get_dev(int dev); block_dev_desc_t* mmc_get_dev(int dev);
block_dev_desc_t* systemace_get_dev(int dev); block_dev_desc_t* systemace_get_dev(int dev);
block_dev_desc_t* mg_disk_get_dev(int dev); block_dev_desc_t* mg_disk_get_dev(int dev);
block_dev_desc_t *host_get_dev(int dev);
int host_get_dev_err(int dev, block_dev_desc_t **blk_devp);
/* disk/part.c */ /* disk/part.c */
int get_partition_info (block_dev_desc_t * dev_desc, int part, disk_partition_t *info); int get_partition_info (block_dev_desc_t * dev_desc, int part, disk_partition_t *info);
...@@ -123,6 +127,7 @@ static inline block_dev_desc_t* usb_stor_get_dev(int dev) { return NULL; } ...@@ -123,6 +127,7 @@ static inline block_dev_desc_t* usb_stor_get_dev(int dev) { return NULL; }
static inline block_dev_desc_t* mmc_get_dev(int dev) { return NULL; } static inline block_dev_desc_t* mmc_get_dev(int dev) { return NULL; }
static inline block_dev_desc_t* systemace_get_dev(int dev) { return NULL; } static inline block_dev_desc_t* systemace_get_dev(int dev) { return NULL; }
static inline block_dev_desc_t* mg_disk_get_dev(int dev) { return NULL; } static inline block_dev_desc_t* mg_disk_get_dev(int dev) { return NULL; }
static inline block_dev_desc_t *host_get_dev(int dev) { return NULL; }
static inline int get_partition_info (block_dev_desc_t * dev_desc, int part, static inline int get_partition_info (block_dev_desc_t * dev_desc, int part,
disk_partition_t *info) { return -1; } disk_partition_t *info) { return -1; }
......
/*
* Copyright (c) 2013, Henrik Nordstrom <henrik@henriknordstrom.net>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#ifndef __SANDBOX_BLOCK_DEV__
#define __SANDBOX_BLOCK_DEV__
struct host_block_dev {
block_dev_desc_t blk_dev;
char *filename;
int fd;
};
int host_dev_bind(int dev, char *filename);
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment