dev: Delete the authors list from files in src/dev.
[gem5.git] / src / dev / storage / disk_image.hh
1 /*
2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 */
28
29 /** @file
30 * Disk Image Interfaces
31 */
32
33 #ifndef __DEV_STORAGE_DISK_IMAGE_HH__
34 #define __DEV_STORAGE_DISK_IMAGE_HH__
35
36 #include <fstream>
37 #include <unordered_map>
38
39 #include "params/CowDiskImage.hh"
40 #include "params/DiskImage.hh"
41 #include "params/RawDiskImage.hh"
42 #include "sim/sim_object.hh"
43
44 #define SectorSize (512)
45
46 /**
47 * Basic interface for accessing a disk image.
48 */
49 class DiskImage : public SimObject
50 {
51 protected:
52 bool initialized;
53
54 public:
55 typedef DiskImageParams Params;
56 DiskImage(const Params *p) : SimObject(p), initialized(false) {}
57 virtual ~DiskImage() {}
58
59 virtual std::streampos size() const = 0;
60
61 virtual std::streampos read(uint8_t *data,
62 std::streampos offset) const = 0;
63 virtual std::streampos write(const uint8_t *data,
64 std::streampos offset) = 0;
65 };
66
67 /**
68 * Specialization for accessing a raw disk image
69 */
70 class RawDiskImage : public DiskImage
71 {
72 protected:
73 mutable std::fstream stream;
74 std::string file;
75 bool readonly;
76 mutable std::streampos disk_size;
77
78 public:
79 typedef RawDiskImageParams Params;
80 RawDiskImage(const Params *p);
81 ~RawDiskImage();
82
83 void notifyFork() override;
84
85 void close();
86 void open(const std::string &filename, bool rd_only = false);
87
88 std::streampos size() const override;
89
90 std::streampos read(uint8_t *data, std::streampos offset) const override;
91 std::streampos write(const uint8_t *data, std::streampos offset) override;
92 };
93
94 /**
95 * Specialization for accessing a copy-on-write disk image layer.
96 * A copy-on-write(COW) layer must be stacked on top of another disk
97 * image layer this layer can be another CowDiskImage, or a
98 * RawDiskImage.
99 *
100 * This object is designed to provide a mechanism for persistant
101 * changes to a main disk image, or to provide a place for temporary
102 * changes to the image to take place that later may be thrown away.
103 */
104 class CowDiskImage : public DiskImage
105 {
106 public:
107 static const uint32_t VersionMajor;
108 static const uint32_t VersionMinor;
109
110 protected:
111 struct Sector {
112 uint8_t data[SectorSize];
113 };
114 typedef std::unordered_map<uint64_t, Sector *> SectorTable;
115
116 protected:
117 std::string filename;
118 DiskImage *child;
119 SectorTable *table;
120
121 public:
122 typedef CowDiskImageParams Params;
123 CowDiskImage(const Params *p);
124 ~CowDiskImage();
125
126 void notifyFork() override;
127
128 void initSectorTable(int hash_size);
129 bool open(const std::string &file);
130 void save() const;
131 void save(const std::string &file) const;
132 void writeback();
133
134 void serialize(CheckpointOut &cp) const override;
135 void unserialize(CheckpointIn &cp) override;
136
137 std::streampos size() const override;
138
139 std::streampos read(uint8_t *data, std::streampos offset) const override;
140 std::streampos write(const uint8_t *data, std::streampos offset) override;
141 };
142
143 void SafeRead(std::ifstream &stream, void *data, int count);
144
145 template<class T>
146 void SafeRead(std::ifstream &stream, T &data);
147
148 template<class T>
149 void SafeReadSwap(std::ifstream &stream, T &data);
150
151 void SafeWrite(std::ofstream &stream, const void *data, int count);
152
153 template<class T>
154 void SafeWrite(std::ofstream &stream, const T &data);
155
156 template<class T>
157 void SafeWriteSwap(std::ofstream &stream, const T &data);
158
159 #endif // __DEV_STORAGE_DISK_IMAGE_HH__