fat.c 4.3 KB

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