xref: /openbmc/hiomapd/vpnor/partition.hpp (revision 150be912)
1 /* SPDX-License-Identifier: Apache-2.0 */
2 /* Copyright (C) 2018 IBM Corp. */
3 #pragma once
4 
5 extern "C" {
6 #include "backend.h"
7 #include "vpnor/backend.h"
8 };
9 
10 #include "vpnor/table.hpp"
11 
12 #include <fcntl.h>
13 #include <unistd.h>
14 
15 #include <filesystem>
16 #include <string>
17 
18 namespace openpower
19 {
20 namespace virtual_pnor
21 {
22 
23 namespace fs = std::filesystem;
24 
25 class Request
26 {
27   public:
28     /** @brief Construct a flash access request
29      *
30      *  @param[in] backend - The backend context used to process the request
31      *  @param[in] offset - The absolute offset into the flash device as
32      *                      provided by the mbox message associated with the
33      *                      request
34      *
35      *  The class does not take ownership of the ctx pointer. The lifetime of
36      *  the ctx pointer must strictly exceed the lifetime of the class
37      *  instance.
38      */
Request(struct backend * backend,size_t offset)39     Request(struct backend* backend, size_t offset) :
40         backend(backend), partition(((struct vpnor_data*)backend->priv)
41                                         ->vpnor->table->partition(offset)),
42         base(partition.data.base << backend->block_size_shift),
43         offset(offset - base)
44     {
45     }
46     Request(const Request&) = delete;
47     Request& operator=(const Request&) = delete;
48     Request(Request&&) = default;
49     Request& operator=(Request&&) = default;
50     ~Request() = default;
51 
52     ssize_t read(void* dst, size_t len);
53     ssize_t write(void* dst, size_t len);
54 
55   private:
56     /** @brief Clamp the access length to the maximum supported by the ToC */
57     size_t clamp(size_t len);
58 
59     /** @brief Returns the partition file path associated with the offset.
60      *
61      *  The search strategy for the partition file depends on the value of the
62      *  flags parameter.
63      *
64      *  For the O_RDONLY case:
65      *
66      *  1.  Depending on the partition type,tries to open the file
67      *      from the associated partition(RW/PRSV/RO).
68      *  1a. if file not found in the corresponding
69      *      partition(RW/PRSV/RO) then tries to read the file from
70      *      the read only partition.
71      *  1b. if the file not found in the read only partition then
72      *      throw exception.
73      *
74      * For the O_RDWR case:
75      *
76      *  1.  Depending on the partition type tries to open the file
77      *      from the associated partition.
78      *  1a. if file not found in the corresponding partition(RW/PRSV)
79      *      then copy the file from the read only partition to the (RW/PRSV)
80      *      partition depending on the partition type.
81      *  1b. if the file not found in the read only partition then throw
82      *      exception.
83      *
84      *  @param[in] flags - The flags that will be used to open the file. Must
85      *                     be one of O_RDONLY or O_RDWR.
86      *
87      *  Post-condition: The file described by the returned path exists
88      *
89      *  Throws: std::filesystem_error, std::bad_alloc
90      */
91     std::filesystem::path getPartitionFilePath(int flags);
92 
93     struct backend* backend;
94     const pnor_partition& partition;
95     size_t base;
96     size_t offset;
97 };
98 
99 } // namespace virtual_pnor
100 } // namespace openpower
101