blob: 855d88d605200114cf6e3046f0c4122dcee5a887 [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
16constexpr auto line = "partition01=HBB,00000000,00000400,ECC,PRESERVED";
17constexpr 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);
69
70 vpnor_create_partition_table_from_path(ctx, tmpdir);
71
72 int rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
73 assert(rc == 1);
74
75 // send the request for partition1
76 rc = mbox_command_dispatch(ctx, create_read_window,
77 sizeof(create_read_window));
78 assert(rc == 1);
79
80 rc = mbox_cmp(ctx, response, sizeof(response));
81 assert(rc == 0);
82
83 // Compare the reserved memory to the pnor
84 rc = memcmp(ctx->mem, data, 6);
85 assert(rc == 0);
86
87 fs::remove_all(fs::path{tmpdir});
88 return rc;
89}