| Unive Tien | 7ad45b4 | 2025-08-18 06:04:53 +0000 | [diff] [blame] | 1 | #include "firmware_inventory_manager.hpp" |
| 2 | |
| 3 | #include "common/types.hpp" |
| 4 | #include "common/utils.hpp" |
| 5 | |
| 6 | #include <phosphor-logging/lg2.hpp> |
| 7 | |
| 8 | PHOSPHOR_LOG2_USING; |
| 9 | |
| 10 | namespace pldm::fw_update |
| 11 | { |
| 12 | |
| 13 | void FirmwareInventoryManager::createFirmwareEntry( |
| 14 | const SoftwareIdentifier& softwareIdentifier, |
| 15 | const SoftwareName& softwareName, const std::string& activeVersion, |
| Unive Tien | ee2bd8a | 2025-10-30 08:11:06 +0000 | [diff] [blame^] | 16 | const Descriptors& /*descriptors*/, const ComponentInfo& /*componentInfo*/) |
| Unive Tien | 7ad45b4 | 2025-08-18 06:04:53 +0000 | [diff] [blame] | 17 | { |
| 18 | struct timespec ts; |
| 19 | clock_gettime(CLOCK_REALTIME, &ts); |
| 20 | unsigned seed = ts.tv_nsec ^ getpid(); |
| 21 | srandom(seed); |
| 22 | |
| rajeeranjan | 05d15a0 | 2025-06-30 12:10:08 +0530 | [diff] [blame] | 23 | // Determine the inventory board path for this EID. If Entity-Manager |
| 24 | // has provided a mapping, use it; otherwise fall back to a default |
| 25 | // placeholder path so that a software object can still be created. |
| 26 | |
| 27 | std::optional<std::filesystem::path> boardPath; |
| 28 | |
| Unive Tien | 7ad45b4 | 2025-08-18 06:04:53 +0000 | [diff] [blame] | 29 | auto& eid = softwareIdentifier.first; |
| 30 | const auto inventoryPath = getInventoryPath(eid); |
| rajeeranjan | 05d15a0 | 2025-06-30 12:10:08 +0530 | [diff] [blame] | 31 | if (inventoryPath) |
| Unive Tien | 7ad45b4 | 2025-08-18 06:04:53 +0000 | [diff] [blame] | 32 | { |
| rajeeranjan | 05d15a0 | 2025-06-30 12:10:08 +0530 | [diff] [blame] | 33 | boardPath = getBoardPath(*dbusHandler, *inventoryPath); |
| Unive Tien | 7ad45b4 | 2025-08-18 06:04:53 +0000 | [diff] [blame] | 34 | } |
| rajeeranjan | 05d15a0 | 2025-06-30 12:10:08 +0530 | [diff] [blame] | 35 | else |
| Unive Tien | 7ad45b4 | 2025-08-18 06:04:53 +0000 | [diff] [blame] | 36 | { |
| rajeeranjan | 05d15a0 | 2025-06-30 12:10:08 +0530 | [diff] [blame] | 37 | boardPath = "/xyz/openbmc_project/inventory/system/board/PLDM_Device"; |
| Unive Tien | 7ad45b4 | 2025-08-18 06:04:53 +0000 | [diff] [blame] | 38 | } |
| 39 | const auto boardName = boardPath->filename().string(); |
| 40 | const auto softwarePath = |
| 41 | std::format("/xyz/openbmc_project/software/{}_{}_{}", boardName, |
| 42 | softwareName, utils::generateSwId()); |
| 43 | |
| 44 | softwareMap.insert_or_assign( |
| Unive Tien | ee2bd8a | 2025-10-30 08:11:06 +0000 | [diff] [blame^] | 45 | softwareIdentifier, |
| 46 | std::make_unique<FirmwareInventory>(softwareIdentifier, softwarePath, |
| 47 | activeVersion, *boardPath)); |
| Unive Tien | 7ad45b4 | 2025-08-18 06:04:53 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void FirmwareInventoryManager::deleteFirmwareEntry(const pldm::eid& eid) |
| 51 | { |
| 52 | std::erase_if(softwareMap, |
| 53 | [&](const auto& pair) { return pair.first.first == eid; }); |
| Unive Tien | ee2bd8a | 2025-10-30 08:11:06 +0000 | [diff] [blame^] | 54 | updateManager.eraseUpdateManagerIf( |
| 55 | [&](const SoftwareIdentifier& softwareIdentifier) { |
| 56 | return softwareIdentifier.first == eid; |
| 57 | }); |
| Unive Tien | 7ad45b4 | 2025-08-18 06:04:53 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | std::optional<std::filesystem::path> getBoardPath( |
| 61 | const pldm::utils::DBusHandler& handler, const InventoryPath& path) |
| 62 | { |
| 63 | constexpr auto boardInterface = "xyz.openbmc_project.Inventory.Item.Board"; |
| 64 | pldm::utils::GetAncestorsResponse response; |
| 65 | |
| 66 | try |
| 67 | { |
| 68 | response = handler.getAncestors(path.c_str(), {boardInterface}); |
| 69 | } |
| 70 | catch (const sdbusplus::exception_t& e) |
| 71 | { |
| 72 | error("Failed to get ancestors for path {PATH}: {ERROR}", "PATH", path, |
| 73 | "ERROR", e); |
| 74 | return std::nullopt; |
| 75 | } |
| 76 | |
| 77 | if (response.empty()) |
| 78 | { |
| 79 | error( |
| 80 | "Failed to get unique board path for Inventory path {PATH}, found: {SIZE}", |
| 81 | "PATH", path, "SIZE", response.size()); |
| 82 | return std::nullopt; |
| 83 | } |
| 84 | |
| 85 | return std::get<ObjectPath>(response.front()); |
| 86 | } |
| 87 | |
| 88 | std::optional<InventoryPath> FirmwareInventoryManager::getInventoryPath( |
| 89 | const pldm::eid& eid) const |
| 90 | { |
| 91 | for (const auto& [configDbusPath, configMctpInfo] : configurations) |
| 92 | { |
| 93 | if (std::get<pldm::eid>(configMctpInfo) == eid) |
| 94 | { |
| 95 | return configDbusPath; |
| 96 | } |
| 97 | } |
| 98 | warning("No inventory path found for EID {EID}", "EID", eid); |
| 99 | return std::nullopt; |
| 100 | } |
| 101 | |
| 102 | } // namespace pldm::fw_update |