blob: 15613967a5810e002754fd06d1090a9623ccc16b [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
Andrew Jefferyfc621582018-02-28 22:20:19 +1030126 vpnor::partition::Table* table;
Deepak Kodihalli7ee307c2017-07-12 03:41:08 -0500127 int rc = size;
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500128
Andrew Jefferyfc621582018-02-28 22:20:19 +1030129 if (!(context && context->vpnor && context->vpnor->table))
130 {
131 MSG_ERR("Trying to copy data with uninitialised context!\n");
132 return -MBOX_R_SYSTEM_ERROR;
133 }
134
135 table = context->vpnor->table;
136
Andrew Jefferyf34db312018-03-09 15:27:03 +1030137 MSG_DBG("Copy virtual pnor to %p for size 0x%.8x from offset 0x%.8x\n", mem,
138 size, offset);
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500139
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 Jefferyfc621582018-02-28 22:20:19 +1030144 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 Gupta8441a392017-05-05 21:42:53 +0530153 try
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500154 {
Andrew Jefferyfc621582018-02-28 22:20:19 +1030155 openpower::virtual_pnor::RORequest roRequest;
156 auto partitionInfo = roRequest.getPartitionInfo(context, offset);
157
158 auto mapped_mem = mmap(NULL, partitionInfo->data.actual, PROT_READ,
159 MAP_PRIVATE, roRequest.fd(), 0);
160
161 if (mapped_mem == MAP_FAILED)
Ratan Gupta8441a392017-05-05 21:42:53 +0530162 {
Andrew Jefferyfc621582018-02-28 22:20:19 +1030163 MSG_ERR("Failed to map partition=%s:Error=%s\n",
164 partitionInfo->data.name, strerror(errno));
165
166 elog<InternalFailure>();
Ratan Gupta8441a392017-05-05 21:42:53 +0530167 }
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500168
Andrew Jefferyfc621582018-02-28 22:20:19 +1030169 // if the asked offset + no of bytes to read is greater
170 // then size of the partition file then throw error.
Ratan Gupta8441a392017-05-05 21:42:53 +0530171
Andrew Jefferyfc621582018-02-28 22:20:19 +1030172 uint32_t baseOffset = partitionInfo->data.base
173 << context->block_size_shift;
174 // copy to the reserved memory area
175 auto diffOffset = offset - baseOffset;
176 rc = std::min(partitionInfo->data.actual - diffOffset, size);
177 memcpy(mem, (char*)mapped_mem + diffOffset, rc);
178 munmap(mapped_mem, partitionInfo->data.actual);
Ratan Gupta8441a392017-05-05 21:42:53 +0530179 }
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030180 catch (vpnor::UnmappedOffset& e)
Ratan Gupta8441a392017-05-05 21:42:53 +0530181 {
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030182 /*
183 * Hooo boy. Pretend that this is valid flash so we don't have
184 * discontiguous regions presented to the host. Instead, fill a window
185 * with 0xff so the 'flash' looks erased. Writes to such regions are
186 * dropped on the floor, see the implementation of write_flash() below.
187 */
188 MSG_INFO("Host requested unmapped region of %" PRId32
189 " bytes at offset 0x%" PRIx32 "\n",
190 size, offset);
191 uint32_t span = e.next - e.base;
192 rc = std::min(size, span);
193 memset(mem, 0xff, rc);
194 }
195 catch (std::exception& e)
196 {
197 MSG_ERR("%s\n", e.what());
198 phosphor::logging::commit<err::InternalFailure>();
Deepak Kodihallif9abed02017-07-17 06:23:08 -0500199 rc = -MBOX_R_SYSTEM_ERROR;
Ratan Gupta8441a392017-05-05 21:42:53 +0530200 }
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500201 return rc;
202}
Ratan Guptadc50ce52017-05-31 15:47:55 +0530203
204/*
205 * write_flash() - Write to the virtual pnor from a provided buffer
206 * @context: The mbox context pointer
207 * @offset: The flash offset to write to (bytes)
208 * @buf: The buffer to write from (must be of atleast size)
209 * @size: The number of bytes to write
210 *
211 * Return: 0 on success otherwise negative error code
212 */
213
214int write_flash(struct mbox_context* context, uint32_t offset, void* buf,
215 uint32_t count)
216{
217 using namespace phosphor::logging;
218 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
219 using namespace std::string_literals;
220
221 int rc = 0;
222 MSG_DBG("Write flash @ 0x%.8x for 0x%.8x from %p\n", offset, count, buf);
223 try
224 {
225 openpower::virtual_pnor::RWRequest rwRequest;
226 auto partitionInfo = rwRequest.getPartitionInfo(context, offset);
227
Andrew Jefferyf34db312018-03-09 15:27:03 +1030228 auto mapped_mem =
229 mmap(NULL, partitionInfo->data.actual, PROT_READ | PROT_WRITE,
230 MAP_SHARED, rwRequest.fd(), 0);
Ratan Guptadc50ce52017-05-31 15:47:55 +0530231 if (mapped_mem == MAP_FAILED)
232 {
233 MSG_ERR("Failed to map partition=%s:Error=%s\n",
234 partitionInfo->data.name, strerror(errno));
235
236 elog<InternalFailure>();
237 }
Andrew Jefferyf34db312018-03-09 15:27:03 +1030238 // copy to the mapped memory.
Ratan Guptadc50ce52017-05-31 15:47:55 +0530239
Andrew Jefferyf34db312018-03-09 15:27:03 +1030240 uint32_t baseOffset = partitionInfo->data.base
241 << context->block_size_shift;
Ratan Guptadc50ce52017-05-31 15:47:55 +0530242
243 // if the asked offset + no of bytes to write is greater
244 // then size of the partition file then throw error.
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030245 //
246 // FIXME: Don't use .actual, use (.size << ctx->block_size_shift),
247 // otherwise we can't grow the size of the data to fill the partition
Ratan Guptadc50ce52017-05-31 15:47:55 +0530248 if ((offset + count) > (baseOffset + partitionInfo->data.actual))
249 {
Ratan Guptadc50ce52017-05-31 15:47:55 +0530250 munmap(mapped_mem, partitionInfo->data.actual);
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030251 std::stringstream err;
252 err << "Write extends beyond the partition length " << std::hex
253 << partitionInfo->data.actual;
254 throw vpnor::OutOfBoundsOffset(err.str());
Ratan Guptadc50ce52017-05-31 15:47:55 +0530255 }
256
257 auto diffOffset = offset - baseOffset;
Andrew Jefferyf34db312018-03-09 15:27:03 +1030258 memcpy((char*)mapped_mem + diffOffset, buf, count);
Ratan Guptadc50ce52017-05-31 15:47:55 +0530259 munmap(mapped_mem, partitionInfo->data.actual);
260
261 set_flash_bytemap(context, offset, count, FLASH_DIRTY);
262 }
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030263 catch (vpnor::UnmappedOffset& e)
Ratan Guptadc50ce52017-05-31 15:47:55 +0530264 {
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030265 /* Paper over the fact that the write isn't persistent */
266 MSG_INFO("Dropping %d bytes host wrote to unmapped offset 0x%" PRIx32
267 "\n",
268 count, offset);
269 return 0;
270 }
271 catch (const vpnor::OutOfBoundsOffset& e)
272 {
273 MSG_ERR("%s\n", e.what());
274 return -MBOX_R_PARAM_ERROR;
275 }
276 catch (const std::exception& e)
277 {
278 MSG_ERR("%s\n", e.what());
279 phosphor::logging::commit<err::InternalFailure>();
280 return -MBOX_R_SYSTEM_ERROR;
Ratan Guptadc50ce52017-05-31 15:47:55 +0530281 }
282 return rc;
283}