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-10-31 16:09:28 +00:00
|
|
|
#include "panic.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-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
|
|
|
|
DEBUGF("[Reading block 0x%lx, %d]\n", start, count);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ata_write_sectors(unsigned long start, unsigned char count, void* buf)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|