blob: 1e46adcbdb6a21444b44788d6d0efdbdb2259329 [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{
Patrick Ventureacd54232018-10-16 16:58:59 -070050 struct PsuResetRequest request;
51
52 if ((*dataLen) < sizeof(request))
Patrick Venture4d49ae62018-09-17 11:35:32 -070053 {
Patrick Venturece07ee02018-09-19 18:09:32 -070054 std::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
Patrick Venturece07ee02018-09-19 18:09:32 -070059 std::memcpy(&request, &reqBuf[0], sizeof(struct PsuResetRequest));
Patrick Venture4d49ae62018-09-17 11:35:32 -070060
61 std::ofstream ofs;
62 ofs.open(TIME_DELAY_FILENAME, std::ofstream::out);
63 if (!ofs.good())
64 {
Patrick Venturece07ee02018-09-19 18:09:32 -070065 std::fprintf(stderr, "Unable to open file for output.\n");
Patrick Venture4d49ae62018-09-17 11:35:32 -070066 return IPMI_CC_INVALID;
67 }
68
69 ofs << "PSU_HARDRESET_DELAY=" << request.delay << std::endl;
70 if (ofs.fail())
71 {
Patrick Venturece07ee02018-09-19 18:09:32 -070072 std::fprintf(stderr, "Write failed\n");
Patrick Venture4d49ae62018-09-17 11:35:32 -070073 ofs.close();
74 return IPMI_CC_INVALID;
75 }
76
77 // Write succeeded, please continue.
78 ofs.flush();
79 ofs.close();
80
81 auto bus = sdbusplus::bus::new_default();
82 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
83 SYSTEMD_INTERFACE, "StartUnit");
84
85 method.append(PSU_HARDRESET_TARGET);
86 method.append("replace");
87
88 try
89 {
90 bus.call_noreply(method);
91 }
92 catch (const sdbusplus::exception::SdBusError& ex)
93 {
94 log<level::ERR>("Failed to call PSU hard reset");
95 return IPMI_CC_INVALID;
96 }
97
98 replyBuf[0] = SysPsuHardReset;
99 (*dataLen) = sizeof(uint8_t);
100
101 return IPMI_CC_OK;
102}
103
104} // namespace ipmi
105} // namespace google