1*4edb58c5SAndrew Jeffery /*
2*4edb58c5SAndrew Jeffery  * MBox Daemon Test File
3*4edb58c5SAndrew Jeffery  *
4*4edb58c5SAndrew Jeffery  * Copyright 2017 IBM
5*4edb58c5SAndrew Jeffery  *
6*4edb58c5SAndrew Jeffery  * Licensed under the Apache License, Version 2.0 (the "License");
7*4edb58c5SAndrew Jeffery  * you may not use this file except in compliance with the License.
8*4edb58c5SAndrew Jeffery  * You may obtain a copy of the License at
9*4edb58c5SAndrew Jeffery  *
10*4edb58c5SAndrew Jeffery  * http://www.apache.org/licenses/LICENSE-2.0
11*4edb58c5SAndrew Jeffery  *
12*4edb58c5SAndrew Jeffery  * Unless required by applicable law or agreed to in writing, software
13*4edb58c5SAndrew Jeffery  * distributed under the License is distributed on an "AS IS" BASIS,
14*4edb58c5SAndrew Jeffery  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15*4edb58c5SAndrew Jeffery  * See the License for the specific language governing permissions and
16*4edb58c5SAndrew Jeffery  * limitations under the License.
17*4edb58c5SAndrew Jeffery  *
18*4edb58c5SAndrew Jeffery  */
19*4edb58c5SAndrew Jeffery 
20*4edb58c5SAndrew Jeffery #include <assert.h>
21*4edb58c5SAndrew Jeffery #include <sys/mman.h>
22*4edb58c5SAndrew Jeffery 
23*4edb58c5SAndrew Jeffery #include "mbox.h"
24*4edb58c5SAndrew Jeffery #include "mboxd_msg.h"
25*4edb58c5SAndrew Jeffery 
26*4edb58c5SAndrew Jeffery #include "test/mbox.h"
27*4edb58c5SAndrew Jeffery #include "test/system.h"
28*4edb58c5SAndrew Jeffery 
29*4edb58c5SAndrew Jeffery static const uint8_t get_info[] = {
30*4edb58c5SAndrew Jeffery 	0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
31*4edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
32*4edb58c5SAndrew Jeffery };
33*4edb58c5SAndrew Jeffery 
34*4edb58c5SAndrew Jeffery static const uint8_t create_write_window[] = {
35*4edb58c5SAndrew Jeffery 	0x06, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
36*4edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
37*4edb58c5SAndrew Jeffery };
38*4edb58c5SAndrew Jeffery 
39*4edb58c5SAndrew Jeffery static const uint8_t mark_write_dirty_left[] = {
40*4edb58c5SAndrew Jeffery 	0x07, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
41*4edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
42*4edb58c5SAndrew Jeffery };
43*4edb58c5SAndrew Jeffery 
44*4edb58c5SAndrew Jeffery static const uint8_t mark_write_dirty_right[] = {
45*4edb58c5SAndrew Jeffery 	0x07, 0x03, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
46*4edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
47*4edb58c5SAndrew Jeffery };
48*4edb58c5SAndrew Jeffery 
49*4edb58c5SAndrew Jeffery static const uint8_t mark_write_erase_middle[] = {
50*4edb58c5SAndrew Jeffery 	0x0a, 0x04, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
51*4edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
52*4edb58c5SAndrew Jeffery };
53*4edb58c5SAndrew Jeffery 
54*4edb58c5SAndrew Jeffery static const uint8_t mark_write_erase_left[] = {
55*4edb58c5SAndrew Jeffery 	0x0a, 0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
56*4edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
57*4edb58c5SAndrew Jeffery };
58*4edb58c5SAndrew Jeffery 
59*4edb58c5SAndrew Jeffery static const uint8_t mark_write_erase_right[] = {
60*4edb58c5SAndrew Jeffery 	0x0a, 0x06, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
61*4edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
62*4edb58c5SAndrew Jeffery };
63*4edb58c5SAndrew Jeffery 
64*4edb58c5SAndrew Jeffery static const uint8_t mark_write_dirty_middle[] = {
65*4edb58c5SAndrew Jeffery 	0x07, 0x07, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
66*4edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
67*4edb58c5SAndrew Jeffery };
68*4edb58c5SAndrew Jeffery 
69*4edb58c5SAndrew Jeffery static const uint8_t write_flush[] = {
70*4edb58c5SAndrew Jeffery 	0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71*4edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
72*4edb58c5SAndrew Jeffery };
73*4edb58c5SAndrew Jeffery 
74*4edb58c5SAndrew Jeffery static const uint8_t flush_response[] = {
75*4edb58c5SAndrew Jeffery 	0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76*4edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x01
77*4edb58c5SAndrew Jeffery };
78*4edb58c5SAndrew Jeffery 
79*4edb58c5SAndrew Jeffery const uint8_t start_data[] = { 0xaa, 0x55, 0xaa };
80*4edb58c5SAndrew Jeffery const uint8_t flush_dirty_erased_dirty_data[] = { 0x55, 0xff, 0x55 };
81*4edb58c5SAndrew Jeffery const uint8_t flush_erased_dirty_erased_data[] = { 0xff, 0x55, 0xff };
82*4edb58c5SAndrew Jeffery 
83*4edb58c5SAndrew Jeffery #define MEM_SIZE	sizeof(start_data)
84*4edb58c5SAndrew Jeffery #define ERASE_SIZE	1
85*4edb58c5SAndrew Jeffery #define N_WINDOWS	1
86*4edb58c5SAndrew Jeffery #define WINDOW_SIZE	sizeof(start_data)
87*4edb58c5SAndrew Jeffery 
88*4edb58c5SAndrew Jeffery int main(void)
89*4edb58c5SAndrew Jeffery {
90*4edb58c5SAndrew Jeffery 	struct mbox_context *ctx;
91*4edb58c5SAndrew Jeffery 	uint8_t *map;
92*4edb58c5SAndrew Jeffery 	int rc;
93*4edb58c5SAndrew Jeffery 
94*4edb58c5SAndrew Jeffery 	system_set_reserved_size(MEM_SIZE);
95*4edb58c5SAndrew Jeffery 	system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE);
96*4edb58c5SAndrew Jeffery 
97*4edb58c5SAndrew Jeffery 	ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
98*4edb58c5SAndrew Jeffery 	rc = mbox_set_mtd_data(ctx, start_data, sizeof(start_data));
99*4edb58c5SAndrew Jeffery 	assert(rc == 0);
100*4edb58c5SAndrew Jeffery 
101*4edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
102*4edb58c5SAndrew Jeffery 	assert(rc == 1);
103*4edb58c5SAndrew Jeffery 
104*4edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, create_write_window,
105*4edb58c5SAndrew Jeffery 			sizeof(create_write_window));
106*4edb58c5SAndrew Jeffery 	assert(rc == 1);
107*4edb58c5SAndrew Jeffery 
108*4edb58c5SAndrew Jeffery 	/* { dirty, erased, dirty } */
109*4edb58c5SAndrew Jeffery 
110*4edb58c5SAndrew Jeffery 	((uint8_t *)ctx->mem)[0] = 0x55;
111*4edb58c5SAndrew Jeffery 
112*4edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, mark_write_dirty_left,
113*4edb58c5SAndrew Jeffery 			sizeof(mark_write_dirty_left));
114*4edb58c5SAndrew Jeffery 	assert(rc == 1);
115*4edb58c5SAndrew Jeffery 
116*4edb58c5SAndrew Jeffery 	((uint8_t *)ctx->mem)[2] = 0x55;
117*4edb58c5SAndrew Jeffery 
118*4edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, mark_write_dirty_right,
119*4edb58c5SAndrew Jeffery 			sizeof(mark_write_dirty_right));
120*4edb58c5SAndrew Jeffery 	assert(rc == 1);
121*4edb58c5SAndrew Jeffery 
122*4edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, mark_write_erase_middle,
123*4edb58c5SAndrew Jeffery 			sizeof(mark_write_erase_middle));
124*4edb58c5SAndrew Jeffery 	assert(rc == 1);
125*4edb58c5SAndrew Jeffery 
126*4edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush));
127*4edb58c5SAndrew Jeffery 	assert(rc == 1);
128*4edb58c5SAndrew Jeffery 
129*4edb58c5SAndrew Jeffery 	rc = mbox_cmp(ctx, flush_response, sizeof(flush_response));
130*4edb58c5SAndrew Jeffery 	assert(rc == 0);
131*4edb58c5SAndrew Jeffery 
132*4edb58c5SAndrew Jeffery 	map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE,
133*4edb58c5SAndrew Jeffery 			ctx->fds[MTD_FD].fd, 0);
134*4edb58c5SAndrew Jeffery 	assert(map != MAP_FAILED);
135*4edb58c5SAndrew Jeffery 
136*4edb58c5SAndrew Jeffery 	rc = memcmp(flush_dirty_erased_dirty_data, map,
137*4edb58c5SAndrew Jeffery 			sizeof(flush_dirty_erased_dirty_data));
138*4edb58c5SAndrew Jeffery 	assert(rc == 0);
139*4edb58c5SAndrew Jeffery 
140*4edb58c5SAndrew Jeffery 	/* { erased, dirty, erased } */
141*4edb58c5SAndrew Jeffery 
142*4edb58c5SAndrew Jeffery 	((uint8_t *)ctx->mem)[1] = 0x55;
143*4edb58c5SAndrew Jeffery 
144*4edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, mark_write_dirty_middle,
145*4edb58c5SAndrew Jeffery 			sizeof(mark_write_dirty_middle));
146*4edb58c5SAndrew Jeffery 	assert(rc == 1);
147*4edb58c5SAndrew Jeffery 
148*4edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, mark_write_erase_left,
149*4edb58c5SAndrew Jeffery 			sizeof(mark_write_erase_left));
150*4edb58c5SAndrew Jeffery 	assert(rc == 1);
151*4edb58c5SAndrew Jeffery 
152*4edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, mark_write_erase_right,
153*4edb58c5SAndrew Jeffery 			sizeof(mark_write_erase_right));
154*4edb58c5SAndrew Jeffery 	assert(rc == 1);
155*4edb58c5SAndrew Jeffery 
156*4edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush));
157*4edb58c5SAndrew Jeffery 	assert(rc == 1);
158*4edb58c5SAndrew Jeffery 
159*4edb58c5SAndrew Jeffery 	rc = mbox_cmp(ctx, flush_response, sizeof(flush_response));
160*4edb58c5SAndrew Jeffery 	assert(rc == 0);
161*4edb58c5SAndrew Jeffery 
162*4edb58c5SAndrew Jeffery 	rc = memcmp(flush_erased_dirty_erased_data, map,
163*4edb58c5SAndrew Jeffery 			sizeof(flush_erased_dirty_erased_data));
164*4edb58c5SAndrew Jeffery 	assert(rc == 0);
165*4edb58c5SAndrew Jeffery 
166*4edb58c5SAndrew Jeffery 	return rc;
167*4edb58c5SAndrew Jeffery }
168