commit bushing's endianess fixes

This commit is contained in:
Dave Murphy 2008-05-01 10:02:19 +00:00
parent 33af69e193
commit 5b9bfaecf0
3 changed files with 50 additions and 17 deletions

View File

@ -44,6 +44,7 @@
#include "disc_io/disc.h" #include "disc_io/disc.h"
#include "mem_allocate.h" #include "mem_allocate.h"
#include "bit_ops.h"
#define CACHE_FREE 0xFFFFFFFF #define CACHE_FREE 0xFFFFFFFF
@ -167,6 +168,19 @@ bool _FAT_cache_readPartialSector (CACHE* cache, void* buffer, u32 sector, u32 o
return true; return true;
} }
bool _FAT_cache_readLittleEndianValue (CACHE* cache, u32 *value, u32 sector, u32 offset, u32 num_bytes) {
u8 buf[4];
if (!_FAT_cache_readPartialSector(cache, buf, sector, offset, num_bytes)) return false;
switch(num_bytes) {
case 1: *value = buf[0]; break;
case 2: *value = u8array_to_u16(buf,0); break;
case 4: *value = u8array_to_u32(buf,0); break;
default: return false;
}
return true;
}
/* /*
Writes some data to a cache page, making sure it is loaded into memory first. Writes some data to a cache page, making sure it is loaded into memory first.
*/ */
@ -188,6 +202,19 @@ bool _FAT_cache_writePartialSector (CACHE* cache, const void* buffer, u32 sector
return true; return true;
} }
bool _FAT_cache_writeLittleEndianValue (CACHE* cache, const u32 value, u32 sector, u32 offset, u32 size) {
u8 buf[4] = {0, 0, 0, 0};
switch(size) {
case 1: buf[0] = value; break;
case 2: u16_to_u8array(buf, 0, value); break;
case 4: u32_to_u8array(buf, 0, value); break;
default: return false;
}
return _FAT_cache_writePartialSector(cache, buf, sector, offset, size);
}
/* /*
Writes some data to a cache page, zeroing out the page first Writes some data to a cache page, zeroing out the page first
*/ */

View File

