[misc] use the more univerasal _mm_alloc() instead of _aligned_malloc()

* Also ensure that our buffers are aligned
* Also remove the use of static buffers in ms-sys
This commit is contained in:
Pete Batard 2016-06-10 12:42:43 +01:00
parent 3fdf622933
commit e76f60a3e8
11 changed files with 125 additions and 113 deletions

View File

@ -256,19 +256,11 @@ static blk_t next_bad = 0;
static bb_badblocks_iterate bb_iter = NULL; static bb_badblocks_iterate bb_iter = NULL;
static __inline void *allocate_buffer(size_t size) { static __inline void *allocate_buffer(size_t size) {
#ifdef __MINGW32__ return _mm_malloc(size, BB_SYS_PAGE_SIZE);
return __mingw_aligned_malloc(size, BB_SYS_PAGE_SIZE);
#else
return _aligned_malloc(size, BB_SYS_PAGE_SIZE);
#endif
} }
static __inline void free_buffer(void* p) { static __inline void free_buffer(void* p) {
#ifdef __MINGW32__ _mm_free(p);
__mingw_aligned_free(p);
#else
_aligned_free(p);
#endif
} }
/* /*

View File

@ -847,7 +847,7 @@ static BOOL WriteMBR(HANDLE hPhysicalDrive)
{ {
BOOL r = FALSE; BOOL r = FALSE;
DWORD size; DWORD size;
unsigned char* buf = NULL; unsigned char* buffer = NULL;
FAKE_FD fake_fd = { 0 }; FAKE_FD fake_fd = { 0 };
FILE* fp = (FILE*)&fake_fd; FILE* fp = (FILE*)&fake_fd;
const char* using_msg = "Using %s MBR\n"; const char* using_msg = "Using %s MBR\n";
@ -859,14 +859,14 @@ static BOOL WriteMBR(HANDLE hPhysicalDrive)
// FormatEx rewrites the MBR and removes the LBA attribute of FAT16 // FormatEx rewrites the MBR and removes the LBA attribute of FAT16
// and FAT32 partitions - we need to correct this in the MBR // and FAT32 partitions - we need to correct this in the MBR
buf = (unsigned char*)malloc(SelectedDrive.SectorSize); buffer = (unsigned char*)_mm_malloc(SelectedDrive.SectorSize, 16);
if (buf == NULL) { if (buffer == NULL) {
uprintf("Could not allocate memory for MBR"); uprintf("Could not allocate memory for MBR");
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_NOT_ENOUGH_MEMORY; FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_NOT_ENOUGH_MEMORY;
goto out; goto out;
} }
if (!read_sectors(hPhysicalDrive, SelectedDrive.SectorSize, 0, 1, buf)) { if (!read_sectors(hPhysicalDrive, SelectedDrive.SectorSize, 0, 1, buffer)) {
uprintf("Could not read MBR\n"); uprintf("Could not read MBR\n");
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_READ_FAULT; FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_READ_FAULT;
goto out; goto out;
@ -874,30 +874,30 @@ static BOOL WriteMBR(HANDLE hPhysicalDrive)
switch (ComboBox_GetItemData(hFileSystem, ComboBox_GetCurSel(hFileSystem))) { switch (ComboBox_GetItemData(hFileSystem, ComboBox_GetCurSel(hFileSystem))) {
case FS_FAT16: case FS_FAT16:
if (buf[0x1c2] == 0x0e) { if (buffer[0x1c2] == 0x0e) {
uprintf("Partition is already FAT16 LBA...\n"); uprintf("Partition is already FAT16 LBA...\n");
} else if ((buf[0x1c2] != 0x04) && (buf[0x1c2] != 0x06)) { } else if ((buffer[0x1c2] != 0x04) && (buffer[0x1c2] != 0x06)) {
uprintf("Warning: converting a non FAT16 partition to FAT16 LBA: FS type=0x%02x\n", buf[0x1c2]); uprintf("Warning: converting a non FAT16 partition to FAT16 LBA: FS type=0x%02x\n", buffer[0x1c2]);
} }
buf[0x1c2] = 0x0e; buffer[0x1c2] = 0x0e;
break; break;
case FS_FAT32: case FS_FAT32:
if (buf[0x1c2] == 0x0c) { if (buffer[0x1c2] == 0x0c) {
uprintf("Partition is already FAT32 LBA...\n"); uprintf("Partition is already FAT32 LBA...\n");
} else if (buf[0x1c2] != 0x0b) { } else if (buffer[0x1c2] != 0x0b) {
uprintf("Warning: converting a non FAT32 partition to FAT32 LBA: FS type=0x%02x\n", buf[0x1c2]); uprintf("Warning: converting a non FAT32 partition to FAT32 LBA: FS type=0x%02x\n", buffer[0x1c2]);
} }
buf[0x1c2] = 0x0c; buffer[0x1c2] = 0x0c;
break; break;
} }
if ((IsChecked(IDC_BOOT)) && (tt == TT_BIOS)) { if ((IsChecked(IDC_BOOT)) && (tt == TT_BIOS)) {
// Set first partition bootable - masquerade as per the DiskID selected // Set first partition bootable - masquerade as per the DiskID selected
buf[0x1be] = IsChecked(IDC_RUFUS_MBR) ? buffer[0x1be] = IsChecked(IDC_RUFUS_MBR) ?
(BYTE)ComboBox_GetItemData(hDiskID, ComboBox_GetCurSel(hDiskID)):0x80; (BYTE)ComboBox_GetItemData(hDiskID, ComboBox_GetCurSel(hDiskID)):0x80;
uprintf("Set bootable USB partition as 0x%02X\n", buf[0x1be]); uprintf("Set bootable USB partition as 0x%02X\n", buffer[0x1be]);
} }
if (!write_sectors(hPhysicalDrive, SelectedDrive.SectorSize, 0, 1, buf)) { if (!write_sectors(hPhysicalDrive, SelectedDrive.SectorSize, 0, 1, buffer)) {
uprintf("Could not write MBR\n"); uprintf("Could not write MBR\n");
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_WRITE_FAULT; FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_WRITE_FAULT;
goto out; goto out;
@ -970,7 +970,7 @@ notify:
uprintf("Failed to notify system about disk properties update: %s\n", WindowsErrorString()); uprintf("Failed to notify system about disk properties update: %s\n", WindowsErrorString());
out: out:
safe_free(buf); safe_mm_free(buffer);
return r; return r;
} }
@ -1143,7 +1143,7 @@ static BOOL SetupWinPE(char drive_letter)
HANDLE handle = INVALID_HANDLE_VALUE; HANDLE handle = INVALID_HANDLE_VALUE;
DWORD i, j, size, rw_size, index = 0; DWORD i, j, size, rw_size, index = 0;
BOOL r = FALSE; BOOL r = FALSE;
char* buf = NULL; char* buffer = NULL;
index = ((img_report.winpe&WINPE_I386) == WINPE_I386)?0:1; index = ((img_report.winpe&WINPE_I386) == WINPE_I386)?0:1;
// Allow other values than harddisk 1, as per user choice for disk ID // Allow other values than harddisk 1, as per user choice for disk ID
@ -1197,10 +1197,10 @@ static BOOL SetupWinPE(char drive_letter)
uprintf("Could not get size for file %s: %s\n", dst, WindowsErrorString()); uprintf("Could not get size for file %s: %s\n", dst, WindowsErrorString());
goto out; goto out;
} }
buf = (char*)malloc(size); buffer = (char*)malloc(size);
if (buf == NULL) if (buffer == NULL)
goto out; goto out;
if ((!ReadFile(handle, buf, size, &rw_size, NULL)) || (size != rw_size)) { if ((!ReadFile(handle, buffer, size, &rw_size, NULL)) || (size != rw_size)) {
uprintf("Could not read file %s: %s\n", dst, WindowsErrorString()); uprintf("Could not read file %s: %s\n", dst, WindowsErrorString());
goto out; goto out;
} }
@ -1209,16 +1209,16 @@ static BOOL SetupWinPE(char drive_letter)
// Patch setupldr.bin // Patch setupldr.bin
uprintf("Patching file %s\n", dst); uprintf("Patching file %s\n", dst);
// Remove CRC check for 32 bit part of setupldr.bin from Win2k3 // Remove CRC check for 32 bit part of setupldr.bin from Win2k3
if ((size > 0x2061) && (buf[0x2060] == 0x74) && (buf[0x2061] == 0x03)) { if ((size > 0x2061) && (buffer[0x2060] == 0x74) && (buffer[0x2061] == 0x03)) {
buf[0x2060] = 0xeb; buffer[0x2060] = 0xeb;
buf[0x2061] = 0x1a; buffer[0x2061] = 0x1a;
uprintf(" 0x00002060: 0x74 0x03 -> 0xEB 0x1A (disable Win2k3 CRC check)\n"); uprintf(" 0x00002060: 0x74 0x03 -> 0xEB 0x1A (disable Win2k3 CRC check)\n");
} }
for (i=1; i<size-32; i++) { for (i=1; i<size-32; i++) {
for (j=0; j<ARRAYSIZE(patch_str_org); j++) { 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) { if (safe_strnicmp(&buffer[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]); uprintf(" 0x%08X: '%s' -> '%s'\n", i, &buffer[i], patch_str_rep[j]);
strcpy(&buf[i], patch_str_rep[j]); strcpy(&buffer[i], patch_str_rep[j]);
i += (DWORD)max(strlen(patch_str_org[j]), strlen(patch_str_rep[j])); // in case org is a substring of rep i += (DWORD)max(strlen(patch_str_org[j]), strlen(patch_str_rep[j])); // in case org is a substring of rep
} }
} }
@ -1229,33 +1229,30 @@ static BOOL SetupWinPE(char drive_letter)
for (i=0; i<size-32; i++) { for (i=0; i<size-32; i++) {
// rdisk(0) -> rdisk(#) disk masquerading // rdisk(0) -> rdisk(#) disk masquerading
// NB: only the first one seems to be needed // NB: only the first one seems to be needed
if (safe_strnicmp(&buf[i], rdisk_zero, strlen(rdisk_zero)-1) == 0) { if (safe_strnicmp(&buffer[i], rdisk_zero, strlen(rdisk_zero)-1) == 0) {
buf[i+6] = 0x30 + ComboBox_GetCurSel(hDiskID); buffer[i+6] = 0x30 + ComboBox_GetCurSel(hDiskID);
uprintf(" 0x%08X: '%s' -> 'rdisk(%c)'\n", i, rdisk_zero, buf[i+6]); uprintf(" 0x%08X: '%s' -> 'rdisk(%c)'\n", i, rdisk_zero, buffer[i+6]);
} }
// $WIN_NT$_~BT -> i386 // $WIN_NT$_~BT -> i386
if (safe_strnicmp(&buf[i], win_nt_bt_org, strlen(win_nt_bt_org)-1) == 0) { if (safe_strnicmp(&buffer[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)]); uprintf(" 0x%08X: '%s' -> '%s%s'\n", i, &buffer[i], win_nt_bt_rep, &buffer[i+strlen(win_nt_bt_org)]);
strcpy(&buf[i], win_nt_bt_rep); strcpy(&buffer[i], win_nt_bt_rep);
// This ensures that we keep the terminator backslash // This ensures that we keep the terminator backslash
buf[i+strlen(win_nt_bt_rep)] = buf[i+strlen(win_nt_bt_org)]; buffer[i+strlen(win_nt_bt_rep)] = buffer[i+strlen(win_nt_bt_org)];
buf[i+strlen(win_nt_bt_rep)+1] = 0; buffer[i+strlen(win_nt_bt_rep)+1] = 0;
} }
} }
} }
if (!WriteFileWithRetry(handle, buf, size, &rw_size, WRITE_RETRIES)) { if (!WriteFileWithRetry(handle, buffer, size, &rw_size, WRITE_RETRIES)) {
uprintf("Could not write patched file: %s\n", WindowsErrorString()); uprintf("Could not write patched file: %s\n", WindowsErrorString());
goto out; goto out;
} }
safe_free(buf);
safe_closehandle(handle);
r = TRUE; r = TRUE;
out: out:
safe_closehandle(handle); safe_closehandle(handle);
safe_free(buf); safe_free(buffer);
return r; return r;
} }
@ -1466,7 +1463,7 @@ static BOOL WriteDrive(HANDLE hPhysicalDrive, HANDLE hSourceImage)
LARGE_INTEGER li; LARGE_INTEGER li;
DWORD rSize, wSize, BufSize; DWORD rSize, wSize, BufSize;
uint64_t wb, target_size = hSourceImage?img_report.projected_size:SelectedDrive.DiskSize; uint64_t wb, target_size = hSourceImage?img_report.projected_size:SelectedDrive.DiskSize;
uint8_t *buffer = NULL, *aligned_buffer; uint8_t *buffer = NULL;
int i; int i;
// We poked the MBR and other stuff, so we need to rewind // We poked the MBR and other stuff, so we need to rewind
@ -1482,16 +1479,20 @@ static BOOL WriteDrive(HANDLE hPhysicalDrive, HANDLE hSourceImage)
bled_exit(); bled_exit();
} else { } else {
uprintf(hSourceImage?"Writing Image...":"Zeroing drive..."); uprintf(hSourceImage?"Writing Image...":"Zeroing drive...");
// Our buffer size must be a multiple of the sector size // Our buffer size must be a multiple of the sector size and *ALIGNED* to the sector size
BufSize = ((DD_BUFFER_SIZE + SelectedDrive.SectorSize - 1) / SelectedDrive.SectorSize) * SelectedDrive.SectorSize; BufSize = ((DD_BUFFER_SIZE + SelectedDrive.SectorSize - 1) / SelectedDrive.SectorSize) * SelectedDrive.SectorSize;
buffer = (uint8_t*)calloc(BufSize + SelectedDrive.SectorSize, 1); // +1 sector for align buffer = (uint8_t*)_mm_malloc(BufSize, SelectedDrive.SectorSize);
if (buffer == NULL) { if (buffer == NULL) {
FormatStatus = ERROR_SEVERITY_ERROR | FAC(FACILITY_STORAGE) | ERROR_NOT_ENOUGH_MEMORY; FormatStatus = ERROR_SEVERITY_ERROR | FAC(FACILITY_STORAGE) | ERROR_NOT_ENOUGH_MEMORY;
uprintf("could not allocate disk write buffer"); uprintf("Could not allocate disk write buffer");
goto out;
}
// Sanity check
if ((uintptr_t)buffer % SelectedDrive.SectorSize != 0) {
FormatStatus = ERROR_SEVERITY_ERROR | FAC(FACILITY_STORAGE) | ERROR_READ_FAULT;
uprintf("Write buffer is not aligned");
goto out; goto out;
} }
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa365747.aspx does buffer sector alignment
aligned_buffer = ((void *)((((uintptr_t)(buffer)) + (SelectedDrive.SectorSize)-1) & (~(((uintptr_t)(SelectedDrive.SectorSize)) - 1))));
// Don't bother trying for something clever, using double buffering overlapped and whatnot: // Don't bother trying for something clever, using double buffering overlapped and whatnot:
// With Windows' default optimizations, sync read + sync write for sequential operations // With Windows' default optimizations, sync read + sync write for sequential operations
@ -1506,7 +1507,7 @@ static BOOL WriteDrive(HANDLE hPhysicalDrive, HANDLE hSourceImage)
} }
if (hSourceImage != NULL) { if (hSourceImage != NULL) {
s = ReadFile(hSourceImage, aligned_buffer, BufSize, &rSize, NULL); s = ReadFile(hSourceImage, buffer, BufSize, &rSize, NULL);
if (!s) { if (!s) {
FormatStatus = ERROR_SEVERITY_ERROR | FAC(FACILITY_STORAGE) | ERROR_READ_FAULT; FormatStatus = ERROR_SEVERITY_ERROR | FAC(FACILITY_STORAGE) | ERROR_READ_FAULT;
uprintf("read error: %s", WindowsErrorString()); uprintf("read error: %s", WindowsErrorString());
@ -1525,7 +1526,7 @@ static BOOL WriteDrive(HANDLE hPhysicalDrive, HANDLE hSourceImage)
rSize = ((rSize + SelectedDrive.SectorSize - 1) / SelectedDrive.SectorSize) * SelectedDrive.SectorSize; rSize = ((rSize + SelectedDrive.SectorSize - 1) / SelectedDrive.SectorSize) * SelectedDrive.SectorSize;
for (i = 0; i < WRITE_RETRIES; i++) { for (i = 0; i < WRITE_RETRIES; i++) {
CHECK_FOR_USER_CANCEL; CHECK_FOR_USER_CANCEL;
s = WriteFile(hPhysicalDrive, aligned_buffer, rSize, &wSize, NULL); s = WriteFile(hPhysicalDrive, buffer, rSize, &wSize, NULL);
if ((s) && (wSize == rSize)) if ((s) && (wSize == rSize))
break; break;
if (s) if (s)
@ -1547,7 +1548,7 @@ static BOOL WriteDrive(HANDLE hPhysicalDrive, HANDLE hSourceImage)
RefreshDriveLayout(hPhysicalDrive); RefreshDriveLayout(hPhysicalDrive);
ret = TRUE; ret = TRUE;
out: out:
safe_free(buffer); safe_mm_free(buffer);
return ret; return ret;
} }
@ -2033,7 +2034,7 @@ DWORD WINAPI SaveImageThread(void* param)
} }
uprintf("Saving to image '%s'...", vhd_save->path); uprintf("Saving to image '%s'...", vhd_save->path);
buffer = (uint8_t*)malloc(DD_BUFFER_SIZE); buffer = (uint8_t*)_mm_malloc(DD_BUFFER_SIZE, 16);
if (buffer == NULL) { if (buffer == NULL) {
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_NOT_ENOUGH_MEMORY; FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_NOT_ENOUGH_MEMORY;
uprintf("could not allocate buffer"); uprintf("could not allocate buffer");
@ -2094,7 +2095,7 @@ DWORD WINAPI SaveImageThread(void* param)
out: out:
safe_free(vhd_save->path); safe_free(vhd_save->path);
safe_free(buffer); safe_mm_free(buffer);
safe_closehandle(hDestImage); safe_closehandle(hDestImage);
safe_unlockclose(hPhysicalDrive); safe_unlockclose(hPhysicalDrive);
PostMessage(hMainDialog, UM_FORMAT_COMPLETED, (WPARAM)TRUE, 0); PostMessage(hMainDialog, UM_FORMAT_COMPLETED, (WPARAM)TRUE, 0);

View File

@ -144,7 +144,6 @@
<AdditionalIncludeDirectories>../inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>../inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary> <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat> <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<AdditionalOptions>/analyze:stacksize32850 %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>28252;28253</DisableSpecificWarnings> <DisableSpecificWarnings>28252;28253</DisableSpecificWarnings>
<CompileAs>CompileAsC</CompileAs> <CompileAs>CompileAsC</CompileAs>
</ClCompile> </ClCompile>
@ -163,7 +162,6 @@
<AdditionalIncludeDirectories>../inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>../inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary> <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat> <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<AdditionalOptions>/analyze:stacksize32850 %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>28252;28253</DisableSpecificWarnings> <DisableSpecificWarnings>28252;28253</DisableSpecificWarnings>
<CompileAs>CompileAsC</CompileAs> <CompileAs>CompileAsC</CompileAs>
</ClCompile> </ClCompile>
@ -179,7 +177,6 @@
<AdditionalIncludeDirectories>../inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>../inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary> <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat> <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<AdditionalOptions>/analyze:stacksize32850 %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>28252;28253</DisableSpecificWarnings> <DisableSpecificWarnings>28252;28253</DisableSpecificWarnings>
<CompileAs>CompileAsC</CompileAs> <CompileAs>CompileAsC</CompileAs>
</ClCompile> </ClCompile>
@ -198,7 +195,6 @@
<AdditionalIncludeDirectories>../inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>../inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary> <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat> <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<AdditionalOptions>/analyze:stacksize32850 %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>28252;28253</DisableSpecificWarnings> <DisableSpecificWarnings>28252;28253</DisableSpecificWarnings>
<CompileAs>CompileAsC</CompileAs> <CompileAs>CompileAsC</CompileAs>
</ClCompile> </ClCompile>

View File

@ -99,22 +99,37 @@ int64_t read_sectors(HANDLE hDrive, uint64_t SectorSize,
int contains_data(FILE *fp, uint64_t Position, int contains_data(FILE *fp, uint64_t Position,
const void *pData, uint64_t Len) const void *pData, uint64_t Len)
{ {
unsigned char aucBuf[MAX_DATA_LEN]; int r = 0;
unsigned char *aucBuf = _mm_malloc(MAX_DATA_LEN, 16);
if(aucBuf == NULL)
return 0;
if(!read_data(fp, Position, aucBuf, Len)) if(!read_data(fp, Position, aucBuf, Len))
return 0; goto out;
if(memcmp(pData, aucBuf, (size_t)Len)) if(memcmp(pData, aucBuf, (size_t)Len))
return 0; goto out;
return 1;
r = 1;
out:
_mm_free(aucBuf);
return r;
} /* contains_data */ } /* contains_data */
int read_data(FILE *fp, uint64_t Position, int read_data(FILE *fp, uint64_t Position,
void *pData, uint64_t Len) void *pData, uint64_t Len)
{ {
unsigned char aucBuf[MAX_DATA_LEN]; int r = 0;
unsigned char *aucBuf = _mm_malloc(MAX_DATA_LEN, 16);
FAKE_FD* fd = (FAKE_FD*)fp; FAKE_FD* fd = (FAKE_FD*)fp;
HANDLE hDrive = (HANDLE)fd->_handle; HANDLE hDrive = (HANDLE)fd->_handle;
uint64_t StartSector, EndSector, NumSectors; uint64_t StartSector, EndSector, NumSectors;
if (aucBuf == NULL)
return 0;
Position += fd->_offset; Position += fd->_offset;
StartSector = Position/ulBytesPerSector; StartSector = Position/ulBytesPerSector;
@ -123,32 +138,44 @@ int read_data(FILE *fp, uint64_t Position,
if((NumSectors*ulBytesPerSector) > MAX_DATA_LEN) if((NumSectors*ulBytesPerSector) > MAX_DATA_LEN)
{ {
uprintf("contains_data: please increase MAX_DATA_LEN in file.h\n"); uprintf("read_data: Please increase MAX_DATA_LEN in file.h\n");
return 0; goto out;
} }
if(Len > 0xFFFFFFFFUL) if(Len > 0xFFFFFFFFUL)
{ {
uprintf("contains_data: Len is too big\n"); uprintf("read_data: Len is too big\n");
return 0; goto out;
} }
if(read_sectors(hDrive, ulBytesPerSector, StartSector, if(read_sectors(hDrive, ulBytesPerSector, StartSector,
NumSectors, aucBuf) <= 0) NumSectors, aucBuf) <= 0)
return 0; goto out;
memcpy(pData, &aucBuf[Position - StartSector*ulBytesPerSector], (size_t)Len); memcpy(pData, &aucBuf[Position - StartSector*ulBytesPerSector], (size_t)Len);
return 1;
r = 1;
out:
_mm_free(aucBuf);
return r;
} /* read_data */ } /* read_data */
/* May read/write the same sector many times, but compatible with existing ms-sys */ /* May read/write the same sector many times, but compatible with existing ms-sys */
int write_data(FILE *fp, uint64_t Position, int write_data(FILE *fp, uint64_t Position,
const void *pData, uint64_t Len) const void *pData, uint64_t Len)
{ {
unsigned char aucBuf[MAX_DATA_LEN]; int r = 0;
/* Windows' WriteFile() may require a buffer that is aligned to the sector size */
/* TODO: We may need to increase the alignment if we get report of issues on 4K */
unsigned char *aucBuf = _mm_malloc(MAX_DATA_LEN, 512);
FAKE_FD* fd = (FAKE_FD*)fp; FAKE_FD* fd = (FAKE_FD*)fp;
HANDLE hDrive = (HANDLE)fd->_handle; HANDLE hDrive = (HANDLE)fd->_handle;
uint64_t StartSector, EndSector, NumSectors; uint64_t StartSector, EndSector, NumSectors;
if (aucBuf == NULL)
return 0;
Position += fd->_offset; Position += fd->_offset;
StartSector = Position/ulBytesPerSector; StartSector = Position/ulBytesPerSector;
@ -157,26 +184,31 @@ int write_data(FILE *fp, uint64_t Position,
if((NumSectors*ulBytesPerSector) > MAX_DATA_LEN) if((NumSectors*ulBytesPerSector) > MAX_DATA_LEN)
{ {
uprintf("Please increase MAX_DATA_LEN in file.h\n"); uprintf("write_data: Please increase MAX_DATA_LEN in file.h\n");
return 0; goto out;
} }
if(Len > 0xFFFFFFFFUL) if(Len > 0xFFFFFFFFUL)
{ {
uprintf("write_data: Len is too big\n"); uprintf("write_data: Len is too big\n");
return 0; goto out;
} }
/* Data to write may not be aligned on a sector boundary => read into a sector buffer first */ /* Data to write may not be aligned on a sector boundary => read into a sector buffer first */
if(read_sectors(hDrive, ulBytesPerSector, StartSector, if(read_sectors(hDrive, ulBytesPerSector, StartSector,
NumSectors, aucBuf) <= 0) NumSectors, aucBuf) <= 0)
return 0; goto out;
if(!memcpy(&aucBuf[Position - StartSector*ulBytesPerSector], pData, (size_t)Len)) if(!memcpy(&aucBuf[Position - StartSector*ulBytesPerSector], pData, (size_t)Len))
return 0; goto out;
if(write_sectors(hDrive, ulBytesPerSector, StartSector, if(write_sectors(hDrive, ulBytesPerSector, StartSector,
NumSectors, aucBuf) <= 0) NumSectors, aucBuf) <= 0)
return 0; goto out;
return 1;
r = 1;
out:
_mm_free(aucBuf);
return r;
} /* write_data */ } /* write_data */

View File

@ -94,6 +94,7 @@
#define CHECK_FOR_USER_CANCEL if (IS_ERROR(FormatStatus)) goto out #define CHECK_FOR_USER_CANCEL if (IS_ERROR(FormatStatus)) goto out
#define safe_free(p) do {free((void*)p); p = NULL;} while(0) #define safe_free(p) do {free((void*)p); p = NULL;} while(0)
#define safe_mm_free(p) do {_mm_free((void*)p); p = NULL;} while(0)
#define safe_min(a, b) min((size_t)(a), (size_t)(b)) #define safe_min(a, b) min((size_t)(a), (size_t)(b))
#define safe_strcp(dst, dst_max, src, count) do {memcpy(dst, src, safe_min(count, dst_max)); \ #define safe_strcp(dst, dst_max, src, count) do {memcpy(dst, src, safe_min(count, dst_max)); \
((char*)dst)[safe_min(count, dst_max)-1] = 0;} while(0) ((char*)dst)[safe_min(count, dst_max)-1] = 0;} while(0)

