blob: d9bcccc22e9010346446fa08f1821c8151d4f878 [file] [log] [blame]
Ratan Gupta3214b512017-05-11 08:58:19 +05301#include "config.h"
2#include "mboxd_pnor_partition_table.h"
3
4extern "C" {
5#include "test/mbox.h"
6#include "test/system.h"
7}
8
9#include <assert.h>
10#include <string.h>
11
12#include <vector>
13#include <fstream>
14#include <experimental/filesystem>
15
Ratan Gupta6a98e182017-06-06 15:04:23 +053016constexpr auto line = "partition01=HBB,00000000,0001000,ECC,PRESERVED";
Ratan Gupta3214b512017-05-11 08:58:19 +053017constexpr auto partition = "HBB";
18char tmplt[] = "/tmp/create_read_test.XXXXXX";
19uint8_t data[8] = { 0xaa, 0x55, 0xaa, 0x66, 0x77, 0x88, 0x99, 0xab };
20
21#define BLOCK_SIZE 4096
22#define MEM_SIZE (BLOCK_SIZE *2)
23#define ERASE_SIZE BLOCK_SIZE
24#define N_WINDOWS 1
25#define WINDOW_SIZE BLOCK_SIZE
26
27static const uint8_t get_info[] = {
28 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
29 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
30};
31// offset 1 and size 6
32static const uint8_t create_read_window[] = {
33 0x04, 0x01, 0x01, 0x00, 0x06, 0x00, 0x00, 0x00,
34 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
35};
36
37static const uint8_t response[] = {
38 0x04, 0x01, 0xfe, 0xff, 0x01, 0x00, 0x01, 0x00,
39 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
40};
41
42namespace fs = std::experimental::filesystem;
43
44int main()
45{
46 char* tmpdir = mkdtemp(tmplt);
47 assert(tmpdir != nullptr);
48
49 // create the toc file
50 fs::path tocFilePath{tmpdir};
51 tocFilePath /= PARTITION_TOC_FILE;
52 std::ofstream tocFile(tocFilePath.c_str());
53 tocFile.write(line, strlen(line));
54 tocFile.close();
55
56 // create the partition file
57 fs::path partitionFilePath{tmpdir};
58 partitionFilePath /= partition;
59 std::ofstream partitionFile(partitionFilePath.c_str());
60
61 partitionFile.write((char*)data,sizeof(data));
62 partitionFile.close();
63
64 system_set_reserved_size(MEM_SIZE);
65 system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE);
66
67 struct mbox_context *ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
68 strcpy(ctx->paths.ro_loc,tmpdir);
Ratan Guptac0ef9872017-06-06 14:31:37 +053069 strcpy(ctx->paths.rw_loc,tmpdir);
70 strcpy(ctx->paths.prsv_loc,tmpdir);
Ratan Gupta3214b512017-05-11 08:58:19 +053071
72 vpnor_create_partition_table_from_path(ctx, tmpdir);
73
74 int rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
75 assert(rc == 1);
76
77 // send the request for partition1
78 rc = mbox_command_dispatch(ctx, create_read_window,
79 sizeof(create_read_window));
80 assert(rc == 1);
81
82 rc = mbox_cmp(ctx, response, sizeof(response));
83 assert(rc == 0);
84
85 // Compare the reserved memory to the pnor
86 rc = memcmp(ctx->mem, data, 6);
87 assert(rc == 0);
88
Ratan Gupta6a98e182017-06-06 15:04:23 +053089 //TODO: Add few more test cases for read from multiple partitions(PRSV/RW)
90 // Read beyond the partition file size.
91 // openbmc/openbmc#1868
92
Ratan Gupta3214b512017-05-11 08:58:19 +053093 fs::remove_all(fs::path{tmpdir});
94 return rc;
95}