2009-12-19 15:06:57 +01:00
|
|
|
/**
|
|
|
|
* gekko_io.c - Gekko style disk io functions.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009 Rhys "Shareese" Koedijk
|
2010-02-01 00:14:14 +01:00
|
|
|
* Copyright (c) 2010 Dimok
|
2009-12-19 15:06:57 +01:00
|
|
|
*
|
|
|
|
* This program/include file 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/include file 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STDLIB_H
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STDIO_H
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_MATH_H
|
|
|
|
#include <math.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LOCALE_H
|
|
|
|
#include <locale.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "ntfs.h"
|
|
|
|
#include "types.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "device_io.h"
|
|
|
|
#include "gekko_io.h"
|
|
|
|
#include "cache.h"
|
|
|
|
#include "device.h"
|
|
|
|
#include "bootsect.h"
|
|
|
|
|
|
|
|
#define DEV_FD(dev) ((gekko_fd *)dev->d_private)
|
|
|
|
|
|
|
|
/* Prototypes */
|
|
|
|
static s64 ntfs_device_gekko_io_readbytes(struct ntfs_device *dev, s64 offset, s64 count, void *buf);
|
|
|
|
static bool ntfs_device_gekko_io_readsectors(struct ntfs_device *dev, sec_t sector, sec_t numSectors, void* buffer);
|
|
|
|
static s64 ntfs_device_gekko_io_writebytes(struct ntfs_device *dev, s64 offset, s64 count, const void *buf);
|
|
|
|
static bool ntfs_device_gekko_io_writesectors(struct ntfs_device *dev, sec_t sector, sec_t numSectors, const void* buffer);
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-02-09 11:59:55 +01:00
|
|
|
static int ntfs_device_gekko_io_open(struct ntfs_device *dev, int flags)
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
ntfs_log_trace("dev %p, flags %i\n", dev, flags);
|
|
|
|
|
|
|
|
// Get the device driver descriptor
|
|
|
|
gekko_fd *fd = DEV_FD(dev);
|
|
|
|
if (!fd) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device interface
|
|
|
|
const DISC_INTERFACE* interface = fd->interface;
|
|
|
|
if (!interface) {
|
|
|
|
errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Start the device interface and ensure that it is inserted
|
|
|
|
if (!interface->startup()) {
|
|
|
|
ntfs_log_perror("device failed to start\n");
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!interface->isInserted()) {
|
|
|
|
ntfs_log_perror("device media is not inserted\n");
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Check that the device isn't already open (used by another volume?)
|
|
|
|
if (NDevOpen(dev)) {
|
|
|
|
ntfs_log_perror("device is busy (already open)\n");
|
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that there is a valid NTFS boot sector at the start of the device
|
|
|
|
NTFS_BOOT_SECTOR boot;
|
|
|
|
if (interface->readSectors(fd->startSector, 1, &boot)) {
|
|
|
|
if (!ntfs_boot_sector_is_ntfs(&boot)) {
|
|
|
|
errno = EINVALPART;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ntfs_log_perror("read failure @ sector %d\n", fd->startSector);
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Parse the boot sector
|
|
|
|
fd->hiddenSectors = le32_to_cpu(boot.bpb.hidden_sectors);
|
|
|
|
fd->sectorSize = le16_to_cpu(boot.bpb.bytes_per_sector);
|
|
|
|
fd->sectorCount = sle64_to_cpu(boot.number_of_sectors);
|
|
|
|
fd->pos = 0;
|
|
|
|
fd->len = (fd->sectorCount * fd->sectorSize);
|
|
|
|
fd->ino = le64_to_cpu(boot.volume_serial_number);
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// If the device sector size is not 512 bytes then we cannot continue,
|
|
|
|
// gekko disc I/O works on the assumption that sectors are always 512 bytes long.
|
|
|
|
// TODO: Implement support for non-512 byte sector sizes through some fancy maths!?
|
|
|
|
if (fd->sectorSize != BYTES_PER_SECTOR) {
|
|
|
|
ntfs_log_error("Boot sector claims there is %i bytes per sector; expected %i\n", fd->sectorSize, BYTES_PER_SECTOR);
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the device as read-only (if required)
|
|
|
|
if (flags & O_RDONLY) {
|
|
|
|
NDevSetReadOnly(dev);
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Create the cache
|
|
|
|
fd->cache = _NTFS_cache_constructor(fd->cachePageCount, fd->cachePageSize, interface, fd->startSector + fd->sectorCount);
|
|
|
|
|
|
|
|
// Mark the device as open
|
|
|
|
NDevSetBlock(dev);
|
|
|
|
NDevSetOpen(dev);
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-02-09 11:59:55 +01:00
|
|
|
static int ntfs_device_gekko_io_close(struct ntfs_device *dev)
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
ntfs_log_trace("dev %p\n", dev);
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device driver descriptor
|
|
|
|
gekko_fd *fd = DEV_FD(dev);
|
|
|
|
if (!fd) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the device is actually open
|
|
|
|
if (!NDevOpen(dev)) {
|
|
|
|
ntfs_log_perror("device is not open\n");
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Mark the device as closed
|
|
|
|
NDevClearOpen(dev);
|
|
|
|
NDevClearBlock(dev);
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Flush the device (if dirty and not read-only)
|
|
|
|
if (NDevDirty(dev) && !NDevReadOnly(dev)) {
|
|
|
|
ntfs_log_debug("device is dirty, will now sync\n");
|
|
|
|
|
|
|
|
// ...?
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Mark the device as clean
|
|
|
|
NDevClearDirty(dev);
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flush and destroy the cache (if required)
|
|
|
|
if (fd->cache) {
|
|
|
|
_NTFS_cache_flush(fd->cache);
|
|
|
|
_NTFS_cache_destructor(fd->cache);
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Shutdown the device interface
|
|
|
|
/*const DISC_INTERFACE* interface = fd->interface;
|
|
|
|
if (interface) {
|
|
|
|
interface->shutdown();
|
|
|
|
}*/
|
|
|
|
|
|
|
|
// Free the device driver private data
|
|
|
|
ntfs_free(dev->d_private);
|
|
|
|
dev->d_private = NULL;
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-02-09 11:59:55 +01:00
|
|
|
static s64 ntfs_device_gekko_io_seek(struct ntfs_device *dev, s64 offset, int whence)
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
ntfs_log_trace("dev %p, offset %Li, whence %i\n", dev, offset, whence);
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device driver descriptor
|
|
|
|
gekko_fd *fd = DEV_FD(dev);
|
|
|
|
if (!fd) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Set the current position on the device (in bytes)
|
2010-02-09 11:59:55 +01:00
|
|
|
switch(whence) {
|
|
|
|
case SEEK_SET: fd->pos = MIN(MAX(offset, 0), fd->len); break;
|
|
|
|
case SEEK_CUR: fd->pos = MIN(MAX(fd->pos + offset, 0), fd->len); break;
|
|
|
|
case SEEK_END: fd->pos = MIN(MAX(fd->len + offset, 0), fd->len); break;
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-02-09 11:59:55 +01:00
|
|
|
static s64 ntfs_device_gekko_io_read(struct ntfs_device *dev, void *buf, s64 count)
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
return ntfs_device_gekko_io_readbytes(dev, DEV_FD(dev)->pos, count, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-02-09 11:59:55 +01:00
|
|
|
static s64 ntfs_device_gekko_io_write(struct ntfs_device *dev, const void *buf, s64 count)
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
return ntfs_device_gekko_io_writebytes(dev, DEV_FD(dev)->pos, count, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-02-09 11:59:55 +01:00
|
|
|
static s64 ntfs_device_gekko_io_pread(struct ntfs_device *dev, void *buf, s64 count, s64 offset)
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
return ntfs_device_gekko_io_readbytes(dev, offset, count, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-01 00:14:14 +01:00
|
|
|
*
|
2009-12-19 15:06:57 +01:00
|
|
|
*/
|
2010-02-09 11:59:55 +01:00
|
|
|
static s64 ntfs_device_gekko_io_pwrite(struct ntfs_device *dev, const void *buf, s64 count, s64 offset)
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
return ntfs_device_gekko_io_writebytes(dev, offset, count, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-02-09 11:59:55 +01:00
|
|
|
static s64 ntfs_device_gekko_io_readbytes(struct ntfs_device *dev, s64 offset, s64 count, void *buf)
|
|
|
|
{
|
2010-02-01 00:14:14 +01:00
|
|
|
//ntfs_log_trace("dev %p, offset %Li, count %Li\n", dev, offset, count);
|
|
|
|
ntfs_log_trace("dev %p, offset %d, count %d\n", dev, (u32)offset, (u32)count);
|
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device driver descriptor
|
|
|
|
gekko_fd *fd = DEV_FD(dev);
|
|
|
|
if (!fd) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device interface
|
|
|
|
const DISC_INTERFACE* interface = fd->interface;
|
|
|
|
if (!interface) {
|
|
|
|
errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2010-02-09 11:59:55 +01:00
|
|
|
if(!count)
|
2010-02-01 00:14:14 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
sec_t sec_start = (sec_t) fd->startSector;
|
2009-12-19 15:06:57 +01:00
|
|
|
sec_t sec_count = 1;
|
|
|
|
u16 buffer_offset = 0;
|
2010-02-01 00:14:14 +01:00
|
|
|
u8 *buffer = NULL;
|
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Determine the range of sectors required for this read
|
|
|
|
if (offset > 0) {
|
2010-02-01 00:14:14 +01:00
|
|
|
sec_start += (sec_t) floor(offset / fd->sectorSize);
|
|
|
|
buffer_offset = (sec_t) offset % fd->sectorSize;
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
|
|
|
if (count > fd->sectorSize) {
|
2010-02-01 00:14:14 +01:00
|
|
|
sec_count = (sec_t) ceil(count / (float)fd->sectorSize);
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// If this read happens to be on the sector boundaries then do the read straight into the destination buffer
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2010-02-09 11:59:55 +01:00
|
|
|
if((offset % fd->sectorSize == 0) && (count % fd->sectorSize == 0)) {
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Read from the device
|
|
|
|
ntfs_log_trace("direct read from sector %d (%d sector(s) long)\n", sec_start, sec_count);
|
2010-02-01 00:14:14 +01:00
|
|
|
if (!ntfs_device_gekko_io_readsectors(dev, sec_start, sec_count, buf)) {
|
2009-12-19 15:06:57 +01:00
|
|
|
ntfs_log_perror("direct read failure @ sector %d (%d sector(s) long)\n", sec_start, sec_count);
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2010-02-09 11:59:55 +01:00
|
|
|
// Else read into a buffer and copy over only what was requested
|
|
|
|
} else
|
|
|
|
{
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Allocate a buffer to hold the read data
|
|
|
|
buffer = (u8*)ntfs_alloc(sec_count * fd->sectorSize);
|
|
|
|
if (!buffer) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Read from the device
|
|
|
|
ntfs_log_trace("buffered read from sector %d (%d sector(s) long)\n", sec_start, sec_count);
|
2010-02-01 00:14:14 +01:00
|
|
|
ntfs_log_trace("count: %d sec_count:%d fd->sectorSize: %d )\n", (u32)count, (u32)sec_count,(u32)fd->sectorSize);
|
2009-12-19 15:06:57 +01:00
|
|
|
if (!ntfs_device_gekko_io_readsectors(dev, sec_start, sec_count, buffer)) {
|
|
|
|
ntfs_log_perror("buffered read failure @ sector %d (%d sector(s) long)\n", sec_start, sec_count);
|
|
|
|
ntfs_free(buffer);
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Copy what was requested to the destination buffer
|
|
|
|
memcpy(buf, buffer + buffer_offset, count);
|
|
|
|
ntfs_free(buffer);
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-01 00:14:14 +01:00
|
|
|
*
|
2009-12-19 15:06:57 +01:00
|
|
|
*/
|
2010-02-09 11:59:55 +01:00
|
|
|
static s64 ntfs_device_gekko_io_writebytes(struct ntfs_device *dev, s64 offset, s64 count, const void *buf)
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
ntfs_log_trace("dev %p, offset %Li, count %Li\n", dev, offset, count);
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device driver descriptor
|
|
|
|
gekko_fd *fd = DEV_FD(dev);
|
|
|
|
if (!fd) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device interface
|
|
|
|
const DISC_INTERFACE* interface = fd->interface;
|
|
|
|
if (!interface) {
|
|
|
|
errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Check that the device can be written to
|
|
|
|
if (NDevReadOnly(dev)) {
|
|
|
|
errno = EROFS;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2010-02-09 11:59:55 +01:00
|
|
|
if(!count)
|
2010-02-01 00:14:14 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
sec_t sec_start = (sec_t) fd->startSector;
|
2009-12-19 15:06:57 +01:00
|
|
|
sec_t sec_count = 1;
|
2010-02-01 00:14:14 +01:00
|
|
|
u32 buffer_offset = 0;
|
|
|
|
u8 *buffer = NULL;
|
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Determine the range of sectors required for this write
|
|
|
|
if (offset > 0) {
|
2010-02-01 00:14:14 +01:00
|
|
|
sec_start += (sec_t) floor(offset / fd->sectorSize);
|
|
|
|
buffer_offset = (u32) ceil(offset % fd->sectorSize);
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
|
|
|
if (count > fd->sectorSize) {
|
2010-02-01 00:14:14 +01:00
|
|
|
sec_count = (sec_t) ceil((float) count / (float)fd->sectorSize);
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// If this write happens to be on the sector boundaries then do the write straight to disc
|
2010-02-09 11:59:55 +01:00
|
|
|
if((offset % fd->sectorSize == 0) && (count % fd->sectorSize == 0)) {
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Write to the device
|
|
|
|
ntfs_log_trace("direct write to sector %d (%d sector(s) long)\n", sec_start, sec_count);
|
|
|
|
if (!ntfs_device_gekko_io_writesectors(dev, sec_start, sec_count, buf)) {
|
|
|
|
ntfs_log_perror("direct write failure @ sector %d (%d sector(s) long)\n", sec_start, sec_count);
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2010-02-09 11:59:55 +01:00
|
|
|
// Else write from a buffer aligned to the sector boundaries
|
2009-12-19 15:06:57 +01:00
|
|
|
} else {
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Allocate a buffer to hold the write data
|
2010-02-01 00:14:14 +01:00
|
|
|
buffer = (u8*)ntfs_alloc((sec_count+1) * fd->sectorSize);
|
2009-12-19 15:06:57 +01:00
|
|
|
if (!buffer) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Read the first and last sectors of the buffer from disc (if required)
|
2010-02-01 00:14:14 +01:00
|
|
|
// NOTE: This is done because the data does not line up with the sector boundaries,
|
2009-12-19 15:06:57 +01:00
|
|
|
// we just read in the buffer edges where the data overlaps with the rest of the disc
|
2010-02-09 11:59:55 +01:00
|
|
|
if(offset % fd->sectorSize != 0) {
|
2009-12-19 15:06:57 +01:00
|
|
|
if (!ntfs_device_gekko_io_readsectors(dev, sec_start, 1, buffer)) {
|
|
|
|
ntfs_log_perror("read failure @ sector %d\n", sec_start);
|
|
|
|
ntfs_free(buffer);
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2010-02-09 11:59:55 +01:00
|
|
|
if(count % fd->sectorSize != 0) {
|
2010-02-01 00:14:14 +01:00
|
|
|
if (!ntfs_device_gekko_io_readsectors(dev, sec_start + sec_count-1, 1, buffer + ((sec_count - 1) * fd->sectorSize))) {
|
2010-02-09 11:59:55 +01:00
|
|
|
ntfs_log_perror("read failure @ sector %d\n", sec_start + sec_count);
|
|
|
|
ntfs_free(buffer);
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
}
|
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Copy the data into the write buffer
|
|
|
|
memcpy(buffer + buffer_offset, buf, count);
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Write to the device
|
|
|
|
ntfs_log_trace("buffered write to sector %d (%d sector(s) long)\n", sec_start, sec_count);
|
|
|
|
if (!ntfs_device_gekko_io_writesectors(dev, sec_start, sec_count, buffer)) {
|
|
|
|
ntfs_log_perror("buffered write failure @ sector %d\n", sec_start);
|
|
|
|
ntfs_free(buffer);
|
|
|
|
errno = EIO;
|
2010-02-01 00:14:14 +01:00
|
|
|
return -1;
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Free the buffer
|
|
|
|
ntfs_free(buffer);
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Mark the device as dirty (if we actually wrote anything)
|
2010-02-01 00:14:14 +01:00
|
|
|
if (count > 0)
|
2009-12-19 15:06:57 +01:00
|
|
|
NDevSetDirty(dev);
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2010-02-09 11:59:55 +01:00
|
|
|
static bool ntfs_device_gekko_io_readsectors(struct ntfs_device *dev, sec_t sector, sec_t numSectors, void* buffer)
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device driver descriptor
|
|
|
|
gekko_fd *fd = DEV_FD(dev);
|
|
|
|
if (!fd) {
|
|
|
|
errno = EBADF;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Read the sectors from disc (or cache, if enabled)
|
|
|
|
if (fd->cache)
|
|
|
|
return _NTFS_cache_readSectors(fd->cache, sector, numSectors, buffer);
|
|
|
|
else
|
2010-02-01 00:14:14 +01:00
|
|
|
return fd->interface->readSectors(sector, numSectors, buffer);
|
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-09 11:59:55 +01:00
|
|
|
static bool ntfs_device_gekko_io_writesectors(struct ntfs_device *dev, sec_t sector, sec_t numSectors, const void* buffer)
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device driver descriptor
|
|
|
|
gekko_fd *fd = DEV_FD(dev);
|
|
|
|
if (!fd) {
|
|
|
|
errno = EBADF;
|
2010-02-01 00:14:14 +01:00
|
|
|
return false;
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Write the sectors to disc (or cache, if enabled)
|
|
|
|
if (fd->cache)
|
|
|
|
return _NTFS_cache_writeSectors(fd->cache, sector, numSectors, buffer);
|
|
|
|
else
|
|
|
|
return fd->interface->writeSectors(sector, numSectors, buffer);
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-02-09 11:59:55 +01:00
|
|
|
static int ntfs_device_gekko_io_sync(struct ntfs_device *dev)
|
|
|
|
{
|
|
|
|
gekko_fd *fd = DEV_FD(dev);
|
2009-12-19 15:06:57 +01:00
|
|
|
ntfs_log_trace("dev %p\n", dev);
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Check that the device can be written to
|
|
|
|
if (NDevReadOnly(dev)) {
|
|
|
|
errno = EROFS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the device as clean
|
|
|
|
NDevClearDirty(dev);
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Flush any sectors in the disc cache (if required)
|
|
|
|
if (fd->cache) {
|
|
|
|
if (!_NTFS_cache_flush(fd->cache)) {
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-02-09 11:59:55 +01:00
|
|
|
static int ntfs_device_gekko_io_stat(struct ntfs_device *dev, struct stat *buf)
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
ntfs_log_trace("dev %p, buf %p\n", dev, buf);
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device driver descriptor
|
|
|
|
gekko_fd *fd = DEV_FD(dev);
|
|
|
|
if (!fd) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Short circuit cases were we don't actually have to do anything
|
|
|
|
if (!buf)
|
|
|
|
return 0;
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Build the device mode
|
|
|
|
mode_t mode = (S_IFBLK) |
|
|
|
|
(S_IRUSR | S_IRGRP | S_IROTH) |
|
|
|
|
((!NDevReadOnly(dev)) ? (S_IWUSR | S_IWGRP | S_IWOTH) : 0);
|
|
|
|
|
|
|
|
// Zero out the stat buffer
|
|
|
|
memset(buf, 0, sizeof(struct stat));
|
|
|
|
|
|
|
|
// Build the device stats
|
|
|
|
buf->st_dev = fd->interface->ioType;
|
|
|
|
buf->st_ino = fd->ino;
|
|
|
|
buf->st_mode = mode;
|
|
|
|
buf->st_rdev = fd->interface->ioType;
|
|
|
|
buf->st_blksize = fd->sectorSize;
|
|
|
|
buf->st_blocks = fd->sectorCount;
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-02-09 11:59:55 +01:00
|
|
|
static int ntfs_device_gekko_io_ioctl(struct ntfs_device *dev, int request, void *argp)
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
ntfs_log_trace("dev %p, request %i, argp %p\n", dev, request, argp);
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device driver descriptor
|
|
|
|
gekko_fd *fd = DEV_FD(dev);
|
|
|
|
if (!fd) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Figure out which i/o control was requested
|
|
|
|
switch (request) {
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get block device size (sectors)
|
2010-02-09 11:59:55 +01:00
|
|
|
#if defined(BLKGETSIZE)
|
|
|
|
case BLKGETSIZE: {
|
2009-12-19 15:06:57 +01:00
|
|
|
*(u32*)argp = fd->sectorCount;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-09 11:59:55 +01:00
|
|
|
#endif
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get block device size (bytes)
|
2010-02-09 11:59:55 +01:00
|
|
|
#if defined(BLKGETSIZE64)
|
|
|
|
case BLKGETSIZE64: {
|
2009-12-19 15:06:57 +01:00
|
|
|
*(u64*)argp = (fd->sectorCount * fd->sectorSize);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-09 11:59:55 +01:00
|
|
|
#endif
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get hard drive geometry
|
2010-02-09 11:59:55 +01:00
|
|
|
#if defined(HDIO_GETGEO)
|
|
|
|
case HDIO_GETGEO: {
|
2009-12-19 15:06:57 +01:00
|
|
|
struct hd_geometry *geo = (struct hd_geometry*)argp;
|
|
|
|
geo->sectors = 0;
|
|
|
|
geo->heads = 0;
|
|
|
|
geo->cylinders = 0;
|
|
|
|
geo->start = fd->hiddenSectors;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-09 11:59:55 +01:00
|
|
|
#endif
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get block device sector size (bytes)
|
2010-02-09 11:59:55 +01:00
|
|
|
#if defined(BLKSSZGET)
|
|
|
|
case BLKSSZGET: {
|
2009-12-19 15:06:57 +01:00
|
|
|
*(int*)argp = fd->sectorSize;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-09 11:59:55 +01:00
|
|
|
#endif
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Set block device block size (bytes)
|
2010-02-09 11:59:55 +01:00
|
|
|
#if defined(BLKBSZSET)
|
|
|
|
case BLKBSZSET: {
|
2009-12-19 15:06:57 +01:00
|
|
|
int sectorSize = *(int*)argp;
|
|
|
|
if (sectorSize != BYTES_PER_SECTOR) {
|
|
|
|
ntfs_log_perror("Attempt to set sector size to an unsupported value (%i), ignored\n", sectorSize);
|
|
|
|
errno = EOPNOTSUPP;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fd->sectorSize = sectorSize;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-09 11:59:55 +01:00
|
|
|
#endif
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Unimplemented ioctrl
|
2010-02-09 11:59:55 +01:00
|
|
|
default: {
|
2010-02-01 00:14:14 +01:00
|
|
|
ntfs_log_perror("Unimplemented ioctrl %i\n", request);
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EOPNOTSUPP;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Device operations for working with gekko style devices and files.
|
|
|
|
*/
|
|
|
|
struct ntfs_device_operations ntfs_device_gekko_io_ops = {
|
|
|
|
.open = ntfs_device_gekko_io_open,
|
|
|
|
.close = ntfs_device_gekko_io_close,
|
|
|
|
.seek = ntfs_device_gekko_io_seek,
|
|
|
|
.read = ntfs_device_gekko_io_read,
|
|
|
|
.write = ntfs_device_gekko_io_write,
|
|
|
|
.pread = ntfs_device_gekko_io_pread,
|
|
|
|
.pwrite = ntfs_device_gekko_io_pwrite,
|
|
|
|
.sync = ntfs_device_gekko_io_sync,
|
|
|
|
.stat = ntfs_device_gekko_io_stat,
|
|
|
|
.ioctl = ntfs_device_gekko_io_ioctl,
|
|
|
|
};
|