blob: 1a5271000bf354c3a5aab7e2f02e0cd85a287b75 [file] [log] [blame]
Miquel Raynaleb446ef2019-10-25 19:39:29 +02001// SPDX-License-Identifier: GPL-2.0+
2
Miquel Raynaleb446ef2019-10-25 19:39:29 +02003#include <jffs2/jffs2.h>
4#include <linux/mtd/mtd.h>
5#include <linux/mtd/partitions.h>
6#include <linux/string.h>
7#include <mtd.h>
8
9static int get_part(const char *partname, int *idx, loff_t *off, loff_t *size,
10 loff_t *maxsize, int devtype)
11{
12#ifdef CONFIG_CMD_MTDPARTS
13 struct mtd_device *dev;
14 struct part_info *part;
15 u8 pnum;
16 int ret;
17
18 ret = mtdparts_init();
19 if (ret)
20 return ret;
21
22 ret = find_dev_and_part(partname, &dev, &pnum, &part);
23 if (ret)
24 return ret;
25
26 if (dev->id->type != devtype) {
27 printf("not same typ %d != %d\n", dev->id->type, devtype);
28 return -1;
29 }
30
31 *off = part->offset;
32 *size = part->size;
33 *maxsize = part->size;
34 *idx = dev->id->num;
35
36 return 0;
37#else
38 puts("mtdparts support missing.\n");
39 return -1;
40#endif
41}
42
43int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
44 loff_t *maxsize, int devtype, uint64_t chipsize)
45{
46 if (!str2off(arg, off))
47 return get_part(arg, idx, off, size, maxsize, devtype);
48
49 if (*off >= chipsize) {
50 puts("Offset exceeds device limit\n");
51 return -1;
52 }
53
54 *maxsize = chipsize - *off;
55 *size = *maxsize;
56 return 0;
57}
58
59int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
60 loff_t *size, loff_t *maxsize, int devtype,
61 uint64_t chipsize)
62{
63 int ret;
64
65 if (argc == 0) {
66 *off = 0;
67 *size = chipsize;
68 *maxsize = *size;
69 goto print;
70 }
71
72 ret = mtd_arg_off(argv[0], idx, off, size, maxsize, devtype,
73 chipsize);
74 if (ret)
75 return ret;
76
77 if (argc == 1)
78 goto print;
79
80 if (!str2off(argv[1], size)) {
81 printf("'%s' is not a number\n", argv[1]);
82 return -1;
83 }
84
85 if (*size > *maxsize) {
86 puts("Size exceeds partition or device limit\n");
87 return -1;
88 }
89
Ashok Reddy Soma899fb5a2023-05-16 05:52:36 -060090 if (*size == 0) {
91 debug("ERROR: Invalid size 0\n");
92 return -1;
93 }
94
Miquel Raynaleb446ef2019-10-25 19:39:29 +020095print:
96 printf("device %d ", *idx);
97 if (*size == chipsize)
98 puts("whole chip\n");
99 else
100 printf("offset 0x%llx, size 0x%llx\n",
101 (unsigned long long)*off, (unsigned long long)*size);
102 return 0;
103}