Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 1 | #include "inband_code_update.hpp" |
| 2 | |
Sagar Srinivas | 78a225a | 2020-08-27 00:52:20 -0500 | [diff] [blame] | 3 | #include "libpldmresponder/pdr.hpp" |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 4 | #include "oem_ibm_handler.hpp" |
| 5 | #include "xyz/openbmc_project/Common/error.hpp" |
| 6 | |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 7 | #include <arpa/inet.h> |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 8 | #include <libpldm/entity.h> |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 9 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 10 | #include <phosphor-logging/lg2.hpp> |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 11 | #include <sdbusplus/server.hpp> |
| 12 | #include <xyz/openbmc_project/Dump/NewDump/server.hpp> |
| 13 | |
| 14 | #include <exception> |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 15 | #include <fstream> |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 16 | |
| 17 | PHOSPHOR_LOG2_USING; |
| 18 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 19 | namespace pldm |
| 20 | { |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 21 | using namespace utils; |
| 22 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 23 | namespace responder |
| 24 | { |
| 25 | using namespace oem_ibm_platform; |
| 26 | |
Adriana Kobylak | fa810d7 | 2020-10-16 16:27:28 -0500 | [diff] [blame] | 27 | /** @brief Directory where the lid files without a header are stored */ |
| 28 | auto lidDirPath = fs::path(LID_STAGING_DIR) / "lid"; |
| 29 | |
Adriana Kobylak | a1f158c | 2020-11-09 12:47:29 -0600 | [diff] [blame] | 30 | /** @brief Directory where the image files are stored as they are built */ |
| 31 | auto imageDirPath = fs::path(LID_STAGING_DIR) / "image"; |
| 32 | |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 33 | /** @brief Directory where the code update tarball files are stored */ |
| 34 | auto updateDirPath = fs::path(LID_STAGING_DIR) / "update"; |
| 35 | |
Adriana Kobylak | a1f158c | 2020-11-09 12:47:29 -0600 | [diff] [blame] | 36 | /** @brief The file name of the code update tarball */ |
| 37 | constexpr auto tarImageName = "image.tar"; |
| 38 | |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 39 | /** @brief The file name of the hostfw image */ |
Adriana Kobylak | 131327e | 2021-03-10 18:45:24 +0000 | [diff] [blame] | 40 | constexpr auto hostfwImageName = "image-hostfw"; |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 41 | |
Adriana Kobylak | a1f158c | 2020-11-09 12:47:29 -0600 | [diff] [blame] | 42 | /** @brief The path to the code update tarball file */ |
| 43 | auto tarImagePath = fs::path(imageDirPath) / tarImageName; |
| 44 | |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 45 | /** @brief The path to the hostfw image */ |
| 46 | auto hostfwImagePath = fs::path(imageDirPath) / hostfwImageName; |
| 47 | |
| 48 | /** @brief The path to the tarball file expected by the phosphor software |
| 49 | * manager */ |
| 50 | auto updateImagePath = fs::path("/tmp/images") / tarImageName; |
| 51 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 52 | std::string CodeUpdate::fetchCurrentBootSide() |
| 53 | { |
| 54 | return currBootSide; |
| 55 | } |
| 56 | |
| 57 | std::string CodeUpdate::fetchNextBootSide() |
| 58 | { |
| 59 | return nextBootSide; |
| 60 | } |
| 61 | |
| 62 | int CodeUpdate::setCurrentBootSide(const std::string& currSide) |
| 63 | { |
| 64 | currBootSide = currSide; |
| 65 | return PLDM_SUCCESS; |
| 66 | } |
| 67 | |
| 68 | int CodeUpdate::setNextBootSide(const std::string& nextSide) |
| 69 | { |
| 70 | nextBootSide = nextSide; |
| 71 | std::string objPath{}; |
| 72 | if (nextBootSide == currBootSide) |
| 73 | { |
| 74 | objPath = runningVersion; |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | objPath = nonRunningVersion; |
| 79 | } |
| 80 | if (objPath.empty()) |
| 81 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 82 | error("no nonRunningVersion present"); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 83 | return PLDM_PLATFORM_INVALID_STATE_VALUE; |
| 84 | } |
| 85 | |
| 86 | pldm::utils::DBusMapping dbusMapping{objPath, redundancyIntf, "Priority", |
| 87 | "uint8_t"}; |
| 88 | uint8_t val = 0; |
| 89 | pldm::utils::PropertyValue value = static_cast<uint8_t>(val); |
| 90 | try |
| 91 | { |
| 92 | dBusIntf->setDbusProperty(dbusMapping, value); |
| 93 | } |
| 94 | catch (const std::exception& e) |
| 95 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 96 | error( |
| 97 | "failed to set the next boot side to {OBJ_PATH} ERROR={ERR_EXCEP}", |
| 98 | "OBJ_PATH", objPath.c_str(), "ERR_EXCEP", e.what()); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 99 | return PLDM_ERROR; |
| 100 | } |
| 101 | return PLDM_SUCCESS; |
| 102 | } |
| 103 | |
Sagar Srinivas | cfdbca7 | 2020-09-22 10:03:35 -0500 | [diff] [blame] | 104 | int CodeUpdate::setRequestedApplyTime() |
| 105 | { |
| 106 | int rc = PLDM_SUCCESS; |
| 107 | pldm::utils::PropertyValue value = |
| 108 | "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset"; |
| 109 | DBusMapping dbusMapping; |
| 110 | dbusMapping.objectPath = "/xyz/openbmc_project/software/apply_time"; |
| 111 | dbusMapping.interface = "xyz.openbmc_project.Software.ApplyTime"; |
| 112 | dbusMapping.propertyName = "RequestedApplyTime"; |
| 113 | dbusMapping.propertyType = "string"; |
| 114 | try |
| 115 | { |
| 116 | pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value); |
| 117 | } |
| 118 | catch (const std::exception& e) |
| 119 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 120 | error("Failed To set RequestedApplyTime property ERROR={ERR_EXCEP}", |
| 121 | "ERR_EXCEP", e.what()); |
Sagar Srinivas | cfdbca7 | 2020-09-22 10:03:35 -0500 | [diff] [blame] | 122 | rc = PLDM_ERROR; |
| 123 | } |
| 124 | return rc; |
| 125 | } |
| 126 | |
| 127 | int CodeUpdate::setRequestedActivation() |
| 128 | { |
| 129 | int rc = PLDM_SUCCESS; |
| 130 | pldm::utils::PropertyValue value = |
| 131 | "xyz.openbmc_project.Software.Activation.RequestedActivations.Active"; |
| 132 | DBusMapping dbusMapping; |
| 133 | dbusMapping.objectPath = newImageId; |
| 134 | dbusMapping.interface = "xyz.openbmc_project.Software.Activation"; |
| 135 | dbusMapping.propertyName = "RequestedActivation"; |
| 136 | dbusMapping.propertyType = "string"; |
| 137 | try |
| 138 | { |
| 139 | pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value); |
| 140 | } |
| 141 | catch (const std::exception& e) |
| 142 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 143 | error("Failed To set RequestedActivation property ERROR={ERR_EXCEP}", |
| 144 | "ERR_EXCEP", e.what()); |
Sagar Srinivas | cfdbca7 | 2020-09-22 10:03:35 -0500 | [diff] [blame] | 145 | rc = PLDM_ERROR; |
| 146 | } |
Sagar Srinivas | cfdbca7 | 2020-09-22 10:03:35 -0500 | [diff] [blame] | 147 | return rc; |
| 148 | } |
| 149 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 150 | void CodeUpdate::setVersions() |
| 151 | { |
| 152 | static constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper"; |
| 153 | static constexpr auto functionalObjPath = |
| 154 | "/xyz/openbmc_project/software/functional"; |
| 155 | static constexpr auto activeObjPath = |
| 156 | "/xyz/openbmc_project/software/active"; |
| 157 | static constexpr auto propIntf = "org.freedesktop.DBus.Properties"; |
| 158 | |
| 159 | auto& bus = dBusIntf->getBus(); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 160 | try |
| 161 | { |
| 162 | auto method = bus.new_method_call(mapperService, functionalObjPath, |
| 163 | propIntf, "Get"); |
| 164 | method.append("xyz.openbmc_project.Association", "endpoints"); |
| 165 | std::variant<std::vector<std::string>> paths; |
| 166 | |
| 167 | auto reply = bus.call(method); |
| 168 | reply.read(paths); |
| 169 | |
| 170 | runningVersion = std::get<std::vector<std::string>>(paths)[0]; |
| 171 | |
| 172 | auto method1 = |
| 173 | bus.new_method_call(mapperService, activeObjPath, propIntf, "Get"); |
| 174 | method1.append("xyz.openbmc_project.Association", "endpoints"); |
| 175 | |
| 176 | auto reply1 = bus.call(method1); |
| 177 | reply1.read(paths); |
| 178 | for (const auto& path : std::get<std::vector<std::string>>(paths)) |
| 179 | { |
| 180 | if (path != runningVersion) |
| 181 | { |
| 182 | nonRunningVersion = path; |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | catch (const std::exception& e) |
| 188 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 189 | error( |
| 190 | "failed to make a d-bus call to Object Mapper Association, ERROR={ERR_EXCEP}", |
| 191 | "ERR_EXCEP", e.what()); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 192 | return; |
| 193 | } |
| 194 | |
| 195 | using namespace sdbusplus::bus::match::rules; |
| 196 | captureNextBootSideChange.push_back( |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 197 | std::make_unique<sdbusplus::bus::match_t>( |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 198 | pldm::utils::DBusHandler::getBus(), |
| 199 | propertiesChanged(runningVersion, redundancyIntf), |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 200 | [this](sdbusplus::message_t& msg) { |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 201 | DbusChangedProps props; |
| 202 | std::string iface; |
| 203 | msg.read(iface, props); |
| 204 | processPriorityChangeNotification(props); |
| 205 | })); |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 206 | fwUpdateMatcher.push_back(std::make_unique<sdbusplus::bus::match_t>( |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 207 | pldm::utils::DBusHandler::getBus(), |
| 208 | "interface='org.freedesktop.DBus.ObjectManager',type='signal'," |
| 209 | "member='InterfacesAdded',path='/xyz/openbmc_project/software'", |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 210 | [this](sdbusplus::message_t& msg) { |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 211 | DBusInterfaceAdded interfaces; |
| 212 | sdbusplus::message::object_path path; |
| 213 | msg.read(path, interfaces); |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 214 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 215 | for (auto& interface : interfaces) |
| 216 | { |
| 217 | if (interface.first == |
| 218 | "xyz.openbmc_project.Software.Activation") |
| 219 | { |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 220 | auto imageInterface = |
| 221 | "xyz.openbmc_project.Software.Activation"; |
| 222 | auto imageObjPath = path.str.c_str(); |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 223 | |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 224 | try |
| 225 | { |
| 226 | auto propVal = dBusIntf->getDbusPropertyVariant( |
| 227 | imageObjPath, "Activation", imageInterface); |
| 228 | const auto& imageProp = std::get<std::string>(propVal); |
| 229 | if (imageProp == "xyz.openbmc_project.Software." |
| 230 | "Activation.Activations.Ready" && |
| 231 | isCodeUpdateInProgress()) |
| 232 | { |
| 233 | newImageId = path.str; |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 234 | if (!imageActivationMatch) |
| 235 | { |
| 236 | imageActivationMatch = std::make_unique< |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 237 | sdbusplus::bus::match_t>( |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 238 | pldm::utils::DBusHandler::getBus(), |
| 239 | propertiesChanged(newImageId, |
| 240 | "xyz.openbmc_project." |
| 241 | "Software.Activation"), |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 242 | [this](sdbusplus::message_t& msg) { |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 243 | DbusChangedProps props; |
| 244 | std::string iface; |
| 245 | msg.read(iface, props); |
| 246 | const auto itr = |
| 247 | props.find("Activation"); |
| 248 | if (itr != props.end()) |
| 249 | { |
| 250 | PropertyValue value = itr->second; |
| 251 | auto propVal = |
| 252 | std::get<std::string>(value); |
| 253 | if (propVal == |
| 254 | "xyz.openbmc_project.Software." |
| 255 | "Activation.Activations.Active") |
| 256 | { |
| 257 | CodeUpdateState state = |
| 258 | CodeUpdateState::END; |
| 259 | setCodeUpdateProgress(false); |
| 260 | auto sensorId = |
| 261 | getFirmwareUpdateSensor(); |
| 262 | sendStateSensorEvent( |
| 263 | sensorId, |
| 264 | PLDM_STATE_SENSOR_STATE, 0, |
| 265 | uint8_t(state), |
| 266 | uint8_t(CodeUpdateState:: |
| 267 | START)); |
| 268 | newImageId.clear(); |
| 269 | } |
| 270 | else if (propVal == |
| 271 | "xyz.openbmc_project." |
| 272 | "Software.Activation." |
| 273 | "Activations.Failed" || |
| 274 | propVal == |
| 275 | "xyz.openbmc_" |
| 276 | "project.Software." |
| 277 | "Activation." |
| 278 | "Activations." |
| 279 | "Invalid") |
| 280 | { |
| 281 | CodeUpdateState state = |
| 282 | CodeUpdateState::FAIL; |
| 283 | setCodeUpdateProgress(false); |
| 284 | auto sensorId = |
| 285 | getFirmwareUpdateSensor(); |
| 286 | sendStateSensorEvent( |
| 287 | sensorId, |
| 288 | PLDM_STATE_SENSOR_STATE, 0, |
| 289 | uint8_t(state), |
| 290 | uint8_t(CodeUpdateState:: |
| 291 | START)); |
| 292 | newImageId.clear(); |
| 293 | } |
| 294 | } |
| 295 | }); |
| 296 | } |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 297 | auto rc = setRequestedActivation(); |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 298 | if (rc != PLDM_SUCCESS) |
| 299 | { |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 300 | CodeUpdateState state = CodeUpdateState::FAIL; |
| 301 | setCodeUpdateProgress(false); |
| 302 | auto sensorId = getFirmwareUpdateSensor(); |
| 303 | sendStateSensorEvent( |
| 304 | sensorId, PLDM_STATE_SENSOR_STATE, 0, |
| 305 | uint8_t(state), |
| 306 | uint8_t(CodeUpdateState::START)); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 307 | error("could not set RequestedActivation"); |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 308 | } |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 309 | break; |
| 310 | } |
| 311 | } |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 312 | catch (const sdbusplus::exception_t& e) |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 313 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 314 | error( |
| 315 | "Error in getting Activation status,ERROR= {ERR_EXCEP}, INTERFACE={IMG_INTERFACE}, OBJECT PATH={OBJ_PATH}", |
| 316 | "ERR_EXCEP", e.what(), "IMG_INTERFACE", |
| 317 | imageInterface, "OBJ_PATH", imageObjPath); |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 318 | } |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 319 | } |
| 320 | } |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 321 | })); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | void CodeUpdate::processPriorityChangeNotification( |
| 325 | const DbusChangedProps& chProperties) |
| 326 | { |
| 327 | static constexpr auto propName = "Priority"; |
| 328 | const auto it = chProperties.find(propName); |
| 329 | if (it == chProperties.end()) |
| 330 | { |
| 331 | return; |
| 332 | } |
| 333 | uint8_t newVal = std::get<uint8_t>(it->second); |
| 334 | nextBootSide = (newVal == 0) ? currBootSide |
| 335 | : ((currBootSide == Tside) ? Pside : Tside); |
| 336 | } |
| 337 | |
| 338 | void CodeUpdate::setOemPlatformHandler( |
| 339 | pldm::responder::oem_platform::Handler* handler) |
| 340 | { |
| 341 | oemPlatformHandler = handler; |
| 342 | } |
| 343 | |
Varsha Kaverappa | 3ca29df | 2020-09-27 12:39:22 -0500 | [diff] [blame] | 344 | void CodeUpdate::clearDirPath(const std::string& dirPath) |
| 345 | { |
Manojkiran Eda | 78124cf | 2022-08-29 11:22:43 +0530 | [diff] [blame] | 346 | if (!fs::is_directory(dirPath)) |
Varsha Kaverappa | 3ca29df | 2020-09-27 12:39:22 -0500 | [diff] [blame] | 347 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 348 | error("The directory does not exist, dirPath = {DIR_PATH}", "DIR_PATH", |
| 349 | dirPath.c_str()); |
Manojkiran Eda | 78124cf | 2022-08-29 11:22:43 +0530 | [diff] [blame] | 350 | return; |
Varsha Kaverappa | 3ca29df | 2020-09-27 12:39:22 -0500 | [diff] [blame] | 351 | } |
Manojkiran Eda | 78124cf | 2022-08-29 11:22:43 +0530 | [diff] [blame] | 352 | for (const auto& iter : fs::directory_iterator(dirPath)) |
| 353 | { |
| 354 | fs::remove_all(iter); |
| 355 | } |
Varsha Kaverappa | 3ca29df | 2020-09-27 12:39:22 -0500 | [diff] [blame] | 356 | } |
| 357 | |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 358 | void CodeUpdate::sendStateSensorEvent( |
| 359 | uint16_t sensorId, enum sensor_event_class_states sensorEventClass, |
| 360 | uint8_t sensorOffset, uint8_t eventState, uint8_t prevEventState) |
| 361 | { |
| 362 | pldm::responder::oem_ibm_platform::Handler* oemIbmPlatformHandler = |
| 363 | dynamic_cast<pldm::responder::oem_ibm_platform::Handler*>( |
| 364 | oemPlatformHandler); |
| 365 | oemIbmPlatformHandler->sendStateSensorEvent( |
| 366 | sensorId, sensorEventClass, sensorOffset, eventState, prevEventState); |
| 367 | } |
| 368 | |
| 369 | void CodeUpdate::deleteImage() |
| 370 | { |
| 371 | static constexpr auto UPDATER_SERVICE = |
| 372 | "xyz.openbmc_project.Software.BMC.Updater"; |
| 373 | static constexpr auto SW_OBJ_PATH = "/xyz/openbmc_project/software"; |
| 374 | static constexpr auto DELETE_INTF = |
| 375 | "xyz.openbmc_project.Collection.DeleteAll"; |
| 376 | |
| 377 | auto& bus = dBusIntf->getBus(); |
| 378 | try |
| 379 | { |
| 380 | auto method = bus.new_method_call(UPDATER_SERVICE, SW_OBJ_PATH, |
| 381 | DELETE_INTF, "DeleteAll"); |
| 382 | bus.call_noreply(method); |
| 383 | } |
| 384 | catch (const std::exception& e) |
| 385 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 386 | error("Failed to delete image, ERROR={ERR_EXCEP}", "ERR_EXCEP", |
| 387 | e.what()); |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | } |
| 391 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 392 | uint8_t fetchBootSide(uint16_t entityInstance, CodeUpdate* codeUpdate) |
| 393 | { |
| 394 | uint8_t sensorOpState = tSideNum; |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 395 | if (entityInstance == 0) |
| 396 | { |
| 397 | auto currSide = codeUpdate->fetchCurrentBootSide(); |
| 398 | if (currSide == Pside) |
| 399 | { |
| 400 | sensorOpState = pSideNum; |
| 401 | } |
| 402 | } |
| 403 | else if (entityInstance == 1) |
| 404 | { |
| 405 | auto nextSide = codeUpdate->fetchNextBootSide(); |
| 406 | if (nextSide == Pside) |
| 407 | { |
| 408 | sensorOpState = pSideNum; |
| 409 | } |
| 410 | } |
| 411 | else |
| 412 | { |
| 413 | sensorOpState = PLDM_SENSOR_UNKNOWN; |
| 414 | } |
| 415 | |
| 416 | return sensorOpState; |
| 417 | } |
| 418 | |
| 419 | int setBootSide(uint16_t entityInstance, uint8_t currState, |
| 420 | const std::vector<set_effecter_state_field>& stateField, |
| 421 | CodeUpdate* codeUpdate) |
| 422 | { |
| 423 | int rc = PLDM_SUCCESS; |
| 424 | auto side = (stateField[currState].effecter_state == pSideNum) ? "P" : "T"; |
| 425 | |
| 426 | if (entityInstance == 0) |
| 427 | { |
| 428 | rc = codeUpdate->setCurrentBootSide(side); |
| 429 | } |
| 430 | else if (entityInstance == 1) |
| 431 | { |
| 432 | rc = codeUpdate->setNextBootSide(side); |
| 433 | } |
| 434 | else |
| 435 | { |
| 436 | rc = PLDM_PLATFORM_INVALID_STATE_VALUE; |
| 437 | } |
| 438 | return rc; |
| 439 | } |
| 440 | |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 441 | template <typename... T> |
| 442 | int executeCmd(T const&... t) |
| 443 | { |
| 444 | std::stringstream cmd; |
| 445 | ((cmd << t << " "), ...) << std::endl; |
| 446 | FILE* pipe = popen(cmd.str().c_str(), "r"); |
| 447 | if (!pipe) |
| 448 | { |
| 449 | throw std::runtime_error("popen() failed!"); |
| 450 | } |
| 451 | int rc = pclose(pipe); |
| 452 | if (WEXITSTATUS(rc)) |
| 453 | { |
| 454 | std::cerr << "Error executing: "; |
| 455 | ((std::cerr << " " << t), ...); |
| 456 | std::cerr << "\n"; |
| 457 | return -1; |
| 458 | } |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 463 | int processCodeUpdateLid(const std::string& filePath) |
| 464 | { |
| 465 | struct LidHeader |
| 466 | { |
| 467 | uint16_t magicNumber; |
Adriana Kobylak | 86d1418 | 2020-10-16 16:11:08 -0500 | [diff] [blame] | 468 | uint16_t headerVersion; |
| 469 | uint32_t lidNumber; |
| 470 | uint32_t lidDate; |
| 471 | uint16_t lidTime; |
| 472 | uint16_t lidClass; |
| 473 | uint32_t lidCrc; |
| 474 | uint32_t lidSize; |
| 475 | uint32_t headerSize; |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 476 | }; |
| 477 | LidHeader header; |
| 478 | |
| 479 | std::ifstream ifs(filePath, std::ios::in | std::ios::binary); |
| 480 | if (!ifs) |
| 481 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 482 | error("ifstream open error: {DIR_PATH}", "DIR_PATH", filePath.c_str()); |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 483 | return PLDM_ERROR; |
| 484 | } |
| 485 | ifs.seekg(0); |
| 486 | ifs.read(reinterpret_cast<char*>(&header), sizeof(header)); |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 487 | |
Adriana Kobylak | 86d1418 | 2020-10-16 16:11:08 -0500 | [diff] [blame] | 488 | // File size should be the value of lid size minus the header size |
| 489 | auto fileSize = fs::file_size(filePath); |
| 490 | fileSize -= htonl(header.headerSize); |
| 491 | if (fileSize < htonl(header.lidSize)) |
| 492 | { |
| 493 | // File is not completely written yet |
Adriana Kobylak | fa810d7 | 2020-10-16 16:27:28 -0500 | [diff] [blame] | 494 | ifs.close(); |
Adriana Kobylak | 86d1418 | 2020-10-16 16:11:08 -0500 | [diff] [blame] | 495 | return PLDM_SUCCESS; |
| 496 | } |
| 497 | |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 498 | constexpr auto magicNumber = 0x0222; |
| 499 | if (htons(header.magicNumber) != magicNumber) |
| 500 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 501 | error("Invalid magic number: {DIR_PATH}", "DIR_PATH", filePath.c_str()); |
Adriana Kobylak | fa810d7 | 2020-10-16 16:27:28 -0500 | [diff] [blame] | 502 | ifs.close(); |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 503 | return PLDM_ERROR; |
| 504 | } |
| 505 | |
Adriana Kobylak | a1f158c | 2020-11-09 12:47:29 -0600 | [diff] [blame] | 506 | fs::create_directories(imageDirPath); |
Adriana Kobylak | fa810d7 | 2020-10-16 16:27:28 -0500 | [diff] [blame] | 507 | fs::create_directories(lidDirPath); |
| 508 | |
Adriana Kobylak | a1f158c | 2020-11-09 12:47:29 -0600 | [diff] [blame] | 509 | constexpr auto bmcClass = 0x2000; |
| 510 | if (htons(header.lidClass) == bmcClass) |
| 511 | { |
| 512 | // Skip the header and concatenate the BMC LIDs into a tar file |
| 513 | std::ofstream ofs(tarImagePath, |
| 514 | std::ios::out | std::ios::binary | std::ios::app); |
| 515 | ifs.seekg(htonl(header.headerSize)); |
| 516 | ofs << ifs.rdbuf(); |
| 517 | ofs.flush(); |
| 518 | ofs.close(); |
| 519 | } |
| 520 | else |
| 521 | { |
| 522 | std::stringstream lidFileName; |
| 523 | lidFileName << std::hex << htonl(header.lidNumber) << ".lid"; |
| 524 | auto lidNoHeaderPath = fs::path(lidDirPath) / lidFileName.str(); |
| 525 | std::ofstream ofs(lidNoHeaderPath, |
| 526 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 527 | ifs.seekg(htonl(header.headerSize)); |
| 528 | ofs << ifs.rdbuf(); |
| 529 | ofs.flush(); |
| 530 | ofs.close(); |
| 531 | } |
Adriana Kobylak | fa810d7 | 2020-10-16 16:27:28 -0500 | [diff] [blame] | 532 | |
| 533 | ifs.close(); |
| 534 | fs::remove(filePath); |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 535 | return PLDM_SUCCESS; |
| 536 | } |
| 537 | |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 538 | int CodeUpdate::assembleCodeUpdateImage() |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 539 | { |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 540 | pid_t pid = fork(); |
| 541 | |
| 542 | if (pid == 0) |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 543 | { |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 544 | pid_t nextPid = fork(); |
| 545 | if (nextPid == 0) |
| 546 | { |
| 547 | // Create the hostfw squashfs image from the LID files without |
| 548 | // header |
| 549 | auto rc = executeCmd("/usr/sbin/mksquashfs", lidDirPath.c_str(), |
| 550 | hostfwImagePath.c_str(), "-all-root", |
| 551 | "-no-recovery"); |
| 552 | if (rc < 0) |
| 553 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 554 | error("Error occurred during the mksqusquashfs call"); |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 555 | setCodeUpdateProgress(false); |
| 556 | auto sensorId = getFirmwareUpdateSensor(); |
| 557 | sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0, |
| 558 | uint8_t(CodeUpdateState::FAIL), |
| 559 | uint8_t(CodeUpdateState::START)); |
| 560 | exit(EXIT_FAILURE); |
| 561 | } |
| 562 | |
| 563 | fs::create_directories(updateDirPath); |
| 564 | |
| 565 | // Extract the BMC tarball content |
| 566 | rc = executeCmd("/bin/tar", "-xf", tarImagePath.c_str(), "-C", |
| 567 | updateDirPath); |
| 568 | if (rc < 0) |
| 569 | { |
| 570 | setCodeUpdateProgress(false); |
| 571 | auto sensorId = getFirmwareUpdateSensor(); |
| 572 | sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0, |
| 573 | uint8_t(CodeUpdateState::FAIL), |
| 574 | uint8_t(CodeUpdateState::START)); |
| 575 | exit(EXIT_FAILURE); |
| 576 | } |
| 577 | |
| 578 | // Add the hostfw image to the directory where the contents were |
| 579 | // extracted |
| 580 | fs::copy_file(hostfwImagePath, |
| 581 | fs::path(updateDirPath) / hostfwImageName, |
| 582 | fs::copy_options::overwrite_existing); |
| 583 | |
| 584 | // Remove the tarball file, then re-generate it with so that the |
| 585 | // hostfw image becomes part of the tarball |
| 586 | fs::remove(tarImagePath); |
| 587 | rc = executeCmd("/bin/tar", "-cf", tarImagePath, ".", "-C", |
| 588 | updateDirPath); |
| 589 | if (rc < 0) |
| 590 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 591 | error("Error occurred during the generation of the tarball"); |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 592 | setCodeUpdateProgress(false); |
| 593 | auto sensorId = getFirmwareUpdateSensor(); |
| 594 | sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0, |
| 595 | uint8_t(CodeUpdateState::FAIL), |
| 596 | uint8_t(CodeUpdateState::START)); |
| 597 | exit(EXIT_FAILURE); |
| 598 | } |
| 599 | |
| 600 | // Copy the tarball to the update directory to trigger the phosphor |
| 601 | // software manager to create a version interface |
| 602 | fs::copy_file(tarImagePath, updateImagePath, |
| 603 | fs::copy_options::overwrite_existing); |
| 604 | |
| 605 | // Cleanup |
| 606 | fs::remove_all(updateDirPath); |
| 607 | fs::remove_all(lidDirPath); |
| 608 | fs::remove_all(imageDirPath); |
| 609 | |
| 610 | exit(EXIT_SUCCESS); |
| 611 | } |
| 612 | else if (nextPid < 0) |
| 613 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 614 | error("Error occurred during fork. ERROR={ERR}", "ERR", errno); |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 615 | exit(EXIT_FAILURE); |
| 616 | } |
| 617 | |
| 618 | // Do nothing as parent. When parent exits, child will be reparented |
| 619 | // under init and be reaped properly. |
| 620 | exit(0); |
| 621 | } |
| 622 | else if (pid > 0) |
| 623 | { |
| 624 | int status; |
| 625 | if (waitpid(pid, &status, 0) < 0) |
| 626 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 627 | error("Error occurred during waitpid. ERROR={ERR}", "ERR", errno); |
| 628 | |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 629 | return PLDM_ERROR; |
| 630 | } |
| 631 | else if (WEXITSTATUS(status) != 0) |
| 632 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 633 | error( |
| 634 | "Failed to execute the assembling of the image. STATUS={IMG_STATUS}", |
| 635 | "IMG_STATUS", status); |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 636 | return PLDM_ERROR; |
| 637 | } |
| 638 | } |
| 639 | else |
| 640 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 641 | error("Error occurred during fork. ERROR={ERR}", "ERR", errno); |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 642 | return PLDM_ERROR; |
| 643 | } |
| 644 | |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 645 | return PLDM_SUCCESS; |
| 646 | } |
| 647 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 648 | } // namespace responder |
| 649 | } // namespace pldm |