blob: e3a5e0863c9a791337131d630a7a8d5f66685639 [file] [log] [blame]
Patrick Venture22e38752018-11-21 08:52:49 -08001/*
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
Patrick Venture1cde5f92018-11-07 08:26:47 -080017#include "lpc_handler.hpp"
18
19#include <cstdint>
Patrick Venture8c535332018-11-08 15:58:00 -080020#include <cstring>
Patrick Venture1cde5f92018-11-07 08:26:47 -080021#include <vector>
22
Patrick Venture1d5a31c2019-05-20 11:38:22 -070023namespace ipmi_flash
Patrick Venture1cde5f92018-11-07 08:26:47 -080024{
25
Patrick Venture0d2a8132018-11-09 11:34:21 -080026bool LpcDataHandler::open()
27{
28 /* For the ASPEED LPC CTRL driver, the ioctl is required to set up the
Patrick Venture74304642019-01-17 09:31:04 -080029 * window, with information from writeMeta() below.
Patrick Venture0d2a8132018-11-09 11:34:21 -080030 */
31 return true;
32}
33
Patrick Venture0fbabf22018-11-09 11:54:12 -080034bool LpcDataHandler::close()
35{
Patrick Venture2343cfc2019-01-17 13:04:36 -080036 mapper->close();
Patrick Venture09c4f7a2018-11-16 20:28:04 -080037
38 return setInitializedAndReturn(false);
Patrick Venture0fbabf22018-11-09 11:54:12 -080039}
40
Patrick Venture1cde5f92018-11-07 08:26:47 -080041std::vector<std::uint8_t> LpcDataHandler::copyFrom(std::uint32_t length)
42{
Patrick Venturef9047502018-11-15 08:44:14 -080043 /* TODO: implement this -- in an earlier and different version of this that
44 * didn't use BLOBs, the region was memory-mapped and the writes to the data
45 * were just done directly from the memory-mapped region instead of a
46 * copyFrom() first call. The idea with this change is that we may not be
47 * able to get a memory-mapped handle from the driver from which to
48 * automatically read data, but rather must perform some ioctl or other
49 * access to get the data from the driver.
50 */
Patrick Venture09c4f7a2018-11-16 20:28:04 -080051 if (!initialized)
52 {
53 /* TODO: Consider designing some exceptions we can catch for when there
54 * is an error.
55 */
56 return {};
57 }
58
Patrick Venture36bffb42019-06-24 10:47:47 -070059 std::vector<std::uint8_t> results(length);
60 std::memcpy(results.data(), memory.mapped + mappingResult.windowOffset,
61 length);
62
63 return results;
Patrick Venture1cde5f92018-11-07 08:26:47 -080064}
65
Patrick Venture74304642019-01-17 09:31:04 -080066bool LpcDataHandler::writeMeta(const std::vector<std::uint8_t>& configuration)
Patrick Venture8c535332018-11-08 15:58:00 -080067{
Patrick Venture043bafa2018-11-15 08:41:04 -080068 struct LpcRegion lpcRegion;
Patrick Venture8c535332018-11-08 15:58:00 -080069
Patrick Venture043bafa2018-11-15 08:41:04 -080070 if (configuration.size() != sizeof(lpcRegion))
Patrick Venture8c535332018-11-08 15:58:00 -080071 {
72 return false;
73 }
74
Patrick Venture043bafa2018-11-15 08:41:04 -080075 std::memcpy(&lpcRegion, configuration.data(), configuration.size());
76
Patrick Venture043bafa2018-11-15 08:41:04 -080077 /* TODO: LpcRegion sanity checking. */
Patrick Venture36bffb42019-06-24 10:47:47 -070078 mappingResult = mapper->mapWindow(lpcRegion.address, lpcRegion.length);
79 if (mappingResult.response != 0)
Patrick Venture043bafa2018-11-15 08:41:04 -080080 {
Patrick Venture36bffb42019-06-24 10:47:47 -070081 std::fprintf(stderr, "mappingResult.response %u\n",
82 mappingResult.response);
Patrick Venture043bafa2018-11-15 08:41:04 -080083 /* Failed to map region. */
Patrick Venture09c4f7a2018-11-16 20:28:04 -080084 return false;
Patrick Venture043bafa2018-11-15 08:41:04 -080085 }
Patrick Venture8c535332018-11-08 15:58:00 -080086
Patrick Venture09c4f7a2018-11-16 20:28:04 -080087 return setInitializedAndReturn(true);
Patrick Venture8c535332018-11-08 15:58:00 -080088}
89
Patrick Venture74304642019-01-17 09:31:04 -080090std::vector<std::uint8_t> LpcDataHandler::readMeta()
Patrick Venture8c535332018-11-08 15:58:00 -080091{
Patrick Venture36bffb42019-06-24 10:47:47 -070092 /* Return the MemoryResult structure packed. */
93 std::vector<std::uint8_t> output(
94 sizeof(std::uint8_t) + sizeof(std::uint32_t) + sizeof(std::uint32_t));
95
96 int index = 0;
97 std::memcpy(&output[index], &mappingResult.response,
98 sizeof(mappingResult.response));
99
100 index += sizeof(mappingResult.response);
101 std::memcpy(&output[index], &mappingResult.windowOffset,
102 sizeof(mappingResult.windowOffset));
103
104 index += sizeof(mappingResult.windowOffset);
105 std::memcpy(&output[index], &mappingResult.windowSize,
106 sizeof(mappingResult.windowSize));
107
108 return output;
Patrick Venture8c535332018-11-08 15:58:00 -0800109}
110
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700111} // namespace ipmi_flash