1 /**
2  * Copyright © 2024 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include "updater.hpp"
19 
20 namespace aeiUpdater
21 {
22 /**
23  * @class AeiUpdater
24  * @brief A class to handle firmware updates for the AEI PSUs.
25  *
26  * This class provides methods to update firmware by writing ISP keys,
27  * validating firmware files, and performing I2C communications. It includes
28  * functions to manage the update process, including downloading firmware blocks
29  * and verifying the update status.
30  */
31 class AeiUpdater : public updater::Updater
32 {
33   public:
34     AeiUpdater() = delete;
35     AeiUpdater(const AeiUpdater&) = delete;
36     AeiUpdater(AeiUpdater&&) = delete;
37     AeiUpdater& operator=(const AeiUpdater&) = delete;
38     AeiUpdater& operator=(AeiUpdater&&) = delete;
39     ~AeiUpdater() = default;
40 
41     /**
42      * @brief Constructor for the AeiUpdater class.
43      *
44      * @param psuInventoryPath The path to the PSU's inventory in the system.
45      * @param devPath The device path for the PSU.
46      * @param imageDir The directory containing the firmware image files.
47      */
AeiUpdater(const std::string & psuInventoryPath,const std::string & devPath,const std::string & imageDir)48     AeiUpdater(const std::string& psuInventoryPath, const std::string& devPath,
49                const std::string& imageDir) :
50         Updater(psuInventoryPath, devPath, imageDir)
51     {}
52 
53     /**
54      * @brief Initiates the firmware update process.
55      *
56      * @return Status code 0 for success or 1 for failure.
57      */
58     int doUpdate() override;
59 
60   private:
61     /**
62      * @brief Writes an ISP (In-System Programming) key to initiate the update.
63      *
64      * @return True if successful, false otherwise.
65      */
66     bool writeIspKey();
67 
68     /**
69      * @brief Writes the mode required for ISP to start firmware programming.
70      *
71      * @return True if successful, false otherwise.
72      */
73     bool writeIspMode();
74 
75     /**
76      * @brief Resets the ISP status to prepare for a firmware update.
77      *
78      * @return True if successful, false otherwise.
79      */
80     bool writeIspStatusReset();
81 
82     /**
83      * @brief Retrieves the path to the firmware file.
84      *
85      * @return The file path of the firmware.
86      */
87     std::string getFirmwarePath();
88 
89     /**
90      * @brief Validates the firmware file.
91      *
92      * @param fspath The file path to validate.
93      * @return True if the file is valid, false otherwise.
94      */
95     bool isFirmwareFileValid(const std::string& fspath);
96 
97     /**
98      * @brief Opens a firmware file in binary mode.
99      *
100      * @param fspath The path to the firmware file.
101      * @return A file stream to read the firmware
102      * file.
103      */
104     std::unique_ptr<std::ifstream> openFirmwareFile(const std::string& fspath);
105 
106     /**
107      * @brief Reads a block of firmware data from the file.
108      *
109      * @param file The input file stream from which to read data.
110      * @param bytesToRead The number of bytes to read.
111      * @return A vector containing the firmware block.
112      */
113     std::vector<uint8_t> readFirmwareBlock(std::ifstream& file,
114                                            const size_t& bytesToRead);
115 
116     /**
117      * @brief Prepares an ISP_MEMORY  command block by processing the firmware
118      * data block.
119      *
120      * @param dataBlockRead The firmware data block read from the file.
121      * @return The prepared command block.
122      */
123     std::vector<uint8_t>
124         prepareCommandBlock(const std::vector<uint8_t>& dataBlockRead);
125 
126     /**
127      * @brief Initiates a reboot of the ISP to apply new firmware.
128      */
129     void ispReboot();
130 
131     /**
132      * @brief Reads the reboot status from the ISP.
133      *
134      * @return True if the reboot status indicates success, false
135      * otherwise.
136      */
137     bool ispReadRebootStatus();
138 
139     /**
140      * @brief Pointer to the I2C interface for communication
141      *
142      * This pointer is not owned by the class. The caller is responsible for
143      * ensuring that 'i2cInterface'  remains valid for the lifetime of this
144      * object. Ensure to check this pointer for null before use.
145      */
146     i2c::I2CInterface* i2cInterface;
147 
148     /**
149      * @brief Stores byte-swapped indices for command processing
150      */
151     std::vector<uint8_t> byteSwappedIndex;
152 };
153 } // namespace aeiUpdater
154