blob: 46e097a1c8860ceb7a17f0756c4f248b1a3db7e7 [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 "psu.hpp"
18
19#include "main.hpp"
20
21#include <cstdint>
Patrick Venturece07ee02018-09-19 18:09:32 -070022#include <cstring>
Patrick Venture4d49ae62018-09-17 11:35:32 -070023#include <fstream>
24#include <phosphor-logging/log.hpp>
25#include <sdbusplus/bus.hpp>
26
27namespace google
28{
29namespace ipmi
30{
31
32using namespace phosphor::logging;
33
34struct PsuResetRequest
35{
36 uint8_t subcommand;
37 // Delay in seconds.
38 uint32_t delay;
39} __attribute__((packed));
40
41static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay";
42static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
43static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
44static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
45static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target";
46
47ipmi_ret_t PsuHardReset(const uint8_t* reqBuf, uint8_t* replyBuf,
48 size_t* dataLen)
49{
50 if ((*dataLen) < sizeof(struct PsuResetRequest))
51 {
Patrick Venturece07ee02018-09-19 18:09:32 -070052 std::fprintf(stderr, "Invalid command length: %u\n",
53 static_cast<uint32_t>(*dataLen));
Patrick Venture4d49ae62018-09-17 11:35:32 -070054 return IPMI_CC_INVALID;
55 }
56
57 struct PsuResetRequest request;
Patrick Venturece07ee02018-09-19 18:09:32 -070058 std::memcpy(&request, &reqBuf[0], sizeof(struct PsuResetRequest));
Patrick Venture4d49ae62018-09-17 11:35:32 -070059
60 std::ofstream ofs;
61 ofs.open(TIME_DELAY_FILENAME, std::ofstream::out);
62 if (!ofs.good())
63 {
Patrick Venturece07ee02018-09-19 18:09:32 -070064 std::fprintf(stderr, "Unable to open file for output.\n");
Patrick Venture4d49ae62018-09-17 11:35:32 -070065 return IPMI_CC_INVALID;
66 }
67
68 ofs << "PSU_HARDRESET_DELAY=" << request.delay << std::endl;
69 if (ofs.fail())
70 {
Patrick Venturece07ee02018-09-19 18:09:32 -070071 std::fprintf(stderr, "Write failed\n");
Patrick Venture4d49ae62018-09-17 11:35:32 -070072 ofs.close();
73 return IPMI_CC_INVALID;
74 }
75
76 // Write succeeded, please continue.
77 ofs.flush();
78 ofs.close();
79
80 auto bus = sdbusplus::bus::new_default();
81 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
82 SYSTEMD_INTERFACE, "StartUnit");
83
84 method.append(PSU_HARDRESET_TARGET);
85 method.append("replace");
86
87 try
88 {
89 bus.call_noreply(method);
90 }
91 catch (const sdbusplus::exception::SdBusError& ex)
92 {
93 log<level::ERR>("Failed to call PSU hard reset");
94 return IPMI_CC_INVALID;
95 }
96
97 replyBuf[0] = SysPsuHardReset;
98 (*dataLen) = sizeof(uint8_t);
99
100 return IPMI_CC_OK;
101}
102
103} // namespace ipmi
104} // namespace google