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