blob: 5bb6f51464a23600a2fe4c7778adafd72c16d1cf [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 beyond the partition length as the partition
105 // file length is 8 byte(TEST2).
106 rc = write_flash(ctx, (OFFSET * 2 + 3), src, sizeof(src) + 20);
107 assert(rc == -1);
108
109 memset(src, 0xcc, sizeof(src));
110 rc = write_flash(ctx, (OFFSET * 2), src, sizeof(src));
111 assert(rc == 0);
Andrew Jeffery24f44592018-02-22 17:51:59 +1030112 fd = open((root.rw() / "TEST2").c_str(), O_RDONLY);
113 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
114 assert(map != MAP_FAILED);
Ratan Gupta2407f152017-05-31 16:01:01 +0530115 rc = memcmp(src, map, sizeof(src));
116 assert(rc == 0);
117
118 src[0] = 0xff;
119 rc = write_flash(ctx, (OFFSET * 2), src, 1);
120 assert(rc == 0);
121 rc = memcmp(src, map, sizeof(src));
122 assert(rc == 0);
123
124 src[1] = 0xff;
125 rc = write_flash(ctx, (OFFSET * 2) + 1, &src[1], 1);
126 assert(rc == 0);
127 rc = memcmp(src, map, sizeof(src));
128 assert(rc == 0);
129
130 src[2] = 0xff;
131 rc = write_flash(ctx, (OFFSET * 2) + 2, &src[2], 1);
132 assert(rc == 0);
133 rc = memcmp(src, map, sizeof(src));
134 assert(rc == 0);
135
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500136 munmap(map, MEM_SIZE);
137 close(fd);
138
139 // START Test patch location - Patch dir has preference over other locations
140 // Copy partition2 file from ro to patch to simulate a patch file that is
141 // different from the one in rw (partition2 in rw was modified with the
142 // previous write test)
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030143 fs::path patch = root.patch() / "TEST2";
144 assert(fs::copy_file(root.ro() / "TEST2", patch));
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500145
146 // Write arbitrary data
Andrew Jefferyf34db312018-03-09 15:27:03 +1030147 char srcPatch[DATA_SIZE]{0};
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500148 memset(srcPatch, 0x33, sizeof(srcPatch));
149 rc = write_flash(ctx, (OFFSET * 2), srcPatch, sizeof(srcPatch));
150 assert(rc == 0);
151
152 // Check that partition file in RW location still contains the original data
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030153 fd = open((root.rw() / "TEST2").c_str(), O_RDONLY);
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500154 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
155 assert(map != MAP_FAILED);
156 rc = memcmp(src, map, sizeof(src));
157 assert(rc == 0);
158 munmap(map, MEM_SIZE);
159 close(fd);
160
161 // Check that partition file in PATCH location was written with the new data
Andrew Jefferyfe5cc8f2018-02-22 15:19:04 +1030162 fd = open(patch.c_str(), O_RDONLY);
Adriana Kobylakc71dfd72017-07-22 11:10:43 -0500163 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
164 assert(map != MAP_FAILED);
165 rc = memcmp(srcPatch, map, sizeof(srcPatch));
166 assert(rc == 0);
167 munmap(map, MEM_SIZE);
168 close(fd);
169
Andrew Jefferycafb0022018-02-21 11:14:39 +1030170 destroy_vpnor(ctx);
171 free(ctx->flash_bmap);
172
Ratan Gupta2407f152017-05-31 16:01:01 +0530173 return rc;
174}