Hey everyone! I am glad to have access to post this here for all of you to share your ideas. I am making a hardware emulator and I have a flash BIOS file that I need it to parse for validation purposes. The file opens like this (ASCII):
"X System Firmware - Copyright 2015."
(Above is just an example so you get the gist!)
What I need for my emulator is to enable the user to select a drop down button and open the file dialog to view the filesystem (Windows). I have that down, but my problem is with reading inside the file and properly handling it. I have searched tutorials but they are not comprehensive and complete enough for me to understand how to do this straight with the Windows API file dialog function, where the data is returned, how to read it a certain way, etc., etc., etc.
I will show the code a little later, but let me just explain a little bit of what I need it to do:
I need the file the user selects (ANY file; no restrictions) to be read right from the beginning, binary-style, all byte-by-byte, one after another, and check each byte that's read against an equivalent ASCII character (with case-sensitivity). What that means is if the hex/binary value read from byte 1 is equal to "X" and it's uppercase, of course, it will match with the first byte in a character pointer in memory that is "X" that it will be checked against, then moving on. Instead of going with more bytes at once or comparing a whole string, I want it done this way for authenticity like a real computer system I'm emulating.
This part of the emulator is known as the "flash parser/validation checker" when executing firmware; the rest of it is useless as any code/date/etc. will be taken over by the emulated CPU interpreter, memory, and other hardware.
The purpose is to make sure that it's the right magic numbers within the right offsets of the file(I know all of the right offsets; what I need to know is how to mostly do this from an OpenFile struct and with Windows API/C++).
Here is relevant code I'm up to at this point and stuck at:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
BOOL WINAPI GetOpenFileName(LPOPENFILENAMEA LPOFNA);
BOOL SelectFile(HWND EDIT, LPCSTR FILEx)
BOOL LoadFile(HWND FILEHNDL, LPCSTR file_to_load);
BOOL SelectFile(HWND EDIT, LPCSTR FILEx);
{
OPENFILENAME OFN;
char fsize[200];
ZeroMemory(&OFN, sizeof(OFN));
OFN.lStructSize = sizeof(OFN);
OFN.hwndOwner = EDIT;
OFN.lpstrFile = fsize;
OFN.lpstrFile[0] ='\0';
OFN.nMaxFile = sizeof(fsize);
OFN.nFilterIndex = 1;
OFN.lpstrFileTitle = NULL;
OFN.nMaxFileTitle = 0;
OFN.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST;
OFN.lpstrFilter = NULL;
OFN.lpstrInitialDir = NULL;
HANDLE hfile;
BOOL SUCCEED = 0;
if(GetOpenFileName(&OFN) == TRUE)
{
LoadFile(EDIT, &OFN.lpstrFile[0]);
}
else
{
MessageBox(EDIT, "FAILED.", "FAILED.", MB_OK);
}
}
BOOL LoadFile(HWND FILEHNDL, LPCSTR file_to_load)
{
HANDLE F;
BOOL SUCCESSFUL = FALSE;
F = CreateFile(file_to_load, GENERIC_READ, NULL, NULL, OPEN_EXISTING, 0, NULL);
}
|
The function "SelectFile()" is called from a switch statement in the main window's message loop under an AppendMenu() ID with the LOWORD of wParam.
Example:
1 2 3 4 5 6
|
case WM_COMMAND:
switch(LOWORD(wParam))
{
case SELECTEDLOADFILE:
SelectFile(hwnd, NULL);
break;
|
Whenever I load ANY file, the program closes immediately because I don't know how to get it to handle the file correctly. I have tried my own intuition to see if I can do it, but all I could figure out what to do is show a message when a file has been loaded or not; still not sure how it's read via the struct and how to jump in the file and view it after it's loaded this way.