blob: c53e8dab0ffa18276a27c565465fb3738052c82c [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}
84
Ratan Gupta2407f152017-05-31 16:01:01 +053085int main(void)
86{
87 namespace fs = std::experimental::filesystem;
88
Andrew Jefferyf34db312018-03-09 15:27:03 +103089 int rc{};
90 char src[DATA_SIZE]{0};
Ratan Gupta2407f152017-05-31 16:01:01 +053091 struct mbox_context context;
92 struct mbox_context* ctx = &context;
93 memset(ctx, 0, sizeof(mbox_context));
94
Andrew Jeffery32476cb2018-02-22 15:34:17 +103095 test::VpnorRoot root(ctx, toc, BLOCK_SIZE);
Ratan Gupta2407f152017-05-31 16:01:01 +053096
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +103097 // Initialize the context before running the test case.
98 init(ctx, root);
Ratan Gupta2407f152017-05-31 16:01:01 +053099
100 // create the partition table
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030101 vpnor_create_partition_table_from_path(ctx, root.ro().c_str());
Ratan Gupta2407f152017-05-31 16:01:01 +0530102
103 // Write to psrv partition
104
105 // As file doesn't exist there, so it copies
106 // the file from RO to PRSV and write the file in PRSV partition.
107
108 memset(src, 0xaa, sizeof(src));
109
Andrew Jefferyf34db312018-03-09 15:27:03 +1030110 rc = write_flash(ctx, (OFFSET * 3), src, sizeof(src));
Ratan Gupta2407f152017-05-31 16:01:01 +0530111 assert(rc == 0);
112
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030113 auto fd = open((root.prsv() / "TEST3").c_str(), O_RDONLY);
Ratan Gupta2407f152017-05-31 16:01:01 +0530114 auto map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
115 assert(map != MAP_FAILED);
116
117 // verify it is written
118 rc = memcmp(src, map, sizeof(src));
119 assert(rc == 0);
120 munmap(map, MEM_SIZE);
121 close(fd);
122
123 // Write to the RO partition
124 memset(src, 0x55, sizeof(src));
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030125 fd = open((root.ro() / "TEST1").c_str(), O_RDONLY);
Ratan Gupta2407f152017-05-31 16:01:01 +0530126 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
127 assert(map != MAP_FAILED);
128 rc = write_flash(ctx, (OFFSET), src, sizeof(src));
129 // Should not be allowed to write on RO
130 assert(rc != 0);
131
132 munmap(map, MEM_SIZE);
133 close(fd);
134
135 // Write to the RW partition
136 memset(src, 0xbb, sizeof(src));
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030137 fd = open((root.rw() / "TEST2").c_str(), O_RDONLY);
Ratan Gupta2407f152017-05-31 16:01:01 +0530138 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
139 assert(map != MAP_FAILED);
140 rc = write_flash(ctx, (OFFSET * 2), src, sizeof(src));
141 assert(rc == 0);
142 rc = memcmp(src, map, sizeof(src));
143 assert(rc == 0);
144
145 // write beyond the partition length as the partition
146 // file length is 8 byte(TEST2).
147 rc = write_flash(ctx, (OFFSET * 2 + 3), src, sizeof(src) + 20);
148 assert(rc == -1);
149
150 memset(src, 0xcc, sizeof(src));
151 rc = write_flash(ctx, (OFFSET * 2), src, sizeof(src));
152 assert(rc == 0);
153 rc = memcmp(src, map, sizeof(src));
154 assert(rc == 0);
155
156 src[0] = 0xff;
157 rc = write_flash(ctx, (OFFSET * 2), src, 1);
158 assert(rc == 0);
159 rc = memcmp(src, map, sizeof(src));
160 assert(rc == 0);
161
162 src[1] = 0xff;
163 rc = write_flash(ctx, (OFFSET * 2) + 1, &src[1], 1);
164 assert(rc == 0);
165 rc = memcmp(src, map, sizeof(src));
166 assert(rc == 0);
167
168 src[2] = 0xff;
169 rc = write_flash(ctx, (OFFSET * 2) + 2, &src[2], 1);
170 assert(rc == 0);
171 rc = memcmp(src, map, sizeof(src));
172 assert(rc == 0);
173
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500174 munmap(map, MEM_SIZE);
175 close(fd);
176
177 // START Test patch location - Patch dir has preference over other locations
178 // Copy partition2 file from ro to patch to simulate a patch file that is
179 // different from the one in rw (partition2 in rw was modified with the
180 // previous write test)
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030181 fs::path patch = root.patch() / "TEST2";
182 assert(fs::copy_file(root.ro() / "TEST2", patch));
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500183
184 // Write arbitrary data
Andrew Jefferyf34db312018-03-09 15:27:03 +1030185 char srcPatch[DATA_SIZE]{0};
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500186 memset(srcPatch, 0x33, sizeof(srcPatch));
187 rc = write_flash(ctx, (OFFSET * 2), srcPatch, sizeof(srcPatch));
188 assert(rc == 0);
189
190 // Check that partition file in RW location still contains the original data
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030191 fd = open((root.rw() / "TEST2").c_str(), O_RDONLY);
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500192 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
193 assert(map != MAP_FAILED);
194 rc = memcmp(src, map, sizeof(src));
195 assert(rc == 0);
196 munmap(map, MEM_SIZE);
197 close(fd);
198
199 // Check that partition file in PATCH location was written with the new data
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030200 fd = open(patch.c_str(), O_RDONLY);
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500201 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
202 assert(map != MAP_FAILED);
203 rc = memcmp(srcPatch, map, sizeof(srcPatch));
204 assert(rc == 0);
205 munmap(map, MEM_SIZE);
206 close(fd);
207
Andrew Jefferycafb0022018-02-21 11:14:39 +1030208 destroy_vpnor(ctx);
209 free(ctx->flash_bmap);
210
Ratan Gupta2407f152017-05-31 16:01:01 +0530211 return rc;
212}