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 "dev/disk_image.hh"
48 #include "sim/sim_exit.hh"
49 #include "sim/byteswap.hh"
53 ////////////////////////////////////////////////////////////////////////
57 RawDiskImage::RawDiskImage(const Params
* p
)
58 : DiskImage(p
), disk_size(0)
59 { open(p
->image_file
, p
->read_only
); }
61 RawDiskImage::~RawDiskImage()
65 RawDiskImage::open(const string
&filename
, bool rd_only
)
67 if (!filename
.empty()) {
72 ios::openmode mode
= ios::in
| ios::binary
;
75 stream
.open(file
.c_str(), mode
);
76 if (!stream
.is_open())
77 panic("Error opening %s", filename
);
88 RawDiskImage::size() const
91 if (!stream
.is_open())
92 panic("file not open!\n");
93 stream
.seekg(0, ios::end
);
94 disk_size
= stream
.tellg();
97 return disk_size
/ SectorSize
;
101 RawDiskImage::read(uint8_t *data
, off_t offset
) const
104 panic("RawDiskImage not initialized");
106 if (!stream
.is_open())
107 panic("file not open!\n");
109 if (stream
.seekg(offset
* SectorSize
, ios::beg
) < 0)
110 panic("Could not seek to location in file");
112 streampos pos
= stream
.tellg();
113 stream
.read((char *)data
, SectorSize
);
115 DPRINTF(DiskImageRead
, "read: offset=%d\n", (uint64_t)offset
);
116 DDUMP(DiskImageRead
, data
, SectorSize
);
118 return stream
.tellg() - pos
;
122 RawDiskImage::write(const uint8_t *data
, off_t offset
)
125 panic("RawDiskImage not initialized");
128 panic("Cannot write to a read only disk image");
130 if (!stream
.is_open())
131 panic("file not open!\n");
133 if (stream
.seekp(offset
* SectorSize
, ios::beg
) < 0)
134 panic("Could not seek to location in file");
136 DPRINTF(DiskImageWrite
, "write: offset=%d\n", (uint64_t)offset
);
137 DDUMP(DiskImageWrite
, data
, SectorSize
);
139 streampos pos
= stream
.tellp();
140 stream
.write((const char *)data
, SectorSize
);
141 return stream
.tellp() - pos
;
145 RawDiskImageParams::create()
147 return new RawDiskImage(this);
150 ////////////////////////////////////////////////////////////////////////
152 // Copy on Write Disk image
154 const uint32_t CowDiskImage::VersionMajor
= 1;
155 const uint32_t CowDiskImage::VersionMinor
= 0;
157 class CowDiskCallback
: public Callback
163 CowDiskCallback(CowDiskImage
*i
) : image(i
) {}
164 void process() { image
->save(); delete this; }
167 CowDiskImage::CowDiskImage(const Params
*p
)
168 : DiskImage(p
), filename(p
->image_file
), child(p
->child
), table(NULL
)
170 if (filename
.empty()) {
173 if (!open(filename
)) {
175 fatal("could not open read-only file");
180 registerExitCallback(new CowDiskCallback(this));
184 CowDiskImage::~CowDiskImage()
186 SectorTable::iterator i
= table
->begin();
187 SectorTable::iterator end
= table
->end();
196 SafeRead(ifstream
&stream
, void *data
, int count
)
198 stream
.read((char *)data
, count
);
199 if (!stream
.is_open())
200 panic("file not open");
203 panic("premature end-of-file");
205 if (stream
.bad() || stream
.fail())
206 panic("error reading cowdisk image");
211 SafeRead(ifstream
&stream
, T
&data
)
213 SafeRead(stream
, &data
, sizeof(data
));
218 SafeReadSwap(ifstream
&stream
, T
&data
)
220 SafeRead(stream
, &data
, sizeof(data
));
221 data
= letoh(data
); //is this the proper byte order conversion?
225 CowDiskImage::open(const string
&file
)
227 ifstream
stream(file
.c_str());
228 if (!stream
.is_open())
231 if (stream
.fail() || stream
.bad())
232 panic("Error opening %s", file
);
235 SafeRead(stream
, magic
);
237 if (memcmp(&magic
, "COWDISK!", sizeof(magic
)) != 0)
238 panic("Could not open %s: Invalid magic", file
);
240 uint32_t major
, minor
;
241 SafeReadSwap(stream
, major
);
242 SafeReadSwap(stream
, minor
);
244 if (major
!= VersionMajor
&& minor
!= VersionMinor
)
245 panic("Could not open %s: invalid version %d.%d != %d.%d",
246 file
, major
, minor
, VersionMajor
, VersionMinor
);
248 uint64_t sector_count
;
249 SafeReadSwap(stream
, sector_count
);
250 table
= new SectorTable(sector_count
);
253 for (uint64_t i
= 0; i
< sector_count
; i
++) {
255 SafeReadSwap(stream
, offset
);
257 Sector
*sector
= new Sector
;
258 SafeRead(stream
, sector
, sizeof(Sector
));
260 assert(table
->find(offset
) == table
->end());
261 (*table
)[offset
] = sector
;
271 CowDiskImage::init(int hash_size
)
273 table
= new SectorTable(hash_size
);
279 SafeWrite(ofstream
&stream
, const void *data
, int count
)
281 stream
.write((const char *)data
, count
);
282 if (!stream
.is_open())
283 panic("file not open");
286 panic("premature end-of-file");
288 if (stream
.bad() || stream
.fail())
289 panic("error reading cowdisk image");
294 SafeWrite(ofstream
&stream
, const T
&data
)
296 SafeWrite(stream
, &data
, sizeof(data
));
301 SafeWriteSwap(ofstream
&stream
, const T
&data
)
303 T swappeddata
= letoh(data
); //is this the proper byte order conversion?
304 SafeWrite(stream
, &swappeddata
, sizeof(data
));
313 CowDiskImage::save(const string
&file
)
316 panic("RawDiskImage not initialized");
318 ofstream
stream(file
.c_str());
319 if (!stream
.is_open() || stream
.fail() || stream
.bad())
320 panic("Error opening %s", file
);
323 memcpy(&magic
, "COWDISK!", sizeof(magic
));
324 SafeWrite(stream
, magic
);
326 SafeWriteSwap(stream
, (uint32_t)VersionMajor
);
327 SafeWriteSwap(stream
, (uint32_t)VersionMinor
);
328 SafeWriteSwap(stream
, (uint64_t)table
->size());
330 uint64_t size
= table
->size();
331 SectorTable::iterator iter
= table
->begin();
332 SectorTable::iterator end
= table
->end();
334 for (uint64_t i
= 0; i
< size
; i
++) {
336 panic("Incorrect Table Size during save of COW disk image");
338 SafeWriteSwap(stream
, (uint64_t)(*iter
).first
);
339 SafeWrite(stream
, (*iter
).second
->data
, sizeof(Sector
));
347 CowDiskImage::writeback()
349 SectorTable::iterator i
= table
->begin();
350 SectorTable::iterator end
= table
->end();
353 child
->write((*i
).second
->data
, (*i
).first
);
359 CowDiskImage::size() const
360 { return child
->size(); }
363 CowDiskImage::read(uint8_t *data
, off_t offset
) const
366 panic("CowDiskImage not initialized");
369 panic("access out of bounds");
371 SectorTable::const_iterator i
= table
->find(offset
);
372 if (i
== table
->end())
373 return child
->read(data
, offset
);
375 memcpy(data
, (*i
).second
->data
, SectorSize
);
376 DPRINTF(DiskImageRead
, "read: offset=%d\n", (uint64_t)offset
);
377 DDUMP(DiskImageRead
, data
, SectorSize
);
383 CowDiskImage::write(const uint8_t *data
, off_t offset
)
386 panic("RawDiskImage not initialized");
389 panic("access out of bounds");
391 SectorTable::iterator i
= table
->find(offset
);
392 if (i
== table
->end()) {
393 Sector
*sector
= new Sector
;
394 memcpy(sector
, data
, SectorSize
);
395 table
->insert(make_pair(offset
, sector
));
397 memcpy((*i
).second
->data
, data
, SectorSize
);
400 DPRINTF(DiskImageWrite
, "write: offset=%d\n", (uint64_t)offset
);
401 DDUMP(DiskImageWrite
, data
, SectorSize
);
407 CowDiskImage::serialize(ostream
&os
)
409 string cowFilename
= name() + ".cow";
410 SERIALIZE_SCALAR(cowFilename
);
411 save(Checkpoint::dir() + "/" + cowFilename
);
415 CowDiskImage::unserialize(Checkpoint
*cp
, const string
§ion
)
418 UNSERIALIZE_SCALAR(cowFilename
);
419 cowFilename
= cp
->cptDir
+ "/" + cowFilename
;
424 CowDiskImageParams::create()
426 return new CowDiskImage(this);