]>
Commit | Line | Data |
---|---|---|
68d7d651 SR |
1 | /* |
2 | * (C) Copyright 2002 | |
3 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
4 | * | |
5 | * (C) Copyright 2002 | |
6 | * Robert Schwebel, Pengutronix, <[email protected]> | |
7 | * | |
8 | * (C) Copyright 2003 | |
9 | * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <[email protected]> | |
10 | * | |
11 | * (C) Copyright 2005 | |
12 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
13 | * | |
14 | * Added support for reading flash partition table from environment. | |
15 | * Parsing routines are based on driver/mtd/cmdline.c from the linux 2.4 | |
16 | * kernel tree. | |
17 | * | |
ca75b20e BG |
18 | * (C) Copyright 2008 |
19 | * Harald Welte, OpenMoko, Inc., Harald Welte <[email protected]> | |
20 | * | |
68d7d651 SR |
21 | * $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $ |
22 | * Copyright 2002 SYSGO Real-Time Solutions GmbH | |
23 | * | |
1a459660 | 24 | * SPDX-License-Identifier: GPL-2.0+ |
68d7d651 SR |
25 | */ |
26 | ||
27 | /* | |
28 | * Three environment variables are used by the parsing routines: | |
29 | * | |
30 | * 'partition' - keeps current partition identifier | |
31 | * | |
32 | * partition := <part-id> | |
33 | * <part-id> := <dev-id>,part_num | |
34 | * | |
35 | * | |
36 | * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping | |
37 | * | |
38 | * mtdids=<idmap>[,<idmap>,...] | |
39 | * | |
40 | * <idmap> := <dev-id>=<mtd-id> | |
41 | * <dev-id> := 'nand'|'nor'|'onenand'<dev-num> | |
42 | * <dev-num> := mtd device number, 0... | |
43 | * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name) | |
44 | * | |
45 | * | |
46 | * 'mtdparts' - partition list | |
47 | * | |
48 | * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...] | |
49 | * | |
50 | * <mtd-def> := <mtd-id>:<part-def>[,<part-def>...] | |
51 | * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name) | |
52 | * <part-def> := <size>[@<offset>][<name>][<ro-flag>] | |
53 | * <size> := standard linux memsize OR '-' to denote all remaining space | |
54 | * <offset> := partition start offset within the device | |
55 | * <name> := '(' NAME ')' | |
56 | * <ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel) | |
57 | * | |
58 | * Notes: | |
59 | * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping | |
60 | * - if the above variables are not set defaults for a given target are used | |
61 | * | |
62 | * Examples: | |
63 | * | |
64 | * 1 NOR Flash, with 1 single writable partition: | |
65 | * mtdids=nor0=edb7312-nor | |
66 | * mtdparts=mtdparts=edb7312-nor:- | |
67 | * | |
68 | * 1 NOR Flash with 2 partitions, 1 NAND with one | |
69 | * mtdids=nor0=edb7312-nor,nand0=edb7312-nand | |
70 | * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home) | |
71 | * | |
72 | */ | |
73 | ||
68d7d651 SR |
74 | #include <common.h> |
75 | #include <command.h> | |
76 | #include <malloc.h> | |
b93b24bf | 77 | #include <jffs2/load_kernel.h> |
68d7d651 SR |
78 | #include <linux/list.h> |
79 | #include <linux/ctype.h> | |
864aa034 SR |
80 | #include <linux/err.h> |
81 | #include <linux/mtd/mtd.h> | |
68d7d651 SR |
82 | |
83 | #if defined(CONFIG_CMD_NAND) | |
68d7d651 SR |
84 | #include <linux/mtd/nand.h> |
85 | #include <nand.h> | |
68d7d651 SR |
86 | #endif |
87 | ||
88 | #if defined(CONFIG_CMD_ONENAND) | |
68d7d651 SR |
89 | #include <linux/mtd/onenand.h> |
90 | #include <onenand_uboot.h> | |
91 | #endif | |
92 | ||
a7eb1d66 JH |
93 | DECLARE_GLOBAL_DATA_PTR; |
94 | ||
68d7d651 | 95 | /* special size referring to all the remaining space in a partition */ |
39ac3447 | 96 | #define SIZE_REMAINING (~0llu) |
68d7d651 SR |
97 | |
98 | /* special offset value, it is used when not provided by user | |
99 | * | |
100 | * this value is used temporarily during parsing, later such offests | |
101 | * are recalculated */ | |
39ac3447 | 102 | #define OFFSET_NOT_SPECIFIED (~0llu) |
68d7d651 SR |
103 | |
104 | /* minimum partition size */ | |
105 | #define MIN_PART_SIZE 4096 | |
106 | ||
107 | /* this flag needs to be set in part_info struct mask_flags | |
108 | * field for read-only partitions */ | |
109 | #define MTD_WRITEABLE_CMD 1 | |
110 | ||
111 | /* default values for mtdids and mtdparts variables */ | |
112 | #if defined(MTDIDS_DEFAULT) | |
113 | static const char *const mtdids_default = MTDIDS_DEFAULT; | |
114 | #else | |
68d7d651 SR |
115 | static const char *const mtdids_default = NULL; |
116 | #endif | |
117 | ||
118 | #if defined(MTDPARTS_DEFAULT) | |
119 | static const char *const mtdparts_default = MTDPARTS_DEFAULT; | |
120 | #else | |
68d7d651 SR |
121 | static const char *const mtdparts_default = NULL; |
122 | #endif | |
123 | ||
124 | /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */ | |
125 | #define MTDIDS_MAXLEN 128 | |
126 | #define MTDPARTS_MAXLEN 512 | |
127 | #define PARTITION_MAXLEN 16 | |
128 | static char last_ids[MTDIDS_MAXLEN]; | |
129 | static char last_parts[MTDPARTS_MAXLEN]; | |
130 | static char last_partition[PARTITION_MAXLEN]; | |
131 | ||
132 | /* low level jffs2 cache cleaning routine */ | |
133 | extern void jffs2_free_cache(struct part_info *part); | |
134 | ||
135 | /* mtdids mapping list, filled by parse_ids() */ | |
088f1b19 | 136 | static struct list_head mtdids; |
68d7d651 SR |
137 | |
138 | /* device/partition list, parse_cmdline() parses into here */ | |
088f1b19 | 139 | static struct list_head devices; |
68d7d651 SR |
140 | |
141 | /* current active device and partition number */ | |
76b5883d SR |
142 | struct mtd_device *current_mtd_dev = NULL; |
143 | u8 current_mtd_partnum = 0; | |
68d7d651 SR |
144 | |
145 | static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num); | |
146 | ||
147 | /* command line only routines */ | |
148 | static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len); | |
149 | static int device_del(struct mtd_device *dev); | |
150 | ||
151 | /** | |
152 | * Parses a string into a number. The number stored at ptr is | |
153 | * potentially suffixed with K (for kilobytes, or 1024 bytes), | |
154 | * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or | |
155 | * 1073741824). If the number is suffixed with K, M, or G, then | |
156 | * the return value is the number multiplied by one kilobyte, one | |
157 | * megabyte, or one gigabyte, respectively. | |
158 | * | |
159 | * @param ptr where parse begins | |
160 | * @param retptr output pointer to next char after parse completes (output) | |
161 | * @return resulting unsigned int | |
162 | */ | |
39ac3447 | 163 | static u64 memsize_parse (const char *const ptr, const char **retptr) |
68d7d651 | 164 | { |
39ac3447 | 165 | u64 ret = simple_strtoull(ptr, (char **)retptr, 0); |
68d7d651 SR |
166 | |
167 | switch (**retptr) { | |
168 | case 'G': | |
169 | case 'g': | |
170 | ret <<= 10; | |
171 | case 'M': | |
172 | case 'm': | |
173 | ret <<= 10; | |
174 | case 'K': | |
175 | case 'k': | |
176 | ret <<= 10; | |
177 | (*retptr)++; | |
178 | default: | |
179 | break; | |
180 | } | |
181 | ||
182 | return ret; | |
183 | } | |
184 | ||
185 | /** | |
186 | * Format string describing supplied size. This routine does the opposite job | |
187 | * to memsize_parse(). Size in bytes is converted to string and if possible | |
188 | * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix. | |
189 | * | |
190 | * Note, that this routine does not check for buffer overflow, it's the caller | |
191 | * who must assure enough space. | |
192 | * | |
193 | * @param buf output buffer | |
194 | * @param size size to be converted to string | |
195 | */ | |
39ac3447 | 196 | static void memsize_format(char *buf, u64 size) |
68d7d651 SR |
197 | { |
198 | #define SIZE_GB ((u32)1024*1024*1024) | |
199 | #define SIZE_MB ((u32)1024*1024) | |
200 | #define SIZE_KB ((u32)1024) | |
201 | ||
202 | if ((size % SIZE_GB) == 0) | |
39ac3447 | 203 | sprintf(buf, "%llug", size/SIZE_GB); |
68d7d651 | 204 | else if ((size % SIZE_MB) == 0) |
39ac3447 | 205 | sprintf(buf, "%llum", size/SIZE_MB); |
68d7d651 | 206 | else if (size % SIZE_KB == 0) |
39ac3447 | 207 | sprintf(buf, "%lluk", size/SIZE_KB); |
68d7d651 | 208 | else |
39ac3447 | 209 | sprintf(buf, "%llu", size); |
68d7d651 SR |
210 | } |
211 | ||
212 | /** | |
213 | * This routine does global indexing of all partitions. Resulting index for | |
214 | * current partition is saved in 'mtddevnum'. Current partition name in | |
215 | * 'mtddevname'. | |
216 | */ | |
217 | static void index_partitions(void) | |
218 | { | |
68d7d651 SR |
219 | u16 mtddevnum; |
220 | struct part_info *part; | |
221 | struct list_head *dentry; | |
222 | struct mtd_device *dev; | |
223 | ||
e1d2950d | 224 | debug("--- index partitions ---\n"); |
68d7d651 | 225 | |
76b5883d | 226 | if (current_mtd_dev) { |
68d7d651 SR |
227 | mtddevnum = 0; |
228 | list_for_each(dentry, &devices) { | |
229 | dev = list_entry(dentry, struct mtd_device, link); | |
76b5883d SR |
230 | if (dev == current_mtd_dev) { |
231 | mtddevnum += current_mtd_partnum; | |
41ef372c | 232 | setenv_ulong("mtddevnum", mtddevnum); |
68d7d651 SR |
233 | break; |
234 | } | |
235 | mtddevnum += dev->num_parts; | |
236 | } | |
237 | ||
76b5883d | 238 | part = mtd_part_info(current_mtd_dev, current_mtd_partnum); |
68d7d651 SR |
239 | setenv("mtddevname", part->name); |
240 | ||
e1d2950d | 241 | debug("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name); |
68d7d651 SR |
242 | } else { |
243 | setenv("mtddevnum", NULL); | |
244 | setenv("mtddevname", NULL); | |
245 | ||
e1d2950d | 246 | debug("=> mtddevnum NULL\n=> mtddevname NULL\n"); |
68d7d651 SR |
247 | } |
248 | } | |
249 | ||
250 | /** | |
251 | * Save current device and partition in environment variable 'partition'. | |
252 | */ | |
253 | static void current_save(void) | |
254 | { | |
255 | char buf[16]; | |
256 | ||
e1d2950d | 257 | debug("--- current_save ---\n"); |
68d7d651 | 258 | |
76b5883d SR |
259 | if (current_mtd_dev) { |
260 | sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_mtd_dev->id->type), | |
261 | current_mtd_dev->id->num, current_mtd_partnum); | |
68d7d651 SR |
262 | |
263 | setenv("partition", buf); | |
264 | strncpy(last_partition, buf, 16); | |
265 | ||
e1d2950d | 266 | debug("=> partition %s\n", buf); |
68d7d651 SR |
267 | } else { |
268 | setenv("partition", NULL); | |
269 | last_partition[0] = '\0'; | |
270 | ||
e1d2950d | 271 | debug("=> partition NULL\n"); |
68d7d651 SR |
272 | } |
273 | index_partitions(); | |
274 | } | |
275 | ||
0a026d3e BG |
276 | |
277 | /** | |
278 | * Produce a mtd_info given a type and num. | |
279 | * | |
280 | * @param type mtd type | |
281 | * @param num mtd number | |
282 | * @param mtd a pointer to an mtd_info instance (output) | |
283 | * @return 0 if device is valid, 1 otherwise | |
284 | */ | |
285 | static int get_mtd_info(u8 type, u8 num, struct mtd_info **mtd) | |
286 | { | |
287 | char mtd_dev[16]; | |
288 | ||
289 | sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(type), num); | |
290 | *mtd = get_mtd_device_nm(mtd_dev); | |
291 | if (IS_ERR(*mtd)) { | |
292 | printf("Device %s not found!\n", mtd_dev); | |
293 | return 1; | |
294 | } | |
1668d644 | 295 | put_mtd_device(*mtd); |
0a026d3e BG |
296 | |
297 | return 0; | |
298 | } | |
299 | ||
68d7d651 | 300 | /** |
864aa034 SR |
301 | * Performs sanity check for supplied flash partition. |
302 | * Table of existing MTD flash devices is searched and partition device | |
303 | * is located. Alignment with the granularity of nand erasesize is verified. | |
68d7d651 SR |
304 | * |
305 | * @param id of the parent device | |
306 | * @param part partition to validate | |
307 | * @return 0 if partition is valid, 1 otherwise | |
308 | */ | |
864aa034 | 309 | static int part_validate_eraseblock(struct mtdids *id, struct part_info *part) |
68d7d651 | 310 | { |
0a026d3e | 311 | struct mtd_info *mtd = NULL; |
864aa034 SR |
312 | int i, j; |
313 | ulong start; | |
39ac3447 | 314 | u64 offset, size; |
864aa034 | 315 | |
0a026d3e | 316 | if (get_mtd_info(id->type, id->num, &mtd)) |
68d7d651 | 317 | return 1; |
68d7d651 | 318 | |
864aa034 | 319 | part->sector_size = mtd->erasesize; |
68d7d651 | 320 | |
864aa034 SR |
321 | if (!mtd->numeraseregions) { |
322 | /* | |
323 | * Only one eraseregion (NAND, OneNAND or uniform NOR), | |
324 | * checking for alignment is easy here | |
325 | */ | |
39ac3447 PB |
326 | offset = part->offset; |
327 | if (do_div(offset, mtd->erasesize)) { | |
864aa034 SR |
328 | printf("%s%d: partition (%s) start offset" |
329 | "alignment incorrect\n", | |
330 | MTD_DEV_TYPE(id->type), id->num, part->name); | |
331 | return 1; | |
332 | } | |
68d7d651 | 333 | |
39ac3447 PB |
334 | size = part->size; |
335 | if (do_div(size, mtd->erasesize)) { | |
864aa034 SR |
336 | printf("%s%d: partition (%s) size alignment incorrect\n", |
337 | MTD_DEV_TYPE(id->type), id->num, part->name); | |
338 | return 1; | |
339 | } | |
340 | } else { | |
341 | /* | |
342 | * Multiple eraseregions (non-uniform NOR), | |
343 | * checking for alignment is more complex here | |
344 | */ | |
345 | ||
346 | /* Check start alignment */ | |
347 | for (i = 0; i < mtd->numeraseregions; i++) { | |
348 | start = mtd->eraseregions[i].offset; | |
349 | for (j = 0; j < mtd->eraseregions[i].numblocks; j++) { | |
350 | if (part->offset == start) | |
351 | goto start_ok; | |
352 | start += mtd->eraseregions[i].erasesize; | |
353 | } | |
354 | } | |
68d7d651 | 355 | |
68d7d651 | 356 | printf("%s%d: partition (%s) start offset alignment incorrect\n", |
864aa034 | 357 | MTD_DEV_TYPE(id->type), id->num, part->name); |
68d7d651 | 358 | return 1; |
68d7d651 | 359 | |
864aa034 | 360 | start_ok: |
68d7d651 | 361 | |
864aa034 SR |
362 | /* Check end/size alignment */ |
363 | for (i = 0; i < mtd->numeraseregions; i++) { | |
364 | start = mtd->eraseregions[i].offset; | |
365 | for (j = 0; j < mtd->eraseregions[i].numblocks; j++) { | |
366 | if ((part->offset + part->size) == start) | |
367 | goto end_ok; | |
368 | start += mtd->eraseregions[i].erasesize; | |
369 | } | |
370 | } | |
371 | /* Check last sector alignment */ | |
372 | if ((part->offset + part->size) == start) | |
373 | goto end_ok; | |
68d7d651 | 374 | |
68d7d651 | 375 | printf("%s%d: partition (%s) size alignment incorrect\n", |
864aa034 | 376 | MTD_DEV_TYPE(id->type), id->num, part->name); |
68d7d651 | 377 | return 1; |
864aa034 SR |
378 | |
379 | end_ok: | |
380 | return 0; | |
68d7d651 SR |
381 | } |
382 | ||
383 | return 0; | |
68d7d651 SR |
384 | } |
385 | ||
386 | ||
387 | /** | |
f501991d OS |
388 | * Performs sanity check for supplied partition. Offset and size are |
389 | * verified to be within valid range. Partition type is checked and | |
390 | * part_validate_eraseblock() is called with the argument of part. | |
68d7d651 SR |
391 | * |
392 | * @param id of the parent device | |
393 | * @param part partition to validate | |
394 | * @return 0 if partition is valid, 1 otherwise | |
395 | */ | |
396 | static int part_validate(struct mtdids *id, struct part_info *part) | |
397 | { | |
398 | if (part->size == SIZE_REMAINING) | |
399 | part->size = id->size - part->offset; | |
400 | ||
401 | if (part->offset > id->size) { | |
39ac3447 | 402 | printf("%s: offset %08llx beyond flash size %08llx\n", |
68d7d651 SR |
403 | id->mtd_id, part->offset, id->size); |
404 | return 1; | |
405 | } | |
406 | ||
407 | if ((part->offset + part->size) <= part->offset) { | |
408 | printf("%s%d: partition (%s) size too big\n", | |
409 | MTD_DEV_TYPE(id->type), id->num, part->name); | |
410 | return 1; | |
411 | } | |
412 | ||
413 | if (part->offset + part->size > id->size) { | |
414 | printf("%s: partitioning exceeds flash size\n", id->mtd_id); | |
415 | return 1; | |
416 | } | |
417 | ||
864aa034 SR |
418 | /* |
419 | * Now we need to check if the partition starts and ends on | |
420 | * sector (eraseblock) regions | |
421 | */ | |
422 | return part_validate_eraseblock(id, part); | |
68d7d651 SR |
423 | } |
424 | ||
425 | /** | |
1f8b546f | 426 | * Delete selected partition from the partition list of the specified device. |
68d7d651 SR |
427 | * |
428 | * @param dev device to delete partition from | |
429 | * @param part partition to delete | |
430 | * @return 0 on success, 1 otherwise | |
431 | */ | |
432 | static int part_del(struct mtd_device *dev, struct part_info *part) | |
433 | { | |
434 | u8 current_save_needed = 0; | |
435 | ||
436 | /* if there is only one partition, remove whole device */ | |
437 | if (dev->num_parts == 1) | |
438 | return device_del(dev); | |
439 | ||
440 | /* otherwise just delete this partition */ | |
441 | ||
76b5883d | 442 | if (dev == current_mtd_dev) { |
68d7d651 SR |
443 | /* we are modyfing partitions for the current device, |
444 | * update current */ | |
445 | struct part_info *curr_pi; | |
76b5883d | 446 | curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum); |
68d7d651 SR |
447 | |
448 | if (curr_pi) { | |
449 | if (curr_pi == part) { | |
450 | printf("current partition deleted, resetting current to 0\n"); | |
76b5883d | 451 | current_mtd_partnum = 0; |
68d7d651 | 452 | } else if (part->offset <= curr_pi->offset) { |
76b5883d | 453 | current_mtd_partnum--; |
68d7d651 SR |
454 | } |
455 | current_save_needed = 1; | |
456 | } | |
457 | } | |
458 | ||
68d7d651 SR |
459 | list_del(&part->link); |
460 | free(part); | |
461 | dev->num_parts--; | |
462 | ||
463 | if (current_save_needed > 0) | |
464 | current_save(); | |
465 | else | |
466 | index_partitions(); | |
467 | ||
468 | return 0; | |
469 | } | |
470 | ||
471 | /** | |
472 | * Delete all partitions from parts head list, free memory. | |
473 | * | |
474 | * @param head list of partitions to delete | |
475 | */ | |
476 | static void part_delall(struct list_head *head) | |
477 | { | |
478 | struct list_head *entry, *n; | |
479 | struct part_info *part_tmp; | |
480 | ||
481 | /* clean tmp_list and free allocated memory */ | |
482 | list_for_each_safe(entry, n, head) { | |
483 | part_tmp = list_entry(entry, struct part_info, link); | |
484 | ||
68d7d651 SR |
485 | list_del(entry); |
486 | free(part_tmp); | |
487 | } | |
488 | } | |
489 | ||
490 | /** | |
491 | * Add new partition to the supplied partition list. Make sure partitions are | |
492 | * sorted by offset in ascending order. | |
493 | * | |
494 | * @param head list this partition is to be added to | |
495 | * @param new partition to be added | |
496 | */ | |
497 | static int part_sort_add(struct mtd_device *dev, struct part_info *part) | |
498 | { | |
499 | struct list_head *entry; | |
500 | struct part_info *new_pi, *curr_pi; | |
501 | ||
502 | /* link partition to parrent dev */ | |
503 | part->dev = dev; | |
504 | ||
505 | if (list_empty(&dev->parts)) { | |
e1d2950d | 506 | debug("part_sort_add: list empty\n"); |
68d7d651 SR |
507 | list_add(&part->link, &dev->parts); |
508 | dev->num_parts++; | |
509 | index_partitions(); | |
510 | return 0; | |
511 | } | |
512 | ||
513 | new_pi = list_entry(&part->link, struct part_info, link); | |
514 | ||
515 | /* get current partition info if we are updating current device */ | |
516 | curr_pi = NULL; | |
76b5883d SR |
517 | if (dev == current_mtd_dev) |
518 | curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum); | |
68d7d651 SR |
519 | |
520 | list_for_each(entry, &dev->parts) { | |
521 | struct part_info *pi; | |
522 | ||
523 | pi = list_entry(entry, struct part_info, link); | |
524 | ||
525 | /* be compliant with kernel cmdline, allow only one partition at offset zero */ | |
526 | if ((new_pi->offset == pi->offset) && (pi->offset == 0)) { | |
527 | printf("cannot add second partition at offset 0\n"); | |
528 | return 1; | |
529 | } | |
530 | ||
531 | if (new_pi->offset <= pi->offset) { | |
532 | list_add_tail(&part->link, entry); | |
533 | dev->num_parts++; | |
534 | ||
535 | if (curr_pi && (pi->offset <= curr_pi->offset)) { | |
536 | /* we are modyfing partitions for the current | |
537 | * device, update current */ | |
76b5883d | 538 | current_mtd_partnum++; |
68d7d651 SR |
539 | current_save(); |
540 | } else { | |
541 | index_partitions(); | |
542 | } | |
543 | return 0; | |
544 | } | |
545 | } | |
546 | ||
547 | list_add_tail(&part->link, &dev->parts); | |
548 | dev->num_parts++; | |
549 | index_partitions(); | |
550 | return 0; | |
551 | } | |
552 | ||
553 | /** | |
554 | * Add provided partition to the partition list of a given device. | |
555 | * | |
556 | * @param dev device to which partition is added | |
557 | * @param part partition to be added | |
558 | * @return 0 on success, 1 otherwise | |
559 | */ | |
560 | static int part_add(struct mtd_device *dev, struct part_info *part) | |
561 | { | |
562 | /* verify alignment and size */ | |
563 | if (part_validate(dev->id, part) != 0) | |
564 | return 1; | |
565 | ||
566 | /* partition is ok, add it to the list */ | |
567 | if (part_sort_add(dev, part) != 0) | |
568 | return 1; | |
569 | ||
570 | return 0; | |
571 | } | |
572 | ||
573 | /** | |
574 | * Parse one partition definition, allocate memory and return pointer to this | |
575 | * location in retpart. | |
576 | * | |
577 | * @param partdef pointer to the partition definition string i.e. <part-def> | |
578 | * @param ret output pointer to next char after parse completes (output) | |
579 | * @param retpart pointer to the allocated partition (output) | |
580 | * @return 0 on success, 1 otherwise | |
581 | */ | |
582 | static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart) | |
583 | { | |
584 | struct part_info *part; | |
39ac3447 PB |
585 | u64 size; |
586 | u64 offset; | |
68d7d651 SR |
587 | const char *name; |
588 | int name_len; | |
589 | unsigned int mask_flags; | |
590 | const char *p; | |
591 | ||
592 | p = partdef; | |
593 | *retpart = NULL; | |
594 | *ret = NULL; | |
595 | ||
596 | /* fetch the partition size */ | |
597 | if (*p == '-') { | |
598 | /* assign all remaining space to this partition */ | |
e1d2950d | 599 | debug("'-': remaining size assigned\n"); |
68d7d651 SR |
600 | size = SIZE_REMAINING; |
601 | p++; | |
602 | } else { | |
603 | size = memsize_parse(p, &p); | |
604 | if (size < MIN_PART_SIZE) { | |
39ac3447 | 605 | printf("partition size too small (%llx)\n", size); |
68d7d651 SR |
606 | return 1; |
607 | } | |
608 | } | |
609 | ||
610 | /* check for offset */ | |
611 | offset = OFFSET_NOT_SPECIFIED; | |
612 | if (*p == '@') { | |
613 | p++; | |
614 | offset = memsize_parse(p, &p); | |
615 | } | |
616 | ||
617 | /* now look for the name */ | |
618 | if (*p == '(') { | |
619 | name = ++p; | |
620 | if ((p = strchr(name, ')')) == NULL) { | |
621 | printf("no closing ) found in partition name\n"); | |
622 | return 1; | |
623 | } | |
624 | name_len = p - name + 1; | |
625 | if ((name_len - 1) == 0) { | |
626 | printf("empty partition name\n"); | |
627 | return 1; | |
628 | } | |
629 | p++; | |
630 | } else { | |
631 | /* 0x00000000@0x00000000 */ | |
632 | name_len = 22; | |
633 | name = NULL; | |
634 | } | |
635 | ||
636 | /* test for options */ | |
637 | mask_flags = 0; | |
638 | if (strncmp(p, "ro", 2) == 0) { | |
639 | mask_flags |= MTD_WRITEABLE_CMD; | |
640 | p += 2; | |
641 | } | |
642 | ||
643 | /* check for next partition definition */ | |
644 | if (*p == ',') { | |
645 | if (size == SIZE_REMAINING) { | |
646 | *ret = NULL; | |
647 | printf("no partitions allowed after a fill-up partition\n"); | |
648 | return 1; | |
649 | } | |
650 | *ret = ++p; | |
651 | } else if ((*p == ';') || (*p == '\0')) { | |
652 | *ret = p; | |
653 | } else { | |
654 | printf("unexpected character '%c' at the end of partition\n", *p); | |
655 | *ret = NULL; | |
656 | return 1; | |
657 | } | |
658 | ||
659 | /* allocate memory */ | |
660 | part = (struct part_info *)malloc(sizeof(struct part_info) + name_len); | |
661 | if (!part) { | |
662 | printf("out of memory\n"); | |
663 | return 1; | |
664 | } | |
665 | memset(part, 0, sizeof(struct part_info) + name_len); | |
666 | part->size = size; | |
667 | part->offset = offset; | |
668 | part->mask_flags = mask_flags; | |
669 | part->name = (char *)(part + 1); | |
670 | ||
671 | if (name) { | |
672 | /* copy user provided name */ | |
673 | strncpy(part->name, name, name_len - 1); | |
674 | part->auto_name = 0; | |
675 | } else { | |
676 | /* auto generated name in form of size@offset */ | |
39ac3447 | 677 | sprintf(part->name, "0x%08llx@0x%08llx", size, offset); |
68d7d651 SR |
678 | part->auto_name = 1; |
679 | } | |
680 | ||
681 | part->name[name_len - 1] = '\0'; | |
682 | INIT_LIST_HEAD(&part->link); | |
683 | ||
39ac3447 | 684 | debug("+ partition: name %-22s size 0x%08llx offset 0x%08llx mask flags %d\n", |
68d7d651 SR |
685 | part->name, part->size, |
686 | part->offset, part->mask_flags); | |
687 | ||
688 | *retpart = part; | |
689 | return 0; | |
690 | } | |
691 | ||
692 | /** | |
693 | * Check device number to be within valid range for given device type. | |
694 | * | |
0a026d3e BG |
695 | * @param type mtd type |
696 | * @param num mtd number | |
697 | * @param size a pointer to the size of the mtd device (output) | |
68d7d651 SR |
698 | * @return 0 if device is valid, 1 otherwise |
699 | */ | |
39ac3447 | 700 | static int mtd_device_validate(u8 type, u8 num, u64 *size) |
68d7d651 | 701 | { |
0a026d3e | 702 | struct mtd_info *mtd = NULL; |
68d7d651 | 703 | |
0a026d3e | 704 | if (get_mtd_info(type, num, &mtd)) |
864aa034 | 705 | return 1; |
68d7d651 | 706 | |
864aa034 | 707 | *size = mtd->size; |
68d7d651 | 708 | |
864aa034 | 709 | return 0; |
68d7d651 SR |
710 | } |
711 | ||
712 | /** | |
713 | * Delete all mtd devices from a supplied devices list, free memory allocated for | |
714 | * each device and delete all device partitions. | |
715 | * | |
716 | * @return 0 on success, 1 otherwise | |
717 | */ | |
718 | static int device_delall(struct list_head *head) | |
719 | { | |
720 | struct list_head *entry, *n; | |
721 | struct mtd_device *dev_tmp; | |
722 | ||
723 | /* clean devices list */ | |
724 | list_for_each_safe(entry, n, head) { | |
725 | dev_tmp = list_entry(entry, struct mtd_device, link); | |
726 | list_del(entry); | |
727 | part_delall(&dev_tmp->parts); | |
728 | free(dev_tmp); | |
729 | } | |
730 | INIT_LIST_HEAD(&devices); | |
731 | ||
732 | return 0; | |
733 | } | |
734 | ||
735 | /** | |
736 | * If provided device exists it's partitions are deleted, device is removed | |
737 | * from device list and device memory is freed. | |
738 | * | |
739 | * @param dev device to be deleted | |
740 | * @return 0 on success, 1 otherwise | |
741 | */ | |
742 | static int device_del(struct mtd_device *dev) | |
743 | { | |
744 | part_delall(&dev->parts); | |
745 | list_del(&dev->link); | |
746 | free(dev); | |
747 | ||
76b5883d | 748 | if (dev == current_mtd_dev) { |
68d7d651 SR |
749 | /* we just deleted current device */ |
750 | if (list_empty(&devices)) { | |
76b5883d | 751 | current_mtd_dev = NULL; |
68d7d651 SR |
752 | } else { |
753 | /* reset first partition from first dev from the | |
754 | * devices list as current */ | |
76b5883d SR |
755 | current_mtd_dev = list_entry(devices.next, struct mtd_device, link); |
756 | current_mtd_partnum = 0; | |
68d7d651 SR |
757 | } |
758 | current_save(); | |
759 | return 0; | |
760 | } | |
761 | ||
762 | index_partitions(); | |
763 | return 0; | |
764 | } | |
765 | ||
766 | /** | |
767 | * Search global device list and return pointer to the device of type and num | |
768 | * specified. | |
769 | * | |
770 | * @param type device type | |
771 | * @param num device number | |
772 | * @return NULL if requested device does not exist | |
773 | */ | |
3c950e2e | 774 | struct mtd_device *device_find(u8 type, u8 num) |
68d7d651 SR |
775 | { |
776 | struct list_head *entry; | |
777 | struct mtd_device *dev_tmp; | |
778 | ||
779 | list_for_each(entry, &devices) { | |
780 | dev_tmp = list_entry(entry, struct mtd_device, link); | |
781 | ||
782 | if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num)) | |
783 | return dev_tmp; | |
784 | } | |
785 | ||
786 | return NULL; | |
787 | } | |
788 | ||
789 | /** | |
790 | * Add specified device to the global device list. | |
791 | * | |
792 | * @param dev device to be added | |
793 | */ | |
794 | static void device_add(struct mtd_device *dev) | |
795 | { | |
796 | u8 current_save_needed = 0; | |
797 | ||
798 | if (list_empty(&devices)) { | |
76b5883d SR |
799 | current_mtd_dev = dev; |
800 | current_mtd_partnum = 0; | |
68d7d651 SR |
801 | current_save_needed = 1; |
802 | } | |
803 | ||
804 | list_add_tail(&dev->link, &devices); | |
805 | ||
806 | if (current_save_needed > 0) | |
807 | current_save(); | |
808 | else | |
809 | index_partitions(); | |
810 | } | |
811 | ||
812 | /** | |
813 | * Parse device type, name and mtd-id. If syntax is ok allocate memory and | |
814 | * return pointer to the device structure. | |
815 | * | |
816 | * @param mtd_dev pointer to the device definition string i.e. <mtd-dev> | |
817 | * @param ret output pointer to next char after parse completes (output) | |
818 | * @param retdev pointer to the allocated device (output) | |
819 | * @return 0 on success, 1 otherwise | |
820 | */ | |
821 | static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev) | |
822 | { | |
823 | struct mtd_device *dev; | |
824 | struct part_info *part; | |
825 | struct mtdids *id; | |
826 | const char *mtd_id; | |
827 | unsigned int mtd_id_len; | |
419a1fe9 | 828 | const char *p; |
419a1fe9 | 829 | const char *pend; |
68d7d651 SR |
830 | LIST_HEAD(tmp_list); |
831 | struct list_head *entry, *n; | |
832 | u16 num_parts; | |
39ac3447 | 833 | u64 offset; |
68d7d651 SR |
834 | int err = 1; |
835 | ||
e1d2950d | 836 | debug("===device_parse===\n"); |
2697eff1 WD |
837 | |
838 | assert(retdev); | |
68d7d651 | 839 | *retdev = NULL; |
68d7d651 | 840 | |
2697eff1 WD |
841 | if (ret) |
842 | *ret = NULL; | |
68d7d651 SR |
843 | |
844 | /* fetch <mtd-id> */ | |
2697eff1 | 845 | mtd_id = p = mtd_dev; |
68d7d651 SR |
846 | if (!(p = strchr(mtd_id, ':'))) { |
847 | printf("no <mtd-id> identifier\n"); | |
848 | return 1; | |
849 | } | |
850 | mtd_id_len = p - mtd_id + 1; | |
851 | p++; | |
852 | ||
853 | /* verify if we have a valid device specified */ | |
854 | if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) { | |
855 | printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id); | |
856 | return 1; | |
857 | } | |
858 | ||
419a1fe9 WD |
859 | #ifdef DEBUG |
860 | pend = strchr(p, ';'); | |
861 | #endif | |
e1d2950d | 862 | debug("dev type = %d (%s), dev num = %d, mtd-id = %s\n", |
68d7d651 SR |
863 | id->type, MTD_DEV_TYPE(id->type), |
864 | id->num, id->mtd_id); | |
5d69a5d1 | 865 | debug("parsing partitions %.*s\n", (int)(pend ? pend - p : strlen(p)), p); |
68d7d651 SR |
866 | |
867 | ||
868 | /* parse partitions */ | |
869 | num_parts = 0; | |
870 | ||
871 | offset = 0; | |
872 | if ((dev = device_find(id->type, id->num)) != NULL) { | |
873 | /* if device already exists start at the end of the last partition */ | |
874 | part = list_entry(dev->parts.prev, struct part_info, link); | |
875 | offset = part->offset + part->size; | |
876 | } | |
877 | ||
878 | while (p && (*p != '\0') && (*p != ';')) { | |
879 | err = 1; | |
880 | if ((part_parse(p, &p, &part) != 0) || (!part)) | |
881 | break; | |
882 | ||
883 | /* calculate offset when not specified */ | |
884 | if (part->offset == OFFSET_NOT_SPECIFIED) | |
885 | part->offset = offset; | |
886 | else | |
887 | offset = part->offset; | |
888 | ||
889 | /* verify alignment and size */ | |
890 | if (part_validate(id, part) != 0) | |
891 | break; | |
892 | ||
893 | offset += part->size; | |
894 | ||
895 | /* partition is ok, add it to the list */ | |
896 | list_add_tail(&part->link, &tmp_list); | |
897 | num_parts++; | |
898 | err = 0; | |
899 | } | |
900 | if (err == 1) { | |
901 | part_delall(&tmp_list); | |
902 | return 1; | |
903 | } | |
904 | ||
905 | if (num_parts == 0) { | |
906 | printf("no partitions for device %s%d (%s)\n", | |
907 | MTD_DEV_TYPE(id->type), id->num, id->mtd_id); | |
908 | return 1; | |
909 | } | |
910 | ||
e1d2950d | 911 | debug("\ntotal partitions: %d\n", num_parts); |
68d7d651 SR |
912 | |
913 | /* check for next device presence */ | |
914 | if (p) { | |
915 | if (*p == ';') { | |
2697eff1 WD |
916 | if (ret) |
917 | *ret = ++p; | |
68d7d651 | 918 | } else if (*p == '\0') { |
2697eff1 WD |
919 | if (ret) |
920 | *ret = p; | |
68d7d651 SR |
921 | } else { |
922 | printf("unexpected character '%c' at the end of device\n", *p); | |
2697eff1 WD |
923 | if (ret) |
924 | *ret = NULL; | |
68d7d651 SR |
925 | return 1; |
926 | } | |
927 | } | |
928 | ||
929 | /* allocate memory for mtd_device structure */ | |
930 | if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) { | |
931 | printf("out of memory\n"); | |
932 | return 1; | |
933 | } | |
934 | memset(dev, 0, sizeof(struct mtd_device)); | |
935 | dev->id = id; | |
936 | dev->num_parts = 0; /* part_sort_add increments num_parts */ | |
937 | INIT_LIST_HEAD(&dev->parts); | |
938 | INIT_LIST_HEAD(&dev->link); | |
939 | ||
940 | /* move partitions from tmp_list to dev->parts */ | |
941 | list_for_each_safe(entry, n, &tmp_list) { | |
942 | part = list_entry(entry, struct part_info, link); | |
943 | list_del(entry); | |
944 | if (part_sort_add(dev, part) != 0) { | |
945 | device_del(dev); | |
946 | return 1; | |
947 | } | |
948 | } | |
949 | ||
950 | *retdev = dev; | |
951 | ||
e1d2950d | 952 | debug("===\n\n"); |
68d7d651 SR |
953 | return 0; |
954 | } | |
955 | ||
956 | /** | |
957 | * Initialize global device list. | |
958 | * | |
959 | * @return 0 on success, 1 otherwise | |
960 | */ | |
961 | static int mtd_devices_init(void) | |
962 | { | |
963 | last_parts[0] = '\0'; | |
76b5883d | 964 | current_mtd_dev = NULL; |
68d7d651 SR |
965 | current_save(); |
966 | ||
967 | return device_delall(&devices); | |
968 | } | |
969 | ||
970 | /* | |
971 | * Search global mtdids list and find id of requested type and number. | |
972 | * | |
973 | * @return pointer to the id if it exists, NULL otherwise | |
974 | */ | |
975 | static struct mtdids* id_find(u8 type, u8 num) | |
976 | { | |
977 | struct list_head *entry; | |
978 | struct mtdids *id; | |
979 | ||
980 | list_for_each(entry, &mtdids) { | |
981 | id = list_entry(entry, struct mtdids, link); | |
982 | ||
983 | if ((id->type == type) && (id->num == num)) | |
984 | return id; | |
985 | } | |
986 | ||
987 | return NULL; | |
988 | } | |
989 | ||
990 | /** | |
991 | * Search global mtdids list and find id of a requested mtd_id. | |
992 | * | |
993 | * Note: first argument is not null terminated. | |
994 | * | |
995 | * @param mtd_id string containing requested mtd_id | |
996 | * @param mtd_id_len length of supplied mtd_id | |
997 | * @return pointer to the id if it exists, NULL otherwise | |
998 | */ | |
999 | static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len) | |
1000 | { | |
1001 | struct list_head *entry; | |
1002 | struct mtdids *id; | |
1003 | ||
e1d2950d | 1004 | debug("--- id_find_by_mtd_id: '%.*s' (len = %d)\n", |
68d7d651 SR |
1005 | mtd_id_len, mtd_id, mtd_id_len); |
1006 | ||
1007 | list_for_each(entry, &mtdids) { | |
1008 | id = list_entry(entry, struct mtdids, link); | |
1009 | ||
5d69a5d1 | 1010 | debug("entry: '%s' (len = %zu)\n", |
68d7d651 SR |
1011 | id->mtd_id, strlen(id->mtd_id)); |
1012 | ||
1013 | if (mtd_id_len != strlen(id->mtd_id)) | |
1014 | continue; | |
1015 | if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0) | |
1016 | return id; | |
1017 | } | |
1018 | ||
1019 | return NULL; | |
1020 | } | |
1021 | ||
1022 | /** | |
1023 | * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>, | |
1024 | * return device type and number. | |
1025 | * | |
1026 | * @param id string describing device id | |
1027 | * @param ret_id output pointer to next char after parse completes (output) | |
1028 | * @param dev_type parsed device type (output) | |
1029 | * @param dev_num parsed device number (output) | |
1030 | * @return 0 on success, 1 otherwise | |
1031 | */ | |
088f1b19 KP |
1032 | int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, |
1033 | u8 *dev_num) | |
68d7d651 SR |
1034 | { |
1035 | const char *p = id; | |
1036 | ||
1037 | *dev_type = 0; | |
1038 | if (strncmp(p, "nand", 4) == 0) { | |
1039 | *dev_type = MTD_DEV_TYPE_NAND; | |
1040 | p += 4; | |
1041 | } else if (strncmp(p, "nor", 3) == 0) { | |
1042 | *dev_type = MTD_DEV_TYPE_NOR; | |
1043 | p += 3; | |
1044 | } else if (strncmp(p, "onenand", 7) == 0) { | |
1045 | *dev_type = MTD_DEV_TYPE_ONENAND; | |
1046 | p += 7; | |
1047 | } else { | |
1048 | printf("incorrect device type in %s\n", id); | |
1049 | return 1; | |
1050 | } | |
1051 | ||
1052 | if (!isdigit(*p)) { | |
1053 | printf("incorrect device number in %s\n", id); | |
1054 | return 1; | |
1055 | } | |
1056 | ||
1057 | *dev_num = simple_strtoul(p, (char **)&p, 0); | |
1058 | if (ret_id) | |
1059 | *ret_id = p; | |
1060 | return 0; | |
1061 | } | |
1062 | ||
1063 | /** | |
1064 | * Process all devices and generate corresponding mtdparts string describing | |
1065 | * all partitions on all devices. | |
1066 | * | |
1067 | * @param buf output buffer holding generated mtdparts string (output) | |
1068 | * @param buflen buffer size | |
1069 | * @return 0 on success, 1 otherwise | |
1070 | */ | |
1071 | static int generate_mtdparts(char *buf, u32 buflen) | |
1072 | { | |
1073 | struct list_head *pentry, *dentry; | |
1074 | struct mtd_device *dev; | |
1075 | struct part_info *part, *prev_part; | |
1076 | char *p = buf; | |
1077 | char tmpbuf[32]; | |
39ac3447 PB |
1078 | u64 size, offset; |
1079 | u32 len, part_cnt; | |
68d7d651 SR |
1080 | u32 maxlen = buflen - 1; |
1081 | ||
e1d2950d | 1082 | debug("--- generate_mtdparts ---\n"); |
68d7d651 SR |
1083 | |
1084 | if (list_empty(&devices)) { | |
1085 | buf[0] = '\0'; | |
1086 | return 0; | |
1087 | } | |
1088 | ||
192bc694 | 1089 | strcpy(p, "mtdparts="); |
68d7d651 SR |
1090 | p += 9; |
1091 | ||
1092 | list_for_each(dentry, &devices) { | |
1093 | dev = list_entry(dentry, struct mtd_device, link); | |
1094 | ||
1095 | /* copy mtd_id */ | |
1096 | len = strlen(dev->id->mtd_id) + 1; | |
1097 | if (len > maxlen) | |
1098 | goto cleanup; | |
1099 | memcpy(p, dev->id->mtd_id, len - 1); | |
1100 | p += len - 1; | |
1101 | *(p++) = ':'; | |
1102 | maxlen -= len; | |
1103 | ||
1104 | /* format partitions */ | |
1105 | prev_part = NULL; | |
1106 | part_cnt = 0; | |
1107 | list_for_each(pentry, &dev->parts) { | |
1108 | part = list_entry(pentry, struct part_info, link); | |
1109 | size = part->size; | |
1110 | offset = part->offset; | |
1111 | part_cnt++; | |
1112 | ||
1113 | /* partition size */ | |
1114 | memsize_format(tmpbuf, size); | |
1115 | len = strlen(tmpbuf); | |
1116 | if (len > maxlen) | |
1117 | goto cleanup; | |
1118 | memcpy(p, tmpbuf, len); | |
1119 | p += len; | |
1120 | maxlen -= len; | |
1121 | ||
1122 | ||
1123 | /* add offset only when there is a gap between | |
1124 | * partitions */ | |
1125 | if ((!prev_part && (offset != 0)) || | |
1126 | (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) { | |
1127 | ||
1128 | memsize_format(tmpbuf, offset); | |
1129 | len = strlen(tmpbuf) + 1; | |
1130 | if (len > maxlen) | |
1131 | goto cleanup; | |
1132 | *(p++) = '@'; | |
1133 | memcpy(p, tmpbuf, len - 1); | |
1134 | p += len - 1; | |
1135 | maxlen -= len; | |
1136 | } | |
1137 | ||
1138 | /* copy name only if user supplied */ | |
1139 | if(!part->auto_name) { | |
1140 | len = strlen(part->name) + 2; | |
1141 | if (len > maxlen) | |
1142 | goto cleanup; | |
1143 | ||
1144 | *(p++) = '('; | |
1145 | memcpy(p, part->name, len - 2); | |
1146 | p += len - 2; | |
1147 | *(p++) = ')'; | |
1148 | maxlen -= len; | |
1149 | } | |
1150 | ||
1151 | /* ro mask flag */ | |
1152 | if (part->mask_flags && MTD_WRITEABLE_CMD) { | |
1153 | len = 2; | |
1154 | if (len > maxlen) | |
1155 | goto cleanup; | |
1156 | *(p++) = 'r'; | |
1157 | *(p++) = 'o'; | |
1158 | maxlen -= 2; | |
1159 | } | |
1160 | ||
1161 | /* print ',' separator if there are other partitions | |
1162 | * following */ | |
1163 | if (dev->num_parts > part_cnt) { | |
1164 | if (1 > maxlen) | |
1165 | goto cleanup; | |
1166 | *(p++) = ','; | |
1167 | maxlen--; | |
1168 | } | |
1169 | prev_part = part; | |
1170 | } | |
1171 | /* print ';' separator if there are other devices following */ | |
1172 | if (dentry->next != &devices) { | |
1173 | if (1 > maxlen) | |
1174 | goto cleanup; | |
1175 | *(p++) = ';'; | |
1176 | maxlen--; | |
1177 | } | |
1178 | } | |
1179 | ||
1180 | /* we still have at least one char left, as we decremented maxlen at | |
1181 | * the begining */ | |
1182 | *p = '\0'; | |
1183 | ||
1184 | return 0; | |
1185 | ||
1186 | cleanup: | |
1187 | last_parts[0] = '\0'; | |
1188 | return 1; | |
1189 | } | |
1190 | ||
1191 | /** | |
1192 | * Call generate_mtdparts to process all devices and generate corresponding | |
1193 | * mtdparts string, save it in mtdparts environment variable. | |
1194 | * | |
1195 | * @param buf output buffer holding generated mtdparts string (output) | |
1196 | * @param buflen buffer size | |
1197 | * @return 0 on success, 1 otherwise | |
1198 | */ | |
1199 | static int generate_mtdparts_save(char *buf, u32 buflen) | |
1200 | { | |
1201 | int ret; | |
1202 | ||
1203 | ret = generate_mtdparts(buf, buflen); | |
1204 | ||
1205 | if ((buf[0] != '\0') && (ret == 0)) | |
1206 | setenv("mtdparts", buf); | |
1207 | else | |
1208 | setenv("mtdparts", NULL); | |
1209 | ||
1210 | return ret; | |
1211 | } | |
1212 | ||
04ac3802 | 1213 | #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) |
68d7d651 | 1214 | /** |
04ac3802 BG |
1215 | * Get the net size (w/o bad blocks) of the given partition. |
1216 | * | |
1217 | * @param mtd the mtd info | |
1218 | * @param part the partition | |
1219 | * @return the calculated net size of this partition | |
68d7d651 | 1220 | */ |
04ac3802 BG |
1221 | static uint64_t net_part_size(struct mtd_info *mtd, struct part_info *part) |
1222 | { | |
36650ca9 SW |
1223 | uint64_t i, net_size = 0; |
1224 | ||
04ac3802 BG |
1225 | if (!mtd->block_isbad) |
1226 | return part->size; | |
1227 | ||
04ac3802 BG |
1228 | for (i = 0; i < part->size; i += mtd->erasesize) { |
1229 | if (!mtd->block_isbad(mtd, part->offset + i)) | |
1230 | net_size += mtd->erasesize; | |
1231 | } | |
36650ca9 | 1232 | |
04ac3802 BG |
1233 | return net_size; |
1234 | } | |
1235 | #endif | |
1236 | ||
1237 | static void print_partition_table(void) | |
68d7d651 SR |
1238 | { |
1239 | struct list_head *dentry, *pentry; | |
1240 | struct part_info *part; | |
1241 | struct mtd_device *dev; | |
1242 | int part_num; | |
1243 | ||
68d7d651 SR |
1244 | list_for_each(dentry, &devices) { |
1245 | dev = list_entry(dentry, struct mtd_device, link); | |
04ac3802 BG |
1246 | /* list partitions for given device */ |
1247 | part_num = 0; | |
1248 | #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) | |
1249 | struct mtd_info *mtd; | |
1250 | ||
1251 | if (get_mtd_info(dev->id->type, dev->id->num, &mtd)) | |
1252 | return; | |
1253 | ||
1254 | printf("\ndevice %s%d <%s>, # parts = %d\n", | |
1255 | MTD_DEV_TYPE(dev->id->type), dev->id->num, | |
1256 | dev->id->mtd_id, dev->num_parts); | |
1257 | printf(" #: name\t\tsize\t\tnet size\toffset\t\tmask_flags\n"); | |
1258 | ||
1259 | list_for_each(pentry, &dev->parts) { | |
1260 | u32 net_size; | |
1261 | char *size_note; | |
1262 | ||
1263 | part = list_entry(pentry, struct part_info, link); | |
1264 | net_size = net_part_size(mtd, part); | |
1265 | size_note = part->size == net_size ? " " : " (!)"; | |
1266 | printf("%2d: %-20s0x%08x\t0x%08x%s\t0x%08x\t%d\n", | |
1267 | part_num, part->name, part->size, | |
1268 | net_size, size_note, part->offset, | |
1269 | part->mask_flags); | |
1270 | #else /* !defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */ | |
68d7d651 SR |
1271 | printf("\ndevice %s%d <%s>, # parts = %d\n", |
1272 | MTD_DEV_TYPE(dev->id->type), dev->id->num, | |
1273 | dev->id->mtd_id, dev->num_parts); | |
08f077da | 1274 | printf(" #: name\t\tsize\t\toffset\t\tmask_flags\n"); |
68d7d651 | 1275 | |
68d7d651 SR |
1276 | list_for_each(pentry, &dev->parts) { |
1277 | part = list_entry(pentry, struct part_info, link); | |
39ac3447 | 1278 | printf("%2d: %-20s0x%08llx\t0x%08llx\t%d\n", |
68d7d651 SR |
1279 | part_num, part->name, part->size, |
1280 | part->offset, part->mask_flags); | |
04ac3802 | 1281 | #endif /* defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */ |
68d7d651 SR |
1282 | part_num++; |
1283 | } | |
1284 | } | |
04ac3802 | 1285 | |
68d7d651 SR |
1286 | if (list_empty(&devices)) |
1287 | printf("no partitions defined\n"); | |
04ac3802 BG |
1288 | } |
1289 | ||
1290 | /** | |
1291 | * Format and print out a partition list for each device from global device | |
1292 | * list. | |
1293 | */ | |
1294 | static void list_partitions(void) | |
1295 | { | |
1296 | struct part_info *part; | |
1297 | ||
1298 | debug("\n---list_partitions---\n"); | |
1299 | print_partition_table(); | |
68d7d651 | 1300 | |
76b5883d SR |
1301 | /* current_mtd_dev is not NULL only when we have non empty device list */ |
1302 | if (current_mtd_dev) { | |
1303 | part = mtd_part_info(current_mtd_dev, current_mtd_partnum); | |
68d7d651 | 1304 | if (part) { |
39ac3447 | 1305 | printf("\nactive partition: %s%d,%d - (%s) 0x%08llx @ 0x%08llx\n", |
76b5883d SR |
1306 | MTD_DEV_TYPE(current_mtd_dev->id->type), |
1307 | current_mtd_dev->id->num, current_mtd_partnum, | |
68d7d651 SR |
1308 | part->name, part->size, part->offset); |
1309 | } else { | |
1310 | printf("could not get current partition info\n\n"); | |
1311 | } | |
1312 | } | |
1313 | ||
1314 | printf("\ndefaults:\n"); | |
36c9169a WD |
1315 | printf("mtdids : %s\n", |
1316 | mtdids_default ? mtdids_default : "none"); | |
a693447c AG |
1317 | /* |
1318 | * Using printf() here results in printbuffer overflow | |
1319 | * if default mtdparts string is greater than console | |
1320 | * printbuffer. Use puts() to prevent system crashes. | |
1321 | */ | |
1322 | puts("mtdparts: "); | |
1323 | puts(mtdparts_default ? mtdparts_default : "none"); | |
1324 | puts("\n"); | |
68d7d651 SR |
1325 | } |
1326 | ||
1327 | /** | |
1328 | * Given partition identifier in form of <dev_type><dev_num>,<part_num> find | |
1329 | * corresponding device and verify partition number. | |
1330 | * | |
1331 | * @param id string describing device and partition or partition name | |
1332 | * @param dev pointer to the requested device (output) | |
1333 | * @param part_num verified partition number (output) | |
1334 | * @param part pointer to requested partition (output) | |
1335 | * @return 0 on success, 1 otherwise | |
1336 | */ | |
1337 | int find_dev_and_part(const char *id, struct mtd_device **dev, | |
1338 | u8 *part_num, struct part_info **part) | |
1339 | { | |
1340 | struct list_head *dentry, *pentry; | |
1341 | u8 type, dnum, pnum; | |
1342 | const char *p; | |
1343 | ||
e1d2950d | 1344 | debug("--- find_dev_and_part ---\nid = %s\n", id); |
68d7d651 SR |
1345 | |
1346 | list_for_each(dentry, &devices) { | |
1347 | *part_num = 0; | |
1348 | *dev = list_entry(dentry, struct mtd_device, link); | |
1349 | list_for_each(pentry, &(*dev)->parts) { | |
1350 | *part = list_entry(pentry, struct part_info, link); | |
1351 | if (strcmp((*part)->name, id) == 0) | |
1352 | return 0; | |
1353 | (*part_num)++; | |
1354 | } | |
1355 | } | |
1356 | ||
1357 | p = id; | |
1358 | *dev = NULL; | |
1359 | *part = NULL; | |
1360 | *part_num = 0; | |
1361 | ||
1362 | if (mtd_id_parse(p, &p, &type, &dnum) != 0) | |
1363 | return 1; | |
1364 | ||
1365 | if ((*p++ != ',') || (*p == '\0')) { | |
1366 | printf("no partition number specified\n"); | |
1367 | return 1; | |
1368 | } | |
1369 | pnum = simple_strtoul(p, (char **)&p, 0); | |
1370 | if (*p != '\0') { | |
1371 | printf("unexpected trailing character '%c'\n", *p); | |
1372 | return 1; | |
1373 | } | |
1374 | ||
1375 | if ((*dev = device_find(type, dnum)) == NULL) { | |
1376 | printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum); | |
1377 | return 1; | |
1378 | } | |
1379 | ||
1380 | if ((*part = mtd_part_info(*dev, pnum)) == NULL) { | |
1381 | printf("no such partition\n"); | |
1382 | *dev = NULL; | |
1383 | return 1; | |
1384 | } | |
1385 | ||
1386 | *part_num = pnum; | |
1387 | ||
1388 | return 0; | |
1389 | } | |
1390 | ||
1391 | /** | |
1392 | * Find and delete partition. For partition id format see find_dev_and_part(). | |
1393 | * | |
1394 | * @param id string describing device and partition | |
1395 | * @return 0 on success, 1 otherwise | |
1396 | */ | |
1397 | static int delete_partition(const char *id) | |
1398 | { | |
1399 | u8 pnum; | |
1400 | struct mtd_device *dev; | |
1401 | struct part_info *part; | |
1402 | ||
1403 | if (find_dev_and_part(id, &dev, &pnum, &part) == 0) { | |
1404 | ||
39ac3447 | 1405 | debug("delete_partition: device = %s%d, partition %d = (%s) 0x%08llx@0x%08llx\n", |
68d7d651 SR |
1406 | MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum, |
1407 | part->name, part->size, part->offset); | |
1408 | ||
1409 | if (part_del(dev, part) != 0) | |
1410 | return 1; | |
1411 | ||
1412 | if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) { | |
a22bf16b | 1413 | printf("generated mtdparts too long, resetting to null\n"); |
68d7d651 SR |
1414 | return 1; |
1415 | } | |
1416 | return 0; | |
1417 | } | |
1418 | ||
1419 | printf("partition %s not found\n", id); | |
1420 | return 1; | |
1421 | } | |
1422 | ||
ca75b20e BG |
1423 | #if defined(CONFIG_CMD_MTDPARTS_SPREAD) |
1424 | /** | |
1425 | * Increase the size of the given partition so that it's net size is at least | |
1426 | * as large as the size member and such that the next partition would start on a | |
1427 | * good block if it were adjacent to this partition. | |
1428 | * | |
1429 | * @param mtd the mtd device | |
1430 | * @param part the partition | |
1431 | * @param next_offset pointer to the offset of the next partition after this | |
1432 | * partition's size has been modified (output) | |
1433 | */ | |
1434 | static void spread_partition(struct mtd_info *mtd, struct part_info *part, | |
1435 | uint64_t *next_offset) | |
1436 | { | |
1437 | uint64_t net_size, padding_size = 0; | |
1438 | int truncated; | |
1439 | ||
1440 | mtd_get_len_incl_bad(mtd, part->offset, part->size, &net_size, | |
1441 | &truncated); | |
1442 | ||
1443 | /* | |
1444 | * Absorb bad blocks immediately following this | |
1445 | * partition also into the partition, such that | |
1446 | * the next partition starts with a good block. | |
1447 | */ | |
1448 | if (!truncated) { | |
1449 | mtd_get_len_incl_bad(mtd, part->offset + net_size, | |
1450 | mtd->erasesize, &padding_size, &truncated); | |
1451 | if (truncated) | |
1452 | padding_size = 0; | |
1453 | else | |
1454 | padding_size -= mtd->erasesize; | |
1455 | } | |
1456 | ||
1457 | if (truncated) { | |
1458 | printf("truncated partition %s to %lld bytes\n", part->name, | |
1459 | (uint64_t) net_size + padding_size); | |
1460 | } | |
1461 | ||
1462 | part->size = net_size + padding_size; | |
1463 | *next_offset = part->offset + part->size; | |
1464 | } | |
1465 | ||
1466 | /** | |
1467 | * Adjust all of the partition sizes, such that all partitions are at least | |
1468 | * as big as their mtdparts environment variable sizes and they each start | |
1469 | * on a good block. | |
1470 | * | |
1471 | * @return 0 on success, 1 otherwise | |
1472 | */ | |
1473 | static int spread_partitions(void) | |
1474 | { | |
1475 | struct list_head *dentry, *pentry; | |
1476 | struct mtd_device *dev; | |
1477 | struct part_info *part; | |
1478 | struct mtd_info *mtd; | |
1479 | int part_num; | |
1480 | uint64_t cur_offs; | |
1481 | ||
1482 | list_for_each(dentry, &devices) { | |
1483 | dev = list_entry(dentry, struct mtd_device, link); | |
1484 | ||
1485 | if (get_mtd_info(dev->id->type, dev->id->num, &mtd)) | |
1486 | return 1; | |
1487 | ||
1488 | part_num = 0; | |
1489 | cur_offs = 0; | |
1490 | list_for_each(pentry, &dev->parts) { | |
1491 | part = list_entry(pentry, struct part_info, link); | |
1492 | ||
1493 | debug("spread_partitions: device = %s%d, partition %d =" | |
1494 | " (%s) 0x%08x@0x%08x\n", | |
1495 | MTD_DEV_TYPE(dev->id->type), dev->id->num, | |
1496 | part_num, part->name, part->size, | |
1497 | part->offset); | |
1498 | ||
1499 | if (cur_offs > part->offset) | |
1500 | part->offset = cur_offs; | |
1501 | ||
1502 | spread_partition(mtd, part, &cur_offs); | |
1503 | ||
1504 | part_num++; | |
1505 | } | |
1506 | } | |
1507 | ||
1508 | index_partitions(); | |
1509 | ||
1510 | if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) { | |
a22bf16b | 1511 | printf("generated mtdparts too long, resetting to null\n"); |
ca75b20e BG |
1512 | return 1; |
1513 | } | |
1514 | return 0; | |
1515 | } | |
1516 | #endif /* CONFIG_CMD_MTDPARTS_SPREAD */ | |
1517 | ||
68d7d651 SR |
1518 | /** |
1519 | * Accept character string describing mtd partitions and call device_parse() | |
1520 | * for each entry. Add created devices to the global devices list. | |
1521 | * | |
1522 | * @param mtdparts string specifing mtd partitions | |
1523 | * @return 0 on success, 1 otherwise | |
1524 | */ | |
1525 | static int parse_mtdparts(const char *const mtdparts) | |
1526 | { | |
06a040a3 | 1527 | const char *p; |
68d7d651 SR |
1528 | struct mtd_device *dev; |
1529 | int err = 1; | |
a7eb1d66 | 1530 | char tmp_parts[MTDPARTS_MAXLEN]; |
68d7d651 | 1531 | |
e1d2950d | 1532 | debug("\n---parse_mtdparts---\nmtdparts = %s\n\n", p); |
68d7d651 SR |
1533 | |
1534 | /* delete all devices and partitions */ | |
1535 | if (mtd_devices_init() != 0) { | |
1536 | printf("could not initialise device list\n"); | |
1537 | return err; | |
1538 | } | |
1539 | ||
1540 | /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */ | |
06a040a3 | 1541 | if (gd->flags & GD_FLG_ENV_READY) |
a7eb1d66 | 1542 | p = getenv("mtdparts"); |
06a040a3 LM |
1543 | else { |
1544 | if (getenv_f("mtdparts", tmp_parts, MTDPARTS_MAXLEN) != -1) | |
1545 | p = tmp_parts; | |
1546 | else | |
1547 | p = NULL; | |
a7eb1d66 | 1548 | } |
68d7d651 | 1549 | |
06a040a3 LM |
1550 | if (!p) |
1551 | p = mtdparts; | |
1552 | ||
68d7d651 SR |
1553 | if (strncmp(p, "mtdparts=", 9) != 0) { |
1554 | printf("mtdparts variable doesn't start with 'mtdparts='\n"); | |
1555 | return err; | |
1556 | } | |
1557 | p += 9; | |
1558 | ||
06a040a3 | 1559 | while (*p != '\0') { |
68d7d651 SR |
1560 | err = 1; |
1561 | if ((device_parse(p, &p, &dev) != 0) || (!dev)) | |
1562 | break; | |
1563 | ||
e1d2950d | 1564 | debug("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type), |
68d7d651 SR |
1565 | dev->id->num, dev->id->mtd_id); |
1566 | ||
1567 | /* check if parsed device is already on the list */ | |
1568 | if (device_find(dev->id->type, dev->id->num) != NULL) { | |
1569 | printf("device %s%d redefined, please correct mtdparts variable\n", | |
1570 | MTD_DEV_TYPE(dev->id->type), dev->id->num); | |
1571 | break; | |
1572 | } | |
1573 | ||
1574 | list_add_tail(&dev->link, &devices); | |
1575 | err = 0; | |
1576 | } | |
06a040a3 | 1577 | if (err == 1) |
68d7d651 | 1578 | device_delall(&devices); |
68d7d651 | 1579 | |
06a040a3 | 1580 | return err; |
68d7d651 SR |
1581 | } |
1582 | ||
1583 | /** | |
1584 | * Parse provided string describing mtdids mapping (see file header for mtdids | |
1585 | * variable format). Allocate memory for each entry and add all found entries | |
1586 | * to the global mtdids list. | |
1587 | * | |
1588 | * @param ids mapping string | |
1589 | * @return 0 on success, 1 otherwise | |
1590 | */ | |
1591 | static int parse_mtdids(const char *const ids) | |
1592 | { | |
1593 | const char *p = ids; | |
1594 | const char *mtd_id; | |
1595 | int mtd_id_len; | |
1596 | struct mtdids *id; | |
1597 | struct list_head *entry, *n; | |
1598 | struct mtdids *id_tmp; | |
1599 | u8 type, num; | |
39ac3447 | 1600 | u64 size; |
68d7d651 SR |
1601 | int ret = 1; |
1602 | ||
e1d2950d | 1603 | debug("\n---parse_mtdids---\nmtdids = %s\n\n", ids); |
68d7d651 SR |
1604 | |
1605 | /* clean global mtdids list */ | |
1606 | list_for_each_safe(entry, n, &mtdids) { | |
1607 | id_tmp = list_entry(entry, struct mtdids, link); | |
e1d2950d | 1608 | debug("mtdids del: %d %d\n", id_tmp->type, id_tmp->num); |
68d7d651 SR |
1609 | list_del(entry); |
1610 | free(id_tmp); | |
1611 | } | |
1612 | last_ids[0] = '\0'; | |
1613 | INIT_LIST_HEAD(&mtdids); | |
1614 | ||
1615 | while(p && (*p != '\0')) { | |
1616 | ||
1617 | ret = 1; | |
1618 | /* parse 'nor'|'nand'|'onenand'<dev-num> */ | |
1619 | if (mtd_id_parse(p, &p, &type, &num) != 0) | |
1620 | break; | |
1621 | ||
1622 | if (*p != '=') { | |
1623 | printf("mtdids: incorrect <dev-num>\n"); | |
1624 | break; | |
1625 | } | |
1626 | p++; | |
1627 | ||
1628 | /* check if requested device exists */ | |
1629 | if (mtd_device_validate(type, num, &size) != 0) | |
1630 | return 1; | |
1631 | ||
1632 | /* locate <mtd-id> */ | |
1633 | mtd_id = p; | |
1634 | if ((p = strchr(mtd_id, ',')) != NULL) { | |
1635 | mtd_id_len = p - mtd_id + 1; | |
1636 | p++; | |
1637 | } else { | |
1638 | mtd_id_len = strlen(mtd_id) + 1; | |
1639 | } | |
1640 | if (mtd_id_len == 0) { | |
1641 | printf("mtdids: no <mtd-id> identifier\n"); | |
1642 | break; | |
1643 | } | |
1644 | ||
1645 | /* check if this id is already on the list */ | |
1646 | int double_entry = 0; | |
1647 | list_for_each(entry, &mtdids) { | |
1648 | id_tmp = list_entry(entry, struct mtdids, link); | |
1649 | if ((id_tmp->type == type) && (id_tmp->num == num)) { | |
1650 | double_entry = 1; | |
1651 | break; | |
1652 | } | |
1653 | } | |
1654 | if (double_entry) { | |
1655 | printf("device id %s%d redefined, please correct mtdids variable\n", | |
1656 | MTD_DEV_TYPE(type), num); | |
1657 | break; | |
1658 | } | |
1659 | ||
1660 | /* allocate mtdids structure */ | |
1661 | if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) { | |
1662 | printf("out of memory\n"); | |
1663 | break; | |
1664 | } | |
1665 | memset(id, 0, sizeof(struct mtdids) + mtd_id_len); | |
1666 | id->num = num; | |
1667 | id->type = type; | |
1668 | id->size = size; | |
1669 | id->mtd_id = (char *)(id + 1); | |
1670 | strncpy(id->mtd_id, mtd_id, mtd_id_len - 1); | |
1671 | id->mtd_id[mtd_id_len - 1] = '\0'; | |
1672 | INIT_LIST_HEAD(&id->link); | |
1673 | ||
39ac3447 | 1674 | debug("+ id %s%d\t%16lld bytes\t%s\n", |
68d7d651 SR |
1675 | MTD_DEV_TYPE(id->type), id->num, |
1676 | id->size, id->mtd_id); | |
1677 | ||
1678 | list_add_tail(&id->link, &mtdids); | |
1679 | ret = 0; | |
1680 | } | |
1681 | if (ret == 1) { | |
1682 | /* clean mtdids list and free allocated memory */ | |
1683 | list_for_each_safe(entry, n, &mtdids) { | |
1684 | id_tmp = list_entry(entry, struct mtdids, link); | |
1685 | list_del(entry); | |
1686 | free(id_tmp); | |
1687 | } | |
1688 | return 1; | |
1689 | } | |
1690 | ||
1691 | return 0; | |
1692 | } | |
1693 | ||
1694 | /** | |
1695 | * Parse and initialize global mtdids mapping and create global | |
1696 | * device/partition list. | |
1697 | * | |
1698 | * @return 0 on success, 1 otherwise | |
1699 | */ | |
1700 | int mtdparts_init(void) | |
1701 | { | |
1702 | static int initialized = 0; | |
1703 | const char *ids, *parts; | |
1704 | const char *current_partition; | |
1705 | int ids_changed; | |
1706 | char tmp_ep[PARTITION_MAXLEN]; | |
a7eb1d66 | 1707 | char tmp_parts[MTDPARTS_MAXLEN]; |
68d7d651 | 1708 | |
e1d2950d | 1709 | debug("\n---mtdparts_init---\n"); |
68d7d651 SR |
1710 | if (!initialized) { |
1711 | INIT_LIST_HEAD(&mtdids); | |
1712 | INIT_LIST_HEAD(&devices); | |
1713 | memset(last_ids, 0, MTDIDS_MAXLEN); | |
1714 | memset(last_parts, 0, MTDPARTS_MAXLEN); | |
1715 | memset(last_partition, 0, PARTITION_MAXLEN); | |
1716 | initialized = 1; | |
1717 | } | |
1718 | ||
1719 | /* get variables */ | |
1720 | ids = getenv("mtdids"); | |
a7eb1d66 JH |
1721 | /* |
1722 | * The mtdparts variable tends to be long. If we need to access it | |
1723 | * before the env is relocated, then we need to use our own stack | |
1724 | * buffer. gd->env_buf will be too small. | |
1725 | */ | |
c0ac3339 | 1726 | if (gd->flags & GD_FLG_ENV_READY) |
a7eb1d66 | 1727 | parts = getenv("mtdparts"); |
c0ac3339 LM |
1728 | else { |
1729 | if (getenv_f("mtdparts", tmp_parts, MTDPARTS_MAXLEN) != -1) | |
1730 | parts = tmp_parts; | |
1731 | else | |
1732 | parts = NULL; | |
a7eb1d66 | 1733 | } |
68d7d651 SR |
1734 | current_partition = getenv("partition"); |
1735 | ||
1736 | /* save it for later parsing, cannot rely on current partition pointer | |
1737 | * as 'partition' variable may be updated during init */ | |
1738 | tmp_ep[0] = '\0'; | |
1739 | if (current_partition) | |
1740 | strncpy(tmp_ep, current_partition, PARTITION_MAXLEN); | |
1741 | ||
e1d2950d WD |
1742 | debug("last_ids : %s\n", last_ids); |
1743 | debug("env_ids : %s\n", ids); | |
1744 | debug("last_parts: %s\n", last_parts); | |
1745 | debug("env_parts : %s\n\n", parts); | |
68d7d651 | 1746 | |
e1d2950d WD |
1747 | debug("last_partition : %s\n", last_partition); |
1748 | debug("env_partition : %s\n", current_partition); | |
68d7d651 | 1749 | |
1cc0a9f4 | 1750 | /* if mtdids variable is empty try to use defaults */ |
68d7d651 SR |
1751 | if (!ids) { |
1752 | if (mtdids_default) { | |
e1d2950d | 1753 | debug("mtdids variable not defined, using default\n"); |
68d7d651 SR |
1754 | ids = mtdids_default; |
1755 | setenv("mtdids", (char *)ids); | |
1756 | } else { | |
1757 | printf("mtdids not defined, no default present\n"); | |
1758 | return 1; | |
1759 | } | |
1760 | } | |
1761 | if (strlen(ids) > MTDIDS_MAXLEN - 1) { | |
1762 | printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN); | |
1763 | return 1; | |
1764 | } | |
1765 | ||
1766 | /* do no try to use defaults when mtdparts variable is not defined, | |
1767 | * just check the length */ | |
1768 | if (!parts) | |
1769 | printf("mtdparts variable not set, see 'help mtdparts'\n"); | |
1770 | ||
1771 | if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) { | |
1772 | printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN); | |
1773 | return 1; | |
1774 | } | |
1775 | ||
1776 | /* check if we have already parsed those mtdids */ | |
1777 | if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) { | |
1778 | ids_changed = 0; | |
1779 | } else { | |
1780 | ids_changed = 1; | |
1781 | ||
1782 | if (parse_mtdids(ids) != 0) { | |
1783 | mtd_devices_init(); | |
1784 | return 1; | |
1785 | } | |
1786 | ||
1787 | /* ok it's good, save new ids */ | |
1788 | strncpy(last_ids, ids, MTDIDS_MAXLEN); | |
1789 | } | |
1790 | ||
1791 | /* parse partitions if either mtdparts or mtdids were updated */ | |
1792 | if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) { | |
1793 | if (parse_mtdparts(parts) != 0) | |
1794 | return 1; | |
1795 | ||
1796 | if (list_empty(&devices)) { | |
1797 | printf("mtdparts_init: no valid partitions\n"); | |
1798 | return 1; | |
1799 | } | |
1800 | ||
1801 | /* ok it's good, save new parts */ | |
1802 | strncpy(last_parts, parts, MTDPARTS_MAXLEN); | |
1803 | ||
1804 | /* reset first partition from first dev from the list as current */ | |
76b5883d SR |
1805 | current_mtd_dev = list_entry(devices.next, struct mtd_device, link); |
1806 | current_mtd_partnum = 0; | |
68d7d651 SR |
1807 | current_save(); |
1808 | ||
e1d2950d | 1809 | debug("mtdparts_init: current_mtd_dev = %s%d, current_mtd_partnum = %d\n", |
76b5883d SR |
1810 | MTD_DEV_TYPE(current_mtd_dev->id->type), |
1811 | current_mtd_dev->id->num, current_mtd_partnum); | |
68d7d651 SR |
1812 | } |
1813 | ||
1814 | /* mtdparts variable was reset to NULL, delete all devices/partitions */ | |
1815 | if (!parts && (last_parts[0] != '\0')) | |
1816 | return mtd_devices_init(); | |
1817 | ||
1818 | /* do not process current partition if mtdparts variable is null */ | |
1819 | if (!parts) | |
1820 | return 0; | |
1821 | ||
1822 | /* is current partition set in environment? if so, use it */ | |
1823 | if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) { | |
1824 | struct part_info *p; | |
1825 | struct mtd_device *cdev; | |
1826 | u8 pnum; | |
1827 | ||
e1d2950d | 1828 | debug("--- getting current partition: %s\n", tmp_ep); |
68d7d651 SR |
1829 | |
1830 | if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) { | |
76b5883d SR |
1831 | current_mtd_dev = cdev; |
1832 | current_mtd_partnum = pnum; | |
68d7d651 SR |
1833 | current_save(); |
1834 | } | |
1835 | } else if (getenv("partition") == NULL) { | |
e1d2950d | 1836 | debug("no partition variable set, setting...\n"); |
68d7d651 SR |
1837 | current_save(); |
1838 | } | |
1839 | ||
1840 | return 0; | |
1841 | } | |
1842 | ||
1843 | /** | |
1844 | * Return pointer to the partition of a requested number from a requested | |
1845 | * device. | |
1846 | * | |
1847 | * @param dev device that is to be searched for a partition | |
1848 | * @param part_num requested partition number | |
1849 | * @return pointer to the part_info, NULL otherwise | |
1850 | */ | |
1851 | static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num) | |
1852 | { | |
1853 | struct list_head *entry; | |
1854 | struct part_info *part; | |
1855 | int num; | |
1856 | ||
1857 | if (!dev) | |
1858 | return NULL; | |
1859 | ||
e1d2950d | 1860 | debug("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n", |
68d7d651 SR |
1861 | part_num, MTD_DEV_TYPE(dev->id->type), |
1862 | dev->id->num, dev->id->mtd_id); | |
1863 | ||
1864 | if (part_num >= dev->num_parts) { | |
1865 | printf("invalid partition number %d for device %s%d (%s)\n", | |
1866 | part_num, MTD_DEV_TYPE(dev->id->type), | |
1867 | dev->id->num, dev->id->mtd_id); | |
1868 | return NULL; | |
1869 | } | |
1870 | ||
1871 | /* locate partition number, return it */ | |
1872 | num = 0; | |
1873 | list_for_each(entry, &dev->parts) { | |
1874 | part = list_entry(entry, struct part_info, link); | |
1875 | ||
1876 | if (part_num == num++) { | |
1877 | return part; | |
1878 | } | |
1879 | } | |
1880 | ||
1881 | return NULL; | |
1882 | } | |
1883 | ||
1884 | /***************************************************/ | |
a187559e | 1885 | /* U-Boot commands */ |
68d7d651 SR |
1886 | /***************************************************/ |
1887 | /* command line only */ | |
1888 | /** | |
1889 | * Routine implementing u-boot chpart command. Sets new current partition based | |
1890 | * on the user supplied partition id. For partition id format see find_dev_and_part(). | |
1891 | * | |
1892 | * @param cmdtp command internal data | |
1893 | * @param flag command flag | |
1894 | * @param argc number of arguments supplied to the command | |
1895 | * @param argv arguments list | |
1896 | * @return 0 on success, 1 otherwise | |
1897 | */ | |
088f1b19 | 1898 | static int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
68d7d651 SR |
1899 | { |
1900 | /* command line only */ | |
1901 | struct mtd_device *dev; | |
1902 | struct part_info *part; | |
1903 | u8 pnum; | |
1904 | ||
1905 | if (mtdparts_init() !=0) | |
1906 | return 1; | |
1907 | ||
1908 | if (argc < 2) { | |
1909 | printf("no partition id specified\n"); | |
1910 | return 1; | |
1911 | } | |
1912 | ||
1913 | if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0) | |
1914 | return 1; | |
1915 | ||
76b5883d SR |
1916 | current_mtd_dev = dev; |
1917 | current_mtd_partnum = pnum; | |
68d7d651 SR |
1918 | current_save(); |
1919 | ||
1920 | printf("partition changed to %s%d,%d\n", | |
1921 | MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum); | |
1922 | ||
1923 | return 0; | |
1924 | } | |
1925 | ||
1926 | /** | |
1927 | * Routine implementing u-boot mtdparts command. Initialize/update default global | |
1928 | * partition list and process user partition request (list, add, del). | |
1929 | * | |
1930 | * @param cmdtp command internal data | |
1931 | * @param flag command flag | |
1932 | * @param argc number of arguments supplied to the command | |
1933 | * @param argv arguments list | |
1934 | * @return 0 on success, 1 otherwise | |
1935 | */ | |
088f1b19 KP |
1936 | static int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, |
1937 | char * const argv[]) | |
68d7d651 SR |
1938 | { |
1939 | if (argc == 2) { | |
1940 | if (strcmp(argv[1], "default") == 0) { | |
1941 | setenv("mtdids", (char *)mtdids_default); | |
1942 | setenv("mtdparts", (char *)mtdparts_default); | |
1943 | setenv("partition", NULL); | |
1944 | ||
1945 | mtdparts_init(); | |
1946 | return 0; | |
1947 | } else if (strcmp(argv[1], "delall") == 0) { | |
1948 | /* this may be the first run, initialize lists if needed */ | |
1949 | mtdparts_init(); | |
1950 | ||
1951 | setenv("mtdparts", NULL); | |
1952 | ||
1953 | /* mtd_devices_init() calls current_save() */ | |
1954 | return mtd_devices_init(); | |
1955 | } | |
1956 | } | |
1957 | ||
1958 | /* make sure we are in sync with env variables */ | |
1959 | if (mtdparts_init() != 0) | |
1960 | return 1; | |
1961 | ||
1962 | if (argc == 1) { | |
1963 | list_partitions(); | |
1964 | return 0; | |
1965 | } | |
1966 | ||
1967 | /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */ | |
59a50d2d | 1968 | if (((argc == 5) || (argc == 6)) && (strncmp(argv[1], "add", 3) == 0)) { |
68d7d651 SR |
1969 | #define PART_ADD_DESC_MAXLEN 64 |
1970 | char tmpbuf[PART_ADD_DESC_MAXLEN]; | |
59a50d2d BG |
1971 | #if defined(CONFIG_CMD_MTDPARTS_SPREAD) |
1972 | struct mtd_info *mtd; | |
1973 | uint64_t next_offset; | |
1974 | #endif | |
68d7d651 SR |
1975 | u8 type, num, len; |
1976 | struct mtd_device *dev; | |
1977 | struct mtd_device *dev_tmp; | |
1978 | struct mtdids *id; | |
1979 | struct part_info *p; | |
1980 | ||
1981 | if (mtd_id_parse(argv[2], NULL, &type, &num) != 0) | |
1982 | return 1; | |
1983 | ||
1984 | if ((id = id_find(type, num)) == NULL) { | |
1985 | printf("no such device %s defined in mtdids variable\n", argv[2]); | |
1986 | return 1; | |
1987 | } | |
1988 | ||
1989 | len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */ | |
1990 | len += strlen(argv[3]); /* size@offset */ | |
1991 | len += strlen(argv[4]) + 2; /* '(' name ')' */ | |
1992 | if (argv[5] && (strlen(argv[5]) == 2)) | |
1993 | len += 2; /* 'ro' */ | |
1994 | ||
1995 | if (len >= PART_ADD_DESC_MAXLEN) { | |
1996 | printf("too long partition description\n"); | |
1997 | return 1; | |
1998 | } | |
1999 | sprintf(tmpbuf, "%s:%s(%s)%s", | |
2000 | id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : ""); | |
e1d2950d | 2001 | debug("add tmpbuf: %s\n", tmpbuf); |
68d7d651 SR |
2002 | |
2003 | if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev)) | |
2004 | return 1; | |
2005 | ||
e1d2950d | 2006 | debug("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type), |
68d7d651 SR |
2007 | dev->id->num, dev->id->mtd_id); |
2008 | ||
59a50d2d BG |
2009 | p = list_entry(dev->parts.next, struct part_info, link); |
2010 | ||
2011 | #if defined(CONFIG_CMD_MTDPARTS_SPREAD) | |
2012 | if (get_mtd_info(dev->id->type, dev->id->num, &mtd)) | |
2013 | return 1; | |
2014 | ||
2015 | if (!strcmp(&argv[1][3], ".spread")) { | |
2016 | spread_partition(mtd, p, &next_offset); | |
2017 | debug("increased %s to %d bytes\n", p->name, p->size); | |
2018 | } | |
2019 | #endif | |
2020 | ||
2021 | dev_tmp = device_find(dev->id->type, dev->id->num); | |
2022 | if (dev_tmp == NULL) { | |
68d7d651 | 2023 | device_add(dev); |
59a50d2d | 2024 | } else if (part_add(dev_tmp, p) != 0) { |
68d7d651 | 2025 | /* merge new partition with existing ones*/ |
59a50d2d BG |
2026 | device_del(dev); |
2027 | return 1; | |
68d7d651 SR |
2028 | } |
2029 | ||
2030 | if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) { | |
a22bf16b | 2031 | printf("generated mtdparts too long, resetting to null\n"); |
68d7d651 SR |
2032 | return 1; |
2033 | } | |
2034 | ||
2035 | return 0; | |
2036 | } | |
2037 | ||
2038 | /* mtdparts del part-id */ | |
2039 | if ((argc == 3) && (strcmp(argv[1], "del") == 0)) { | |
e1d2950d | 2040 | debug("del: part-id = %s\n", argv[2]); |
68d7d651 SR |
2041 | |
2042 | return delete_partition(argv[2]); | |
2043 | } | |
2044 | ||
ca75b20e BG |
2045 | #if defined(CONFIG_CMD_MTDPARTS_SPREAD) |
2046 | if ((argc == 2) && (strcmp(argv[1], "spread") == 0)) | |
2047 | return spread_partitions(); | |
2048 | #endif /* CONFIG_CMD_MTDPARTS_SPREAD */ | |
2049 | ||
4c12eeb8 | 2050 | return CMD_RET_USAGE; |
68d7d651 SR |
2051 | } |
2052 | ||
2053 | /***************************************************/ | |
2054 | U_BOOT_CMD( | |
2055 | chpart, 2, 0, do_chpart, | |
2056 | "change active partition", | |
2057 | "part-id\n" | |
a89c33db | 2058 | " - change active partition (e.g. part-id = nand0,1)" |
68d7d651 SR |
2059 | ); |
2060 | ||
088f1b19 KP |
2061 | #ifdef CONFIG_SYS_LONGHELP |
2062 | static char mtdparts_help_text[] = | |
68d7d651 SR |
2063 | "\n" |
2064 | " - list partition table\n" | |
2065 | "mtdparts delall\n" | |
2066 | " - delete all partitions\n" | |
2067 | "mtdparts del part-id\n" | |
2068 | " - delete partition (e.g. part-id = nand0,1)\n" | |
2069 | "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n" | |
2070 | " - add partition\n" | |
59a50d2d BG |
2071 | #if defined(CONFIG_CMD_MTDPARTS_SPREAD) |
2072 | "mtdparts add.spread <mtd-dev> <size>[@<offset>] [<name>] [ro]\n" | |
2073 | " - add partition, padding size by skipping bad blocks\n" | |
2074 | #endif | |
68d7d651 | 2075 | "mtdparts default\n" |
ca75b20e BG |
2076 | " - reset partition table to defaults\n" |
2077 | #if defined(CONFIG_CMD_MTDPARTS_SPREAD) | |
2078 | "mtdparts spread\n" | |
2079 | " - adjust the sizes of the partitions so they are\n" | |
2080 | " at least as big as the mtdparts variable specifies\n" | |
2081 | " and they each start on a good block\n\n" | |
2082 | #else | |
2083 | "\n" | |
2084 | #endif /* CONFIG_CMD_MTDPARTS_SPREAD */ | |
68d7d651 SR |
2085 | "-----\n\n" |
2086 | "this command uses three environment variables:\n\n" | |
2087 | "'partition' - keeps current partition identifier\n\n" | |
2088 | "partition := <part-id>\n" | |
2089 | "<part-id> := <dev-id>,part_num\n\n" | |
2090 | "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n" | |
2091 | "mtdids=<idmap>[,<idmap>,...]\n\n" | |
2092 | "<idmap> := <dev-id>=<mtd-id>\n" | |
2093 | "<dev-id> := 'nand'|'nor'|'onenand'<dev-num>\n" | |
2094 | "<dev-num> := mtd device number, 0...\n" | |
2095 | "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n" | |
2096 | "'mtdparts' - partition list\n\n" | |
2097 | "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n" | |
2098 | "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n" | |
2099 | "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n" | |
2100 | "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n" | |
2101 | "<size> := standard linux memsize OR '-' to denote all remaining space\n" | |
2102 | "<offset> := partition start offset within the device\n" | |
2103 | "<name> := '(' NAME ')'\n" | |
088f1b19 KP |
2104 | "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)"; |
2105 | #endif | |
2106 | ||
2107 | U_BOOT_CMD( | |
2108 | mtdparts, 6, 0, do_mtdparts, | |
2109 | "define flash/nand partitions", mtdparts_help_text | |
68d7d651 SR |
2110 | ); |
2111 | /***************************************************/ |