]> git.ipfire.org Git - thirdparty/u-boot.git/blobdiff - cmd/mtdparts.c
Revert "Merge patch series "pxe: Allow extlinux booting without CMDLINE enabled""
[thirdparty/u-boot.git] / cmd / mtdparts.c
index 72bff92a9aee591060233b5169509ea06b05dffc..b31db73ebfc9759636f961e0edbf622acd95f6a0 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 <asm/global_data.h>
 #include <jffs2/load_kernel.h>
 #include <linux/list.h>
 #include <linux/ctype.h>
@@ -81,7 +83,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,20 +111,17 @@ DECLARE_GLOBAL_DATA_PTR;
 #define MTD_WRITEABLE_CMD              1
 
 /* default values for mtdids and mtdparts variables */
-#if !defined(MTDIDS_DEFAULT)
 #ifdef CONFIG_MTDIDS_DEFAULT
 #define MTDIDS_DEFAULT CONFIG_MTDIDS_DEFAULT
 #else
 #define MTDIDS_DEFAULT NULL
 #endif
-#endif
-#if !defined(MTDPARTS_DEFAULT)
 #ifdef CONFIG_MTDPARTS_DEFAULT
 #define MTDPARTS_DEFAULT CONFIG_MTDPARTS_DEFAULT
 #else
 #define MTDPARTS_DEFAULT NULL
 #endif
-#endif
+
 #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
 extern void board_mtdparts_default(const char **mtdids, const char **mtdparts);
 #endif
@@ -168,7 +167,7 @@ static int device_del(struct mtd_device *dev);
  *
  * @param ptr where parse begins
  * @param retptr output pointer to next char after parse completes (output)
- * @return resulting unsigned int
+ * Return: resulting unsigned int
  */
 static u64 memsize_parse (const char *const ptr, const char **retptr)
 {
@@ -178,13 +177,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;
        }
@@ -297,7 +299,7 @@ static void current_save(void)
  * @param type mtd type
  * @param num mtd number
  * @param mtd a pointer to an mtd_info instance (output)
