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