blob: 1b55dbe577321597fa39c1f4752b08b495091e2b [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>
Andrew Jeffery26558db2018-08-10 00:22:38 +09305#include <mtd/mtd-abi.h>
Andrew Jeffery26b35672017-04-12 14:22:25 +09306#include <stdio.h>
7#include <stdlib.h>
Andrew Jeffery26558db2018-08-10 00:22:38 +09308#include <string.h>
Andrew Jeffery26b35672017-04-12 14:22:25 +09309#include <sys/ioctl.h>
10#include <sys/mman.h>
11#include <unistd.h>
12
Andrew Jeffery26558db2018-08-10 00:22:38 +093013#include "common.h"
14#include "mboxd.h"
Andrew Jefferyeebc6bd2018-08-08 10:38:19 +093015#include "flash.h"
Andrew Jeffery26b35672017-04-12 14:22:25 +093016
17#include "test/tmpf.h"
18
19struct tmpf _tmp, *tmp = &_tmp;
20
21void cleanup(void)
22{
23 tmpf_destroy(tmp);
24}
25
26char *get_dev_mtd(void)
27{
28 int rc;
29
Andrew Jefferyc3144042018-02-26 13:24:52 +103030 rc = tmpf_init(tmp, "flash-store.XXXXXX");
Andrew Jeffery26b35672017-04-12 14:22:25 +093031 if (rc < 0)
32 return NULL;
33
34 return strdup(tmp->path);
35}
36
37#define MEM_SIZE 3
38#define ERASE_SIZE 1
39
40int ioctl(int fd, 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
58int main(void)
59{
60 struct mbox_context _context, *context = &_context;
61 char src[MEM_SIZE];
62 uint8_t *map;
63 int rc;
64
65 atexit(cleanup);
66
Suraj Jitindar Singh6f3197d2017-05-03 16:57:25 +100067 mbox_vlog = &mbox_log_console;
68
Andrew Jefferyd6b09bc2018-08-08 16:47:54 +093069 rc = flash_dev_init(context);
Andrew Jeffery26b35672017-04-12 14:22:25 +093070 assert(rc == 0);
71
72 map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, tmp->fd, 0);
73 assert(map != MAP_FAILED);
74
75 memset(src, 0xaa, sizeof(src));
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093076 rc = flash_write(context, 0, src, sizeof(src));
Andrew Jeffery26b35672017-04-12 14:22:25 +093077 assert(rc == 0);
78 rc = memcmp(src, map, sizeof(src));
79 assert(rc == 0);
80
81 memset(src, 0x55, sizeof(src));
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093082 rc = flash_write(context, 0, src, sizeof(src));
Andrew Jeffery26b35672017-04-12 14:22:25 +093083 assert(rc == 0);
84 rc = memcmp(src, map, sizeof(src));
85 assert(rc == 0);
86
87 src[0] = 0xff;
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093088 rc = flash_write(context, 0, src, 1);
Andrew Jeffery26b35672017-04-12 14:22:25 +093089 assert(rc == 0);
90 rc = memcmp(src, map, sizeof(src));
91 assert(rc == 0);
92
93 src[1] = 0xff;
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093094 rc = flash_write(context, 1, &src[1], 1);
Andrew Jeffery26b35672017-04-12 14:22:25 +093095 assert(rc == 0);
96 rc = memcmp(src, map, sizeof(src));
97 assert(rc == 0);
98
99 src[2] = 0xff;
Andrew Jeffery0293f2f2018-08-08 16:59:45 +0930100 rc = flash_write(context, 2, &src[2], 1);
Andrew Jeffery26b35672017-04-12 14:22:25 +0930101 assert(rc == 0);
102 rc = memcmp(src, map, sizeof(src));
103 assert(rc == 0);
104
Andrew Jefferydec6ca62018-08-08 16:49:43 +0930105 flash_dev_free(context);
Andrew Jeffery26b35672017-04-12 14:22:25 +0930106
107 return rc;
108}