View File

@ -33,7 +33,7 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG DIALOGEX 12, 12, 242, 376 IDD_DIALOG DIALOGEX 12, 12, 242, 376
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_ACCEPTFILES EXSTYLE WS_EX_ACCEPTFILES
CAPTION "Rufus 2.10.957" CAPTION "Rufus 2.10.958"
FONT 8, "Segoe UI Symbol", 400, 0, 0x0 FONT 8, "Segoe UI Symbol", 400, 0, 0x0
BEGIN BEGIN
LTEXT "Device",IDS_DEVICE_TXT,9,6,200,8 LTEXT "Device",IDS_DEVICE_TXT,9,6,200,8
@ -320,8 +320,8 @@ END
// //
VS_VERSION_INFO VERSIONINFO VS_VERSION_INFO VERSIONINFO
FILEVERSION 2,10,957,0 FILEVERSION 2,10,958,0
PRODUCTVERSION 2,10,957,0 PRODUCTVERSION 2,10,958,0
FILEFLAGSMASK 0x3fL FILEFLAGSMASK 0x3fL
#ifdef _DEBUG #ifdef _DEBUG
FILEFLAGS 0x1L FILEFLAGS 0x1L
@ -338,13 +338,13 @@ BEGIN
BEGIN BEGIN
VALUE "CompanyName", "Akeo Consulting (http://akeo.ie)" VALUE "CompanyName", "Akeo Consulting (http://akeo.ie)"
VALUE "FileDescription", "Rufus" VALUE "FileDescription", "Rufus"
VALUE "FileVersion", "2.10.957" VALUE "FileVersion", "2.10.958"
VALUE "InternalName", "Rufus" VALUE "InternalName", "Rufus"
VALUE "LegalCopyright", "© 2011-2016 Pete Batard (GPL v3)" VALUE "LegalCopyright", "© 2011-2016 Pete Batard (GPL v3)"
VALUE "LegalTrademarks", "http://www.gnu.org/copyleft/gpl.html" VALUE "LegalTrademarks", "http://www.gnu.org/copyleft/gpl.html"
VALUE "OriginalFilename", "rufus.exe" VALUE "OriginalFilename", "rufus.exe"
VALUE "ProductName", "Rufus" VALUE "ProductName", "Rufus"
VALUE "ProductVersion", "2.10.957" VALUE "ProductVersion", "2.10.958"
END END
END END
BLOCK "VarFileInfo" BLOCK "VarFileInfo"

