blob: 2e2d2b8cefb0ee0ad48a2ce7982dc2afaa326548 [file] [log] [blame]
wdenk2262cfe2002-11-18 00:14:45 +00001/*
2 * (C) Copyright 2002
3 * Stäubli Faverges - <www.staubli.com>
4 * Pierre AUBERT p.aubert@staubli.com
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25#include <common.h>
26#include <config.h>
27#include <malloc.h>
28
Jon Loeligerdd60d122007-07-09 17:56:50 -050029#if defined(CONFIG_CMD_FDOS)
wdenk2262cfe2002-11-18 00:14:45 +000030
31#include "dos.h"
32#include "fdos.h"
33
34
35/*-----------------------------------------------------------------------------
wdenk8bde7f72003-06-27 21:31:46 +000036 * fat_decode --
wdenk2262cfe2002-11-18 00:14:45 +000037 *-----------------------------------------------------------------------------
38 */
39unsigned int fat_decode (Fs_t *fs, unsigned int num)
40{
41 unsigned int start = num * 3 / 2;
42 unsigned char *address = fs -> fat_buf + start;
wdenk8bde7f72003-06-27 21:31:46 +000043
wdenk2262cfe2002-11-18 00:14:45 +000044 if (num < 2 || start + 1 > (fs -> fat_len * SZ_STD_SECTOR))
wdenk8bde7f72003-06-27 21:31:46 +000045 return 1;
46
wdenk2262cfe2002-11-18 00:14:45 +000047 if (num & 1)
wdenk8bde7f72003-06-27 21:31:46 +000048 return ((address [1] & 0xff) << 4) | ((address [0] & 0xf0 ) >> 4);
wdenk2262cfe2002-11-18 00:14:45 +000049 else
wdenk8bde7f72003-06-27 21:31:46 +000050 return ((address [1] & 0xf) << 8) | (address [0] & 0xff );
wdenk2262cfe2002-11-18 00:14:45 +000051}
52/*-----------------------------------------------------------------------------
wdenk8bde7f72003-06-27 21:31:46 +000053 * check_fat --
wdenk2262cfe2002-11-18 00:14:45 +000054 *-----------------------------------------------------------------------------
55 */
56static int check_fat (Fs_t *fs)
57{
58 int i, f;
59
60 /* Cluster verification */
61 for (i = 3 ; i < fs -> num_clus; i++){
wdenk8bde7f72003-06-27 21:31:46 +000062 f = fat_decode (fs, i);
63 if (f < FAT12_LAST && f > fs -> num_clus){
64 /* Wrong cluster number detected */
65 return (-1);
66 }
wdenk2262cfe2002-11-18 00:14:45 +000067 }
68 return (0);
69}
70/*-----------------------------------------------------------------------------
wdenk8bde7f72003-06-27 21:31:46 +000071 * read_one_fat --
wdenk2262cfe2002-11-18 00:14:45 +000072 *-----------------------------------------------------------------------------
73 */
74static int read_one_fat (BootSector_t *boot, Fs_t *fs, int nfat)
75{
76 if (dev_read (fs -> fat_buf,
wdenk8bde7f72003-06-27 21:31:46 +000077 (fs -> fat_start + nfat * fs -> fat_len),
78 fs -> fat_len) < 0) {
79 return (-1);
wdenk2262cfe2002-11-18 00:14:45 +000080 }
81
82 if (fs -> fat_buf [0] || fs -> fat_buf [1] || fs -> fat_buf [2]) {
wdenk8bde7f72003-06-27 21:31:46 +000083 if ((fs -> fat_buf [0] != boot -> descr &&
84 (fs -> fat_buf [0] != 0xf9 || boot -> descr != MEDIA_STD)) ||
85 fs -> fat_buf [0] < MEDIA_STD){
86 /* Unknown Media */
87 return (-1);
88 }
89 if (fs -> fat_buf [1] != 0xff || fs -> fat_buf [2] != 0xff){
90 /* FAT doesn't start with good values */
91 return (-1);
92 }
wdenk2262cfe2002-11-18 00:14:45 +000093 }
94
95 if (fs -> num_clus >= FAT12_MAX_NB) {
wdenk8bde7f72003-06-27 21:31:46 +000096 /* Too much clusters */
97 return (-1);
wdenk2262cfe2002-11-18 00:14:45 +000098 }
99
100 return check_fat (fs);
101}
102/*-----------------------------------------------------------------------------
wdenk8bde7f72003-06-27 21:31:46 +0000103 * read_fat --
wdenk2262cfe2002-11-18 00:14:45 +0000104 *-----------------------------------------------------------------------------
105 */
106int read_fat (BootSector_t *boot, Fs_t *fs)
107{
108 unsigned int buflen;
109 int i;
110
111 /* Allocate Fat Buffer */
112 buflen = fs -> fat_len * SZ_STD_SECTOR;
113 if (fs -> fat_buf) {
wdenk8bde7f72003-06-27 21:31:46 +0000114 free (fs -> fat_buf);
wdenk2262cfe2002-11-18 00:14:45 +0000115 }
116
117 if ((fs -> fat_buf = malloc (buflen)) == NULL) {
wdenk8bde7f72003-06-27 21:31:46 +0000118 return (-1);
wdenk2262cfe2002-11-18 00:14:45 +0000119 }
120
121 /* Try to read each Fat */
122 for (i = 0; i< fs -> nb_fat; i++){
wdenk8bde7f72003-06-27 21:31:46 +0000123 if (read_one_fat (boot, fs, i) == 0) {
124 /* Fat is OK */
125 fs -> num_fat = i;
126 break;
127 }
wdenk2262cfe2002-11-18 00:14:45 +0000128 }
129
130 if (i == fs -> nb_fat){
wdenk8bde7f72003-06-27 21:31:46 +0000131 return (-1);
wdenk2262cfe2002-11-18 00:14:45 +0000132 }
wdenk8bde7f72003-06-27 21:31:46 +0000133
wdenk2262cfe2002-11-18 00:14:45 +0000134 if (fs -> fat_len > (((fs -> num_clus + 2) *
wdenk8bde7f72003-06-27 21:31:46 +0000135 (FAT_BITS / 4) -1 ) / 2 /
136 SZ_STD_SECTOR + 1)) {
137 return (-1);
wdenk2262cfe2002-11-18 00:14:45 +0000138 }
139 return (0);
140}
141
wdenk2262cfe2002-11-18 00:14:45 +0000142#endif