From 83ff4930c1a0407460b6ffa8bf3a551221e6d406 Mon Sep 17 00:00:00 2001
From: germeier <germeier>
Date: Wed, 11 Sep 2002 11:55:35 +0000
Subject: fixed block address issues in spare area

---
 libmpio/io.c   | 92 +++++++++++++++++++++++++++++++++++++++-------------------
 libmpio/mpio.c |  4 +--
 2 files changed, 64 insertions(+), 32 deletions(-)

(limited to 'libmpio')

diff --git a/libmpio/io.c b/libmpio/io.c
index 4cc134a..4490a97 100644
--- a/libmpio/io.c
+++ b/libmpio/io.c
@@ -2,7 +2,7 @@
 
 /* 
  *
- * $Id: io.c,v 1.9 2002/09/11 00:18:34 germeier Exp $
+ * $Id: io.c,v 1.10 2002/09/11 11:55:37 germeier Exp $
  *
  * Library for USB MPIO-*
  *
@@ -45,6 +45,7 @@
 #include "ecc.h"
 
 WORD index2blockaddress(WORD);
+WORD cluster2blockaddress(DWORD, BYTE);
 int  cluster2block(int mem, int sector);
 void fatentry2hw(mpio_fatentry_t *, BYTE *, DWORD *);
 
@@ -128,7 +129,8 @@ cluster2block(int mem, int sector)
   return a;
 }
 
-WORD index2blockaddress(WORD ba)
+WORD 
+index2blockaddress(WORD ba)
 {
   WORD addr;
   BYTE p = 0, c = 0;
@@ -158,6 +160,49 @@ WORD index2blockaddress(WORD ba)
   return addr;
 }
 
+WORD 
+cluster2blockaddress(DWORD index, BYTE size)
+{
+  DWORD ba;
+  WORD  block_address;
+  
+  if (index<0x40) 
+    {
+      block_address=0;
+    } else {  
+      if (index >= 0x8000) 
+	{	  
+	  ba = ((index % 0x8000) / 0x20);
+	  /* I'm so large in *not* knowing! */
+	  /* these are the same jumps as found in cluster2block */
+	  if (size == 64)
+	    {
+	      if (index >= 0x1d5e0)
+		ba--;	      
+	    }
+	} else {       
+	  ba = (index / 0x20) - 2;
+	  /* I'm so large in *not* knowing! */
+	  /* these are the same jumps as found in cluster2block */
+	  if (size == 32) 
+	    {
+	      if (ba >= 0x33f)
+		ba--;	      
+	    }	  
+	  if (size == 64)
+	    {
+	      if (ba >= 0x05b)
+		ba--;	      
+	    }	  
+	}
+      block_address= index2blockaddress(ba);
+      debugn(2, "block-foo: %06x %04x %04x\n", index, ba, block_address);
+    }
+  
+  return block_address;
+}
+
+
 /*
  * low-low level functions
  */
@@ -462,17 +507,7 @@ mpio_io_sector_write(mpio_t *m, BYTE mem, DWORD index, BYTE *input)
   memcpy(sendbuff, input, SECTOR_SIZE);
   
   if (mem==MPIO_EXTERNAL_MEM) 
-    if (index<0x40) 
-      {
-	block_address=0;
-      } else {  
-	ba= (index / 0x20) - 2;
-	if (ba > 0x8000)
-	  ba %= 0x8000;
-	debugn(2, "sector-foo: %4x\n", ba);
-	block_address= index2blockaddress(ba);
-	debugn(2, "sector-foo: %4x\n", block_address);
-      }
+    block_address = cluster2blockaddress(index, sm->size);
   
     {    
       /* generate ECC information for spare area ! */
@@ -522,6 +557,7 @@ mpio_io_block_read(mpio_t *m, BYTE mem, mpio_fatentry_t *f, BYTE *output)
   BYTE  chip;
   DWORD address;
   BYTE cmdpacket[CMD_SIZE], recvbuff[BLOCK_TRANS];
+  DWORD block_address, ba;
 
   if (mem == MPIO_INTERNAL_MEM) sm = &m->internal;
   if (mem == MPIO_EXTERNAL_MEM) sm = &m->external;
@@ -567,6 +603,17 @@ mpio_io_block_read(mpio_t *m, BYTE mem, mpio_fatentry_t *f, BYTE *output)
 				((recvbuff +(i * SECTOR_TRANS) 
 				  + SECTOR_SIZE + 8))))
 	  debug ("ECC error @ (%02x : %06x)\n", chip, address);
+
+	if (i==0) 
+	  {	    
+	    block_address = cluster2blockaddress(address, sm->size);
+	    
+	    ba = recvbuff[(i * SECTOR_TRANS) + SECTOR_SIZE + 0x06] * 0x100 + 
+	         recvbuff[(i * SECTOR_TRANS) + SECTOR_SIZE + 0x07] ;
+	    if (block_address != ba)
+	      debugn(2,"different block_addresses during read: %04x vs. %04x\n",
+		     block_address, ba);
+	  }
 	
       }
       
@@ -728,23 +775,8 @@ mpio_io_block_write(mpio_t *m, BYTE mem, mpio_fatentry_t *f, BYTE *data)
       /* fill in block information */
       if (mem == MPIO_EXTERNAL_MEM) 
 	{      
-	  if (address < 0x40) 
-	    {
-	      block_address = 0;
-	    } else {  
-	      debugn(2, "block-foo-1: %06x\n", address);
-	      /* block address is relativ to zone */
-	      if (address >= 0x8000) 
-		{		  
-		  ba = (address % 0x8000) / 0x20;
-		} else {
-		  ba = (address / 0x20) - 2;
-		}	      
-	      debugn(2, "block-foo-2: %4x\n", ba);
-	      block_address = index2blockaddress(ba);
-	      debugn(2, "block-foo-3: %4x\n", block_address);
-	    }
-	  
+	  block_address = cluster2blockaddress(address, sm->size);
+
 	  ba = (block_address / 0x100) & 0xff;
 	  sendbuff[(i * SECTOR_TRANS) + SECTOR_SIZE + 0x06] = ba;
 	  sendbuff[(i * SECTOR_TRANS) + SECTOR_SIZE + 0x0b] = ba;
diff --git a/libmpio/mpio.c b/libmpio/mpio.c
index d024918..e2a319f 100644
--- a/libmpio/mpio.c
+++ b/libmpio/mpio.c
@@ -1,6 +1,6 @@
 /* 
  *
- * $Id: mpio.c,v 1.9 2002/09/11 00:18:34 germeier Exp $
+ * $Id: mpio.c,v 1.10 2002/09/11 11:55:38 germeier Exp $
  *
  * Library for USB MPIO-*
  *
@@ -285,7 +285,7 @@ mpio_file_get(mpio_t *m, mpio_mem_t mem, BYTE *filename,
     
     do
       {
-	debugn(2, "sector: %4x\n", f->entry);	    
+	debugn(2, "entry: %4x\n", f->entry);	    
     
 	mpio_io_block_read(m, mem, f, block);
 
-- 
cgit v1.2.3