blob: f0c107c5b3ed980e17c416c9908826693c9d3c62 [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Andrew Jeffery26b35672017-04-12 14:22:25 +09303
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"
Andrew Jefferyeebc6bd2018-08-08 10:38:19 +093012#include "flash.h"
Andrew Jeffery26b35672017-04-12 14:22:25 +093013
14#include "test/tmpf.h"
15
16struct tmpf _tmp, *tmp = &_tmp;
17
18void cleanup(void)
19{
20 tmpf_destroy(tmp);
21}
22
23char *get_dev_mtd(void)
24{
25 int rc;
26
Andrew Jefferyc3144042018-02-26 13:24:52 +103027 rc = tmpf_init(tmp, "flash-store.XXXXXX");
Andrew Jeffery26b35672017-04-12 14:22:25 +093028 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
37int 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
55int 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
Suraj Jitindar Singh6f3197d2017-05-03 16:57:25 +100064 mbox_vlog = &mbox_log_console;
65
Andrew Jefferyd6b09bc2018-08-08 16:47:54 +093066 rc = flash_dev_init(context);
Andrew Jeffery26b35672017-04-12 14:22:25 +093067 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
Andrew Jefferydec6ca62018-08-08 16:49:43 +0930102 flash_dev_free(context);
Andrew Jeffery26b35672017-04-12 14:22:25 +0930103
104 return rc;
105}