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