用 C 语言进行低级编写

发布于 2024-12-23 02:12:38 字数 1020 浏览 2 评论 0原文

如何使用 C 编程语言写入 HDD 上的任何块?

一个问题关于编写MBR 但它并没有涵盖太多 C 方面的内容。

由于文件描述符 - 正如这个词所说 - 用于文件,我想在这种情况下无法使用它们。包含低级 I/O C标准库中也是用文件描述符来实现的。

更准确地说:
这个问题是关于写入硬盘块而不是文件(与操作系统无关)。

上述问题的答案基本上建议使用 dd (coreutils) 在 UNIX 系统上。这就是为什么我在 C 中寻求一种方法。也许引导加载程序(GRUB)和引导部门病毒使用不同的技术?

我想更改文件描述符内部的实际指针不是合法的方法。

问题和限制:
我知道有某些方面需要记住,例如

  • 某些操作系统限制对卷的直接访问(例如Windows)
  • 写入错误以及将错误的数据写入某些块 可能会导致文件系统损坏(HDD 上的数据丢失)。
  • 防病毒软件可能会将其标记为可疑代码。

这个问题是偏理论性的。

How can I write to any block on my HDD using the C programming language?

There was a question about writing the MBR but it didn't cover the C aspects that much.

Since filedescriptors are - as the word says - for files, I guess there is no way to use them in this case. The Low-level I/O included in the C standard library is also implemented with filedescriptors.

To put it more precisely:

This question is rather about writing HDD blocks than files (OS independent).

The answers to the question mentioned above basically suggested using dd (coreutils) on UNIX-Systems. This is why I am asking for a way in C. Maybe bootloaders (GRUB) and boot sector viruses use different techniques?

I guess changing the actual pointer inside of a filedescriptor is not a legitimate way.

Problems and limitations:

I know that there are certain aspects to keep in mind, such as

  • Some operating systems restrict direct access to volumes (e.g. Windows)
  • Writing errors as well as writing the wrong data into certain blocks
    might result in filesystem corruption (loss of data on the HDD).
  • Antivirus-Software might flag it as suspicious code.

This question is oriented more theoretical.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

堇色安年 2024-12-30 02:12:38

C 语言可以通过函数 fopen/fclose/fread/fwrite 等来访问文件。但是该语言中没有块设备这样的东西(甚至没有< em>设备,就此而言)。

另一方面,POSIX 具有访问文件的低级函数打开/关闭/读/写,并具有块设备的概念。这些函数可以用于块设备,只要您遵循一些简单的规则(主要是块对齐)并且您知道设备的名称(/dev /xxx)。

如果您使用的是非 POSIX 系统,例如 Windows,那么操作系统将有特定的方式来处理块设备访问。例如,在 Windows 中,您可以将 CreateFile 函数与设备名称 \\.\PhysicalDrive0\\.\C: 或这样的。

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

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

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

递刀给你 2024-12-30 02:12:38

对于类 Unix 操作系统,这是毫无疑问的:硬盘就像任何其他文件一样是一个设备文件,并且像任何其他文件一样被访问。你得到一个文件描述符,你寻找,你写入,你使文件系统崩溃,一切都很好。

这意味着您也应该使用 C 语言中的普通文件 I/O 例程。区分高级 I/O 和低级 I/O 不是您的工作,而是操作系统的工作。因为现在大多数操作系统都支持设备即文件的隐喻,所以 C 中没有进一步的抽象。虽然这种方法可能不适用于 DOS 及其衍生版本,但这只是避免使用 DOS 以避免维护过时代码的一个原因。

For Unix-like operating systems, this is a non-question: The hard disk is a device file just like any other, and accessed just like any other file. You get a file descriptor, you seek, you write, you crash the filesystem, and all is good.

This implies that you should use the normal file I/O routines in C, too. Distinguishing between high-level and low-level I/O is not your job, but the operating system's. Because most operating systems nowadays support the device-as-file metaphor, there is no further abstraction in C. While this approach might not work for DOS and derivatives, this is only a reason to avoid DOS to avoid maintaining obsolete code.

青衫负雪 2024-12-30 02:12:38

如果您想让它独立于操作系统,那么您可能需要使用某种多态性。然后,您可以创建一个结构来支持您所需的功能。

您可以在其中创建一个结构,该结构包含指向指针的函数,当代码移动操作系统时,这些指针可以更改。然后,您需要为您想要支持的每个操作系统提供基于操作系统的实现。

If you want to make it OS independent then you probably want into using some kind of polymorphism. You then can create a structure to support your required functionality.

Where you create a structure that would then contain functions to pointers which can be changed when the code moves OS. You would then need a OS based implementation for each OS you want to support.

浅唱ヾ落雨殇 2024-12-30 02:12:38

这与 C 几乎没有关系。如何以编程方式访问硬件取决于 CPU 如何与该设备通信(即系统/硬件架构/设计),并且如果涉及任何操作系统,则取决于 CPU 如何与该设备通信。操作系统允许其中运行的其他软件访问该设备(如果有的话)。

在x86 PC上,如果没有运行操作系统,可以使用BIOS中断13h函数来读写HDD扇区。可以使用众所周知的 I/O 端口和 ATA(PI) 命令来实现相同的目的,而无需使用 BIOS。如果有DOS的话,也差不多,对硬件访问没有限制。如果有 Windows 或 Linux,操作系统不会让你做这种事情,除非你有足够的权限和/或以某种方式做事。

This has little to nothing to do with C. How one programmatically accesses a piece of hardware depends on how the CPU can communicate with that device (i.e. on the system/hardware architecture/design) and, if there's any OS involved, on how the OS lets other software running in it access the device (if at all).

On the x86 PC, if there's no OS running, you can use BIOS interrupt 13h functions to read and write HDD sectors. It's possible to use well-known I/O ports and ATA(PI) commands to achieve the same without using the BIOS. If there's DOS, it's about the same, there's no restriction on hardware accesses. If there's Windows or Linux, the OS won't let you do this kind of things unless you have sufficient privileges and/or do things in a certain way.

泡沫很甜 2024-12-30 02:12:38

open() close() read() write() 不是 C 语言的一部分,它们是操作系统的一部分。

此外:write() 不写入任何内容,它是一个系统调用,要求操作系统为您写入它。操作系统可以对此请求执行任何操作(例如忽略它)。

/dev/* 条目只是机器私有部分的一个钩子,由操作系统提供给您,使您能够执行其他方式无法执行的操作。但是,当您 open() 和 write() 其中一个 /dev/disk 设备时,操作系统仍然有权忽略您的请求。

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

Besides: write() doen not write anything, it is a systemcall asking the OS to write it for you. The OS can do anything it wants with this request (such as ignore it)

The /dev/* entries are just a hook into the machine's private parts, given to you by the OS to enable you do things that cannot be done otherwise. But when you open() and write() one of the /dev/disk devices, the OS still has the power to ignore your request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文