Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Mailbox Daemon Window Helpers |
| 3 | * |
| 4 | * Copyright 2017 IBM |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | */ |
| 19 | |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 20 | #include <fcntl.h> |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <syslog.h> |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 24 | #include <sys/mman.h> |
| 25 | #include <unistd.h> |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 26 | #include <sys/ioctl.h> |
Deepak Kodihalli | 7ee307c | 2017-07-12 03:41:08 -0500 | [diff] [blame] | 27 | #include <algorithm> |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 28 | |
| 29 | extern "C" { |
| 30 | #include "common.h" |
| 31 | } |
| 32 | |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 33 | #include "config.h" |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 34 | #include "mboxd_flash.h" |
| 35 | #include "mboxd_pnor_partition_table.h" |
Ratan Gupta | 6a98e18 | 2017-06-06 15:04:23 +0530 | [diff] [blame] | 36 | #include "pnor_partition.hpp" |
| 37 | #include "xyz/openbmc_project/Common/error.hpp" |
| 38 | #include <phosphor-logging/log.hpp> |
| 39 | #include <phosphor-logging/elog-errors.hpp> |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 40 | |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 41 | #include <memory> |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 42 | #include <string> |
| 43 | #include <exception> |
| 44 | #include <stdexcept> |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 45 | |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 46 | /** @brief unique_ptr functor to release a char* reference. */ |
| 47 | struct StringDeleter |
| 48 | { |
| 49 | void operator()(char* ptr) const |
| 50 | { |
| 51 | free(ptr); |
| 52 | } |
| 53 | }; |
| 54 | using StringPtr = std::unique_ptr<char, StringDeleter>; |
| 55 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 56 | int init_flash_dev(struct mbox_context* context) |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 57 | { |
| 58 | StringPtr filename(get_dev_mtd()); |
| 59 | int fd = 0; |
| 60 | int rc = 0; |
| 61 | |
| 62 | if (!filename) |
| 63 | { |
| 64 | MSG_ERR("Couldn't find the flash /dev/mtd partition\n"); |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | MSG_DBG("Opening %s\n", filename.get()); |
| 69 | |
| 70 | fd = open(filename.get(), O_RDWR); |
| 71 | if (fd < 0) |
| 72 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 73 | MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", filename.get(), |
| 74 | strerror(errno)); |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 75 | return -errno; |
| 76 | } |
| 77 | |
| 78 | // Read the Flash Info |
| 79 | if (ioctl(fd, MEMGETINFO, &context->mtd_info) == -1) |
| 80 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 81 | MSG_ERR("Couldn't get information about MTD: %s\n", strerror(errno)); |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 82 | close(fd); |
| 83 | return -errno; |
| 84 | } |
| 85 | |
| 86 | if (context->flash_size == 0) |
| 87 | { |
| 88 | // See comment in mboxd_flash_physical.c on why |
| 89 | // this is needed. |
| 90 | context->flash_size = context->mtd_info.size; |
| 91 | } |
| 92 | |
| 93 | // Hostboot requires a 4K block-size to be used in the FFS flash structure |
| 94 | context->mtd_info.erasesize = 4096; |
| 95 | context->erase_size_shift = log_2(context->mtd_info.erasesize); |
| 96 | context->flash_bmap = NULL; |
| 97 | context->fds[MTD_FD].fd = -1; |
| 98 | |
| 99 | close(fd); |
| 100 | return rc; |
| 101 | } |
| 102 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 103 | void free_flash_dev(struct mbox_context* context) |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 104 | { |
| 105 | // No-op |
| 106 | } |
| 107 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 108 | int set_flash_bytemap(struct mbox_context* context, uint32_t offset, |
Deepak Kodihalli | abd52a7 | 2017-07-13 12:04:06 -0500 | [diff] [blame] | 109 | uint32_t count, uint8_t val) |
| 110 | { |
| 111 | // No-op |
| 112 | return 0; |
| 113 | } |
| 114 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 115 | 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] | 116 | { |
| 117 | // No-op |
| 118 | return 0; |
| 119 | } |
| 120 | |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 121 | /* |
| 122 | * copy_flash() - Copy data from the virtual pnor into a provided buffer |
| 123 | * @context: The mbox context pointer |
| 124 | * @offset: The pnor offset to copy from (bytes) |
| 125 | * @mem: The buffer to copy into (must be of atleast 'size' bytes) |
| 126 | * @size: The number of bytes to copy |
Deepak Kodihalli | 7ee307c | 2017-07-12 03:41:08 -0500 | [diff] [blame] | 127 | * Return: Number of bytes copied on success, otherwise negative error |
| 128 | * code. copy_flash will copy at most 'size' bytes, but it may |
| 129 | * copy less. |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 130 | */ |
Deepak Kodihalli | 7ee307c | 2017-07-12 03:41:08 -0500 | [diff] [blame] | 131 | int64_t copy_flash(struct mbox_context* context, uint32_t offset, void* mem, |
| 132 | uint32_t size) |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 133 | { |
Ratan Gupta | 6a98e18 | 2017-06-06 15:04:23 +0530 | [diff] [blame] | 134 | using namespace phosphor::logging; |
| 135 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 136 | using namespace std::string_literals; |
| 137 | |
Deepak Kodihalli | 7ee307c | 2017-07-12 03:41:08 -0500 | [diff] [blame] | 138 | int rc = size; |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 139 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 140 | MSG_DBG("Copy virtual pnor to %p for size 0x%.8x from offset 0x%.8x\n", mem, |
| 141 | size, offset); |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 142 | |
| 143 | /* The virtual PNOR partition table starts at offset 0 in the virtual |
| 144 | * pnor image. Check if host asked for an offset that lies within the |
| 145 | * partition table. |
| 146 | */ |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 147 | try |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 148 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 149 | size_t sz = vpnor_get_partition_table_size(context) |
| 150 | << context->block_size_shift; |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 151 | if (offset < sz) |
| 152 | { |
| 153 | const struct pnor_partition_table* table = |
| 154 | vpnor_get_partition_table(context); |
Deepak Kodihalli | 7ee307c | 2017-07-12 03:41:08 -0500 | [diff] [blame] | 155 | rc = std::min(sz - offset, static_cast<size_t>(size)); |
| 156 | memcpy(mem, ((uint8_t*)table) + offset, rc); |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 157 | } |
| 158 | else |
| 159 | { |
Ratan Gupta | 6a98e18 | 2017-06-06 15:04:23 +0530 | [diff] [blame] | 160 | openpower::virtual_pnor::RORequest roRequest; |
| 161 | auto partitionInfo = roRequest.getPartitionInfo(context, offset); |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 162 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 163 | auto mapped_mem = mmap(NULL, partitionInfo->data.actual, PROT_READ, |
| 164 | MAP_PRIVATE, roRequest.fd(), 0); |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 165 | |
Adriana Kobylak | 0a2cc95 | 2017-10-12 11:13:06 -0500 | [diff] [blame] | 166 | if (mapped_mem == MAP_FAILED) |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 167 | { |
Ratan Gupta | 6a98e18 | 2017-06-06 15:04:23 +0530 | [diff] [blame] | 168 | MSG_ERR("Failed to map partition=%s:Error=%s\n", |
| 169 | partitionInfo->data.name, strerror(errno)); |
| 170 | |
| 171 | elog<InternalFailure>(); |
| 172 | } |
| 173 | |
| 174 | // if the asked offset + no of bytes to read is greater |
| 175 | // then size of the partition file then throw error. |
| 176 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 177 | uint32_t baseOffset = partitionInfo->data.base |
| 178 | << context->block_size_shift; |
| 179 | // copy to the reserved memory area |
Ratan Gupta | 6a98e18 | 2017-06-06 15:04:23 +0530 | [diff] [blame] | 180 | auto diffOffset = offset - baseOffset; |
Deepak Kodihalli | b100fb8 | 2017-07-12 04:44:41 -0500 | [diff] [blame] | 181 | rc = std::min(partitionInfo->data.actual - diffOffset, size); |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 182 | memcpy(mem, (char*)mapped_mem + diffOffset, rc); |
Ratan Gupta | 6a98e18 | 2017-06-06 15:04:23 +0530 | [diff] [blame] | 183 | munmap(mapped_mem, partitionInfo->data.actual); |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 184 | } |
| 185 | } |
Ratan Gupta | 6a98e18 | 2017-06-06 15:04:23 +0530 | [diff] [blame] | 186 | catch (InternalFailure& e) |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 187 | { |
Ratan Gupta | 6a98e18 | 2017-06-06 15:04:23 +0530 | [diff] [blame] | 188 | commit<InternalFailure>(); |
Deepak Kodihalli | f9abed0 | 2017-07-17 06:23:08 -0500 | [diff] [blame] | 189 | rc = -MBOX_R_SYSTEM_ERROR; |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 190 | } |
Deepak Kodihalli | 6c2fa90 | 2017-05-01 06:36:02 -0500 | [diff] [blame] | 191 | return rc; |
| 192 | } |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * write_flash() - Write to the virtual pnor from a provided buffer |
| 196 | * @context: The mbox context pointer |
| 197 | * @offset: The flash offset to write to (bytes) |
| 198 | * @buf: The buffer to write from (must be of atleast size) |
| 199 | * @size: The number of bytes to write |
| 200 | * |
| 201 | * Return: 0 on success otherwise negative error code |
| 202 | */ |
| 203 | |
| 204 | int write_flash(struct mbox_context* context, uint32_t offset, void* buf, |
| 205 | uint32_t count) |
| 206 | { |
| 207 | using namespace phosphor::logging; |
| 208 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 209 | using namespace std::string_literals; |
| 210 | |
| 211 | int rc = 0; |
| 212 | MSG_DBG("Write flash @ 0x%.8x for 0x%.8x from %p\n", offset, count, buf); |
| 213 | try |
| 214 | { |
| 215 | openpower::virtual_pnor::RWRequest rwRequest; |
| 216 | auto partitionInfo = rwRequest.getPartitionInfo(context, offset); |
| 217 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 218 | auto mapped_mem = |
| 219 | mmap(NULL, partitionInfo->data.actual, PROT_READ | PROT_WRITE, |
| 220 | MAP_SHARED, rwRequest.fd(), 0); |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 221 | if (mapped_mem == MAP_FAILED) |
| 222 | { |
| 223 | MSG_ERR("Failed to map partition=%s:Error=%s\n", |
| 224 | partitionInfo->data.name, strerror(errno)); |
| 225 | |
| 226 | elog<InternalFailure>(); |
| 227 | } |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 228 | // copy to the mapped memory. |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 229 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 230 | uint32_t baseOffset = partitionInfo->data.base |
| 231 | << context->block_size_shift; |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 232 | |
| 233 | // if the asked offset + no of bytes to write is greater |
| 234 | // then size of the partition file then throw error. |
| 235 | if ((offset + count) > (baseOffset + partitionInfo->data.actual)) |
| 236 | { |
| 237 | MSG_ERR("Offset is beyond the partition file length[0x%.8x]\n", |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 238 | partitionInfo->data.actual); |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 239 | munmap(mapped_mem, partitionInfo->data.actual); |
| 240 | elog<InternalFailure>(); |
| 241 | } |
| 242 | |
| 243 | auto diffOffset = offset - baseOffset; |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 244 | memcpy((char*)mapped_mem + diffOffset, buf, count); |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 245 | munmap(mapped_mem, partitionInfo->data.actual); |
| 246 | |
| 247 | set_flash_bytemap(context, offset, count, FLASH_DIRTY); |
| 248 | } |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 249 | catch (InternalFailure& e) |
Ratan Gupta | dc50ce5 | 2017-05-31 15:47:55 +0530 | [diff] [blame] | 250 | { |
| 251 | rc = -1; |
| 252 | } |
| 253 | return rc; |
| 254 | } |