buffer: Implement "readBufferHeader" and enforce endianness

Add buffer header reader and a getter API for the cachedBufferHeader for
testing.

Also update CircularBufferHeader with boost endian library.

Tested: Unit Tested

Signed-off-by: Brandon Kim <brandonkim@google.com>
Change-Id: I3afaa96f1fab2dd160058e8127dcba494efe3ce7
diff --git a/src/buffer.cpp b/src/buffer.cpp
index a0491eb..a2b8ee4 100644
--- a/src/buffer.cpp
+++ b/src/buffer.cpp
@@ -4,6 +4,10 @@
 
 #include <fmt/format.h>
 
+#include <boost/endian/arithmetic.hpp>
+#include <boost/endian/conversion.hpp>
+
+#include <algorithm>
 #include <array>
 #include <cstdint>
 #include <memory>
@@ -32,10 +36,16 @@
 
     // Create an initial buffer header and write to it
     struct CircularBufferHeader initializationHeader = {};
-    initializationHeader.bmcInterfaceVersion = bmcInterfaceVersion;
-    initializationHeader.queueSize = queueSize;
-    initializationHeader.ueRegionSize = ueRegionSize;
-    initializationHeader.magicNumber = magicNumber;
+    initializationHeader.bmcInterfaceVersion =
+        boost::endian::native_to_little(bmcInterfaceVersion);
+    initializationHeader.queueSize = boost::endian::native_to_little(queueSize);
+    initializationHeader.ueRegionSize =
+        boost::endian::native_to_little(ueRegionSize);
+    std::transform(magicNumber.begin(), magicNumber.end(),
+                   initializationHeader.magicNumber.begin(),
+                   [](uint32_t number) -> little_uint32_t {
+                       return boost::endian::native_to_little(number);
+                   });
 
     uint8_t* initializationHeaderPtr =
         reinterpret_cast<uint8_t*>(&initializationHeader);
@@ -52,4 +62,26 @@
     }
 }
 
+void BufferImpl::readBufferHeader()
+{
+    size_t headerSize = sizeof(struct CircularBufferHeader);
+    std::vector<uint8_t> bytesRead =
+        dataInterface->read(/* offset */ 0, headerSize);
+
+    if (bytesRead.size() != headerSize)
+    {
+        throw std::runtime_error(
+            fmt::format("Buffer header read only read '{}', expected '{}'",
+                        bytesRead.size(), headerSize));
+    }
+
+    cachedBufferHeader =
+        *reinterpret_cast<CircularBufferHeader*>(bytesRead.data());
+};
+
+struct CircularBufferHeader BufferImpl::getCachedBufferHeader() const
+{
+    return cachedBufferHeader;
+}
+
 } // namespace bios_bmc_smm_error_logger