blob: 049cb94db76f519f468e78e7cc4020aa0f6bb864 [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 Jefferyf34db312018-03-09 15:27:03 +103040uint8_t data[8] = {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7};
Ratan Gupta2407f152017-05-31 16:01:01 +053041
Andrew Jefferyf34db312018-03-09 15:27:03 +103042#define BLOCK_SIZE 4096
43#define OFFSET BLOCK_SIZE
44#define MEM_SIZE (BLOCK_SIZE * 2)
Ratan Gupta2407f152017-05-31 16:01:01 +053045#define DATA_SIZE sizeof(data)
46#define ERASE_SIZE BLOCK_SIZE
47#define BLOCK_SIZE_SHIFT 12
48
Ratan Gupta2407f152017-05-31 16:01:01 +053049void init(struct mbox_context* ctx)
50{
51
52 namespace fs = std::experimental::filesystem;
53 using namespace std::string_literals;
54
Andrew Jefferyf34db312018-03-09 15:27:03 +103055 std::string tocData =
56 "partition01=TEST1,00001000,00001400,ECC,READONLY\n"s +
57 "partition02=TEST2,00002000,00002008,ECC,READWRITE\n"s +
58 "partition03=TEST3,00003000,00003400,ECC,PRESERVED"s;
Ratan Gupta2407f152017-05-31 16:01:01 +053059
Andrew Jefferyf34db312018-03-09 15:27:03 +103060 std::vector<std::string> templatePaths = {
61 "/tmp/ro.XXXXXX", "/tmp/rw.XXXXXX", "/tmp/prsv.XXXXXX",
62 "/tmp/patch.XXXXXX"};
Ratan Gupta2407f152017-05-31 16:01:01 +053063
Andrew Jefferyf34db312018-03-09 15:27:03 +103064 std::vector<std::string> partitions = {"TEST1", "TEST2", "TEST3"};
Ratan Gupta2407f152017-05-31 16:01:01 +053065
66 // create various partition directory
67
68 std::string tmpROdir = mkdtemp(const_cast<char*>(templatePaths[0].c_str()));
69 assert(tmpROdir.length() != 0);
70
71 std::string tmpRWdir = mkdtemp(const_cast<char*>(templatePaths[1].c_str()));
72 assert(tmpRWdir.length() != 0);
73
Andrew Jefferyf34db312018-03-09 15:27:03 +103074 std::string tmpPRSVdir =
75 mkdtemp(const_cast<char*>(templatePaths[2].c_str()));
Ratan Gupta2407f152017-05-31 16:01:01 +053076 assert(tmpPRSVdir.length() != 0);
77
Andrew Jefferyf34db312018-03-09 15:27:03 +103078 std::string tmpPATCHdir =
79 mkdtemp(const_cast<char*>(templatePaths[3].c_str()));
Adriana Kobylakc71dfd72017-07-22 11:10:43 -050080 assert(tmpPATCHdir.length() != 0);
81
Ratan Gupta2407f152017-05-31 16:01:01 +053082 // create the toc file
83 fs::path tocFilePath = tmpROdir;
84
85 tocFilePath /= PARTITION_TOC_FILE;
86 std::ofstream tocFile(tocFilePath.c_str());
87 tocFile.write(tocData.c_str(), static_cast<int>(tocData.length()));
88 tocFile.close();
89
90 // create the partition files in the ro directory
91 for (auto partition : partitions)
92 {
Andrew Jefferyf34db312018-03-09 15:27:03 +103093 fs::path partitionFilePath{tmpROdir};
Ratan Gupta2407f152017-05-31 16:01:01 +053094 partitionFilePath /= partition;
95 std::ofstream partitionFile(partitionFilePath.c_str());
96 partitionFile.write(reinterpret_cast<char*>(data), sizeof(data));
97 partitionFile.close();
Ratan Gupta2407f152017-05-31 16:01:01 +053098 }
99
100 // copy partition2 file from ro to rw
101 std::string roFile = tmpROdir + "/" + "TEST2";
102 std::string rwFile = tmpRWdir + "/" + "TEST2";
103 assert(fs::copy_file(roFile, rwFile) == true);
104
105 mbox_vlog = &mbox_log_console;
106 verbosity = (verbose)2;
107
108 // setting context parameters
109 ctx->erase_size_shift = BLOCK_SIZE_SHIFT;
110 ctx->block_size_shift = BLOCK_SIZE_SHIFT;
111 ctx->flash_bmap = reinterpret_cast<uint8_t*>(
Andrew Jefferyf34db312018-03-09 15:27:03 +1030112 calloc(MEM_SIZE >> ctx->erase_size_shift, sizeof(*ctx->flash_bmap)));
Ratan Gupta2407f152017-05-31 16:01:01 +0530113
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());
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500117 strcpy(ctx->paths.patch_loc, tmpPATCHdir.c_str());
Ratan Gupta2407f152017-05-31 16:01:01 +0530118}
119
Ratan Gupta2407f152017-05-31 16:01:01 +0530120int main(void)
121{
122 namespace fs = std::experimental::filesystem;
123
Andrew Jefferyf34db312018-03-09 15:27:03 +1030124 int rc{};
125 char src[DATA_SIZE]{0};
Ratan Gupta2407f152017-05-31 16:01:01 +0530126 struct mbox_context context;
127 struct mbox_context* ctx = &context;
128 memset(ctx, 0, sizeof(mbox_context));
129
Andrew Jefferyf34db312018-03-09 15:27:03 +1030130 // Initialize the context before running the test case.
Ratan Gupta2407f152017-05-31 16:01:01 +0530131 init(ctx);
132
133 std::string tmpROdir = ctx->paths.ro_loc;
134 std::string tmpRWdir = ctx->paths.rw_loc;
135 std::string tmpPRSVdir = ctx->paths.prsv_loc;
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500136 std::string tmpPATCHdir = ctx->paths.patch_loc;
Ratan Gupta2407f152017-05-31 16:01:01 +0530137
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
Andrew Jefferyf34db312018-03-09 15:27:03 +1030148 rc = write_flash(ctx, (OFFSET * 3), src, sizeof(src));
Ratan Gupta2407f152017-05-31 16:01:01 +0530149 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
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500212 munmap(map, MEM_SIZE);
213 close(fd);
214
215 // START Test patch location - Patch dir has preference over other locations
216 // Copy partition2 file from ro to patch to simulate a patch file that is
217 // different from the one in rw (partition2 in rw was modified with the
218 // previous write test)
219 fs::path roFile(tmpROdir);
220 roFile /= "TEST2";
221 fs::path patchFile(tmpPATCHdir);
222 patchFile /= "TEST2";
223 assert(fs::copy_file(roFile, patchFile) == true);
224
225 // Write arbitrary data
Andrew Jefferyf34db312018-03-09 15:27:03 +1030226 char srcPatch[DATA_SIZE]{0};
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500227 memset(srcPatch, 0x33, sizeof(srcPatch));
228 rc = write_flash(ctx, (OFFSET * 2), srcPatch, sizeof(srcPatch));
229 assert(rc == 0);
230
231 // Check that partition file in RW location still contains the original data
232 fs::path rwFile(tmpRWdir);
233 rwFile /= "TEST2";
234 fd = open(rwFile.c_str(), O_RDONLY);
235 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
236 assert(map != MAP_FAILED);
237 rc = memcmp(src, map, sizeof(src));
238 assert(rc == 0);
239 munmap(map, MEM_SIZE);
240 close(fd);
241
242 // Check that partition file in PATCH location was written with the new data
243 fd = open(patchFile.c_str(), O_RDONLY);
244 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
245 assert(map != MAP_FAILED);
246 rc = memcmp(srcPatch, map, sizeof(srcPatch));
247 assert(rc == 0);
248 munmap(map, MEM_SIZE);
249 close(fd);
250
251 // Remove patch file so that subsequent tests don't use it
252 fs::remove(patchFile);
253 // END Test patch location
254
Andrew Jefferyf34db312018-03-09 15:27:03 +1030255 fs::remove_all(fs::path{tmpROdir});
256 fs::remove_all(fs::path{tmpRWdir});
257 fs::remove_all(fs::path{tmpPRSVdir});
258 fs::remove_all(fs::path{tmpPATCHdir});
Ratan Gupta2407f152017-05-31 16:01:01 +0530259
Andrew Jefferycafb0022018-02-21 11:14:39 +1030260 destroy_vpnor(ctx);
261 free(ctx->flash_bmap);
262
Ratan Gupta2407f152017-05-31 16:01:01 +0530263 return rc;
264}