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