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$");
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;
227256113Semaste	close(fd);
228227961Semaste	return (buf);
229227961Semastebailout:
230227961Semaste	free(buf);
231227961Semastebailout1:
232227961Semaste	close(fd);
233227961Semaste	*num_bytes = 0;
234227961Semaste	return (NULL);
235227961Semaste}
236227961Semaste
237227961Semaste/*
238227961Semaste * Download firmware stored in buf to cam_dev. If simulation mode
239227961Semaste * is enabled, only show what packet sizes would be sent to the
240227961Semaste * device but do not sent any actual packets
241227961Semaste */
242227961Semastestatic int
243228407Sedfw_download_img(struct cam_device *cam_dev, const struct fw_vendor *vp,
244241737Sed    char *buf, int img_size, int sim_mode, int printerrors, int retry_count,
245237281Sscottl    int timeout, const char *imgname, const char *type)
246227961Semaste{
247227961Semaste	struct scsi_write_buffer cdb;
248237281Sscottl	progress_t progress;
249237281Sscottl	int size;
250227961Semaste	union ccb *ccb;
251227961Semaste	int pkt_count = 0;
252237281Sscottl	int max_pkt_size;
253227961Semaste	u_int32_t pkt_size = 0;
254227961Semaste	char *pkt_ptr = buf;
255227961Semaste	u_int32_t offset;
256227961Semaste	int last_pkt = 0;
257237281Sscottl	int16_t *ptr;
258227961Semaste
259227961Semaste	if ((ccb = cam_getccb(cam_dev)) == NULL) {
260227961Semaste		warnx("Could not allocate CCB");
261227961Semaste		return (1);
262227961Semaste	}
263237281Sscottl	if (strcmp(type, "scsi") == 0) {
264237281Sscottl		scsi_test_unit_ready(&ccb->csio, 0, NULL, MSG_SIMPLE_Q_TAG,
265237281Sscottl		    SSD_FULL_SIZE, 5000);
266237281Sscottl	} else if (strcmp(type, "ata") == 0) {
267237281Sscottl		/* cam_getccb cleans up the header, caller has to zero the payload */
268237281Sscottl		bzero(&(&ccb->ccb_h)[1],
269237281Sscottl		      sizeof(struct ccb_ataio) - sizeof(struct ccb_hdr));
270237281Sscottl
271237281Sscottl		ptr = (uint16_t *)malloc(sizeof(struct ata_params));
272237281Sscottl
273237281Sscottl		if (ptr == NULL) {
274237281Sscottl			cam_freeccb(ccb);
275237281Sscottl			warnx("can't malloc memory for identify\n");
276237281Sscottl			return(1);
277237281Sscottl		}
278237281Sscottl		bzero(ptr, sizeof(struct ata_params));
279237281Sscottl		cam_fill_ataio(&ccb->ataio,
280237281Sscottl                      1,
281237281Sscottl                      NULL,
282237281Sscottl                      /*flags*/CAM_DIR_IN,
283237281Sscottl                      MSG_SIMPLE_Q_TAG,
284237281Sscottl                      /*data_ptr*/(uint8_t *)ptr,
285237281Sscottl                      /*dxfer_len*/sizeof(struct ata_params),
286237281Sscottl                      timeout ? timeout : 30 * 1000);
287237281Sscottl		ata_28bit_cmd(&ccb->ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
288237281Sscottl	} else {
289237281Sscottl		warnx("weird disk type '%s'", type);
290256113Semaste		cam_freeccb(ccb);
291237281Sscottl		return 1;
292237281Sscottl	}
293227961Semaste	/* Disable freezing the device queue. */
294227961Semaste	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
295227961Semaste	if (cam_send_ccb(cam_dev, ccb) < 0) {
296237281Sscottl		warnx("Error sending identify/test unit ready");
297241737Sed		if (printerrors)
298227961Semaste			cam_error_print(cam_dev, ccb, CAM_ESF_ALL,
299227961Semaste			    CAM_EPF_ALL, stderr);
300227961Semaste		cam_freeccb(ccb);
301227961Semaste		return(1);
302227961Semaste	}
303227961Semaste	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
304227961Semaste		warnx("Device is not ready");
305241737Sed		if (printerrors)
306227961Semaste			cam_error_print(cam_dev, ccb, CAM_ESF_ALL,
307227961Semaste			    CAM_EPF_ALL, stderr);
308227961Semaste		cam_freeccb(ccb);
309227961Semaste		return (1);
310227961Semaste	}
311237281Sscottl	max_pkt_size = vp->max_pkt_size;
312237281Sscottl	if (vp->max_pkt_size == 0 && strcmp(type, "ata") == 0) {
313237281Sscottl		max_pkt_size = UNKNOWN_MAX_PKT_SIZE;
314237281Sscottl	}
315227961Semaste	pkt_size = vp->max_pkt_size;
316237281Sscottl	progress_init(&progress, imgname, size = img_size);
317227961Semaste	/* Download single fw packets. */
318227961Semaste	do {
319237281Sscottl		if (img_size <= max_pkt_size) {
320227961Semaste			last_pkt = 1;
321227961Semaste			pkt_size = img_size;
322227961Semaste		}
323237281Sscottl		progress_update(&progress, size - img_size);
324237281Sscottl		progress_draw(&progress);
325227961Semaste		bzero(&cdb, sizeof(cdb));
326237281Sscottl		if (strcmp(type, "scsi") == 0) {
327237281Sscottl			cdb.opcode  = WRITE_BUFFER;
328237281Sscottl			cdb.control = 0;
329237281Sscottl			/* Parameter list length. */
330237281Sscottl			scsi_ulto3b(pkt_size, &cdb.length[0]);
331237281Sscottl			offset = vp->inc_cdb_offset ? (pkt_ptr - buf) : 0;
332237281Sscottl			scsi_ulto3b(offset, &cdb.offset[0]);
333237281Sscottl			cdb.byte2 = last_pkt ? vp->cdb_byte2_last : vp->cdb_byte2;
334237281Sscottl			cdb.buffer_id = vp->inc_cdb_buffer_id ? pkt_count : 0;
335237281Sscottl			/* Zero out payload of ccb union after ccb header. */
336237281Sscottl			bzero((u_char *)ccb + sizeof(struct ccb_hdr),
337237281Sscottl			    sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
338237281Sscottl			/* Copy previously constructed cdb into ccb_scsiio struct. */
339237281Sscottl			bcopy(&cdb, &ccb->csio.cdb_io.cdb_bytes[0],
340237281Sscottl			    sizeof(struct scsi_write_buffer));
341237281Sscottl			/* Fill rest of ccb_scsiio struct. */
342237281Sscottl			if (!sim_mode) {
343237281Sscottl				cam_fill_csio(&ccb->csio,		/* ccb_scsiio	*/
344237281Sscottl				    retry_count,			/* retries	*/
345237281Sscottl				    NULL,				/* cbfcnp	*/
346237281Sscottl				    CAM_DIR_OUT | CAM_DEV_QFRZDIS,	/* flags	*/
347237281Sscottl				    CAM_TAG_ACTION_NONE,		/* tag_action	*/
348237281Sscottl				    (u_char *)pkt_ptr,			/* data_ptr	*/
349237281Sscottl				    pkt_size,				/* dxfer_len	*/
350237281Sscottl				    SSD_FULL_SIZE,			/* sense_len	*/
351237281Sscottl				    sizeof(struct scsi_write_buffer),	/* cdb_len	*/
352237281Sscottl				    timeout ? timeout : CMD_TIMEOUT);	/* timeout	*/
353237281Sscottl			}
354237281Sscottl		} else if (strcmp(type, "ata") == 0) {
355237281Sscottl			bzero(&(&ccb->ccb_h)[1],
356237281Sscottl			      sizeof(struct ccb_ataio) - sizeof(struct ccb_hdr));
357237281Sscottl			if (!sim_mode) {
358237281Sscottl				uint32_t	off;
359237281Sscottl
360237281Sscottl				cam_fill_ataio(&ccb->ataio,
361237281Sscottl					(last_pkt) ? 256 : retry_count,
362237281Sscottl					NULL,
363237281Sscottl					/*flags*/CAM_DIR_OUT | CAM_DEV_QFRZDIS,
364237281Sscottl					CAM_TAG_ACTION_NONE,
365237281Sscottl					/*data_ptr*/(uint8_t *)pkt_ptr,
366237281Sscottl					/*dxfer_len*/pkt_size,
367237281Sscottl					timeout ? timeout : 30 * 1000);
368237281Sscottl				off = (uint32_t)(pkt_ptr - buf);
369237281Sscottl				ata_28bit_cmd(&ccb->ataio, ATA_DOWNLOAD_MICROCODE,
370237281Sscottl					USE_OFFSETS_FEATURE,
371237281Sscottl					ATA_MAKE_LBA(off, pkt_size),
372237281Sscottl					ATA_MAKE_SECTORS(pkt_size));
373237281Sscottl			}
374237281Sscottl		}
375227961Semaste		if (!sim_mode) {
376227961Semaste			/* Execute the command. */
377251743Smav			if (cam_send_ccb(cam_dev, ccb) < 0 ||
378251743Smav			    (ccb->ccb_h.status & CAM_STATUS_MASK) !=
379251743Smav			    CAM_REQ_CMP) {
380227961Semaste				warnx("Error writing image to device");
381241737Sed				if (printerrors)
382227961Semaste					cam_error_print(cam_dev, ccb, CAM_ESF_ALL,
383237281Sscottl						   CAM_EPF_ALL, stderr);
384227961Semaste				goto bailout;
385227961Semaste			}
386227961Semaste		}
387227961Semaste		/* Prepare next round. */
388227961Semaste		pkt_count++;
389227961Semaste		pkt_ptr += pkt_size;
390227961Semaste		img_size -= pkt_size;
391227961Semaste	} while(!last_pkt);
392237281Sscottl	progress_complete(&progress, size - img_size);
393227961Semaste	cam_freeccb(ccb);
394227961Semaste	return (0);
395227961Semastebailout:
396237281Sscottl	progress_complete(&progress, size - img_size);
397227961Semaste	cam_freeccb(ccb);
398227961Semaste	return (1);
399227961Semaste}
400227961Semaste
401227961Semasteint
402227961Semastefwdownload(struct cam_device *device, int argc, char **argv,
403241737Sed    char *combinedopt, int printerrors, int retry_count, int timeout,
404237281Sscottl    const char *type)
405227961Semaste{
406228407Sed	const struct fw_vendor *vp;
407227961Semaste	char *fw_img_path = NULL;
408227961Semaste	char *buf;
409227961Semaste	int img_size;
410227961Semaste	int c;
411227961Semaste	int sim_mode = 0;
412227961Semaste	int confirmed = 0;
413227961Semaste
414227961Semaste	while ((c = getopt(argc, argv, combinedopt)) != -1) {
415227961Semaste		switch (c) {
416227961Semaste		case 's':
417227961Semaste			sim_mode = 1;
418227961Semaste			confirmed = 1;
419227961Semaste			break;
420227961Semaste		case 'f':
421227961Semaste			fw_img_path = optarg;
422227961Semaste			break;
423227961Semaste		case 'y':
424227961Semaste			confirmed = 1;
425227961Semaste			break;
426227961Semaste		default:
427227961Semaste			break;
428227961Semaste		}
429227961Semaste	}
430227961Semaste
431227961Semaste	if (fw_img_path == NULL)
432237281Sscottl		errx(1, "you must specify a firmware image file using -f option");
433227961Semaste
434227961Semaste	vp = fw_get_vendor(device);
435237281Sscottl	if (vp == NULL)
436237281Sscottl		errx(1, "NULL vendor");
437237281Sscottl	if (vp->type == VENDOR_UNKNOWN)
438237281Sscottl		warnx("Unsupported device - flashing through an HBA?");
439227961Semaste
440227961Semaste	buf = fw_read_img(fw_img_path, vp, &img_size);
441227961Semaste	if (buf == NULL)
442227961Semaste		goto fail;
443227961Semaste
444227961Semaste	if (!confirmed) {
445227961Semaste		fprintf(stdout, "You are about to download firmware image (%s)"
446227961Semaste		    " into the following device:\n",
447227961Semaste		    fw_img_path);
448227961Semaste		fprintf(stdout, "\nIt may damage your drive. ");
449227961Semaste		if (!get_confirmation())
450227961Semaste			goto fail;
451227961Semaste	}
452227961Semaste	if (sim_mode)
453227961Semaste		fprintf(stdout, "Running in simulation mode\n");
454227961Semaste
455241737Sed	if (fw_download_img(device, vp, buf, img_size, sim_mode, printerrors,
456237281Sscottl	    retry_count, timeout, fw_img_path, type) != 0) {
457227961Semaste		fprintf(stderr, "Firmware download failed\n");
458227961Semaste		goto fail;
459237281Sscottl	}
460237281Sscottl	else
461227961Semaste		fprintf(stdout, "Firmware download successful\n");
462227961Semaste
463227961Semaste	free(buf);
464227961Semaste	return (0);
465227961Semastefail:
466227961Semaste	if (buf != NULL)
467227961Semaste		free(buf);
468227961Semaste	return (1);
469227961Semaste}
470227961Semaste
471