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 8511361Sandreas@sandberg.pp.se void notifyFork() override; 8611361Sandreas@sandberg.pp.se 872SN/A void close(); 882SN/A void open(const std::string &filename, bool rd_only = false); 892SN/A 9011442Sandreas.hansson@arm.com std::streampos size() const override; 912SN/A 9211613Sandreas.sandberg@arm.com std::streampos read(uint8_t *data, std::streampos offset) const override; 9311613Sandreas.sandberg@arm.com std::streampos write(const uint8_t *data, std::streampos offset) override; 942SN/A}; 952SN/A 961722SN/A/** 972SN/A * Specialization for accessing a copy-on-write disk image layer. 982SN/A * A copy-on-write(COW) layer must be stacked on top of another disk 992SN/A * image layer this layer can be another CowDiskImage, or a 1002SN/A * RawDiskImage. 1012SN/A * 1022SN/A * This object is designed to provide a mechanism for persistant 1032SN/A * changes to a main disk image, or to provide a place for temporary 1042SN/A * changes to the image to take place that later may be thrown away. 1052SN/A */ 1062SN/Aclass CowDiskImage : public DiskImage 1072SN/A{ 1082SN/A public: 1096227SN/A static const uint32_t VersionMajor; 1106227SN/A static const uint32_t VersionMinor; 1112SN/A 1122SN/A protected: 1132SN/A struct Sector { 1142SN/A uint8_t data[SectorSize]; 1152SN/A }; 11611168SN/A typedef std::unordered_map<uint64_t, Sector *> SectorTable; 1172SN/A 1182SN/A protected: 1192SN/A std::string filename; 1202SN/A DiskImage *child; 1212SN/A SectorTable *table; 1222SN/A 1232SN/A public: 1245034SN/A typedef CowDiskImageParams Params; 1255034SN/A CowDiskImage(const Params *p); 1262SN/A ~CowDiskImage(); 1272SN/A 12811361Sandreas@sandberg.pp.se void notifyFork() override; 12911361Sandreas@sandberg.pp.se 1308737SN/A void initSectorTable(int hash_size); 131259SN/A bool open(const std::string &file); 13210905SN/A void save() const; 13310905SN/A void save(const std::string &file) const; 1342SN/A void writeback(); 13510905SN/A 13611168SN/A void serialize(CheckpointOut &cp) const override; 13711168SN/A void unserialize(CheckpointIn &cp) override; 1382SN/A 13911169SN/A std::streampos size() const override; 1402SN/A 14111169SN/A std::streampos read(uint8_t *data, std::streampos offset) const override; 14211169SN/A std::streampos write(const uint8_t *data, std::streampos offset) override; 1432SN/A}; 1442SN/A 1459554SN/Avoid SafeRead(std::ifstream &stream, void *data, int count); 1469554SN/A 1479554SN/Atemplate<class T> 1489554SN/Avoid SafeRead(std::ifstream &stream, T &data); 1499554SN/A 1509554SN/Atemplate<class T> 1519554SN/Avoid SafeReadSwap(std::ifstream &stream, T &data); 1529554SN/A 1539554SN/Avoid SafeWrite(std::ofstream &stream, const void *data, int count); 1549554SN/A 1559554SN/Atemplate<class T> 1569554SN/Avoid SafeWrite(std::ofstream &stream, const T &data); 1579554SN/A 1589554SN/Atemplate<class T> 1599554SN/Avoid SafeWriteSwap(std::ofstream &stream, const T &data); 1609554SN/A 16111264Sandreas.sandberg@arm.com#endif // __DEV_STORAGE_DISK_IMAGE_HH__ 162