blob: 23d065d4d8e145bae9fab589d77e5b4580e09663 [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{};
Andrew Jefferyad341a22018-02-22 17:13:15 +103089 int fd;
90 void* map;
Andrew Jefferyf34db312018-03-09 15:27:03 +103091 char src[DATA_SIZE]{0};
Ratan Gupta2407f152017-05-31 16:01:01 +053092 struct mbox_context context;
93 struct mbox_context* ctx = &context;
94 memset(ctx, 0, sizeof(mbox_context));
95
Andrew Jeffery32476cb2018-02-22 15:34:17 +103096 test::VpnorRoot root(ctx, toc, BLOCK_SIZE);
Ratan Gupta2407f152017-05-31 16:01:01 +053097
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +103098 // Initialize the context before running the test case.
99 init(ctx, root);
Ratan Gupta2407f152017-05-31 16:01:01 +0530100
101 // create the partition table
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030102 vpnor_create_partition_table_from_path(ctx, root.ro().c_str());
Ratan Gupta2407f152017-05-31 16:01:01 +0530103
Ratan Gupta2407f152017-05-31 16:01:01 +0530104 // Write to the RO partition
105 memset(src, 0x55, sizeof(src));
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030106 fd = open((root.ro() / "TEST1").c_str(), O_RDONLY);
Ratan Gupta2407f152017-05-31 16:01:01 +0530107 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
108 assert(map != MAP_FAILED);
109 rc = write_flash(ctx, (OFFSET), src, sizeof(src));
110 // Should not be allowed to write on RO
111 assert(rc != 0);
112
113 munmap(map, MEM_SIZE);
114 close(fd);
115
116 // Write to the RW partition
117 memset(src, 0xbb, sizeof(src));
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030118 fd = open((root.rw() / "TEST2").c_str(), O_RDONLY);
Ratan Gupta2407f152017-05-31 16:01:01 +0530119 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
120 assert(map != MAP_FAILED);
121 rc = write_flash(ctx, (OFFSET * 2), src, sizeof(src));
122 assert(rc == 0);
123 rc = memcmp(src, map, sizeof(src));
124 assert(rc == 0);
125
126 // write beyond the partition length as the partition
127 // file length is 8 byte(TEST2).
128 rc = write_flash(ctx, (OFFSET * 2 + 3), src, sizeof(src) + 20);
129 assert(rc == -1);
130
131 memset(src, 0xcc, sizeof(src));
132 rc = write_flash(ctx, (OFFSET * 2), src, sizeof(src));
133 assert(rc == 0);
134 rc = memcmp(src, map, sizeof(src));
135 assert(rc == 0);
136
137 src[0] = 0xff;
138 rc = write_flash(ctx, (OFFSET * 2), src, 1);
139 assert(rc == 0);
140 rc = memcmp(src, map, sizeof(src));
141 assert(rc == 0);
142
143 src[1] = 0xff;
144 rc = write_flash(ctx, (OFFSET * 2) + 1, &src[1], 1);
145 assert(rc == 0);
146 rc = memcmp(src, map, sizeof(src));
147 assert(rc == 0);
148
149 src[2] = 0xff;
150 rc = write_flash(ctx, (OFFSET * 2) + 2, &src[2], 1);
151 assert(rc == 0);
152 rc = memcmp(src, map, sizeof(src));
153 assert(rc == 0);
154
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500155 munmap(map, MEM_SIZE);
156 close(fd);
157
158 // START Test patch location - Patch dir has preference over other locations
159 // Copy partition2 file from ro to patch to simulate a patch file that is
160 // different from the one in rw (partition2 in rw was modified with the
161 // previous write test)
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030162 fs::path patch = root.patch() / "TEST2";
163 assert(fs::copy_file(root.ro() / "TEST2", patch));
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500164
165 // Write arbitrary data
Andrew Jefferyf34db312018-03-09 15:27:03 +1030166 char srcPatch[DATA_SIZE]{0};
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500167 memset(srcPatch, 0x33, sizeof(srcPatch));
168 rc = write_flash(ctx, (OFFSET * 2), srcPatch, sizeof(srcPatch));
169 assert(rc == 0);
170
171 // Check that partition file in RW location still contains the original data
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030172 fd = open((root.rw() / "TEST2").c_str(), O_RDONLY);
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500173 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
174 assert(map != MAP_FAILED);
175 rc = memcmp(src, map, sizeof(src));
176 assert(rc == 0);
177 munmap(map, MEM_SIZE);
178 close(fd);
179
180 // Check that partition file in PATCH location was written with the new data
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030181 fd = open(patch.c_str(), O_RDONLY);
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500182 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
183 assert(map != MAP_FAILED);
184 rc = memcmp(srcPatch, map, sizeof(srcPatch));
185 assert(rc == 0);
186 munmap(map, MEM_SIZE);
187 close(fd);
188
Andrew Jefferycafb0022018-02-21 11:14:39 +1030189 destroy_vpnor(ctx);
190 free(ctx->flash_bmap);
191
Ratan Gupta2407f152017-05-31 16:01:01 +0530192 return rc;
193}