blob: 7c344f7f8ebc2c787505dfe1b835b238ffea5d71 [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
Adriana Kobylak2ad21322017-11-28 14:59:19 -060016// A read window assumes that the toc is located at offset 0,
17// so create dummy partition at arbitrary offset 0x100.
18constexpr auto line = "partition01=HBB,00000100,0001000,ECC,PRESERVED";
Ratan Gupta3214b512017-05-11 08:58:19 +053019constexpr auto partition = "HBB";
20char tmplt[] = "/tmp/create_read_test.XXXXXX";
21uint8_t data[8] = { 0xaa, 0x55, 0xaa, 0x66, 0x77, 0x88, 0x99, 0xab };
22
23#define BLOCK_SIZE 4096
24#define MEM_SIZE (BLOCK_SIZE *2)
25#define ERASE_SIZE BLOCK_SIZE
26#define N_WINDOWS 1
27#define WINDOW_SIZE BLOCK_SIZE
28
29static const uint8_t get_info[] = {
30 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
31 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
32};
Adriana Kobylak2ad21322017-11-28 14:59:19 -060033// offset 0x100 and size 6
Ratan Gupta3214b512017-05-11 08:58:19 +053034static const uint8_t create_read_window[] = {
35 0x04, 0x01, 0x01, 0x00, 0x06, 0x00, 0x00, 0x00,
36 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
37};
38
39static const uint8_t response[] = {
40 0x04, 0x01, 0xfe, 0xff, 0x01, 0x00, 0x01, 0x00,
41 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
42};
43
44namespace fs = std::experimental::filesystem;
45
46int main()
47{
48 char* tmpdir = mkdtemp(tmplt);
49 assert(tmpdir != nullptr);
50
51 // create the toc file
52 fs::path tocFilePath{tmpdir};
53 tocFilePath /= PARTITION_TOC_FILE;
54 std::ofstream tocFile(tocFilePath.c_str());
55 tocFile.write(line, strlen(line));
56 tocFile.close();
57
58 // create the partition file
59 fs::path partitionFilePath{tmpdir};
60 partitionFilePath /= partition;
61 std::ofstream partitionFile(partitionFilePath.c_str());
62
63 partitionFile.write((char*)data,sizeof(data));
64 partitionFile.close();
65
66 system_set_reserved_size(MEM_SIZE);
67 system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE);
68
69 struct mbox_context *ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
70 strcpy(ctx->paths.ro_loc,tmpdir);
Ratan Guptac0ef9872017-06-06 14:31:37 +053071 strcpy(ctx->paths.rw_loc,tmpdir);
72 strcpy(ctx->paths.prsv_loc,tmpdir);
Ratan Gupta3214b512017-05-11 08:58:19 +053073
74 vpnor_create_partition_table_from_path(ctx, tmpdir);
75
76 int rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
77 assert(rc == 1);
78
79 // send the request for partition1
80 rc = mbox_command_dispatch(ctx, create_read_window,
81 sizeof(create_read_window));
82 assert(rc == 1);
83
84 rc = mbox_cmp(ctx, response, sizeof(response));
85 assert(rc == 0);
86
87 // Compare the reserved memory to the pnor
88 rc = memcmp(ctx->mem, data, 6);
89 assert(rc == 0);
90
Ratan Gupta6a98e182017-06-06 15:04:23 +053091 //TODO: Add few more test cases for read from multiple partitions(PRSV/RW)
92 // Read beyond the partition file size.
93 // openbmc/openbmc#1868
94
Ratan Gupta3214b512017-05-11 08:58:19 +053095 fs::remove_all(fs::path{tmpdir});
96 return rc;
97}