xref: /csrg-svn/old/np100/npload.c (revision 26270)
126180Ssklower /*
2*26270Ssklower  * Copyright (c) 1986 MICOM-Interlan, Inc., Boxborough Mass.
3*26270Ssklower  * All rights reserved.  The Berkeley software License Agreement
4*26270Ssklower  * specifies the terms and conditions for redistribution.
526180Ssklower  *
626180Ssklower  */
726180Ssklower #ifndef lint
826180Ssklower char copyright[] =
9*26270Ssklower "@(#) Copyright (c) 1986 MICOM-Interlan, Inc., Boxborough Mass.\n\
1026180Ssklower  All rights reserved.\n";
1126180Ssklower #endif not lint
1226180Ssklower 
1326180Ssklower #ifndef lint
14*26270Ssklower static char sccsid[] = "@(#)npload.c	6.2 (Berkeley) 02/20/86";
1526180Ssklower #endif not lint
1626180Ssklower 
1726180Ssklower #include <stdio.h>
1826180Ssklower #include <fcntl.h>
1926180Ssklower #include "npcmd.h"
2026180Ssklower #include <sys/ioctl.h>
2126180Ssklower 
2226180Ssklower extern int errno;
2326180Ssklower 
main(argc,argv)2426180Ssklower main(argc,argv)
2526180Ssklower int argc;
2626180Ssklower char **argv;
2726180Ssklower {
2826180Ssklower 
2926180Ssklower 	int	ret;
3026180Ssklower 	int	ed;
3126180Ssklower 	int	fd;
3226180Ssklower 	int	nbyte;
3326180Ssklower 	char	*fname;
3426180Ssklower 	char	ibuf[1024];
3526180Ssklower 	char	obuf[1024];
3626180Ssklower 	long	stadd = 0x400;
3726180Ssklower 	char	*devname = "/dev/np00";
3826180Ssklower 
3926180Ssklower 	switch (argc) {
4026180Ssklower 	case 3:
4126180Ssklower 		/* Pathname for device to be loaded */
4226180Ssklower 		devname = argv[2];
4326180Ssklower 	case 2:
4426180Ssklower 		/* Name of the image file to be loaded */
4526180Ssklower 		fname = argv[1];
4626180Ssklower 		break;
4726180Ssklower 	default:
4826180Ssklower 		printf("usage: npload imagefile [device]\n");
4926180Ssklower 		exit(1);
5026180Ssklower 	}
5126180Ssklower 	/* Open the device to be loaded */
5226180Ssklower 
5326180Ssklower 	if((ed = open(devname,O_RDWR)) == -1) {
5426180Ssklower 		char fullpath[50];
5526180Ssklower 		(void) sprintf(fullpath, "/dev/%s", devname);
5626180Ssklower 		if((ed = open(devname,O_RDWR)) == -1) {
5726180Ssklower 			fprintf(stderr,
5826180Ssklower 				"%s unable to open device %s errno = %d\n",
5926180Ssklower 				argv[0], devname, errno);
6026180Ssklower 			exit(2);
6126180Ssklower 		}
6226180Ssklower 	}
6326180Ssklower 
6426180Ssklower 	/* Open the image file */
6526180Ssklower 
6626180Ssklower 	if((fd = open(fname,O_RDONLY)) == -1) {
6726180Ssklower 		fprintf(stderr,"%s: unable to open file %s errno = %d\n",
6826180Ssklower 		    argv[0],fname,errno);
6926180Ssklower 		exit(3);
7026180Ssklower 	}
7126180Ssklower 
7226180Ssklower 	/* Reset the specified device */
7326180Ssklower 
7426180Ssklower 	if(ioctl(ed,NPRESET | IOC_VOID,&stadd) == -1) {
7526180Ssklower 		fprintf(stderr,"unable to reset %s errno = %d\n",devname,errno);
7626180Ssklower 		exit(4);
7726180Ssklower 	}
7826180Ssklower 
7926180Ssklower 	/* Seek to address 400 on the device */
8026180Ssklower 
8126180Ssklower 	if(lseek(ed,(long)0x400,0) == -1) {
8226180Ssklower 		fprintf(stderr,"seek failed on %s errno = %d.\n",devname,errno);
8326180Ssklower 		exit(5);
8426180Ssklower 	}
8526180Ssklower 
8626180Ssklower 	/* Seek to address 400 on the image file */
8726180Ssklower 
8826180Ssklower 	if(lseek(fd,(long)0x400,0) == -1) {
8926180Ssklower 		fprintf(stderr,"seek failed on %s errno = %d.\n",fname,errno);
9026180Ssklower 		exit(6);
9126180Ssklower 	}
9226180Ssklower 
9326180Ssklower 	/* Read from the image file and write to the device */
9426180Ssklower 
9526180Ssklower 	while((nbyte = read(fd,ibuf,1024)) > 0) {
9626180Ssklower 
9726180Ssklower 		if((ret = write(ed,ibuf,nbyte)) == -1) {
9826180Ssklower 			fprintf(stderr,"Bad write to %s errno = %d\n",
9926180Ssklower 			    argv[0],errno);
10026180Ssklower 			exit(7);
10126180Ssklower 		}
10226180Ssklower 	}
10326180Ssklower 
10426180Ssklower 	/* Issue a begin execution command to the device */
10526180Ssklower 
10626180Ssklower 	if(ioctl(ed,NPSTART | IOC_VOID,&stadd) == -1) {
10726180Ssklower 		fprintf(stderr,"Start error on %s errno = %d.\n",devname,errno);
10826180Ssklower 		exit(8);
10926180Ssklower 	}
11026180Ssklower 
11126180Ssklower 	close(fd);
11226180Ssklower 	close(ed);
11326180Ssklower 
11426180Ssklower 	exit(0);
11526180Ssklower }
116