Skip to contentSkip to conversation

Patch for different sector sizes

PATCH Clemens Helfmeier 1 participant 2 messages Oct 7, 2008 – Oct 8, 2008

lore
Share & tools
lore
#0 lore raw

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

lvm.c +1 -1
Index: disk/lvm.c
===================================================================
@@ -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);
scsi.c +2 -2
Index: disk/scsi.c
===================================================================
@@ -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.  */
ata.c +5 -2
Index: disk/ata.c
===================================================================
@@ -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)
     {
i386/pc/biosdisk.c +5 -6
Index: disk/i386/pc/biosdisk.c
===================================================================
@@ -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;
     }
raid.c +8 -4
Index: disk/raid.c
===================================================================
@@ -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;
memdisk.c +2 -2
Index: disk/memdisk.c
===================================================================
@@ -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;
 }
 
raid5_recover.c +4 -1
Index: disk/raid5_recover.c
===================================================================
@@ -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;
loopback.c +4 -4
Index: disk/loopback.c
===================================================================
@@ -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;
raid6_recover.c +4 -1
Index: disk/raid6_recover.c
===================================================================
@@ -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;
fs.c +30 -8
Index: kern/fs.c
===================================================================
@@ -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;
rescue.c +2 -1
Index: kern/rescue.c
===================================================================
@@ -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)
disk.c +32 -32

163 lines not on this page · raw message

xfs.c +20 -5
Index: fs/xfs.c
===================================================================
@@ -188,6 +188,23 @@
 #define GRUB_XFS_NEXT_DIRENT(pos,len)		\
   (pos) + GRUB_XFS_ROUND_TO_DIRENT (8 + 1 + len + 2)
 
+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));
 }
 
 
afs.c +22 -5
Index: fs/afs.c
===================================================================
@@ -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
ntfs.c +4 -4
Index: fs/ntfs.c
===================================================================
@@ -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)
fat.c +10 -9
Index: fs/fat.c
===================================================================
@@ -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;
 
iso9660.c +8 -8
Index: fs/iso9660.c
===================================================================
@@ -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.  */
affs.c +7 -7
Index: fs/affs.c
===================================================================
@@ -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)
fshelp.c +4 -4
Index: fs/fshelp.c
===================================================================
@@ -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;
reiserfs.c +16 -16
Index: fs/reiserfs.c
===================================================================
@@ -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);
     }
jfs.c +11 -13
Index: fs/jfs.c
===================================================================
@@ -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;
minix.c +4 -4
Index: fs/minix.c
===================================================================
@@ -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,
hfsplus.c +20 -3
Index: fs/hfsplus.c
===================================================================
@@ -225,6 +225,23 @@
 #endif
 
 
+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);
cpio.c +3 -3
Index: fs/cpio.c
===================================================================
@@ -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;
 }
grub/ntfs.h +2 -1
Index: include/grub/ntfs.h
===================================================================
@@ -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)
grub/disk.h +5 -2
Index: include/grub/disk.h
===================================================================
@@ -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
cat.c +2 -1
Index: commands/cat.c
===================================================================
@@ -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;
 
blocklist.c +6 -3
Index: commands/blocklist.c
===================================================================
@@ -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;
 }
 
i386/pc/linux.c +5 -5
Index: loader/i386/pc/linux.c
===================================================================
@@ -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,
i386/pc/chainloader.c +31 -25

69 lines not on this page · raw message

grub-probe.c +20 -2
Index: util/grub-probe.c
===================================================================
@@ -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);
   
i386/pc/grub-mkimage.c +20 -6
Index: util/i386/pc/grub-mkimage.c
===================================================================
@@ -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)
i386/pc/grub-setup.c +36 -31

153 lines not on this page · raw message

grub-mkdevicemap.c +14 -3
Index: util/grub-mkdevicemap.c
===================================================================
@@ -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;
 }
hostdisk.c +57 -35

161 lines not on this page · raw message

#1 lore raw

Hi everyone,

i have now finished most of the sector size modifications. Still, there are a few things to be done: e.g. the file system drivers must be checked for correct handling of places, etc. (ext2 is fixed with this patch, fat works here on a 2048 device without modification, a few others should still be done).

Most likely the ATA and SCSI drivers are also broken. Best would be to have someone check on that who knows the hardware here.

