blob: 9854159e7c025e0b132f0177318101942cebec3f [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Andrew Jeffery71eaa732018-08-08 16:44:24 +09303extern "C" {
Evan Lojewskif1e547c2019-03-14 14:34:33 +10304#include "mboxd.h"
Andrew Jeffery71eaa732018-08-08 16:44:24 +09305}
6
William A. Kennington IIId5f1d402018-10-11 13:55:04 -07007#include "config.h"
8
Ratan Guptac0ef9872017-06-06 14:31:37 +05309#include "pnor_partition.hpp"
Andrew Jefferyad343102018-02-28 00:35:50 +103010#include "pnor_partition_table.hpp"
Ratan Guptac0ef9872017-06-06 14:31:37 +053011#include "xyz/openbmc_project/Common/error.hpp"
Ratan Guptac0ef9872017-06-06 14:31:37 +053012
Andrew Jefferyad343102018-02-28 00:35:50 +103013#include <assert.h>
Andrew Jeffery1a3f8432018-03-02 10:18:02 +103014#include <fcntl.h>
Ratan Guptac0ef9872017-06-06 14:31:37 +053015#include <stdint.h>
16#include <stdlib.h>
Ratan Guptac0ef9872017-06-06 14:31:37 +053017#include <sys/ioctl.h>
18#include <sys/mman.h>
William A. Kennington IIId5f1d402018-10-11 13:55:04 -070019#include <sys/types.h>
20#include <syslog.h>
Andrew Jeffery1a3f8432018-03-02 10:18:02 +103021#include <unistd.h>
Ratan Guptac0ef9872017-06-06 14:31:37 +053022
Ratan Guptac0ef9872017-06-06 14:31:37 +053023#include <exception>
Ratan Guptac0ef9872017-06-06 14:31:37 +053024#include <iostream>
William A. Kennington IIId5f1d402018-10-11 13:55:04 -070025#include <phosphor-logging/elog-errors.hpp>
26#include <phosphor-logging/log.hpp>
27#include <stdexcept>
28#include <string>
29
30#include "common.h"
31#include "mboxd_pnor_partition_table.h"
Ratan Guptac0ef9872017-06-06 14:31:37 +053032
33namespace openpower
34{
35namespace virtual_pnor
36{
37
Andrew Jefferyad343102018-02-28 00:35:50 +103038namespace fs = std::experimental::filesystem;
Ratan Guptac0ef9872017-06-06 14:31:37 +053039
Andrew Jefferyad343102018-02-28 00:35:50 +103040fs::path Request::getPartitionFilePath(int flags)
Ratan Guptac0ef9872017-06-06 14:31:37 +053041{
Evan Lojewskif1e547c2019-03-14 14:34:33 +103042 struct vpnor_data* priv = (struct vpnor_data*)backend->priv;
43
Adriana Kobylakc71dfd72017-07-22 11:10:43 -050044 // Check if partition exists in patch location
Evan Lojewskif1e547c2019-03-14 14:34:33 +103045 auto dst = fs::path(priv->paths.patch_loc) / partition.data.name;
Andrew Jefferyad343102018-02-28 00:35:50 +103046 if (fs::is_regular_file(dst))
Adriana Kobylakc71dfd72017-07-22 11:10:43 -050047 {
Andrew Jefferyad343102018-02-28 00:35:50 +103048 return dst;
Adriana Kobylakc71dfd72017-07-22 11:10:43 -050049 }
50
Andrew Jefferyad343102018-02-28 00:35:50 +103051 switch (partition.data.user.data[1] &
Ratan Guptac0ef9872017-06-06 14:31:37 +053052 (PARTITION_PRESERVED | PARTITION_READONLY))
53 {
54 case PARTITION_PRESERVED:
Evan Lojewskif1e547c2019-03-14 14:34:33 +103055 dst = priv->paths.prsv_loc;
Ratan Guptac0ef9872017-06-06 14:31:37 +053056 break;
57
58 case PARTITION_READONLY:
Evan Lojewskif1e547c2019-03-14 14:34:33 +103059 dst = priv->paths.ro_loc;
Ratan Guptac0ef9872017-06-06 14:31:37 +053060 break;
61
62 default:
Evan Lojewskif1e547c2019-03-14 14:34:33 +103063 dst = priv->paths.rw_loc;
Ratan Guptac0ef9872017-06-06 14:31:37 +053064 }
Andrew Jefferyad343102018-02-28 00:35:50 +103065 dst /= partition.data.name;
Ratan Guptac0ef9872017-06-06 14:31:37 +053066
Andrew Jefferyad343102018-02-28 00:35:50 +103067 if (fs::exists(dst))
Ratan Guptac0ef9872017-06-06 14:31:37 +053068 {
Andrew Jefferyad343102018-02-28 00:35:50 +103069 return dst;
Ratan Guptac0ef9872017-06-06 14:31:37 +053070 }
Andrew Jefferyad343102018-02-28 00:35:50 +103071
72 if (flags == O_RDONLY)
Ratan Guptac0ef9872017-06-06 14:31:37 +053073 {
Evan Lojewskif1e547c2019-03-14 14:34:33 +103074 dst = fs::path(priv->paths.ro_loc) / partition.data.name;
Andrew Jefferyad343102018-02-28 00:35:50 +103075 assert(fs::exists(dst));
76 return dst;
Ratan Guptac0ef9872017-06-06 14:31:37 +053077 }
78
Andrew Jefferyad343102018-02-28 00:35:50 +103079 assert(flags == O_RDWR);
Evan Lojewskif1e547c2019-03-14 14:34:33 +103080 auto src = fs::path(priv->paths.ro_loc) / partition.data.name;
Andrew Jefferyad343102018-02-28 00:35:50 +103081 assert(fs::exists(src));
Ratan Guptac0ef9872017-06-06 14:31:37 +053082
Andrew Jefferye8a79ff2018-02-27 10:56:32 +103083 MSG_DBG("RWRequest: Didn't find '%s' under '%s', copying from '%s'\n",
Andrew Jefferyad343102018-02-28 00:35:50 +103084 partition.data.name, dst.c_str(), src.c_str());
Andrew Jefferye8a79ff2018-02-27 10:56:32 +103085
Evan Lojewskif1e547c2019-03-14 14:34:33 +103086 dst = priv->paths.rw_loc;
Andrew Jefferyad343102018-02-28 00:35:50 +103087 if (partition.data.user.data[1] & PARTITION_PRESERVED)
Ratan Guptac0ef9872017-06-06 14:31:37 +053088 {
Evan Lojewskif1e547c2019-03-14 14:34:33 +103089 dst = priv->paths.prsv_loc;
Ratan Guptac0ef9872017-06-06 14:31:37 +053090 }
91
Andrew Jefferyad343102018-02-28 00:35:50 +103092 dst /= partition.data.name;
93 fs::copy_file(src, dst);
Ratan Guptac0ef9872017-06-06 14:31:37 +053094
Andrew Jefferyad343102018-02-28 00:35:50 +103095 return dst;
96}
97
Andrew Jeffery1a3f8432018-03-02 10:18:02 +103098size_t Request::clamp(size_t len)
99{
100 size_t maxAccess = offset + len;
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030101 size_t partSize = partition.data.size << backend->block_size_shift;
Andrew Jeffery1a3f8432018-03-02 10:18:02 +1030102 return std::min(maxAccess, partSize) - offset;
103}
104
William A. Kennington IIId5f1d402018-10-11 13:55:04 -0700105void Request::resize(const fs::path& path, size_t len)
Andrew Jeffery1a3f8432018-03-02 10:18:02 +1030106{
107 size_t maxAccess = offset + len;
108 size_t fileSize = fs::file_size(path);
109 if (maxAccess < fileSize)
110 {
111 return;
112 }
113 MSG_DBG("Resizing %s to %zu bytes\n", path.c_str(), maxAccess);
114 int rc = truncate(path.c_str(), maxAccess);
115 if (rc == -1)
116 {
117 MSG_ERR("Failed to resize %s: %d\n", path.c_str(), errno);
118 throw std::system_error(errno, std::system_category());
119 }
120}
121
William A. Kennington IIId5f1d402018-10-11 13:55:04 -0700122size_t Request::fulfil(const fs::path& path, int flags, void* buf, size_t len)
Andrew Jefferyad343102018-02-28 00:35:50 +1030123{
124 if (!(flags == O_RDONLY || flags == O_RDWR))
Ratan Guptac0ef9872017-06-06 14:31:37 +0530125 {
Andrew Jefferyad343102018-02-28 00:35:50 +1030126 std::stringstream err;
127 err << "Require O_RDONLY (0x" << std::hex << O_RDONLY << " or O_RDWR "
128 << std::hex << O_RDWR << " for flags, got: 0x" << std::hex << flags;
129 throw std::invalid_argument(err.str());
Ratan Guptac0ef9872017-06-06 14:31:37 +0530130 }
131
Andrew Jefferyad343102018-02-28 00:35:50 +1030132 int fd = ::open(path.c_str(), flags);
133 if (fd == -1)
134 {
Andrew Jeffery974507e2018-04-30 15:33:55 +0930135 MSG_ERR("Failed to open backing file at '%s': %d\n", path.c_str(),
136 errno);
Andrew Jefferyad343102018-02-28 00:35:50 +1030137 throw std::system_error(errno, std::system_category());
138 }
139
Andrew Jefferycc650612018-08-30 12:17:59 +0930140 if (flags == O_RDONLY)
141 {
142 MSG_INFO("Fulfilling read request against %s at offset 0x%zx into %p "
143 "for %zu\n",
144 path.c_str(), offset, buf, len);
145 }
146 else
147 {
148 MSG_INFO("Fulfilling write request against %s at offset 0x%zx from %p "
149 "for %zu\n",
150 path.c_str(), offset, buf, len);
151 }
152
Andrew Jeffery1a3f8432018-03-02 10:18:02 +1030153 size_t fileSize = fs::file_size(path);
Andrew Jefferyad343102018-02-28 00:35:50 +1030154 int mprot = PROT_READ | ((flags == O_RDWR) ? PROT_WRITE : 0);
Andrew Jeffery1a3f8432018-03-02 10:18:02 +1030155 auto map = mmap(NULL, fileSize, mprot, MAP_SHARED, fd, 0);
Andrew Jefferyad343102018-02-28 00:35:50 +1030156 if (map == MAP_FAILED)
157 {
158 close(fd);
Andrew Jeffery1a3f8432018-03-02 10:18:02 +1030159 MSG_ERR("Failed to map backing file '%s' for %zd bytes: %d\n",
160 path.c_str(), fileSize, errno);
Andrew Jefferyad343102018-02-28 00:35:50 +1030161 throw std::system_error(errno, std::system_category());
162 }
163
164 // copy to the reserved memory area
165 if (flags == O_RDONLY)
166 {
Andrew Jeffery1a3f8432018-03-02 10:18:02 +1030167 memset(buf, 0xff, len);
William A. Kennington IIId5f1d402018-10-11 13:55:04 -0700168 memcpy(buf, (char*)map + offset, std::min(len, fileSize));
Andrew Jefferyad343102018-02-28 00:35:50 +1030169 }
170 else
171 {
William A. Kennington IIId5f1d402018-10-11 13:55:04 -0700172 memcpy((char*)map + offset, buf, len);
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030173 backend_set_bytemap(backend, base + offset, len, FLASH_DIRTY);
Andrew Jefferyad343102018-02-28 00:35:50 +1030174 }
Andrew Jeffery1a3f8432018-03-02 10:18:02 +1030175 munmap(map, fileSize);
Andrew Jefferyad343102018-02-28 00:35:50 +1030176 close(fd);
177
178 return len;
Ratan Guptac0ef9872017-06-06 14:31:37 +0530179}
180
Andrew Jefferyf34db312018-03-09 15:27:03 +1030181} // namespace virtual_pnor
182} // namespace openpower