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