1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright (C) 2018 IBM Corp.
3
4 #include <assert.h>
5 #include <mtd/mtd-abi.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/ioctl.h>
10 #include <sys/mman.h>
11 #include <unistd.h>
12
13 #include "common.h"
14 #include "mboxd.h"
15 #include "backend.h"
16
17 #include "test/tmpf.h"
18
19 struct tmpf _tmp, *tmp = &_tmp;
20
cleanup(void)21 void cleanup(void)
22 {
23 tmpf_destroy(tmp);
24 }
25
get_dev_mtd(void)26 char *get_dev_mtd(void)
27 {
28 int rc;
29
30 rc = tmpf_init(tmp, "flash-store.XXXXXX");
31 if (rc < 0)
32 return NULL;
33
34 return tmp->path;
35 }
36
37 #define MEM_SIZE 3
38 #define ERASE_SIZE 1
39
ioctl(int fd,unsigned long request,...)40 int ioctl(int fd __attribute__((unused)), unsigned long request, ...)
41 {
42 va_list ap;
43
44 if (request != MEMGETINFO)
45 return -1;
46
47 struct mtd_info_user *info;
48
49 va_start(ap, request);
50 info = va_arg(ap, struct mtd_info_user *);
51 info->size = MEM_SIZE;
52 info->erasesize = ERASE_SIZE;
53 va_end(ap);
54
55 return 0;
56 }
57
main(void)58 int main(void)
59 {
60 struct mbox_context _context, *context = &_context;
61 struct backend *backend = &context->backend;
62 uint8_t src[MEM_SIZE];
63 uint8_t *map;
64 int rc;
65
66 atexit(cleanup);
67
68 mbox_vlog = &mbox_log_console;
69
70 rc = backend_probe_mtd(backend, get_dev_mtd());
71 assert(rc == 0);
72
73 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, tmp->fd, 0);
74 assert(map != MAP_FAILED);
75
76 memset(src, 0xaa, sizeof(src));
77 rc = backend_write(backend, 0, src, sizeof(src));
78 assert(rc == 0);
79 rc = memcmp(src, map, sizeof(src));
80 assert(rc == 0);
81
82 memset(src, 0x55, sizeof(src));
83 rc = backend_write(backend, 0, src, sizeof(src));
84 assert(rc == 0);
85 rc = memcmp(src, map, sizeof(src));
86 assert(rc == 0);
87
88 src[0] = 0xff;
89 rc = backend_write(backend, 0, src, 1);
90 assert(rc == 0);
91 rc = memcmp(src, map, sizeof(src));
92 assert(rc == 0);
93
94 src[1] = 0xff;
95 rc = backend_write(backend, 1, &src[1], 1);
96 assert(rc == 0);
97 rc = memcmp(src, map, sizeof(src));
98 assert(rc == 0);
99
100 src[2] = 0xff;
101 rc = backend_write(backend, 2, &src[2], 1);
102 assert(rc == 0);
103 rc = memcmp(src, map, sizeof(src));
104 assert(rc == 0);
105
106 backend_free(backend);
107
108 return rc;
109 }
110