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