Merge tag 'efi-2020-07-rc6' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / cmd / ubi.c
index 22ba5b1a2cedefe2f843f3464f10cce72e65adc9..171377cc66b547f039e12217e9fd9be1f90bc323 100644 (file)
--- a/cmd/ubi.c
+++ b/cmd/ubi.c
 #include <command.h>
 #include <env.h>
 #include <exports.h>
+#include <malloc.h>
 #include <memalign.h>
 #include <mtd.h>
 #include <nand.h>
 #include <onenand_uboot.h>
+#include <dm/devres.h>
 #include <linux/mtd/mtd.h>
 #include <linux/mtd/partitions.h>
 #include <linux/err.h>
@@ -249,6 +251,39 @@ out_err:
        return err;
 }
 
+static int ubi_rename_vol(char *oldname, char *newname)
+{
+       struct ubi_volume *vol;
+       struct ubi_rename_entry rename;
+       struct ubi_volume_desc desc;
+       struct list_head list;
+
+       vol = ubi_find_volume(oldname);
+       if (!vol) {
+               printf("%s: volume %s doesn't exist\n", __func__, oldname);
+               return ENODEV;
+       }
+
+       printf("Rename UBI volume %s to %s\n", oldname, newname);
+
+       if (ubi->ro_mode) {
+               printf("%s: ubi device is in read-only mode\n", __func__);
+               return EROFS;
+       }
+
+       rename.new_name_len = strlen(newname);
+       strcpy(rename.new_name, newname);
+       rename.remove = 0;
+       desc.vol = vol;
+       desc.mode = 0;
+       rename.desc = &desc;
+       INIT_LIST_HEAD(&rename.list);
+       INIT_LIST_HEAD(&list);
+       list_add(&rename.list, &list);
+
+       return ubi_rename_volumes(ubi, &list);
+}
+
 static int ubi_volume_continue_write(char *volume, void *buf, size_t size)
 {
        int err = 1;
@@ -493,7 +528,7 @@ int ubi_part(char *part_name, const char *vid_header_offset)
        return 0;
 }
 
-static int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
        int64_t size = 0;
        ulong addr = 0;
@@ -602,6 +637,9 @@ static int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                        return ubi_remove_vol(argv[2]);
        }
 
+       if (IS_ENABLED(CONFIG_CMD_UBI_RENAME) && !strncmp(argv[1], "rename", 6))
+               return ubi_rename_vol(argv[2], argv[3]);
+
        if (strncmp(argv[1], "skipcheck", 9) == 0) {
                /* E.g., change skip_check flag */
                if (argc == 4) {
@@ -690,6 +728,9 @@ U_BOOT_CMD(
                " - Read volume to address with size\n"
        "ubi remove[vol] volume"
                " - Remove volume\n"
+#if IS_ENABLED(CONFIG_CMD_UBI_RENAME)
+       "ubi rename oldname newname\n"
+#endif
        "ubi skipcheck volume on/off - Set or clear skip_check flag in volume header\n"
        "[Legends]\n"
        " volume: character name\n"