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_left[] = { 24 0x07, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 25 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 26 }; 27 28 static const uint8_t mark_write_dirty_right[] = { 29 0x07, 0x03, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 31 }; 32 33 static const uint8_t mark_write_erase_middle[] = { 34 0x0a, 0x04, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 36 }; 37 38 static const uint8_t mark_write_erase_left[] = { 39 0x0a, 0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 41 }; 42 43 static const uint8_t mark_write_erase_right[] = { 44 0x0a, 0x06, 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_middle[] = { 49 0x07, 0x07, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 50 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 51 }; 52 53 static const uint8_t write_flush[] = { 54 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 56 }; 57 58 static const uint8_t flush_response[] = { 59 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 60 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 61 }; 62 63 const uint8_t start_data[] = { 0xaa, 0x55, 0xaa }; 64 const uint8_t flush_dirty_erased_dirty_data[] = { 0x55, 0xff, 0x55 }; 65 const uint8_t flush_erased_dirty_erased_data[] = { 0xff, 0x55, 0xff }; 66 67 #define MEM_SIZE sizeof(start_data) 68 #define ERASE_SIZE 1 69 #define N_WINDOWS 1 70 #define WINDOW_SIZE sizeof(start_data) 71 72 int main(void) 73 { 74 struct mbox_context *ctx; 75 uint8_t *map; 76 int rc; 77 78 system_set_reserved_size(MEM_SIZE); 79 system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE); 80 81 ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE); 82 rc = mbox_set_mtd_data(ctx, start_data, sizeof(start_data)); 83 assert(rc == 0); 84 85 rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info)); 86 assert(rc == 1); 87 88 rc = mbox_command_dispatch(ctx, create_write_window, 89 sizeof(create_write_window)); 90 assert(rc == 1); 91 92 /* { dirty, erased, dirty } */ 93 94 ((uint8_t *)ctx->mem)[0] = 0x55; 95 96 rc = mbox_command_dispatch(ctx, mark_write_dirty_left, 97 sizeof(mark_write_dirty_left)); 98 assert(rc == 1); 99 100 ((uint8_t *)ctx->mem)[2] = 0x55; 101 102 rc = mbox_command_dispatch(ctx, mark_write_dirty_right, 103 sizeof(mark_write_dirty_right)); 104 assert(rc == 1); 105 106 rc = mbox_command_dispatch(ctx, mark_write_erase_middle, 107 sizeof(mark_write_erase_middle)); 108 assert(rc == 1); 109 110 rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush)); 111 assert(rc == 1); 112 113 rc = mbox_cmp(ctx, flush_response, sizeof(flush_response)); 114 assert(rc == 0); 115 116 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, 117 ctx->fds[MTD_FD].fd, 0); 118 assert(map != MAP_FAILED); 119 120 rc = memcmp(flush_dirty_erased_dirty_data, map, 121 sizeof(flush_dirty_erased_dirty_data)); 122 assert(rc == 0); 123 124 /* { erased, dirty, erased } */ 125 126 ((uint8_t *)ctx->mem)[1] = 0x55; 127 128 rc = mbox_command_dispatch(ctx, mark_write_dirty_middle, 129 sizeof(mark_write_dirty_middle)); 130 assert(rc == 1); 131 132 rc = mbox_command_dispatch(ctx, mark_write_erase_left, 133 sizeof(mark_write_erase_left)); 134 assert(rc == 1); 135 136 rc = mbox_command_dispatch(ctx, mark_write_erase_right, 137 sizeof(mark_write_erase_right)); 138 assert(rc == 1); 139 140 rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush)); 141 assert(rc == 1); 142 143 rc = mbox_cmp(ctx, flush_response, sizeof(flush_response)); 144 assert(rc == 0); 145 146 rc = memcmp(flush_erased_dirty_erased_data, map, 147 sizeof(flush_erased_dirty_erased_data)); 148 assert(rc == 0); 149 150 return rc; 151 } 152