blob: 9575705e31cac46f2a8641e896fec429fb1e0371 [file] [log] [blame]
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -05001/*
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 Gupta8441a392017-05-05 21:42:53 +053020#include <fcntl.h>
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -050021#include <stdint.h>
22#include <stdlib.h>
23#include <syslog.h>
Ratan Gupta8441a392017-05-05 21:42:53 +053024#include <sys/mman.h>
25#include <unistd.h>
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050026#include <sys/ioctl.h>
Deepak Kodihalli7ee307c2017-07-12 03:41:08 -050027#include <algorithm>
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -050028
29extern "C" {
30#include "common.h"
31}
32
Ratan Gupta8441a392017-05-05 21:42:53 +053033#include "config.h"
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -050034#include "mboxd_flash.h"
35#include "mboxd_pnor_partition_table.h"
Ratan Gupta6a98e182017-06-06 15:04:23 +053036#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 Kodihalli6c2fa902017-05-01 06:36:02 -050040
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050041#include <memory>
Ratan Gupta8441a392017-05-05 21:42:53 +053042#include <string>
43#include <exception>
44#include <stdexcept>
Ratan Gupta8441a392017-05-05 21:42:53 +053045
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050046/** @brief unique_ptr functor to release a char* reference. */
47struct StringDeleter
48{
49 void operator()(char* ptr) const
50 {
51 free(ptr);
52 }
53};
54using StringPtr = std::unique_ptr<char, StringDeleter>;
55
Andrew Jefferyf34db312018-03-09 15:27:03 +103056int init_flash_dev(struct mbox_context* context)
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050057{
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 Jefferyf34db312018-03-09 15:27:03 +103073 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", filename.get(),
74 strerror(errno));
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050075 return -errno;
76 }
77
78 // Read the Flash Info
79 if (ioctl(fd, MEMGETINFO, &context->mtd_info) == -1)
80 {
Andrew Jefferyf34db312018-03-09 15:27:03 +103081 MSG_ERR("Couldn't get information about MTD: %s\n", strerror(errno));
Deepak Kodihalliabd52a72017-07-13 12:04:06 -050082 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 Jefferyf34db312018-03-09 15:27:03 +1030103void free_flash_dev(struct mbox_context* context)
Deepak Kodihalliabd52a72017-07-13 12:04:06 -0500104{
105 // No-op
106}
107
Andrew Jefferyf34db312018-03-09 15:27:03 +1030108int set_flash_bytemap(struct mbox_context* context, uint32_t offset,
Deepak Kodihalliabd52a72017-07-13 12:04:06 -0500109 uint32_t count, uint8_t val)
110{
111 // No-op
112 return 0;
113}
114
Andrew Jefferyf34db312018-03-09 15:27:03 +1030115int erase_flash(struct mbox_context* context, uint32_t offset, uint32_t count)
Deepak Kodihalliabd52a72017-07-13 12:04:06 -0500116{
117 // No-op
118 return 0;
119}
120
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500121/*
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 Kodihalli7ee307c2017-07-12 03:41:08 -0500127 * 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 Kodihalli6c2fa902017-05-01 06:36:02 -0500130 */
Deepak Kodihalli7ee307c2017-07-12 03:41:08 -0500131int64_t copy_flash(struct mbox_context* context, uint32_t offset, void* mem,
132 uint32_t size)
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500133{
Ratan Gupta6a98e182017-06-06 15:04:23 +0530134 using namespace phosphor::logging;
135 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
136 using namespace std::string_literals;
137
Deepak Kodihalli7ee307c2017-07-12 03:41:08 -0500138 int rc = size;
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500139
Andrew Jefferyf34db312018-03-09 15:27:03 +1030140 MSG_DBG("Copy virtual pnor to %p for size 0x%.8x from offset 0x%.8x\n", mem,
141 size, offset);
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500142
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 Gupta8441a392017-05-05 21:42:53 +0530147 try
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500148 {
Andrew Jefferyf34db312018-03-09 15:27:03 +1030149 size_t sz = vpnor_get_partition_table_size(context)
150 << context->block_size_shift;
Ratan Gupta8441a392017-05-05 21:42:53 +0530151 if (offset < sz)
152 {
153 const struct pnor_partition_table* table =
154 vpnor_get_partition_table(context);
Deepak Kodihalli7ee307c2017-07-12 03:41:08 -0500155 rc = std::min(sz - offset, static_cast<size_t>(size));
156 memcpy(mem, ((uint8_t*)table) + offset, rc);
Ratan Gupta8441a392017-05-05 21:42:53 +0530157 }
158 else
159 {
Ratan Gupta6a98e182017-06-06 15:04:23 +0530160 openpower::virtual_pnor::RORequest roRequest;
161 auto partitionInfo = roRequest.getPartitionInfo(context, offset);
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500162
Andrew Jefferyf34db312018-03-09 15:27:03 +1030163 auto mapped_mem = mmap(NULL, partitionInfo->data.actual, PROT_READ,
164 MAP_PRIVATE, roRequest.fd(), 0);
Ratan Gupta8441a392017-05-05 21:42:53 +0530165
Adriana Kobylak0a2cc952017-10-12 11:13:06 -0500166 if (mapped_mem == MAP_FAILED)
Ratan Gupta8441a392017-05-05 21:42:53 +0530167 {
Ratan Gupta6a98e182017-06-06 15:04:23 +0530168 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 Jefferyf34db312018-03-09 15:27:03 +1030177 uint32_t baseOffset = partitionInfo->data.base
178 << context->block_size_shift;
179 // copy to the reserved memory area
Ratan Gupta6a98e182017-06-06 15:04:23 +0530180 auto diffOffset = offset - baseOffset;
Deepak Kodihallib100fb82017-07-12 04:44:41 -0500181 rc = std::min(partitionInfo->data.actual - diffOffset, size);
Andrew Jefferyf34db312018-03-09 15:27:03 +1030182 memcpy(mem, (char*)mapped_mem + diffOffset, rc);
Ratan Gupta6a98e182017-06-06 15:04:23 +0530183 munmap(mapped_mem, partitionInfo->data.actual);
Ratan Gupta8441a392017-05-05 21:42:53 +0530184 }
185 }
Ratan Gupta6a98e182017-06-06 15:04:23 +0530186 catch (InternalFailure& e)
Ratan Gupta8441a392017-05-05 21:42:53 +0530187 {
Ratan Gupta6a98e182017-06-06 15:04:23 +0530188 commit<InternalFailure>();
Deepak Kodihallif9abed02017-07-17 06:23:08 -0500189 rc = -MBOX_R_SYSTEM_ERROR;
Ratan Gupta8441a392017-05-05 21:42:53 +0530190 }
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500191 return rc;
192}
Ratan Guptadc50ce52017-05-31 15:47:55 +0530193
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
204int 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 Jefferyf34db312018-03-09 15:27:03 +1030218 auto mapped_mem =
219 mmap(NULL, partitionInfo->data.actual, PROT_READ | PROT_WRITE,
220 MAP_SHARED, rwRequest.fd(), 0);
Ratan Guptadc50ce52017-05-31 15:47:55 +0530221 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 Jefferyf34db312018-03-09 15:27:03 +1030228 // copy to the mapped memory.
Ratan Guptadc50ce52017-05-31 15:47:55 +0530229
Andrew Jefferyf34db312018-03-09 15:27:03 +1030230 uint32_t baseOffset = partitionInfo->data.base
231 << context->block_size_shift;
Ratan Guptadc50ce52017-05-31 15:47:55 +0530232
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 Jefferyf34db312018-03-09 15:27:03 +1030238 partitionInfo->data.actual);
Ratan Guptadc50ce52017-05-31 15:47:55 +0530239 munmap(mapped_mem, partitionInfo->data.actual);
240 elog<InternalFailure>();
241 }
242
243 auto diffOffset = offset - baseOffset;
Andrew Jefferyf34db312018-03-09 15:27:03 +1030244 memcpy((char*)mapped_mem + diffOffset, buf, count);
Ratan Guptadc50ce52017-05-31 15:47:55 +0530245 munmap(mapped_mem, partitionInfo->data.actual);
246
247 set_flash_bytemap(context, offset, count, FLASH_DIRTY);
248 }
Andrew Jefferyf34db312018-03-09 15:27:03 +1030249 catch (InternalFailure& e)
Ratan Guptadc50ce52017-05-31 15:47:55 +0530250 {
251 rc = -1;
252 }
253 return rc;
254}