Commit 09c3280754f8f68a4d7fc0ee397a92b38c4f59e4

Authored by Heiko Schocher
Committed by Jagan Teki
1 parent 9fe6d8716e

mtd, nand: Move common functions from cmd_nand.c to common place

Move common functions from cmd_nand.c (for calculating offset
and size from cmdline paramter) to common place, so they could
used from other commands which use mtd partitions.

For onenand the arg_off_size() is left in common/cmd_onenand.c.
It should use now the common arg_off() function, but as I could
not test onenand I let it there ...

Signed-off-by: Heiko Schocher <hs@denx.de>
Acked-by: Scott Wood <scottwood@freescale.com>
Reviewed-by: Jagannadh Teki <jteki@openedev.com>

Showing 8 changed files with 164 additions and 141 deletions Side-by-side Diff

... ... @@ -133,115 +133,6 @@
133 133 return 0;
134 134 }
135 135  
136   -static inline int str2off(const char *p, loff_t *num)
137   -{
138   - char *endptr;
139   -
140   - *num = simple_strtoull(p, &endptr, 16);
141   - return *p != '\0' && *endptr == '\0';
142   -}
143   -
144   -static inline int str2long(const char *p, ulong *num)
145   -{
146   - char *endptr;
147   -
148   - *num = simple_strtoul(p, &endptr, 16);
149   - return *p != '\0' && *endptr == '\0';
150   -}
151   -
152   -static int get_part(const char *partname, int *idx, loff_t *off, loff_t *size,
153   - loff_t *maxsize)
154   -{
155   -#ifdef CONFIG_CMD_MTDPARTS
156   - struct mtd_device *dev;
157   - struct part_info *part;
158   - u8 pnum;
159   - int ret;
160   -
161   - ret = mtdparts_init();
162   - if (ret)
163   - return ret;
164   -
165   - ret = find_dev_and_part(partname, &dev, &pnum, &part);
166   - if (ret)
167   - return ret;
168   -
169   - if (dev->id->type != MTD_DEV_TYPE_NAND) {
170   - puts("not a NAND device\n");
171   - return -1;
172   - }
173   -
174   - *off = part->offset;
175   - *size = part->size;
176   - *maxsize = part->size;
177   - *idx = dev->id->num;
178   -
179   - ret = set_dev(*idx);
180   - if (ret)
181   - return ret;
182   -
183   - return 0;
184   -#else
185   - puts("offset is not a number\n");
186   - return -1;
187   -#endif
188   -}
189   -
190   -static int arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
191   - loff_t *maxsize)
192   -{
193   - if (!str2off(arg, off))
194   - return get_part(arg, idx, off, size, maxsize);
195   -
196   - if (*off >= nand_info[*idx].size) {
197   - puts("Offset exceeds device limit\n");
198   - return -1;
199   - }
200   -
201   - *maxsize = nand_info[*idx].size - *off;
202   - *size = *maxsize;
203   - return 0;
204   -}
205   -
206   -static int arg_off_size(int argc, char *const argv[], int *idx,
207   - loff_t *off, loff_t *size, loff_t *maxsize)
208   -{
209   - int ret;
210   -
211   - if (argc == 0) {
212   - *off = 0;
213   - *size = nand_info[*idx].size;
214   - *maxsize = *size;
215   - goto print;
216   - }
217   -
218   - ret = arg_off(argv[0], idx, off, size, maxsize);
219   - if (ret)
220   - return ret;
221   -
222   - if (argc == 1)
223   - goto print;
224   -
225   - if (!str2off(argv[1], size)) {
226   - printf("'%s' is not a number\n", argv[1]);
227   - return -1;
228   - }
229   -
230   - if (*size > *maxsize) {
231   - puts("Size exceeds partition or device limit\n");
232   - return -1;
233   - }
234   -
235   -print:
236   - printf("device %d ", *idx);
237   - if (*size == nand_info[*idx].size)
238   - puts("whole chip\n");
239   - else
240   - printf("offset 0x%llx, size 0x%llx\n",
241   - (unsigned long long)*off, (unsigned long long)*size);
242   - return 0;
243   -}
244   -
245 136 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
246 137 static void print_status(ulong start, ulong end, ulong erasesize, int status)
247 138 {
248 139  
... ... @@ -322,10 +213,15 @@
322 213 goto usage;
323 214  
324 215 /* We don't care about size, or maxsize. */
325   - if (arg_off(argv[2], &idx, &addr, &maxsize, &maxsize)) {
  216 + if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize,
  217 + MTD_DEV_TYPE_NAND, nand_info[idx].size)) {
326 218 puts("Offset or partition name expected\n");
327 219 return 1;
328 220 }
  221 + if (set_dev(idx)) {
  222 + puts("Offset or partition name expected\n");
  223 + return 1;
  224 + }
