/* // Copyright (c) 2017 2018 Intel Corporation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. */ #include #include #include #include #include #include #include #include namespace ipmi { namespace storage { constexpr static const size_t maxMessageSize = 64; constexpr static const size_t maxFruSdrNameSize = 16; using ManagedObjectType = boost::container::flat_map< sdbusplus::message::object_path, boost::container::flat_map< std::string, boost::container::flat_map>>; using ManagedEntry = std::pair< sdbusplus::message::object_path, boost::container::flat_map< std::string, boost::container::flat_map>>; constexpr static const char* fruDeviceServiceName = "xyz.openbmc_project.FruDevice"; constexpr static const size_t cacheTimeoutSeconds = 10; static std::vector fruCache; static uint8_t cacheBus = 0xFF; static uint8_t cacheAddr = 0XFF; std::unique_ptr cacheTimer = nullptr; // we unfortunately have to build a map of hashes in case there is a // collision to verify our dev-id boost::container::flat_map> deviceHashes; void registerStorageFunctions() __attribute__((constructor)); static sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection()); bool writeFru() { sdbusplus::message::message writeFru = dbus.new_method_call( fruDeviceServiceName, "/xyz/openbmc_project/FruDevice", "xyz.openbmc_project.FruDeviceManager", "WriteFru"); writeFru.append(cacheBus, cacheAddr, fruCache); try { sdbusplus::message::message writeFruResp = dbus.call(writeFru); } catch (sdbusplus::exception_t&) { // todo: log sel? phosphor::logging::log( "error writing fru"); return false; } return true; } void createTimer() { if (cacheTimer == nullptr) { cacheTimer = std::make_unique(writeFru); } } ipmi_ret_t replaceCacheFru(uint8_t devId) { static uint8_t lastDevId = 0xFF; bool timerRunning = (cacheTimer != nullptr) && !cacheTimer->isExpired(); if (lastDevId == devId && timerRunning) { return IPMI_CC_OK; // cache already up to date } // if timer is running, stop it and writeFru manually else if (timerRunning) { cacheTimer->stop(); writeFru(); } sdbusplus::message::message getObjects = dbus.new_method_call( fruDeviceServiceName, "/", "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); ManagedObjectType frus; try { sdbusplus::message::message resp = dbus.call(getObjects); resp.read(frus); } catch (sdbusplus::exception_t&) { phosphor::logging::log( "replaceCacheFru: error getting managed objects"); return IPMI_CC_RESPONSE_ERROR; } deviceHashes.clear(); // hash the object paths to create unique device id's. increment on // collision std::hash hasher; for (const auto& fru : frus) { auto fruIface = fru.second.find("xyz.openbmc_project.FruDevice"); if (fruIface == fru.second.end()) { continue; } auto busFind = fruIface->second.find("BUS"); auto addrFind = fruIface->second.find("ADDRESS"); if (busFind == fruIface->second.end() || addrFind == fruIface->second.end()) { phosphor::logging::log( "fru device missing Bus or Address", phosphor::logging::entry("FRU=%s", fru.first.str.c_str())); continue; } uint8_t fruBus = sdbusplus::message::variant_ns::get(busFind->second); uint8_t fruAddr = sdbusplus::message::variant_ns::get(addrFind->second); uint8_t fruHash = 0; if (fruBus != 0 || fruAddr != 0) { fruHash = hasher(fru.first.str); // can't be 0xFF based on spec, and 0 is reserved for baseboard if (fruHash == 0 || fruHash == 0xFF) { fruHash = 1; } } std::pair newDev(fruBus, fruAddr); bool emplacePassed = false; while (!emplacePassed) { auto resp = deviceHashes.emplace(fruHash, newDev); emplacePassed = resp.second; if (!emplacePassed) { fruHash++; // can't be 0xFF based on spec, and 0 is reserved for // baseboard if (fruHash == 0XFF) { fruHash = 0x1; } } } } auto deviceFind = deviceHashes.find(devId); if (deviceFind == deviceHashes.end()) { return IPMI_CC_SENSOR_INVALID; } fruCache.clear(); sdbusplus::message::message getRawFru = dbus.new_method_call( fruDeviceServiceName, "/xyz/openbmc_project/FruDevice", "xyz.openbmc_project.FruDeviceManager", "GetRawFru"); cacheBus = deviceFind->second.first; cacheAddr = deviceFind->second.second; getRawFru.append(cacheBus, cacheAddr); try { sdbusplus::message::message getRawResp = dbus.call(getRawFru); getRawResp.read(fruCache); } catch (sdbusplus::exception_t&) { lastDevId = 0xFF; cacheBus = 0xFF; cacheAddr = 0xFF; return IPMI_CC_RESPONSE_ERROR; } lastDevId = devId; return IPMI_CC_OK; } ipmi_ret_t ipmiStorageReadFRUData(ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request, ipmi_response_t response, ipmi_data_len_t dataLen, ipmi_context_t context) { if (*dataLen != 4) { *dataLen = 0; return IPMI_CC_REQ_DATA_LEN_INVALID; } *dataLen = 0; // default to 0 in case of an error auto req = static_cast(request); if (req->countToRead > maxMessageSize - 1) { return IPMI_CC_INVALID_FIELD_REQUEST; } ipmi_ret_t status = replaceCacheFru(req->fruDeviceID); if (status != IPMI_CC_OK) { return status; } size_t fromFRUByteLen = 0; if (req->countToRead + req->fruInventoryOffset < fruCache.size()) { fromFRUByteLen = req->countToRead; } else if (fruCache.size() > req->fruInventoryOffset) { fromFRUByteLen = fruCache.size() - req->fruInventoryOffset; } size_t padByteLen = req->countToRead - fromFRUByteLen; uint8_t* respPtr = static_cast(response); *respPtr = req->countToRead; std::copy(fruCache.begin() + req->fruInventoryOffset, fruCache.begin() + req->fruInventoryOffset + fromFRUByteLen, ++respPtr); // if longer than the fru is requested, fill with 0xFF if (padByteLen) { respPtr += fromFRUByteLen; std::fill(respPtr, respPtr + padByteLen, 0xFF); } *dataLen = fromFRUByteLen + 1; return IPMI_CC_OK; } ipmi_ret_t ipmiStorageWriteFRUData(ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request, ipmi_response_t response, ipmi_data_len_t dataLen, ipmi_context_t context) { if (*dataLen < 4 || *dataLen >= 0xFF + 3) // count written return is one byte, so limit to one byte // of data after the three request data bytes { *dataLen = 0; return IPMI_CC_REQ_DATA_LEN_INVALID; } auto req = static_cast(request); size_t writeLen = *dataLen - 3; *dataLen = 0; // default to 0 in case of an error ipmi_ret_t status = replaceCacheFru(req->fruDeviceID); if (status != IPMI_CC_OK) { return status; } int lastWriteAddr = req->fruInventoryOffset + writeLen; if (fruCache.size() < lastWriteAddr) { fruCache.resize(req->fruInventoryOffset + writeLen); } std::copy(req->data, req->data + writeLen, fruCache.begin() + req->fruInventoryOffset); bool atEnd = false; if (fruCache.size() >= sizeof(FRUHeader)) { FRUHeader* header = reinterpret_cast(fruCache.data()); int lastRecordStart = std::max( header->internalOffset, std::max(header->chassisOffset, std::max(header->boardOffset, header->productOffset))); // TODO: Handle Multi-Record FRUs? lastRecordStart *= 8; // header starts in are multiples of 8 bytes // get the length of the area in multiples of 8 bytes if (lastWriteAddr > (lastRecordStart + 1)) { // second byte in record area is the length int areaLength(fruCache[lastRecordStart + 1]); areaLength *= 8; // it is in multiples of 8 bytes if (lastWriteAddr >= (areaLength + lastRecordStart)) { atEnd = true; } } } uint8_t* respPtr = static_cast(response); if (atEnd) { // cancel timer, we're at the end so might as well send it cacheTimer->stop(); if (!writeFru()) { return IPMI_CC_INVALID_FIELD_REQUEST; } *respPtr = std::min(fruCache.size(), static_cast(0xFF)); } else { // start a timer, if no further data is sent in cacheTimeoutSeconds // seconds, check to see if it is valid createTimer(); cacheTimer->start(std::chrono::duration_cast( std::chrono::seconds(cacheTimeoutSeconds))); *respPtr = 0; } *dataLen = 1; return IPMI_CC_OK; } ipmi_ret_t ipmiStorageGetFRUInvAreaInfo(ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request, ipmi_response_t response, ipmi_data_len_t dataLen, ipmi_context_t context) { if (*dataLen != 1) { *dataLen = 0; return IPMI_CC_REQ_DATA_LEN_INVALID; } *dataLen = 0; // default to 0 in case of an error uint8_t reqDev = *(static_cast(request)); if (reqDev == 0xFF) { return IPMI_CC_INVALID_FIELD_REQUEST; } ipmi_ret_t status = replaceCacheFru(reqDev); if (status != IPMI_CC_OK) { return status; } GetFRUAreaResp* respPtr = static_cast(response); respPtr->inventorySizeLSB = fruCache.size() & 0xFF; respPtr->inventorySizeMSB = fruCache.size() >> 8; respPtr->accessType = static_cast(GetFRUAreaAccessType::byte); *dataLen = sizeof(GetFRUAreaResp); return IPMI_CC_OK; } ipmi_ret_t getFruSdrCount(size_t& count) { ipmi_ret_t ret = replaceCacheFru(0); if (ret != IPMI_CC_OK) { return ret; } count = deviceHashes.size(); return IPMI_CC_OK; } ipmi_ret_t getFruSdrs(size_t index, get_sdr::SensorDataFruRecord& resp) { ipmi_ret_t ret = replaceCacheFru(0); // this will update the hash list if (ret != IPMI_CC_OK) { return ret; } if (deviceHashes.size() < index) { return IPMI_CC_INVALID_FIELD_REQUEST; } auto device = deviceHashes.begin() + index; uint8_t& bus = device->second.first; uint8_t& address = device->second.second; ManagedObjectType frus; sdbusplus::message::message getObjects = dbus.new_method_call( fruDeviceServiceName, "/", "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); try { sdbusplus::message::message resp = dbus.call(getObjects); resp.read(frus); } catch (sdbusplus::exception_t&) { return IPMI_CC_RESPONSE_ERROR; } boost::container::flat_map* fruData = nullptr; auto fru = std::find_if(frus.begin(), frus.end(), [bus, address, &fruData](ManagedEntry& entry) { auto findFruDevice = entry.second.find("xyz.openbmc_project.FruDevice"); if (findFruDevice == entry.second.end()) { return false; } fruData = &(findFruDevice->second); auto findBus = findFruDevice->second.find("BUS"); auto findAddress = findFruDevice->second.find("ADDRESS"); if (findBus == findFruDevice->second.end() || findAddress == findFruDevice->second.end()) { return false; } if (sdbusplus::message::variant_ns::get( findBus->second) != bus) { return false; } if (sdbusplus::message::variant_ns::get( findAddress->second) != address) { return false; } return true; }); if (fru == frus.end()) { return IPMI_CC_RESPONSE_ERROR; } std::string name; auto findProductName = fruData->find("BOARD_PRODUCT_NAME"); auto findBoardName = fruData->find("PRODUCT_PRODUCT_NAME"); if (findProductName != fruData->end()) { name = sdbusplus::message::variant_ns::get( findProductName->second); } else if (findBoardName != fruData->end()) { name = sdbusplus::message::variant_ns::get( findBoardName->second); } else { name = "UNKNOWN"; } if (name.size() > maxFruSdrNameSize) { name = name.substr(0, maxFruSdrNameSize); } size_t sizeDiff = maxFruSdrNameSize - name.size(); resp.header.record_id_lsb = 0x0; // calling code is to implement these resp.header.record_id_msb = 0x0; resp.header.sdr_version = ipmiSdrVersion; resp.header.record_type = 0x11; // FRU Device Locator resp.header.record_length = sizeof(resp.body) + sizeof(resp.key) - sizeDiff; resp.key.deviceAddress = 0x20; resp.key.fruID = device->first; resp.key.accessLun = 0x80; // logical / physical fru device resp.key.channelNumber = 0x0; resp.body.reserved = 0x0; resp.body.deviceType = 0x10; resp.body.entityID = 0x0; resp.body.entityInstance = 0x1; resp.body.oem = 0x0; resp.body.deviceIDLen = name.size(); name.copy(resp.body.deviceID, name.size()); return IPMI_CC_OK; } void registerStorageFunctions() { // ipmiPrintAndRegister( NETFUN_STORAGE, static_cast(IPMINetfnStorageCmds::ipmiCmdGetFRUInvAreaInfo), NULL, ipmiStorageGetFRUInvAreaInfo, PRIVILEGE_OPERATOR); // (IPMINetfnStorageCmds::ipmiCmdReadFRUData), NULL, ipmiStorageReadFRUData, PRIVILEGE_OPERATOR); // (IPMINetfnStorageCmds::ipmiCmdWriteFRUData), NULL, ipmiStorageWriteFRUData, PRIVILEGE_OPERATOR); } } // namespace storage } // namespace ipmi