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 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame^] | 172 | auto method1 = bus.new_method_call(mapperService, activeObjPath, |
| 173 | propIntf, "Get"); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 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) { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame^] | 201 | DbusChangedProps props; |
| 202 | std::string iface; |
| 203 | msg.read(iface, props); |
| 204 | processPriorityChangeNotification(props); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 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) { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -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 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame^] | 215 | for (auto& interface : interfaces) |
| 216 | { |
| 217 | if (interface.first == "xyz.openbmc_project.Software.Activation") |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 218 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame^] | 219 | auto imageInterface = "xyz.openbmc_project.Software.Activation"; |
| 220 | auto imageObjPath = path.str.c_str(); |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 221 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame^] | 222 | try |
| 223 | { |
| 224 | auto propVal = dBusIntf->getDbusPropertyVariant( |
| 225 | imageObjPath, "Activation", imageInterface); |
| 226 | const auto& imageProp = std::get<std::string>(propVal); |
| 227 | if (imageProp == "xyz.openbmc_project.Software." |
| 228 | "Activation.Activations.Ready" && |
| 229 | isCodeUpdateInProgress()) |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 230 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame^] | 231 | newImageId = path.str; |
| 232 | if (!imageActivationMatch) |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 233 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame^] | 234 | imageActivationMatch = |
| 235 | std::make_unique<sdbusplus::bus::match_t>( |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 236 | pldm::utils::DBusHandler::getBus(), |
| 237 | propertiesChanged(newImageId, |
| 238 | "xyz.openbmc_project." |
| 239 | "Software.Activation"), |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 240 | [this](sdbusplus::message_t& msg) { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame^] | 241 | DbusChangedProps props; |
| 242 | std::string iface; |
| 243 | msg.read(iface, props); |
| 244 | const auto itr = props.find("Activation"); |
| 245 | if (itr != props.end()) |
| 246 | { |
| 247 | PropertyValue value = itr->second; |
| 248 | auto propVal = std::get<std::string>(value); |
| 249 | if (propVal == |
| 250 | "xyz.openbmc_project.Software." |
| 251 | "Activation.Activations.Active") |
| 252 | { |
| 253 | CodeUpdateState state = |
| 254 | CodeUpdateState::END; |
| 255 | setCodeUpdateProgress(false); |
| 256 | auto sensorId = |
| 257 | getFirmwareUpdateSensor(); |
| 258 | sendStateSensorEvent( |
| 259 | sensorId, PLDM_STATE_SENSOR_STATE, |
| 260 | 0, uint8_t(state), |
| 261 | uint8_t(CodeUpdateState::START)); |
| 262 | newImageId.clear(); |
| 263 | } |
| 264 | else if (propVal == "xyz.openbmc_project." |
| 265 | "Software.Activation." |
| 266 | "Activations.Failed" || |
| 267 | propVal == "xyz.openbmc_" |
| 268 | "project.Software." |
| 269 | "Activation." |
| 270 | "Activations." |
| 271 | "Invalid") |
| 272 | { |
| 273 | CodeUpdateState state = |
| 274 | CodeUpdateState::FAIL; |
| 275 | setCodeUpdateProgress(false); |
| 276 | auto sensorId = |
| 277 | getFirmwareUpdateSensor(); |
| 278 | sendStateSensorEvent( |
| 279 | sensorId, PLDM_STATE_SENSOR_STATE, |
| 280 | 0, uint8_t(state), |
| 281 | uint8_t(CodeUpdateState::START)); |
| 282 | newImageId.clear(); |
| 283 | } |
| 284 | } |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 285 | }); |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 286 | } |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame^] | 287 | auto rc = setRequestedActivation(); |
| 288 | if (rc != PLDM_SUCCESS) |
| 289 | { |
| 290 | CodeUpdateState state = CodeUpdateState::FAIL; |
| 291 | setCodeUpdateProgress(false); |
| 292 | auto sensorId = getFirmwareUpdateSensor(); |
| 293 | sendStateSensorEvent( |
| 294 | sensorId, PLDM_STATE_SENSOR_STATE, 0, |
| 295 | uint8_t(state), |
| 296 | uint8_t(CodeUpdateState::START)); |
| 297 | error("could not set RequestedActivation"); |
| 298 | } |
| 299 | break; |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 300 | } |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 301 | } |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame^] | 302 | catch (const sdbusplus::exception_t& e) |
| 303 | { |
| 304 | error( |
| 305 | "Error in getting Activation status,ERROR= {ERR_EXCEP}, INTERFACE={IMG_INTERFACE}, OBJECT PATH={OBJ_PATH}", |
| 306 | "ERR_EXCEP", e.what(), "IMG_INTERFACE", imageInterface, |
| 307 | "OBJ_PATH", imageObjPath); |
| 308 | } |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 309 | } |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame^] | 310 | } |
Sagar Srinivas | 9a64b4a | 2021-02-09 07:55:38 -0600 | [diff] [blame] | 311 | })); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void CodeUpdate::processPriorityChangeNotification( |
| 315 | const DbusChangedProps& chProperties) |
| 316 | { |
| 317 | static constexpr auto propName = "Priority"; |
| 318 | const auto it = chProperties.find(propName); |
| 319 | if (it == chProperties.end()) |
| 320 | { |
| 321 | return; |
| 322 | } |
| 323 | uint8_t newVal = std::get<uint8_t>(it->second); |
| 324 | nextBootSide = (newVal == 0) ? currBootSide |
| 325 | : ((currBootSide == Tside) ? Pside : Tside); |
| 326 | } |
| 327 | |
| 328 | void CodeUpdate::setOemPlatformHandler( |
| 329 | pldm::responder::oem_platform::Handler* handler) |
| 330 | { |
| 331 | oemPlatformHandler = handler; |
| 332 | } |
| 333 | |
Varsha Kaverappa | 3ca29df | 2020-09-27 12:39:22 -0500 | [diff] [blame] | 334 | void CodeUpdate::clearDirPath(const std::string& dirPath) |
| 335 | { |
Manojkiran Eda | 78124cf | 2022-08-29 11:22:43 +0530 | [diff] [blame] | 336 | if (!fs::is_directory(dirPath)) |
Varsha Kaverappa | 3ca29df | 2020-09-27 12:39:22 -0500 | [diff] [blame] | 337 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 338 | error("The directory does not exist, dirPath = {DIR_PATH}", "DIR_PATH", |
| 339 | dirPath.c_str()); |
Manojkiran Eda | 78124cf | 2022-08-29 11:22:43 +0530 | [diff] [blame] | 340 | return; |
Varsha Kaverappa | 3ca29df | 2020-09-27 12:39:22 -0500 | [diff] [blame] | 341 | } |
Manojkiran Eda | 78124cf | 2022-08-29 11:22:43 +0530 | [diff] [blame] | 342 | for (const auto& iter : fs::directory_iterator(dirPath)) |
| 343 | { |
| 344 | fs::remove_all(iter); |
| 345 | } |
Varsha Kaverappa | 3ca29df | 2020-09-27 12:39:22 -0500 | [diff] [blame] | 346 | } |
| 347 | |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 348 | void CodeUpdate::sendStateSensorEvent( |
| 349 | uint16_t sensorId, enum sensor_event_class_states sensorEventClass, |
| 350 | uint8_t sensorOffset, uint8_t eventState, uint8_t prevEventState) |
| 351 | { |
| 352 | pldm::responder::oem_ibm_platform::Handler* oemIbmPlatformHandler = |
| 353 | dynamic_cast<pldm::responder::oem_ibm_platform::Handler*>( |
| 354 | oemPlatformHandler); |
| 355 | oemIbmPlatformHandler->sendStateSensorEvent( |
| 356 | sensorId, sensorEventClass, sensorOffset, eventState, prevEventState); |
| 357 | } |
| 358 | |
| 359 | void CodeUpdate::deleteImage() |
| 360 | { |
| 361 | static constexpr auto UPDATER_SERVICE = |
| 362 | "xyz.openbmc_project.Software.BMC.Updater"; |
| 363 | static constexpr auto SW_OBJ_PATH = "/xyz/openbmc_project/software"; |
| 364 | static constexpr auto DELETE_INTF = |
| 365 | "xyz.openbmc_project.Collection.DeleteAll"; |
| 366 | |
| 367 | auto& bus = dBusIntf->getBus(); |
| 368 | try |
| 369 | { |
| 370 | auto method = bus.new_method_call(UPDATER_SERVICE, SW_OBJ_PATH, |
| 371 | DELETE_INTF, "DeleteAll"); |
| 372 | bus.call_noreply(method); |
| 373 | } |
| 374 | catch (const std::exception& e) |
| 375 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 376 | error("Failed to delete image, ERROR={ERR_EXCEP}", "ERR_EXCEP", |
| 377 | e.what()); |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 378 | return; |
| 379 | } |
| 380 | } |
| 381 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 382 | uint8_t fetchBootSide(uint16_t entityInstance, CodeUpdate* codeUpdate) |
| 383 | { |
| 384 | uint8_t sensorOpState = tSideNum; |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 385 | if (entityInstance == 0) |
| 386 | { |
| 387 | auto currSide = codeUpdate->fetchCurrentBootSide(); |
| 388 | if (currSide == Pside) |
| 389 | { |
| 390 | sensorOpState = pSideNum; |
| 391 | } |
| 392 | } |
| 393 | else if (entityInstance == 1) |
| 394 | { |
| 395 | auto nextSide = codeUpdate->fetchNextBootSide(); |
| 396 | if (nextSide == Pside) |
| 397 | { |
| 398 | sensorOpState = pSideNum; |
| 399 | } |
| 400 | } |
| 401 | else |
| 402 | { |
| 403 | sensorOpState = PLDM_SENSOR_UNKNOWN; |
| 404 | } |
| 405 | |
| 406 | return sensorOpState; |
| 407 | } |
| 408 | |
| 409 | int setBootSide(uint16_t entityInstance, uint8_t currState, |
| 410 | const std::vector<set_effecter_state_field>& stateField, |
| 411 | CodeUpdate* codeUpdate) |
| 412 | { |
| 413 | int rc = PLDM_SUCCESS; |
| 414 | auto side = (stateField[currState].effecter_state == pSideNum) ? "P" : "T"; |
| 415 | |
| 416 | if (entityInstance == 0) |
| 417 | { |
| 418 | rc = codeUpdate->setCurrentBootSide(side); |
| 419 | } |
| 420 | else if (entityInstance == 1) |
| 421 | { |
| 422 | rc = codeUpdate->setNextBootSide(side); |
| 423 | } |
| 424 | else |
| 425 | { |
| 426 | rc = PLDM_PLATFORM_INVALID_STATE_VALUE; |
| 427 | } |
| 428 | return rc; |
| 429 | } |
| 430 | |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 431 | template <typename... T> |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame^] | 432 | int executeCmd(const T&... t) |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 433 | { |
| 434 | std::stringstream cmd; |
| 435 | ((cmd << t << " "), ...) << std::endl; |
| 436 | FILE* pipe = popen(cmd.str().c_str(), "r"); |
| 437 | if (!pipe) |
| 438 | { |
| 439 | throw std::runtime_error("popen() failed!"); |
| 440 | } |
| 441 | int rc = pclose(pipe); |
| 442 | if (WEXITSTATUS(rc)) |
| 443 | { |
| 444 | std::cerr << "Error executing: "; |
| 445 | ((std::cerr << " " << t), ...); |
| 446 | std::cerr << "\n"; |
| 447 | return -1; |
| 448 | } |
| 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 453 | int processCodeUpdateLid(const std::string& filePath) |
| 454 | { |
| 455 | struct LidHeader |
| 456 | { |
| 457 | uint16_t magicNumber; |
Adriana Kobylak | 86d1418 | 2020-10-16 16:11:08 -0500 | [diff] [blame] | 458 | uint16_t headerVersion; |
| 459 | uint32_t lidNumber; |
| 460 | uint32_t lidDate; |
| 461 | uint16_t lidTime; |
| 462 | uint16_t lidClass; |
| 463 | uint32_t lidCrc; |
| 464 | uint32_t lidSize; |
| 465 | uint32_t headerSize; |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 466 | }; |
| 467 | LidHeader header; |
| 468 | |
| 469 | std::ifstream ifs(filePath, std::ios::in | std::ios::binary); |
| 470 | if (!ifs) |
| 471 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 472 | error("ifstream open error: {DIR_PATH}", "DIR_PATH", filePath.c_str()); |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 473 | return PLDM_ERROR; |
| 474 | } |
| 475 | ifs.seekg(0); |
| 476 | ifs.read(reinterpret_cast<char*>(&header), sizeof(header)); |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 477 | |
Adriana Kobylak | 86d1418 | 2020-10-16 16:11:08 -0500 | [diff] [blame] | 478 | // File size should be the value of lid size minus the header size |
| 479 | auto fileSize = fs::file_size(filePath); |
| 480 | fileSize -= htonl(header.headerSize); |
| 481 | if (fileSize < htonl(header.lidSize)) |
| 482 | { |
| 483 | // File is not completely written yet |
Adriana Kobylak | fa810d7 | 2020-10-16 16:27:28 -0500 | [diff] [blame] | 484 | ifs.close(); |
Adriana Kobylak | 86d1418 | 2020-10-16 16:11:08 -0500 | [diff] [blame] | 485 | return PLDM_SUCCESS; |
| 486 | } |
| 487 | |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 488 | constexpr auto magicNumber = 0x0222; |
| 489 | if (htons(header.magicNumber) != magicNumber) |
| 490 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 491 | error("Invalid magic number: {DIR_PATH}", "DIR_PATH", filePath.c_str()); |
Adriana Kobylak | fa810d7 | 2020-10-16 16:27:28 -0500 | [diff] [blame] | 492 | ifs.close(); |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 493 | return PLDM_ERROR; |
| 494 | } |
| 495 | |
Adriana Kobylak | a1f158c | 2020-11-09 12:47:29 -0600 | [diff] [blame] | 496 | fs::create_directories(imageDirPath); |
Adriana Kobylak | fa810d7 | 2020-10-16 16:27:28 -0500 | [diff] [blame] | 497 | fs::create_directories(lidDirPath); |
| 498 | |
Adriana Kobylak | a1f158c | 2020-11-09 12:47:29 -0600 | [diff] [blame] | 499 | constexpr auto bmcClass = 0x2000; |
| 500 | if (htons(header.lidClass) == bmcClass) |
| 501 | { |
| 502 | // Skip the header and concatenate the BMC LIDs into a tar file |
| 503 | std::ofstream ofs(tarImagePath, |
| 504 | std::ios::out | std::ios::binary | std::ios::app); |
| 505 | ifs.seekg(htonl(header.headerSize)); |
| 506 | ofs << ifs.rdbuf(); |
| 507 | ofs.flush(); |
| 508 | ofs.close(); |
| 509 | } |
| 510 | else |
| 511 | { |
| 512 | std::stringstream lidFileName; |
| 513 | lidFileName << std::hex << htonl(header.lidNumber) << ".lid"; |
| 514 | auto lidNoHeaderPath = fs::path(lidDirPath) / lidFileName.str(); |
| 515 | std::ofstream ofs(lidNoHeaderPath, |
| 516 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 517 | ifs.seekg(htonl(header.headerSize)); |
| 518 | ofs << ifs.rdbuf(); |
| 519 | ofs.flush(); |
| 520 | ofs.close(); |
| 521 | } |
Adriana Kobylak | fa810d7 | 2020-10-16 16:27:28 -0500 | [diff] [blame] | 522 | |
| 523 | ifs.close(); |
| 524 | fs::remove(filePath); |
Adriana Kobylak | 727f738 | 2020-09-01 14:38:25 -0500 | [diff] [blame] | 525 | return PLDM_SUCCESS; |
| 526 | } |
| 527 | |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 528 | int CodeUpdate::assembleCodeUpdateImage() |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 529 | { |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 530 | pid_t pid = fork(); |
| 531 | |
| 532 | if (pid == 0) |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 533 | { |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 534 | pid_t nextPid = fork(); |
| 535 | if (nextPid == 0) |
| 536 | { |
| 537 | // Create the hostfw squashfs image from the LID files without |
| 538 | // header |
| 539 | auto rc = executeCmd("/usr/sbin/mksquashfs", lidDirPath.c_str(), |
| 540 | hostfwImagePath.c_str(), "-all-root", |
| 541 | "-no-recovery"); |
| 542 | if (rc < 0) |
| 543 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 544 | error("Error occurred during the mksqusquashfs call"); |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 545 | setCodeUpdateProgress(false); |
| 546 | auto sensorId = getFirmwareUpdateSensor(); |
| 547 | sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0, |
| 548 | uint8_t(CodeUpdateState::FAIL), |
| 549 | uint8_t(CodeUpdateState::START)); |
| 550 | exit(EXIT_FAILURE); |
| 551 | } |
| 552 | |
| 553 | fs::create_directories(updateDirPath); |
| 554 | |
| 555 | // Extract the BMC tarball content |
| 556 | rc = executeCmd("/bin/tar", "-xf", tarImagePath.c_str(), "-C", |
| 557 | updateDirPath); |
| 558 | if (rc < 0) |
| 559 | { |
| 560 | setCodeUpdateProgress(false); |
| 561 | auto sensorId = getFirmwareUpdateSensor(); |
| 562 | sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0, |
| 563 | uint8_t(CodeUpdateState::FAIL), |
| 564 | uint8_t(CodeUpdateState::START)); |
| 565 | exit(EXIT_FAILURE); |
| 566 | } |
| 567 | |
| 568 | // Add the hostfw image to the directory where the contents were |
| 569 | // extracted |
| 570 | fs::copy_file(hostfwImagePath, |
| 571 | fs::path(updateDirPath) / hostfwImageName, |
| 572 | fs::copy_options::overwrite_existing); |
| 573 | |
| 574 | // Remove the tarball file, then re-generate it with so that the |
| 575 | // hostfw image becomes part of the tarball |
| 576 | fs::remove(tarImagePath); |
| 577 | rc = executeCmd("/bin/tar", "-cf", tarImagePath, ".", "-C", |
| 578 | updateDirPath); |
| 579 | if (rc < 0) |
| 580 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 581 | error("Error occurred during the generation of the tarball"); |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 582 | setCodeUpdateProgress(false); |
| 583 | auto sensorId = getFirmwareUpdateSensor(); |
| 584 | sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0, |
| 585 | uint8_t(CodeUpdateState::FAIL), |
| 586 | uint8_t(CodeUpdateState::START)); |
| 587 | exit(EXIT_FAILURE); |
| 588 | } |
| 589 | |
| 590 | // Copy the tarball to the update directory to trigger the phosphor |
| 591 | // software manager to create a version interface |
| 592 | fs::copy_file(tarImagePath, updateImagePath, |
| 593 | fs::copy_options::overwrite_existing); |
| 594 | |
| 595 | // Cleanup |
| 596 | fs::remove_all(updateDirPath); |
| 597 | fs::remove_all(lidDirPath); |
| 598 | fs::remove_all(imageDirPath); |
| 599 | |
| 600 | exit(EXIT_SUCCESS); |
| 601 | } |
| 602 | else if (nextPid < 0) |
| 603 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 604 | error("Error occurred during fork. ERROR={ERR}", "ERR", errno); |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 605 | exit(EXIT_FAILURE); |
| 606 | } |
| 607 | |
| 608 | // Do nothing as parent. When parent exits, child will be reparented |
| 609 | // under init and be reaped properly. |
| 610 | exit(0); |
| 611 | } |
| 612 | else if (pid > 0) |
| 613 | { |
| 614 | int status; |
| 615 | if (waitpid(pid, &status, 0) < 0) |
| 616 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 617 | error("Error occurred during waitpid. ERROR={ERR}", "ERR", errno); |
| 618 | |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 619 | return PLDM_ERROR; |
| 620 | } |
| 621 | else if (WEXITSTATUS(status) != 0) |
| 622 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 623 | error( |
| 624 | "Failed to execute the assembling of the image. STATUS={IMG_STATUS}", |
| 625 | "IMG_STATUS", status); |
Adriana Kobylak | 9296f24 | 2021-09-22 15:52:00 +0000 | [diff] [blame] | 626 | return PLDM_ERROR; |
| 627 | } |
| 628 | } |
| 629 | else |
| 630 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 631 | error("Error occurred during fork. ERROR={ERR}", "ERR", errno); |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 632 | return PLDM_ERROR; |
| 633 | } |
| 634 | |
Adriana Kobylak | 837fb47 | 2020-10-16 16:53:42 -0500 | [diff] [blame] | 635 | return PLDM_SUCCESS; |
| 636 | } |
| 637 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 638 | } // namespace responder |
| 639 | } // namespace pldm |