]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_fat.c
Merge branch 'master' of git://git.denx.de/u-boot-net
[karo-tx-uboot.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 offset;
40         unsigned long count = 0;
41         unsigned long pos = 0;
42         char buf [12];
43         block_dev_desc_t *dev_desc=NULL;
44         disk_partition_t info;
45         int part, dev;
46
47         if (argc < 5) {
48                 printf("usage: fatload <interface> [<dev[:part]>] "
49                         "<addr> <filename> [bytes [pos]]\n");
50                 return 1;
51         }
52
53         part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
54         if (part < 0)
55                 return 1;
56
57         dev = dev_desc->dev;
58         if (fat_register_device(dev_desc,part)!=0) {
59                 printf("\n** Unable to use %s %d:%d for fatload **\n",
60                         argv[1], dev, part);
61                 return 1;
62         }
63         offset = simple_strtoul(argv[3], NULL, 16);
64         if (argc >= 6)
65                 count = simple_strtoul(argv[5], NULL, 16);
66         if (argc >= 7)
67                 pos = simple_strtoul(argv[6], NULL, 16);
68         size = file_fat_read_at(argv[4], pos, (unsigned char *)offset, count);
69
70         if(size==-1) {
71                 printf("\n** Unable to read \"%s\" from %s %d:%d **\n",
72                         argv[4], argv[1], dev, part);
73                 return 1;
74         }
75
76         printf("\n%ld bytes read\n", size);
77
78         sprintf(buf, "%lX", size);
79         setenv("filesize", buf);
80
81         return 0;
82 }
83
84
85 U_BOOT_CMD(
86         fatload,        7,      0,      do_fat_fsload,
87         "load binary file from a dos filesystem",
88         "<interface> [<dev[:part]>]  <addr> <filename> [bytes [pos]]\n"
89         "    - Load binary file 'filename' from 'dev' on 'interface'\n"
90         "      to address 'addr' from dos filesystem.\n"
91         "      'pos' gives the file position to start loading from.\n"
92         "      If 'pos' is omitted, 0 is used. 'pos' requires 'bytes'.\n"
93         "      'bytes' gives the size to load. If 'bytes' is 0 or omitted,\n"
94         "      the load stops on end of file."
95 );
96
97 int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
98 {
99         char *filename = "/";
100         int ret, dev, part;
101         block_dev_desc_t *dev_desc=NULL;
102         disk_partition_t info;
103
104         if (argc < 2) {
105                 printf("usage: fatls <interface> [<dev[:part]>] [directory]\n");
106                 return 0;
107         }
108
109         part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
110         if (part < 0)
111                 return 1;
112
113         dev = dev_desc->dev;
114         if (fat_register_device(dev_desc,part)!=0) {
115                 printf("\n** Unable to use %s %d:%d for fatls **\n",
116                         argv[1], dev, part);
117                 return 1;
118         }
119         if (argc == 4)
120                 ret = file_fat_ls(argv[3]);
121         else
122                 ret = file_fat_ls(filename);
123
124         if(ret!=0)
125                 printf("No Fat FS detected\n");
126         return ret;
127 }
128
129 U_BOOT_CMD(
130         fatls,  4,      1,      do_fat_ls,
131         "list files in a directory (default /)",
132         "<interface> [<dev[:part]>] [directory]\n"
133         "    - list files from 'dev' on 'interface' in a 'directory'"
134 );
135
136 int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
137 {
138         int dev, part;
139         block_dev_desc_t *dev_desc;
140         disk_partition_t info;
141
142         if (argc < 2) {
143                 printf("usage: fatinfo <interface> [<dev[:part]>]\n");
144                 return 0;
145         }
146
147         part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
148         if (part < 0)
149                 return 1;
150
151         dev = dev_desc->dev;
152         if (fat_register_device(dev_desc,part)!=0) {
153                 printf("\n** Unable to use %s %d:%d for fatinfo **\n",
154                         argv[1], dev, part);
155                 return 1;
156         }
157         return file_fat_detectfs();
158 }
159
160 U_BOOT_CMD(
161         fatinfo,        3,      1,      do_fat_fsinfo,
162         "print information about filesystem",
163         "<interface> [<dev[:part]>]\n"
164         "    - print information about filesystem from 'dev' on 'interface'"
165 );
166
167 #ifdef CONFIG_FAT_WRITE
168 static int do_fat_fswrite(cmd_tbl_t *cmdtp, int flag,
169                 int argc, char * const argv[])
170 {
171         long size;
172         unsigned long addr;
173         unsigned long count;
174         block_dev_desc_t *dev_desc = NULL;
175         disk_partition_t info;
176         int dev = 0;
177         int part = 1;
178         char *ep;
179
180         if (argc < 5)
181                 return cmd_usage(cmdtp);
182
183         part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
184         if (part < 0)
185                 return 1;
186
187         dev = dev_desc->dev;
188
189         if (fat_register_device(dev_desc, part) != 0) {
190                 printf("\n** Unable to use %s %d:%d for fatwrite **\n",
191                         argv[1], dev, part);
192                 return 1;
193         }
194         addr = simple_strtoul(argv[3], NULL, 16);
195         count = simple_strtoul(argv[5], NULL, 16);
196
197         size = file_fat_write(argv[4], (void *)addr, count);
198         if (size == -1) {
199                 printf("\n** Unable to write \"%s\" from %s %d:%d **\n",
200                         argv[4], argv[1], dev, part);
201                 return 1;
202         }
203
204         printf("%ld bytes written\n", size);
205
206         return 0;
207 }
208
209 U_BOOT_CMD(
210         fatwrite,       6,      0,      do_fat_fswrite,
211         "write file into a dos filesystem",
212         "<interface> <dev[:part]> <addr> <filename> <bytes>\n"
213         "    - write file 'filename' from the address 'addr' in RAM\n"
214         "      to 'dev' on 'interface'"
215 );
216 #endif