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/*
29237740Sscottl * This software is derived from Andre Albsmeier's fwprog.c which contained
30237740Sscottl * the following note:
31237740Sscottl *
32237740Sscottl * Many thanks goes to Marc Frajola <marc@terasolutions.com> from
33237740Sscottl * TeraSolutions for the initial idea and his programme for upgrading
34237740Sscottl * the firmware of I*M DDYS drives.
35237740Sscottl */
36237740Sscottl
37237740Sscottl/*
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$");
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
67237740Sscottl#include "progress.h"
68237740Sscottl
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,
78237740Sscottl	VENDOR_QUALSTAR,
79227961Semaste	VENDOR_QUANTUM,
80227961Semaste	VENDOR_SEAGATE,
81227961Semaste	VENDOR_UNKNOWN
82227961Semaste} fw_vendor_t;
83227961Semaste
84227961Semastestruct fw_vendor {
85227961Semaste	fw_vendor_t type;
86227961Semaste	const char *pattern;
87227961Semaste	int max_pkt_size;
88227961Semaste	u_int8_t cdb_byte2;
89227961Semaste	u_int8_t cdb_byte2_last;
90227961Semaste	int inc_cdb_buffer_id;
91227961Semaste	int inc_cdb_offset;
92227961Semaste};
93227961Semaste
94237740Sscottlstatic const struct fw_vendor vendors_list[] = {
95227961Semaste	{VENDOR_HITACHI,	"HITACHI",	0x8000, 0x05, 0x05, 1, 0},
96227961Semaste	{VENDOR_HP,		"HP",		0x8000, 0x07, 0x07, 0, 1},
97227961Semaste	{VENDOR_IBM,		"IBM",		0x8000, 0x05, 0x05, 1, 0},
98227961Semaste	{VENDOR_PLEXTOR,	"PLEXTOR",	0x2000, 0x04, 0x05, 0, 1},
99237740Sscottl	{VENDOR_QUALSTAR,	"QUALSTAR",	0x2030, 0x05, 0x05, 0, 0},
100227961Semaste	{VENDOR_QUANTUM,	"QUANTUM",	0x2000, 0x04, 0x05, 0, 1},
101227961Semaste	{VENDOR_SEAGATE,	"SEAGATE",	0x8000, 0x07, 0x07, 0, 1},
102237740Sscottl	/* the next 2 are SATA disks going through SAS HBA */
103237740Sscottl	{VENDOR_SEAGATE,	"ATA ST",	0x8000, 0x07, 0x07, 0, 1},
104237740Sscottl	{VENDOR_HITACHI,	"ATA HDS",	0x8000, 0x05, 0x05, 1, 0},
105227961Semaste	{VENDOR_UNKNOWN,	NULL,		0x0000, 0x00, 0x00, 0, 0}
106227961Semaste};
107227961Semaste
108237740Sscottl#ifndef ATA_DOWNLOAD_MICROCODE
109237740Sscottl#define ATA_DOWNLOAD_MICROCODE	0x92
110237740Sscottl#endif
111237740Sscottl
112237740Sscottl#define USE_OFFSETS_FEATURE	0x3
113237740Sscottl
114237740Sscottl#ifndef LOW_SECTOR_SIZE
115237740Sscottl#define LOW_SECTOR_SIZE		512
116237740Sscottl#endif
117237740Sscottl
118237740Sscottl#define ATA_MAKE_LBA(o, p)	\
119237740Sscottl	((((((o) / LOW_SECTOR_SIZE) >> 8) & 0xff) << 16) | \
120237740Sscottl	  ((((o) / LOW_SECTOR_SIZE) & 0xff) << 8) | \
121237740Sscottl	  ((((p) / LOW_SECTOR_SIZE) >> 8) & 0xff))
122237740Sscottl
123237740Sscottl#define ATA_MAKE_SECTORS(p)	(((p) / 512) & 0xff)
124237740Sscottl
125237740Sscottl#ifndef UNKNOWN_MAX_PKT_SIZE
126237740Sscottl#define UNKNOWN_MAX_PKT_SIZE	0x8000
127237740Sscottl#endif
128237740Sscottl
129237740Sscottlstatic const struct fw_vendor *fw_get_vendor(struct cam_device *cam_dev);
130237740Sscottlstatic char	*fw_read_img(const char *fw_img_path,
131237740Sscottl		    const struct fw_vendor *vp, int *num_bytes);
132227961Semastestatic int	 fw_download_img(struct cam_device *cam_dev,
133237740Sscottl		    const struct fw_vendor *vp, char *buf, int img_size,
134243294Semaste		    int sim_mode, int printerrors, int retry_count, int timeout,
135237740Sscottl		    const char */*name*/, const char */*type*/);
136227961Semaste
137227961Semaste/*
138227961Semaste * Find entry in vendors list that belongs to
139227961Semaste * the vendor of given cam device.
140227961Semaste */
141237740Sscottlstatic const struct fw_vendor *
142227961Semastefw_get_vendor(struct cam_device *cam_dev)
143227961Semaste{
144227961Semaste	char vendor[SID_VENDOR_SIZE + 1];
145237740Sscottl	const struct fw_vendor *vp;
146227961Semaste
147227961Semaste	if (cam_dev == NULL)
148227961Semaste		return (NULL);
149227961Semaste	cam_strvis((u_char *)vendor, (u_char *)cam_dev->inq_data.vendor,
150227961Semaste	    sizeof(cam_dev->inq_data.vendor), sizeof(vendor));
151227961Semaste	for (vp = vendors_list; vp->pattern != NULL; vp++) {
152227961Semaste		if (!cam_strmatch((const u_char *)vendor,
153227961Semaste		    (const u_char *)vp->pattern, strlen(vendor)))
154227961Semaste			break;
155227961Semaste	}
156227961Semaste	return (vp);
157227961Semaste}
158227961Semaste
159227961Semaste/*
160227961Semaste * Allocate a buffer and read fw image file into it
161227961Semaste * from given path. Number of bytes read is stored
162227961Semaste * in num_bytes.
163227961Semaste */
164227961Semastestatic char *
165237740Sscottlfw_read_img(const char *fw_img_path, const struct fw_vendor *vp, int *num_bytes)
166227961Semaste{
167227961Semaste	int fd;
168227961Semaste	struct stat stbuf;
169227961Semaste	char *buf;
170227961Semaste	off_t img_size;
171227961Semaste	int skip_bytes = 0;
172227961Semaste
173227961Semaste	if ((fd = open(fw_img_path, O_RDONLY)) < 0) {
174227961Semaste		warn("Could not open image file %s", fw_img_path);
175227961Semaste		return (NULL);
176227961Semaste	}
177227961Semaste	if (fstat(fd, &stbuf) < 0) {
178227961Semaste		warn("Could not stat image file %s", fw_img_path);
179227961Semaste		goto bailout1;
180227961Semaste	}
181227961Semaste	if ((img_size = stbuf.st_size) == 0) {
182227961Semaste		warnx("Zero length image file %s", fw_img_path);
183227961Semaste		goto bailout1;
184227961Semaste	}
185227961Semaste	if ((buf = malloc(img_size)) == NULL) {
186227961Semaste		warnx("Could not allocate buffer to read image file %s",
187227961Semaste		    fw_img_path);
188227961Semaste		goto bailout1;
189227961Semaste	}
190227961Semaste	/* Skip headers if applicable. */
191227961Semaste	switch (vp->type) {
192227961Semaste	case VENDOR_SEAGATE:
193227961Semaste		if (read(fd, buf, 16) != 16) {
194227961Semaste			warn("Could not read image file %s", fw_img_path);
195227961Semaste			goto bailout;
196227961Semaste		}
197227961Semaste		if (lseek(fd, 0, SEEK_SET) == -1) {
198227961Semaste			warn("Unable to lseek");
199227961Semaste			goto bailout;
200227961Semaste		}
201227961Semaste		if ((strncmp(buf, "SEAGATE,SEAGATE ", 16) == 0) ||
202227961Semaste		    (img_size % 512 == 80))
203227961Semaste			skip_bytes = 80;
204227961Semaste		break;
205237740Sscottl	case VENDOR_QUALSTAR:
206237740Sscottl		skip_bytes = img_size % 1030;
207237740Sscottl		break;
208227961Semaste	default:
209227961Semaste		break;
210227961Semaste	}
211227961Semaste	if (skip_bytes != 0) {
212227961Semaste		fprintf(stdout, "Skipping %d byte header.\n", skip_bytes);
213227961Semaste		if (lseek(fd, skip_bytes, SEEK_SET) == -1) {
214227961Semaste			warn("Could not lseek");
215227961Semaste			goto bailout;
216227961Semaste		}
217227961Semaste		img_size -= skip_bytes;
218227961Semaste	}
219227961Semaste	/* Read image into a buffer. */
220227961Semaste	if (read(fd, buf, img_size) != img_size) {
221227961Semaste		warn("Could not read image file %s", fw_img_path);
222227961Semaste		goto bailout;
223227961Semaste	}
224227961Semaste	*num_bytes = img_size;
225227961Semaste	return (buf);
226227961Semastebailout:
227227961Semaste	free(buf);
228227961Semastebailout1:
229227961Semaste	close(fd);
230227961Semaste	*num_bytes = 0;
231227961Semaste	return (NULL);
232227961Semaste}
233227961Semaste
234227961Semaste/*
235227961Semaste * Download firmware stored in buf to cam_dev. If simulation mode
236227961Semaste * is enabled, only show what packet sizes would be sent to the
237227961Semaste * device but do not sent any actual packets
238227961Semaste */
239227961Semastestatic int
240237740Sscottlfw_download_img(struct cam_device *cam_dev, const struct fw_vendor *vp,
241243294Semaste    char *buf, int img_size, int sim_mode, int printerrors, int retry_count,
242237740Sscottl    int timeout, const char *imgname, const char *type)
243227961Semaste{
244227961Semaste	struct scsi_write_buffer cdb;
245237740Sscottl	progress_t progress;
246237740Sscottl	int size;
247227961Semaste	union ccb *ccb;
248227961Semaste	int pkt_count = 0;
249237740Sscottl	int max_pkt_size;
250227961Semaste	u_int32_t pkt_size = 0;
251227961Semaste	char *pkt_ptr = buf;
252227961Semaste	u_int32_t offset;
253227961Semaste	int last_pkt = 0;
254237740Sscottl	int16_t *ptr;
255227961Semaste
256227961Semaste	if ((ccb = cam_getccb(cam_dev)) == NULL) {
257227961Semaste		warnx("Could not allocate CCB");
258227961Semaste		return (1);
259227961Semaste	}
260237740Sscottl	if (strcmp(type, "scsi") == 0) {
261237740Sscottl		scsi_test_unit_ready(&ccb->csio, 0, NULL, MSG_SIMPLE_Q_TAG,
262237740Sscottl		    SSD_FULL_SIZE, 5000);
263237740Sscottl	} else if (strcmp(type, "ata") == 0) {
264237740Sscottl		/* cam_getccb cleans up the header, caller has to zero the payload */
265237740Sscottl		bzero(&(&ccb->ccb_h)[1],
266237740Sscottl		      sizeof(struct ccb_ataio) - sizeof(struct ccb_hdr));
267237740Sscottl
268237740Sscottl		ptr = (uint16_t *)malloc(sizeof(struct ata_params));
269237740Sscottl
270237740Sscottl		if (ptr == NULL) {
271237740Sscottl			cam_freeccb(ccb);
272237740Sscottl			warnx("can't malloc memory for identify\n");
273237740Sscottl			return(1);
274237740Sscottl		}
275237740Sscottl		bzero(ptr, sizeof(struct ata_params));
276237740Sscottl		cam_fill_ataio(&ccb->ataio,
277237740Sscottl                      1,
278237740Sscottl                      NULL,
279237740Sscottl                      /*flags*/CAM_DIR_IN,
280237740Sscottl                      MSG_SIMPLE_Q_TAG,
281237740Sscottl                      /*data_ptr*/(uint8_t *)ptr,
282237740Sscottl                      /*dxfer_len*/sizeof(struct ata_params),
283237740Sscottl                      timeout ? timeout : 30 * 1000);
284237740Sscottl		ata_28bit_cmd(&ccb->ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
285237740Sscottl	} else {
286237740Sscottl		warnx("weird disk type '%s'", type);
287237740Sscottl		return 1;
288237740Sscottl	}
289227961Semaste	/* Disable freezing the device queue. */
290227961Semaste	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
291227961Semaste	if (cam_send_ccb(cam_dev, ccb) < 0) {
292237740Sscottl		warnx("Error sending identify/test unit ready");
293243294Semaste		if (printerrors)
294227961Semaste			cam_error_print(cam_dev, ccb, CAM_ESF_ALL,
295227961Semaste			    CAM_EPF_ALL, stderr);
296227961Semaste		cam_freeccb(ccb);
297227961Semaste		return(1);
298227961Semaste	}
299227961Semaste	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
300227961Semaste		warnx("Device is not ready");
301243294Semaste		if (printerrors)
302227961Semaste			cam_error_print(cam_dev, ccb, CAM_ESF_ALL,
303227961Semaste			    CAM_EPF_ALL, stderr);
304227961Semaste		cam_freeccb(ccb);
305227961Semaste		return (1);
306227961Semaste	}
307237740Sscottl	max_pkt_size = vp->max_pkt_size;
308237740Sscottl	if (vp->max_pkt_size == 0 && strcmp(type, "ata") == 0) {
309237740Sscottl		max_pkt_size = UNKNOWN_MAX_PKT_SIZE;
310237740Sscottl	}
311227961Semaste	pkt_size = vp->max_pkt_size;
312237740Sscottl	progress_init(&progress, imgname, size = img_size);
313227961Semaste	/* Download single fw packets. */
314227961Semaste	do {
315237740Sscottl		if (img_size <= max_pkt_size) {
316227961Semaste			last_pkt = 1;
317227961Semaste			pkt_size = img_size;
318227961Semaste		}
319237740Sscottl		progress_update(&progress, size - img_size);
320237740Sscottl		progress_draw(&progress);
321227961Semaste		bzero(&cdb, sizeof(cdb));
322237740Sscottl		if (strcmp(type, "scsi") == 0) {
323237740Sscottl			cdb.opcode  = WRITE_BUFFER;
324237740Sscottl			cdb.control = 0;
325237740Sscottl			/* Parameter list length. */
326237740Sscottl			scsi_ulto3b(pkt_size, &cdb.length[0]);
327237740Sscottl			offset = vp->inc_cdb_offset ? (pkt_ptr - buf) : 0;
328237740Sscottl			scsi_ulto3b(offset, &cdb.offset[0]);
329237740Sscottl			cdb.byte2 = last_pkt ? vp->cdb_byte2_last : vp->cdb_byte2;
330237740Sscottl			cdb.buffer_id = vp->inc_cdb_buffer_id ? pkt_count : 0;
331237740Sscottl			/* Zero out payload of ccb union after ccb header. */
332237740Sscottl			bzero((u_char *)ccb + sizeof(struct ccb_hdr),
333237740Sscottl			    sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
334237740Sscottl			/* Copy previously constructed cdb into ccb_scsiio struct. */
335237740Sscottl			bcopy(&cdb, &ccb->csio.cdb_io.cdb_bytes[0],
336237740Sscottl			    sizeof(struct scsi_write_buffer));
337237740Sscottl			/* Fill rest of ccb_scsiio struct. */
338237740Sscottl			if (!sim_mode) {
339237740Sscottl				cam_fill_csio(&ccb->csio,		/* ccb_scsiio	*/
340237740Sscottl				    retry_count,			/* retries	*/
341237740Sscottl				    NULL,				/* cbfcnp	*/
342237740Sscottl				    CAM_DIR_OUT | CAM_DEV_QFRZDIS,	/* flags	*/
343237740Sscottl				    CAM_TAG_ACTION_NONE,		/* tag_action	*/
344237740Sscottl				    (u_char *)pkt_ptr,			/* data_ptr	*/
345237740Sscottl				    pkt_size,				/* dxfer_len	*/
346237740Sscottl				    SSD_FULL_SIZE,			/* sense_len	*/
347237740Sscottl				    sizeof(struct scsi_write_buffer),	/* cdb_len	*/
348237740Sscottl				    timeout ? timeout : CMD_TIMEOUT);	/* timeout	*/
349237740Sscottl			}
350237740Sscottl		} else if (strcmp(type, "ata") == 0) {
351237740Sscottl			bzero(&(&ccb->ccb_h)[1],
352237740Sscottl			      sizeof(struct ccb_ataio) - sizeof(struct ccb_hdr));
353237740Sscottl			if (!sim_mode) {
354237740Sscottl				uint32_t	off;
355237740Sscottl
356237740Sscottl				cam_fill_ataio(&ccb->ataio,
357237740Sscottl					(last_pkt) ? 256 : retry_count,
358237740Sscottl					NULL,
359237740Sscottl					/*flags*/CAM_DIR_OUT | CAM_DEV_QFRZDIS,
360237740Sscottl					CAM_TAG_ACTION_NONE,
361237740Sscottl					/*data_ptr*/(uint8_t *)pkt_ptr,
362237740Sscottl					/*dxfer_len*/pkt_size,
363237740Sscottl					timeout ? timeout : 30 * 1000);
364237740Sscottl				off = (uint32_t)(pkt_ptr - buf);
365237740Sscottl				ata_28bit_cmd(&ccb->ataio, ATA_DOWNLOAD_MICROCODE,
366237740Sscottl					USE_OFFSETS_FEATURE,
367237740Sscottl					ATA_MAKE_LBA(off, pkt_size),
368237740Sscottl					ATA_MAKE_SECTORS(pkt_size));
369237740Sscottl			}
370237740Sscottl		}
371227961Semaste		if (!sim_mode) {
372227961Semaste			/* Execute the command. */
373252199Smav			if (cam_send_ccb(cam_dev, ccb) < 0 ||
374252199Smav			    (ccb->ccb_h.status & CAM_STATUS_MASK) !=
375252199Smav			    CAM_REQ_CMP) {
376227961Semaste				warnx("Error writing image to device");
377243294Semaste				if (printerrors)
378227961Semaste					cam_error_print(cam_dev, ccb, CAM_ESF_ALL,
379237740Sscottl						   CAM_EPF_ALL, stderr);
380227961Semaste				goto bailout;
381227961Semaste			}
382227961Semaste		}
383227961Semaste		/* Prepare next round. */
384227961Semaste		pkt_count++;
385227961Semaste		pkt_ptr += pkt_size;
386227961Semaste		img_size -= pkt_size;
387227961Semaste	} while(!last_pkt);
388237740Sscottl	progress_complete(&progress, size - img_size);
389227961Semaste	cam_freeccb(ccb);
390227961Semaste	return (0);
391227961Semastebailout:
392237740Sscottl	progress_complete(&progress, size - img_size);
393227961Semaste	cam_freeccb(ccb);
394227961Semaste	return (1);
395227961Semaste}
396227961Semaste
397227961Semasteint
398227961Semastefwdownload(struct cam_device *device, int argc, char **argv,
399243294Semaste    char *combinedopt, int printerrors, int retry_count, int timeout,
400237740Sscottl    const char *type)
401227961Semaste{
402237740Sscottl	const struct fw_vendor *vp;
403227961Semaste	char *fw_img_path = NULL;
404227961Semaste	char *buf;
405227961Semaste	int img_size;
406227961Semaste	int c;
407227961Semaste	int sim_mode = 0;
408227961Semaste	int confirmed = 0;
409227961Semaste
410227961Semaste	while ((c = getopt(argc, argv, combinedopt)) != -1) {
411227961Semaste		switch (c) {
412227961Semaste		case 's':
413227961Semaste			sim_mode = 1;
414227961Semaste			confirmed = 1;
415227961Semaste			break;
416227961Semaste		case 'f':
417227961Semaste			fw_img_path = optarg;
418227961Semaste			break;
419227961Semaste		case 'y':
420227961Semaste			confirmed = 1;
421227961Semaste			break;
422227961Semaste		default:
423227961Semaste			break;
424227961Semaste		}
425227961Semaste	}
426227961Semaste
427227961Semaste	if (fw_img_path == NULL)
428237740Sscottl		errx(1, "you must specify a firmware image file using -f option");
429227961Semaste
430227961Semaste	vp = fw_get_vendor(device);
431237740Sscottl	if (vp == NULL)
432237740Sscottl		errx(1, "NULL vendor");
433237740Sscottl	if (vp->type == VENDOR_UNKNOWN)
434237740Sscottl		warnx("Unsupported device - flashing through an HBA?");
435227961Semaste
436227961Semaste	buf = fw_read_img(fw_img_path, vp, &img_size);
437227961Semaste	if (buf == NULL)
438227961Semaste		goto fail;
439227961Semaste
440227961Semaste	if (!confirmed) {
441227961Semaste		fprintf(stdout, "You are about to download firmware image (%s)"
442227961Semaste		    " into the following device:\n",
443227961Semaste		    fw_img_path);
444227961Semaste		fprintf(stdout, "\nIt may damage your drive. ");
445227961Semaste		if (!get_confirmation())
446227961Semaste			goto fail;
447227961Semaste	}
448227961Semaste	if (sim_mode)
449227961Semaste		fprintf(stdout, "Running in simulation mode\n");
450227961Semaste
451243294Semaste	if (fw_download_img(device, vp, buf, img_size, sim_mode, printerrors,
452237740Sscottl	    retry_count, timeout, fw_img_path, type) != 0) {
453227961Semaste		fprintf(stderr, "Firmware download failed\n");
454227961Semaste		goto fail;
455237740Sscottl	}
456237740Sscottl	else
457227961Semaste		fprintf(stdout, "Firmware download successful\n");
458227961Semaste
459227961Semaste	free(buf);
460227961Semaste	return (0);
461227961Semastefail:
462227961Semaste	if (buf != NULL)
463227961Semaste		free(buf);
464227961Semaste	return (1);
465227961Semaste}
466227961Semaste
467