From f1f620411c91e86334136268ee98e0be9312c06f Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Mon, 6 Jun 2016 19:39:53 +0100 Subject: [PATCH] [syslinux] use SetFilePointerEx and other improvements --- src/format.c | 3 ++- src/rufus.rc | 10 +++++----- src/syslinux.c | 37 +++++++++++++++++++++---------------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/format.c b/src/format.c index a356a1db..73e3914d 100644 --- a/src/format.c +++ b/src/format.c @@ -1138,6 +1138,7 @@ static BOOL SetupWinPE(char drive_letter) 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 *rdisk_zero = "rdisk(0)"; + const LARGE_INTEGER liZero = { {0, 0} }; char setupsrcdev[64]; HANDLE handle = INVALID_HANDLE_VALUE; DWORD i, j, size, rw_size, index = 0; @@ -1203,7 +1204,7 @@ static BOOL SetupWinPE(char drive_letter) uprintf("Could not read file %s: %s\n", dst, WindowsErrorString()); goto out; } - SetFilePointer(handle, 0, NULL, FILE_BEGIN); + SetFilePointerEx(handle, liZero, NULL, FILE_BEGIN); // Patch setupldr.bin uprintf("Patching file %s\n", dst); diff --git a/src/rufus.rc b/src/rufus.rc index b84c4a20..754eff1a 100644 --- a/src/rufus.rc +++ b/src/rufus.rc @@ -33,7 +33,7 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL IDD_DIALOG DIALOGEX 12, 12, 242, 376 STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU EXSTYLE WS_EX_ACCEPTFILES -CAPTION "Rufus 2.10.953" +CAPTION "Rufus 2.10.954" FONT 8, "Segoe UI Symbol", 400, 0, 0x0 BEGIN LTEXT "Device",IDS_DEVICE_TXT,9,6,200,8 @@ -320,8 +320,8 @@ END // VS_VERSION_INFO VERSIONINFO - FILEVERSION 2,10,953,0 - PRODUCTVERSION 2,10,953,0 + FILEVERSION 2,10,954,0 + PRODUCTVERSION 2,10,954,0 FILEFLAGSMASK 0x3fL #ifdef _DEBUG FILEFLAGS 0x1L @@ -338,13 +338,13 @@ BEGIN BEGIN VALUE "CompanyName", "Akeo Consulting (http://akeo.ie)" VALUE "FileDescription", "Rufus" - VALUE "FileVersion", "2.10.953" + VALUE "FileVersion", "2.10.954" VALUE "InternalName", "Rufus" VALUE "LegalCopyright", "© 2011-2016 Pete Batard (GPL v3)" VALUE "LegalTrademarks", "http://www.gnu.org/copyleft/gpl.html" VALUE "OriginalFilename", "rufus.exe" VALUE "ProductName", "Rufus" - VALUE "ProductVersion", "2.10.953" + VALUE "ProductVersion", "2.10.954" END END BLOCK "VarFileInfo" diff --git a/src/syslinux.c b/src/syslinux.c index 0f3dd82f..6281fc49 100644 --- a/src/syslinux.c +++ b/src/syslinux.c @@ -55,20 +55,24 @@ uint32_t LIBFAT_SECTOR_MASK = 511; /* * Wrapper for ReadFile suitable for libfat */ -int libfat_readfile(intptr_t pp, void *buf, size_t secsize, - libfat_sector_t sector) +int libfat_readfile(intptr_t pp, void *buf, size_t secsize, libfat_sector_t sector) { - uint64_t offset = (uint64_t) sector * secsize; - LONG loword = (LONG) offset; - LONG hiword = (LONG) (offset >> 32); - LONG hiwordx = hiword; + LARGE_INTEGER offset; DWORD bytes_read; - if (SetFilePointer((HANDLE) pp, loword, &hiwordx, FILE_BEGIN) != loword || - hiword != hiwordx || - !ReadFile((HANDLE) pp, buf, (DWORD)secsize, &bytes_read, NULL) || - bytes_read != secsize) { - uprintf("Cannot read sector %u\n", sector); + offset.QuadPart = (LONGLONG) sector * secsize; + if (!SetFilePointerEx((HANDLE) pp, offset, NULL, FILE_BEGIN)) { + uprintf("Could not set pointer to position %llu: %s", offset.QuadPart, WindowsErrorString()); + return 0; + } + + if (!ReadFile((HANDLE) pp, buf, (DWORD) secsize, &bytes_read, NULL)) { + uprintf("Could not read sector %llu: %s", sector, WindowsErrorString()); + return 0; + } + + if (bytes_read != secsize) { + uprintf("Sector %llu: Read %d bytes instead of %d requested", sector, bytes_read, secsize); return 0; } @@ -81,6 +85,7 @@ int libfat_readfile(intptr_t pp, void *buf, size_t secsize, */ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type) { + const LARGE_INTEGER liZero = { {0, 0} }; HANDLE f_handle = INVALID_HANDLE_VALUE; HANDLE d_handle = INVALID_HANDLE_VALUE; DWORD bytes_read, bytes_written, err; @@ -198,7 +203,7 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type) /* Reopen the volume (we already have a lock) */ d_handle = GetLogicalHandle(drive_index, TRUE, FALSE); - if (d_handle == INVALID_HANDLE_VALUE) { + if ((d_handle == INVALID_HANDLE_VALUE) || (d_handle == NULL)) { uprintf("Could open volume for Syslinux installation"); goto out; } @@ -270,10 +275,10 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type) } /* Rewrite the file */ - if (SetFilePointer(f_handle, 0, NULL, FILE_BEGIN) != 0 || + if (!SetFilePointerEx(f_handle, liZero, NULL, FILE_BEGIN) || !WriteFileWithRetry(f_handle, syslinux_ldlinux[0], syslinux_ldlinux_len[0], &bytes_written, WRITE_RETRIES)) { - uprintf("Could not write '%s': %s\n", &path[3], WindowsErrorString()); + uprintf("Could not rewrite '%s': %s\n", &path[3], WindowsErrorString()); goto out; } @@ -281,7 +286,7 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type) safe_closehandle(f_handle); /* Read existing FAT data into boot sector */ - if (SetFilePointer(d_handle, 0, NULL, FILE_BEGIN) != 0 || + if (!SetFilePointerEx(d_handle, liZero, NULL, FILE_BEGIN) || !ReadFile(d_handle, sectbuf, SECTOR_SIZE, &bytes_read, NULL)) { uprintf("Could not read Syslinux boot record: %s", WindowsErrorString()); @@ -296,7 +301,7 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type) syslinux_make_bootsect(sectbuf, (fs_type == FS_NTFS)?NTFS:VFAT); /* Write boot sector back */ - if (SetFilePointer(d_handle, 0, NULL, FILE_BEGIN) != 0 || + if (!SetFilePointerEx(d_handle, liZero, NULL, FILE_BEGIN) || !WriteFileWithRetry(d_handle, sectbuf, SECTOR_SIZE, &bytes_written, WRITE_RETRIES)) { uprintf("Could not write Syslinux boot record: %s", WindowsErrorString());