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