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. |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 3 | |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 4 | #include <fcntl.h> |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 5 | #include <stdint.h> |
| 6 | #include <stdlib.h> |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 7 | #include <sys/ioctl.h> |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame^] | 8 | #include <sys/mman.h> |
| 9 | #include <syslog.h> |
| 10 | #include <unistd.h> |
| 11 | |
Deepak Kodihalli | 7ee307c | 2017-07-12 03:41:08 -0500 | [diff] [blame] | 12 | #include <algorithm> |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 13 | |
| 14 | extern "C" { |
| 15 | #include "common.h" |
Andrew Jeffery | 71eaa73 | 2018-08-08 16:44:24 +0930 | [diff] [blame] | 16 | #include "flash.h" |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 17 | } |
| 18 | |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 19 | #include "config.h" |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame^] | 20 | |
Ratan Gupta | 6a98e18 | 2017-06-06 15:04:23 +0530 | [diff] [blame] | 21 | #include "pnor_partition.hpp" |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame] | 22 | #include "pnor_partition_table.hpp" |
Ratan Gupta | 6a98e18 | 2017-06-06 15:04:23 +0530 | [diff] [blame] | 23 | #include "xyz/openbmc_project/Common/error.hpp" |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 24 | |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 25 | #include <exception> |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame^] | 26 | #include <memory> |
| 27 | #include <phosphor-logging/elog-errors.hpp> |
| 28 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 29 | #include <stdexcept> |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame^] | 30 | #include <string> |
| 31 | |
| 32 | #include "mboxd_pnor_partition_table.h" |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 33 | |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame] | 34 | namespace err = sdbusplus::xyz::openbmc_project::Common::Error; |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 35 | namespace fs = std::experimental::filesystem; |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame] | 36 | namespace vpnor = openpower::virtual_pnor; |
| 37 | |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 38 | /** @brief unique_ptr functor to release a char* reference. */ |
| 39 | struct StringDeleter |
| 40 | { |
| 41 | void operator()(char* ptr) const |
| 42 | { |
| 43 | free(ptr); |
| 44 | } |
| 45 | }; |
| 46 | using StringPtr = std::unique_ptr<char, StringDeleter>; |
| 47 | |
Andrew Jeffery | d6b09bc | 2018-08-08 16:47:54 +0930 | [diff] [blame] | 48 | int flash_dev_init(struct mbox_context* context) |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 49 | { |
| 50 | StringPtr filename(get_dev_mtd()); |
| 51 | int fd = 0; |
| 52 | int rc = 0; |
| 53 | |
| 54 | if (!filename) |
| 55 | { |
| 56 | MSG_ERR("Couldn't find the flash /dev/mtd partition\n"); |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | MSG_DBG("Opening %s\n", filename.get()); |
| 61 | |
| 62 | fd = open(filename.get(), O_RDWR); |
| 63 | if (fd < 0) |
| 64 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 65 | MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", filename.get(), |
| 66 | strerror(errno)); |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 67 | return -errno; |
| 68 | } |
| 69 | |
| 70 | // Read the Flash Info |
| 71 | if (ioctl(fd, MEMGETINFO, &context->mtd_info) == -1) |
| 72 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 73 | MSG_ERR("Couldn't get information about MTD: %s\n", strerror(errno)); |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 74 | close(fd); |
| 75 | return -errno; |
| 76 | } |
| 77 | |
| 78 | if (context->flash_size == 0) |
| 79 | { |
| 80 | // See comment in mboxd_flash_physical.c on why |
| 81 | // this is needed. |
| 82 | context->flash_size = context->mtd_info.size; |
| 83 | } |
| 84 | |
| 85 | // Hostboot requires a 4K block-size to be used in the FFS flash structure |
| 86 | context->mtd_info.erasesize = 4096; |
| 87 | context->erase_size_shift = log_2(context->mtd_info.erasesize); |
| 88 | context->flash_bmap = NULL; |
| 89 | context->fds[MTD_FD].fd = -1; |
| 90 | |
| 91 | close(fd); |
| 92 | return rc; |
| 93 | } |
| 94 | |
Andrew Jeffery | dec6ca6 | 2018-08-08 16:49:43 +0930 | [diff] [blame] | 95 | void flash_dev_free(struct mbox_context* context) |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 96 | { |
| 97 | // No-op |
| 98 | } |
| 99 | |
Andrew Jeffery | f953f79 | 2018-08-08 16:56:03 +0930 | [diff] [blame] | 100 | int flash_set_bytemap(struct mbox_context* context, uint32_t offset, |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 101 | uint32_t count, uint8_t val) |
| 102 | { |
| 103 | // No-op |
| 104 | return 0; |
| 105 | } |
| 106 | |
Andrew Jeffery | 4f5d29c | 2018-08-08 16:58:04 +0930 | [diff] [blame] | 107 | int flash_erase(struct mbox_context* context, uint32_t offset, uint32_t count) |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 108 | { |
| 109 | // No-op |
| 110 | return 0; |
| 111 | } |
| 112 | |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 113 | /* |
Andrew Jeffery | e0cdd3e | 2018-08-08 16:51:44 +0930 | [diff] [blame] | 114 | * flash_copy() - Copy data from the virtual pnor into a provided buffer |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 115 | * @context: The mbox context pointer |
| 116 | * @offset: The pnor offset to copy from (bytes) |
| 117 | * @mem: The buffer to copy into (must be of atleast 'size' bytes) |
| 118 | * @size: The number of bytes to copy |
Deepak Kodihalli | 7ee307c | 2017-07-12 03:41:08 -0500 | [diff] [blame] | 119 | * Return: Number of bytes copied on success, otherwise negative error |
Andrew Jeffery | e0cdd3e | 2018-08-08 16:51:44 +0930 | [diff] [blame] | 120 | * code. flash_copy will copy at most 'size' bytes, but it may |
Deepak Kodihalli | 7ee307c | 2017-07-12 03:41:08 -0500 | [diff] [blame] | 121 | * copy less. |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 122 | */ |
Andrew Jeffery | e0cdd3e | 2018-08-08 16:51:44 +0930 | [diff] [blame] | 123 | int64_t flash_copy(struct mbox_context* context, uint32_t offset, void* mem, |
Deepak Kodihalli | 7ee307c | 2017-07-12 03:41:08 -0500 | [diff] [blame] | 124 | uint32_t size) |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 125 | { |
Andrew Jeffery | fc62158 | 2018-02-28 22:20:19 +1030 | [diff] [blame] | 126 | vpnor::partition::Table* table; |
Deepak Kodihalli | 7ee307c | 2017-07-12 03:41:08 -0500 | [diff] [blame] | 127 | int rc = size; |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 128 | |
Andrew Jeffery | fc62158 | 2018-02-28 22:20:19 +1030 | [diff] [blame] | 129 | if (!(context && context->vpnor && context->vpnor->table)) |
| 130 | { |
| 131 | MSG_ERR("Trying to copy data with uninitialised context!\n"); |
Andrew Jeffery | 8eab215 | 2018-08-09 23:47:29 +0930 | [diff] [blame] | 132 | return -EINVAL; |
Andrew Jeffery | fc62158 | 2018-02-28 22:20:19 +1030 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | table = context->vpnor->table; |
| 136 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 137 | MSG_DBG("Copy virtual pnor to %p for size 0x%.8x from offset 0x%.8x\n", mem, |
| 138 | size, offset); |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 139 | |
| 140 | /* The virtual PNOR partition table starts at offset 0 in the virtual |
| 141 | * pnor image. Check if host asked for an offset that lies within the |
| 142 | * partition table. |
| 143 | */ |
Andrew Jeffery | fc62158 | 2018-02-28 22:20:19 +1030 | [diff] [blame] | 144 | size_t sz = table->size(); |
| 145 | if (offset < sz) |
| 146 | { |
| 147 | const pnor_partition_table& toc = table->getHostTable(); |
| 148 | rc = std::min(sz - offset, static_cast<size_t>(size)); |
| 149 | memcpy(mem, ((uint8_t*)&toc) + offset, rc); |
| 150 | return rc; |
| 151 | } |
| 152 | |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 153 | try |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 154 | { |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 155 | vpnor::Request req(context, offset); |
| 156 | rc = req.read(mem, size); |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 157 | } |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame] | 158 | catch (vpnor::UnmappedOffset& e) |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 159 | { |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame] | 160 | /* |
| 161 | * Hooo boy. Pretend that this is valid flash so we don't have |
| 162 | * discontiguous regions presented to the host. Instead, fill a window |
| 163 | * with 0xff so the 'flash' looks erased. Writes to such regions are |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 164 | * dropped on the floor, see the implementation of flash_write() below. |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame] | 165 | */ |
| 166 | MSG_INFO("Host requested unmapped region of %" PRId32 |
| 167 | " bytes at offset 0x%" PRIx32 "\n", |
| 168 | size, offset); |
| 169 | uint32_t span = e.next - e.base; |
| 170 | rc = std::min(size, span); |
| 171 | memset(mem, 0xff, rc); |
| 172 | } |
| 173 | catch (std::exception& e) |
| 174 | { |
| 175 | MSG_ERR("%s\n", e.what()); |
| 176 | phosphor::logging::commit<err::InternalFailure>(); |
Andrew Jeffery | 8eab215 | 2018-08-09 23:47:29 +0930 | [diff] [blame] | 177 | rc = -EIO; |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 178 | } |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 179 | return rc; |
| 180 | } |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 181 | |
| 182 | /* |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 183 | * flash_write() - Write to the virtual pnor from a provided buffer |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 184 | * @context: The mbox context pointer |
| 185 | * @offset: The flash offset to write to (bytes) |
| 186 | * @buf: The buffer to write from (must be of atleast size) |
| 187 | * @size: The number of bytes to write |
| 188 | * |
| 189 | * Return: 0 on success otherwise negative error code |
| 190 | */ |
| 191 | |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 192 | int flash_write(struct mbox_context* context, uint32_t offset, void* buf, |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 193 | uint32_t count) |
| 194 | { |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 195 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 196 | if (!(context && context->vpnor && context->vpnor->table)) |
| 197 | { |
| 198 | MSG_ERR("Trying to write data with uninitialised context!\n"); |
Andrew Jeffery | 8eab215 | 2018-08-09 23:47:29 +0930 | [diff] [blame] | 199 | return -EINVAL; |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | vpnor::partition::Table* table = context->vpnor->table; |
| 203 | |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 204 | try |
| 205 | { |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 206 | const struct pnor_partition& part = table->partition(offset); |
| 207 | if (part.data.user.data[1] & PARTITION_READONLY) |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 208 | { |
Andrew Jeffery | 52a8319 | 2018-03-27 10:35:31 +1030 | [diff] [blame] | 209 | MSG_ERR("Unreachable: Host attempted to write to read-only " |
| 210 | "partition %s\n", |
| 211 | part.data.name); |
Andrew Jeffery | 8eab215 | 2018-08-09 23:47:29 +0930 | [diff] [blame] | 212 | return -EPERM; |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 213 | } |
| 214 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 215 | MSG_DBG("Write flash @ 0x%.8x for 0x%.8x from %p\n", offset, count, |
| 216 | buf); |
| 217 | vpnor::Request req(context, offset); |
| 218 | req.write(buf, count); |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 219 | } |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame] | 220 | catch (vpnor::UnmappedOffset& e) |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 221 | { |
Andrew Jeffery | 52a8319 | 2018-03-27 10:35:31 +1030 | [diff] [blame] | 222 | MSG_ERR("Unreachable: Host attempted to write %" PRIu32 |
| 223 | " bytes to unmapped offset 0x%" PRIx32 "\n", |
| 224 | count, offset); |
Andrew Jeffery | 8eab215 | 2018-08-09 23:47:29 +0930 | [diff] [blame] | 225 | return -EACCES; |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame] | 226 | } |
| 227 | catch (const vpnor::OutOfBoundsOffset& e) |
| 228 | { |
| 229 | MSG_ERR("%s\n", e.what()); |
Andrew Jeffery | 8eab215 | 2018-08-09 23:47:29 +0930 | [diff] [blame] | 230 | return -EINVAL; |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame] | 231 | } |
| 232 | catch (const std::exception& e) |
| 233 | { |
| 234 | MSG_ERR("%s\n", e.what()); |
| 235 | phosphor::logging::commit<err::InternalFailure>(); |
Andrew Jeffery | 8eab215 | 2018-08-09 23:47:29 +0930 | [diff] [blame] | 236 | return -EIO; |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 237 | } |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 238 | return 0; |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 239 | } |