2007-03-15 22:55:36 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2007-03-15 23:02:37 +00:00
|
|
|
* $Id$
|
2007-03-15 22:55:36 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2007 Dave Chapman
|
|
|
|
*
|
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.
|
2007-03-15 22:55:36 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-12-04 19:40:35 +00:00
|
|
|
#if !defined(_WIN32) /* all non-Windows platforms supported are POSIX. */
|
2007-03-15 22:55:36 +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 <sys/ioctl.h>
|
2008-05-11 16:58:02 +00:00
|
|
|
#include <errno.h>
|
2007-03-15 22:55:36 +00:00
|
|
|
|
|
|
|
#if defined(linux) || defined (__linux)
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#define SANSA_SECTORSIZE_IOCTL BLKSSZGET
|
|
|
|
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) \
|
|
|
|
|| defined(__bsdi__) || defined(__DragonFly__)
|
|
|
|
#include <sys/disk.h>
|
|
|
|
#define SANSA_SECTORSIZE_IOCTL DIOCGSECTORSIZE
|
|
|
|
#elif defined(__APPLE__) && defined(__MACH__)
|
|
|
|
#include <sys/disk.h>
|
|
|
|
#define SANSA_SECTORSIZE_IOCTL DKIOCGETBLOCKSIZE
|
|
|
|
#else
|
|
|
|
#error No sector-size detection implemented for this platform
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "sansaio.h"
|
|
|
|
|
2007-09-02 19:20:59 +00:00
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
|
|
|
static int sansa_unmount(struct sansa_t* sansa)
|
|
|
|
{
|
|
|
|
char cmd[4096];
|
|
|
|
int res;
|
|
|
|
|
|
|
|
sprintf(cmd, "/usr/sbin/diskutil unmount \"%ss1\"",sansa->diskname);
|
|
|
|
fprintf(stderr,"[INFO] ");
|
|
|
|
res = system(cmd);
|
|
|
|
|
|
|
|
if (res==0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
perror("Unmount failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-11-08 13:38:10 +00:00
|
|
|
void sansa_print_error(char* msg)
|
2007-03-15 22:55:36 +00:00
|
|
|
{
|
|
|
|
perror(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sansa_open(struct sansa_t* sansa, int silent)
|
|
|
|
{
|
|
|
|
sansa->dh=open(sansa->diskname,O_RDONLY);
|
|
|
|
if (sansa->dh < 0) {
|
|
|
|
if (!silent) perror(sansa->diskname);
|
2008-05-11 16:58:02 +00:00
|
|
|
if(errno == EACCES) return -2;
|
|
|
|
else return -1;
|
2007-03-15 22:55:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(ioctl(sansa->dh,SANSA_SECTORSIZE_IOCTL,&sansa->sector_size) < 0) {
|
|
|
|
sansa->sector_size=512;
|
|
|
|
if (!silent) {
|
|
|
|
fprintf(stderr,"[ERR] ioctl() call to get sector size failed, defaulting to %d\n"
|
|
|
|
,sansa->sector_size);
|
2008-08-31 09:44:45 +00:00
|
|
|
}
|
2007-03-15 22:55:36 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int sansa_reopen_rw(struct sansa_t* sansa)
|
|
|
|
{
|
2007-09-02 19:20:59 +00:00
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
|
|
|
if (sansa_unmount(sansa) < 0)
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
|
2007-03-15 22:55:36 +00:00
|
|
|
close(sansa->dh);
|
|
|
|
sansa->dh=open(sansa->diskname,O_RDWR);
|
|
|
|
if (sansa->dh < 0) {
|
|
|
|
perror(sansa->diskname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sansa_close(struct sansa_t* sansa)
|
|
|
|
{
|
|
|
|
close(sansa->dh);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-23 22:36:00 +00:00
|
|
|
int sansa_alloc_buffer(struct sansa_t *sansa, int bufsize)
|
2007-03-15 22:55:36 +00:00
|
|
|
{
|
2012-12-23 22:36:00 +00:00
|
|
|
sansa->sectorbuf=malloc(bufsize);
|
|
|
|
if (sansa->sectorbuf == NULL) {
|
2007-03-15 22:55:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sansa_seek(struct sansa_t* sansa, loff_t pos)
|
|
|
|
{
|
|
|
|
off_t res;
|
|
|
|
|
|
|
|
res = lseek64(sansa->dh, pos, SEEK_SET);
|
|
|
|
|
|
|
|
if (res == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sansa_read(struct sansa_t* sansa, unsigned char* buf, int nbytes)
|
|
|
|
{
|
|
|
|
return read(sansa->dh, buf, nbytes);
|
|
|
|
}
|
|
|
|
|
2012-12-23 22:36:00 +00:00
|
|
|
int sansa_write(struct sansa_t* sansa, int nbytes)
|
2007-03-15 22:55:36 +00:00
|
|
|
{
|
2012-12-23 22:36:00 +00:00
|
|
|
return write(sansa->dh, sansa->sectorbuf, nbytes);
|
2007-03-15 22:55:36 +00:00
|
|
|
}
|
2011-12-04 19:40:35 +00:00
|
|
|
#endif
|
|
|
|
|