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