wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 29 | #include "dos.h" |
| 30 | #include "fdos.h" |
| 31 | |
| 32 | |
| 33 | /*----------------------------------------------------------------------------- |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 34 | * fat_decode -- |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 35 | *----------------------------------------------------------------------------- |
| 36 | */ |
| 37 | unsigned int fat_decode (Fs_t *fs, unsigned int num) |
| 38 | { |
| 39 | unsigned int start = num * 3 / 2; |
| 40 | unsigned char *address = fs -> fat_buf + start; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 41 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 42 | if (num < 2 || start + 1 > (fs -> fat_len * SZ_STD_SECTOR)) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 43 | return 1; |
| 44 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 45 | if (num & 1) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 46 | return ((address [1] & 0xff) << 4) | ((address [0] & 0xf0 ) >> 4); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 47 | else |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 48 | return ((address [1] & 0xf) << 8) | (address [0] & 0xff ); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 49 | } |
| 50 | /*----------------------------------------------------------------------------- |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 51 | * check_fat -- |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 52 | *----------------------------------------------------------------------------- |
| 53 | */ |
| 54 | static int check_fat (Fs_t *fs) |
| 55 | { |
| 56 | int i, f; |
| 57 | |
| 58 | /* Cluster verification */ |
| 59 | for (i = 3 ; i < fs -> num_clus; i++){ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 60 | f = fat_decode (fs, i); |
| 61 | if (f < FAT12_LAST && f > fs -> num_clus){ |
| 62 | /* Wrong cluster number detected */ |
| 63 | return (-1); |
| 64 | } |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 65 | } |
| 66 | return (0); |
| 67 | } |
| 68 | /*----------------------------------------------------------------------------- |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 69 | * read_one_fat -- |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 70 | *----------------------------------------------------------------------------- |
| 71 | */ |
| 72 | static int read_one_fat (BootSector_t *boot, Fs_t *fs, int nfat) |
| 73 | { |
| 74 | if (dev_read (fs -> fat_buf, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 75 | (fs -> fat_start + nfat * fs -> fat_len), |
| 76 | fs -> fat_len) < 0) { |
| 77 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | if (fs -> fat_buf [0] || fs -> fat_buf [1] || fs -> fat_buf [2]) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 81 | if ((fs -> fat_buf [0] != boot -> descr && |
| 82 | (fs -> fat_buf [0] != 0xf9 || boot -> descr != MEDIA_STD)) || |
| 83 | fs -> fat_buf [0] < MEDIA_STD){ |
| 84 | /* Unknown Media */ |
| 85 | return (-1); |
| 86 | } |
| 87 | if (fs -> fat_buf [1] != 0xff || fs -> fat_buf [2] != 0xff){ |
| 88 | /* FAT doesn't start with good values */ |
| 89 | return (-1); |
| 90 | } |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | if (fs -> num_clus >= FAT12_MAX_NB) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 94 | /* Too much clusters */ |
| 95 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | return check_fat (fs); |
| 99 | } |
| 100 | /*----------------------------------------------------------------------------- |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 101 | * read_fat -- |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 102 | *----------------------------------------------------------------------------- |
| 103 | */ |
| 104 | int read_fat (BootSector_t *boot, Fs_t *fs) |
| 105 | { |
| 106 | unsigned int buflen; |
| 107 | int i; |
| 108 | |
| 109 | /* Allocate Fat Buffer */ |
| 110 | buflen = fs -> fat_len * SZ_STD_SECTOR; |
| 111 | if (fs -> fat_buf) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 112 | free (fs -> fat_buf); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | if ((fs -> fat_buf = malloc (buflen)) == NULL) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 116 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* Try to read each Fat */ |
| 120 | for (i = 0; i< fs -> nb_fat; i++){ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 121 | if (read_one_fat (boot, fs, i) == 0) { |
| 122 | /* Fat is OK */ |
| 123 | fs -> num_fat = i; |
| 124 | break; |
| 125 | } |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | if (i == fs -> nb_fat){ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 129 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 130 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 131 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 132 | if (fs -> fat_len > (((fs -> num_clus + 2) * |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 133 | (FAT_BITS / 4) -1 ) / 2 / |
| 134 | SZ_STD_SECTOR + 1)) { |
| 135 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 136 | } |
| 137 | return (0); |
| 138 | } |