blob: 4a9e6fc934720d0e1905ca8ec578ac0f60de4e0a [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
38};
39
40class 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
Alexander Hansenac4fdd02025-05-20 12:54:50 +020084 bool isSPIControllerBound();
Alexander Hansenf2c95a02024-11-26 11:16:44 +010085 bool isSPIFlashBound();
86
87 // @description preconditions:
88 // - host is powered off
89 // @returns true on success
90 sdbusplus::async::task<bool> writeSPIFlash(const uint8_t* image,
91 size_t image_size);
92
93 // @description preconditions:
94 // - host is powered off
95 // - gpio / mux is set
96 // - spi device is bound to the driver
97 // we write the flat image here
98 // @param image the component image
99 // @param image_size size of 'image'
100 // @returns true on success
101 sdbusplus::async::task<bool> writeSPIFlashDefault(const uint8_t* image,
102 size_t image_size);
103
104 // @description preconditions:
105 // - host is powered off
106 // - gpio / mux is set
107 // - spi device is bound to the driver
108 // we use 'flashrom' here to write the image since it can deal with
109 // Intel Flash Descriptor
110 // @param image the component image
111 // @param image_size size of 'image'
112 // @returns 0 on success
113 sdbusplus::async::task<int> writeSPIFlashWithFlashrom(
114 const uint8_t* image, size_t image_size) const;
115
116 // @returns nullopt on error
117 std::optional<std::string> getMTDDevicePath() const;
118};