2006-12-13 09:02:18 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2007-02-05 01:20:20 +00:00
|
|
|
* Copyright (C) 2006-2007 Dave Chapman
|
2006-12-13 09:02:18 +00:00
|
|
|
*
|
|
|
|
* error(), lock_volume() and unlock_volume() functions and inspiration taken
|
|
|
|
* from:
|
|
|
|
* RawDisk - Direct Disk Read/Write Access for NT/2000/XP
|
|
|
|
* Copyright (c) 2003 Jan Kiszka
|
|
|
|
* http://www.stud.uni-hannover.de/user/73174/RawDisk/
|
|
|
|
*
|
2008-06-28 18:10:04 +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.
|
2006-12-13 09:02:18 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-12-07 20:06:48 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
|
2006-12-13 09:02:18 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <windows.h>
|
2009-08-04 20:32:30 +00:00
|
|
|
#include <stddef.h>
|
2006-12-13 09:02:18 +00:00
|
|
|
#include <winioctl.h>
|
|
|
|
|
|
|
|
#include "ipodio.h"
|
|
|
|
|
|
|
|
static int lock_volume(HANDLE hDisk)
|
|
|
|
{
|
|
|
|
DWORD dummy;
|
|
|
|
|
|
|
|
return DeviceIoControl(hDisk, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0,
|
|
|
|
&dummy, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int unlock_volume(HANDLE hDisk)
|
|
|
|
{
|
|
|
|
DWORD dummy;
|
|
|
|
|
|
|
|
return DeviceIoControl(hDisk, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0,
|
|
|
|
&dummy, NULL);
|
|
|
|
}
|
|
|
|
|
2009-11-08 13:38:10 +00:00
|
|
|
void ipod_print_error(char* msg)
|
2006-12-13 09:02:18 +00:00
|
|
|
{
|
2009-09-20 17:03:58 +00:00
|
|
|
LPSTR pMsgBuf = NULL;
|
2006-12-13 09:02:18 +00:00
|
|
|
|
|
|
|
printf(msg);
|
2009-09-20 17:03:58 +00:00
|
|
|
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
2006-12-13 09:02:18 +00:00
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
|
2009-09-20 17:03:58 +00:00
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), pMsgBuf,
|
2006-12-13 09:02:18 +00:00
|
|
|
0, NULL);
|
|
|
|
printf(pMsgBuf);
|
|
|
|
LocalFree(pMsgBuf);
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int ipod_open(struct ipod_t* ipod, int silent)
|
2006-12-13 09:02:18 +00:00
|
|
|
{
|
|
|
|
DISK_GEOMETRY_EX diskgeometry_ex;
|
|
|
|
DISK_GEOMETRY diskgeometry;
|
|
|
|
unsigned long n;
|
|
|
|
|
2007-11-06 19:28:14 +00:00
|
|
|
ipod->dh = CreateFileA(ipod->diskname, GENERIC_READ,
|
2006-12-13 09:02:18 +00:00
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
|
|
|
|
FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL);
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod->dh == INVALID_HANDLE_VALUE) {
|
2009-11-08 13:38:10 +00:00
|
|
|
if (!silent) ipod_print_error(" Error opening disk: ");
|
2008-05-04 11:59:04 +00:00
|
|
|
if(GetLastError() == ERROR_ACCESS_DENIED)
|
|
|
|
return -2;
|
|
|
|
else
|
|
|
|
return -1;
|
2006-12-13 09:02:18 +00:00
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (!lock_volume(ipod->dh)) {
|
2009-11-08 13:38:10 +00:00
|
|
|
if (!silent) ipod_print_error(" Error locking disk: ");
|
2006-12-13 09:02:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-06-16 22:32:57 +00:00
|
|
|
/* Defaults */
|
|
|
|
ipod->num_heads = 0;
|
|
|
|
ipod->sectors_per_track = 0;
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (!DeviceIoControl(ipod->dh,
|
2006-12-13 09:02:18 +00:00
|
|
|
IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
&diskgeometry_ex,
|
|
|
|
sizeof(diskgeometry_ex),
|
|
|
|
&n,
|
|
|
|
NULL)) {
|
2007-02-04 11:42:11 +00:00
|
|
|
if (!DeviceIoControl(ipod->dh,
|
2006-12-13 09:02:18 +00:00
|
|
|
IOCTL_DISK_GET_DRIVE_GEOMETRY,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
&diskgeometry,
|
|
|
|
sizeof(diskgeometry),
|
|
|
|
&n,
|
|
|
|
NULL)) {
|
2009-11-08 13:38:10 +00:00
|
|
|
if (!silent) ipod_print_error(" Error reading disk geometry: ");
|
2006-12-13 09:02:18 +00:00
|
|
|
return -1;
|
|
|
|
} else {
|
2007-06-16 22:32:57 +00:00
|
|
|
ipod->sector_size = diskgeometry.BytesPerSector;
|
|
|
|
ipod->num_heads = diskgeometry.TracksPerCylinder;
|
|
|
|
ipod->sectors_per_track = diskgeometry.SectorsPerTrack;
|
2006-12-13 09:02:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
2007-06-16 22:32:57 +00:00
|
|
|
ipod->sector_size = diskgeometry_ex.Geometry.BytesPerSector;
|
|
|
|
ipod->num_heads = diskgeometry_ex.Geometry.TracksPerCylinder;
|
|
|
|
ipod->sectors_per_track = diskgeometry_ex.Geometry.SectorsPerTrack;
|
2006-12-13 09:02:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int ipod_reopen_rw(struct ipod_t* ipod)
|
2006-12-13 09:02:18 +00:00
|
|
|
{
|
|
|
|
/* Close existing file and re-open for writing */
|
2007-02-04 11:42:11 +00:00
|
|
|
unlock_volume(ipod->dh);
|
|
|
|
CloseHandle(ipod->dh);
|
2006-12-13 09:02:18 +00:00
|
|
|
|
2007-11-06 19:28:14 +00:00
|
|
|
ipod->dh = CreateFileA(ipod->diskname, GENERIC_READ | GENERIC_WRITE,
|
2006-12-13 09:02:18 +00:00
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
|
|
|
|
FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL);
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (ipod->dh == INVALID_HANDLE_VALUE) {
|
2009-11-08 13:38:10 +00:00
|
|
|
ipod_print_error(" Error opening disk: ");
|
2006-12-13 09:02:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
if (!lock_volume(ipod->dh)) {
|
2009-11-08 13:38:10 +00:00
|
|
|
ipod_print_error(" Error locking disk: ");
|
2006-12-13 09:02:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int ipod_close(struct ipod_t* ipod)
|
2006-12-13 09:02:18 +00:00
|
|
|
{
|
2007-02-04 11:42:11 +00:00
|
|
|
unlock_volume(ipod->dh);
|
|
|
|
CloseHandle(ipod->dh);
|
2022-04-13 19:31:02 +00:00
|
|
|
ipod->dh = INVALID_HANDLE_VALUE;
|
2006-12-13 09:02:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-23 22:30:57 +00:00
|
|
|
int ipod_alloc_buffer(struct ipod_t* ipod, int bufsize)
|
2006-12-13 09:02:18 +00:00
|
|
|
{
|
|
|
|
/* The ReadFile function requires a memory buffer aligned to a multiple of
|
|
|
|
the disk sector size. */
|
2012-12-23 22:30:57 +00:00
|
|
|
ipod->sectorbuf = (unsigned char*)VirtualAlloc(NULL, bufsize, MEM_COMMIT, PAGE_READWRITE);
|
|
|
|
if (ipod->sectorbuf== NULL) {
|
2009-11-08 13:38:10 +00:00
|
|
|
ipod_print_error(" Error allocating a buffer: ");
|
2006-12-13 09:02:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-01 10:04:21 +00:00
|
|
|
int ipod_dealloc_buffer(struct ipod_t* ipod)
|
|
|
|
{
|
|
|
|
if (ipod->sectorbuf == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(!VirtualFree(ipod->sectorbuf, 0, MEM_RELEASE)) {
|
|
|
|
ipod_print_error(" Error releasing buffer ");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ipod->sectorbuf = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-04 11:42:11 +00:00
|
|
|
int ipod_seek(struct ipod_t* ipod, unsigned long pos)
|
2006-12-13 09:02:18 +00:00
|
|
|
{
|
2007-02-04 11:42:11 +00:00
|
|
|
if (SetFilePointer(ipod->dh, pos, NULL, FILE_BEGIN)==0xffffffff) {
|
2009-11-08 13:38:10 +00:00
|
|
|
ipod_print_error(" Seek error ");
|
2006-12-13 09:02:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-23 22:30:57 +00:00
|
|
|
ssize_t ipod_read(struct ipod_t* ipod, int nbytes)
|
2006-12-13 09:02:18 +00:00
|
|
|
{
|
|
|
|
unsigned long count;
|
|
|
|
|
2012-12-23 22:30:57 +00:00
|
|
|
if(ipod->sectorbuf == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!ReadFile(ipod->dh, ipod->sectorbuf, nbytes, &count, NULL)) {
|
2009-11-08 13:38:10 +00:00
|
|
|
ipod_print_error(" Error reading from disk: ");
|
2006-12-13 09:02:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-12-23 22:30:57 +00:00
|
|
|
ssize_t ipod_write(struct ipod_t* ipod, int nbytes)
|
2006-12-13 09:02:18 +00:00
|
|
|
{
|
|
|
|
unsigned long count;
|
|
|
|
|
2012-12-23 22:30:57 +00:00
|
|
|
if(ipod->sectorbuf == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!WriteFile(ipod->dh, ipod->sectorbuf, nbytes, &count, NULL)) {
|
2009-11-08 13:38:10 +00:00
|
|
|
ipod_print_error(" Error writing to disk: ");
|
2006-12-13 09:02:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
2009-08-04 20:32:30 +00:00
|
|
|
|
2011-12-07 20:06:48 +00:00
|
|
|
#endif
|
|
|
|
|