Patrick Venture | 22e3875 | 2018-11-21 08:52:49 -0800 | [diff] [blame] | 1 | /* |
| 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 Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 17 | #include "lpc_handler.hpp" |
| 18 | |
Patrick Venture | a606550 | 2020-10-26 13:06:40 -0700 | [diff] [blame] | 19 | #include "mapper_errors.hpp" |
| 20 | |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 21 | #include <cstdint> |
Patrick Venture | a606550 | 2020-10-26 13:06:40 -0700 | [diff] [blame] | 22 | #include <cstdio> |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 23 | #include <cstring> |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 26 | namespace ipmi_flash |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 27 | { |
| 28 | |
Patrick Venture | a606550 | 2020-10-26 13:06:40 -0700 | [diff] [blame] | 29 | bool LpcDataHandler::setInitializedAndReturn(bool value) |
| 30 | { |
| 31 | if (value) |
| 32 | { |
| 33 | try |
| 34 | { |
| 35 | /* Try really opening the map. */ |
| 36 | memory = mapper->open(); |
| 37 | } |
| 38 | catch (const MapperException& e) |
| 39 | { |
| 40 | std::fprintf(stderr, "received mapper exception: %s\n", e.what()); |
| 41 | return false; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | initialized = value; |
| 46 | return value; |
| 47 | } |
| 48 | |
Patrick Venture | 0d2a813 | 2018-11-09 11:34:21 -0800 | [diff] [blame] | 49 | bool LpcDataHandler::open() |
| 50 | { |
| 51 | /* For the ASPEED LPC CTRL driver, the ioctl is required to set up the |
Patrick Venture | 7430464 | 2019-01-17 09:31:04 -0800 | [diff] [blame] | 52 | * window, with information from writeMeta() below. |
Patrick Venture | 0d2a813 | 2018-11-09 11:34:21 -0800 | [diff] [blame] | 53 | */ |
| 54 | return true; |
| 55 | } |
| 56 | |
Patrick Venture | 0fbabf2 | 2018-11-09 11:54:12 -0800 | [diff] [blame] | 57 | bool LpcDataHandler::close() |
| 58 | { |
Patrick Venture | 2343cfc | 2019-01-17 13:04:36 -0800 | [diff] [blame] | 59 | mapper->close(); |
Patrick Venture | 09c4f7a | 2018-11-16 20:28:04 -0800 | [diff] [blame] | 60 | |
| 61 | return setInitializedAndReturn(false); |
Patrick Venture | 0fbabf2 | 2018-11-09 11:54:12 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 64 | std::vector<std::uint8_t> LpcDataHandler::copyFrom(std::uint32_t length) |
| 65 | { |
Patrick Venture | f904750 | 2018-11-15 08:44:14 -0800 | [diff] [blame] | 66 | /* TODO: implement this -- in an earlier and different version of this that |
| 67 | * didn't use BLOBs, the region was memory-mapped and the writes to the data |
| 68 | * were just done directly from the memory-mapped region instead of a |
| 69 | * copyFrom() first call. The idea with this change is that we may not be |
| 70 | * able to get a memory-mapped handle from the driver from which to |
| 71 | * automatically read data, but rather must perform some ioctl or other |
| 72 | * access to get the data from the driver. |
| 73 | */ |
Patrick Venture | 09c4f7a | 2018-11-16 20:28:04 -0800 | [diff] [blame] | 74 | if (!initialized) |
| 75 | { |
| 76 | /* TODO: Consider designing some exceptions we can catch for when there |
| 77 | * is an error. |
| 78 | */ |
| 79 | return {}; |
| 80 | } |
| 81 | |
Patrick Venture | 36bffb4 | 2019-06-24 10:47:47 -0700 | [diff] [blame] | 82 | std::vector<std::uint8_t> results(length); |
| 83 | std::memcpy(results.data(), memory.mapped + mappingResult.windowOffset, |
| 84 | length); |
| 85 | |
| 86 | return results; |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Patrick Venture | 7430464 | 2019-01-17 09:31:04 -0800 | [diff] [blame] | 89 | bool LpcDataHandler::writeMeta(const std::vector<std::uint8_t>& configuration) |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 90 | { |
Patrick Venture | 043bafa | 2018-11-15 08:41:04 -0800 | [diff] [blame] | 91 | struct LpcRegion lpcRegion; |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 92 | |
Patrick Venture | 043bafa | 2018-11-15 08:41:04 -0800 | [diff] [blame] | 93 | if (configuration.size() != sizeof(lpcRegion)) |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 94 | { |
| 95 | return false; |
| 96 | } |
| 97 | |
Patrick Venture | 043bafa | 2018-11-15 08:41:04 -0800 | [diff] [blame] | 98 | std::memcpy(&lpcRegion, configuration.data(), configuration.size()); |
| 99 | |
Patrick Venture | 043bafa | 2018-11-15 08:41:04 -0800 | [diff] [blame] | 100 | /* TODO: LpcRegion sanity checking. */ |
Patrick Venture | 36bffb4 | 2019-06-24 10:47:47 -0700 | [diff] [blame] | 101 | mappingResult = mapper->mapWindow(lpcRegion.address, lpcRegion.length); |
| 102 | if (mappingResult.response != 0) |
Patrick Venture | 043bafa | 2018-11-15 08:41:04 -0800 | [diff] [blame] | 103 | { |
Patrick Venture | 36bffb4 | 2019-06-24 10:47:47 -0700 | [diff] [blame] | 104 | std::fprintf(stderr, "mappingResult.response %u\n", |
| 105 | mappingResult.response); |
Patrick Venture | 043bafa | 2018-11-15 08:41:04 -0800 | [diff] [blame] | 106 | /* Failed to map region. */ |
Patrick Venture | 09c4f7a | 2018-11-16 20:28:04 -0800 | [diff] [blame] | 107 | return false; |
Patrick Venture | 043bafa | 2018-11-15 08:41:04 -0800 | [diff] [blame] | 108 | } |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 109 | |
Patrick Venture | 09c4f7a | 2018-11-16 20:28:04 -0800 | [diff] [blame] | 110 | return setInitializedAndReturn(true); |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Patrick Venture | 7430464 | 2019-01-17 09:31:04 -0800 | [diff] [blame] | 113 | std::vector<std::uint8_t> LpcDataHandler::readMeta() |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 114 | { |
Patrick Venture | 36bffb4 | 2019-06-24 10:47:47 -0700 | [diff] [blame] | 115 | /* Return the MemoryResult structure packed. */ |
| 116 | std::vector<std::uint8_t> output( |
| 117 | sizeof(std::uint8_t) + sizeof(std::uint32_t) + sizeof(std::uint32_t)); |
| 118 | |
| 119 | int index = 0; |
| 120 | std::memcpy(&output[index], &mappingResult.response, |
| 121 | sizeof(mappingResult.response)); |
| 122 | |
| 123 | index += sizeof(mappingResult.response); |
| 124 | std::memcpy(&output[index], &mappingResult.windowOffset, |
| 125 | sizeof(mappingResult.windowOffset)); |
| 126 | |
| 127 | index += sizeof(mappingResult.windowOffset); |
| 128 | std::memcpy(&output[index], &mappingResult.windowSize, |
| 129 | sizeof(mappingResult.windowSize)); |
| 130 | |
| 131 | return output; |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 134 | } // namespace ipmi_flash |