Unfortunately i could not yet test the bootup procedure since my hardware does not detect the pendrive which i have here correctly. maybe i will have a different one with 2048bytes sector size another day. Perhaps there are volunteers who want to test this patch?

IMPORTANT:
the grub-mkimage tool has got a new option (--sector-size=XX) which must be given. Since this tool does not actually handle things with hard drives, it can not detect the correct sector size on it's own. Any suggestions on how to fix this? (maybe we can make it work with either sector-size or target-device supplied on command line)

Bye,
Clemens (sum)

Quoted 6 lines · Clemens Helfmeier

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

Quoted 8 lines

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

lvm.c +2 -1
Index: disk/lvm.c
===================================================================
@@ -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);
@@ -221,6 +221,7 @@
   struct grub_lvm_vg *vg;
   struct grub_lvm_pv *pv;
   
+
   disk = grub_disk_open (name);
   if (!disk)
     return 0;
scsi.c +2 -2
Index: disk/scsi.c
===================================================================
@@ -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, grub_divmod64(scsi->blocksize, disk->sector_size, NULL),
 			  NULL);
 
   /* Depending on the type, select a read function.  */
ata.c +13 -10
Index: disk/ata.c
===================================================================
@@ -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)
     {
@@ -681,9 +684,9 @@
 	  for (sect = 0; sect < batch; sect++)
 	    {
 	      if (grub_ata_pio_read (dev, buf,
-				     GRUB_DISK_SECTOR_SIZE))
+				     GRUB_ATA_SECTOR_SIZE))
 		return grub_errno;
-	      buf += GRUB_DISK_SECTOR_SIZE;
+	      buf += GRUB_ATA_SECTOR_SIZE;
 	    }
 	}
       else
@@ -699,9 +702,9 @@
 	  for (sect = 0; sect < batch; sect++)
 	    {
 	      if (grub_ata_pio_write (dev, buf,
-				      GRUB_DISK_SECTOR_SIZE))
+				      GRUB_ATA_SECTOR_SIZE))
 		return grub_error (GRUB_ERR_WRITE_ERROR, "ATA write error");
-	      buf += GRUB_DISK_SECTOR_SIZE;
+	      buf += GRUB_ATA_SECTOR_SIZE;
 	    }
 	}
       sector += batch;
@@ -723,9 +726,9 @@
 
       for (sect = 0; sect < (size % batch); sect++)
 	{
-	  if (grub_ata_pio_read (dev, buf, GRUB_DISK_SECTOR_SIZE))
+	  if (grub_ata_pio_read (dev, buf, GRUB_ATA_SECTOR_SIZE))
 	    return grub_errno;
-	  buf += GRUB_DISK_SECTOR_SIZE;
+	  buf += GRUB_ATA_SECTOR_SIZE;
 	}
     } else {
       /* Write sectors.  */
@@ -738,9 +741,9 @@
 
       for (sect = 0; sect < (size % batch); sect++)
 	{
-	  if (grub_ata_pio_write (dev, buf, GRUB_DISK_SECTOR_SIZE))
+	  if (grub_ata_pio_write (dev, buf, GRUB_ATA_SECTOR_SIZE))
 	    return grub_error (GRUB_ERR_WRITE_ERROR, "ATA write error");
-	  buf += GRUB_DISK_SECTOR_SIZE;
+	  buf += GRUB_ATA_SECTOR_SIZE;
 	}
     }
 
i386/pc/biosdisk.c +5 -6
Index: disk/i386/pc/biosdisk.c
===================================================================
@@ -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;
     }
raid.c +8 -4
Index: disk/raid.c
===================================================================
@@ -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;
raid5_recover.c +4 -1
Index: disk/raid5_recover.c
===================================================================
@@ -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;
memdisk.c +7 -3
Index: disk/memdisk.c
===================================================================
@@ -25,6 +25,8 @@
 #include <grub/types.h>
 #include <grub/machine/kernel.h>
 
+#define GRUB_MEMDISK_SECTOR_SIZE	512
+
 static char *memdisk_addr;
 static grub_off_t memdisk_size = 0;
 
@@ -40,7 +42,9 @@
   if (grub_strcmp (name, "memdisk"))
       return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a memdisk");
 
