Read a Single Sector from a Disk

Reading a specific sector from a hard drive using C on Linux (GNU/Linux )

You cannot printf() array of chat like that, you need right print result. For example as hex dump:

for (int i = 0; i < sizeof(secteur); i++) {
printf ("%02x ", secteur[i]);
if ((i + 1) % 16 == 0)
printf ("\n");
}

Reading a sector on the boot disk

In Linux, you can read from the special device file /dev/sda, assuming the hard drive you want to read is the first one. You need to be root to read this file. To read sector 2, you just seek to offset 2*SECTOR_SIZE and read in SECTOR_SIZE bytes.

I don't know if this device file is available on OS X. Check for interestingly named files under /dev such as /dev/sda or /dev/hda.

Read and write hard disk sector directly and efficiently

"Blocks in size of 4096" is not a special need, and you have not mentioned any access patterns that would break the kernel's built-in caching mechanisms.

The most efficient way to read and write your data is to use a file.

Read specific sector on hard drive using C language on windows

The problem is the sharing mode. You have specified FILE_SHARE_READ which means that nobody else is allowed to write to the device, but the partition is already mounted read/write so it isn't possible to give you that sharing mode. If you use FILE_SHARE_READ|FILE_SHARE_WRITE it will work. (Well, provided the disk sector size is 512 bytes, and provided the process is running with administrator privilege.)

You're also checking for failure incorrectly; CreateFile returns INVALID_HANDLE_VALUE on failure rather than NULL.

I tested this code successfully:

#include <windows.h>

#include <stdio.h>

int main(int argc, char ** argv)
{
int retCode = 0;
BYTE sector[512];
DWORD bytesRead;
HANDLE device = NULL;
int numSector = 5;

device = CreateFile(L"\\\\.\\C:", // Drive to open
GENERIC_READ, // Access mode
FILE_SHARE_READ|FILE_SHARE_WRITE, // Share Mode
NULL, // Security Descriptor
OPEN_EXISTING, // How to create
0, // File attributes
NULL); // Handle to template

if(device == INVALID_HANDLE_VALUE)
{
printf("CreateFile: %u\n", GetLastError());
return 1;
}

SetFilePointer (device, numSector*512, NULL, FILE_BEGIN) ;

if (!ReadFile(device, sector, 512, &bytesRead, NULL))
{
printf("ReadFile: %u\n", GetLastError());
}
else
{
printf("Success!\n");
}

return 0;
}

Unable to read a sector using int13H using custom Bootloader

Problems like these are usually related to the segment (ES) register not being set properly. Int 13h/AH=2h is documented this way:

AH = 02h
AL = number of sectors to read (must be nonzero)
CH = low eight bits of cylinder number
CL = sector number 1-63 (bits 0-5)
high two bits of cylinder (bits 6-7, hard disk only)
DH = head number
DL = drive number (bit 7 set for hard disk)
ES:BX -> data buffer

ES needs to be set to the segment that the data buffer offset resides in.

When you set up things your bootloader or JMP to code in another segment you should always make sure that the segment registers are loaded with the appropriate values. The segments you use will depend on what ORG directive you are using in your code.

I have general bootloader tips that may be of use.



Related Topics



Leave a reply



Submit