preparing hdimage code for the the compilation with a new bximage utility

written in C++ supporting image creation, conversion and redolog commit
This commit is contained in:
Volker Ruppert 2013-10-13 14:33:55 +00:00
parent 9fb7384e6b
commit 6025e38333
11 changed files with 127 additions and 44 deletions

View File

@ -24,8 +24,14 @@
// is used to know when we are exporting symbols and when we are importing.
#define BX_PLUGGABLE
#ifdef BXIMAGE
#include "config.h"
#include "osdep.h"
#include "misc/bxcompat.h"
#else
#include "iodev.h"
#include "cdrom.h"
#endif
#include "hdimage.h"
#include "vmware3.h"
#include "vmware4.h"
@ -42,6 +48,8 @@
#define LOG_THIS theHDImageCtl->
#ifndef BXIMAGE
bx_hdimage_ctl_c* theHDImageCtl = NULL;
int libhdimage_LTX_plugin_init(plugin_t *plugin, plugintype_t type, int argc, char *argv[])
@ -138,6 +146,8 @@ LOWLEVEL_CDROM* bx_hdimage_ctl_c::init_cdrom(const char *dev)
}
#endif
#endif // ifndef BXIMAGE
// helper functions
int bx_read_image(int fd, Bit64s offset, void *buf, int count)
{
@ -244,6 +254,34 @@ int hdimage_detect_image_mode(const char *pathname)
return result;
}
// if return_time==0, this returns the fat_date, else the fat_time
#ifndef WIN32
Bit16u fat_datetime(time_t time, int return_time)
{
struct tm* t;
struct tm t1;
t = &t1;
localtime_r(&time, t);
if (return_time)
return htod16((t->tm_sec/2) | (t->tm_min<<5) | (t->tm_hour<<11));
return htod16((t->tm_mday) | ((t->tm_mon+1)<<5) | ((t->tm_year-80)<<9));
}
#else
Bit16u fat_datetime(FILETIME time, int return_time)
{
FILETIME localtime;
SYSTEMTIME systime;
FileTimeToLocalFileTime(&time, &localtime);
FileTimeToSystemTime(&localtime, &systime);
if (return_time)
return htod16((systime.wSecond/2) | (systime.wMinute<<5) | (systime.wHour<<11));
return htod16((systime.wDay) | (systime.wMonth<<5) | ((systime.wYear-1980)<<9));
}
#endif
#ifndef BXIMAGE
// generic save/restore functions
Bit64s hdimage_save_handler(void *class_ptr, bx_param_c *param)
{
@ -366,6 +404,7 @@ bx_bool hdimage_copy_file(const char *src, const char *dst)
return ret;
#endif
}
#endif
/*** base class device_image_t ***/
@ -389,11 +428,13 @@ Bit32u device_image_t::get_timestamp()
return (fat_datetime(mtime, 1) | (fat_datetime(mtime, 0) << 16));
}
#ifndef BXIMAGE
void device_image_t::register_state(bx_list_c *parent)
{
bx_param_bool_c *image = new bx_param_bool_c(parent, "image", NULL, NULL, 0);
image->set_sr_handlers(this, hdimage_save_handler, hdimage_restore_handler);
}
#endif
/*** default_image_t function definitions ***/
@ -444,6 +485,7 @@ int default_image_t::check_format(int fd, Bit64u imgsize)
}
}
#ifndef BXIMAGE
bx_bool default_image_t::save_state(const char *backup_fname)
{
return hdimage_backup_file(fd, backup_fname);
@ -460,6 +502,7 @@ void default_image_t::restore_state(const char *backup_fname)
BX_PANIC(("Failed to open restored image '%s'", pathname));
}
}
#endif
// helper function for concat and sparse mode images
@ -614,6 +657,7 @@ ssize_t concat_image_t::write(const void* buf, size_t count)
return ::write(fd, (char*) buf, count);
}
#ifndef BXIMAGE
bx_bool concat_image_t::save_state(const char *backup_fname)
{
bx_bool ret = 1;
@ -645,6 +689,7 @@ void concat_image_t::restore_state(const char *backup_fname)
free(image_name);
device_image_t::open(pathname0);
}
#endif
/*** sparse_image_t function definitions ***/
@ -759,7 +804,7 @@ int sparse_image_t::read_header()
int sparse_image_t::open(const char* pathname0, int flags)
{
pathname = strdup(pathname0);
BX_DEBUG(("sparse_image_t.open"));
BX_DEBUG(("sparse_image_t::open"));
if ((fd = hdimage_open_file(pathname, flags, &underlying_filesize, &mtime)) < 0) {
return -1;
@ -848,7 +893,7 @@ Bit64s sparse_image_t::lseek(Bit64s offset, int whence)
if (whence != SEEK_SET)
BX_PANIC(("lseek HD with whence not SEEK_SET"));
BX_DEBUG(("sparse_image_t.lseek(%d)", whence));
BX_DEBUG(("sparse_image_t::lseek(%d)", whence));
if (offset > total_size)
{
@ -1178,6 +1223,7 @@ int sparse_image_t::check_format(int fd, Bit64u imgsize)
return HDIMAGE_FORMAT_OK;
}
#ifndef BXIMAGE
bx_bool sparse_image_t::save_state(const char *backup_fname)
{
return hdimage_backup_file(fd, backup_fname);
@ -1211,6 +1257,7 @@ void sparse_image_t::restore_state(const char *backup_fname)
}
free(temp_pathname);
}
#endif
#ifdef WIN32
@ -1743,10 +1790,12 @@ int redolog_t::check_format(int fd, const char *subtype)
return HDIMAGE_FORMAT_OK;
}
#ifndef BXIMAGE
bx_bool redolog_t::save_state(const char *backup_fname)
{
return hdimage_backup_file(fd, backup_fname);
}
#endif
/*** growing_image_t function definitions ***/
@ -1811,6 +1860,7 @@ int growing_image_t::check_format(int fd, Bit64u imgsize)
return redolog_t::check_format(fd, REDOLOG_SUBTYPE_GROWING);
}
#ifndef BXIMAGE
bx_bool growing_image_t::save_state(const char *backup_fname)
{
return redolog->save_state(backup_fname);
@ -1841,6 +1891,7 @@ void growing_image_t::restore_state(const char *backup_fname)
BX_PANIC(("Failed to open restored growing image '%s'", pathname));
}
}
#endif
// compare hd_size and modification time of r/o disk and journal
@ -1892,7 +1943,7 @@ int undoable_image_t::open(const char* pathname, int flags)
BX_PANIC(("r/o disk image mode not detected"));
return -1;
}
ro_disk = theHDImageCtl->init_image(mode, 0, NULL);
ro_disk = DEV_hdimage_init_image(mode, 0, NULL);
if (ro_disk == NULL) {
return -1;
}
@ -1965,6 +2016,7 @@ ssize_t undoable_image_t::write(const void* buf, size_t count)
return (ret < 0) ? ret : count;
}
#ifndef BXIMAGE
bx_bool undoable_image_t::save_state(const char *backup_fname)
{
return redolog->save_state(backup_fname);
@ -1993,6 +2045,7 @@ void undoable_image_t::restore_state(const char *backup_fname)
}
}
}
#endif
/*** volatile_image_t function definitions ***/
@ -2025,7 +2078,7 @@ int volatile_image_t::open(const char* pathname, int flags)
BX_PANIC(("r/o disk image mode not detected"));
return -1;
}
ro_disk = theHDImageCtl->init_image(mode, 0, NULL);
ro_disk = DEV_hdimage_init_image(mode, 0, NULL);
if (ro_disk == NULL) {
return -1;
}
@ -2117,6 +2170,7 @@ ssize_t volatile_image_t::write(const void* buf, size_t count)
return (ret < 0) ? ret : count;
}
#ifndef BXIMAGE
bx_bool volatile_image_t::save_state(const char *backup_fname)
{
return redolog->save_state(backup_fname);
@ -2150,3 +2204,4 @@ void volatile_image_t::restore_state(const char *backup_fname)
unlink(redolog_temp);
#endif
}
#endif

