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