disk_image.hh revision 5034
12SN/A/*
21762SN/A * Copyright (c) 2001-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
292665Ssaidi@eecs.umich.edu */
302665Ssaidi@eecs.umich.edu
312665Ssaidi@eecs.umich.edu/** @file
322SN/A * Disk Image Interfaces
332SN/A */
342SN/A
352SN/A#ifndef __DISK_IMAGE_HH__
362SN/A#define __DISK_IMAGE_HH__
3756SN/A
381717SN/A#include <fstream>
392518SN/A
4056SN/A#include "base/hashmap.hh"
412518SN/A#include "sim/sim_object.hh"
422518SN/A#include "params/DiskImage.hh"
432SN/A#include "params/CowDiskImage.hh"
442SN/A#include "params/RawDiskImage.hh"
452SN/A
462SN/A#define SectorSize (512)
472SN/A
482SN/A/**
492SN/A * Basic interface for accessing a disk image.
502SN/A */
512SN/Aclass DiskImage : public SimObject
522SN/A{
532SN/A  protected:
542SN/A    bool initialized;
552SN/A
561904SN/A  public:
571968SN/A    typedef DiskImageParams Params;
581968SN/A    DiskImage(const Params *p) : SimObject(p), initialized(false) {}
591968SN/A    virtual ~DiskImage() {}
601968SN/A
611968SN/A    virtual off_t size() const = 0;
621968SN/A
631967SN/A    virtual off_t read(uint8_t *data, off_t offset) const = 0;
641967SN/A    virtual off_t write(const uint8_t *data, off_t offset) = 0;
651967SN/A};
661967SN/A
671967SN/A/**
681967SN/A * Specialization for accessing a raw disk image
691967SN/A */
701967SN/Aclass RawDiskImage : public DiskImage
711967SN/A{
721967SN/A  protected:
731904SN/A    mutable std::fstream stream;
741904SN/A    std::string file;
751904SN/A    bool readonly;
761904SN/A    mutable off_t disk_size;
77452SN/A
781904SN/A  public:
792SN/A    typedef RawDiskImageParams Params;
801904SN/A    RawDiskImage(const Params *p);
811904SN/A    ~RawDiskImage();
822SN/A
831904SN/A    void close();
841904SN/A    void open(const std::string &filename, bool rd_only = false);
852SN/A
862SN/A    virtual off_t size() const;
871904SN/A
881904SN/A    virtual off_t read(uint8_t *data, off_t offset) const;
891904SN/A    virtual off_t write(const uint8_t *data, off_t offset);
901904SN/A};
911904SN/A
921904SN/A/**
931904SN/A * Specialization for accessing a copy-on-write disk image layer.
941904SN/A * A copy-on-write(COW) layer must be stacked on top of another disk
951904SN/A * image layer this layer can be another CowDiskImage, or a
961904SN/A * RawDiskImage.
971904SN/A *
98452SN/A * This object is designed to provide a mechanism for persistant
991904SN/A * changes to a main disk image, or to provide a place for temporary
1001904SN/A * changes to the image to take place that later may be thrown away.
1011904SN/A */
1022SN/Aclass CowDiskImage : public DiskImage
1032SN/A{
1041904SN/A  public:
1051904SN/A    static const int VersionMajor;
1061904SN/A    static const int VersionMinor;
1071904SN/A
1081904SN/A  protected:
1091904SN/A    struct Sector {
1102SN/A        uint8_t data[SectorSize];
1111904SN/A    };
1122SN/A    typedef m5::hash_map<uint64_t, Sector *> SectorTable;
1132SN/A
1141904SN/A  protected:
1152SN/A    std::string filename;
1161904SN/A    DiskImage *child;
1171904SN/A    SectorTable *table;
1181904SN/A
1191904SN/A  public:
1201904SN/A    typedef CowDiskImageParams Params;
1211904SN/A    CowDiskImage(const Params *p);
1221904SN/A    ~CowDiskImage();
1231904SN/A
1241904SN/A    void init(int hash_size);
1251904SN/A    bool open(const std::string &file);
1261904SN/A    void save();
1271904SN/A    void save(const std::string &file);
1281904SN/A    void writeback();
1291904SN/A    void serialize(std::ostream &os);
1301904SN/A    void unserialize(Checkpoint *cp, const std::string &section);
1311904SN/A
1321904SN/A    virtual off_t size() const;
1331904SN/A
1341904SN/A    virtual off_t read(uint8_t *data, off_t offset) const;
1351904SN/A    virtual off_t write(const uint8_t *data, off_t offset);
1362525SN/A};
1371904SN/A
1382525SN/A#endif // __DISK_IMAGE_HH__
1392525SN/A