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