Xiuzhi Cheng | 4f3eb90 | 2022-06-08 09:06:33 +0800 | [diff] [blame] | 1 | #include <filesystem> |
| 2 | #include <ipmid/api.hpp> |
| 3 | #include <phosphor-logging/log.hpp> |
| 4 | |
| 5 | #include "config.h" |
| 6 | #include "kunlun_oem.hpp" |
| 7 | |
| 8 | namespace ipmi |
| 9 | { |
| 10 | |
| 11 | using namespace phosphor::logging; |
| 12 | |
| 13 | // save cpu infomation from bios to file |
| 14 | // index[in]: cpu index |
| 15 | // info[in]: data saving to file |
| 16 | static int saveInfoToFile(uint8_t index, std::vector<char> &info) |
| 17 | { |
| 18 | char infoPath[PATH_MAX]; |
| 19 | |
| 20 | std::snprintf(infoPath, sizeof(infoPath), IPMI_BIOSDATA_DIR"/cpu%02x", index); |
| 21 | |
| 22 | if (!(std::filesystem::exists(IPMI_BIOSDATA_DIR))) |
| 23 | { |
| 24 | std::filesystem::create_directory(IPMI_BIOSDATA_DIR); |
| 25 | } |
| 26 | |
| 27 | std::ofstream ofs(infoPath, std::ios::out | std::ios::binary); |
| 28 | ofs.seekp(0); |
| 29 | ofs.write(info.data(), info.size()); |
| 30 | ofs.close(); |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | // load cpu infomation from file |
| 36 | // index[in]: cpu index |
| 37 | // info[out]: data loading from file |
| 38 | static int loadInfoFromFile(uint8_t index, std::vector<char> &info) |
| 39 | { |
| 40 | char infoPath[PATH_MAX]; |
| 41 | |
| 42 | std::snprintf(infoPath, sizeof(infoPath), IPMI_BIOSDATA_DIR"/cpu%02x", index); |
| 43 | |
| 44 | if (!(std::filesystem::exists(infoPath))) |
| 45 | { |
| 46 | std::filesystem::create_directory(infoPath); |
| 47 | } |
| 48 | |
| 49 | std::ifstream ifs(infoPath, std::ios::in | std::ios::binary); |
| 50 | |
| 51 | ifs.seekg(0, std::ios::end); |
| 52 | auto size = ifs.tellg(); |
| 53 | ifs.seekg(0, std::ios::beg); |
| 54 | |
| 55 | info.resize(size); |
| 56 | |
| 57 | ifs.read(info.data(), size); |
| 58 | ifs.close(); |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | RspType<> setCpuInfo(uint8_t index, std::vector<char> info) |
| 64 | { |
| 65 | saveInfoToFile(index, info); |
| 66 | |
| 67 | return responseSuccess(); |
| 68 | } |
| 69 | |
| 70 | RspType<std::vector<char>> getCpuInfo(uint8_t index) |
| 71 | { |
| 72 | std::vector<char> output; |
| 73 | |
| 74 | loadInfoFromFile(index, output); |
| 75 | |
| 76 | return responseSuccess(output); |
| 77 | } |
| 78 | |
| 79 | void register_kunlun_oem_functions(void) |
| 80 | { |
| 81 | registerHandler(prioOemBase, netfnKunlunOem, cmd::cmdSetCpuInfo, |
| 82 | Privilege::Admin, setCpuInfo); |
| 83 | registerHandler(prioOemBase, netfnKunlunOem, cmd::cmdGetCpuInfo, |
| 84 | Privilege::User, getCpuInfo); |
| 85 | } |
| 86 | |
| 87 | void register_kunlun_oem_functions() __attribute__((constructor)); |
| 88 | |
| 89 | } // namespace ipmi |