Move floppy management to libfloppy

This commit is contained in:
Laurent Vivier 2005-11-16 01:29:10 +00:00
parent 5ae61f058f
commit e84481140d
5 changed files with 151 additions and 0 deletions

34
libfloppy/Makefile Normal file
View File

@ -0,0 +1,34 @@
#
# (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
#
TOP=$(shell pwd)
CFLAGS = -nostdlib -nodefaultlibs -Wall -Werror -Wno-multichar -fpic -O2
CPPFLAGS = -I$(TOP)/../libmacos -DARCH_M68K
LIBRARY = libfloppy.a
SOURCES = floppy_lseek.c floppy_read.c floppy_read_sector.c
HEADERS = libfloppy.h
DISTFILES = $(SOURCES) $(HEADERS)
OBJS = $(patsubst %.S,%.o,$(SOURCES:.c=.o))
all: $(LIBRARY)
$(LIBRARY): $(OBJS)
$(AR) rc $@ $^
dist:
for file in $(DISTFILES); do \
dir=$$(dirname $$file); \
if [ "$$dir" != "" ] ; then \
mkdir -p $(DISTDIR)/libfloppy/$$dir; \
fi; \
cp -p $$file $(DISTDIR)/libfloppy/$$file; \
done
clean:
rm -f $(OBJS) $(LIBRARY)

25
libfloppy/floppy_lseek.c Normal file
View File

@ -0,0 +1,25 @@
#include "libfloppy.h"
int floppy_lseek(floppy_FILE *file, off_t offset, int whence)
{
long new_offset;
switch(whence)
{
case SEEK_SET:
new_offset = offset;
break;
case SEEK_CUR:
new_offset = file->offset + offset;
break;
default:
return -1;
}
if (new_offset < 0)
return -1;
file->offset = new_offset;
return new_offset;
}

38
libfloppy/floppy_read.c Normal file
View File

@ -0,0 +1,38 @@
#include <string.h>
#include "libfloppy.h"
size_t floppy_read(floppy_FILE *file, void *ptr, size_t size)
{
int read = 0;
int ret;
while (size != 0)
{
int part;
int cylinder_nb = file->offset / CYLINDER_SIZE;
int cylinder_offset = file->offset % CYLINDER_SIZE;
if (cylinder_nb != file->current_cylinder)
{
ret = floppy_read_sector((cylinder_nb * CYLINDER_SIZE) >> SECTOR_SIZE_BITS,
file->cylinder,
CYLINDER_SIZE);
if (ret == -1)
return read;
file->current_cylinder = cylinder_nb;
}
part = CYLINDER_SIZE - cylinder_offset;
if (part > size)
part = size;
memcpy(ptr, file->cylinder + cylinder_offset, part);
size -= part;
ptr = (char*)ptr + part;
file->offset += part;
read += part;
}
return read;
}

View File

@ -0,0 +1,36 @@
#include <sys/types.h>
#include <string.h>
#include <macos/devices.h>
#include "libfloppy.h"
/* offset is a block number
* size is the number of bytes to read
*/
int floppy_read_sector(off_t offset, void* buffer, size_t size)
{
OSErr err;
ParamBlockRec_t param_block;
/* check size to read is multiple of sector size */
if (size & (SECTOR_SIZE - 1))
return -1;
memset(&param_block, 0, sizeof(param_block));
param_block.ioBuffer = (unsigned long)buffer;
param_block.ioVRefNum = 1;
param_block.ioRefNum = -5;
param_block.ioReqCount = size;
param_block.ioPosMode = fsFromStart;
param_block.ioPosOffset = offset << SECTOR_SIZE_BITS;
err = PBReadSync(&param_block);
if (err != noErr)
return -1;
return 0;
}

18
libfloppy/libfloppy.h Normal file
View File

@ -0,0 +1,18 @@
#include <sys/types.h>
#include <unistd.h>
#define SECTOR_SIZE_BITS 9
#define SECTOR_SIZE (1 << (SECTOR_SIZE_BITS))
#define SECTOR_PER_TRACK 18
#define SIDE_NB 2
#define CYLINDER_SIZE (SIDE_NB*SECTOR_PER_TRACK*SECTOR_SIZE)
typedef struct {
int offset;
int current_cylinder;
unsigned char cylinder[CYLINDER_SIZE];
} floppy_FILE;
extern int floppy_read_sector(off_t offset, void* buffer, size_t size);
extern size_t floppy_read(floppy_FILE *file, void *ptr, size_t size);
extern int floppy_lseek(floppy_FILE *file, off_t offset, int whence);