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