Merge tag 'ti-v2020.07-rc3' of https://gitlab.denx.de/u-boot/custodians/u-boot-ti
[oweals/u-boot.git] / cmd / mtdparts.c
index 86a4689616c777df4569dee2bd954c1de7910708..5220142b9ab25d704229832d84fe46e1241ff391 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2002
  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
@@ -20,8 +21,6 @@
  *
  *   $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $
  *   Copyright 2002 SYSGO Real-Time Solutions GmbH
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 /*
  * mtdids=<idmap>[,<idmap>,...]
  *
  * <idmap>    := <dev-id>=<mtd-id>
- * <dev-id>   := 'nand'|'nor'|'onenand'<dev-num>
+ * <dev-id>   := 'nand'|'nor'|'onenand'|'spi-nand'<dev-num>
  * <dev-num>  := mtd device number, 0...
  * <mtd-id>   := unique device tag used by linux kernel to find mtd device (mtd->name)
  *
  *
  * 'mtdparts' - partition list
  *
- * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]
+ * mtdparts=[mtdparts=]<mtd-def>[;<mtd-def>...]
  *
  * <mtd-def>  := <mtd-id>:<part-def>[,<part-def>...]
  * <mtd-id>   := unique device tag used by linux kernel to find mtd device (mtd->name)
  *
  * 1 NOR Flash, with 1 single writable partition:
  * mtdids=nor0=edb7312-nor
- * mtdparts=mtdparts=edb7312-nor:-
+ * mtdparts=[mtdparts=]edb7312-nor:-
  *
  * 1 NOR Flash with 2 partitions, 1 NAND with one
  * mtdids=nor0=edb7312-nor,nand0=edb7312-nand
- * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
+ * mtdparts=[mtdparts=]edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
  *
  */
 
 #include <common.h>
 #include <command.h>
+#include <env.h>
+#include <log.h>
 #include <malloc.h>
 #include <jffs2/load_kernel.h>
 #include <linux/list.h>
@@ -81,7 +82,7 @@
 #include <linux/mtd/mtd.h>
 
 #if defined(CONFIG_CMD_NAND)
-#include <linux/mtd/nand.h>
+#include <linux/mtd/rawnand.h>
 #include <nand.h>
 #endif
 
@@ -109,25 +110,33 @@ DECLARE_GLOBAL_DATA_PTR;
 #define MTD_WRITEABLE_CMD              1
 
 /* default values for mtdids and mtdparts variables */
-#if defined(MTDIDS_DEFAULT)
-static const char *const mtdids_default = MTDIDS_DEFAULT;
+#if !defined(MTDIDS_DEFAULT)
+#ifdef CONFIG_MTDIDS_DEFAULT
+#define MTDIDS_DEFAULT CONFIG_MTDIDS_DEFAULT
 #else
-static const char *const mtdids_default = NULL;
+#define MTDIDS_DEFAULT NULL
 #endif
-
-#if defined(MTDPARTS_DEFAULT)
-static const char *const mtdparts_default = MTDPARTS_DEFAULT;
+#endif
+#if !defined(MTDPARTS_DEFAULT)
+#ifdef CONFIG_MTDPARTS_DEFAULT
+#define MTDPARTS_DEFAULT CONFIG_MTDPARTS_DEFAULT
 #else
-static const char *const mtdparts_default = NULL;
+#define MTDPARTS_DEFAULT NULL
+#endif
+#endif
+#if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
+extern void board_mtdparts_default(const char **mtdids, const char **mtdparts);
 #endif
+static const char *mtdids_default = MTDIDS_DEFAULT;
+static const char *mtdparts_default = MTDPARTS_DEFAULT;
 
 /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
 #define MTDIDS_MAXLEN          128
 #define MTDPARTS_MAXLEN                512
 #define PARTITION_MAXLEN       16
-static char last_ids[MTDIDS_MAXLEN];
-static char last_parts[MTDPARTS_MAXLEN];
-static char last_partition[PARTITION_MAXLEN];
+static char last_ids[MTDIDS_MAXLEN + 1];
+static char last_parts[MTDPARTS_MAXLEN + 1];
+static char last_partition[PARTITION_MAXLEN + 1];
 
 /* low level jffs2 cache cleaning routine */
 extern void jffs2_free_cache(struct part_info *part);
