Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 1 | #include "config.h" |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 2 | |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 3 | #include "serialize.hpp" |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 4 | |
| 5 | #include <cereal/archives/json.hpp> |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 6 | #include <phosphor-logging/lg2.hpp> |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 7 | #include <sdbusplus/server.hpp> |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 8 | |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 9 | #include <filesystem> |
| 10 | #include <fstream> |
| 11 | |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 12 | namespace phosphor |
| 13 | { |
| 14 | namespace software |
| 15 | { |
| 16 | namespace updater |
| 17 | { |
| 18 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 19 | PHOSPHOR_LOG2_USING; |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame] | 20 | namespace fs = std::filesystem; |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 21 | |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 22 | const std::string priorityName = "priority"; |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 23 | const std::string purposeName = "purpose"; |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 24 | |
| 25 | void storePriority(const std::string& versionId, uint8_t priority) |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 26 | { |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 27 | auto path = fs::path(PERSIST_DIR) / versionId; |
| 28 | if (!fs::is_directory(path)) |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 29 | { |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 30 | if (fs::exists(path)) |
| 31 | { |
| 32 | // Delete if it's a non-directory file |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 33 | warning("Removing non-directory file: {PATH}", "PATH", path); |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 34 | fs::remove_all(path); |
| 35 | } |
| 36 | fs::create_directories(path); |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 37 | } |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 38 | path = path / priorityName; |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 39 | |
| 40 | std::ofstream os(path.c_str()); |
| 41 | cereal::JSONOutputArchive oarchive(os); |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 42 | oarchive(cereal::make_nvp(priorityName, priority)); |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 45 | void storePurpose(const std::string& versionId, VersionPurpose purpose) |
| 46 | { |
| 47 | auto path = fs::path(PERSIST_DIR) / versionId; |
| 48 | if (!fs::is_directory(path)) |
| 49 | { |
| 50 | if (fs::exists(path)) |
| 51 | { |
| 52 | // Delete if it's a non-directory file |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 53 | warning("Removing non-directory file: {PATH}", "PATH", path); |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 54 | fs::remove_all(path); |
| 55 | } |
| 56 | fs::create_directories(path); |
| 57 | } |
| 58 | path = path / purposeName; |
| 59 | |
| 60 | std::ofstream os(path.c_str()); |
| 61 | cereal::JSONOutputArchive oarchive(os); |
| 62 | oarchive(cereal::make_nvp(purposeName, purpose)); |
| 63 | } |
| 64 | |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 65 | bool restorePriority(const std::string& versionId, uint8_t& priority) |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 66 | { |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 67 | auto path = fs::path(PERSIST_DIR) / versionId / priorityName; |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 68 | if (fs::exists(path)) |
| 69 | { |
| 70 | std::ifstream is(path.c_str(), std::ios::in); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 71 | try |
| 72 | { |
| 73 | cereal::JSONInputArchive iarchive(is); |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 74 | iarchive(cereal::make_nvp(priorityName, priority)); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 75 | return true; |
| 76 | } |
Adriana Kobylak | 9eb631d | 2019-11-05 14:10:42 -0600 | [diff] [blame] | 77 | catch (cereal::Exception& e) |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 78 | { |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 79 | fs::remove_all(path); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 80 | } |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 81 | } |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 82 | |
| 83 | // Find the mtd device "u-boot-env" to retrieve the environment variables |
| 84 | std::ifstream mtdDevices("/proc/mtd"); |
| 85 | std::string device, devicePath; |
| 86 | |
| 87 | try |
| 88 | { |
Gunnar Mills | ffe0492 | 2017-10-06 13:20:43 -0500 | [diff] [blame] | 89 | while (std::getline(mtdDevices, device)) |
| 90 | { |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 91 | if (device.find("u-boot-env") != std::string::npos) |
| 92 | { |
| 93 | devicePath = "/dev/" + device.substr(0, device.find(':')); |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if (!devicePath.empty()) |
| 99 | { |
| 100 | std::ifstream input(devicePath.c_str()); |
| 101 | std::string envVars; |
| 102 | std::getline(input, envVars); |
| 103 | |
Michael Tritz | 6273efd | 2017-12-14 15:21:18 -0600 | [diff] [blame] | 104 | std::string versionVar = versionId + "="; |
| 105 | auto varPosition = envVars.find(versionVar); |
| 106 | |
| 107 | if (varPosition != std::string::npos) |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 108 | { |
| 109 | // Grab the environment variable for this versionId. These |
| 110 | // variables follow the format "versionId=priority\0" |
Michael Tritz | 6273efd | 2017-12-14 15:21:18 -0600 | [diff] [blame] | 111 | auto var = envVars.substr(varPosition); |
| 112 | priority = std::stoi(var.substr(versionVar.length())); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 113 | return true; |
| 114 | } |
| 115 | } |
| 116 | } |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 117 | catch (const std::exception& e) |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 118 | { |
| 119 | error("Error during processing: {ERROR}", "ERROR", e); |
| 120 | } |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 121 | |
| 122 | return false; |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 123 | } |
| 124 | |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 125 | bool restorePurpose(const std::string& versionId, VersionPurpose& purpose) |
| 126 | { |
| 127 | auto path = fs::path(PERSIST_DIR) / versionId / purposeName; |
| 128 | if (fs::exists(path)) |
| 129 | { |
| 130 | std::ifstream is(path.c_str(), std::ios::in); |
| 131 | try |
| 132 | { |
| 133 | cereal::JSONInputArchive iarchive(is); |
| 134 | iarchive(cereal::make_nvp(purposeName, purpose)); |
| 135 | return true; |
| 136 | } |
| 137 | catch (cereal::Exception& e) |
| 138 | { |
| 139 | fs::remove_all(path); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return false; |
| 144 | } |
| 145 | |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 146 | void removePersistDataDirectory(const std::string& versionId) |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 147 | { |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 148 | auto path = fs::path(PERSIST_DIR) / versionId; |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 149 | if (fs::exists(path)) |
| 150 | { |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 151 | fs::remove_all(path); |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Gunnar Mills | fa34e02 | 2018-09-04 10:05:45 -0500 | [diff] [blame] | 155 | } // namespace updater |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 156 | } // namespace software |
Gunnar Mills | fa34e02 | 2018-09-04 10:05:45 -0500 | [diff] [blame] | 157 | } // namespace phosphor |