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 | |
| 7 | #include <exception> |
| 8 | #include <string> |
Andrew Geissler | cdac8f4 | 2022-03-24 15:02:13 -0500 | [diff] [blame^] | 9 | #include <thread> |
Andrew Geissler | 03b3082 | 2022-03-24 11:04:40 -0500 | [diff] [blame] | 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 | cdac8f4 | 2022-03-24 15:02:13 -0500 | [diff] [blame^] | 15 | bool sideSwitchNeeded(sdbusplus::bus::bus& bus) |
Andrew Geissler | 03b3082 | 2022-03-24 11:04:40 -0500 | [diff] [blame] | 16 | { |
Andrew Geissler | cdac8f4 | 2022-03-24 15:02:13 -0500 | [diff] [blame^] | 17 | |
Andrew Geissler | 03b3082 | 2022-03-24 11:04:40 -0500 | [diff] [blame] | 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 | cdac8f4 | 2022-03-24 15:02:13 -0500 | [diff] [blame^] | 127 | bool powerOffSystem(sdbusplus::bus::bus& bus) |
| 128 | { |
| 129 | |
| 130 | try |
| 131 | { |
| 132 | utils::PropertyValue chassOff = |
| 133 | "xyz.openbmc_project.State.Chassis.Transition.Off"; |
| 134 | utils::setProperty(bus, "/xyz/openbmc_project/state/chassis0", |
| 135 | "xyz.openbmc_project.State.Chassis", |
| 136 | "RequestedPowerTransition", chassOff); |
| 137 | } |
| 138 | catch (const std::exception& e) |
| 139 | { |
| 140 | error("chassis off request failed: {ERROR}", "ERROR", e); |
| 141 | return (false); |
| 142 | } |
| 143 | |
| 144 | // Now just wait for power to turn off |
| 145 | // Worst case is a systemd service hangs in power off for 2 minutes so |
| 146 | // take that and double it to avoid any timing issues. The user has |
| 147 | // requested we switch to the other side, so a lengthy delay is warranted |
| 148 | // if needed. On most systems the power off takes 5-15 seconds. |
| 149 | for (int i = 0; i < 240; i++) |
| 150 | { |
| 151 | std::this_thread::sleep_for(std::chrono::milliseconds(1000)); |
| 152 | try |
| 153 | { |
| 154 | auto currentPwrState = utils::getProperty<std::string>( |
| 155 | bus, "/xyz/openbmc_project/state/chassis0", |
| 156 | "xyz.openbmc_project.State.Chassis", "CurrentPowerState"); |
| 157 | |
| 158 | if (currentPwrState == |
| 159 | "xyz.openbmc_project.State.Chassis.PowerState.Off") |
| 160 | { |
| 161 | info("chassis power is off"); |
| 162 | return (true); |
| 163 | } |
| 164 | } |
| 165 | catch (const std::exception& e) |
| 166 | { |
| 167 | error("reading chassis power state failed: {ERROR}", "ERROR", e); |
| 168 | return (false); |
| 169 | } |
| 170 | } |
| 171 | error("timeout waiting for chassis power to turn off"); |
| 172 | return (false); |
| 173 | } |
| 174 | |
Andrew Geissler | 70d72f8 | 2022-03-23 13:15:05 -0500 | [diff] [blame] | 175 | int main() |
| 176 | { |
| 177 | info("Checking for side switch reboot"); |
| 178 | |
Andrew Geissler | cdac8f4 | 2022-03-24 15:02:13 -0500 | [diff] [blame^] | 179 | auto bus = sdbusplus::bus::new_default(); |
| 180 | |
| 181 | if (!sideSwitchNeeded(bus)) |
Andrew Geissler | 03b3082 | 2022-03-24 11:04:40 -0500 | [diff] [blame] | 182 | { |
| 183 | info("Side switch not needed"); |
| 184 | return 0; |
| 185 | } |
| 186 | |
Andrew Geissler | cdac8f4 | 2022-03-24 15:02:13 -0500 | [diff] [blame^] | 187 | if (!powerOffSystem(bus)) |
| 188 | { |
| 189 | error("unable to power off chassis"); |
| 190 | return 0; |
| 191 | } |
| 192 | |
Andrew Geissler | 03b3082 | 2022-03-24 11:04:40 -0500 | [diff] [blame] | 193 | // TODO - Future commits in series to fill in rest of logic |
Andrew Geissler | 70d72f8 | 2022-03-23 13:15:05 -0500 | [diff] [blame] | 194 | } |