All of lore.kernel.org
 help / color / mirror / Atom feed
* Patch for different sector sizes
@ 2008-10-07 22:30 Clemens Helfmeier
  2008-10-08 16:11 ` Clemens Helfmeier
  0 siblings, 1 reply; 2+ messages in thread
From: Clemens Helfmeier @ 2008-10-07 22:30 UTC (permalink / raw)
  To: grub-devel

[-- Attachment #1: Type: text/plain, Size: 387 bytes --]

Hey Guys,

this seems to be a tough topic ;-)

At Least i have fixed a few lines of code today, it compiles not yet in total, 
but i hope the task can be completed in a few more hours of work. I guess a few 
things will still be broken after completion, i didn't yet test anything.

Maybe this is even too bad to be done? what do you think?

Path Attached, please comment!

bye,
 sumsum

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: variable_sector_size.patch --]
[-- Type: text/x-diff; charset=unknown-8bit, Size: 65527 bytes --]

Index: disk/lvm.c
===================================================================
--- disk/lvm.c	(revision 1886)
+++ disk/lvm.c	(working copy)
@@ -185,7 +185,7 @@
      read from.  */
   if (pv->disk)
     err = grub_disk_read (pv->disk, offset, 0,
-			  size << GRUB_DISK_SECTOR_BITS, buf);
+			  size * pv->disk->sector_size, buf);
   else
     err = grub_error (GRUB_ERR_UNKNOWN_DEVICE,
 		      "Physical volume %s not found", pv->name);
Index: disk/scsi.c
===================================================================
--- disk/scsi.c	(revision 1886)
+++ disk/scsi.c	(working copy)
@@ -294,7 +294,7 @@
 	  /* SCSI blocks can be something else than 512, although GRUB
 	     wants 512 byte blocks.  */
 	  disk->total_sectors = ((scsi->size * scsi->blocksize)
-				 << GRUB_DISK_SECTOR_BITS);
+				 * disk->sector_size);
 
 	  grub_dprintf ("scsi", "capacity=%llu, blksize=%d\n",
 			disk->total_sectors, scsi->blocksize);
@@ -325,7 +325,7 @@
 
   /* SCSI sectors are variable in size.  GRUB uses 512 byte
      sectors.  */
-  sector = grub_divmod64 (sector, scsi->blocksize >> GRUB_DISK_SECTOR_BITS,
+  sector = grub_divmod64 (sector, scsi->blocksize / disk->sector_size,
 			  NULL);
 
   /* Depending on the type, select a read function.  */
Index: disk/ata.c
===================================================================
--- disk/ata.c	(revision 1886)
+++ disk/ata.c	(working copy)
@@ -77,6 +77,9 @@
     GRUB_ATA_CMD_EXEC_DEV_DIAGNOSTICS = 0x90
   };
 
+// TODO: Put the sector size into grub_ata_device
+#define GRUB_ATA_SECTOR_SIZE 512
+
 struct grub_ata_device
 {
   /* IDE port to use.  */
@@ -323,7 +326,7 @@
   grub_uint16_t *info16;
   int ataerr = 0;
 
-  info = grub_malloc (GRUB_DISK_SECTOR_SIZE);
+  info = grub_malloc (GRUB_ATA_SECTOR_SIZE);
   if (! info)
     return grub_errno;
 
@@ -343,7 +346,7 @@
     }
   grub_ata_wait ();
 
-  if (grub_ata_pio_read (dev, info, GRUB_DISK_SECTOR_SIZE))
+  if (grub_ata_pio_read (dev, info, GRUB_ATA_SECTOR_SIZE))
     ataerr = grub_ata_regget (dev, GRUB_ATA_REG_ERROR);
   if (ataerr & 4)
     {
Index: disk/i386/pc/biosdisk.c
===================================================================
--- disk/i386/pc/biosdisk.c	(revision 1886)
+++ disk/i386/pc/biosdisk.c	(working copy)
@@ -196,8 +196,7 @@
       struct grub_biosdisk_dap *dap;
       
       dap = (struct grub_biosdisk_dap *) (GRUB_MEMORY_MACHINE_SCRATCH_ADDR
-					  + (data->sectors
-					     << GRUB_DISK_SECTOR_BITS));
+					  + (data->sectors) * disk->sector_size);
       dap->length = sizeof (*dap);
       dap->reserved = 0;
       dap->blocks = size;
@@ -305,8 +304,8 @@
 	return grub_errno;
 
       grub_memcpy (buf, (void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR,
-		   len << GRUB_DISK_SECTOR_BITS);
-      buf += len << GRUB_DISK_SECTOR_BITS;
+		   len * disk->sector_size);
+      buf += len * disk->sector_size;
       sector += len;
       size -= len;
     }
@@ -329,13 +328,13 @@
 	len = size;
 
       grub_memcpy ((void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR, buf,
-		   len << GRUB_DISK_SECTOR_BITS);
+		   len * disk->sector_size);
 
       if (grub_biosdisk_rw (GRUB_BIOSDISK_WRITE, disk, sector, len,
 			    GRUB_MEMORY_MACHINE_SCRATCH_SEG))
 	return grub_errno;
 
-      buf += len << GRUB_DISK_SECTOR_BITS;
+      buf += len * disk->sector_size;
       sector += len;
       size -= len;
     }
Index: disk/raid.c
===================================================================
--- disk/raid.c	(revision 1886)
+++ disk/raid.c	(working copy)
@@ -240,6 +240,9 @@
             if (read_size > size)
               read_size = size;
 
+	    // sector size for array (must be equal for all drives, not checked)
+	    unsigned int sector_size = 0;
+
             for (i = 0; i < near; i++)
               {
                 unsigned int k;
@@ -249,13 +252,14 @@
                   {
                     if (array->device[k])
                       {
+			sector_size = array->device[k]->sector_size;
                         if (grub_errno == GRUB_ERR_READ_ERROR)
                           grub_errno = GRUB_ERR_NONE;
 
                         err = grub_disk_read (array->device[k],
                                               read_sector + j * far_ofs + b,
                                               0,
-                                              read_size << GRUB_DISK_SECTOR_BITS,
+                                              read_size * sector_size,
                                               buf);
                         if (! err)
                           break;
@@ -285,7 +289,7 @@
             if (err)
               return err;
 
-            buf += read_size << GRUB_DISK_SECTOR_BITS;
+            buf += read_size * sector_size;
 	    size -= read_size;
 	    if (! size)
 	      break;
@@ -366,7 +370,7 @@
 
                 err = grub_disk_read (array->device[disknr],
                                       read_sector + b, 0,
-                                      read_size << GRUB_DISK_SECTOR_BITS,
+                                      read_size * array->device[disknr]->sector_size,
                                       buf);
 
                 if ((err) && (err != GRUB_ERR_READ_ERROR))
@@ -405,7 +409,7 @@
                   break;
               }
 	    
-	    buf += read_size << GRUB_DISK_SECTOR_BITS;
+	    buf += read_size * array->device[disknr]->sector_size;
 	    size -= read_size;
 	    if (! size)
 	      break;
Index: disk/memdisk.c
===================================================================
--- disk/memdisk.c	(revision 1886)
+++ disk/memdisk.c	(working copy)
@@ -56,7 +56,7 @@
 grub_memdisk_read (grub_disk_t disk __attribute((unused)), grub_disk_addr_t sector,
 		    grub_size_t size, char *buf)
 {
-  grub_memcpy (buf, memdisk_addr + (sector << GRUB_DISK_SECTOR_BITS), size << GRUB_DISK_SECTOR_BITS);
+  grub_memcpy (buf, memdisk_addr + (sector * disk->sector_size), size * disk->sector_size);
   return 0;
 }
 
@@ -64,7 +64,7 @@
 grub_memdisk_write (grub_disk_t disk __attribute((unused)), grub_disk_addr_t sector,
 		     grub_size_t size, const char *buf)
 {
-  grub_memcpy (memdisk_addr + (sector << GRUB_DISK_SECTOR_BITS), buf, size << GRUB_DISK_SECTOR_BITS);
+  grub_memcpy (memdisk_addr + (sector * disk->sector_size), buf, size * disk->sector_size);
   return 0;
 }
 
Index: disk/raid5_recover.c
===================================================================
--- disk/raid5_recover.c	(revision 1886)
+++ disk/raid5_recover.c	(working copy)
@@ -31,7 +31,10 @@
   char *buf2;
   int i;
 
-  size <<= GRUB_DISK_SECTOR_BITS;
+  if (array->device[0] == 0)
+    return grub_errno;
+
+  size *= array->device[0]->sector_size;
   buf2 = grub_malloc (size);
   if (!buf2)
     return grub_errno;
Index: disk/loopback.c
===================================================================
--- disk/loopback.c	(revision 1886)
+++ disk/loopback.c	(working copy)
@@ -200,20 +200,20 @@
   grub_file_t file = (grub_file_t) disk->data;
   grub_off_t pos;
   
-  grub_file_seek (file, sector << GRUB_DISK_SECTOR_BITS);
+  grub_file_seek (file, sector * disk->sector_size);
   
-  grub_file_read (file, buf, size << GRUB_DISK_SECTOR_BITS);
+  grub_file_read (file, buf, size * disk->sector_size);
   if (grub_errno)
     return grub_errno;
   
   /* In case there is more data read than there is available, in case
      of files that are not a multiple of GRUB_DISK_SECTOR_SIZE, fill
      the rest with zeros.  */
-  pos = (sector + size) << GRUB_DISK_SECTOR_BITS;
+  pos = (sector + size) * disk->sector_size;
   if (pos > file->size)
     {
       grub_size_t amount = pos - file->size;
-      grub_memset (buf + (size << GRUB_DISK_SECTOR_BITS) - amount, 0, amount);
+      grub_memset (buf + (size * disk->sector_size) - amount, 0, amount);
     }
   
   return 0;
Index: disk/raid6_recover.c
===================================================================
--- disk/raid6_recover.c	(revision 1886)
+++ disk/raid6_recover.c	(working copy)
@@ -95,7 +95,10 @@
   int err[2], nerr;
   char *pbuf = 0, *qbuf = 0;
 
-  size <<= GRUB_DISK_SECTOR_BITS;
+  if (array->device[0] == 0)
+   goto quit;
+
+  size *= array->device[0]->sector_size;
   pbuf = grub_malloc (size);
   if (!pbuf)
     goto quit;
Index: kern/fs.c
===================================================================
--- kern/fs.c	(revision 1886)
+++ kern/fs.c	(working copy)
@@ -198,7 +198,7 @@
 	  goto fail;
 	}
       
-      file->size += (blocks[i].length << GRUB_DISK_SECTOR_BITS);
+      file->size += (blocks[i].length * disk->sector_size);
       p++;
     }
 
@@ -212,6 +212,23 @@
   return grub_errno;
 }
 
+static grub_uint32_t
+grub_fs_log2 (grub_uint32_t x)
+{
+  grub_uint32_t i;
+  
+  if (x == 0)
+    return -1;
+
+  for (i = 0; (x & 1) == 0; i++)
+    x >>= 1;
+
+  if (x != 1)
+    return -1;
+
+  return i;
+}
+
 static grub_ssize_t
 grub_fs_blocklist_read (grub_file_t file, char *buf, grub_size_t len)
 {
@@ -219,12 +236,17 @@
   grub_disk_addr_t sector;
   grub_off_t offset;
   grub_ssize_t ret = 0;
+  grub_disk_t disk = file->device->disk;
 
   if (len > file->size - file->offset)
     len = file->size - file->offset;
 
-  sector = (file->offset >> GRUB_DISK_SECTOR_BITS);
-  offset = (file->offset & (GRUB_DISK_SECTOR_SIZE - 1));
+// TODO: find something nice for those 64 bit divisions in here,
+// for now we will use a log2 based shifting approach.
+  grub_uint32_t sector_shift = grub_fs_log2( disk->sector_size );
+
+  sector = (file->offset >> sector_shift);
+  offset = (file->offset & (disk->sector_size - 1));
   for (p = file->data; p->length && len > 0; p++)
     {
       if (sector < p->length)
@@ -232,9 +254,9 @@
 	  grub_size_t size;
 
 	  size = len;
-	  if (((size + offset + GRUB_DISK_SECTOR_SIZE - 1)
-	       >> GRUB_DISK_SECTOR_BITS) > p->length - sector)
-	    size = ((p->length - sector) << GRUB_DISK_SECTOR_BITS) - offset;
+	  if (((size + offset + disk->sector_size - 1)
+	       >> sector_shift) > p->length - sector)
+	    size = ((p->length - sector) * disk->sector_size) - offset;
 	  
 	  if (grub_disk_read (file->device->disk, p->offset + sector, offset,
 			      size, buf) != GRUB_ERR_NONE)
@@ -242,8 +264,8 @@
 	  
 	  ret += size;
 	  len -= size;
-	  sector -= ((size + offset) >> GRUB_DISK_SECTOR_BITS);
-	  offset = ((size + offset) & (GRUB_DISK_SECTOR_SIZE - 1));
+	  sector -= ((size + offset) >> sector_shift);
+	  offset = ((size + offset) & (disk->sector_size - 1));
 	}
       else
 	sector -= p->length;
Index: kern/rescue.c
===================================================================
--- kern/rescue.c	(revision 1886)
+++ kern/rescue.c	(working copy)
@@ -129,8 +129,9 @@
 static void
 grub_rescue_cmd_cat (int argc, char *argv[])
 {
+#define CHUNK_SIZE 512
   grub_file_t file;
-  char buf[GRUB_DISK_SECTOR_SIZE];
+  char buf[CHUNK_SIZE];
   grub_ssize_t size;
 
   if (argc < 1)
Index: kern/disk.c
===================================================================
--- kern/disk.c	(revision 1886)
+++ kern/disk.c	(working copy)
@@ -148,25 +148,25 @@
 }
 
 static grub_err_t
-grub_disk_cache_store (unsigned long dev_id, unsigned long disk_id,
+grub_disk_cache_store (unsigned long dev_id, grub_disk_t disk,
 		       grub_disk_addr_t sector, const char *data)
 {
   unsigned index;
   struct grub_disk_cache *cache;
   
-  grub_disk_cache_invalidate (dev_id, disk_id, sector);
+  grub_disk_cache_invalidate (dev_id, disk->id, sector);
   
-  index = grub_disk_cache_get_index (dev_id, disk_id, sector);
+  index = grub_disk_cache_get_index (dev_id, disk->id, sector);
   cache = grub_disk_cache_table + index;
   
-  cache->data = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
+  cache->data = grub_malloc ( disk->sector_size << GRUB_DISK_CACHE_BITS);
   if (! cache->data)
     return grub_errno;
   
   grub_memcpy (cache->data, data,
-	       GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
+	       disk->sector_size << GRUB_DISK_CACHE_BITS);
   cache->dev_id = dev_id;
-  cache->disk_id = disk_id;
+  cache->disk_id = disk->id;
   cache->sector = sector;
 
   return GRUB_ERR_NONE;
@@ -330,8 +330,8 @@
 grub_disk_adjust_range (grub_disk_t disk, grub_disk_addr_t *sector,
 		       grub_off_t *offset, grub_size_t size)
 {
-  *sector += *offset >> GRUB_DISK_SECTOR_BITS;
-  *offset &= GRUB_DISK_SECTOR_SIZE - 1;
+  *sector += *offset * disk->sector_size;
+  *offset &= disk->sector_size - 1;
   
   if (disk->partition)
     {
@@ -342,16 +342,16 @@
       len = grub_partition_get_len (disk->partition);
 
       if (*sector >= len
-	  || len - *sector < ((*offset + size + GRUB_DISK_SECTOR_SIZE - 1)
-			      >> GRUB_DISK_SECTOR_BITS))
+	  || len - *sector < ((*offset + size + disk->sector_size - 1)
+			      * disk->sector_size))
 	return grub_error (GRUB_ERR_OUT_OF_RANGE, "out of partition");
 
       *sector += start;
     }
 
   if (disk->total_sectors <= *sector
-      || ((*offset + size + GRUB_DISK_SECTOR_SIZE - 1)
-	  >> GRUB_DISK_SECTOR_BITS) > disk->total_sectors - *sector)
+      || ((*offset + size + disk->sector_size - 1)
+	  * disk->sector_size) > disk->total_sectors - *sector)
     return grub_error (GRUB_ERR_OUT_OF_RANGE, "out of disk");
 
   return GRUB_ERR_NONE;
@@ -380,7 +380,7 @@
   real_offset = offset;
   
   /* Allocate a temporary buffer.  */
-  tmp_buf = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
+  tmp_buf = grub_malloc (disk->sector_size << GRUB_DISK_CACHE_BITS);
   if (! tmp_buf)
     return grub_errno;
 
@@ -394,8 +394,8 @@
 
       /* For reading bulk data.  */
       start_sector = sector & ~(GRUB_DISK_CACHE_SIZE - 1);
-      pos = (sector - start_sector) << GRUB_DISK_SECTOR_BITS;
-      len = ((GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS)
+      pos = (sector - start_sector) * disk->sector_size;
+      len = ((disk->sector_size << GRUB_DISK_CACHE_BITS)
 	     - pos - real_offset);
       if (len > size)
 	len = size;
@@ -421,10 +421,10 @@
 
 	      grub_errno = GRUB_ERR_NONE;
 
-	      num = ((size + GRUB_DISK_SECTOR_SIZE - 1)
-		     >> GRUB_DISK_SECTOR_BITS);
+	      num = ((size + disk->sector_size - 1)
+		     / disk->sector_size);
 
-	      p = grub_realloc (tmp_buf, num << GRUB_DISK_SECTOR_BITS);
+	      p = grub_realloc (tmp_buf, num * disk->sector_size);
 	      if (!p)
 		goto finish;
 
@@ -445,11 +445,11 @@
 		while (size)
 		  {
 		    (disk->read_hook) (sector, real_offset,
-				       ((size > GRUB_DISK_SECTOR_SIZE)
-					? GRUB_DISK_SECTOR_SIZE
+				       ((size > disk->sector_size)
+					? disk->sector_size
 					: size));
 		    sector++;
-		    size -= GRUB_DISK_SECTOR_SIZE - real_offset;
+		    size -= disk->sector_size - real_offset;
 		    real_offset = 0;
 		  }
 
@@ -459,7 +459,7 @@
 
 	  /* Copy it and store it in the disk cache.  */
 	  grub_memcpy (buf, tmp_buf + pos + real_offset, len);
-	  grub_disk_cache_store (disk->dev->id, disk->id,
+	  grub_disk_cache_store (disk->dev->id, disk,
 				 start_sector, tmp_buf);
 	}
 
@@ -472,15 +472,15 @@
 	  while (l)
 	    {
 	      (disk->read_hook) (s, real_offset,
-				 ((l > GRUB_DISK_SECTOR_SIZE)
-				  ? GRUB_DISK_SECTOR_SIZE
+				 ((l > disk->sector_size)
+				  ? disk->sector_size
 				  : l));
 	      
-	      if (l < GRUB_DISK_SECTOR_SIZE - real_offset)
+	      if (l < disk->sector_size - real_offset)
 		break;
 	      
 	      s++;
-	      l -= GRUB_DISK_SECTOR_SIZE - real_offset;
+	      l -= disk->sector_size - real_offset;
 	      real_offset = 0;
 	    }
 	}
@@ -513,16 +513,16 @@
   
   while (size)
     {
-      if (real_offset != 0 || (size < GRUB_DISK_SECTOR_SIZE && size != 0))
+      if (real_offset != 0 || (size < disk->sector_size && size != 0))
 	{
-	  char tmp_buf[GRUB_DISK_SECTOR_SIZE];
+	  char tmp_buf[disk->sector_size];
 	  grub_size_t len;
 	  
-	  if (grub_disk_read (disk, sector, 0, GRUB_DISK_SECTOR_SIZE, tmp_buf)
+	  if (grub_disk_read (disk, sector, 0, disk->sector_size, tmp_buf)
 	      != GRUB_ERR_NONE)
 	    goto finish;
 
-	  len = GRUB_DISK_SECTOR_SIZE - real_offset;
+	  len = disk->sector_size - real_offset;
 	  if (len > size)
 	    len = size;
 	  
@@ -543,8 +543,8 @@
 	  grub_size_t len;
 	  grub_size_t n;
 
-	  len = size & ~(GRUB_DISK_SECTOR_SIZE - 1);
-	  n = size >> GRUB_DISK_SECTOR_BITS;
+	  len = size & ~(disk->sector_size - 1);
+	  n = size / disk->sector_size;
 	  
 	  if ((disk->dev->write) (disk, sector, n, buf) != GRUB_ERR_NONE)
 	    goto finish;
Index: fs/xfs.c
===================================================================
--- fs/xfs.c	(revision 1886)
+++ fs/xfs.c	(working copy)
@@ -188,6 +188,23 @@
 #define GRUB_XFS_NEXT_DIRENT(pos,len)		\
   (pos) + GRUB_XFS_ROUND_TO_DIRENT (8 + 1 + len + 2)
 \f
+static int
+xfs_log2 (unsigned x)
+{
+  int i;
+  
+  if (x == 0)
+    return -1;
+
+  for (i = 0; (x & 1) == 0; i++)
+    x >>= 1;
+
+  if (x != 1)
+    return -1;
+
+  return i;
+}
+
 static inline int
 grub_xfs_inode_block (struct grub_xfs_data *data,
 		      grub_uint64_t ino)
@@ -197,7 +214,7 @@
   long long block;
 
   block = (inoinag >> 4) + ag * data->agsize;
-  block <<= (data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS);
+  block <<= (data->sblock.log2_bsize - data->disk->sector_size);
   return block;
 }
 
@@ -267,8 +284,7 @@
 
           if (grub_disk_read (node->data->disk,
                               grub_be_to_cpu64 (keys[i - 1 + XFS_INODE_EXTENTS])
-                              << (node->data->sblock.log2_bsize
-                                  - GRUB_DISK_SECTOR_BITS),
+                              << (node->data->sblock.log2_bsize) / node->data->disk->sector_size,
                               0, node->data->sblock.bsize, (char *) leaf))
             return 0;
 
@@ -333,8 +349,7 @@
   return grub_fshelp_read_file (node->data->disk, node, read_hook,
 				pos, len, buf, grub_xfs_read_block,
 				grub_be_to_cpu64 (node->inode.size),
-				node->data->sblock.log2_bsize
-				- GRUB_DISK_SECTOR_BITS);
+				node->data->sblock.log2_bsize - xfs_log2 (node->data->disk->sector_size));
 }
 
 
Index: fs/afs.c
===================================================================
--- fs/afs.c	(revision 1886)
+++ fs/afs.c	(working copy)
@@ -179,6 +179,23 @@
 static grub_dl_t my_mod;
 #endif
 
+static int
+afs_log2 (unsigned x)
+{
+  int i;
+  
+  if (x == 0)
+    return -1;
+
+  for (i = 0; (x & 1) == 0; i++)
+    x >>= 1;
+
+  if (x != 1)
+    return -1;
+
+  return i;
+}
+
 static grub_afs_off_t
 grub_afs_run_to_num (struct grub_afs_sblock *sb,
                      struct grub_afs_blockrun *run)
@@ -193,7 +210,7 @@
 {
   return grub_disk_read (data->disk,
                          ino *
-                         (data->sblock.block_size >> GRUB_DISK_SECTOR_BITS),
+                         (data->sblock.block_size / data->disk->sector_size),
                          0, sizeof (struct grub_afs_inode),
                          (char *) inode);
 }
@@ -228,7 +245,7 @@
           int j;
 
           if (grub_disk_read (node->data->disk,
-                              blk * (sb->block_size >> GRUB_DISK_SECTOR_BITS),
+                              blk * (sb->block_size / node->data->disk->sector_size),
                               0, sizeof (indir),
                               (char *) indir))
             return 0;
@@ -264,14 +281,14 @@
       if (grub_disk_read (node->data->disk,
                           (grub_afs_run_to_num (sb, &ds->double_indirect)
                            + idblk) *
-                          (sb->block_size >> GRUB_DISK_SECTOR_BITS),
+                          (sb->block_size / node->data->disk->sector_size),
                           0, sizeof (indir),
                           (char *) indir))
         return 0;
 
       if (grub_disk_read (node->data->disk,
                           (grub_afs_run_to_num (sb, &indir[idptr]) + dblk) *
-                          (sb->block_size >> GRUB_DISK_SECTOR_BITS),
+                          (sb->block_size / node->data->disk->sector_size),
                           0, sizeof (indir),
                           (char *) indir))
         return 0;
@@ -293,7 +310,7 @@
                                 U64 (&node->data->sblock,
                                      node->inode.stream.size),
 				node->data->sblock.block_shift
-                                - GRUB_DISK_SECTOR_BITS);
+                                - afs_log2 (node->data->disk->sector_size));
 }
 
 static int
Index: fs/ntfs.c
===================================================================
--- fs/ntfs.c	(revision 1886)
+++ fs/ntfs.c	(working copy)
@@ -42,10 +42,10 @@
     return grub_error (GRUB_ERR_BAD_FS, "%s label not found", magic);
 
   ss = u16at (buf, 6) - 1;
-  if (ss * (int) data->blocksize != len * GRUB_DISK_SECTOR_SIZE)
+  if (ss * (int) data->blocksize != len * data->disk->sector_size)
     return grub_error (GRUB_ERR_BAD_FS, "Size not match",
 		       ss * (int) data->blocksize,
-		       len * GRUB_DISK_SECTOR_SIZE);
+		       len * data->disk->sector_size);
   pu = buf + u16at (buf, 4);
   us = u16at (pu, 0);
   buf -= 2;
@@ -178,8 +178,8 @@
 	{
           int n;
 
-          n = ((u32at (pa, 0x30) + GRUB_DISK_SECTOR_SIZE - 1)
-               & (~(GRUB_DISK_SECTOR_SIZE - 1)));
+          n = ((u32at (pa, 0x30) + at->mft->data->disk->sector_size - 1)
+               & (~(at->mft->data->disk->sector_size - 1)));
 	  at->attr_cur = at->attr_end;
 	  at->edat_buf = grub_malloc (n);
 	  if (!at->edat_buf)
Index: fs/fat.c
===================================================================
--- fs/fat.c	(revision 1886)
+++ fs/fat.c	(working copy)
@@ -188,11 +188,12 @@
     goto fail;
 
   /* Get the sizes of logical sectors and clusters.  */
-  data->logical_sector_bits =
-    fat_log2 (grub_le_to_cpu16 (bpb.bytes_per_sector));
-  if (data->logical_sector_bits < GRUB_DISK_SECTOR_BITS)
-    goto fail;
-  data->logical_sector_bits -= GRUB_DISK_SECTOR_BITS;
+//  data->logical_sector_bits =
+//    fat_log2 (grub_le_to_cpu16 (bpb.bytes_per_sector));
+//  if (data->logical_sector_bits < GRUB_DISK_SECTOR_BITS)
+//    goto fail;
+//  data->logical_sector_bits -= GRUB_DISK_SECTOR_BITS;
+  data->logical_sector_bits = fat_log2 (grub_le_to_cpu16 (bpb.bytes_per_sector / disk->sector_size) );
   
   data->cluster_bits = fat_log2 (bpb.sectors_per_cluster);
   if (data->cluster_bits < 0)
@@ -226,10 +227,10 @@
 
   data->root_sector = data->fat_sector + bpb.num_fats * data->sectors_per_fat;
   data->num_root_sectors
-    = ((((grub_uint32_t) grub_le_to_cpu16 (bpb.num_root_entries)
+    = (((((grub_uint32_t) grub_le_to_cpu16 (bpb.num_root_entries)
 	 * GRUB_FAT_DIR_ENTRY_SIZE
 	 + grub_le_to_cpu16 (bpb.bytes_per_sector) - 1)
-	>> (data->logical_sector_bits + GRUB_DISK_SECTOR_BITS))
+	>> (data->logical_sector_bits)) / disk->sector_size )
        << (data->logical_sector_bits));
 
   data->cluster_sector = data->root_sector + data->num_root_sectors;
@@ -353,7 +354,7 @@
      in clusters.  */
   if (data->file_cluster == ~0U)
     {
-      size = (data->num_root_sectors << GRUB_DISK_SECTOR_BITS) - offset;
+      size = (data->num_root_sectors * disk->sector_size) - offset;
       if (size > len)
 	size = len;
 
@@ -366,7 +367,7 @@
   /* Calculate the logical cluster number and offset.  */
   logical_cluster_bits = (data->cluster_bits
 			  + data->logical_sector_bits
-			  + GRUB_DISK_SECTOR_BITS);
+			  + fat_log2 (disk->sector_size));
   logical_cluster = offset >> logical_cluster_bits;
   offset &= (1 << logical_cluster_bits) - 1;
 
Index: fs/iso9660.c
===================================================================
--- fs/iso9660.c	(revision 1886)
+++ fs/iso9660.c	(working copy)
@@ -550,8 +550,8 @@
     {
       if (grub_disk_read (dir->data->disk,
 			  (dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
-			  + offset / GRUB_DISK_SECTOR_SIZE,
-			  offset % GRUB_DISK_SECTOR_SIZE,
+			  + offset / dir->data->disk->sector_size,
+			  offset % dir->data->disk->sector_size,
 			  sizeof (dirent), (char *) &dirent))
 	return 0;
       
@@ -580,16 +580,16 @@
 	    && grub_iso9660_susp_iterate (dir->data,
 					  (dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
 					  + (sua_off
-					     / GRUB_DISK_SECTOR_SIZE),
-					  sua_off % GRUB_DISK_SECTOR_SIZE,
+					     / dir->data->disk->sector_size),
+					  sua_off % dir->data->disk->sector_size,
 					  sua_size, susp_iterate_dir))
 	  return 0;
 	
 	/* Read the name.  */
 	if (grub_disk_read (dir->data->disk,
 			    (dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
-			    + nameoffset / GRUB_DISK_SECTOR_SIZE,
-			    nameoffset % GRUB_DISK_SECTOR_SIZE,
+			    + nameoffset / dir->data->disk->sector_size,
+			    nameoffset % dir->data->disk->sector_size,
 			    dirent.namelen, (char *) name))
 	  return 0;
 	
@@ -602,8 +602,8 @@
 	node->size = grub_le_to_cpu32 (dirent.size);
 	node->blk = grub_le_to_cpu32 (dirent.first_sector);
 	node->dir_blk = ((dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
-			 + offset / GRUB_DISK_SECTOR_SIZE);
-	node->dir_off = offset % GRUB_DISK_SECTOR_SIZE;
+			 + offset / dir->data->disk->sector_size);
+	node->dir_off = offset % dir->data->disk->sector_size;
 	
 	/* If the filetype was not stored using rockridge, use
 	   whatever is stored in the iso9660 filesystem.  */
Index: fs/affs.c
===================================================================
--- fs/affs.c	(revision 1886)
+++ fs/affs.c	(working copy)
@@ -124,7 +124,7 @@
   for (links = grub_divmod64 (fileblock, data->htsize, &mod); links; links--)
     {
       grub_disk_read (data->disk, block + data->blocksize - 1,
-		      data->blocksize * (GRUB_DISK_SECTOR_SIZE
+		      data->blocksize * (data->disk->sector_size
 					 - GRUB_AFFS_FILE_LOCATION),
 		      sizeof (file), (char *) &file);
       if (grub_errno)
@@ -203,7 +203,7 @@
 
   /* No sane person uses more than 8KB for a block.  At least I hope
      for that person because in that case this won't work.  */
-  rootblock = grub_malloc (GRUB_DISK_SECTOR_SIZE * 16);
+  rootblock = grub_malloc (disk->sector_size * 16);
   if (!rootblock)
     goto fail;
 
@@ -211,7 +211,7 @@
 
   /* Read the rootblock.  */
   grub_disk_read (disk, (disk->total_sectors >> 1) + blocksize, 0,
-		  GRUB_DISK_SECTOR_SIZE * 16, (char *) rootblock);
+		  disk->sector_size * 16, (char *) rootblock);
   if (grub_errno)
     goto fail;
 
@@ -222,10 +222,10 @@
   rblock->checksum = 0;
   for (blocksize = 0; blocksize < 8; blocksize++)
     {
-      grub_uint32_t *currblock = rootblock + GRUB_DISK_SECTOR_SIZE * blocksize;
+      grub_uint32_t *currblock = rootblock + disk->sector_size * blocksize;
       unsigned int i;
 
-      for (i = 0; i < GRUB_DISK_SECTOR_SIZE / sizeof (*currblock); i++)
+      for (i = 0; i < disk->sector_size / sizeof (*currblock); i++)
 	checksum += grub_be_to_cpu32 (currblock[i]);
 
       if (checksumr == -checksum)
@@ -350,7 +350,7 @@
       while (next)
 	{
 	  grub_disk_read (data->disk, next + data->blocksize - 1,
-			  data->blocksize * GRUB_DISK_SECTOR_SIZE
+			  data->blocksize * data->disk->sector_size
 			  - GRUB_AFFS_FILE_LOCATION,
 			  sizeof (file), (char *) &file);
 	  if (grub_errno)
@@ -524,7 +524,7 @@
       /* The rootblock maps quite well on a file header block, it's
 	 something we can use here.  */
       grub_disk_read (data->disk, disk->total_sectors >> 1,
-		      data->blocksize * (GRUB_DISK_SECTOR_SIZE
+		      data->blocksize * (data->disk->sector_size
 					 - GRUB_AFFS_FILE_LOCATION),
 		      sizeof (file), (char *) &file);
       if (grub_errno)
Index: fs/fshelp.c
===================================================================
--- fs/fshelp.c	(revision 1886)
+++ fs/fshelp.c	(working copy)
@@ -233,15 +233,15 @@
 		       grub_off_t filesize, int log2blocksize)
 {
   grub_disk_addr_t i, blockcnt;
-  int blocksize = 1 << (log2blocksize + GRUB_DISK_SECTOR_BITS);
+  int blocksize = (1 << log2blocksize) * disk->sector_size;
 
   /* Adjust LEN so it we can't read past the end of the file.  */
   if (pos + len > filesize)
     len = filesize - pos;
 
-  blockcnt = ((len + pos) + blocksize - 1) >> (log2blocksize + GRUB_DISK_SECTOR_BITS);
+  blockcnt = (((len + pos) + blocksize - 1) >> log2blocksize) / disk->sector_size;
 
-  for (i = pos >> (log2blocksize + GRUB_DISK_SECTOR_BITS); i < blockcnt; i++)
+  for (i = (pos >> log2blocksize) / disk->sector_size; i < blockcnt; i++)
     {
       grub_disk_addr_t blknr;
       int blockoff = pos & (blocksize - 1);
@@ -266,7 +266,7 @@
 	}
 
       /* First block.  */
-      if (i == (pos >> (log2blocksize + GRUB_DISK_SECTOR_BITS)))
+      if (i == ((pos >> log2blocksize) / disk->sector_size))
 	{
 	  skipfirst = blockoff;
 	  blockend -= skipfirst;
Index: fs/reiserfs.c
===================================================================
--- fs/reiserfs.c	(revision 1886)
+++ fs/reiserfs.c	(working copy)
@@ -509,9 +509,9 @@
   do
     {
       grub_disk_read (data->disk,
-                      block_number * (block_size >> GRUB_DISK_SECTOR_BITS),
+                      block_number * (block_size / data->disk->sector_size),
                       (((grub_off_t) block_number * block_size)
-                       & (GRUB_DISK_SECTOR_SIZE - 1)),
+                       & (data->disk->sector_size - 1)),
                       block_size, (char *) block_header);
       if (grub_errno)
         goto fail;
@@ -659,7 +659,7 @@
 
   block_size = grub_le_to_cpu16 (node->data->superblock.block_size);
   len = grub_le_to_cpu16 (found.header.item_size);
-  block = found.block_number * (block_size  >> GRUB_DISK_SECTOR_BITS);
+  block = found.block_number * (block_size  / node->data->disk->sector_size);
   offset = grub_le_to_cpu16 (found.header.item_location);
 
   symlink_buffer = grub_malloc (len + 1);
@@ -686,7 +686,7 @@
   data = grub_malloc (sizeof (*data));
   if (! data)
     goto fail;
-  grub_disk_read (disk, REISERFS_SUPER_BLOCK_OFFSET / GRUB_DISK_SECTOR_SIZE,
+  grub_disk_read (disk, REISERFS_SUPER_BLOCK_OFFSET / disk->sector_size,
                   0, sizeof (data->superblock), (char *) &(data->superblock));
   if (grub_errno)
     goto fail;
@@ -744,9 +744,9 @@
       struct grub_reiserfs_item_header *item_headers;
       
       grub_disk_read (data->disk,
-                      block_number * (block_size >> GRUB_DISK_SECTOR_BITS),
+                      block_number * (block_size / data->disk->sector_size),
                       (((grub_off_t) block_number * block_size)
-                       & (GRUB_DISK_SECTOR_SIZE - 1)),
+                       & (data->disk->sector_size - 1)),
                       block_size, (char *) block_header);
       if (grub_errno)
         goto fail;
@@ -838,7 +838,7 @@
                         {
                           struct grub_reiserfs_stat_item_v1 entry_v1_stat;
                           grub_disk_read (data->disk,
-                                          entry_block_number * (block_size >> GRUB_DISK_SECTOR_BITS),
+                                          entry_block_number * (block_size / data->disk->sector_size),
                                           grub_le_to_cpu16 (entry_item->header.item_location),
                                           sizeof (entry_v1_stat),
                                           (char *) &entry_v1_stat);
@@ -880,7 +880,7 @@
                         {
                           struct grub_reiserfs_stat_item_v2 entry_v2_stat;
                           grub_disk_read (data->disk,
-                                          entry_block_number * (block_size >> GRUB_DISK_SECTOR_BITS),
+                                          entry_block_number * (block_size / data->disk->sector_size),
                                           grub_le_to_cpu16 (entry_item->header.item_location),
                                           sizeof (entry_v2_stat),
                                           (char *) &entry_v2_stat);
@@ -1028,10 +1028,10 @@
     {
       struct grub_reiserfs_stat_item_v1 entry_v1_stat;
       grub_disk_read (data->disk,
-                      block_number * (block_size >> GRUB_DISK_SECTOR_BITS),
+                      block_number * (block_size / data->disk->sector_size),
                       entry_location
                       + (((grub_off_t) block_number * block_size)
-                         & (GRUB_DISK_SECTOR_SIZE - 1)),
+                         & (data->disk->sector_size - 1)),
                       sizeof (entry_v1_stat), (char *) &entry_v1_stat);
       if (grub_errno)
         goto fail;
@@ -1041,10 +1041,10 @@
     {
       struct grub_reiserfs_stat_item_v2 entry_v2_stat;
       grub_disk_read (data->disk,
-                      block_number * (block_size  >> GRUB_DISK_SECTOR_BITS),
+                      block_number * (block_size  / data->disk->sector_size),
                       entry_location
                       + (((grub_off_t) block_number * block_size)
-                         & (GRUB_DISK_SECTOR_SIZE - 1)),
+                         & (data->disk->sector_size - 1)),
                       sizeof (entry_v2_stat), (char *) &entry_v2_stat);
       if (grub_errno)
         goto fail;
@@ -1111,7 +1111,7 @@
       switch (found.type)
         {
         case GRUB_REISERFS_DIRECT:
-          block = found.block_number * (block_size  >> GRUB_DISK_SECTOR_BITS);
+          block = found.block_number * (block_size  / data->disk->sector_size);
           grub_dprintf ("reiserfs_blocktype", "D: %u\n", (unsigned) block);
           if (initial_position < current_position + item_size)
             {
@@ -1143,7 +1143,7 @@
           if (! indirect_block_ptr)
             goto fail;
           grub_disk_read (found.data->disk,
-                          found.block_number * (block_size >> GRUB_DISK_SECTOR_BITS),
+                          found.block_number * (block_size / found.data->disk->sector_size),
                           grub_le_to_cpu16 (found.header.item_location),
                           item_size, (char *) indirect_block_ptr);
           if (grub_errno)
@@ -1155,7 +1155,7 @@
                indirect_block++)
             {
               block = grub_le_to_cpu32 (indirect_block_ptr[indirect_block]) *
-                      (block_size >> GRUB_DISK_SECTOR_BITS);
+                      (block_size / found.data->disk->sector_size);
               grub_dprintf ("reiserfs_blocktype", "I: %u\n", (unsigned) block);
               if (current_position + block_size >= initial_position)
                 {
@@ -1334,7 +1334,7 @@
   if (*label)
     {
       grub_disk_read (device->disk,
-                      REISERFS_SUPER_BLOCK_OFFSET / GRUB_DISK_SECTOR_SIZE,
+                      REISERFS_SUPER_BLOCK_OFFSET / device->disk->sector_size,
                       REISERFS_LABEL_OFFSET, REISERFS_MAX_LABEL_LENGTH,
                       *label);
     }
Index: fs/jfs.c
===================================================================
--- fs/jfs.c	(revision 1886)
+++ fs/jfs.c	(working copy)
@@ -277,8 +277,7 @@
 	  	  
 	  if (grub_disk_read (data->disk,
 			      grub_le_to_cpu32 (extents[found].extent.blk2)
-			      << (grub_le_to_cpu16 (data->sblock.log2_blksz)
-				  - GRUB_DISK_SECTOR_BITS), 0,
+			      << (grub_le_to_cpu16 (data->sblock.log2_blksz)) * data->disk->sector_size, 0,
 			      sizeof (tree), (char *) &tree))
 	    return -1;
 	  
@@ -309,14 +308,14 @@
 
   /* Read in the IAG.  */
   if (grub_disk_read (data->disk,
-		      iagblk << (grub_le_to_cpu16 (data->sblock.log2_blksz)
-				 - GRUB_DISK_SECTOR_BITS), 0,
+		      iagblk << (grub_le_to_cpu16 (data->sblock.log2_blksz)) * data->disk->sector_size, 0,
 		      sizeof (struct grub_jfs_iag), (char *) &iag))
     return grub_errno;
   
   inoblk = grub_le_to_cpu32 (iag.inodes[inoext].blk2);
-  inoblk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz)
-	      - GRUB_DISK_SECTOR_BITS);
+  inoblk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz));
+  inoblk /= data->disk->sector_size;
+
   inoblk += inonum;
   
   if (grub_disk_read (data->disk, inoblk, 0,
@@ -412,7 +411,8 @@
     }
   
   blk = grub_le_to_cpu32 (de[inode->dir.header.sorted[0]].ex.blk2);
-  blk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS);
+  blk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz));
+  blk /= data->disk->sector_size;
   
   /* Read in the nodes until we are on the leaf node level.  */
   do
@@ -430,8 +430,7 @@
       de = (struct grub_jfs_internal_dirent *) diro->dirpage->dirent;
       index = diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
       blk = (grub_le_to_cpu32 (de[index].ex.blk2)
-	     << (grub_le_to_cpu16 (data->sblock.log2_blksz)
-		 - GRUB_DISK_SECTOR_BITS));
+	     << (grub_le_to_cpu16 (data->sblock.log2_blksz)) * data->disk->sector_size);
     } while (!(diro->dirpage->header.flags & GRUB_JFS_TREE_LEAF));
 
   diro->leaf = diro->dirpage->dirent;
@@ -485,8 +484,8 @@
 	return GRUB_ERR_OUT_OF_RANGE;
       
       next = grub_le_to_cpu64 (diro->dirpage->header.nextb);
-      next <<= (grub_le_to_cpu16 (diro->data->sblock.log2_blksz)
-		- GRUB_DISK_SECTOR_BITS);
+      next <<= (grub_le_to_cpu16 (diro->data->sblock.log2_blksz));
+      next /= diro->data->disk->sector_size;
       
       if (grub_disk_read (diro->data->disk, next, 0,
 			  grub_le_to_cpu32 (diro->data->sblock.blksz),
@@ -583,8 +582,7 @@
       
       data->disk->read_hook = read_hook;
       grub_disk_read (data->disk,
-		      blknr << (grub_le_to_cpu16 (data->sblock.log2_blksz)
-				- GRUB_DISK_SECTOR_BITS),
+		      blknr << (grub_le_to_cpu16 (data->sblock.log2_blksz)) * data->disk->sector_size,
 		      skipfirst, blockend, buf);
       
       data->disk->read_hook = 0;
Index: fs/minix.c
===================================================================
--- fs/minix.c	(revision 1886)
+++ fs/minix.c	(working copy)
@@ -263,8 +263,8 @@
   
   if (data->version == 1)
     {
-      block += ino / (GRUB_DISK_SECTOR_SIZE / sizeof (struct grub_minix_inode));
-      int offs = (ino % (GRUB_DISK_SECTOR_SIZE 
+      block += ino / (data->disk->sector_size / sizeof (struct grub_minix_inode));
+      int offs = (ino % (data->disk->sector_size 
 			 / sizeof (struct grub_minix_inode))
 		  * sizeof (struct grub_minix_inode));
       
@@ -273,10 +273,10 @@
     }
   else
     {
-      block += ino / (GRUB_DISK_SECTOR_SIZE 
+      block += ino / (data->disk->sector_size 
 		      / sizeof (struct grub_minix2_inode));
       int offs = (ino 
-		  % (GRUB_DISK_SECTOR_SIZE / sizeof (struct grub_minix2_inode))
+		  % (data->disk->sector_size / sizeof (struct grub_minix2_inode))
 		  * sizeof (struct grub_minix2_inode));
       
       grub_disk_read (data->disk, block, offs,
Index: fs/hfsplus.c
===================================================================
--- fs/hfsplus.c	(revision 1886)
+++ fs/hfsplus.c	(working copy)
@@ -225,6 +225,23 @@
 #endif
 
 \f
+static int
+hfsplus_log2 (unsigned x)
+{
+  int i;
+  
+  if (x == 0)
+    return -1;
+
+  for (i = 0; (x & 1) == 0; i++)
+    x >>= 1;
+
+  if (x != 1)
+    return -1;
+
+  return i;
+}
+
 /* Return the offset of the record with the index INDEX, in the node
    NODE which is part of the B+ tree BTREE.  */
 static inline unsigned int
@@ -310,7 +327,7 @@
       if (blk != -1)
 	return (blk
 		+ (node->data->embedded_offset >> (node->data->log2blksize
-						   - GRUB_DISK_SECTOR_BITS)));
+						   - hfsplus_log2 (node->data->disk->sector_size))));
 
       /* For the extent overflow file, extra extents can't be found in
 	 the extent overflow file.  If this happens, you found a
@@ -363,7 +380,7 @@
   return grub_fshelp_read_file (node->data->disk, node, read_hook,
 				pos, len, buf, grub_hfsplus_read_block,
 				node->size,
-				node->data->log2blksize - GRUB_DISK_SECTOR_BITS);
+				node->data->log2blksize - hfsplus_log2 (node->data->disk->sector_size));
 }
 
 static struct grub_hfsplus_data *
@@ -409,7 +426,7 @@
       ablk_start = grub_be_to_cpu16 (volheader.hfs.first_block);
       data->embedded_offset = (ablk_start
 			       + extent_start
-			       * (ablk_size >> GRUB_DISK_SECTOR_BITS));
+			       * (ablk_size / data->disk->sector_size));
 
       grub_disk_read (disk, data->embedded_offset + GRUB_HFSPLUS_SBLOCK, 0,
 		      sizeof (volheader), (char *) &volheader);
Index: fs/cpio.c
===================================================================
--- fs/cpio.c	(revision 1886)
+++ fs/cpio.c	(working copy)
@@ -145,9 +145,9 @@
 	return grub_errno;
 
       data->size = grub_strtoul (hd.size, NULL, 8);
-      data->dofs = data->hofs + GRUB_DISK_SECTOR_SIZE;
-      *ofs = data->dofs + ((data->size + GRUB_DISK_SECTOR_SIZE - 1) &
-			   ~(GRUB_DISK_SECTOR_SIZE - 1));
+      data->dofs = data->hofs + data->disk->sector_size;
+      *ofs = data->dofs + ((data->size + data->disk->sector_size - 1) &
+			   ~(data->disk->sector_size - 1));
     }
   return GRUB_ERR_NONE;
 }
Index: include/grub/ntfs.h
===================================================================
--- include/grub/ntfs.h	(revision 1886)
+++ include/grub/ntfs.h	(working copy)
@@ -67,7 +67,8 @@
 #define FLAG_ENCRYPTED		0x4000
 #define FLAG_SPARSE		0x8000
 
-#define BLK_SHR		GRUB_DISK_SECTOR_BITS
+// TODO: This is quite bad, was GRUB_DISK_SECTOR_BITS (whch in turn was 9 for 512 Byte/Sector disks)
+#define BLK_SHR		9
 
 #define MAX_MFT		(1024 >> BLK_SHR)
 #define MAX_IDX		(16384 >> BLK_SHR)
Index: include/grub/disk.h
===================================================================
--- include/grub/disk.h	(revision 1886)
+++ include/grub/disk.h	(working copy)
@@ -94,6 +94,9 @@
   /* The underlying disk device.  */
   grub_disk_dev_t dev;
 
+  /* The size of each sector. */
+  grub_uint32_t sector_size;
+
   /* The total number of sectors.  */
   grub_uint64_t total_sectors;
 
@@ -126,8 +129,8 @@
 #endif
 
 /* The sector size.  */
-#define GRUB_DISK_SECTOR_SIZE	0x200
-#define GRUB_DISK_SECTOR_BITS	9
+#define GRUB_DISK_SECTOR_SIZE	(disk->sector_size)
+// #define GRUB_DISK_SECTOR_BITS	9
 
 /* The maximum number of disk caches.  */
 #define GRUB_DISK_CACHE_NUM	1021
Index: commands/cat.c
===================================================================
--- commands/cat.c	(revision 1886)
+++ commands/cat.c	(working copy)
@@ -31,8 +31,9 @@
 	      int argc, char **args)
 
 {
+#define GRUB_CMD_CAT_CHUNK_SIZE 512
   grub_file_t file;
-  char buf[GRUB_DISK_SECTOR_SIZE];
+  char buf[GRUB_CMD_CAT_CHUNK_SIZE];
   grub_ssize_t size;
   int key = 0;
 
Index: commands/blocklist.c
===================================================================
--- commands/blocklist.c	(revision 1886)
+++ commands/blocklist.c	(working copy)
@@ -31,7 +31,7 @@
 		    int argc, char **args)
 {
   grub_file_t file;
-  char buf[GRUB_DISK_SECTOR_SIZE];
+  char *buf;
   unsigned long start_sector = 0;
   unsigned num_sectors = 0;
   int num_entries = 0;
@@ -47,7 +47,7 @@
       if (num_sectors > 0)
 	{
 	  if (start_sector + num_sectors == sector
-	      && offset == 0 && length == GRUB_DISK_SECTOR_SIZE)
+	      && offset == 0 && length == file->device->disk->sector_size)
 	    {
 	      num_sectors++;
 	      return;
@@ -57,7 +57,7 @@
 	  num_sectors = 0;
 	}
 
-      if (offset == 0 && length == GRUB_DISK_SECTOR_SIZE)
+      if (offset == 0 && length == file->device->disk->sector_size)
 	{
 	  start_sector = sector;
 	  num_sectors++;
@@ -90,6 +90,8 @@
     return grub_error (GRUB_ERR_BAD_DEVICE,
 		       "this command is available only for disk devices.");
 
+  buf = grub_malloc (file->device->disk->sector_size);
+
   if (file->device->disk->partition)
     part_start = grub_partition_get_start (file->device->disk->partition);
   
@@ -103,6 +105,7 @@
   
   grub_file_close (file);
 
+  grub_free (buf);
   return grub_errno;
 }
 
Index: loader/i386/pc/linux.c
===================================================================
--- loader/i386/pc/linux.c	(revision 1886)
+++ loader/i386/pc/linux.c	(working copy)
@@ -143,8 +143,8 @@
   if (! setup_sects)
     setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
   
-  real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
-  prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE;
+  real_size = setup_sects * file->device->disk->sector_size;
+  prot_size = grub_file_size (file) - real_size - file->device->disk->sector_size;
   
   grub_linux_tmp_addr = (char *) GRUB_LINUX_BZIMAGE_ADDR + prot_size;
 
@@ -229,7 +229,7 @@
   /* Put the real mode code at the temporary address.  */
   grub_memmove (grub_linux_tmp_addr, &lh, sizeof (lh));
 
-  len = real_size + GRUB_DISK_SECTOR_SIZE - sizeof (lh);
+  len = real_size + file->device->disk->sector_size - sizeof (lh);
   if (grub_file_read (file, grub_linux_tmp_addr + sizeof (lh), len) != len)
     {
       grub_error (GRUB_ERR_FILE_READ_ERROR, "Couldn't read file");
@@ -240,10 +240,10 @@
       || grub_le_to_cpu16 (lh.version) < 0x0200)
     /* Clear the heap space.  */
     grub_memset (grub_linux_tmp_addr
-		 + ((setup_sects + 1) << GRUB_DISK_SECTOR_BITS),
+		 + ((setup_sects + 1) * file->device->disk->sector_size),
 		 0,
 		 ((GRUB_LINUX_MAX_SETUP_SECTS - setup_sects - 1)
-		  << GRUB_DISK_SECTOR_BITS));
+		  * file->device->disk->sector_size));
 
   /* Specify the boot file.  */
   dest = grub_stpcpy (grub_linux_tmp_addr + GRUB_LINUX_CL_OFFSET,
Index: loader/i386/pc/chainloader.c
===================================================================
--- loader/i386/pc/chainloader.c	(revision 1886)
+++ loader/i386/pc/chainloader.c	(working copy)
@@ -63,32 +63,9 @@
 
   grub_dl_ref (my_mod);
   
-  file = grub_file_open (filename);
-  if (! file)
-    goto fail;
 
-  /* Read the first block.  */
-  if (grub_file_read (file, (char *) 0x7C00, GRUB_DISK_SECTOR_SIZE)
-      != GRUB_DISK_SECTOR_SIZE)
-    {
-      if (grub_errno == GRUB_ERR_NONE)
-	grub_error (GRUB_ERR_BAD_OS, "too small");
-
-      goto fail;
-    }
-
-  /* Check the signature.  */
-  signature = *((grub_uint16_t *) (0x7C00 + GRUB_DISK_SECTOR_SIZE - 2));
-  if (signature != grub_le_to_cpu16 (0xaa55)
-      && ! (flags & GRUB_CHAINLOADER_FORCE))
-    {
-      grub_error (GRUB_ERR_BAD_OS, "invalid signature");
-      goto fail;
-    }
-
-  grub_file_close (file);
-
-  /* Obtain the partition table from the root device.  */
+  /* Obtain the partition table from the root device. we have to open the 
+    device in order to get the sector size */
   dev = grub_device_open (0);
   if (dev)
     {
@@ -96,6 +73,35 @@
       
       if (disk)
 	{
+	  file = grub_file_open (filename);
+	  if (! file)
+	    {
+	      goto fail;
+	      grub_device_close (dev);
+	    }
+
+	  /* Read the first block.  */
+	  if (grub_file_read (file, (char *) 0x7C00, disk->sector_size)
+	      != disk->sector_size)
+	    {
+	      if (grub_errno == GRUB_ERR_NONE)
+		grub_error (GRUB_ERR_BAD_OS, "too small");
+	      grub_device_close (dev);
+	      goto fail;
+	    }
+
+	  /* Check the signature.  */
+	  signature = *((grub_uint16_t *) (0x7C00 + disk->sector_size - 2));
+	  if (signature != grub_le_to_cpu16 (0xaa55)
+	      && ! (flags & GRUB_CHAINLOADER_FORCE))
+	    {
+	      grub_error (GRUB_ERR_BAD_OS, "invalid signature");
+	      grub_device_close (dev);
+	      goto fail;
+	    }
+
+	  grub_file_close (file);
+
 	  grub_partition_t p = disk->partition;
 	  
 	  /* In i386-pc, the id is equal to the BIOS drive number.  */
Index: util/grub-probe.c
===================================================================
--- util/grub-probe.c	(revision 1886)
+++ util/grub-probe.c	(working copy)
@@ -112,18 +112,26 @@
   int abstraction_type;
   grub_device_t dev = NULL;
   grub_fs_t fs;
+
+  grub_util_info ("starting probe...");
   
   if (path == NULL)
     {
       if (! grub_util_check_block_device (device_name))
+        grub_util_info ("2");
         grub_util_error ("%s is not a block device.\n", device_name);
     }
-  else
+  else {
+    grub_util_info ("3");
     device_name = grub_guess_root_device (path);
+  }
+  grub_util_info ("1");
 
   if (! device_name)
     grub_util_error ("cannot find a device for %s.\n", path);
 
+  grub_util_info ("2");
+
   if (print == PRINT_DEVICE)
     {
       printf ("%s\n", device_name);
@@ -165,7 +173,7 @@
   grub_util_info ("opening %s", drive_name);
   dev = grub_device_open (drive_name);
   if (! dev)
-    grub_util_error ("%s", grub_errmsg);
+    grub_util_error ("grub_device_open failed: %s", grub_errmsg);
 
   if (print == PRINT_PARTMAP)
     {
@@ -358,22 +366,32 @@
     }
 
   argument = argv[optind];
+
+  grub_util_info ("arguments parsed.");
   
   /* Initialize the emulated biosdisk driver.  */
   grub_util_biosdisk_init (dev_map ? : DEFAULT_DEVICE_MAP);
+
+  grub_util_info ("biosdisk initialized");
   
   /* Initialize all modules. */
   grub_init_all ();
 
+  grub_util_info ("all modules initialized");
+
   /* Do it.  */
   if (argument_is_device)
     probe (NULL, argument);
   else
     probe (argument, NULL);
+
+  grub_util_info ("probe executed");
   
   /* Free resources.  */
   grub_fini_all ();
   grub_util_biosdisk_fini ();
+
+  grub_util_info ("all modules deinitialized");
   
   free (dev_map);
   
Index: util/i386/pc/grub-mkimage.c
===================================================================
--- util/i386/pc/grub-mkimage.c	(revision 1886)
+++ util/i386/pc/grub-mkimage.c	(working copy)
@@ -129,7 +129,7 @@
 #endif
 
 static void
-generate_image (const char *dir, char *prefix, FILE *out, char *mods[], char *memdisk_path)
+generate_image (const char *dir, char *prefix, FILE *out, char *mods[], char *memdisk_path, grub_uint32_t target_sector_size)
 {
   grub_addr_t module_addr = 0;
   char *kernel_img, *boot_img, *core_img;
@@ -208,19 +208,19 @@
 
   grub_util_info ("the core size is 0x%x", core_size);
   
-  num = ((core_size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS);
+  num = ((core_size + target_sector_size - 1) / target_sector_size);
   if (num > 0xffff)
     grub_util_error ("the core image is too big");
 
   boot_path = grub_util_get_path (dir, "diskboot.img");
   boot_size = grub_util_get_image_size (boot_path);
-  if (boot_size != GRUB_DISK_SECTOR_SIZE)
+  if (boot_size != target_sector_size)
     grub_util_error ("diskboot.img is not one sector size");
   
   boot_img = grub_util_read_image (boot_path);
   
   /* i386 is a little endian architecture.  */
-  *((grub_uint16_t *) (boot_img + GRUB_DISK_SECTOR_SIZE
+  *((grub_uint16_t *) (boot_img + target_sector_size
 		       - GRUB_BOOT_MACHINE_LIST_SIZE + 8))
     = grub_cpu_to_le16 (num);
 
@@ -229,7 +229,7 @@
   free (boot_path);
   
   module_addr = (path_list
-		 ? (GRUB_BOOT_MACHINE_KERNEL_ADDR + GRUB_DISK_SECTOR_SIZE
+		 ? (GRUB_BOOT_MACHINE_KERNEL_ADDR + target_sector_size
 		    + kernel_size)
 		 : 0);
 
@@ -280,6 +280,7 @@
     {"help", no_argument, 0, 'h'},
     {"version", no_argument, 0, 'V'},
     {"verbose", no_argument, 0, 'v'},
+    {"sector-size", required_argument, 0, 's'},
     {0, 0, 0, 0}
   };
 
@@ -301,6 +302,7 @@
   -h, --help              display this message and exit\n\
   -V, --version           print version information and exit\n\
   -v, --verbose           print verbose messages\n\
+  -s, --sector-size=NR    set sector size to NR bytes\n\
 \n\
 Report bugs to <%s>.\n\
 ", GRUB_LIBDIR, DEFAULT_DIRECTORY, PACKAGE_BUGREPORT);
@@ -316,6 +318,7 @@
   char *prefix = NULL;
   char *memdisk = NULL;
   FILE *fp = stdout;
+  grub_uint32_t target_sector_size = 0;
 
   progname = "grub-mkimage";
   
@@ -372,6 +375,10 @@
 	  case 'v':
 	    verbosity++;
 	    break;
+   
+	  case 's':
+	    target_sector_size = atoi (optarg);
+	    break;
 
 	  default:
 	    usage (1);
@@ -386,8 +393,15 @@
 	grub_util_error ("cannot open %s", output);
     }
 
-  generate_image (dir ? : GRUB_LIBDIR, prefix ? : DEFAULT_DIRECTORY, fp, argv + optind, memdisk);
+    if (target_sector_size == 0)
+    {
+      printf ("invalid sector size given!");
+      usage (1);
+    }
+      
 
+  generate_image (dir ? : GRUB_LIBDIR, prefix ? : DEFAULT_DIRECTORY, fp, argv + optind, memdisk, target_sector_size);
+
   fclose (fp);
 
   if (dir)
Index: util/i386/pc/grub-setup.c
===================================================================
--- util/i386/pc/grub-setup.c	(revision 1886)
+++ util/i386/pc/grub-setup.c	(working copy)
@@ -105,9 +105,8 @@
   char *tmp_img;
   int i;
   grub_disk_addr_t first_sector;
-  grub_uint16_t current_segment
-    = GRUB_BOOT_MACHINE_KERNEL_SEG + (GRUB_DISK_SECTOR_SIZE >> 4);
-  grub_uint16_t last_length = GRUB_DISK_SECTOR_SIZE;
+  grub_uint16_t current_segment;
+  grub_uint16_t last_length = 0;
   grub_file_t file;
   FILE *fp;
   struct { grub_uint64_t start; grub_uint64_t end; } embed_region;
@@ -160,7 +159,7 @@
       grub_util_info ("the first sector is <%llu,%u,%u>",
 		      sector, offset, length);
       
-      if (offset != 0 || length != GRUB_DISK_SECTOR_SIZE)
+      if (offset != 0 || length != dest_dev->disk->sector_size)
 	grub_util_error ("The first sector of the core file is not sector-aligned");
 
       first_sector = sector;
@@ -174,7 +173,7 @@
       grub_util_info ("saving <%llu,%u,%u> with the segment 0x%x",
 		      sector, offset, length, (unsigned) current_segment);
       
-      if (offset != 0 || last_length != GRUB_DISK_SECTOR_SIZE)
+      if (offset != 0 || last_length != dest_dev->disk->sector_size)
 	grub_util_error ("Non-sector-aligned data is found in the core file");
 
       if (block != first_block
@@ -193,15 +192,25 @@
 	}
       
       last_length = length;
-      current_segment += GRUB_DISK_SECTOR_SIZE >> 4;
+      current_segment += dest_dev->disk->sector_size >> 4;
     }
-  
+
+  /* Open the destination device and read sector size (automagically done by grub_device_open)*/
+  dest_dev = grub_device_open (dest);
+  if (! dest_dev)
+    grub_util_error ("%s", grub_errmsg);
+
+  /* initialize sector-size dependant variables */
+  last_length = dest_dev->disk->sector_size;
+  current_segment = GRUB_BOOT_MACHINE_KERNEL_SEG + (dest_dev->disk->sector_size >> 4);
+
   /* Read the boot image by the OS service.  */
   boot_path = grub_util_get_path (dir, boot_file);
   boot_size = grub_util_get_image_size (boot_path);
-  if (boot_size != GRUB_DISK_SECTOR_SIZE)
-    grub_util_error ("The size of `%s' is not %d",
-		     boot_path, GRUB_DISK_SECTOR_SIZE);
+  if (boot_size != dest_dev->disk->sector_size)
+    grub_util_error ("The size of `%s' is not %d, you should adapt the image sector size to the\
+                      sector size of the target block device.",
+		     boot_path, dest_dev->disk->sector_size);
   boot_img = grub_util_read_image (boot_path);
   free (boot_path);
 
