10293f2f5SAndrew Jeffery // SPDX-License-Identifier: Apache-2.0 20293f2f5SAndrew Jeffery // Copyright (C) 2018 IBM Corp. 30293f2f5SAndrew Jeffery 40293f2f5SAndrew Jeffery #include <assert.h> 526558dbbSAndrew Jeffery #include <mtd/mtd-abi.h> 60293f2f5SAndrew Jeffery #include <stdio.h> 70293f2f5SAndrew Jeffery #include <stdlib.h> 826558dbbSAndrew Jeffery #include <string.h> 90293f2f5SAndrew Jeffery #include <sys/ioctl.h> 100293f2f5SAndrew Jeffery #include <sys/mman.h> 110293f2f5SAndrew Jeffery #include <unistd.h> 120293f2f5SAndrew Jeffery 1326558dbbSAndrew Jeffery #include "common.h" 1426558dbbSAndrew Jeffery #include "mboxd.h" 15*f1e547c7SEvan Lojewski #include "backend.h" 160293f2f5SAndrew Jeffery 170293f2f5SAndrew Jeffery #include "test/tmpf.h" 180293f2f5SAndrew Jeffery 190293f2f5SAndrew Jeffery struct tmpf _tmp, *tmp = &_tmp; 200293f2f5SAndrew Jeffery 210293f2f5SAndrew Jeffery void cleanup(void) 220293f2f5SAndrew Jeffery { 230293f2f5SAndrew Jeffery tmpf_destroy(tmp); 240293f2f5SAndrew Jeffery } 250293f2f5SAndrew Jeffery 260293f2f5SAndrew Jeffery char *get_dev_mtd(void) 270293f2f5SAndrew Jeffery { 280293f2f5SAndrew Jeffery int rc; 290293f2f5SAndrew Jeffery 300293f2f5SAndrew Jeffery rc = tmpf_init(tmp, "flash-store.XXXXXX"); 310293f2f5SAndrew Jeffery if (rc < 0) 320293f2f5SAndrew Jeffery return NULL; 330293f2f5SAndrew Jeffery 34*f1e547c7SEvan Lojewski return tmp->path; 350293f2f5SAndrew Jeffery } 360293f2f5SAndrew Jeffery 370293f2f5SAndrew Jeffery #define MEM_SIZE 3 380293f2f5SAndrew Jeffery #define ERASE_SIZE 1 390293f2f5SAndrew Jeffery 400293f2f5SAndrew Jeffery int ioctl(int fd, unsigned long request, ...) 410293f2f5SAndrew Jeffery { 420293f2f5SAndrew Jeffery va_list ap; 430293f2f5SAndrew Jeffery 440293f2f5SAndrew Jeffery if (request != MEMGETINFO) 450293f2f5SAndrew Jeffery return -1; 460293f2f5SAndrew Jeffery 470293f2f5SAndrew Jeffery struct mtd_info_user *info; 480293f2f5SAndrew Jeffery 490293f2f5SAndrew Jeffery va_start(ap, request); 500293f2f5SAndrew Jeffery info = va_arg(ap, struct mtd_info_user *); 510293f2f5SAndrew Jeffery info->size = MEM_SIZE; 520293f2f5SAndrew Jeffery info->erasesize = ERASE_SIZE; 530293f2f5SAndrew Jeffery va_end(ap); 540293f2f5SAndrew Jeffery 550293f2f5SAndrew Jeffery return 0; 560293f2f5SAndrew Jeffery } 570293f2f5SAndrew Jeffery 580293f2f5SAndrew Jeffery int main(void) 590293f2f5SAndrew Jeffery { 600293f2f5SAndrew Jeffery struct mbox_context _context, *context = &_context; 61*f1e547c7SEvan Lojewski struct backend *backend = &context->backend; 620293f2f5SAndrew Jeffery char src[MEM_SIZE]; 630293f2f5SAndrew Jeffery uint8_t *map; 640293f2f5SAndrew Jeffery int rc; 650293f2f5SAndrew Jeffery 660293f2f5SAndrew Jeffery atexit(cleanup); 670293f2f5SAndrew Jeffery 680293f2f5SAndrew Jeffery mbox_vlog = &mbox_log_console; 690293f2f5SAndrew Jeffery 70*f1e547c7SEvan Lojewski rc = backend_probe_mtd(backend, get_dev_mtd()); 710293f2f5SAndrew Jeffery assert(rc == 0); 720293f2f5SAndrew Jeffery 730293f2f5SAndrew Jeffery map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, tmp->fd, 0); 740293f2f5SAndrew Jeffery assert(map != MAP_FAILED); 750293f2f5SAndrew Jeffery 760293f2f5SAndrew Jeffery memset(src, 0xaa, sizeof(src)); 770293f2f5SAndrew Jeffery rc = flash_write(context, 0, src, sizeof(src)); 780293f2f5SAndrew Jeffery assert(rc == 0); 790293f2f5SAndrew Jeffery rc = memcmp(src, map, sizeof(src)); 800293f2f5SAndrew Jeffery assert(rc == 0); 810293f2f5SAndrew Jeffery 820293f2f5SAndrew Jeffery memset(src, 0x55, sizeof(src)); 830293f2f5SAndrew Jeffery rc = flash_write(context, 0, src, sizeof(src)); 840293f2f5SAndrew Jeffery assert(rc == 0); 850293f2f5SAndrew Jeffery rc = memcmp(src, map, sizeof(src)); 860293f2f5SAndrew Jeffery assert(rc == 0); 870293f2f5SAndrew Jeffery 880293f2f5SAndrew Jeffery src[0] = 0xff; 890293f2f5SAndrew Jeffery rc = flash_write(context, 0, src, 1); 900293f2f5SAndrew Jeffery assert(rc == 0); 910293f2f5SAndrew Jeffery rc = memcmp(src, map, sizeof(src)); 920293f2f5SAndrew Jeffery assert(rc == 0); 930293f2f5SAndrew Jeffery 940293f2f5SAndrew Jeffery src[1] = 0xff; 950293f2f5SAndrew Jeffery rc = flash_write(context, 1, &src[1], 1); 960293f2f5SAndrew Jeffery assert(rc == 0); 970293f2f5SAndrew Jeffery rc = memcmp(src, map, sizeof(src)); 980293f2f5SAndrew Jeffery assert(rc == 0); 990293f2f5SAndrew Jeffery 1000293f2f5SAndrew Jeffery src[2] = 0xff; 1010293f2f5SAndrew Jeffery rc = flash_write(context, 2, &src[2], 1); 1020293f2f5SAndrew Jeffery assert(rc == 0); 1030293f2f5SAndrew Jeffery rc = memcmp(src, map, sizeof(src)); 1040293f2f5SAndrew Jeffery assert(rc == 0); 1050293f2f5SAndrew Jeffery 106*f1e547c7SEvan Lojewski backend_free(backend); 1070293f2f5SAndrew Jeffery 1080293f2f5SAndrew Jeffery return rc; 1090293f2f5SAndrew Jeffery } 110