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;
|
|
|
|
|
|
|
|
int ata_read_sectors(unsigned long start, unsigned char count, void* buf)
|
|
|
|
{
|
2002-10-22 15:06:08 +00:00
|
|
|
DEBUGF("[Reading block 0x%lx]\n",start);
|
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-08-08 20:32:09 +00:00
|
|
|
printf("Failed reading %d blocks starting at block 0x%lx\n",count,start);
|
2002-04-26 16:44:58 +00:00
|
|
|
perror("fread");
|
2002-10-22 15:06:08 +00:00
|
|
|
return -2;
|
2002-04-26 16:44:58 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ata_write_sectors(unsigned long start, unsigned char count, void* buf)
|
|
|
|
{
|
2002-10-22 15:06:08 +00:00
|
|
|
DEBUGF("[Writing block 0x%lx]\n",start);
|
2002-10-20 22:50:58 +00:00
|
|
|
|
|
|
|
if (start == 0) {
|
|
|
|
DEBUGF("Holy crap! You're writing on sector 0!\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
perror("fwrite");
|
2002-10-22 15:06:08 +00:00
|
|
|
return -2;
|
2002-04-26 16:44:58 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-08-08 20:32:09 +00:00
|
|
|
int ata_init(char* filename)
|
2002-04-26 16:44:58 +00:00
|
|
|
{
|
2002-10-15 14:36:52 +00:00
|
|
|
if (!filename)
|
|
|
|
filename = "disk.img";
|
2002-04-26 16:44:58 +00:00
|
|
|
/* check disk size */
|
2002-08-08 20:32:09 +00:00
|
|
|
file=fopen(filename,"r+");
|
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);
|
|
|
|
}
|