Andrew Jeffery | 4fe996c | 2018-02-27 12:16:48 +1030 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 3 | |
| 4 | #include <assert.h> |
Andrew Jeffery | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame] | 5 | #include <mtd/mtd-abi.h> |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
Andrew Jeffery | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame] | 8 | #include <string.h> |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 9 | #include <sys/ioctl.h> |
| 10 | #include <sys/mman.h> |
| 11 | #include <unistd.h> |
| 12 | |
Andrew Jeffery | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame] | 13 | #include "common.h" |
| 14 | #include "mboxd.h" |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 15 | #include "backend.h" |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 16 | |
| 17 | #include "test/tmpf.h" |
| 18 | |
| 19 | struct tmpf _tmp, *tmp = &_tmp; |
| 20 | |
| 21 | void cleanup(void) |
| 22 | { |
| 23 | tmpf_destroy(tmp); |
| 24 | } |
| 25 | |
| 26 | char *get_dev_mtd(void) |
| 27 | { |
| 28 | int rc; |
| 29 | |
Andrew Jeffery | c314404 | 2018-02-26 13:24:52 +1030 | [diff] [blame] | 30 | rc = tmpf_init(tmp, "flash-store.XXXXXX"); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 31 | if (rc < 0) |
| 32 | return NULL; |
| 33 | |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 34 | return tmp->path; |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | #define MEM_SIZE 3 |
| 38 | #define ERASE_SIZE 1 |
| 39 | |
| 40 | int 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 | |
| 58 | int main(void) |
| 59 | { |
| 60 | struct mbox_context _context, *context = &_context; |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 61 | struct backend *backend = &context->backend; |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 62 | char src[MEM_SIZE]; |
| 63 | uint8_t *map; |
| 64 | int rc; |
| 65 | |
| 66 | atexit(cleanup); |
| 67 | |
Suraj Jitindar Singh | 6f3197d | 2017-05-03 16:57:25 +1000 | [diff] [blame] | 68 | mbox_vlog = &mbox_log_console; |
| 69 | |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 70 | rc = backend_probe_mtd(backend, get_dev_mtd()); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 71 | 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 Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 77 | rc = backend_write(backend, 0, src, sizeof(src)); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 78 | assert(rc == 0); |
| 79 | rc = memcmp(src, map, sizeof(src)); |
| 80 | assert(rc == 0); |
| 81 | |
| 82 | memset(src, 0x55, sizeof(src)); |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 83 | rc = backend_write(backend, 0, src, sizeof(src)); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 84 | assert(rc == 0); |
| 85 | rc = memcmp(src, map, sizeof(src)); |
| 86 | assert(rc == 0); |
| 87 | |
| 88 | src[0] = 0xff; |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 89 | rc = backend_write(backend, 0, src, 1); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 90 | assert(rc == 0); |
| 91 | rc = memcmp(src, map, sizeof(src)); |
| 92 | assert(rc == 0); |
| 93 | |
| 94 | src[1] = 0xff; |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 95 | rc = backend_write(backend, 1, &src[1], 1); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 96 | assert(rc == 0); |
| 97 | rc = memcmp(src, map, sizeof(src)); |
| 98 | assert(rc == 0); |
| 99 | |
| 100 | src[2] = 0xff; |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 101 | rc = backend_write(backend, 2, &src[2], 1); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 102 | assert(rc == 0); |
| 103 | rc = memcmp(src, map, sizeof(src)); |
| 104 | assert(rc == 0); |
| 105 | |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 106 | backend_free(backend); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 107 | |
| 108 | return rc; |
| 109 | } |