@@ -215,23 +224,23 @@
   
   core_path = grub_util_get_path (dir, core_file);
   core_size = grub_util_get_image_size (core_path);
-  core_sectors = ((core_size + GRUB_DISK_SECTOR_SIZE - 1)
-		  >> GRUB_DISK_SECTOR_BITS);
-  if (core_size < GRUB_DISK_SECTOR_SIZE)
+  core_sectors = ((core_size + dest_dev->disk->sector_size - 1)
+		  / dest_dev->disk->sector_size);
+  if (core_size < dest_dev->disk->sector_size)
     grub_util_error ("The size of `%s' is too small", core_path);
-  else if (core_size > 0xFFFF * GRUB_DISK_SECTOR_SIZE)
+  else if (core_size > 0xFFFF * dest_dev->disk->sector_size)
     grub_util_error ("The size of `%s' is too large", core_path);
   
   core_img = grub_util_read_image (core_path);
 
   /* Have FIRST_BLOCK to point to the first blocklist.  */
   first_block = (struct boot_blocklist *) (core_img
-					   + GRUB_DISK_SECTOR_SIZE
+					   + dest_dev->disk->sector_size
 					   - sizeof (*block));
 
-  install_dos_part = (grub_int32_t *) (core_img + GRUB_DISK_SECTOR_SIZE
+  install_dos_part = (grub_int32_t *) (core_img + dest_dev->disk->sector_size
 				       + GRUB_KERNEL_MACHINE_INSTALL_DOS_PART);
-  install_bsd_part = (grub_int32_t *) (core_img + GRUB_DISK_SECTOR_SIZE
+  install_bsd_part = (grub_int32_t *) (core_img + dest_dev->disk->sector_size
 				       + GRUB_KERNEL_MACHINE_INSTALL_BSD_PART);
 
   /* Open the root device and the destination device.  */
@@ -239,17 +248,13 @@
   if (! root_dev)
     grub_util_error ("%s", grub_errmsg);
 
-  dest_dev = grub_device_open (dest);
-  if (! dest_dev)
-    grub_util_error ("%s", grub_errmsg);
-
   grub_util_info ("setting the root device to `%s'", root);
   if (grub_env_set ("root", root) != GRUB_ERR_NONE)
     grub_util_error ("%s", grub_errmsg);
 
   /* Read the original sector from the disk.  */
-  tmp_img = xmalloc (GRUB_DISK_SECTOR_SIZE);
-  if (grub_disk_read (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, tmp_img))
+  tmp_img = xmalloc (dest_dev->disk->sector_size);
+  if (grub_disk_read (dest_dev->disk, 0, 0, dest_dev->disk->sector_size, tmp_img))
     grub_util_error ("%s", grub_errmsg);
 
   /* Copy the possible DOS BPB.  */
@@ -327,7 +332,7 @@
 	  first_block->len = grub_cpu_to_le16 (core_sectors - 1);
 	  first_block->segment
 	    = grub_cpu_to_le16 (GRUB_BOOT_MACHINE_KERNEL_SEG
-				+ (GRUB_DISK_SECTOR_SIZE >> 4));
+				+ (dest_dev->disk->sector_size >> 4));
 
 	  /* Make sure that the second blocklist is a terminator.  */
 	  block = first_block - 1;
@@ -346,7 +351,7 @@
 	  *kernel_sector = grub_cpu_to_le64 (embed_region.start);
 
 	  /* Write the boot image onto the disk.  */
-	  if (grub_disk_write (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE,
+	  if (grub_disk_write (dest_dev->disk, 0, 0, dest_dev->disk->sector_size,
 			       boot_img))
 	    grub_util_error ("%s", grub_errmsg);
 
@@ -457,14 +462,14 @@
     grub_util_error ("%s", grub_errmsg);
   
   file->read_hook = save_first_sector;
-  if (grub_file_read (file, tmp_img, GRUB_DISK_SECTOR_SIZE)
-      != GRUB_DISK_SECTOR_SIZE)
+  if (grub_file_read (file, tmp_img, dest_dev->disk->sector_size)
+      != dest_dev->disk->sector_size)
     grub_util_error ("Failed to read the first sector of the core image");
 
   block = first_block;
   file->read_hook = save_blocklists;
-  if (grub_file_read (file, tmp_img, core_size - GRUB_DISK_SECTOR_SIZE)
-      != (grub_ssize_t) core_size - GRUB_DISK_SECTOR_SIZE)
+  if (grub_file_read (file, tmp_img, core_size - dest_dev->disk->sector_size)
+      != (grub_ssize_t) core_size - dest_dev->disk->sector_size)
     grub_util_error ("Failed to read the rest sectors of the core image");
 
   grub_file_close (file);
@@ -487,11 +492,11 @@
   if (! fp)
     grub_util_error ("Cannot open `%s'", core_path);
 
-  grub_util_write_image (core_img, GRUB_DISK_SECTOR_SIZE * 2, fp);
+  grub_util_write_image (core_img, dest_dev->disk->sector_size * 2, fp);
   fclose (fp);
 
   /* Write the boot image onto the disk.  */
-  if (grub_disk_write (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, boot_img))
+  if (grub_disk_write (dest_dev->disk, 0, 0, dest_dev->disk->sector_size, boot_img))
     grub_util_error ("%s", grub_errmsg);
 
  finish:
Index: util/grub-mkdevicemap.c
===================================================================
--- util/grub-mkdevicemap.c	(revision 1886)
+++ util/grub-mkdevicemap.c	(working copy)
@@ -70,6 +70,9 @@
 # ifndef BLKGETSIZE
 #  define BLKGETSIZE	_IO(0x12,96)	/* return device size */
 # endif /* ! BLKGETSIZE */
+# ifndef BLKSSZGET
+#  define BLKSSZGET  _IO(0x12,104)/* get block device sector size */
+#endif
 #endif /* __linux__ */
 
 /* Use __FreeBSD_kernel__ instead of __FreeBSD__ for compatibility with
@@ -328,7 +331,7 @@
 static int
 check_device (const char *device)
 {
-  char buf[512];
+  char *buf;
   FILE *fp;
 
   /* If DEVICE is empty, just return error.  */
@@ -393,12 +396,20 @@
 #endif /* __FreeBSD_kernel__ || __NetBSD__ || __OpenBSD__ */
   
   /* Attempt to read the first sector.  */
-  if (fread (buf, 1, 512, fp) != 512)
+  grub_uint32_t size;
+  if (ioctl (fileno (fp), BLKSSZGET, &size))
     {
+      grub_util_info ("can not get sector size for %s, assuming 512Bytes (non fatal).", device);
+      size = 512;
+    }
+  buf = grub_malloc( size );
+  if (fread (buf, 1, size, fp) != size)
+    {
+      free (buf);
       fclose (fp);
       return 0;
     }
-  
+  free (buf);
   fclose (fp);
   return 1;
 }
Index: util/hostdisk.c
===================================================================
--- util/hostdisk.c	(revision 1886)
+++ util/hostdisk.c	(working copy)
@@ -64,6 +64,9 @@
 # ifndef BLKGETSIZE64
 #  define BLKGETSIZE64  _IOR(0x12,114,size_t)    /* return device size */
 # endif /* ! BLKGETSIZE64 */
+# ifndef BLKSSZGET
+#  define BLKSSZGET  _IO(0x12,104)/* get block device sector size */
+#endif
 # ifndef MAJOR
 #  ifndef MINORBITS
 #   define MINORBITS	8
@@ -170,10 +173,14 @@
 
     size = grub_util_get_disk_size (map[drive].device);
 
-    if (size % 512)
+// TODO: find correct sector size for mingw32
+    grub_util_info ("compiled with mingw32, assuming 512 byte sector size!");
+    disk->sector_size = 512;
+
+    if (size % disk->sector_size)
       grub_util_error ("unaligned device size");
 
-    disk->total_sectors = size >> 9;
+    disk->total_sectors = size / disk->sector_size;
 
     grub_util_info ("the size of %s is %llu", name, disk->total_sectors);
 
@@ -194,6 +201,13 @@
 	goto fail;
       }
     
+    if (ioctl (fd, BLKSSZGET, &nr))
+      {
+        close (fd);
+        goto fail;
+      }
+    disk->sector_size = nr;
+ 
     if (ioctl (fd, BLKGETSIZE64, &nr))
       {
 	close (fd);
@@ -201,12 +215,12 @@
       }
 
     close (fd);
-    disk->total_sectors = nr / 512;
+    disk->total_sectors = nr / disk->sector_size;
 
-    if (nr % 512)
+    if (nr % disk->sector_size)
       grub_util_error ("unaligned device size");
     
-    grub_util_info ("the size of %s is %llu", name, disk->total_sectors);
+    grub_util_info ("the size of %s is %llu with sector size %iu", name, disk->total_sectors, disk->sector_size);
     
     return GRUB_ERR_NONE;
   }