View File

@ -331,7 +331,7 @@ BOOL Identify(HANDLE hPhysical)
// You'll get an error here if your compiler does not properly pack the IDENTIFY struct // You'll get an error here if your compiler does not properly pack the IDENTIFY struct
COMPILE_TIME_ASSERT(sizeof(IDENTIFY_DEVICE_DATA) == 512); COMPILE_TIME_ASSERT(sizeof(IDENTIFY_DEVICE_DATA) == 512);
idd = (IDENTIFY_DEVICE_DATA*)_aligned_malloc(sizeof(IDENTIFY_DEVICE_DATA), 0x10); idd = (IDENTIFY_DEVICE_DATA*)_mm_malloc(sizeof(IDENTIFY_DEVICE_DATA), 0x10);
if (idd == NULL) if (idd == NULL)
return FALSE; return FALSE;
@ -352,7 +352,7 @@ BOOL Identify(HANDLE hPhysical)
if (i >= ARRAYSIZE(pt)) if (i >= ARRAYSIZE(pt))
uprintf("NO ATA FOR YOU!\n"); uprintf("NO ATA FOR YOU!\n");
_aligned_free(idd); _mm_free(idd);
return TRUE; return TRUE;
} }
#endif #endif

View File

@ -21,11 +21,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#if defined(__MINGW32__)
#define _aligned_malloc __mingw_aligned_malloc
#define _aligned_free __mingw_aligned_free
#endif
// From http://stackoverflow.com/a/9284679 // From http://stackoverflow.com/a/9284679
#define COMPILE_TIME_ASSERT(pred) switch(0) {case 0: case pred:;} #define COMPILE_TIME_ASSERT(pred) switch(0) {case 0: case pred:;}

