1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright (C) 2018 IBM Corp. 3 4 #include <assert.h> 5 #include <sys/mman.h> 6 7 #include "mbox.h" 8 #include "mboxd_msg.h" 9 10 #include "test/mbox.h" 11 #include "test/system.h" 12 13 static const uint8_t get_info[] = { 14 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 15 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 16 }; 17 18 static const uint8_t create_write_window[] = { 19 0x06, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 20 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 21 }; 22 23 static const uint8_t mark_write_dirty_middle[] = { 24 0x07, 0x02, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 25 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 26 }; 27 28 static const uint8_t write_flush[] = { 29 0x08, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 31 }; 32 33 static const uint8_t response[] = { 34 0x08, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 35 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 36 }; 37 38 static const uint8_t mark_write_dirty_left[] = { 39 0x07, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 41 }; 42 43 static const uint8_t mark_write_dirty_right[] = { 44 0x07, 0x05, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 46 }; 47 48 static const uint8_t mark_write_dirty_all[] = { 49 0x07, 0x06, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 50 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 51 }; 52 53 const uint8_t start_data[] = { 0xaa, 0x55, 0xaa }; 54 const uint8_t flush_middle_data[] = { 0xaa, 0x00, 0xaa }; 55 const uint8_t flush_ends_data[] = { 0x55, 0x00, 0x55 }; 56 const uint8_t flush_all_data[] = { 0x01, 0x02, 0x03 }; 57 58 #define MEM_SIZE sizeof(start_data) 59 #define ERASE_SIZE 1 60 #define N_WINDOWS 1 61 #define WINDOW_SIZE sizeof(start_data) 62 63 int main(void) 64 { 65 struct mbox_context *ctx; 66 uint8_t *map; 67 int rc; 68 69 system_set_reserved_size(MEM_SIZE); 70 system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE); 71 72 ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE); 73 rc = mbox_set_mtd_data(ctx, start_data, sizeof(start_data)); 74 assert(rc == 0); 75 76 rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info)); 77 assert(rc == 1); 78 79 rc = mbox_command_dispatch(ctx, create_write_window, 80 sizeof(create_write_window)); 81 assert(rc == 1); 82 83 /* { clean, dirty, clean } */ 84 85 ((uint8_t *)ctx->mem)[1] = 0x00; 86 87 rc = mbox_command_dispatch(ctx, mark_write_dirty_middle, 88 sizeof(mark_write_dirty_middle)); 89 assert(rc == 1); 90 91 rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush)); 92 assert(rc == 1); 93 94 rc = mbox_cmp(ctx, response, sizeof(response)); 95 assert(rc == 0); 96 97 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, 98 ctx->fds[MTD_FD].fd, 0); 99 assert(map != MAP_FAILED); 100 101 rc = memcmp(flush_middle_data, map, sizeof(flush_middle_data)); 102 assert(rc == 0); 103 104 /* { dirty, clean, dirty } */ 105 106 ((uint8_t *)ctx->mem)[0] = 0x55; 107 108 rc = mbox_command_dispatch(ctx, mark_write_dirty_left, 109 sizeof(mark_write_dirty_left)); 110 assert(rc == 1); 111 112 ((uint8_t *)ctx->mem)[2] = 0x55; 113 114 rc = mbox_command_dispatch(ctx, mark_write_dirty_right, 115 sizeof(mark_write_dirty_right)); 116 assert(rc == 1); 117 118 rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush)); 119 assert(rc == 1); 120 121 rc = mbox_cmp(ctx, response, sizeof(response)); 122 assert(rc == 0); 123 124 rc = memcmp(flush_ends_data, map, sizeof(flush_ends_data)); 125 assert(rc == 0); 126 127 /* { dirty, dirty, dirty } */ 128 129 memcpy(ctx->mem, flush_all_data, sizeof(flush_all_data)); 130 131 rc = mbox_command_dispatch(ctx, mark_write_dirty_all, 132 sizeof(mark_write_dirty_all)); 133 assert(rc == 1); 134 135 rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush)); 136 assert(rc == 1); 137 138 rc = mbox_cmp(ctx, response, sizeof(response)); 139 assert(rc == 0); 140 141 rc = memcmp(flush_all_data, map, sizeof(flush_all_data)); 142 assert(rc == 0); 143 144 return rc; 145 }; 146