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 | { |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 18 | namespace virtual_pnor |
| 19 | { |
| 20 | |
| 21 | namespace fs = std::experimental::filesystem; |
| 22 | |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 23 | class Request |
| 24 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 25 | public: |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 26 | /** @brief Construct a flash access request |
| 27 | * |
| 28 | * @param[in] ctx - The mbox context used to process the request |
| 29 | * @param[in] offset - The absolute offset into the flash device as |
| 30 | * provided by the mbox message associated with the |
| 31 | * request |
| 32 | * |
| 33 | * The class does not take ownership of the ctx pointer. The lifetime of |
| 34 | * the ctx pointer must strictly exceed the lifetime of the class |
| 35 | * instance. |
| 36 | */ |
| 37 | Request(struct mbox_context* ctx, size_t offset) : |
| 38 | ctx(ctx), partition(ctx->vpnor->table->partition(offset)), |
| 39 | base(partition.data.base << ctx->block_size_shift), |
| 40 | offset(offset - base) |
| 41 | { |
| 42 | } |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 43 | Request(const Request&) = delete; |
| 44 | Request& operator=(const Request&) = delete; |
| 45 | Request(Request&&) = default; |
| 46 | Request& operator=(Request&&) = default; |
| 47 | ~Request() = default; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 48 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 49 | ssize_t read(void* dst, size_t len) |
| 50 | { |
| 51 | return fulfil(dst, len, O_RDONLY); |
| 52 | } |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 53 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 54 | ssize_t write(void* dst, size_t len) |
| 55 | { |
| 56 | return fulfil(dst, len, O_RDWR); |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | /** @brief Returns the partition file path associated with the offset. |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 61 | * |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 62 | * The search strategy for the partition file depends on the value of the |
| 63 | * flags parameter. |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 64 | * |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 65 | * For the O_RDONLY case: |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 66 | * |
| 67 | * 1. Depending on the partition type,tries to open the file |
| 68 | * from the associated partition(RW/PRSV/RO). |
| 69 | * 1a. if file not found in the corresponding |
| 70 | * partition(RW/PRSV/RO) then tries to read the file from |
| 71 | * the read only partition. |
| 72 | * 1b. if the file not found in the read only partition then |
| 73 | * throw exception. |
| 74 | * |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 75 | * For the O_RDWR case: |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 76 | * |
| 77 | * 1. Depending on the partition type tries to open the file |
| 78 | * from the associated partition. |
| 79 | * 1a. if file not found in the corresponding partition(RW/PRSV) |
| 80 | * then copy the file from the read only partition to the (RW/PRSV) |
| 81 | * partition depending on the partition type. |
| 82 | * 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] | 83 | * exception. |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 84 | * |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 85 | * @param[in] flags - The flags that will be used to open the file. Must |
| 86 | * be one of O_RDONLY or O_RDWR. |
| 87 | * |
| 88 | * Post-condition: The file described by the returned path exists |
| 89 | * |
| 90 | * Throws: std::filesystem_error, std::bad_alloc |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 91 | */ |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 92 | std::experimental::filesystem::path getPartitionFilePath(int flags); |
| 93 | |
| 94 | /** @brief Fill dst with the content of the partition relative to offset. |
| 95 | * |
| 96 | * @param[in] offset - The pnor offset(bytes). |
| 97 | * @param[out] dst - The buffer to fill with partition data |
| 98 | * @param[in] len - The length of the destination buffer |
| 99 | */ |
| 100 | ssize_t fulfil(void* dst, size_t len, int flags); |
| 101 | |
| 102 | struct mbox_context* ctx; |
| 103 | const pnor_partition& partition; |
| 104 | size_t base; |
| 105 | size_t offset; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 106 | }; |
| 107 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 108 | } // namespace virtual_pnor |
| 109 | } // namespace openpower |