FreeBSD/sbin/camcontrol/fwdownload.c
/*
 * Allocate a buffer and read fw image file into it
 * from given path. Number of bytes read is stored
 * in num_bytes.
 */
Allocate a buffer and read fw image file into it
from given path. Number of bytes read is stored
in num_bytes.
Allocate a buffer and read fw image file into it from given path . Number of bytes read is stored in num_bytes .
static char *
fw_read_img(struct cam_device *dev, int retry_count, int timeout, int quiet,
	    const char *fw_img_path, struct fw_vendor *vp, int *num_bytes)
{
	int fd;
	struct stat stbuf;
	char *buf;
	off_t img_size;
	int skip_bytes = 0;
	if ((fd = open(fw_img_path, O_RDONLY)) < 0) {
		warn("Could not open image file %s", fw_img_path);
		return (NULL);
	}
	if (fstat(fd, &stbuf) < 0) {
		warn("Could not stat image file %s", fw_img_path);
		goto bailout1;
	}
	if ((img_size = stbuf.st_size) == 0) {
		warnx("Zero length image file %s", fw_img_path);
		goto bailout1;
	}
	if ((buf = malloc(img_size)) == NULL) {
		warnx("Could not allocate buffer to read image file %s",
		    fw_img_path);
		goto bailout1;
	}
	switch (vp->type) {
	case VENDOR_SEAGATE:
		if (read(fd, buf, 16) != 16) {
			warn("Could not read image file %s", fw_img_path);
			goto bailout;
		}
		if (lseek(fd, 0, SEEK_SET) == -1) {
			warn("Unable to lseek");
			goto bailout;
		}
		if ((strncmp(buf, "SEAGATE,SEAGATE ", 16) == 0) ||
		    (img_size % 512 == 80))
			skip_bytes = 80;
		break;
	case VENDOR_QUALSTAR:
		skip_bytes = img_size % 1030;
		break;
	case VENDOR_IBM: {
		if (vp->dev_type != T_SEQUENTIAL)
			break;
		if (fw_validate_ibm(dev, retry_count, timeout, fd, buf,
				    fw_img_path, quiet) != 0)
			goto bailout;
		break;
	}
	default:
		break;
	}
	if (skip_bytes != 0) {
		fprintf(stdout, "Skipping %d byte header.\n", skip_bytes);
		if (lseek(fd, skip_bytes, SEEK_SET) == -1) {
			warn("Could not lseek");
			goto bailout;
		}
		img_size -= skip_bytes;
	}
	if (read(fd, buf, img_size) != img_size) {
		warn("Could not read image file %s", fw_img_path);
		goto bailout;
	}
	*num_bytes = img_size;
	close(fd);
	return (buf);
bailout:
	free(buf);
bailout1:
	close(fd);
	*num_bytes = 0;
	return (NULL);
}
static char * fw_read_img ( struct cam_device * dev , int retry_count , int timeout , int quiet , const char * fw_img_path , struct fw_vendor * vp , int * num_bytes ) { int fd ; struct stat stbuf ; char * buf ; off_t img_size ; int skip_bytes = 0 ; if ( ( fd = open ( fw_img_path , O_RDONLY ) ) < 0 ) { warn ( " " , fw_img_path ) ; return ( NULL ) ; } if ( fstat ( fd , & stbuf ) < 0 ) { warn ( " " , fw_img_path ) ; goto bailout1 ; } if ( ( img_size = stbuf . st_size ) == 0 ) { warnx ( " " , fw_img_path ) ; goto bailout1 ; } if ( ( buf = malloc ( img_size ) ) == NULL ) { warnx ( " " , fw_img_path ) ; goto bailout1 ; } switch ( vp -> type ) { case VENDOR_SEAGATE : if ( read ( fd , buf , 16 ) != 16 ) { warn ( " " , fw_img_path ) ; goto bailout ; } if ( lseek ( fd , 0 , SEEK_SET ) == -1 ) { warn ( " " ) ; goto bailout ; } if ( ( strncmp ( buf , " " , 16 ) == 0 ) || ( img_size % 512 == 80 ) ) skip_bytes = 80 ; break ; case VENDOR_QUALSTAR : skip_bytes = img_size % 1030 ; break ; case VENDOR_IBM : { if ( vp -> dev_type != T_SEQUENTIAL ) break ; if ( fw_validate_ibm ( dev , retry_count , timeout , fd , buf , fw_img_path , quiet ) != 0 ) goto bailout ; break ; } default : break ; } if ( skip_bytes != 0 ) { fprintf ( stdout , " \n " , skip_bytes ) ; if ( lseek ( fd , skip_bytes , SEEK_SET ) == -1 ) { warn ( " " ) ; goto bailout ; } img_size -= skip_bytes ; } if ( read ( fd , buf , img_size ) != img_size ) { warn ( " " , fw_img_path ) ; goto bailout ; } * num_bytes = img_size ; close ( fd ) ; return ( buf ) ; bailout : free ( buf ) ; bailout1 : close ( fd ) ; * num_bytes = 0 ; return ( NULL ) ; }
Allocate a buffer and read fw image file into it
 from given path.
Allocate a buffer and read fw image file into it from given path .
/* Skip headers if applicable. */ /* Read image into a buffer. */