View File

@ -118,11 +118,15 @@
// htod : convert host to disk (little) endianness
// dtoh : convert disk (little) to host endianness
#if defined (BX_LITTLE_ENDIAN)
#define htod16(val) (val)
#define dtoh16(val) (val)
#define htod32(val) (val)
#define dtoh32(val) (val)
#define htod64(val) (val)
#define dtoh64(val) (val)
#else
#define htod16(val) ((((val)&0xff00)>>8) | (((val)&0xff)<<8))
#define dtoh16(val) htod16(val)
#define htod32(val) bx_bswap32(val)
#define dtoh32(val) htod32(val)
#define htod64(val) bx_bswap64(val)
@ -131,6 +135,9 @@
#ifndef HDIMAGE_HEADERS_ONLY
class device_image_t;
class redolog_t;
int bx_read_image(int fd, Bit64s offset, void *buf, int count);
int bx_write_image(int fd, Bit64s offset, void *buf, int count);
#ifndef WIN32
@ -138,8 +145,15 @@ int hdimage_open_file(const char *pathname, int flags, Bit64u *fsize, time_t *mt
#else
int hdimage_open_file(const char *pathname, int flags, Bit64u *fsize, FILETIME *mtime);
#endif
int hdimage_detect_image_mode(const char *pathname);
bx_bool hdimage_backup_file(int fd, const char *backup_fname);
bx_bool hdimage_copy_file(const char *src, const char *dst);
bx_bool coherency_check(device_image_t *ro_disk, redolog_t *redolog);
#ifndef WIN32
Bit16u fat_datetime(time_t time, int return_time);
#else
Bit16u fat_datetime(FILETIME time, int return_time);
#endif
// base class
class device_image_t
@ -179,10 +193,12 @@ class device_image_t
// Check image format
static int check_format(int fd, Bit64u imgsize) {return HDIMAGE_NO_SIGNATURE;}
#ifndef BXIMAGE
// Save/restore support
virtual void register_state(bx_list_c *parent);
virtual bx_bool save_state(const char *backup_fname) {return 0;}
virtual void restore_state(const char *backup_fname) {}
#endif
unsigned cylinders;
unsigned heads;
@ -221,9 +237,11 @@ class default_image_t : public device_image_t
// Check image format
static int check_format(int fd, Bit64u imgsize);
#ifndef BXIMAGE
// Save/restore support
bx_bool save_state(const char *backup_fname);
void restore_state(const char *backup_fname);
#endif
private:
int fd;
@ -255,9 +273,11 @@ class concat_image_t : public device_image_t
// written (count).
ssize_t write(const void* buf, size_t count);
#ifndef BXIMAGE
// Save/restore support
bx_bool save_state(const char *backup_fname);
void restore_state(const char *backup_fname);
#endif
private:
#define BX_CONCAT_MAX_IMAGES 8
@ -315,9 +335,11 @@ class sparse_image_t : public device_image_t
// Check image format
static int check_format(int fd, Bit64u imgsize);
#ifndef BXIMAGE
// Save/restore support
bx_bool save_state(const char *backup_fname);
void restore_state(const char *backup_fname);
#endif
private:
int fd;
@ -418,7 +440,9 @@ class redolog_t
static int check_format(int fd, const char *subtype);
#ifndef BXIMAGE
bx_bool save_state(const char *backup_fname);
#endif
private:
void print_header();
@ -466,9 +490,11 @@ class growing_image_t : public device_image_t
// Check image format
static int check_format(int fd, Bit64u imgsize);
#ifndef BXIMAGE
// Save/restore support
bx_bool save_state(const char *backup_fname);
void restore_state(const char *backup_fname);
#endif
private:
redolog_t *redolog;
@ -501,9 +527,11 @@ class undoable_image_t : public device_image_t
// written (count).
ssize_t write(const void* buf, size_t count);
#ifndef BXIMAGE
// Save/restore support
bx_bool save_state(const char *backup_fname);
void restore_state(const char *backup_fname);
#endif
private:
redolog_t *redolog; // Redolog instance
@ -538,9 +566,11 @@ class volatile_image_t : public device_image_t
// written (count).
ssize_t write(const void* buf, size_t count);
#ifndef BXIMAGE
// Save/restore support
bx_bool save_state(const char *backup_fname);
void restore_state(const char *backup_fname);
#endif
private:
redolog_t *redolog; // Redolog instance
@ -550,6 +580,7 @@ class volatile_image_t : public device_image_t
};
#ifndef BXIMAGE
class bx_hdimage_ctl_c : public bx_hdimage_ctl_stub_c {
public:
bx_hdimage_ctl_c();
@ -559,6 +590,7 @@ public:
virtual LOWLEVEL_CDROM* init_cdrom(const char *dev);
#endif
};
#endif // BXIMAGE
#endif // HDIMAGE_HEADERS_ONLY

