blob: 201be8fe18beed6ae4562949876bf9638ca7f23e [file] [log] [blame]
Brandon Kim55dcada2022-03-09 02:18:01 -08001#include "pci_handler.hpp"
2
3#include <fcntl.h>
4#include <fmt/format.h>
5
6#include <stdplus/fd/managed.hpp>
7#include <stdplus/fd/mmap.hpp>
8
9#include <cstdint>
10#include <cstring>
11#include <memory>
12#include <span>
13#include <vector>
14
15namespace bios_bmc_smm_error_logger
16{
17
18PciDataHandler::PciDataHandler(uint32_t regionAddress, size_t regionSize,
19 std::unique_ptr<stdplus::fd::Fd> fd) :
Edward Lee3cbb6ef2023-03-15 17:08:40 +000020 regionSize(regionSize),
21 fd(std::move(fd)),
Brandon Kim55dcada2022-03-09 02:18:01 -080022 mmap(stdplus::fd::MMap(
23 *this->fd, regionSize, stdplus::fd::ProtFlags{PROT_READ | PROT_WRITE},
24 stdplus::fd::MMapFlags{stdplus::fd::MMapAccess::Shared}, regionAddress))
25{}
26
27std::vector<uint8_t> PciDataHandler::read(const uint32_t offset,
28 const uint32_t length)
29{
30 if (offset > regionSize || length == 0)
31 {
32 fmt::print(stderr,
33 "[read] Offset [{}] was bigger than regionSize [{}] "
34 "OR length [{}] was equal to 0\n",
35 offset, regionSize, length);
36 return {};
37 }
38
39 // Read up to regionSize in case the offset + length overflowed
Patrick Williamsb8125762023-05-10 07:51:26 -050040 uint32_t finalLength = (offset + length < regionSize) ? length
41 : regionSize - offset;
Brandon Kim55dcada2022-03-09 02:18:01 -080042 std::vector<uint8_t> results(finalLength);
43
44 std::memcpy(results.data(), mmap.get().data() + offset, finalLength);
45 return results;
46}
47
48uint32_t PciDataHandler::write(const uint32_t offset,
49 const std::span<const uint8_t> bytes)
50{
51 const size_t length = bytes.size();
52 if (offset > regionSize || length == 0)
53 {
54 fmt::print(stderr,
55 "[write] Offset [{}] was bigger than regionSize [{}] "
56 "OR length [{}] was equal to 0\n",
57 offset, regionSize, length);
58 return 0;
59 }
60
61 // Write up to regionSize in case the offset + length overflowed
Patrick Williamsb8125762023-05-10 07:51:26 -050062 uint16_t finalLength = (offset + length < regionSize) ? length
63 : regionSize - offset;
Brandon Kim55dcada2022-03-09 02:18:01 -080064 std::memcpy(mmap.get().data() + offset, bytes.data(), finalLength);
65 return finalLength;
66}
67
68uint32_t PciDataHandler::getMemoryRegionSize()
69{
70 return regionSize;
71}
72
73} // namespace bios_bmc_smm_error_logger