Andrew Jeffery | 4fe996c | 2018-02-27 12:16:48 +1030 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 */ |
| 2 | /* Copyright (C) 2018 IBM Corp. */ |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 3 | #pragma once |
| 4 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame^] | 5 | extern "C" { |
| 6 | #include "mbox.h" |
| 7 | }; |
| 8 | |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 9 | #include "mboxd_pnor_partition_table.h" |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame^] | 10 | #include "pnor_partition_table.hpp" |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 11 | #include <fcntl.h> |
| 12 | #include <string> |
| 13 | #include <unistd.h> |
| 14 | #include <experimental/filesystem> |
| 15 | |
| 16 | namespace openpower |
| 17 | { |
| 18 | namespace file |
| 19 | { |
| 20 | |
| 21 | class Descriptor |
| 22 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 23 | private: |
| 24 | /** default value */ |
| 25 | int fd = -1; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 26 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 27 | public: |
| 28 | Descriptor() = default; |
| 29 | Descriptor(const Descriptor&) = delete; |
| 30 | Descriptor& operator=(const Descriptor&) = delete; |
| 31 | Descriptor(Descriptor&&) = delete; |
| 32 | Descriptor& operator=(Descriptor&&) = delete; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 33 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 34 | Descriptor(int fd) : fd(fd) |
| 35 | { |
| 36 | } |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 37 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 38 | ~Descriptor() |
| 39 | { |
| 40 | if (fd >= 0) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 41 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 42 | close(fd); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 43 | } |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 44 | } |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 45 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 46 | int operator()() const |
| 47 | { |
| 48 | return fd; |
| 49 | } |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 50 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 51 | void set(int descriptor) |
| 52 | { |
| 53 | fd = descriptor; |
| 54 | } |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 55 | }; |
| 56 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 57 | } // namespace file |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 58 | |
| 59 | namespace virtual_pnor |
| 60 | { |
| 61 | |
| 62 | namespace fs = std::experimental::filesystem; |
| 63 | |
| 64 | enum class ReturnCode : uint8_t |
| 65 | { |
| 66 | FILE_NOT_FOUND = 0, |
| 67 | PARTITION_NOT_FOUND = 1, |
| 68 | PARTITION_READ_ONLY = 2, |
| 69 | FILE_OPEN_FAILURE = 3, |
| 70 | SUCCESS = 4, |
| 71 | }; |
| 72 | |
| 73 | class Request |
| 74 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 75 | public: |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame^] | 76 | /** @brief Construct a flash access request |
| 77 | * |
| 78 | * @param[in] ctx - The mbox context used to process the request |
| 79 | * @param[in] offset - The absolute offset into the flash device as |
| 80 | * provided by the mbox message associated with the |
| 81 | * request |
| 82 | * |
| 83 | * The class does not take ownership of the ctx pointer. The lifetime of |
| 84 | * the ctx pointer must strictly exceed the lifetime of the class |
| 85 | * instance. |
| 86 | */ |
| 87 | Request(struct mbox_context* ctx, size_t offset) : |
| 88 | ctx(ctx), partition(ctx->vpnor->table->partition(offset)), |
| 89 | base(partition.data.base << ctx->block_size_shift), |
| 90 | offset(offset - base) |
| 91 | { |
| 92 | } |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 93 | Request(const Request&) = delete; |
| 94 | Request& operator=(const Request&) = delete; |
| 95 | Request(Request&&) = default; |
| 96 | Request& operator=(Request&&) = default; |
| 97 | ~Request() = default; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 98 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame^] | 99 | ssize_t read(void* dst, size_t len) |
| 100 | { |
| 101 | return fulfil(dst, len, O_RDONLY); |
| 102 | } |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 103 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame^] | 104 | ssize_t write(void* dst, size_t len) |
| 105 | { |
| 106 | return fulfil(dst, len, O_RDWR); |
| 107 | } |
| 108 | |
| 109 | private: |
| 110 | /** @brief Returns the partition file path associated with the offset. |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 111 | * |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame^] | 112 | * The search strategy for the partition file depends on the value of the |
| 113 | * flags parameter. |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 114 | * |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame^] | 115 | * For the O_RDONLY case: |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 116 | * |
| 117 | * 1. Depending on the partition type,tries to open the file |
| 118 | * from the associated partition(RW/PRSV/RO). |
| 119 | * 1a. if file not found in the corresponding |
| 120 | * partition(RW/PRSV/RO) then tries to read the file from |
| 121 | * the read only partition. |
| 122 | * 1b. if the file not found in the read only partition then |
| 123 | * throw exception. |
| 124 | * |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame^] | 125 | * For the O_RDWR case: |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 126 | * |
| 127 | * 1. Depending on the partition type tries to open the file |
| 128 | * from the associated partition. |
| 129 | * 1a. if file not found in the corresponding partition(RW/PRSV) |
| 130 | * then copy the file from the read only partition to the (RW/PRSV) |
| 131 | * partition depending on the partition type. |
| 132 | * 1b. if the file not found in the read only partition then throw |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame^] | 133 | * exception. |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 134 | * |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame^] | 135 | * @param[in] flags - The flags that will be used to open the file. Must |
| 136 | * be one of O_RDONLY or O_RDWR. |
| 137 | * |
| 138 | * Post-condition: The file described by the returned path exists |
| 139 | * |
| 140 | * Throws: std::filesystem_error, std::bad_alloc |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 141 | */ |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame^] | 142 | std::experimental::filesystem::path getPartitionFilePath(int flags); |
| 143 | |
| 144 | /** @brief Fill dst with the content of the partition relative to offset. |
| 145 | * |
| 146 | * @param[in] offset - The pnor offset(bytes). |
| 147 | * @param[out] dst - The buffer to fill with partition data |
| 148 | * @param[in] len - The length of the destination buffer |
| 149 | */ |
| 150 | ssize_t fulfil(void* dst, size_t len, int flags); |
| 151 | |
| 152 | struct mbox_context* ctx; |
| 153 | const pnor_partition& partition; |
| 154 | size_t base; |
| 155 | size_t offset; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 156 | }; |
| 157 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 158 | } // namespace virtual_pnor |
| 159 | } // namespace openpower |