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 "mboxd.h" 8 #include "mtd/backend.h" 9 #include "transport_mbox.h" 10 11 #include "test/mbox.h" 12 #include "test/system.h" 13 14 static const uint8_t get_info[] = { 15 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 16 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 17 }; 18 19 static const uint8_t create_write_window[] = { 20 0x06, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 21 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 22 }; 23 24 static const uint8_t mark_write_dirty[] = { 25 0x07, 0x02, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 26 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 27 }; 28 29 static const uint8_t create_read_window[] = { 30 0x04, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 31 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 32 }; 33 34 static const uint8_t create_read_window_response[] = { 35 0x04, 0x03, 0xfd, 0xff, 0x03, 0x00, 0x00, 0x00, 36 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 37 }; 38 39 static const uint8_t close_window[] = { 40 0x05, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 42 }; 43 44 static const uint8_t close_window_response[] = { 45 0x05, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 46 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 47 }; 48 49 const uint8_t start_data[] = { 0xaa, 0x55, 0xaa }; 50 const uint8_t finish_data[] = { 0xaa, 0x00, 0xaa }; 51 52 #define MEM_SIZE sizeof(start_data) 53 #define ERASE_SIZE 1 54 #define N_WINDOWS 1 55 #define WINDOW_SIZE sizeof(start_data) 56 57 int setup(struct mbox_context *ctx) 58 { 59 int rc; 60 61 rc = mbox_set_mtd_data(ctx, start_data, sizeof(start_data)); 62 assert(rc == 0); 63 64 rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info)); 65 assert(rc == 1); 66 67 rc = mbox_command_dispatch(ctx, create_write_window, 68 sizeof(create_write_window)); 69 assert(rc == 1); 70 71 return rc; 72 } 73 74 int flush_on_close(struct mbox_context *ctx) 75 { 76 uint8_t *map; 77 int rc; 78 79 rc = setup(ctx); 80 assert(rc == 1); 81 82 ((uint8_t *)ctx->mem)[1] = 0x00; 83 84 rc = mbox_command_dispatch(ctx, mark_write_dirty, 85 sizeof(mark_write_dirty)); 86 assert(rc == 1); 87 88 rc = mbox_command_dispatch(ctx, close_window, sizeof(close_window)); 89 assert(rc == 1); 90 91 rc = mbox_cmp(ctx, close_window_response, 92 sizeof(close_window_response)); 93 assert(rc == 0); 94 95 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, 96 ((struct mtd_data *)ctx->backend.priv)->fd, 0); 97 assert(map != MAP_FAILED); 98 99 rc = memcmp(finish_data, map, sizeof(finish_data)); 100 assert(rc == 0); 101 102 return rc; 103 } 104 105 int flush_on_create(struct mbox_context *ctx) 106 { 107 uint8_t *map; 108 int rc; 109 110 rc = setup(ctx); 111 assert(rc == 1); 112 113 ((uint8_t *)ctx->mem)[1] = 0x00; 114 115 rc = mbox_command_dispatch(ctx, mark_write_dirty, 116 sizeof(mark_write_dirty)); 117 assert(rc == 1); 118 119 rc = mbox_command_dispatch(ctx, create_read_window, 120 sizeof(create_read_window)); 121 assert(rc == 1); 122 123 rc = mbox_cmp(ctx, create_read_window_response, 124 sizeof(create_read_window_response)); 125 assert(rc == 0); 126 127 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, 128 ((struct mtd_data *)ctx->backend.priv)->fd, 0); 129 assert(map != MAP_FAILED); 130 131 rc = memcmp(finish_data, map, sizeof(finish_data)); 132 assert(rc == 0); 133 134 return rc; 135 } 136 137 int main(void) 138 { 139 struct mbox_context *ctx; 140 141 system_set_reserved_size(MEM_SIZE); 142 system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE); 143 144 ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE); 145 146 flush_on_close(ctx); 147 148 flush_on_create(ctx); 149 150 return 0; 151 }; 152