summaryrefslogtreecommitdiffstats
path: root/pnor_partition.hpp
blob: a11a921c75d696f58848842d316939b9eb6800a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* SPDX-License-Identifier: Apache-2.0 */
/* Copyright (C) 2018 IBM Corp. */
#pragma once

#include "mboxd_pnor_partition_table.h"
#include <fcntl.h>
#include <string>
#include <unistd.h>
#include <experimental/filesystem>

namespace openpower
{
namespace file
{

class Descriptor
{
  private:
    /** default value */
    int fd = -1;

  public:
    Descriptor() = default;
    Descriptor(const Descriptor&) = delete;
    Descriptor& operator=(const Descriptor&) = delete;
    Descriptor(Descriptor&&) = delete;
    Descriptor& operator=(Descriptor&&) = delete;

    Descriptor(int fd) : fd(fd)
    {
    }

    ~Descriptor()
    {
        if (fd >= 0)
        {
            close(fd);
        }
    }

    int operator()() const
    {
        return fd;
    }

    void set(int descriptor)
    {
        fd = descriptor;
    }
};

} // namespace file

namespace virtual_pnor
{

namespace fs = std::experimental::filesystem;

enum class ReturnCode : uint8_t
{
    FILE_NOT_FOUND = 0,
    PARTITION_NOT_FOUND = 1,
    PARTITION_READ_ONLY = 2,
    FILE_OPEN_FAILURE = 3,
    SUCCESS = 4,
};

class Request
{
  public:
    Request() = default;
    Request(const Request&) = delete;
    Request& operator=(const Request&) = delete;
    Request(Request&&) = default;
    Request& operator=(Request&&) = default;
    ~Request() = default;

    openpower::file::Descriptor fd;

  protected:
    /** @brief opens the partition file
     *
     *  @param[in] filePath - Absolute file path.
     *  @param[in] mode - File open mode.
     */
    ReturnCode open(const std::string& filePath, int mode);

    /** @brief returns the partition file path associated with the offset.
     *
     *  @param[in] context - The mbox context pointer.
     *  @param[in] offset - The pnor offset(bytes).
     */

    std::string getPartitionFilePath(struct mbox_context* context,
                                     uint32_t offset);

    const pnor_partition* partition = nullptr;
};

/** @class RORequest
 *  @brief Represent the read request of the partition.
 *         Stores the partition meta data.
 */
class RORequest : public Request
{
  public:
    RORequest() = default;
    RORequest(const RORequest&) = delete;
    RORequest& operator=(const RORequest&) = delete;
    RORequest(RORequest&&) = default;
    RORequest& operator=(RORequest&&) = default;
    ~RORequest(){};

    /** @brief opens the partition file associated with the offset
     *         in read only mode and gets the partition details.
     *
     *  1.  Depending on the partition type,tries to open the file
     *      from the associated partition(RW/PRSV/RO).
     *  1a. if file not found in the corresponding
     *      partition(RW/PRSV/RO) then tries to read the file from
     *      the read only partition.
     *  1b. if the file not found in the read only partition then
     *      throw exception.
     *
     *  @param[in] context - The mbox context pointer.
     *  @param[in] offset - The pnor offset(bytes).
     */
    const pnor_partition* getPartitionInfo(struct mbox_context* context,
                                           uint32_t offset);
};

/** @class RWRequest
 *  @brief Represent the write request of the partition.
 *         Stores the partition meta data.
 */
class RWRequest : public Request
{
  public:
    RWRequest() = default;
    RWRequest(const RWRequest&) = delete;
    RWRequest& operator=(const RWRequest&) = delete;
    RWRequest(RWRequest&&) = default;
    RWRequest& operator=(RWRequest&&) = default;
    ~RWRequest(){};

    /** @brief opens the partition file associated with the offset
     *         in write mode and gets the parttition details.
     *
     *  1.  Depending on the partition type tries to open the file
     *      from the associated partition.
     *  1a. if file not found in the corresponding partition(RW/PRSV)
     *      then copy the file from the read only partition to the (RW/PRSV)
     *      partition depending on the partition type.
     *  1b. if the file not found in the read only partition then throw
     * exception.
     *
     *  @param[in] context - The mbox context pointer.
     *  @param[in] offset - The pnor offset(bytes).
     */
    const pnor_partition* getPartitionInfo(struct mbox_context* context,
                                           uint32_t offset);
};

} // namespace virtual_pnor
} // namespace openpower
OpenPOWER on IntegriCloud