- * @return 0 if device is valid, 1 otherwise
+ * Return: 0 if device is valid, 1 otherwise
  */
 static int get_mtd_info(u8 type, u8 num, struct mtd_info **mtd)
 {
@@ -321,7 +323,7 @@ static int get_mtd_info(u8 type, u8 num, struct mtd_info **mtd)
  *
  * @param id of the parent device
  * @param part partition to validate
- * @return 0 if partition is valid, 1 otherwise
+ * Return: 0 if partition is valid, 1 otherwise
  */
 static int part_validate_eraseblock(struct mtdids *id, struct part_info *part)
 {
@@ -337,7 +339,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;
@@ -408,7 +410,7 @@ static int part_validate_eraseblock(struct mtdids *id, struct part_info *part)
  *
  * @param id of the parent device
  * @param part partition to validate
- * @return 0 if partition is valid, 1 otherwise
+ * Return: 0 if partition is valid, 1 otherwise
  */
 static int part_validate(struct mtdids *id, struct part_info *part)
 {
@@ -444,7 +446,7 @@ static int part_validate(struct mtdids *id, struct part_info *part)
  *
  * @param dev device to delete partition from
  * @param part partition to delete
- * @return 0 on success, 1 otherwise
+ * Return: 0 on success, 1 otherwise
  */
 static int part_del(struct mtd_device *dev, struct part_info *part)
 {
@@ -572,7 +574,7 @@ static int part_sort_add(struct mtd_device *dev, struct part_info *part)
  *
  * @param dev device to which partition is added
  * @param part partition to be added
- * @return 0 on success, 1 otherwise
+ * Return: 0 on success, 1 otherwise
  */
 static int part_add(struct mtd_device *dev, struct part_info *part)
 {
@@ -594,7 +596,7 @@ static int part_add(struct mtd_device *dev, struct part_info *part)
  * @param partdef pointer to the partition definition string i.e. <part-def>
  * @param ret output pointer to next char after parse completes (output)
  * @param retpart pointer to the allocated partition (output)
- * @return 0 on success, 1 otherwise
+ * Return: 0 on success, 1 otherwise
  */
 static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
 {
@@ -691,7 +693,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;
        }
 
@@ -712,7 +714,7 @@ static int part_parse(const char *const partdef, const char **ret, struct part_i
  * @param type mtd type
  * @param num mtd number
  * @param size a pointer to the size of the mtd device (output)
- * @return 0 if device is valid, 1 otherwise
+ * Return: 0 if device is valid, 1 otherwise
  */
 static int mtd_device_validate(u8 type, u8 num, u64 *size)
 {
@@ -730,7 +732,7 @@ static int mtd_device_validate(u8 type, u8 num, u64 *size)
  * Delete all mtd devices from a supplied devices list, free memory allocated for
  * each device and delete all device partitions.
  *
- * @return 0 on success, 1 otherwise
+ * Return: 0 on success, 1 otherwise
  */
 static int device_delall(struct list_head *head)
 {
@@ -754,7 +756,7 @@ static int device_delall(struct list_head *head)
  * from device list and device memory is freed.
  *
  * @param dev device to be deleted
- * @return 0 on success, 1 otherwise
+ * Return: 0 on success, 1 otherwise
  */
 static int device_del(struct mtd_device *dev)
 {
@@ -786,7 +788,7 @@ static int device_del(struct mtd_device *dev)
  *
  * @param type device type
  * @param num device number
- * @return NULL if requested device does not exist
+ * Return: NULL if requested device does not exist
  */
 struct mtd_device *device_find(u8 type, u8 num)
 {
@@ -833,7 +835,7 @@ static void device_add(struct mtd_device *dev)
  * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
  * @param ret output pointer to next char after parse completes (output)
  * @param retdev pointer to the allocated device (output)
- * @return 0 on success, 1 otherwise
+ * Return: 0 on success, 1 otherwise
  */
 static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
 {
@@ -873,15 +875,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;
 
@@ -967,7 +966,7 @@ static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_
 /**
  * Initialize global device list.
  *
- * @return 0 on success, 1 otherwise
+ * Return: 0 on success, 1 otherwise
  */
 static int mtd_devices_init(void)
 {
@@ -981,7 +980,7 @@ static int mtd_devices_init(void)
 /*
  * Search global mtdids list and find id of requested type and number.
  *
- * @return pointer to the id if it exists, NULL otherwise
+ * Return: pointer to the id if it exists, NULL otherwise
  */
 static struct mtdids* id_find(u8 type, u8 num)
 {
@@ -1005,7 +1004,7 @@ static struct mtdids* id_find(u8 type, u8 num)
  *
  * @param mtd_id string containing requested mtd_id
  * @param mtd_id_len length of supplied mtd_id
- * @return pointer to the id if it exists, NULL otherwise
+ * Return: pointer to the id if it exists, NULL otherwise
  */
 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
 {
@@ -1031,14 +1030,14 @@ 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
  * @param ret_id output pointer to next char after parse completes (output)
  * @param dev_type parsed device type (output)
  * @param dev_num parsed device number (output)
- * @return 0 on success, 1 otherwise
+ * Return: 0 on success, 1 otherwise
  */
 int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type,
                 u8 *dev_num)
@@ -1055,6 +1054,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;
@@ -1077,7 +1079,7 @@ int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type,
  *
  * @param buf output buffer holding generated mtdparts string (output)
  * @param buflen buffer size
- * @return 0 on success, 1 otherwise
+ * Return: 0 on success, 1 otherwise
  */
 static int generate_mtdparts(char *buf, u32 buflen)
 {
@@ -1097,9 +1099,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);
 
@@ -1205,7 +1204,7 @@ cleanup:
  *
  * @param buf output buffer holding generated mtdparts string (output)
  * @param buflen buffer size
- * @return 0 on success, 1 otherwise
+ * Return: 0 on success, 1 otherwise
  */
 static int generate_mtdparts_save(char *buf, u32 buflen)
 {
@@ -1227,17 +1226,17 @@ static int generate_mtdparts_save(char *buf, u32 buflen)
  *
  * @param mtd the mtd info
  * @param part the partition
- * @return the calculated net size of this partition
+ * Return: the calculated net size of this partition
  */
 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;
        }
 
@@ -1274,7 +1273,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);
@@ -1343,7 +1342,7 @@ static void list_partitions(void)
  * @param dev pointer to the requested device (output)
  * @param part_num verified partition number (output)
  * @param part pointer to requested partition (output)
- * @return 0 on success, 1 otherwise
+ * Return: 0 on success, 1 otherwise
  */
 int find_dev_and_part(const char *id, struct mtd_device **dev,
                u8 *part_num, struct part_info **part)
@@ -1403,7 +1402,7 @@ int find_dev_and_part(const char *id, struct mtd_device **dev,
  * Find and delete partition. For partition id format see find_dev_and_part().
  *
  * @param id string describing device and partition
- * @return 0 on success, 1 otherwise
+ * Return: 0 on success, 1 otherwise
  */
 static int delete_partition(const char *id)
 {
@@ -1479,7 +1478,7 @@ static void spread_partition(struct mtd_info *mtd, struct part_info *part,
  * as big as their mtdparts environment variable sizes and they each start
  * on a good block.
  *
- * @return 0 on success, 1 otherwise
+ * Return: 0 on success, 1 otherwise
  */
 static int spread_partitions(void)
 {
@@ -1532,7 +1531,7 @@ static int spread_partitions(void)
  * buffer.  gd->env_buf will be too small.
  *
  * @param buf temporary buffer pointer MTDPARTS_MAXLEN long
- * @return mtdparts variable string, NULL if not found
+ * Return: mtdparts variable string, NULL if not found
  */
 static const char *env_get_mtdparts(char *buf)
 {
@@ -1548,7 +1547,7 @@ static const char *env_get_mtdparts(char *buf)
  * for each entry. Add created devices to the global devices list.
  *
  * @param mtdparts string specifing mtd partitions
- * @return 0 on success, 1 otherwise
+ * Return: 0 on success, 1 otherwise
  */
 static int parse_mtdparts(const char *const mtdparts)
 {
@@ -1570,11 +1569,9 @@ static int parse_mtdparts(const char *const mtdparts)
        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 != '\0') {
                err = 1;
@@ -1608,7 +1605,7 @@ static int parse_mtdparts(const char *const mtdparts)
  * to the global mtdids list.
  *
  * @param ids mapping string
- * @return 0 on success, 1 otherwise
+ * Return: 0 on success, 1 otherwise
  */
 static int parse_mtdids(const char *const ids)
 {
@@ -1637,7 +1634,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;
 
@@ -1718,7 +1715,7 @@ static int parse_mtdids(const char *const ids)
  * Parse and initialize global mtdids mapping and create global
  * device/partition list.
  *
- * @return 0 on success, 1 otherwise
+ * Return: 0 on success, 1 otherwise
  */
 int mtdparts_init(void)
 {
@@ -1726,7 +1723,7 @@ 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");
@@ -1750,7 +1747,8 @@ int mtdparts_init(void)
 
        /* 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);
 
@@ -1867,7 +1865,7 @@ int mtdparts_init(void)
  *
  * @param dev device that is to be searched for a partition
  * @param part_num requested partition number
- * @return pointer to the part_info, NULL otherwise
+ * Return: pointer to the part_info, NULL otherwise
  */
 static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num)
 {
@@ -1914,9 +1912,10 @@ static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part
  * @param flag command flag
  * @param argc number of arguments supplied to the command
  * @param argv arguments list
- * @return 0 on success, 1 otherwise
+ * 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;
@@ -1952,10 +1951,10 @@ static int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  * @param flag command flag
  * @param argc number of arguments supplied to the command
  * @param argv arguments list
- * @return 0 on success, 1 otherwise
+ * 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) {
@@ -2075,13 +2074,12 @@ static int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc,
 /***************************************************/
 U_BOOT_CMD(
        chpart, 2,      0,      do_chpart,
-       "change active partition",
+       "change active partition of a MTD device",
        "part-id\n"
-       "    - change active partition (e.g. part-id = nand0,1)"
+       "    - change active partition (e.g. part-id = nand0,1) of a MTD device"
 );
 
-#ifdef CONFIG_SYS_LONGHELP
-static char mtdparts_help_text[] =
+U_BOOT_LONGHELP(mtdparts,
        "\n"
        "    - list partition table\n"
        "mtdparts delall\n"
@@ -2112,7 +2110,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"
@@ -2123,8 +2121,7 @@ static char mtdparts_help_text[] =
        "<size>     := standard linux memsize OR '-' to denote all remaining space\n"
        "<offset>   := partition start offset within the device\n"
        "<name>     := '(' NAME ')'\n"
-       "<ro-flag>  := when set to 'ro' makes partition read-only (not used, passed to kernel)";
-#endif
+       "<ro-flag>  := when set to 'ro' makes partition read-only (not used, passed to kernel)");
 
 U_BOOT_CMD(
        mtdparts,       6,      0,      do_mtdparts,