]> git.sur5r.net Git - u-boot/blob - common/cmd_jffs2.c
* Patch by Robert Schwebel, 15 Dec 2003:
[u-boot] / common / cmd_jffs2.c
1 /*
2  * (C) Copyright 2002
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  * (C) Copyright 2002
5  * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
6  * (C) Copyright 2003
7  * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
8  *
9  * See file CREDITS for list of people who contributed to this
10  * project.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25  * MA 02111-1307 USA
26  */
27
28 /*
29  * Boot support
30  */
31 #include <common.h>
32 #include <command.h>
33 #include <s_record.h>
34 #include <jffs2/load_kernel.h>
35 #include <net.h>
36
37 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
38
39 #include <cramfs/cramfs_fs.h>
40
41 static int part_num=0;
42
43 #ifndef CFG_JFFS_CUSTOM_PART
44
45 static struct part_info part;
46
47 struct part_info*
48 jffs2_part_info(int part_num)
49 {
50         extern flash_info_t flash_info[];       /* info for FLASH chips */
51         int i;
52
53         if(part_num==0){
54
55                 if(part.usr_priv==(void*)1)
56                         return &part;
57
58                 memset(&part, 0, sizeof(part));
59
60 #if defined(CFG_JFFS2_FIRST_SECTOR)
61                 part.offset = (unsigned char *) flash_info[CFG_JFFS2_FIRST_BANK].start[CFG_JFFS2_FIRST_SECTOR];
62 #else
63                 part.offset = (unsigned char *) flash_info[CFG_JFFS2_FIRST_BANK].start[0];
64 #endif
65
66                 /* Figure out flash partition size */
67                 for (i = CFG_JFFS2_FIRST_BANK; i < CFG_JFFS2_NUM_BANKS+CFG_JFFS2_FIRST_BANK; i++)
68                         part.size += flash_info[i].size;
69
70 #if defined(CFG_JFFS2_FIRST_SECTOR) && (CFG_JFFS2_FIRST_SECTOR > 0)
71                 part.size -=
72                         flash_info[CFG_JFFS2_FIRST_BANK].start[CFG_JFFS2_FIRST_SECTOR] -
73                         flash_info[CFG_JFFS2_FIRST_BANK].start[0];
74 #endif
75
76                 /* unused in current jffs2 loader */
77                 part.erasesize = 0;
78
79                 /* Mark the struct as ready */
80                 part.usr_priv=(void*)1;
81
82                 return &part;
83         }
84         return 0;
85 }
86 #endif /* ifndef CFG_JFFS_CUSTOM_PART */
87
88 int
89 do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
90 {
91     struct part_info* jffs2_part_info(int);
92     int jffs2_1pass_load(char *, struct part_info *,const char *);
93     char *fsname;
94
95         char *filename = "uImage";
96         ulong offset = CFG_LOAD_ADDR;
97         int size;
98         struct part_info *part;
99
100         if (argc == 2) {
101                 filename = argv[1];
102         }
103         if (argc == 3) {
104                 offset = simple_strtoul(argv[1], NULL, 16);
105                 filename = argv[2];
106         }
107
108         if (0 != (part=jffs2_part_info(part_num))){
109
110                 /* check partition type for cramfs */
111                 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
112                 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
113
114                 if (cramfs_check(part)) {
115                         size = cramfs_load ((char *) offset, part, filename);
116                 } else {
117                         /* if this is not cramfs assume jffs2 */
118                         size = jffs2_1pass_load((char *)offset, part, filename);
119                 }
120
121                 if (size > 0) {
122                         char buf[10];
123                         printf("### %s load complete: %d bytes loaded to 0x%lx\n",
124                                 fsname, size, offset);
125                         sprintf(buf, "%x", size);
126                         setenv("filesize", buf);
127                 } else {
128                         printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
129                 }
130
131                 return !(size > 0);
132         }
133         printf("Active partition not valid\n");
134         return 0;
135 }
136
137 int
138 do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
139 {
140    struct part_info* jffs2_part_info(int);
141    int jffs2_1pass_ls(struct part_info *,char *);
142
143    char *filename = "/";
144         int ret;
145         struct part_info *part;
146
147         if (argc == 2)
148                 filename = argv[1];
149
150         if (0 != (part=jffs2_part_info(part_num))){
151
152                 /* check partition type for cramfs */
153                 if (cramfs_check(part)) {
154                         ret = cramfs_ls (part, filename);
155                 } else {
156                         /* if this is not cramfs assume jffs2 */
157                         ret = jffs2_1pass_ls(part, filename);
158                 }
159
160                 return (ret == 1);
161         }
162         printf("Active partition not valid\n");
163         return 0;
164 }
165
166 int
167 do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
168 {
169         struct part_info* jffs2_part_info(int);
170         int jffs2_1pass_info(struct part_info *);
171         struct part_info *part;
172         char *fsname;
173         int ret;
174
175         if ((part=jffs2_part_info(part_num))){
176
177                 /* check partition type for cramfs */
178                 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
179                 printf("### filesystem type is %s\n", fsname);
180
181                 if (cramfs_check(part)) {
182                         ret = cramfs_info (part);
183                 } else {
184                         /* if this is not cramfs assume jffs2 */
185                         ret = jffs2_1pass_info(part);
186                 }
187
188                 return (ret == 1);
189         }
190         printf("Active partition not valid\n");
191         return 0;
192 }
193
194 int
195 do_jffs2_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
196 {
197         int tmp_part;
198         char str_part_num[3];
199    struct part_info* jffs2_part_info(int);
200
201    if (argc >= 2) {
202                 tmp_part = simple_strtoul(argv[1], NULL, 16);
203         }else{
204                 printf("Need partition number in argument list\n");
205                 return 0;
206
207         }
208
209         if (jffs2_part_info(tmp_part)){
210                 printf("Partition changed to %d\n",tmp_part);
211                 part_num=tmp_part;
212                 sprintf(str_part_num, "%d", part_num);
213                 setenv("partition", str_part_num);
214                 return 0;
215         }
216
217         printf("Partition %d is not valid partiton\n",tmp_part);
218         return 0;
219
220 }
221
222 /***************************************************/
223
224 U_BOOT_CMD(
225         fsload, 3,      0,      do_jffs2_fsload,
226         "fsload  - load binary file from a filesystem image\n",
227         "[ off ] [ filename ]\n"
228         "    - load binary file from flash bank\n"
229         "      with offset 'off'\n"
230 );
231
232 U_BOOT_CMD(
233         fsinfo, 1,      1,      do_jffs2_fsinfo,
234         "fsinfo  - print information about filesystems\n",
235         "    - print information about filesystems\n"
236 );
237
238 U_BOOT_CMD(
239         ls,     2,      1,      do_jffs2_ls,
240         "ls      - list files in a directory (default /)\n",
241         "[ directory ]\n"
242         "    - list files in a directory.\n"
243 );
244
245 U_BOOT_CMD(
246         chpart, 2,      0,      do_jffs2_chpart,
247         "chpart  - change active partition\n",
248         "    - change active partition\n"
249 );
250
251 #endif /* CFG_CMD_JFFS2 */