2011-11-29 20:18:23 +00:00
|
|
|
/******************************************************************
|
|
|
|
Copyright (C) 2009 Henrik Carlqvist
|
2019-04-09 20:37:08 +00:00
|
|
|
Modified for Rufus/Windows (C) 2011-2019 Pete Batard
|
2011-11-29 20:18:23 +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 2 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, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
******************************************************************/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2012-03-03 22:59:58 +00:00
|
|
|
#include <stdint.h>
|
2011-11-29 20:18:23 +00:00
|
|
|
|
2012-01-11 00:05:16 +00:00
|
|
|
#include "../rufus.h"
|
2011-11-29 20:18:23 +00:00
|
|
|
#include "file.h"
|
|
|
|
|
2016-01-07 15:49:58 +00:00
|
|
|
extern unsigned long ulBytesPerSector;
|
|
|
|
|
2011-12-06 02:23:28 +00:00
|
|
|
/* Returns the number of bytes written or -1 on error */
|
2012-03-03 22:59:58 +00:00
|
|
|
int64_t write_sectors(HANDLE hDrive, uint64_t SectorSize,
|
|
|
|
uint64_t StartSector, uint64_t nSectors,
|
|
|
|
const void *pBuf)
|
2011-11-29 20:18:23 +00:00
|
|
|
{
|
|
|
|
LARGE_INTEGER ptr;
|
2012-03-03 22:59:58 +00:00
|
|
|
DWORD Size;
|
|
|
|
|
|
|
|
if((nSectors*SectorSize) > 0xFFFFFFFFUL)
|
|
|
|
{
|
|
|
|
uprintf("write_sectors: nSectors x SectorSize is too big\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Size = (DWORD)(nSectors*SectorSize);
|
2011-11-29 20:18:23 +00:00
|
|
|
|
|
|
|
ptr.QuadPart = StartSector*SectorSize;
|
|
|
|
if(!SetFilePointerEx(hDrive, ptr, NULL, FILE_BEGIN))
|
|
|
|
{
|
2015-01-28 23:22:11 +00:00
|
|
|
uprintf("write_sectors: Could not access sector 0x%08" PRIx64 " - %s\n", StartSector, WindowsErrorString());
|
2011-12-06 02:23:28 +00:00
|
|
|
return -1;
|
2011-11-29 20:18:23 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 20:37:08 +00:00
|
|
|
LastWriteError = 0;
|
2020-07-17 20:51:15 +00:00
|
|
|
if(!WriteFileWithRetry(hDrive, pBuf, Size, &Size, WRITE_RETRIES))
|
2011-11-29 20:18:23 +00:00
|
|
|
{
|
2019-04-09 20:37:08 +00:00
|
|
|
LastWriteError = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|GetLastError();
|
2016-09-06 16:56:36 +00:00
|
|
|
uprintf("write_sectors: Write error %s\n", WindowsErrorString());
|
2015-01-28 23:22:11 +00:00
|
|
|
uprintf(" StartSector: 0x%08" PRIx64 ", nSectors: 0x%" PRIx64 ", SectorSize: 0x%" PRIx64 "\n", StartSector, nSectors, SectorSize);
|
2016-09-06 16:56:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (Size != nSectors*SectorSize)
|
|
|
|
{
|
|
|
|
/* Some large drives return 0, even though all the data was written - See github #787 */
|
|
|
|
if (large_drive && Size == 0) {
|
|
|
|
uprintf("Warning: Possible short write\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2019-04-09 20:37:08 +00:00
|
|
|
uprintf("write_sectors: Write error\n");
|
|
|
|
LastWriteError = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_WRITE_FAULT;
|
2016-09-06 16:56:36 +00:00
|
|
|
uprintf(" Wrote: %d, Expected: %" PRIu64 "\n", Size, nSectors*SectorSize);
|
|
|
|
uprintf(" StartSector: 0x%08" PRIx64 ", nSectors: 0x%" PRIx64 ", SectorSize: 0x%" PRIx64 "\n", StartSector, nSectors, SectorSize);
|
|
|
|
return -1;
|
2011-11-29 20:18:23 +00:00
|
|
|
}
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
return (int64_t)Size;
|
2011-11-29 20:18:23 +00:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:23:28 +00:00
|
|
|
/* Returns the number of bytes read or -1 on error */
|
2012-03-03 22:59:58 +00:00
|
|
|
int64_t read_sectors(HANDLE hDrive, uint64_t SectorSize,
|
|
|
|
uint64_t StartSector, uint64_t nSectors,
|
|
|
|
void *pBuf)
|
2011-11-29 20:18:23 +00:00
|
|
|
{
|
|
|
|
LARGE_INTEGER ptr;
|
2012-03-03 22:59:58 +00:00
|
|
|
DWORD Size;
|
|
|
|
|
|
|
|
if((nSectors*SectorSize) > 0xFFFFFFFFUL)
|
|
|
|
{
|
|
|
|
uprintf("read_sectors: nSectors x SectorSize is too big\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Size = (DWORD)(nSectors*SectorSize);
|
2011-11-29 20:18:23 +00:00
|
|
|
|
|
|
|
ptr.QuadPart = StartSector*SectorSize;
|
|
|
|
if(!SetFilePointerEx(hDrive, ptr, NULL, FILE_BEGIN))
|
|
|
|
{
|
2015-01-28 23:22:11 +00:00
|
|
|
uprintf("read_sectors: Could not access sector 0x%08" PRIx64 " - %s\n", StartSector, WindowsErrorString());
|
2011-12-06 02:23:28 +00:00
|
|
|
return -1;
|
2011-11-29 20:18:23 +00:00
|
|
|
}
|
|
|
|
|
2011-12-06 14:05:53 +00:00
|
|
|
if((!ReadFile(hDrive, pBuf, Size, &Size, NULL)) || (Size != nSectors*SectorSize))
|
2011-11-29 20:18:23 +00:00
|
|
|
{
|
2013-06-25 01:55:25 +00:00
|
|
|
uprintf("read_sectors: Read error %s\n", (GetLastError()!=ERROR_SUCCESS)?WindowsErrorString():"");
|
2015-01-28 23:22:11 +00:00
|
|
|
uprintf(" Read: %d, Expected: %" PRIu64 "\n", Size, nSectors*SectorSize);
|
|
|
|
uprintf(" StartSector: 0x%08" PRIx64 ", nSectors: 0x%" PRIx64 ", SectorSize: 0x%" PRIx64 "\n", StartSector, nSectors, SectorSize);
|
2011-11-29 20:18:23 +00:00
|
|
|
}
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
return (int64_t)Size;
|
2011-11-29 20:18:23 +00:00
|
|
|
}
|
|
|
|
|
2011-12-13 23:29:09 +00:00
|
|
|
/*
|
2016-01-07 15:49:58 +00:00
|
|
|
* The following calls use a hijacked fp on Windows that contains:
|
|
|
|
* fp->_handle: a Windows handle
|
|
|
|
* fp->_offset: a file offset
|
|
|
|
*/
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
int contains_data(FILE *fp, uint64_t Position,
|
2016-01-07 15:49:58 +00:00
|
|
|
const void *pData, uint64_t Len)
|
|
|
|
{
|
2016-06-10 11:42:43 +00:00
|
|
|
int r = 0;
|
|
|
|
unsigned char *aucBuf = _mm_malloc(MAX_DATA_LEN, 16);
|
2016-01-07 15:49:58 +00:00
|
|
|
|
2016-06-10 11:42:43 +00:00
|
|
|
if(aucBuf == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(!read_data(fp, Position, aucBuf, Len))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if(memcmp(pData, aucBuf, (size_t)Len))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
r = 1;
|
|
|
|
|
|
|
|
out:
|
|
|
|
_mm_free(aucBuf);
|
|
|
|
return r;
|
2016-01-07 15:49:58 +00:00
|
|
|
} /* contains_data */
|
|
|
|
|
|
|
|
int read_data(FILE *fp, uint64_t Position,
|
|
|
|
void *pData, uint64_t Len)
|
2011-11-29 20:18:23 +00:00
|
|
|
{
|
2016-06-10 11:42:43 +00:00
|
|
|
int r = 0;
|
|
|
|
unsigned char *aucBuf = _mm_malloc(MAX_DATA_LEN, 16);
|
2015-08-10 22:19:57 +00:00
|
|
|
FAKE_FD* fd = (FAKE_FD*)fp;
|
|
|
|
HANDLE hDrive = (HANDLE)fd->_handle;
|
2012-03-03 22:59:58 +00:00
|
|
|
uint64_t StartSector, EndSector, NumSectors;
|
2016-06-10 11:42:43 +00:00
|
|
|
|
|
|
|
if (aucBuf == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2015-08-10 22:19:57 +00:00
|
|
|
Position += fd->_offset;
|
2011-11-29 20:18:23 +00:00
|
|
|
|
2016-01-07 15:49:58 +00:00
|
|
|
StartSector = Position/ulBytesPerSector;
|
|
|
|
EndSector = (Position+Len+ulBytesPerSector -1)/ulBytesPerSector;
|
2012-03-03 22:59:58 +00:00
|
|
|
NumSectors = (size_t)(EndSector - StartSector);
|
2011-11-29 20:18:23 +00:00
|
|
|
|
2016-01-07 15:49:58 +00:00
|
|
|
if((NumSectors*ulBytesPerSector) > MAX_DATA_LEN)
|
2011-11-29 20:18:23 +00:00
|
|
|
{
|
2016-06-10 11:42:43 +00:00
|
|
|
uprintf("read_data: Please increase MAX_DATA_LEN in file.h\n");
|
|
|
|
goto out;
|
2012-03-03 22:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(Len > 0xFFFFFFFFUL)
|
|
|
|
{
|
2016-06-10 11:42:43 +00:00
|
|
|
uprintf("read_data: Len is too big\n");
|
|
|
|
goto out;
|
2011-11-29 20:18:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 15:49:58 +00:00
|
|
|
if(read_sectors(hDrive, ulBytesPerSector, StartSector,
|
2011-12-06 02:23:28 +00:00
|
|
|
NumSectors, aucBuf) <= 0)
|
2016-06-10 11:42:43 +00:00
|
|
|
goto out;
|
2011-11-29 20:18:23 +00:00
|
|
|
|
2016-01-07 15:49:58 +00:00
|
|
|
memcpy(pData, &aucBuf[Position - StartSector*ulBytesPerSector], (size_t)Len);
|
2016-06-10 11:42:43 +00:00
|
|
|
|
|
|
|
r = 1;
|
|
|
|
|
|
|
|
out:
|
|
|
|
_mm_free(aucBuf);
|
|
|
|
return r;
|
2016-01-07 15:49:58 +00:00
|
|
|
} /* read_data */
|
2011-11-29 23:45:19 +00:00
|
|
|
|
|
|
|
/* May read/write the same sector many times, but compatible with existing ms-sys */
|
2012-03-03 22:59:58 +00:00
|
|
|
int write_data(FILE *fp, uint64_t Position,
|
|
|
|
const void *pData, uint64_t Len)
|
2011-11-29 23:45:19 +00:00
|
|
|
{
|
2016-06-10 11:42:43 +00:00
|
|
|
int r = 0;
|
|
|
|
/* Windows' WriteFile() may require a buffer that is aligned to the sector size */
|
2019-09-06 10:32:37 +00:00
|
|
|
unsigned char *aucBuf = _mm_malloc(MAX_DATA_LEN, 4096);
|
2015-08-10 22:19:57 +00:00
|
|
|
FAKE_FD* fd = (FAKE_FD*)fp;
|
|
|
|
HANDLE hDrive = (HANDLE)fd->_handle;
|
2012-03-03 22:59:58 +00:00
|
|
|
uint64_t StartSector, EndSector, NumSectors;
|
2016-06-10 11:42:43 +00:00
|
|
|
|
|
|
|
if (aucBuf == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2015-08-10 22:19:57 +00:00
|
|
|
Position += fd->_offset;
|
2011-11-29 23:45:19 +00:00
|
|
|
|
2016-01-07 15:49:58 +00:00
|
|
|
StartSector = Position/ulBytesPerSector;
|
|
|
|
EndSector = (Position+Len+ulBytesPerSector-1)/ulBytesPerSector;
|
2011-12-01 16:22:00 +00:00
|
|
|
NumSectors = EndSector - StartSector;
|
2011-11-29 20:18:23 +00:00
|
|
|
|
2016-01-07 15:49:58 +00:00
|
|
|
if((NumSectors*ulBytesPerSector) > MAX_DATA_LEN)
|
2011-11-29 20:18:23 +00:00
|
|
|
{
|
2016-06-10 11:42:43 +00:00
|
|
|
uprintf("write_data: Please increase MAX_DATA_LEN in file.h\n");
|
|
|
|
goto out;
|
2011-11-29 20:18:23 +00:00
|
|
|
}
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
if(Len > 0xFFFFFFFFUL)
|
|
|
|
{
|
|
|
|
uprintf("write_data: Len is too big\n");
|
2016-06-10 11:42:43 +00:00
|
|
|
goto out;
|
2012-03-03 22:59:58 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:45:19 +00:00
|
|
|
/* Data to write may not be aligned on a sector boundary => read into a sector buffer first */
|
2016-01-07 15:49:58 +00:00
|
|
|
if(read_sectors(hDrive, ulBytesPerSector, StartSector,
|
2011-12-06 02:23:28 +00:00
|
|
|
NumSectors, aucBuf) <= 0)
|
2016-06-10 11:42:43 +00:00
|
|
|
goto out;
|
2011-11-29 20:18:23 +00:00
|
|
|
|
2016-01-07 15:49:58 +00:00
|
|
|
if(!memcpy(&aucBuf[Position - StartSector*ulBytesPerSector], pData, (size_t)Len))
|
2016-06-10 11:42:43 +00:00
|
|
|
goto out;
|
2011-11-29 20:18:23 +00:00
|
|
|
|
2016-01-07 15:49:58 +00:00
|
|
|
if(write_sectors(hDrive, ulBytesPerSector, StartSector,
|
2011-12-06 02:23:28 +00:00
|
|
|
NumSectors, aucBuf) <= 0)
|
2016-06-10 11:42:43 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
r = 1;
|
|
|
|
|
|
|
|
out:
|
|
|
|
_mm_free(aucBuf);
|
|
|
|
return r;
|
2011-11-29 20:18:23 +00:00
|
|
|
} /* write_data */
|