ea433583859c582c22d7797ec0d56a1c748716d0
[firefly-linux-kernel-4.4.55.git] / fs / partitions / mtdpart.c
1 #include <linux/kernel.h>
2 #include <linux/slab.h>
3 #include <linux/bootmem.h>
4
5 #include "check.h"
6 #include "mtdpart.h"
7
8 /* error message prefix */
9 #define ERRP "mtd: "
10
11 /* debug macro */
12 #if 0
13 #define dbg(x) do { printk("DEBUG-CMDLINE-PART: "); printk x; } while(0)
14 #else
15 #define dbg(x)
16 #endif
17
18 #define SECTOR_1G       0x200000        // 0x200000 * 512 = 1G
19 #define FROM_OFFSET     0x2000          // 4MB
20
21 /* special size referring to all the remaining space in a partition */
22 #define SIZE_REMAINING UINT_MAX
23 #define OFFSET_CONTINUOUS UINT_MAX
24
25 struct mtd_partition{
26         char *name;
27         sector_t from;
28         sector_t size;
29 };
30 struct cmdline_mtd_partition {
31         struct cmdline_mtd_partition *next;
32         char *mtd_id;
33         int num_parts;
34         struct mtd_partition *parts;
35 };
36
37 /* mtdpart_setup() parses into here */
38 static struct cmdline_mtd_partition *partitions;
39
40 /* the command line passed to mtdpart_setupd() */
41 static char *cmdline;
42 static int cmdline_parsed = 0;
43
44 /*
45  * Parse one partition definition for an MTD. Since there can be many
46  * comma separated partition definitions, this function calls itself
47  * recursively until no more partition definitions are found. Nice side
48  * effect: the memory to keep the mtd_partition structs and the names
49  * is allocated upon the last definition being found. At that point the
50  * syntax has been verified ok.
51  */
52 static struct mtd_partition * newpart(char *s,
53                                       char **retptr,
54                                       int *num_parts,
55                                       int this_part,
56                                       unsigned char **extra_mem_ptr,
57                                       int extra_mem_size)
58 {
59         struct mtd_partition *parts;
60         sector_t size;
61         sector_t from = OFFSET_CONTINUOUS;
62         char *name;
63         int name_len;
64         unsigned char *extra_mem;
65         char delim;
66
67         /* fetch the partition size */
68         if (*s == '-')
69         {       /* assign all remaining space to this partition */
70                 size = SIZE_REMAINING;
71                 s++;
72         }
73         else
74         {
75                 size = memparse(s, &s);
76                 if (size < PAGE_SIZE)
77                 {
78                         printk(KERN_ERR ERRP "partition size too small (%llx)\n", size);
79                         return NULL;
80                 }
81         }
82
83         /* fetch partition name */
84         delim = 0;
85         /* check for from */
86         if (*s == '@')
87         {
88                 s++;
89                 from = memparse(s, &s);
90         }
91         /* now look for name */
92         if (*s == '(')
93         {
94                 delim = ')';
95         }
96
97         if (delim)
98         {
99                 char *p;
100
101                 name = ++s;
102                 p = strchr(name, delim);
103                 if (!p)
104                 {
105                         printk(KERN_ERR ERRP "no closing %c found in partition name\n", delim);
106                         return NULL;
107                 }
108                 name_len = p - name;
109                 s = p + 1;
110         }
111         else
112         {
113                 name = NULL;
114                 name_len = 13; /* Partition_000 */
115         }
116
117         /* record name length for memory allocation later */
118         extra_mem_size += name_len + 1;
119
120         /* test if more partitions are following */
121         if (*s == ',')
122         {
123                 if (size == SIZE_REMAINING)
124                 {
125                         printk(KERN_ERR ERRP "no partitions allowed after a fill-up partition\n");
126                         return NULL;
127                 }
128                 /* more partitions follow, parse them */
129                 parts = newpart(s + 1, &s, num_parts, this_part + 1,
130                                 &extra_mem, extra_mem_size);
131                 if (!parts)
132                         return NULL;
133         }
134         else
135         {       /* this is the last partition: allocate space for all */
136                 int alloc_size;
137
138                 *num_parts = this_part + 1;
139                 alloc_size = *num_parts * sizeof(struct mtd_partition) +
140                              extra_mem_size;
141                 parts = kzalloc(alloc_size, GFP_KERNEL);
142                 if (!parts)
143                 {
144                         printk(KERN_ERR ERRP "out of memory\n");
145                         return NULL;
146                 }
147                 extra_mem = (unsigned char *)(parts + *num_parts);
148         }
149         /* enter this partition (from will be calculated later if it is zero at this point) */
150         parts[this_part].size = size;
151         parts[this_part].from = from;
152         if (name)
153         {
154                 strlcpy(extra_mem, name, name_len + 1);
155         }
156         else
157         {
158                 sprintf(extra_mem, "Partition_%03d", this_part);
159         }
160         parts[this_part].name = extra_mem;
161         extra_mem += name_len + 1;
162
163         dbg(("partition %d: name <%s>, from %llx, size %llx\n",
164              this_part,
165              parts[this_part].name,
166              parts[this_part].from,
167              parts[this_part].size));
168
169         /* return (updated) pointer to extra_mem memory */
170         if (extra_mem_ptr)
171           *extra_mem_ptr = extra_mem;
172
173         /* return (updated) pointer command line string */
174         *retptr = s;
175
176         /* return partition table */
177         return parts;
178 }
179
180 /*
181  * Parse the command line.
182  */
183 static int mtdpart_setup_real(char *s)
184 {
185         cmdline_parsed = 1;
186
187         for( ; s != NULL; )
188         {
189                 struct cmdline_mtd_partition *this_mtd;
190                 struct mtd_partition *parts;
191                 int mtd_id_len;
192                 int num_parts;
193                 char *p, *mtd_id;
194
195                 mtd_id = s;
196                 /* fetch <mtd-id> */
197                 if (!(p = strchr(s, ':')))
198                 {
199                         dbg(( "no mtd-id\n"));
200                         return 0;
201                 }
202                 mtd_id_len = p - mtd_id;
203
204                 dbg(("parsing <%s>\n", p+1));
205
206                 /*
207                  * parse one mtd. have it reserve memory for the
208                  * struct cmdline_mtd_partition and the mtd-id string.
209                  */
210                 parts = newpart(p + 1,          /* cmdline */
211                                 &s,             /* out: updated cmdline ptr */
212                                 &num_parts,     /* out: number of parts */
213                                 0,              /* first partition */
214                                 (unsigned char**)&this_mtd, /* out: extra mem */
215                                 mtd_id_len + 1 + sizeof(*this_mtd) +
216                                 sizeof(void*)-1 /*alignment*/);
217                 if(!parts)
218                 {
219                         /*
220                          * An error occurred. We're either:
221                          * a) out of memory, or
222                          * b) in the middle of the partition spec
223                          * Either way, this mtd is hosed and we're
224                          * unlikely to succeed in parsing any more
225                          */
226                          return 0;
227                  }
228
229                 /* align this_mtd */
230                 this_mtd = (struct cmdline_mtd_partition *)
231                         ALIGN((unsigned long)this_mtd, sizeof(void*));
232                 /* enter results */
233                 this_mtd->parts = parts;
234                 this_mtd->num_parts = num_parts;
235                 this_mtd->mtd_id = (char*)(this_mtd + 1);
236                 strlcpy(this_mtd->mtd_id, mtd_id, mtd_id_len + 1);
237
238                 /* link into chain */
239                 this_mtd->next = partitions;
240                 partitions = this_mtd;
241
242                 dbg(("mtdid=<%s> num_parts=<%d>\n",
243                      this_mtd->mtd_id, this_mtd->num_parts));
244
245                 /* EOS - we're done */
246                 if (*s == 0)
247                         break;
248 #if 0
249                 /* does another spec follow? */
250                 if (*s != ';')
251                 {
252                         printk(KERN_ERR ERRP "bad character after partition (%c)\n", *s);
253                         return 0;
254                 }
255 #endif
256                 s++;
257         }
258         return 1;
259 }
260
261 /*
262  * Main function to be called from the MTD mapping driver/device to
263  * obtain the partitioning information. At this point the command line
264  * arguments will actually be parsed and turned to struct mtd_partition
265  * information. It returns partitions for the requested mtd device, or
266  * the first one in the chain if a NULL mtd_id is passed in.
267  */
268 static int parse_cmdline_partitions(sector_t n,
269                                     struct mtd_partition **pparts,
270                                     unsigned long origin)
271 {
272         unsigned long from;
273         int i;
274         struct cmdline_mtd_partition *part;
275         const char *mtd_id = "rk29xxnand";
276
277         /* parse command line */
278         if (!cmdline_parsed)
279                 mtdpart_setup_real(cmdline);
280
281         for(part = partitions; part; part = part->next)
282         {
283                 if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id)))
284                 {
285                         for(i = 0, from = 0; i < part->num_parts; i++)
286                         {
287                                 if (part->parts[i].from == OFFSET_CONTINUOUS)
288                                   part->parts[i].from = from;
289                                 else
290                                   from = part->parts[i].from;
291                                 if (part->parts[i].size == SIZE_REMAINING)
292                                   part->parts[i].size = n - from - FROM_OFFSET;
293                                 if (from + part->parts[i].size > n)
294                                 {
295                                         printk(KERN_WARNING ERRP
296                                                "%s: partitioning exceeds flash size, truncating\n",
297                                                part->mtd_id);
298                                         part->parts[i].size = n - from;
299                                         part->num_parts = i;
300                                 }
301                                 from += part->parts[i].size;
302                         }
303                         *pparts = kmemdup(part->parts,
304                                         sizeof(*part->parts) * part->num_parts,
305                                         GFP_KERNEL);
306                         if (!*pparts)
307                                 return -ENOMEM;
308                         return part->num_parts;
309                 }
310         }
311         return 0;
312 }
313
314 int mtdpart_partition(struct parsed_partitions *state)
315 {
316         int num_parts = 0, i;
317         sector_t n = get_capacity(state->bdev->bd_disk);
318         struct mtd_partition *parts = NULL;
319
320         if(n < SECTOR_1G)
321                 return 0;
322
323         cmdline = strstr(saved_command_line, "mtdparts=") + 9;
324         
325         num_parts = parse_cmdline_partitions(n, &parts, 0);
326         if(num_parts < 0)
327                 return num_parts;
328
329         for(i = 0; i < num_parts; i++){
330                 put_partition(state, i, parts[i].from + FROM_OFFSET, parts[i].size);
331                 strcpy(state->parts[i].info.volname, parts[i].name);
332                 printk(KERN_INFO "%10s: 0x%09llx -- 0x%09llx (%llu MB)\n", 
333                                 parts[i].name,
334                                 parts[i].from * 512,
335                                 (parts[i].from + parts[i].size) * 512,
336                                 parts[i].size / 2048);
337         }
338
339         return 1;
340 }
341
342