fwdownload.c revision 255310
1227961Semaste/*-
2227961Semaste * Copyright (c) 2011 Sandvine Incorporated. All rights reserved.
3227961Semaste * Copyright (c) 2002-2011 Andre Albsmeier <andre@albsmeier.net>
4227961Semaste * All rights reserved.
5227961Semaste *
6227961Semaste * Redistribution and use in source and binary forms, with or without
7227961Semaste * modification, are permitted provided that the following conditions
8227961Semaste * are met:
9227961Semaste * 1. Redistributions of source code must retain the above copyright
10227961Semaste *    notice, this list of conditions and the following disclaimer,
11227961Semaste *    without modification, immediately at the beginning of the file.
12227961Semaste * 2. Redistributions in binary form must reproduce the above copyright
13227961Semaste *    notice, this list of conditions and the following disclaimer in the
14227961Semaste *    documentation and/or other materials provided with the distribution.
15227961Semaste *
16227961Semaste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17227961Semaste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18227961Semaste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19227961Semaste * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20227961Semaste * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21227961Semaste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22227961Semaste * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23227961Semaste * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24227961Semaste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25227961Semaste * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26227961Semaste */
27227961Semaste
28227961Semaste/*
29228203Semaste * This software is derived from Andre Albsmeier's fwprog.c which contained
30228203Semaste * the following note:
31228203Semaste *
32228203Semaste * Many thanks goes to Marc Frajola <marc@terasolutions.com> from
33228203Semaste * TeraSolutions for the initial idea and his programme for upgrading
34228203Semaste * the firmware of I*M DDYS drives.
35228203Semaste */
36228203Semaste
37228203Semaste/*
38227961Semaste * BEWARE:
39227961Semaste *
40227961Semaste * The fact that you see your favorite vendor listed below does not
41227961Semaste * imply that your equipment won't break when you use this software
42227961Semaste * with it. It only means that the firmware of at least one device type
43227961Semaste * of each vendor listed has been programmed successfully using this code.
44227961Semaste *
45227961Semaste * The -s option simulates a download but does nothing apart from that.
46227961Semaste * It can be used to check what chunk sizes would have been used with the
47227961Semaste * specified device.
48227961Semaste */
49227961Semaste
50227961Semaste#include <sys/cdefs.h>
51227961Semaste__FBSDID("$FreeBSD: head/sbin/camcontrol/fwdownload.c 255310 2013-09-06 16:34:09Z bryanv $");
52227961Semaste
53227961Semaste#include <sys/types.h>
54227961Semaste#include <sys/stat.h>
55227961Semaste
56227961Semaste#include <err.h>
57227961Semaste#include <fcntl.h>
58227961Semaste#include <stdio.h>
59227961Semaste#include <stdlib.h>
60227961Semaste#include <string.h>
61227961Semaste#include <unistd.h>
62227961Semaste
63227961Semaste#include <cam/scsi/scsi_all.h>
64227961Semaste#include <cam/scsi/scsi_message.h>
65227961Semaste#include <camlib.h>
66227961Semaste
67237281Sscottl#include "progress.h"
68237281Sscottl
69227961Semaste#include "camcontrol.h"
70227961Semaste
71227961Semaste#define	CMD_TIMEOUT 50000	/* 50 seconds */
72227961Semaste
73227961Semastetypedef enum {
74227961Semaste	VENDOR_HITACHI,
75227961Semaste	VENDOR_HP,
76227961Semaste	VENDOR_IBM,
77227961Semaste	VENDOR_PLEXTOR,
78237281Sscottl	VENDOR_QUALSTAR,
79227961Semaste	VENDOR_QUANTUM,
80255310Sbryanv	VENDOR_SAMSUNG,
81227961Semaste	VENDOR_SEAGATE,
82227961Semaste	VENDOR_UNKNOWN
83227961Semaste} fw_vendor_t;
84227961Semaste
85227961Semastestruct fw_vendor {
86227961Semaste	fw_vendor_t type;
87227961Semaste	const char *pattern;
88227961Semaste	int max_pkt_size;
89227961Semaste	u_int8_t cdb_byte2;
90227961Semaste	u_int8_t cdb_byte2_last;
91227961Semaste	int inc_cdb_buffer_id;
92227961Semaste	int inc_cdb_offset;
93227961Semaste};
94227961Semaste
95228407Sedstatic const struct fw_vendor vendors_list[] = {
96227961Semaste	{VENDOR_HITACHI,	"HITACHI",	0x8000, 0x05, 0x05, 1, 0},
97227961Semaste	{VENDOR_HP,		"HP",		0x8000, 0x07, 0x07, 0, 1},
98227961Semaste	{VENDOR_IBM,		"IBM",		0x8000, 0x05, 0x05, 1, 0},
99227961Semaste	{VENDOR_PLEXTOR,	"PLEXTOR",	0x2000, 0x04, 0x05, 0, 1},
100237281Sscottl	{VENDOR_QUALSTAR,	"QUALSTAR",	0x2030, 0x05, 0x05, 0, 0},
101227961Semaste	{VENDOR_QUANTUM,	"QUANTUM",	0x2000, 0x04, 0x05, 0, 1},
102255310Sbryanv	{VENDOR_SAMSUNG,	"SAMSUNG",	0x8000, 0x07, 0x07, 0, 1},
103227961Semaste	{VENDOR_SEAGATE,	"SEAGATE",	0x8000, 0x07, 0x07, 0, 1},
104237281Sscottl	/* the next 2 are SATA disks going through SAS HBA */
105237281Sscottl	{VENDOR_SEAGATE,	"ATA ST",	0x8000, 0x07, 0x07, 0, 1},
106237281Sscottl	{VENDOR_HITACHI,	"ATA HDS",	0x8000, 0x05, 0x05, 1, 0},
107227961Semaste	{VENDOR_UNKNOWN,	NULL,		0x0000, 0x00, 0x00, 0, 0}
108227961Semaste};
109227961Semaste
110237281Sscottl#ifndef ATA_DOWNLOAD_MICROCODE
111237281Sscottl#define ATA_DOWNLOAD_MICROCODE	0x92
112237281Sscottl#endif
113237281Sscottl
114237281Sscottl#define USE_OFFSETS_FEATURE	0x3
115237281Sscottl
116237281Sscottl#ifndef LOW_SECTOR_SIZE
117237281Sscottl#define LOW_SECTOR_SIZE		512
118237281Sscottl#endif
119237281Sscottl
120237281Sscottl#define ATA_MAKE_LBA(o, p)	\
121237281Sscottl	((((((o) / LOW_SECTOR_SIZE) >> 8) & 0xff) << 16) | \
122237281Sscottl	  ((((o) / LOW_SECTOR_SIZE) & 0xff) << 8) | \
123237281Sscottl	  ((((p) / LOW_SECTOR_SIZE) >> 8) & 0xff))
124237281Sscottl
125237281Sscottl#define ATA_MAKE_SECTORS(p)	(((p) / 512) & 0xff)
126237281Sscottl
127237281Sscottl#ifndef UNKNOWN_MAX_PKT_SIZE
128237281Sscottl#define UNKNOWN_MAX_PKT_SIZE	0x8000
129237281Sscottl#endif
130237281Sscottl
131228407Sedstatic const struct fw_vendor *fw_get_vendor(struct cam_device *cam_dev);
132228407Sedstatic char	*fw_read_img(const char *fw_img_path,
133228407Sed		    const struct fw_vendor *vp, int *num_bytes);
134227961Semastestatic int	 fw_download_img(struct cam_device *cam_dev,
135228407Sed		    const struct fw_vendor *vp, char *buf, int img_size,
136241737Sed		    int sim_mode, int printerrors, int retry_count, int timeout,
137237281Sscottl		    const char */*name*/, const char */*type*/);
138227961Semaste
139227961Semaste/*
140227961Semaste * Find entry in vendors list that belongs to
141227961Semaste * the vendor of given cam device.
142227961Semaste */
143228407Sedstatic const struct fw_vendor *
144227961Semastefw_get_vendor(struct cam_device *cam_dev)
145227961Semaste{
146227961Semaste	char vendor[SID_VENDOR_SIZE + 1];
147228407Sed	const struct fw_vendor *vp;
148227961Semaste
149227961Semaste	if (cam_dev == NULL)
150227961Semaste		return (NULL);
151227961Semaste	cam_strvis((u_char *)vendor, (u_char *)cam_dev->inq_data.vendor,
152227961Semaste	    sizeof(cam_dev->inq_data.vendor), sizeof(vendor));
153227961Semaste	for (vp = vendors_list; vp->pattern != NULL; vp++) {
154227961Semaste		if (!cam_strmatch((const u_char *)vendor,
155227961Semaste		    (const u_char *)vp->pattern, strlen(vendor)))
156227961Semaste			break;
157227961Semaste	}
158227961Semaste	return (vp);
159227961Semaste}
160227961Semaste
161227961Semaste/*
162227961Semaste * Allocate a buffer and read fw image file into it
163227961Semaste * from given path. Number of bytes read is stored
164227961Semaste * in num_bytes.
165227961Semaste */
166227961Semastestatic char *
167228407Sedfw_read_img(const char *fw_img_path, const struct fw_vendor *vp, int *num_bytes)
168227961Semaste{
169227961Semaste	int fd;
170227961Semaste	struct stat stbuf;
171227961Semaste	char *buf;
172227961Semaste	off_t img_size;
173227961Semaste	int skip_bytes = 0;
174227961Semaste
175227961Semaste	if ((fd = open(fw_img_path, O_RDONLY)) < 0) {
176227961Semaste		warn("Could not open image file %s", fw_img_path);
177227961Semaste		return (NULL);
178227961Semaste	}
179227961Semaste	if (fstat(fd, &stbuf) < 0) {
180227961Semaste		warn("Could not stat image file %s", fw_img_path);
181227961Semaste		goto bailout1;
182227961Semaste	}
183227961Semaste	if ((img_size = stbuf.st_size) == 0) {
184227961Semaste		warnx("Zero length image file %s", fw_img_path);
185227961Semaste		goto bailout1;
186227961Semaste	}
187227961Semaste	if ((buf = malloc(img_size)) == NULL) {
188227961Semaste		warnx("Could not allocate buffer to read image file %s",
189227961Semaste		    fw_img_path);
190227961Semaste		goto bailout1;
191227961Semaste	}
192227961Semaste	/* Skip headers if applicable. */
193227961Semaste	switch (vp->type) {
194227961Semaste	case VENDOR_SEAGATE:
195227961Semaste		if (read(fd, buf, 16) != 16) {
196227961Semaste			warn("Could not read image file %s", fw_img_path);
197227961Semaste			goto bailout;
198227961Semaste		}
199227961Semaste		if (lseek(fd, 0, SEEK_SET) == -1) {
200227961Semaste			warn("Unable to lseek");
201227961Semaste			goto bailout;
202227961Semaste		}
203227961Semaste		if ((strncmp(buf, "SEAGATE,SEAGATE ", 16) == 0) ||
204227961Semaste		    (img_size % 512 == 80))
205227961Semaste			skip_bytes = 80;
206227961Semaste		break;
207237281Sscottl	case VENDOR_QUALSTAR:
208237281Sscottl		skip_bytes = img_size % 1030;
209237281Sscottl		break;
210227961Semaste	default:
211227961Semaste		break;
212227961Semaste	}
213227961Semaste	if (skip_bytes != 0) {
214227961Semaste		fprintf(stdout, "Skipping %d byte header.\n", skip_bytes);
215227961Semaste		if (lseek(fd, skip_bytes, SEEK_SET) == -1) {
216227961Semaste			warn("Could not lseek");
217227961Semaste			goto bailout;
218227961Semaste		}
219227961Semaste		img_size -= skip_bytes;
220227961Semaste	}
221227961Semaste	/* Read image into a buffer. */
222227961Semaste	if (read(fd, buf, img_size) != img_size) {
223227961Semaste		warn("Could not read image file %s", fw_img_path);
224227961Semaste		goto bailout;
225227961Semaste	}
226227961Semaste	*num_bytes = img_size;
227227961Semaste	return (buf);
228227961Semastebailout:
229227961Semaste	free(buf);
230227961Semastebailout1:
231227961Semaste	close(fd);
232227961Semaste	*num_bytes = 0;
233227961Semaste	return (NULL);
234227961Semaste}
235227961Semaste
236227961Semaste/*
237227961Semaste * Download firmware stored in buf to cam_dev. If simulation mode
238227961Semaste * is enabled, only show what packet sizes would be sent to the
239227961Semaste * device but do not sent any actual packets
240227961Semaste */
241227961Semastestatic int
242228407Sedfw_download_img(struct cam_device *cam_dev, const struct fw_vendor *vp,
243241737Sed    char *buf, int img_size, int sim_mode, int printerrors, int retry_count,
244237281Sscottl    int timeout, const char *imgname, const char *type)
245227961Semaste{
246227961Semaste	struct scsi_write_buffer cdb;
247237281Sscottl	progress_t progress;
248237281Sscottl	int size;
249227961Semaste	union ccb *ccb;
250227961Semaste	int pkt_count = 0;
251237281Sscottl	int max_pkt_size;
252227961Semaste	u_int32_t pkt_size = 0;
253227961Semaste	char *pkt_ptr = buf;
254227961Semaste	u_int32_t offset;
255227961Semaste	int last_pkt = 0;
256237281Sscottl	int16_t *ptr;
257227961Semaste
258227961Semaste	if ((ccb = cam_getccb(cam_dev)) == NULL) {
259227961Semaste		warnx("Could not allocate CCB");
260227961Semaste		return (1);
261227961Semaste	}
262237281Sscottl	if (strcmp(type, "scsi") == 0) {
263237281Sscottl		scsi_test_unit_ready(&ccb->csio, 0, NULL, MSG_SIMPLE_Q_TAG,
264237281Sscottl		    SSD_FULL_SIZE, 5000);
265237281Sscottl	} else if (strcmp(type, "ata") == 0) {
266237281Sscottl		/* cam_getccb cleans up the header, caller has to zero the payload */
267237281Sscottl		bzero(&(&ccb->ccb_h)[1],
268237281Sscottl		      sizeof(struct ccb_ataio) - sizeof(struct ccb_hdr));
269237281Sscottl
270237281Sscottl		ptr = (uint16_t *)malloc(sizeof(struct ata_params));
271237281Sscottl
272237281Sscottl		if (ptr == NULL) {
273237281Sscottl			cam_freeccb(ccb);
274237281Sscottl			warnx("can't malloc memory for identify\n");
275237281Sscottl			return(1);
276237281Sscottl		}
277237281Sscottl		bzero(ptr, sizeof(struct ata_params));
278237281Sscottl		cam_fill_ataio(&ccb->ataio,
279237281Sscottl                      1,
280237281Sscottl                      NULL,
281237281Sscottl                      /*flags*/CAM_DIR_IN,
282237281Sscottl                      MSG_SIMPLE_Q_TAG,
283237281Sscottl                      /*data_ptr*/(uint8_t *)ptr,
284237281Sscottl                      /*dxfer_len*/sizeof(struct ata_params),
285237281Sscottl                      timeout ? timeout : 30 * 1000);
286237281Sscottl		ata_28bit_cmd(&ccb->ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
287237281Sscottl	} else {
288237281Sscottl		warnx("weird disk type '%s'", type);
289237281Sscottl		return 1;
290237281Sscottl	}
291227961Semaste	/* Disable freezing the device queue. */
292227961Semaste	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
293227961Semaste	if (cam_send_ccb(cam_dev, ccb) < 0) {
294237281Sscottl		warnx("Error sending identify/test unit ready");
295241737Sed		if (printerrors)
296227961Semaste			cam_error_print(cam_dev, ccb, CAM_ESF_ALL,
297227961Semaste			    CAM_EPF_ALL, stderr);
298227961Semaste		cam_freeccb(ccb);
299227961Semaste		return(1);
300227961Semaste	}
301227961Semaste	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
302227961Semaste		warnx("Device is not ready");
303241737Sed		if (printerrors)
304227961Semaste			cam_error_print(cam_dev, ccb, CAM_ESF_ALL,
305227961Semaste			    CAM_EPF_ALL, stderr);
306227961Semaste		cam_freeccb(ccb);
307227961Semaste		return (1);
308227961Semaste	}
309237281Sscottl	max_pkt_size = vp->max_pkt_size;
310237281Sscottl	if (vp->max_pkt_size == 0 && strcmp(type, "ata") == 0) {
311237281Sscottl		max_pkt_size = UNKNOWN_MAX_PKT_SIZE;
312237281Sscottl	}
313227961Semaste	pkt_size = vp->max_pkt_size;
314237281Sscottl	progress_init(&progress, imgname, size = img_size);
315227961Semaste	/* Download single fw packets. */
316227961Semaste	do {
317237281Sscottl		if (img_size <= max_pkt_size) {
318227961Semaste			last_pkt = 1;
319227961Semaste			pkt_size = img_size;
320227961Semaste		}
321237281Sscottl		progress_update(&progress, size - img_size);
322237281Sscottl		progress_draw(&progress);
323227961Semaste		bzero(&cdb, sizeof(cdb));
324237281Sscottl		if (strcmp(type, "scsi") == 0) {
325237281Sscottl			cdb.opcode  = WRITE_BUFFER;
326237281Sscottl			cdb.control = 0;
327237281Sscottl			/* Parameter list length. */
328237281Sscottl			scsi_ulto3b(pkt_size, &cdb.length[0]);
329237281Sscottl			offset = vp->inc_cdb_offset ? (pkt_ptr - buf) : 0;
330237281Sscottl			scsi_ulto3b(offset, &cdb.offset[0]);
331237281Sscottl			cdb.byte2 = last_pkt ? vp->cdb_byte2_last : vp->cdb_byte2;
332237281Sscottl			cdb.buffer_id = vp->inc_cdb_buffer_id ? pkt_count : 0;
333237281Sscottl			/* Zero out payload of ccb union after ccb header. */
334237281Sscottl			bzero((u_char *)ccb + sizeof(struct ccb_hdr),
335237281Sscottl			    sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
336237281Sscottl			/* Copy previously constructed cdb into ccb_scsiio struct. */
337237281Sscottl			bcopy(&cdb, &ccb->csio.cdb_io.cdb_bytes[0],
338237281Sscottl			    sizeof(struct scsi_write_buffer));
339237281Sscottl			/* Fill rest of ccb_scsiio struct. */
340237281Sscottl			if (!sim_mode) {
341237281Sscottl				cam_fill_csio(&ccb->csio,		/* ccb_scsiio	*/
342237281Sscottl				    retry_count,			/* retries	*/
343237281Sscottl				    NULL,				/* cbfcnp	*/
344237281Sscottl				    CAM_DIR_OUT | CAM_DEV_QFRZDIS,	/* flags	*/
345237281Sscottl				    CAM_TAG_ACTION_NONE,		/* tag_action	*/
346237281Sscottl				    (u_char *)pkt_ptr,			/* data_ptr	*/
347237281Sscottl				    pkt_size,				/* dxfer_len	*/
348237281Sscottl				    SSD_FULL_SIZE,			/* sense_len	*/
349237281Sscottl				    sizeof(struct scsi_write_buffer),	/* cdb_len	*/
350237281Sscottl				    timeout ? timeout : CMD_TIMEOUT);	/* timeout	*/
351237281Sscottl			}
352237281Sscottl		} else if (strcmp(type, "ata") == 0) {
353237281Sscottl			bzero(&(&ccb->ccb_h)[1],
354237281Sscottl			      sizeof(struct ccb_ataio) - sizeof(struct ccb_hdr));
355237281Sscottl			if (!sim_mode) {
356237281Sscottl				uint32_t	off;
357237281Sscottl
358237281Sscottl				cam_fill_ataio(&ccb->ataio,
359237281Sscottl					(last_pkt) ? 256 : retry_count,
360237281Sscottl					NULL,
361237281Sscottl					/*flags*/CAM_DIR_OUT | CAM_DEV_QFRZDIS,
362237281Sscottl					CAM_TAG_ACTION_NONE,
363237281Sscottl					/*data_ptr*/(uint8_t *)pkt_ptr,
364237281Sscottl					/*dxfer_len*/pkt_size,
365237281Sscottl					timeout ? timeout : 30 * 1000);
366237281Sscottl				off = (uint32_t)(pkt_ptr - buf);
367237281Sscottl				ata_28bit_cmd(&ccb->ataio, ATA_DOWNLOAD_MICROCODE,
368237281Sscottl					USE_OFFSETS_FEATURE,
369237281Sscottl					ATA_MAKE_LBA(off, pkt_size),
370237281Sscottl					ATA_MAKE_SECTORS(pkt_size));
371237281Sscottl			}
372237281Sscottl		}
373227961Semaste		if (!sim_mode) {
374227961Semaste			/* Execute the command. */
375251743Smav			if (cam_send_ccb(cam_dev, ccb) < 0 ||
376251743Smav			    (ccb->ccb_h.status & CAM_STATUS_MASK) !=
377251743Smav			    CAM_REQ_CMP) {
378227961Semaste				warnx("Error writing image to device");
379241737Sed				if (printerrors)
380227961Semaste					cam_error_print(cam_dev, ccb, CAM_ESF_ALL,
381237281Sscottl						   CAM_EPF_ALL, stderr);
382227961Semaste				goto bailout;
383227961Semaste			}
384227961Semaste		}
385227961Semaste		/* Prepare next round. */
386227961Semaste		pkt_count++;
387227961Semaste		pkt_ptr += pkt_size;
388227961Semaste		img_size -= pkt_size;
389227961Semaste	} while(!last_pkt);
390237281Sscottl	progress_complete(&progress, size - img_size);
391227961Semaste	cam_freeccb(ccb);
392227961Semaste	return (0);
393227961Semastebailout:
394237281Sscottl	progress_complete(&progress, size - img_size);
395227961Semaste	cam_freeccb(ccb);
396227961Semaste	return (1);
397227961Semaste}
398227961Semaste
399227961Semasteint
400227961Semastefwdownload(struct cam_device *device, int argc, char **argv,
401241737Sed    char *combinedopt, int printerrors, int retry_count, int timeout,
402237281Sscottl    const char *type)
403227961Semaste{
404228407Sed	const struct fw_vendor *vp;
405227961Semaste	char *fw_img_path = NULL;
406227961Semaste	char *buf;
407227961Semaste	int img_size;
408227961Semaste	int c;
409227961Semaste	int sim_mode = 0;
410227961Semaste	int confirmed = 0;
411227961Semaste
412227961Semaste	while ((c = getopt(argc, argv, combinedopt)) != -1) {
413227961Semaste		switch (c) {
414227961Semaste		case 's':
415227961Semaste			sim_mode = 1;
416227961Semaste			confirmed = 1;
417227961Semaste			break;
418227961Semaste		case 'f':
419227961Semaste			fw_img_path = optarg;
420227961Semaste			break;
421227961Semaste		case 'y':
422227961Semaste			confirmed = 1;
423227961Semaste			break;
424227961Semaste		default:
425227961Semaste			break;
426227961Semaste		}
427227961Semaste	}
428227961Semaste
429227961Semaste	if (fw_img_path == NULL)
430237281Sscottl		errx(1, "you must specify a firmware image file using -f option");
431227961Semaste
432227961Semaste	vp = fw_get_vendor(device);
433237281Sscottl	if (vp == NULL)
434237281Sscottl		errx(1, "NULL vendor");
435237281Sscottl	if (vp->type == VENDOR_UNKNOWN)
436237281Sscottl		warnx("Unsupported device - flashing through an HBA?");
437227961Semaste
438227961Semaste	buf = fw_read_img(fw_img_path, vp, &img_size);
439227961Semaste	if (buf == NULL)
440227961Semaste		goto fail;
441227961Semaste
442227961Semaste	if (!confirmed) {
443227961Semaste		fprintf(stdout, "You are about to download firmware image (%s)"
444227961Semaste		    " into the following device:\n",
445227961Semaste		    fw_img_path);
446227961Semaste		fprintf(stdout, "\nIt may damage your drive. ");
447227961Semaste		if (!get_confirmation())
448227961Semaste			goto fail;
449227961Semaste	}
450227961Semaste	if (sim_mode)
451227961Semaste		fprintf(stdout, "Running in simulation mode\n");
452227961Semaste
453241737Sed	if (fw_download_img(device, vp, buf, img_size, sim_mode, printerrors,
454237281Sscottl	    retry_count, timeout, fw_img_path, type) != 0) {
455227961Semaste		fprintf(stderr, "Firmware download failed\n");
456227961Semaste		goto fail;
457237281Sscottl	}
458237281Sscottl	else
459227961Semaste		fprintf(stdout, "Firmware download successful\n");
460227961Semaste
461227961Semaste	free(buf);
462227961Semaste	return (0);
463227961Semastefail:
464227961Semaste	if (buf != NULL)
465227961Semaste		free(buf);
466227961Semaste	return (1);
467227961Semaste}
468227961Semaste
469