2005-11-13 19:43:28 +00:00
|
|
|
/*
|
|
|
|
*
|
2006-09-15 14:55:39 +00:00
|
|
|
* (c) 2005 Laurent Vivier <Laurent@lvivier.info>
|
2005-11-13 19:43:28 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-11-12 22:28:50 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2007-10-09 19:16:11 +00:00
|
|
|
#include <sys/stat.h>
|
2005-11-12 22:28:50 +00:00
|
|
|
#include <stdio.h>
|
2007-10-09 19:16:11 +00:00
|
|
|
#include <fcntl.h>
|
2005-11-12 22:28:50 +00:00
|
|
|
|
2005-11-21 22:09:16 +00:00
|
|
|
#include "device.h"
|
|
|
|
|
2007-10-09 19:16:11 +00:00
|
|
|
int device_sector_size = 2048;
|
|
|
|
|
|
|
|
#define BLOCKS(X) (((X)/device_sector_size)+(((X)%device_sector_size)?1:0))
|
2005-11-12 22:28:50 +00:00
|
|
|
|
2007-10-10 16:46:03 +00:00
|
|
|
int device_get_blocksize(void *data)
|
|
|
|
{
|
|
|
|
return device_sector_size;
|
|
|
|
}
|
|
|
|
|
2005-11-21 22:09:16 +00:00
|
|
|
int device_read_sector(void *data,off_t offset, void* buffer, size_t size)
|
2005-11-12 22:28:50 +00:00
|
|
|
{
|
2007-10-09 19:16:11 +00:00
|
|
|
int fd = (int)data;
|
|
|
|
int ret;
|
|
|
|
|
2007-10-10 16:46:03 +00:00
|
|
|
lseek(fd, offset * device_sector_size, SEEK_SET);
|
|
|
|
ret = read(fd, buffer, BLOCKS(size) * device_sector_size);
|
|
|
|
if (ret != BLOCKS(size) * device_sector_size)
|
2007-10-09 19:16:11 +00:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2005-11-21 22:09:16 +00:00
|
|
|
|
2007-10-09 19:16:11 +00:00
|
|
|
int device_write_sector(void *data,off_t offset, void* buffer, size_t size)
|
|
|
|
{
|
|
|
|
int fd = (int)data;
|
|
|
|
int ret;
|
|
|
|
|
2007-10-10 16:46:03 +00:00
|
|
|
lseek(fd, offset * device_sector_size, SEEK_SET);
|
|
|
|
ret = write(fd, buffer, BLOCKS(size) * device_sector_size);
|
|
|
|
if (ret != BLOCKS(size) * device_sector_size)
|
2007-10-09 19:16:11 +00:00
|
|
|
return -1;
|
|
|
|
return 0;
|
2005-11-12 22:28:50 +00:00
|
|
|
}
|
|
|
|
|
2005-11-21 22:09:16 +00:00
|
|
|
void device_close(void *data)
|
2005-11-12 22:28:50 +00:00
|
|
|
{
|
2007-10-09 19:16:11 +00:00
|
|
|
close((int)data);
|
2005-11-12 22:28:50 +00:00
|
|
|
}
|
|
|
|
|
2007-10-09 19:16:11 +00:00
|
|
|
int device_open(char *device, int flags)
|
2005-11-12 22:28:50 +00:00
|
|
|
{
|
2007-10-09 19:16:11 +00:00
|
|
|
int fd;
|
2005-11-21 22:09:16 +00:00
|
|
|
|
2006-09-08 22:57:36 +00:00
|
|
|
if (device == NULL)
|
2007-10-09 19:16:11 +00:00
|
|
|
return -1;
|
2006-09-08 22:57:36 +00:00
|
|
|
|
2007-10-09 19:16:11 +00:00
|
|
|
fd = open(device, flags);
|
2005-11-21 22:09:16 +00:00
|
|
|
|
2007-10-09 19:16:11 +00:00
|
|
|
return fd;
|
2005-11-12 22:28:50 +00:00
|
|
|
}
|