Deepak Kodihalli | cb7f2d4 | 2019-06-19 13:25:31 +0530 | [diff] [blame] | 1 | #include "bios_table.hpp" |
| 2 | |
| 3 | #include <fstream> |
| 4 | |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 5 | #include "bios_table.h" |
| 6 | |
Deepak Kodihalli | cb7f2d4 | 2019-06-19 13:25:31 +0530 | [diff] [blame] | 7 | namespace pldm |
| 8 | { |
| 9 | |
| 10 | namespace responder |
| 11 | { |
| 12 | |
| 13 | namespace bios |
| 14 | { |
| 15 | |
| 16 | BIOSTable::BIOSTable(const char* filePath) : filePath(filePath) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | bool BIOSTable::isEmpty() const noexcept |
| 21 | { |
| 22 | bool empty = false; |
| 23 | try |
| 24 | { |
| 25 | empty = fs::is_empty(filePath); |
| 26 | } |
| 27 | catch (fs::filesystem_error& e) |
| 28 | { |
| 29 | return true; |
| 30 | } |
| 31 | return empty; |
| 32 | } |
| 33 | |
| 34 | void BIOSTable::store(const Table& table) |
| 35 | { |
| 36 | std::ofstream stream(filePath.string(), std::ios::out | std::ios::binary); |
| 37 | stream.write(reinterpret_cast<const char*>(table.data()), table.size()); |
| 38 | } |
| 39 | |
| 40 | void BIOSTable::load(Response& response) const |
| 41 | { |
| 42 | auto currSize = response.size(); |
| 43 | auto fileSize = fs::file_size(filePath); |
| 44 | response.resize(currSize + fileSize); |
| 45 | std::ifstream stream(filePath.string(), std::ios::in | std::ios::binary); |
| 46 | stream.read(reinterpret_cast<char*>(response.data() + currSize), fileSize); |
| 47 | } |
| 48 | |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 49 | BIOSStringTable::BIOSStringTable(const Table& stringTable) : |
| 50 | stringTable(stringTable) |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 51 | { |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | BIOSStringTable::BIOSStringTable(const BIOSTable& biosTable) |
| 55 | { |
| 56 | biosTable.load(stringTable); |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | std::string BIOSStringTable::findString(uint16_t handle) const |
| 60 | { |
| 61 | auto stringEntry = pldm_bios_table_string_find_by_handle( |
| 62 | stringTable.data(), stringTable.size(), handle); |
| 63 | if (stringEntry == nullptr) |
| 64 | { |
| 65 | throw std::invalid_argument("Invalid String Handle"); |
| 66 | } |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame^] | 67 | return table::string::decodeString(stringEntry); |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 68 | } |
| 69 | |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 70 | uint16_t BIOSStringTable::findHandle(const std::string& name) const |
| 71 | { |
| 72 | auto stringEntry = pldm_bios_table_string_find_by_string( |
| 73 | stringTable.data(), stringTable.size(), name.c_str()); |
| 74 | if (stringEntry == nullptr) |
| 75 | { |
| 76 | throw std::invalid_argument("Invalid String Name"); |
| 77 | } |
| 78 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame^] | 79 | return table::string::decodeHandle(stringEntry); |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 80 | } |
| 81 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame^] | 82 | namespace table |
| 83 | { |
| 84 | |
| 85 | namespace string |
| 86 | { |
| 87 | |
| 88 | uint16_t decodeHandle(const pldm_bios_string_table_entry* entry) |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 89 | { |
| 90 | return pldm_bios_table_string_entry_decode_handle(entry); |
| 91 | } |
| 92 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame^] | 93 | std::string decodeString(const pldm_bios_string_table_entry* entry) |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 94 | { |
| 95 | auto strLength = pldm_bios_table_string_entry_decode_string_length(entry); |
| 96 | std::vector<char> buffer(strLength + 1 /* sizeof '\0' */); |
| 97 | pldm_bios_table_string_entry_decode_string(entry, buffer.data(), |
| 98 | buffer.size()); |
| 99 | return std::string(buffer.data(), buffer.data() + strLength); |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 100 | } |
| 101 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame^] | 102 | } // namespace string |
| 103 | |
| 104 | namespace attribute |
| 105 | { |
| 106 | |
| 107 | TableHeader decodeHeader(const pldm_bios_attr_table_entry* entry) |
| 108 | { |
| 109 | auto attrHandle = pldm_bios_table_attr_entry_decode_attribute_handle(entry); |
| 110 | auto attrType = pldm_bios_table_attr_entry_decode_attribute_type(entry); |
| 111 | auto stringHandle = pldm_bios_table_attr_entry_decode_string_handle(entry); |
| 112 | return {attrHandle, attrType, stringHandle}; |
| 113 | } |
| 114 | |
| 115 | const pldm_bios_attr_table_entry* |
| 116 | constructStringEntry(Table& table, |
| 117 | pldm_bios_table_attr_entry_string_info* info) |
| 118 | { |
| 119 | auto entryLength = |
| 120 | pldm_bios_table_attr_entry_string_encode_length(info->def_length); |
| 121 | |
| 122 | auto tableSize = table.size(); |
| 123 | table.resize(tableSize + entryLength, 0); |
| 124 | pldm_bios_table_attr_entry_string_encode(table.data() + tableSize, |
| 125 | entryLength, info); |
| 126 | return reinterpret_cast<pldm_bios_attr_table_entry*>(table.data() + |
| 127 | tableSize); |
| 128 | } |
| 129 | |
| 130 | StringField decodeStringEntry(const pldm_bios_attr_table_entry* entry) |
| 131 | { |
| 132 | auto strType = pldm_bios_table_attr_entry_string_decode_string_type(entry); |
| 133 | auto minLength = pldm_bios_table_attr_entry_string_decode_min_length(entry); |
| 134 | auto maxLength = pldm_bios_table_attr_entry_string_decode_max_length(entry); |
| 135 | auto defLength = |
| 136 | pldm_bios_table_attr_entry_string_decode_def_string_length(entry); |
| 137 | |
| 138 | std::vector<char> buffer(defLength + 1); |
| 139 | pldm_bios_table_attr_entry_string_decode_def_string(entry, buffer.data(), |
| 140 | buffer.size()); |
| 141 | return {strType, minLength, maxLength, defLength, |
| 142 | std::string(buffer.data(), buffer.data() + defLength)}; |
| 143 | } |
| 144 | |
| 145 | } // namespace attribute |
| 146 | |
| 147 | namespace attribute_value |
| 148 | { |
| 149 | |
| 150 | TableHeader decodeHeader(const pldm_bios_attr_val_table_entry* entry) |
| 151 | { |
| 152 | auto handle = |
| 153 | pldm_bios_table_attr_value_entry_decode_attribute_handle(entry); |
| 154 | auto type = pldm_bios_table_attr_value_entry_decode_attribute_type(entry); |
| 155 | return {handle, type}; |
| 156 | } |
| 157 | |
| 158 | std::string decodeStringEntry(const pldm_bios_attr_val_table_entry* entry) |
| 159 | { |
| 160 | variable_field currentString{}; |
| 161 | pldm_bios_table_attr_value_entry_string_decode_string(entry, |
| 162 | ¤tString); |
| 163 | return std::string(currentString.ptr, |
| 164 | currentString.ptr + currentString.length); |
| 165 | } |
| 166 | |
| 167 | const pldm_bios_attr_val_table_entry* |
| 168 | constructStringEntry(Table& table, uint16_t attrHandle, uint8_t attrType, |
| 169 | const std::string& str) |
| 170 | { |
| 171 | auto strLen = str.size(); |
| 172 | auto entryLength = |
| 173 | pldm_bios_table_attr_value_entry_encode_string_length(strLen); |
| 174 | auto tableSize = table.size(); |
| 175 | table.resize(tableSize + entryLength); |
| 176 | pldm_bios_table_attr_value_entry_encode_string( |
| 177 | table.data() + tableSize, entryLength, attrHandle, attrType, strLen, |
| 178 | str.c_str()); |
| 179 | return reinterpret_cast<pldm_bios_attr_val_table_entry*>(table.data() + |
| 180 | tableSize); |
| 181 | } |
| 182 | |
| 183 | } // namespace attribute_value |
| 184 | |
| 185 | } // namespace table |
| 186 | |
Deepak Kodihalli | cb7f2d4 | 2019-06-19 13:25:31 +0530 | [diff] [blame] | 187 | } // namespace bios |
| 188 | } // namespace responder |
| 189 | } // namespace pldm |