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> |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 6 | #include <phosphor-logging/log.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 | |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 19 | using namespace phosphor::logging; |
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 |
| 33 | log<level::WARNING>("Removing non-directory file", |
| 34 | entry("PATH=%s", path.c_str())); |
| 35 | fs::remove_all(path); |
| 36 | } |
| 37 | fs::create_directories(path); |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 38 | } |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 39 | path = path / priorityName; |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 40 | |
| 41 | std::ofstream os(path.c_str()); |
| 42 | cereal::JSONOutputArchive oarchive(os); |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 43 | oarchive(cereal::make_nvp(priorityName, priority)); |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 44 | } |
| 45 | |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 46 | void storePurpose(const std::string& versionId, VersionPurpose purpose) |
| 47 | { |
| 48 | auto path = fs::path(PERSIST_DIR) / versionId; |
| 49 | if (!fs::is_directory(path)) |
| 50 | { |
| 51 | if (fs::exists(path)) |
| 52 | { |
| 53 | // Delete if it's a non-directory file |
| 54 | log<level::WARNING>("Removing non-directory file", |
| 55 | entry("PATH=%s", path.c_str())); |
| 56 | fs::remove_all(path); |
| 57 | } |
| 58 | fs::create_directories(path); |
| 59 | } |
| 60 | path = path / purposeName; |
| 61 | |
| 62 | std::ofstream os(path.c_str()); |
| 63 | cereal::JSONOutputArchive oarchive(os); |
| 64 | oarchive(cereal::make_nvp(purposeName, purpose)); |
| 65 | } |
| 66 | |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 67 | bool restorePriority(const std::string& versionId, uint8_t& priority) |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 68 | { |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 69 | auto path = fs::path(PERSIST_DIR) / versionId / priorityName; |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 70 | if (fs::exists(path)) |
| 71 | { |
| 72 | std::ifstream is(path.c_str(), std::ios::in); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 73 | try |
| 74 | { |
| 75 | cereal::JSONInputArchive iarchive(is); |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 76 | iarchive(cereal::make_nvp(priorityName, priority)); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 77 | return true; |
| 78 | } |
Adriana Kobylak | 9eb631d | 2019-11-05 14:10:42 -0600 | [diff] [blame] | 79 | catch (cereal::Exception& e) |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 80 | { |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 81 | fs::remove_all(path); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 82 | } |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 83 | } |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 84 | |
| 85 | // Find the mtd device "u-boot-env" to retrieve the environment variables |
| 86 | std::ifstream mtdDevices("/proc/mtd"); |
| 87 | std::string device, devicePath; |
| 88 | |
| 89 | try |
| 90 | { |
Gunnar Mills | ffe0492 | 2017-10-06 13:20:43 -0500 | [diff] [blame] | 91 | while (std::getline(mtdDevices, device)) |
| 92 | { |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 93 | if (device.find("u-boot-env") != std::string::npos) |
| 94 | { |
| 95 | devicePath = "/dev/" + device.substr(0, device.find(':')); |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (!devicePath.empty()) |
| 101 | { |
| 102 | std::ifstream input(devicePath.c_str()); |
| 103 | std::string envVars; |
| 104 | std::getline(input, envVars); |
| 105 | |
Michael Tritz | 6273efd | 2017-12-14 15:21:18 -0600 | [diff] [blame] | 106 | std::string versionVar = versionId + "="; |
| 107 | auto varPosition = envVars.find(versionVar); |
| 108 | |
| 109 | if (varPosition != std::string::npos) |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 110 | { |
| 111 | // Grab the environment variable for this versionId. These |
| 112 | // variables follow the format "versionId=priority\0" |
Michael Tritz | 6273efd | 2017-12-14 15:21:18 -0600 | [diff] [blame] | 113 | auto var = envVars.substr(varPosition); |
| 114 | priority = std::stoi(var.substr(versionVar.length())); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | } |
| 118 | } |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 119 | catch (const std::exception& e) |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 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 |