2008-01-10 15:20:57 +00:00
|
|
|
/*
|
|
|
|
data source for libisoburn.
|
|
|
|
|
2012-03-11 16:20:30 +00:00
|
|
|
Copyright 2007 - 2012 Vreixo Formoso Lopes <metalpain2002@yahoo.es>
|
2008-06-18 16:14:44 +00:00
|
|
|
and Thomas Schmitt <scdbackup@gmx.net>
|
2010-02-14 15:09:05 +00:00
|
|
|
Provided under GPL version 2 or later.
|
2008-01-10 15:20:57 +00:00
|
|
|
*/
|
|
|
|
|
2010-05-16 09:26:41 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "../config.h"
|
|
|
|
#endif
|
|
|
|
|
2008-01-10 15:20:57 +00:00
|
|
|
#include <stdlib.h>
|
2008-02-07 15:50:37 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2008-01-10 15:20:57 +00:00
|
|
|
|
2008-01-20 10:30:43 +00:00
|
|
|
|
|
|
|
#ifndef Xorriso_standalonE
|
|
|
|
|
2008-01-10 15:20:57 +00:00
|
|
|
#include <libburn/libburn.h>
|
|
|
|
|
2008-01-26 00:26:57 +00:00
|
|
|
#include <libisofs/libisofs.h>
|
2008-01-10 15:20:57 +00:00
|
|
|
|
2008-01-20 10:30:43 +00:00
|
|
|
#else /* ! Xorriso_standalonE */
|
|
|
|
|
|
|
|
#include "../libisofs/libisofs.h"
|
|
|
|
#include "../libburn/libburn.h"
|
2008-01-10 15:20:57 +00:00
|
|
|
|
2008-01-20 10:30:43 +00:00
|
|
|
#endif /* Xorriso_standalonE */
|
|
|
|
|
|
|
|
|
|
|
|
#include "isoburn.h"
|
2008-01-10 15:20:57 +00:00
|
|
|
|
2008-02-07 15:50:37 +00:00
|
|
|
|
2012-03-11 16:20:30 +00:00
|
|
|
/* Cached reading of image tree data by multiple tiles */
|
2008-04-01 21:31:25 +00:00
|
|
|
|
2008-05-22 19:27:13 +00:00
|
|
|
|
2008-04-01 21:31:25 +00:00
|
|
|
/* Debugging only: This reports cache loads on stderr.
|
|
|
|
#define Libisoburn_read_cache_reporT 1
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
struct isoburn_cache_tile {
|
2012-03-11 16:20:30 +00:00
|
|
|
char *cache_data;
|
2008-04-01 21:31:25 +00:00
|
|
|
uint32_t cache_lba;
|
|
|
|
uint32_t last_error_lba;
|
|
|
|
uint32_t last_aligned_error_lba;
|
|
|
|
int cache_hits;
|
|
|
|
int age;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct isoburn_cached_drive {
|
|
|
|
struct burn_drive *drive;
|
2012-03-11 16:20:30 +00:00
|
|
|
struct isoburn_cache_tile **tiles;
|
|
|
|
int num_tiles;
|
|
|
|
int tile_blocks;
|
2008-04-01 21:31:25 +00:00
|
|
|
int current_age;
|
2010-11-30 09:39:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Offset to be applied to all block addresses to compensate for an
|
|
|
|
eventual displacement of the block addresses relative to the image
|
|
|
|
start block address that was assumed when the image was created.
|
|
|
|
E.g. if track number 2 gets copied into a disk file and shall then
|
|
|
|
be loaded as ISO filesystem.
|
|
|
|
If displacement_sign is 1 then the displacement number will be
|
|
|
|
added to .read_block() addresses, if -1 it will be subtracted.
|
|
|
|
Else it will be ignored.
|
|
|
|
*/
|
|
|
|
uint32_t displacement;
|
|
|
|
int displacement_sign;
|
|
|
|
|
2008-04-01 21:31:25 +00:00
|
|
|
};
|
|
|
|
|
2008-04-20 11:11:42 +00:00
|
|
|
#define Libisoburn_max_agE 2000000000
|
2008-04-01 21:31:25 +00:00
|
|
|
|
|
|
|
static int ds_inc_age(struct isoburn_cached_drive *icd, int idx, int flag);
|
|
|
|
|
|
|
|
|
|
|
|
int ds_read_block(IsoDataSource *src, uint32_t lba, uint8_t *buffer)
|
|
|
|
{
|
|
|
|
int ret, i, oldest, oldest_age;
|
|
|
|
struct burn_drive *d;
|
|
|
|
off_t count;
|
|
|
|
uint32_t aligned_lba;
|
|
|
|
char msg[80];
|
2012-03-11 16:20:30 +00:00
|
|
|
struct isoburn_cache_tile **tiles;
|
2008-04-01 21:31:25 +00:00
|
|
|
struct isoburn_cached_drive *icd;
|
|
|
|
|
|
|
|
if(src == NULL || buffer == NULL)
|
2015-09-20 12:39:46 +00:00
|
|
|
/* It is not required by the specs of libisofs but implicitly assumed
|
2008-05-01 13:55:09 +00:00
|
|
|
by its current implementation that a data source read result <0 is
|
|
|
|
a valid libisofs error code.
|
|
|
|
*/
|
|
|
|
return ISO_NULL_POINTER;
|
2008-04-01 21:31:25 +00:00
|
|
|
|
|
|
|
icd = (struct isoburn_cached_drive *) src->data;
|
|
|
|
d = (struct burn_drive*) icd->drive;
|
2008-07-05 13:25:06 +00:00
|
|
|
|
|
|
|
if(d == NULL) {
|
|
|
|
/* This would happen if libisoburn saw output data in the fifo and
|
|
|
|
performed early drive release and afterwards libisofs still tries
|
|
|
|
to read data.
|
|
|
|
That would constitute a bad conceptual problem in libisoburn.
|
|
|
|
*/
|
|
|
|
isoburn_msgs_submit(NULL, 0x00060000,
|
|
|
|
"Programming error: Drive released while libisofs still attempts to read",
|
|
|
|
0, "FATAL", 0);
|
|
|
|
return ISO_ASSERT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-03-11 16:20:30 +00:00
|
|
|
tiles = icd->tiles;
|
2008-04-01 21:31:25 +00:00
|
|
|
|
2010-11-30 09:39:44 +00:00
|
|
|
if(icd->displacement_sign == 1) {
|
|
|
|
if(lba + icd->displacement < lba) {
|
|
|
|
address_rollover:;
|
|
|
|
return ISO_DISPLACE_ROLLOVER;
|
|
|
|
} else
|
|
|
|
lba += icd->displacement;
|
|
|
|
} else if(icd->displacement_sign == -1) {
|
|
|
|
if(lba < icd->displacement )
|
|
|
|
goto address_rollover;
|
|
|
|
else
|
|
|
|
lba -= icd->displacement;
|
|
|
|
}
|
|
|
|
|
2012-03-11 16:20:30 +00:00
|
|
|
aligned_lba= lba & ~(icd->tile_blocks - 1);
|
2008-04-01 21:31:25 +00:00
|
|
|
|
2012-03-11 16:20:30 +00:00
|
|
|
for (i = 0; i < icd->num_tiles; i++) {
|
|
|
|
if(aligned_lba == tiles[i]->cache_lba &&
|
|
|
|
tiles[i]->cache_lba != 0xffffffff) {
|
|
|
|
(tiles[i]->cache_hits)++;
|
|
|
|
memcpy(buffer, tiles[i]->cache_data + (lba - aligned_lba) * 2048, 2048);
|
2008-04-01 21:31:25 +00:00
|
|
|
count= 2048;
|
|
|
|
ds_inc_age(icd, i, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find oldest tile */
|
|
|
|
oldest_age= Libisoburn_max_agE;
|
|
|
|
oldest= 0;
|
2012-03-11 16:20:30 +00:00
|
|
|
for(i = 0; i < icd->num_tiles; i++) {
|
|
|
|
if(tiles[i]->cache_lba == 0xffffffff) {
|
2008-04-01 21:31:25 +00:00
|
|
|
oldest= i;
|
|
|
|
break;
|
|
|
|
}
|
2012-03-11 16:20:30 +00:00
|
|
|
if(tiles[i]->age < oldest_age) {
|
|
|
|
oldest_age= tiles[i]->age;
|
2008-04-01 21:31:25 +00:00
|
|
|
oldest= i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-11 16:20:30 +00:00
|
|
|
tiles[oldest]->cache_lba= 0xffffffff; /* invalidate cache */
|
|
|
|
if(tiles[oldest]->last_aligned_error_lba == aligned_lba) {
|
2008-04-01 21:31:25 +00:00
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
ret = burn_read_data(d, (off_t) aligned_lba * (off_t) 2048,
|
2012-03-11 16:20:30 +00:00
|
|
|
(char *) tiles[oldest]->cache_data,
|
|
|
|
icd->tile_blocks * 2048, &count, 2);
|
2008-04-01 21:31:25 +00:00
|
|
|
}
|
|
|
|
if (ret <= 0 ) {
|
2012-03-11 16:20:30 +00:00
|
|
|
tiles[oldest]->last_aligned_error_lba = aligned_lba;
|
2008-04-01 21:31:25 +00:00
|
|
|
|
|
|
|
/* Read-ahead failure ? Try to read 2048 directly. */
|
2012-03-11 16:20:30 +00:00
|
|
|
if(tiles[oldest]->last_error_lba == lba)
|
2008-04-01 21:31:25 +00:00
|
|
|
ret = 0;
|
|
|
|
else
|
|
|
|
ret = burn_read_data(d, (off_t) lba * (off_t) 2048, (char *) buffer,
|
|
|
|
2048, &count, 0);
|
|
|
|
if (ret > 0)
|
|
|
|
return 1;
|
2012-03-11 16:20:30 +00:00
|
|
|
tiles[oldest]->last_error_lba = lba;
|
2010-11-30 09:39:44 +00:00
|
|
|
sprintf(msg, "ds_read_block(%lu) returns %lX",
|
|
|
|
(unsigned long) lba, (unsigned long) ret);
|
2008-06-18 16:14:44 +00:00
|
|
|
isoburn_msgs_submit(NULL, 0x00060000, msg, 0, "DEBUG", 0);
|
2010-11-30 09:39:44 +00:00
|
|
|
return ISO_DATA_SOURCE_MISHAP;
|
2008-04-01 21:31:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef Libisoburn_read_cache_reporT
|
|
|
|
fprintf(stderr, "Tile %2.2d : After %3d hits, new load from %8x , count= %d\n",
|
2012-03-11 16:20:30 +00:00
|
|
|
oldest, tiles[oldest]->cache_hits, aligned_lba, (int) count);
|
2008-04-01 21:31:25 +00:00
|
|
|
#endif
|
|
|
|
|
2012-03-11 16:20:30 +00:00
|
|
|
tiles[oldest]->cache_lba= aligned_lba;
|
|
|
|
tiles[oldest]->cache_hits= 1;
|
2008-04-01 21:31:25 +00:00
|
|
|
ds_inc_age(icd, oldest, 0);
|
|
|
|
|
2012-03-11 16:20:30 +00:00
|
|
|
memcpy(buffer, tiles[oldest]->cache_data + (lba - aligned_lba) * 2048, 2048);
|
2008-04-01 21:31:25 +00:00
|
|
|
count= 2048;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ds_open(IsoDataSource *src)
|
|
|
|
{
|
|
|
|
/* nothing to do, device is always grabbed */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ds_close(IsoDataSource *src)
|
|
|
|
{
|
|
|
|
/* nothing to do, device is always grabbed */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-11 16:20:30 +00:00
|
|
|
|
|
|
|
static int isoburn_cache_tile_destroy(struct isoburn_cache_tile **o,
|
|
|
|
int flag)
|
|
|
|
{
|
|
|
|
if (*o == NULL)
|
|
|
|
return(0);
|
|
|
|
if ((*o)->cache_data != NULL)
|
|
|
|
free((*o)->cache_data);
|
|
|
|
free(*o);
|
|
|
|
*o = NULL;
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int isoburn_cache_tile_new(struct isoburn_cache_tile **o,
|
|
|
|
int tile_blocks, int flag)
|
|
|
|
{
|
|
|
|
struct isoburn_cache_tile *t;
|
|
|
|
|
|
|
|
*o = t = calloc(1, sizeof(struct isoburn_cache_tile));
|
|
|
|
if (t == NULL)
|
|
|
|
goto fail;
|
|
|
|
t->cache_data = NULL;
|
|
|
|
t->cache_lba = 0xffffffff;
|
|
|
|
t->cache_hits = 0;
|
|
|
|
t->last_error_lba = 0xffffffff;
|
|
|
|
t->last_aligned_error_lba = 0xffffffff;
|
|
|
|
t->age= 0;
|
|
|
|
|
|
|
|
t->cache_data = calloc(1, tile_blocks * 2048);
|
|
|
|
if (t->cache_data == NULL)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return(1);
|
|
|
|
fail:;
|
|
|
|
isoburn_cache_tile_destroy(o, 0);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int isoburn_cached_drive_destroy(struct isoburn_cached_drive **o,
|
|
|
|
int flag)
|
|
|
|
{
|
|
|
|
struct isoburn_cached_drive *c;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (*o == NULL)
|
|
|
|
return(0);
|
|
|
|
c= *o;
|
|
|
|
if (c->tiles != NULL) {
|
|
|
|
for (i = 0; i < c->num_tiles; i++)
|
|
|
|
isoburn_cache_tile_destroy(&(c->tiles[i]), 0);
|
|
|
|
free(c->tiles);
|
|
|
|
}
|
|
|
|
free(c);
|
|
|
|
*o= NULL;
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int isoburn_cached_drive_new(struct isoburn_cached_drive **o,
|
|
|
|
struct burn_drive *d, int cache_tiles,
|
|
|
|
int tile_blocks, int flag)
|
|
|
|
{
|
|
|
|
struct isoburn_cached_drive *icd;
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
*o = icd = calloc(1,sizeof(struct isoburn_cached_drive));
|
2012-03-21 19:30:38 +00:00
|
|
|
if (*o == NULL)
|
2012-03-11 16:20:30 +00:00
|
|
|
return(-1);
|
|
|
|
icd->drive = d;
|
|
|
|
icd->tiles = NULL;
|
|
|
|
icd->num_tiles = cache_tiles;
|
|
|
|
icd->tile_blocks = tile_blocks;
|
|
|
|
icd->current_age = 0;
|
|
|
|
icd->displacement = 0;
|
|
|
|
icd->displacement_sign = 0;
|
|
|
|
|
|
|
|
icd->tiles = calloc(1, sizeof(struct isoburn_cache_tile *) * icd->num_tiles);
|
|
|
|
if (icd->tiles == NULL)
|
|
|
|
goto fail;
|
|
|
|
for (i = 0; i < icd->num_tiles; i++) {
|
|
|
|
ret = isoburn_cache_tile_new(&(icd->tiles[i]), icd->tile_blocks, 0);
|
|
|
|
if (ret <= 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
return(1);
|
|
|
|
fail:;
|
|
|
|
isoburn_cached_drive_destroy(o, 0);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-01 21:31:25 +00:00
|
|
|
static void ds_free_data(IsoDataSource *src)
|
|
|
|
{
|
2012-03-11 16:20:30 +00:00
|
|
|
struct isoburn_cached_drive *icd;
|
|
|
|
|
|
|
|
if(src->data != NULL) {
|
|
|
|
icd= (struct isoburn_cached_drive *) src->data;
|
|
|
|
isoburn_cached_drive_destroy(&icd, 0);
|
|
|
|
}
|
2008-04-01 21:31:25 +00:00
|
|
|
src->data= NULL;
|
|
|
|
}
|
|
|
|
|
2008-07-05 13:25:06 +00:00
|
|
|
|
|
|
|
int isoburn_data_source_shutdown(IsoDataSource *src, int flag)
|
|
|
|
{
|
|
|
|
struct isoburn_cached_drive *icd;
|
|
|
|
|
|
|
|
if(src==NULL)
|
|
|
|
return(0);
|
|
|
|
icd= (struct isoburn_cached_drive *) src->data;
|
|
|
|
icd->drive= NULL;
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-30 09:39:44 +00:00
|
|
|
IsoDataSource *isoburn_data_source_new(struct burn_drive *d,
|
2012-03-11 16:20:30 +00:00
|
|
|
uint32_t displacement, int displacement_sign,
|
|
|
|
int cache_tiles, int tile_blocks)
|
2008-04-01 21:31:25 +00:00
|
|
|
{
|
2012-03-11 16:20:30 +00:00
|
|
|
IsoDataSource *src;
|
2008-04-01 21:31:25 +00:00
|
|
|
struct isoburn_cached_drive *icd= NULL;
|
2012-03-11 16:20:30 +00:00
|
|
|
int ret;
|
2008-04-01 21:31:25 +00:00
|
|
|
|
|
|
|
if (d==NULL)
|
|
|
|
return NULL;
|
2012-03-11 16:20:30 +00:00
|
|
|
src = malloc(sizeof(IsoDataSource));
|
|
|
|
if (src == NULL)
|
|
|
|
return NULL;
|
|
|
|
ret = isoburn_cached_drive_new(&icd, d, cache_tiles, tile_blocks, 0);
|
|
|
|
if (ret <= 0) {
|
|
|
|
free(src);
|
2008-04-01 21:31:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-03-11 16:20:30 +00:00
|
|
|
src->version = 0;
|
|
|
|
src->refcount = 1;
|
|
|
|
src->read_block = ds_read_block;
|
|
|
|
src->open = ds_open;
|
|
|
|
src->close = ds_close;
|
|
|
|
src->free_data = ds_free_data;
|
|
|
|
src->data = icd;
|
2010-11-30 09:39:44 +00:00
|
|
|
icd->displacement = displacement;
|
|
|
|
icd->displacement_sign = displacement_sign;
|
2012-03-11 16:20:30 +00:00
|
|
|
return src;
|
2008-04-01 21:31:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ds_inc_age(struct isoburn_cached_drive *icd, int idx, int flag)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
(icd->current_age)++;
|
|
|
|
if(icd->current_age>=Libisoburn_max_agE) { /* reset all ages (allow waste) */
|
2012-03-11 16:20:30 +00:00
|
|
|
for(i = 0; i < icd->num_tiles; i++)
|
|
|
|
(icd->tiles)[i]->age= 0;
|
2008-04-01 21:31:25 +00:00
|
|
|
icd->current_age= 1;
|
|
|
|
}
|
2012-03-11 16:20:30 +00:00
|
|
|
(icd->tiles)[idx]->age= icd->current_age;
|
2008-04-01 21:31:25 +00:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|