EMILE/second/load.c

156 lines
2.9 KiB
C
Raw Normal View History

2004-05-26 21:59:06 +00:00
/*
*
* (c) 2004 Laurent Vivier <LaurentVivier@wanadoo.fr>
*
*/
#include <stdio.h>
#include <malloc.h>
2005-05-22 21:14:41 +00:00
#include <string.h>
2004-07-12 21:32:44 +00:00
#include "bank.h"
#include "misc.h"
2004-05-26 21:59:06 +00:00
#include "glue.h"
2004-12-03 00:19:02 +00:00
#include "head.h"
2004-05-26 21:59:06 +00:00
#include "load.h"
#include "uncompress.h"
2004-05-26 21:59:06 +00:00
2004-12-03 00:19:02 +00:00
#ifdef SCSI_SUPPORT
#include "scsi.h"
static int load_container(struct emile_container* container, char* image)
2004-05-26 21:59:06 +00:00
{
2004-12-03 00:19:02 +00:00
int target;
int i;
2004-05-26 21:59:06 +00:00
int err;
2004-12-03 00:19:02 +00:00
target = container->unit_id;
2004-05-26 21:59:06 +00:00
2004-12-03 00:19:02 +00:00
i = 0;
while (container->blocks[i].count != 0)
2004-05-26 21:59:06 +00:00
{
2004-12-03 00:19:02 +00:00
err = scsi_READ(target, container->blocks[i].offset,
container->blocks[i].count,
image,
container->block_size * container->blocks[i].count);
if (err != noErr)
return -1;
2004-12-03 00:19:02 +00:00
image += container->block_size * container->blocks[i].count;
i++;
2004-05-26 21:59:06 +00:00
}
return 0;
2004-12-03 00:19:02 +00:00
}
#else /* SCSI_SUPPORT */
static int load_blocks(unsigned long offset, unsigned long size, char *image)
2004-12-03 00:19:02 +00:00
{
int err;
ParamBlockRec_t param_block;
2004-06-07 18:51:51 +00:00
2004-05-26 21:59:06 +00:00
memset(&param_block, 0, sizeof(param_block));
param_block.ioBuffer = (unsigned long)image;
2004-12-03 00:19:02 +00:00
param_block.ioVRefNum = 1;
2004-05-26 21:59:06 +00:00
param_block.ioRefNum = -5;
param_block.ioReqCount = size;
param_block.ioPosMode = fsFromStart;
param_block.ioPosOffset = offset;
err = PBReadSync(&param_block);
if (err != noErr)
return -1;
2004-05-26 21:59:06 +00:00
return 0;
2004-05-26 21:59:06 +00:00
}
2004-12-03 00:19:02 +00:00
#endif /* SCSI_SUPPORT */
int load_image(unsigned long offset, unsigned long size, char *image)
2004-12-03 00:19:02 +00:00
{
if (size == 0)
return -1;
2004-12-03 00:19:02 +00:00
if (image == NULL)
return -1;
2004-12-03 00:19:02 +00:00
#ifdef SCSI_SUPPORT
return load_container((struct emile_container*)offset, image);
#else
return load_blocks(offset, size, image);
#endif
}
static unsigned char* gzip_image;
#ifdef SCSI_SUPPORT
unsigned char load_get_byte(unsigned long inptr)
{
return gzip_image[inptr];
}
#else
#define SECTOR_SIZE 512
#define SECTOR_PER_TRACK 18
#define SIDE_NB 2
#define CYLINDER_SIZE (SIDE_NB*SECTOR_PER_TRACK*SECTOR_SIZE)
static unsigned long buffer_size;
static unsigned long remaining_size;
static unsigned long buffer_offset;
static unsigned long disk_offset;
#define MIN(a,b) ((a) < (b) ? (a) : (b))
unsigned char load_get_byte(unsigned long inptr)
{
if (buffer_offset == buffer_size)
{
unsigned to_read = MIN(buffer_size, remaining_size);
load_image(disk_offset, to_read, gzip_image);
buffer_offset = 0;
remaining_size -= to_read;
disk_offset += to_read;
}
return gzip_image[buffer_offset++];
}
#endif
int load_gzip(unsigned long offset, unsigned long size, char *image)
{
#ifdef SCSI_SUPPORT
int ret;
/* allocate memory for image */
gzip_image = (char*)malloc(size);
if (gzip_image == NULL)
return -1;
/* load image */
ret = load_image(offset, size, gzip_image);
if (ret == -1)
return -1;
#else
disk_offset = offset;
buffer_size = size;
remaining_size = size;
buffer_size = CYLINDER_SIZE;
buffer_offset = buffer_size;
gzip_image = (char*)malloc(buffer_size);
if (gzip_image == NULL)
return -1;
#endif
/* uncompress */
uncompress(image, load_get_byte);
printf("\n");
/* free kernel image */
free(gzip_image);
return 0;
}