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; |
| 32 | namespace vpnor = openpower::virtual_pnor; |
| 33 | |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 34 | /** @brief unique_ptr functor to release a char* reference. */ |
| 35 | struct StringDeleter |
| 36 | { |
| 37 | void operator()(char* ptr) const |
| 38 | { |
| 39 | free(ptr); |
| 40 | } |
| 41 | }; |
| 42 | using StringPtr = std::unique_ptr<char, StringDeleter>; |
| 43 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 44 | int init_flash_dev(struct mbox_context* context) |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 45 | { |
| 46 | StringPtr filename(get_dev_mtd()); |
| 47 | int fd = 0; |
| 48 | int rc = 0; |
| 49 | |
| 50 | if (!filename) |
| 51 | { |
| 52 | MSG_ERR("Couldn't find the flash /dev/mtd partition\n"); |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | MSG_DBG("Opening %s\n", filename.get()); |
| 57 | |
| 58 | fd = open(filename.get(), O_RDWR); |
| 59 | if (fd < 0) |
| 60 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 61 | MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", filename.get(), |
| 62 | strerror(errno)); |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 63 | return -errno; |
| 64 | } |
| 65 | |
| 66 | // Read the Flash Info |
| 67 | if (ioctl(fd, MEMGETINFO, &context->mtd_info) == -1) |
| 68 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 69 | MSG_ERR("Couldn't get information about MTD: %s\n", strerror(errno)); |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 70 | close(fd); |
| 71 | return -errno; |
| 72 | } |
| 73 | |
| 74 | if (context->flash_size == 0) |
| 75 | { |
| 76 | // See comment in mboxd_flash_physical.c on why |
| 77 | // this is needed. |
| 78 | context->flash_size = context->mtd_info.size; |
| 79 | } |
| 80 | |
| 81 | // Hostboot requires a 4K block-size to be used in the FFS flash structure |
| 82 | context->mtd_info.erasesize = 4096; |
| 83 | context->erase_size_shift = log_2(context->mtd_info.erasesize); |
| 84 | context->flash_bmap = NULL; |
| 85 | context->fds[MTD_FD].fd = -1; |
| 86 | |
| 87 | close(fd); |
| 88 | return rc; |
| 89 | } |
| 90 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 91 | void free_flash_dev(struct mbox_context* context) |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 92 | { |
| 93 | // No-op |
| 94 | } |
| 95 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 96 | int set_flash_bytemap(struct mbox_context* context, uint32_t offset, |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 97 | uint32_t count, uint8_t val) |
| 98 | { |
| 99 | // No-op |
| 100 | return 0; |
| 101 | } |
| 102 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 103 | 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] | 104 | { |
| 105 | // No-op |
| 106 | return 0; |
| 107 | } |
| 108 | |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 109 | /* |
| 110 | * copy_flash() - Copy data from the virtual pnor into a provided buffer |
| 111 | * @context: The mbox context pointer |
| 112 | * @offset: The pnor offset to copy from (bytes) |
| 113 | * @mem: The buffer to copy into (must be of atleast 'size' bytes) |
| 114 | * @size: The number of bytes to copy |
Deepak Kodihalli | 7ee307c | 2017-07-12 03:41:08 -0500 | [diff] [blame] | 115 | * Return: Number of bytes copied on success, otherwise negative error |
| 116 | * code. copy_flash will copy at most 'size' bytes, but it may |
| 117 | * copy less. |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 118 | */ |
Deepak Kodihalli | 7ee307c | 2017-07-12 03:41:08 -0500 | [diff] [blame] | 119 | int64_t copy_flash(struct mbox_context* context, uint32_t offset, void* mem, |
| 120 | uint32_t size) |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 121 | { |
Ratan Gupta | 6a98e18 | 2017-06-06 15:04:23 +0530 | [diff] [blame] | 122 | using namespace phosphor::logging; |
| 123 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 124 | using namespace std::string_literals; |
| 125 | |
Deepak Kodihalli | 7ee307c | 2017-07-12 03:41:08 -0500 | [diff] [blame] | 126 | int rc = size; |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 127 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 128 | MSG_DBG("Copy virtual pnor to %p for size 0x%.8x from offset 0x%.8x\n", mem, |
| 129 | size, offset); |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 130 | |
| 131 | /* The virtual PNOR partition table starts at offset 0 in the virtual |
| 132 | * pnor image. Check if host asked for an offset that lies within the |
| 133 | * partition table. |
| 134 | */ |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 135 | try |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 136 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 137 | size_t sz = vpnor_get_partition_table_size(context) |
| 138 | << context->block_size_shift; |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 139 | if (offset < sz) |
| 140 | { |
| 141 | const struct pnor_partition_table* table = |
| 142 | vpnor_get_partition_table(context); |
Deepak Kodihalli | 7ee307c | 2017-07-12 03:41:08 -0500 | [diff] [blame] | 143 | rc = std::min(sz - offset, static_cast<size_t>(size)); |
| 144 | memcpy(mem, ((uint8_t*)table) + offset, rc); |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 145 | } |
| 146 | else |
| 147 | { |
Ratan Gupta | 6a98e18 | 2017-06-06 15:04:23 +0530 | [diff] [blame] | 148 | openpower::virtual_pnor::RORequest roRequest; |
| 149 | auto partitionInfo = roRequest.getPartitionInfo(context, offset); |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 150 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 151 | auto mapped_mem = mmap(NULL, partitionInfo->data.actual, PROT_READ, |
| 152 | MAP_PRIVATE, roRequest.fd(), 0); |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 153 | |
Adriana Kobylak | 0a2cc95 | 2017-10-12 11:13:06 -0500 | [diff] [blame] | 154 | if (mapped_mem == MAP_FAILED) |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 155 | { |
Ratan Gupta | 6a98e18 | 2017-06-06 15:04:23 +0530 | [diff] [blame] | 156 | MSG_ERR("Failed to map partition=%s:Error=%s\n", |
| 157 | partitionInfo->data.name, strerror(errno)); |
| 158 | |
| 159 | elog<InternalFailure>(); |
| 160 | } |
| 161 | |
| 162 | // if the asked offset + no of bytes to read is greater |
| 163 | // then size of the partition file then throw error. |
| 164 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 165 | uint32_t baseOffset = partitionInfo->data.base |
| 166 | << context->block_size_shift; |
| 167 | // copy to the reserved memory area |
Ratan Gupta | 6a98e18 | 2017-06-06 15:04:23 +0530 | [diff] [blame] | 168 | auto diffOffset = offset - baseOffset; |
Deepak Kodihalli | b100fb8 | 2017-07-12 04:44:41 -0500 | [diff] [blame] | 169 | rc = std::min(partitionInfo->data.actual - diffOffset, size); |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 170 | memcpy(mem, (char*)mapped_mem + diffOffset, rc); |
Ratan Gupta | 6a98e18 | 2017-06-06 15:04:23 +0530 | [diff] [blame] | 171 | munmap(mapped_mem, partitionInfo->data.actual); |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 172 | } |
| 173 | } |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame^] | 174 | catch (vpnor::UnmappedOffset& e) |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 175 | { |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame^] | 176 | /* |
| 177 | * Hooo boy. Pretend that this is valid flash so we don't have |
| 178 | * discontiguous regions presented to the host. Instead, fill a window |
| 179 | * with 0xff so the 'flash' looks erased. Writes to such regions are |
| 180 | * dropped on the floor, see the implementation of write_flash() below. |
| 181 | */ |
| 182 | MSG_INFO("Host requested unmapped region of %" PRId32 |
| 183 | " bytes at offset 0x%" PRIx32 "\n", |
| 184 | size, offset); |
| 185 | uint32_t span = e.next - e.base; |
| 186 | rc = std::min(size, span); |
| 187 | memset(mem, 0xff, rc); |
| 188 | } |
| 189 | catch (std::exception& e) |
| 190 | { |
| 191 | MSG_ERR("%s\n", e.what()); |
| 192 | phosphor::logging::commit<err::InternalFailure>(); |
Deepak Kodihalli | f9abed0 | 2017-07-17 06:23:08 -0500 | [diff] [blame] | 193 | rc = -MBOX_R_SYSTEM_ERROR; |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 194 | } |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 195 | return rc; |
| 196 | } |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | * write_flash() - Write to the virtual pnor from a provided buffer |
| 200 | * @context: The mbox context pointer |
| 201 | * @offset: The flash offset to write to (bytes) |
| 202 | * @buf: The buffer to write from (must be of atleast size) |
| 203 | * @size: The number of bytes to write |
| 204 | * |
| 205 | * Return: 0 on success otherwise negative error code |
| 206 | */ |
| 207 | |
| 208 | int write_flash(struct mbox_context* context, uint32_t offset, void* buf, |
| 209 | uint32_t count) |
| 210 | { |
| 211 | using namespace phosphor::logging; |
| 212 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 213 | using namespace std::string_literals; |
| 214 | |
| 215 | int rc = 0; |
| 216 | MSG_DBG("Write flash @ 0x%.8x for 0x%.8x from %p\n", offset, count, buf); |
| 217 | try |
| 218 | { |
| 219 | openpower::virtual_pnor::RWRequest rwRequest; |
| 220 | auto partitionInfo = rwRequest.getPartitionInfo(context, offset); |
| 221 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 222 | auto mapped_mem = |
| 223 | mmap(NULL, partitionInfo->data.actual, PROT_READ | PROT_WRITE, |
| 224 | MAP_SHARED, rwRequest.fd(), 0); |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 225 | if (mapped_mem == MAP_FAILED) |
| 226 | { |
| 227 | MSG_ERR("Failed to map partition=%s:Error=%s\n", |
| 228 | partitionInfo->data.name, strerror(errno)); |
| 229 | |
| 230 | elog<InternalFailure>(); |
| 231 | } |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 232 | // copy to the mapped memory. |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 233 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 234 | uint32_t baseOffset = partitionInfo->data.base |
| 235 | << context->block_size_shift; |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 236 | |
| 237 | // if the asked offset + no of bytes to write is greater |
| 238 | // then size of the partition file then throw error. |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame^] | 239 | // |
| 240 | // FIXME: Don't use .actual, use (.size << ctx->block_size_shift), |
| 241 | // otherwise we can't grow the size of the data to fill the partition |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 242 | if ((offset + count) > (baseOffset + partitionInfo->data.actual)) |
| 243 | { |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 244 | munmap(mapped_mem, partitionInfo->data.actual); |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame^] | 245 | std::stringstream err; |
| 246 | err << "Write extends beyond the partition length " << std::hex |
| 247 | << partitionInfo->data.actual; |
| 248 | throw vpnor::OutOfBoundsOffset(err.str()); |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | auto diffOffset = offset - baseOffset; |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 252 | memcpy((char*)mapped_mem + diffOffset, buf, count); |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 253 | munmap(mapped_mem, partitionInfo->data.actual); |
| 254 | |
| 255 | set_flash_bytemap(context, offset, count, FLASH_DIRTY); |
| 256 | } |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame^] | 257 | catch (vpnor::UnmappedOffset& e) |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 258 | { |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame^] | 259 | /* Paper over the fact that the write isn't persistent */ |
| 260 | MSG_INFO("Dropping %d bytes host wrote to unmapped offset 0x%" PRIx32 |
| 261 | "\n", |
| 262 | count, offset); |
| 263 | return 0; |
| 264 | } |
| 265 | catch (const vpnor::OutOfBoundsOffset& e) |
| 266 | { |
| 267 | MSG_ERR("%s\n", e.what()); |
| 268 | return -MBOX_R_PARAM_ERROR; |
| 269 | } |
| 270 | catch (const std::exception& e) |
| 271 | { |
| 272 | MSG_ERR("%s\n", e.what()); |
| 273 | phosphor::logging::commit<err::InternalFailure>(); |
| 274 | return -MBOX_R_SYSTEM_ERROR; |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 275 | } |
| 276 | return rc; |
| 277 | } |