How can I access a hard drive sector

Asked

Viewed 325 times

0

I would like to know how I can make a code to manipulate a sector of hard drive and test on it as the Regeneration HDD does for example. And what libraries do I use in C ? (I do not know if in HD is pointer tmb so I’m calling sector, forgive my ignorance).

In theory it would be, take some data, write to the sector, read the sector, copy the data to another sector and erase the data from the sector ??

from now on, thank you!

  • 3

    What have you ever tried to do?

  • Still nothing, just wanted to get a tip on how to start !!!

  • 1

    The way your question is going to be closed off as out of scope. If you ask for tips on how to get started, it will be closed as based on opinion. See this part: How to ask a good question?

  • Got it, obg msm like this, I’ll see if I can figure out then how do!!!

1 answer

2

You don’t do it. Not as a user-level program: The operating system does not allow programs (in contrast to device drivers) to have access to the hardware - but allows access to layers abstracted from it.

So - at the very least, you have to say which operating system you’re working on: Windows, Linux, Mac OS X or other - and make code to work within it.

Windows At the time of DOS and early Windows - which could not prevent programs from accessing the Hardware directly - maybe even XP, if not ME - you could have "physical" access to the disk by calling BIOS functions directly - to "int 13h" - These "int"s, or interruptions, handle hardware events, but were also a way to call an API - as if it were a library - straight into the BIOS for access to Hardware.

To access a "read a sector" routine, its code had to load the parameters for the call into CPU-specific registers, and then run the "int 13h" machine statement - the BIOS ran the routine - read the data, and transfer it to the memory areade specified in the registers.

Because it depends on the exact values in the registers, in generate encoded in Assembler itself - at most as Assembler Inline code within a C function. The compilers always gave an alternative or other to write code that needed to do this.

Nowadays, if you have a PC that allows you to download a legacy BIOS, and a diskette drive, it is possible that you can write Assembler code by calling this routine directly, and put it in the boot sector of the diskette. - Pcs are compatible with Pcs from that time: you will write 16-bit Assembler (this - its 8th generation i5, with 8mb L1 cache, when it is switched on, wakes up thinking it is an 8086 with 1MB of maximum memory - and special machine instructions are required to change the CPU operating modes. Good - it may be that with current UEFI systems this is no longer true - but, if your boot is BIOS, yes, that’s it).

In the absence of a diskettes drive, you can try the same approah using the DOSBOX, and effectively programming a virtual machine in the DOS system - will be the best way for Voce to be able to do what you want.

(Look - I searched for BIOS int 13 disk access to see if I found well structured information on how this works - but, bad news - all fragmented, and nothing that "coemce from the beginning" to show how you can do it from the beginning (with compialdores, etc...)- even in an emulated DOS environment). Here at least you have the most documented int13h API: http://stanislavs.org/helppc/int_13.html

To access "32bit age" drives you may be able to use a Windows API directly - but, it will be uphill above: https://support.microsoft.com/en-us/kb/100027

Linux and the other Unixes: These systems expose disk devices as "a giant archive" - a single file of all disk size.

So as a user root, and the normal read, write and Seek calls in files, you move things straight on disk. This abstracts all part of calculation of sectors, etc... But.. since the time of the 512MB Hds, some part of the Hardware already abstracted it for you anyway. (So it didn’t even occur to me to give this explanation first when answering your question - and goes off talking about "how it was" )

If you want to see for example the boot sector of your hard drive on Linux, just look at the 512 bytes of the file /dev/sda - You can use a USB stick - it appears as a block device /dev/sdb - if you write a porgrama that is valid machine code in Assembler 8086 (16bit) and copy to the first 512 bytes of that device, when bootar by the USB stick, (turning off UEFI boot, etc...) - you will see your program running.

And finally - as to that part of your question:

In theory it would be, take some data, write to the sector, read the sector, copy the data to another sector and erase the data from the sector ??

With disk access as "block device" in Unix you can do this - but if you do, the chance is that you destroy the information contained in the disk files - because you have to respect the data structures of the file system that is on the disk.

Again, the file system used at the time of DOS is FAT -and is still used to this day in flash drives, memory cards, etc... is one of the simplest, and one of the only ones where it is possible to make an independent progress in accessing the data without using the file system’s own code itself.

  • Thank you very much Mr. jsbueno, you helped me a lot!!!! : D

  • These BIOS functions over these "int"s 13.1 and 13.4, do exactly what I need thank you very much. now I find q with some simple counters and a sorting algorithm I can solve my problem !! Thank you very much msm !!! ^_^

  • @Matheuschagas if the answer solved your problem, you can click on V next to the answer and mark your question as answered

Browser other questions tagged

You are not signed in. Login or sign up in order to post.