1 #pragma once 2 #include "async_resp.hpp" 3 #include "dbus_utility.hpp" 4 #include "error_messages.hpp" 5 #include "generated/enums/resource.hpp" 6 #include "http/utility.hpp" 7 #include "utils/dbus_utils.hpp" 8 9 #include <boost/system/error_code.hpp> 10 #include <boost/url/format.hpp> 11 #include <sdbusplus/asio/property.hpp> 12 #include <sdbusplus/unpack_properties.hpp> 13 14 #include <algorithm> 15 #include <array> 16 #include <string> 17 #include <string_view> 18 #include <vector> 19 20 namespace redfish 21 { 22 namespace sw_util 23 { 24 /* @brief String that indicates a bios software instance */ 25 constexpr const char* biosPurpose = 26 "xyz.openbmc_project.Software.Version.VersionPurpose.Host"; 27 28 /* @brief String that indicates a BMC software instance */ 29 constexpr const char* bmcPurpose = 30 "xyz.openbmc_project.Software.Version.VersionPurpose.BMC"; 31 32 /** 33 * @brief Populate the running software version and image links 34 * 35 * @param[i,o] aResp Async response object 36 * @param[i] swVersionPurpose Indicates what target to look for 37 * @param[i] activeVersionPropName Index in aResp->res.jsonValue to write 38 * the running software version to 39 * @param[i] populateLinkToImages Populate aResp->res "Links" 40 * "ActiveSoftwareImage" with a link to the running software image and 41 * "SoftwareImages" with a link to the all its software images 42 * 43 * @return void 44 */ 45 inline void 46 populateSoftwareInformation(const std::shared_ptr<bmcweb::AsyncResp>& aResp, 47 const std::string& swVersionPurpose, 48 const std::string& activeVersionPropName, 49 const bool populateLinkToImages) 50 { 51 // Used later to determine running (known on Redfish as active) Sw images 52 dbus::utility::getAssociationEndPoints( 53 "/xyz/openbmc_project/software/functional", 54 [aResp, swVersionPurpose, activeVersionPropName, populateLinkToImages]( 55 const boost::system::error_code& ec, 56 const dbus::utility::MapperEndPoints& functionalSw) { 57 BMCWEB_LOG_DEBUG << "populateSoftwareInformation enter"; 58 if (ec) 59 { 60 BMCWEB_LOG_ERROR << "error_code = " << ec; 61 BMCWEB_LOG_ERROR << "error msg = " << ec.message(); 62 messages::internalError(aResp->res); 63 return; 64 } 65 66 if (functionalSw.empty()) 67 { 68 // Could keep going and try to populate SoftwareImages but 69 // something is seriously wrong, so just fail 70 BMCWEB_LOG_ERROR << "Zero functional software in system"; 71 messages::internalError(aResp->res); 72 return; 73 } 74 75 std::vector<std::string> functionalSwIds; 76 // example functionalSw: 77 // v as 2 "/xyz/openbmc_project/software/ace821ef" 78 // "/xyz/openbmc_project/software/230fb078" 79 for (const auto& sw : functionalSw) 80 { 81 sdbusplus::message::object_path path(sw); 82 std::string leaf = path.filename(); 83 if (leaf.empty()) 84 { 85 continue; 86 } 87 88 functionalSwIds.push_back(leaf); 89 } 90 91 constexpr std::array<std::string_view, 1> interfaces = { 92 "xyz.openbmc_project.Software.Version"}; 93 dbus::utility::getSubTree( 94 "/xyz/openbmc_project/software", 0, interfaces, 95 [aResp, swVersionPurpose, activeVersionPropName, 96 populateLinkToImages, functionalSwIds]( 97 const boost::system::error_code& ec2, 98 const dbus::utility::MapperGetSubTreeResponse& subtree) { 99 if (ec2) 100 { 101 BMCWEB_LOG_ERROR << "error_code = " << ec2; 102 BMCWEB_LOG_ERROR << "error msg = " << ec2.message(); 103 messages::internalError(aResp->res); 104 return; 105 } 106 107 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " images"; 108 109 for (const std::pair<std::string, 110 std::vector<std::pair< 111 std::string, std::vector<std::string>>>>& 112 obj : subtree) 113 { 114 sdbusplus::message::object_path path(obj.first); 115 std::string swId = path.filename(); 116 if (swId.empty()) 117 { 118 messages::internalError(aResp->res); 119 BMCWEB_LOG_ERROR << "Invalid software ID"; 120 121 return; 122 } 123 124 bool runningImage = false; 125 // Look at Ids from 126 // /xyz/openbmc_project/software/functional 127 // to determine if this is a running image 128 if (std::find(functionalSwIds.begin(), functionalSwIds.end(), 129 swId) != functionalSwIds.end()) 130 { 131 runningImage = true; 132 } 133 134 // Now grab its version info 135 sdbusplus::asio::getAllProperties( 136 *crow::connections::systemBus, obj.second[0].first, 137 obj.first, "xyz.openbmc_project.Software.Version", 138 [aResp, swId, runningImage, swVersionPurpose, 139 activeVersionPropName, populateLinkToImages]( 140 const boost::system::error_code& ec3, 141 const dbus::utility::DBusPropertiesMap& 142 propertiesList) { 143 if (ec3) 144 { 145 BMCWEB_LOG_ERROR << "error_code = " << ec3; 146 BMCWEB_LOG_ERROR << "error msg = " << ec3.message(); 147 // Have seen the code update app delete the D-Bus 148 // object, during code update, between the call to 149 // mapper and here. Just leave these properties off if 150 // resource not found. 151 if (ec3.value() == EBADR) 152 { 153 return; 154 } 155 messages::internalError(aResp->res); 156 return; 157 } 158 // example propertiesList 159 // a{sv} 2 "Version" s 160 // "IBM-witherspoon-OP9-v2.0.10-2.22" "Purpose" 161 // s 162 // "xyz.openbmc_project.Software.Version.VersionPurpose.Host" 163 const std::string* version = nullptr; 164 const std::string* swInvPurpose = nullptr; 165 166 const bool success = sdbusplus::unpackPropertiesNoThrow( 167 dbus_utils::UnpackErrorPrinter(), propertiesList, 168 "Purpose", swInvPurpose, "Version", version); 169 170 if (!success) 171 { 172 messages::internalError(aResp->res); 173 return; 174 } 175 176 if (version == nullptr || version->empty()) 177 { 178 messages::internalError(aResp->res); 179 return; 180 } 181 if (swInvPurpose == nullptr || 182 *swInvPurpose != swVersionPurpose) 183 { 184 // Not purpose we're looking for 185 return; 186 } 187 188 BMCWEB_LOG_DEBUG << "Image ID: " << swId; 189 BMCWEB_LOG_DEBUG << "Running image: " << runningImage; 190 BMCWEB_LOG_DEBUG << "Image purpose: " << *swInvPurpose; 191 192 if (populateLinkToImages) 193 { 194 nlohmann::json& softwareImageMembers = 195 aResp->res.jsonValue["Links"]["SoftwareImages"]; 196 // Firmware images are at 197 // /redfish/v1/UpdateService/FirmwareInventory/<Id> 198 // e.g. .../FirmwareInventory/82d3ec86 199 nlohmann::json::object_t member; 200 member["@odata.id"] = boost::urls::format( 201 "/redfish/v1/UpdateService/FirmwareInventory/{}", 202 swId); 203 softwareImageMembers.emplace_back(std::move(member)); 204 aResp->res 205 .jsonValue["Links"]["SoftwareImages@odata.count"] = 206 softwareImageMembers.size(); 207 208 if (runningImage) 209 { 210 nlohmann::json::object_t runningMember; 211 runningMember["@odata.id"] = boost::urls::format( 212 "/redfish/v1/UpdateService/FirmwareInventory/{}", 213 swId); 214 // Create the link to the running image 215 aResp->res 216 .jsonValue["Links"]["ActiveSoftwareImage"] = 217 std::move(runningMember); 218 } 219 } 220 if (!activeVersionPropName.empty() && runningImage) 221 { 222 aResp->res.jsonValue[activeVersionPropName] = *version; 223 } 224 }); 225 } 226 }); 227 }); 228 } 229 230 /** 231 * @brief Translate input swState to Redfish state 232 * 233 * This function will return the corresponding Redfish state 234 * 235 * @param[i] swState The OpenBMC software state 236 * 237 * @return The corresponding Redfish state 238 */ 239 inline resource::State getRedfishSwState(const std::string& swState) 240 { 241 if (swState == "xyz.openbmc_project.Software.Activation.Activations.Active") 242 { 243 return resource::State::Enabled; 244 } 245 if (swState == "xyz.openbmc_project.Software.Activation." 246 "Activations.Activating") 247 { 248 return resource::State::Updating; 249 } 250 if (swState == "xyz.openbmc_project.Software.Activation." 251 "Activations.StandbySpare") 252 { 253 return resource::State::StandbySpare; 254 } 255 BMCWEB_LOG_DEBUG << "Default sw state " << swState << " to Disabled"; 256 return resource::State::Disabled; 257 } 258 259 /** 260 * @brief Translate input swState to Redfish health state 261 * 262 * This function will return the corresponding Redfish health state 263 * 264 * @param[i] swState The OpenBMC software state 265 * 266 * @return The corresponding Redfish health state 267 */ 268 inline std::string getRedfishSwHealth(const std::string& swState) 269 { 270 if ((swState == 271 "xyz.openbmc_project.Software.Activation.Activations.Active") || 272 (swState == "xyz.openbmc_project.Software.Activation.Activations." 273 "Activating") || 274 (swState == 275 "xyz.openbmc_project.Software.Activation.Activations.Ready")) 276 { 277 return "OK"; 278 } 279 BMCWEB_LOG_DEBUG << "Sw state " << swState << " to Warning"; 280 return "Warning"; 281 } 282 283 /** 284 * @brief Put status of input swId into json response 285 * 286 * This function will put the appropriate Redfish state of the input 287 * software id to ["Status"]["State"] within the json response 288 * 289 * @param[i,o] aResp Async response object 290 * @param[i] swId The software ID to get status for 291 * @param[i] dbusSvc The dbus service implementing the software object 292 * 293 * @return void 294 */ 295 inline void getSwStatus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 296 const std::shared_ptr<std::string>& swId, 297 const std::string& dbusSvc) 298 { 299 BMCWEB_LOG_DEBUG << "getSwStatus: swId " << *swId << " svc " << dbusSvc; 300 301 sdbusplus::asio::getAllProperties( 302 *crow::connections::systemBus, dbusSvc, 303 "/xyz/openbmc_project/software/" + *swId, 304 "xyz.openbmc_project.Software.Activation", 305 [asyncResp, 306 swId](const boost::system::error_code& errorCode, 307 const dbus::utility::DBusPropertiesMap& propertiesList) { 308 if (errorCode) 309 { 310 // not all swtypes are updateable, this is ok 311 asyncResp->res.jsonValue["Status"]["State"] = "Enabled"; 312 return; 313 } 314 315 const std::string* swInvActivation = nullptr; 316 317 const bool success = sdbusplus::unpackPropertiesNoThrow( 318 dbus_utils::UnpackErrorPrinter(), propertiesList, "Activation", 319 swInvActivation); 320 321 if (!success) 322 { 323 messages::internalError(asyncResp->res); 324 return; 325 } 326 327 if (swInvActivation == nullptr) 328 { 329 messages::internalError(asyncResp->res); 330 return; 331 } 332 333 BMCWEB_LOG_DEBUG << "getSwStatus: Activation " << *swInvActivation; 334 asyncResp->res.jsonValue["Status"]["State"] = 335 getRedfishSwState(*swInvActivation); 336 asyncResp->res.jsonValue["Status"]["Health"] = 337 getRedfishSwHealth(*swInvActivation); 338 }); 339 } 340 341 /** 342 * @brief Updates programmable status of input swId into json response 343 * 344 * This function checks whether software inventory component 345 * can be programmable or not and fill's the "Updatable" 346 * Property. 347 * 348 * @param[i,o] asyncResp Async response object 349 * @param[i] swId The software ID 350 */ 351 inline void 352 getSwUpdatableStatus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 353 const std::shared_ptr<std::string>& swId) 354 { 355 dbus::utility::getAssociationEndPoints( 356 "/xyz/openbmc_project/software/updateable", 357 [asyncResp, swId](const boost::system::error_code& ec, 358 const dbus::utility::MapperEndPoints& objPaths) { 359 if (ec) 360 { 361 BMCWEB_LOG_DEBUG << " error_code = " << ec 362 << " error msg = " << ec.message(); 363 // System can exist with no updateable software, 364 // so don't throw error here. 365 return; 366 } 367 std::string reqSwObjPath = "/xyz/openbmc_project/software/" + *swId; 368 369 if (std::find(objPaths.begin(), objPaths.end(), reqSwObjPath) != 370 objPaths.end()) 371 { 372 asyncResp->res.jsonValue["Updateable"] = true; 373 return; 374 } 375 }); 376 } 377 378 } // namespace sw_util 379 } // namespace redfish 380