-  disk->total_sectors = memdisk_size / GRUB_DISK_SECTOR_SIZE;
+  // Memdisk may have any number of bytes/sectors, just choose something here.
+  disk->sector_size = GRUB_MEMDISK_SECTOR_SIZE;
+  disk->total_sectors = grub_divmod64(memdisk_size, disk->sector_size, 0);
   disk->id = (unsigned long) "mdsk";
   disk->has_partitions = 0;
 
@@ -56,7 +60,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 +68,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;
 }
 
raid6_recover.c +4 -1
Index: disk/raid6_recover.c
===================================================================
@@ -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;
loopback.c +11 -6
Index: disk/loopback.c
===================================================================
@@ -25,6 +25,9 @@
 #include <grub/disk.h>
 #include <grub/mm.h>
 
+// any value is adequate. this affects the loopback disk
+#define GRUB_LOOPBACK_SECTOR_SIZE	512
+
 struct grub_loopback
 {
   char *devname;
@@ -174,9 +177,11 @@
   if (! file)
     return grub_errno;
   
+  /* set size of sectors */
+  disk->sector_size = GRUB_LOOPBACK_SECTOR_SIZE;
   /* Use the filesize for the disk size, round up to a complete sector.  */
-  disk->total_sectors = ((file->size + GRUB_DISK_SECTOR_SIZE - 1)
-			 / GRUB_DISK_SECTOR_SIZE);
+  disk->total_sectors = grub_divmod64((file->size + disk->sector_size - 1),
+			 disk->sector_size, NULL);
   disk->id = (unsigned long) dev;
   
   disk->has_partitions = dev->has_partitions;
@@ -200,20 +205,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;
fs.c +7 -8
Index: kern/fs.c
===================================================================
@@ -198,7 +198,7 @@
 	  goto fail;
 	}
       
-      file->size += (blocks[i].length << GRUB_DISK_SECTOR_BITS);
+      file->size += (blocks[i].length * disk->sector_size);
       p++;
     }
 
@@ -219,12 +219,12 @@
   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));
+  sector = grub_divmod64(file->offset, disk->sector_size, &offset);
   for (p = file->data; p->length && len > 0; p++)
     {
       if (sector < p->length)
@@ -232,9 +232,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 ((grub_divmod64(size + offset + disk->sector_size - 1,
+	       disk->sector_size, NULL) > 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 +242,7 @@
 	  
 	  ret += size;
 	  len -= size;
-	  sector -= ((size + offset) >> GRUB_DISK_SECTOR_BITS);
-	  offset = ((size + offset) & (GRUB_DISK_SECTOR_SIZE - 1));
+	  sector -= grub_divmod64((size + offset), disk->sector_size, &offset);
 	}
       else
 	sector -= p->length;
rescue.c +1 -1
Index: kern/rescue.c
===================================================================
@@ -130,7 +130,7 @@
 grub_rescue_cmd_cat (int argc, char *argv[])
 {
   grub_file_t file;
-  char buf[GRUB_DISK_SECTOR_SIZE];
+  char buf[GRUB_RESCUE_BUF_SIZE];
   grub_ssize_t size;
 
   if (argc < 1)
disk.c +36 -33

179 lines not on this page · raw message

xfs.c +6 -6
Index: fs/xfs.c
===================================================================
@@ -188,6 +188,7 @@
 #define GRUB_XFS_NEXT_DIRENT(pos,len)		\
   (pos) + GRUB_XFS_ROUND_TO_DIRENT (8 + 1 + len + 2)
 
+
 static inline int
 grub_xfs_inode_block (struct grub_xfs_data *data,
 		      grub_uint64_t ino)
@@ -197,7 +198,7 @@
   long long block;
 
   block = (inoinag >> 4) + ag * data->agsize;
-  block <<= (data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS);
+  block = grub_divmod64((block << data->sblock.log2_bsize), data->disk->sector_size, NULL);
   return block;
 }
 
@@ -266,9 +267,8 @@
             }
 
           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),
+                              grub_divmod64( grub_be_to_cpu64 (keys[i - 1 + XFS_INODE_EXTENTS])
+                              << (node->data->sblock.log2_bsize), node->data->disk->sector_size, NULL),
                               0, node->data->sblock.bsize, (char *) leaf))
             return 0;
 
@@ -330,11 +330,11 @@
 					unsigned offset, unsigned length),
 		     int pos, grub_size_t len, char *buf)
 {
+// TODO: Check weather the last argument here is correct. maybe it is fixed size and not fixed sector count.
   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 - fat_log2 (node->data->disk->sector_size));
 }
 
 
