I have been working on a fantasy console type project. I decided to zero a flash drive I had laying around and use it as the ROM input to the fantasy console. While trying to write code to read bytes directly from the drive (rather than from a file on the drive) I have found a peculiar bug.
Trying to open the drive via CreateFile
and ReadFile
with the file path \\.\\PhysicalDrive1
results in the error ""\\.\\PhysicalDrive1" is not a core dump: file format not recognized
"
I have tried changing input options and I have tried alternate file paths, but anything I try results in the drive either not being found or the error shown above. Information on this issue is very scarse from my research, and what little info I have found is specific to Linux, and involves very different situations.
Here is my code (not the cleanest but it should work):
#include <windows.h>
#include <fileapi.h>
#include <stdio.h>
#include <stdint.h>
int main(int argc, char *argv[]) {
printf("Opening %s\n", argv[1]);
HANDLE romf = CreateFile((LPCSTR)argv[1],
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (romf == INVALID_HANDLE_VALUE) {
printf("File %s not found\n", argv[1]);
return -1;
}
uint8_t rom[4] = { 0 };
LPDWORD read = 0;
int res = ReadFile(romf, rom, 4, read, NULL);
if (res == 0) {
printf("Error reading file (%d)\n", argv[1], GetLastError());
CloseHandle(romf);
return -1;
}
printf("Read $%x bytes\n", read);
printf("%02x %02x %02x %02x\n", rom[0], rom[1], rom[2], rom[3]);
CloseHandle(romf);
return 0;
}
I have used gdb
to debug the code and to get the error message.