14fe996c2SAndrew Jeffery // SPDX-License-Identifier: Apache-2.0
24fe996c2SAndrew Jeffery // Copyright (C) 2018 IBM Corp.
34edb58c5SAndrew Jeffery 
44edb58c5SAndrew Jeffery #include <assert.h>
54edb58c5SAndrew Jeffery #include <sys/mman.h>
64edb58c5SAndrew Jeffery 
74edb58c5SAndrew Jeffery #include "mbox.h"
8*457a6e5fSAndrew Jeffery #include "transport_mbox.h"
94edb58c5SAndrew Jeffery 
104edb58c5SAndrew Jeffery #include "test/mbox.h"
114edb58c5SAndrew Jeffery #include "test/system.h"
124edb58c5SAndrew Jeffery 
134edb58c5SAndrew Jeffery static const uint8_t get_info[] = {
144edb58c5SAndrew Jeffery 	0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
154edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
164edb58c5SAndrew Jeffery };
174edb58c5SAndrew Jeffery 
184edb58c5SAndrew Jeffery static const uint8_t create_write_window[] = {
194edb58c5SAndrew Jeffery 	0x06, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
204edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
214edb58c5SAndrew Jeffery };
224edb58c5SAndrew Jeffery 
234edb58c5SAndrew Jeffery static const uint8_t mark_write_dirty_left[] = {
244edb58c5SAndrew Jeffery 	0x07, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
254edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
264edb58c5SAndrew Jeffery };
274edb58c5SAndrew Jeffery 
284edb58c5SAndrew Jeffery static const uint8_t mark_write_dirty_right[] = {
294edb58c5SAndrew Jeffery 	0x07, 0x03, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
304edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
314edb58c5SAndrew Jeffery };
324edb58c5SAndrew Jeffery 
334edb58c5SAndrew Jeffery static const uint8_t mark_write_erase_middle[] = {
344edb58c5SAndrew Jeffery 	0x0a, 0x04, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
354edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
364edb58c5SAndrew Jeffery };
374edb58c5SAndrew Jeffery 
384edb58c5SAndrew Jeffery static const uint8_t mark_write_erase_left[] = {
394edb58c5SAndrew Jeffery 	0x0a, 0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
404edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
414edb58c5SAndrew Jeffery };
424edb58c5SAndrew Jeffery 
434edb58c5SAndrew Jeffery static const uint8_t mark_write_erase_right[] = {
444edb58c5SAndrew Jeffery 	0x0a, 0x06, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
454edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
464edb58c5SAndrew Jeffery };
474edb58c5SAndrew Jeffery 
484edb58c5SAndrew Jeffery static const uint8_t mark_write_dirty_middle[] = {
494edb58c5SAndrew Jeffery 	0x07, 0x07, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
504edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
514edb58c5SAndrew Jeffery };
524edb58c5SAndrew Jeffery 
534edb58c5SAndrew Jeffery static const uint8_t write_flush[] = {
544edb58c5SAndrew Jeffery 	0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
554edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
564edb58c5SAndrew Jeffery };
574edb58c5SAndrew Jeffery 
584edb58c5SAndrew Jeffery static const uint8_t flush_response[] = {
594edb58c5SAndrew Jeffery 	0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
604edb58c5SAndrew Jeffery 	0x00, 0x00, 0x00, 0x00, 0x00, 0x01
614edb58c5SAndrew Jeffery };
624edb58c5SAndrew Jeffery 
634edb58c5SAndrew Jeffery const uint8_t start_data[] = { 0xaa, 0x55, 0xaa };
644edb58c5SAndrew Jeffery const uint8_t flush_dirty_erased_dirty_data[] = { 0x55, 0xff, 0x55 };
654edb58c5SAndrew Jeffery const uint8_t flush_erased_dirty_erased_data[] = { 0xff, 0x55, 0xff };
664edb58c5SAndrew Jeffery 
674edb58c5SAndrew Jeffery #define MEM_SIZE	sizeof(start_data)
684edb58c5SAndrew Jeffery #define ERASE_SIZE	1
694edb58c5SAndrew Jeffery #define N_WINDOWS	1
704edb58c5SAndrew Jeffery #define WINDOW_SIZE	sizeof(start_data)
714edb58c5SAndrew Jeffery 
724edb58c5SAndrew Jeffery int main(void)
734edb58c5SAndrew Jeffery {
744edb58c5SAndrew Jeffery 	struct mbox_context *ctx;
754edb58c5SAndrew Jeffery 	uint8_t *map;
764edb58c5SAndrew Jeffery 	int rc;
774edb58c5SAndrew Jeffery 
784edb58c5SAndrew Jeffery 	system_set_reserved_size(MEM_SIZE);
794edb58c5SAndrew Jeffery 	system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE);
804edb58c5SAndrew Jeffery 
814edb58c5SAndrew Jeffery 	ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
824edb58c5SAndrew Jeffery 	rc = mbox_set_mtd_data(ctx, start_data, sizeof(start_data));
834edb58c5SAndrew Jeffery 	assert(rc == 0);
844edb58c5SAndrew Jeffery 
854edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
864edb58c5SAndrew Jeffery 	assert(rc == 1);
874edb58c5SAndrew Jeffery 
884edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, create_write_window,
894edb58c5SAndrew Jeffery 			sizeof(create_write_window));
904edb58c5SAndrew Jeffery 	assert(rc == 1);
914edb58c5SAndrew Jeffery 
924edb58c5SAndrew Jeffery 	/* { dirty, erased, dirty } */
934edb58c5SAndrew Jeffery 
944edb58c5SAndrew Jeffery 	((uint8_t *)ctx->mem)[0] = 0x55;
954edb58c5SAndrew Jeffery 
964edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, mark_write_dirty_left,
974edb58c5SAndrew Jeffery 			sizeof(mark_write_dirty_left));
984edb58c5SAndrew Jeffery 	assert(rc == 1);
994edb58c5SAndrew Jeffery 
1004edb58c5SAndrew Jeffery 	((uint8_t *)ctx->mem)[2] = 0x55;
1014edb58c5SAndrew Jeffery 
1024edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, mark_write_dirty_right,
1034edb58c5SAndrew Jeffery 			sizeof(mark_write_dirty_right));
1044edb58c5SAndrew Jeffery 	assert(rc == 1);
1054edb58c5SAndrew Jeffery 
1064edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, mark_write_erase_middle,
1074edb58c5SAndrew Jeffery 			sizeof(mark_write_erase_middle));
1084edb58c5SAndrew Jeffery 	assert(rc == 1);
1094edb58c5SAndrew Jeffery 
1104edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush));
1114edb58c5SAndrew Jeffery 	assert(rc == 1);
1124edb58c5SAndrew Jeffery 
1134edb58c5SAndrew Jeffery 	rc = mbox_cmp(ctx, flush_response, sizeof(flush_response));
1144edb58c5SAndrew Jeffery 	assert(rc == 0);
1154edb58c5SAndrew Jeffery 
1164edb58c5SAndrew Jeffery 	map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE,
1174edb58c5SAndrew Jeffery 			ctx->fds[MTD_FD].fd, 0);
1184edb58c5SAndrew Jeffery 	assert(map != MAP_FAILED);
1194edb58c5SAndrew Jeffery 
1204edb58c5SAndrew Jeffery 	rc = memcmp(flush_dirty_erased_dirty_data, map,
1214edb58c5SAndrew Jeffery 			sizeof(flush_dirty_erased_dirty_data));
1224edb58c5SAndrew Jeffery 	assert(rc == 0);
1234edb58c5SAndrew Jeffery 
1244edb58c5SAndrew Jeffery 	/* { erased, dirty, erased } */
1254edb58c5SAndrew Jeffery 
1264edb58c5SAndrew Jeffery 	((uint8_t *)ctx->mem)[1] = 0x55;
1274edb58c5SAndrew Jeffery 
1284edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, mark_write_dirty_middle,
1294edb58c5SAndrew Jeffery 			sizeof(mark_write_dirty_middle));
1304edb58c5SAndrew Jeffery 	assert(rc == 1);
1314edb58c5SAndrew Jeffery 
1324edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, mark_write_erase_left,
1334edb58c5SAndrew Jeffery 			sizeof(mark_write_erase_left));
1344edb58c5SAndrew Jeffery 	assert(rc == 1);
1354edb58c5SAndrew Jeffery 
1364edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, mark_write_erase_right,
1374edb58c5SAndrew Jeffery 			sizeof(mark_write_erase_right));
1384edb58c5SAndrew Jeffery 	assert(rc == 1);
1394edb58c5SAndrew Jeffery 
1404edb58c5SAndrew Jeffery 	rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush));
1414edb58c5SAndrew Jeffery 	assert(rc == 1);
1424edb58c5SAndrew Jeffery 
1434edb58c5SAndrew Jeffery 	rc = mbox_cmp(ctx, flush_response, sizeof(flush_response));
1444edb58c5SAndrew Jeffery 	assert(rc == 0);
1454edb58c5SAndrew Jeffery 
1464edb58c5SAndrew Jeffery 	rc = memcmp(flush_erased_dirty_erased_data, map,
1474edb58c5SAndrew Jeffery 			sizeof(flush_erased_dirty_erased_data));
1484edb58c5SAndrew Jeffery 	assert(rc == 0);
1494edb58c5SAndrew Jeffery 
1504edb58c5SAndrew Jeffery 	return rc;
1514edb58c5SAndrew Jeffery }
152