blob: f95d18709b8eb5e1b4b0b2a0ba4f80c2aff0ef0b [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * Read flash partition table from command line
3 *
4 * Copyright © 2002 SYSGO Real-Time Solutions GmbH
5 * Copyright © 2002-2010 David Woodhouse <dwmw2@infradead.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 * The format for the command line is as follows:
22 *
23 * mtdparts=<mtddef>[;<mtddef]
24 * <mtddef> := <mtd-id>:<partdef>[,<partdef>]
25 * where <mtd-id> is the name from the "cat /proc/mtd" command
26 * <partdef> := <size>[@offset][<name>][ro][lk]
27 * <mtd-id> := unique name used in mapping driver/device (mtd->name)
28 * <size> := standard linux memsize OR "-" to denote all remaining space
29 * <name> := '(' NAME ')'
30 *
31 * Examples:
32 *
33 * 1 NOR Flash, with 1 single writable partition:
34 * edb7312-nor:-
35 *
36 * 1 NOR Flash with 2 partitions, 1 NAND with one
37 * edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
38 */
39
40#include <linux/kernel.h>
41#include <linux/slab.h>
42
43#include <linux/mtd/mtd.h>
44#include <linux/mtd/partitions.h>
45#include <linux/bootmem.h>
46#include <linux/module.h>
47
48/* error message prefix */
49#define ERRP "mtd: "
50
51/* debug macro */
52#if 0
53#define dbg(x) do { printk("DEBUG-CMDLINE-PART: "); printk x; } while(0)
54#else
55#define dbg(x)
56#endif
57
58
59/* special size referring to all the remaining space in a partition */
60#define SIZE_REMAINING UINT_MAX
61#define OFFSET_CONTINUOUS UINT_MAX
62
63struct cmdline_mtd_partition {
64 struct cmdline_mtd_partition *next;
65 char *mtd_id;
66 int num_parts;
67 struct mtd_partition *parts;
68};
69
70/* mtdpart_setup() parses into here */
71 struct cmdline_mtd_partition *partitions;
72
73/* the command line passed to mtdpart_setupd() */
74static char *cmdline;
75static int cmdline_parsed = 0;
xf.liaa4d92f2023-09-13 00:18:58 -070076char *nor_cmdline;
lh9ed821d2023-04-07 01:36:19 -070077
78/*
79 * Parse one partition definition for an MTD. Since there can be many
80 * comma separated partition definitions, this function calls itself
81 * recursively until no more partition definitions are found. Nice side
82 * effect: the memory to keep the mtd_partition structs and the names
83 * is allocated upon the last definition being found. At that point the
84 * syntax has been verified ok.
85 */
86static struct mtd_partition * newpart(char *s,
87 char **retptr,
88 int *num_parts,
89 int this_part,
90 unsigned char **extra_mem_ptr,
91 int extra_mem_size)
92{
93 struct mtd_partition *parts;
94 unsigned long size;
95 unsigned long offset = OFFSET_CONTINUOUS;
96 char *name;
97 int name_len;
98 unsigned char *extra_mem;
99 char delim;
100 unsigned int mask_flags;
101
102 /* fetch the partition size */
103 if (*s == '-')
104 { /* assign all remaining space to this partition */
105 size = SIZE_REMAINING;
106 s++;
107 }
108 else
109 {
110 size = memparse(s, &s);
111 if (size < PAGE_SIZE)
112 {
113 printk(KERN_ERR ERRP "partition size too small (%lx)\n", size);
114 return NULL;
115 }
116 }
117
118 /* fetch partition name and flags */
119 mask_flags = 0; /* this is going to be a regular partition */
120 delim = 0;
121 /* check for offset */
122 if (*s == '@')
123 {
124 s++;
125 offset = memparse(s, &s);
126 }
127 /* now look for name */
128 if (*s == '(')
129 {
130 delim = ')';
131 }
132
133 if (delim)
134 {
135 char *p;
136
137 name = ++s;
138 p = strchr(name, delim);
139 if (!p)
140 {
141 printk(KERN_ERR ERRP "no closing %c found in partition name\n", delim);
142 return NULL;
143 }
144 name_len = p - name;
145 s = p + 1;
146 }
147 else
148 {
149 name = NULL;
150 name_len = 13; /* Partition_000 */
151 }
152
153 /* record name length for memory allocation later */
154 extra_mem_size += name_len + 1;
155
156 /* test for options */
157 if (strncmp(s, "ro", 2) == 0)
158 {
159 mask_flags |= MTD_WRITEABLE;
160 s += 2;
161 }
162
163 /* if lk is found do NOT unlock the MTD partition*/
164 if (strncmp(s, "lk", 2) == 0)
165 {
166 mask_flags |= MTD_POWERUP_LOCK;
167 s += 2;
168 }
169
170 /* test if more partitions are following */
171 if (*s == ',')
172 {
173 if (size == SIZE_REMAINING)
174 {
175 printk(KERN_ERR ERRP "no partitions allowed after a fill-up partition\n");
176 return NULL;
177 }
178 /* more partitions follow, parse them */
179 parts = newpart(s + 1, &s, num_parts, this_part + 1,
180 &extra_mem, extra_mem_size);
181 if (!parts)
182 return NULL;
183 }
184 else
185 { /* this is the last partition: allocate space for all */
186 int alloc_size;
187
188 *num_parts = this_part + 1;
189 alloc_size = *num_parts * sizeof(struct mtd_partition) +
190 extra_mem_size;
191 parts = kzalloc(alloc_size, GFP_KERNEL);
192 if (!parts)
193 return NULL;
194 extra_mem = (unsigned char *)(parts + *num_parts);
195 }
196 /* enter this partition (offset will be calculated later if it is zero at this point) */
197 parts[this_part].size = size;
198 parts[this_part].offset = offset;
199 parts[this_part].mask_flags = mask_flags;
200 if (name)
201 {
202 strlcpy(extra_mem, name, name_len + 1);
203 }
204 else
205 {
206 sprintf(extra_mem, "Partition_%03d", this_part);
207 }
208 parts[this_part].name = extra_mem;
209 extra_mem += name_len + 1;
210
211 dbg(("partition %d: name <%s>, offset %llx, size %llx, mask flags %x\n",
212 this_part,
213 parts[this_part].name,
214 parts[this_part].offset,
215 parts[this_part].size,
216 parts[this_part].mask_flags));
217
218 /* return (updated) pointer to extra_mem memory */
219 if (extra_mem_ptr)
220 *extra_mem_ptr = extra_mem;
221
222 /* return (updated) pointer command line string */
223 *retptr = s;
224
225 /* return partition table */
226 return parts;
227}
228
229/*
230 * Parse the command line.
231 */
232static int mtdpart_setup_real(char *s)
233{
234 cmdline_parsed = 1;
235
236 for( ; s != NULL; )
237 {
238 struct cmdline_mtd_partition *this_mtd;
239 struct mtd_partition *parts;
240 int mtd_id_len;
241 int num_parts;
242 char *p, *mtd_id;
243
244 mtd_id = s;
245 /* fetch <mtd-id> */
246 if (!(p = strchr(s, ':')))
247 {
248 printk(KERN_ERR ERRP "no mtd-id\n");
249 return 0;
250 }
251 mtd_id_len = p - mtd_id;
252
253 dbg(("parsing <%s>\n", p+1));
254
255 /*
256 * parse one mtd. have it reserve memory for the
257 * struct cmdline_mtd_partition and the mtd-id string.
258 */
259 parts = newpart(p + 1, /* cmdline */
260 &s, /* out: updated cmdline ptr */
261 &num_parts, /* out: number of parts */
262 0, /* first partition */
263 (unsigned char**)&this_mtd, /* out: extra mem */
264 mtd_id_len + 1 + sizeof(*this_mtd) +
265 sizeof(void*)-1 /*alignment*/);
266 if(!parts)
267 {
268 /*
269 * An error occurred. We're either:
270 * a) out of memory, or
271 * b) in the middle of the partition spec
272 * Either way, this mtd is hosed and we're
273 * unlikely to succeed in parsing any more
274 */
275 return 0;
276 }
277
278 /* align this_mtd */
279 this_mtd = (struct cmdline_mtd_partition *)
280 ALIGN((unsigned long)this_mtd, sizeof(void*));
281 /* enter results */
282 this_mtd->parts = parts;
283 this_mtd->num_parts = num_parts;
284 this_mtd->mtd_id = (char*)(this_mtd + 1);
285 strlcpy(this_mtd->mtd_id, mtd_id, mtd_id_len + 1);
286
287 /* link into chain */
288 this_mtd->next = partitions;
289 partitions = this_mtd;
290
291 dbg(("mtdid=<%s> num_parts=<%d>\n",
292 this_mtd->mtd_id, this_mtd->num_parts));
293
294
295 /* EOS - we're done */
296 if (*s == 0)
297 break;
298
299 /* does another spec follow? */
300 if (*s != ';')
301 {
302 printk(KERN_ERR ERRP "bad character after partition (%c)\n", *s);
303 return 0;
304 }
305 s++;
306 }
307 return 1;
308}
309
310/*
311 * Main function to be called from the MTD mapping driver/device to
312 * obtain the partitioning information. At this point the command line
313 * arguments will actually be parsed and turned to struct mtd_partition
314 * information. It returns partitions for the requested mtd device, or
315 * the first one in the chain if a NULL mtd_id is passed in.
316 */
317static int parse_cmdline_partitions(struct mtd_info *master,
318 struct mtd_partition **pparts,
319 struct mtd_part_parser_data *data)
320{
321 unsigned long offset;
322 int i;
323 struct cmdline_mtd_partition *part;
324 const char *mtd_id = master->name;
325
326 /* parse command line */
327 if (!cmdline_parsed)
328 mtdpart_setup_real(cmdline);
329 //mtdpart_setup_real("denali-nand:128k@0x000000(zloader),1536k@0x20000(uboot),384k@0x001a0000(proc-lp),1M@0x00200000(nvr),4M@0x00300000(nvrw),18M@0x00700000(proc-ps),10M@0x01900000(proc-phy),1M@0x02300000(proc-zsp),10M@0x02400000(proc-app),8M@0x02E00000(ramdisk),13M@0x03600000(recovery),80M@0x04300000(cdrom),40M@0x09300000(userdata),30M@0x0BB00000(ota-update)");
330 for(part = partitions; part; part = part->next)
331 {
332 if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id)))
333 {
334 for(i = 0, offset = 0; i < part->num_parts; i++)
335 {
336 if (part->parts[i].offset == OFFSET_CONTINUOUS)
337 part->parts[i].offset = offset;
338 else
339 offset = part->parts[i].offset;
340 if (part->parts[i].size == SIZE_REMAINING)
341 part->parts[i].size = master->size - offset;
342 if (offset + part->parts[i].size > master->size)
343 {
344 printk(KERN_WARNING ERRP
345 "%s: partitioning exceeds flash size, truncating\n",
346 part->mtd_id);
347 part->parts[i].size = master->size - offset;
348 part->num_parts = i;
349 }
350 offset += part->parts[i].size;
351 }
352 *pparts = kmemdup(part->parts,
353 sizeof(*part->parts) * part->num_parts,
354 GFP_KERNEL);
355 if (!*pparts)
356 return -ENOMEM;
357 return part->num_parts;
358 }
359 }
360 return 0;
361}
362
363
364/*
365 * This is the handler for our kernel parameter, called from
366 * main.c::checksetup(). Note that we can not yet kmalloc() anything,
367 * so we only save the commandline for later processing.
368 *
369 * This function needs to be visible for bootloaders.
370 */
371static int mtdpart_setup(char *s)
372{
373 cmdline = s;
374 return 1;
375}
376
377__setup("mtdparts=", mtdpart_setup);
378
xf.liaa4d92f2023-09-13 00:18:58 -0700379static int nor_cmdline_setup(char *s)
380{
381 nor_cmdline = s;
382 return 1;
383}
384
385__setup("EnhancedSecurity=", nor_cmdline_setup);
386
387
lh9ed821d2023-04-07 01:36:19 -0700388static struct mtd_part_parser cmdline_parser = {
389 .owner = THIS_MODULE,
390 .parse_fn = parse_cmdline_partitions,
391 .name = "cmdlinepart",
392};
393
394static int __init cmdline_parser_init(void)
395{
396 return register_mtd_parser(&cmdline_parser);
397}
398
399module_init(cmdline_parser_init);
400
401MODULE_LICENSE("GPL");
402MODULE_AUTHOR("Marius Groeger <mag@sysgo.de>");
403MODULE_DESCRIPTION("Command line configuration of MTD partitions");