blob: c5f998c9586314a72bc7d21ecc473261d308da5a [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"
Evan Lojewskif1e547c2019-03-14 14:34:33 +103015#include "backend.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
Evan Lojewskif1e547c2019-03-14 14:34:33 +103034 return tmp->path;
Andrew Jeffery26b35672017-04-12 14:22:25 +093035}
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;
Evan Lojewskif1e547c2019-03-14 14:34:33 +103061 struct backend *backend = &context->backend;
Andrew Jeffery26b35672017-04-12 14:22:25 +093062 char src[MEM_SIZE];
63 uint8_t *map;
64 int rc;
65
66 atexit(cleanup);
67
Suraj Jitindar Singh6f3197d2017-05-03 16:57:25 +100068 mbox_vlog = &mbox_log_console;
69
Evan Lojewskif1e547c2019-03-14 14:34:33 +103070 rc = backend_probe_mtd(backend, get_dev_mtd());
Andrew Jeffery26b35672017-04-12 14:22:25 +093071 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));
Andrew Jeffery0297e5b2019-03-14 16:36:27 +103077 rc = backend_write(backend, 0, src, sizeof(src));
Andrew Jeffery26b35672017-04-12 14:22:25 +093078 assert(rc == 0);
79 rc = memcmp(src, map, sizeof(src));
80 assert(rc == 0);
81
82 memset(src, 0x55, sizeof(src));
Andrew Jeffery0297e5b2019-03-14 16:36:27 +103083 rc = backend_write(backend, 0, src, sizeof(src));
Andrew Jeffery26b35672017-04-12 14:22:25 +093084 assert(rc == 0);
85 rc = memcmp(src, map, sizeof(src));
86 assert(rc == 0);
87
88 src[0] = 0xff;
Andrew Jeffery0297e5b2019-03-14 16:36:27 +103089 rc = backend_write(backend, 0, src, 1);
Andrew Jeffery26b35672017-04-12 14:22:25 +093090 assert(rc == 0);
91 rc = memcmp(src, map, sizeof(src));
92 assert(rc == 0);
93
94 src[1] = 0xff;
Andrew Jeffery0297e5b2019-03-14 16:36:27 +103095 rc = backend_write(backend, 1, &src[1], 1);
Andrew Jeffery26b35672017-04-12 14:22:25 +093096 assert(rc == 0);
97 rc = memcmp(src, map, sizeof(src));
98 assert(rc == 0);
99
100 src[2] = 0xff;
Andrew Jeffery0297e5b2019-03-14 16:36:27 +1030101 rc = backend_write(backend, 2, &src[2], 1);
Andrew Jeffery26b35672017-04-12 14:22:25 +0930102 assert(rc == 0);
103 rc = memcmp(src, map, sizeof(src));
104 assert(rc == 0);
105
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030106 backend_free(backend);
Andrew Jeffery26b35672017-04-12 14:22:25 +0930107
108 return rc;
109}