Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 1 | /* |
| 2 | * MBox Daemon Test File |
| 3 | * |
| 4 | * Copyright 2017 IBM |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <assert.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include "mbox.h" |
| 28 | #include "mboxd_flash.h" |
| 29 | |
| 30 | #include "test/tmpf.h" |
| 31 | |
| 32 | struct tmpf _tmp, *tmp = &_tmp; |
| 33 | |
| 34 | void cleanup(void) |
| 35 | { |
| 36 | tmpf_destroy(tmp); |
| 37 | } |
| 38 | |
| 39 | char *get_dev_mtd(void) |
| 40 | { |
| 41 | int rc; |
| 42 | |
Andrew Jeffery | c314404 | 2018-02-26 13:24:52 +1030 | [diff] [blame] | 43 | rc = tmpf_init(tmp, "flash-store.XXXXXX"); |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 44 | if (rc < 0) |
| 45 | return NULL; |
| 46 | |
| 47 | return strdup(tmp->path); |
| 48 | } |
| 49 | |
| 50 | #define MEM_SIZE 3 |
| 51 | #define ERASE_SIZE 1 |
| 52 | |
| 53 | int ioctl(int fd, unsigned long request, ...) |
| 54 | { |
| 55 | va_list ap; |
| 56 | |
| 57 | if (request != MEMGETINFO) |
| 58 | return -1; |
| 59 | |
| 60 | struct mtd_info_user *info; |
| 61 | |
| 62 | va_start(ap, request); |
| 63 | info = va_arg(ap, struct mtd_info_user *); |
| 64 | info->size = MEM_SIZE; |
| 65 | info->erasesize = ERASE_SIZE; |
| 66 | va_end(ap); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int main(void) |
| 72 | { |
| 73 | struct mbox_context _context, *context = &_context; |
| 74 | char src[MEM_SIZE]; |
| 75 | uint8_t *map; |
| 76 | int rc; |
| 77 | |
| 78 | atexit(cleanup); |
| 79 | |
Suraj Jitindar Singh | 6f3197d | 2017-05-03 16:57:25 +1000 | [diff] [blame] | 80 | mbox_vlog = &mbox_log_console; |
| 81 | |
Andrew Jeffery | 26b3567 | 2017-04-12 14:22:25 +0930 | [diff] [blame] | 82 | rc = init_flash_dev(context); |
| 83 | assert(rc == 0); |
| 84 | |
| 85 | map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, tmp->fd, 0); |
| 86 | assert(map != MAP_FAILED); |
| 87 | |
| 88 | memset(src, 0xaa, sizeof(src)); |
| 89 | rc = write_flash(context, 0, src, sizeof(src)); |
| 90 | assert(rc == 0); |
| 91 | rc = memcmp(src, map, sizeof(src)); |
| 92 | assert(rc == 0); |
| 93 | |
| 94 | memset(src, 0x55, sizeof(src)); |
| 95 | rc = write_flash(context, 0, src, sizeof(src)); |
| 96 | assert(rc == 0); |
| 97 | rc = memcmp(src, map, sizeof(src)); |
| 98 | assert(rc == 0); |
| 99 | |
| 100 | src[0] = 0xff; |
| 101 | rc = write_flash(context, 0, src, 1); |
| 102 | assert(rc == 0); |
| 103 | rc = memcmp(src, map, sizeof(src)); |
| 104 | assert(rc == 0); |
| 105 | |
| 106 | src[1] = 0xff; |
| 107 | rc = write_flash(context, 1, &src[1], 1); |
| 108 | assert(rc == 0); |
| 109 | rc = memcmp(src, map, sizeof(src)); |
| 110 | assert(rc == 0); |
| 111 | |
| 112 | src[2] = 0xff; |
| 113 | rc = write_flash(context, 2, &src[2], 1); |
| 114 | assert(rc == 0); |
| 115 | rc = memcmp(src, map, sizeof(src)); |
| 116 | assert(rc == 0); |
| 117 | |
| 118 | free_flash_dev(context); |
| 119 | |
| 120 | return rc; |
| 121 | } |