1 // SPDX-License-Identifier: GPL-2.0+
4 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
7 #include <linux/mtd/mtd.h>
8 #include <jffs2/jffs2.h> /* Legacy */
11 * mtd_search_alternate_name - Search an alternate name for @mtdname thanks to
12 * the mtdids legacy environment variable.
14 * The mtdids string is a list of comma-separated 'dev_id=mtd_id' tupples.
15 * Check if one of the mtd_id matches mtdname, in this case save dev_id in
18 * @mtdname: Current MTD device name
19 * @altname: Alternate name to return
20 * @max_len: Length of the alternate name buffer
22 * @return 0 on success, an error otherwise.
24 int mtd_search_alternate_name(const char *mtdname, char *altname,
27 const char *mtdids, *equal, *comma, *dev_id, *mtd_id;
28 int dev_id_len, mtd_id_len;
30 mtdids = env_get("mtdids");
35 /* Find the '=' sign */
37 equal = strchr(dev_id, '=');
40 dev_id_len = equal - mtdids;
43 /* Find the end of the tupple */
44 comma = strchr(mtdids, ',');
46 mtd_id_len = comma - mtd_id;
48 mtd_id_len = &mtdids[strlen(mtdids)] - mtd_id + 1;
50 if (!dev_id_len || !mtd_id_len)
53 if (dev_id_len + 1 > max_len)
56 /* Compare the name we search with the current mtd_id */
57 if (!strncmp(mtdname, mtd_id, mtd_id_len)) {
58 strncpy(altname, dev_id, dev_id_len);
59 altname[dev_id_len] = 0;
64 /* Go to the next tupple */
73 static int get_part(const char *partname, int *idx, loff_t *off, loff_t *size,
74 loff_t *maxsize, int devtype)
76 #ifdef CONFIG_CMD_MTDPARTS
77 struct mtd_device *dev;
78 struct part_info *part;
82 ret = mtdparts_init();
86 ret = find_dev_and_part(partname, &dev, &pnum, &part);
90 if (dev->id->type != devtype) {
91 printf("not same typ %d != %d\n", dev->id->type, devtype);
97 *maxsize = part->size;
102 puts("mtdparts support missing.\n");
107 int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
108 loff_t *maxsize, int devtype, uint64_t chipsize)
110 if (!str2off(arg, off))
111 return get_part(arg, idx, off, size, maxsize, devtype);
113 if (*off >= chipsize) {
114 puts("Offset exceeds device limit\n");
118 *maxsize = chipsize - *off;
123 int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
124 loff_t *size, loff_t *maxsize, int devtype,
136 ret = mtd_arg_off(argv[0], idx, off, size, maxsize, devtype,
144 if (!str2off(argv[1], size)) {
145 printf("'%s' is not a number\n", argv[1]);
149 if (*size > *maxsize) {
150 puts("Size exceeds partition or device limit\n");
155 printf("device %d ", *idx);
156 if (*size == chipsize)
157 puts("whole chip\n");
159 printf("offset 0x%llx, size 0x%llx\n",
160 (unsigned long long)*off, (unsigned long long)*size);