-
Notifications
You must be signed in to change notification settings - Fork 2
/
bios_setting.cpp
127 lines (112 loc) · 3.88 KB
/
bios_setting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2024 Google LLC
//
// 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 "bios_setting.hpp"
#include "commands.hpp"
#include "errors.hpp"
#include "handler.hpp"
#include <ipmid/api-types.hpp>
#include <stdplus/fd/create.hpp>
#include <stdplus/fd/managed.hpp>
#include <stdplus/fd/ops.hpp>
#include <stdplus/numeric/endian.hpp>
#include <stdplus/print.hpp>
#include <stdplus/raw.hpp>
#include <filesystem>
#include <fstream>
#include <span>
#include <vector>
namespace google
{
namespace ipmi
{
std::vector<uint8_t> readBiosSettingFromFile(const std::string& biosSettingPath)
{
std::vector<uint8_t> biosSettings;
try
{
stdplus::ManagedFd managedFd = stdplus::fd::open(
biosSettingPath,
stdplus::fd::OpenFlags(stdplus::fd::OpenAccess::ReadOnly));
biosSettings = stdplus::fd::readAll<std::vector<uint8_t>>(managedFd);
}
catch (const std::exception& e)
{
stdplus::print(stderr, "Read unsuccessful: {}\n", e.what());
return {};
}
return biosSettings;
}
Resp readBiosSetting(std::span<const uint8_t>, HandlerInterface*,
const std::string& biosSettingPath)
{
std::vector<uint8_t> biosSettings =
readBiosSettingFromFile(biosSettingPath);
size_t settingsLength = biosSettings.size();
if (settingsLength == 0)
{
return ::ipmi::responseRetBytesUnavailable();
}
// Reply format is: Length of the payload (1 byte) + payload
std::vector<std::uint8_t> reply;
reply.reserve(1 + settingsLength);
reply.emplace_back(static_cast<uint8_t>(settingsLength));
reply.insert(reply.end(), biosSettings.begin(), biosSettings.end());
return ::ipmi::responseSuccess(SysOEMCommands::SysReadBiosSetting, reply);
}
Resp writeBiosSetting(std::span<const uint8_t> data, HandlerInterface*,
const std::string& biosSettingPath)
{
std::uint8_t payloadSize;
try
{
// This subspans the data automatically
payloadSize = stdplus::raw::extract<
stdplus::EndianPacked<decltype(payloadSize), std::endian::little>>(
data);
}
catch (const std::exception& e)
{
stdplus::print(stderr, "Extracting payload failed: {}\n", e.what());
return ::ipmi::responseReqDataLenInvalid();
}
if (data.size() != payloadSize)
{
stdplus::print(stderr, "Invalid command length {} vs. payloadSize {}\n",
static_cast<uint32_t>(data.size()),
static_cast<uint32_t>(payloadSize));
return ::ipmi::responseReqDataLenInvalid();
}
// Write the setting
try
{
stdplus::ManagedFd managedFd = stdplus::fd::open(
biosSettingPath,
stdplus::fd::OpenFlags(stdplus::fd::OpenAccess::WriteOnly)
.set(stdplus::fd::OpenFlag::Trunc)
.set(stdplus::fd::OpenFlag::Create));
stdplus::fd::writeExact(managedFd, data);
}
catch (const std::exception& e)
{
stdplus::print(stderr, "Write unsuccessful: {}\n", e.what());
return ::ipmi::responseRetBytesUnavailable();
}
// Reply format is: Length of the payload written
std::vector<std::uint8_t> reply;
reply.reserve(1);
reply.emplace_back(static_cast<uint8_t>(payloadSize));
return ::ipmi::responseSuccess(SysOEMCommands::SysWriteBiosSetting, reply);
}
} // namespace ipmi
} // namespace google