@@ -215,15 +229,19 @@
   /* In GNU/Hurd, stat() will return the right size.  */
 #elif !defined (__GNU__)
 # warning "No special routine to get the size of a block device is implemented for your OS. This is not possibly fatal."
+# warning "No routine for getting sector size is implemented, assuming 512 bytes."
+  disk->sector_size = 512;
 #endif
-  if (stat (map[drive].device, &st) < 0)
-    return grub_error (GRUB_ERR_BAD_DEVICE, "cannot stat `%s'", map[drive].device);
+  {
+    if (stat (map[drive].device, &st) < 0)
+      return grub_error (GRUB_ERR_BAD_DEVICE, "cannot stat `%s'", map[drive].device);
 
-  disk->total_sectors = st.st_size >> GRUB_DISK_SECTOR_BITS;
+    disk->total_sectors = st.st_size / disk->sector_size;
   
-  grub_util_info ("the size of %s is %lu", name, disk->total_sectors);
+    grub_util_info ("the size of %s is %lu with sector size %iu", name, disk->total_sectors, disk->sector_size);
   
-  return GRUB_ERR_NONE;
+    return GRUB_ERR_NONE;
+  }
 }
 
 #ifdef __linux__
@@ -346,7 +364,7 @@
     _syscall5 (int, _llseek, uint, filedes, ulong, hi, ulong, lo,
                loff_t *, res, uint, wh);
 
