2011-11-27 02:09:45 +00:00
|
|
|
/*
|
2011-12-05 11:36:02 +00:00
|
|
|
* Rufus: The Reliable USB Formatting Utility
|
2011-12-21 00:53:31 +00:00
|
|
|
* DOS boot file extraction, from the FAT12 floppy image in diskcopy.dll
|
|
|
|
* (MS WinME DOS) or from the embedded FreeDOS resource files
|
2015-01-23 02:26:41 +00:00
|
|
|
* Copyright © 2011-2015 Pete Batard <pete@akeo.ie>
|
2011-11-27 02:09:45 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Memory leaks detection - define _CRTDBG_MAP_ALLOC as preprocessor macro */
|
|
|
|
#ifdef _CRTDBG_MAP_ALLOC
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <windows.h>
|
2012-01-12 02:52:40 +00:00
|
|
|
#include <windowsx.h>
|
2011-11-27 02:09:45 +00:00
|
|
|
#include <stdio.h>
|
2013-01-25 01:38:10 +00:00
|
|
|
#include <stdlib.h>
|
2011-11-27 02:09:45 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "rufus.h"
|
2011-12-21 00:53:31 +00:00
|
|
|
#include "dos.h"
|
2011-12-15 00:46:47 +00:00
|
|
|
#include "resource.h"
|
2011-11-27 02:09:45 +00:00
|
|
|
|
2013-01-25 01:38:10 +00:00
|
|
|
static BYTE* DiskImage = NULL;
|
|
|
|
static DWORD DiskImageSize;
|
2011-11-27 02:09:45 +00:00
|
|
|
|
2011-11-30 17:42:25 +00:00
|
|
|
/*
|
|
|
|
* FAT time conversion, from ReactOS' time.c
|
|
|
|
*/
|
|
|
|
#define TICKSPERMIN 600000000
|
|
|
|
#define TICKSPERSEC 10000000
|
|
|
|
#define TICKSPERMSEC 10000
|
|
|
|
#define SECSPERDAY 86400
|
|
|
|
#define SECSPERHOUR 3600
|
|
|
|
#define SECSPERMIN 60
|
|
|
|
#define MINSPERHOUR 60
|
|
|
|
#define HOURSPERDAY 24
|
|
|
|
#define EPOCHWEEKDAY 1
|
|
|
|
#define DAYSPERWEEK 7
|
|
|
|
#define EPOCHYEAR 1601
|
|
|
|
#define DAYSPERNORMALYEAR 365
|
|
|
|
#define DAYSPERLEAPYEAR 366
|
|
|
|
#define MONSPERYEAR 12
|
|
|
|
|
|
|
|
typedef struct _TIME_FIELDS {
|
|
|
|
short Year;
|
|
|
|
short Month;
|
|
|
|
short Day;
|
|
|
|
short Hour;
|
|
|
|
short Minute;
|
|
|
|
short Second;
|
|
|
|
short Milliseconds;
|
|
|
|
short Weekday;
|
|
|
|
} TIME_FIELDS, *PTIME_FIELDS;
|
|
|
|
|
|
|
|
#define ARGUMENT_PRESENT(ArgumentPointer) \
|
|
|
|
((CHAR*)((ULONG_PTR)(ArgumentPointer)) != (CHAR*)NULL)
|
|
|
|
|
|
|
|
static const int YearLengths[2] =
|
|
|
|
{
|
|
|
|
DAYSPERNORMALYEAR, DAYSPERLEAPYEAR
|
|
|
|
};
|
|
|
|
static const UCHAR MonthLengths[2][MONSPERYEAR] =
|
|
|
|
{
|
|
|
|
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
|
|
|
|
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static __inline int IsLeapYear(int Year)
|
|
|
|
{
|
|
|
|
return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int DaysSinceEpoch(int Year)
|
|
|
|
{
|
|
|
|
int Days;
|
|
|
|
Year--; /* Don't include a leap day from the current year */
|
|
|
|
Days = Year * DAYSPERNORMALYEAR + Year / 4 - Year / 100 + Year / 400;
|
|
|
|
Days -= (EPOCHYEAR - 1) * DAYSPERNORMALYEAR + (EPOCHYEAR - 1) / 4 - (EPOCHYEAR - 1) / 100 + (EPOCHYEAR - 1) / 400;
|
|
|
|
return Days;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOLEAN RtlTimeFieldsToTime(PTIME_FIELDS TimeFields, PLARGE_INTEGER Time)
|
|
|
|
{
|
|
|
|
int CurMonth;
|
|
|
|
TIME_FIELDS IntTimeFields;
|
|
|
|
|
|
|
|
memcpy(&IntTimeFields,
|
|
|
|
TimeFields,
|
|
|
|
sizeof(TIME_FIELDS));
|
|
|
|
|
|
|
|
if (TimeFields->Milliseconds < 0 || TimeFields->Milliseconds > 999 ||
|
|
|
|
TimeFields->Second < 0 || TimeFields->Second > 59 ||
|
|
|
|
TimeFields->Minute < 0 || TimeFields->Minute > 59 ||
|
|
|
|
TimeFields->Hour < 0 || TimeFields->Hour > 23 ||
|
|
|
|
TimeFields->Month < 1 || TimeFields->Month > 12 ||
|
|
|
|
TimeFields->Day < 1 ||
|
|
|
|
TimeFields->Day >
|
|
|
|
MonthLengths[IsLeapYear(TimeFields->Year)][TimeFields->Month - 1] ||
|
|
|
|
TimeFields->Year < 1601) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the time */
|
|
|
|
Time->QuadPart = DaysSinceEpoch(IntTimeFields.Year);
|
|
|
|
for (CurMonth = 1; CurMonth < IntTimeFields.Month; CurMonth++) {
|
|
|
|
Time->QuadPart += MonthLengths[IsLeapYear(IntTimeFields.Year)][CurMonth - 1];
|
|
|
|
}
|
|
|
|
Time->QuadPart += IntTimeFields.Day - 1;
|
|
|
|
Time->QuadPart *= SECSPERDAY;
|
|
|
|
Time->QuadPart += IntTimeFields.Hour * SECSPERHOUR + IntTimeFields.Minute * SECSPERMIN +
|
|
|
|
IntTimeFields.Second;
|
|
|
|
Time->QuadPart *= TICKSPERSEC;
|
|
|
|
Time->QuadPart += IntTimeFields.Milliseconds * TICKSPERMSEC;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FatDateTimeToSystemTime(PLARGE_INTEGER SystemTime, PFAT_DATETIME FatDateTime, UCHAR TenMs OPTIONAL)
|
|
|
|
{
|
|
|
|
TIME_FIELDS TimeFields;
|
|
|
|
|
|
|
|
/* Setup time fields */
|
|
|
|
TimeFields.Year = FatDateTime->Date.Year + 1980;
|
|
|
|
TimeFields.Month = FatDateTime->Date.Month;
|
|
|
|
TimeFields.Day = FatDateTime->Date.Day;
|
|
|
|
TimeFields.Hour = FatDateTime->Time.Hour;
|
|
|
|
TimeFields.Minute = FatDateTime->Time.Minute;
|
|
|
|
TimeFields.Second = (FatDateTime->Time.DoubleSeconds << 1);
|
|
|
|
|
|
|
|
/* Adjust up to 10 milliseconds
|
|
|
|
* if the parameter was supplied
|
|
|
|
*/
|
|
|
|
if (ARGUMENT_PRESENT(TenMs)) {
|
|
|
|
TimeFields.Second += TenMs / 100;
|
|
|
|
TimeFields.Milliseconds = (TenMs % 100) * 10;
|
|
|
|
} else {
|
|
|
|
TimeFields.Milliseconds = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fix seconds value that might get beyond the bound */
|
|
|
|
if (TimeFields.Second > 59) TimeFields.Second = 0;
|
|
|
|
|
2013-06-20 14:41:22 +00:00
|
|
|
/* Perform conversion to system time if possible */
|
2011-11-30 17:42:25 +00:00
|
|
|
if (!RtlTimeFieldsToTime(&TimeFields, SystemTime)) {
|
|
|
|
/* Set to default time if conversion failed */
|
|
|
|
SystemTime->QuadPart = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* http://www.multiboot.ru/msdos8.htm & http://en.wikipedia.org/wiki/Windows_Me#Real_mode_DOS
|
|
|
|
* COMMAND.COM and IO.SYS from diskcopy.dll are from the WinME crippled version
|
|
|
|
* that removed real mode DOS => they must be patched:
|
|
|
|
* IO.SYS 000003AA 75 -> EB
|
|
|
|
* COMMAND.COM 00006510 75 -> EB
|
|
|
|
*/
|
2013-01-25 01:38:10 +00:00
|
|
|
static BOOL Patch_COMMAND_COM(size_t filestart, size_t filesize)
|
2011-11-30 17:42:25 +00:00
|
|
|
{
|
|
|
|
const BYTE expected[8] = { 0x15, 0x80, 0xFA, 0x03, 0x75, 0x10, 0xB8, 0x0E };
|
|
|
|
|
|
|
|
uprintf("Patching COMMAND.COM...\n");
|
2013-01-25 01:38:10 +00:00
|
|
|
if (filesize != 93040) {
|
2011-11-30 17:42:25 +00:00
|
|
|
uprintf(" unexpected file size\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-01-25 01:38:10 +00:00
|
|
|
if (memcmp(&DiskImage[filestart+0x650c], expected, sizeof(expected)) != 0) {
|
2011-11-30 17:42:25 +00:00
|
|
|
uprintf(" unexpected binary data\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-01-25 01:38:10 +00:00
|
|
|
DiskImage[filestart+0x6510] = 0xeb;
|
2011-11-30 17:42:25 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-01-25 01:38:10 +00:00
|
|
|
static BOOL Patch_IO_SYS(size_t filestart, size_t filesize)
|
2011-11-30 17:42:25 +00:00
|
|
|
{
|
|
|
|
const BYTE expected[8] = { 0xFA, 0x80, 0x75, 0x09, 0x8D, 0xB6, 0x99, 0x00 };
|
|
|
|
|
|
|
|
uprintf("Patching IO.SYS...\n");
|
2013-01-25 01:38:10 +00:00
|
|
|
if (filesize != 116736) {
|
2011-11-30 17:42:25 +00:00
|
|
|
uprintf(" unexpected file size\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-01-25 01:38:10 +00:00
|
|
|
if (memcmp(&DiskImage[filestart+0x3a8], expected, sizeof(expected)) != 0) {
|
2011-11-30 17:42:25 +00:00
|
|
|
uprintf(" unexpected binary data\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-01-25 01:38:10 +00:00
|
|
|
DiskImage[filestart+0x3aa] = 0xeb;
|
2011-11-30 17:42:25 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-11-27 02:09:45 +00:00
|
|
|
/* Extract the file identified by FAT RootDir index 'entry' to 'path' */
|
|
|
|
static BOOL ExtractFAT(int entry, const char* path)
|
|
|
|
{
|
2011-11-30 17:42:25 +00:00
|
|
|
HANDLE hFile;
|
|
|
|
DWORD Size;
|
2011-11-27 02:09:45 +00:00
|
|
|
char filename[MAX_PATH];
|
2011-11-30 17:42:25 +00:00
|
|
|
size_t i, pos, fnamepos;
|
|
|
|
size_t filestart, filesize;
|
|
|
|
FAT_DATETIME LastAccessTime;
|
|
|
|
LARGE_INTEGER liCreationTime, liLastAccessTime, liLastWriteTime;
|
|
|
|
FILETIME ftCreationTime, ftLastAccessTime, ftLastWriteTime;
|
|
|
|
PDIR_ENTRY dir_entry = (PDIR_ENTRY)&DiskImage[FAT12_ROOTDIR_OFFSET + entry*FAT_BYTES_PER_DIRENT];
|
2011-11-27 02:09:45 +00:00
|
|
|
|
|
|
|
if ((path == NULL) || ((safe_strlen(path) + 14) > sizeof(filename))) {
|
|
|
|
uprintf("invalid path supplied for MS-DOS FAT extraction\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
strcpy(filename, path);
|
|
|
|
pos = strlen(path);
|
2011-11-30 17:42:25 +00:00
|
|
|
fnamepos = pos;
|
2011-11-27 02:09:45 +00:00
|
|
|
|
|
|
|
for(i=0; i<8; i++) {
|
2011-11-30 17:42:25 +00:00
|
|
|
if (dir_entry->FileName[i] == ' ')
|
2011-11-27 02:09:45 +00:00
|
|
|
break;
|
2011-11-30 17:42:25 +00:00
|
|
|
filename[pos++] = dir_entry->FileName[i];
|
2011-11-27 02:09:45 +00:00
|
|
|
}
|
|
|
|
filename[pos++] = '.';
|
|
|
|
for (i=8; i<11; i++) {
|
2011-11-30 17:42:25 +00:00
|
|
|
if (dir_entry->FileName[i] == ' ')
|
2011-11-27 02:09:45 +00:00
|
|
|
break;
|
2011-11-30 17:42:25 +00:00
|
|
|
filename[pos++] = dir_entry->FileName[i];
|
2011-11-27 02:09:45 +00:00
|
|
|
}
|
|
|
|
filename[pos] = 0;
|
2011-11-30 17:42:25 +00:00
|
|
|
filestart = (dir_entry->FirstCluster + FAT12_CLUSTER_OFFSET)*FAT12_CLUSTER_SIZE;
|
|
|
|
filesize = dir_entry->FileSize;
|
2011-11-27 02:09:45 +00:00
|
|
|
if ((filestart + filesize) > DiskImageSize) {
|
2011-11-30 17:42:25 +00:00
|
|
|
uprintf("FAT File %s would be out of bounds: %X, %X\n", filename, filestart, filesize);
|
|
|
|
uprintf("%X, %X\n", dir_entry->FirstCluster, dir_entry->FileSize);
|
2011-11-27 02:09:45 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-01-25 01:38:10 +00:00
|
|
|
/* WinME DOS files need to be patched */
|
|
|
|
if (strcmp(&filename[fnamepos], "COMMAND.COM") == 0) {
|
|
|
|
Patch_COMMAND_COM(filestart, filesize);
|
|
|
|
} else if (strcmp(&filename[fnamepos], "IO.SYS") == 0) {
|
|
|
|
Patch_IO_SYS(filestart, filesize);
|
|
|
|
}
|
|
|
|
|
2011-11-30 17:42:25 +00:00
|
|
|
/* Create a file, using the same attributes as found in the FAT */
|
|
|
|
hFile = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
|
2014-11-11 19:17:39 +00:00
|
|
|
NULL, CREATE_ALWAYS, dir_entry->Attributes, NULL);
|
2011-11-30 17:42:25 +00:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE) {
|
|
|
|
uprintf("Unable to create file '%s': %s.\n", filename, WindowsErrorString());
|
2011-11-27 02:09:45 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-11-30 17:42:25 +00:00
|
|
|
if ((!WriteFile(hFile, &DiskImage[filestart], (DWORD)filesize, &Size, 0)) || (filesize != Size)) {
|
|
|
|
uprintf("Couldn't write file '%s': %s.\n", filename, WindowsErrorString());
|
|
|
|
safe_closehandle(hFile);
|
2011-12-15 00:46:47 +00:00
|
|
|
return FALSE;
|
2011-11-30 17:42:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore timestamps from FAT */
|
|
|
|
FatDateTimeToSystemTime(&liCreationTime, &dir_entry->CreationDateTime, dir_entry->CreationTimeTenMs);
|
|
|
|
ftCreationTime.dwHighDateTime = liCreationTime.HighPart;
|
|
|
|
ftCreationTime.dwLowDateTime = liCreationTime.LowPart;
|
|
|
|
LastAccessTime.Value = 0;
|
|
|
|
LastAccessTime.Date = dir_entry->LastAccessDate;
|
|
|
|
FatDateTimeToSystemTime(&liLastAccessTime, &LastAccessTime, 0);
|
|
|
|
ftLastAccessTime.dwHighDateTime = liLastAccessTime.HighPart;
|
|
|
|
ftLastAccessTime.dwLowDateTime = liLastAccessTime.LowPart;
|
|
|
|
FatDateTimeToSystemTime(&liLastWriteTime, &dir_entry->LastWriteDateTime, 0);
|
|
|
|
ftLastWriteTime.dwHighDateTime = liLastWriteTime.HighPart;
|
|
|
|
ftLastWriteTime.dwLowDateTime = liLastWriteTime.LowPart;
|
|
|
|
if (!SetFileTime(hFile, &ftCreationTime, &ftLastAccessTime, &ftLastWriteTime)) {
|
|
|
|
uprintf("Could not set timestamps: %s\n", WindowsErrorString());
|
|
|
|
}
|
|
|
|
|
|
|
|
safe_closehandle(hFile);
|
2013-06-20 14:41:22 +00:00
|
|
|
uprintf("Successfully wrote '%s' (%d bytes)\n", filename, filesize);
|
2011-11-27 02:09:45 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract the MS-DOS files contained in the FAT12 1.4MB floppy
|
|
|
|
image included as resource "BINFILE" in diskcopy.dll */
|
2011-12-15 00:46:47 +00:00
|
|
|
static BOOL ExtractMSDOS(const char* path)
|
2011-11-27 02:09:45 +00:00
|
|
|
{
|
|
|
|
char dllname[MAX_PATH] = "C:\\Windows\\System32";
|
|
|
|
int i, j;
|
2015-01-23 02:26:41 +00:00
|
|
|
UINT len;
|
2013-01-25 01:38:10 +00:00
|
|
|
BOOL r = FALSE;
|
|
|
|
HMODULE hDLL = NULL;
|
2011-12-21 00:53:31 +00:00
|
|
|
char locale_path[MAX_PATH];
|
|
|
|
char* extractlist[] = { "MSDOS SYS", "COMMAND COM", "IO SYS", "MODE COM",
|
|
|
|
"KEYB COM", "KEYBOARDSYS", "KEYBRD2 SYS", "KEYBRD3 SYS", "KEYBRD4 SYS",
|
|
|
|
"DISPLAY SYS", "EGA CPI", "EGA2 CPI", "EGA3 CPI" };
|
2011-11-27 02:09:45 +00:00
|
|
|
|
2011-12-21 00:53:31 +00:00
|
|
|
// Reduce the visible mess by placing all the locale files into a subdir
|
|
|
|
safe_strcpy(locale_path, sizeof(locale_path), path);
|
2013-01-25 01:38:10 +00:00
|
|
|
safe_strcat(locale_path, sizeof(locale_path), "LOCALE\\");
|
2011-12-21 00:53:31 +00:00
|
|
|
CreateDirectoryA(locale_path, NULL);
|
2011-11-27 02:09:45 +00:00
|
|
|
|
2015-01-23 02:26:41 +00:00
|
|
|
len = GetSystemDirectoryA(dllname, sizeof(dllname));
|
|
|
|
if ((len == 0) || (len >= sizeof(dllname))) {
|
|
|
|
uprintf("Unable to get system directory: %s\n", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
2011-11-27 02:09:45 +00:00
|
|
|
safe_strcat(dllname, sizeof(dllname), "\\diskcopy.dll");
|
|
|
|
hDLL = LoadLibraryA(dllname);
|
|
|
|
if (hDLL == NULL) {
|
|
|
|
uprintf("Unable to open %s: %s\n", dllname, WindowsErrorString());
|
2013-01-25 01:38:10 +00:00
|
|
|
goto out;
|
2011-11-27 02:09:45 +00:00
|
|
|
}
|
2013-01-25 01:38:10 +00:00
|
|
|
|
|
|
|
DiskImage = (BYTE*)GetResource(hDLL, MAKEINTRESOURCEA(1), "BINFILE", "disk image", &DiskImageSize, TRUE);
|
|
|
|
if (DiskImage == NULL)
|
|
|
|
goto out;
|
|
|
|
|
2011-11-27 02:09:45 +00:00
|
|
|
// Sanity check
|
|
|
|
if (DiskImageSize < 700*1024) {
|
|
|
|
uprintf("MS-DOS disk image is too small (%d bytes)\n", dllname, DiskImageSize);
|
2013-01-25 01:38:10 +00:00
|
|
|
goto out;
|
2011-11-27 02:09:45 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 01:38:10 +00:00
|
|
|
for (i=0, r=TRUE; r && i<FAT_FN_DIR_ENTRY_LAST; i++) {
|
2011-11-30 17:42:25 +00:00
|
|
|
if (DiskImage[FAT12_ROOTDIR_OFFSET + i*FAT_BYTES_PER_DIRENT] == FAT_DIRENT_DELETED)
|
2011-11-27 02:09:45 +00:00
|
|
|
continue;
|
2011-12-06 02:23:28 +00:00
|
|
|
for (j=0; r && j<ARRAYSIZE(extractlist); j++) {
|
2011-11-30 17:42:25 +00:00
|
|
|
if (memcmp(extractlist[j], &DiskImage[FAT12_ROOTDIR_OFFSET + i*FAT_BYTES_PER_DIRENT], 8+3) == 0) {
|
2011-12-21 00:53:31 +00:00
|
|
|
r = ExtractFAT(i, (j<3)?path:locale_path);
|
2011-12-25 22:24:38 +00:00
|
|
|
if ((j == 2) || (j == 7) || (j == 12))
|
2011-12-21 00:53:31 +00:00
|
|
|
UpdateProgress(OP_DOS, -1.0f);
|
2011-11-27 02:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-25 22:24:38 +00:00
|
|
|
if (r)
|
|
|
|
r = SetDOSLocale(path, FALSE);
|
2011-11-30 17:42:25 +00:00
|
|
|
|
2013-01-25 01:38:10 +00:00
|
|
|
out:
|
|
|
|
if (hDLL != NULL)
|
|
|
|
FreeLibrary(hDLL);
|
|
|
|
safe_free(DiskImage);
|
2011-12-06 02:23:28 +00:00
|
|
|
return r;
|
2011-11-27 02:09:45 +00:00
|
|
|
}
|
2011-12-15 00:46:47 +00:00
|
|
|
|
|
|
|
/* Extract the FreeDOS files embedded in the app */
|
|
|
|
BOOL ExtractFreeDOS(const char* path)
|
|
|
|
{
|
2011-12-25 22:24:38 +00:00
|
|
|
const char* res_name[] = { "COMMAND.COM", "KERNEL.SYS", "DISPLAY.EXE", "KEYB.EXE",
|
|
|
|
"MODE.COM", "KEYBOARD.SYS", "KEYBRD2.SYS", "KEYBRD3.SYS", "KEYBRD4.SYS", "ega.cpx",
|
|
|
|
"ega2.cpx", "ega3.cpx", "ega4.cpx", "ega5.cpx", "ega6.cpx",
|
|
|
|
"ega7.cpx", "ega8.cpx", "ega9.cpx", "ega10.cpx", "ega11.cpx",
|
|
|
|
"ega12.cpx", "ega13.cpx", "ega14.cpx", "ega15.cpx", "ega16.cpx",
|
|
|
|
"ega17.cpx", "ega18.cpx" };
|
|
|
|
const int res_id[ARRAYSIZE(res_name)] = { IDR_FD_COMMAND_COM, IDR_FD_KERNEL_SYS, IDR_FD_DISPLAY_EXE, IDR_FD_KEYB_EXE,
|
|
|
|
IDR_FD_MODE_COM, IDR_FD_KB1_SYS, IDR_FD_KB2_SYS, IDR_FD_KB3_SYS, IDR_FD_KB4_SYS, IDR_FD_EGA1_CPX,
|
|
|
|
IDR_FD_EGA2_CPX, IDR_FD_EGA3_CPX, IDR_FD_EGA4_CPX, IDR_FD_EGA5_CPX, IDR_FD_EGA6_CPX,
|
|
|
|
IDR_FD_EGA7_CPX, IDR_FD_EGA8_CPX, IDR_FD_EGA9_CPX, IDR_FD_EGA10_CPX, IDR_FD_EGA11_CPX,
|
|
|
|
IDR_FD_EGA12_CPX, IDR_FD_EGA13_CPX, IDR_FD_EGA14_CPX, IDR_FD_EGA15_CPX, IDR_FD_EGA16_CPX,
|
|
|
|
IDR_FD_EGA17_CPX, IDR_FD_EGA18_CPX };
|
|
|
|
char filename[MAX_PATH], locale_path[MAX_PATH];
|
|
|
|
BYTE* res_data;
|
|
|
|
DWORD res_size, Size;
|
2011-12-15 00:46:47 +00:00
|
|
|
HANDLE hFile;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if ((path == NULL) || ((safe_strlen(path) + 14) > sizeof(filename))) {
|
|
|
|
uprintf("invalid path supplied for FreeDOS extraction\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-12-25 22:24:38 +00:00
|
|
|
// Reduce the visible mess by placing all the locale files into a subdir
|
|
|
|
safe_strcpy(locale_path, sizeof(locale_path), path);
|
2013-01-25 01:38:10 +00:00
|
|
|
safe_strcat(locale_path, sizeof(locale_path), "LOCALE\\");
|
2011-12-25 22:24:38 +00:00
|
|
|
CreateDirectoryA(locale_path, NULL);
|
|
|
|
|
|
|
|
for (i=0; i<ARRAYSIZE(res_name); i++) {
|
2013-01-25 01:38:10 +00:00
|
|
|
res_data = (BYTE*)GetResource(hMainInstance, MAKEINTRESOURCEA(res_id[i]), _RT_RCDATA, res_name[i], &res_size, FALSE);
|
2011-12-15 00:46:47 +00:00
|
|
|
|
2013-01-20 23:34:13 +00:00
|
|
|
safe_strcpy(filename, sizeof(filename), ((i<2)?path:locale_path));
|
2011-12-15 00:46:47 +00:00
|
|
|
safe_strcat(filename, sizeof(filename), res_name[i]);
|
|
|
|
|
2014-11-11 19:17:39 +00:00
|
|
|
hFile = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
|
|
|
|
CREATE_ALWAYS, (i<2)?(FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM):FILE_ATTRIBUTE_NORMAL, NULL);
|
2011-12-15 00:46:47 +00:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE) {
|
|
|
|
uprintf("Unable to create file '%s': %s.\n", filename, WindowsErrorString());
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-12-25 22:24:38 +00:00
|
|
|
if ((!WriteFile(hFile, res_data, res_size, &Size, 0)) || (res_size != Size)) {
|
2011-12-15 00:46:47 +00:00
|
|
|
uprintf("Couldn't write file '%s': %s.\n", filename, WindowsErrorString());
|
|
|
|
safe_closehandle(hFile);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-12-15 17:05:19 +00:00
|
|
|
// We don't restore timestamps for FreeDOS, as there's no metadata in the files,
|
|
|
|
// thus we would need to have a separate header with each file's timestamps
|
2011-12-15 00:46:47 +00:00
|
|
|
|
|
|
|
safe_closehandle(hFile);
|
2013-06-20 14:41:22 +00:00
|
|
|
uprintf("Successfully wrote '%s' (%d bytes)\n", filename, res_size);
|
2011-12-16 17:11:19 +00:00
|
|
|
|
2011-12-25 22:24:38 +00:00
|
|
|
if ((i == 4) || (i == 10) || (i == 16) || (i == 22) || (i == ARRAYSIZE(res_name)-1))
|
|
|
|
UpdateProgress(OP_DOS, -1.0f);
|
2011-12-16 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
2011-12-25 22:24:38 +00:00
|
|
|
return SetDOSLocale(path, TRUE);
|
2011-12-15 00:46:47 +00:00
|
|
|
}
|
|
|
|
|
2012-01-12 02:52:40 +00:00
|
|
|
BOOL ExtractDOS(const char* path)
|
2011-12-15 00:46:47 +00:00
|
|
|
{
|
2013-01-24 21:30:11 +00:00
|
|
|
switch(ComboBox_GetItemData(hBootType, ComboBox_GetCurSel(hBootType))) {
|
2015-07-01 20:26:15 +00:00
|
|
|
case BT_MSDOS:
|
2011-12-15 00:46:47 +00:00
|
|
|
return ExtractMSDOS(path);
|
2015-07-01 20:26:15 +00:00
|
|
|
case BT_FREEDOS:
|
2012-01-12 02:52:40 +00:00
|
|
|
return ExtractFreeDOS(path);
|
|
|
|
}
|
|
|
|
return FALSE;
|
2011-12-15 00:46:47 +00:00
|
|
|
}
|