Andrew Geissler | 70d72f8 | 2022-03-23 13:15:05 -0500 | [diff] [blame] | 1 | #include "side_switch.hpp" |
| 2 | |
Andrew Geissler | 03b3082 | 2022-03-24 11:04:40 -0500 | [diff] [blame^] | 3 | #include "utils.hpp" |
| 4 | |
Andrew Geissler | 70d72f8 | 2022-03-23 13:15:05 -0500 | [diff] [blame] | 5 | #include <phosphor-logging/lg2.hpp> |
Andrew Geissler | 03b3082 | 2022-03-24 11:04:40 -0500 | [diff] [blame^] | 6 | #include <sdbusplus/bus.hpp> |
| 7 | |
| 8 | #include <exception> |
| 9 | #include <string> |
| 10 | #include <variant> |
| 11 | #include <vector> |
Andrew Geissler | 70d72f8 | 2022-03-23 13:15:05 -0500 | [diff] [blame] | 12 | |
| 13 | PHOSPHOR_LOG2_USING; |
| 14 | |
Andrew Geissler | 03b3082 | 2022-03-24 11:04:40 -0500 | [diff] [blame^] | 15 | bool sideSwitchNeeded() |
| 16 | { |
| 17 | auto bus = sdbusplus::bus::new_default(); |
| 18 | std::string fwRunningVersionPath; |
| 19 | uint8_t fwRunningPriority = 0; |
| 20 | |
| 21 | // Get active image |
| 22 | try |
| 23 | { |
| 24 | std::vector<std::string> paths = |
| 25 | utils::getProperty<std::vector<std::string>>( |
| 26 | bus, "/xyz/openbmc_project/software/functional", |
| 27 | "xyz.openbmc_project.Association", "endpoints"); |
| 28 | if (paths.size() != 1) |
| 29 | { |
| 30 | info("side-switch only supports BMC-purpose image systems"); |
| 31 | return (false); |
| 32 | } |
| 33 | fwRunningVersionPath = paths[0]; |
| 34 | info("Running firmware version path is {FW_PATH}", "FW_PATH", |
| 35 | fwRunningVersionPath); |
| 36 | } |
| 37 | catch (const std::exception& e) |
| 38 | { |
| 39 | error("failed to retrieve active firmware version: {ERROR}", "ERROR", |
| 40 | e); |
| 41 | return (false); |
| 42 | } |
| 43 | |
| 44 | // Check if active image has highest priority (0) |
| 45 | try |
| 46 | { |
| 47 | fwRunningPriority = utils::getProperty<uint8_t>( |
| 48 | bus, fwRunningVersionPath.c_str(), |
| 49 | "xyz.openbmc_project.Software.RedundancyPriority", "Priority"); |
| 50 | info("Running firmware version priority is {FW_PRIORITY}", |
| 51 | "FW_PRIORITY", fwRunningPriority); |
| 52 | } |
| 53 | catch (const std::exception& e) |
| 54 | { |
| 55 | error("failed to read priority from active image: {ERROR}", "ERROR", e); |
| 56 | return (false); |
| 57 | } |
| 58 | |
| 59 | // If running at highest priority (0) then no side switch needed |
| 60 | if (fwRunningPriority == 0) |
| 61 | { |
| 62 | info("Running image is at priority 0, no side switch needed"); |
| 63 | return (false); |
| 64 | } |
| 65 | |
| 66 | // Need to check if any other BMC images on system have a higher priority |
| 67 | std::vector<std::string> allSoftwarePaths; |
| 68 | try |
| 69 | { |
| 70 | auto method = bus.new_method_call("xyz.openbmc_project.ObjectMapper", |
| 71 | "/xyz/openbmc_project/object_mapper", |
| 72 | "xyz.openbmc_project.ObjectMapper", |
| 73 | "GetSubTreePaths"); |
| 74 | method.append("/xyz/openbmc_project/software"); |
| 75 | method.append(0); // Depth 0 to search all |
| 76 | method.append( |
| 77 | std::vector<std::string>({"xyz.openbmc_project.Software.Version"})); |
| 78 | auto reply = bus.call(method); |
| 79 | reply.read(allSoftwarePaths); |
| 80 | if (allSoftwarePaths.size() <= 1) |
| 81 | { |
| 82 | info("only 1 image present in flash so no side switch needed"); |
| 83 | return (false); |
| 84 | } |
| 85 | } |
| 86 | catch (const std::exception& e) |
| 87 | { |
| 88 | error("failed to retrieve all firmware versions: {ERROR}", "ERROR", e); |
| 89 | return (false); |
| 90 | } |
| 91 | |
| 92 | // Cycle through all firmware images looking for a BMC version that |
| 93 | // has a higher priority then our running image |
| 94 | for (auto& fwPath : allSoftwarePaths) |
| 95 | { |
| 96 | if (fwPath == fwRunningVersionPath) |
| 97 | { |
| 98 | info("{FW_PATH} is the running image, skip", "FW_PATH", fwPath); |
| 99 | continue; |
| 100 | } |
| 101 | try |
| 102 | { |
| 103 | uint8_t thisPathPri = utils::getProperty<uint8_t>( |
| 104 | bus, fwPath.c_str(), |
| 105 | "xyz.openbmc_project.Software.RedundancyPriority", "Priority"); |
| 106 | |
| 107 | if (thisPathPri < fwRunningPriority) |
| 108 | { |
| 109 | info( |
| 110 | "{FW_PATH} has a higher priority, {FW_PRIORITY}, then running priority", |
| 111 | "FW_PATH", fwPath, "FW_PRIORITY", thisPathPri); |
| 112 | return (true); |
| 113 | } |
| 114 | } |
| 115 | catch (const std::exception& e) |
| 116 | { |
| 117 | // This could just be a host firmware image, just keep going |
| 118 | info("failed to read a BMC priority from {FW_PATH}: {ERROR}", |
| 119 | "FW_PATH", fwPath, "ERROR", e); |
| 120 | continue; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return (false); |
| 125 | } |
| 126 | |
Andrew Geissler | 70d72f8 | 2022-03-23 13:15:05 -0500 | [diff] [blame] | 127 | int main() |
| 128 | { |
| 129 | info("Checking for side switch reboot"); |
| 130 | |
Andrew Geissler | 03b3082 | 2022-03-24 11:04:40 -0500 | [diff] [blame^] | 131 | if (!sideSwitchNeeded()) |
| 132 | { |
| 133 | info("Side switch not needed"); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | // TODO - Future commits in series to fill in rest of logic |
Andrew Geissler | 70d72f8 | 2022-03-23 13:15:05 -0500 | [diff] [blame] | 138 | } |