使用C / C ++访问RAW磁盘

时间:2022-08-17 08:58:36

I have a large storage device (flash memory) plugged onto my computer via the PCIe bus, I want to access such device directly, i.e., without any file system (e.g., NTFS or ext4) on it.

我有一个大型存储设备(闪存)通过PCIe总线插入我的计算机,我想直接访问这样的设备,即没有任何文件系统(例如,NTFS或ext4)。

How can I do this using C/C++? (on both Windows 7 and Linux) I am wondering if I can 1) open the device just as a file, and then read and write binary data to it, or 2) allocate the whole device using some function like malloc, then each byte on the device have an address so that I can access them based on the addresses.

我怎么能用C / C ++做到这一点? (在Windows 7和Linux上)我想知道我是否可以1)打开设备作为文件,然后读取和写入二进制数据,或2)使用像malloc这样的函数分配整个设备,然后每个字节在设备上有一个地址,以便我可以根据地址访问它们。

I prefer the second way if it possible, but I don't know if the OS supports this since it seems the address space needs to be shared with the main memory.

如果可能的话,我更喜欢第二种方式,但我不知道OS是否支持这个,因为看起来地址空间需要与主内存共享。

2 个解决方案

#1


7  

On Linux each storage device ends up getting a device entry in /dev. The first storage device is typically /dev/sda, the second storage device, if one is present, is /dev/sdb. Note that an optical disk is a storage device, so a CD-ROM or a DVD-ROM drive, if one is present, would get a device node entry.

在Linux上,每个存储设备最终都会在/ dev中获取设备条目。第一存储设备通常是/ dev / sda,第二存储设备(如果存在的话)是/ dev / sdb。请注意,光盘是存储设备,因此CD-ROM或DVD-ROM驱动器(如果存在)将获得设备节点条目。

Some Linux distributions may use a different naming convention, but this is what it usually is. So, you'll need to figure out which device corresponds to your flash disk, and just open the /dev/sdX device, and simply read and write from it. Your reads and writes must be for even block (sector) sizes, and seeking the opened file governs which disk blocks/sectors the subsequent read or write will affect.

某些Linux发行版可能使用不同的命名约定,但这通常是它。因此,您需要确定哪个设备对应于您的闪存盘,然后只需打开/ dev / sdX设备,然后只需从中读取和写入。您的读取和写入必须是偶数块(扇区)大小,并且寻找打开的文件将控制后续读取或写入将影响的磁盘块/扇区。

Generally, /dev/sdX will be owned by root, but there are usually some Linux distribution-specific ways to fiddle the userid that owns a particular device node.

通常,/ dev / sdX将由root拥有,但通常有一些特定于Linux发行版的方法来摆弄拥有特定设备节点的用户标识。

#2


10  

According to Microsoft documentation:

根据Microsoft文档:

On Windows you can open a physical drive using CreateFile using a path of the form

在Windows上,您可以使用表单的路径使用CreateFile打开物理驱动器

\\.\PhysicalDriveN

where N is the device number or a logical drive using a path of the form

其中N是设备号或使用表单路径的逻辑驱动器

\\.\X:

You will need to seek, read and write in multiples of the sector size which can be retrieved using DeviceIoControl() with IOCTL_DISK_GET_DRIVE_GEOMETRY.

您需要以扇区大小的倍数进行搜索,读取和写入,可以使用IOCTL_DISK_GET_DRIVE_GEOMETRY使用DeviceIoControl()检索。

#1


7  

On Linux each storage device ends up getting a device entry in /dev. The first storage device is typically /dev/sda, the second storage device, if one is present, is /dev/sdb. Note that an optical disk is a storage device, so a CD-ROM or a DVD-ROM drive, if one is present, would get a device node entry.

在Linux上,每个存储设备最终都会在/ dev中获取设备条目。第一存储设备通常是/ dev / sda,第二存储设备(如果存在的话)是/ dev / sdb。请注意,光盘是存储设备,因此CD-ROM或DVD-ROM驱动器(如果存在)将获得设备节点条目。

Some Linux distributions may use a different naming convention, but this is what it usually is. So, you'll need to figure out which device corresponds to your flash disk, and just open the /dev/sdX device, and simply read and write from it. Your reads and writes must be for even block (sector) sizes, and seeking the opened file governs which disk blocks/sectors the subsequent read or write will affect.

某些Linux发行版可能使用不同的命名约定,但这通常是它。因此,您需要确定哪个设备对应于您的闪存盘,然后只需打开/ dev / sdX设备,然后只需从中读取和写入。您的读取和写入必须是偶数块(扇区)大小,并且寻找打开的文件将控制后续读取或写入将影响的磁盘块/扇区。

Generally, /dev/sdX will be owned by root, but there are usually some Linux distribution-specific ways to fiddle the userid that owns a particular device node.

通常,/ dev / sdX将由root拥有,但通常有一些特定于Linux发行版的方法来摆弄拥有特定设备节点的用户标识。

#2


10  

According to Microsoft documentation:

根据Microsoft文档:

On Windows you can open a physical drive using CreateFile using a path of the form

在Windows上,您可以使用表单的路径使用CreateFile打开物理驱动器

\\.\PhysicalDriveN

where N is the device number or a logical drive using a path of the form

其中N是设备号或使用表单路径的逻辑驱动器

\\.\X:

You will need to seek, read and write in multiples of the sector size which can be retrieved using DeviceIoControl() with IOCTL_DISK_GET_DRIVE_GEOMETRY.

您需要以扇区大小的倍数进行搜索,读取和写入,可以使用IOCTL_DISK_GET_DRIVE_GEOMETRY使用DeviceIoControl()检索。