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