View File

@ -31,7 +31,13 @@
// is used to know when we are exporting symbols and when we are importing.
#define BX_PLUGGABLE
#ifdef BXIMAGE
#include "config.h"
#include "osdep.h"
#include "misc/bxcompat.h"
#else
#include "iodev.h"
#endif
#include "hdimage.h"
#include "vmware3.h"
@ -521,6 +527,7 @@ Bit32u vmware3_image_t::get_capabilities(void)
return HDIMAGE_HAS_GEOMETRY;
}
#ifndef BXIMAGE
bx_bool vmware3_image_t::save_state(const char *backup_fname)
{
bx_bool ret = 1;
@ -571,3 +578,4 @@ void vmware3_image_t::restore_state(const char *backup_fname)
device_image_t::open(pathname);
}
}
#endif

View File

@ -43,8 +43,10 @@ class vmware3_image_t : public device_image_t
Bit32u get_capabilities();
static int check_format(int fd, Bit64u imgsize);
#ifndef BXIMAGE
bx_bool save_state(const char *backup_fname);
void restore_state(const char *backup_fname);
#endif
private:
static const off_t INVALID_OFFSET;

View File

@ -31,7 +31,13 @@
// is used to know when we are exporting symbols and when we are importing.
#define BX_PLUGGABLE
#ifdef BXIMAGE
#include "config.h"
#include "osdep.h"
#include "misc/bxcompat.h"
#else
#include "iodev.h"
#endif
#include "hdimage.h"
#include "vmware4.h"
@ -336,6 +342,7 @@ Bit32u vmware4_image_t::get_capabilities(void)
return HDIMAGE_HAS_GEOMETRY;
}
#ifndef BXIMAGE
bx_bool vmware4_image_t::save_state(const char *backup_fname)
{
return hdimage_backup_file(file_descriptor, backup_fname);
@ -364,3 +371,4 @@ void vmware4_image_t::restore_state(const char *backup_fname)
}
device_image_t::open(pathname);
}
#endif

