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> |
Brad Bishop | 8facccf | 2020-11-04 09:44:58 -0500 | [diff] [blame^] | 6 | #include <sdbusplus/server.hpp> |
| 7 | |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 8 | #include <experimental/filesystem> |
| 9 | #include <fstream> |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 10 | |
| 11 | namespace openpower |
| 12 | { |
| 13 | namespace software |
| 14 | { |
| 15 | namespace updater |
| 16 | { |
| 17 | |
| 18 | namespace fs = std::experimental::filesystem; |
| 19 | |
Lei YU | 1db9adf | 2019-03-05 16:02:31 +0800 | [diff] [blame] | 20 | void storeToFile(const std::string& versionId, uint8_t priority) |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 21 | { |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 22 | auto bus = sdbusplus::bus::new_default(); |
| 23 | |
Gunnar Mills | e7ff645 | 2017-09-21 16:19:36 -0500 | [diff] [blame] | 24 | if (!fs::is_directory(PERSIST_DIR)) |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 25 | { |
Michael Tritz | 48d9a4e | 2017-09-18 14:30:46 -0500 | [diff] [blame] | 26 | fs::create_directories(PERSIST_DIR); |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 27 | } |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 28 | |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 29 | // store one copy in /var/lib/obmc/openpower-pnor-code-mgmt/[versionId] |
| 30 | auto varPath = PERSIST_DIR + versionId; |
| 31 | std::ofstream varOutput(varPath.c_str()); |
| 32 | cereal::JSONOutputArchive varArchive(varOutput); |
| 33 | varArchive(cereal::make_nvp("priority", priority)); |
| 34 | |
Gunnar Mills | e7ff645 | 2017-09-21 16:19:36 -0500 | [diff] [blame] | 35 | if (fs::is_directory(PNOR_RW_PREFIX + versionId)) |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 36 | { |
| 37 | // store another copy in /media/pnor-rw-[versionId]/[versionId] |
| 38 | auto rwPath = PNOR_RW_PREFIX + versionId + "/" + versionId; |
| 39 | std::ofstream rwOutput(rwPath.c_str()); |
| 40 | cereal::JSONOutputArchive rwArchive(rwOutput); |
| 41 | rwArchive(cereal::make_nvp("priority", priority)); |
| 42 | } |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 43 | |
| 44 | // lastly, store the priority as an environment variable pnor-[versionId] |
| 45 | std::string serviceFile = "obmc-flash-bmc-setenv@pnor\\x2d" + versionId + |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 46 | "\\x3d" + std::to_string(priority) + ".service"; |
| 47 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 48 | SYSTEMD_INTERFACE, "StartUnit"); |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 49 | method.append(serviceFile, "replace"); |
| 50 | bus.call_noreply(method); |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 51 | } |
| 52 | |
Lei YU | 1db9adf | 2019-03-05 16:02:31 +0800 | [diff] [blame] | 53 | bool restoreFromFile(const std::string& versionId, uint8_t& priority) |
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 | auto varPath = PERSIST_DIR + versionId; |
| 56 | if (fs::exists(varPath)) |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 57 | { |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 58 | std::ifstream varInput(varPath.c_str(), std::ios::in); |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 59 | try |
| 60 | { |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 61 | cereal::JSONInputArchive varArchive(varInput); |
| 62 | varArchive(cereal::make_nvp("priority", priority)); |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 63 | return true; |
| 64 | } |
Gunnar Mills | e7ff645 | 2017-09-21 16:19:36 -0500 | [diff] [blame] | 65 | catch (cereal::RapidJSONException& e) |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 66 | { |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 67 | fs::remove(varPath); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | auto rwPath = PNOR_RW_PREFIX + versionId + "/" + versionId; |
| 72 | if (fs::exists(rwPath)) |
| 73 | { |
| 74 | std::ifstream rwInput(rwPath.c_str(), std::ios::in); |
| 75 | try |
| 76 | { |
| 77 | cereal::JSONInputArchive rwArchive(rwInput); |
| 78 | rwArchive(cereal::make_nvp("priority", priority)); |
| 79 | return true; |
| 80 | } |
Gunnar Mills | e7ff645 | 2017-09-21 16:19:36 -0500 | [diff] [blame] | 81 | catch (cereal::RapidJSONException& e) |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 82 | { |
| 83 | fs::remove(rwPath); |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 84 | } |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 85 | } |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 86 | |
| 87 | try |
| 88 | { |
| 89 | std::string devicePath = "/dev/mtd/u-boot-env"; |
| 90 | |
| 91 | if (fs::exists(devicePath) && !devicePath.empty()) |
| 92 | { |
| 93 | std::ifstream input(devicePath.c_str()); |
| 94 | std::string envVars; |
| 95 | std::getline(input, envVars); |
| 96 | |
| 97 | std::string versionVar = "pnor-" + versionId + "="; |
| 98 | auto varPosition = envVars.find(versionVar); |
| 99 | |
| 100 | if (varPosition != std::string::npos) |
| 101 | { |
| 102 | // Grab the environment variable for this versionId. These |
| 103 | // variables follow the format "pnor-[versionId]=[priority]\0" |
| 104 | auto var = envVars.substr(varPosition); |
| 105 | priority = std::stoi(var.substr(versionVar.length())); |
| 106 | return true; |
| 107 | } |
| 108 | } |
| 109 | } |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 110 | catch (const std::exception& e) |
Brad Bishop | 8facccf | 2020-11-04 09:44:58 -0500 | [diff] [blame^] | 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 | |
Lei YU | 1db9adf | 2019-03-05 16:02:31 +0800 | [diff] [blame] | 116 | void removeFile(const std::string& versionId) |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 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 |