blob: f186d195098095a61044b43ced2cf1e145ff7e23 [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Andrew Jefferyd4a5fc82017-04-12 22:50:04 +09303
4#include <assert.h>
5#include <sys/mman.h>
6
Andrew Jeffery26558db2018-08-10 00:22:38 +09307#include "mboxd.h"
Evan Lojewskif1e547c2019-03-14 14:34:33 +10308#include "mtd/backend.h"
Andrew Jeffery457a6e52018-08-08 11:21:08 +09309#include "transport_mbox.h"
Andrew Jefferyd4a5fc82017-04-12 22:50:04 +093010
11#include "test/mbox.h"
12#include "test/system.h"
13
14static const uint8_t get_info[] = {
15 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
16 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
17};
18
19static const uint8_t create_write_window[] = {
20 0x06, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
21 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
22};
23
24static const uint8_t mark_write_dirty[] = {
25 0x07, 0x02, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
26 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
27};
28
29static const uint8_t create_read_window[] = {
30 0x04, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
31 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
32};
33
34static const uint8_t create_read_window_response[] = {
35 0x04, 0x03, 0xfd, 0xff, 0x03, 0x00, 0x00, 0x00,
36 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
37};
38
39static const uint8_t close_window[] = {
40 0x05, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
42};
43
44static const uint8_t close_window_response[] = {
45 0x05, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
47};
48
49const uint8_t start_data[] = { 0xaa, 0x55, 0xaa };
50const uint8_t finish_data[] = { 0xaa, 0x00, 0xaa };
51
52#define MEM_SIZE sizeof(start_data)
53#define ERASE_SIZE 1
54#define N_WINDOWS 1
55#define WINDOW_SIZE sizeof(start_data)
56
57int setup(struct mbox_context *ctx)
58{
59 int rc;
60
61 rc = mbox_set_mtd_data(ctx, start_data, sizeof(start_data));
62 assert(rc == 0);
63
64 rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
65 assert(rc == 1);
66
67 rc = mbox_command_dispatch(ctx, create_write_window,
68 sizeof(create_write_window));
69 assert(rc == 1);
70
71 return rc;
72}
73
74int flush_on_close(struct mbox_context *ctx)
75{
76 uint8_t *map;
77 int rc;
78
79 rc = setup(ctx);
80 assert(rc == 1);
81
82 ((uint8_t *)ctx->mem)[1] = 0x00;
83
84 rc = mbox_command_dispatch(ctx, mark_write_dirty,
85 sizeof(mark_write_dirty));
86 assert(rc == 1);
87
88 rc = mbox_command_dispatch(ctx, close_window, sizeof(close_window));
89 assert(rc == 1);
90
91 rc = mbox_cmp(ctx, close_window_response,
92 sizeof(close_window_response));
93 assert(rc == 0);
94
95 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE,
Evan Lojewskif1e547c2019-03-14 14:34:33 +103096 ((struct mtd_data *)ctx->backend.priv)->fd, 0);
Andrew Jefferyd4a5fc82017-04-12 22:50:04 +093097 assert(map != MAP_FAILED);
98
99 rc = memcmp(finish_data, map, sizeof(finish_data));
100 assert(rc == 0);
101
102 return rc;
103}
104
105int flush_on_create(struct mbox_context *ctx)
106{
107 uint8_t *map;
108 int rc;
109
110 rc = setup(ctx);
111 assert(rc == 1);
112
113 ((uint8_t *)ctx->mem)[1] = 0x00;
114
115 rc = mbox_command_dispatch(ctx, mark_write_dirty,
116 sizeof(mark_write_dirty));
117 assert(rc == 1);
118
119 rc = mbox_command_dispatch(ctx, create_read_window,
120 sizeof(create_read_window));
121 assert(rc == 1);
122
123 rc = mbox_cmp(ctx, create_read_window_response,
124 sizeof(create_read_window_response));
125 assert(rc == 0);
126
127 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE,
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030128 ((struct mtd_data *)ctx->backend.priv)->fd, 0);
Andrew Jefferyd4a5fc82017-04-12 22:50:04 +0930129 assert(map != MAP_FAILED);
130
131 rc = memcmp(finish_data, map, sizeof(finish_data));
132 assert(rc == 0);
133
134 return rc;
135}
136
137int main(void)
138{
139 struct mbox_context *ctx;
140
141 system_set_reserved_size(MEM_SIZE);
142 system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE);
143
144 ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
145
146 flush_on_close(ctx);
147
148 flush_on_create(ctx);
149
150 return 0;
151};