Permalink
Browse files

[misc] refactoring and cleanup - part 2

* add GetResource() function call to handle resource loading and
  revert 98ff7a9
* add separate BootCheck() call
* better handling of passes tooltip
* remove superfluous backslashes
* fix standalone EFI support
* add GPL v3 license file and update README.txt
  • Loading branch information...
1 parent bba1772 commit 647d9f18adcc1cbbf54a472252ca7cb3aff2a6f1 @pbatard committed Jan 25, 2013
Showing with 860 additions and 233 deletions.
  1. +674 −0 LICENSE.txt
  2. +4 −4 README.txt
  3. +35 −79 src/dos.c
  4. +2 −2 src/drive.c
  5. +4 −15 src/format.c
  6. +4 −14 src/icon.c
  7. +81 −73 src/rufus.c
  8. +6 −0 src/rufus.h
  9. +5 −5 src/rufus.rc
  10. +34 −0 src/stdfn.c
  11. +8 −39 src/syslinux.c
  12. +3 −2 src/syslinux/libinstaller/syslinux.h
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -3,19 +3,19 @@ Rufus: The Reliable USB Formatting Utility
Features:
- Formats USB flash drives to FAT/FAT32/NTFS/exFAT
- Creates DOS bootable USB drives, with no external files required
-- Creates bootable USB drives from bootable ISOs (Windows, Linux, etc.)
+- Creates bootable USB drives from bootable ISOs (Windows, Linux, EFI, etc.)
- Twice as fast as Microsoft's USB/DVD tool or UNetbootin, on ISO->USB (1)
- Bad blocks check, with detection for fake capacity
-- Modern UI, with UAC elevation for Windows Vista and later
+- Modern UI
- Small footprint, no installation required
- 100% Free Source Software (GPL v3)
Compilation:
- Use either Visual Studio 2010, WDK (Windows Driver Kit) or MinGW and then
+ Use either Visual Studio 2012, WDK 7.1 (Windows Driver Kit) or MinGW and then
invoke the .sln, wdk_build.cmd or configure/make respectively.
Additional information:
- Rufus provides extensive information about what it is doing through its easily
+ Rufus provides extensive information about what it is doing, through its easily
accessible log or the Windows debug facility.
More info:
View
114 src/dos.c
@@ -27,14 +27,15 @@
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include "rufus.h"
#include "dos.h"
#include "resource.h"
-static BYTE* DiskImage;
-static size_t DiskImageSize;
+static BYTE* DiskImage = NULL;
+static DWORD DiskImageSize;
/*
* FAT time conversion, from ReactOS' time.c
@@ -166,57 +167,37 @@ static void FatDateTimeToSystemTime(PLARGE_INTEGER SystemTime, PFAT_DATETIME Fat
* IO.SYS 000003AA 75 -> EB
* COMMAND.COM 00006510 75 -> EB
*/
-static BOOL Patch_COMMAND_COM(HANDLE hFile)
+static BOOL Patch_COMMAND_COM(size_t filestart, size_t filesize)
{
const BYTE expected[8] = { 0x15, 0x80, 0xFA, 0x03, 0x75, 0x10, 0xB8, 0x0E };
- BYTE data[sizeof(expected)] = { 0x00 };
- DWORD size = sizeof(data);
uprintf("Patching COMMAND.COM...\n");
- if (GetFileSize(hFile, NULL) != 93040) {
+ if (filesize != 93040) {
uprintf(" unexpected file size\n");
return FALSE;
}
- SetFilePointer(hFile, 0x650c, NULL, FILE_BEGIN);
- if (!ReadFile(hFile, data, size, &size, NULL)) {
- uprintf(" could not read data\n");
- return FALSE;
- }
- if (memcmp(data, expected, sizeof(expected)) != 0) {
+ if (memcmp(&DiskImage[filestart+0x650c], expected, sizeof(expected)) != 0) {
uprintf(" unexpected binary data\n");
return FALSE;
}
- data[4] = 0xeb;
- SetFilePointer(hFile, 0x650c, NULL, FILE_BEGIN);
- size = sizeof(data);
- WriteFile(hFile, data, size, &size, NULL);
+ DiskImage[filestart+0x6510] = 0xeb;
return TRUE;
}
-static BOOL Patch_IO_SYS(HANDLE hFile)
+static BOOL Patch_IO_SYS(size_t filestart, size_t filesize)
{
const BYTE expected[8] = { 0xFA, 0x80, 0x75, 0x09, 0x8D, 0xB6, 0x99, 0x00 };
- BYTE data[sizeof(expected)] = { 0x00 };
- DWORD size = sizeof(data);
uprintf("Patching IO.SYS...\n");
- if (GetFileSize(hFile, NULL) != 116736) {
+ if (filesize != 116736) {
uprintf(" unexpected file size\n");
return FALSE;
}
- SetFilePointer(hFile, 0x3a8, NULL, FILE_BEGIN);
- if (!ReadFile(hFile, data, size, &size, NULL)) {
- uprintf(" could not read data\n");
- return FALSE;
- }
- if (memcmp(data, expected, sizeof(expected)) != 0) {
+ if (memcmp(&DiskImage[filestart+0x3a8], expected, sizeof(expected)) != 0) {
uprintf(" unexpected binary data\n");
return FALSE;
}
- data[2] = 0xeb;
- SetFilePointer(hFile, 0x3a8, NULL, FILE_BEGIN);
- size = sizeof(data);
- WriteFile(hFile, data, size, &size, NULL);
+ DiskImage[filestart+0x3aa] = 0xeb;
return TRUE;
}
@@ -239,7 +220,6 @@ static BOOL ExtractFAT(int entry, const char* path)
}
strcpy(filename, path);
pos = strlen(path);
- filename[pos++] = '\\';
fnamepos = pos;
for(i=0; i<8; i++) {
@@ -262,6 +242,13 @@ static BOOL ExtractFAT(int entry, const char* path)
return FALSE;
}
+ /* 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);
+ }
+
/* 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,
NULL, CREATE_ALWAYS, dir_entry->Attributes, 0);
@@ -276,13 +263,6 @@ static BOOL ExtractFAT(int entry, const char* path)
return FALSE;
}
- /* WinME DOS files need to be patched */
- if (strcmp(&filename[fnamepos], "COMMAND.COM") == 0) {
- Patch_COMMAND_COM(hFile);
- } else if (strcmp(&filename[fnamepos], "IO.SYS") == 0) {
- Patch_IO_SYS(hFile);
- }
-
/* Restore timestamps from FAT */
FatDateTimeToSystemTime(&liCreationTime, &dir_entry->CreationDateTime, dir_entry->CreationTimeTenMs);
ftCreationTime.dwHighDateTime = liCreationTime.HighPart;
@@ -311,50 +291,37 @@ static BOOL ExtractMSDOS(const char* path)
{
char dllname[MAX_PATH] = "C:\\Windows\\System32";
int i, j;
- BOOL r = TRUE;
- HMODULE hDLL;
- HGLOBAL hRes;
- HRSRC hDiskImage;
+ BOOL r = FALSE;
+ HMODULE hDLL = NULL;
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" };
// Reduce the visible mess by placing all the locale files into a subdir
safe_strcpy(locale_path, sizeof(locale_path), path);
- safe_strcat(locale_path, sizeof(locale_path), "\\LOCALE");
+ safe_strcat(locale_path, sizeof(locale_path), "LOCALE\\");
CreateDirectoryA(locale_path, NULL);
GetSystemDirectoryA(dllname, sizeof(dllname));
safe_strcat(dllname, sizeof(dllname), "\\diskcopy.dll");
hDLL = LoadLibraryA(dllname);
if (hDLL == NULL) {
uprintf("Unable to open %s: %s\n", dllname, WindowsErrorString());
- return FALSE;
+ goto out;
}
- hDiskImage = FindResourceA(hDLL, MAKEINTRESOURCEA(1), "BINFILE");
- if (hDiskImage == NULL) {
- uprintf("Unable to locate disk image in %s: %s\n", dllname, WindowsErrorString());
- FreeLibrary(hDLL);
- return FALSE;
- }
- hRes = LoadResource(hDLL, hDiskImage);
- if (hRes != NULL)
- DiskImage = (BYTE*)LockResource(hRes);
- if ((hRes == NULL) || (DiskImage == NULL) ){
- uprintf("Unable to access disk image in %s: %s\n", dllname, WindowsErrorString());
- FreeLibrary(hDLL);
- return FALSE;
- }
- DiskImageSize = (size_t)SizeofResource(hDLL, hDiskImage);
+
+ DiskImage = (BYTE*)GetResource(hDLL, MAKEINTRESOURCEA(1), "BINFILE", "disk image", &DiskImageSize, TRUE);
+ if (DiskImage == NULL)
+ goto out;
+
// Sanity check
if (DiskImageSize < 700*1024) {
uprintf("MS-DOS disk image is too small (%d bytes)\n", dllname, DiskImageSize);
- FreeLibrary(hDLL);
- return FALSE;
+ goto out;
}
- for (i=0; r && i<FAT_FN_DIR_ENTRY_LAST; i++) {
+ for (i=0, r=TRUE; r && i<FAT_FN_DIR_ENTRY_LAST; i++) {
if (DiskImage[FAT12_ROOTDIR_OFFSET + i*FAT_BYTES_PER_DIRENT] == FAT_DIRENT_DELETED)
continue;
for (j=0; r && j<ARRAYSIZE(extractlist); j++) {
@@ -365,10 +332,13 @@ static BOOL ExtractMSDOS(const char* path)
}
}
}
- FreeLibrary(hDLL);
if (r)
r = SetDOSLocale(path, FALSE);
+out:
+ if (hDLL != NULL)
+ FreeLibrary(hDLL);
+ safe_free(DiskImage);
return r;
}
@@ -388,8 +358,6 @@ BOOL ExtractFreeDOS(const char* path)
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];
- HGLOBAL res_handle;
- HRSRC res;
BYTE* res_data;
DWORD res_size, Size;
HANDLE hFile;
@@ -402,25 +370,13 @@ BOOL ExtractFreeDOS(const char* path)
// Reduce the visible mess by placing all the locale files into a subdir
safe_strcpy(locale_path, sizeof(locale_path), path);
- safe_strcat(locale_path, sizeof(locale_path), "\\LOCALE");
+ safe_strcat(locale_path, sizeof(locale_path), "LOCALE\\");
CreateDirectoryA(locale_path, NULL);
for (i=0; i<ARRAYSIZE(res_name); i++) {
- res = FindResource(hMainInstance, MAKEINTRESOURCE(res_id[i]), RT_RCDATA);
- if (res == NULL) {
- uprintf("Unable to locate FreeDOS resource %s: %s\n", res_name[i], WindowsErrorString());
- return FALSE;
- }
- res_handle = LoadResource(NULL, res);
- if (res_handle == NULL) {
- uprintf("Unable to load FreeDOS resource %s: %s\n", res_name[i], WindowsErrorString());
- return FALSE;
- }
- res_data = (BYTE*)LockResource(res_handle);
- res_size = SizeofResource(NULL, res);
+ res_data = (BYTE*)GetResource(hMainInstance, MAKEINTRESOURCEA(res_id[i]), _RT_RCDATA, res_name[i], &res_size, FALSE);
safe_strcpy(filename, sizeof(filename), ((i<2)?path:locale_path));
- safe_strcat(filename, sizeof(filename), "\\");
safe_strcat(filename, sizeof(filename), res_name[i]);
hFile = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
View
@@ -75,7 +75,7 @@ HANDLE GetDriveHandle(DWORD DriveIndex, char* DriveLetter, BOOL bWriteAccess, BO
goto out;
}
if (bWriteAccess) {
- uprintf("Caution: Opened %s drive for write access\n", physical_drive);
+ uprintf("Caution: Opened %s drive for write access\n", &physical_drive[4]);
}
} else {
*DriveLetter = ' ';
@@ -130,7 +130,7 @@ HANDLE GetDriveHandle(DWORD DriveIndex, char* DriveLetter, BOOL bWriteAccess, BO
goto out;
}
if (bWriteAccess) {
- uprintf("Caution: Opened %s drive for write access\n", logical_drive);
+ uprintf("Caution: Opened %s drive for write access\n", &logical_drive[4]);
}
*DriveLetter = *drive?*drive:' ';
}
View
@@ -779,23 +779,12 @@ static BOOL ClearMBRGPT(HANDLE hPhysicalDrive, LONGLONG DiskSize, DWORD SectorSi
*/
BOOL WriteRufusMBR(FILE *fp)
{
- HGLOBAL res_handle;
- HRSRC res;
+ DWORD size;
unsigned char aucRef[] = {0x55, 0xAA};
unsigned char* rufus_mbr;
// TODO: Will we need to edit the disk ID according to UI selection in the MBR as well?
- res = FindResource(hMainInstance, MAKEINTRESOURCE(IDR_BR_MBR_BIN), RT_RCDATA);
- if (res == NULL) {
- uprintf("Unable to locate mbr.bin resource: %s\n", WindowsErrorString());
- return FALSE;
- }
- res_handle = LoadResource(NULL, res);
- if (res_handle == NULL) {
- uprintf("Unable to load mbr.bin resource: %s\n", WindowsErrorString());
- return FALSE;
- }
- rufus_mbr = (unsigned char*)LockResource(res_handle);
+ rufus_mbr = GetResource(hMainInstance, MAKEINTRESOURCEA(IDR_BR_MBR_BIN), _RT_RCDATA, "mbr.bin", &size, FALSE);
return
write_data(fp, 0x0, rufus_mbr, 0x1b8) &&
@@ -1089,9 +1078,9 @@ static BOOL RemountVolume(char drive_letter)
if (DeleteVolumeMountPointA(drive_name)) {
Sleep(200);
if (SetVolumeMountPointA(drive_name, drive_guid)) {
- uprintf("Successfully remounted %s on %s\n", drive_guid, drive_name);
+ uprintf("Successfully remounted %s on %s\n", &drive_guid[4], drive_name);
} else {
- uprintf("Failed to remount %s on %s\n", drive_guid, drive_name);
+ uprintf("Failed to remount %s on %s\n", &drive_guid[4], drive_name);
// This will leave the drive unaccessible and must be flagged as an error
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|APPERR(ERROR_CANT_REMOUNT_VOLUME);
return FALSE;
View
@@ -98,17 +98,7 @@ static BOOL SaveIcon(const char* filename)
BOOL r = FALSE;
GRPICONDIR* icondir;
- res = FindResource(hMainInstance, MAKEINTRESOURCE(IDI_ICON), RT_GROUP_ICON);
- if (res == NULL) {
- uprintf("Unable to locate icon resource: %s\n", WindowsErrorString());
- goto out;
- }
- res_handle = LoadResource(NULL, res);
- if (res_handle == NULL) {
- uprintf("Unable to load icon resource: %s\n", WindowsErrorString());
- goto out;
- }
- icondir = (GRPICONDIR*)LockResource(res_handle);
+ icondir = (GRPICONDIR*)GetResource(hMainInstance, MAKEINTRESOURCEA(IDI_ICON), _RT_GROUP_ICON, "icon", &res_size, FALSE);
hFile = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, CREATE_NEW, 0, 0);
@@ -132,7 +122,7 @@ static BOOL SaveIcon(const char* filename)
uprintf("Couldn't write ICONDIRENTRY[%d]: %s.\n", i, WindowsErrorString());
goto out;
}
- res = FindResource(hMainInstance, MAKEINTRESOURCE(icondir->idEntries[i].nID), RT_ICON);
+ res = FindResourceA(hMainInstance, MAKEINTRESOURCEA(icondir->idEntries[i].nID), _RT_ICON);
// Write the DWORD offset
if ( (!WriteFile(hFile, &offset, sizeof(offset), &Size, NULL)) || (Size != sizeof(offset)) ) {
uprintf("Couldn't write ICONDIRENTRY[%d] offset: %s.\n", i, WindowsErrorString());
@@ -142,7 +132,7 @@ static BOOL SaveIcon(const char* filename)
}
for (i=0; i<icondir->idCount; i++) {
// Write icon data
- res = FindResource(hMainInstance, MAKEINTRESOURCE(icondir->idEntries[i].nID), RT_ICON);
+ res = FindResourceA(hMainInstance, MAKEINTRESOURCEA(icondir->idEntries[i].nID), _RT_ICON);
res_handle = LoadResource(NULL, res);
res_data = (BYTE*)LockResource(res_handle);
res_size = SizeofResource(NULL, res);
@@ -170,7 +160,7 @@ BOOL SetAutorun(const char* path)
char filename[64];
wchar_t wlabel[128], wRufusVersion[32];
- safe_sprintf(filename, sizeof(filename), "%s\\autorun.inf", path);
+ safe_sprintf(filename, sizeof(filename), "%sautorun.inf", path);
fd = fopen(filename, "r"); // If there's an existing autorun, don't overwrite
if (fd != NULL) {
uprintf("%s already exists - keeping it\n", filename);
Oops, something went wrong.

0 comments on commit 647d9f1

Please sign in to comment.