blob: 24a8e4263db1a8b7a08946200072a0fce041ac4f [file] [log] [blame]
Andrew Jefferyacee6832018-02-21 22:17:08 +10301/*
2 * MBox Daemon Test File
3 *
4 * Copyright 2018 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 Gupta3214b512017-05-11 08:58:19 +053020#include "config.h"
21#include "mboxd_pnor_partition_table.h"
22
23extern "C" {
24#include "test/mbox.h"
25#include "test/system.h"
26}
27
28#include <assert.h>
29#include <string.h>
30
31#include <vector>
32#include <fstream>
33#include <experimental/filesystem>
34
Adriana Kobylak2ad21322017-11-28 14:59:19 -060035// A read window assumes that the toc is located at offset 0,
36// so create dummy partition at arbitrary offset 0x100.
37constexpr auto line = "partition01=HBB,00000100,0001000,ECC,PRESERVED";
Ratan Gupta3214b512017-05-11 08:58:19 +053038constexpr auto partition = "HBB";
39char tmplt[] = "/tmp/create_read_test.XXXXXX";
Andrew Jefferyf34db312018-03-09 15:27:03 +103040uint8_t data[8] = {0xaa, 0x55, 0xaa, 0x66, 0x77, 0x88, 0x99, 0xab};
Ratan Gupta3214b512017-05-11 08:58:19 +053041
Andrew Jefferyf34db312018-03-09 15:27:03 +103042#define BLOCK_SIZE 4096
43#define MEM_SIZE (BLOCK_SIZE * 2)
44#define ERASE_SIZE BLOCK_SIZE
45#define N_WINDOWS 1
Ratan Gupta3214b512017-05-11 08:58:19 +053046#define WINDOW_SIZE BLOCK_SIZE
47
Andrew Jefferyf34db312018-03-09 15:27:03 +103048static const uint8_t get_info[] = {0x02, 0x00, 0x02, 0x00, 0x00, 0x00,
49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50 0x00, 0x00, 0x00, 0x00};
Adriana Kobylak2ad21322017-11-28 14:59:19 -060051// offset 0x100 and size 6
Andrew Jefferyf34db312018-03-09 15:27:03 +103052static const uint8_t create_read_window[] = {0x04, 0x01, 0x01, 0x00, 0x06, 0x00,
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54 0x00, 0x00, 0x00, 0x00};
Ratan Gupta3214b512017-05-11 08:58:19 +053055
Andrew Jefferyf34db312018-03-09 15:27:03 +103056static const uint8_t response[] = {0x04, 0x01, 0xfe, 0xff, 0x01, 0x00, 0x01,
57 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
Ratan Gupta3214b512017-05-11 08:58:19 +053058
59namespace fs = std::experimental::filesystem;
60
61int main()
62{
63 char* tmpdir = mkdtemp(tmplt);
64 assert(tmpdir != nullptr);
65
66 // create the toc file
67 fs::path tocFilePath{tmpdir};
68 tocFilePath /= PARTITION_TOC_FILE;
69 std::ofstream tocFile(tocFilePath.c_str());
70 tocFile.write(line, strlen(line));
71 tocFile.close();
72
73 // create the partition file
74 fs::path partitionFilePath{tmpdir};
75 partitionFilePath /= partition;
76 std::ofstream partitionFile(partitionFilePath.c_str());
77
Andrew Jefferyf34db312018-03-09 15:27:03 +103078 partitionFile.write((char*)data, sizeof(data));
Ratan Gupta3214b512017-05-11 08:58:19 +053079 partitionFile.close();
80
81 system_set_reserved_size(MEM_SIZE);
82 system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE);
83
Andrew Jefferyf34db312018-03-09 15:27:03 +103084 struct mbox_context* ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
85 strcpy(ctx->paths.ro_loc, tmpdir);
86 strcpy(ctx->paths.rw_loc, tmpdir);
87 strcpy(ctx->paths.prsv_loc, tmpdir);
Ratan Gupta3214b512017-05-11 08:58:19 +053088
89 vpnor_create_partition_table_from_path(ctx, tmpdir);
90
91 int rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
92 assert(rc == 1);
93
94 // send the request for partition1
95 rc = mbox_command_dispatch(ctx, create_read_window,
Andrew Jefferyf34db312018-03-09 15:27:03 +103096 sizeof(create_read_window));
Ratan Gupta3214b512017-05-11 08:58:19 +053097 assert(rc == 1);
98
99 rc = mbox_cmp(ctx, response, sizeof(response));
100 assert(rc == 0);
101
102 // Compare the reserved memory to the pnor
103 rc = memcmp(ctx->mem, data, 6);
104 assert(rc == 0);
105
Andrew Jefferyf34db312018-03-09 15:27:03 +1030106 // TODO: Add few more test cases for read from multiple partitions(PRSV/RW)
Ratan Gupta6a98e182017-06-06 15:04:23 +0530107 // Read beyond the partition file size.
108 // openbmc/openbmc#1868
109
Ratan Gupta3214b512017-05-11 08:58:19 +0530110 fs::remove_all(fs::path{tmpdir});
111 return rc;
112}