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