2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Nathan Binkert
32 * Disk Image Definitions
35 #include <sys/types.h>
44 #include "base/callback.hh"
45 #include "base/misc.hh"
46 #include "base/trace.hh"
47 #include "debug/DiskImageRead.hh"
48 #include "debug/DiskImageWrite.hh"
49 #include "dev/disk_image.hh"
50 #include "sim/byteswap.hh"
51 #include "sim/sim_exit.hh"
55 ////////////////////////////////////////////////////////////////////////
59 RawDiskImage::RawDiskImage(const Params
* p
)
60 : DiskImage(p
), disk_size(0)
61 { open(p
->image_file
, p
->read_only
); }
63 RawDiskImage::~RawDiskImage()
67 RawDiskImage::open(const string
&filename
, bool rd_only
)
69 if (!filename
.empty()) {
74 ios::openmode mode
= ios::in
| ios::binary
;
77 stream
.open(file
.c_str(), mode
);
78 if (!stream
.is_open())
79 panic("Error opening %s", filename
);
90 RawDiskImage::size() const
93 if (!stream
.is_open())
94 panic("file not open!\n");
95 stream
.seekg(0, ios::end
);
96 disk_size
= stream
.tellg();
99 return disk_size
/ SectorSize
;
103 RawDiskImage::read(uint8_t *data
, std::streampos offset
) const
106 panic("RawDiskImage not initialized");
108 if (!stream
.is_open())
109 panic("file not open!\n");
111 if (stream
.seekg(offset
* SectorSize
, ios::beg
) < 0)
112 panic("Could not seek to location in file");
114 streampos pos
= stream
.tellg();
115 stream
.read((char *)data
, SectorSize
);
117 DPRINTF(DiskImageRead
, "read: offset=%d\n", (uint64_t)offset
);
118 DDUMP(DiskImageRead
, data
, SectorSize
);
120 return stream
.tellg() - pos
;
124 RawDiskImage::write(const uint8_t *data
, std::streampos offset
)
127 panic("RawDiskImage not initialized");
130 panic("Cannot write to a read only disk image");
132 if (!stream
.is_open())
133 panic("file not open!\n");
135 if (stream
.seekp(offset
* SectorSize
, ios::beg
) < 0)
136 panic("Could not seek to location in file");
138 DPRINTF(DiskImageWrite
, "write: offset=%d\n", (uint64_t)offset
);
139 DDUMP(DiskImageWrite
, data
, SectorSize
);
141 streampos pos
= stream
.tellp();
142 stream
.write((const char *)data
, SectorSize
);
143 return stream
.tellp() - pos
;
147 RawDiskImageParams::create()
149 return new RawDiskImage(this);
152 ////////////////////////////////////////////////////////////////////////
154 // Copy on Write Disk image
156 const uint32_t CowDiskImage::VersionMajor
= 1;
157 const uint32_t CowDiskImage::VersionMinor
= 0;
159 class CowDiskCallback
: public Callback
165 CowDiskCallback(CowDiskImage
*i
) : image(i
) {}
166 void process() { image
->save(); delete this; }
169 CowDiskImage::CowDiskImage(const Params
*p
)
170 : DiskImage(p
), filename(p
->image_file
), child(p
->child
), table(NULL
)
172 if (filename
.empty()) {
173 initSectorTable(p
->table_size
);
175 if (!open(filename
)) {
177 fatal("could not open read-only file");
178 initSectorTable(p
->table_size
);
182 registerExitCallback(new CowDiskCallback(this));
186 CowDiskImage::~CowDiskImage()
188 SectorTable::iterator i
= table
->begin();
189 SectorTable::iterator end
= table
->end();
198 SafeRead(ifstream
&stream
, void *data
, int count
)
200 stream
.read((char *)data
, count
);
201 if (!stream
.is_open())
202 panic("file not open");
205 panic("premature end-of-file");
207 if (stream
.bad() || stream
.fail())
208 panic("error reading cowdisk image");
213 SafeRead(ifstream
&stream
, T
&data
)
215 SafeRead(stream
, &data
, sizeof(data
));
220 SafeReadSwap(ifstream
&stream
, T
&data
)
222 SafeRead(stream
, &data
, sizeof(data
));
223 data
= letoh(data
); //is this the proper byte order conversion?
227 CowDiskImage::open(const string
&file
)
229 ifstream
stream(file
.c_str());
230 if (!stream
.is_open())
233 if (stream
.fail() || stream
.bad())
234 panic("Error opening %s", file
);
237 SafeRead(stream
, magic
);
239 if (memcmp(&magic
, "COWDISK!", sizeof(magic
)) != 0)
240 panic("Could not open %s: Invalid magic", file
);
242 uint32_t major
, minor
;
243 SafeReadSwap(stream
, major
);
244 SafeReadSwap(stream
, minor
);
246 if (major
!= VersionMajor
&& minor
!= VersionMinor
)
247 panic("Could not open %s: invalid version %d.%d != %d.%d",
248 file
, major
, minor
, VersionMajor
, VersionMinor
);
250 uint64_t sector_count
;
251 SafeReadSwap(stream
, sector_count
);
252 table
= new SectorTable(sector_count
);
255 for (uint64_t i
= 0; i
< sector_count
; i
++) {
257 SafeReadSwap(stream
, offset
);
259 Sector
*sector
= new Sector
;
260 SafeRead(stream
, sector
, sizeof(Sector
));
262 assert(table
->find(offset
) == table
->end());
263 (*table
)[offset
] = sector
;
273 CowDiskImage::initSectorTable(int hash_size
)
275 table
= new SectorTable(hash_size
);
281 SafeWrite(ofstream
&stream
, const void *data
, int count
)
283 stream
.write((const char *)data
, count
);
284 if (!stream
.is_open())
285 panic("file not open");
288 panic("premature end-of-file");
290 if (stream
.bad() || stream
.fail())
291 panic("error reading cowdisk image");
296 SafeWrite(ofstream
&stream
, const T
&data
)
298 SafeWrite(stream
, &data
, sizeof(data
));
303 SafeWriteSwap(ofstream
&stream
, const T
&data
)
305 T swappeddata
= letoh(data
); //is this the proper byte order conversion?
306 SafeWrite(stream
, &swappeddata
, sizeof(data
));
315 CowDiskImage::save(const string
&file
)
318 panic("RawDiskImage not initialized");
320 ofstream
stream(file
.c_str());
321 if (!stream
.is_open() || stream
.fail() || stream
.bad())
322 panic("Error opening %s", file
);
325 memcpy(&magic
, "COWDISK!", sizeof(magic
));
326 SafeWrite(stream
, magic
);
328 SafeWriteSwap(stream
, (uint32_t)VersionMajor
);
329 SafeWriteSwap(stream
, (uint32_t)VersionMinor
);
330 SafeWriteSwap(stream
, (uint64_t)table
->size());
332 uint64_t size
= table
->size();
333 SectorTable::iterator iter
= table
->begin();
334 SectorTable::iterator end
= table
->end();
336 for (uint64_t i
= 0; i
< size
; i
++) {
338 panic("Incorrect Table Size during save of COW disk image");
340 SafeWriteSwap(stream
, (uint64_t)(*iter
).first
);
341 SafeWrite(stream
, (*iter
).second
->data
, sizeof(Sector
));
349 CowDiskImage::writeback()
351 SectorTable::iterator i
= table
->begin();
352 SectorTable::iterator end
= table
->end();
355 child
->write((*i
).second
->data
, (*i
).first
);
361 CowDiskImage::size() const
362 { return child
->size(); }
365 CowDiskImage::read(uint8_t *data
, std::streampos offset
) const
368 panic("CowDiskImage not initialized");
371 panic("access out of bounds");
373 SectorTable::const_iterator i
= table
->find(offset
);
374 if (i
== table
->end())
375 return child
->read(data
, offset
);
377 memcpy(data
, (*i
).second
->data
, SectorSize
);
378 DPRINTF(DiskImageRead
, "read: offset=%d\n", (uint64_t)offset
);
379 DDUMP(DiskImageRead
, data
, SectorSize
);
385 CowDiskImage::write(const uint8_t *data
, std::streampos offset
)
388 panic("RawDiskImage not initialized");
391 panic("access out of bounds");
393 SectorTable::iterator i
= table
->find(offset
);
394 if (i
== table
->end()) {
395 Sector
*sector
= new Sector
;
396 memcpy(sector
, data
, SectorSize
);
397 table
->insert(make_pair(offset
, sector
));
399 memcpy((*i
).second
->data
, data
, SectorSize
);
402 DPRINTF(DiskImageWrite
, "write: offset=%d\n", (uint64_t)offset
);
403 DDUMP(DiskImageWrite
, data
, SectorSize
);
409 CowDiskImage::serialize(ostream
&os
)
411 string cowFilename
= name() + ".cow";
412 SERIALIZE_SCALAR(cowFilename
);
413 save(Checkpoint::dir() + "/" + cowFilename
);
417 CowDiskImage::unserialize(Checkpoint
*cp
, const string
§ion
)
420 UNSERIALIZE_SCALAR(cowFilename
);
421 cowFilename
= cp
->cptDir
+ "/" + cowFilename
;
426 CowDiskImageParams::create()
428 return new CowDiskImage(this);