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_erased[] = { 25 0x0a, 0x02, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 26 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 27 }; 28 29 static const uint8_t response[] = { 30 0x0a, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 31 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 32 }; 33 34 static const uint8_t write_flush[] = { 35 0x08, 0x03, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 36 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 37 }; 38 39 const uint8_t start_data[] = { 0xaa, 0x55, 0xaa }; 40 const uint8_t finish_data[] = { 0xaa, 0xff, 0xaa }; 41 42 #define MEM_SIZE sizeof(start_data) 43 #define ERASE_SIZE 1 44 #define N_WINDOWS 1 45 #define WINDOW_SIZE sizeof(start_data) 46 47 int main(void) 48 { 49 struct mbox_context *ctx; 50 uint8_t *map; 51 int rc; 52 53 system_set_reserved_size(MEM_SIZE); 54 system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE); 55 56 ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE); 57 rc = mbox_set_mtd_data(ctx, start_data, sizeof(start_data)); 58 assert(rc == 0); 59 60 rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info)); 61 assert(rc == 1); 62 63 rc = mbox_command_dispatch(ctx, create_write_window, 64 sizeof(create_write_window)); 65 assert(rc == 1); 66 67 rc = mbox_command_dispatch(ctx, mark_write_erased, 68 sizeof(mark_write_erased)); 69 assert(rc == 1); 70 71 rc = mbox_cmp(ctx, response, sizeof(response)); 72 assert(rc == 0); 73 74 rc = memcmp(ctx->mem, finish_data, sizeof(finish_data)); 75 assert(rc == 0); 76 77 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, 78 ((struct mtd_data *)ctx->backend.priv)->fd, 0); 79 assert(map != MAP_FAILED); 80 81 rc = memcmp(start_data, map, sizeof(start_data)); 82 assert(rc == 0); 83 84 rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush)); 85 assert(rc == 1); 86 87 rc = memcmp(finish_data, map, sizeof(finish_data)); 88 assert(rc == 0); 89 90 return rc; 91 }; 92