HD.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #include "HD.hh"
  2. #include "FileContext.hh"
  3. #include "FilePool.hh"
  4. #include "DeviceConfig.hh"
  5. #include "CliComm.hh"
  6. #include "HDImageCLI.hh"
  7. #include "MSXMotherBoard.hh"
  8. #include "Reactor.hh"
  9. #include "Display.hh"
  10. #include "GlobalSettings.hh"
  11. #include "MSXException.hh"
  12. #include "HDCommand.hh"
  13. #include "Timer.hh"
  14. #include "serialize.hh"
  15. #include "tiger.hh"
  16. #include "xrange.hh"
  17. #include <cassert>
  18. #include <memory>
  19. namespace openmsx {
  20. using std::string;
  21. HD::HD(const DeviceConfig& config)
  22. : motherBoard(config.getMotherBoard())
  23. , name("hdX")
  24. {
  25. hdInUse = motherBoard.getSharedStuff<HDInUse>("hdInUse");
  26. unsigned id = 0;
  27. while ((*hdInUse)[id]) {
  28. ++id;
  29. if (id == MAX_HD) {
  30. throw MSXException("Too many HDs");
  31. }
  32. }
  33. // for exception safety, set hdInUse only at the end
  34. name[2] = char('a' + id);
  35. // For the initial hd image, savestate should only try exactly this
  36. // (resolved) filename. For user-specified hd images (commandline or
  37. // via hda command) savestate will try to re-resolve the filename.
  38. auto mode = File::NORMAL;
  39. string cliImage = HDImageCLI::getImageForId(id);
  40. if (cliImage.empty()) {
  41. string original = config.getChildData("filename");
  42. string resolved = config.getFileContext().resolveCreate(original);
  43. filename = Filename(resolved);
  44. mode = File::CREATE;
  45. } else {
  46. filename = Filename(cliImage, userFileContext());
  47. }
  48. file = File(filename, mode);
  49. filesize = file.getSize();
  50. if (mode == File::CREATE && filesize == 0) {
  51. // OK, the file was just newly created. Now make sure the file
  52. // is of the right (default) size
  53. file.truncate(size_t(config.getChildDataAsInt("size")) * 1024 * 1024);
  54. filesize = file.getSize();
  55. }
  56. tigerTree = std::make_unique<TigerTree>(
  57. *this, filesize, filename.getResolved());
  58. (*hdInUse)[id] = true;
  59. hdCommand = std::make_unique<HDCommand>(
  60. motherBoard.getCommandController(),
  61. motherBoard.getStateChangeDistributor(),
  62. motherBoard.getScheduler(),
  63. *this,
  64. motherBoard.getReactor().getGlobalSettings().getPowerSetting());
  65. motherBoard.getMSXCliComm().update(CliComm::HARDWARE, name, "add");
  66. }
  67. HD::~HD()
  68. {
  69. motherBoard.getMSXCliComm().update(CliComm::HARDWARE, name, "remove");
  70. unsigned id = name[2] - 'a';
  71. assert((*hdInUse)[id]);
  72. (*hdInUse)[id] = false;
  73. }
  74. void HD::switchImage(const Filename& newFilename)
  75. {
  76. file = File(newFilename);
  77. filename = newFilename;
  78. filesize = file.getSize();
  79. tigerTree = std::make_unique<TigerTree>(*this, filesize,
  80. filename.getResolved());
  81. motherBoard.getMSXCliComm().update(CliComm::MEDIA, getName(),
  82. filename.getResolved());
  83. }
  84. size_t HD::getNbSectorsImpl() const
  85. {
  86. return filesize / sizeof(SectorBuffer);
  87. }
  88. void HD::readSectorImpl(size_t sector, SectorBuffer& buf)
  89. {
  90. file.seek(sector * sizeof(buf));
  91. file.read(&buf, sizeof(buf));
  92. }
  93. void HD::writeSectorImpl(size_t sector, const SectorBuffer& buf)
  94. {
  95. file.seek(sector * sizeof(buf));
  96. file.write(&buf, sizeof(buf));
  97. tigerTree->notifyChange(sector * sizeof(buf), sizeof(buf),
  98. file.getModificationDate());
  99. }
  100. bool HD::isWriteProtectedImpl() const
  101. {
  102. return file.isReadOnly();
  103. }
  104. Sha1Sum HD::getSha1SumImpl(FilePool& filePool)
  105. {
  106. if (hasPatches()) {
  107. return SectorAccessibleDisk::getSha1SumImpl(filePool);
  108. }
  109. return filePool.getSha1Sum(file);
  110. }
  111. void HD::showProgress(size_t position, size_t maxPosition)
  112. {
  113. // only show progress iff:
  114. // - 1 second has passed since last progress OR
  115. // - we reach completion and did progress before (to show the 100%)
  116. // This avoids showing any progress if the operation would take less than 1 second.
  117. auto now = Timer::getTime();
  118. if (((now - lastProgressTime) > 1000000) ||
  119. ((position == maxPosition) && everDidProgress)) {
  120. lastProgressTime = now;
  121. int percentage = int((100 * position) / maxPosition);
  122. motherBoard.getMSXCliComm().printProgress(
  123. "Calculating hash for ", filename.getResolved(),
  124. "... ", percentage, '%');
  125. motherBoard.getReactor().getDisplay().repaintDelayed(0);
  126. everDidProgress = true;
  127. }
  128. }
  129. std::string HD::getTigerTreeHash()
  130. {
  131. lastProgressTime = Timer::getTime();
  132. everDidProgress = false;
  133. auto callback = [this](size_t p, size_t t) { showProgress(p, t); };
  134. return tigerTree->calcHash(callback).toString(); // calls HD::getData()
  135. }
  136. uint8_t* HD::getData(size_t offset, size_t size)
  137. {
  138. assert(size <= 1024);
  139. assert((offset % sizeof(SectorBuffer)) == 0);
  140. assert((size % sizeof(SectorBuffer)) == 0);
  141. struct Work {
  142. char extra; // at least one byte before 'bufs'
  143. // likely here are padding bytes inbetween
  144. SectorBuffer bufs[1024 / sizeof(SectorBuffer)];
  145. };
  146. static Work work; // not reentrant
  147. size_t sector = offset / sizeof(SectorBuffer);
  148. for (auto i : xrange(size / sizeof(SectorBuffer))) {
  149. // This possibly applies IPS patches.
  150. readSector(sector++, work.bufs[i]);
  151. }
  152. return work.bufs[0].raw;
  153. }
  154. bool HD::isCacheStillValid(time_t& cacheTime)
  155. {
  156. time_t fileTime = file.getModificationDate();
  157. bool result = fileTime == cacheTime;
  158. cacheTime = fileTime;
  159. return result;
  160. }
  161. SectorAccessibleDisk* HD::getSectorAccessibleDisk()
  162. {
  163. return this;
  164. }
  165. const std::string& HD::getContainerName() const
  166. {
  167. return getName();
  168. }
  169. bool HD::diskChanged()
  170. {
  171. return false; // TODO not implemented
  172. }
  173. int HD::insertDisk(std::string_view newFilename)
  174. {
  175. try {
  176. switchImage(Filename(string(newFilename)));
  177. return 0;
  178. } catch (MSXException&) {
  179. return -1;
  180. }
  181. }
  182. // version 1: initial version
  183. // version 2: replaced 'checksum'(=sha1) with 'tthsum`
  184. template<typename Archive>
  185. void HD::serialize(Archive& ar, unsigned version)
  186. {
  187. Filename tmp = file.is_open() ? filename : Filename();
  188. ar.serialize("filename", tmp);
  189. if (ar.isLoader()) {
  190. if (tmp.empty()) {
  191. // Lazily open file specified in config. And close if
  192. // it was already opened (in the constructor). The
  193. // latter can occur in the following scenario:
  194. // - The hd image doesn't exist yet
  195. // - Reverse creates savestates, these still have
  196. // tmp="" (because file=nullptr)
  197. // - At some later point the hd image gets created
  198. // (e.g. on first access to the image)
  199. // - Now reverse to some point in EmuTime before the
  200. // first disk access
  201. // - The loadstate re-constructs this HD object, but
  202. // because the hd image does exist now, it gets
  203. // opened in the constructor (file!=nullptr).
  204. // - So to get in the same state as the initial
  205. // savestate we again close the file. Otherwise the
  206. // checksum-check code below goes wrong.
  207. file.close();
  208. } else {
  209. tmp.updateAfterLoadState();
  210. if (filename != tmp) switchImage(tmp);
  211. assert(file.is_open());
  212. }
  213. }
  214. // store/check checksum
  215. if (file.is_open()) {
  216. bool mismatch = false;
  217. if (ar.versionAtLeast(version, 2)) {
  218. // use tiger-tree-hash
  219. string oldTiger = ar.isLoader() ? string{} : getTigerTreeHash();
  220. ar.serialize("tthsum", oldTiger);
  221. if (ar.isLoader()) {
  222. string newTiger = getTigerTreeHash();
  223. mismatch = oldTiger != newTiger;
  224. }
  225. } else {
  226. // use sha1
  227. auto& filepool = motherBoard.getReactor().getFilePool();
  228. Sha1Sum oldChecksum;
  229. if (!ar.isLoader()) {
  230. oldChecksum = getSha1Sum(filepool);
  231. }
  232. string oldChecksumStr = oldChecksum.empty()
  233. ? string{}
  234. : oldChecksum.toString();
  235. ar.serialize("checksum", oldChecksumStr);
  236. oldChecksum = oldChecksumStr.empty()
  237. ? Sha1Sum()
  238. : Sha1Sum(oldChecksumStr);
  239. if (ar.isLoader()) {
  240. Sha1Sum newChecksum = getSha1Sum(filepool);
  241. mismatch = oldChecksum != newChecksum;
  242. }
  243. }
  244. if (ar.isLoader() && mismatch) {
  245. motherBoard.getMSXCliComm().printWarning(
  246. "The content of the harddisk ",
  247. tmp.getResolved(),
  248. " has changed since the time this savestate was "
  249. "created. This might result in emulation problems "
  250. "or even diskcorruption. To prevent the latter, "
  251. "the harddisk is now write-protected.");
  252. forceWriteProtect();
  253. }
  254. }
  255. }
  256. INSTANTIATE_SERIALIZE_METHODS(HD);
  257. } // namespace openmsx