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" |
Andrew Jeffery | eebc6bd | 2018-08-08 10:38:19 +0930 | [diff] [blame] | 15 | #include "flash.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 | |
| 34 | return strdup(tmp->path); |
| 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; |
| 61 | char src[MEM_SIZE]; |
| 62 | uint8_t *map; |
| 63 | int rc; |
| 64 | |
| 65 | atexit(cleanup); |
| 66 | |
Suraj Jitindar Singh | 6f3197d | 2017-05-03 16:57:25 +1000 | [diff] [blame] | 67 | mbox_vlog = &mbox_log_console; |
| 68 | |
Andrew Jeffery | d6b09bc | 2018-08-08 16:47:54 +0930 | [diff] [blame] | 69 | rc = flash_dev_init(context); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 70 | 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 Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 76 | rc = flash_write(context, 0, src, sizeof(src)); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 77 | assert(rc == 0); |
| 78 | rc = memcmp(src, map, sizeof(src)); |
| 79 | assert(rc == 0); |
| 80 | |
| 81 | memset(src, 0x55, sizeof(src)); |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 82 | rc = flash_write(context, 0, src, sizeof(src)); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 83 | assert(rc == 0); |
| 84 | rc = memcmp(src, map, sizeof(src)); |
| 85 | assert(rc == 0); |
| 86 | |
| 87 | src[0] = 0xff; |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 88 | rc = flash_write(context, 0, src, 1); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 89 | assert(rc == 0); |
| 90 | rc = memcmp(src, map, sizeof(src)); |
| 91 | assert(rc == 0); |
| 92 | |
| 93 | src[1] = 0xff; |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 94 | rc = flash_write(context, 1, &src[1], 1); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 95 | assert(rc == 0); |
| 96 | rc = memcmp(src, map, sizeof(src)); |
| 97 | assert(rc == 0); |
| 98 | |
| 99 | src[2] = 0xff; |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 100 | rc = flash_write(context, 2, &src[2], 1); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 101 | assert(rc == 0); |
| 102 | rc = memcmp(src, map, sizeof(src)); |
| 103 | assert(rc == 0); |
| 104 | |
Andrew Jeffery | dec6ca6 | 2018-08-08 16:49:43 +0930 | [diff] [blame] | 105 | flash_dev_free(context); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 106 | |
| 107 | return rc; |
| 108 | } |