blob: e25216e5ce9c0f71ba22cb8bdfa85844feb3f91d [file] [log] [blame]
#include "io.hpp"
#include "internal/sys.hpp"
#include <fcntl.h>
#include <cstdint>
#include <cstring>
#include <string>
namespace host_tool
{
const std::string DevMemDevice::devMemPath = "/dev/mem";
bool DevMemDevice::write(const std::size_t offset, const std::size_t length,
const void* const source)
{
if (!opened)
{
devMemFd = sys->open(devMemPath.c_str(), O_RDWR);
if (devMemFd < 0)
{
return false;
}
opened = true;
}
// addr, length, prot, flags, fd, offset
devMemMapped =
sys->mmap(0, length, PROT_WRITE, MAP_SHARED, devMemFd, offset);
if (devMemMapped == MAP_FAILED)
{
return false; /* but leave the file open. */
}
/* Copy the bytes. */
std::memcpy(devMemMapped, source, length);
/* Close the map between writes for now. */
sys->munmap(devMemMapped, length);
return true;
}
} // namespace host_tool