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