blob: cec6c92a9f98cd53ae549131f66c479a7eb64952 [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));
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093073 rc = flash_write(context, 0, src, sizeof(src));
Andrew Jeffery26b35672017-04-12 14:22:25 +093074 assert(rc == 0);
75 rc = memcmp(src, map, sizeof(src));
76 assert(rc == 0);
77
78 memset(src, 0x55, sizeof(src));
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093079 rc = flash_write(context, 0, src, sizeof(src));
Andrew Jeffery26b35672017-04-12 14:22:25 +093080 assert(rc == 0);
81 rc = memcmp(src, map, sizeof(src));
82 assert(rc == 0);
83
84 src[0] = 0xff;
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093085 rc = flash_write(context, 0, src, 1);
Andrew Jeffery26b35672017-04-12 14:22:25 +093086 assert(rc == 0);
87 rc = memcmp(src, map, sizeof(src));
88 assert(rc == 0);
89
90 src[1] = 0xff;
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093091 rc = flash_write(context, 1, &src[1], 1);
Andrew Jeffery26b35672017-04-12 14:22:25 +093092 assert(rc == 0);
93 rc = memcmp(src, map, sizeof(src));
94 assert(rc == 0);
95
96 src[2] = 0xff;
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093097 rc = flash_write(context, 2, &src[2], 1);
Andrew Jeffery26b35672017-04-12 14:22:25 +093098 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}