Andrew Jeffery | 4fe996c | 2018-02-27 12:16:48 +1030 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
Andrew Jeffery | 71eaa73 | 2018-08-08 16:44:24 +0930 | [diff] [blame] | 3 | extern "C" { |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 4 | #include "mboxd.h" |
Andrew Jeffery | 71eaa73 | 2018-08-08 16:44:24 +0930 | [diff] [blame] | 5 | } |
| 6 | |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 7 | #include "config.h" |
| 8 | |
Andrew Jeffery | fb01e14 | 2019-03-18 13:17:08 +1030 | [diff] [blame] | 9 | #include "vpnor/partition.hpp" |
Andrew Jeffery | de08ca2 | 2019-03-18 13:23:46 +1030 | [diff] [blame] | 10 | #include "vpnor/table.hpp" |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 11 | #include "xyz/openbmc_project/Common/error.hpp" |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 12 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 13 | #include <assert.h> |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 14 | #include <fcntl.h> |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 15 | #include <stdint.h> |
| 16 | #include <stdlib.h> |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 17 | #include <sys/ioctl.h> |
| 18 | #include <sys/mman.h> |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | #include <syslog.h> |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 21 | #include <unistd.h> |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 22 | |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 23 | #include <exception> |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 24 | #include <iostream> |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 25 | #include <phosphor-logging/elog-errors.hpp> |
| 26 | #include <phosphor-logging/log.hpp> |
| 27 | #include <stdexcept> |
| 28 | #include <string> |
| 29 | |
| 30 | #include "common.h" |
Andrew Jeffery | f4bc335 | 2019-03-18 12:09:48 +1030 | [diff] [blame] | 31 | #include "vpnor/backend.h" |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 32 | |
| 33 | namespace openpower |
| 34 | { |
| 35 | namespace virtual_pnor |
| 36 | { |
| 37 | |
Patrick Williams | 150be91 | 2022-06-16 16:46:09 -0500 | [diff] [blame^] | 38 | namespace fs = std::filesystem; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 39 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 40 | fs::path Request::getPartitionFilePath(int flags) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 41 | { |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 42 | struct vpnor_data* priv = (struct vpnor_data*)backend->priv; |
| 43 | |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 44 | // Check if partition exists in patch location |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 45 | auto dst = fs::path(priv->paths.patch_loc) / partition.data.name; |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 46 | if (fs::is_regular_file(dst)) |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 47 | { |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 48 | return dst; |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 49 | } |
| 50 | |
Adriana Kobylak | 5ff50e3 | 2020-11-30 16:25:36 -0600 | [diff] [blame] | 51 | // Check if partition exists in rw location (default) |
| 52 | dst = fs::path(priv->paths.rw_loc) / partition.data.name; |
| 53 | if (fs::exists(dst)) |
| 54 | { |
| 55 | return dst; |
| 56 | } |
| 57 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 58 | switch (partition.data.user.data[1] & |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 59 | (PARTITION_PRESERVED | PARTITION_READONLY)) |
| 60 | { |
| 61 | case PARTITION_PRESERVED: |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 62 | dst = priv->paths.prsv_loc; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 63 | break; |
| 64 | |
| 65 | case PARTITION_READONLY: |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 66 | dst = priv->paths.ro_loc; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 67 | break; |
| 68 | |
| 69 | default: |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 70 | dst = priv->paths.rw_loc; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 71 | } |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 72 | dst /= partition.data.name; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 73 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 74 | if (fs::exists(dst)) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 75 | { |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 76 | return dst; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 77 | } |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 78 | |
| 79 | if (flags == O_RDONLY) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 80 | { |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 81 | dst = fs::path(priv->paths.ro_loc) / partition.data.name; |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 82 | assert(fs::exists(dst)); |
| 83 | return dst; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 84 | } |
| 85 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 86 | assert(flags == O_RDWR); |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 87 | auto src = fs::path(priv->paths.ro_loc) / partition.data.name; |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 88 | assert(fs::exists(src)); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 89 | |
Andrew Jeffery | e8a79ff | 2018-02-27 10:56:32 +1030 | [diff] [blame] | 90 | MSG_DBG("RWRequest: Didn't find '%s' under '%s', copying from '%s'\n", |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 91 | partition.data.name, dst.c_str(), src.c_str()); |
Andrew Jeffery | e8a79ff | 2018-02-27 10:56:32 +1030 | [diff] [blame] | 92 | |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 93 | dst = priv->paths.rw_loc; |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 94 | if (partition.data.user.data[1] & PARTITION_PRESERVED) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 95 | { |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 96 | dst = priv->paths.prsv_loc; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 97 | } |
| 98 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 99 | dst /= partition.data.name; |
| 100 | fs::copy_file(src, dst); |
Patrick Williams | 150be91 | 2022-06-16 16:46:09 -0500 | [diff] [blame^] | 101 | fs::permissions(dst, fs::perms::owner_write, fs::perm_options::add); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 102 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 103 | return dst; |
| 104 | } |
| 105 | |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 106 | size_t Request::clamp(size_t len) |
| 107 | { |
| 108 | size_t maxAccess = offset + len; |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 109 | size_t partSize = partition.data.size << backend->block_size_shift; |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 110 | return std::min(maxAccess, partSize) - offset; |
| 111 | } |
| 112 | |
Andrew Jeffery | e2744c0 | 2020-01-28 15:08:13 +1030 | [diff] [blame] | 113 | /* Perform reads and writes for an entire buffer's worth of data |
| 114 | * |
| 115 | * Post-condition: All bytes read or written, or an error has occurred |
| 116 | * |
| 117 | * Yields 0 if the entire buffer was processed, otherwise -1 |
| 118 | */ |
| 119 | #define fd_process_all(fn, fd, src, len) \ |
| 120 | ({ \ |
| 121 | size_t __len = len; \ |
| 122 | ssize_t __accessed = 0; \ |
| 123 | do \ |
| 124 | { \ |
| 125 | __len -= __accessed; \ |
| 126 | __accessed = TEMP_FAILURE_RETRY(fn(fd, src, __len)); \ |
| 127 | } while (__len > 0 && __accessed > 0); \ |
| 128 | __len ? -1 : 0; \ |
| 129 | }) |
| 130 | |
| 131 | ssize_t Request::read(void* dst, size_t len) |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 132 | { |
Andrew Jeffery | e2744c0 | 2020-01-28 15:08:13 +1030 | [diff] [blame] | 133 | len = clamp(len); |
| 134 | |
| 135 | fs::path path = getPartitionFilePath(O_RDONLY); |
| 136 | |
| 137 | MSG_INFO("Fulfilling read request against %s at offset 0x%zx into %p " |
| 138 | "for %zu\n", |
| 139 | path.c_str(), offset, dst, len); |
| 140 | |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 141 | size_t fileSize = fs::file_size(path); |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 142 | |
Andrew Jeffery | 5b97044 | 2020-01-31 16:49:22 +1030 | [diff] [blame] | 143 | size_t access_len = 0; |
| 144 | if (offset < fileSize) |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 145 | { |
Andrew Jeffery | 5b97044 | 2020-01-31 16:49:22 +1030 | [diff] [blame] | 146 | int fd = ::open(path.c_str(), O_RDONLY); |
| 147 | if (fd == -1) |
| 148 | { |
| 149 | MSG_ERR("Failed to open backing file at '%s': %d\n", path.c_str(), |
| 150 | errno); |
| 151 | throw std::system_error(errno, std::system_category()); |
| 152 | } |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 153 | |
Andrew Jeffery | 5b97044 | 2020-01-31 16:49:22 +1030 | [diff] [blame] | 154 | int rc = lseek(fd, offset, SEEK_SET); |
| 155 | if (rc < 0) |
| 156 | { |
Andrew Jeffery | 02821c6 | 2020-01-31 16:56:03 +1030 | [diff] [blame] | 157 | int lerrno = errno; |
| 158 | close(fd); |
Andrew Jeffery | 5b97044 | 2020-01-31 16:49:22 +1030 | [diff] [blame] | 159 | MSG_ERR("Failed to seek to %zu in %s (%zu bytes): %d\n", offset, |
Andrew Jeffery | 02821c6 | 2020-01-31 16:56:03 +1030 | [diff] [blame] | 160 | path.c_str(), fileSize, lerrno); |
| 161 | throw std::system_error(lerrno, std::system_category()); |
Andrew Jeffery | 5b97044 | 2020-01-31 16:49:22 +1030 | [diff] [blame] | 162 | } |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 163 | |
Andrew Jeffery | 5b97044 | 2020-01-31 16:49:22 +1030 | [diff] [blame] | 164 | access_len = std::min(len, fileSize - offset); |
| 165 | rc = fd_process_all(::read, fd, dst, access_len); |
| 166 | if (rc < 0) |
| 167 | { |
Andrew Jeffery | 02821c6 | 2020-01-31 16:56:03 +1030 | [diff] [blame] | 168 | int lerrno = errno; |
| 169 | close(fd); |
Andrew Jeffery | 5b97044 | 2020-01-31 16:49:22 +1030 | [diff] [blame] | 170 | MSG_ERR( |
| 171 | "Requested %zu bytes but failed to read %zu from %s (%zu) at " |
Andrew Jeffery | e2744c0 | 2020-01-28 15:08:13 +1030 | [diff] [blame] | 172 | "%zu: %d\n", |
Andrew Jeffery | 02821c6 | 2020-01-31 16:56:03 +1030 | [diff] [blame] | 173 | len, access_len, path.c_str(), fileSize, offset, lerrno); |
| 174 | throw std::system_error(lerrno, std::system_category()); |
Andrew Jeffery | 5b97044 | 2020-01-31 16:49:22 +1030 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | close(fd); |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 178 | } |
Andrew Jeffery | e2744c0 | 2020-01-28 15:08:13 +1030 | [diff] [blame] | 179 | |
| 180 | /* Set any remaining buffer space to the erased state */ |
| 181 | memset((char*)dst + access_len, 0xff, len - access_len); |
| 182 | |
Andrew Jeffery | e2744c0 | 2020-01-28 15:08:13 +1030 | [diff] [blame] | 183 | return len; |
| 184 | } |
| 185 | |
| 186 | ssize_t Request::write(void* dst, size_t len) |
| 187 | { |
| 188 | if (len != clamp(len)) |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 189 | { |
Andrew Jeffery | e2744c0 | 2020-01-28 15:08:13 +1030 | [diff] [blame] | 190 | std::stringstream err; |
| 191 | err << "Request size 0x" << std::hex << len << " from offset 0x" |
| 192 | << std::hex << offset << " exceeds the partition size 0x" |
| 193 | << std::hex << (partition.data.size << backend->block_size_shift); |
| 194 | throw OutOfBoundsOffset(err.str()); |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 195 | } |
Andrew Jeffery | e2744c0 | 2020-01-28 15:08:13 +1030 | [diff] [blame] | 196 | |
| 197 | /* Ensure file is at least the size of the maximum access */ |
| 198 | fs::path path = getPartitionFilePath(O_RDWR); |
| 199 | |
| 200 | MSG_INFO("Fulfilling write request against %s at offset 0x%zx from %p " |
| 201 | "for %zu\n", |
| 202 | path.c_str(), offset, dst, len); |
| 203 | |
| 204 | int fd = ::open(path.c_str(), O_RDWR); |
| 205 | if (fd == -1) |
| 206 | { |
| 207 | MSG_ERR("Failed to open backing file at '%s': %d\n", path.c_str(), |
| 208 | errno); |
| 209 | throw std::system_error(errno, std::system_category()); |
| 210 | } |
| 211 | |
| 212 | int rc = lseek(fd, offset, SEEK_SET); |
| 213 | if (rc < 0) |
| 214 | { |
Andrew Jeffery | 02821c6 | 2020-01-31 16:56:03 +1030 | [diff] [blame] | 215 | int lerrno = errno; |
| 216 | close(fd); |
Andrew Jeffery | e2744c0 | 2020-01-28 15:08:13 +1030 | [diff] [blame] | 217 | MSG_ERR("Failed to seek to %zu in %s: %d\n", offset, path.c_str(), |
Andrew Jeffery | 02821c6 | 2020-01-31 16:56:03 +1030 | [diff] [blame] | 218 | lerrno); |
| 219 | throw std::system_error(lerrno, std::system_category()); |
Andrew Jeffery | e2744c0 | 2020-01-28 15:08:13 +1030 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | rc = fd_process_all(::write, fd, dst, len); |
| 223 | if (rc < 0) |
| 224 | { |
Andrew Jeffery | 02821c6 | 2020-01-31 16:56:03 +1030 | [diff] [blame] | 225 | int lerrno = errno; |
| 226 | close(fd); |
Andrew Jeffery | e2744c0 | 2020-01-28 15:08:13 +1030 | [diff] [blame] | 227 | MSG_ERR("Failed to write %zu bytes to %s at %zu: %d\n", len, |
Andrew Jeffery | 02821c6 | 2020-01-31 16:56:03 +1030 | [diff] [blame] | 228 | path.c_str(), offset, lerrno); |
| 229 | throw std::system_error(lerrno, std::system_category()); |
Andrew Jeffery | e2744c0 | 2020-01-28 15:08:13 +1030 | [diff] [blame] | 230 | } |
| 231 | backend_set_bytemap(backend, base + offset, len, FLASH_DIRTY); |
| 232 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 233 | close(fd); |
| 234 | |
| 235 | return len; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 236 | } |
| 237 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 238 | } // namespace virtual_pnor |
| 239 | } // namespace openpower |