Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Tomáš Pecka <tomas.pecka@cesnet.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
Václav Kubernát | b7f2635 | 2021-11-26 14:58:33 +0100 | [diff] [blame] | 8 | #include <regex> |
Tomáš Pecka | 594a676 | 2021-01-29 11:06:08 +0100 | [diff] [blame] | 9 | #include "Firmware.h" |
Jan Kundrát | 7935e81 | 2024-09-17 17:02:01 +0200 | [diff] [blame] | 10 | #include "utils/libyang.h" |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 11 | #include "utils/log.h" |
| 12 | #include "utils/sysrepo.h" |
| 13 | |
| 14 | using namespace std::literals; |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | const auto CZECHLIGHT_SYSTEM_MODULE_NAME = "czechlight-system"s; |
| 19 | const auto CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX = "/"s + CZECHLIGHT_SYSTEM_MODULE_NAME + ":firmware/"s; |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 20 | const auto FIRMWARE_SLOTS = {"rootfs.0"s, "rootfs.1"s}; |
Václav Kubernát | b7f2635 | 2021-11-26 14:58:33 +0100 | [diff] [blame] | 21 | // Modified regex of yang:date-and-time |
| 22 | const auto DATE_TIME_REGEX = std::regex{R"(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z)"}; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | namespace velia::system { |
| 26 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 27 | Firmware::Firmware(::sysrepo::Connection srConn, sdbus::IConnection& dbusConnectionSignals, sdbus::IConnection& dbusConnectionMethods) |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 28 | : m_rauc(std::make_shared<RAUC>( |
| 29 | dbusConnectionSignals, |
| 30 | dbusConnectionMethods, |
| 31 | [this](const std::string& operation) { |
| 32 | if (operation == "installing") { |
| 33 | std::lock_guard<std::mutex> lck(m_mtx); |
Tomáš Pecka | 0eefdf1 | 2021-02-17 19:28:13 +0100 | [diff] [blame] | 34 | m_installMessage = ""; |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 35 | m_installStatus = "in-progress"; |
| 36 | } |
| 37 | }, |
| 38 | [this](int32_t perc, const std::string& msg) { |
| 39 | std::map<std::string, std::string> data = { |
| 40 | {CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/update/message", msg}, |
| 41 | {CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/update/progress", std::to_string(perc)}, |
| 42 | }; |
| 43 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 44 | std::optional<libyang::DataNode> dataNode; |
| 45 | auto session = m_srConn.sessionStart(); |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 46 | |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame] | 47 | utils::valuesToYang(data, {}, {}, session, dataNode); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 48 | session.sendNotification(*dataNode, sysrepo::Wait::No); // No need to wait, it's just a notification. |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 49 | }, |
| 50 | [this](int32_t retVal, const std::string& lastError) { |
| 51 | auto lock = updateSlotStatus(); |
| 52 | m_installStatus = retVal == 0 ? "succeeded" : "failed"; |
| 53 | m_installMessage = lastError; |
| 54 | })) |
| 55 | , m_log(spdlog::get("system")) |
| 56 | , m_srConn(std::move(srConn)) |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 57 | , m_srSessionOps(m_srConn.sessionStart()) |
| 58 | , m_srSessionRPC(m_srConn.sessionStart()) |
| 59 | , m_srSubscribeOps() |
| 60 | , m_srSubscribeRPC() |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 61 | { |
Jan Kundrát | 7a30cf4 | 2022-07-12 22:24:09 +0200 | [diff] [blame] | 62 | utils::ensureModuleImplemented(m_srSessionOps, "czechlight-system", "2022-07-08"); |
Tomáš Pecka | b78372f | 2021-04-22 11:09:20 +0200 | [diff] [blame] | 63 | |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 64 | { |
| 65 | auto raucOperation = m_rauc->operation(); |
| 66 | auto raucLastError = m_rauc->lastError(); |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 67 | |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 68 | auto lock = updateSlotStatus(); |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 69 | |
| 70 | m_installMessage = raucLastError; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 71 | |
| 72 | if (raucOperation == "installing") { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 73 | m_installStatus = "in-progress"; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 74 | } else if (!raucLastError.empty()) { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 75 | m_installStatus = "failed"; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 76 | } else { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 77 | m_installStatus = "none"; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 78 | } |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 81 | ::sysrepo::RpcActionCb cbRPC = [this](auto session, auto, auto, auto input, auto, auto, auto) { |
| 82 | auto lock = updateSlotStatus(); |
| 83 | |
| 84 | try { |
Jan Kundrát | 8dd54de | 2024-09-17 17:04:00 +0200 | [diff] [blame] | 85 | auto source = utils::asString(*input.findPath("url")); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 86 | m_rauc->install(source); |
| 87 | } catch (sdbus::Error& e) { |
| 88 | m_log->warn("RAUC install error: '{}'", e.what()); |
| 89 | utils::setErrors(session, e.getMessage()); |
| 90 | return ::sysrepo::ErrorCode::OperationFailed; |
| 91 | } |
| 92 | return ::sysrepo::ErrorCode::Ok; |
| 93 | }; |
| 94 | |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 95 | m_srSubscribeRPC = m_srSessionRPC.onRPCAction(CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/install", cbRPC); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 96 | |
Jan Kundrát | 3795cab | 2022-07-13 18:08:19 +0200 | [diff] [blame] | 97 | ::sysrepo::RpcActionCb markSlotAs = [this](auto, auto, auto path, auto input, auto, auto, auto) { |
Jan Kundrát | 8dd54de | 2024-09-17 17:04:00 +0200 | [diff] [blame] | 98 | auto bootName = utils::asString(*input.parent()->findPath("name")); |
Jan Kundrát | 3795cab | 2022-07-13 18:08:19 +0200 | [diff] [blame] | 99 | std::string action; |
| 100 | if (path == CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "firmware-slot/set-active-after-reboot") { |
| 101 | action = "active"; |
| 102 | } else if (path == CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "firmware-slot/set-unhealthy") { |
| 103 | action = "bad"; |
| 104 | } else { |
| 105 | throw std::logic_error{"action callback XPath mismatch"}; |
| 106 | } |
| 107 | std::string slot; |
| 108 | { |
| 109 | auto lock = updateSlotStatus(); |
| 110 | auto slotIt = m_bootNameToSlot.find(bootName); |
| 111 | if (slotIt == m_bootNameToSlot.end()) { |
| 112 | throw std::runtime_error{"cannot map FW slot boot name '" + bootName + "' to a RAUC slot name"}; |
| 113 | } |
| 114 | slot = slotIt->second; |
| 115 | } |
| 116 | m_log->debug("RAUC: marking boot slot {} ({}) as {}", bootName, slot, action); |
| 117 | m_rauc->mark(action, slot); |
| 118 | return ::sysrepo::ErrorCode::Ok; |
| 119 | }; |
| 120 | |
| 121 | m_srSubscribeRPC->onRPCAction(CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "firmware-slot/set-active-after-reboot", markSlotAs); |
| 122 | m_srSubscribeRPC->onRPCAction(CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "firmware-slot/set-unhealthy", markSlotAs); |
| 123 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 124 | ::sysrepo::OperGetCb cbOper = [this](auto session, auto, auto, auto, auto, auto, auto& parent) { |
| 125 | std::map<std::string, std::string> data; |
| 126 | |
| 127 | { |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 128 | auto lock = updateSlotStatus(); |
| 129 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 130 | data.insert(m_slotStatusCache.begin(), m_slotStatusCache.end()); |
| 131 | data[CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/status"] = m_installStatus; |
| 132 | data[CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/message"] = m_installMessage; |
| 133 | } |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 134 | |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame] | 135 | utils::valuesToYang(data, {}, {}, session, parent); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 136 | return ::sysrepo::ErrorCode::Ok; |
| 137 | }; |
| 138 | |
| 139 | m_srSubscribeOps = m_srSessionOps.onOperGet( |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 140 | CZECHLIGHT_SYSTEM_MODULE_NAME, |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 141 | cbOper, |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 142 | CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "*", |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 143 | ::sysrepo::SubscribeOptions::Passive | ::sysrepo::SubscribeOptions::OperMerge); |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 144 | } |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 145 | |
| 146 | /** @brief Updates the slot status cache with the new data obtained via RAUC |
| 147 | * |
| 148 | * Gets current slot status data from RAUC and updates the local slot status cache if new data are available. |
| 149 | * The methods manipulates with the local cache which is shared among multiple thread. |
| 150 | * |
| 151 | * @return an unique_lock (in locked state) that can be further used to manipulate with the local cache |
| 152 | * */ |
| 153 | std::unique_lock<std::mutex> Firmware::updateSlotStatus() |
| 154 | { |
| 155 | std::map<std::string, velia::system::RAUC::SlotProperties> slotStatus; |
Jan Kundrát | e55b56b | 2022-07-13 12:51:44 +0200 | [diff] [blame] | 156 | std::string primarySlot; |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 157 | |
| 158 | try { |
| 159 | slotStatus = m_rauc->slotStatus(); |
Jan Kundrát | e55b56b | 2022-07-13 12:51:44 +0200 | [diff] [blame] | 160 | primarySlot = m_rauc->primarySlot(); |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 161 | } catch (sdbus::Error& e) { |
| 162 | m_log->warn("Could not fetch RAUC slot status data: {}", e.getMessage()); |
| 163 | } |
| 164 | |
| 165 | std::unique_lock<std::mutex> lck(m_mtx); |
Jan Kundrát | 3795cab | 2022-07-13 18:08:19 +0200 | [diff] [blame] | 166 | m_bootNameToSlot.clear(); |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 167 | |
| 168 | for (const auto& slotName : FIRMWARE_SLOTS) { |
| 169 | if (auto it = slotStatus.find(slotName); it != slotStatus.end()) { // if there is an update for the slot "slotName" |
Václav Kubernát | b7f2635 | 2021-11-26 14:58:33 +0100 | [diff] [blame] | 170 | auto& props = it->second; |
Tomáš Pecka | 825542f | 2021-02-24 14:00:10 +0100 | [diff] [blame] | 171 | std::string xpathPrefix; |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 172 | |
Tomáš Pecka | 825542f | 2021-02-24 14:00:10 +0100 | [diff] [blame] | 173 | // Better be defensive about provided properties. If somebody removes /slot.raucs, RAUC doesn't provide all the data (at least bundle.version and installed.timestamp). |
| 174 | if (auto pit = props.find("bootname"); pit != props.end()) { |
| 175 | xpathPrefix = CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "firmware-slot[name='" + std::get<std::string>(pit->second) + "']/"; |
Jan Kundrát | 3795cab | 2022-07-13 18:08:19 +0200 | [diff] [blame] | 176 | m_bootNameToSlot[std::get<std::string>(pit->second)] = slotName; |
Tomáš Pecka | 825542f | 2021-02-24 14:00:10 +0100 | [diff] [blame] | 177 | } else { |
Jan Kundrát | e26ac36 | 2023-12-19 18:04:27 +0100 | [diff] [blame] | 178 | m_log->error("RAUC didn't provide 'bootname' property for slot '{}'. Skipping update for that slot.", slotName); |
Tomáš Pecka | 825542f | 2021-02-24 14:00:10 +0100 | [diff] [blame] | 179 | continue; |
| 180 | } |
| 181 | |
Václav Kubernát | b7f2635 | 2021-11-26 14:58:33 +0100 | [diff] [blame] | 182 | // sysrepo needs the "-00:00" suffix instead of the "Z" suffix. |
| 183 | if (auto pit = props.find("installed.timestamp"); pit != props.end()) { |
| 184 | auto& ts = std::get<std::string>(pit->second); |
| 185 | if (std::regex_match(ts, DATE_TIME_REGEX)) { |
| 186 | ts.pop_back(); // Get rid of the "Z". |
| 187 | ts.append("-00:00"); |
| 188 | } else { |
| 189 | m_log->warn("RAUC provided a timestamp in an unexpected format: {}", ts); |
| 190 | } |
| 191 | } |
| 192 | |
Jan Kundrát | 58b39ae | 2022-07-08 18:54:10 +0200 | [diff] [blame] | 193 | for (const auto& [yangKey, raucKey] : {std::pair{"version", "bundle.version"}, {"installed", "installed.timestamp"}}) { |
Tomáš Pecka | 825542f | 2021-02-24 14:00:10 +0100 | [diff] [blame] | 194 | if (auto pit = props.find(raucKey); pit != props.end()) { |
| 195 | m_slotStatusCache[xpathPrefix + yangKey] = std::get<std::string>(pit->second); |
| 196 | } else { |
| 197 | m_log->warn("RAUC didn't provide '{}' property for slot '{}'.", raucKey, slotName); |
| 198 | } |
| 199 | } |
Jan Kundrát | 58b39ae | 2022-07-08 18:54:10 +0200 | [diff] [blame] | 200 | |
| 201 | if (auto pit = props.find("state"); pit != props.end()) { |
| 202 | m_slotStatusCache[xpathPrefix + "is-booted-now"] = (std::get<std::string>(pit->second) == "booted" ? "true" : "false"); |
| 203 | } else { |
| 204 | m_log->warn("RAUC didn't provide 'state' property for slot '{}'.", slotName); |
| 205 | } |
| 206 | if (auto pit = props.find("boot-status"); pit != props.end()) { |
| 207 | m_slotStatusCache[xpathPrefix + "is-healthy"] = (std::get<std::string>(pit->second) == "good" ? "true" : "false"); |
| 208 | } else { |
| 209 | m_log->warn("RAUC didn't provide 'boot-status' property for slot '{}'.", slotName); |
| 210 | } |
Jan Kundrát | e55b56b | 2022-07-13 12:51:44 +0200 | [diff] [blame] | 211 | m_slotStatusCache[xpathPrefix + "will-boot-next"] = (slotName == primarySlot ? "true" : "false"); |
Jan Kundrát | 58b39ae | 2022-07-08 18:54:10 +0200 | [diff] [blame] | 212 | |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | return lck; |
| 217 | } |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 218 | } |