329 225  
330 226 if (idx != 0) {
331 227 puts("Partition not on first NAND device\n");
332 228  
... ... @@ -597,10 +493,14 @@
597 493  
598 494 printf("\nNAND %s: ", cmd);
599 495 /* skip first two or three arguments, look for offset and size */
600   - if (arg_off_size(argc - o, argv + o, &dev, &off, &size,
601   - &maxsize) != 0)
  496 + if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size,
  497 + &maxsize, MTD_DEV_TYPE_NAND,
  498 + nand_info[dev].size) != 0)
602 499 return 1;
603 500  
  501 + if (set_dev(dev))
  502 + return 1;
  503 +
604 504 nand = &nand_info[dev];
605 505  
606 506 memset(&opts, 0, sizeof(opts));
607 507  
... ... @@ -658,9 +558,14 @@
658 558 if (s && !strcmp(s, ".raw")) {
659 559 raw = 1;
660 560  
661   - if (arg_off(argv[3], &dev, &off, &size, &maxsize))
  561 + if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize,
  562 + MTD_DEV_TYPE_NAND,
  563 + nand_info[dev].size))
662 564 return 1;
663 565  
  566 + if (set_dev(dev))
  567 + return 1;
  568 +
664 569 nand = &nand_info[dev];
665 570  
666 571 if (argc > 4 && !str2long(argv[4], &pagecount)) {
667 572  
... ... @@ -675,10 +580,15 @@
675 580  
676 581 rwsize = pagecount * (nand->writesize + nand->oobsize);
677 582 } else {
678   - if (arg_off_size(argc - 3, argv + 3, &dev,
679   - &off, &size, &maxsize) != 0)
  583 + if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off,
  584 + &size, &maxsize,
  585 + MTD_DEV_TYPE_NAND,
  586 + nand_info[dev].size) != 0)
680 587 return 1;
681 588  
  589 + if (set_dev(dev))
  590 + return 1;
  591 +
682 592 /* size is unspecified */
683 593 if (argc < 5)
684 594 adjust_size_for_badblocks(&size, off, dev);
... ... @@ -814,8 +724,12 @@
814 724 if (s && !strcmp(s, ".allexcept"))
815 725 allexcept = 1;
816 726  
817   - if (arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
818   - &maxsize) < 0)
  727 + if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
  728 + &maxsize, MTD_DEV_TYPE_NAND,
  729 + nand_info[dev].size) < 0)
  730 + return 1;
  731 +
  732 + if (set_dev(dev))
