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 | |
| 20 | #include "config.h" |
| 21 | #include "mboxd_pnor_partition_table.h" |
| 22 | |
| 23 | extern "C" { |
| 24 | #include "mbox.h" |
| 25 | #include "test/tmpf.h" |
| 26 | #include "mboxd_flash.h" |
| 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 | |
| 40 | |
| 41 | uint8_t data[8] = { 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7 }; |
| 42 | |
| 43 | #define BLOCK_SIZE 4096 |
| 44 | #define OFFSET BLOCK_SIZE |
| 45 | #define MEM_SIZE (BLOCK_SIZE *2) |
| 46 | #define DATA_SIZE sizeof(data) |
| 47 | #define ERASE_SIZE BLOCK_SIZE |
| 48 | #define BLOCK_SIZE_SHIFT 12 |
| 49 | |
| 50 | |
| 51 | void init(struct mbox_context* ctx) |
| 52 | { |
| 53 | |
| 54 | namespace fs = std::experimental::filesystem; |
| 55 | using namespace std::string_literals; |
| 56 | |
| 57 | std::string tocData = "partition01=TEST1,00000000,00000400,ECC,READONLY\n"s |
| 58 | + "partition02=TEST2,00000000,00000008,ECC,READWRITE\n"s |
| 59 | + "partition03=TEST3,00000000,00000400,ECC,PRESERVED"s; |
| 60 | |
| 61 | std::vector<std::string> templatePaths = { "/tmp/ro.XXXXXX", |
| 62 | "/tmp/rw.XXXXXX" , |
| 63 | "/tmp/prsv.XXXXXX" |
| 64 | }; |
| 65 | |
| 66 | std::vector<std::string>partitions = { "TEST1", "TEST2", "TEST3" }; |
| 67 | |
| 68 | |
| 69 | // create various partition directory |
| 70 | |
| 71 | std::string tmpROdir = mkdtemp(const_cast<char*>(templatePaths[0].c_str())); |
| 72 | assert(tmpROdir.length() != 0); |
| 73 | |
| 74 | std::string tmpRWdir = mkdtemp(const_cast<char*>(templatePaths[1].c_str())); |
| 75 | assert(tmpRWdir.length() != 0); |
| 76 | |
| 77 | std::string tmpPRSVdir = mkdtemp(const_cast<char*>(templatePaths[2].c_str())); |
| 78 | assert(tmpPRSVdir.length() != 0); |
| 79 | |
| 80 | // create the toc file |
| 81 | fs::path tocFilePath = tmpROdir; |
| 82 | |
| 83 | tocFilePath /= PARTITION_TOC_FILE; |
| 84 | std::ofstream tocFile(tocFilePath.c_str()); |
| 85 | tocFile.write(tocData.c_str(), static_cast<int>(tocData.length())); |
| 86 | tocFile.close(); |
| 87 | |
| 88 | // create the partition files in the ro directory |
| 89 | for (auto partition : partitions) |
| 90 | { |
| 91 | fs::path partitionFilePath {tmpROdir}; |
| 92 | partitionFilePath /= partition; |
| 93 | std::ofstream partitionFile(partitionFilePath.c_str()); |
| 94 | partitionFile.write(reinterpret_cast<char*>(data), sizeof(data)); |
| 95 | partitionFile.close(); |
| 96 | |
| 97 | } |
| 98 | |
| 99 | // copy partition2 file from ro to rw |
| 100 | std::string roFile = tmpROdir + "/" + "TEST2"; |
| 101 | std::string rwFile = tmpRWdir + "/" + "TEST2"; |
| 102 | assert(fs::copy_file(roFile, rwFile) == true); |
| 103 | |
| 104 | mbox_vlog = &mbox_log_console; |
| 105 | verbosity = (verbose)2; |
| 106 | |
| 107 | // setting context parameters |
| 108 | ctx->erase_size_shift = BLOCK_SIZE_SHIFT; |
| 109 | ctx->block_size_shift = BLOCK_SIZE_SHIFT; |
| 110 | ctx->flash_bmap = reinterpret_cast<uint8_t*>( |
| 111 | calloc(MEM_SIZE >> ctx->erase_size_shift, |
| 112 | sizeof(*ctx->flash_bmap))); |
| 113 | |
| 114 | strcpy(ctx->paths.ro_loc, tmpROdir.c_str()); |
| 115 | strcpy(ctx->paths.rw_loc, tmpRWdir.c_str()); |
| 116 | strcpy(ctx->paths.prsv_loc, tmpPRSVdir.c_str()); |
| 117 | |
| 118 | } |
| 119 | |
| 120 | |
| 121 | int main(void) |
| 122 | { |
| 123 | namespace fs = std::experimental::filesystem; |
| 124 | |
| 125 | int rc {}; |
| 126 | char src[DATA_SIZE] {0}; |
| 127 | struct mbox_context context; |
| 128 | struct mbox_context* ctx = &context; |
| 129 | memset(ctx, 0, sizeof(mbox_context)); |
| 130 | |
| 131 | //Initialize the context before running the test case. |
| 132 | init(ctx); |
| 133 | |
| 134 | std::string tmpROdir = ctx->paths.ro_loc; |
| 135 | std::string tmpRWdir = ctx->paths.rw_loc; |
| 136 | std::string tmpPRSVdir = ctx->paths.prsv_loc; |
| 137 | |
| 138 | // create the partition table |
| 139 | vpnor_create_partition_table_from_path(ctx, tmpROdir.c_str()); |
| 140 | |
| 141 | // Write to psrv partition |
| 142 | |
| 143 | // As file doesn't exist there, so it copies |
| 144 | // the file from RO to PRSV and write the file in PRSV partition. |
| 145 | |
| 146 | memset(src, 0xaa, sizeof(src)); |
| 147 | |
| 148 | rc = write_flash(ctx, (OFFSET * 3) , src, sizeof(src)); |
| 149 | assert(rc == 0); |
| 150 | |
| 151 | auto fd = open((tmpPRSVdir + "/" + "TEST3").c_str(), O_RDONLY); |
| 152 | auto map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0); |
| 153 | assert(map != MAP_FAILED); |
| 154 | |
| 155 | // verify it is written |
| 156 | rc = memcmp(src, map, sizeof(src)); |
| 157 | assert(rc == 0); |
| 158 | munmap(map, MEM_SIZE); |
| 159 | close(fd); |
| 160 | |
| 161 | // Write to the RO partition |
| 162 | memset(src, 0x55, sizeof(src)); |
| 163 | fd = open((tmpROdir + "/" + "TEST1").c_str(), O_RDONLY); |
| 164 | map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0); |
| 165 | assert(map != MAP_FAILED); |
| 166 | rc = write_flash(ctx, (OFFSET), src, sizeof(src)); |
| 167 | // Should not be allowed to write on RO |
| 168 | assert(rc != 0); |
| 169 | |
| 170 | munmap(map, MEM_SIZE); |
| 171 | close(fd); |
| 172 | |
| 173 | // Write to the RW partition |
| 174 | memset(src, 0xbb, sizeof(src)); |
| 175 | fd = open((tmpRWdir + "/" + "TEST2").c_str(), O_RDONLY); |
| 176 | map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0); |
| 177 | assert(map != MAP_FAILED); |
| 178 | rc = write_flash(ctx, (OFFSET * 2), src, sizeof(src)); |
| 179 | assert(rc == 0); |
| 180 | rc = memcmp(src, map, sizeof(src)); |
| 181 | assert(rc == 0); |
| 182 | |
| 183 | // write beyond the partition length as the partition |
| 184 | // file length is 8 byte(TEST2). |
| 185 | rc = write_flash(ctx, (OFFSET * 2 + 3), src, sizeof(src) + 20); |
| 186 | assert(rc == -1); |
| 187 | |
| 188 | memset(src, 0xcc, sizeof(src)); |
| 189 | rc = write_flash(ctx, (OFFSET * 2), src, sizeof(src)); |
| 190 | assert(rc == 0); |
| 191 | rc = memcmp(src, map, sizeof(src)); |
| 192 | assert(rc == 0); |
| 193 | |
| 194 | src[0] = 0xff; |
| 195 | rc = write_flash(ctx, (OFFSET * 2), src, 1); |
| 196 | assert(rc == 0); |
| 197 | rc = memcmp(src, map, sizeof(src)); |
| 198 | assert(rc == 0); |
| 199 | |
| 200 | src[1] = 0xff; |
| 201 | rc = write_flash(ctx, (OFFSET * 2) + 1, &src[1], 1); |
| 202 | assert(rc == 0); |
| 203 | rc = memcmp(src, map, sizeof(src)); |
| 204 | assert(rc == 0); |
| 205 | |
| 206 | src[2] = 0xff; |
| 207 | rc = write_flash(ctx, (OFFSET * 2) + 2, &src[2], 1); |
| 208 | assert(rc == 0); |
| 209 | rc = memcmp(src, map, sizeof(src)); |
| 210 | assert(rc == 0); |
| 211 | |
| 212 | fs::remove_all(fs::path {tmpROdir}); |
| 213 | fs::remove_all(fs::path {tmpRWdir}); |
| 214 | fs::remove_all(fs::path {tmpPRSVdir}); |
| 215 | |
| 216 | return rc; |
| 217 | } |