getNumberOfSectors() returns incorrect number

Bug #932321 reported by Magnus Karlsson
30
This bug affects 8 people
Affects Status Importance Assigned to Milestone
Image Writer
Fix Released
Undecided
Unassigned

Bug Description

The function getNumberOfSectors is not returning the actual sector count. For example, on one of my 32 GB sd-cards with 64845824 sectors the function returns 64838340. This means that the whole disk is not read when doing an image read from the SD card.

Below is a version of getNumberOfSectors that returns the correct number:

unsigned long long getNumberOfSectors(HANDLE handle, unsigned long long *sectorsize)
{
 DWORD junk;
 DISK_GEOMETRY geometry;
 PARTITION_INFORMATION part;
 BOOL bResult;
 bResult = DeviceIoControl(handle, IOCTL_DISK_GET_PARTITION_INFO, NULL, 0, &part, sizeof(part), &junk, NULL) &
   DeviceIoControl(handle, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geometry, sizeof(geometry), &junk, NULL);
 if (!bResult)
 {
  char *errormessage=NULL;
  FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, GetLastError(), 0, errormessage, 0, NULL);
  QMessageBox::critical(NULL, "Device Error", QString("An error occurred when attempting to get the device's geometry.\nError %1: %2").arg(GetLastError()).arg(errormessage));
  LocalFree(errormessage);
  return 0;
 }
 if (sectorsize != NULL)
  *sectorsize = (unsigned long long)geometry.BytesPerSector;
 return (unsigned long long)part.PartitionLength.QuadPart / (unsigned long long)geometry.BytesPerSector;
}

Revision history for this message
Jeff B (skydiver38) wrote :

Thanks for the patch. So the length of a partition (in sectors) is not necessarily equal to the cylinders * tracks * sectors given in the DISK_GEOMETRY struct. Good to know, I guess.

I wonder if using the "disk size (in bytes)" field of the DISK_GEOMETRY_EX structure would report the same as C*T*S*Bytes (per sector), or the same as the partition info.

At any rate - thanks again for the patch, and I'll get it incorporated for a future release.

Revision history for this message
Magnus Karlsson (u-magnds-b) wrote :

I changed my code to use the DISK_GEOMETRY_EX like you suggest and this seems to work fine too.

The is the patch based on this verson:

unsigned long long getNumberOfSectors(HANDLE handle, unsigned long long *sectorsize)
{
 DWORD junk;
 DISK_GEOMETRY_EX diskgeometry;
 BOOL bResult;

 bResult = DeviceIoControl(handle, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0, &diskgeometry, sizeof(diskgeometry), &junk, NULL);
 if (!bResult)
 {
  char *errormessage=NULL;
  FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, GetLastError(), 0, errormessage, 0, NULL);
  QMessageBox::critical(NULL, "Device Error", QString("An error occurred when attempting to get the device's geometry.\nError %1: %2").arg(GetLastError()).arg(errormessage));
  LocalFree(errormessage);
  return 0;
 }

 if (sectorsize != NULL)
  *sectorsize = (unsigned long long)diskgeometry.Geometry.BytesPerSector;
 return (unsigned long long)diskgeometry.DiskSize.QuadPart / (unsigned long long)diskgeometry.Geometry.BytesPerSector;

}

Revision history for this message
Tobin Davis (gruemaster) wrote :

Gah! I had already incorporated the first patch and was testing it. Let me get the new patch incorporated. I plan on pushing the new release today (while I have a few spare cycles).

Revision history for this message
Tobin Davis (gruemaster) wrote : Re: [Bug 932321] Re: getNumberOfSectors() returns incorrect number

On Wed, 2012-02-15 at 21:36 +0000, Jeff B wrote:
> Thanks for the patch. So the length of a partition (in sectors) is not
> necessarily equal to the cylinders * tracks * sectors given in the
> DISK_GEOMETRY struct. Good to know, I guess.
>
> I wonder if using the "disk size (in bytes)" field of the
> DISK_GEOMETRY_EX structure would report the same as C*T*S*Bytes (per
> sector), or the same as the partition info.
>
> At any rate - thanks again for the patch, and I'll get it incorporated
> for a future release.
>

