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