Quantcast
Channel: Windows – delog
Viewing all articles
Browse latest Browse all 44

Write Ubuntu image file to SD card on Windows

$
0
0

I use an 8 GB (class 4) SD card to run the Ubuntu distribution for OMAP4 on a PandaBoard. In this post I’ll quickly detail 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 a SD card image, this is useful to make backups. Backups can be be rewritten to 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 Disk Management tool may also work.

Patch for version 0.3 of Image Writer for Windows 

[This is section is outdated. Please obtain version 0.4 or better from launchpad.net.]

The version 0.3 binary, that I tested, 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 two functions in disk.cpp, as follows:

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 large SD cards. I have forked the source code at github, a binary is available for download there.


Filed under: ARM, Linux, Windows

Viewing all articles
Browse latest Browse all 44

Trending Articles