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