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. |
Andrew Jeffery | 71eaa73 | 2018-08-08 16:44:24 +0930 | [diff] [blame] | 3 | extern "C" { |
| 4 | #include "flash.h" |
| 5 | } |
| 6 | |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 7 | #include "pnor_partition.hpp" |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 8 | #include "pnor_partition_table.hpp" |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 9 | #include "config.h" |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 10 | #include "mboxd_pnor_partition_table.h" |
| 11 | #include "xyz/openbmc_project/Common/error.hpp" |
| 12 | #include <phosphor-logging/log.hpp> |
| 13 | #include <phosphor-logging/elog-errors.hpp> |
| 14 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 15 | #include <assert.h> |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 16 | #include <fcntl.h> |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 17 | #include <stdint.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <syslog.h> |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 20 | #include <sys/types.h> |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 21 | #include <sys/ioctl.h> |
| 22 | #include <sys/mman.h> |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 23 | #include <unistd.h> |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 24 | |
| 25 | #include "common.h" |
| 26 | |
| 27 | #include <string> |
| 28 | #include <exception> |
| 29 | #include <stdexcept> |
| 30 | #include <iostream> |
| 31 | |
| 32 | namespace openpower |
| 33 | { |
| 34 | namespace virtual_pnor |
| 35 | { |
| 36 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 37 | namespace fs = std::experimental::filesystem; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 38 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 39 | fs::path Request::getPartitionFilePath(int flags) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 40 | { |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 41 | // Check if partition exists in patch location |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 42 | auto dst = fs::path(ctx->paths.patch_loc) / partition.data.name; |
| 43 | if (fs::is_regular_file(dst)) |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 44 | { |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 45 | return dst; |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 48 | switch (partition.data.user.data[1] & |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 49 | (PARTITION_PRESERVED | PARTITION_READONLY)) |
| 50 | { |
| 51 | case PARTITION_PRESERVED: |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 52 | dst = ctx->paths.prsv_loc; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 53 | break; |
| 54 | |
| 55 | case PARTITION_READONLY: |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 56 | dst = ctx->paths.ro_loc; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 57 | break; |
| 58 | |
| 59 | default: |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 60 | dst = ctx->paths.rw_loc; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 61 | } |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 62 | dst /= partition.data.name; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 63 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 64 | if (fs::exists(dst)) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 65 | { |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 66 | return dst; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 67 | } |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 68 | |
| 69 | if (flags == O_RDONLY) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 70 | { |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 71 | dst = fs::path(ctx->paths.ro_loc) / partition.data.name; |
| 72 | assert(fs::exists(dst)); |
| 73 | return dst; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 74 | } |
| 75 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 76 | assert(flags == O_RDWR); |
| 77 | auto src = fs::path(ctx->paths.ro_loc) / partition.data.name; |
| 78 | assert(fs::exists(src)); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 79 | |
Andrew Jeffery | e8a79ff | 2018-02-27 10:56:32 +1030 | [diff] [blame] | 80 | MSG_DBG("RWRequest: Didn't find '%s' under '%s', copying from '%s'\n", |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 81 | partition.data.name, dst.c_str(), src.c_str()); |
Andrew Jeffery | e8a79ff | 2018-02-27 10:56:32 +1030 | [diff] [blame] | 82 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 83 | dst = ctx->paths.rw_loc; |
| 84 | if (partition.data.user.data[1] & PARTITION_PRESERVED) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 85 | { |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 86 | dst = ctx->paths.prsv_loc; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 87 | } |
| 88 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 89 | dst /= partition.data.name; |
| 90 | fs::copy_file(src, dst); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 91 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 92 | return dst; |
| 93 | } |
| 94 | |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 95 | size_t Request::clamp(size_t len) |
| 96 | { |
| 97 | size_t maxAccess = offset + len; |
| 98 | size_t partSize = partition.data.size << ctx->block_size_shift; |
| 99 | return std::min(maxAccess, partSize) - offset; |
| 100 | } |
| 101 | |
| 102 | void Request::resize(const fs::path &path, size_t len) |
| 103 | { |
| 104 | size_t maxAccess = offset + len; |
| 105 | size_t fileSize = fs::file_size(path); |
| 106 | if (maxAccess < fileSize) |
| 107 | { |
| 108 | return; |
| 109 | } |
| 110 | MSG_DBG("Resizing %s to %zu bytes\n", path.c_str(), maxAccess); |
| 111 | int rc = truncate(path.c_str(), maxAccess); |
| 112 | if (rc == -1) |
| 113 | { |
| 114 | MSG_ERR("Failed to resize %s: %d\n", path.c_str(), errno); |
| 115 | throw std::system_error(errno, std::system_category()); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | size_t Request::fulfil(const fs::path &path, int flags, void *buf, size_t len) |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 120 | { |
| 121 | if (!(flags == O_RDONLY || flags == O_RDWR)) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 122 | { |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 123 | std::stringstream err; |
| 124 | err << "Require O_RDONLY (0x" << std::hex << O_RDONLY << " or O_RDWR " |
| 125 | << std::hex << O_RDWR << " for flags, got: 0x" << std::hex << flags; |
| 126 | throw std::invalid_argument(err.str()); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 127 | } |
| 128 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 129 | int fd = ::open(path.c_str(), flags); |
| 130 | if (fd == -1) |
| 131 | { |
Andrew Jeffery | 974507e | 2018-04-30 15:33:55 +0930 | [diff] [blame] | 132 | MSG_ERR("Failed to open backing file at '%s': %d\n", path.c_str(), |
| 133 | errno); |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 134 | throw std::system_error(errno, std::system_category()); |
| 135 | } |
| 136 | |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 137 | size_t fileSize = fs::file_size(path); |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 138 | int mprot = PROT_READ | ((flags == O_RDWR) ? PROT_WRITE : 0); |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 139 | auto map = mmap(NULL, fileSize, mprot, MAP_SHARED, fd, 0); |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 140 | if (map == MAP_FAILED) |
| 141 | { |
| 142 | close(fd); |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 143 | MSG_ERR("Failed to map backing file '%s' for %zd bytes: %d\n", |
| 144 | path.c_str(), fileSize, errno); |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 145 | throw std::system_error(errno, std::system_category()); |
| 146 | } |
| 147 | |
| 148 | // copy to the reserved memory area |
| 149 | if (flags == O_RDONLY) |
| 150 | { |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 151 | memset(buf, 0xff, len); |
| 152 | memcpy(buf, (char *)map + offset, std::min(len, fileSize)); |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 153 | } |
| 154 | else |
| 155 | { |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 156 | memcpy((char *)map + offset, buf, len); |
Andrew Jeffery | f953f79 | 2018-08-08 16:56:03 +0930 | [diff] [blame] | 157 | flash_set_bytemap(ctx, base + offset, len, FLASH_DIRTY); |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 158 | } |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 159 | munmap(map, fileSize); |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 160 | close(fd); |
| 161 | |
| 162 | return len; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 163 | } |
| 164 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 165 | } // namespace virtual_pnor |
| 166 | } // namespace openpower |