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