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 | { |
Sagar Srinivas | 9040347 | 2023-08-21 06:08:55 -0500 | [diff] [blame] | 120 | error( |
| 121 | "Failed To set RequestedApplyTime property, OBJ_PATH={OBJ_PATH}, INTERFACE={INTERFACE}, PROP_NAME={PROP_NAME}, ERROR={ERR_EXCEP}", |
| 122 | "OBJ_PATH", dbusMapping.objectPath, "INTERFACE", |
| 123 | dbusMapping.interface, "PROP_NAME", dbusMapping.propertyName, |
| 124 | "ERR_EXCEP", e.what()); |
Sagar Srinivas | cfdbca7 | 2020-09-22 10:03:35 -0500 | [diff] [blame] | 125 | rc = PLDM_ERROR; |
| 126 | } |
| 127 | return rc; |
| 128 | } |
| 129 | |
| 130 | int CodeUpdate::setRequestedActivation() |
| 131 | { |
| 132 | int rc = PLDM_SUCCESS; |
| 133 | pldm::utils::PropertyValue value = |
| 134 | "xyz.openbmc_project.Software.Activation.RequestedActivations.Active"; |
| 135 | DBusMapping dbusMapping; |
| 136 | dbusMapping.objectPath = newImageId; |
| 137 | dbusMapping.interface = "xyz.openbmc_project.Software.Activation"; |
| 138 | dbusMapping.propertyName = "RequestedActivation"; |
| 139 | dbusMapping.propertyType = "string"; |
| 140 | try |
| 141 | { |
| 142 | pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value); |
| 143 | } |
| 144 | catch (const std::exception& e) |
| 145 | { |
Sagar Srinivas | 9040347 | 2023-08-21 06:08:55 -0500 | [diff] [blame] | 146 | error( |
| 147 | "Failed To set RequestedActivation property, OBJ_PATH={OBJ_PATH}, INTERFACE={INTERFACE}, PROP_NAME={PROP_NAME}, ERROR={ERR_EXCEP}", |
| 148 | "OBJ_PATH", dbusMapping.objectPath, "INTERFACE", |
| 149 | dbusMapping.interface, "PROP_NAME", dbusMapping.propertyName, |
| 150 | "ERR_EXCEP", e.what()); |
Sagar Srinivas | cfdbca7 | 2020-09-22 10:03:35 -0500 | [diff] [blame] | 151 | rc = PLDM_ERROR; |
| 152 | } |
Sagar Srinivas | cfdbca7 | 2020-09-22 10:03:35 -0500 | [diff] [blame] | 153 | return rc; |
| 154 | } |
| 155 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 156 | void CodeUpdate::setVersions() |
| 157 | { |
| 158 | static constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper"; |
| 159 | static constexpr auto functionalObjPath = |
| 160 | "/xyz/openbmc_project/software/functional"; |
| 161 | static constexpr auto activeObjPath = |
| 162 | "/xyz/openbmc_project/software/active"; |
| 163 | static constexpr auto propIntf = "org.freedesktop.DBus.Properties"; |
| 164 | |
| 165 | auto& bus = dBusIntf->getBus(); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 166 | try |
| 167 | { |
| 168 | auto method = bus.new_method_call(mapperService, functionalObjPath, |
| 169 | propIntf, "Get"); |
| 170 | method.append("xyz.openbmc_project.Association", "endpoints"); |
| 171 | std::variant<std::vector<std::string>> paths; |
| 172 | |
vkaverap@in.ibm.com | 9138c20 | 2023-05-19 07:50:47 -0500 | [diff] [blame] | 173 | auto reply = bus.call( |
| 174 | method, |
| 175 | std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count()); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 176 | reply.read(paths); |
| 177 | |
| 178 | runningVersion = std::get<std::vector<std::string>>(paths)[0]; |
| 179 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 180 | auto method1 = bus.new_method_call(mapperService, activeObjPath, |
| 181 | propIntf, "Get"); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 182 | method1.append("xyz.openbmc_project.Association", "endpoints"); |
| 183 | |
vkaverap@in.ibm.com | 9138c20 | 2023-05-19 07:50:47 -0500 | [diff] [blame] | 184 | auto reply1 = bus.call( |
| 185 | method1, |
| 186 | std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count()); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 187 | reply1.read(paths); |
| 188 | for (const auto& path : std::get<std::vector<std::string>>(paths)) |
| 189 | { |
| 190 | if (path != runningVersion) |
| 191 | { |
| 192 | nonRunningVersion = path; |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | catch (const std::exception& e) |
| 198 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 199 | error( |
| 200 | "failed to make a d-bus call to Object Mapper Association, ERROR={ERR_EXCEP}", |
| 201 | "ERR_EXCEP", e.what()); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | |
| 205 | using namespace sdbusplus::bus::match::rules; |
| 206 | captureNextBootSideChange.push_back( |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 207 | std::make_unique<sdbusplus::bus::match_t>( |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 208 | pldm::utils::DBusHandler::getBus(), |
| 209 | propertiesChanged(runningVersion, redundancyIntf), |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 210 | [this](sdbusplus::message_t& msg) { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 211 | DbusChangedProps props; |
| 212 | std::string iface; |
| 213 | msg.read(iface, props); |
| 214 | processPriorityChangeNotification(props); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 215 | })); |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 216 | fwUpdateMatcher.push_back(std::make_unique<sdbusplus::bus::match_t>( |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 217 | pldm::utils::DBusHandler::getBus(), |
| 218 | "interface='org.freedesktop.DBus.ObjectManager',type='signal'," |
| 219 | "member='InterfacesAdded',path='/xyz/openbmc_project/software'", |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 220 | [this](sdbusplus::message_t& msg) { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 221 | DBusInterfaceAdded interfaces; |
| 222 | sdbusplus::message::object_path path; |
| 223 | msg.read(path, interfaces); |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 224 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 225 | for (auto& interface : interfaces) |
| 226 | { |
| 227 | if (interface.first == "xyz.openbmc_project.Software.Activation") |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 228 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 229 | auto imageInterface = "xyz.openbmc_project.Software.Activation"; |
| 230 | auto imageObjPath = path.str.c_str(); |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 231 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 232 | try |
| 233 | { |
| 234 | auto propVal = dBusIntf->getDbusPropertyVariant( |
| 235 | imageObjPath, "Activation", imageInterface); |
| 236 | const auto& imageProp = std::get<std::string>(propVal); |
| 237 | if (imageProp == "xyz.openbmc_project.Software." |
| 238 | "Activation.Activations.Ready" && |
| 239 | isCodeUpdateInProgress()) |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 240 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 241 | newImageId = path.str; |
| 242 | if (!imageActivationMatch) |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 243 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 244 | imageActivationMatch = |
| 245 | std::make_unique<sdbusplus::bus::match_t>( |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 246 | pldm::utils::DBusHandler::getBus(), |
| 247 | propertiesChanged(newImageId, |
| 248 | "xyz.openbmc_project." |
| 249 | "Software.Activation"), |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 250 | [this](sdbusplus::message_t& msg) { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 251 | DbusChangedProps props; |
| 252 | std::string iface; |
| 253 | msg.read(iface, props); |
| 254 | const auto itr = props.find("Activation"); |
| 255 | if (itr != props.end()) |
| 256 | { |
| 257 | PropertyValue value = itr->second; |
| 258 | auto propVal = std::get<std::string>(value); |
| 259 | if (propVal == |
| 260 | "xyz.openbmc_project.Software." |
| 261 | "Activation.Activations.Active") |
| 262 | { |
| 263 | CodeUpdateState state = |
| 264 | CodeUpdateState::END; |
| 265 | setCodeUpdateProgress(false); |
| 266 | auto sensorId = |
| 267 | getFirmwareUpdateSensor(); |
| 268 | sendStateSensorEvent( |
| 269 | sensorId, PLDM_STATE_SENSOR_STATE, |
| 270 | 0, uint8_t(state), |
| 271 | uint8_t(CodeUpdateState::START)); |
| 272 | newImageId.clear(); |
| 273 | } |
| 274 | else if (propVal == "xyz.openbmc_project." |
| 275 | "Software.Activation." |
| 276 | "Activations.Failed" || |
| 277 | propVal == "xyz.openbmc_" |
| 278 | "project.Software." |
| 279 | "Activation." |
| 280 | "Activations." |
| 281 | "Invalid") |
| 282 | { |
| 283 | CodeUpdateState state = |
| 284 | CodeUpdateState::FAIL; |
| 285 | setCodeUpdateProgress(false); |
| 286 | auto sensorId = |
| 287 | getFirmwareUpdateSensor(); |
| 288 | sendStateSensorEvent( |
| 289 | sensorId, PLDM_STATE_SENSOR_STATE, |
| 290 | 0, uint8_t(state), |
| 291 | uint8_t(CodeUpdateState::START)); |
| 292 | newImageId.clear(); |
| 293 | } |
| 294 | } |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 295 | }); |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 296 | } |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 297 | auto rc = setRequestedActivation(); |
| 298 | if (rc != PLDM_SUCCESS) |
| 299 | { |
| 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)); |
| 307 | error("could not set RequestedActivation"); |
| 308 | } |
| 309 | break; |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 310 | } |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 311 | } |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 312 | catch (const sdbusplus::exception_t& e) |
| 313 | { |
| 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", imageInterface, |
| 317 | "OBJ_PATH", imageObjPath); |
| 318 | } |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 319 | } |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 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"); |
vkaverap@in.ibm.com | 5b71b86 | 2023-08-21 05:19:04 +0000 | [diff] [blame^] | 382 | bus.call_noreply(method, dbusTimeout); |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 383 | } |
| 384 | catch (const std::exception& e) |
| 385 | { |
Sagar Srinivas | 9040347 | 2023-08-21 06:08:55 -0500 | [diff] [blame] | 386 | error( |
| 387 | "Failed to delete image, OBJ_PATH={OBJ_PATH}, INTERFACE={INTERFACE}, ERROR={ERR_EXCEP}", |
| 388 | "OBJ_PATH", SW_OBJ_PATH, "INTERFACE", DELETE_INTF, "ERR_EXCEP", |
| 389 | e.what()); |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 390 | return; |
| 391 | } |
| 392 | } |
| 393 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 394 | uint8_t fetchBootSide(uint16_t entityInstance, CodeUpdate* codeUpdate) |
| 395 | { |
| 396 | uint8_t sensorOpState = tSideNum; |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 397 | if (entityInstance == 0) |
| 398 | { |
| 399 | auto currSide = codeUpdate->fetchCurrentBootSide(); |
| 400 | if (currSide == Pside) |
| 401 | { |
| 402 | sensorOpState = pSideNum; |
| 403 | } |
| 404 | } |
| 405 | else if (entityInstance == 1) |
| 406 | { |
| 407 | auto nextSide = codeUpdate->fetchNextBootSide(); |
| 408 | if (nextSide == Pside) |
| 409 | { |
| 410 | sensorOpState = pSideNum; |
| 411 | } |
| 412 | } |
| 413 | else |
| 414 | { |
| 415 | sensorOpState = PLDM_SENSOR_UNKNOWN; |
| 416 | } |
| 417 | |
| 418 | return sensorOpState; |
| 419 | } |
| 420 | |
| 421 | int setBootSide(uint16_t entityInstance, uint8_t currState, |
| 422 | const std::vector<set_effecter_state_field>& stateField, |
| 423 | CodeUpdate* codeUpdate) |
| 424 | { |
| 425 | int rc = PLDM_SUCCESS; |
| 426 | auto side = (stateField[currState].effecter_state == pSideNum) ? "P" : "T"; |
| 427 | |
| 428 | if (entityInstance == 0) |
| 429 | { |
| 430 | rc = codeUpdate->setCurrentBootSide(side); |
| 431 | } |
| 432 | else if (entityInstance == 1) |
| 433 | { |
| 434 | rc = codeUpdate->setNextBootSide(side); |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | rc = PLDM_PLATFORM_INVALID_STATE_VALUE; |
| 439 | } |
| 440 | return rc; |
| 441 | } |
| 442 | |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 443 | template <typename... T> |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 444 | int executeCmd(const T&... t) |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 445 | { |
| 446 | std::stringstream cmd; |
| 447 | ((cmd << t << " "), ...) << std::endl; |
| 448 | FILE* pipe = popen(cmd.str().c_str(), "r"); |
| 449 | if (!pipe) |
| 450 | { |
| 451 | throw std::runtime_error("popen() failed!"); |
| 452 | } |
| 453 | int rc = pclose(pipe); |
| 454 | if (WEXITSTATUS(rc)) |
| 455 | { |
| 456 | std::cerr << "Error executing: "; |
| 457 | ((std::cerr << " " << t), ...); |
| 458 | std::cerr << "\n"; |
| 459 | return -1; |
| 460 | } |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 465 | int processCodeUpdateLid(const std::string& filePath) |
| 466 | { |
| 467 | struct LidHeader |
| 468 | { |
| 469 | uint16_t magicNumber; |
Adriana Kobylak | 86d1418 | 2020-10-16 16:11:08 -0500 | [diff] [blame] | 470 | uint16_t headerVersion; |
| 471 | uint32_t lidNumber; |
| 472 | uint32_t lidDate; |
| 473 | uint16_t lidTime; |
| 474 | uint16_t lidClass; |
| 475 | uint32_t lidCrc; |
| 476 | uint32_t lidSize; |
| 477 | uint32_t headerSize; |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 478 | }; |
| 479 | LidHeader header; |
| 480 | |
| 481 | std::ifstream ifs(filePath, std::ios::in | std::ios::binary); |
| 482 | if (!ifs) |
| 483 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 484 | error("ifstream open error: {DIR_PATH}", "DIR_PATH", filePath.c_str()); |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 485 | return PLDM_ERROR; |
| 486 | } |
| 487 | ifs.seekg(0); |
| 488 | ifs.read(reinterpret_cast<char*>(&header), sizeof(header)); |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 489 | |
Adriana Kobylak | 86d1418 | 2020-10-16 16:11:08 -0500 | [diff] [blame] | 490 | // File size should be the value of lid size minus the header size |
| 491 | auto fileSize = fs::file_size(filePath); |
| 492 | fileSize -= htonl(header.headerSize); |
| 493 | if (fileSize < htonl(header.lidSize)) |
| 494 | { |
| 495 | // File is not completely written yet |
Adriana Kobylak | fa810d7 | 2020-10-16 16:27:28 -0500 | [diff] [blame] | 496 | ifs.close(); |
Adriana Kobylak | 86d1418 | 2020-10-16 16:11:08 -0500 | [diff] [blame] | 497 | return PLDM_SUCCESS; |
| 498 | } |
| 499 | |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 500 | constexpr auto magicNumber = 0x0222; |
| 501 | if (htons(header.magicNumber) != magicNumber) |
| 502 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 503 | error("Invalid magic number: {DIR_PATH}", "DIR_PATH", filePath.c_str()); |
Adriana Kobylak | fa810d7 | 2020-10-16 16:27:28 -0500 | [diff] [blame] | 504 | ifs.close(); |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 505 | return PLDM_ERROR; |
| 506 | } |
| 507 | |
Adriana Kobylak | a1f158c | 2020-11-09 12:47:29 -0600 | [diff] [blame] | 508 | fs::create_directories(imageDirPath); |
Adriana Kobylak | fa810d7 | 2020-10-16 16:27:28 -0500 | [diff] [blame] | 509 | fs::create_directories(lidDirPath); |
| 510 | |
Adriana Kobylak | a1f158c | 2020-11-09 12:47:29 -0600 | [diff] [blame] | 511 | constexpr auto bmcClass = 0x2000; |
| 512 | if (htons(header.lidClass) == bmcClass) |
| 513 | { |
| 514 | // Skip the header and concatenate the BMC LIDs into a tar file |
| 515 | std::ofstream ofs(tarImagePath, |
| 516 | std::ios::out | std::ios::binary | std::ios::app); |
| 517 | ifs.seekg(htonl(header.headerSize)); |
| 518 | ofs << ifs.rdbuf(); |
| 519 | ofs.flush(); |
| 520 | ofs.close(); |
| 521 | } |
| 522 | else |
| 523 | { |
| 524 | std::stringstream lidFileName; |
| 525 | lidFileName << std::hex << htonl(header.lidNumber) << ".lid"; |
| 526 | auto lidNoHeaderPath = fs::path(lidDirPath) / lidFileName.str(); |
| 527 | std::ofstream ofs(lidNoHeaderPath, |
| 528 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 529 | ifs.seekg(htonl(header.headerSize)); |
| 530 | ofs << ifs.rdbuf(); |
| 531 | ofs.flush(); |
| 532 | ofs.close(); |
| 533 | } |
Adriana Kobylak | fa810d7 | 2020-10-16 16:27:28 -0500 | [diff] [blame] | 534 | |
| 535 | ifs.close(); |
| 536 | fs::remove(filePath); |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 537 | return PLDM_SUCCESS; |
| 538 | } |
| 539 | |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 540 | int CodeUpdate::assembleCodeUpdateImage() |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 541 | { |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 542 | pid_t pid = fork(); |
| 543 | |
| 544 | if (pid == 0) |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 545 | { |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 546 | pid_t nextPid = fork(); |
| 547 | if (nextPid == 0) |
| 548 | { |
| 549 | // Create the hostfw squashfs image from the LID files without |
| 550 | // header |
| 551 | auto rc = executeCmd("/usr/sbin/mksquashfs", lidDirPath.c_str(), |
| 552 | hostfwImagePath.c_str(), "-all-root", |
| 553 | "-no-recovery"); |
| 554 | if (rc < 0) |
| 555 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 556 | error("Error occurred during the mksqusquashfs call"); |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 557 | setCodeUpdateProgress(false); |
| 558 | auto sensorId = getFirmwareUpdateSensor(); |
| 559 | sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0, |
| 560 | uint8_t(CodeUpdateState::FAIL), |
| 561 | uint8_t(CodeUpdateState::START)); |
| 562 | exit(EXIT_FAILURE); |
| 563 | } |
| 564 | |
| 565 | fs::create_directories(updateDirPath); |
| 566 | |
| 567 | // Extract the BMC tarball content |
| 568 | rc = executeCmd("/bin/tar", "-xf", tarImagePath.c_str(), "-C", |
| 569 | updateDirPath); |
| 570 | if (rc < 0) |
| 571 | { |
| 572 | setCodeUpdateProgress(false); |
| 573 | auto sensorId = getFirmwareUpdateSensor(); |
| 574 | sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0, |
| 575 | uint8_t(CodeUpdateState::FAIL), |
| 576 | uint8_t(CodeUpdateState::START)); |
| 577 | exit(EXIT_FAILURE); |
| 578 | } |
| 579 | |
| 580 | // Add the hostfw image to the directory where the contents were |
| 581 | // extracted |
| 582 | fs::copy_file(hostfwImagePath, |
| 583 | fs::path(updateDirPath) / hostfwImageName, |
| 584 | fs::copy_options::overwrite_existing); |
| 585 | |
| 586 | // Remove the tarball file, then re-generate it with so that the |
| 587 | // hostfw image becomes part of the tarball |
| 588 | fs::remove(tarImagePath); |
| 589 | rc = executeCmd("/bin/tar", "-cf", tarImagePath, ".", "-C", |
| 590 | updateDirPath); |
| 591 | if (rc < 0) |
| 592 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 593 | error("Error occurred during the generation of the tarball"); |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 594 | setCodeUpdateProgress(false); |
| 595 | auto sensorId = getFirmwareUpdateSensor(); |
| 596 | sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0, |
| 597 | uint8_t(CodeUpdateState::FAIL), |
| 598 | uint8_t(CodeUpdateState::START)); |
| 599 | exit(EXIT_FAILURE); |
| 600 | } |
| 601 | |
| 602 | // Copy the tarball to the update directory to trigger the phosphor |
| 603 | // software manager to create a version interface |
| 604 | fs::copy_file(tarImagePath, updateImagePath, |
| 605 | fs::copy_options::overwrite_existing); |
| 606 | |
| 607 | // Cleanup |
| 608 | fs::remove_all(updateDirPath); |
| 609 | fs::remove_all(lidDirPath); |
| 610 | fs::remove_all(imageDirPath); |
| 611 | |
| 612 | exit(EXIT_SUCCESS); |
| 613 | } |
| 614 | else if (nextPid < 0) |
| 615 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 616 | error("Error occurred during fork. ERROR={ERR}", "ERR", errno); |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 617 | exit(EXIT_FAILURE); |
| 618 | } |
| 619 | |
| 620 | // Do nothing as parent. When parent exits, child will be reparented |
| 621 | // under init and be reaped properly. |
| 622 | exit(0); |
| 623 | } |
| 624 | else if (pid > 0) |
| 625 | { |
| 626 | int status; |
| 627 | if (waitpid(pid, &status, 0) < 0) |
| 628 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 629 | error("Error occurred during waitpid. ERROR={ERR}", "ERR", errno); |
| 630 | |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 631 | return PLDM_ERROR; |
| 632 | } |
| 633 | else if (WEXITSTATUS(status) != 0) |
| 634 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 635 | error( |
| 636 | "Failed to execute the assembling of the image. STATUS={IMG_STATUS}", |
| 637 | "IMG_STATUS", status); |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 638 | return PLDM_ERROR; |
| 639 | } |
| 640 | } |
| 641 | else |
| 642 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 643 | error("Error occurred during fork. ERROR={ERR}", "ERR", errno); |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 644 | return PLDM_ERROR; |
| 645 | } |
| 646 | |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 647 | return PLDM_SUCCESS; |
| 648 | } |
| 649 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 650 | } // namespace responder |
| 651 | } // namespace pldm |