-    offset = (loff_t) sector << GRUB_DISK_SECTOR_BITS;
+    offset = (loff_t) sector * disk->sector_size;
     if (_llseek (fd, offset >> 32, offset & 0xffffffff, &result, SEEK_SET))
       {
 	grub_error (GRUB_ERR_BAD_DEVICE, "cannot seek `%s'", map[disk->id].device);
@@ -356,7 +374,7 @@
   }
 #else
   {
-    off_t offset = (off_t) sector << GRUB_DISK_SECTOR_BITS;
+    off_t offset = (off_t) sector * disk->sector_size;
 
     if (lseek (fd, offset, SEEK_SET) != offset)
       {
@@ -422,6 +440,8 @@
   return size;
 }
 
+
+/* read size number of SECTORS which each in turn is of disk->sector_size bytes */
 static grub_err_t
 grub_util_biosdisk_read (grub_disk_t disk, grub_disk_addr_t sector,
 			 grub_size_t size, char *buf)
@@ -432,27 +452,29 @@
   if (fd < 0)
     return grub_errno;
   
-#ifdef __linux__
-  if (sector == 0 && size > 1)
-    {
-      /* Work around a bug in Linux ez remapping.  Linux remaps all
-	 sectors that are read together with the MBR in one read.  It
-	 should only remap the MBR, so we split the read in two 
-	 parts. -jochen  */
-      if (nread (fd, buf, GRUB_DISK_SECTOR_SIZE) != GRUB_DISK_SECTOR_SIZE)
-	{
-	  grub_error (GRUB_ERR_READ_ERROR, "cannot read `%s'", map[disk->id].device);
-	  close (fd);
-	  return grub_errno;
-	}
-      
-      buf += GRUB_DISK_SECTOR_SIZE;
-      size--;
-    }
-#endif /* __linux__ */
-  
-  if (nread (fd, buf, size << GRUB_DISK_SECTOR_BITS)
-      != (ssize_t) (size << GRUB_DISK_SECTOR_BITS))
+//#ifdef __linux__
+//  if (sector == 0 && size > 1)
+//    {
+//      /* Work around a bug in Linux ez remapping.  Linux remaps all
+//	 sectors that are read together with the MBR in one read.  It
+//	 should only remap the MBR, so we split the read in two 
+//	 parts. -jochen  */
+//      if (nread (fd, buf, GRUB_DISK_SECTOR_SIZE) != GRUB_DISK_SECTOR_SIZE)
+//	{
+//	  grub_error (GRUB_ERR_READ_ERROR, "cannot read `%s'", map[disk->id].device);
+//	  close (fd);
+//	  return grub_errno;
+//	}
+//      
+//      buf += GRUB_DISK_SECTOR_SIZE;
+//      size--;
+//    }
+//#endif /* __linux__ */
+// since we now read sectors only, we don'Ãt need no sepcial handling for MBRsize!=SectorSize
+// this _must_ be done by the caller now!  
+
+  if (nread (fd, buf, size * disk->sector_size)
+      != (ssize_t) (size * disk->sector_size))
     grub_error (GRUB_ERR_READ_ERROR, "cannot read from `%s'", map[disk->id].device);
 
   close (fd);
@@ -469,8 +491,8 @@
   if (fd < 0)
     return grub_errno;
   
-  if (nwrite (fd, buf, size << GRUB_DISK_SECTOR_BITS)
-      != (ssize_t) (size << GRUB_DISK_SECTOR_BITS))
+  if (nwrite (fd, buf, size * disk->sector_size)
+      != (ssize_t) (size * disk->sector_size))
     grub_error (GRUB_ERR_WRITE_ERROR, "cannot write to `%s'", map[disk->id].device);
 
   close (fd);

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-10-08 16:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-10-07 22:30 Patch for different sector sizes Clemens Helfmeier
2008-10-08 16:11 ` Clemens Helfmeier

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.