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