Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 1 | /* |
| 2 | * MBox Daemon Test File |
| 3 | * |
| 4 | * Copyright 2017 IBM |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | */ |
| 19 | |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 20 | extern "C" { |
Andrew Jeffery | 8598591 | 2018-02-22 10:20:31 +1030 | [diff] [blame] | 21 | #include "config.h" |
| 22 | #include "common.h" |
| 23 | #include "mboxd_flash.h" |
| 24 | #include "mboxd_pnor_partition_table.h" |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 25 | #include "mbox.h" |
| 26 | #include "test/tmpf.h" |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | #include <assert.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #include <fstream> |
| 33 | #include <experimental/filesystem> |
| 34 | |
| 35 | #include <sys/mman.h> |
| 36 | #include <sys/syslog.h> |
| 37 | #include <sys/ioctl.h> |
| 38 | #include <fcntl.h> |
| 39 | |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame^] | 40 | #include "test/vpnor/tmpd.hpp" |
| 41 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 42 | uint8_t data[8] = {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7}; |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 43 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 44 | #define BLOCK_SIZE 4096 |
| 45 | #define OFFSET BLOCK_SIZE |
| 46 | #define MEM_SIZE (BLOCK_SIZE * 2) |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 47 | #define DATA_SIZE sizeof(data) |
| 48 | #define ERASE_SIZE BLOCK_SIZE |
| 49 | #define BLOCK_SIZE_SHIFT 12 |
| 50 | |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame^] | 51 | const std::string toc[] = { |
| 52 | "partition01=TEST1,00001000,00001400,ECC,READONLY", |
| 53 | "partition02=TEST2,00002000,00002008,ECC,READWRITE", |
| 54 | "partition03=TEST3,00003000,00003400,ECC,PRESERVED", |
| 55 | }; |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 56 | |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame^] | 57 | std::vector<std::string> partitions = {"TEST1", "TEST2", "TEST3"}; |
| 58 | |
| 59 | namespace test = openpower::virtual_pnor::test; |
| 60 | |
| 61 | void init(struct mbox_context* ctx, test::VpnorRoot& root) |
| 62 | { |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 63 | namespace fs = std::experimental::filesystem; |
| 64 | using namespace std::string_literals; |
| 65 | |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 66 | // create the partition files in the ro directory |
| 67 | for (auto partition : partitions) |
| 68 | { |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame^] | 69 | root.write(partition, data, sizeof(data)); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // copy partition2 file from ro to rw |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame^] | 73 | assert(fs::copy_file(root.ro() / "TEST2", root.rw() / "TEST2")); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 74 | |
| 75 | mbox_vlog = &mbox_log_console; |
| 76 | verbosity = (verbose)2; |
| 77 | |
| 78 | // setting context parameters |
| 79 | ctx->erase_size_shift = BLOCK_SIZE_SHIFT; |
| 80 | ctx->block_size_shift = BLOCK_SIZE_SHIFT; |
| 81 | ctx->flash_bmap = reinterpret_cast<uint8_t*>( |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 82 | calloc(MEM_SIZE >> ctx->erase_size_shift, sizeof(*ctx->flash_bmap))); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 83 | |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame^] | 84 | strcpy(ctx->paths.ro_loc, root.ro().c_str()); |
| 85 | strcpy(ctx->paths.rw_loc, root.rw().c_str()); |
| 86 | strcpy(ctx->paths.prsv_loc, root.prsv().c_str()); |
| 87 | strcpy(ctx->paths.patch_loc, root.patch().c_str()); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 88 | } |
| 89 | |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 90 | int main(void) |
| 91 | { |
| 92 | namespace fs = std::experimental::filesystem; |
| 93 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 94 | int rc{}; |
| 95 | char src[DATA_SIZE]{0}; |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 96 | struct mbox_context context; |
| 97 | struct mbox_context* ctx = &context; |
| 98 | memset(ctx, 0, sizeof(mbox_context)); |
| 99 | |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame^] | 100 | test::VpnorRoot root(toc, BLOCK_SIZE); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 101 | |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame^] | 102 | // Initialize the context before running the test case. |
| 103 | init(ctx, root); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 104 | |
| 105 | // create the partition table |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame^] | 106 | vpnor_create_partition_table_from_path(ctx, root.ro().c_str()); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 107 | |
| 108 | // Write to psrv partition |
| 109 | |
| 110 | // As file doesn't exist there, so it copies |
| 111 | // the file from RO to PRSV and write the file in PRSV partition. |
| 112 | |
| 113 | memset(src, 0xaa, sizeof(src)); |
| 114 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 115 | rc = write_flash(ctx, (OFFSET * 3), src, sizeof(src)); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 116 | assert(rc == 0); |
| 117 | |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame^] | 118 | auto fd = open((root.prsv() / "TEST3").c_str(), O_RDONLY); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 119 | auto map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0); |
| 120 | assert(map != MAP_FAILED); |
| 121 | |
| 122 | // verify it is written |
| 123 | rc = memcmp(src, map, sizeof(src)); |
| 124 | assert(rc == 0); |
| 125 | munmap(map, MEM_SIZE); |
| 126 | close(fd); |
| 127 | |
| 128 | // Write to the RO partition |
| 129 | memset(src, 0x55, sizeof(src)); |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame^] | 130 | fd = open((root.ro() / "TEST1").c_str(), O_RDONLY); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 131 | map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0); |
| 132 | assert(map != MAP_FAILED); |
| 133 | rc = write_flash(ctx, (OFFSET), src, sizeof(src)); |
| 134 | // Should not be allowed to write on RO |
| 135 | assert(rc != 0); |
| 136 | |
| 137 | munmap(map, MEM_SIZE); |
| 138 | close(fd); |
| 139 | |
| 140 | // Write to the RW partition |
| 141 | memset(src, 0xbb, sizeof(src)); |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame^] | 142 | fd = open((root.rw() / "TEST2").c_str(), O_RDONLY); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 143 | map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0); |
| 144 | assert(map != MAP_FAILED); |
| 145 | rc = write_flash(ctx, (OFFSET * 2), src, sizeof(src)); |
| 146 | assert(rc == 0); |
| 147 | rc = memcmp(src, map, sizeof(src)); |
| 148 | assert(rc == 0); |
| 149 | |
| 150 | // write beyond the partition length as the partition |
| 151 | // file length is 8 byte(TEST2). |
| 152 | rc = write_flash(ctx, (OFFSET * 2 + 3), src, sizeof(src) + 20); |
| 153 | assert(rc == -1); |
| 154 | |
| 155 | memset(src, 0xcc, sizeof(src)); |
| 156 | rc = write_flash(ctx, (OFFSET * 2), src, sizeof(src)); |
| 157 | assert(rc == 0); |
| 158 | rc = memcmp(src, map, sizeof(src)); |
| 159 | assert(rc == 0); |
| 160 | |
| 161 | src[0] = 0xff; |
| 162 | rc = write_flash(ctx, (OFFSET * 2), src, 1); |
| 163 | assert(rc == 0); |
| 164 | rc = memcmp(src, map, sizeof(src)); |
| 165 | assert(rc == 0); |
| 166 | |
| 167 | src[1] = 0xff; |
| 168 | rc = write_flash(ctx, (OFFSET * 2) + 1, &src[1], 1); |
| 169 | assert(rc == 0); |
| 170 | rc = memcmp(src, map, sizeof(src)); |
| 171 | assert(rc == 0); |
| 172 | |
| 173 | src[2] = 0xff; |
| 174 | rc = write_flash(ctx, (OFFSET * 2) + 2, &src[2], 1); |
| 175 | assert(rc == 0); |
| 176 | rc = memcmp(src, map, sizeof(src)); |
| 177 | assert(rc == 0); |
| 178 | |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 179 | munmap(map, MEM_SIZE); |
| 180 | close(fd); |
| 181 | |
| 182 | // START Test patch location - Patch dir has preference over other locations |
| 183 | // Copy partition2 file from ro to patch to simulate a patch file that is |
| 184 | // different from the one in rw (partition2 in rw was modified with the |
| 185 | // previous write test) |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame^] | 186 | fs::path patch = root.patch() / "TEST2"; |
| 187 | assert(fs::copy_file(root.ro() / "TEST2", patch)); |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 188 | |
| 189 | // Write arbitrary data |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 190 | char srcPatch[DATA_SIZE]{0}; |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 191 | memset(srcPatch, 0x33, sizeof(srcPatch)); |
| 192 | rc = write_flash(ctx, (OFFSET * 2), srcPatch, sizeof(srcPatch)); |
| 193 | assert(rc == 0); |
| 194 | |
| 195 | // Check that partition file in RW location still contains the original data |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame^] | 196 | fd = open((root.rw() / "TEST2").c_str(), O_RDONLY); |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 197 | map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0); |
| 198 | assert(map != MAP_FAILED); |
| 199 | rc = memcmp(src, map, sizeof(src)); |
| 200 | assert(rc == 0); |
| 201 | munmap(map, MEM_SIZE); |
| 202 | close(fd); |
| 203 | |
| 204 | // Check that partition file in PATCH location was written with the new data |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame^] | 205 | fd = open(patch.c_str(), O_RDONLY); |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 206 | map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0); |
| 207 | assert(map != MAP_FAILED); |
| 208 | rc = memcmp(srcPatch, map, sizeof(srcPatch)); |
| 209 | assert(rc == 0); |
| 210 | munmap(map, MEM_SIZE); |
| 211 | close(fd); |
| 212 | |
Andrew Jeffery | cafb002 | 2018-02-21 11:14:39 +1030 | [diff] [blame] | 213 | destroy_vpnor(ctx); |
| 214 | free(ctx->flash_bmap); |
| 215 | |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 216 | return rc; |
| 217 | } |