blob: 242da37c33e312b80283c380370338c11f00d732 [file] [log] [blame]
Ratan Guptac0ef9872017-06-06 14:31:37 +05301#pragma once
2
3#include "mboxd_pnor_partition_table.h"
4#include <fcntl.h>
5#include <string>
6#include <unistd.h>
7#include <experimental/filesystem>
8
9namespace openpower
10{
11namespace file
12{
13
14class Descriptor
15{
Andrew Jefferyf34db312018-03-09 15:27:03 +103016 private:
17 /** default value */
18 int fd = -1;
Ratan Guptac0ef9872017-06-06 14:31:37 +053019
Andrew Jefferyf34db312018-03-09 15:27:03 +103020 public:
21 Descriptor() = default;
22 Descriptor(const Descriptor&) = delete;
23 Descriptor& operator=(const Descriptor&) = delete;
24 Descriptor(Descriptor&&) = delete;
25 Descriptor& operator=(Descriptor&&) = delete;
Ratan Guptac0ef9872017-06-06 14:31:37 +053026
Andrew Jefferyf34db312018-03-09 15:27:03 +103027 Descriptor(int fd) : fd(fd)
28 {
29 }
Ratan Guptac0ef9872017-06-06 14:31:37 +053030
Andrew Jefferyf34db312018-03-09 15:27:03 +103031 ~Descriptor()
32 {
33 if (fd >= 0)
Ratan Guptac0ef9872017-06-06 14:31:37 +053034 {
Andrew Jefferyf34db312018-03-09 15:27:03 +103035 close(fd);
Ratan Guptac0ef9872017-06-06 14:31:37 +053036 }
Andrew Jefferyf34db312018-03-09 15:27:03 +103037 }
Ratan Guptac0ef9872017-06-06 14:31:37 +053038
Andrew Jefferyf34db312018-03-09 15:27:03 +103039 int operator()() const
40 {
41 return fd;
42 }
Ratan Guptac0ef9872017-06-06 14:31:37 +053043
Andrew Jefferyf34db312018-03-09 15:27:03 +103044 void set(int descriptor)
45 {
46 fd = descriptor;
47 }
Ratan Guptac0ef9872017-06-06 14:31:37 +053048};
49
Andrew Jefferyf34db312018-03-09 15:27:03 +103050} // namespace file
Ratan Guptac0ef9872017-06-06 14:31:37 +053051
52namespace virtual_pnor
53{
54
55namespace fs = std::experimental::filesystem;
56
57enum class ReturnCode : uint8_t
58{
59 FILE_NOT_FOUND = 0,
60 PARTITION_NOT_FOUND = 1,
61 PARTITION_READ_ONLY = 2,
62 FILE_OPEN_FAILURE = 3,
63 SUCCESS = 4,
64};
65
66class Request
67{
Andrew Jefferyf34db312018-03-09 15:27:03 +103068 public:
69 Request() = default;
70 Request(const Request&) = delete;
71 Request& operator=(const Request&) = delete;
72 Request(Request&&) = default;
73 Request& operator=(Request&&) = default;
74 ~Request() = default;
Ratan Guptac0ef9872017-06-06 14:31:37 +053075
Andrew Jefferyf34db312018-03-09 15:27:03 +103076 openpower::file::Descriptor fd;
Ratan Guptac0ef9872017-06-06 14:31:37 +053077
Andrew Jefferyf34db312018-03-09 15:27:03 +103078 protected:
79 /** @brief opens the partition file
80 *
81 * @param[in] filePath - Absolute file path.
82 * @param[in] mode - File open mode.
83 */
84 ReturnCode open(const std::string& filePath, int mode);
Ratan Guptac0ef9872017-06-06 14:31:37 +053085
Andrew Jefferyf34db312018-03-09 15:27:03 +103086 /** @brief returns the partition file path associated with the offset.
87 *
88 * @param[in] context - The mbox context pointer.
89 * @param[in] offset - The pnor offset(bytes).
90 */
Ratan Guptac0ef9872017-06-06 14:31:37 +053091
Andrew Jefferyf34db312018-03-09 15:27:03 +103092 std::string getPartitionFilePath(struct mbox_context* context,
93 uint32_t offset);
Ratan Guptac0ef9872017-06-06 14:31:37 +053094
Andrew Jefferyf34db312018-03-09 15:27:03 +103095 const pnor_partition* partition = nullptr;
Ratan Guptac0ef9872017-06-06 14:31:37 +053096};
97
98/** @class RORequest
99 * @brief Represent the read request of the partition.
100 * Stores the partition meta data.
101 */
102class RORequest : public Request
103{
Andrew Jefferyf34db312018-03-09 15:27:03 +1030104 public:
105 RORequest() = default;
106 RORequest(const RORequest&) = delete;
107 RORequest& operator=(const RORequest&) = delete;
108 RORequest(RORequest&&) = default;
109 RORequest& operator=(RORequest&&) = default;
110 ~RORequest(){};
Ratan Guptac0ef9872017-06-06 14:31:37 +0530111
Andrew Jefferyf34db312018-03-09 15:27:03 +1030112 /** @brief opens the partition file associated with the offset
113 * in read only mode and gets the partition details.
114 *
115 * 1. Depending on the partition type,tries to open the file
116 * from the associated partition(RW/PRSV/RO).
117 * 1a. if file not found in the corresponding
118 * partition(RW/PRSV/RO) then tries to read the file from
119 * the read only partition.
120 * 1b. if the file not found in the read only partition then
121 * throw exception.
122 *
123 * @param[in] context - The mbox context pointer.
124 * @param[in] offset - The pnor offset(bytes).
125 */
126 const pnor_partition* getPartitionInfo(struct mbox_context* context,
127 uint32_t offset);
Ratan Guptac0ef9872017-06-06 14:31:37 +0530128};
129
130/** @class RWRequest
131 * @brief Represent the write request of the partition.
132 * Stores the partition meta data.
133 */
134class RWRequest : public Request
135{
Andrew Jefferyf34db312018-03-09 15:27:03 +1030136 public:
137 RWRequest() = default;
138 RWRequest(const RWRequest&) = delete;
139 RWRequest& operator=(const RWRequest&) = delete;
140 RWRequest(RWRequest&&) = default;
141 RWRequest& operator=(RWRequest&&) = default;
142 ~RWRequest(){};
Ratan Guptac0ef9872017-06-06 14:31:37 +0530143
Andrew Jefferyf34db312018-03-09 15:27:03 +1030144 /** @brief opens the partition file associated with the offset
145 * in write mode and gets the parttition details.
146 *
147 * 1. Depending on the partition type tries to open the file
148 * from the associated partition.
149 * 1a. if file not found in the corresponding partition(RW/PRSV)
150 * then copy the file from the read only partition to the (RW/PRSV)
151 * partition depending on the partition type.
152 * 1b. if the file not found in the read only partition then throw
153 * exception.
154 *
155 * @param[in] context - The mbox context pointer.
156 * @param[in] offset - The pnor offset(bytes).
157 */
158 const pnor_partition* getPartitionInfo(struct mbox_context* context,
159 uint32_t offset);
Ratan Guptac0ef9872017-06-06 14:31:37 +0530160};
161
Andrew Jefferyf34db312018-03-09 15:27:03 +1030162} // namespace virtual_pnor
163} // namespace openpower