| 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" { | 
| Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 6 | #include "backend.h" | 
|  | 7 | #include "mboxd_pnor_partition_table.h" | 
| Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 8 | }; | 
|  | 9 |  | 
| Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 10 | #include "pnor_partition_table.hpp" | 
| William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 11 |  | 
| Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 12 | #include <fcntl.h> | 
| Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 13 | #include <unistd.h> | 
| William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 14 |  | 
| Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 15 | #include <experimental/filesystem> | 
| William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 16 | #include <string> | 
|  | 17 |  | 
| Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 18 | namespace openpower | 
|  | 19 | { | 
| Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 20 | namespace virtual_pnor | 
|  | 21 | { | 
|  | 22 |  | 
|  | 23 | namespace fs = std::experimental::filesystem; | 
|  | 24 |  | 
| Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 25 | class Request | 
|  | 26 | { | 
| Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 27 | public: | 
| Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 28 | /** @brief Construct a flash access request | 
|  | 29 | * | 
| Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 30 | *  @param[in] backend - The backend context used to process the request | 
| Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 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 | */ | 
| Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 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), | 
| Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 43 | offset(offset - base) | 
|  | 44 | { | 
|  | 45 | } | 
| Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 46 | Request(const Request&) = delete; | 
|  | 47 | Request& operator=(const Request&) = delete; | 
|  | 48 | Request(Request&&) = default; | 
|  | 49 | Request& operator=(Request&&) = default; | 
|  | 50 | ~Request() = default; | 
| Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 51 |  | 
| Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 52 | ssize_t read(void* dst, size_t len) | 
|  | 53 | { | 
| Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 54 | len = clamp(len); | 
|  | 55 | constexpr auto flags = O_RDONLY; | 
|  | 56 | fs::path path = getPartitionFilePath(flags); | 
|  | 57 | return fulfil(path, flags, dst, len); | 
| Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 58 | } | 
| Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 59 |  | 
| Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 60 | ssize_t write(void* dst, size_t len) | 
|  | 61 | { | 
| Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 62 | if (len != clamp(len)) | 
|  | 63 | { | 
|  | 64 | std::stringstream err; | 
|  | 65 | err << "Request size 0x" << std::hex << len << " from offset 0x" | 
|  | 66 | << std::hex << offset << " exceeds the partition size 0x" | 
| Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 67 | << std::hex | 
|  | 68 | << (partition.data.size << backend->block_size_shift); | 
| Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 69 | throw OutOfBoundsOffset(err.str()); | 
|  | 70 | } | 
|  | 71 | constexpr auto flags = O_RDWR; | 
|  | 72 | /* Ensure file is at least the size of the maximum access */ | 
|  | 73 | fs::path path = getPartitionFilePath(flags); | 
|  | 74 | resize(path, len); | 
|  | 75 | return fulfil(path, flags, dst, len); | 
| Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 76 | } | 
|  | 77 |  | 
|  | 78 | private: | 
| Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 79 | /** @brief Clamp the access length to the maximum supported by the ToC */ | 
|  | 80 | size_t clamp(size_t len); | 
|  | 81 |  | 
|  | 82 | /** @brief Ensure the backing file is sized appropriately for the access | 
|  | 83 | * | 
|  | 84 | *  We need to ensure the file is big enough to satisfy the request so that | 
|  | 85 | *  mmap() will succeed for the required size. | 
|  | 86 | * | 
|  | 87 | *  @return The valid access length | 
|  | 88 | * | 
|  | 89 | *  Throws: std::system_error | 
|  | 90 | */ | 
|  | 91 | void resize(const std::experimental::filesystem::path& path, size_t len); | 
|  | 92 |  | 
| Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 93 | /** @brief Returns the partition file path associated with the offset. | 
| Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 94 | * | 
| Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 95 | *  The search strategy for the partition file depends on the value of the | 
|  | 96 | *  flags parameter. | 
| Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 97 | * | 
| Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 98 | *  For the O_RDONLY case: | 
| Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 99 | * | 
|  | 100 | *  1.  Depending on the partition type,tries to open the file | 
|  | 101 | *      from the associated partition(RW/PRSV/RO). | 
|  | 102 | *  1a. if file not found in the corresponding | 
|  | 103 | *      partition(RW/PRSV/RO) then tries to read the file from | 
|  | 104 | *      the read only partition. | 
|  | 105 | *  1b. if the file not found in the read only partition then | 
|  | 106 | *      throw exception. | 
|  | 107 | * | 
| Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 108 | * For the O_RDWR case: | 
| Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 109 | * | 
|  | 110 | *  1.  Depending on the partition type tries to open the file | 
|  | 111 | *      from the associated partition. | 
|  | 112 | *  1a. if file not found in the corresponding partition(RW/PRSV) | 
|  | 113 | *      then copy the file from the read only partition to the (RW/PRSV) | 
|  | 114 | *      partition depending on the partition type. | 
|  | 115 | *  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] | 116 | *      exception. | 
| Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 117 | * | 
| Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 118 | *  @param[in] flags - The flags that will be used to open the file. Must | 
|  | 119 | *                     be one of O_RDONLY or O_RDWR. | 
|  | 120 | * | 
|  | 121 | *  Post-condition: The file described by the returned path exists | 
|  | 122 | * | 
|  | 123 | *  Throws: std::filesystem_error, std::bad_alloc | 
| Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 124 | */ | 
| Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 125 | std::experimental::filesystem::path getPartitionFilePath(int flags); | 
|  | 126 |  | 
|  | 127 | /** @brief Fill dst with the content of the partition relative to offset. | 
|  | 128 | * | 
|  | 129 | *  @param[in] offset - The pnor offset(bytes). | 
|  | 130 | *  @param[out] dst - The buffer to fill with partition data | 
|  | 131 | *  @param[in] len - The length of the destination buffer | 
|  | 132 | */ | 
| Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 133 | size_t fulfil(const std::experimental::filesystem::path& path, int flags, | 
|  | 134 | void* dst, size_t len); | 
| Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 135 |  | 
| Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 136 | struct backend* backend; | 
| Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 137 | const pnor_partition& partition; | 
|  | 138 | size_t base; | 
|  | 139 | size_t offset; | 
| Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 140 | }; | 
|  | 141 |  | 
| Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 142 | } // namespace virtual_pnor | 
|  | 143 | } // namespace openpower |