47#include "debug/DiskImageRead.hh"
48#include "debug/DiskImageWrite.hh"
63 open(
p.image_file,
p.read_only);
75 panic(
"Attempting to fork system with read-write raw disk image.");
79 open(
p.image_file,
p.read_only);
85 if (!filename.empty()) {
90 std::ios::openmode
mode = std::ios::in | std::ios::binary;
92 mode |= std::ios::out;
95 panic(
"Error opening %s", filename);
110 panic(
"file not open!\n");
111 stream.seekg(0, std::ios::end);
122 panic(
"RawDiskImage not initialized");
125 panic(
"file not open!\n");
129 panic(
"Could not seek to location in file");
131 std::streampos pos =
stream.tellg();
134 DPRINTF(DiskImageRead,
"read: offset=%d\n", (uint64_t)
offset);
137 return stream.tellg() - pos;
144 panic(
"RawDiskImage not initialized");
147 panic(
"Cannot write to a read only disk image");
150 panic(
"file not open!\n");
154 panic(
"Could not seek to location in file");
156 DPRINTF(DiskImageWrite,
"write: offset=%d\n", (uint64_t)
offset);
159 std::streampos pos =
stream.tellp();
161 return stream.tellp() - pos;
179 fatal(
"could not open read-only file");
190 SectorTable::iterator
i =
table->begin();
191 SectorTable::iterator end =
table->end();
202 if (!
dynamic_cast<const Params &
>(
params()).read_only &&
204 inform(
"Disabling saving of COW image in forked child process.\n");
213 if (!stream.is_open())
214 panic(
"file not open");
217 panic(
"premature end-of-file");
219 if (stream.bad() || stream.fail())
220 panic(
"error reading cowdisk image");
241 std::ifstream stream(file.c_str());
242 if (!stream.is_open())
245 if (stream.fail() || stream.bad())
246 panic(
"Error opening %s", file);
251 if (memcmp(&magic,
"COWDISK!",
sizeof(magic)) != 0)
252 panic(
"Could not open %s: Invalid magic", file);
254 uint32_t major_version, minor_version;
259 panic(
"Could not open %s: invalid version %d.%d != %d.%d",
262 uint64_t sector_count;
267 for (uint64_t
i = 0;
i < sector_count;
i++) {
275 (*table)[
offset] = sector;
296 if (!stream.is_open())
297 panic(
"file not open");
300 panic(
"premature end-of-file");
302 if (stream.bad() || stream.fail())
303 panic(
"error reading cowdisk image");
334 panic(
"RawDiskImage not initialized");
336 std::ofstream stream(file.c_str());
337 if (!stream.is_open() || stream.fail() || stream.bad())
338 panic(
"Error opening %s", file);
341 memcpy(&magic,
"COWDISK!",
sizeof(magic));
349 SectorTable::iterator iter =
table->begin();
350 SectorTable::iterator end =
table->end();
352 for (uint64_t
i = 0;
i <
size;
i++) {
354 panic(
"Incorrect Table Size during save of COW disk image");
367 SectorTable::iterator
i =
table->begin();
368 SectorTable::iterator end =
table->end();
384 panic(
"CowDiskImage not initialized");
387 panic(
"access out of bounds");
394 DPRINTF(DiskImageRead,
"read: offset=%d\n", (uint64_t)
offset);
404 panic(
"RawDiskImage not initialized");
407 panic(
"access out of bounds");
418 DPRINTF(DiskImageWrite,
"write: offset=%d\n", (uint64_t)
offset);
427 std::string cowFilename =
name() +
".cow";
435 std::string cowFilename;
437 cowFilename = cp.
getCptDir() +
"/" + cowFilename;
#define DDUMP(x, data, count)
DPRINTF is a debugging trace facility that allows one to selectively enable tracing statements.
void notifyFork() override
Notify a child process of a fork.
std::streampos write(const uint8_t *data, std::streampos offset) override
std::streampos size() const override
void unserialize(CheckpointIn &cp) override
Unserialize an object.
static const uint32_t VersionMajor
std::streampos read(uint8_t *data, std::streampos offset) const override
void serialize(CheckpointOut &cp) const override
Serialize an object.
std::unordered_map< uint64_t, Sector * > SectorTable
bool open(const std::string &file)
CowDiskImageParams Params
static const uint32_t VersionMinor
void initSectorTable(int hash_size)
CowDiskImage(const Params &p)
Basic interface for accessing a disk image.
virtual std::streampos write(const uint8_t *data, std::streampos offset)=0
virtual std::streampos size() const =0
virtual std::streampos read(uint8_t *data, std::streampos offset) const =0
virtual std::string name() const
void notifyFork() override
Notify a child process of a fork.
void open(const std::string &filename, bool rd_only=false)
std::streampos size() const override
RawDiskImage(const Params &p)
std::streampos read(uint8_t *data, std::streampos offset) const override
std::streampos write(const uint8_t *data, std::streampos offset) override
RawDiskImageParams Params
#define panic(...)
This implements a cprintf based panic() function.
#define fatal(...)
This implements a cprintf based fatal() function.
const std::string getCptDir()
static std::string dir()
Get the current checkout directory name.
const Params & params() const
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
void SafeWriteSwap(std::ofstream &stream, const T &data)
std::ostream CheckpointOut
void SafeRead(std::ifstream &stream, void *data, int count)
const uint8_t image_file[]
This image file contains the text "This is a test image.\n" 31 times.
void SafeWrite(std::ofstream &stream, const void *data, int count)
void SafeReadSwap(std::ifstream &stream, T &data)
void registerExitCallback(const std::function< void()> &callback)
Register an exit callback.
#define UNSERIALIZE_SCALAR(scalar)
#define SERIALIZE_SCALAR(scalar)