2011-11-29 20:18:23 +00:00
|
|
|
/******************************************************************
|
|
|
|
Copyright (C) 2009 Henrik Carlqvist
|
2011-11-29 23:45:19 +00:00
|
|
|
Modified for Rufus/Windows (C) 2011 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>
|
|
|
|
|
|
|
|
#include "rufus.h"
|
|
|
|
#include "file.h"
|
|
|
|
|
|
|
|
int write_sectors(HANDLE hDrive, size_t SectorSize,
|
|
|
|
size_t StartSector, size_t nSectors,
|
|
|
|
const void *pBuf, size_t BufSize)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER ptr;
|
2011-12-01 16:22:00 +00:00
|
|
|
DWORD Size;
|
2011-11-29 20:18:23 +00:00
|
|
|
|
|
|
|
if(SectorSize * nSectors > BufSize)
|
|
|
|
{
|
|
|
|
uprintf("write_sectors: Buffer is too small\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr.QuadPart = StartSector*SectorSize;
|
|
|
|
if(!SetFilePointerEx(hDrive, ptr, NULL, FILE_BEGIN))
|
|
|
|
{
|
|
|
|
uprintf("write_sectors: Could not access sector %d - %s\n", StartSector, WindowsErrorString());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-01 16:22:00 +00:00
|
|
|
if((!WriteFile(hDrive, pBuf, (DWORD)BufSize, &Size, NULL)) || (Size != BufSize))
|
2011-11-29 20:18:23 +00:00
|
|
|
{
|
|
|
|
uprintf("write_sectors: Write error - %s\n", WindowsErrorString());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int read_sectors(HANDLE hDrive, size_t SectorSize,
|
|
|
|
size_t StartSector, size_t nSectors,
|
|
|
|
void *pBuf, size_t BufSize)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER ptr;
|
2011-12-01 16:22:00 +00:00
|
|
|
DWORD Size;
|
2011-11-29 20:18:23 +00:00
|
|
|
|
|
|
|
if(SectorSize * nSectors > BufSize)
|
|
|
|
{
|
|
|
|
uprintf("read_sectors: Buffer is too small\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr.QuadPart = StartSector*SectorSize;
|
|
|
|
if(!SetFilePointerEx(hDrive, ptr, NULL, FILE_BEGIN))
|
|
|
|
{
|
|
|
|
uprintf("read_sectors: Could not access sector %d - %s\n", StartSector, WindowsErrorString());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-01 16:22:00 +00:00
|
|
|
if((!ReadFile(hDrive, pBuf, (DWORD)BufSize, &Size, NULL)) || (Size != BufSize))
|
2011-11-29 20:18:23 +00:00
|
|
|
{
|
|
|
|
uprintf("read_sectors: Read error - %s\n", WindowsErrorString());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use a bastardized fp that contains a Windows handle and the sector size */
|
2011-12-01 16:22:00 +00:00
|
|
|
int contains_data(FILE *fp, size_t Position,
|
|
|
|
const void *pData, size_t Len)
|
2011-11-29 20:18:23 +00:00
|
|
|
{
|
|
|
|
unsigned char aucBuf[MAX_DATA_LEN];
|
|
|
|
HANDLE hDrive = (HANDLE)fp->_ptr;
|
2011-12-01 16:22:00 +00:00
|
|
|
size_t SectorSize = (size_t)fp->_bufsiz;
|
|
|
|
size_t StartSector, EndSector, NumSectors;
|
2011-11-29 20:18:23 +00:00
|
|
|
|
2011-12-01 16:22:00 +00:00
|
|
|
StartSector = Position/SectorSize;
|
|
|
|
EndSector = (Position+Len+SectorSize-1)/SectorSize;
|
|
|
|
NumSectors = EndSector - StartSector;
|
2011-11-29 20:18:23 +00:00
|
|
|
|
2011-12-01 16:22:00 +00:00
|
|
|
if((NumSectors*SectorSize) > MAX_DATA_LEN)
|
2011-11-29 20:18:23 +00:00
|
|
|
{
|
|
|
|
uprintf("Please increase MAX_DATA_LEN in file.h\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-01 16:22:00 +00:00
|
|
|
if(!read_sectors(hDrive, SectorSize, StartSector,
|
|
|
|
NumSectors, aucBuf, sizeof(aucBuf)))
|
2011-11-29 20:18:23 +00:00
|
|
|
return 0;
|
|
|
|
|
2011-12-01 16:22:00 +00:00
|
|
|
if(memcmp(pData, &aucBuf[Position - StartSector*SectorSize], Len))
|
2011-11-29 20:18:23 +00:00
|
|
|
return 0;
|
|
|
|
return 1;
|
2011-11-29 23:45:19 +00:00
|
|
|
} /* contains_data */
|
|
|
|
|
|
|
|
/* May read/write the same sector many times, but compatible with existing ms-sys */
|
2011-12-01 16:22:00 +00:00
|
|
|
int write_data(FILE *fp, size_t Position,
|
|
|
|
const void *pData, size_t Len)
|
2011-11-29 23:45:19 +00:00
|
|
|
{
|
|
|
|
unsigned char aucBuf[MAX_DATA_LEN];
|
|
|
|
HANDLE hDrive = (HANDLE)fp->_ptr;
|
2011-12-01 16:22:00 +00:00
|
|
|
size_t SectorSize = (size_t)fp->_bufsiz;
|
|
|
|
size_t StartSector, EndSector, NumSectors;
|
2011-11-29 23:45:19 +00:00
|
|
|
|
2011-12-01 16:22:00 +00:00
|
|
|
StartSector = Position/SectorSize;
|
|
|
|
EndSector = (Position+Len+SectorSize-1)/SectorSize;
|
|
|
|
NumSectors = EndSector - StartSector;
|
2011-11-29 20:18:23 +00:00
|
|
|
|
2011-12-01 16:22:00 +00:00
|
|
|
if((NumSectors*SectorSize) > MAX_DATA_LEN)
|
2011-11-29 20:18:23 +00:00
|
|
|
{
|
2011-11-29 23:45:19 +00:00
|
|
|
uprintf("Please increase MAX_DATA_LEN in file.h\n");
|
2011-11-29 20:18:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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 */
|
2011-12-01 16:22:00 +00:00
|
|
|
if(!read_sectors(hDrive, SectorSize, StartSector,
|
|
|
|
NumSectors, aucBuf, sizeof(aucBuf)))
|
2011-11-29 20:18:23 +00:00
|
|
|
return 0;
|
|
|
|
|
2011-12-01 16:22:00 +00:00
|
|
|
if(!memcpy(&aucBuf[Position - StartSector*SectorSize], pData, Len))
|
2011-11-29 20:18:23 +00:00
|
|
|
return 0;
|
|
|
|
|
2011-12-01 16:22:00 +00:00
|
|
|
if(!write_sectors(hDrive, SectorSize, StartSector,
|
|
|
|
NumSectors, aucBuf, sizeof(aucBuf)))
|
2011-11-29 20:18:23 +00:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
} /* write_data */
|