2002-11-18 00:14:45 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2002
|
2011-08-04 16:45:45 +00:00
|
|
|
* Stäubli Faverges - <www.staubli.com>
|
2002-11-18 00:14:45 +00:00
|
|
|
* Pierre AUBERT p.aubert@staubli.com
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2002-11-18 00:14:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <config.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
|
|
|
|
#include "dos.h"
|
|
|
|
#include "fdos.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------
|
2003-06-27 21:31:46 +00:00
|
|
|
* fat_decode --
|
2002-11-18 00:14:45 +00:00
|
|
|
*-----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
unsigned int fat_decode (Fs_t *fs, unsigned int num)
|
|
|
|
{
|
|
|
|
unsigned int start = num * 3 / 2;
|
|
|
|
unsigned char *address = fs -> fat_buf + start;
|
2003-06-27 21:31:46 +00:00
|
|
|
|
2002-11-18 00:14:45 +00:00
|
|
|
if (num < 2 || start + 1 > (fs -> fat_len * SZ_STD_SECTOR))
|
2003-06-27 21:31:46 +00:00
|
|
|
return 1;
|
|
|
|
|
2002-11-18 00:14:45 +00:00
|
|
|
if (num & 1)
|
2003-06-27 21:31:46 +00:00
|
|
|
return ((address [1] & 0xff) << 4) | ((address [0] & 0xf0 ) >> 4);
|
2002-11-18 00:14:45 +00:00
|
|
|
else
|
2003-06-27 21:31:46 +00:00
|
|
|
return ((address [1] & 0xf) << 8) | (address [0] & 0xff );
|
2002-11-18 00:14:45 +00:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------
|
2003-06-27 21:31:46 +00:00
|
|
|
* check_fat --
|
2002-11-18 00:14:45 +00:00
|
|
|
*-----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static int check_fat (Fs_t *fs)
|
|
|
|
{
|
|
|
|
int i, f;
|
|
|
|
|
|
|
|
/* Cluster verification */
|
|
|
|
for (i = 3 ; i < fs -> num_clus; i++){
|
2003-06-27 21:31:46 +00:00
|
|
|
f = fat_decode (fs, i);
|
|
|
|
if (f < FAT12_LAST && f > fs -> num_clus){
|
|
|
|
/* Wrong cluster number detected */
|
|
|
|
return (-1);
|
|
|
|
}
|
2002-11-18 00:14:45 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------
|
2003-06-27 21:31:46 +00:00
|
|
|
* read_one_fat --
|
2002-11-18 00:14:45 +00:00
|
|
|
*-----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static int read_one_fat (BootSector_t *boot, Fs_t *fs, int nfat)
|
|
|
|
{
|
|
|
|
if (dev_read (fs -> fat_buf,
|
2003-06-27 21:31:46 +00:00
|
|
|
(fs -> fat_start + nfat * fs -> fat_len),
|
|
|
|
fs -> fat_len) < 0) {
|
|
|
|
return (-1);
|
2002-11-18 00:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fs -> fat_buf [0] || fs -> fat_buf [1] || fs -> fat_buf [2]) {
|
2003-06-27 21:31:46 +00:00
|
|
|
if ((fs -> fat_buf [0] != boot -> descr &&
|
|
|
|
(fs -> fat_buf [0] != 0xf9 || boot -> descr != MEDIA_STD)) ||
|
|
|
|
fs -> fat_buf [0] < MEDIA_STD){
|
|
|
|
/* Unknown Media */
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (fs -> fat_buf [1] != 0xff || fs -> fat_buf [2] != 0xff){
|
|
|
|
/* FAT doesn't start with good values */
|
|
|
|
return (-1);
|
|
|
|
}
|
2002-11-18 00:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fs -> num_clus >= FAT12_MAX_NB) {
|
2003-06-27 21:31:46 +00:00
|
|
|
/* Too much clusters */
|
|
|
|
return (-1);
|
2002-11-18 00:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return check_fat (fs);
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------
|
2003-06-27 21:31:46 +00:00
|
|
|
* read_fat --
|
2002-11-18 00:14:45 +00:00
|
|
|
*-----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
int read_fat (BootSector_t *boot, Fs_t *fs)
|
|
|
|
{
|
|
|
|
unsigned int buflen;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Allocate Fat Buffer */
|
|
|
|
buflen = fs -> fat_len * SZ_STD_SECTOR;
|
|
|
|
if (fs -> fat_buf) {
|
2003-06-27 21:31:46 +00:00
|
|
|
free (fs -> fat_buf);
|
2002-11-18 00:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((fs -> fat_buf = malloc (buflen)) == NULL) {
|
2003-06-27 21:31:46 +00:00
|
|
|
return (-1);
|
2002-11-18 00:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to read each Fat */
|
|
|
|
for (i = 0; i< fs -> nb_fat; i++){
|
2003-06-27 21:31:46 +00:00
|
|
|
if (read_one_fat (boot, fs, i) == 0) {
|
|
|
|
/* Fat is OK */
|
|
|
|
fs -> num_fat = i;
|
|
|
|
break;
|
|
|
|
}
|
2002-11-18 00:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i == fs -> nb_fat){
|
2003-06-27 21:31:46 +00:00
|
|
|
return (-1);
|
2002-11-18 00:14:45 +00:00
|
|
|
}
|
2003-06-27 21:31:46 +00:00
|
|
|
|
2002-11-18 00:14:45 +00:00
|
|
|
if (fs -> fat_len > (((fs -> num_clus + 2) *
|
2003-06-27 21:31:46 +00:00
|
|
|
(FAT_BITS / 4) -1 ) / 2 /
|
|
|
|
SZ_STD_SECTOR + 1)) {
|
|
|
|
return (-1);
|
2002-11-18 00:14:45 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|