1*4fe996c2SAndrew Jeffery // SPDX-License-Identifier: Apache-2.0
2*4fe996c2SAndrew Jeffery // Copyright (C) 2018 IBM Corp.
3c5da1489SAndrew Jeffery
4c5da1489SAndrew Jeffery #include <assert.h>
5c5da1489SAndrew Jeffery #include <sys/mman.h>
6c5da1489SAndrew Jeffery
7c5da1489SAndrew Jeffery #include "mbox.h"
8c5da1489SAndrew Jeffery #include "mboxd_msg.h"
9c5da1489SAndrew Jeffery
10c5da1489SAndrew Jeffery #include "test/mbox.h"
11c5da1489SAndrew Jeffery #include "test/system.h"
12c5da1489SAndrew Jeffery
13c5da1489SAndrew Jeffery static const uint8_t get_info[] = {
14c5da1489SAndrew Jeffery 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
15c5da1489SAndrew Jeffery 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
16c5da1489SAndrew Jeffery };
17c5da1489SAndrew Jeffery
18c5da1489SAndrew Jeffery static const uint8_t create_write_window[] = {
19c5da1489SAndrew Jeffery 0x06, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
20c5da1489SAndrew Jeffery 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
21c5da1489SAndrew Jeffery };
22c5da1489SAndrew Jeffery
23c5da1489SAndrew Jeffery static const uint8_t mark_write_erased[] = {
24c5da1489SAndrew Jeffery 0x0a, 0x02, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
25c5da1489SAndrew Jeffery 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
26c5da1489SAndrew Jeffery };
27c5da1489SAndrew Jeffery
28c5da1489SAndrew Jeffery static const uint8_t response[] = {
29c5da1489SAndrew Jeffery 0x0a, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30c5da1489SAndrew Jeffery 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
31c5da1489SAndrew Jeffery };
32c5da1489SAndrew Jeffery
33c5da1489SAndrew Jeffery static const uint8_t write_flush[] = {
34c5da1489SAndrew Jeffery 0x08, 0x03, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
35c5da1489SAndrew Jeffery 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
36c5da1489SAndrew Jeffery };
37c5da1489SAndrew Jeffery
38c5da1489SAndrew Jeffery const uint8_t start_data[] = { 0xaa, 0x55, 0xaa };
39c5da1489SAndrew Jeffery const uint8_t finish_data[] = { 0xaa, 0xff, 0xaa };
40c5da1489SAndrew Jeffery
41c5da1489SAndrew Jeffery #define MEM_SIZE sizeof(start_data)
42c5da1489SAndrew Jeffery #define ERASE_SIZE 1
43c5da1489SAndrew Jeffery #define N_WINDOWS 1
44c5da1489SAndrew Jeffery #define WINDOW_SIZE sizeof(start_data)
45c5da1489SAndrew Jeffery
main(void)46c5da1489SAndrew Jeffery int main(void)
47c5da1489SAndrew Jeffery {
48c5da1489SAndrew Jeffery struct mbox_context *ctx;
49c5da1489SAndrew Jeffery uint8_t *map;
50c5da1489SAndrew Jeffery int rc;
51c5da1489SAndrew Jeffery
52c5da1489SAndrew Jeffery system_set_reserved_size(MEM_SIZE);
53c5da1489SAndrew Jeffery system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE);
54c5da1489SAndrew Jeffery
55c5da1489SAndrew Jeffery ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
56c5da1489SAndrew Jeffery rc = mbox_set_mtd_data(ctx, start_data, sizeof(start_data));
57c5da1489SAndrew Jeffery assert(rc == 0);
58c5da1489SAndrew Jeffery
59c5da1489SAndrew Jeffery rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
60c5da1489SAndrew Jeffery assert(rc == 1);
61c5da1489SAndrew Jeffery
62c5da1489SAndrew Jeffery rc = mbox_command_dispatch(ctx, create_write_window,
63c5da1489SAndrew Jeffery sizeof(create_write_window));
64c5da1489SAndrew Jeffery assert(rc == 1);
65c5da1489SAndrew Jeffery
66c5da1489SAndrew Jeffery rc = mbox_command_dispatch(ctx, mark_write_erased,
67c5da1489SAndrew Jeffery sizeof(mark_write_erased));
68c5da1489SAndrew Jeffery assert(rc == 1);
69c5da1489SAndrew Jeffery
70c5da1489SAndrew Jeffery rc = mbox_cmp(ctx, response, sizeof(response));
71c5da1489SAndrew Jeffery assert(rc == 0);
72c5da1489SAndrew Jeffery
73c5da1489SAndrew Jeffery rc = memcmp(ctx->mem, finish_data, sizeof(finish_data));
74c5da1489SAndrew Jeffery assert(rc == 0);
75c5da1489SAndrew Jeffery
76c5da1489SAndrew Jeffery map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE,
77c5da1489SAndrew Jeffery ctx->fds[MTD_FD].fd, 0);
78c5da1489SAndrew Jeffery assert(map != MAP_FAILED);
79c5da1489SAndrew Jeffery
80c5da1489SAndrew Jeffery rc = memcmp(start_data, map, sizeof(start_data));
81c5da1489SAndrew Jeffery assert(rc == 0);
82c5da1489SAndrew Jeffery
83c5da1489SAndrew Jeffery rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush));
84c5da1489SAndrew Jeffery assert(rc == 1);
85c5da1489SAndrew Jeffery
86c5da1489SAndrew Jeffery rc = memcmp(finish_data, map, sizeof(finish_data));
87c5da1489SAndrew Jeffery assert(rc == 0);
88c5da1489SAndrew Jeffery
89c5da1489SAndrew Jeffery return rc;
90c5da1489SAndrew Jeffery };
91