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 stream
.seekg(offset
* SectorSize
, ios::beg
);
113 panic("Could not seek to location in file");
115 streampos pos
= stream
.tellg();
116 stream
.read((char *)data
, SectorSize
);
118 DPRINTF(DiskImageRead
, "read: offset=%d\n", (uint64_t)offset
);
119 DDUMP(DiskImageRead
, data
, SectorSize
);
121 return stream
.tellg() - pos
;
125 RawDiskImage::write(const uint8_t *data
, std::streampos offset
)
128 panic("RawDiskImage not initialized");
131 panic("Cannot write to a read only disk image");
133 if (!stream
.is_open())
134 panic("file not open!\n");
136 stream
.seekp(offset
* SectorSize
, ios::beg
);
138 panic("Could not seek to location in file");
140 DPRINTF(DiskImageWrite
, "write: offset=%d\n", (uint64_t)offset
);
141 DDUMP(DiskImageWrite
, data
, SectorSize
);
143 streampos pos
= stream
.tellp();
144 stream
.write((const char *)data
, SectorSize
);
145 return stream
.tellp() - pos
;
149 RawDiskImageParams::create()
151 return new RawDiskImage(this);
154 ////////////////////////////////////////////////////////////////////////
156 // Copy on Write Disk image
158 const uint32_t CowDiskImage::VersionMajor
= 1;
159 const uint32_t CowDiskImage::VersionMinor
= 0;
161 class CowDiskCallback
: public Callback
167 CowDiskCallback(CowDiskImage
*i
) : image(i
) {}
168 void process() { image
->save(); delete this; }
171 CowDiskImage::CowDiskImage(const Params
*p
)
172 : DiskImage(p
), filename(p
->image_file
), child(p
->child
), table(NULL
)
174 if (filename
.empty()) {
175 initSectorTable(p
->table_size
);
177 if (!open(filename
)) {
179 fatal("could not open read-only file");
180 initSectorTable(p
->table_size
);
184 registerExitCallback(new CowDiskCallback(this));
188 CowDiskImage::~CowDiskImage()
190 SectorTable::iterator i
= table
->begin();
191 SectorTable::iterator end
= table
->end();
200 SafeRead(ifstream
&stream
, void *data
, int count
)
202 stream
.read((char *)data
, count
);
203 if (!stream
.is_open())
204 panic("file not open");
207 panic("premature end-of-file");
209 if (stream
.bad() || stream
.fail())
210 panic("error reading cowdisk image");
215 SafeRead(ifstream
&stream
, T
&data
)
217 SafeRead(stream
, &data
, sizeof(data
));
222 SafeReadSwap(ifstream
&stream
, T
&data
)
224 SafeRead(stream
, &data
, sizeof(data
));
225 data
= letoh(data
); //is this the proper byte order conversion?
229 CowDiskImage::open(const string
&file
)
231 ifstream
stream(file
.c_str());
232 if (!stream
.is_open())
235 if (stream
.fail() || stream
.bad())
236 panic("Error opening %s", file
);
239 SafeRead(stream
, magic
);
241 if (memcmp(&magic
, "COWDISK!", sizeof(magic
)) != 0)
242 panic("Could not open %s: Invalid magic", file
);
244 uint32_t major
, minor
;
245 SafeReadSwap(stream
, major
);
246 SafeReadSwap(stream
, minor
);
248 if (major
!= VersionMajor
&& minor
!= VersionMinor
)
249 panic("Could not open %s: invalid version %d.%d != %d.%d",
250 file
, major
, minor
, VersionMajor
, VersionMinor
);
252 uint64_t sector_count
;
253 SafeReadSwap(stream
, sector_count
);
254 table
= new SectorTable(sector_count
);
257 for (uint64_t i
= 0; i
< sector_count
; i
++) {
259 SafeReadSwap(stream
, offset
);
261 Sector
*sector
= new Sector
;
262 SafeRead(stream
, sector
, sizeof(Sector
));
264 assert(table
->find(offset
) == table
->end());
265 (*table
)[offset
] = sector
;
275 CowDiskImage::initSectorTable(int hash_size
)
277 table
= new SectorTable(hash_size
);
283 SafeWrite(ofstream
&stream
, const void *data
, int count
)
285 stream
.write((const char *)data
, count
);
286 if (!stream
.is_open())
287 panic("file not open");
290 panic("premature end-of-file");
292 if (stream
.bad() || stream
.fail())
293 panic("error reading cowdisk image");
298 SafeWrite(ofstream
&stream
, const T
&data
)
300 SafeWrite(stream
, &data
, sizeof(data
));
305 SafeWriteSwap(ofstream
&stream
, const T
&data
)
307 T swappeddata
= letoh(data
); //is this the proper byte order conversion?
308 SafeWrite(stream
, &swappeddata
, sizeof(data
));
311 CowDiskImage::save() const
317 CowDiskImage::save(const string
&file
) const
320 panic("RawDiskImage not initialized");
322 ofstream
stream(file
.c_str());
323 if (!stream
.is_open() || stream
.fail() || stream
.bad())
324 panic("Error opening %s", file
);
327 memcpy(&magic
, "COWDISK!", sizeof(magic
));
328 SafeWrite(stream
, magic
);
330 SafeWriteSwap(stream
, (uint32_t)VersionMajor
);
331 SafeWriteSwap(stream
, (uint32_t)VersionMinor
);
332 SafeWriteSwap(stream
, (uint64_t)table
->size());
334 uint64_t size
= table
->size();
335 SectorTable::iterator iter
= table
->begin();
336 SectorTable::iterator end
= table
->end();
338 for (uint64_t i
= 0; i
< size
; i
++) {
340 panic("Incorrect Table Size during save of COW disk image");
342 SafeWriteSwap(stream
, (uint64_t)(*iter
).first
);
343 SafeWrite(stream
, (*iter
).second
->data
, sizeof(Sector
));
351 CowDiskImage::writeback()
353 SectorTable::iterator i
= table
->begin();
354 SectorTable::iterator end
= table
->end();
357 child
->write((*i
).second
->data
, (*i
).first
);
363 CowDiskImage::size() const
364 { return child
->size(); }
367 CowDiskImage::read(uint8_t *data
, std::streampos offset
) const
370 panic("CowDiskImage not initialized");
373 panic("access out of bounds");
375 SectorTable::const_iterator i
= table
->find(offset
);
376 if (i
== table
->end())
377 return child
->read(data
, offset
);
379 memcpy(data
, (*i
).second
->data
, SectorSize
);
380 DPRINTF(DiskImageRead
, "read: offset=%d\n", (uint64_t)offset
);
381 DDUMP(DiskImageRead
, data
, SectorSize
);
387 CowDiskImage::write(const uint8_t *data
, std::streampos offset
)
390 panic("RawDiskImage not initialized");
393 panic("access out of bounds");
395 SectorTable::iterator i
= table
->find(offset
);
396 if (i
== table
->end()) {
397 Sector
*sector
= new Sector
;
398 memcpy(sector
, data
, SectorSize
);
399 table
->insert(make_pair(offset
, sector
));
401 memcpy((*i
).second
->data
, data
, SectorSize
);
404 DPRINTF(DiskImageWrite
, "write: offset=%d\n", (uint64_t)offset
);
405 DDUMP(DiskImageWrite
, data
, SectorSize
);
411 CowDiskImage::serialize(CheckpointOut
&cp
) const
413 string cowFilename
= name() + ".cow";
414 SERIALIZE_SCALAR(cowFilename
);
415 save(CheckpointIn::dir() + "/" + cowFilename
);
419 CowDiskImage::unserialize(CheckpointIn
&cp
)
422 UNSERIALIZE_SCALAR(cowFilename
);
423 cowFilename
= cp
.cptDir
+ "/" + cowFilename
;
428 CowDiskImageParams::create()
430 return new CowDiskImage(this);