Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    A low-level access to hard drives under linux?

    I am making a hard drive modifying function for my program. I need a low-level access to hard drives (IDE/SATA)(write/read). Does C have a simple method to do this? I need to write/read single bytes and bits.

  2. #2
    Linux does not have a library call to do direct disk I/O - sometimes called physical I/O. All direct I/O is under the control of the kernel.
    The reason is that you could write to a file (using an offset on to hard drive) that is part of a file you don't own, or may not have permissions to write.

    If you actually need to twiddle around in a file like that, then you will have to call mmap to map a file into memory. Then you can do exactly what you want.

    Old MS-DOS systems running TURBO C did have such a thing - called a BIOS interrupt.

  3. #3
    Linux Guru
    Join Date
    Oct 2001
    Täby, Sweden
    Actually, there are many ways to access a disk at a lower level. There are also many lower levels.

    First of all, though: C in itself does not provide any such thing, since hardware is platform dependent, and C itself only does platform independent things. It is Linux, not C, that provides disk access.

    So, the question is just how low-level you wish to access the disk. If you'd be content with just reading and writing arbitrary bytes to and from the disk, just open the corresponding device file (e.g. /dev/hda for the first IDE disk) and read and write it. Of course, as with any other file, you'll need permission to open the device file.

    If you want to do direct ATA calls to the disk, you'll need to find an ioctl to call. The ioctl would also be called directly on the device file, though.

    If you need to access the IDE/S-ATA controller directly (for whatever reason), you'll need to either turn off I/O space protection using either the ioperm or the iopl syscalls, or map the device controller's I/O range somewhere in your own address space, using /dev/mem. I'm not sure how to hook IRQ lines in user space, however, so if you need IRQ service, you'll probably need to write kernel code.

  4. $spacer_open

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts