tizen 2.3.1 release
[platform/kernel/u-boot.git] / common / cmd_fat.c
1 /*
2  * (C) Copyright 2002
3  * Richard Jones, rjones@nexus-tech.net
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 /*
25  * Boot support
26  */
27 #include <common.h>
28 #include <command.h>
29 #include <s_record.h>
30 #include <net.h>
31 #include <ata.h>
32 #include <part.h>
33 #include <fat.h>
34
35
36 int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
37 {
38         long size;
39         unsigned long addr;
40         unsigned long offset;
41         unsigned long count;
42         char buf [12];
43         block_dev_desc_t *dev_desc=NULL;
44         int dev=0;
45         int part=1;
46         char *ep;
47
48         if (argc < 5) {
49                 printf( "usage: fatload <interface> <dev[:part]> "
50                         "<addr> <filename> [offset] [bytes]\n");
51                 return 1;
52         }
53
54         dev = (int)simple_strtoul(argv[2], &ep, 16);
55         dev_desc = get_dev(argv[1],dev);
56         if (dev_desc == NULL) {
57                 puts("\n** Invalid boot device **\n");
58                 return 1;
59         }
60         if (*ep) {
61                 if (*ep != ':') {
62                         puts("\n** Invalid boot device, use `dev[:part]' **\n");
63                         return 1;
64                 }
65                 part = (int)simple_strtoul(++ep, NULL, 16);
66         }
67         if (fat_register_device(dev_desc,part)!=0) {
68                 printf("\n** Unable to use %s %d:%d for fatload **\n",
69                         argv[1], dev, part);
70                 return 1;
71         }
72         addr = simple_strtoul(argv[3], NULL, 16);
73         if (argc == 7) {
74                 offset = simple_strtoul(argv[5], NULL, 16);
75                 count = simple_strtoul(argv[6], NULL, 16);
76         } else {
77                 offset = 0;
78                 count = 0;
79         }
80         size = file_fat_read(argv[4], (unsigned char *)addr, offset, count);
81
82         if(size==-1) {
83                 printf("\n** Unable to read \"%s\" from %s %d:%d **\n",
84                         argv[4], argv[1], dev, part);
85                 return 1;
86         }
87
88         printf("%ld bytes read\n", size);
89
90         sprintf(buf, "%lX", size);
91         setenv("filesize", buf);
92
93         return 0;
94 }
95
96
97 U_BOOT_CMD(
98         fatload,        7,      0,      do_fat_fsload,
99         "load binary file from a dos filesystem",
100         "<interface> <dev[:part]>  <addr> <filename> [offset] [bytes]\n"
101         "    - load binary file 'filename' from 'dev' on 'interface'\n"
102         "      to address 'addr' from dos filesystem"
103 );
104
105 int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
106 {
107         long size;
108         char buf [12];
109         char *filename = "/";
110         int dev=0;
111         int part=1;
112         char *ep;
113         block_dev_desc_t *dev_desc=NULL;
114
115         if (argc < 3) {
116                 printf("usage: fatls <interface> <dev[:part]> [directory]\n");
117                 return 0;
118         }
119         dev = (int)simple_strtoul(argv[2], &ep, 16);
120         dev_desc = get_dev(argv[1],dev);
121         if (dev_desc == NULL) {
122                 puts("\n** Invalid boot device **\n");
123                 return 1;
124         }
125         if (*ep) {
126                 if (*ep != ':') {
127                         puts("\n** Invalid boot device, use `dev[:part]' **\n");
128                         return 1;
129                 }
130                 part = (int)simple_strtoul(++ep, NULL, 16);
131         }
132         if (fat_register_device(dev_desc,part)!=0) {
133                 printf("\n** Unable to use %s %d:%d for fatls **\n",
134                         argv[1], dev, part);
135                 return 1;
136         }
137         if (argc == 4)
138                 size = file_fat_ls(argv[3]);
139         else
140                 size = file_fat_ls(filename);
141
142         if(size < 0) {
143                 printf("No Fat FS detected\n");
144                 return 1;
145         }
146
147         sprintf(buf, "%lX", size);
148         setenv("filesize", buf);
149
150         return 0;
151 }
152
153 U_BOOT_CMD(
154         fatls,  4,      1,      do_fat_ls,
155         "list files in a directory (default /)",
156         "<interface> <dev[:part]> [directory]\n"
157         "    - list files from 'dev' on 'interface' in a 'directory'"
158 );
159
160 int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
161 {
162         int dev=0;
163         int part=1;
164         char *ep;
165         block_dev_desc_t *dev_desc=NULL;
166
167         if (argc < 2) {
168                 printf("usage: fatinfo <interface> <dev[:part]>\n");
169                 return 0;
170         }
171         dev = (int)simple_strtoul(argv[2], &ep, 16);
172         dev_desc = get_dev(argv[1],dev);
173         if (dev_desc == NULL) {
174                 puts("\n** Invalid boot device **\n");
175                 return 1;
176         }
177         if (*ep) {
178                 if (*ep != ':') {
179                         puts("\n** Invalid boot device, use `dev[:part]' **\n");
180                         return 1;
181                 }
182                 part = (int)simple_strtoul(++ep, NULL, 16);
183         }
184         if (fat_register_device(dev_desc,part)!=0) {
185                 printf("\n** Unable to use %s %d:%d for fatinfo **\n",
186                         argv[1], dev, part);
187                 return 1;
188         }
189         return file_fat_detectfs();
190 }
191
192 U_BOOT_CMD(
193         fatinfo,        3,      1,      do_fat_fsinfo,
194         "print information about filesystem",
195         "<interface> <dev[:part]>\n"
196         "    - print information about filesystem from 'dev' on 'interface'"
197 );