blob: 7c8f5069287612c15013a656b2acd61b1612b641 [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)
Ratan Gupta2407f152017-05-31 16:01:01 +053048#define BLOCK_SIZE_SHIFT 12
49
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +103050const 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 Gupta2407f152017-05-31 16:01:01 +053055
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +103056std::vector<std::string> partitions = {"TEST1", "TEST2", "TEST3"};
57
58namespace test = openpower::virtual_pnor::test;
59
60void init(struct mbox_context* ctx, test::VpnorRoot& root)
61{
Ratan Gupta2407f152017-05-31 16:01:01 +053062 namespace fs = std::experimental::filesystem;
63 using namespace std::string_literals;
64
Ratan Gupta2407f152017-05-31 16:01:01 +053065 // create the partition files in the ro directory
66 for (auto partition : partitions)
67 {
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +103068 root.write(partition, data, sizeof(data));
Ratan Gupta2407f152017-05-31 16:01:01 +053069 }
70
71 // copy partition2 file from ro to rw
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +103072 assert(fs::copy_file(root.ro() / "TEST2", root.rw() / "TEST2"));
Ratan Gupta2407f152017-05-31 16:01:01 +053073
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 Jefferyf34db312018-03-09 15:27:03 +103081 calloc(MEM_SIZE >> ctx->erase_size_shift, sizeof(*ctx->flash_bmap)));
Ratan Gupta2407f152017-05-31 16:01:01 +053082}
83
Ratan Gupta2407f152017-05-31 16:01:01 +053084int main(void)
85{
86 namespace fs = std::experimental::filesystem;
87
Andrew Jefferyf34db312018-03-09 15:27:03 +103088 int rc{};
89 char src[DATA_SIZE]{0};
Ratan Gupta2407f152017-05-31 16:01:01 +053090 struct mbox_context context;
91 struct mbox_context* ctx = &context;
92 memset(ctx, 0, sizeof(mbox_context));
93
Andrew Jeffery32476cb2018-02-22 15:34:17 +103094 test::VpnorRoot root(ctx, toc, BLOCK_SIZE);
Ratan Gupta2407f152017-05-31 16:01:01 +053095
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +103096 // Initialize the context before running the test case.
97 init(ctx, root);
Ratan Gupta2407f152017-05-31 16:01:01 +053098
99 // create the partition table
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030100 vpnor_create_partition_table_from_path(ctx, root.ro().c_str());
Ratan Gupta2407f152017-05-31 16:01:01 +0530101
102 // Write to psrv partition
103
104 // As file doesn't exist there, so it copies
105 // the file from RO to PRSV and write the file in PRSV partition.
106
107 memset(src, 0xaa, sizeof(src));
108
Andrew Jefferyf34db312018-03-09 15:27:03 +1030109 rc = write_flash(ctx, (OFFSET * 3), src, sizeof(src));
Ratan Gupta2407f152017-05-31 16:01:01 +0530110 assert(rc == 0);
111
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030112 auto fd = open((root.prsv() / "TEST3").c_str(), O_RDONLY);
Ratan Gupta2407f152017-05-31 16:01:01 +0530113 auto map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
114 assert(map != MAP_FAILED);
115
116 // verify it is written
117 rc = memcmp(src, map, sizeof(src));
118 assert(rc == 0);
119 munmap(map, MEM_SIZE);
120 close(fd);
121
122 // Write to the RO partition
123 memset(src, 0x55, sizeof(src));
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030124 fd = open((root.ro() / "TEST1").c_str(), O_RDONLY);
Ratan Gupta2407f152017-05-31 16:01:01 +0530125 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
126 assert(map != MAP_FAILED);
127 rc = write_flash(ctx, (OFFSET), src, sizeof(src));
128 // Should not be allowed to write on RO
129 assert(rc != 0);
130
131 munmap(map, MEM_SIZE);
132 close(fd);
133
134 // Write to the RW partition
135 memset(src, 0xbb, sizeof(src));
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030136 fd = open((root.rw() / "TEST2").c_str(), O_RDONLY);
Ratan Gupta2407f152017-05-31 16:01:01 +0530137 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
138 assert(map != MAP_FAILED);
139 rc = write_flash(ctx, (OFFSET * 2), src, sizeof(src));
140 assert(rc == 0);
141 rc = memcmp(src, map, sizeof(src));
142 assert(rc == 0);
143
144 // write beyond the partition length as the partition
145 // file length is 8 byte(TEST2).
146 rc = write_flash(ctx, (OFFSET * 2 + 3), src, sizeof(src) + 20);
147 assert(rc == -1);
148
149 memset(src, 0xcc, sizeof(src));
150 rc = write_flash(ctx, (OFFSET * 2), src, sizeof(src));
151 assert(rc == 0);
152 rc = memcmp(src, map, sizeof(src));
153 assert(rc == 0);
154
155 src[0] = 0xff;
156 rc = write_flash(ctx, (OFFSET * 2), src, 1);
157 assert(rc == 0);
158 rc = memcmp(src, map, sizeof(src));
159 assert(rc == 0);
160
161 src[1] = 0xff;
162 rc = write_flash(ctx, (OFFSET * 2) + 1, &src[1], 1);
163 assert(rc == 0);
164 rc = memcmp(src, map, sizeof(src));
165 assert(rc == 0);
166
167 src[2] = 0xff;
168 rc = write_flash(ctx, (OFFSET * 2) + 2, &src[2], 1);
169 assert(rc == 0);
170 rc = memcmp(src, map, sizeof(src));
171 assert(rc == 0);
172
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500173 munmap(map, MEM_SIZE);
174 close(fd);
175
176 // START Test patch location - Patch dir has preference over other locations
177 // Copy partition2 file from ro to patch to simulate a patch file that is
178 // different from the one in rw (partition2 in rw was modified with the
179 // previous write test)
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030180 fs::path patch = root.patch() / "TEST2";
181 assert(fs::copy_file(root.ro() / "TEST2", patch));
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500182
183 // Write arbitrary data
Andrew Jefferyf34db312018-03-09 15:27:03 +1030184 char srcPatch[DATA_SIZE]{0};
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500185 memset(srcPatch, 0x33, sizeof(srcPatch));
186 rc = write_flash(ctx, (OFFSET * 2), srcPatch, sizeof(srcPatch));
187 assert(rc == 0);
188
189 // Check that partition file in RW location still contains the original data
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030190 fd = open((root.rw() / "TEST2").c_str(), O_RDONLY);
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500191 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
192 assert(map != MAP_FAILED);
193 rc = memcmp(src, map, sizeof(src));
194 assert(rc == 0);
195 munmap(map, MEM_SIZE);
196 close(fd);
197
198 // Check that partition file in PATCH location was written with the new data
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030199 fd = open(patch.c_str(), O_RDONLY);
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500200 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
201 assert(map != MAP_FAILED);
202 rc = memcmp(srcPatch, map, sizeof(srcPatch));
203 assert(rc == 0);
204 munmap(map, MEM_SIZE);
205 close(fd);
206
Andrew Jefferycafb0022018-02-21 11:14:39 +1030207 destroy_vpnor(ctx);
208 free(ctx->flash_bmap);
209
Ratan Gupta2407f152017-05-31 16:01:01 +0530210 return rc;
211}