Low Level C Writing

How can I write any block on my hard drive using the C programming language?

There was a question about writing an MBR , but it doesn’t cover aspects of C. very often.

Since filedescriptors - as they say in the word - are for files, I think that in this case it is impossible to use them. The low-level I / O included in the C standard library is also implemented using filedescriptors.

More precisely:
This question is more about writing hard drive blocks than files (regardless of OS).

The answers to the above question basically suggested using dd (coreutils) on UNIX-Systems. That's why I ask you to specify the path in C. Perhaps the boot loaders ( GRUB ), and the boot sector viruses use different methods?

I assume that changing the actual pointer inside the filedescriptor is not a legal way.

Problems and limitations:
I know that there are certain aspects that need to be kept in mind, for example

  • Some operating systems restrict direct access to volumes (such as Windows)
  • Writing errors, as well as writing incorrect data to certain blocks, can lead to damage to the file system (data loss on the hard disk).
  • Antivirus software may flag it as suspicious code.

This question is oriented more theoretically.

+4
source share
5 answers

The C language has access to files with the functions fopen/fclose/fread/fwrite , etc. But in the language there is no such thing as a block device (not even a device).

POSIX, on the other hand, has low level open/close/read/write functions for accessing files and has the concept of a block device. These functions can be used (with caution) for a block device if you follow a few simple rules (mainly block alignment) and you know the name of your device ( /dev/xxx ).

If you are on a system other than POSIX, such as Windows, then the OS will have a specific way to handle access to a block device. On Windows, for example, you can use the CreateFile function with the device name \\.\PhysicalDrive0 , \\.\C: or such.

+4
source

For Unix-like operating systems, this is not a question: a hard disk is a device file, like any other, and access to it, like any other file. You get a file descriptor, you look, write, you break the file system, and all is well.

This means that you should also use the usual file I / O routines in C. The distinction between high-level and low-level I / O is not your job, but the operating system. Since most operating systems currently support the device-as-file metaphor, there is no additional abstraction in C. Although this approach may not work for DOS and derivatives, this is just a reason to avoid DOS, to avoid storing obsolete code.

+1
source

If you want to make it OS independent, you probably want to use some kind of polymorphism. Then you can create a structure to support the required functions.

Where do you create a structure that will then contain pointer functions that can be changed when the code moves the OS. Then you will need an OS-based implementation for each OS that you want to support.

+1
source

This has nothing to do with C. How one software access to a piece of hardware depends on how the CPU can interact with this device (for example, in a system / hardware architecture / design) and, if there is any OS, about how the OS allows other software running on it to access the device (if at all).

On an x86 computer, if there is no operating system, you can use the 13h BIOS interrupt functions to read and write HDD sectors. You can use well-known I / O ports and ATA (PI) commands to achieve the same without using the BIOS. If DOS, then about the same, there are no restrictions on access to hardware. If you have Windows or Linux, the OS will not allow you to do such things if you do not have sufficient privileges and / or do something in a certain way.

0
source

open () close () read () write () are not part of the C language, they are part of the OS.

In addition: write () doen does not write anything, it is a system call that prompts the OS to write it for you. OS can do whatever it wants with this request (for example, ignore it)

The / dev / * entries are just a hook into the private part of the machine provided to you by the OS so that you can do things that cannot be done otherwise. But when you open () and write () one of the / dev / disk devices, the OS still has the right to ignore your request.

-1
source

Source: https://habr.com/ru/post/1388278/


All Articles