Faisal Awada | 5a582d3 | 2024-11-15 14:11:44 -0600 | [diff] [blame] | 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 | */ |
| 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 | * |
Faisal Awada | 8dca507 | 2024-11-23 08:07:38 -0600 | [diff] [blame] | 56 | * @return Status code 0 for success or 1 for failure. |
Faisal Awada | 5a582d3 | 2024-11-15 14:11:44 -0600 | [diff] [blame] | 57 | */ |
| 58 | int doUpdate() override; |
| 59 | |
| 60 | private: |
| 61 | /** |
| 62 | * @brief Writes an ISP (In-System Programming) key to initiate the update. |
| 63 | * |
Faisal Awada | 8dca507 | 2024-11-23 08:07:38 -0600 | [diff] [blame] | 64 | * @return True if successful, false otherwise. |
Faisal Awada | 5a582d3 | 2024-11-15 14:11:44 -0600 | [diff] [blame] | 65 | */ |
| 66 | bool writeIspKey(); |
| 67 | |
| 68 | /** |
| 69 | * @brief Writes the mode required for ISP to start firmware programming. |
| 70 | * |
Faisal Awada | 8dca507 | 2024-11-23 08:07:38 -0600 | [diff] [blame] | 71 | * @return True if successful, false otherwise. |
Faisal Awada | 5a582d3 | 2024-11-15 14:11:44 -0600 | [diff] [blame] | 72 | */ |
| 73 | bool writeIspMode(); |
| 74 | |
| 75 | /** |
| 76 | * @brief Resets the ISP status to prepare for a firmware update. |
| 77 | * |
Faisal Awada | 8dca507 | 2024-11-23 08:07:38 -0600 | [diff] [blame] | 78 | * @return True if successful, false otherwise. |
Faisal Awada | 5a582d3 | 2024-11-15 14:11:44 -0600 | [diff] [blame] | 79 | */ |
| 80 | bool writeIspStatusReset(); |
| 81 | |
| 82 | /** |
Faisal Awada | 8dca507 | 2024-11-23 08:07:38 -0600 | [diff] [blame] | 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 | /** |
Faisal Awada | 5ace9fb | 2025-01-07 13:26:25 -0600 | [diff] [blame^] | 117 | * @brief Prepares an ISP_MEMORY command block by processing the firmware |
Faisal Awada | 8dca507 | 2024-11-23 08:07:38 -0600 | [diff] [blame] | 118 | * data block. |
| 119 | * |
| 120 | * @param dataBlockRead The firmware data block read from the file. |
Faisal Awada | 8dca507 | 2024-11-23 08:07:38 -0600 | [diff] [blame] | 121 | */ |
Faisal Awada | 5ace9fb | 2025-01-07 13:26:25 -0600 | [diff] [blame^] | 122 | void prepareCommandBlock(const std::vector<uint8_t>& dataBlockRead); |
| 123 | |
| 124 | /** |
| 125 | * @brief Performs firmware update for the power supply unit (PSU) |
| 126 | * |
| 127 | * This function retrieves the firmware file from appropriate path, |
| 128 | * validate existence of the file and initiate the update process. |
| 129 | * The process includes processing the data into blocks, and writes |
| 130 | * these blocks to the PSU. |
| 131 | * |
| 132 | * @return True if firmware download was successful, false otherwise. |
| 133 | */ |
| 134 | bool downloadPsuFirmware(); |
| 135 | |
| 136 | /** |
| 137 | * @brief Performs an I2C write and read with retry logic. |
| 138 | * |
| 139 | * This function attempts to write a command block to PSU register |
| 140 | * and read next block sequence and CML write status. If the block |
| 141 | * sequence number the same as written block, then retry to write |
| 142 | * same block again. |
| 143 | * |
| 144 | * @param regAddr The register address to write to. |
| 145 | * @param expectedReadSize The size of data read from i2c device. |
| 146 | * @param readData The buffer to store read data. |
| 147 | * @param retries The number of retry attempts allowed. |
| 148 | * @param delayTime The delay time between retries. |
| 149 | * @return True if the operation is successful, false otherwise. |
| 150 | */ |
| 151 | bool performI2cWriteReadWithRetries( |
| 152 | uint8_t regAddr, const uint8_t expectedReadSize, uint8_t* readData, |
| 153 | const int retries, const int delayTime); |
| 154 | |
| 155 | /** |
| 156 | * @brief Performs a single I2C write and read without retry logic. |
| 157 | * |
| 158 | * @param regAddr The register address to write to. |
| 159 | * @param readReplySize The size of data read from i2c device. |
| 160 | * @param readData The buffer to store read data. |
| 161 | * @param delayTime The delay time between write and read operations. |
| 162 | */ |
| 163 | void performI2cWriteRead(uint8_t regAddr, uint8_t& readReplySize, |
| 164 | uint8_t* readData, const int& delayTime); |
| 165 | /** |
| 166 | * @brief Verifies the status of the firmware download. |
| 167 | * |
| 168 | * @return True if the download status is verified as successful, false |
| 169 | * otherwise. |
| 170 | */ |
| 171 | bool verifyDownloadFWStatus(); |
Faisal Awada | 8dca507 | 2024-11-23 08:07:38 -0600 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * @brief Initiates a reboot of the ISP to apply new firmware. |
| 175 | */ |
| 176 | void ispReboot(); |
| 177 | |
| 178 | /** |
| 179 | * @brief Reads the reboot status from the ISP. |
| 180 | * |
| 181 | * @return True if the reboot status indicates success, false |
| 182 | * otherwise. |
| 183 | */ |
| 184 | bool ispReadRebootStatus(); |
| 185 | |
| 186 | /** |
Faisal Awada | 5a582d3 | 2024-11-15 14:11:44 -0600 | [diff] [blame] | 187 | * @brief Pointer to the I2C interface for communication |
| 188 | * |
| 189 | * This pointer is not owned by the class. The caller is responsible for |
| 190 | * ensuring that 'i2cInterface' remains valid for the lifetime of this |
| 191 | * object. Ensure to check this pointer for null before use. |
| 192 | */ |
| 193 | i2c::I2CInterface* i2cInterface; |
| 194 | |
| 195 | /** |
| 196 | * @brief Stores byte-swapped indices for command processing |
| 197 | */ |
| 198 | std::vector<uint8_t> byteSwappedIndex; |
Faisal Awada | 5ace9fb | 2025-01-07 13:26:25 -0600 | [diff] [blame^] | 199 | |
| 200 | /** |
| 201 | * @brief Command block used for writing data to the device |
| 202 | */ |
| 203 | std::vector<uint8_t> cmdBlockWrite; |
Faisal Awada | 5a582d3 | 2024-11-15 14:11:44 -0600 | [diff] [blame] | 204 | }; |
| 205 | } // namespace aeiUpdater |