xref: /openbmc/phosphor-bmc-code-mgmt/bios/spi_device.hpp (revision 994a77ff25aeb28b2cce7142d081af767f9eb542)
1 #pragma once
2 
3 #include "common/include/NotifyWatch.hpp"
4 #include "common/include/device.hpp"
5 #include "common/include/software.hpp"
6 #include "common/include/software_manager.hpp"
7 
8 #include <sdbusplus/asio/connection.hpp>
9 #include <sdbusplus/asio/object_server.hpp>
10 #include <sdbusplus/async/context.hpp>
11 
12 #include <string>
13 
14 class SPIDevice;
15 
16 using namespace phosphor::software;
17 using namespace phosphor::software::manager;
18 using namespace phosphor::notify::watch;
19 
20 using NotifyWatchIntf = phosphor::notify::watch::NotifyWatch<SPIDevice>;
21 
22 const std::string biosVersionDirPath = "/var/bios/";
23 const std::string biosVersionFilename = "host0_bios_version.txt";
24 const std::string biosVersionPath = biosVersionDirPath + biosVersionFilename;
25 
26 const std::string versionUnknown = "Unknown";
27 
28 enum FlashLayout
29 {
30     flashLayoutFlat,
31     flashLayoutIntelFlashDescriptor,
32 };
33 
34 enum FlashTool
35 {
36     flashToolNone,     // write directly to the mtd device
37     flashToolFlashrom, // use flashrom, to handle e.g. IFD
38 };
39 
40 class SPIDevice : public Device, public NotifyWatchIntf
41 {
42   public:
43     using Device::softwareCurrent;
44     SPIDevice(sdbusplus::async::context& ctx, uint64_t spiControllerIndex,
45               uint64_t spiDeviceIndex, bool dryRun,
46               const std::vector<std::string>& gpioLinesIn,
47               const std::vector<uint64_t>& gpioValuesIn, SoftwareConfig& config,
48               SoftwareManager* parent, enum FlashLayout layout,
49               enum FlashTool tool,
50               const std::string& versionDirPath = biosVersionDirPath);
51 
52     sdbusplus::async::task<bool> updateDevice(const uint8_t* image,
53                                               size_t image_size) final;
54 
55     // @returns       the bios version which is externally provided.
56     static std::string getVersion();
57 
58     /** @brief Process async changes to cable configuration */
59     auto processUpdate(std::string versionFileName) -> sdbusplus::async::task<>;
60 
61   private:
62     bool dryRun;
63 
64     std::vector<std::string> gpioLines;
65 
66     std::vector<int> gpioValues;
67 
68     uint64_t spiControllerIndex;
69     uint64_t spiDeviceIndex;
70 
71     // e.g. "1e631000.spi"
72     std::string spiDev;
73 
74     enum FlashLayout layout;
75 
76     enum FlashTool tool;
77 
78     // @returns          true on success
79     sdbusplus::async::task<bool> bindSPIFlash();
80 
81     // @returns          true on success
82     sdbusplus::async::task<bool> unbindSPIFlash();
83 
84     bool isSPIFlashBound();
85 
86     // @description preconditions:
87     // - host is powered off
88     // @returns   true on success
89     sdbusplus::async::task<bool> writeSPIFlash(const uint8_t* image,
90                                                size_t image_size);
91 
92     // @description preconditions:
93     // - host is powered off
94     // - gpio / mux is set
95     // - spi device is bound to the driver
96     // we write the flat image here
97     // @param image           the component image
98     // @param image_size      size of 'image'
99     // @returns               true on success
100     sdbusplus::async::task<bool> writeSPIFlashDefault(const uint8_t* image,
101                                                       size_t image_size);
102 
103     // @description preconditions:
104     // - host is powered off
105     // - gpio / mux is set
106     // - spi device is bound to the driver
107     // we use 'flashrom' here to write the image since it can deal with
108     // Intel Flash Descriptor
109     // @param image           the component image
110     // @param image_size      size of 'image'
111     // @returns               0 on success
112     sdbusplus::async::task<int> writeSPIFlashWithFlashrom(
113         const uint8_t* image, size_t image_size) const;
114 
115     // @returns nullopt on error
116     std::optional<std::string> getMTDDevicePath() const;
117 };
118