fpgafs/test/load_rw.c

117 lines
2.9 KiB
C

/* *****************************************************************
* (C) Copyright 2007 Benjamin Krill <ben@codiert.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301 USA
* *****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
static unsigned char src[] = { 0x12, 0x34, 0x56, 0x78, 0x90};
static unsigned char des[] = { 0x0, 0x0, 0x0, 0x0, 0x0};
/*
* LOAD WRITE/READ TEST
*/
int
main(int argc, char **argv)
{
int fd, res, i;
char *str, *drv, *dir;
if (argc < 2) {
printf("USAGE: %20s <fpgafs-mountpoint>\n", argv[0]);
return -1;
}
dir=malloc(sizeof(argv[1]) + 10);
str=malloc(sizeof(argv[1]) + 15);
drv=malloc(sizeof(argv[1]) + 15);
sprintf(dir, "%s/bla", argv[1]);
sprintf(drv, "%s/bla/lldrv", argv[1]);
sprintf(str, "%s/bla/load", argv[1]);
/* create context */
if (mkdir(dir, 0777) < 0) {
printf("ERROR-%d\n", __LINE__);
return -1;
}
printf("created: %s\n", dir);
/* set low level driver */
if ((fd = open(drv, O_WRONLY, 0)) < 0) {
printf("ERROR-%d: %d\n", __LINE__, fd);
return -1;
}
printf("opened: %s\n", drv);
if ((res=write(fd, "dbg", sizeof("dbg"))) < 0) {
printf("ERROR-%d: %d\n", __LINE__, res);
goto out;
}
close(fd);
printf("set low level driver: %d\n", res);
/* load image */
if ((fd = open(str, O_WRONLY, 0)) < 0) {
printf("ERROR-%d: %d\n", __LINE__, fd);
return -1;
}
printf("opened: %s\n", str);fflush(stdout);
if ((res=write(fd, src, sizeof(src))) < 0) {
printf("ERROR-%d: %d\n", __LINE__, res);
goto out;
}
printf("written (%d): ", res);
for (i=0; i < sizeof(src); i++)
printf("0x%x ", src[i]);
printf("\n");
close(fd);
/* read image */
if ((fd = open(str, O_RDONLY, 0)) < 0) {
printf("ERROR-%d: %d\n", __LINE__, fd);
return -1;
}
printf("opened: %s\n", str);
if ((res=read(fd, des, sizeof(des))) < 0) {
printf("ERROR-%d: %d\n", __LINE__, res);
goto out;
}
printf("read (%d): ", res);
for (i=0; i < sizeof(des); i++)
printf("0x%x ", des[i]);
printf("\n");
out:
close(fd);
/* destroy context */
if (rmdir(dir) < 0) {
printf("ERROR-%d\n", __LINE__);
return -1;
}
printf("removed: %s\n", dir);
return 0;
}