disk_image.hh revision 11264
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.
272665SN/A *
282665SN/A * Authors: Nathan Binkert
292SN/A */
302SN/A
311722SN/A/** @file
322SN/A * Disk Image Interfaces
332SN/A */
342SN/A
3511264Sandreas.sandberg@arm.com#ifndef __DEV_STORAGE_DISK_IMAGE_HH__
3611264Sandreas.sandberg@arm.com#define __DEV_STORAGE_DISK_IMAGE_HH__
372SN/A
382SN/A#include <fstream>
3911168SN/A#include <unordered_map>
402SN/A
418229SN/A#include "params/CowDiskImage.hh"
428229SN/A#include "params/DiskImage.hh"
438229SN/A#include "params/RawDiskImage.hh"
4456SN/A#include "sim/sim_object.hh"
452SN/A
462SN/A#define SectorSize (512)
472SN/A
481722SN/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
562SN/A  public:
575034SN/A    typedef DiskImageParams Params;
585034SN/A    DiskImage(const Params *p) : SimObject(p), initialized(false) {}
592SN/A    virtual ~DiskImage() {}
602SN/A
619533SN/A    virtual std::streampos size() const = 0;
622SN/A
639533SN/A    virtual std::streampos read(uint8_t *data,
649533SN/A                                std::streampos offset) const = 0;
659533SN/A    virtual std::streampos write(const uint8_t *data,
669533SN/A                                 std::streampos offset) = 0;
672SN/A};
682SN/A
691722SN/A/**
702SN/A * Specialization for accessing a raw disk image
712SN/A */
722SN/Aclass RawDiskImage : public DiskImage
732SN/A{
742SN/A  protected:
752SN/A    mutable std::fstream stream;
762SN/A    std::string file;
772SN/A    bool readonly;
789533SN/A    mutable std::streampos disk_size;
792SN/A
802SN/A  public:
815034SN/A    typedef RawDiskImageParams Params;
825034SN/A    RawDiskImage(const Params *p);
832SN/A    ~RawDiskImage();
842SN/A
852SN/A    void close();
862SN/A    void open(const std::string &filename, bool rd_only = false);
872SN/A
889533SN/A    virtual std::streampos size() const;
892SN/A
909533SN/A    virtual std::streampos read(uint8_t *data, std::streampos offset) const;
919533SN/A    virtual std::streampos write(const uint8_t *data, std::streampos offset);
922SN/A};
932SN/A
941722SN/A/**
952SN/A * Specialization for accessing a copy-on-write disk image layer.
962SN/A * A copy-on-write(COW) layer must be stacked on top of another disk
972SN/A * image layer this layer can be another CowDiskImage, or a
982SN/A * RawDiskImage.
992SN/A *
1002SN/A * This object is designed to provide a mechanism for persistant
1012SN/A * changes to a main disk image, or to provide a place for temporary
1022SN/A * changes to the image to take place that later may be thrown away.
1032SN/A */
1042SN/Aclass CowDiskImage : public DiskImage
1052SN/A{
1062SN/A  public:
1076227SN/A    static const uint32_t VersionMajor;
1086227SN/A    static const uint32_t VersionMinor;
1092SN/A
1102SN/A  protected:
1112SN/A    struct Sector {
1122SN/A        uint8_t data[SectorSize];
1132SN/A    };
11411168SN/A    typedef std::unordered_map<uint64_t, Sector *> SectorTable;
1152SN/A
1162SN/A  protected:
1172SN/A    std::string filename;
1182SN/A    DiskImage *child;
1192SN/A    SectorTable *table;
1202SN/A
1212SN/A  public:
1225034SN/A    typedef CowDiskImageParams Params;
1235034SN/A    CowDiskImage(const Params *p);
1242SN/A    ~CowDiskImage();
1252SN/A
1268737SN/A    void initSectorTable(int hash_size);
127259SN/A    bool open(const std::string &file);
12810905SN/A    void save() const;
12910905SN/A    void save(const std::string &file) const;
1302SN/A    void writeback();
13110905SN/A
13211168SN/A    void serialize(CheckpointOut &cp) const override;
13311168SN/A    void unserialize(CheckpointIn &cp) override;
1342SN/A
13511169SN/A    std::streampos size() const override;
1362SN/A
13711169SN/A    std::streampos read(uint8_t *data, std::streampos offset) const override;
13811169SN/A    std::streampos write(const uint8_t *data, std::streampos offset) override;
1392SN/A};
1402SN/A
1419554SN/Avoid SafeRead(std::ifstream &stream, void *data, int count);
1429554SN/A
1439554SN/Atemplate<class T>
1449554SN/Avoid SafeRead(std::ifstream &stream, T &data);
1459554SN/A
1469554SN/Atemplate<class T>
1479554SN/Avoid SafeReadSwap(std::ifstream &stream, T &data);
1489554SN/A
1499554SN/Avoid SafeWrite(std::ofstream &stream, const void *data, int count);
1509554SN/A
1519554SN/Atemplate<class T>
1529554SN/Avoid SafeWrite(std::ofstream &stream, const T &data);
1539554SN/A
1549554SN/Atemplate<class T>
1559554SN/Avoid SafeWriteSwap(std::ofstream &stream, const T &data);
1569554SN/A
15711264Sandreas.sandberg@arm.com#endif // __DEV_STORAGE_DISK_IMAGE_HH__
158