blob: 986ca1f716d41870d406e96d4f4f47aa68256346 [file] [log] [blame]
Andrew Jefferyacee6832018-02-21 22:17:08 +10301/*
2 * Mailbox Daemon Implementation
3 *
4 * Copyright 2018 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 */
Ratan Guptac0ef9872017-06-06 14:31:37 +053019#pragma once
20
21#include "mboxd_pnor_partition_table.h"
22#include <fcntl.h>
23#include <string>
24#include <unistd.h>
25#include <experimental/filesystem>
26
27namespace openpower
28{
29namespace file
30{
31
32class Descriptor
33{
Andrew Jefferyf34db312018-03-09 15:27:03 +103034 private:
35 /** default value */
36 int fd = -1;
Ratan Guptac0ef9872017-06-06 14:31:37 +053037
Andrew Jefferyf34db312018-03-09 15:27:03 +103038 public:
39 Descriptor() = default;
40 Descriptor(const Descriptor&) = delete;
41 Descriptor& operator=(const Descriptor&) = delete;
42 Descriptor(Descriptor&&) = delete;
43 Descriptor& operator=(Descriptor&&) = delete;
Ratan Guptac0ef9872017-06-06 14:31:37 +053044
Andrew Jefferyf34db312018-03-09 15:27:03 +103045 Descriptor(int fd) : fd(fd)
46 {
47 }
Ratan Guptac0ef9872017-06-06 14:31:37 +053048
Andrew Jefferyf34db312018-03-09 15:27:03 +103049 ~Descriptor()
50 {
51 if (fd >= 0)
Ratan Guptac0ef9872017-06-06 14:31:37 +053052 {
Andrew Jefferyf34db312018-03-09 15:27:03 +103053 close(fd);
Ratan Guptac0ef9872017-06-06 14:31:37 +053054 }
Andrew Jefferyf34db312018-03-09 15:27:03 +103055 }
Ratan Guptac0ef9872017-06-06 14:31:37 +053056
Andrew Jefferyf34db312018-03-09 15:27:03 +103057 int operator()() const
58 {
59 return fd;
60 }
Ratan Guptac0ef9872017-06-06 14:31:37 +053061
Andrew Jefferyf34db312018-03-09 15:27:03 +103062 void set(int descriptor)
63 {
64 fd = descriptor;
65 }
Ratan Guptac0ef9872017-06-06 14:31:37 +053066};
67
Andrew Jefferyf34db312018-03-09 15:27:03 +103068} // namespace file
Ratan Guptac0ef9872017-06-06 14:31:37 +053069
70namespace virtual_pnor
71{
72
73namespace fs = std::experimental::filesystem;
74
75enum class ReturnCode : uint8_t
76{
77 FILE_NOT_FOUND = 0,
78 PARTITION_NOT_FOUND = 1,
79 PARTITION_READ_ONLY = 2,
80 FILE_OPEN_FAILURE = 3,
81 SUCCESS = 4,
82};
83
84class Request
85{
Andrew Jefferyf34db312018-03-09 15:27:03 +103086 public:
87 Request() = default;
88 Request(const Request&) = delete;
89 Request& operator=(const Request&) = delete;
90 Request(Request&&) = default;
91 Request& operator=(Request&&) = default;
92 ~Request() = default;
Ratan Guptac0ef9872017-06-06 14:31:37 +053093
Andrew Jefferyf34db312018-03-09 15:27:03 +103094 openpower::file::Descriptor fd;
Ratan Guptac0ef9872017-06-06 14:31:37 +053095
Andrew Jefferyf34db312018-03-09 15:27:03 +103096 protected:
97 /** @brief opens the partition file
98 *
99 * @param[in] filePath - Absolute file path.
100 * @param[in] mode - File open mode.
101 */
102 ReturnCode open(const std::string& filePath, int mode);
Ratan Guptac0ef9872017-06-06 14:31:37 +0530103
Andrew Jefferyf34db312018-03-09 15:27:03 +1030104 /** @brief returns the partition file path associated with the offset.
105 *
106 * @param[in] context - The mbox context pointer.
107 * @param[in] offset - The pnor offset(bytes).
108 */
Ratan Guptac0ef9872017-06-06 14:31:37 +0530109
Andrew Jefferyf34db312018-03-09 15:27:03 +1030110 std::string getPartitionFilePath(struct mbox_context* context,
111 uint32_t offset);
Ratan Guptac0ef9872017-06-06 14:31:37 +0530112
Andrew Jefferyf34db312018-03-09 15:27:03 +1030113 const pnor_partition* partition = nullptr;
Ratan Guptac0ef9872017-06-06 14:31:37 +0530114};
115
116/** @class RORequest
117 * @brief Represent the read request of the partition.
118 * Stores the partition meta data.
119 */
120class RORequest : public Request
121{
Andrew Jefferyf34db312018-03-09 15:27:03 +1030122 public:
123 RORequest() = default;
124 RORequest(const RORequest&) = delete;
125 RORequest& operator=(const RORequest&) = delete;
126 RORequest(RORequest&&) = default;
127 RORequest& operator=(RORequest&&) = default;
128 ~RORequest(){};
Ratan Guptac0ef9872017-06-06 14:31:37 +0530129
Andrew Jefferyf34db312018-03-09 15:27:03 +1030130 /** @brief opens the partition file associated with the offset
131 * in read only mode and gets the partition details.
132 *
133 * 1. Depending on the partition type,tries to open the file
134 * from the associated partition(RW/PRSV/RO).
135 * 1a. if file not found in the corresponding
136 * partition(RW/PRSV/RO) then tries to read the file from
137 * the read only partition.
138 * 1b. if the file not found in the read only partition then
139 * throw exception.
140 *
141 * @param[in] context - The mbox context pointer.
142 * @param[in] offset - The pnor offset(bytes).
143 */
144 const pnor_partition* getPartitionInfo(struct mbox_context* context,
145 uint32_t offset);
Ratan Guptac0ef9872017-06-06 14:31:37 +0530146};
147
148/** @class RWRequest
149 * @brief Represent the write request of the partition.
150 * Stores the partition meta data.
151 */
152class RWRequest : public Request
153{
Andrew Jefferyf34db312018-03-09 15:27:03 +1030154 public:
155 RWRequest() = default;
156 RWRequest(const RWRequest&) = delete;
157 RWRequest& operator=(const RWRequest&) = delete;
158 RWRequest(RWRequest&&) = default;
159 RWRequest& operator=(RWRequest&&) = default;
160 ~RWRequest(){};
Ratan Guptac0ef9872017-06-06 14:31:37 +0530161
Andrew Jefferyf34db312018-03-09 15:27:03 +1030162 /** @brief opens the partition file associated with the offset
163 * in write mode and gets the parttition details.
164 *
165 * 1. Depending on the partition type tries to open the file
166 * from the associated partition.
167 * 1a. if file not found in the corresponding partition(RW/PRSV)
168 * then copy the file from the read only partition to the (RW/PRSV)
169 * partition depending on the partition type.
170 * 1b. if the file not found in the read only partition then throw
171 * exception.
172 *
173 * @param[in] context - The mbox context pointer.
174 * @param[in] offset - The pnor offset(bytes).
175 */
176 const pnor_partition* getPartitionInfo(struct mbox_context* context,
177 uint32_t offset);
Ratan Guptac0ef9872017-06-06 14:31:37 +0530178};
179
Andrew Jefferyf34db312018-03-09 15:27:03 +1030180} // namespace virtual_pnor
181} // namespace openpower