afs.c +8 -5
Index: fs/afs.c
===================================================================
@@ -179,6 +179,7 @@
 static grub_dl_t my_mod;
 #endif
 
+
 static grub_afs_off_t
 grub_afs_run_to_num (struct grub_afs_sblock *sb,
                      struct grub_afs_blockrun *run)
@@ -193,7 +194,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 +229,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 +265,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;
@@ -288,12 +289,14 @@
                                                         unsigned offset, unsigned length),
                     int pos, grub_size_t len, char *buf)
 {
+// TODO: get some nice way to calculate the log here. check if afs requires this value to
+// be sector oriented and not 512-byte oriented.
   return grub_fshelp_read_file (node->data->disk, node, read_hook,
 				pos, len, buf, grub_afs_read_block,
                                 U64 (&node->data->sblock,
                                      node->inode.stream.size),
 				node->data->sblock.block_shift
-                                - GRUB_DISK_SECTOR_BITS);
+                                - fat_log2 (node->data->disk->sector_size));
 }
 
 static int
ntfs.c +4 -4
Index: fs/ntfs.c
===================================================================
@@ -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)
fat.c +5 -9
Index: fs/fat.c
===================================================================
@@ -188,11 +188,7 @@
     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 / disk->sector_size) );
   
   data->cluster_bits = fat_log2 (bpb.sectors_per_cluster);
   if (data->cluster_bits < 0)
@@ -226,10 +222,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 +349,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 +362,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;
 
iso9660.c +8 -8
Index: fs/iso9660.c
===================================================================
@@ -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.  */
affs.c +7 -7
Index: fs/affs.c
===================================================================
@@ -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)
fshelp.c +4 -4
Index: fs/fshelp.c
===================================================================
@@ -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 = grub_divmod64(((len + pos) + blocksize - 1) >> log2blocksize, disk->sector_size, NULL);
 
-  for (i = pos >> (log2blocksize + GRUB_DISK_SECTOR_BITS); i < blockcnt; i++)
+  for (i = grub_divmod64((pos >> log2blocksize), disk->sector_size, NULL); 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 == (grub_divmod64(pos >> log2blocksize, disk->sector_size, NULL)))
 	{
 	  skipfirst = blockoff;
 	  blockend -= skipfirst;
reiserfs.c +16 -16
Index: fs/reiserfs.c
===================================================================
@@ -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);
     }
jfs.c +12 -14
Index: fs/jfs.c
===================================================================
@@ -276,9 +276,8 @@
 	  } tree;
 	  	  
 	  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_divmod64(grub_le_to_cpu32 (extents[found].extent.blk2)
+			      << (grub_le_to_cpu16 (data->sblock.log2_blksz)), data->disk->sector_size, NULL), 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,
+		      grub_divmod64 (iagblk << (grub_le_to_cpu16 (data->sblock.log2_blksz)), data->disk->sector_size, NULL), 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;
ext2.c +2 -2
Index: fs/ext2.c
===================================================================
@@ -495,8 +495,8 @@
   if (!data)
     return 0;
 
