2011-11-29 20:18:23 +00:00
|
|
|
/******************************************************************
|
|
|
|
Copyright (C) 2009 Henrik Carlqvist
|
2012-01-02 23:55:47 +00:00
|
|
|
Modified for Rufus/Windows (C) 2011-2012 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"
|
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
uprintf("write_sectors: Could not access sector %d - %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((!WriteFile(hDrive, pBuf, Size, &Size, NULL)) || (Size != nSectors*SectorSize))
|
2011-11-29 20:18:23 +00:00
|
|
|
{
|
|
|
|
uprintf("write_sectors: Write error - %s\n", WindowsErrorString());
|
2011-12-02 00:11:53 +00:00
|
|
|
uprintf(" StartSector:%0X, nSectors:%0X, SectorSize:%0X\n", StartSector, nSectors, SectorSize);
|
2011-12-06 02:23:28 +00:00
|
|
|
return Size;
|
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))
|
|
|
|
{
|
|
|
|
uprintf("read_sectors: Could not access sector %d - %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
|
|
|
{
|
|
|
|
uprintf("read_sectors: Read error - %s\n", WindowsErrorString());
|
2011-12-02 00:11:53 +00:00
|
|
|
uprintf(" StartSector:%0X, nSectors:%0X, SectorSize:%0X\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
|
|
|
/*
|
|
|
|
* The following calls use a bastardized fp on Windows that contains:
|
|
|
|
* fp->_ptr: a Windows handle
|
|
|
|
* fp->_bufsiz: the sector size
|
|
|
|
* fp->_cnt: a file offset
|
|
|
|
*/
|
2012-03-03 22:59:58 +00:00
|
|
|
int contains_data(FILE *fp, uint64_t Position,
|
|
|
|
const void *pData, uint64_t Len)
|
2011-11-29 20:18:23 +00:00
|
|
|
{
|
|
|
|
unsigned char aucBuf[MAX_DATA_LEN];
|
|
|
|
HANDLE hDrive = (HANDLE)fp->_ptr;
|
2012-03-03 22:59:58 +00:00
|
|
|
uint64_t SectorSize = (uint64_t)fp->_bufsiz;
|
|
|
|
uint64_t StartSector, EndSector, NumSectors;
|
|
|
|
Position += (uint64_t)fp->_cnt;
|
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;
|
2012-03-03 22:59:58 +00:00
|
|
|
NumSectors = (size_t)(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
|
|
|
{
|
2012-03-03 22:59:58 +00:00
|
|
|
uprintf("contains_data: please increase MAX_DATA_LEN in file.h\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Len > 0xFFFFFFFFUL)
|
|
|
|
{
|
|
|
|
uprintf("contains_data: Len is too big\n");
|
2011-11-29 20:18:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:23:28 +00:00
|
|
|
if(read_sectors(hDrive, SectorSize, StartSector,
|
|
|
|
NumSectors, aucBuf) <= 0)
|
2011-11-29 20:18:23 +00:00
|
|
|
return 0;
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
if(memcmp(pData, &aucBuf[Position - StartSector*SectorSize], (size_t)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 */
|
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
|
|
|
{
|
|
|
|
unsigned char aucBuf[MAX_DATA_LEN];
|
|
|
|
HANDLE hDrive = (HANDLE)fp->_ptr;
|
2012-03-03 22:59:58 +00:00
|
|
|
uint64_t SectorSize = (uint64_t)fp->_bufsiz;
|
|
|
|
uint64_t StartSector, EndSector, NumSectors;
|
|
|
|
Position += (uint64_t)fp->_cnt;
|
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;
|
|
|
|
}
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
if(Len > 0xFFFFFFFFUL)
|
|
|
|
{
|
|
|
|
uprintf("write_data: Len is too big\n");
|
|
|
|
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-06 02:23:28 +00:00
|
|
|
if(read_sectors(hDrive, SectorSize, StartSector,
|
|
|
|
NumSectors, aucBuf) <= 0)
|
2011-11-29 20:18:23 +00:00
|
|
|
return 0;
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
if(!memcpy(&aucBuf[Position - StartSector*SectorSize], pData, (size_t)Len))
|
2011-11-29 20:18:23 +00:00
|
|
|
return 0;
|
|
|
|
|
2011-12-06 02:23:28 +00:00
|
|
|
if(write_sectors(hDrive, SectorSize, StartSector,
|
|
|
|
NumSectors, aucBuf) <= 0)
|
2011-11-29 20:18:23 +00:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
} /* write_data */
|