blob: 42d4ee6976ac6ed377c0b04a4005a84a0081e716 [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -05003
Ratan Gupta8441a392017-05-05 21:42:53 +05304#include <fcntl.h>
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -05005#include <stdint.h>
6#include <stdlib.h>
7#include <syslog.h>
Ratan Gupta8441a392017-05-05 21:42:53 +05308#include <sys/mman.h>
9#include <unistd.h>
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050010#include <sys/ioctl.h>
Deepak Kodihalli7ee307c2017-07-12 03:41:08 -050011#include <algorithm>
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -050012
13extern "C" {
14#include "common.h"
15}
16
Ratan Gupta8441a392017-05-05 21:42:53 +053017#include "config.h"
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -050018#include "mboxd_flash.h"
19#include "mboxd_pnor_partition_table.h"
Ratan Gupta6a98e182017-06-06 15:04:23 +053020#include "pnor_partition.hpp"
Andrew Jefferyae1edb92018-02-28 23:16:48 +103021#include "pnor_partition_table.hpp"
Ratan Gupta6a98e182017-06-06 15:04:23 +053022#include "xyz/openbmc_project/Common/error.hpp"
23#include <phosphor-logging/log.hpp>
24#include <phosphor-logging/elog-errors.hpp>
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -050025
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050026#include <memory>
Ratan Gupta8441a392017-05-05 21:42:53 +053027#include <string>
28#include <exception>
29#include <stdexcept>
Ratan Gupta8441a392017-05-05 21:42:53 +053030
Andrew Jefferyae1edb92018-02-28 23:16:48 +103031namespace err = sdbusplus::xyz::openbmc_project::Common::Error;
32namespace vpnor = openpower::virtual_pnor;
33
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050034/** @brief unique_ptr functor to release a char* reference. */
35struct StringDeleter
36{
37 void operator()(char* ptr) const
38 {
39 free(ptr);
40 }
41};
42using StringPtr = std::unique_ptr<char, StringDeleter>;
43
Andrew Jefferyf34db312018-03-09 15:27:03 +103044int init_flash_dev(struct mbox_context* context)
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050045{
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 Jefferyf34db312018-03-09 15:27:03 +103061 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", filename.get(),
62 strerror(errno));
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050063 return -errno;
64 }
65
66 // Read the Flash Info
67 if (ioctl(fd, MEMGETINFO, &context->mtd_info) == -1)
68 {
Andrew Jefferyf34db312018-03-09 15:27:03 +103069 MSG_ERR("Couldn't get information about MTD: %s\n", strerror(errno));
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050070 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 Jefferyf34db312018-03-09 15:27:03 +103091void free_flash_dev(struct mbox_context* context)
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050092{
93 // No-op
94}
95
Andrew Jefferyf34db312018-03-09 15:27:03 +103096int set_flash_bytemap(struct mbox_context* context, uint32_t offset,
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050097 uint32_t count, uint8_t val)
98{
99 // No-op
100 return 0;
101}
102
Andrew Jefferyf34db312018-03-09 15:27:03 +1030103int erase_flash(struct mbox_context* context, uint32_t offset, uint32_t count)
Deepak Kodihalliabd52a72017-07-13 12:04:06 -0500104{
105 // No-op
106 return 0;
107}
108
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500109/*
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 Kodihalli7ee307c2017-07-12 03:41:08 -0500115 * 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 Kodihalli6c2fa902017-05-01 06:36:02 -0500118 */
Deepak Kodihalli7ee307c2017-07-12 03:41:08 -0500119int64_t copy_flash(struct mbox_context* context, uint32_t offset, void* mem,
120 uint32_t size)
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500121{
Ratan Gupta6a98e182017-06-06 15:04:23 +0530122 using namespace phosphor::logging;
123 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
124 using namespace std::string_literals;
125
Deepak Kodihalli7ee307c2017-07-12 03:41:08 -0500126 int rc = size;
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500127
Andrew Jefferyf34db312018-03-09 15:27:03 +1030128 MSG_DBG("Copy virtual pnor to %p for size 0x%.8x from offset 0x%.8x\n", mem,
129 size, offset);
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500130
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 Gupta8441a392017-05-05 21:42:53 +0530135 try
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500136 {
Andrew Jefferyf34db312018-03-09 15:27:03 +1030137 size_t sz = vpnor_get_partition_table_size(context)
138 << context->block_size_shift;
Ratan Gupta8441a392017-05-05 21:42:53 +0530139 if (offset < sz)
140 {
141 const struct pnor_partition_table* table =
142 vpnor_get_partition_table(context);
Deepak Kodihalli7ee307c2017-07-12 03:41:08 -0500143 rc = std::min(sz - offset, static_cast<size_t>(size));
144 memcpy(mem, ((uint8_t*)table) + offset, rc);
Ratan Gupta8441a392017-05-05 21:42:53 +0530145 }
146 else
147 {
Ratan Gupta6a98e182017-06-06 15:04:23 +0530148 openpower::virtual_pnor::RORequest roRequest;
149 auto partitionInfo = roRequest.getPartitionInfo(context, offset);
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500150
Andrew Jefferyf34db312018-03-09 15:27:03 +1030151 auto mapped_mem = mmap(NULL, partitionInfo->data.actual, PROT_READ,
152 MAP_PRIVATE, roRequest.fd(), 0);
Ratan Gupta8441a392017-05-05 21:42:53 +0530153
Adriana Kobylak0a2cc952017-10-12 11:13:06 -0500154 if (mapped_mem == MAP_FAILED)
Ratan Gupta8441a392017-05-05 21:42:53 +0530155 {
Ratan Gupta6a98e182017-06-06 15:04:23 +0530156 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 Jefferyf34db312018-03-09 15:27:03 +1030165 uint32_t baseOffset = partitionInfo->data.base
166 << context->block_size_shift;
167 // copy to the reserved memory area
Ratan Gupta6a98e182017-06-06 15:04:23 +0530168 auto diffOffset = offset - baseOffset;
Deepak Kodihallib100fb82017-07-12 04:44:41 -0500169 rc = std::min(partitionInfo->data.actual - diffOffset, size);
Andrew Jefferyf34db312018-03-09 15:27:03 +1030170 memcpy(mem, (char*)mapped_mem + diffOffset, rc);
Ratan Gupta6a98e182017-06-06 15:04:23 +0530171 munmap(mapped_mem, partitionInfo->data.actual);
Ratan Gupta8441a392017-05-05 21:42:53 +0530172 }
173 }
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030174 catch (vpnor::UnmappedOffset& e)
Ratan Gupta8441a392017-05-05 21:42:53 +0530175 {
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030176 /*
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 Kodihallif9abed02017-07-17 06:23:08 -0500193 rc = -MBOX_R_SYSTEM_ERROR;
Ratan Gupta8441a392017-05-05 21:42:53 +0530194 }
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500195 return rc;
196}
Ratan Guptadc50ce52017-05-31 15:47:55 +0530197
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
208int 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 Jefferyf34db312018-03-09 15:27:03 +1030222 auto mapped_mem =
223 mmap(NULL, partitionInfo->data.actual, PROT_READ | PROT_WRITE,
224 MAP_SHARED, rwRequest.fd(), 0);
Ratan Guptadc50ce52017-05-31 15:47:55 +0530225 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 Jefferyf34db312018-03-09 15:27:03 +1030232 // copy to the mapped memory.
Ratan Guptadc50ce52017-05-31 15:47:55 +0530233
Andrew Jefferyf34db312018-03-09 15:27:03 +1030234 uint32_t baseOffset = partitionInfo->data.base
235 << context->block_size_shift;
Ratan Guptadc50ce52017-05-31 15:47:55 +0530236
237 // if the asked offset + no of bytes to write is greater
238 // then size of the partition file then throw error.
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030239 //
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 Guptadc50ce52017-05-31 15:47:55 +0530242 if ((offset + count) > (baseOffset + partitionInfo->data.actual))
243 {
Ratan Guptadc50ce52017-05-31 15:47:55 +0530244 munmap(mapped_mem, partitionInfo->data.actual);
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030245 std::stringstream err;
246 err << "Write extends beyond the partition length " << std::hex
247 << partitionInfo->data.actual;
248 throw vpnor::OutOfBoundsOffset(err.str());
Ratan Guptadc50ce52017-05-31 15:47:55 +0530249 }
250
251 auto diffOffset = offset - baseOffset;
Andrew Jefferyf34db312018-03-09 15:27:03 +1030252 memcpy((char*)mapped_mem + diffOffset, buf, count);
Ratan Guptadc50ce52017-05-31 15:47:55 +0530253 munmap(mapped_mem, partitionInfo->data.actual);
254
255 set_flash_bytemap(context, offset, count, FLASH_DIRTY);
256 }
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030257 catch (vpnor::UnmappedOffset& e)
Ratan Guptadc50ce52017-05-31 15:47:55 +0530258 {
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030259 /* 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 Guptadc50ce52017-05-31 15:47:55 +0530275 }
276 return rc;
277}