@@ -142,6 +151,8 @@ static struct list_head devices;
 struct mtd_device *current_mtd_dev = NULL;
 u8 current_mtd_partnum = 0;
 
+u8 use_defaults;
+
 static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num);
 
 /* command line only routines */
@@ -168,13 +179,16 @@ static u64 memsize_parse (const char *const ptr, const char **retptr)
                case 'G':
                case 'g':
                        ret <<= 10;
+                       /* Fallthrough */
                case 'M':
                case 'm':
                        ret <<= 10;
+                       /* Fallthrough */
                case 'K':
                case 'k':
                        ret <<= 10;
                        (*retptr)++;
+                       /* Fallthrough */
                default:
                        break;
        }
@@ -229,19 +243,26 @@ static void index_partitions(void)
                        dev = list_entry(dentry, struct mtd_device, link);
                        if (dev == current_mtd_dev) {
                                mtddevnum += current_mtd_partnum;
-                               setenv_ulong("mtddevnum", mtddevnum);
+                               env_set_ulong("mtddevnum", mtddevnum);
+                               debug("=> mtddevnum %d,\n", mtddevnum);
                                break;
                        }
                        mtddevnum += dev->num_parts;
                }
 
                part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
-               setenv("mtddevname", part->name);
+               if (part) {
+                       env_set("mtddevname", part->name);
 
-               debug("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
+                       debug("=> mtddevname %s\n", part->name);
+               } else {
+                       env_set("mtddevname", NULL);
+
+                       debug("=> mtddevname NULL\n");
+               }
        } else {
-               setenv("mtddevnum", NULL);
-               setenv("mtddevname", NULL);
+               env_set("mtddevnum", NULL);
+               env_set("mtddevname", NULL);
 
                debug("=> mtddevnum NULL\n=> mtddevname NULL\n");
        }
@@ -260,12 +281,12 @@ static void current_save(void)
                sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_mtd_dev->id->type),
                                        current_mtd_dev->id->num, current_mtd_partnum);
 
-               setenv("partition", buf);
+               env_set("partition", buf);
                strncpy(last_partition, buf, 16);
 
                debug("=> partition %s\n", buf);
        } else {
-               setenv("partition", NULL);
+               env_set("partition", NULL);
                last_partition[0] = '\0';
 
                debug("=> partition NULL\n");
@@ -320,7 +341,7 @@ static int part_validate_eraseblock(struct mtdids *id, struct part_info *part)
 
        if (!mtd->numeraseregions) {
                /*
-                * Only one eraseregion (NAND, OneNAND or uniform NOR),
+                * Only one eraseregion (NAND, SPI-NAND, OneNAND or uniform NOR),
                 * checking for alignment is easy here
                 */
                offset = part->offset;
@@ -674,7 +695,7 @@ static int part_parse(const char *const partdef, const char **ret, struct part_i
                part->auto_name = 0;
        } else {
                /* auto generated name in form of size@offset */
-               sprintf(part->name, "0x%08llx@0x%08llx", size, offset);
+               snprintf(part->name, name_len, "0x%08llx@0x%08llx", size, offset);
                part->auto_name = 1;
        }
 
@@ -856,15 +877,12 @@ static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_
                return 1;
        }
 
-#ifdef DEBUG
        pend = strchr(p, ';');
-#endif
        debug("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
                        id->type, MTD_DEV_TYPE(id->type),
                        id->num, id->mtd_id);
        debug("parsing partitions %.*s\n", (int)(pend ? pend - p : strlen(p)), p);
 
-
        /* parse partitions */
        num_parts = 0;
 
@@ -902,12 +920,6 @@ static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_
                return 1;
        }
 
