14fe996c2SAndrew Jeffery // SPDX-License-Identifier: Apache-2.0 24fe996c2SAndrew Jeffery // Copyright (C) 2018 IBM Corp. 31a03647eSAndrew Jeffery 41a03647eSAndrew Jeffery #include <assert.h> 51a03647eSAndrew Jeffery #include <sys/mman.h> 61a03647eSAndrew Jeffery 71a03647eSAndrew Jeffery #include "mbox.h" 8*457a6e5fSAndrew Jeffery #include "transport_mbox.h" 91a03647eSAndrew Jeffery 101a03647eSAndrew Jeffery #include "test/mbox.h" 111a03647eSAndrew Jeffery #include "test/system.h" 121a03647eSAndrew Jeffery 131a03647eSAndrew Jeffery static const uint8_t get_info[] = { 141a03647eSAndrew Jeffery 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 151a03647eSAndrew Jeffery 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 161a03647eSAndrew Jeffery }; 171a03647eSAndrew Jeffery 181a03647eSAndrew Jeffery static const uint8_t create_write_window[] = { 191a03647eSAndrew Jeffery 0x06, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 201a03647eSAndrew Jeffery 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 211a03647eSAndrew Jeffery }; 221a03647eSAndrew Jeffery 231a03647eSAndrew Jeffery static const uint8_t mark_write_dirty_middle[] = { 241a03647eSAndrew Jeffery 0x07, 0x02, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 251a03647eSAndrew Jeffery 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 261a03647eSAndrew Jeffery }; 271a03647eSAndrew Jeffery 281a03647eSAndrew Jeffery static const uint8_t write_flush[] = { 291a03647eSAndrew Jeffery 0x08, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 301a03647eSAndrew Jeffery 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 311a03647eSAndrew Jeffery }; 321a03647eSAndrew Jeffery 331a03647eSAndrew Jeffery static const uint8_t response[] = { 341a03647eSAndrew Jeffery 0x08, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 351a03647eSAndrew Jeffery 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 361a03647eSAndrew Jeffery }; 371a03647eSAndrew Jeffery 381a03647eSAndrew Jeffery static const uint8_t mark_write_dirty_left[] = { 391a03647eSAndrew Jeffery 0x07, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 401a03647eSAndrew Jeffery 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 411a03647eSAndrew Jeffery }; 421a03647eSAndrew Jeffery 431a03647eSAndrew Jeffery static const uint8_t mark_write_dirty_right[] = { 441a03647eSAndrew Jeffery 0x07, 0x05, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 451a03647eSAndrew Jeffery 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 461a03647eSAndrew Jeffery }; 471a03647eSAndrew Jeffery 481a03647eSAndrew Jeffery static const uint8_t mark_write_dirty_all[] = { 491a03647eSAndrew Jeffery 0x07, 0x06, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 501a03647eSAndrew Jeffery 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 511a03647eSAndrew Jeffery }; 521a03647eSAndrew Jeffery 531a03647eSAndrew Jeffery const uint8_t start_data[] = { 0xaa, 0x55, 0xaa }; 541a03647eSAndrew Jeffery const uint8_t flush_middle_data[] = { 0xaa, 0x00, 0xaa }; 551a03647eSAndrew Jeffery const uint8_t flush_ends_data[] = { 0x55, 0x00, 0x55 }; 561a03647eSAndrew Jeffery const uint8_t flush_all_data[] = { 0x01, 0x02, 0x03 }; 571a03647eSAndrew Jeffery 581a03647eSAndrew Jeffery #define MEM_SIZE sizeof(start_data) 591a03647eSAndrew Jeffery #define ERASE_SIZE 1 601a03647eSAndrew Jeffery #define N_WINDOWS 1 611a03647eSAndrew Jeffery #define WINDOW_SIZE sizeof(start_data) 621a03647eSAndrew Jeffery 631a03647eSAndrew Jeffery int main(void) 641a03647eSAndrew Jeffery { 651a03647eSAndrew Jeffery struct mbox_context *ctx; 661a03647eSAndrew Jeffery uint8_t *map; 671a03647eSAndrew Jeffery int rc; 681a03647eSAndrew Jeffery 691a03647eSAndrew Jeffery system_set_reserved_size(MEM_SIZE); 701a03647eSAndrew Jeffery system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE); 711a03647eSAndrew Jeffery 721a03647eSAndrew Jeffery ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE); 731a03647eSAndrew Jeffery rc = mbox_set_mtd_data(ctx, start_data, sizeof(start_data)); 741a03647eSAndrew Jeffery assert(rc == 0); 751a03647eSAndrew Jeffery 761a03647eSAndrew Jeffery rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info)); 771a03647eSAndrew Jeffery assert(rc == 1); 781a03647eSAndrew Jeffery 791a03647eSAndrew Jeffery rc = mbox_command_dispatch(ctx, create_write_window, 801a03647eSAndrew Jeffery sizeof(create_write_window)); 811a03647eSAndrew Jeffery assert(rc == 1); 821a03647eSAndrew Jeffery 831a03647eSAndrew Jeffery /* { clean, dirty, clean } */ 841a03647eSAndrew Jeffery 851a03647eSAndrew Jeffery ((uint8_t *)ctx->mem)[1] = 0x00; 861a03647eSAndrew Jeffery 871a03647eSAndrew Jeffery rc = mbox_command_dispatch(ctx, mark_write_dirty_middle, 881a03647eSAndrew Jeffery sizeof(mark_write_dirty_middle)); 891a03647eSAndrew Jeffery assert(rc == 1); 901a03647eSAndrew Jeffery 911a03647eSAndrew Jeffery rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush)); 921a03647eSAndrew Jeffery assert(rc == 1); 931a03647eSAndrew Jeffery 941a03647eSAndrew Jeffery rc = mbox_cmp(ctx, response, sizeof(response)); 951a03647eSAndrew Jeffery assert(rc == 0); 961a03647eSAndrew Jeffery 971a03647eSAndrew Jeffery map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, 981a03647eSAndrew Jeffery ctx->fds[MTD_FD].fd, 0); 991a03647eSAndrew Jeffery assert(map != MAP_FAILED); 1001a03647eSAndrew Jeffery 1011a03647eSAndrew Jeffery rc = memcmp(flush_middle_data, map, sizeof(flush_middle_data)); 1021a03647eSAndrew Jeffery assert(rc == 0); 1031a03647eSAndrew Jeffery 1041a03647eSAndrew Jeffery /* { dirty, clean, dirty } */ 1051a03647eSAndrew Jeffery 1061a03647eSAndrew Jeffery ((uint8_t *)ctx->mem)[0] = 0x55; 1071a03647eSAndrew Jeffery 1081a03647eSAndrew Jeffery rc = mbox_command_dispatch(ctx, mark_write_dirty_left, 1091a03647eSAndrew Jeffery sizeof(mark_write_dirty_left)); 1101a03647eSAndrew Jeffery assert(rc == 1); 1111a03647eSAndrew Jeffery 1121a03647eSAndrew Jeffery ((uint8_t *)ctx->mem)[2] = 0x55; 1131a03647eSAndrew Jeffery 1141a03647eSAndrew Jeffery rc = mbox_command_dispatch(ctx, mark_write_dirty_right, 1151a03647eSAndrew Jeffery sizeof(mark_write_dirty_right)); 1161a03647eSAndrew Jeffery assert(rc == 1); 1171a03647eSAndrew Jeffery 1181a03647eSAndrew Jeffery rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush)); 1191a03647eSAndrew Jeffery assert(rc == 1); 1201a03647eSAndrew Jeffery 1211a03647eSAndrew Jeffery rc = mbox_cmp(ctx, response, sizeof(response)); 1221a03647eSAndrew Jeffery assert(rc == 0); 1231a03647eSAndrew Jeffery 1241a03647eSAndrew Jeffery rc = memcmp(flush_ends_data, map, sizeof(flush_ends_data)); 1251a03647eSAndrew Jeffery assert(rc == 0); 1261a03647eSAndrew Jeffery 1271a03647eSAndrew Jeffery /* { dirty, dirty, dirty } */ 1281a03647eSAndrew Jeffery 1291a03647eSAndrew Jeffery memcpy(ctx->mem, flush_all_data, sizeof(flush_all_data)); 1301a03647eSAndrew Jeffery 1311a03647eSAndrew Jeffery rc = mbox_command_dispatch(ctx, mark_write_dirty_all, 1321a03647eSAndrew Jeffery sizeof(mark_write_dirty_all)); 1331a03647eSAndrew Jeffery assert(rc == 1); 1341a03647eSAndrew Jeffery 1351a03647eSAndrew Jeffery rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush)); 1361a03647eSAndrew Jeffery assert(rc == 1); 1371a03647eSAndrew Jeffery 1381a03647eSAndrew Jeffery rc = mbox_cmp(ctx, response, sizeof(response)); 1391a03647eSAndrew Jeffery assert(rc == 0); 1401a03647eSAndrew Jeffery 1411a03647eSAndrew Jeffery rc = memcmp(flush_all_data, map, sizeof(flush_all_data)); 1421a03647eSAndrew Jeffery assert(rc == 0); 1431a03647eSAndrew Jeffery 1441a03647eSAndrew Jeffery return rc; 1451a03647eSAndrew Jeffery }; 146