1 /* 2 * Copyright 2018 Google Inc. 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 "cpld.hpp" 18 19 #include "main.hpp" 20 21 #include <experimental/filesystem> 22 #include <fstream> 23 #include <sstream> 24 25 namespace google 26 { 27 namespace ipmi 28 { 29 namespace fs = std::experimental::filesystem; 30 31 struct CpldRequest 32 { 33 uint8_t subcommand; 34 uint8_t id; 35 } __attribute__((packed)); 36 37 struct CpldReply 38 { 39 uint8_t subcommand; 40 uint8_t major; 41 uint8_t minor; 42 uint8_t point; 43 uint8_t subpoint; 44 } __attribute__((packed)); 45 46 // 47 // Handle reading the cpld version from the tmpfs. 48 // 49 ipmi_ret_t CpldVersion(const uint8_t* reqBuf, uint8_t* replyBuf, 50 size_t* dataLen) 51 { 52 if ((*dataLen) < sizeof(struct CpldRequest)) 53 { 54 fprintf(stderr, "Invalid command length: %lu\n", (*dataLen)); 55 return IPMI_CC_INVALID; 56 } 57 58 // reqBuf[0] is the subcommand. 59 // reqBuf[1] is the CPLD id. "/run/cpld{id}.version" is what we read. 60 // Verified that this cast actually returns the value 255 and not something 61 // negative in the case where reqBuf[1] is 0xff. However, it looks weird 62 // since I would expect int(uint8(0xff)) to be -1. So, just cast it 63 // unsigned. we're casting to an int width to avoid it thinking it's a 64 // letter, because it does that. 65 66 const auto request = 67 reinterpret_cast<const struct CpldRequest*>(&reqBuf[0]); 68 69 std::ostringstream opath; 70 opath << "/run/cpld" << static_cast<unsigned int>(request->id) 71 << ".version"; 72 // Check for file 73 74 std::error_code ec; 75 if (!fs::exists(opath.str(), ec)) 76 { 77 fprintf(stderr, "Path: '%s' doesn't exist.\n", opath.str().c_str()); 78 return IPMI_CC_INVALID; 79 } 80 // We're uninterested in the state of ec. 81 82 // If file exists, read. 83 std::ifstream ifs; 84 ifs.exceptions(std::ifstream::failbit); 85 std::string value; 86 try 87 { 88 ifs.open(opath.str()); 89 ifs >> value; 90 } 91 catch (std::ios_base::failure& fail) 92 { 93 return IPMI_CC_INVALID; 94 } 95 96 // If value parses as expected, return version. 97 int major = 0; 98 int minor = 0; 99 int point = 0; 100 int subpoint = 0; 101 102 int num_fields = 103 sscanf(value.c_str(), "%d.%d.%d.%d", &major, &minor, &point, &subpoint); 104 if (num_fields == 0) 105 { 106 fprintf(stderr, "Invalid version.\n"); 107 return IPMI_CC_INVALID; 108 } 109 110 // Truncate if the version is too high (documented). 111 struct CpldReply reply; 112 reply.subcommand = SysCpldVersion; 113 reply.major = static_cast<uint8_t>(major); 114 reply.minor = static_cast<uint8_t>(minor); 115 reply.point = static_cast<uint8_t>(point); 116 reply.subpoint = static_cast<uint8_t>(subpoint); 117 118 memcpy(&replyBuf[0], &reply, sizeof(struct CpldReply)); 119 (*dataLen) = sizeof(struct CpldReply); 120 121 return IPMI_CC_OK; 122 } 123 124 } // namespace ipmi 125 } // namespace google 126