-  /* Read the superblock.  */
-  grub_disk_read (disk, 1 * 2, 0, sizeof (struct grub_ext2_sblock),
+  /* Read the superblock. from address 1024 instead of some sector!  */
+  grub_disk_read (disk, 0, 1024, sizeof (struct grub_ext2_sblock),
                   (char *) &data->sblock);
   if (grub_errno)
     goto fail;
minix.c +4 -4
Index: fs/minix.c
===================================================================
@@ -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,
hfsplus.c +4 -5
Index: fs/hfsplus.c
===================================================================
@@ -224,7 +224,6 @@
 static grub_dl_t my_mod;
 #endif
 
-
 /* 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
@@ -309,8 +308,7 @@
       
       if (blk != -1)
 	return (blk
-		+ (node->data->embedded_offset >> (node->data->log2blksize
-						   - GRUB_DISK_SECTOR_BITS)));
+		+ (node->data->embedded_offset * node->data->disk->sector_size) >> (node->data->log2blksize));
 
       /* For the extent overflow file, extra extents can't be found in
 	 the extent overflow file.  If this happens, you found a
@@ -360,10 +358,11 @@
 					   unsigned offset, unsigned length),
 			int pos, grub_size_t len, char *buf)
 {
+// TODO: Thinks of some nice way for log2 here...
   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 - fat_log2 (node->data->disk->sector_size));
 }
 
 static struct grub_hfsplus_data *
@@ -409,7 +408,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);
cpio.c +3 -3
Index: fs/cpio.c
===================================================================
@@ -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;
 }
grub/ntfs.h +3 -1
Index: include/grub/ntfs.h
===================================================================
@@ -67,7 +67,9 @@
 #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)
+// Check if this is ok on devices with different sector sizes
+#define BLK_SHR		9
 
 #define MAX_MFT		(1024 >> BLK_SHR)
 #define MAX_IDX		(16384 >> BLK_SHR)
grub/disk.h +6 -3
Index: include/grub/disk.h
===================================================================
@@ -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;
 
@@ -125,9 +128,9 @@
 typedef struct grub_disk_memberlist *grub_disk_memberlist_t;
 #endif
 
-/* The sector size.  */
-#define GRUB_DISK_SECTOR_SIZE	0x200
-#define GRUB_DISK_SECTOR_BITS	9
+/* The sector size is now contained in grub_disk structure  */
+//#define GRUB_DISK_SECTOR_SIZE	512
+//#define GRUB_DISK_SECTOR_BITS	9
 
 /* The maximum number of disk caches.  */
 #define GRUB_DISK_CACHE_NUM	1021
grub/lvm.h +2 -1
Index: include/grub/lvm.h
===================================================================
@@ -65,7 +65,8 @@
   struct grub_lvm_pv *pv;
 };
 
-#define GRUB_LVM_LABEL_SIZE GRUB_DISK_SECTOR_SIZE
+// TODO: Check if LVM_LABEL_SIZE is 1 sector or 512 Bytes and change code accordingly
+#define GRUB_LVM_LABEL_SIZE 512
 #define GRUB_LVM_LABEL_SCAN_SECTORS 4L
 
 #define GRUB_LVM_LABEL_ID "LABELONE"
crc.c +2 -1
Index: commands/crc.c
===================================================================
@@ -24,6 +24,7 @@
 #include <grub/file.h>
 #include <grub/misc.h>
 #include <grub/lib/crc.h>
