2021-05-02 01:10:46 +00:00
|
|
|
//
|
|
|
|
// RawSectorDump.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 01/05/2021.
|
|
|
|
// Copyright © 2021 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef RawSectorDump_h
|
|
|
|
#define RawSectorDump_h
|
|
|
|
|
|
|
|
#include "../MassStorageDevice.hpp"
|
|
|
|
#include "../../FileHolder.hpp"
|
|
|
|
|
2021-05-13 23:11:19 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
2023-05-10 21:02:18 +00:00
|
|
|
namespace Storage::MassStorage {
|
2021-05-02 01:10:46 +00:00
|
|
|
|
2022-08-30 19:51:29 +00:00
|
|
|
template <long sector_size> class RawSectorDump: public MassStorageDevice {
|
2021-05-02 01:10:46 +00:00
|
|
|
public:
|
2022-08-30 19:51:29 +00:00
|
|
|
RawSectorDump(const std::string &file_name, long offset = 0, long length = -1) :
|
|
|
|
file_(file_name),
|
|
|
|
file_size_((length == -1) ? long(file_.stats().st_size) : length),
|
|
|
|
file_start_(offset)
|
|
|
|
{
|
2021-05-02 01:10:46 +00:00
|
|
|
// Is the file a multiple of sector_size bytes in size?
|
2022-08-30 19:51:29 +00:00
|
|
|
if(file_size_ % sector_size) throw std::exception();
|
2021-05-02 01:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* MassStorageDevices overrides. */
|
|
|
|
size_t get_block_size() final {
|
|
|
|
return sector_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t get_number_of_blocks() final {
|
2022-08-30 19:51:29 +00:00
|
|
|
return size_t(file_size_ / sector_size);
|
2021-05-02 01:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint8_t> get_block(size_t address) final {
|
2022-08-30 19:51:29 +00:00
|
|
|
file_.seek(file_start_ + long(address * sector_size), SEEK_SET);
|
2021-05-02 01:10:46 +00:00
|
|
|
return file_.read(sector_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_block(size_t address, const std::vector<uint8_t> &contents) final {
|
|
|
|
assert(contents.size() == sector_size);
|
2022-08-30 19:51:29 +00:00
|
|
|
file_.seek(file_start_ + long(address * sector_size), SEEK_SET);
|
2021-05-02 01:10:46 +00:00
|
|
|
file_.write(contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FileHolder file_;
|
2022-08-30 19:51:29 +00:00
|
|
|
const long file_size_, file_start_;
|
2021-05-02 01:10:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* RawSectorDump_h */
|