-       if (num_parts == 0) {
-               printf("no partitions for device %s%d (%s)\n",
-                               MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
-               return 1;
-       }
-
        debug("\ntotal partitions: %d\n", num_parts);
 
        /* check for next device presence */
@@ -1020,7 +1032,7 @@ static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_
 }
 
 /**
- * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
+ * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'|'spi-nand'<dev-num>,
  * return device type and number.
  *
  * @param id string describing device id
@@ -1044,6 +1056,9 @@ int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type,
        } else if (strncmp(p, "onenand", 7) == 0) {
                *dev_type = MTD_DEV_TYPE_ONENAND;
                p += 7;
+       } else if (strncmp(p, "spi-nand", 8) == 0) {
+               *dev_type = MTD_DEV_TYPE_SPINAND;
+               p += 8;
        } else {
                printf("incorrect device type in %s\n", id);
                return 1;
@@ -1086,9 +1101,6 @@ static int generate_mtdparts(char *buf, u32 buflen)
                return 0;
        }
 
-       strcpy(p, "mtdparts=");
-       p += 9;
-
        list_for_each(dentry, &devices) {
                dev = list_entry(dentry, struct mtd_device, link);
 
@@ -1203,9 +1215,9 @@ static int generate_mtdparts_save(char *buf, u32 buflen)
        ret = generate_mtdparts(buf, buflen);
 
        if ((buf[0] != '\0') && (ret == 0))
-               setenv("mtdparts", buf);
+               env_set("mtdparts", buf);
        else
-               setenv("mtdparts", NULL);
+               env_set("mtdparts", NULL);
 
        return ret;
 }
@@ -1222,11 +1234,11 @@ static uint64_t net_part_size(struct mtd_info *mtd, struct part_info *part)
 {
        uint64_t i, net_size = 0;
 
-       if (!mtd->block_isbad)
+       if (!mtd->_block_isbad)
                return part->size;
 
        for (i = 0; i < part->size; i += mtd->erasesize) {
-               if (!mtd->block_isbad(mtd, part->offset + i))
+               if (!mtd->_block_isbad(mtd, part->offset + i))
                        net_size += mtd->erasesize;
        }
 
@@ -1263,7 +1275,7 @@ static void print_partition_table(void)
                        part = list_entry(pentry, struct part_info, link);
                        net_size = net_part_size(mtd, part);
                        size_note = part->size == net_size ? " " : " (!)";
-                       printf("%2d: %-20s0x%08x\t0x%08x%s\t0x%08x\t%d\n",
+                       printf("%2d: %-20s0x%08llx\t0x%08x%s\t0x%08llx\t%d\n",
                                        part_num, part->name, part->size,
                                        net_size, size_note, part->offset,
                                        part->mask_flags);
@@ -1491,7 +1503,7 @@ static int spread_partitions(void)
                        part = list_entry(pentry, struct part_info, link);
 
                        debug("spread_partitions: device = %s%d, partition %d ="
-                               " (%s) 0x%08x@0x%08x\n",
+                               " (%s) 0x%08llx@0x%08llx\n",
                                MTD_DEV_TYPE(dev->id->type), dev->id->num,
                                part_num, part->name, part->size,
                                part->offset);
@@ -1515,6 +1527,23 @@ static int spread_partitions(void)
 }
 #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
 
+/**
+ * The mtdparts variable tends to be long. If we need to access it
+ * before the env is relocated, then we need to use our own stack
+ * buffer.  gd->env_buf will be too small.
+ *
+ * @param buf temporary buffer pointer MTDPARTS_MAXLEN long
+ * @return mtdparts variable string, NULL if not found
+ */
+static const char *env_get_mtdparts(char *buf)
+{
+       if (gd->flags & GD_FLG_ENV_READY)
+               return env_get("mtdparts");
+       if (env_get_f("mtdparts", buf, MTDPARTS_MAXLEN) != -1)
+               return buf;
+       return NULL;
+}
+
 /**
  * Accept character string describing mtd partitions and call device_parse()
  * for each entry. Add created devices to the global devices list.
@@ -1524,12 +1553,12 @@ static int spread_partitions(void)
  */
 static int parse_mtdparts(const char *const mtdparts)
 {
-       const char *p = mtdparts;
+       const char *p;
        struct mtd_device *dev;
        int err = 1;
        char tmp_parts[MTDPARTS_MAXLEN];
 
-       debug("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
+       debug("\n---parse_mtdparts---\nmtdparts = %s\n\n", mtdparts);
 
        /* delete all devices and partitions */
        if (mtd_devices_init() != 0) {
@@ -1538,20 +1567,15 @@ static int parse_mtdparts(const char *const mtdparts)
        }
 
        /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */
-       if (gd->flags & GD_FLG_ENV_READY) {
-               p = getenv("mtdparts");
-       } else {
-               p = tmp_parts;
-               getenv_f("mtdparts", tmp_parts, MTDPARTS_MAXLEN);
-       }
+       p = env_get_mtdparts(tmp_parts);
+       if (!p)
+               p = mtdparts;
 
-       if (strncmp(p, "mtdparts=", 9) != 0) {
-               printf("mtdparts variable doesn't start with 'mtdparts='\n");
-               return err;
-       }
-       p += 9;
+       /* Skip the useless prefix, if any */
+       if (strncmp(p, "mtdparts=", 9) == 0)
+               p += 9;
 
-       while (p && (*p != '\0')) {
+       while (*p != '\0') {
                err = 1;
                if ((device_parse(p, &p, &dev) != 0) || (!dev))
                        break;
@@ -1570,11 +1594,11 @@ static int parse_mtdparts(const char *const mtdparts)
                err = 0;
        }
        if (err == 1) {
+               free(dev);
                device_delall(&devices);
-               return 1;
        }
 
-       return 0;
+       return err;
 }
 
 /**
@@ -1612,7 +1636,7 @@ static int parse_mtdids(const char *const ids)
        while(p && (*p != '\0')) {
 
                ret = 1;
-               /* parse 'nor'|'nand'|'onenand'<dev-num> */
+               /* parse 'nor'|'nand'|'onenand'|'spi-nand'<dev-num> */
                if (mtd_id_parse(p, &p, &type, &num) != 0)
                        break;
 
@@ -1688,6 +1712,7 @@ static int parse_mtdids(const char *const ids)
        return 0;
 }
 
+
 /**
  * Parse and initialize global mtdids mapping and create global
  * device/partition list.
@@ -1700,37 +1725,32 @@ int mtdparts_init(void)
        const char *ids, *parts;
        const char *current_partition;
        int ids_changed;
-       char tmp_ep[PARTITION_MAXLEN];
+       char tmp_ep[PARTITION_MAXLEN + 1];
        char tmp_parts[MTDPARTS_MAXLEN];
 
        debug("\n---mtdparts_init---\n");
        if (!initialized) {
                INIT_LIST_HEAD(&mtdids);
                INIT_LIST_HEAD(&devices);
-               memset(last_ids, 0, MTDIDS_MAXLEN);
-               memset(last_parts, 0, MTDPARTS_MAXLEN);
-               memset(last_partition, 0, PARTITION_MAXLEN);
+               memset(last_ids, 0, sizeof(last_ids));
+               memset(last_parts, 0, sizeof(last_parts));
+               memset(last_partition, 0, sizeof(last_partition));
+#if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
+               board_mtdparts_default(&mtdids_default, &mtdparts_default);
+#endif
+               use_defaults = 1;
                initialized = 1;
        }
 
        /* get variables */
-       ids = getenv("mtdids");
-       /*
-        * The mtdparts variable tends to be long. If we need to access it
-        * before the env is relocated, then we need to use our own stack
-        * buffer.  gd->env_buf will be too small.
-        */
-       if (gd->flags & GD_FLG_ENV_READY) {
-               parts = getenv("mtdparts");
-       } else {
-               parts = tmp_parts;
-               getenv_f("mtdparts", tmp_parts, MTDPARTS_MAXLEN);
-       }
-       current_partition = getenv("partition");
+       ids = env_get("mtdids");
+       parts = env_get_mtdparts(tmp_parts);
+       current_partition = env_get("partition");
 
        /* save it for later parsing, cannot rely on current partition pointer
         * as 'partition' variable may be updated during init */
-       tmp_ep[0] = '\0';
+       memset(tmp_parts, 0, sizeof(tmp_parts));
+       memset(tmp_ep, 0, sizeof(tmp_ep));
        if (current_partition)
                strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
 
@@ -1742,12 +1762,12 @@ int mtdparts_init(void)
        debug("last_partition : %s\n", last_partition);
        debug("env_partition  : %s\n", current_partition);
 
-       /* if mtdids varible is empty try to use defaults */
+       /* if mtdids variable is empty try to use defaults */
        if (!ids) {
                if (mtdids_default) {
                        debug("mtdids variable not defined, using default\n");
                        ids = mtdids_default;
-                       setenv("mtdids", (char *)ids);
+                       env_set("mtdids", (char *)ids);
                } else {
                        printf("mtdids not defined, no default present\n");
                        return 1;
@@ -1758,10 +1778,16 @@ int mtdparts_init(void)
                return 1;
        }
 
-       /* do no try to use defaults when mtdparts variable is not defined,
-        * just check the length */
-       if (!parts)
-               printf("mtdparts variable not set, see 'help mtdparts'\n");
+       /* use defaults when mtdparts variable is not defined
+        * once mtdparts is saved environment, drop use_defaults flag */
+       if (!parts) {
+               if (mtdparts_default && use_defaults) {
+                       parts = mtdparts_default;
+                       if (env_set("mtdparts", (char *)parts) == 0)
+                               use_defaults = 0;
+               } else
+                       printf("mtdparts variable not set, see 'help mtdparts'\n");
+       }
 
        if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
                printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
@@ -1827,7 +1853,7 @@ int mtdparts_init(void)
                        current_mtd_partnum = pnum;
                        current_save();
                }
-       } else if (getenv("partition") == NULL) {
+       } else if (env_get("partition") == NULL) {
                debug("no partition variable set, setting...\n");
                current_save();
        }