I'm already incorporating that patch (and now the revised one). I also
(finally) have your md5 patch in the tree. I'll push it up soon, then
prep a release today.

I have been over-swamped with work since returning from our rally in
Budapest. This is the first time I have had a chance to get back to the
code.

--
Tobin Davis

If you teach your children to like computers and to know how to gamble
then they'll always be interested in something and won't come to no real harm.

Revision history for this message
Tobin Davis (gruemaster) wrote :

In the future, could you add changes as a patch diff? I'm sure there are tools in Windows to do this. If not, use Bazaar Explorer for WIndows (http://bazaar.canonical.com).

Revision history for this message
Magnus Karlsson (u-magnds-b) wrote :

One more comment:
The cylinders, tracks/cylinder, sectors/track data is something that Windows makes up since the sd-card or thumbdrive is not structured like that, they only report the total size in blocks, where 1 block = 512 bytes.

Since the c, t, s values are integers, Windows must pick c, t and s values so that the formula c * t * s gives a conservative approximation of the disk size, but this is very likely not the correct size.

Revision history for this message
Jeff B (skydiver38) wrote :

Magnus - great to know. Thanks.

Tobin - OK. Good luck with the release, and I'll just take your update for this.

Is this bug really a duplicate of Bug 786060? (https://bugs.launchpad.net/win32-image-writer/+bug/786060) It talks about modifying the sector calculation as well.

Revision history for this message
Tobin Davis (gruemaster) wrote :

Ah, yes. Will tag that bug as fixed as well. Thanks for remembering this one.

Revision history for this message
Dennis Jackson (dennis-m-jackson) wrote :

Greetings -

     Just tried v0.5 of Win32 Disk Imager. Executable errors out at startup
indicating that libgcc_s_dw2-1.dll is missing. I located and downloaded
that DLL and installed it in the same directory as Win32 Disk Imager.exe.
Errors out again, but missing DLL is now reported to be libstdc++-6.dll. I
notice that another user has already reported this error, report number is
187903. Thanks.

----- Original Message -----
From: "Tobin Davis" <email address hidden>
To: <email address hidden>
Sent: Wednesday, February 15, 2012 5:43 PM
Subject: [Bug 932321] Re: getNumberOfSectors() returns incorrect number

Ah, yes. Will tag that bug as fixed as well. Thanks for remembering
this one.

--
You received this bug notification because you are subscribed to a
duplicate bug report (912259).
https://bugs.launchpad.net/bugs/932321

Title:
  getNumberOfSectors() returns incorrect number

Status in Image Writer for Windows:
  New

Bug description:
  The function getNumberOfSectors is not returning the actual sector
  count. For example, on one of my 32 GB sd-cards with 64845824 sectors
  the function returns 64838340. This means that the whole disk is not
  read when doing an image read from the SD card.

  Below is a version of getNumberOfSectors that returns the correct
  number:

  unsigned long long getNumberOfSectors(HANDLE handle, unsigned long long
*sectorsize)
  {
  DWORD junk;
  DISK_GEOMETRY geometry;
  PARTITION_INFORMATION part;
  BOOL bResult;
  bResult = DeviceIoControl(handle, IOCTL_DISK_GET_PARTITION_INFO, NULL, 0,
&part, sizeof(part), &junk, NULL) &
  DeviceIoControl(handle, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geometry,
sizeof(geometry), &junk, NULL);
  if (!bResult)
  {
  char *errormessage=NULL;
  FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL, GetLastError(), 0, errormessage, 0, NULL);
  QMessageBox::critical(NULL, "Device Error", QString("An error occurred
when attempting to get the device's geometry.\nError %1:
%2").arg(GetLastError()).arg(errormessage));
  LocalFree(errormessage);
  return 0;
  }
  if (sectorsize != NULL)
  *sectorsize = (unsigned long long)geometry.BytesPerSector;
  return (unsigned long long)part.PartitionLength.QuadPart / (unsigned long
long)geometry.BytesPerSector;
  }

To manage notifications about this bug go to:
https://bugs.launchpad.net/win32-image-writer/+bug/932321/+subscriptions

Tobin Davis (gruemaster)
Changed in win32-image-writer:
status: New → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.