Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame^] | 1 | /* |
| 2 | // Copyright (c) 2019 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "mdrv2.hpp" |
| 18 | |
| 19 | #include <sys/mman.h> |
| 20 | |
| 21 | #include <fstream> |
| 22 | #include <phosphor-logging/elog-errors.hpp> |
| 23 | #include <sdbusplus/exception.hpp> |
| 24 | #include <xyz/openbmc_project/Smbios/MDR_V2/error.hpp> |
| 25 | |
| 26 | namespace phosphor |
| 27 | { |
| 28 | namespace smbios |
| 29 | { |
| 30 | |
| 31 | std::vector<uint8_t> MDR_V2::getDirectoryInformation(uint8_t dirIndex) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | bool MDR_V2::smbiosIsAvailForUpdate(uint8_t index) |
| 36 | { |
| 37 | bool ret = false; |
| 38 | if (index > maxDirEntries) |
| 39 | { |
| 40 | return ret; |
| 41 | } |
| 42 | |
| 43 | switch (smbiosDir.dir[index].stage) |
| 44 | { |
| 45 | case MDR2SMBIOSStatusEnum::mdr2Updating: |
| 46 | ret = false; |
| 47 | break; |
| 48 | |
| 49 | case MDR2SMBIOSStatusEnum::mdr2Init: |
| 50 | // This *looks* like there should be a break statement here, |
| 51 | // as the effects of the previous statement are a noop given |
| 52 | // the following code that this falls through to. |
| 53 | // We've elected not to change it, though, since it's been |
| 54 | // this way since the old generation, and it would affect |
| 55 | // the way the code functions. |
| 56 | // If it ain't broke, don't fix it. |
| 57 | |
| 58 | case MDR2SMBIOSStatusEnum::mdr2Loaded: |
| 59 | case MDR2SMBIOSStatusEnum::mdr2Updated: |
| 60 | if (smbiosDir.dir[index].lock == MDR2DirLockEnum::mdr2DirLock) |
| 61 | { |
| 62 | ret = false; |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | ret = true; |
| 67 | } |
| 68 | break; |
| 69 | |
| 70 | default: |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | std::vector<uint8_t> MDR_V2::getDataOffer() |
| 78 | { |
| 79 | std::vector<uint8_t> offer(sizeof(DataIdStruct)); |
| 80 | if (smbiosIsAvailForUpdate(0)) |
| 81 | { |
| 82 | std::copy(smbiosDir.dir[0].common.id.dataInfo, |
| 83 | &smbiosDir.dir[0].common.id.dataInfo[16], offer.data()); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 88 | "smbios is not ready for update"); |
| 89 | throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error:: |
| 90 | UpdateInProgress(); |
| 91 | } |
| 92 | return offer; |
| 93 | } |
| 94 | |
| 95 | inline uint8_t MDR_V2::smbiosValidFlag(uint8_t index) |
| 96 | { |
| 97 | FlagStatus ret = FlagStatus::flagIsInvalid; |
| 98 | MDR2SMBIOSStatusEnum stage = smbiosDir.dir[index].stage; |
| 99 | MDR2DirLockEnum lock = smbiosDir.dir[index].lock; |
| 100 | |
| 101 | switch (stage) |
| 102 | { |
| 103 | case MDR2SMBIOSStatusEnum::mdr2Loaded: |
| 104 | case MDR2SMBIOSStatusEnum::mdr2Updated: |
| 105 | if (lock == MDR2DirLockEnum::mdr2DirLock) |
| 106 | { |
| 107 | ret = FlagStatus::flagIsLocked; // locked |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | ret = FlagStatus::flagIsValid; // valid |
| 112 | } |
| 113 | break; |
| 114 | |
| 115 | case MDR2SMBIOSStatusEnum::mdr2Updating: |
| 116 | case MDR2SMBIOSStatusEnum::mdr2Init: |
| 117 | ret = FlagStatus::flagIsInvalid; // invalid |
| 118 | break; |
| 119 | |
| 120 | default: |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | return static_cast<uint8_t>(ret); |
| 125 | } |
| 126 | |
| 127 | std::vector<uint8_t> MDR_V2::getDataInformation(uint8_t idIndex) |
| 128 | { |
| 129 | std::vector<uint8_t> responseInfo; |
| 130 | responseInfo.push_back(mdr2Version); |
| 131 | |
| 132 | if (idIndex >= maxDirEntries) |
| 133 | { |
| 134 | throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error:: |
| 135 | InvalidParameter(); |
| 136 | } |
| 137 | |
| 138 | for (uint8_t index = 0; index < sizeof(DataIdStruct); index++) |
| 139 | { |
| 140 | responseInfo.push_back( |
| 141 | smbiosDir.dir[idIndex].common.id.dataInfo[index]); |
| 142 | } |
| 143 | responseInfo.push_back(smbiosValidFlag(idIndex)); |
| 144 | responseInfo.push_back(smbiosDir.dir[idIndex].common.size); |
| 145 | responseInfo.push_back(smbiosDir.dir[idIndex].common.dataVersion); |
| 146 | responseInfo.push_back(smbiosDir.dir[idIndex].common.timestamp); |
| 147 | |
| 148 | return responseInfo; |
| 149 | } |
| 150 | |
| 151 | bool MDR_V2::sendDirectoryInformation(uint8_t dirVersion, uint8_t dirIndex, |
| 152 | uint8_t returnedEntries, |
| 153 | uint8_t remainingEntries, |
| 154 | std::vector<uint8_t> dirEntry) |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | bool MDR_V2::sendDataInformation(uint8_t idIndex, uint8_t flag, |
| 159 | uint32_t dataLen, uint32_t dataVer, |
| 160 | uint32_t timeStamp) |
| 161 | { |
| 162 | if (idIndex >= maxDirEntries) |
| 163 | { |
| 164 | throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error:: |
| 165 | InvalidParameter(); |
| 166 | } |
| 167 | int entryChanged = 0; |
| 168 | if (smbiosDir.dir[idIndex].common.dataSetSize != dataLen) |
| 169 | { |
| 170 | entryChanged++; |
| 171 | smbiosDir.dir[idIndex].common.dataSetSize = dataLen; |
| 172 | } |
| 173 | |
| 174 | if (smbiosDir.dir[idIndex].common.dataVersion != dataVer) |
| 175 | { |
| 176 | entryChanged++; |
| 177 | smbiosDir.dir[idIndex].common.dataVersion = dataVer; |
| 178 | } |
| 179 | |
| 180 | if (smbiosDir.dir[idIndex].common.timestamp != timeStamp) |
| 181 | { |
| 182 | entryChanged++; |
| 183 | smbiosDir.dir[idIndex].common.timestamp = timeStamp; |
| 184 | } |
| 185 | if (entryChanged == 0) |
| 186 | { |
| 187 | return false; |
| 188 | } |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | int MDR_V2::findIdIndex(std::vector<uint8_t> dataInfo) |
| 193 | { |
| 194 | if (dataInfo.size() != sizeof(DataIdStruct)) |
| 195 | { |
| 196 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 197 | "Length of dataInfo invalid"); |
| 198 | throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error:: |
| 199 | InvalidParameter(); |
| 200 | } |
| 201 | std::array<uint8_t, 16> arrayDataInfo; |
| 202 | |
| 203 | std::copy(dataInfo.begin(), dataInfo.end(), arrayDataInfo.begin()); |
| 204 | |
| 205 | for (int index = 0; index < smbiosDir.dirEntries; index++) |
| 206 | { |
| 207 | int info = 0; |
| 208 | for (; info < arrayDataInfo.size(); info++) |
| 209 | { |
| 210 | if (arrayDataInfo[info] != |
| 211 | smbiosDir.dir[index].common.id.dataInfo[info]) |
| 212 | { |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | if (info == arrayDataInfo.size()) |
| 217 | { |
| 218 | return index; |
| 219 | } |
| 220 | } |
| 221 | throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::InvalidId(); |
| 222 | } |
| 223 | |
| 224 | uint8_t MDR_V2::directoryEntries(uint8_t value) |
| 225 | { |
| 226 | value = smbiosDir.dirEntries; |
| 227 | return sdbusplus::xyz::openbmc_project::Smbios::server::MDR_V2:: |
| 228 | directoryEntries(value); |
| 229 | } |
| 230 | |
| 231 | bool MDR_V2::agentSynchronizeData() |
| 232 | { |
| 233 | } |
| 234 | |
| 235 | std::vector<uint32_t> MDR_V2::synchronizeDirectoryCommonData(uint8_t idIndex, |
| 236 | uint32_t size) |
| 237 | { |
| 238 | } |
| 239 | |
| 240 | } // namespace smbios |
| 241 | } // namespace phosphor |