@@ -1890,7 +1916,8 @@ static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part
  * @param argv arguments list
  * @return 0 on success, 1 otherwise
  */
-static int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+static int do_chpart(struct cmd_tbl *cmdtp, int flag, int argc,
+                    char *const argv[])
 {
 /* command line only */
        struct mtd_device *dev;
@@ -1928,14 +1955,15 @@ static int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  * @param argv arguments list
  * @return 0 on success, 1 otherwise
  */
-static int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc,
-                      char * const argv[])
+static int do_mtdparts(struct cmd_tbl *cmdtp, int flag, int argc,
+                      char *const argv[])
 {
        if (argc == 2) {
                if (strcmp(argv[1], "default") == 0) {
-                       setenv("mtdids", (char *)mtdids_default);
-                       setenv("mtdparts", (char *)mtdparts_default);
-                       setenv("partition", NULL);
+                       env_set("mtdids", NULL);
+                       env_set("mtdparts", NULL);
+                       env_set("partition", NULL);
+                       use_defaults = 1;
 
                        mtdparts_init();
                        return 0;
@@ -1943,7 +1971,7 @@ static int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc,
                        /* this may be the first run, initialize lists if needed */
                        mtdparts_init();
 
-                       setenv("mtdparts", NULL);
+                       env_set("mtdparts", NULL);
 
                        /* mtd_devices_init() calls current_save() */
                        return mtd_devices_init();
@@ -2009,7 +2037,7 @@ static int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc,
 
                if (!strcmp(&argv[1][3], ".spread")) {
                        spread_partition(mtd, p, &next_offset);
-                       debug("increased %s to %d bytes\n", p->name, p->size);
+                       debug("increased %s to %llu bytes\n", p->name, p->size);
                }
 #endif
 
@@ -2085,7 +2113,7 @@ static char mtdparts_help_text[] =
        "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
        "mtdids=<idmap>[,<idmap>,...]\n\n"
        "<idmap>    := <dev-id>=<mtd-id>\n"
-       "<dev-id>   := 'nand'|'nor'|'onenand'<dev-num>\n"
+       "<dev-id>   := 'nand'|'nor'|'onenand'|'spi-nand'<dev-num>\n"
        "<dev-num>  := mtd device number, 0...\n"
        "<mtd-id>   := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
        "'mtdparts' - partition list\n\n"