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> |
| 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 Jeffery | eebc6bd | 2018-08-08 10:38:19 +0930 | [diff] [blame] | 12 | #include "flash.h" |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 13 | |
| 14 | #include "test/tmpf.h" |
| 15 | |
| 16 | struct tmpf _tmp, *tmp = &_tmp; |
| 17 | |
| 18 | void cleanup(void) |
| 19 | { |
| 20 | tmpf_destroy(tmp); |
| 21 | } |
| 22 | |
| 23 | char *get_dev_mtd(void) |
| 24 | { |
| 25 | int rc; |
| 26 | |
Andrew Jeffery | c314404 | 2018-02-26 13:24:52 +1030 | [diff] [blame] | 27 | rc = tmpf_init(tmp, "flash-store.XXXXXX"); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 28 | 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 | |
| 37 | int 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 | |
| 55 | int 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 Singh | 6f3197d | 2017-05-03 16:57:25 +1000 | [diff] [blame] | 64 | mbox_vlog = &mbox_log_console; |
| 65 | |
Andrew Jeffery | d6b09bc | 2018-08-08 16:47:54 +0930 | [diff] [blame] | 66 | rc = flash_dev_init(context); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 67 | 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)); |
| 73 | rc = write_flash(context, 0, src, sizeof(src)); |
| 74 | assert(rc == 0); |
| 75 | rc = memcmp(src, map, sizeof(src)); |
| 76 | assert(rc == 0); |
| 77 | |
| 78 | memset(src, 0x55, sizeof(src)); |
| 79 | rc = write_flash(context, 0, src, sizeof(src)); |
| 80 | assert(rc == 0); |
| 81 | rc = memcmp(src, map, sizeof(src)); |
| 82 | assert(rc == 0); |
| 83 | |
| 84 | src[0] = 0xff; |
| 85 | rc = write_flash(context, 0, src, 1); |
| 86 | assert(rc == 0); |
| 87 | rc = memcmp(src, map, sizeof(src)); |
| 88 | assert(rc == 0); |
| 89 | |
| 90 | src[1] = 0xff; |
| 91 | rc = write_flash(context, 1, &src[1], 1); |
| 92 | assert(rc == 0); |
| 93 | rc = memcmp(src, map, sizeof(src)); |
| 94 | assert(rc == 0); |
| 95 | |
| 96 | src[2] = 0xff; |
| 97 | rc = write_flash(context, 2, &src[2], 1); |
| 98 | assert(rc == 0); |
| 99 | rc = memcmp(src, map, sizeof(src)); |
| 100 | assert(rc == 0); |
| 101 | |
Andrew Jeffery | dec6ca6 | 2018-08-08 16:49:43 +0930 | [diff] [blame^] | 102 | flash_dev_free(context); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 103 | |
| 104 | return rc; |
| 105 | } |