2006-04-27 19:11:45 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2011-02-25 01:05:47 +03:00
|
|
|
// $Id$
|
2006-04-27 19:11:45 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2003-05-03 20:37:18 +04:00
|
|
|
/*
|
|
|
|
* This file provides support for VMWare's virtual disk image
|
2006-12-17 11:17:28 +03:00
|
|
|
* format version 3.
|
2003-05-03 20:37:18 +04:00
|
|
|
*
|
|
|
|
* Author: Sharvil Nanavati, for Net Integration Technologies, Inc.
|
|
|
|
* Contact: snrrrub@yahoo.com
|
2008-01-27 01:24:03 +03:00
|
|
|
*
|
2003-05-03 20:37:18 +04:00
|
|
|
* Copyright (C) 2003 Net Integration Technologies, Inc.
|
2008-01-27 01:24:03 +03:00
|
|
|
*
|
2003-05-03 20:37:18 +04:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2008-01-27 01:24:03 +03:00
|
|
|
*
|
2003-05-03 20:37:18 +04:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2008-01-27 01:24:03 +03:00
|
|
|
*
|
2003-05-03 20:37:18 +04:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2009-02-08 12:05:52 +03:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-05-03 20:37:18 +04:00
|
|
|
*/
|
|
|
|
|
2003-05-25 12:14:04 +04:00
|
|
|
// Define BX_PLUGGABLE in files that can be compiled into plugins. For
|
2008-01-27 01:24:03 +03:00
|
|
|
// platforms that require a special tag on exported symbols, BX_PLUGGABLE
|
2003-05-25 12:14:04 +04:00
|
|
|
// is used to know when we are exporting symbols and when we are importing.
|
|
|
|
#define BX_PLUGGABLE
|
|
|
|
|
2013-10-13 18:33:55 +04:00
|
|
|
#ifdef BXIMAGE
|
|
|
|
#include "config.h"
|
|
|
|
#include "misc/bxcompat.h"
|
2013-10-14 00:30:51 +04:00
|
|
|
#include "osdep.h"
|
2013-10-13 18:33:55 +04:00
|
|
|
#else
|
2004-06-19 19:20:15 +04:00
|
|
|
#include "iodev.h"
|
2013-10-13 18:33:55 +04:00
|
|
|
#endif
|
2005-11-06 14:07:01 +03:00
|
|
|
#include "hdimage.h"
|
2004-09-05 14:30:19 +04:00
|
|
|
#include "vmware3.h"
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2003-08-03 21:04:02 +04:00
|
|
|
const off_t vmware3_image_t::INVALID_OFFSET=(off_t)-1;
|
2011-01-14 19:43:55 +03:00
|
|
|
|
|
|
|
#define LOG_THIS bx_devices.pluginHDImageCtl->
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2004-01-18 19:39:02 +03:00
|
|
|
#define DTOH32_HEADER(field) (header.field = (dtoh32(header.field)))
|
|
|
|
#define HTOD32_HEADER(field) (header.field = (htod32(header.field)))
|
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
int vmware3_image_t::check_format(int fd, Bit64u imgsize)
|
2004-01-18 19:39:02 +03:00
|
|
|
{
|
2012-10-07 15:38:55 +04:00
|
|
|
COW_Header header;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = bx_read_image(fd, 0, &header, sizeof(COW_Header))) < 0) {
|
|
|
|
return HDIMAGE_READ_ERROR;
|
|
|
|
}
|
|
|
|
if (header.id[0] != 'C' || header.id[1] != 'O' || header.id[2] != 'W' || header.id[3] != 'D') {
|
|
|
|
return HDIMAGE_NO_SIGNATURE;
|
|
|
|
}
|
|
|
|
DTOH32_HEADER(header_version);
|
|
|
|
DTOH32_HEADER(vmware_version);
|
|
|
|
if (header.header_version != 3) {
|
|
|
|
return HDIMAGE_VERSION_ERROR;
|
|
|
|
}
|
|
|
|
if (header.vmware_version != 2) {
|
|
|
|
return HDIMAGE_VERSION_ERROR;
|
|
|
|
}
|
|
|
|
return HDIMAGE_FORMAT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bx_bool vmware3_image_t::read_header(int fd, COW_Header & header)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = check_format(fd, 0)) != HDIMAGE_FORMAT_OK) {
|
|
|
|
switch (ret) {
|
|
|
|
case HDIMAGE_READ_ERROR:
|
|
|
|
BX_ERROR(("vmware3 image read error"));
|
|
|
|
break;
|
|
|
|
case HDIMAGE_NO_SIGNATURE:
|
|
|
|
BX_ERROR(("not a vmware3 COW disk"));
|
|
|
|
break;
|
|
|
|
case HDIMAGE_VERSION_ERROR:
|
|
|
|
BX_ERROR(("unsupported vmware3 image version"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bx_read_image(fd, 0, &header, sizeof(COW_Header)) != sizeof(COW_Header))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
DTOH32_HEADER(header_version);
|
|
|
|
DTOH32_HEADER(flags);
|
|
|
|
DTOH32_HEADER(total_sectors);
|
|
|
|
DTOH32_HEADER(tlb_size_sectors);
|
|
|
|
DTOH32_HEADER(flb_offset_sectors);
|
|
|
|
DTOH32_HEADER(flb_count);
|
|
|
|
DTOH32_HEADER(next_sector_to_allocate);
|
|
|
|
DTOH32_HEADER(cylinders);
|
|
|
|
DTOH32_HEADER(heads);
|
|
|
|
DTOH32_HEADER(sectors);
|
|
|
|
DTOH32_HEADER(last_modified_time);
|
|
|
|
DTOH32_HEADER(last_modified_time_save);
|
|
|
|
DTOH32_HEADER(chain_id);
|
|
|
|
DTOH32_HEADER(number_of_chains);
|
|
|
|
DTOH32_HEADER(cylinders_in_disk);
|
|
|
|
DTOH32_HEADER(heads_in_disk);
|
|
|
|
DTOH32_HEADER(sectors_in_disk);
|
|
|
|
DTOH32_HEADER(total_sectors_in_disk);
|
|
|
|
DTOH32_HEADER(vmware_version);
|
|
|
|
|
|
|
|
return 1;
|
2004-01-18 19:39:02 +03:00
|
|
|
}
|
|
|
|
|
2008-01-27 01:24:03 +03:00
|
|
|
int vmware3_image_t::write_header(int fd, COW_Header & hostHeader)
|
2004-01-18 19:39:02 +03:00
|
|
|
{
|
2012-10-07 15:38:55 +04:00
|
|
|
COW_Header header;
|
|
|
|
|
|
|
|
memcpy(&header, &hostHeader, sizeof(COW_Header));
|
|
|
|
|
|
|
|
HTOD32_HEADER(header_version);
|
|
|
|
HTOD32_HEADER(flags);
|
|
|
|
HTOD32_HEADER(total_sectors);
|
|
|
|
HTOD32_HEADER(tlb_size_sectors);
|
|
|
|
HTOD32_HEADER(flb_offset_sectors);
|
|
|
|
HTOD32_HEADER(flb_count);
|
|
|
|
HTOD32_HEADER(next_sector_to_allocate);
|
|
|
|
HTOD32_HEADER(cylinders);
|
|
|
|
HTOD32_HEADER(heads);
|
|
|
|
HTOD32_HEADER(sectors);
|
|
|
|
HTOD32_HEADER(last_modified_time);
|
|
|
|
HTOD32_HEADER(last_modified_time_save);
|
|
|
|
HTOD32_HEADER(chain_id);
|
|
|
|
HTOD32_HEADER(number_of_chains);
|
|
|
|
HTOD32_HEADER(cylinders_in_disk);
|
|
|
|
HTOD32_HEADER(heads_in_disk);
|
|
|
|
HTOD32_HEADER(sectors_in_disk);
|
|
|
|
HTOD32_HEADER(total_sectors_in_disk);
|
|
|
|
HTOD32_HEADER(vmware_version);
|
|
|
|
|
|
|
|
return bx_write_image(fd, 0, &header, sizeof(COW_Header));
|
2004-01-18 19:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef DTOH32_HEADER
|
|
|
|
#undef HTOD32_HEADER
|
|
|
|
|
|
|
|
int vmware3_image_t::read_ints(int fd, Bit32u *buffer, size_t count)
|
|
|
|
{
|
2012-10-07 15:38:55 +04:00
|
|
|
size_t i;
|
|
|
|
Bit32u *p;
|
2004-01-18 19:39:02 +03:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
int res = ::read(fd, (void*)buffer, count * 4);
|
|
|
|
for (p = buffer, i=0; i<count; p++, i++)
|
|
|
|
*p=dtoh32(*p);
|
2004-01-18 19:39:02 +03:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
return res;
|
2004-01-18 19:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int vmware3_image_t::write_ints(int fd, Bit32u *buffer, size_t count)
|
|
|
|
{
|
2012-10-07 15:38:55 +04:00
|
|
|
size_t i;
|
|
|
|
Bit32u *p;
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
for (p = buffer, i=0; i<count; p++, i++)
|
|
|
|
*p=htod32(*p);
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
int res = ::write(fd, (void*)buffer, count * 4);
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
for (p = buffer, i=0; i<count; p++, i++)
|
|
|
|
*p=dtoh32(*p);
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
return res;
|
2003-05-03 20:37:18 +04:00
|
|
|
}
|
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
char* vmware3_image_t::generate_cow_name(const char * filename, unsigned chain)
|
2003-05-03 20:37:18 +04:00
|
|
|
{
|
2012-10-07 15:38:55 +04:00
|
|
|
char * name = new char[strlen(filename) + 4];
|
|
|
|
if(name == NULL)
|
|
|
|
BX_PANIC(("unable to allocate %u bytes for vmware3 COW file name (base: %s, chain: %u)", (unsigned)strlen(filename) + 4, filename, chain));
|
|
|
|
strcpy(name, filename);
|
|
|
|
if (chain != 0) {
|
|
|
|
char * period = strrchr(name, '.');
|
|
|
|
if (period != 0) {
|
|
|
|
char temp[1024];
|
|
|
|
strcpy(temp, period + 1);
|
|
|
|
*period = 0;
|
|
|
|
sprintf(name, "%s-%02d.%s", name, chain + 1, temp);
|
|
|
|
} else {
|
|
|
|
sprintf(name, "%s-%02d", name, chain + 1);
|
2003-05-03 20:37:18 +04:00
|
|
|
}
|
2012-10-07 15:38:55 +04:00
|
|
|
}
|
|
|
|
return name;
|
2003-05-03 20:37:18 +04:00
|
|
|
}
|
|
|
|
|
2008-01-27 01:24:03 +03:00
|
|
|
/*
|
2003-05-03 20:37:18 +04:00
|
|
|
* This function will panic if errors occur when attempting to open an image
|
|
|
|
* file. Now if only I could use exceptions to handle the errors in an elegant
|
|
|
|
* fashion...
|
|
|
|
*/
|
2012-10-07 22:36:22 +04:00
|
|
|
int vmware3_image_t::open(const char* _pathname, int flags)
|
2003-05-03 20:37:18 +04:00
|
|
|
{
|
2012-10-07 15:38:55 +04:00
|
|
|
COW_Header header;
|
|
|
|
int file;
|
2012-10-07 22:36:22 +04:00
|
|
|
Bit64u imgsize = 0;
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
pathname = _pathname;
|
|
|
|
// Set so close doesn't segfault, in case something goes wrong
|
|
|
|
images = NULL;
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
/* Open the virtual disk */
|
2012-10-07 22:36:22 +04:00
|
|
|
file = hdimage_open_file(pathname, flags, &imgsize, &mtime);
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
if (file < 0)
|
|
|
|
return -1;
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
/* Read the header */
|
|
|
|
if (!read_header(file, header)) {
|
|
|
|
BX_PANIC(("unable to read vmware3 COW Disk header or invalid header from file '%s'", pathname));
|
|
|
|
return -1;
|
|
|
|
}
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
::close(file);
|
2004-01-20 02:25:47 +03:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
tlb_size = header.tlb_size_sectors * 512;
|
|
|
|
slb_count = (1 << FL_SHIFT) / tlb_size;
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
// we must have at least one chain
|
|
|
|
unsigned count = header.number_of_chains;
|
|
|
|
if (count < 1) count = 1;
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
images = new COW_Image[count];
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
off_t offset = 0;
|
|
|
|
for (unsigned i = 0; i < count; ++i) {
|
|
|
|
char* filename = generate_cow_name(pathname, i);
|
|
|
|
current = &images[i];
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
current->fd = ::open(filename, flags);
|
|
|
|
if (current->fd < 0)
|
|
|
|
BX_PANIC(("unable to open vmware3 COW Disk file '%s'", filename));
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
if (!read_header(current->fd, current->header))
|
|
|
|
BX_PANIC(("unable to read header or invalid header in vmware3 COW Disk file '%s'", filename));
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
current->flb = new unsigned [current->header.flb_count];
|
|
|
|
if (current->flb == 0)
|
|
|
|
BX_PANIC(("cannot allocate %d bytes for flb in vmware3 COW Disk '%s'", current->header.flb_count * 4, filename));
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
current->slb = new unsigned* [current->header.flb_count];
|
|
|
|
if (current->slb == 0)
|
|
|
|
BX_PANIC(("cannot allocate %d bytes for slb in vmware3 COW Disk '%s'", current->header.flb_count * 4, filename));
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
unsigned j;
|
|
|
|
for (j = 0; j < current->header.flb_count; ++j) {
|
|
|
|
current->slb[j] = new unsigned [slb_count];
|
|
|
|
if (current->slb[j] == 0)
|
|
|
|
BX_PANIC(("cannot allocate %d bytes for slb[] in vmware3 COW Disk '%s'", slb_count * 4, filename));
|
|
|
|
}
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
current->tlb = new Bit8u[tlb_size];
|
|
|
|
if (current->tlb == 0)
|
|
|
|
BX_PANIC(("cannot allocate %d bytes for tlb in vmware3 COW Disk '%s'", tlb_size, filename));
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
if (::lseek(current->fd, current->header.flb_offset_sectors * 512, SEEK_SET) < 0)
|
|
|
|
BX_PANIC(("unable to seek vmware3 COW Disk file '%s'", filename));
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
if (read_ints(current->fd, current->flb, current->header.flb_count) < 0)
|
|
|
|
BX_PANIC(("unable to read flb from vmware3 COW Disk file '%s'", filename));
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
for (j = 0; j < current->header.flb_count; ++j)
|
|
|
|
if(current->flb[j] != 0) {
|
|
|
|
if (::lseek(current->fd, current->flb[j] * 512, SEEK_SET) < 0)
|
|
|
|
BX_PANIC(("unable to seek vmware3 COW Disk file '%s'", filename));
|
|
|
|
if (read_ints(current->fd, current->slb[j], slb_count) < 0)
|
|
|
|
BX_PANIC(("unable to read slb from vmware3 COW Disk file '%s'", filename));
|
|
|
|
}
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
current->min_offset = offset;
|
|
|
|
offset += current->header.total_sectors * 512;
|
|
|
|
current->max_offset = offset;
|
2004-01-18 19:39:02 +03:00
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
current->offset = INVALID_OFFSET;
|
|
|
|
current->synced = true;
|
|
|
|
delete [] filename;
|
|
|
|
}
|
|
|
|
current = &images[0];
|
|
|
|
requested_offset = 0;
|
|
|
|
if (header.total_sectors_in_disk != 0) {
|
|
|
|
cylinders = header.cylinders_in_disk;
|
|
|
|
heads = header.heads_in_disk;
|
|
|
|
spt = header.sectors_in_disk;
|
|
|
|
hd_size = header.total_sectors_in_disk * 512;
|
|
|
|
} else {
|
|
|
|
cylinders = header.cylinders;
|
|
|
|
heads = header.heads;
|
|
|
|
spt = header.sectors;
|
|
|
|
hd_size = header.total_sectors * 512;
|
|
|
|
}
|
|
|
|
return 1;
|
2003-05-03 20:37:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
off_t vmware3_image_t::perform_seek()
|
|
|
|
{
|
2011-09-11 21:15:21 +04:00
|
|
|
if(requested_offset < current->min_offset || requested_offset >= current->max_offset)
|
|
|
|
{
|
|
|
|
if(!sync())
|
|
|
|
{
|
|
|
|
BX_DEBUG(("could not sync before switching vmware3 COW files"));
|
|
|
|
return INVALID_OFFSET;
|
|
|
|
}
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2011-09-11 21:15:21 +04:00
|
|
|
while(requested_offset < current->min_offset)
|
|
|
|
current = &images[current->header.chain_id - 1];
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2011-09-11 21:15:21 +04:00
|
|
|
while(requested_offset >= current->max_offset)
|
|
|
|
current = &images[current->header.chain_id + 1];
|
|
|
|
}
|
2003-05-03 20:37:18 +04:00
|
|
|
|
|
|
|
if(current->offset != INVALID_OFFSET && requested_offset >= current->offset
|
|
|
|
&& requested_offset < current->offset + tlb_size)
|
|
|
|
return (requested_offset - current->offset);
|
|
|
|
|
2011-09-11 21:15:21 +04:00
|
|
|
if(!sync())
|
|
|
|
{
|
|
|
|
BX_DEBUG(("could not sync before seeking vmware3 COW file"));
|
|
|
|
return INVALID_OFFSET;
|
|
|
|
}
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2005-06-04 21:44:59 +04:00
|
|
|
unsigned relative_offset = (unsigned)(requested_offset - current->min_offset);
|
2003-05-03 20:37:18 +04:00
|
|
|
unsigned i = relative_offset >> FL_SHIFT;
|
|
|
|
unsigned j = (relative_offset & ~FL_MASK) / tlb_size;
|
|
|
|
|
|
|
|
if(current->slb[i][j])
|
|
|
|
{
|
|
|
|
if(::lseek(current->fd, current->slb[i][j] * 512, SEEK_SET) < 0)
|
|
|
|
{
|
|
|
|
BX_DEBUG(("could not seek vmware3 COW to sector slb[%d][%d]", i, j));
|
|
|
|
return INVALID_OFFSET;
|
|
|
|
}
|
|
|
|
if(::read(current->fd, current->tlb, tlb_size) < 0)
|
|
|
|
{
|
|
|
|
BX_DEBUG(("could not read %d bytes from vmware3 COW image", tlb_size));
|
|
|
|
return INVALID_OFFSET;
|
|
|
|
}
|
|
|
|
}
|
2011-09-11 21:15:21 +04:00
|
|
|
else memset(current->tlb, 0, tlb_size);
|
2003-05-03 20:37:18 +04:00
|
|
|
|
|
|
|
current->offset = (requested_offset / tlb_size) * tlb_size;
|
|
|
|
return (requested_offset - current->offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t vmware3_image_t::read(void * buf, size_t count)
|
|
|
|
{
|
|
|
|
ssize_t total = 0;
|
|
|
|
while(count > 0)
|
|
|
|
{
|
|
|
|
off_t offset = perform_seek();
|
|
|
|
if(offset == INVALID_OFFSET)
|
|
|
|
{
|
2011-01-02 19:51:08 +03:00
|
|
|
BX_DEBUG(("vmware3 COW read failed on %u bytes", (unsigned)count));
|
2003-05-03 20:37:18 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2005-06-04 21:44:59 +04:00
|
|
|
unsigned bytes_remaining = (unsigned)(tlb_size - offset);
|
2003-05-03 20:37:18 +04:00
|
|
|
unsigned amount = (bytes_remaining > count) ? count : bytes_remaining;
|
|
|
|
memcpy(buf, current->tlb + offset, amount);
|
|
|
|
requested_offset += amount;
|
|
|
|
total += amount;
|
|
|
|
count -= amount;
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This could be done much better, I'm sure. In fact, the whole header doesn't
|
|
|
|
* need to be re-written each time a new tlb is allocated nor does the whole
|
|
|
|
* slb need to be re-written (most of the time) but that can be changed whenever
|
|
|
|
* it becomes an issue... image I/O is not a bottleneck.
|
|
|
|
*/
|
|
|
|
bool vmware3_image_t::sync()
|
|
|
|
{
|
2011-09-11 21:15:21 +04:00
|
|
|
if(current->synced)
|
|
|
|
return true;
|
2003-05-03 20:37:18 +04:00
|
|
|
|
2005-06-04 21:44:59 +04:00
|
|
|
unsigned relative_offset = (unsigned)(current->offset - current->min_offset);
|
2003-05-03 20:37:18 +04:00
|
|
|
unsigned i = relative_offset >> FL_SHIFT;
|
|
|
|
unsigned j = (relative_offset & ~FL_MASK) / tlb_size;
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2003-05-03 20:37:18 +04:00
|
|
|
if (current->slb[i][j] == 0)
|
|
|
|
{
|
|
|
|
if (current->flb[i] == 0)
|
|
|
|
{
|
|
|
|
unsigned slb_size = slb_count * 4;
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2003-05-03 20:37:18 +04:00
|
|
|
/* Re-write the FLB */
|
|
|
|
current->flb[i] = current->header.next_sector_to_allocate;
|
|
|
|
if(::lseek(current->fd, current->header.flb_offset_sectors * 512, SEEK_SET) < 0)
|
|
|
|
{
|
|
|
|
BX_DEBUG(("could not seek vmware3 COW image to flb on sync"));
|
|
|
|
return false;
|
|
|
|
}
|
2004-01-18 19:39:02 +03:00
|
|
|
if(write_ints(current->fd, current->flb, current->header.flb_count) < 0)
|
2003-05-03 20:37:18 +04:00
|
|
|
{
|
|
|
|
BX_DEBUG(("could not re-write flb to vmware3 COW image on sync"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
current->header.next_sector_to_allocate += (slb_size / 512) + ((slb_size % 512) ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-write the SLB */
|
|
|
|
current->slb[i][j] = current->header.next_sector_to_allocate;
|
|
|
|
if(::lseek(current->fd, current->flb[i] * 512, SEEK_SET) < 0)
|
|
|
|
{
|
|
|
|
BX_DEBUG(("could not seek vmware3 COW image to slb on sync"));
|
|
|
|
return false;
|
|
|
|
}
|
2004-01-18 19:39:02 +03:00
|
|
|
if(write_ints(current->fd, current->slb[i], slb_count) < 0)
|
2003-05-03 20:37:18 +04:00
|
|
|
{
|
|
|
|
BX_DEBUG(("could not re-write slb to vmware3 COW image on sync"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
current->header.next_sector_to_allocate += current->header.tlb_size_sectors;
|
|
|
|
|
|
|
|
/* Update the header */
|
|
|
|
if(::lseek(current->fd, 0, SEEK_SET) < 0)
|
|
|
|
{
|
|
|
|
BX_DEBUG(("could not seek to vmware3 COW image to offset 0 on sync"));
|
|
|
|
return false;
|
|
|
|
}
|
2004-01-18 19:39:02 +03:00
|
|
|
if(write_header(current->fd, current->header) < 0)
|
2003-05-03 20:37:18 +04:00
|
|
|
{
|
|
|
|
BX_DEBUG(("could not re-write header to vmware3 COW image on sync"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(::lseek(current->fd, current->slb[i][j] * 512, SEEK_SET) < 0)
|
|
|
|
{
|
|
|
|
BX_DEBUG(("could not seek vmware3 COW image to offset %d on sync", current->slb[i][j] * 512));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(::write(current->fd, current->tlb, tlb_size) < 0)
|
|
|
|
{
|
|
|
|
BX_DEBUG(("could not write tlb to vmware3 COW image on sync"));
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-11 21:15:21 +04:00
|
|
|
current->synced = true;
|
2003-05-03 20:37:18 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t vmware3_image_t::write(const void * buf, size_t count)
|
|
|
|
{
|
|
|
|
ssize_t total = 0;
|
|
|
|
while(count > 0)
|
|
|
|
{
|
|
|
|
off_t offset = perform_seek();
|
|
|
|
if(offset == INVALID_OFFSET)
|
|
|
|
return -1;
|
2005-06-04 21:44:59 +04:00
|
|
|
unsigned bytes_remaining = (unsigned)(tlb_size - offset);
|
2003-05-03 20:37:18 +04:00
|
|
|
unsigned amount = 0;
|
2011-09-11 21:15:21 +04:00
|
|
|
current->synced = false;
|
2003-05-03 20:37:18 +04:00
|
|
|
if(bytes_remaining > count)
|
|
|
|
{
|
|
|
|
memcpy(current->tlb + offset, buf, count);
|
|
|
|
amount = count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(current->tlb + offset, buf, bytes_remaining);
|
|
|
|
if(!sync())
|
|
|
|
{
|
2011-01-02 19:51:08 +03:00
|
|
|
BX_DEBUG(("failed to sync when writing %u bytes", (unsigned)count));
|
2003-05-03 20:37:18 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
amount = bytes_remaining;
|
|
|
|
}
|
|
|
|
requested_offset += amount;
|
|
|
|
total += amount;
|
|
|
|
count -= amount;
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2006-11-19 12:55:23 +03:00
|
|
|
Bit64s vmware3_image_t::lseek(Bit64s offset, int whence)
|
2003-05-03 20:37:18 +04:00
|
|
|
{
|
|
|
|
if(whence == SEEK_SET)
|
2006-11-19 12:55:23 +03:00
|
|
|
requested_offset = (off_t)offset;
|
2003-05-03 20:37:18 +04:00
|
|
|
else if (whence == SEEK_CUR)
|
2006-11-19 12:55:23 +03:00
|
|
|
requested_offset += (off_t)offset;
|
2003-05-03 20:37:18 +04:00
|
|
|
else if (whence == SEEK_END)
|
2006-11-19 12:55:23 +03:00
|
|
|
requested_offset = (off_t)(current->header.total_sectors_in_disk * 512) + (off_t)offset;
|
2003-05-03 20:37:18 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
BX_DEBUG(("unknown 'whence' value (%d) when trying to seek vmware3 COW image", whence));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return requested_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vmware3_image_t::close()
|
|
|
|
{
|
|
|
|
if(current == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned count = current->header.number_of_chains;
|
2004-01-20 02:25:47 +03:00
|
|
|
if (count < 1) count = 1;
|
2003-06-08 12:21:12 +04:00
|
|
|
for(unsigned i = 0; i < count; ++i)
|
2003-05-03 20:37:18 +04:00
|
|
|
{
|
2004-01-18 19:39:02 +03:00
|
|
|
if (images != NULL)
|
|
|
|
{
|
|
|
|
current = &images[i];
|
|
|
|
for(unsigned j = 0; j < current->header.flb_count; ++j)
|
|
|
|
delete[] current->slb[j];
|
|
|
|
delete[] current->flb;
|
|
|
|
delete[] current->slb;
|
|
|
|
delete[] current->tlb;
|
|
|
|
::close(current->fd);
|
|
|
|
delete[] images;
|
|
|
|
images = NULL;
|
|
|
|
}
|
2003-05-03 20:37:18 +04:00
|
|
|
}
|
|
|
|
current = 0;
|
|
|
|
}
|
2010-12-22 00:47:41 +03:00
|
|
|
|
|
|
|
Bit32u vmware3_image_t::get_capabilities(void)
|
|
|
|
{
|
|
|
|
return HDIMAGE_HAS_GEOMETRY;
|
|
|
|
}
|
2012-09-20 01:05:18 +04:00
|
|
|
|
2013-10-13 18:33:55 +04:00
|
|
|
#ifndef BXIMAGE
|
2012-09-20 01:05:18 +04:00
|
|
|
bx_bool vmware3_image_t::save_state(const char *backup_fname)
|
|
|
|
{
|
|
|
|
bx_bool ret = 1;
|
|
|
|
char tempfn[BX_PATHNAME_LEN];
|
|
|
|
|
|
|
|
unsigned count = current->header.number_of_chains;
|
|
|
|
if (count < 1) count = 1;
|
|
|
|
for (unsigned i = 0; i < count; ++i) {
|
|
|
|
sprintf(tempfn, "%s%d", backup_fname, i);
|
|
|
|
ret &= hdimage_backup_file(images[i].fd, tempfn);
|
|
|
|
if (ret == 0) break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2012-09-25 20:24:19 +04:00
|
|
|
|
|
|
|
void vmware3_image_t::restore_state(const char *backup_fname)
|
|
|
|
{
|
2012-10-07 15:38:55 +04:00
|
|
|
int temp_fd;
|
|
|
|
Bit64u imgsize;
|
2012-09-26 20:20:38 +04:00
|
|
|
bx_bool ret = 1;
|
|
|
|
char tempfn[BX_PATHNAME_LEN];
|
|
|
|
|
2012-10-07 15:38:55 +04:00
|
|
|
if ((temp_fd = hdimage_open_file(backup_fname, O_RDONLY, &imgsize, NULL)) < 0) {
|
|
|
|
BX_PANIC(("Cannot open vmware3 image backup '%s'", backup_fname));
|
2012-09-26 20:20:38 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-10-07 15:38:55 +04:00
|
|
|
|
|
|
|
if (check_format(temp_fd, imgsize) < HDIMAGE_FORMAT_OK) {
|
|
|
|
::close(temp_fd);
|
|
|
|
BX_PANIC(("Cannot detect vmware3 image header"));
|
2012-09-26 20:20:38 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-10-07 15:38:55 +04:00
|
|
|
::close(temp_fd);
|
2012-09-26 20:20:38 +04:00
|
|
|
unsigned count = current->header.number_of_chains;
|
|
|
|
close();
|
|
|
|
if (count < 1) count = 1;
|
|
|
|
for (unsigned i = 0; i < count; ++i) {
|
|
|
|
sprintf(tempfn, "%s%d", backup_fname, i);
|
|
|
|
char *filename = generate_cow_name(pathname, i);
|
|
|
|
ret &= hdimage_copy_file(tempfn, filename);
|
|
|
|
delete [] filename;
|
|
|
|
if (ret == 0) {
|
|
|
|
BX_PANIC(("Failed to restore vmware3 image '%s'", filename));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret == 1) {
|
2012-10-07 22:36:22 +04:00
|
|
|
device_image_t::open(pathname);
|
2012-09-26 20:20:38 +04:00
|
|
|
}
|
2012-09-25 20:24:19 +04:00
|
|
|
}
|
2013-10-13 18:33:55 +04:00
|
|
|
#endif
|