]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_fat.c
* Code cleanup:
[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 <cmd_autoscript.h>
30 #include <s_record.h>
31 #include <net.h>
32 #include <ata.h>
33
34 #if (CONFIG_COMMANDS & CFG_CMD_FAT)
35
36 #undef  DEBUG
37
38 #include <fat.h>
39
40 extern block_dev_desc_t *ide_get_dev (int dev);
41
42 int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
43 {
44         long size;
45         unsigned long offset;
46         unsigned long count;
47
48         if (argc < 3) {
49                 printf ("usage:fatload <filename> <addr> [bytes]\n");
50                 return (0);
51         }
52
53         offset = simple_strtoul (argv[2], NULL, 16);
54         if (argc == 4)
55                 count = simple_strtoul (argv[3], NULL, 16);
56         else
57                 count = 0;
58
59         size = file_fat_read (argv[1], (unsigned char *) offset, count);
60
61         printf ("%ld bytes read\n", size);
62
63         return size;
64 }
65
66 int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
67 {
68         char *filename = "/";
69         int ret;
70
71         if (argc == 2)
72                 ret = file_fat_ls (argv[1]);
73         else
74                 ret = file_fat_ls (filename);
75
76         return (ret);
77 }
78
79 int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
80 {
81         int ret;
82
83         ret = 0;
84
85         printf ("FAT info: %d\n", file_fat_detectfs ());
86
87         return (ret);
88 }
89
90 #ifdef NOT_IMPLEMENTED_YET
91 /* find first device whose first partition is a DOS filesystem */
92 int find_fat_partition (void)
93 {
94         int i, j;
95         block_dev_desc_t *dev_desc;
96         unsigned char *part_table;
97         unsigned char buffer[ATA_BLOCKSIZE];
98
99         for (i = 0; i < CFG_IDE_MAXDEVICE; i++) {
100                 dev_desc = ide_get_dev (i);
101                 if (!dev_desc) {
102                         debug ("couldn't get ide device!\n");
103                         return (-1);
104                 }
105                 if (dev_desc->part_type == PART_TYPE_DOS) {
106                         if (dev_desc->
107                                 block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
108                                 debug ("can't perform block_read!\n");
109                                 return (-1);
110                         }
111                         part_table = &buffer[0x1be];    /* start with partition #4 */
112                         for (j = 0; j < 4; j++) {
113                                 if ((part_table[4] == 1 ||      /* 12-bit FAT */
114                                      part_table[4] == 4 ||      /* 16-bit FAT */
115                                      part_table[4] == 6) &&     /* > 32Meg part */
116                                     part_table[0] == 0x80) {    /* bootable? */
117                                         curr_dev = i;
118                                         part_offset = part_table[11];
119                                         part_offset <<= 8;
120                                         part_offset |= part_table[10];
121                                         part_offset <<= 8;
122                                         part_offset |= part_table[9];
123                                         part_offset <<= 8;
124                                         part_offset |= part_table[8];
125                                         debug ("found partition start at %ld\n", part_offset);
126                                         return (0);
127                                 }
128                                 part_table += 16;
129                         }
130                 }
131         }
132
133         debug ("no valid devices found!\n");
134         return (-1);
135 }
136
137 int
138 do_fat_dump (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
139 {
140         __u8 block[1024];
141         int ret;
142         int bknum;
143
144         ret = 0;
145
146         if (argc != 2) {
147                 printf ("needs an argument!\n");
148                 return (0);
149         }
150
151         bknum = simple_strtoul (argv[1], NULL, 10);
152
153         if (disk_read (0, bknum, block) != 0) {
154                 printf ("Error: reading block\n");
155                 return -1;
156         }
157         printf ("FAT dump: %d\n", bknum);
158         hexdump (512, block);
159
160         return (ret);
161 }
162
163 int disk_read (__u32 startblock, __u32 getsize, __u8 *bufptr)
164 {
165         ulong tot;
166         block_dev_desc_t *dev_desc;
167
168         if (curr_dev < 0) {
169                 if (find_fat_partition () != 0)
170                         return (-1);
171         }
172
173         dev_desc = ide_get_dev (curr_dev);
174         if (!dev_desc) {
175                 debug ("couldn't get ide device\n");
176                 return (-1);
177         }
178
179         tot = dev_desc->block_read (0, startblock + part_offset,
180                                     getsize, (ulong *) bufptr);
181
182         /* should we do this here?
183            flush_cache ((ulong)buf, cnt*ide_dev_desc[device].blksz);
184          */
185
186         if (tot == getsize)
187                 return (0);
188
189         debug ("unable to read from device!\n");
190
191         return (-1);
192 }
193
194
195 static int isprint (unsigned char ch)
196 {
197         if (ch >= 32 && ch < 127)
198                 return (1);
199
200         return (0);
201 }
202
203
204 void hexdump (int cnt, unsigned char *data)
205 {
206         int i;
207         int run;
208         int offset;
209
210         offset = 0;
211         while (cnt) {
212                 printf ("%04X : ", offset);
213                 if (cnt >= 16)
214                         run = 16;
215                 else
216                         run = cnt;
217                 cnt -= run;
218                 for (i = 0; i < run; i++)
219                         printf ("%02X ", (unsigned int) data[i]);
220                 printf (": ");
221                 for (i = 0; i < run; i++)
222                         printf ("%c", isprint (data[i]) ? data[i] : '.');
223                 printf ("\n");
224                 data = &data[16];
225                 offset += run;
226         }
227 }
228 #endif  /* NOT_IMPLEMENTED_YET */
229
230 #endif  /* CFG_CMD_FAT */