View File

@ -45,7 +45,7 @@ unsigned long syslinux_ldlinux_len[2];
unsigned char* syslinux_mboot = NULL; unsigned char* syslinux_mboot = NULL;
unsigned long syslinux_mboot_len; unsigned long syslinux_mboot_len;
// Workaround for 4K support /* Workaround for 4K support */
uint32_t SECTOR_SHIFT = 9; uint32_t SECTOR_SHIFT = 9;
uint32_t SECTOR_SIZE = 512; uint32_t SECTOR_SIZE = 512;
uint32_t LIBFAT_SECTOR_SHIFT = 9; uint32_t LIBFAT_SECTOR_SHIFT = 9;
@ -128,7 +128,7 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type)
LIBFAT_SECTOR_MASK = SECTOR_SIZE - 1; LIBFAT_SECTOR_MASK = SECTOR_SIZE - 1;
/* sectbuf should be aligned to at least 8 bytes - see github #767 */ /* sectbuf should be aligned to at least 8 bytes - see github #767 */
sectbuf = _aligned_malloc(SECTOR_SIZE, 16); sectbuf = _mm_malloc(SECTOR_SIZE, 16);
if (sectbuf == NULL) if (sectbuf == NULL)
goto out; goto out;
@ -382,8 +382,7 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type)
r = TRUE; r = TRUE;
out: out:
if (sectbuf != NULL) safe_mm_free(sectbuf);
_aligned_free(sectbuf);
safe_free(syslinux_ldlinux[0]); safe_free(syslinux_ldlinux[0]);
safe_free(syslinux_ldlinux[1]); safe_free(syslinux_ldlinux[1]);
safe_free(sectors); safe_free(sectors);

