1
1
Fork 0
mirror of https://github.com/pbatard/rufus.git synced 2024-08-14 23:57:05 +00:00

[pe] Improve PE and add Windows 2003 support

* Use \i386 always through SetupSourceDevice in txtsetup.sif
  and prevent deletion of install directory
* Removed setupdd.sy_ patch. Now, none of the files in \i386 are modified
* Better /minint detection for MBR setup
* Also fixed an issue where mbr.bin resources would not be included
This commit is contained in:
Pete Batard 2012-03-27 20:31:15 +01:00
parent 04a20922d5
commit db76e396fd
7 changed files with 343 additions and 188 deletions

View file

@ -475,8 +475,9 @@ static BOOL WriteMBR(HANDLE hPhysicalDrive)
break;
}
if (IsChecked(IDC_DOS)) {
// Set first partition bootable - masquerade as 0x81 for XP
buf[0x1be] = ((iso_report.winpe&WINPE_I386)==WINPE_I386)?0x81:0x80;
// Set first partition bootable - masquerade as 0x81 for non /minint WinPE
buf[0x1be] = (IS_WINPE(iso_report.winpe) && iso_report.uses_minint)?0x80:0x81;
uprintf("Set bootable USB partition as 0x%02X\n", buf[0x1be]);
}
if (!write_sectors(hPhysicalDrive, SecSize, 0, nSecs, buf)) {
@ -492,7 +493,7 @@ static BOOL WriteMBR(HANDLE hPhysicalDrive)
if ((dt == DT_ISO) && ((fs == FS_FAT16) || (fs == FS_FAT32))) {
r = write_syslinux_mbr(&fake_fd);
} else {
if (IsChecked(IDC_RUFUS_MBR)) {
if ((IS_WINPE(iso_report.winpe) && !iso_report.uses_minint) || (IsChecked(IDC_RUFUS_MBR))) {
uprintf("Using Rufus bootable USB selection MBR\n");
r = WriteRufusMBR(&fake_fd);
} else {
@ -579,44 +580,41 @@ static BOOL SetupWinPE(char drive_letter)
{
char src[64], dst[32];
const char* basedir[] = { "i386", "minint" };
const char* patch_str_org[] = { "\\minint\\txtsetup.sif", "\\minint\\system32\\" };
const char* patch_str_rep[] = { "\\$\\i386\\txtsetup.sif", "\\$\\i386\\system32\\" };
const char *win_nt_bt_org = "$win_nt$.~bt", *win_nt_bt_rep = "$\\i386";
const char* patch_str_org[] = { "\\minint\\txtsetup.sif", "\\minint\\system32\\", "\\ntdetect.com" };
// NB: that last patched string will overflow onto the "ntdetect.com" used for net, which we don't care about
const char* patch_str_rep[] = { "\\i386\\txtsetup.sif", "\\i386\\system32\\", "\\i386\\ntdetect.com" };
const char *win_nt_bt_org = "$win_nt$.~bt", *win_nt_bt_rep = "i386";
const char *rdisk_zero = "rdisk(0)";
const char* winnt_sif = "[Data]\n msdosinitiated = \"1\"\n";
const wchar_t* win_nt_ls = L"$win_nt$.~ls";
STARTUPINFOA si;
PROCESS_INFORMATION pi;
// TODO: allow other values than harddisk 1, as per user choice?
char* setupsrcdev = "SetupSourceDevice = \"\\device\\harddisk1\\partition1\"";
HANDLE handle = INVALID_HANDLE_VALUE;
DWORD i, j, size, rw_size, index = 0, exit_code;
BOOL minint = FALSE, r = FALSE;
DWORD i, j, size, rw_size, index = 0;
BOOL r = FALSE;
char* buf = NULL;
index = ((iso_report.winpe&WINPE_I386) == WINPE_I386)?0:1;
// Parse TXTSETUP.SIF to see if /minint was provided for OsLoadOptions
safe_sprintf(src, sizeof(src), "%c:\\%s\\txtsetup.sif", drive_letter, basedir[index]);
buf = get_token_data(src, "OsLoadOptions");
if (buf != NULL) {
for (i=0; i<strlen(buf); i++)
buf[i] = (char)tolower(buf[i]);
uprintf("OsLoadOptions = %s\n", buf);
minint = (strstr(buf, "/minint") != NULL);
if (!iso_report.uses_minint) {
// Create a copy of txtsetup.sif, as we want to keep the i386 files unmodified
safe_sprintf(src, sizeof(src), "%c:\\%s\\txtsetup.sif", drive_letter, basedir[index]);
safe_sprintf(dst, sizeof(dst), "%c:\\txtsetup.sif", drive_letter);
// TODO: detect if overwrite?
CopyFileA(src, dst, TRUE);
if (insert_section_data(dst, "[SetupData]", setupsrcdev, FALSE) == NULL) {
uprintf("Failed to add SetupSourceDevice in %s\n", dst);
goto out;
}
uprintf("Succesfully added '%s' to %s\n", setupsrcdev, dst);
}
safe_free(buf);
// TODO: detect if overwrite?
safe_sprintf(src, sizeof(src), "%c:\\%s\\ntdetect.com", drive_letter, basedir[index]);
safe_sprintf(dst, sizeof(dst), "%c:\\ntdetect.com", drive_letter);
CopyFileA(src, dst, TRUE);
safe_sprintf(src, sizeof(src), "%c:\\%s\\setupldr.bin", drive_letter, basedir[index]);
safe_sprintf(dst, sizeof(dst), "%c:\\BOOTMGR", drive_letter);
// TODO: detect if overwrite?
CopyFileA(src, dst, TRUE);
// \minint with /minint option doesn't require further processing => return true
// \minint and no \i386 without /minint is unclear => return error
if (iso_report.winpe&WINPE_MININT) {
if (minint) {
if (iso_report.uses_minint) {
uprintf("Detected \\minint directory with /minint option: nothing to patch\n");
r = TRUE;
} else if (!(iso_report.winpe&WINPE_I386)) {
@ -645,155 +643,54 @@ static BOOL SetupWinPE(char drive_letter)
}
SetFilePointer(handle, 0, NULL, FILE_BEGIN);
/* Patch setupldr.bin so that [\$]\i386 can be used instead of \minint */
// Patch setupldr.bin
uprintf("Patching file %s\n", dst);
for (i=0; i<size-32; i++) {
// Remove CRC check for 32 bit part of setupldr.bin from Win2k3
if ((size > 0x2061) && (buf[0x2060] == 0x74) && (buf[0x2061] == 0x03)) {
// TODO: amend this is not all Win2k3 setupldr.bin use the same header
buf[0x2060] = 0xeb;
buf[0x2061] = 0x1a;
uprintf(" 0x00002060: 0x74 0x03 -> 0xEB 0x1A (disable Win2k3 CRC check)\n");
}
for (i=1; i<size-32; i++) {
for (j=0; j<ARRAYSIZE(patch_str_org); j++) {
if (safe_strnicmp(&buf[i], patch_str_org[j], strlen(patch_str_org[j])-1) == 0) {
uprintf(" 0x%08X: '%s' -> '%s'\n", i, &buf[i], &patch_str_rep[j][minint?2:0]);
strcpy(&buf[i], &patch_str_rep[j][minint?2:0]);
if ((j == 2) && (buf[i-1] == '6')) // Avoid patching a later "\i386\ntdetect.com"
continue;
uprintf(" 0x%08X: '%s' -> '%s'\n", i, &buf[i], patch_str_rep[j]);
strcpy(&buf[i], patch_str_rep[j]);
i += max(strlen(patch_str_org[j]), strlen(patch_str_rep[j])); // in case org is a substring of rep
}
}
}
if (minint) {
if ((!WriteFile(handle, buf, size, &rw_size, NULL)) || (size != rw_size)) {
uprintf("Could not write patched file: %s\n", WindowsErrorString());
goto out;
}
}else {
// Finish patching \bootmgr
if (!iso_report.uses_minint) {
// Additional setupldr.bin/bootmgr patching
for (i=0; i<size-32; i++) {
// $WIN_NT$_~BT -> $\i386
if (safe_strnicmp(&buf[i], win_nt_bt_org, strlen(win_nt_bt_org)-1) == 0) {
uprintf(" 0x%08X: '%s' -> '%s%s'\n", i, &buf[i], win_nt_bt_rep, &buf[i+strlen(win_nt_bt_org)]);
strcpy(&buf[i], win_nt_bt_rep);
buf[i+strlen(win_nt_bt_rep)] = buf[i+strlen(win_nt_bt_org)];
buf[i+strlen(win_nt_bt_rep)+1] = 0;
}
// rdisk(0) -> rdisk(1) (MBR disk masquerading)
// rdisk(0) -> rdisk(1) disk masquerading
// TODO: only the first one seems to be needed
if (safe_strnicmp(&buf[i], rdisk_zero, strlen(rdisk_zero)-1) == 0) {
buf[i+6] = '1';
uprintf(" 0x%08X: '%s' -> 'rdisk(1)'\n", i, rdisk_zero);
}
}
if ((!WriteFile(handle, buf, size, &rw_size, NULL)) || (size != rw_size)) {
uprintf("Could not write patched file: %s\n", WindowsErrorString());
goto out;
}
safe_free(buf);
safe_closehandle(handle);
// Rename \i386 to \$\i386
safe_sprintf(src, sizeof(src), "%c:\\$", drive_letter);
if (!CreateDirectoryA(src, NULL)) {
uprintf("Could not create %s: %s\n", src, WindowsErrorString());
goto out;
}
safe_sprintf(src, sizeof(src), "%c:\\i386", drive_letter);
safe_sprintf(dst, sizeof(dst), "%c:\\$\\i386", drive_letter);
uprintf("Renaming '%s' to '%s'\n", src, dst);
if (!MoveFileA(src, dst)) {
uprintf(" failed: %s\n", WindowsErrorString());
goto out;
}
// Create a \$\i386\winnt.sif (avoids "Please insert" errors)
safe_sprintf(src, sizeof(src), "%c:\\$\\i386\\winnt.sif", drive_letter);
size = strlen(winnt_sif);
handle = CreateFileA(src, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if (handle == INVALID_HANDLE_VALUE) {
uprintf("Could not create %s: %s\n", src, WindowsErrorString());
goto out;
}
if ((!WriteFile(handle, winnt_sif, size, &rw_size, NULL)) || (size != rw_size)) {
uprintf("Could not write %s: %s\n", src, WindowsErrorString());
goto out;
}
safe_closehandle(handle);
// Uncompress $\\i386\\setupdd.sy_
// Don't bother calling Extract() from cabinet.dll - calling expand.exe is much simpler
// TODO: factorize CreateProcess()?
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
memset(&pi, 0, sizeof(pi));
uprintf("Decompressing %c:\\$\\i386\\setupdd.sy_\n", drive_letter);
safe_sprintf(src, sizeof(src), "expand %c:\\$\\i386\\setupdd.sy_ %c:\\$\\i386\\setupdd.sys", drive_letter, drive_letter);
if (!CreateProcessA(NULL, src, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
uprintf("Could not launch command '%s': %s", src, WindowsErrorString());
goto out;
}
WaitForSingleObject(pi.hProcess, INFINITE);
GetExitCodeProcess(pi.hProcess, &exit_code);
safe_closehandle(pi.hProcess);
safe_closehandle(pi.hThread);
if (exit_code != 0) {
uprintf("Command '%s' failed with exit code: %d\n", src, exit_code);
goto out;
}
// Patch the uncompressed $\\i386\\setupdd.sys
safe_sprintf(dst, sizeof(dst), "%c:\\$\\i386\\setupdd.sys", drive_letter);
handle = CreateFileA(dst, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (handle == INVALID_HANDLE_VALUE) {
uprintf("Could not open %s for patching: %s\n", dst, WindowsErrorString());
goto out;
}
size = GetFileSize(handle, NULL);
if (size == INVALID_FILE_SIZE) {
uprintf("Could not get size for file %s: %s\n", dst, WindowsErrorString());
goto out;
}
buf = (char*)malloc(size);
if (buf == NULL)
goto out;
if ((!ReadFile(handle, buf, size, &rw_size, NULL)) || (size != rw_size)) {
uprintf("Could not read file %s: %s\n", dst, WindowsErrorString());
goto out;
}
SetFilePointer(handle, 0, NULL, FILE_BEGIN);
uprintf("Patching %s\n", dst);
for (i=0; i<size-32; i++) {
if (_wcsnicmp((wchar_t*)&buf[i], win_nt_ls, wcslen(win_nt_ls)-1) == 0) {
uprintf(" 0x%08X: '$win_nt$.~ls' -> '$'\n", i);
buf[i+2] = 0;
// $WIN_NT$_~BT -> i386
if (safe_strnicmp(&buf[i], win_nt_bt_org, strlen(win_nt_bt_org)-1) == 0) {
uprintf(" 0x%08X: '%s' -> '%s%s'\n", i, &buf[i], win_nt_bt_rep, &buf[i+strlen(win_nt_bt_org)]);
strcpy(&buf[i], win_nt_bt_rep);
// This ensures that we keep the terminator backslash
buf[i+strlen(win_nt_bt_rep)] = buf[i+strlen(win_nt_bt_org)];
buf[i+strlen(win_nt_bt_rep)+1] = 0;
}
}
if ((!WriteFile(handle, buf, size, &rw_size, NULL)) || (size != rw_size)) {
uprintf("Could not write patched file: %s\n", WindowsErrorString());
goto out;
}
safe_closehandle(handle);
// Recompress to $\\i386\\setupdd.sy_
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
memset(&pi, 0, sizeof(pi));
uprintf("Compressing back %s\n", dst);
safe_sprintf(src, sizeof(src), "makecab %c:\\$\\i386\\setupdd.sys %c:\\$\\i386\\setupdd.sy_", drive_letter, drive_letter);
if (!CreateProcessA(NULL, src, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
uprintf("Could not launch command '%s': %s", src, WindowsErrorString());
goto out;
}
WaitForSingleObject(pi.hProcess, INFINITE);
GetExitCodeProcess(pi.hProcess, &exit_code);
safe_closehandle(pi.hProcess);
safe_closehandle(pi.hThread);
if (exit_code != 0) {
uprintf("Command '%s' failed with exit code: %d\n", src, exit_code);
goto out;
}
// Delete uncompressed file
_unlink(dst);
}
if ((!WriteFile(handle, buf, size, &rw_size, NULL)) || (size != rw_size)) {
uprintf("Could not write patched file: %s\n", WindowsErrorString());
goto out;
}
safe_free(buf);
safe_closehandle(handle);
r = TRUE;
out:
@ -1021,7 +918,8 @@ DWORD WINAPI FormatThread(LPVOID param)
}
if (IS_WINPE(iso_report.winpe)) {
// Apply WinPe fixup
SetupWinPE(drive_name[0]);
if (!SetupWinPE(drive_name[0]))
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|APPERR(ERROR_CANT_PATCH);
}
}
if (IsChecked(IDC_SET_ICON))

147
src/iso.c
View file

@ -247,7 +247,7 @@ static int udf_extract_files(udf_t *p_udf, udf_dirent_t *p_udf_dirent, const cha
if (is_syslinux_cfg) {
// Workaround for isolinux config files requiring an ISO label for kernel
// append that may be different from our USB label.
if (replace_in_token_data(psz_fullpath, "append", iso_report.label, iso_report.usb_label) != NULL)
if (replace_in_token_data(psz_fullpath, "append", iso_report.label, iso_report.usb_label, TRUE) != NULL)
uprintf("Patched %s: '%s' -> '%s'\n", psz_fullpath, iso_report.label, iso_report.usb_label);
}
}
@ -355,7 +355,7 @@ static int iso_extract_files(iso9660_t* p_iso, const char *psz_path)
}
ISO_BLOCKING(safe_closehandle(file_handle));
if (is_syslinux_cfg) {
if (replace_in_token_data(psz_fullpath, "append", iso_report.label, iso_report.usb_label) != NULL)
if (replace_in_token_data(psz_fullpath, "append", iso_report.label, iso_report.usb_label, TRUE) != NULL)
uprintf("Patched %s: '%s' -> '%s'\n", psz_fullpath, iso_report.label, iso_report.usb_label);
}
}
@ -377,9 +377,11 @@ BOOL ExtractISO(const char* src_iso, const char* dest_dir, bool scan)
udf_t* p_udf = NULL;
udf_dirent_t* p_udf_root;
LONG progress_style;
char* vol_id;
char syslinux_path[64];
char* tmp;
char path[64];
const char* scan_text = "Scanning ISO image...";
const char* basedir[] = { "i386", "minint" };
const char* tmp_sif = ".\\txtsetup.sif~";
if ((src_iso == NULL) || (dest_dir == NULL))
return FALSE;
@ -441,9 +443,9 @@ try_iso:
i_joliet_level = iso9660_ifs_get_joliet_level(p_iso);
uprintf("Disc image is an ISO9660 image\n");
if (scan_only) {
if (iso9660_ifs_get_volume_id(p_iso, &vol_id)) {
safe_strcpy(iso_report.label, sizeof(iso_report.label), vol_id);
safe_free(vol_id);
if (iso9660_ifs_get_volume_id(p_iso, &tmp)) {
safe_strcpy(iso_report.label, sizeof(iso_report.label), tmp);
safe_free(tmp);
} else
iso_report.label[0] = 0;
}
@ -464,15 +466,32 @@ out:
}
uprintf("Will use %s for Syslinux\n", iso_report.cfg_path);
}
if (IS_WINPE(iso_report.winpe)) {
// In case we have a WinPE 1.x based iso, we extract and parse txtsetup.sif
// during scan, to see if /minint was provided for OsLoadOptions, as it decides
// whether we should use 0x80 or 0x81 as the disk ID in the MBR
safe_sprintf(path, sizeof(path), "/%s/txtsetup.sif",
basedir[((iso_report.winpe&WINPE_I386) == WINPE_I386)?0:1]);
ExtractISOFile(src_iso, path, tmp_sif);
tmp = get_token_data(tmp_sif, "OsLoadOptions");
if (tmp != NULL) {
for (i=0; i<strlen(tmp); i++)
tmp[i] = (char)tolower(tmp[i]);
uprintf("Checking txtsetup.sif:\n OsLoadOptions = %s\n", tmp);
iso_report.uses_minint = (strstr(tmp, "/minint") != NULL);
}
_unlink(tmp_sif);
safe_free(tmp);
}
StrArrayDestroy(&config_path);
} else if (iso_report.has_isolinux) {
safe_sprintf(syslinux_path, sizeof(syslinux_path), "%s\\syslinux.cfg", dest_dir);
safe_sprintf(path, sizeof(path), "%s\\syslinux.cfg", dest_dir);
// Create a /syslinux.cfg (if none exists) that points to the existing isolinux cfg
fd = fopen(syslinux_path, "r");
fd = fopen(path, "r");
if (fd == NULL) {
fd = fopen(syslinux_path, "w"); // No "/syslinux.cfg" => create a new one
fd = fopen(path, "w"); // No "/syslinux.cfg" => create a new one
if (fd == NULL) {
uprintf("Unable to create %s - booting from USB will not work\n", syslinux_path);
uprintf("Unable to create %s - booting from USB will not work\n", path);
r = 1;
} else {
fprintf(fd, "DEFAULT loadconfig\n\nLABEL loadconfig\n CONFIG %s\n", iso_report.cfg_path);
@ -482,7 +501,7 @@ out:
fprintf(fd, " APPEND %s/\n", iso_report.cfg_path);
iso_report.cfg_path[i] = '/';
}
uprintf("Created: %s\n", syslinux_path);
uprintf("Created: %s\n", path);
}
}
if (fd != NULL)
@ -497,3 +516,107 @@ out:
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|APPERR((scan_only?ERROR_ISO_SCAN:ERROR_ISO_EXTRACT));
return (r == 0);
}
BOOL ExtractISOFile(const char* iso, const char* iso_file, const char* dest_file)
{
size_t i;
ssize_t read;
int64_t file_length;
char buf[UDF_BLOCKSIZE];
DWORD buf_size, wr_size;
BOOL s, r = FALSE;
iso9660_t* p_iso = NULL;
udf_t* p_udf = NULL;
udf_dirent_t *p_udf_root = NULL, *p_udf_file = NULL;
iso9660_stat_t *p_statbuf = NULL;
lsn_t lsn;
HANDLE file_handle = INVALID_HANDLE_VALUE;
file_handle = CreateFileU(dest_file, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (file_handle == INVALID_HANDLE_VALUE) {
uprintf(" Unable to create file %s: %s\n", dest_file, WindowsErrorString());
goto out;
}
/* First try to open as UDF - fallback to ISO if it failed */
p_udf = udf_open(iso);
if (p_udf == NULL)
goto try_iso;
uprintf("Disc image is an UDF image\n");
p_udf_root = udf_get_root(p_udf, true, 0);
if (p_udf_root == NULL) {
uprintf("Couldn't locate UDF root directory\n");
goto out;
}
p_udf_file = udf_fopen(p_udf_root, iso_file);
if (!p_udf_file) {
uprintf("Couldn't locate file %s in ISO image\n", iso_file);
goto out;
}
file_length = udf_get_file_length(p_udf_file);
while (file_length > 0) {
memset(buf, 0, UDF_BLOCKSIZE);
read = udf_read_block(p_udf_file, buf, 1);
if (read < 0) {
uprintf("Error reading UDF file %s\n", iso_file);
goto out;
}
buf_size = (DWORD)MIN(file_length, read);
s = WriteFile(file_handle, buf, buf_size, &wr_size, NULL);
if ((!s) || (buf_size != wr_size)) {
uprintf(" Error writing file %s: %s\n", dest_file, WindowsErrorString());
goto out;
}
file_length -= read;
}
r = TRUE;
goto out;
try_iso:
p_iso = iso9660_open(iso);
if (p_iso == NULL) {
uprintf("Unable to open image '%s'.\n", iso);
goto out;
}
uprintf("Disc image is an ISO9660 image\n");
p_statbuf = iso9660_ifs_stat_translate(p_iso, iso_file);
if (p_statbuf == NULL) {
uprintf("Could not get ISO-9660 file information for file %s\n", iso_file);
goto out;
}
file_length = p_statbuf->size;
for (i = 0; file_length > 0; i++) {
memset(buf, 0, ISO_BLOCKSIZE);
lsn = p_statbuf->lsn + (lsn_t)i;
if (iso9660_iso_seek_read(p_iso, buf, lsn, 1) != ISO_BLOCKSIZE) {
uprintf(" Error reading ISO9660 file %s at LSN %lu\n", iso_file, (long unsigned int)lsn);
goto out;
}
buf_size = (DWORD)MIN(file_length, ISO_BLOCKSIZE);
s = WriteFile(file_handle, buf, buf_size, &wr_size, NULL);
if ((!s) || (buf_size != wr_size)) {
uprintf(" Error writing file %s: %s\n", dest_file, WindowsErrorString());
goto out;
}
file_length -= ISO_BLOCKSIZE;
}
r = TRUE;
out:
safe_closehandle(file_handle);
safe_free(p_statbuf->rr.psz_symlink);
safe_free(p_statbuf);
if (p_udf_root != NULL)
udf_dirent_free(p_udf_root);
if (p_udf_file != NULL)
udf_dirent_free(p_udf_file);
if (p_iso != NULL)
iso9660_close(p_iso);
if (p_udf != NULL)
udf_close(p_udf);
return (r == 0);
}

View file

@ -118,11 +118,137 @@ out:
return ret;
}
// Insert entry 'data' under section 'section' of a config file
// Section must include the relevant delimitors (eg '[', ']') if needed
char* insert_section_data(const char* filename, const char* section, const char* data, BOOL dos2unix)
{
const wchar_t* outmode[] = { L"w", L"w, ccs=UTF-8", L"w, ccs=UTF-16LE" };
wchar_t *wsection = NULL, *wfilename = NULL, *wtmpname = NULL, *wdata = NULL, bom = 0;
wchar_t wspace[] = L" \t";
wchar_t buf[1024];
FILE *fd_in = NULL, *fd_out = NULL;
size_t i, size;
int mode;
char *ret = NULL, tmp[2];
if ((filename == NULL) || (section == NULL) || (data == NULL))
return NULL;
if ((filename[0] == 0) || (section[0] == 0) || (data[0] == 0))
return NULL;
wfilename = utf8_to_wchar(filename);
if (wfilename == NULL) {
uprintf("Could not convert '%s' to UTF-16\n", filename);
goto out;
}
wsection = utf8_to_wchar(section);
if (wfilename == NULL) {
uprintf("Could not convert '%s' to UTF-16\n", section);
goto out;
}
wdata = utf8_to_wchar(data);
if (wdata == NULL) {
uprintf("Could not convert '%s' to UTF-16\n", data);
goto out;
}
fd_in = _wfopen(wfilename, L"r, ccs=UNICODE");
if (fd_in == NULL) {
uprintf("Could not open file '%s'\n", filename);
goto out;
}
// Check the input file's BOM and create an output file with the same
fread(&bom, sizeof(bom), 1, fd_in);
switch(bom) {
case 0xFEFF:
mode = 2; // UTF-16 (LE)
break;
case 0xBBEF: // Yeah, the UTF-8 BOM is really 0xEF,0xBB,0xBF, but
mode = 1; // find me a non UTF-8 file that actually begins with "ï»"
break;
default:
mode = 0; // ANSI
break;
}
fseek(fd_in, 0, SEEK_SET);
// uprintf("'%s' was detected as %s\n", filename,
// (mode==0)?"ANSI/UTF8 (no BOM)":((mode==1)?"UTF8 (with BOM)":"UTF16 (with BOM"));
wtmpname = (wchar_t*)calloc(wcslen(wfilename)+2, sizeof(wchar_t));
if (wtmpname == NULL) {
uprintf("Could not allocate space for temporary output name\n");
goto out;
}
wcscpy(wtmpname, wfilename);
wtmpname[wcslen(wtmpname)] = '~';
fd_out = _wfopen(wtmpname, outmode[mode]);
if (fd_out == NULL) {
uprintf("Could not open temporary output file %s~\n", filename);
goto out;
}
// Process individual lines. NUL is always appended.
while (fgetws(buf, ARRAYSIZE(buf), fd_in) != NULL) {
i = 0;
// Skip leading spaces
i += wcsspn(&buf[i], wspace);
// Our token should begin a line
if (_wcsnicmp(&buf[i], wsection, wcslen(wsection)) != 0) {
fputws(buf, fd_out);
continue;
}
// Section was found, output it
fputws(buf, fd_out);
// Now output the new data
fwprintf(fd_out, L"%s\n", wdata);
ret = (char*)data;
}
out:
if (fd_in != NULL) fclose(fd_in);
if (fd_out != NULL) fclose(fd_out);
// If an insertion occured, delete existing file and use the new one
if (ret != NULL) {
// We're in Windows text mode => Remove CRs if requested
fd_in = _wfopen(wtmpname, L"rb");
fd_out = _wfopen(wfilename, L"wb");
// Don't check fds
if ((fd_in != NULL) && (fd_out != NULL)) {
size = (mode==2)?2:1;
while(fread(tmp, size, 1, fd_in) == 1) {
if ((!dos2unix) || (tmp[0] != 0x0D))
fwrite(tmp, size, 1, fd_out);
}
fclose(fd_in);
fclose(fd_out);
} else {
uprintf("Could not write %s - original file has been left unmodifiedn", filename);
ret = NULL;
if (fd_in != NULL) fclose(fd_in);
if (fd_out != NULL) fclose(fd_out);
}
}
_wunlink(wtmpname);
safe_free(wfilename);
safe_free(wtmpname);
safe_free(wsection);
safe_free(wdata);
return ret;
}
// Search for a specific 'src' substring the data for all occurences of 'token', and replace
// if with 'rep'. File can be ANSI or UNICODE and is overwritten. Parameters are UTF-8.
// The parsed line is of the form: [ ]token[ ]data
// Returns a pointer to rep if replacement occured, NULL otherwise
char* replace_in_token_data(const char* filename, const char* token, const char* src, const char* rep)
char* replace_in_token_data(const char* filename, const char* token, const char* src, const char* rep, BOOL dos2unix)
{
const wchar_t* outmode[] = { L"w", L"w, ccs=UTF-8", L"w, ccs=UTF-16LE" };
wchar_t *wtoken = NULL, *wfilename = NULL, *wtmpname = NULL, *wsrc = NULL, *wrep = NULL, bom = 0;
@ -236,24 +362,25 @@ out:
// If a replacement occured, delete existing file and use the new one
if (ret != NULL) {
// We're in Windows text mode => Remove CRs
// We're in Windows text mode => Remove CRs if requested
fd_in = _wfopen(wtmpname, L"rb");
fd_out = _wfopen(wfilename, L"wb");
// Don't check fds
if ((fd_in != NULL) && (fd_out != NULL)) {
size = (mode==2)?2:1;
while(fread(tmp, size, 1, fd_in) == 1) {
if (tmp[0] != 0x0D)
if ((!dos2unix) || (tmp[0] != 0x0D))
fwrite(tmp, size, 1, fd_out);
}
fclose(fd_in);
fclose(fd_out);
} else {
uprintf("Dos2Unix conversion failed for %s - file will be unusable!\n", filename);
uprintf("Could not write %s - original file has been left unmodified.\n", filename);
ret = NULL;
if (fd_in != NULL) fclose(fd_in);
if (fd_out != NULL) fclose(fd_out);
}
}
}
_wunlink(wtmpname);
safe_free(wfilename);
safe_free(wtmpname);

View file

@ -1041,9 +1041,9 @@ DWORD WINAPI ISOScanThread(LPVOID param)
safe_free(iso_path);
goto out;
}
uprintf("ISO label: '%s'\n size: %lld bytes, 4GB:%c, bootmgr:%c, winpe:%c, isolinux:%c, old vesa:%c\n",
uprintf("ISO label: '%s'\n size: %lld bytes, 4GB:%c, bootmgr:%c, winpe:%c (/minint:%c), isolinux:%c, old vesa:%c\n",
iso_report.label, iso_report.projected_size, iso_report.has_4GB_file?'Y':'N',
iso_report.has_bootmgr?'Y':'N', IS_WINPE(iso_report.winpe)?'Y':'N',
iso_report.has_bootmgr?'Y':'N', IS_WINPE(iso_report.winpe)?'Y':'N', (iso_report.uses_minint)?'Y':'N',
iso_report.has_isolinux?'Y':'N', iso_report.has_old_vesamenu?'Y':'N');
if ((!iso_report.has_bootmgr) && (!iso_report.has_isolinux) && (!IS_WINPE(iso_report.winpe))) {
MessageBoxU(hMainDialog, "This version of Rufus only supports bootable ISOs\n"
@ -1053,7 +1053,7 @@ DWORD WINAPI ISOScanThread(LPVOID param)
} else {
EnableWindow(GetDlgItem(hMainDialog, IDC_RUFUS_MBR), (iso_report.has_bootmgr) || (IS_WINPE(iso_report.winpe)));
CheckDlgButton(hMainDialog, IDC_RUFUS_MBR,
((iso_report.winpe&WINPE_I386)==WINPE_I386)?BST_CHECKED:BST_UNCHECKED);
(((iso_report.winpe&WINPE_I386)==WINPE_I386)&&(!iso_report.uses_minint))?BST_CHECKED:BST_UNCHECKED);
if (iso_report.has_old_vesamenu) {
fd = fopen(vesamenu_filename, "rb");
if (fd != NULL) {

View file

@ -156,6 +156,7 @@ typedef struct {
BOOL has_isolinux;
BOOL has_autorun;
BOOL has_old_vesamenu;
BOOL uses_minint;
} RUFUS_ISO_REPORT;
/*
@ -193,6 +194,7 @@ extern void DestroyAllTooltips(void);
extern BOOL Notification(int type, char* title, char* format, ...);
extern BOOL ExtractDOS(const char* path);
extern BOOL ExtractISO(const char* src_iso, const char* dest_dir, BOOL scan);
extern BOOL ExtractISOFile(const char* iso, const char* iso_file, const char* dest_file);
extern BOOL InstallSyslinux(DWORD num, const char* drive_name);
DWORD WINAPI FormatThread(void* param);
extern BOOL CreatePartition(HANDLE hDrive);
@ -205,7 +207,8 @@ extern char* FileDialog(BOOL save, char* path, char* filename, char* ext, char*
extern LONG GetEntryWidth(HWND hDropDown, const char* entry);
extern BOOL DownloadFile(const char* url, const char* file);
extern char* get_token_data(const char* filename, const char* token);
extern char* replace_in_token_data(const char* filename, const char* token, const char* src, const char* rep);
extern char* insert_section_data(const char* filename, const char* section, const char* data, BOOL dos2unix);
extern char* replace_in_token_data(const char* filename, const char* token, const char* src, const char* rep, BOOL dos2unix);
__inline static BOOL UnlockDrive(HANDLE hDrive)
{
@ -278,6 +281,7 @@ typedef struct {
#define ERROR_ISO_SCAN 0x1207
#define ERROR_ISO_EXTRACT 0x1208
#define ERROR_CANT_REMOUNT_VOLUME 0x1209
#define ERROR_CANT_PATCH 0x1210
/* More niceties */
#ifndef MIN

View file

@ -33,7 +33,7 @@ LANGUAGE LANG_ENGLISH, SUBLANG_NEUTRAL
IDD_DIALOG DIALOGEX 12, 12, 206, 289
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_APPWINDOW
CAPTION "Rufus v1.1.6.159"
CAPTION "Rufus v1.1.6.160"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
DEFPUSHBUTTON "Start",IDC_START,94,248,50,14
@ -74,7 +74,7 @@ BEGIN
DEFPUSHBUTTON "OK",IDOK,231,175,50,14,WS_GROUP
CONTROL "<a href=""http://rufus.akeo.ie"">http://rufus.akeo.ie</a>",IDC_ABOUT_RUFUS_URL,
"SysLink",WS_TABSTOP,46,47,114,9
LTEXT "Version 1.1.6 (Build 159)",IDC_STATIC,46,19,78,8
LTEXT "Version 1.1.6 (Build 160)",IDC_STATIC,46,19,78,8
PUSHBUTTON "License...",IDC_ABOUT_LICENSE,46,175,50,14,WS_GROUP
EDITTEXT IDC_ABOUT_COPYRIGHTS,46,107,235,63,ES_MULTILINE | ES_READONLY | WS_VSCROLL
LTEXT "Report bugs or request enhancements at:",IDC_STATIC,46,66,187,8
@ -143,6 +143,7 @@ BEGIN
"\r\n"
"IDR_SL_LDLINUX_BSS RCDATA ""../res/syslinux/ldlinux.bss""\r\n"
"IDR_SL_LDLINUX_SYS RCDATA ""../res/syslinux/ldlinux.sys""\r\n"
"IDR_BR_MBR_BIN RCDATA ""../res/mbr/mbr.bin""\r\n"
"// Only include these in rufus_fd\r\n"
"#if defined(WITH_FREEDOS)\r\n"
"IDR_FD_COMMAND_COM RCDATA ""../res/freedos/COMMAND.COM""\r\n"
@ -224,8 +225,8 @@ END
//
VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,1,6,159
PRODUCTVERSION 1,1,6,159
FILEVERSION 1,1,6,160
PRODUCTVERSION 1,1,6,160
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
@ -242,13 +243,13 @@ BEGIN
BEGIN
VALUE "CompanyName", "akeo.ie"
VALUE "FileDescription", "Rufus"
VALUE "FileVersion", "1.1.6.159"
VALUE "FileVersion", "1.1.6.160"
VALUE "InternalName", "Rufus"
VALUE "LegalCopyright", "© 2011 Pete Batard (GPL v3)"
VALUE "LegalTrademarks", "http://www.gnu.org/copyleft/gpl.html"
VALUE "OriginalFilename", "rufus.exe"
VALUE "ProductName", "Rufus"
VALUE "ProductVersion", "1.1.6.159"
VALUE "ProductVersion", "1.1.6.160"
END
END
BLOCK "VarFileInfo"

View file

@ -229,6 +229,8 @@ const char* StrError(DWORD error_code)
case ERROR_CANT_REMOUNT_VOLUME:
return "Unable to remount volume. You may have to use the\n"
"mountvol.exe command to make your device accessible again";
case ERROR_CANT_PATCH:
return "Unable to patch/setup files for boot";
default:
uprintf("Unknown error: %08X\n", error_code);
SetLastError(error_code);