blob: ffe75c67e3d3c72e05588c5214a824d2187a897c [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;
Andrew Jefferyad343102018-02-28 00:35:50 +103032namespace fs = std::experimental::filesystem;
Andrew Jefferyae1edb92018-02-28 23:16:48 +103033namespace vpnor = openpower::virtual_pnor;
34
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050035/** @brief unique_ptr functor to release a char* reference. */
36struct StringDeleter
37{
38 void operator()(char* ptr) const
39 {
40 free(ptr);
41 }
42};
43using StringPtr = std::unique_ptr<char, StringDeleter>;
44
Andrew Jefferyf34db312018-03-09 15:27:03 +103045int init_flash_dev(struct mbox_context* context)
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050046{
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 Jefferyf34db312018-03-09 15:27:03 +103062 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", filename.get(),
63 strerror(errno));
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050064 return -errno;
65 }
66
67 // Read the Flash Info
68 if (ioctl(fd, MEMGETINFO, &context->mtd_info) == -1)
69 {
Andrew Jefferyf34db312018-03-09 15:27:03 +103070 MSG_ERR("Couldn't get information about MTD: %s\n", strerror(errno));
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050071 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 Jefferyf34db312018-03-09 15:27:03 +103092void free_flash_dev(struct mbox_context* context)
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050093{
94 // No-op
95}
96
Andrew Jefferyf34db312018-03-09 15:27:03 +103097int set_flash_bytemap(struct mbox_context* context, uint32_t offset,
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050098 uint32_t count, uint8_t val)
99{
100 // No-op
101 return 0;
102}
103
Andrew Jefferyf34db312018-03-09 15:27:03 +1030104int erase_flash(struct mbox_context* context, uint32_t offset, uint32_t count)
Deepak Kodihalliabd52a72017-07-13 12:04:06 -0500105{
106 // No-op
107 return 0;
108}
109
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500110/*
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 Kodihalli7ee307c2017-07-12 03:41:08 -0500116 * 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 Kodihalli6c2fa902017-05-01 06:36:02 -0500119 */
Deepak Kodihalli7ee307c2017-07-12 03:41:08 -0500120int64_t copy_flash(struct mbox_context* context, uint32_t offset, void* mem,
121 uint32_t size)
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500122{
Andrew Jefferyfc621582018-02-28 22:20:19 +1030123 vpnor::partition::Table* table;
Deepak Kodihalli7ee307c2017-07-12 03:41:08 -0500124 int rc = size;
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500125
Andrew Jefferyfc621582018-02-28 22:20:19 +1030126 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 Jefferyf34db312018-03-09 15:27:03 +1030134 MSG_DBG("Copy virtual pnor to %p for size 0x%.8x from offset 0x%.8x\n", mem,
135 size, offset);
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500136
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 Jefferyfc621582018-02-28 22:20:19 +1030141 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 Gupta8441a392017-05-05 21:42:53 +0530150 try
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500151 {
Andrew Jefferyad343102018-02-28 00:35:50 +1030152 vpnor::Request req(context, offset);
153 rc = req.read(mem, size);
Ratan Gupta8441a392017-05-05 21:42:53 +0530154 }
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030155 catch (vpnor::UnmappedOffset& e)
Ratan Gupta8441a392017-05-05 21:42:53 +0530156 {
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030157 /*
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 Kodihallif9abed02017-07-17 06:23:08 -0500174 rc = -MBOX_R_SYSTEM_ERROR;
Ratan Gupta8441a392017-05-05 21:42:53 +0530175 }
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500176 return rc;
177}
Ratan Guptadc50ce52017-05-31 15:47:55 +0530178
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
189int write_flash(struct mbox_context* context, uint32_t offset, void* buf,
190 uint32_t count)
191{
Ratan Guptadc50ce52017-05-31 15:47:55 +0530192
Andrew Jefferyad343102018-02-28 00:35:50 +1030193 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 Guptadc50ce52017-05-31 15:47:55 +0530201 try
202 {
Andrew Jefferyad343102018-02-28 00:35:50 +1030203 const struct pnor_partition& part = table->partition(offset);
204 if (part.data.user.data[1] & PARTITION_READONLY)
Ratan Guptadc50ce52017-05-31 15:47:55 +0530205 {
Andrew Jefferyad343102018-02-28 00:35:50 +1030206 /* FIXME: This should be done on CREATE_WRITE_WINDOW, not here */
207 return -MBOX_R_WRITE_ERROR;
Ratan Guptadc50ce52017-05-31 15:47:55 +0530208 }
209
Andrew Jefferyad343102018-02-28 00:35:50 +1030210 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 Guptadc50ce52017-05-31 15:47:55 +0530214 }
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030215 catch (vpnor::UnmappedOffset& e)
Ratan Guptadc50ce52017-05-31 15:47:55 +0530216 {
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030217 /* 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 Guptadc50ce52017-05-31 15:47:55 +0530233 }
Andrew Jefferyad343102018-02-28 00:35:50 +1030234 return 0;
Ratan Guptadc50ce52017-05-31 15:47:55 +0530235}