View File

@ -17,21 +17,17 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <malloc.h>
#include "libfatint.h" #include "libfatint.h"
/* /*
* We need to align our sector buffers to at least the 8-byte mark, as some Windows * NB: We need to align our sector buffers to at least the 8-byte mark, as some Windows
* disk devices, notably O2Micro PCI-E SD card readers, return ERROR_INVALID_PARAMETER * disk devices, notably O2Micro PCI-E SD card readers, return ERROR_INVALID_PARAMETER
* when attempting to use ReadFile() against a non 8-byte aligned buffer. * when attempting to use ReadFile() against a non 8-byte aligned buffer.
* For good measure, we'll go further and align our buffers on a 16-byte boundary. * For good measure, we'll go further and align our buffers on a 16-byte boundary.
* Also, since struct libfat_sector's data[0] is our buffer, this means we must BOTH * Also, since struct libfat_sector's data[0] is our buffer, this means we must BOTH
* align that member in the struct declaration, and use aligned malloc/free. * align that member in the struct declaration, and use aligned malloc/free.
*/ */
#if defined(__MINGW32__)
#define _aligned_malloc __mingw_aligned_malloc
#define _aligned_free __mingw_aligned_free
#endif
extern void _uprintf(const char *format, ...); extern void _uprintf(const char *format, ...);
void *libfat_get_sector(struct libfat_filesystem *fs, libfat_sector_t n) void *libfat_get_sector(struct libfat_filesystem *fs, libfat_sector_t n)
{ {
@ -43,10 +39,10 @@ void *libfat_get_sector(struct libfat_filesystem *fs, libfat_sector_t n)
} }
/* Not found in cache */ /* Not found in cache */
ls = _aligned_malloc(sizeof(struct libfat_sector) + LIBFAT_SECTOR_SIZE, 16); ls = _mm_malloc(sizeof(struct libfat_sector) + LIBFAT_SECTOR_SIZE, 16);
if (!ls) { if (!ls) {
libfat_flush(fs); libfat_flush(fs);
ls = _aligned_malloc(sizeof(struct libfat_sector) + LIBFAT_SECTOR_SIZE, 16); ls = _mm_malloc(sizeof(struct libfat_sector) + LIBFAT_SECTOR_SIZE, 16);
if (!ls) if (!ls)
return NULL; /* Can't allocate memory */ return NULL; /* Can't allocate memory */
@ -54,7 +50,7 @@ void *libfat_get_sector(struct libfat_filesystem *fs, libfat_sector_t n)
if (fs->read(fs->readptr, ls->data, LIBFAT_SECTOR_SIZE, n) if (fs->read(fs->readptr, ls->data, LIBFAT_SECTOR_SIZE, n)
!= LIBFAT_SECTOR_SIZE) { != LIBFAT_SECTOR_SIZE) {
_aligned_free(ls); _mm_free(ls);
return NULL; /* I/O error */ return NULL; /* I/O error */
} }
@ -74,6 +70,6 @@ void libfat_flush(struct libfat_filesystem *fs)
for (ls = lsnext; ls; ls = lsnext) { for (ls = lsnext; ls; ls = lsnext) {
lsnext = ls->next; lsnext = ls->next;
_aligned_free(ls); _mm_free(ls);
} }
} }

View File

@ -30,12 +30,12 @@
#define ALIGN_END(m) #define ALIGN_END(m)
#endif #endif
struct libfat_sector { ALIGN_START(16) struct libfat_sector {
libfat_sector_t n; /* Sector number */ libfat_sector_t n; /* Sector number */
struct libfat_sector *next; /* Next in list */ struct libfat_sector *next; /* Next in list */
/* data[0] MUST be aligned to at least 8 bytes - see cache.c */ /* data[0] MUST be aligned to at least 8 bytes - see cache.c */
ALIGN_START(16) char data[0] ALIGN_END(16); ALIGN_START(16) char data[0] ALIGN_END(16);
}; } ALIGN_END(16);
enum fat_type { enum fat_type {
FAT12, FAT12,