xref: /openbmc/phosphor-bmc-code-mgmt/bios/spi_device.hpp (revision a2eb951f7384c2b4fa494f90e78295f615c12a56)
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     flashToolFlashcp,
39 };
40 
41 class SPIDevice : public Device, public NotifyWatchIntf
42 {
43   public:
44     using Device::softwareCurrent;
45     SPIDevice(sdbusplus::async::context& ctx, uint64_t spiControllerIndex,
46               uint64_t spiDeviceIndex, bool dryRun,
47               const std::vector<std::string>& gpioLinesIn,
48               const std::vector<uint64_t>& gpioValuesIn, SoftwareConfig& config,
49               SoftwareManager* parent, enum FlashLayout layout,
50               enum FlashTool tool,
51               const std::string& versionDirPath = biosVersionDirPath);
52 
53     sdbusplus::async::task<bool> updateDevice(const uint8_t* image,
54                                               size_t image_size) final;
55 
56     // @returns       the bios version which is externally provided.
57     static std::string getVersion();
58 
59     /** @brief Process async changes to cable configuration */
60     auto processUpdate(std::string versionFileName) -> sdbusplus::async::task<>;
61 
62   private:
63     bool dryRun;
64 
65     std::vector<std::string> gpioLines;
66 
67     std::vector<int> gpioValues;
68 
69     uint64_t spiControllerIndex;
70     uint64_t spiDeviceIndex;
71 
72     // e.g. "1e631000.spi"
73     std::string spiDev;
74 
75     enum FlashLayout layout;
76 
77     enum FlashTool tool;
78 
79     // @returns          true on success
80     sdbusplus::async::task<bool> bindSPIFlash();
81 
82     // @returns          true on success
83     sdbusplus::async::task<bool> unbindSPIFlash();
84 
85     bool isSPIControllerBound();
86     bool isSPIFlashBound();
87 
88     // @description preconditions:
89     // - host is powered off
90     // @returns   true on success
91     sdbusplus::async::task<bool> writeSPIFlash(const uint8_t* image,
92                                                size_t image_size);
93 
94     // @description preconditions:
95     // - host is powered off
96     // - gpio / mux is set
97     // - spi device is bound to the driver
98     // we write the flat image here
99     // @param image           the component image
100     // @param image_size      size of 'image'
101     // @returns               true on success
102     sdbusplus::async::task<bool> writeSPIFlashDefault(const uint8_t* image,
103                                                       size_t image_size);
104 
105     // @description preconditions:
106     // - host is powered off
107     // - gpio / mux is set
108     // - spi device is bound to the driver
109     // we use 'flashrom' here to write the image since it can deal with
110     // Intel Flash Descriptor
111     // @param image           the component image
112     // @param image_size      size of 'image'
113     // @returns               true on success
114     sdbusplus::async::task<bool> writeSPIFlashWithFlashrom(
115         const uint8_t* image, size_t image_size) const;
116 
117     // @description preconditions:
118     // - host is powered off
119     // - gpio / mux is set
120     // - spi device is bound to the driver
121     // @param image           the component image
122     // @param image_size      size of 'image'
123     // @returns               true on success
124     sdbusplus::async::task<bool> writeSPIFlashWithFlashcp(
125         const uint8_t* image, size_t image_size) const;
126 
127     // @returns nullopt on error
128     std::optional<std::string> getMTDDevicePath() const;
129 };
130