View File

@ -44,8 +44,10 @@ class vmware4_image_t : public device_image_t
Bit32u get_capabilities();
static int check_format(int fd, Bit64u imgsize);
#ifndef BXIMAGE
bx_bool save_state(const char *backup_fname);
void restore_state(const char *backup_fname);
#endif
private:
static const off_t INVALID_OFFSET;

View File

@ -33,7 +33,14 @@
// is used to know when we are exporting symbols and when we are importing.
#define BX_PLUGGABLE
#ifdef BXIMAGE
#include "config.h"
#include "osdep.h"
#include "misc/bswap.h"
#include "misc/bxcompat.h"
#else
#include "iodev.h"
#endif
#include "hdimage.h"
#include "vpc-img.h"
@ -283,6 +290,7 @@ Bit32u vpc_image_t::get_capabilities(void)
return HDIMAGE_HAS_GEOMETRY;
}
#ifndef BXIMAGE
bx_bool vpc_image_t::save_state(const char *backup_fname)
{
return hdimage_backup_file(fd, backup_fname);
@ -311,6 +319,7 @@ void vpc_image_t::restore_state(const char *backup_fname)
}
device_image_t::open(pathname);
}
#endif
Bit32u vpc_image_t::vpc_checksum(Bit8u *buf, size_t size)
{

View File

@ -147,8 +147,10 @@ class vpc_image_t : public device_image_t
Bit32u get_capabilities();
static int check_format(int fd, Bit64u imgsize);
#ifndef BXIMAGE
bx_bool save_state(const char *backup_fname);
void restore_state(const char *backup_fname);
#endif
private:
Bit32u vpc_checksum(Bit8u *buf, size_t size);

View File

@ -59,13 +59,6 @@
#define VVFAT_BOOT "vvfat_boot.bin"
#define VVFAT_ATTR "vvfat_attr.cfg"
#if defined (BX_LITTLE_ENDIAN)
#define htod16(val) (val)
#else
#define htod16(val) ( (((val)&0xff00)>>8) | (((val)&0xff)<<8) )
#endif
#define dtoh16 htod16
#ifndef F_OK
#define F_OK 0
#endif
@ -491,33 +484,6 @@ static inline Bit8u fat_chksum(const direntry_t* entry)
return chksum;
}
// if return_time==0, this returns the fat_date, else the fat_time
#ifndef WIN32
Bit16u fat_datetime(time_t time, int return_time)
{
struct tm* t;
struct tm t1;
t = &t1;
localtime_r(&time, t);
if (return_time)
return htod16((t->tm_sec/2) | (t->tm_min<<5) | (t->tm_hour<<11));
return htod16((t->tm_mday) | ((t->tm_mon+1)<<5) | ((t->tm_year-80)<<9));
}
#else
Bit16u fat_datetime(FILETIME time, int return_time)
{
FILETIME localtime;
SYSTEMTIME systime;
FileTimeToLocalFileTime(&time, &localtime);
FileTimeToSystemTime(&localtime, &systime);
if (return_time)
return htod16((systime.wSecond/2) | (systime.wMinute<<5) | (systime.wHour<<11));
return htod16((systime.wDay) | (systime.wMonth<<5) | ((systime.wYear-1980)<<9));
}
#endif
void vvfat_image_t::fat_set(unsigned int cluster, Bit32u value)
{
if (fat_type == 32) {

View File

@ -114,12 +114,6 @@ typedef struct mapping_t {
int read_only;
} mapping_t;
#ifndef WIN32
Bit16u fat_datetime(time_t time, int return_time);
#else
Bit16u fat_datetime(FILETIME time, int return_time);
#endif
class vvfat_image_t : public device_image_t
{
public:

View File

@ -22,6 +22,11 @@
#ifndef BX_BSWAP_H
#define BX_BSWAP_H
BX_CPP_INLINE Bit16u bx_bswap16(Bit16u val16)
{
return (val16<<8) | (val16>>8);
}
#if BX_HAVE___BUILTIN_BSWAP32
#define bx_bswap32 __builtin_bswap32
#else