]> Git Repo - J-u-boot.git/blobdiff - cmd/host.c
Merge patch series "Add OPP_LOW support for J7200"
[J-u-boot.git] / cmd / host.c
index f7d3eae5b1ad1313f0ee8032fe69ac2df556be65..e03576b4d2def808687a02f7db48d359a66426ef 100644 (file)
  * Copyright (c) 2012, Google Inc.
  */
 
-#include <common.h>
+#include <command.h>
 #include <dm.h>
 #include <fs.h>
 #include <part.h>
-#include <sandboxblockdev.h>
+#include <sandbox_host.h>
+#include <dm/device_compat.h>
+#include <dm/device-internal.h>
+#include <dm/uclass-internal.h>
 #include <linux/errno.h>
+#include <linux/log2.h>
 
-static int host_curr_device = -1;
-
-static int do_host_load(cmd_tbl_t *cmdtp, int flag, int argc,
-                          char * const argv[])
+static int do_host_load(struct cmd_tbl *cmdtp, int flag, int argc,
+                       char *const argv[])
 {
        return do_load(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
 }
 
-static int do_host_ls(cmd_tbl_t *cmdtp, int flag, int argc,
-                          char * const argv[])
+static int do_host_ls(struct cmd_tbl *cmdtp, int flag, int argc,
+                     char *const argv[])
 {
        return do_ls(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
 }
 
-static int do_host_size(cmd_tbl_t *cmdtp, int flag, int argc,
-                          char * const argv[])
+static int do_host_size(struct cmd_tbl *cmdtp, int flag, int argc,
+                       char *const argv[])
 {
        return do_size(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
 }
 
-static int do_host_save(cmd_tbl_t *cmdtp, int flag, int argc,
-                          char * const argv[])
+static int do_host_save(struct cmd_tbl *cmdtp, int flag, int argc,
+                       char *const argv[])
 {
        return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
 }
 
-static int do_host_bind(cmd_tbl_t *cmdtp, int flag, int argc,
-                          char * const argv[])
+static int do_host_bind(struct cmd_tbl *cmdtp, int flag, int argc,
+                       char *const argv[])
 {
-       if (argc < 2 || argc > 3)
+       bool removable = false;
+       struct udevice *dev;
+       const char *label;
+       char *file;
+       unsigned long blksz = DEFAULT_BLKSZ;
+       int ret;
+
+       /* Skip 'bind' */
+       argc--;
+       argv++;
+       if (argc < 2)
                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);
+
+       if (!strcmp(argv[0], "-r")) {
+               removable = true;
+               argc--;
+               argv++;
+       }
+
+       if (argc < 2 || argc > 3)
                return CMD_RET_USAGE;
+       label = argv[0];
+       file = argv[1];
+       if (argc > 2) {
+               blksz = dectoul(argv[2], NULL);
+               if (blksz < DEFAULT_BLKSZ || !is_power_of_2(blksz)) {
+                       printf("blksz must be >= 512 and power of 2\n");
+                       return CMD_RET_FAILURE;
+               }
+       }
+
+       ret = host_create_attach_file(label, file, removable, blksz, &dev);
+       if (ret) {
+               printf("Cannot create device / bind file\n");
+               return CMD_RET_FAILURE;
        }
-       return host_dev_bind(dev, file);
+
+       return 0;
 }
 
-static int do_host_info(cmd_tbl_t *cmdtp, int flag, int argc,
-                          char * const argv[])
+/**
+ * parse_host_label() - Parse a device label or sequence number
+ *
+ * This shows an error if it returns NULL
+ *
+ * @label: String containing the label or sequence number
+ * Returns: Associated device, or NULL if not found
+ */
+static struct udevice *parse_host_label(const char *label)
 {
-       if (argc < 1 || argc > 2)
-               return CMD_RET_USAGE;
-       int min_dev = 0;
-       int max_dev = CONFIG_HOST_MAX_DEVICES - 1;
-       if (argc >= 2) {
+       struct udevice *dev;
+
+       dev = host_find_by_label(label);
+       if (!dev) {
+               int devnum;
                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;
+
+               devnum = hextoul(label, &ep);
+               if (*ep ||
+                   uclass_find_device_by_seq(UCLASS_HOST, devnum, &dev)) {
+                       printf("No such device '%s'\n", label);
+                       return NULL;
                }
-               min_dev = dev;
-               max_dev = dev;
        }
-       int dev;
-       printf("%3s %12s %s\n", "dev", "blocks", "path");
-       for (dev = min_dev; dev <= max_dev; dev++) {
-               struct blk_desc *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;
-
-#ifdef CONFIG_BLK
-               host_dev = dev_get_priv(blk_dev->bdev);
-#else
-               host_dev = blk_dev->priv;
-#endif
-               printf("%12lu %s\n", (unsigned long)blk_dev->lba,
-                      host_dev->filename);
+
+       return dev;
+}
+
+static int do_host_unbind(struct cmd_tbl *cmdtp, int flag, int argc,
+                         char *const argv[])
+{
+       struct udevice *dev;
+       const char *label;
+       int ret;
+
+       if (argc < 2)
+               return CMD_RET_USAGE;
+
+       label = argv[1];
+       dev = parse_host_label(label);
+       if (!dev)
+               return CMD_RET_FAILURE;
+
+       ret = host_detach_file(dev);
+       if (ret) {
+               printf("Cannot detach file (err=%d)\n", ret);
+               return CMD_RET_FAILURE;
+       }
+
+       ret = device_unbind(dev);
+       if (ret) {
+               printf("Cannot attach file\n");
+               ret = device_unbind(dev);
+               if (ret)
+                       printf("Cannot unbind device '%s'\n", dev->name);
+               return CMD_RET_FAILURE;
        }
+
        return 0;
 }
 
-static int do_host_dev(cmd_tbl_t *cmdtp, int flag, int argc,
-                      char * const argv[])
+static void show_host_dev(struct udevice *dev)
 {
-       int dev;
-       char *ep;
-       struct blk_desc *blk_dev;
+       struct host_sb_plat *plat = dev_get_plat(dev);
+       struct blk_desc *desc;
+       struct udevice *blk;
        int ret;
 
+       printf("%3d ", dev_seq(dev));
+       if (!plat->fd) {
+               printf("Not bound to a backing file\n");
+               return;
+       }
+       ret = blk_get_from_parent(dev, &blk);
+       if (ret)  /* cannot happen */
+               return;
+
+       desc = dev_get_uclass_plat(blk);
+       printf("%12lu %6lu %-15s %s\n", (unsigned long)desc->lba, desc->blksz,
+              plat->label, plat->filename);
+}
+
+static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc,
+                       char *const argv[])
+{
+       struct udevice *dev;
+
+       if (argc < 1)
+               return CMD_RET_USAGE;
+
+       dev = NULL;
+       if (argc >= 2) {
+               dev = parse_host_label(argv[1]);
+               if (!dev)
+                       return CMD_RET_FAILURE;
+       }
+
+       printf("%3s %12s %6s %-15s %s\n",
+              "dev", "blocks", "blksz", "label", "path");
+       if (dev) {
+               show_host_dev(dev);
+       } else {
+               struct uclass *uc;
+
+               uclass_id_foreach_dev(UCLASS_HOST, dev, uc)
+                       show_host_dev(dev);
+       }
+
+       return 0;
+}
+
+static int do_host_dev(struct cmd_tbl *cmdtp, int flag, int argc,
+                      char *const argv[])
+{
+       struct udevice *dev;
+       const char *label;
+
        if (argc < 1 || argc > 3)
                return CMD_RET_USAGE;
 
        if (argc == 1) {
-               if (host_curr_device < 0) {
+               struct host_sb_plat *plat;
+
+               dev = host_get_cur_dev();
+               if (!dev) {
                        printf("No current host device\n");
-                       return 1;
+                       return CMD_RET_FAILURE;
                }
-               printf("Current host device %d\n", host_curr_device);
+               plat = dev_get_plat(dev);
+               printf("Current host device: %d: %s\n", dev_seq(dev),
+                      plat->label);
                return 0;
        }
 
-       dev = simple_strtoul(argv[1], &ep, 16);
-       if (*ep) {
-               printf("** Bad device specification %s **\n", argv[2]);
-               return CMD_RET_USAGE;
-       }
+       label = argv[1];
+       dev = parse_host_label(argv[1]);
+       if (!dev)
+               return CMD_RET_FAILURE;
 
-       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");
-
-               return 1;
-       }
+       host_set_cur_dev(dev);
 
-       host_curr_device = dev;
        return 0;
 }
 
-static cmd_tbl_t cmd_host_sub[] = {
+static struct cmd_tbl cmd_host_sub[] = {
        U_BOOT_CMD_MKENT(load, 7, 0, do_host_load, "", ""),
        U_BOOT_CMD_MKENT(ls, 3, 0, do_host_ls, "", ""),
        U_BOOT_CMD_MKENT(save, 6, 0, do_host_save, "", ""),
        U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""),
-       U_BOOT_CMD_MKENT(bind, 3, 0, do_host_bind, "", ""),
+       U_BOOT_CMD_MKENT(bind, 4, 0, do_host_bind, "", ""),
+       U_BOOT_CMD_MKENT(unbind, 4, 0, do_host_unbind, "", ""),
        U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""),
        U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
 };
 
-static int do_host(cmd_tbl_t *cmdtp, int flag, int argc,
-                     char * const argv[])
+static int do_host(struct cmd_tbl *cmdtp, int flag, int argc,
+                  char *const argv[])
 {
-       cmd_tbl_t *c;
+       struct cmd_tbl *c;
 
        /* Skip past 'host' */
        argc--;
        argv++;
 
-       c = find_cmd_tbl(argv[0], cmd_host_sub,
-                        ARRAY_SIZE(cmd_host_sub));
+       c = find_cmd_tbl(argv[0], cmd_host_sub, ARRAY_SIZE(cmd_host_sub));
 
        if (c)
                return c->cmd(cmdtp, flag, argc, argv);
@@ -176,9 +262,12 @@ U_BOOT_CMD(
        "host save hostfs - <addr> <filename> <bytes> [<offset>] - "
                "save a file to host\n"
        "host size hostfs - <filename> - determine size of file on host\n"
-       "host bind <dev> [<filename>] - bind \"host\" device to file\n"
-       "host info [<dev>]            - show device binding & info\n"
-       "host dev [<dev>] - Set or retrieve the current host device\n"
+       "host bind [-r] <label> <filename> [<blksz>] - bind \"host\" device to file,\n"
+       "     and optionally set the device's logical block size\n"
+       "     -r = mark as removable\n"
+       "host unbind <label>     - unbind file from \"host\" device\n"
+       "host info [<label>]     - show device binding & info\n"
+       "host dev [<label>]      - set or retrieve the current host device\n"
        "host commands use the \"hostfs\" device. The \"host\" device is used\n"
        "with standard IO commands such as fatls or ext2load"
 );
This page took 0.036466 seconds and 4 git commands to generate.