+#include <grub/cmd.h>
 
 static grub_err_t
 grub_cmd_crc (struct grub_arg_list *state __attribute__ ((unused)),
@@ -31,7 +32,7 @@
 
 {
   grub_file_t file;
-  char buf[GRUB_DISK_SECTOR_SIZE];
+  char buf[GRUB_CMD_CHUNK_SIZE];
   grub_ssize_t size;
   grub_uint32_t crc;
 
cat.c +2 -1
Index: commands/cat.c
===================================================================
@@ -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;
 
hexdump.c +18 -7
Index: commands/hexdump.c
===================================================================
@@ -26,6 +26,8 @@
 #include <grub/gzio.h>
 #include <grub/partition.h>
 #include <grub/lib/hexdump.h>
+#include <grub/mm.h>
+#include <grub/cmd.h>
 
 static const struct grub_arg_option options[] = {
   {"skip", 's', 0, "skip offset bytes from the beginning of file.", 0,
@@ -37,7 +39,8 @@
 static grub_err_t
 grub_cmd_hexdump (struct grub_arg_list *state, int argc, char **args)
 {
-  char buf[GRUB_DISK_SECTOR_SIZE * 4];
+  // we can not use GRUB_CMD_CHUNK_SIZE here since we want to directly read from the device
+  char *buf;
   grub_ssize_t size, length;
   grub_addr_t skip;
   int namelen;
@@ -63,10 +66,14 @@
         return 0;
 
       if (disk->partition)
-        skip += grub_partition_get_start (disk->partition) << GRUB_DISK_SECTOR_BITS;
+        skip += grub_partition_get_start (disk->partition) * disk->sector_size;
 
-      sector = (skip >> (GRUB_DISK_SECTOR_BITS + 2)) * 4;
-      ofs = skip & (GRUB_DISK_SECTOR_SIZE * 4 - 1);
+      // we are going to read directly from disk, so take a whole sector!
+      // TODO: Why not use grub_disk_read which can handle also smaller and bigger chunks
+      buf = grub_malloc (disk->sector_size);
+
+      sector = (grub_divmod64(skip, disk->sector_size, 0) >> 2) * 4;
+      ofs = skip & (disk->sector_size * 4 - 1);
       while (length)
         {
           grub_size_t len, n;
@@ -75,8 +82,8 @@
           if (ofs + len > sizeof (buf))
             len = sizeof (buf) - ofs;
 
-          n = ((ofs + len + GRUB_DISK_SECTOR_SIZE - 1)
-               >> GRUB_DISK_SECTOR_BITS);
+          n = grub_divmod64((ofs + len + disk->sector_size - 1),
+               disk->sector_size, 0);
           if (disk->dev->read (disk, sector, n, buf))
             break;
 
@@ -89,6 +96,7 @@
         }
 
       grub_disk_close (disk);
+      grub_free (buf);
     }
   else
     {
@@ -99,8 +107,10 @@
 	return 0;
 
       file->offset = skip;
+      // here the size of the buffer does not matter since we use file access
+      buf = grub_malloc (GRUB_CMD_CHUNK_SIZE);
 
-      while ((size = grub_file_read (file, buf, sizeof (buf))) > 0)
+      while ((size = grub_file_read (file, buf, GRUB_CMD_CHUNK_SIZE)) > 0)
 	{
 	  unsigned long len;
 
@@ -116,6 +126,7 @@
 	}
 
       grub_file_close (file);
+      grub_free (buf);
     }
 
   return 0;
blocklist.c +6 -3
Index: commands/blocklist.c
===================================================================
@@ -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;
 }
 
loadenv.c +17 -12
Index: commands/loadenv.c
===================================================================
@@ -157,8 +157,8 @@
 {
   grub_file_t file;
   grub_disk_t disk;
-  grub_disk_addr_t addr[GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS];
-  char buf[GRUB_DISK_SECTOR_SIZE];
+  grub_disk_addr_t *addr;
+  char *buf;
   grub_disk_addr_t part_start = 0;
   int num = 0;
 
@@ -168,10 +168,10 @@
   void NESTED_FUNC_ATTR hook (grub_disk_addr_t sector,
                               unsigned offset, unsigned length)
     {
-      if ((offset != 0) || (length != GRUB_DISK_SECTOR_SIZE))
+      if ((offset != 0) || (length != disk->sector_size))
         return;
 
-      if (num < (GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS))
+      if (num < (GRUB_ENVBLK_MAXLEN / disk->sector_size))
         addr[num++] = sector;
     }
 
@@ -184,7 +184,7 @@
 
   file->read_hook = 0;
 
-  if (num != GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS)
+  if (num != GRUB_ENVBLK_MAXLEN / disk->sector_size)
     {
       grub_error (GRUB_ERR_BAD_DEVICE, "invalid blocklist");
       goto quit;
@@ -194,14 +194,17 @@
   if (disk->partition)
     part_start = grub_partition_get_start (disk->partition);
 
-  for (num = 0; num < (GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS); num++)
+  buf = grub_malloc (disk->sector_size);
+  addr = grub_malloc (sizeof (*addr) * GRUB_ENVBLK_MAXLEN / disk->sector_size);
+
+  for (num = 0; num < (GRUB_ENVBLK_MAXLEN / disk->sector_size); num++)
     {
       if (grub_disk_read (disk, addr[num] - part_start, 0,
-                          GRUB_DISK_SECTOR_SIZE,  buf))
+                          disk->sector_size,  buf))
         goto quit;
 
-      if (grub_memcmp (&buffer[num << GRUB_DISK_SECTOR_BITS], buf,
-                       GRUB_DISK_SECTOR_SIZE))
+      if (grub_memcmp (&buffer[num * disk->sector_size], buf,
+                       disk->sector_size))
         {
           grub_error (GRUB_ERR_BAD_DEVICE, "invalid blocklist");
           goto quit;
@@ -226,14 +229,16 @@
       args++;
     }
 
-  for (num = 0; num < (GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS); num++)
+  for (num = 0; num < (GRUB_ENVBLK_MAXLEN / disk->sector_size); num++)
     if (grub_disk_write (disk, addr[num] - part_start, 0,
-                         GRUB_DISK_SECTOR_SIZE,
-                         &buffer[num << GRUB_DISK_SECTOR_BITS]))
+                         disk->sector_size,
+                         &buffer[num * disk->sector_size]))
       goto quit;
 
 quit:
   grub_file_close (file);
+  grub_free (buf);
+  grub_free (addr);
 
   return grub_errno;
 }
apple.c +5 -5
Index: partmap/apple.c
===================================================================
@@ -110,7 +110,7 @@
   struct grub_apple_part apart;
   struct grub_disk raw;
   int partno = 0;
-  unsigned pos = GRUB_DISK_SECTOR_SIZE;
+  unsigned pos = disk->sector_size;
 
   /* Enforce raw disk access.  */
   raw = *disk;
@@ -132,8 +132,8 @@
 
   for (;;)
     {
-      if (grub_disk_read (&raw, pos / GRUB_DISK_SECTOR_SIZE,
-			  pos % GRUB_DISK_SECTOR_SIZE,
+      if (grub_disk_read (&raw, pos / disk->sector_size,
+			  pos % disk->sector_size,
 			  sizeof (struct grub_apple_part),  (char *) &apart))
 	return grub_errno;
 
@@ -161,14 +161,14 @@
 	return grub_errno;
 
       if (grub_be_to_cpu32 (apart.first_phys_block)
-	  == GRUB_DISK_SECTOR_SIZE * 2)
+	  == disk->sector_size * 2)
 	return 0;
 
       pos += sizeof (struct grub_apple_part);
       partno++;
     }
 
-  if (pos != GRUB_DISK_SECTOR_SIZE)
+  if (pos != disk->sector_size)
     return 0;
 
  fail:
acorn.c +1 -1
Index: partmap/acorn.c
===================================================================
@@ -59,7 +59,7 @@
   unsigned int sectors_per_cylinder;
   int i;
 
-  err = grub_disk_read (disk, 0xC00 / GRUB_DISK_SECTOR_SIZE, 0,
+  err = grub_disk_read (disk, 0xC00 / disk->sector_size, 0,
 			sizeof (struct grub_acorn_boot_block),
 			(char *) &boot);
   if (err)
gpt.c +1 -1
Index: partmap/gpt.c
===================================================================
@@ -107,7 +107,7 @@
 	}
 
       last_offset += grub_le_to_cpu32 (gpt.partentry_size);
-      if (last_offset == GRUB_DISK_SECTOR_SIZE)
+      if (last_offset == disk->sector_size)
 	{
 	  last_offset = 0;
 	  entries++;
i386/pc/linux.c +5 -5
Index: loader/i386/pc/linux.c
===================================================================
@@ -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,
i386/pc/chainloader.c +31 -25

69 lines not on this page · raw message

geninit.sh +1 -1
Index: geninit.sh
===================================================================
@@ -49,7 +49,7 @@
 while read line; do
   file=`echo $line | cut -f1 -d:`
   if echo $@ | grep $file >/dev/null; then
-    echo $line | sed -e 's/.*GRUB_MOD_INIT *(\([a-zA-Z0-9_]*\)).*/  grub_\1_init ();/'
+    echo $line | sed -e 's/.*GRUB_MOD_INIT *(\([a-zA-Z0-9_]*\)).*/  grub_\1_init (); grub_util_info ("\1");/'
   fi
 done < ${lst}
 
grub-probe.c +27 -3
Index: util/grub-probe.c
===================================================================
@@ -112,18 +112,32 @@
   int abstraction_type;
   grub_device_t dev = NULL;
   grub_fs_t fs;
+
+  grub_util_info ("starting probe...");
+  if (path != NULL) grub_util_info ("path = %s.", path);
+  if (device_name != NULL) grub_util_info ("device_name = %s", device_name);
   
   if (path == NULL)
     {
-      if (! grub_util_check_block_device (device_name))
+      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 (path != NULL) grub_util_info ("path = %s.", path);
+  if (device_name != NULL) grub_util_info ("device_name = %s", device_name);
+
   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 +179,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 +372,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);
   
i386/pc/grub-mkimage.c +20 -6
Index: util/i386/pc/grub-mkimage.c
===================================================================
@@ -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)
i386/pc/grub-setup.c +36 -31

153 lines not on this page · raw message

grub-mkdevicemap.c +14 -3
Index: util/grub-mkdevicemap.c
===================================================================
@@ -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;
 }
hostdisk.c +52 -25

194 lines not on this page · raw message