blob: 3ea6bb546901cdc9ee7381b20e8ccd36c2daac19 [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";
Andrew Jefferyf34db312018-03-09 15:27:03 +103021uint8_t data[8] = {0xaa, 0x55, 0xaa, 0x66, 0x77, 0x88, 0x99, 0xab};
Ratan Gupta3214b512017-05-11 08:58:19 +053022
Andrew Jefferyf34db312018-03-09 15:27:03 +103023#define BLOCK_SIZE 4096
24#define MEM_SIZE (BLOCK_SIZE * 2)
25#define ERASE_SIZE BLOCK_SIZE
26#define N_WINDOWS 1
Ratan Gupta3214b512017-05-11 08:58:19 +053027#define WINDOW_SIZE BLOCK_SIZE
28
Andrew Jefferyf34db312018-03-09 15:27:03 +103029static const uint8_t get_info[] = {0x02, 0x00, 0x02, 0x00, 0x00, 0x00,
30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
31 0x00, 0x00, 0x00, 0x00};
Adriana Kobylak2ad21322017-11-28 14:59:19 -060032// offset 0x100 and size 6
Andrew Jefferyf34db312018-03-09 15:27:03 +103033static const uint8_t create_read_window[] = {0x04, 0x01, 0x01, 0x00, 0x06, 0x00,
34 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
35 0x00, 0x00, 0x00, 0x00};
Ratan Gupta3214b512017-05-11 08:58:19 +053036
Andrew Jefferyf34db312018-03-09 15:27:03 +103037static const uint8_t response[] = {0x04, 0x01, 0xfe, 0xff, 0x01, 0x00, 0x01,
38 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
Ratan Gupta3214b512017-05-11 08:58:19 +053039
40namespace fs = std::experimental::filesystem;
41
42int main()
43{
44 char* tmpdir = mkdtemp(tmplt);
45 assert(tmpdir != nullptr);
46
47 // create the toc file
48 fs::path tocFilePath{tmpdir};
49 tocFilePath /= PARTITION_TOC_FILE;
50 std::ofstream tocFile(tocFilePath.c_str());
51 tocFile.write(line, strlen(line));
52 tocFile.close();
53
54 // create the partition file
55 fs::path partitionFilePath{tmpdir};
56 partitionFilePath /= partition;
57 std::ofstream partitionFile(partitionFilePath.c_str());
58
Andrew Jefferyf34db312018-03-09 15:27:03 +103059 partitionFile.write((char*)data, sizeof(data));
Ratan Gupta3214b512017-05-11 08:58:19 +053060 partitionFile.close();
61
62 system_set_reserved_size(MEM_SIZE);
63 system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE);
64
Andrew Jefferyf34db312018-03-09 15:27:03 +103065 struct mbox_context* ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
66 strcpy(ctx->paths.ro_loc, tmpdir);
67 strcpy(ctx->paths.rw_loc, tmpdir);
68 strcpy(ctx->paths.prsv_loc, tmpdir);
Ratan Gupta3214b512017-05-11 08:58:19 +053069
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,
Andrew Jefferyf34db312018-03-09 15:27:03 +103077 sizeof(create_read_window));
Ratan Gupta3214b512017-05-11 08:58:19 +053078 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
Andrew Jefferyf34db312018-03-09 15:27:03 +103087 // TODO: Add few more test cases for read from multiple partitions(PRSV/RW)
Ratan Gupta6a98e182017-06-06 15:04:23 +053088 // Read beyond the partition file size.
89 // openbmc/openbmc#1868
90
Ratan Gupta3214b512017-05-11 08:58:19 +053091 fs::remove_all(fs::path{tmpdir});
92 return rc;
93}