blob: 03e7d380c3b69e68649ecf62679294bdbfe9f7d9 [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Andrew Jeffery4edb58c2017-04-13 15:10:15 +09303
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
13static 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
18static 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
23static const uint8_t mark_write_dirty_left[] = {
24 0x07, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
25 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
26};
27
28static const uint8_t mark_write_dirty_right[] = {
29 0x07, 0x03, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
31};
32
33static const uint8_t mark_write_erase_middle[] = {
34 0x0a, 0x04, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
36};
37
38static const uint8_t mark_write_erase_left[] = {
39 0x0a, 0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
41};
42
43static const uint8_t mark_write_erase_right[] = {
44 0x0a, 0x06, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
46};
47
48static const uint8_t mark_write_dirty_middle[] = {
49 0x07, 0x07, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
50 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
51};
52
53static const uint8_t write_flush[] = {
54 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
56};
57
58static const uint8_t flush_response[] = {
59 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
61};
62
63const uint8_t start_data[] = { 0xaa, 0x55, 0xaa };
64const uint8_t flush_dirty_erased_dirty_data[] = { 0x55, 0xff, 0x55 };
65const uint8_t flush_erased_dirty_erased_data[] = { 0xff, 0x55, 0xff };
66
67#define MEM_SIZE sizeof(start_data)
68#define ERASE_SIZE 1
69#define N_WINDOWS 1
70#define WINDOW_SIZE sizeof(start_data)
71
72int main(void)
73{
74 struct mbox_context *ctx;
75 uint8_t *map;
76 int rc;
77
78 system_set_reserved_size(MEM_SIZE);
79 system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE);
80
81 ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
82 rc = mbox_set_mtd_data(ctx, start_data, sizeof(start_data));
83 assert(rc == 0);
84
85 rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
86 assert(rc == 1);
87
88 rc = mbox_command_dispatch(ctx, create_write_window,
89 sizeof(create_write_window));
90 assert(rc == 1);
91
92 /* { dirty, erased, dirty } */
93
94 ((uint8_t *)ctx->mem)[0] = 0x55;
95
96 rc = mbox_command_dispatch(ctx, mark_write_dirty_left,
97 sizeof(mark_write_dirty_left));
98 assert(rc == 1);
99
100 ((uint8_t *)ctx->mem)[2] = 0x55;
101
102 rc = mbox_command_dispatch(ctx, mark_write_dirty_right,
103 sizeof(mark_write_dirty_right));
104 assert(rc == 1);
105
106 rc = mbox_command_dispatch(ctx, mark_write_erase_middle,
107 sizeof(mark_write_erase_middle));
108 assert(rc == 1);
109
110 rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush));
111 assert(rc == 1);
112
113 rc = mbox_cmp(ctx, flush_response, sizeof(flush_response));
114 assert(rc == 0);
115
116 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE,
117 ctx->fds[MTD_FD].fd, 0);
118 assert(map != MAP_FAILED);
119
120 rc = memcmp(flush_dirty_erased_dirty_data, map,
121 sizeof(flush_dirty_erased_dirty_data));
122 assert(rc == 0);
123
124 /* { erased, dirty, erased } */
125
126 ((uint8_t *)ctx->mem)[1] = 0x55;
127
128 rc = mbox_command_dispatch(ctx, mark_write_dirty_middle,
129 sizeof(mark_write_dirty_middle));
130 assert(rc == 1);
131
132 rc = mbox_command_dispatch(ctx, mark_write_erase_left,
133 sizeof(mark_write_erase_left));
134 assert(rc == 1);
135
136 rc = mbox_command_dispatch(ctx, mark_write_erase_right,
137 sizeof(mark_write_erase_right));
138 assert(rc == 1);
139
140 rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush));
141 assert(rc == 1);
142
143 rc = mbox_cmp(ctx, flush_response, sizeof(flush_response));
144 assert(rc == 0);
145
146 rc = memcmp(flush_erased_dirty_erased_data, map,
147 sizeof(flush_erased_dirty_erased_data));
148 assert(rc == 0);
149
150 return rc;
151}