xref: /openbmc/phosphor-ipmi-flash/bmc/firmware-handler/lpc_handler.hpp (revision ded66d0f7317f6e43a5301da6c3d9dda80b17250)
1 #pragma once
2 
3 #include "data_handler.hpp"
4 #include "mapper_errors.hpp"
5 #include "window_hw_interface.hpp"
6 
7 #include <cstdint>
8 #include <memory>
9 #include <vector>
10 
11 namespace ipmi_flash
12 {
13 
14 struct LpcRegion
15 {
16     /* Host LPC address where the chunk is to be mapped. */
17     std::uint32_t address;
18 
19     /* Size of the chunk to be mapped. */
20     std::uint32_t length;
21 } __attribute__((packed));
22 
23 /**
24  * Data Handler for configuration the ASPEED LPC memory region, reading and
25  * writing data.
26  */
27 class LpcDataHandler : public DataInterface
28 {
29   public:
30     /**
31      * Create an LpcDataHandler.
32      *
33      * @param[in] mapper - pointer to a mapper implementation to use.
34      */
35     explicit LpcDataHandler(std::unique_ptr<HardwareMapperInterface> mapper) :
36         mapper(std::move(mapper)), initialized(false)
37     {}
38 
39     bool open() override;
40     bool close() override;
41     std::vector<std::uint8_t> copyFrom(std::uint32_t length) override;
42     bool writeMeta(const std::vector<std::uint8_t>& configuration) override;
43     std::vector<std::uint8_t> readMeta() override;
44 
45   private:
46     bool setInitializedAndReturn(bool value)
47     {
48         if (value)
49         {
50             try
51             {
52                 /* Try really opening the map. */
53                 memory = mapper->open();
54             }
55             catch (const MapperException& e)
56             {
57                 std::fprintf(stderr, "received mapper exception: %s\n",
58                              e.what());
59                 return false;
60             }
61         }
62 
63         initialized = value;
64         return value;
65     }
66 
67     std::unique_ptr<HardwareMapperInterface> mapper;
68     bool initialized;
69     /* The LPC Handler does not take ownership of this, in case there's cleanup
70      * required for close()
71      */
72     MemorySet memory = {};
73 
74     /* Offset in reserved memory at which host data arrives. */
75     /* Size of the chunk of the memory region in use by the host (e.g.
76      * mapped over external block mechanism).
77      */
78     WindowMapResult mappingResult = {};
79 };
80 
81 } // namespace ipmi_flash
82