blob: 37561418b24cf611d94f687b7c7dca499fa411e0 [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"
Andrew Jeffery71eaa732018-08-08 16:44:24 +093015#include "flash.h"
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -050016}
17
Ratan Gupta8441a392017-05-05 21:42:53 +053018#include "config.h"
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -050019#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 Jefferyd6b09bc2018-08-08 16:47:54 +093045int flash_dev_init(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 Jefferydec6ca62018-08-08 16:49:43 +093092void flash_dev_free(struct mbox_context* context)
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050093{
94 // No-op
95}
96
Andrew Jefferyf953f792018-08-08 16:56:03 +093097int flash_set_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 Jeffery4f5d29c2018-08-08 16:58:04 +0930104int flash_erase(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/*
Andrew Jefferye0cdd3e2018-08-08 16:51:44 +0930111 * flash_copy() - Copy data from the virtual pnor into a provided buffer
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500112 * @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
Andrew Jefferye0cdd3e2018-08-08 16:51:44 +0930117 * code. flash_copy will copy at most 'size' bytes, but it may
Deepak Kodihalli7ee307c2017-07-12 03:41:08 -0500118 * copy less.
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500119 */
Andrew Jefferye0cdd3e2018-08-08 16:51:44 +0930120int64_t flash_copy(struct mbox_context* context, uint32_t offset, void* mem,
Deepak Kodihalli7ee307c2017-07-12 03:41:08 -0500121 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
Andrew Jeffery0293f2f2018-08-08 16:59:45 +0930161 * dropped on the floor, see the implementation of flash_write() below.
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030162 */
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/*
Andrew Jeffery0293f2f2018-08-08 16:59:45 +0930180 * flash_write() - Write to the virtual pnor from a provided buffer
Ratan Guptadc50ce52017-05-31 15:47:55 +0530181 * @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
Andrew Jeffery0293f2f2018-08-08 16:59:45 +0930189int flash_write(struct mbox_context* context, uint32_t offset, void* buf,
Ratan Guptadc50ce52017-05-31 15:47:55 +0530190 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 Jeffery52a83192018-03-27 10:35:31 +1030206 MSG_ERR("Unreachable: Host attempted to write to read-only "
207 "partition %s\n",
208 part.data.name);
Andrew Jefferyad343102018-02-28 00:35:50 +1030209 return -MBOX_R_WRITE_ERROR;
Ratan Guptadc50ce52017-05-31 15:47:55 +0530210 }
211
Andrew Jefferyad343102018-02-28 00:35:50 +1030212 MSG_DBG("Write flash @ 0x%.8x for 0x%.8x from %p\n", offset, count,
213 buf);
214 vpnor::Request req(context, offset);
215 req.write(buf, count);
Ratan Guptadc50ce52017-05-31 15:47:55 +0530216 }
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030217 catch (vpnor::UnmappedOffset& e)
Ratan Guptadc50ce52017-05-31 15:47:55 +0530218 {
Andrew Jeffery52a83192018-03-27 10:35:31 +1030219 MSG_ERR("Unreachable: Host attempted to write %" PRIu32
220 " bytes to unmapped offset 0x%" PRIx32 "\n",
221 count, offset);
222 return -MBOX_R_WRITE_ERROR;
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030223 }
224 catch (const vpnor::OutOfBoundsOffset& e)
225 {
226 MSG_ERR("%s\n", e.what());
227 return -MBOX_R_PARAM_ERROR;
228 }
229 catch (const std::exception& e)
230 {
231 MSG_ERR("%s\n", e.what());
232 phosphor::logging::commit<err::InternalFailure>();
233 return -MBOX_R_SYSTEM_ERROR;
Ratan Guptadc50ce52017-05-31 15:47:55 +0530234 }
Andrew Jefferyad343102018-02-28 00:35:50 +1030235 return 0;
Ratan Guptadc50ce52017-05-31 15:47:55 +0530236}