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