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 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 38 | namespace fs = std::experimental::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 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 51 | switch (partition.data.user.data[1] & |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 52 | (PARTITION_PRESERVED | PARTITION_READONLY)) |
| 53 | { |
| 54 | case PARTITION_PRESERVED: |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 55 | dst = priv->paths.prsv_loc; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 56 | break; |
| 57 | |
| 58 | case PARTITION_READONLY: |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 59 | dst = priv->paths.ro_loc; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 60 | break; |
| 61 | |
| 62 | default: |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 63 | dst = priv->paths.rw_loc; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 64 | } |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 65 | dst /= partition.data.name; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 66 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 67 | if (fs::exists(dst)) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 68 | { |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 69 | return dst; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 70 | } |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 71 | |
| 72 | if (flags == O_RDONLY) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 73 | { |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 74 | dst = fs::path(priv->paths.ro_loc) / partition.data.name; |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 75 | assert(fs::exists(dst)); |
| 76 | return dst; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 77 | } |
| 78 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 79 | assert(flags == O_RDWR); |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 80 | auto src = fs::path(priv->paths.ro_loc) / partition.data.name; |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 81 | assert(fs::exists(src)); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 82 | |
Andrew Jeffery | e8a79ff | 2018-02-27 10:56:32 +1030 | [diff] [blame] | 83 | 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] | 84 | partition.data.name, dst.c_str(), src.c_str()); |
Andrew Jeffery | e8a79ff | 2018-02-27 10:56:32 +1030 | [diff] [blame] | 85 | |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 86 | dst = priv->paths.rw_loc; |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 87 | if (partition.data.user.data[1] & PARTITION_PRESERVED) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 88 | { |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 89 | dst = priv->paths.prsv_loc; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 90 | } |
| 91 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 92 | dst /= partition.data.name; |
| 93 | fs::copy_file(src, dst); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 94 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 95 | return dst; |
| 96 | } |
| 97 | |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 98 | size_t Request::clamp(size_t len) |
| 99 | { |
| 100 | size_t maxAccess = offset + len; |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 101 | size_t partSize = partition.data.size << backend->block_size_shift; |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 102 | return std::min(maxAccess, partSize) - offset; |
| 103 | } |
| 104 | |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 105 | void Request::resize(const fs::path& path, size_t len) |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 106 | { |
| 107 | size_t maxAccess = offset + len; |
| 108 | size_t fileSize = fs::file_size(path); |
| 109 | if (maxAccess < fileSize) |
| 110 | { |
| 111 | return; |
| 112 | } |
| 113 | MSG_DBG("Resizing %s to %zu bytes\n", path.c_str(), maxAccess); |
| 114 | int rc = truncate(path.c_str(), maxAccess); |
| 115 | if (rc == -1) |
| 116 | { |
| 117 | MSG_ERR("Failed to resize %s: %d\n", path.c_str(), errno); |
| 118 | throw std::system_error(errno, std::system_category()); |
| 119 | } |
| 120 | } |
| 121 | |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 122 | size_t Request::fulfil(const fs::path& path, int flags, void* buf, size_t len) |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 123 | { |
| 124 | if (!(flags == O_RDONLY || flags == O_RDWR)) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 125 | { |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 126 | std::stringstream err; |
| 127 | err << "Require O_RDONLY (0x" << std::hex << O_RDONLY << " or O_RDWR " |
| 128 | << std::hex << O_RDWR << " for flags, got: 0x" << std::hex << flags; |
| 129 | throw std::invalid_argument(err.str()); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 130 | } |
| 131 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 132 | int fd = ::open(path.c_str(), flags); |
| 133 | if (fd == -1) |
| 134 | { |
Andrew Jeffery | 974507e | 2018-04-30 15:33:55 +0930 | [diff] [blame] | 135 | MSG_ERR("Failed to open backing file at '%s': %d\n", path.c_str(), |
| 136 | errno); |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 137 | throw std::system_error(errno, std::system_category()); |
| 138 | } |
| 139 | |
Andrew Jeffery | cc65061 | 2018-08-30 12:17:59 +0930 | [diff] [blame] | 140 | if (flags == O_RDONLY) |
| 141 | { |
| 142 | MSG_INFO("Fulfilling read request against %s at offset 0x%zx into %p " |
| 143 | "for %zu\n", |
| 144 | path.c_str(), offset, buf, len); |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | MSG_INFO("Fulfilling write request against %s at offset 0x%zx from %p " |
| 149 | "for %zu\n", |
| 150 | path.c_str(), offset, buf, len); |
| 151 | } |
| 152 | |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 153 | size_t fileSize = fs::file_size(path); |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 154 | int mprot = PROT_READ | ((flags == O_RDWR) ? PROT_WRITE : 0); |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 155 | auto map = mmap(NULL, fileSize, mprot, MAP_SHARED, fd, 0); |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 156 | if (map == MAP_FAILED) |
| 157 | { |
| 158 | close(fd); |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 159 | MSG_ERR("Failed to map backing file '%s' for %zd bytes: %d\n", |
| 160 | path.c_str(), fileSize, errno); |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 161 | throw std::system_error(errno, std::system_category()); |
| 162 | } |
| 163 | |
| 164 | // copy to the reserved memory area |
| 165 | if (flags == O_RDONLY) |
| 166 | { |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 167 | memset(buf, 0xff, len); |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 168 | memcpy(buf, (char*)map + offset, std::min(len, fileSize)); |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 169 | } |
| 170 | else |
| 171 | { |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 172 | memcpy((char*)map + offset, buf, len); |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 173 | backend_set_bytemap(backend, base + offset, len, FLASH_DIRTY); |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 174 | } |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 175 | munmap(map, fileSize); |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 176 | close(fd); |
| 177 | |
| 178 | return len; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 179 | } |
| 180 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 181 | } // namespace virtual_pnor |
| 182 | } // namespace openpower |