disk_image.hh revision 8737
16019Shines@cs.fsu.edu/* 26019Shines@cs.fsu.edu * Copyright (c) 2001-2005 The Regents of The University of Michigan 36019Shines@cs.fsu.edu * All rights reserved. 46019Shines@cs.fsu.edu * 56019Shines@cs.fsu.edu * Redistribution and use in source and binary forms, with or without 66019Shines@cs.fsu.edu * modification, are permitted provided that the following conditions are 76019Shines@cs.fsu.edu * met: redistributions of source code must retain the above copyright 86019Shines@cs.fsu.edu * notice, this list of conditions and the following disclaimer; 96019Shines@cs.fsu.edu * redistributions in binary form must reproduce the above copyright 106019Shines@cs.fsu.edu * notice, this list of conditions and the following disclaimer in the 116019Shines@cs.fsu.edu * documentation and/or other materials provided with the distribution; 126019Shines@cs.fsu.edu * neither the name of the copyright holders nor the names of its 136019Shines@cs.fsu.edu * contributors may be used to endorse or promote products derived from 146019Shines@cs.fsu.edu * this software without specific prior written permission. 156019Shines@cs.fsu.edu * 166019Shines@cs.fsu.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 176019Shines@cs.fsu.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 186019Shines@cs.fsu.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 196019Shines@cs.fsu.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 206019Shines@cs.fsu.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 216019Shines@cs.fsu.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 226019Shines@cs.fsu.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 236019Shines@cs.fsu.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 246019Shines@cs.fsu.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 256019Shines@cs.fsu.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 266019Shines@cs.fsu.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 276019Shines@cs.fsu.edu * 286019Shines@cs.fsu.edu * Authors: Nathan Binkert 296019Shines@cs.fsu.edu */ 306019Shines@cs.fsu.edu 316019Shines@cs.fsu.edu/** @file 326019Shines@cs.fsu.edu * Disk Image Interfaces 336019Shines@cs.fsu.edu */ 346019Shines@cs.fsu.edu 356019Shines@cs.fsu.edu#ifndef __DISK_IMAGE_HH__ 366019Shines@cs.fsu.edu#define __DISK_IMAGE_HH__ 376019Shines@cs.fsu.edu 386019Shines@cs.fsu.edu#include <fstream> 396019Shines@cs.fsu.edu 406019Shines@cs.fsu.edu#include "base/hashmap.hh" 416019Shines@cs.fsu.edu#include "params/CowDiskImage.hh" 426019Shines@cs.fsu.edu#include "params/DiskImage.hh" 436019Shines@cs.fsu.edu#include "params/RawDiskImage.hh" 446019Shines@cs.fsu.edu#include "sim/sim_object.hh" 456019Shines@cs.fsu.edu 466019Shines@cs.fsu.edu#define SectorSize (512) 476019Shines@cs.fsu.edu 486019Shines@cs.fsu.edu/** 496019Shines@cs.fsu.edu * Basic interface for accessing a disk image. 506019Shines@cs.fsu.edu */ 516019Shines@cs.fsu.educlass DiskImage : public SimObject 526019Shines@cs.fsu.edu{ 536019Shines@cs.fsu.edu protected: 546019Shines@cs.fsu.edu bool initialized; 556019Shines@cs.fsu.edu 566019Shines@cs.fsu.edu public: 576019Shines@cs.fsu.edu typedef DiskImageParams Params; 586019Shines@cs.fsu.edu DiskImage(const Params *p) : SimObject(p), initialized(false) {} 596019Shines@cs.fsu.edu virtual ~DiskImage() {} 606019Shines@cs.fsu.edu 616019Shines@cs.fsu.edu virtual off_t size() const = 0; 626019Shines@cs.fsu.edu 636019Shines@cs.fsu.edu virtual off_t read(uint8_t *data, off_t offset) const = 0; 646019Shines@cs.fsu.edu virtual off_t write(const uint8_t *data, off_t offset) = 0; 65}; 66 67/** 68 * Specialization for accessing a raw disk image 69 */ 70class RawDiskImage : public DiskImage 71{ 72 protected: 73 mutable std::fstream stream; 74 std::string file; 75 bool readonly; 76 mutable off_t disk_size; 77 78 public: 79 typedef RawDiskImageParams Params; 80 RawDiskImage(const Params *p); 81 ~RawDiskImage(); 82 83 void close(); 84 void open(const std::string &filename, bool rd_only = false); 85 86 virtual off_t size() const; 87 88 virtual off_t read(uint8_t *data, off_t offset) const; 89 virtual off_t write(const uint8_t *data, off_t offset); 90}; 91 92/** 93 * Specialization for accessing a copy-on-write disk image layer. 94 * A copy-on-write(COW) layer must be stacked on top of another disk 95 * image layer this layer can be another CowDiskImage, or a 96 * RawDiskImage. 97 * 98 * This object is designed to provide a mechanism for persistant 99 * changes to a main disk image, or to provide a place for temporary 100 * changes to the image to take place that later may be thrown away. 101 */ 102class CowDiskImage : public DiskImage 103{ 104 public: 105 static const uint32_t VersionMajor; 106 static const uint32_t VersionMinor; 107 108 protected: 109 struct Sector { 110 uint8_t data[SectorSize]; 111 }; 112 typedef m5::hash_map<uint64_t, Sector *> SectorTable; 113 114 protected: 115 std::string filename; 116 DiskImage *child; 117 SectorTable *table; 118 119 public: 120 typedef CowDiskImageParams Params; 121 CowDiskImage(const Params *p); 122 ~CowDiskImage(); 123 124 void initSectorTable(int hash_size); 125 bool open(const std::string &file); 126 void save(); 127 void save(const std::string &file); 128 void writeback(); 129 void serialize(std::ostream &os); 130 void unserialize(Checkpoint *cp, const std::string §ion); 131 132 virtual off_t size() const; 133 134 virtual off_t read(uint8_t *data, off_t offset) const; 135 virtual off_t write(const uint8_t *data, off_t offset); 136}; 137 138#endif // __DISK_IMAGE_HH__ 139