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