819 733 return 1;
820 734  
821 735 if (!nand_unlock(&nand_info[dev], off, size, allexcept)) {
common/cmd_onenand.c
... ... @@ -24,16 +24,9 @@
24 24 static loff_t next_ofs;
25 25 static loff_t skip_ofs;
26 26  
27   -static inline int str2long(char *p, ulong *num)
  27 +static int arg_off_size_onenand(int argc, char * const argv[], ulong *off,
  28 + size_t *size)
28 29 {
29   - char *endptr;
30   -
31   - *num = simple_strtoul(p, &endptr, 16);
32   - return (*p != '\0' && *endptr == '\0') ? 1 : 0;
33   -}
34   -
35   -static int arg_off_size(int argc, char * const argv[], ulong *off, size_t *size)
36   -{
37 30 if (argc >= 1) {
38 31 if (!(str2long(argv[0], off))) {
39 32 printf("'%s' is not a number\n", argv[0]);
... ... @@ -399,7 +392,7 @@
399 392 addr = (ulong)simple_strtoul(argv[1], NULL, 16);
400 393  
401 394 printf("\nOneNAND read: ");
402   - if (arg_off_size(argc - 2, argv + 2, &ofs, &len) != 0)
  395 + if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0)
403 396 return 1;
404 397  
405 398 ret = onenand_block_read(ofs, len, &retlen, (u8 *)addr, oob);
... ... @@ -425,7 +418,7 @@
425 418 addr = (ulong)simple_strtoul(argv[1], NULL, 16);
426 419  
427 420 printf("\nOneNAND write: ");
428   - if (arg_off_size(argc - 2, argv + 2, &ofs, &len) != 0)
  421 + if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0)
429 422 return 1;
430 423  
431 424 ret = onenand_block_write(ofs, len, &retlen, (u8 *)addr, withoob);
... ... @@ -461,7 +454,7 @@
461 454 printf("\nOneNAND erase: ");
462 455  
463 456 /* skip first two or three arguments, look for offset and size */
464   - if (arg_off_size(argc, argv, &ofs, &len) != 0)
  457 + if (arg_off_size_onenand(argc, argv, &ofs, &len) != 0)
465 458 return 1;
466 459  
467 460 ret = onenand_block_erase(ofs, len, force);
... ... @@ -486,7 +479,7 @@
486 479 printf("\nOneNAND test: ");
487 480  
488 481 /* skip first two or three arguments, look for offset and size */
489   - if (arg_off_size(argc - 1, argv + 1, &ofs, &len) != 0)
  482 + if (arg_off_size_onenand(argc - 1, argv + 1, &ofs, &len) != 0)
490 483 return 1;
491 484  
492 485 ret = onenand_block_test(ofs, len);
... ... @@ -5,15 +5,6 @@
5 5 * SPDX-License-Identifier: GPL-2.0+
6 6 */
7 7  
8   -/*
9   - * Define _STDBOOL_H here to avoid macro expansion of true and false.
10   - * If the future code requires macro true or false, remove this define
11   - * and undef true and false before U_BOOT_CMD. This define and comment
12   - * shall be removed if change to U_BOOT_CMD is made to take string
13   - * instead of stringifying it.
14   - */
15   -#define _STDBOOL_H
16   -
17 8 #include <common.h>
18 9 #include <command.h>
19 10 #include <fs.h>
... ... @@ -190,6 +181,9 @@
190 181  
191 182 return expr;
192 183 }
  184 +
  185 +#undef true
  186 +#undef false
193 187  
194 188 U_BOOT_CMD(
195 189 test, CONFIG_SYS_MAXARGS, 1, do_test,
drivers/mtd/Makefile
... ... @@ -5,8 +5,8 @@
5 5 # SPDX-License-Identifier: GPL-2.0+
6 6 #
7 7  
8   -ifneq (,$(findstring y,$(CONFIG_MTD_DEVICE)$(CONFIG_CMD_NAND)$(CONFIG_CMD_ONENAND)))
9   -obj-y += mtdcore.o
  8 +ifneq (,$(findstring y,$(CONFIG_MTD_DEVICE)$(CONFIG_CMD_NAND)$(CONFIG_CMD_ONENAND)$(CONFIG_CMD_SF)))
  9 +obj-y += mtdcore.o mtd_uboot.o
10 10 endif
11 11 obj-$(CONFIG_MTD_PARTITIONS) += mtdpart.o
12 12 obj-$(CONFIG_MTD_CONCAT) += mtdconcat.o
drivers/mtd/mtd_uboot.c
  1 +/*
  2 + * (C) Copyright 2014
  3 + * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  4 + *
  5 + * SPDX-License-Identifier: GPL-2.0+
  6 + */
  7 +#include <common.h>
  8 +#include <linux/mtd/mtd.h>
  9 +#include <jffs2/jffs2.h>
  10 +
  11 +static int get_part(const char *partname, int *idx, loff_t *off, loff_t *size,
  12 + loff_t *maxsize, int devtype)
  13 +{
  14 +#ifdef CONFIG_CMD_MTDPARTS
  15 + struct mtd_device *dev;
  16 + struct part_info *part;
  17 + u8 pnum;
  18 + int ret;
  19 +
  20 + ret = mtdparts_init();
  21 + if (ret)
  22 + return ret;
  23 +
  24 + ret = find_dev_and_part(partname, &dev, &pnum, &part);
  25 + if (ret)
  26 + return ret;
  27 +
  28 + if (dev->id->type != devtype) {
  29 + printf("not same typ %d != %d\n", dev->id->type, devtype);
  30 + return -1;
  31 + }
  32 +
  33 + *off = part->offset;
  34 + *size = part->size;
  35 + *maxsize = part->size;
  36 + *idx = dev->id->num;
  37 +
  38 + return 0;
  39 +#else
  40 + puts("offset is not a number\n");
  41 + return -1;
  42 +#endif
  43 +}
  44 +
  45 +int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
  46 + loff_t *maxsize, int devtype, int chipsize)
  47 +{
  48 + if (!str2off(arg, off))
  49 + return get_part(arg, idx, off, size, maxsize, devtype);
  50 +
  51 + if (*off >= chipsize) {
  52 + puts("Offset exceeds device limit\n");
  53 + return -1;
  54 + }
  55 +
  56 + *maxsize = chipsize - *off;
  57 + *size = *maxsize;
  58 + return 0;
  59 +}
  60 +
  61 +int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
  62 + loff_t *size, loff_t *maxsize, int devtype, int chipsize)
  63 +{
  64 + int ret;
  65 +
  66 + if (argc == 0) {
  67 + *off = 0;
  68 + *size = chipsize;
  69 + *maxsize = *size;
  70 + goto print;
  71 + }
  72 +
  73 + ret = mtd_arg_off(argv[0], idx, off, size, maxsize, devtype,
  74 + chipsize);
  75 + if (ret)
  76 + return ret;
  77 +
  78 + if (argc == 1)
  79 + goto print;
  80 +
  81 + if (!str2off(argv[1], size)) {
  82 + printf("'%s' is not a number\n", argv[1]);
  83 + return -1;
  84 + }
  85 +
  86 + if (*size > *maxsize) {
  87 + puts("Size exceeds partition or device limit\n");
  88 + return -1;
  89 + }
  90 +
  91 +print:
  92 + printf("device %d ", *idx);
  93 + if (*size == chipsize)
  94 + puts("whole chip\n");
  95 + else
  96 + printf("offset 0x%llx, size 0x%llx\n",
  97 + (unsigned long long)*off, (unsigned long long)*size);
  98 + return 0;
  99 +}
