disk_image.hh revision 259
1/*
2 * Copyright (c) 2003 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 __DISK_IMAGE_HH__
34#define __DISK_IMAGE_HH__
35
36#include <fstream>
37
38#include "base/hashmap.hh"
39#include "sim/sim_object.hh"
40
41#define SectorSize (512)
42
43/*
44 * Basic interface for accessing a disk image.
45 */
46class DiskImage : public SimObject
47{
48  protected:
49    bool initialized;
50
51  public:
52    DiskImage(const std::string &name) : SimObject(name), initialized(false) {}
53    virtual ~DiskImage() {}
54
55    virtual off_t size() const = 0;
56
57    virtual off_t read(uint8_t *data, off_t offset) const = 0;
58    virtual off_t write(const uint8_t *data, off_t offset) = 0;
59};
60
61/*
62 * Specialization for accessing a raw disk image
63 */
64class RawDiskImage : public DiskImage
65{
66  protected:
67    mutable std::fstream stream;
68    std::string file;
69    bool readonly;
70    mutable off_t disk_size;
71
72  public:
73    RawDiskImage(const std::string &name, const std::string &filename,
74                 bool rd_only);
75    ~RawDiskImage();
76
77    void close();
78    void open(const std::string &filename, bool rd_only = false);
79
80    virtual off_t size() const;
81
82    virtual off_t read(uint8_t *data, off_t offset) const;
83    virtual off_t write(const uint8_t *data, off_t offset);
84};
85
86/*
87 * Specialization for accessing a copy-on-write disk image layer.
88 * A copy-on-write(COW) layer must be stacked on top of another disk
89 * image layer this layer can be another CowDiskImage, or a
90 * RawDiskImage.
91 *
92 * This object is designed to provide a mechanism for persistant
93 * changes to a main disk image, or to provide a place for temporary
94 * changes to the image to take place that later may be thrown away.
95 */
96class CowDiskImage : public DiskImage
97{
98  public:
99    static const int VersionMajor;
100    static const int VersionMinor;
101
102  protected:
103    struct Sector {
104        uint8_t data[SectorSize];
105    };
106    typedef m5::hash_map<uint64_t, Sector *> SectorTable;
107
108  protected:
109    std::string filename;
110    DiskImage *child;
111    SectorTable *table;
112
113  public:
114    CowDiskImage(const std::string &name, DiskImage *kid, int hash_size);
115    CowDiskImage(const std::string &name, DiskImage *kid, int hash_size,
116                 const std::string &filename, bool read_only);
117    ~CowDiskImage();
118
119    void init(int hash_size);
120    bool open(const std::string &file);
121    void save();
122    void save(const std::string &file);
123    void writeback();
124    void serialize(std::ostream &os);
125    void unserialize(Checkpoint *cp, const std::string &section);
126
127    virtual off_t size() const;
128
129    virtual off_t read(uint8_t *data, off_t offset) const;
130    virtual off_t write(const uint8_t *data, off_t offset);
131};
132
133#endif // __DISK_IMAGE_HH__
134