blob: 384c13a317b266f4144a60fde3d0a003fc55f969 [file] [log] [blame]
Ratan Gupta2407f152017-05-31 16:01:01 +05301/*
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 Gupta2407f152017-05-31 16:01:01 +053020extern "C" {
Andrew Jeffery85985912018-02-22 10:20:31 +103021#include "config.h"
22#include "common.h"
23#include "mboxd_flash.h"
24#include "mboxd_pnor_partition_table.h"
Ratan Gupta2407f152017-05-31 16:01:01 +053025#include "mbox.h"
26#include "test/tmpf.h"
Ratan Gupta2407f152017-05-31 16:01:01 +053027}
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 Jefferyfe5cc8f2018-02-22 15:19:04 +103040#include "test/vpnor/tmpd.hpp"
41
Andrew Jefferyf34db312018-03-09 15:27:03 +103042uint8_t data[8] = {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7};
Ratan Gupta2407f152017-05-31 16:01:01 +053043
Andrew Jefferyf34db312018-03-09 15:27:03 +103044#define BLOCK_SIZE 4096
45#define OFFSET BLOCK_SIZE
46#define MEM_SIZE (BLOCK_SIZE * 2)
Ratan Gupta2407f152017-05-31 16:01:01 +053047#define DATA_SIZE sizeof(data)
48#define ERASE_SIZE BLOCK_SIZE
49#define BLOCK_SIZE_SHIFT 12
50
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +103051const 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 Gupta2407f152017-05-31 16:01:01 +053056
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +103057std::vector<std::string> partitions = {"TEST1", "TEST2", "TEST3"};
58
59namespace test = openpower::virtual_pnor::test;
60
61void init(struct mbox_context* ctx, test::VpnorRoot& root)
62{
Ratan Gupta2407f152017-05-31 16:01:01 +053063 namespace fs = std::experimental::filesystem;
64 using namespace std::string_literals;
65
Ratan Gupta2407f152017-05-31 16:01:01 +053066 // create the partition files in the ro directory
67 for (auto partition : partitions)
68 {
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +103069 root.write(partition, data, sizeof(data));
Ratan Gupta2407f152017-05-31 16:01:01 +053070 }
71
72 // copy partition2 file from ro to rw
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +103073 assert(fs::copy_file(root.ro() / "TEST2", root.rw() / "TEST2"));
Ratan Gupta2407f152017-05-31 16:01:01 +053074
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 Jefferyf34db312018-03-09 15:27:03 +103082 calloc(MEM_SIZE >> ctx->erase_size_shift, sizeof(*ctx->flash_bmap)));
Ratan Gupta2407f152017-05-31 16:01:01 +053083
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +103084 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 Gupta2407f152017-05-31 16:01:01 +053088}
89
Ratan Gupta2407f152017-05-31 16:01:01 +053090int main(void)
91{
92 namespace fs = std::experimental::filesystem;
93
Andrew Jefferyf34db312018-03-09 15:27:03 +103094 int rc{};
95 char src[DATA_SIZE]{0};
Ratan Gupta2407f152017-05-31 16:01:01 +053096 struct mbox_context context;
97 struct mbox_context* ctx = &context;
98 memset(ctx, 0, sizeof(mbox_context));
99
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030100 test::VpnorRoot root(toc, BLOCK_SIZE);
Ratan Gupta2407f152017-05-31 16:01:01 +0530101
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030102 // Initialize the context before running the test case.
103 init(ctx, root);
Ratan Gupta2407f152017-05-31 16:01:01 +0530104
105 // create the partition table
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030106 vpnor_create_partition_table_from_path(ctx, root.ro().c_str());
Ratan Gupta2407f152017-05-31 16:01:01 +0530107
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 Jefferyf34db312018-03-09 15:27:03 +1030115 rc = write_flash(ctx, (OFFSET * 3), src, sizeof(src));
Ratan Gupta2407f152017-05-31 16:01:01 +0530116 assert(rc == 0);
117
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030118 auto fd = open((root.prsv() / "TEST3").c_str(), O_RDONLY);
Ratan Gupta2407f152017-05-31 16:01:01 +0530119 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 Jefferyfe5cc8f2018-02-22 15:19:04 +1030130 fd = open((root.ro() / "TEST1").c_str(), O_RDONLY);
Ratan Gupta2407f152017-05-31 16:01:01 +0530131 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 Jefferyfe5cc8f2018-02-22 15:19:04 +1030142 fd = open((root.rw() / "TEST2").c_str(), O_RDONLY);
Ratan Gupta2407f152017-05-31 16:01:01 +0530143 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 Kobylakc71dfd72017-07-22 11:10:43 -0500179 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 Jefferyfe5cc8f2018-02-22 15:19:04 +1030186 fs::path patch = root.patch() / "TEST2";
187 assert(fs::copy_file(root.ro() / "TEST2", patch));
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500188
189 // Write arbitrary data
Andrew Jefferyf34db312018-03-09 15:27:03 +1030190 char srcPatch[DATA_SIZE]{0};
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500191 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 Jefferyfe5cc8f2018-02-22 15:19:04 +1030196 fd = open((root.rw() / "TEST2").c_str(), O_RDONLY);
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500197 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 Jefferyfe5cc8f2018-02-22 15:19:04 +1030205 fd = open(patch.c_str(), O_RDONLY);
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500206 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 Jefferycafb0022018-02-21 11:14:39 +1030213 destroy_vpnor(ctx);
214 free(ctx->flash_bmap);
215
Ratan Gupta2407f152017-05-31 16:01:01 +0530216 return rc;
217}