@ -67,6 +67,8 @@ Precondition: offset + size <= BYTES_PER_READ
*/ */
bool _FAT_cache_readPartialSector (CACHE* cache, void* buffer, u32 sector, u32 offset, u32 size); bool _FAT_cache_readPartialSector (CACHE* cache, void* buffer, u32 sector, u32 offset, u32 size);
bool _FAT_cache_readLittleEndianValue (CACHE* cache, u32 *value, u32 sector, u32 offset, u32 num_bytes);
/* /*
Write data to a sector in the cache Write data to a sector in the cache
If the sector is not in the cache, it will be swapped in. If the sector is not in the cache, it will be swapped in.
@ -77,6 +79,8 @@ Precondition: offset + size <= BYTES_PER_READ
*/ */
bool _FAT_cache_writePartialSector (CACHE* cache, const void* buffer, u32 sector, u32 offset, u32 size); bool _FAT_cache_writePartialSector (CACHE* cache, const void* buffer, u32 sector, u32 offset, u32 size);
bool _FAT_cache_writeLittleEndianValue (CACHE* cache, const u32 value, u32 sector, u32 offset, u32 num_bytes);
/* /*
Write data to a sector in the cache, zeroing the sector first Write data to a sector in the cache, zeroing the sector first
If the sector is not in the cache, it will be swapped in. If the sector is not in the cache, it will be swapped in.

View File

@ -71,7 +71,7 @@ u32 _FAT_fat_nextCluster(PARTITION* partition, u32 cluster)
offset = ((cluster * 3) / 2) % BYTES_PER_READ; offset = ((cluster * 3) / 2) % BYTES_PER_READ;
_FAT_cache_readPartialSector (partition->cache, &nextCluster, sector, offset, sizeof(u8)); _FAT_cache_readLittleEndianValue (partition->cache, &nextCluster, sector, offset, sizeof(u8));
offset++; offset++;
@ -79,8 +79,10 @@ u32 _FAT_fat_nextCluster(PARTITION* partition, u32 cluster)
offset = 0; offset = 0;
sector++; sector++;
} }
u32 nextCluster_h = 0;
_FAT_cache_readPartialSector (partition->cache, ((u8*)&nextCluster) + sizeof(u8), sector, offset, sizeof(u8)); _FAT_cache_readLittleEndianValue (partition->cache, &nextCluster_h, sector, offset, sizeof(u8));
nextCluster |= (nextCluster_h << 8);
if (cluster & 0x01) { if (cluster & 0x01) {
nextCluster = nextCluster >> 4; nextCluster = nextCluster >> 4;
@ -99,7 +101,7 @@ u32 _FAT_fat_nextCluster(PARTITION* partition, u32 cluster)
sector = partition->fat.fatStart + ((cluster << 1) / BYTES_PER_READ); sector = partition->fat.fatStart + ((cluster << 1) / BYTES_PER_READ);
offset = (cluster % (BYTES_PER_READ >> 1)) << 1; offset = (cluster % (BYTES_PER_READ >> 1)) << 1;
_FAT_cache_readPartialSector (partition->cache, &nextCluster, sector, offset, sizeof(u16)); _FAT_cache_readLittleEndianValue (partition->cache, &nextCluster, sector, offset, sizeof(u16));
if (nextCluster >= 0xFFF7) if (nextCluster >= 0xFFF7)
{ {
@ -111,7 +113,7 @@ u32 _FAT_fat_nextCluster(PARTITION* partition, u32 cluster)
sector = partition->fat.fatStart + ((cluster << 2) / BYTES_PER_READ); sector = partition->fat.fatStart + ((cluster << 2) / BYTES_PER_READ);
offset = (cluster % (BYTES_PER_READ >> 2)) << 2; offset = (cluster % (BYTES_PER_READ >> 2)) << 2;
_FAT_cache_readPartialSector (partition->cache, &nextCluster, sector, offset, sizeof(u32)); _FAT_cache_readLittleEndianValue (partition->cache, &nextCluster, sector, offset, sizeof(u32));
if (nextCluster >= 0x0FFFFFF7) if (nextCluster >= 0x0FFFFFF7)
{ {
@ -134,7 +136,7 @@ on the cluster number.
static bool _FAT_fat_writeFatEntry (PARTITION* partition, u32 cluster, u32 value) { static bool _FAT_fat_writeFatEntry (PARTITION* partition, u32 cluster, u32 value) {
u32 sector; u32 sector;
int offset; int offset;
u8 oldValue; u32 oldValue;
if ((cluster < CLUSTER_FIRST) || (cluster > partition->fat.lastCluster /* This will catch CLUSTER_ERROR */)) if ((cluster < CLUSTER_FIRST) || (cluster > partition->fat.lastCluster /* This will catch CLUSTER_ERROR */))
{ {
@ -153,11 +155,11 @@ static bool _FAT_fat_writeFatEntry (PARTITION* partition, u32 cluster, u32 value
if (cluster & 0x01) { if (cluster & 0x01) {
_FAT_cache_readPartialSector (partition->cache, &oldValue, sector, offset, sizeof(u8)); _FAT_cache_readLittleEndianValue (partition->cache, &oldValue, sector, offset, sizeof(u8));
value = (value << 4) | (oldValue & 0x0F); value = (value << 4) | (oldValue & 0x0F);
_FAT_cache_writePartialSector (partition->cache, &value, sector, offset, sizeof(u8)); _FAT_cache_writeLittleEndianValue (partition->cache, value & 0xFF, sector, offset, sizeof(u8));
offset++; offset++;
if (offset >= BYTES_PER_READ) { if (offset >= BYTES_PER_READ) {
@ -165,11 +167,11 @@ static bool _FAT_fat_writeFatEntry (PARTITION* partition, u32 cluster, u32 value
sector++; sector++;
} }
_FAT_cache_writePartialSector (partition->cache, ((u8*)&value) + sizeof(u8), sector, offset, sizeof(u8)); _FAT_cache_writeLittleEndianValue (partition->cache, (value >> 8) & 0xFF, sector, offset, sizeof(u8));
} else { } else {
_FAT_cache_writePartialSector (partition->cache, &value, sector, offset, sizeof(u8)); _FAT_cache_writeLittleEndianValue (partition->cache, value, sector, offset, sizeof(u8));
offset++; offset++;
if (offset >= BYTES_PER_READ) { if (offset >= BYTES_PER_READ) {
@ -177,11 +179,11 @@ static bool _FAT_fat_writeFatEntry (PARTITION* partition, u32 cluster, u32 value
sector++; sector++;
} }
_FAT_cache_readPartialSector (partition->cache, &oldValue, sector, offset, sizeof(u8)); _FAT_cache_readLittleEndianValue (partition->cache, &oldValue, sector, offset, sizeof(u8));
value = ((value >> 8) & 0x0F) | (oldValue & 0xF0); value = ((value >> 8) & 0x0F) | (oldValue & 0xF0);
_FAT_cache_writePartialSector (partition->cache, &value, sector, offset, sizeof(u8)); _FAT_cache_writeLittleEndianValue (partition->cache, value, sector, offset, sizeof(u8));
} }
break; break;
@ -190,7 +192,7 @@ static bool _FAT_fat_writeFatEntry (PARTITION* partition, u32 cluster, u32 value
sector = partition->fat.fatStart + ((cluster << 1) / BYTES_PER_READ); sector = partition->fat.fatStart + ((cluster << 1) / BYTES_PER_READ);
offset = (cluster % (BYTES_PER_READ >> 1)) << 1; offset = (cluster % (BYTES_PER_READ >> 1)) << 1;
_FAT_cache_writePartialSector (partition->cache, &value, sector, offset, sizeof(u16)); _FAT_cache_writeLittleEndianValue (partition->cache, value, sector, offset, sizeof(u16));
break; break;
@ -198,7 +200,7 @@ static bool _FAT_fat_writeFatEntry (PARTITION* partition, u32 cluster, u32 value
sector = partition->fat.fatStart + ((cluster << 2) / BYTES_PER_READ); sector = partition->fat.fatStart + ((cluster << 2) / BYTES_PER_READ);
offset = (cluster % (BYTES_PER_READ >> 2)) << 2; offset = (cluster % (BYTES_PER_READ >> 2)) << 2;
_FAT_cache_writePartialSector (partition->cache, &value, sector, offset, sizeof(u32)); _FAT_cache_writeLittleEndianValue (partition->cache, value, sector, offset, sizeof(u32));
break; break;