Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "item_updater.hpp" |
| 4 | |
| 5 | #include "images.hpp" |
| 6 | #include "serialize.hpp" |
| 7 | #include "version.hpp" |
Chanh Nguyen | 1fd6ddd | 2021-01-06 11:09:09 +0700 | [diff] [blame] | 8 | #include "xyz/openbmc_project/Software/ExtendedVersion/server.hpp" |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 9 | #include "xyz/openbmc_project/Software/Version/server.hpp" |
| 10 | |
Adriana Kobylak | d5b8f75 | 2019-05-01 11:52:35 -0500 | [diff] [blame] | 11 | #include <phosphor-logging/elog-errors.hpp> |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 12 | #include <phosphor-logging/elog.hpp> |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 13 | #include <phosphor-logging/lg2.hpp> |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 14 | #include <xyz/openbmc_project/Common/error.hpp> |
| 15 | #include <xyz/openbmc_project/Software/Image/error.hpp> |
| 16 | |
| 17 | #include <filesystem> |
| 18 | #include <fstream> |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 19 | #include <queue> |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 20 | #include <set> |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 21 | #include <string> |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 22 | |
| 23 | namespace phosphor |
| 24 | { |
| 25 | namespace software |
| 26 | { |
| 27 | namespace updater |
| 28 | { |
| 29 | |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 30 | // When you see server:: you know we're referencing our base class |
| 31 | namespace server = sdbusplus::xyz::openbmc_project::Software::server; |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 32 | namespace control = sdbusplus::xyz::openbmc_project::Control::server; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 33 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 34 | PHOSPHOR_LOG2_USING; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 35 | using namespace phosphor::logging; |
Adriana Kobylak | 43699ca | 2018-10-17 14:56:29 -0500 | [diff] [blame] | 36 | using namespace sdbusplus::xyz::openbmc_project::Software::Image::Error; |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 37 | using namespace phosphor::software::image; |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame] | 38 | namespace fs = std::filesystem; |
Adriana Kobylak | d5b8f75 | 2019-05-01 11:52:35 -0500 | [diff] [blame] | 39 | using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 40 | |
Patrick Williams | bf2bb2b | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 41 | void ItemUpdater::createActivation(sdbusplus::message_t& msg) |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 42 | { |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 43 | |
| 44 | using SVersion = server::Version; |
| 45 | using VersionPurpose = SVersion::VersionPurpose; |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 46 | using VersionClass = phosphor::software::manager::Version; |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 47 | |
Patrick Williams | bc1facd | 2020-06-03 05:58:27 -0500 | [diff] [blame] | 48 | sdbusplus::message::object_path objPath; |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 49 | auto purpose = VersionPurpose::Unknown; |
Chanh Nguyen | 1fd6ddd | 2021-01-06 11:09:09 +0700 | [diff] [blame] | 50 | std::string extendedVersion; |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 51 | std::string version; |
Justin Ledford | 054bb0b | 2022-03-15 15:46:58 -0700 | [diff] [blame] | 52 | std::map<std::string, |
| 53 | std::map<std::string, |
| 54 | std::variant<std::string, std::vector<std::string>>>> |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 55 | interfaces; |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 56 | msg.read(objPath, interfaces); |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 57 | std::string path(std::move(objPath)); |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 58 | std::string filePath; |
Justin Ledford | 054bb0b | 2022-03-15 15:46:58 -0700 | [diff] [blame] | 59 | std::vector<std::string> compatibleNames; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 60 | |
| 61 | for (const auto& intf : interfaces) |
| 62 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 63 | if (intf.first == VERSION_IFACE) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 64 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 65 | for (const auto& property : intf.second) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 66 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 67 | if (property.first == "Purpose") |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 68 | { |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 69 | auto value = SVersion::convertVersionPurposeFromString( |
Patrick Williams | e883fb8 | 2020-05-13 11:38:55 -0500 | [diff] [blame] | 70 | std::get<std::string>(property.second)); |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 71 | if (value == VersionPurpose::BMC || |
Vijay Khemka | e9f6c84 | 2020-01-14 14:32:39 -0800 | [diff] [blame] | 72 | #ifdef HOST_BIOS_UPGRADE |
| 73 | value == VersionPurpose::Host || |
| 74 | #endif |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 75 | value == VersionPurpose::System) |
| 76 | { |
| 77 | purpose = value; |
| 78 | } |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 79 | } |
| 80 | else if (property.first == "Version") |
| 81 | { |
Patrick Williams | e883fb8 | 2020-05-13 11:38:55 -0500 | [diff] [blame] | 82 | version = std::get<std::string>(property.second); |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 86 | else if (intf.first == FILEPATH_IFACE) |
| 87 | { |
| 88 | for (const auto& property : intf.second) |
| 89 | { |
| 90 | if (property.first == "Path") |
| 91 | { |
Patrick Williams | e883fb8 | 2020-05-13 11:38:55 -0500 | [diff] [blame] | 92 | filePath = std::get<std::string>(property.second); |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
Chanh Nguyen | 1fd6ddd | 2021-01-06 11:09:09 +0700 | [diff] [blame] | 96 | else if (intf.first == EXTENDED_VERSION_IFACE) |
| 97 | { |
| 98 | for (const auto& property : intf.second) |
| 99 | { |
| 100 | if (property.first == "ExtendedVersion") |
| 101 | { |
| 102 | extendedVersion = std::get<std::string>(property.second); |
| 103 | } |
| 104 | } |
| 105 | } |
Justin Ledford | 054bb0b | 2022-03-15 15:46:58 -0700 | [diff] [blame] | 106 | else if (intf.first == COMPATIBLE_IFACE) |
| 107 | { |
| 108 | for (const auto& property : intf.second) |
| 109 | { |
| 110 | if (property.first == "Names") |
| 111 | { |
| 112 | compatibleNames = |
| 113 | std::get<std::vector<std::string>>(property.second); |
| 114 | } |
| 115 | } |
| 116 | } |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 117 | } |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 118 | if (version.empty() || filePath.empty() || |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 119 | purpose == VersionPurpose::Unknown) |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 120 | { |
| 121 | return; |
| 122 | } |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 123 | |
| 124 | // Version id is the last item in the path |
| 125 | auto pos = path.rfind("/"); |
| 126 | if (pos == std::string::npos) |
| 127 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 128 | error("No version id found in object path: {PATH}", "PATH", path); |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 129 | return; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | auto versionId = path.substr(pos + 1); |
| 133 | |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 134 | if (activations.find(versionId) == activations.end()) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 135 | { |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 136 | // Determine the Activation state by processing the given image dir. |
| 137 | auto activationState = server::Activation::Activations::Invalid; |
Vijay Khemka | e9f6c84 | 2020-01-14 14:32:39 -0800 | [diff] [blame] | 138 | ItemUpdater::ActivationStatus result; |
| 139 | if (purpose == VersionPurpose::BMC || purpose == VersionPurpose::System) |
| 140 | result = ItemUpdater::validateSquashFSImage(filePath); |
| 141 | else |
| 142 | result = ItemUpdater::ActivationStatus::ready; |
| 143 | |
Gunnar Mills | 43b25cd | 2017-09-07 13:19:34 -0500 | [diff] [blame] | 144 | AssociationList associations = {}; |
| 145 | |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 146 | if (result == ItemUpdater::ActivationStatus::ready) |
| 147 | { |
| 148 | activationState = server::Activation::Activations::Ready; |
Gunnar Mills | 43b25cd | 2017-09-07 13:19:34 -0500 | [diff] [blame] | 149 | // Create an association to the BMC inventory item |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 150 | associations.emplace_back( |
| 151 | std::make_tuple(ACTIVATION_FWD_ASSOCIATION, |
| 152 | ACTIVATION_REV_ASSOCIATION, bmcInventoryPath)); |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 153 | } |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 154 | |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 155 | auto versionPtr = std::make_unique<VersionClass>( |
Chanh Nguyen | 1fd6ddd | 2021-01-06 11:09:09 +0700 | [diff] [blame] | 156 | bus, path, version, purpose, extendedVersion, filePath, |
Justin Ledford | 054bb0b | 2022-03-15 15:46:58 -0700 | [diff] [blame] | 157 | compatibleNames, |
Adriana Kobylak | 59b640b | 2022-01-21 19:45:22 +0000 | [diff] [blame] | 158 | std::bind(&ItemUpdater::erase, this, std::placeholders::_1), |
| 159 | versionId); |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 160 | versionPtr->deleteObject = |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 161 | std::make_unique<phosphor::software::manager::Delete>(bus, path, |
| 162 | *versionPtr); |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 163 | versions.insert(std::make_pair(versionId, std::move(versionPtr))); |
Adriana Kobylak | 88ba1f9 | 2022-03-09 21:14:15 +0000 | [diff] [blame] | 164 | |
| 165 | activations.insert(std::make_pair( |
| 166 | versionId, |
| 167 | std::make_unique<Activation>(bus, path, *this, versionId, |
| 168 | activationState, associations))); |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 169 | } |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 170 | return; |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 171 | } |
| 172 | |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 173 | void ItemUpdater::processBMCImage() |
| 174 | { |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 175 | using VersionClass = phosphor::software::manager::Version; |
Lei YU | 269bff3 | 2018-08-21 15:21:40 +0800 | [diff] [blame] | 176 | |
| 177 | // Check MEDIA_DIR and create if it does not exist |
| 178 | try |
| 179 | { |
| 180 | if (!fs::is_directory(MEDIA_DIR)) |
| 181 | { |
| 182 | fs::create_directory(MEDIA_DIR); |
| 183 | } |
| 184 | } |
| 185 | catch (const fs::filesystem_error& e) |
| 186 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 187 | error("Failed to prepare dir: {ERROR}", "ERROR", e); |
Lei YU | 269bff3 | 2018-08-21 15:21:40 +0800 | [diff] [blame] | 188 | return; |
| 189 | } |
| 190 | |
Adriana Kobylak | 1e81f23 | 2022-01-18 22:28:47 +0000 | [diff] [blame] | 191 | // Functional images are mounted as rofs-<location>-functional |
| 192 | constexpr auto functionalSuffix = "-functional"; |
Lei YU | d474d9c | 2021-12-10 16:21:21 +0800 | [diff] [blame] | 193 | bool functionalFound = false; |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 194 | |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 195 | // Read os-release from folders under /media/ to get |
| 196 | // BMC Software Versions. |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 197 | for (const auto& iter : fs::directory_iterator(MEDIA_DIR)) |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 198 | { |
| 199 | auto activationState = server::Activation::Activations::Active; |
Saqib Khan | 6fab70d | 2017-09-07 00:13:50 -0500 | [diff] [blame] | 200 | static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 201 | |
| 202 | // Check if the BMC_RO_PREFIXis the prefix of the iter.path |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 203 | if (0 == |
| 204 | iter.path().native().compare(0, BMC_RO_PREFIX_LEN, BMC_ROFS_PREFIX)) |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 205 | { |
Adriana Kobylak | 716cd78 | 2020-06-08 13:27:43 -0500 | [diff] [blame] | 206 | // Get the version to calculate the id |
Adriana Kobylak | 24a8d83 | 2020-06-10 08:29:36 -0500 | [diff] [blame] | 207 | fs::path releaseFile(OS_RELEASE_FILE); |
| 208 | auto osRelease = iter.path() / releaseFile.relative_path(); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 209 | if (!fs::is_regular_file(osRelease)) |
| 210 | { |
Lei YU | e56bf87 | 2022-02-22 18:40:37 +0800 | [diff] [blame] | 211 | #ifdef BMC_STATIC_DUAL_IMAGE |
| 212 | // For dual image, it is possible that the secondary image is |
| 213 | // empty or contains invalid data, ignore such case. |
| 214 | info("Unable to find osRelease: {PATH}", "PATH", osRelease); |
| 215 | #else |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 216 | error("Failed to read osRelease: {PATH}", "PATH", osRelease); |
Adriana Kobylak | 716cd78 | 2020-06-08 13:27:43 -0500 | [diff] [blame] | 217 | |
| 218 | // Try to get the version id from the mount directory name and |
| 219 | // call to delete it as this version may be corrupted. Dynamic |
| 220 | // volumes created by the UBI layout for example have the id in |
| 221 | // the mount directory name. The worst that can happen is that |
| 222 | // erase() is called with an non-existent id and returns. |
| 223 | auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN); |
Saqib Khan | 021c365 | 2017-09-26 12:11:02 -0500 | [diff] [blame] | 224 | ItemUpdater::erase(id); |
Lei YU | e56bf87 | 2022-02-22 18:40:37 +0800 | [diff] [blame] | 225 | #endif |
Adriana Kobylak | 716cd78 | 2020-06-08 13:27:43 -0500 | [diff] [blame] | 226 | |
Saqib Khan | 021c365 | 2017-09-26 12:11:02 -0500 | [diff] [blame] | 227 | continue; |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 228 | } |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 229 | auto version = VersionClass::getBMCVersion(osRelease); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 230 | if (version.empty()) |
| 231 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 232 | error("Failed to read version from osRelease: {PATH}", "PATH", |
| 233 | osRelease); |
Adriana Kobylak | 716cd78 | 2020-06-08 13:27:43 -0500 | [diff] [blame] | 234 | |
| 235 | // Try to delete the version, same as above if the |
| 236 | // OS_RELEASE_FILE does not exist. |
| 237 | auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN); |
| 238 | ItemUpdater::erase(id); |
| 239 | |
| 240 | continue; |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 241 | } |
Saqib Khan | 021c365 | 2017-09-26 12:11:02 -0500 | [diff] [blame] | 242 | |
Adriana Kobylak | 59b640b | 2022-01-21 19:45:22 +0000 | [diff] [blame] | 243 | // The flash location is part of the mount name: rofs-<location> |
| 244 | auto flashId = iter.path().native().substr(BMC_RO_PREFIX_LEN); |
| 245 | |
| 246 | auto id = VersionClass::getId(version + flashId); |
Adriana Kobylak | 716cd78 | 2020-06-08 13:27:43 -0500 | [diff] [blame] | 247 | |
Adriana Kobylak | f383d27 | 2020-06-16 15:17:22 -0500 | [diff] [blame] | 248 | // Check if the id has already been added. This can happen if the |
| 249 | // BMC partitions / devices were manually flashed with the same |
| 250 | // image. |
| 251 | if (versions.find(id) != versions.end()) |
| 252 | { |
| 253 | continue; |
| 254 | } |
| 255 | |
Adriana Kobylak | 1e81f23 | 2022-01-18 22:28:47 +0000 | [diff] [blame] | 256 | auto functional = false; |
| 257 | if (iter.path().native().find(functionalSuffix) != |
| 258 | std::string::npos) |
| 259 | { |
| 260 | // Set functional to true and remove the functional suffix |
| 261 | functional = true; |
| 262 | flashId.erase(flashId.length() - strlen(functionalSuffix)); |
Lei YU | d474d9c | 2021-12-10 16:21:21 +0800 | [diff] [blame] | 263 | functionalFound = true; |
Adriana Kobylak | 1e81f23 | 2022-01-18 22:28:47 +0000 | [diff] [blame] | 264 | } |
Adriana Kobylak | 780220f | 2022-01-18 20:01:53 +0000 | [diff] [blame] | 265 | |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 266 | auto purpose = server::Version::VersionPurpose::BMC; |
Adriana Kobylak | 780220f | 2022-01-18 20:01:53 +0000 | [diff] [blame] | 267 | restorePurpose(flashId, purpose); |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 268 | |
Chanh Nguyen | 1fd6ddd | 2021-01-06 11:09:09 +0700 | [diff] [blame] | 269 | // Read os-release from /etc/ to get the BMC extended version |
| 270 | std::string extendedVersion = |
| 271 | VersionClass::getBMCExtendedVersion(osRelease); |
| 272 | |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 273 | auto path = fs::path(SOFTWARE_OBJPATH) / id; |
| 274 | |
Lei YU | 269bff3 | 2018-08-21 15:21:40 +0800 | [diff] [blame] | 275 | // Create functional association if this is the functional |
| 276 | // version |
Adriana Kobylak | 1e81f23 | 2022-01-18 22:28:47 +0000 | [diff] [blame] | 277 | if (functional) |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 278 | { |
| 279 | createFunctionalAssociation(path); |
| 280 | } |
| 281 | |
Gunnar Mills | 43b25cd | 2017-09-07 13:19:34 -0500 | [diff] [blame] | 282 | AssociationList associations = {}; |
| 283 | |
| 284 | if (activationState == server::Activation::Activations::Active) |
| 285 | { |
| 286 | // Create an association to the BMC inventory item |
| 287 | associations.emplace_back(std::make_tuple( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 288 | ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION, |
| 289 | bmcInventoryPath)); |
Gunnar Mills | 43b25cd | 2017-09-07 13:19:34 -0500 | [diff] [blame] | 290 | |
| 291 | // Create an active association since this image is active |
| 292 | createActiveAssociation(path); |
| 293 | } |
| 294 | |
AppaRao Puli | bbebec7 | 2020-01-28 23:57:41 +0530 | [diff] [blame] | 295 | // All updateable firmware components must expose the updateable |
| 296 | // association. |
| 297 | createUpdateableAssociation(path); |
| 298 | |
Adriana Kobylak | ee590c7 | 2017-09-26 15:16:06 -0500 | [diff] [blame] | 299 | // Create Version instance for this version. |
| 300 | auto versionPtr = std::make_unique<VersionClass>( |
Adriana Kobylak | a84f06d | 2022-01-18 15:41:57 +0000 | [diff] [blame] | 301 | bus, path, version, purpose, extendedVersion, flashId, |
Justin Ledford | 054bb0b | 2022-03-15 15:46:58 -0700 | [diff] [blame] | 302 | std::vector<std::string>(), |
Adriana Kobylak | 59b640b | 2022-01-21 19:45:22 +0000 | [diff] [blame] | 303 | std::bind(&ItemUpdater::erase, this, std::placeholders::_1), |
| 304 | id); |
Adriana Kobylak | 1e81f23 | 2022-01-18 22:28:47 +0000 | [diff] [blame] | 305 | if (functional) |
| 306 | { |
| 307 | versionPtr->setFunctional(true); |
| 308 | } |
| 309 | else |
Michael Tritz | 4254bec | 2017-10-03 17:18:22 -0500 | [diff] [blame] | 310 | { |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 311 | versionPtr->deleteObject = |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 312 | std::make_unique<phosphor::software::manager::Delete>( |
| 313 | bus, path, *versionPtr); |
Michael Tritz | 4254bec | 2017-10-03 17:18:22 -0500 | [diff] [blame] | 314 | } |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 315 | versions.insert(std::make_pair(id, std::move(versionPtr))); |
Michael Tritz | 4254bec | 2017-10-03 17:18:22 -0500 | [diff] [blame] | 316 | |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 317 | // Create Activation instance for this version. |
| 318 | activations.insert(std::make_pair( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 319 | id, std::make_unique<Activation>( |
| 320 | bus, path, *this, id, activationState, associations))); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 321 | |
Lei YU | bdf2d6c | 2021-12-15 14:05:01 +0800 | [diff] [blame] | 322 | #ifdef BMC_STATIC_DUAL_IMAGE |
| 323 | uint8_t priority; |
| 324 | if ((functional && (runningImageSlot == 0)) || |
| 325 | (!functional && (runningImageSlot == 1))) |
| 326 | { |
| 327 | priority = 0; |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | priority = 1; |
| 332 | } |
| 333 | activations.find(id)->second->redundancyPriority = |
| 334 | std::make_unique<RedundancyPriority>( |
| 335 | bus, path, *(activations.find(id)->second), priority, |
| 336 | false); |
| 337 | #else |
Lei YU | 269bff3 | 2018-08-21 15:21:40 +0800 | [diff] [blame] | 338 | // If Active, create RedundancyPriority instance for this |
| 339 | // version. |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 340 | if (activationState == server::Activation::Activations::Active) |
| 341 | { |
| 342 | uint8_t priority = std::numeric_limits<uint8_t>::max(); |
Adriana Kobylak | 780220f | 2022-01-18 20:01:53 +0000 | [diff] [blame] | 343 | if (!restorePriority(flashId, priority)) |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 344 | { |
Adriana Kobylak | 1e81f23 | 2022-01-18 22:28:47 +0000 | [diff] [blame] | 345 | if (functional) |
Adriana Kobylak | ee590c7 | 2017-09-26 15:16:06 -0500 | [diff] [blame] | 346 | { |
| 347 | priority = 0; |
| 348 | } |
| 349 | else |
| 350 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 351 | error( |
| 352 | "Unable to restore priority from file for {VERSIONID}", |
| 353 | "VERSIONID", id); |
Adriana Kobylak | ee590c7 | 2017-09-26 15:16:06 -0500 | [diff] [blame] | 354 | } |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 355 | } |
| 356 | activations.find(id)->second->redundancyPriority = |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 357 | std::make_unique<RedundancyPriority>( |
| 358 | bus, path, *(activations.find(id)->second), priority, |
| 359 | false); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 360 | } |
Lei YU | bdf2d6c | 2021-12-15 14:05:01 +0800 | [diff] [blame] | 361 | #endif |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 362 | } |
| 363 | } |
Saqib Khan | dcbfa04 | 2017-09-18 13:08:39 -0500 | [diff] [blame] | 364 | |
Lei YU | d474d9c | 2021-12-10 16:21:21 +0800 | [diff] [blame] | 365 | if (!functionalFound) |
Saqib Khan | dcbfa04 | 2017-09-18 13:08:39 -0500 | [diff] [blame] | 366 | { |
Lei YU | d474d9c | 2021-12-10 16:21:21 +0800 | [diff] [blame] | 367 | // If there is no functional version found, read the /etc/os-release and |
| 368 | // create rofs-<versionId>-functional under MEDIA_DIR, then call again |
| 369 | // processBMCImage() to create the D-Bus interface for it. |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 370 | auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE); |
Adriana Kobylak | 59b640b | 2022-01-21 19:45:22 +0000 | [diff] [blame] | 371 | auto id = phosphor::software::manager::Version::getId(version + |
| 372 | functionalSuffix); |
Adriana Kobylak | 1e81f23 | 2022-01-18 22:28:47 +0000 | [diff] [blame] | 373 | auto versionFileDir = BMC_ROFS_PREFIX + id + functionalSuffix + "/etc/"; |
Saqib Khan | dcbfa04 | 2017-09-18 13:08:39 -0500 | [diff] [blame] | 374 | try |
| 375 | { |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 376 | if (!fs::is_directory(versionFileDir)) |
Saqib Khan | dcbfa04 | 2017-09-18 13:08:39 -0500 | [diff] [blame] | 377 | { |
| 378 | fs::create_directories(versionFileDir); |
| 379 | } |
Adriana Kobylak | 1e81f23 | 2022-01-18 22:28:47 +0000 | [diff] [blame] | 380 | auto versionFilePath = |
| 381 | BMC_ROFS_PREFIX + id + functionalSuffix + OS_RELEASE_FILE; |
Saqib Khan | dcbfa04 | 2017-09-18 13:08:39 -0500 | [diff] [blame] | 382 | fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath); |
| 383 | ItemUpdater::processBMCImage(); |
| 384 | } |
| 385 | catch (const std::exception& e) |
| 386 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 387 | error("Exception during processing: {ERROR}", "ERROR", e); |
Saqib Khan | dcbfa04 | 2017-09-18 13:08:39 -0500 | [diff] [blame] | 388 | } |
| 389 | } |
Eddie James | eaa1ee0 | 2018-03-01 10:09:10 -0600 | [diff] [blame] | 390 | |
| 391 | mirrorUbootToAlt(); |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 392 | return; |
| 393 | } |
| 394 | |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 395 | void ItemUpdater::erase(std::string entryId) |
| 396 | { |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 397 | // Find entry in versions map |
| 398 | auto it = versions.find(entryId); |
| 399 | if (it != versions.end()) |
| 400 | { |
Lei YU | 0f88b5a | 2018-08-21 15:28:53 +0800 | [diff] [blame] | 401 | if (it->second->isFunctional() && ACTIVE_BMC_MAX_ALLOWED > 1) |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 402 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 403 | error( |
| 404 | "Version ({VERSIONID}) is currently running on the BMC; unable to remove.", |
| 405 | "VERSIONID", entryId); |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 406 | return; |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 407 | } |
Adriana Kobylak | d1a55ad | 2020-06-11 14:01:28 -0500 | [diff] [blame] | 408 | } |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 409 | |
Adriana Kobylak | d1a55ad | 2020-06-11 14:01:28 -0500 | [diff] [blame] | 410 | // First call resetUbootEnvVars() so that the BMC points to a valid image to |
| 411 | // boot from. If resetUbootEnvVars() is called after the image is actually |
| 412 | // deleted from the BMC flash, there'd be a time window where the BMC would |
| 413 | // be pointing to a non-existent image to boot from. |
| 414 | // Need to remove the entries from the activations map before that call so |
| 415 | // that resetUbootEnvVars() doesn't use the version to be deleted. |
| 416 | auto iteratorActivations = activations.find(entryId); |
| 417 | if (iteratorActivations == activations.end()) |
| 418 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 419 | error( |
| 420 | "Failed to find version ({VERSIONID}) in item updater activations map; unable to remove.", |
| 421 | "VERSIONID", entryId); |
Adriana Kobylak | d1a55ad | 2020-06-11 14:01:28 -0500 | [diff] [blame] | 422 | } |
| 423 | else |
| 424 | { |
| 425 | removeAssociations(iteratorActivations->second->path); |
Zami Seck | ae06d76 | 2021-08-13 20:11:15 -0500 | [diff] [blame] | 426 | iteratorActivations->second->deleteImageManagerObject(); |
Adriana Kobylak | d1a55ad | 2020-06-11 14:01:28 -0500 | [diff] [blame] | 427 | this->activations.erase(entryId); |
| 428 | } |
| 429 | ItemUpdater::resetUbootEnvVars(); |
| 430 | |
| 431 | if (it != versions.end()) |
| 432 | { |
Adriana Kobylak | 780220f | 2022-01-18 20:01:53 +0000 | [diff] [blame] | 433 | auto flashId = it->second->path(); |
| 434 | |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 435 | // Delete version data if it has been installed on flash (path is not |
| 436 | // the upload directory) |
| 437 | if (flashId.find(IMG_UPLOAD_DIR) == std::string::npos) |
| 438 | { |
| 439 | removeReadOnlyPartition(entryId); |
| 440 | removePersistDataDirectory(flashId); |
| 441 | helper.clearEntry(flashId); |
| 442 | } |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 443 | |
| 444 | // Removing entry in versions map |
| 445 | this->versions.erase(entryId); |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 446 | } |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 447 | |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 448 | return; |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 449 | } |
| 450 | |
Michael Tritz | bc1bf3a | 2017-09-18 16:38:23 -0500 | [diff] [blame] | 451 | void ItemUpdater::deleteAll() |
| 452 | { |
Adriana Kobylak | 83cd21f | 2018-02-28 15:48:48 -0600 | [diff] [blame] | 453 | std::vector<std::string> deletableVersions; |
| 454 | |
Michael Tritz | bc1bf3a | 2017-09-18 16:38:23 -0500 | [diff] [blame] | 455 | for (const auto& versionIt : versions) |
| 456 | { |
| 457 | if (!versionIt.second->isFunctional()) |
| 458 | { |
Adriana Kobylak | 83cd21f | 2018-02-28 15:48:48 -0600 | [diff] [blame] | 459 | deletableVersions.push_back(versionIt.first); |
Michael Tritz | bc1bf3a | 2017-09-18 16:38:23 -0500 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
Adriana Kobylak | 83cd21f | 2018-02-28 15:48:48 -0600 | [diff] [blame] | 463 | for (const auto& deletableIt : deletableVersions) |
| 464 | { |
| 465 | ItemUpdater::erase(deletableIt); |
| 466 | } |
| 467 | |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 468 | helper.cleanup(); |
Michael Tritz | bc1bf3a | 2017-09-18 16:38:23 -0500 | [diff] [blame] | 469 | } |
| 470 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 471 | ItemUpdater::ActivationStatus |
| 472 | ItemUpdater::validateSquashFSImage(const std::string& filePath) |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 473 | { |
Bright Cheng | 8e9ccfe | 2019-11-18 16:18:44 +0800 | [diff] [blame] | 474 | bool valid = true; |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 475 | |
Bright Cheng | 8e9ccfe | 2019-11-18 16:18:44 +0800 | [diff] [blame] | 476 | // Record the images which are being updated |
| 477 | // First check for the fullimage, then check for images with partitions |
| 478 | imageUpdateList.push_back(bmcFullImages); |
| 479 | valid = checkImage(filePath, imageUpdateList); |
| 480 | if (!valid) |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 481 | { |
Bright Cheng | 8e9ccfe | 2019-11-18 16:18:44 +0800 | [diff] [blame] | 482 | imageUpdateList.clear(); |
| 483 | imageUpdateList.assign(bmcImages.begin(), bmcImages.end()); |
| 484 | valid = checkImage(filePath, imageUpdateList); |
| 485 | if (!valid) |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 486 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 487 | error("Failed to find the needed BMC images."); |
Bright Cheng | 8e9ccfe | 2019-11-18 16:18:44 +0800 | [diff] [blame] | 488 | return ItemUpdater::ActivationStatus::invalid; |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 489 | } |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 490 | } |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 491 | |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 492 | return ItemUpdater::ActivationStatus::ready; |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 493 | } |
| 494 | |
Adriana Kobylak | bbcb7be | 2018-07-17 15:47:34 -0500 | [diff] [blame] | 495 | void ItemUpdater::savePriority(const std::string& versionId, uint8_t value) |
| 496 | { |
Adriana Kobylak | 780220f | 2022-01-18 20:01:53 +0000 | [diff] [blame] | 497 | auto flashId = versions.find(versionId)->second->path(); |
| 498 | storePriority(flashId, value); |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 499 | helper.setEntry(flashId, value); |
Adriana Kobylak | bbcb7be | 2018-07-17 15:47:34 -0500 | [diff] [blame] | 500 | } |
| 501 | |
Saqib Khan | b9da663 | 2017-09-13 09:48:37 -0500 | [diff] [blame] | 502 | void ItemUpdater::freePriority(uint8_t value, const std::string& versionId) |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 503 | { |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 504 | std::map<std::string, uint8_t> priorityMap; |
| 505 | |
| 506 | // Insert the requested version and priority, it may not exist yet. |
| 507 | priorityMap.insert(std::make_pair(versionId, value)); |
| 508 | |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 509 | for (const auto& intf : activations) |
| 510 | { |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 511 | if (intf.second->redundancyPriority) |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 512 | { |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 513 | priorityMap.insert(std::make_pair( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 514 | intf.first, intf.second->redundancyPriority.get()->priority())); |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 515 | } |
| 516 | } |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 517 | |
| 518 | // Lambda function to compare 2 priority values, use <= to allow duplicates |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 519 | typedef std::function<bool(std::pair<std::string, uint8_t>, |
| 520 | std::pair<std::string, uint8_t>)> |
| 521 | cmpPriority; |
| 522 | cmpPriority cmpPriorityFunc = |
| 523 | [](std::pair<std::string, uint8_t> priority1, |
| 524 | std::pair<std::string, uint8_t> priority2) { |
| 525 | return priority1.second <= priority2.second; |
| 526 | }; |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 527 | |
| 528 | // Sort versions by ascending priority |
| 529 | std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 530 | priorityMap.begin(), priorityMap.end(), cmpPriorityFunc); |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 531 | |
| 532 | auto freePriorityValue = value; |
| 533 | for (auto& element : prioritySet) |
| 534 | { |
| 535 | if (element.first == versionId) |
| 536 | { |
| 537 | continue; |
| 538 | } |
| 539 | if (element.second == freePriorityValue) |
| 540 | { |
| 541 | ++freePriorityValue; |
| 542 | auto it = activations.find(element.first); |
| 543 | it->second->redundancyPriority.get()->sdbusPriority( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 544 | freePriorityValue); |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
| 548 | auto lowestVersion = prioritySet.begin()->first; |
| 549 | if (value == prioritySet.begin()->second) |
| 550 | { |
| 551 | lowestVersion = versionId; |
| 552 | } |
| 553 | updateUbootEnvVars(lowestVersion); |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 554 | } |
| 555 | |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 556 | void ItemUpdater::reset() |
| 557 | { |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 558 | helper.factoryReset(); |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 559 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 560 | info("BMC factory reset will take effect upon reboot."); |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 561 | } |
| 562 | |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 563 | void ItemUpdater::removeReadOnlyPartition(std::string versionId) |
| 564 | { |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 565 | auto flashId = versions.find(versionId)->second->path(); |
| 566 | helper.removeVersion(flashId); |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 567 | } |
| 568 | |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 569 | bool ItemUpdater::fieldModeEnabled(bool value) |
| 570 | { |
| 571 | // enabling field mode is intended to be one way: false -> true |
| 572 | if (value && !control::FieldMode::fieldModeEnabled()) |
| 573 | { |
| 574 | control::FieldMode::fieldModeEnabled(value); |
| 575 | |
Adriana Kobylak | 22848ec | 2019-10-28 10:08:39 -0500 | [diff] [blame] | 576 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 577 | SYSTEMD_INTERFACE, "StartUnit"); |
| 578 | method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service", |
| 579 | "replace"); |
| 580 | bus.call_noreply(method); |
| 581 | |
| 582 | method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 583 | SYSTEMD_INTERFACE, "StopUnit"); |
| 584 | method.append("usr-local.mount", "replace"); |
| 585 | bus.call_noreply(method); |
| 586 | |
| 587 | std::vector<std::string> usrLocal = {"usr-local.mount"}; |
| 588 | |
| 589 | method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 590 | SYSTEMD_INTERFACE, "MaskUnitFiles"); |
| 591 | method.append(usrLocal, false, true); |
| 592 | bus.call_noreply(method); |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 593 | } |
Adriana Kobylak | d5b8f75 | 2019-05-01 11:52:35 -0500 | [diff] [blame] | 594 | else if (!value && control::FieldMode::fieldModeEnabled()) |
| 595 | { |
| 596 | elog<NotAllowed>(xyz::openbmc_project::Common::NotAllowed::REASON( |
| 597 | "FieldMode is not allowed to be cleared")); |
| 598 | } |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 599 | |
| 600 | return control::FieldMode::fieldModeEnabled(); |
| 601 | } |
| 602 | |
| 603 | void ItemUpdater::restoreFieldModeStatus() |
| 604 | { |
Isaac Kurth | deb86b4 | 2022-03-08 19:47:53 -0600 | [diff] [blame] | 605 | // The fieldmode u-boot environment variable may not exist since it is not |
| 606 | // part of the default environment, run fw_printenv with 2>&1 to ignore the |
| 607 | // error message in the journal "Error: "fieldmode" not defined" |
| 608 | std::pair<int, std::string> ret = |
| 609 | utils::execute("/sbin/fw_printenv", "-n", "fieldmode", "2>&1"); |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 610 | |
Isaac Kurth | deb86b4 | 2022-03-08 19:47:53 -0600 | [diff] [blame] | 611 | if (ret.first != 0) |
| 612 | { |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | // truncate any extra characters off the end to compare against a "true" str |
| 617 | std::string result = ret.second.substr(0, 4); |
| 618 | if (result == "true") |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 619 | { |
| 620 | ItemUpdater::fieldModeEnabled(true); |
| 621 | } |
| 622 | } |
| 623 | |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 624 | void ItemUpdater::setBMCInventoryPath() |
| 625 | { |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 626 | auto depth = 0; |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 627 | auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 628 | MAPPER_INTERFACE, "GetSubTreePaths"); |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 629 | |
Adriana Kobylak | 1254c62 | 2017-12-07 12:24:56 -0600 | [diff] [blame] | 630 | mapperCall.append(INVENTORY_PATH); |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 631 | mapperCall.append(depth); |
Adriana Kobylak | 1254c62 | 2017-12-07 12:24:56 -0600 | [diff] [blame] | 632 | std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE}; |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 633 | mapperCall.append(filter); |
| 634 | |
Ed Tanous | 87c7817 | 2018-08-10 12:51:53 -0700 | [diff] [blame] | 635 | try |
| 636 | { |
| 637 | auto response = bus.call(mapperCall); |
| 638 | |
| 639 | using ObjectPaths = std::vector<std::string>; |
| 640 | ObjectPaths result; |
| 641 | response.read(result); |
| 642 | |
| 643 | if (!result.empty()) |
| 644 | { |
| 645 | bmcInventoryPath = result.front(); |
| 646 | } |
| 647 | } |
Patrick Williams | bf2bb2b | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 648 | catch (const sdbusplus::exception_t& e) |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 649 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 650 | error("Error in mapper GetSubTreePath: {ERROR}", "ERROR", e); |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 651 | return; |
| 652 | } |
| 653 | |
Adriana Kobylak | 1254c62 | 2017-12-07 12:24:56 -0600 | [diff] [blame] | 654 | return; |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 655 | } |
| 656 | |
Gunnar Mills | f10b232 | 2017-09-21 15:31:55 -0500 | [diff] [blame] | 657 | void ItemUpdater::createActiveAssociation(const std::string& path) |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 658 | { |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 659 | assocs.emplace_back( |
| 660 | std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path)); |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 661 | associations(assocs); |
| 662 | } |
| 663 | |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 664 | void ItemUpdater::createFunctionalAssociation(const std::string& path) |
| 665 | { |
| 666 | assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION, |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 667 | FUNCTIONAL_REV_ASSOCIATION, path)); |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 668 | associations(assocs); |
| 669 | } |
| 670 | |
AppaRao Puli | bbebec7 | 2020-01-28 23:57:41 +0530 | [diff] [blame] | 671 | void ItemUpdater::createUpdateableAssociation(const std::string& path) |
| 672 | { |
| 673 | assocs.emplace_back(std::make_tuple(UPDATEABLE_FWD_ASSOCIATION, |
| 674 | UPDATEABLE_REV_ASSOCIATION, path)); |
| 675 | associations(assocs); |
| 676 | } |
| 677 | |
Adriana Kobylak | 991af7e | 2018-12-10 13:08:38 -0600 | [diff] [blame] | 678 | void ItemUpdater::removeAssociations(const std::string& path) |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 679 | { |
| 680 | for (auto iter = assocs.begin(); iter != assocs.end();) |
| 681 | { |
Adriana Kobylak | 991af7e | 2018-12-10 13:08:38 -0600 | [diff] [blame] | 682 | if ((std::get<2>(*iter)).compare(path) == 0) |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 683 | { |
| 684 | iter = assocs.erase(iter); |
| 685 | associations(assocs); |
| 686 | } |
| 687 | else |
| 688 | { |
| 689 | ++iter; |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
Saqib Khan | b9da663 | 2017-09-13 09:48:37 -0500 | [diff] [blame] | 694 | bool ItemUpdater::isLowestPriority(uint8_t value) |
| 695 | { |
| 696 | for (const auto& intf : activations) |
| 697 | { |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 698 | if (intf.second->redundancyPriority) |
Saqib Khan | b9da663 | 2017-09-13 09:48:37 -0500 | [diff] [blame] | 699 | { |
| 700 | if (intf.second->redundancyPriority.get()->priority() < value) |
| 701 | { |
| 702 | return false; |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | return true; |
| 707 | } |
| 708 | |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 709 | void ItemUpdater::updateUbootEnvVars(const std::string& versionId) |
| 710 | { |
Lei YU | 9c76a0a | 2022-03-14 11:37:15 +0800 | [diff] [blame] | 711 | auto it = versions.find(versionId); |
| 712 | if (it == versions.end()) |
| 713 | { |
| 714 | return; |
| 715 | } |
| 716 | auto flashId = it->second->path(); |
Adriana Kobylak | 25773a7 | 2022-01-21 15:24:48 +0000 | [diff] [blame] | 717 | helper.updateUbootVersionId(flashId); |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 718 | } |
| 719 | |
Saqib Khan | 49446ae | 2017-10-02 10:54:20 -0500 | [diff] [blame] | 720 | void ItemUpdater::resetUbootEnvVars() |
| 721 | { |
| 722 | decltype(activations.begin()->second->redundancyPriority.get()->priority()) |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 723 | lowestPriority = std::numeric_limits<uint8_t>::max(); |
Saqib Khan | 49446ae | 2017-10-02 10:54:20 -0500 | [diff] [blame] | 724 | decltype(activations.begin()->second->versionId) lowestPriorityVersion; |
| 725 | for (const auto& intf : activations) |
| 726 | { |
| 727 | if (!intf.second->redundancyPriority.get()) |
| 728 | { |
| 729 | // Skip this version if the redundancyPriority is not initialized. |
| 730 | continue; |
| 731 | } |
| 732 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 733 | if (intf.second->redundancyPriority.get()->priority() <= lowestPriority) |
Saqib Khan | 49446ae | 2017-10-02 10:54:20 -0500 | [diff] [blame] | 734 | { |
| 735 | lowestPriority = intf.second->redundancyPriority.get()->priority(); |
| 736 | lowestPriorityVersion = intf.second->versionId; |
| 737 | } |
| 738 | } |
| 739 | |
Saqib Khan | f0382c3 | 2017-10-24 13:36:22 -0500 | [diff] [blame] | 740 | // Update the U-boot environment variable to point to the lowest priority |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 741 | updateUbootEnvVars(lowestPriorityVersion); |
Saqib Khan | 49446ae | 2017-10-02 10:54:20 -0500 | [diff] [blame] | 742 | } |
| 743 | |
Lei YU | d8c9eea | 2021-12-16 16:08:30 +0800 | [diff] [blame] | 744 | void ItemUpdater::freeSpace([[maybe_unused]] const Activation& caller) |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 745 | { |
Lei YU | d8c9eea | 2021-12-16 16:08:30 +0800 | [diff] [blame] | 746 | #ifdef BMC_STATIC_DUAL_IMAGE |
| 747 | // For the golden image case, always remove the version on the primary side |
| 748 | std::string versionIDtoErase; |
| 749 | for (const auto& iter : activations) |
| 750 | { |
| 751 | if (iter.second->redundancyPriority && |
| 752 | iter.second->redundancyPriority.get()->priority() == 0) |
| 753 | { |
| 754 | versionIDtoErase = iter.second->versionId; |
| 755 | break; |
| 756 | } |
| 757 | } |
| 758 | if (!versionIDtoErase.empty()) |
| 759 | { |
| 760 | erase(versionIDtoErase); |
| 761 | } |
| 762 | else |
| 763 | { |
| 764 | warning("Failed to find version to erase"); |
| 765 | } |
| 766 | #else |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 767 | // Versions with the highest priority in front |
| 768 | std::priority_queue<std::pair<int, std::string>, |
| 769 | std::vector<std::pair<int, std::string>>, |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 770 | std::less<std::pair<int, std::string>>> |
| 771 | versionsPQ; |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 772 | |
| 773 | std::size_t count = 0; |
| 774 | for (const auto& iter : activations) |
| 775 | { |
| 776 | if ((iter.second.get()->activation() == |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 777 | server::Activation::Activations::Active) || |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 778 | (iter.second.get()->activation() == |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 779 | server::Activation::Activations::Failed)) |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 780 | { |
| 781 | count++; |
| 782 | // Don't put the functional version on the queue since we can't |
| 783 | // remove the "running" BMC version. |
Lei YU | 0f88b5a | 2018-08-21 15:28:53 +0800 | [diff] [blame] | 784 | // If ACTIVE_BMC_MAX_ALLOWED <= 1, there is only one active BMC, |
| 785 | // so remove functional version as well. |
Adriana Kobylak | a696359 | 2018-09-07 14:13:29 -0500 | [diff] [blame] | 786 | // Don't delete the the Activation object that called this function. |
| 787 | if ((versions.find(iter.second->versionId) |
| 788 | ->second->isFunctional() && |
| 789 | ACTIVE_BMC_MAX_ALLOWED > 1) || |
| 790 | (iter.second->versionId == caller.versionId)) |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 791 | { |
| 792 | continue; |
| 793 | } |
Adriana Kobylak | a696359 | 2018-09-07 14:13:29 -0500 | [diff] [blame] | 794 | |
| 795 | // Failed activations don't have priority, assign them a large value |
| 796 | // for sorting purposes. |
| 797 | auto priority = 999; |
| 798 | if (iter.second.get()->activation() == |
Lei YU | cfb4b20 | 2021-05-06 17:47:28 +0800 | [diff] [blame] | 799 | server::Activation::Activations::Active && |
| 800 | iter.second->redundancyPriority) |
Adriana Kobylak | a696359 | 2018-09-07 14:13:29 -0500 | [diff] [blame] | 801 | { |
| 802 | priority = iter.second->redundancyPriority.get()->priority(); |
| 803 | } |
| 804 | |
| 805 | versionsPQ.push(std::make_pair(priority, iter.second->versionId)); |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | |
| 809 | // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1, |
| 810 | // remove the highest priority one(s). |
| 811 | while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty())) |
| 812 | { |
| 813 | erase(versionsPQ.top().second); |
| 814 | versionsPQ.pop(); |
| 815 | count--; |
| 816 | } |
Lei YU | d8c9eea | 2021-12-16 16:08:30 +0800 | [diff] [blame] | 817 | #endif |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 818 | } |
| 819 | |
Eddie James | eaa1ee0 | 2018-03-01 10:09:10 -0600 | [diff] [blame] | 820 | void ItemUpdater::mirrorUbootToAlt() |
| 821 | { |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 822 | helper.mirrorAlt(); |
Eddie James | eaa1ee0 | 2018-03-01 10:09:10 -0600 | [diff] [blame] | 823 | } |
| 824 | |
Bright Cheng | 8e9ccfe | 2019-11-18 16:18:44 +0800 | [diff] [blame] | 825 | bool ItemUpdater::checkImage(const std::string& filePath, |
| 826 | const std::vector<std::string>& imageList) |
| 827 | { |
| 828 | bool valid = true; |
| 829 | |
| 830 | for (auto& bmcImage : imageList) |
| 831 | { |
| 832 | fs::path file(filePath); |
| 833 | file /= bmcImage; |
| 834 | std::ifstream efile(file.c_str()); |
| 835 | if (efile.good() != 1) |
| 836 | { |
| 837 | valid = false; |
| 838 | break; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | return valid; |
| 843 | } |
| 844 | |
Lei YU | 6e9fb1d | 2021-02-19 18:01:40 +0800 | [diff] [blame] | 845 | #ifdef HOST_BIOS_UPGRADE |
| 846 | void ItemUpdater::createBIOSObject() |
| 847 | { |
| 848 | std::string path = BIOS_OBJPATH; |
| 849 | // Get version id from last item in the path |
| 850 | auto pos = path.rfind("/"); |
| 851 | if (pos == std::string::npos) |
| 852 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 853 | error("No version id found in object path {PATH}", "PATH", path); |
Lei YU | 6e9fb1d | 2021-02-19 18:01:40 +0800 | [diff] [blame] | 854 | return; |
| 855 | } |
| 856 | |
| 857 | createActiveAssociation(path); |
| 858 | createFunctionalAssociation(path); |
| 859 | |
| 860 | auto versionId = path.substr(pos + 1); |
| 861 | auto version = "null"; |
| 862 | AssociationList assocs = {}; |
| 863 | biosActivation = std::make_unique<Activation>( |
| 864 | bus, path, *this, versionId, server::Activation::Activations::Active, |
| 865 | assocs); |
| 866 | auto dummyErase = [](std::string /*entryId*/) { |
| 867 | // Do nothing; |
| 868 | }; |
| 869 | biosVersion = std::make_unique<VersionClass>( |
| 870 | bus, path, version, VersionPurpose::Host, "", "", |
Justin Ledford | 054bb0b | 2022-03-15 15:46:58 -0700 | [diff] [blame] | 871 | std::vector<std::string>(), |
Adriana Kobylak | 59b640b | 2022-01-21 19:45:22 +0000 | [diff] [blame] | 872 | std::bind(dummyErase, std::placeholders::_1), ""); |
Lei YU | 6e9fb1d | 2021-02-19 18:01:40 +0800 | [diff] [blame] | 873 | biosVersion->deleteObject = |
| 874 | std::make_unique<phosphor::software::manager::Delete>(bus, path, |
| 875 | *biosVersion); |
| 876 | } |
| 877 | #endif |
| 878 | |
Lei YU | 531fbc2 | 2021-12-10 20:03:18 +0800 | [diff] [blame] | 879 | void ItemUpdater::getRunningSlot() |
| 880 | { |
| 881 | // Check /run/media/slot to get the slot number |
| 882 | constexpr auto slotFile = "/run/media/slot"; |
| 883 | std::fstream f(slotFile, std::ios_base::in); |
| 884 | f >> runningImageSlot; |
| 885 | } |
| 886 | |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 887 | } // namespace updater |
| 888 | } // namespace software |
| 889 | } // namespace phosphor |