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) |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 48 | #define BLOCK_SIZE_SHIFT 12 |
| 49 | |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame] | 50 | const std::string toc[] = { |
| 51 | "partition01=TEST1,00001000,00001400,ECC,READONLY", |
| 52 | "partition02=TEST2,00002000,00002008,ECC,READWRITE", |
| 53 | "partition03=TEST3,00003000,00003400,ECC,PRESERVED", |
| 54 | }; |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 55 | |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame] | 56 | std::vector<std::string> partitions = {"TEST1", "TEST2", "TEST3"}; |
| 57 | |
| 58 | namespace test = openpower::virtual_pnor::test; |
| 59 | |
| 60 | void init(struct mbox_context* ctx, test::VpnorRoot& root) |
| 61 | { |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 62 | namespace fs = std::experimental::filesystem; |
| 63 | using namespace std::string_literals; |
| 64 | |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 65 | // create the partition files in the ro directory |
| 66 | for (auto partition : partitions) |
| 67 | { |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame] | 68 | root.write(partition, data, sizeof(data)); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // copy partition2 file from ro to rw |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame] | 72 | assert(fs::copy_file(root.ro() / "TEST2", root.rw() / "TEST2")); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 73 | |
| 74 | mbox_vlog = &mbox_log_console; |
| 75 | verbosity = (verbose)2; |
| 76 | |
| 77 | // setting context parameters |
| 78 | ctx->erase_size_shift = BLOCK_SIZE_SHIFT; |
| 79 | ctx->block_size_shift = BLOCK_SIZE_SHIFT; |
| 80 | ctx->flash_bmap = reinterpret_cast<uint8_t*>( |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 81 | calloc(MEM_SIZE >> ctx->erase_size_shift, sizeof(*ctx->flash_bmap))); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 82 | } |
| 83 | |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 84 | int main(void) |
| 85 | { |
| 86 | namespace fs = std::experimental::filesystem; |
| 87 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 88 | int rc{}; |
Andrew Jeffery | ad341a2 | 2018-02-22 17:13:15 +1030 | [diff] [blame] | 89 | int fd; |
| 90 | void* map; |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 91 | char src[DATA_SIZE]{0}; |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 92 | struct mbox_context context; |
| 93 | struct mbox_context* ctx = &context; |
| 94 | memset(ctx, 0, sizeof(mbox_context)); |
| 95 | |
Andrew Jeffery | 32476cb | 2018-02-22 15:34:17 +1030 | [diff] [blame] | 96 | test::VpnorRoot root(ctx, toc, BLOCK_SIZE); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 97 | |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame] | 98 | // Initialize the context before running the test case. |
| 99 | init(ctx, root); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 100 | |
| 101 | // create the partition table |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame] | 102 | vpnor_create_partition_table_from_path(ctx, root.ro().c_str()); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 103 | |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 104 | // write beyond the partition length as the partition |
| 105 | // file length is 8 byte(TEST2). |
| 106 | rc = write_flash(ctx, (OFFSET * 2 + 3), src, sizeof(src) + 20); |
| 107 | assert(rc == -1); |
| 108 | |
| 109 | memset(src, 0xcc, sizeof(src)); |
| 110 | rc = write_flash(ctx, (OFFSET * 2), src, sizeof(src)); |
| 111 | assert(rc == 0); |
Andrew Jeffery | 24f4459 | 2018-02-22 17:51:59 +1030 | [diff] [blame^] | 112 | fd = open((root.rw() / "TEST2").c_str(), O_RDONLY); |
| 113 | map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0); |
| 114 | assert(map != MAP_FAILED); |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 115 | rc = memcmp(src, map, sizeof(src)); |
| 116 | assert(rc == 0); |
| 117 | |
| 118 | src[0] = 0xff; |
| 119 | rc = write_flash(ctx, (OFFSET * 2), src, 1); |
| 120 | assert(rc == 0); |
| 121 | rc = memcmp(src, map, sizeof(src)); |
| 122 | assert(rc == 0); |
| 123 | |
| 124 | src[1] = 0xff; |
| 125 | rc = write_flash(ctx, (OFFSET * 2) + 1, &src[1], 1); |
| 126 | assert(rc == 0); |
| 127 | rc = memcmp(src, map, sizeof(src)); |
| 128 | assert(rc == 0); |
| 129 | |
| 130 | src[2] = 0xff; |
| 131 | rc = write_flash(ctx, (OFFSET * 2) + 2, &src[2], 1); |
| 132 | assert(rc == 0); |
| 133 | rc = memcmp(src, map, sizeof(src)); |
| 134 | assert(rc == 0); |
| 135 | |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 136 | munmap(map, MEM_SIZE); |
| 137 | close(fd); |
| 138 | |
| 139 | // START Test patch location - Patch dir has preference over other locations |
| 140 | // Copy partition2 file from ro to patch to simulate a patch file that is |
| 141 | // different from the one in rw (partition2 in rw was modified with the |
| 142 | // previous write test) |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame] | 143 | fs::path patch = root.patch() / "TEST2"; |
| 144 | assert(fs::copy_file(root.ro() / "TEST2", patch)); |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 145 | |
| 146 | // Write arbitrary data |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 147 | char srcPatch[DATA_SIZE]{0}; |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 148 | memset(srcPatch, 0x33, sizeof(srcPatch)); |
| 149 | rc = write_flash(ctx, (OFFSET * 2), srcPatch, sizeof(srcPatch)); |
| 150 | assert(rc == 0); |
| 151 | |
| 152 | // Check that partition file in RW location still contains the original data |
Andrew Jeffery | fe5cc8f | 2018-02-22 15:19:04 +1030 | [diff] [blame] | 153 | fd = open((root.rw() / "TEST2").c_str(), O_RDONLY); |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 154 | map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0); |
| 155 | assert(map != MAP_FAILED); |
| 156 | rc = memcmp(src, map, sizeof(src)); |
| 157 | assert(rc == 0); |
| 158 | munmap(map, MEM_SIZE); |
| 159 | close(fd); |
| 160 | |
| 161 | // 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] | 162 | fd = open(patch.c_str(), O_RDONLY); |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 163 | map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0); |
| 164 | assert(map != MAP_FAILED); |
| 165 | rc = memcmp(srcPatch, map, sizeof(srcPatch)); |
| 166 | assert(rc == 0); |
| 167 | munmap(map, MEM_SIZE); |
| 168 | close(fd); |
| 169 | |
Andrew Jeffery | cafb002 | 2018-02-21 11:14:39 +1030 | [diff] [blame] | 170 | destroy_vpnor(ctx); |
| 171 | free(ctx->flash_bmap); |
| 172 | |
Ratan Gupta | 2407f15 | 2017-05-31 16:01:01 +0530 | [diff] [blame] | 173 | return rc; |
| 174 | } |