blob: 25a53d12ba7daea66b602edab3d7fdfb5bed1f85 [file] [log] [blame]
Alexander Hansenf2c95a02024-11-26 11:16:44 +01001#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
14class SPIDevice;
15
16using namespace phosphor::software;
17using namespace phosphor::software::manager;
18using namespace phosphor::notify::watch;
19
20using NotifyWatchIntf = phosphor::notify::watch::NotifyWatch<SPIDevice>;
21
22const std::string biosVersionDirPath = "/var/bios/";
23const std::string biosVersionFilename = "host0_bios_version.txt";
24const std::string biosVersionPath = biosVersionDirPath + biosVersionFilename;
25
26const std::string versionUnknown = "Unknown";
27
28enum FlashLayout
29{
30 flashLayoutFlat,
31 flashLayoutIntelFlashDescriptor,
32};
33
34enum FlashTool
35{
36 flashToolNone, // write directly to the mtd device
37 flashToolFlashrom, // use flashrom, to handle e.g. IFD
Alexander Hansen5db0c6b2025-05-30 11:21:30 +020038 flashToolFlashcp,
Alexander Hansenf2c95a02024-11-26 11:16:44 +010039};
40
41class 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
Alexander Hansenac4fdd02025-05-20 12:54:50 +020085 bool isSPIControllerBound();
Alexander Hansenf2c95a02024-11-26 11:16:44 +010086 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 0 on success
114 sdbusplus::async::task<int> writeSPIFlashWithFlashrom(
115 const uint8_t* image, size_t image_size) const;
116
Alexander Hansen5db0c6b2025-05-30 11:21:30 +0200117 // @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
Alexander Hansenf2c95a02024-11-26 11:16:44 +0100127 // @returns nullopt on error
128 std::optional<std::string> getMTDDevicePath() const;
129};