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