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