2002-04-26 16:44:58 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2002-10-15 14:36:52 +00:00
|
|
|
#include "debug.h"
|
2002-04-26 16:44:58 +00:00
|
|
|
|
|
|
|
#define BLOCK_SIZE 512
|
|
|
|
|
|
|
|
static FILE* file;
|
|
|
|
|
2005-04-18 14:16:35 +00:00
|
|
|
void panicf( const char *fmt, ... );
|
|
|
|
|
2002-12-03 14:00:33 +00:00
|
|
|
int ata_read_sectors(unsigned long start, int count, void* buf)
|
2002-04-26 16:44:58 +00:00
|
|
|
{
|
2002-10-31 20:40:15 +00:00
|
|
|
if ( count > 1 )
|
|
|
|
DEBUGF("[Reading %d blocks: 0x%lx to 0x%lx]\n",
|
|
|
|
count, start, start+count-1);
|
|
|
|
else
|
2002-11-01 15:26:06 +00:00
|
|
|
DEBUGF("[Reading block 0x%lx]\n", start);
|
2002-10-31 16:09:28 +00:00
|
|
|
|
2002-04-26 16:44:58 +00:00
|
|
|
if(fseek(file,start*BLOCK_SIZE,SEEK_SET)) {
|
|
|
|
perror("fseek");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(!fread(buf,BLOCK_SIZE,count,file)) {
|
2002-10-31 19:06:14 +00:00
|
|
|
DEBUGF("ata_write_sectors(0x%x, 0x%x, 0x%x)\n", start, count, buf );
|
2002-04-26 16:44:58 +00:00
|
|
|
perror("fread");
|
2002-10-31 16:09:28 +00:00
|
|
|
panicf("Disk error\n");
|
2002-04-26 16:44:58 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-12-03 14:00:33 +00:00
|
|
|
int ata_write_sectors(unsigned long start, int count, void* buf)
|
2002-04-26 16:44:58 +00:00
|
|
|
{
|
2002-10-31 20:40:15 +00:00
|
|
|
if ( count > 1 )
|
|
|
|
DEBUGF("[Writing %d blocks: 0x%lx to 0x%lx]\n",
|
|
|
|
count, start, start+count-1);
|
|
|
|
else
|
|
|
|
DEBUGF("[Writing block 0x%lx]\n", start);
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2002-10-31 16:09:28 +00:00
|
|
|
if (start == 0)
|
|
|
|
panicf("Writing on sector 0!\n");
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2002-04-26 16:44:58 +00:00
|
|
|
if(fseek(file,start*BLOCK_SIZE,SEEK_SET)) {
|
|
|
|
perror("fseek");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(!fwrite(buf,BLOCK_SIZE,count,file)) {
|
2002-10-31 19:06:14 +00:00
|
|
|
DEBUGF("ata_write_sectors(0x%x, 0x%x, 0x%x)\n", start, count, buf );
|
2002-04-26 16:44:58 +00:00
|
|
|
perror("fwrite");
|
2002-10-31 16:09:28 +00:00
|
|
|
panicf("Disk error\n");
|
2002-04-26 16:44:58 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-18 14:16:35 +00:00
|
|
|
int ata_init(void)
|
2002-04-26 16:44:58 +00:00
|
|
|
{
|
2005-04-18 14:16:35 +00:00
|
|
|
char* filename = "disk.img";
|
2002-04-26 16:44:58 +00:00
|
|
|
/* check disk size */
|
2004-10-01 19:19:09 +00:00
|
|
|
file=fopen(filename,"rb+");
|
2002-04-26 16:44:58 +00:00
|
|
|
if(!file) {
|
2002-10-15 14:36:52 +00:00
|
|
|
fprintf(stderr, "read_disk() - Could not find \"%s\"\n",filename);
|
2002-04-26 16:44:58 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ata_exit(void)
|
|
|
|
{
|
|
|
|
fclose(file);
|
|
|
|
}
|