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> |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 13 | #include <stdint.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <syslog.h> |
| 16 | #include <sys/ioctl.h> |
| 17 | #include <sys/mman.h> |
| 18 | |
| 19 | #include "common.h" |
| 20 | |
| 21 | #include <string> |
| 22 | #include <exception> |
| 23 | #include <stdexcept> |
| 24 | #include <iostream> |
| 25 | |
| 26 | namespace openpower |
| 27 | { |
| 28 | namespace virtual_pnor |
| 29 | { |
| 30 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 31 | namespace fs = std::experimental::filesystem; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 32 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 33 | fs::path Request::getPartitionFilePath(int flags) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 34 | { |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 35 | // Check if partition exists in patch location |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 36 | auto dst = fs::path(ctx->paths.patch_loc) / partition.data.name; |
| 37 | if (fs::is_regular_file(dst)) |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 38 | { |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 39 | return dst; |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 40 | } |
| 41 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 42 | switch (partition.data.user.data[1] & |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 43 | (PARTITION_PRESERVED | PARTITION_READONLY)) |
| 44 | { |
| 45 | case PARTITION_PRESERVED: |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 46 | dst = ctx->paths.prsv_loc; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 47 | break; |
| 48 | |
| 49 | case PARTITION_READONLY: |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 50 | dst = ctx->paths.ro_loc; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 51 | break; |
| 52 | |
| 53 | default: |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 54 | dst = ctx->paths.rw_loc; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 55 | } |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 56 | dst /= partition.data.name; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 57 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 58 | if (fs::exists(dst)) |
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 | return dst; |
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 | |
| 63 | if (flags == O_RDONLY) |
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 | dst = fs::path(ctx->paths.ro_loc) / partition.data.name; |
| 66 | assert(fs::exists(dst)); |
| 67 | return dst; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 68 | } |
| 69 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 70 | assert(flags == O_RDWR); |
| 71 | auto src = fs::path(ctx->paths.ro_loc) / partition.data.name; |
| 72 | assert(fs::exists(src)); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 73 | |
Andrew Jeffery | e8a79ff | 2018-02-27 10:56:32 +1030 | [diff] [blame] | 74 | 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] | 75 | partition.data.name, dst.c_str(), src.c_str()); |
Andrew Jeffery | e8a79ff | 2018-02-27 10:56:32 +1030 | [diff] [blame] | 76 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 77 | dst = ctx->paths.rw_loc; |
| 78 | if (partition.data.user.data[1] & PARTITION_PRESERVED) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 79 | { |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 80 | dst = ctx->paths.prsv_loc; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 81 | } |
| 82 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 83 | dst /= partition.data.name; |
| 84 | fs::copy_file(src, dst); |
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 | return dst; |
| 87 | } |
| 88 | |
| 89 | ssize_t Request::fulfil(void *buf, size_t len, int flags) |
| 90 | { |
| 91 | if (!(flags == O_RDONLY || flags == O_RDWR)) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 92 | { |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 93 | std::stringstream err; |
| 94 | err << "Require O_RDONLY (0x" << std::hex << O_RDONLY << " or O_RDWR " |
| 95 | << std::hex << O_RDWR << " for flags, got: 0x" << std::hex << flags; |
| 96 | throw std::invalid_argument(err.str()); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 97 | } |
| 98 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 99 | fs::path path = getPartitionFilePath(flags); |
| 100 | |
| 101 | int fd = ::open(path.c_str(), flags); |
| 102 | if (fd == -1) |
| 103 | { |
| 104 | MSG_ERR("Failed to open backing file at '%s': %d\n", |
| 105 | path.c_str(), errno); |
| 106 | throw std::system_error(errno, std::system_category()); |
| 107 | } |
| 108 | |
| 109 | int mprot = PROT_READ | ((flags == O_RDWR) ? PROT_WRITE : 0); |
| 110 | auto map = mmap(NULL, partition.data.actual, mprot, MAP_SHARED, fd, 0); |
| 111 | if (map == MAP_FAILED) |
| 112 | { |
| 113 | close(fd); |
| 114 | MSG_ERR("Failed to map backing file '%s' for %d bytes: %d\n", |
| 115 | path.c_str(), partition.data.actual, errno); |
| 116 | throw std::system_error(errno, std::system_category()); |
| 117 | } |
| 118 | |
| 119 | // copy to the reserved memory area |
| 120 | if (flags == O_RDONLY) |
| 121 | { |
| 122 | len = std::min(partition.data.actual - offset, len); |
| 123 | memcpy(buf, (char *)map + offset, len); |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | // if the asked offset + no of bytes to read is greater |
| 128 | // then size of the partition file then throw error. |
| 129 | // |
| 130 | // FIXME: Don't use .actual, use (.size << ctx->block_size_shift), |
| 131 | // otherwise we can't grow the size of the data to fill the partition |
| 132 | if ((base + offset + len) > (base + partition.data.actual)) |
| 133 | { |
| 134 | munmap(map, partition.data.actual); |
| 135 | close(fd); |
| 136 | |
| 137 | /* FIXME: offset calculation */ |
| 138 | std::stringstream err; |
| 139 | err << "Request size 0x" << std::hex << len << " from offset 0x" |
| 140 | << std::hex << offset << " exceeds the partition size 0x" |
| 141 | << std::hex << partition.data.actual; |
| 142 | throw OutOfBoundsOffset(err.str()); |
| 143 | } |
| 144 | memcpy((char *)map + offset, buf, len); |
| 145 | set_flash_bytemap(ctx, base + offset, len, FLASH_DIRTY); |
| 146 | } |
| 147 | munmap(map, partition.data.actual); |
| 148 | close(fd); |
| 149 | |
| 150 | return len; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 151 | } |
| 152 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 153 | } // namespace virtual_pnor |
| 154 | } // namespace openpower |