Write Ubuntu image file to SD card on Windows


I use an 8 GB class 4 SD Card to run the Ubuntu distribution for OMAP4 on a PandaBoard. This post details the procedure I use to prepare the SD Card on Windows.

Download and write Ubuntu image

You’ll need the following tools

  1. 7-zip or other decompressor that can extract gzipped file (extension gz).
  2. Image Writer for Windows to write the img file to the SD Card.
  3. A laptop with SD Card reader, or an external USB SD Card reader.

Here’s the procedure to prepare the SD Card

  1. Download Ubuntu gzipped image file for OMAP4 boards.
  2. Extract img file using 7-zip.
  3. Insert SD Card into reader.
  4. Execute Win32DiskImager.exe – it requests admin privileges on Windows 7.
  5. Select the image file extracted in step 2.
  6. Select the device that corresponds to the SD Card reader.
  7. Write the image file. This will take a while.
  8. Eject the SD Card.

Backup or Clone SD Card

Image Writer for Windows can also be used to read an SD Card. You can write the image file to the same or another SD Card.

Restore SD Card to Original State

The SD Card will be partitioned after the procedure above. To restore it to its original state i.e. create a single partition with all the available space, you’ll need to use the GParted Partition Editor on a Linux box, or from a live CD. Windows 8 Disk Management tool may also work.

Patch for version 0.3 of Image Writer for Windows
Skip this section, it refers to an older version of Image Writer for Windows. Retaining it to provide context for the comments.

Version 0.3 binary of Image Writer has a bug in a call to Win32 SetFilePointer function. The image read is about half the size of my 8 GB SD Card. I built it from source using QT Creator after patching the following two functions in disk.cpp

char *readSectorDataFromHandle(HANDLE handle, unsigned long long startsector, unsigned long long numsectors, unsigned long long sectorsize)
{
	unsigned long bytesread;
	char *data = new char[sectorsize * numsectors];
	LARGE_INTEGER li;
	li.QuadPart = startsector * sectorsize;
	SetFilePointer(handle, li.LowPart, &li.HighPart, FILE_BEGIN);
	if (!ReadFile(handle, data, sectorsize * numsectors, &bytesread, NULL))
	{
		char *errormessage=NULL;
		FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, GetLastError(), 0, (LPSTR)&errormessage, 0, NULL);
		QMessageBox::critical(NULL, "Read Error", QString("An error occurred when attempting to read data from handle.\nError %1: %2").arg(GetLastError()).arg(errormessage));
		LocalFree(errormessage);
		delete data;
		data = NULL;
	}
	return data;
}

bool writeSectorDataToHandle(HANDLE handle, char *data, unsigned long long startsector, unsigned long long numsectors, unsigned long long sectorsize)
{
	unsigned long byteswritten;
	BOOL bResult;
	LARGE_INTEGER li;
	li.QuadPart = startsector * sectorsize;
	SetFilePointer(handle, li.LowPart, &li.HighPart, FILE_BEGIN);
	bResult = WriteFile(handle, data, sectorsize * numsectors, &byteswritten, NULL);
	if (!bResult)
	{
		char *errormessage=NULL;
		FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, GetLastError(), 0, (LPSTR)&errormessage, 0, NULL);
		QMessageBox::critical(NULL, "Write Error", QString("An error occurred when attempting to write data from handle.\nError %1: %2").arg(GetLastError()).arg(errormessage));
		LocalFree(errormessage);
	}
	return (bResult == TRUE);
}

That fixed the problem with reading and writing large SD Cards.

9 thoughts on “Write Ubuntu image file to SD card on Windows

  1. I am getting ready to push an update to win32-image-writer. I will add your changes. I hope to be a little more responsive next year with getting updates moved into the main trunk.

    1. Thank you for the update Tobin.

      I tested my changes using the following procedure on Windows:
      1. Use random data file creator (rdfc) to create a 8 GB (8027873280 bytes) test file.
      2. Use the tool to write the image file to SD card.
      3. Use the tool to read data from SD card into a new image file.
      4. Compare the two image files using diff.

  2. Hii
    Im trying to write an image file on sd card for beaglboard-xm and i incouter several problems.
    1. After unzipping the .gz file i get .raw file shouldnt i get .img file??

    2. After wiriting into the sd card an putting into the beaglboard nothing happens.

    I would like ur help (my first time with linux)

    Yair

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s