include/linux/mtd/mtd.h
... ... @@ -482,6 +482,11 @@
482 482 int del_mtd_device(struct mtd_info *mtd);
483 483 int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
484 484 int del_mtd_partitions(struct mtd_info *);
  485 +
  486 +int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
  487 + loff_t *maxsize, int devtype, int chipsize);
  488 +int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
  489 + loff_t *size, loff_t *maxsize, int devtype, int chipsize);
485 490 #endif
486 491 #endif /* __MTD_MTD_H__ */
... ... @@ -196,5 +196,7 @@
196 196 */
197 197 void print_grouped_ull(unsigned long long int_val, int digits);
198 198  
  199 +bool str2off(const char *p, loff_t *num);
  200 +bool str2long(const char *p, ulong *num);
199 201 #endif
... ... @@ -909,4 +909,20 @@
909 909 grab = 3;
910 910 }
911 911 }
  912 +
  913 +bool str2off(const char *p, loff_t *num)
  914 +{
  915 + char *endptr;
  916 +
  917 + *num = simple_strtoull(p, &endptr, 16);
  918 + return *p != '\0' && *endptr == '\0';
  919 +}
  920 +
  921 +bool str2long(const char *p, ulong *num)
  922 +{
  923 + char *endptr;
  924 +
  925 + *num = simple_strtoul(p, &endptr, 16);
  926 + return *p != '\0' && *endptr == '\0';
  927 +}