Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | #include <experimental/filesystem> |
Michael Tritz | 5c90711 | 2017-08-02 15:25:51 -0500 | [diff] [blame] | 3 | #include <cereal/archives/json.hpp> |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 4 | #include <fstream> |
| 5 | #include "serialize.hpp" |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 6 | #include <sdbusplus/server.hpp> |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 7 | |
| 8 | namespace openpower |
| 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 | { |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 19 | auto bus = sdbusplus::bus::new_default(); |
| 20 | |
Gunnar Mills | e7ff645 | 2017-09-21 16:19:36 -0500 | [diff] [blame] | 21 | if (!fs::is_directory(PERSIST_DIR)) |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 22 | { |
Michael Tritz | 48d9a4e | 2017-09-18 14:30:46 -0500 | [diff] [blame] | 23 | fs::create_directories(PERSIST_DIR); |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 24 | } |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 25 | |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 26 | // store one copy in /var/lib/obmc/openpower-pnor-code-mgmt/[versionId] |
| 27 | auto varPath = PERSIST_DIR + versionId; |
| 28 | std::ofstream varOutput(varPath.c_str()); |
| 29 | cereal::JSONOutputArchive varArchive(varOutput); |
| 30 | varArchive(cereal::make_nvp("priority", priority)); |
| 31 | |
Gunnar Mills | e7ff645 | 2017-09-21 16:19:36 -0500 | [diff] [blame] | 32 | if (fs::is_directory(PNOR_RW_PREFIX + versionId)) |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 33 | { |
| 34 | // store another copy in /media/pnor-rw-[versionId]/[versionId] |
| 35 | auto rwPath = PNOR_RW_PREFIX + versionId + "/" + versionId; |
| 36 | std::ofstream rwOutput(rwPath.c_str()); |
| 37 | cereal::JSONOutputArchive rwArchive(rwOutput); |
| 38 | rwArchive(cereal::make_nvp("priority", priority)); |
| 39 | } |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 40 | |
| 41 | // lastly, store the priority as an environment variable pnor-[versionId] |
| 42 | std::string serviceFile = "obmc-flash-bmc-setenv@pnor\\x2d" + versionId + |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 43 | "\\x3d" + std::to_string(priority) + ".service"; |
| 44 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 45 | SYSTEMD_INTERFACE, "StartUnit"); |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 46 | method.append(serviceFile, "replace"); |
| 47 | bus.call_noreply(method); |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 48 | } |
| 49 | |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 50 | bool restoreFromFile(std::string versionId, uint8_t& priority) |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 51 | { |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 52 | auto varPath = PERSIST_DIR + versionId; |
| 53 | if (fs::exists(varPath)) |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 54 | { |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 55 | std::ifstream varInput(varPath.c_str(), std::ios::in); |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 56 | try |
| 57 | { |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 58 | cereal::JSONInputArchive varArchive(varInput); |
| 59 | varArchive(cereal::make_nvp("priority", priority)); |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 60 | return true; |
| 61 | } |
Gunnar Mills | e7ff645 | 2017-09-21 16:19:36 -0500 | [diff] [blame] | 62 | catch (cereal::RapidJSONException& e) |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 63 | { |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 64 | fs::remove(varPath); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | auto rwPath = PNOR_RW_PREFIX + versionId + "/" + versionId; |
| 69 | if (fs::exists(rwPath)) |
| 70 | { |
| 71 | std::ifstream rwInput(rwPath.c_str(), std::ios::in); |
| 72 | try |
| 73 | { |
| 74 | cereal::JSONInputArchive rwArchive(rwInput); |
| 75 | rwArchive(cereal::make_nvp("priority", priority)); |
| 76 | return true; |
| 77 | } |
Gunnar Mills | e7ff645 | 2017-09-21 16:19:36 -0500 | [diff] [blame] | 78 | catch (cereal::RapidJSONException& e) |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 79 | { |
| 80 | fs::remove(rwPath); |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 81 | } |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 82 | } |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 83 | |
| 84 | try |
| 85 | { |
| 86 | std::string devicePath = "/dev/mtd/u-boot-env"; |
| 87 | |
| 88 | if (fs::exists(devicePath) && !devicePath.empty()) |
| 89 | { |
| 90 | std::ifstream input(devicePath.c_str()); |
| 91 | std::string envVars; |
| 92 | std::getline(input, envVars); |
| 93 | |
| 94 | std::string versionVar = "pnor-" + versionId + "="; |
| 95 | auto varPosition = envVars.find(versionVar); |
| 96 | |
| 97 | if (varPosition != std::string::npos) |
| 98 | { |
| 99 | // Grab the environment variable for this versionId. These |
| 100 | // variables follow the format "pnor-[versionId]=[priority]\0" |
| 101 | auto var = envVars.substr(varPosition); |
| 102 | priority = std::stoi(var.substr(versionVar.length())); |
| 103 | return true; |
| 104 | } |
| 105 | } |
| 106 | } |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 107 | catch (const std::exception& e) |
| 108 | { |
| 109 | } |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 110 | |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 111 | return false; |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void removeFile(std::string versionId) |
| 115 | { |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 116 | auto bus = sdbusplus::bus::new_default(); |
| 117 | |
| 118 | // Clear the environment variable pnor-[versionId]. |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 119 | std::string serviceFile = |
| 120 | "obmc-flash-bmc-setenv@pnor\\x2d" + versionId + ".service"; |
| 121 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 122 | SYSTEMD_INTERFACE, "StartUnit"); |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 123 | method.append(serviceFile, "replace"); |
| 124 | bus.call_noreply(method); |
| 125 | |
| 126 | // Delete the file /var/lib/obmc/openpower-pnor-code-mgmt/[versionId]. |
| 127 | // Note that removeFile() is called in the case of a version being deleted, |
| 128 | // so the file /media/pnor-rw-[versionId]/[versionId] will also be deleted |
| 129 | // along with its surrounding directory. |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 130 | std::string path = PERSIST_DIR + versionId; |
| 131 | if (fs::exists(path)) |
| 132 | { |
| 133 | fs::remove(path); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | } // namespace updater |
| 138 | } // namespace software |
| 139 | } // namespace openpower |