fwdownload.c revision 300547
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 300547 2016-05-24 00:57:11Z truckman $");
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
71286965Sken#define	WB_TIMEOUT 50000	/* 50 seconds */
72227961Semaste
73227961Semastetypedef enum {
74286965Sken	VENDOR_HGST,
75227961Semaste	VENDOR_HITACHI,
76227961Semaste	VENDOR_HP,
77227961Semaste	VENDOR_IBM,
78227961Semaste	VENDOR_PLEXTOR,
79237281Sscottl	VENDOR_QUALSTAR,
80227961Semaste	VENDOR_QUANTUM,
81255310Sbryanv	VENDOR_SAMSUNG,
82227961Semaste	VENDOR_SEAGATE,
83286965Sken	VENDOR_SMART,
84286965Sken	VENDOR_ATA,
85227961Semaste	VENDOR_UNKNOWN
86227961Semaste} fw_vendor_t;
87227961Semaste
88286965Sken/*
89286965Sken * FW_TUR_READY:     The drive must return good status for a test unit ready.
90286965Sken *
91286965Sken * FW_TUR_NOT_READY: The drive must return not ready status for a test unit
92286965Sken *		     ready.  You may want this in a removable media drive.
93286965Sken *
94286965Sken * FW_TUR_NA:	     It doesn't matter whether the drive is ready or not.
95286965Sken * 		     This may be the case for a removable media drive.
96286965Sken */
97286965Skentypedef enum {
98286965Sken	FW_TUR_NONE,
99286965Sken	FW_TUR_READY,
100286965Sken	FW_TUR_NOT_READY,
101286965Sken	FW_TUR_NA
102286965Sken} fw_tur_status;
103286965Sken
104286965Sken/*
105286965Sken * FW_TIMEOUT_DEFAULT:		Attempt to probe for a WRITE BUFFER timeout
106286965Sken *				value from the drive.  If we get an answer,
107286965Sken *				use the Recommended timeout.  Otherwise,
108286965Sken * 				use the default value from the table.
109286965Sken *
110286965Sken * FW_TIMEOUT_DEV_REPORTED:	The timeout value was probed directly from
111286965Sken *				the device.
112286965Sken *
113286965Sken * FW_TIMEOUT_NO_PROBE:		Do not ask the device for a WRITE BUFFER
114286965Sken * 				timeout value.  Use the device-specific
115286965Sken *				value.
116286965Sken *
117286965Sken * FW_TIMEOUT_USER_SPEC:	The user specified a timeout on the command
118286965Sken *				line with the -t option.  This overrides any
119286965Sken *				probe or default timeout.
120286965Sken */
121286965Skentypedef enum {
122286965Sken	FW_TIMEOUT_DEFAULT,
123286965Sken	FW_TIMEOUT_DEV_REPORTED,
124286965Sken	FW_TIMEOUT_NO_PROBE,
125286965Sken	FW_TIMEOUT_USER_SPEC
126286965Sken} fw_timeout_type;
127286965Sken
128286965Sken/*
129286965Sken * type: 		Enumeration for the particular vendor.
130286965Sken *
131286965Sken * pattern:		Pattern to match for the Vendor ID from the SCSI
132286965Sken *			Inquiry data.
133286965Sken *
134286965Sken * dev_type:		SCSI device type to match, or T_ANY to match any
135286965Sken *			device from the given vendor.  Note that if there
136286965Sken *			is a specific device type listed for a particular
137286965Sken *			vendor, it must be listed before a T_ANY entry.
138286965Sken *
139286965Sken * max_pkt_size:	Maximum packet size when talking to a device.  Note
140286965Sken *			that although large data sizes may be supported by
141286965Sken *			the target device, they may not be supported by the
142286965Sken *			OS or the controller.
143286965Sken *
144286965Sken * cdb_byte2:		This specifies byte 2 (byte 1 when counting from 0)
145286965Sken *			of the CDB.  This is generally the WRITE BUFFER mode.
146286965Sken *
147286965Sken * cdb_byte2_last:	This specifies byte 2 for the last chunk of the
148286965Sken *			download.
149286965Sken *
150286965Sken * inc_cdb_buffer_id:	Increment the buffer ID by 1 for each chunk sent
151286965Sken *			down to the drive.
152286965Sken *
153286965Sken * inc_cdb_offset:	Increment the offset field in the CDB with the byte
154286965Sken *			offset into the firmware file.
155286965Sken *
156286965Sken * tur_status:		Pay attention to whether the device is ready before
157286965Sken *			upgrading the firmware, or not.  See above for the
158286965Sken *			values.
159286965Sken */
160227961Semastestruct fw_vendor {
161227961Semaste	fw_vendor_t type;
162227961Semaste	const char *pattern;
163286965Sken	int dev_type;
164227961Semaste	int max_pkt_size;
165227961Semaste	u_int8_t cdb_byte2;
166227961Semaste	u_int8_t cdb_byte2_last;
167227961Semaste	int inc_cdb_buffer_id;
168227961Semaste	int inc_cdb_offset;
169286965Sken	fw_tur_status tur_status;
170286965Sken	int timeout_ms;
171286965Sken	fw_timeout_type timeout_type;
172227961Semaste};
173227961Semaste
174286965Sken/*
175286965Sken * Vendor notes:
176286965Sken *
177286965Sken * HGST:     The packets need to be sent in multiples of 4K.
178286965Sken *
179286965Sken * IBM:      For LTO and TS drives, the buffer ID is ignored in mode 7 (and
180286965Sken * 	     some other modes).  It treats the request as a firmware download.
181286965Sken *           The offset (and therefore the length of each chunk sent) needs
182286965Sken *           to be a multiple of the offset boundary specified for firmware
183286965Sken *           (buffer ID 4) in the read buffer command.  At least for LTO-6,
184286965Sken *           that seems to be 0, but using a 32K chunk size should satisfy
185286965Sken *           most any alignment requirement.
186286965Sken *
187286965Sken * SmrtStor: Mode 5 is also supported, but since the firmware is 400KB or
188286965Sken *           so, we can't fit it in a single request in most cases.
189286965Sken */
190286965Skenstatic struct fw_vendor vendors_list[] = {
191286965Sken	{VENDOR_HGST,	 	"HGST",		T_DIRECT,
192286965Sken	0x1000, 0x07, 0x07, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
193286965Sken	{VENDOR_HITACHI, 	"HITACHI",	T_ANY,
194286965Sken	0x8000, 0x05, 0x05, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
195286965Sken	{VENDOR_HP,	 	"HP",		T_ANY,
196286965Sken	0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
197286965Sken	{VENDOR_IBM,		"IBM",		T_SEQUENTIAL,
198286965Sken	0x8000, 0x07, 0x07, 0, 1, FW_TUR_NA, 300 * 1000, FW_TIMEOUT_DEFAULT},
199286965Sken	{VENDOR_IBM,		"IBM",		T_ANY,
200286965Sken	0x8000, 0x05, 0x05, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
201286965Sken	{VENDOR_PLEXTOR,	"PLEXTOR",	T_ANY,
202286965Sken	0x2000, 0x04, 0x05, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
203286965Sken	{VENDOR_QUALSTAR,	"QUALSTAR",	T_ANY,
204286965Sken	0x2030, 0x05, 0x05, 0, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
205286965Sken	{VENDOR_QUANTUM,	"QUANTUM",	T_ANY,
206286965Sken	0x2000, 0x04, 0x05, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
207286965Sken	{VENDOR_SAMSUNG,	"SAMSUNG",	T_ANY,
208286965Sken	0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
209286965Sken	{VENDOR_SEAGATE,	"SEAGATE",	T_ANY,
210286965Sken	0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
211286965Sken	{VENDOR_SMART,		"SmrtStor",	T_DIRECT,
212286965Sken	0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
213286965Sken
214286965Sken	/*
215286965Sken	 * We match any ATA device.  This is really just a placeholder,
216286965Sken	 * since we won't actually send a WRITE BUFFER with any of the
217286965Sken	 * listed parameters.  If a SATA device is behind a SAS controller,
218286965Sken	 * the SCSI to ATA translation code (at least for LSI) doesn't
219298858Spfg	 * generally translate a SCSI WRITE BUFFER into an ATA DOWNLOAD
220286965Sken	 * MICROCODE command.  So, we use the SCSI ATA PASS_THROUGH command
221286965Sken	 * to send the ATA DOWNLOAD MICROCODE command instead.
222286965Sken	 */
223286965Sken	{VENDOR_ATA,		"ATA",		T_ANY,
224286965Sken	 0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT,
225286965Sken	 FW_TIMEOUT_NO_PROBE},
226286965Sken	{VENDOR_UNKNOWN,	NULL,		T_ANY,
227286965Sken	0x0000, 0x00, 0x00, 0, 0, FW_TUR_NONE, WB_TIMEOUT, FW_TIMEOUT_DEFAULT}
228227961Semaste};
229227961Semaste
230286965Skenstruct fw_timeout_desc {
231286965Sken	fw_timeout_type timeout_type;
232286965Sken	const char *timeout_desc;
233286965Sken};
234286965Sken
235286965Skenstatic const struct fw_timeout_desc fw_timeout_desc_table[] = {
236286965Sken	{ FW_TIMEOUT_DEFAULT, "the default" },
237286965Sken	{ FW_TIMEOUT_DEV_REPORTED, "recommended by this particular device" },
238286965Sken	{ FW_TIMEOUT_NO_PROBE, "the default" },
239286965Sken	{ FW_TIMEOUT_USER_SPEC, "what was specified on the command line" }
240286965Sken};
241286965Sken
242237281Sscottl#ifndef ATA_DOWNLOAD_MICROCODE
243237281Sscottl#define ATA_DOWNLOAD_MICROCODE	0x92
244237281Sscottl#endif
245237281Sscottl
246237281Sscottl#define USE_OFFSETS_FEATURE	0x3
247237281Sscottl
248237281Sscottl#ifndef LOW_SECTOR_SIZE
249237281Sscottl#define LOW_SECTOR_SIZE		512
250237281Sscottl#endif
251237281Sscottl
252237281Sscottl#define ATA_MAKE_LBA(o, p)	\
253237281Sscottl	((((((o) / LOW_SECTOR_SIZE) >> 8) & 0xff) << 16) | \
254237281Sscottl	  ((((o) / LOW_SECTOR_SIZE) & 0xff) << 8) | \
255237281Sscottl	  ((((p) / LOW_SECTOR_SIZE) >> 8) & 0xff))
256237281Sscottl
257237281Sscottl#define ATA_MAKE_SECTORS(p)	(((p) / 512) & 0xff)
258237281Sscottl
259237281Sscottl#ifndef UNKNOWN_MAX_PKT_SIZE
260237281Sscottl#define UNKNOWN_MAX_PKT_SIZE	0x8000
261237281Sscottl#endif
262237281Sscottl
263286965Skenstatic struct fw_vendor *fw_get_vendor(struct cam_device *cam_dev,
264286965Sken				       struct ata_params *ident_buf);
265286965Skenstatic int fw_get_timeout(struct cam_device *cam_dev, struct fw_vendor *vp,
266286965Sken			  int retry_count, int timeout);
267286965Skenstatic int fw_validate_ibm(struct cam_device *dev, int retry_count,
268286965Sken			   int timeout, int fd, char *buf,
269286965Sken			    const char *fw_img_path, int quiet);
270286965Skenstatic char *fw_read_img(struct cam_device *dev, int retry_count,
271286965Sken			 int timeout, int quiet, const char *fw_img_path,
272286965Sken			 struct fw_vendor *vp, int *num_bytes);
273286965Skenstatic int fw_check_device_ready(struct cam_device *dev,
274286965Sken				 camcontrol_devtype devtype,
275286965Sken				 struct fw_vendor *vp, int printerrors,
276286965Sken				 int timeout);
277286965Skenstatic int fw_download_img(struct cam_device *cam_dev,
278286965Sken			   struct fw_vendor *vp, char *buf, int img_size,
279286965Sken			   int sim_mode, int printerrors, int quiet,
280286965Sken			   int retry_count, int timeout, const char */*name*/,
281286965Sken			   camcontrol_devtype devtype);
282227961Semaste
283227961Semaste/*
284227961Semaste * Find entry in vendors list that belongs to
285227961Semaste * the vendor of given cam device.
286227961Semaste */
287286965Skenstatic struct fw_vendor *
288286965Skenfw_get_vendor(struct cam_device *cam_dev, struct ata_params *ident_buf)
289227961Semaste{
290286965Sken	char vendor[42];
291286965Sken	struct fw_vendor *vp;
292227961Semaste
293227961Semaste	if (cam_dev == NULL)
294227961Semaste		return (NULL);
295286965Sken
296286965Sken	if (ident_buf != NULL) {
297286965Sken		cam_strvis((u_char *)vendor, ident_buf->model,
298286965Sken		    sizeof(ident_buf->model), sizeof(vendor));
299286965Sken		for (vp = vendors_list; vp->pattern != NULL; vp++) {
300286965Sken			if (vp->type == VENDOR_ATA)
301286965Sken				return (vp);
302286965Sken		}
303286965Sken	} else {
304286965Sken		cam_strvis((u_char *)vendor, (u_char *)cam_dev->inq_data.vendor,
305286965Sken		    sizeof(cam_dev->inq_data.vendor), sizeof(vendor));
306286965Sken	}
307227961Semaste	for (vp = vendors_list; vp->pattern != NULL; vp++) {
308227961Semaste		if (!cam_strmatch((const u_char *)vendor,
309286965Sken		    (const u_char *)vp->pattern, strlen(vendor))) {
310286965Sken			if ((vp->dev_type == T_ANY)
311286965Sken			 || (vp->dev_type == SID_TYPE(&cam_dev->inq_data)))
312286965Sken				break;
313286965Sken		}
314227961Semaste	}
315227961Semaste	return (vp);
316227961Semaste}
317227961Semaste
318286965Skenstatic int
319286965Skenfw_get_timeout(struct cam_device *cam_dev, struct fw_vendor *vp,
320286965Sken	       int retry_count, int timeout)
321286965Sken{
322286965Sken	struct scsi_report_supported_opcodes_one *one;
323286965Sken	struct scsi_report_supported_opcodes_timeout *td;
324286965Sken	uint8_t *buf = NULL;
325286965Sken	uint32_t fill_len = 0, cdb_len = 0, rec_timeout = 0;
326286965Sken	int retval = 0;
327286965Sken
328286965Sken	/*
329286965Sken	 * If the user has specified a timeout on the command line, we let
330286965Sken	 * him override any default or probed value.
331286965Sken	 */
332286965Sken	if (timeout != 0) {
333286965Sken		vp->timeout_type = FW_TIMEOUT_USER_SPEC;
334286965Sken		vp->timeout_ms = timeout;
335286965Sken		goto bailout;
336286965Sken	}
337286965Sken
338286965Sken	/*
339286965Sken	 * Check to see whether we should probe for a timeout for this
340286965Sken	 * device.
341286965Sken	 */
342286965Sken	if (vp->timeout_type == FW_TIMEOUT_NO_PROBE)
343286965Sken		goto bailout;
344286965Sken
345286965Sken	retval = scsigetopcodes(/*device*/ cam_dev,
346286965Sken				/*opcode_set*/ 1,
347286965Sken				/*opcode*/ WRITE_BUFFER,
348286965Sken				/*show_sa_errors*/ 1,
349286965Sken				/*sa_set*/ 0,
350286965Sken				/*service_action*/ 0,
351286965Sken				/*timeout_desc*/ 1,
352286965Sken				/*retry_count*/ retry_count,
353286965Sken				/*timeout*/ 10000,
354286965Sken				/*verbose*/ 0,
355286965Sken				/*fill_len*/ &fill_len,
356286965Sken				/*data_ptr*/ &buf);
357286965Sken	/*
358286965Sken	 * It isn't an error if we can't get a timeout descriptor.  We just
359286965Sken	 * continue on with the default timeout.
360286965Sken	 */
361286965Sken	if (retval != 0) {
362286965Sken		retval = 0;
363286965Sken		goto bailout;
364286965Sken	}
365286965Sken
366286965Sken	/*
367286965Sken	 * Even if the drive didn't return a SCSI error, if we don't have
368286965Sken	 * enough data to contain the one opcode descriptor, the CDB
369286965Sken	 * structure and a timeout descriptor, we don't have the timeout
370286965Sken	 * value we're looking for.  So we'll just fall back to the
371286965Sken	 * default value.
372286965Sken	 */
373286965Sken	if (fill_len < (sizeof(*one) + sizeof(struct scsi_write_buffer) +
374286965Sken	    sizeof(*td)))
375286965Sken		goto bailout;
376286965Sken
377286965Sken	one = (struct scsi_report_supported_opcodes_one *)buf;
378286965Sken
379286965Sken	/*
380286965Sken	 * If the drive claims to not support the WRITE BUFFER command...
381286965Sken	 * fall back to the default timeout value and let things fail on
382286965Sken	 * the actual firmware download.
383286965Sken	 */
384286965Sken	if ((one->support & RSO_ONE_SUP_MASK) == RSO_ONE_SUP_NOT_SUP)
385286965Sken		goto bailout;
386286965Sken
387286965Sken	cdb_len = scsi_2btoul(one->cdb_length);
388286965Sken	td = (struct scsi_report_supported_opcodes_timeout *)
389286965Sken	    &buf[sizeof(*one) + cdb_len];
390286965Sken
391286965Sken	rec_timeout = scsi_4btoul(td->recommended_time);
392286965Sken	/*
393286965Sken	 * If the recommended timeout is 0, then the device has probably
394286965Sken	 * returned a bogus value.
395286965Sken	 */
396286965Sken	if (rec_timeout == 0)
397286965Sken		goto bailout;
398286965Sken
399286965Sken	/* CAM timeouts are in ms */
400286965Sken	rec_timeout *= 1000;
401286965Sken
402286965Sken	vp->timeout_ms = rec_timeout;
403286965Sken	vp->timeout_type = FW_TIMEOUT_DEV_REPORTED;
404286965Sken
405286965Skenbailout:
406286965Sken	return (retval);
407286965Sken}
408286965Sken
409286965Sken#define	SVPD_IBM_FW_DESIGNATION		0x03
410286965Sken
411227961Semaste/*
412286965Sken * IBM LTO and TS tape drives have an INQUIRY VPD page 0x3 with the following
413286965Sken * format:
414286965Sken */
415286965Skenstruct fw_ibm_tape_fw_designation {
416286965Sken	uint8_t	device;
417286965Sken	uint8_t page_code;
418286965Sken	uint8_t reserved;
419286965Sken	uint8_t length;
420286965Sken	uint8_t ascii_length;
421286965Sken	uint8_t reserved2[3];
422286965Sken	uint8_t load_id[4];
423286965Sken	uint8_t fw_rev[4];
424286965Sken	uint8_t ptf_number[4];
425286965Sken	uint8_t patch_number[4];
426286965Sken	uint8_t ru_name[8];
427286965Sken	uint8_t lib_seq_num[5];
428286965Sken};
429286965Sken
430286965Sken/*
431286965Sken * The firmware for IBM tape drives has the following header format.  The
432286965Sken * load_id and ru_name in the header file should match what is returned in
433286965Sken * VPD page 0x3.
434286965Sken */
435286965Skenstruct fw_ibm_tape_fw_header {
436286965Sken	uint8_t unspec[4];
437286965Sken	uint8_t length[4];		/* Firmware and header! */
438286965Sken	uint8_t load_id[4];
439286965Sken	uint8_t fw_rev[4];
440286965Sken	uint8_t reserved[8];
441286965Sken	uint8_t ru_name[8];
442286965Sken};
443286965Sken
444286965Skenstatic int
445286965Skenfw_validate_ibm(struct cam_device *dev, int retry_count, int timeout, int fd,
446286965Sken		char *buf, const char *fw_img_path, int quiet)
447286965Sken{
448286965Sken	union ccb *ccb;
449286965Sken	struct fw_ibm_tape_fw_designation vpd_page;
450286965Sken	struct fw_ibm_tape_fw_header *header;
451286965Sken	char drive_rev[sizeof(vpd_page.fw_rev) + 1];
452286965Sken	char file_rev[sizeof(vpd_page.fw_rev) + 1];
453286965Sken	int retval = 1;
454286965Sken
455286965Sken	ccb = cam_getccb(dev);
456286965Sken	if (ccb == NULL) {
457286965Sken		warnx("couldn't allocate CCB");
458286965Sken		goto bailout;
459286965Sken	}
460286965Sken
461286965Sken	/* cam_getccb cleans up the header, caller has to zero the payload */
462300547Struckman	CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
463286965Sken
464286965Sken	bzero(&vpd_page, sizeof(vpd_page));
465286965Sken
466286965Sken	scsi_inquiry(&ccb->csio,
467286965Sken		     /*retries*/ retry_count,
468286965Sken		     /*cbfcnp*/ NULL,
469286965Sken		     /* tag_action */ MSG_SIMPLE_Q_TAG,
470286965Sken		     /* inq_buf */ (u_int8_t *)&vpd_page,
471286965Sken		     /* inq_len */ sizeof(vpd_page),
472286965Sken		     /* evpd */ 1,
473286965Sken		     /* page_code */ SVPD_IBM_FW_DESIGNATION,
474286965Sken		     /* sense_len */ SSD_FULL_SIZE,
475286965Sken		     /* timeout */ timeout ? timeout : 5000);
476286965Sken
477286965Sken	/* Disable freezing the device queue */
478286965Sken	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
479286965Sken
480286965Sken	if (retry_count != 0)
481286965Sken		ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
482286965Sken
483286965Sken	if (cam_send_ccb(dev, ccb) < 0) {
484286965Sken		warn("error getting firmware designation page");
485286965Sken
486286965Sken		cam_error_print(dev, ccb, CAM_ESF_ALL,
487286965Sken				CAM_EPF_ALL, stderr);
488286965Sken
489286965Sken		cam_freeccb(ccb);
490299490Scem		ccb = NULL;
491286965Sken		goto bailout;
492286965Sken	}
493286965Sken
494286965Sken	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
495286965Sken		cam_error_print(dev, ccb, CAM_ESF_ALL,
496286965Sken				CAM_EPF_ALL, stderr);
497286965Sken		goto bailout;
498286965Sken	}
499286965Sken
500286965Sken	/*
501286965Sken	 * Read the firmware header only.
502286965Sken	 */
503286965Sken	if (read(fd, buf, sizeof(*header)) != sizeof(*header)) {
504286965Sken		warn("unable to read %zu bytes from %s", sizeof(*header),
505286965Sken		     fw_img_path);
506286965Sken		goto bailout;
507286965Sken	}
508286965Sken
509286965Sken	/* Rewind the file back to 0 for the full file read. */
510286965Sken	if (lseek(fd, 0, SEEK_SET) == -1) {
511286965Sken		warn("Unable to lseek");
512286965Sken		goto bailout;
513286965Sken	}
514286965Sken
515286965Sken	header = (struct fw_ibm_tape_fw_header *)buf;
516286965Sken
517286965Sken	bzero(drive_rev, sizeof(drive_rev));
518286965Sken	bcopy(vpd_page.fw_rev, drive_rev, sizeof(vpd_page.fw_rev));
519286965Sken	bzero(file_rev, sizeof(file_rev));
520286965Sken	bcopy(header->fw_rev, file_rev, sizeof(header->fw_rev));
521286965Sken
522286965Sken	if (quiet == 0) {
523286965Sken		fprintf(stdout, "Current Drive Firmware version: %s\n",
524286965Sken			drive_rev);
525286965Sken		fprintf(stdout, "Firmware File version: %s\n", file_rev);
526286965Sken	}
527286965Sken
528286965Sken	/*
529286965Sken	 * For IBM tape drives the load ID and RU name reported by the
530286965Sken	 * drive should match what is in the firmware file.
531286965Sken	 */
532286965Sken	if (bcmp(vpd_page.load_id, header->load_id,
533286965Sken		 MIN(sizeof(vpd_page.load_id), sizeof(header->load_id))) != 0) {
534286965Sken		warnx("Drive Firmware load ID 0x%x does not match firmware "
535286965Sken		      "file load ID 0x%x", scsi_4btoul(vpd_page.load_id),
536286965Sken		      scsi_4btoul(header->load_id));
537286965Sken		goto bailout;
538286965Sken	}
539286965Sken
540286965Sken	if (bcmp(vpd_page.ru_name, header->ru_name,
541286965Sken		 MIN(sizeof(vpd_page.ru_name), sizeof(header->ru_name))) != 0) {
542286965Sken		warnx("Drive Firmware RU name 0x%jx does not match firmware "
543286965Sken		      "file RU name 0x%jx",
544286965Sken		      (uintmax_t)scsi_8btou64(vpd_page.ru_name),
545286965Sken		      (uintmax_t)scsi_8btou64(header->ru_name));
546286965Sken		goto bailout;
547286965Sken	}
548286965Sken	if (quiet == 0)
549286965Sken		fprintf(stdout, "Firmware file is valid for this drive.\n");
550286965Sken	retval = 0;
551286965Skenbailout:
552299490Scem	if (ccb != NULL)
553299490Scem		cam_freeccb(ccb);
554286965Sken
555286965Sken	return (retval);
556286965Sken}
557286965Sken
558286965Sken/*
559227961Semaste * Allocate a buffer and read fw image file into it
560227961Semaste * from given path. Number of bytes read is stored
561227961Semaste * in num_bytes.
562227961Semaste */
563227961Semastestatic char *
564286965Skenfw_read_img(struct cam_device *dev, int retry_count, int timeout, int quiet,
565286965Sken	    const char *fw_img_path, struct fw_vendor *vp, int *num_bytes)
566227961Semaste{
567227961Semaste	int fd;
568227961Semaste	struct stat stbuf;
569227961Semaste	char *buf;
570227961Semaste	off_t img_size;
571227961Semaste	int skip_bytes = 0;
572227961Semaste
573227961Semaste	if ((fd = open(fw_img_path, O_RDONLY)) < 0) {
574227961Semaste		warn("Could not open image file %s", fw_img_path);
575227961Semaste		return (NULL);
576227961Semaste	}
577227961Semaste	if (fstat(fd, &stbuf) < 0) {
578227961Semaste		warn("Could not stat image file %s", fw_img_path);
579227961Semaste		goto bailout1;
580227961Semaste	}
581227961Semaste	if ((img_size = stbuf.st_size) == 0) {
582227961Semaste		warnx("Zero length image file %s", fw_img_path);
583227961Semaste		goto bailout1;
584227961Semaste	}
585227961Semaste	if ((buf = malloc(img_size)) == NULL) {
586227961Semaste		warnx("Could not allocate buffer to read image file %s",
587227961Semaste		    fw_img_path);
588227961Semaste		goto bailout1;
589227961Semaste	}
590227961Semaste	/* Skip headers if applicable. */
591227961Semaste	switch (vp->type) {
592227961Semaste	case VENDOR_SEAGATE:
593227961Semaste		if (read(fd, buf, 16) != 16) {
594227961Semaste			warn("Could not read image file %s", fw_img_path);
595227961Semaste			goto bailout;
596227961Semaste		}
597227961Semaste		if (lseek(fd, 0, SEEK_SET) == -1) {
598227961Semaste			warn("Unable to lseek");
599227961Semaste			goto bailout;
600227961Semaste		}
601227961Semaste		if ((strncmp(buf, "SEAGATE,SEAGATE ", 16) == 0) ||
602227961Semaste		    (img_size % 512 == 80))
603227961Semaste			skip_bytes = 80;
604227961Semaste		break;
605237281Sscottl	case VENDOR_QUALSTAR:
606237281Sscottl		skip_bytes = img_size % 1030;
607237281Sscottl		break;
608286965Sken	case VENDOR_IBM: {
609286965Sken		if (vp->dev_type != T_SEQUENTIAL)
610286965Sken			break;
611286965Sken		if (fw_validate_ibm(dev, retry_count, timeout, fd, buf,
612286965Sken				    fw_img_path, quiet) != 0)
613286965Sken			goto bailout;
614286965Sken		break;
615286965Sken	}
616227961Semaste	default:
617227961Semaste		break;
618227961Semaste	}
619227961Semaste	if (skip_bytes != 0) {
620227961Semaste		fprintf(stdout, "Skipping %d byte header.\n", skip_bytes);
621227961Semaste		if (lseek(fd, skip_bytes, SEEK_SET) == -1) {
622227961Semaste			warn("Could not lseek");
623227961Semaste			goto bailout;
624227961Semaste		}
625227961Semaste		img_size -= skip_bytes;
626227961Semaste	}
627227961Semaste	/* Read image into a buffer. */
628227961Semaste	if (read(fd, buf, img_size) != img_size) {
629227961Semaste		warn("Could not read image file %s", fw_img_path);
630227961Semaste		goto bailout;
631227961Semaste	}
632227961Semaste	*num_bytes = img_size;
633256113Semaste	close(fd);
634227961Semaste	return (buf);
635227961Semastebailout:
636227961Semaste	free(buf);
637227961Semastebailout1:
638227961Semaste	close(fd);
639227961Semaste	*num_bytes = 0;
640227961Semaste	return (NULL);
641227961Semaste}
642227961Semaste
643286965Sken/*
644286965Sken * Returns 0 for "success", where success means that the device has met the
645286965Sken * requirement in the vendor structure for being ready or not ready when
646286965Sken * firmware is downloaded.
647286965Sken *
648286965Sken * Returns 1 for a failure to be ready to accept a firmware download.
649286965Sken * (e.g., a drive needs to be ready, but returns not ready)
650286965Sken *
651286965Sken * Returns -1 for any other failure.
652286965Sken */
653286965Skenstatic int
654286965Skenfw_check_device_ready(struct cam_device *dev, camcontrol_devtype devtype,
655286965Sken		      struct fw_vendor *vp, int printerrors, int timeout)
656286965Sken{
657286965Sken	union ccb *ccb;
658286965Sken	int retval = 0;
659286965Sken	int16_t *ptr = NULL;
660286965Sken	size_t dxfer_len = 0;
661286965Sken
662286965Sken	if ((ccb = cam_getccb(dev)) == NULL) {
663286965Sken		warnx("Could not allocate CCB");
664286965Sken		retval = -1;
665286965Sken		goto bailout;
666286965Sken	}
667286965Sken
668300547Struckman	CCB_CLEAR_ALL_EXCEPT_HDR(ccb);
669286965Sken
670286965Sken	if (devtype != CC_DT_SCSI) {
671286965Sken		dxfer_len = sizeof(struct ata_params);
672286965Sken
673286965Sken		ptr = (uint16_t *)malloc(dxfer_len);
674286965Sken		if (ptr == NULL) {
675286965Sken			warnx("can't malloc memory for identify");
676286965Sken			retval = -1;
677286965Sken			goto bailout;
678286965Sken		}
679286965Sken		bzero(ptr, dxfer_len);
680286965Sken	}
681286965Sken
682286965Sken	switch (devtype) {
683286965Sken	case CC_DT_SCSI:
684286965Sken		scsi_test_unit_ready(&ccb->csio,
685286965Sken				     /*retries*/ 0,
686286965Sken				     /*cbfcnp*/ NULL,
687286965Sken				     /*tag_action*/ MSG_SIMPLE_Q_TAG,
688286965Sken		    		     /*sense_len*/ SSD_FULL_SIZE,
689286965Sken				     /*timeout*/ 5000);
690286965Sken		break;
691286965Sken	case CC_DT_ATA_BEHIND_SCSI:
692286965Sken	case CC_DT_ATA: {
693300207Sken		retval = build_ata_cmd(ccb,
694286965Sken			     /*retries*/ 1,
695286965Sken			     /*flags*/ CAM_DIR_IN,
696286965Sken			     /*tag_action*/ MSG_SIMPLE_Q_TAG,
697286965Sken			     /*protocol*/ AP_PROTO_PIO_IN,
698286965Sken			     /*ata_flags*/ AP_FLAG_BYT_BLOK_BYTES |
699286965Sken					   AP_FLAG_TLEN_SECT_CNT |
700286965Sken					   AP_FLAG_TDIR_FROM_DEV,
701286965Sken			     /*features*/ 0,
702286965Sken			     /*sector_count*/ (uint8_t) dxfer_len,
703286965Sken			     /*lba*/ 0,
704286965Sken			     /*command*/ ATA_ATA_IDENTIFY,
705300207Sken			     /*auxiliary*/ 0,
706286965Sken			     /*data_ptr*/ (uint8_t *)ptr,
707286965Sken			     /*dxfer_len*/ dxfer_len,
708300207Sken			     /*cdb_storage*/ NULL,
709300207Sken			     /*cdb_storage_len*/ 0,
710286965Sken			     /*sense_len*/ SSD_FULL_SIZE,
711286965Sken			     /*timeout*/ timeout ? timeout : 30 * 1000,
712286965Sken			     /*is48bit*/ 0,
713286965Sken			     /*devtype*/ devtype);
714300207Sken		if (retval != 0) {
715300207Sken			retval = -1;
716300207Sken			warnx("%s: build_ata_cmd() failed, likely "
717300207Sken			    "programmer error", __func__);
718300207Sken			goto bailout;
719300207Sken		}
720286965Sken		break;
721286965Sken	}
722286965Sken	default:
723286965Sken		warnx("Unknown disk type %d", devtype);
724286965Sken		retval = -1;
725286965Sken		goto bailout;
726286965Sken		break; /*NOTREACHED*/
727286965Sken	}
728286965Sken
729286965Sken	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
730286965Sken
731286965Sken	retval = cam_send_ccb(dev, ccb);
732286965Sken	if (retval != 0) {
733286965Sken		warn("error sending %s CCB", (devtype == CC_DT_SCSI) ?
734286965Sken		     "Test Unit Ready" : "Identify");
735286965Sken		retval = -1;
736286965Sken		goto bailout;
737286965Sken	}
738286965Sken
739286965Sken	if (((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
740286965Sken	 && (vp->tur_status == FW_TUR_READY)) {
741286965Sken		warnx("Device is not ready");
742286965Sken		if (printerrors)
743286965Sken			cam_error_print(dev, ccb, CAM_ESF_ALL,
744286965Sken			    CAM_EPF_ALL, stderr);
745286965Sken		retval = 1;
746286965Sken		goto bailout;
747286965Sken	} else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
748286965Sken		&& (vp->tur_status == FW_TUR_NOT_READY)) {
749286965Sken		warnx("Device cannot have media loaded when firmware is "
750286965Sken		    "downloaded");
751286965Sken		retval = 1;
752286965Sken		goto bailout;
753286965Sken	}
754286965Skenbailout:
755286965Sken	if (ccb != NULL)
756286965Sken		cam_freeccb(ccb);
757286965Sken
758286965Sken	return (retval);
759286965Sken}
760286965Sken
761227961Semaste/*
762227961Semaste * Download firmware stored in buf to cam_dev. If simulation mode
763227961Semaste * is enabled, only show what packet sizes would be sent to the
764227961Semaste * device but do not sent any actual packets
765227961Semaste */
766227961Semastestatic int
767286965Skenfw_download_img(struct cam_device *cam_dev, struct fw_vendor *vp,
768286965Sken    char *buf, int img_size, int sim_mode, int printerrors, int quiet,
769286965Sken    int retry_count, int timeout, const char *imgname,
770286965Sken    camcontrol_devtype devtype)
771227961Semaste{
772227961Semaste	struct scsi_write_buffer cdb;
773237281Sscottl	progress_t progress;
774286965Sken	int size = 0;
775286965Sken	union ccb *ccb = NULL;
776227961Semaste	int pkt_count = 0;
777237281Sscottl	int max_pkt_size;
778227961Semaste	u_int32_t pkt_size = 0;
779227961Semaste	char *pkt_ptr = buf;
780227961Semaste	u_int32_t offset;
781227961Semaste	int last_pkt = 0;
782286965Sken	int retval = 0;
783227961Semaste
784286965Sken	/*
785286965Sken	 * Check to see whether the device is ready to accept a firmware
786286965Sken	 * download.
787286965Sken	 */
788286965Sken	retval = fw_check_device_ready(cam_dev, devtype, vp, printerrors,
789286965Sken				       timeout);
790286965Sken	if (retval != 0)
791286965Sken		goto bailout;
792286965Sken
793227961Semaste	if ((ccb = cam_getccb(cam_dev)) == NULL) {
794227961Semaste		warnx("Could not allocate CCB");
795286965Sken		retval = 1;
796286965Sken		goto bailout;
797227961Semaste	}
798237281Sscottl
799300547Struckman	CCB_CLEAR_ALL_EXCEPT_HDR(ccb);
800237281Sscottl
801237281Sscottl	max_pkt_size = vp->max_pkt_size;
802286965Sken	if (max_pkt_size == 0)
803237281Sscottl		max_pkt_size = UNKNOWN_MAX_PKT_SIZE;
804286965Sken
805286965Sken	pkt_size = max_pkt_size;
806237281Sscottl	progress_init(&progress, imgname, size = img_size);
807227961Semaste	/* Download single fw packets. */
808227961Semaste	do {
809237281Sscottl		if (img_size <= max_pkt_size) {
810227961Semaste			last_pkt = 1;
811227961Semaste			pkt_size = img_size;
812227961Semaste		}
813237281Sscottl		progress_update(&progress, size - img_size);
814286965Sken		if (((sim_mode == 0) && (quiet == 0))
815286965Sken		 || ((sim_mode != 0) && (printerrors == 0)))
816286965Sken			progress_draw(&progress);
817227961Semaste		bzero(&cdb, sizeof(cdb));
818286965Sken		switch (devtype) {
819286965Sken		case CC_DT_SCSI:
820237281Sscottl			cdb.opcode  = WRITE_BUFFER;
821237281Sscottl			cdb.control = 0;
822237281Sscottl			/* Parameter list length. */
823237281Sscottl			scsi_ulto3b(pkt_size, &cdb.length[0]);
824237281Sscottl			offset = vp->inc_cdb_offset ? (pkt_ptr - buf) : 0;
825237281Sscottl			scsi_ulto3b(offset, &cdb.offset[0]);
826286965Sken			cdb.byte2 = last_pkt ? vp->cdb_byte2_last :
827286965Sken					       vp->cdb_byte2;
828237281Sscottl			cdb.buffer_id = vp->inc_cdb_buffer_id ? pkt_count : 0;
829237281Sscottl			/* Zero out payload of ccb union after ccb header. */
830300547Struckman			CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
831286965Sken			/*
832286965Sken			 * Copy previously constructed cdb into ccb_scsiio
833286965Sken			 * struct.
834286965Sken			 */
835237281Sscottl			bcopy(&cdb, &ccb->csio.cdb_io.cdb_bytes[0],
836237281Sscottl			    sizeof(struct scsi_write_buffer));
837237281Sscottl			/* Fill rest of ccb_scsiio struct. */
838286965Sken			cam_fill_csio(&ccb->csio,		/* ccb_scsiio*/
839286965Sken			    retry_count,			/* retries*/
840286965Sken			    NULL,				/* cbfcnp*/
841286965Sken			    CAM_DIR_OUT | CAM_DEV_QFRZDIS,	/* flags*/
842286965Sken			    CAM_TAG_ACTION_NONE,		/* tag_action*/
843286965Sken			    (u_char *)pkt_ptr,			/* data_ptr*/
844286965Sken			    pkt_size,				/* dxfer_len*/
845286965Sken			    SSD_FULL_SIZE,			/* sense_len*/
846286965Sken			    sizeof(struct scsi_write_buffer),	/* cdb_len*/
847286965Sken			    timeout ? timeout : WB_TIMEOUT);	/* timeout*/
848286965Sken			break;
849286965Sken		case CC_DT_ATA:
850286965Sken		case CC_DT_ATA_BEHIND_SCSI: {
851286965Sken			uint32_t	off;
852237281Sscottl
853286965Sken			off = (uint32_t)(pkt_ptr - buf);
854286965Sken
855300207Sken			retval = build_ata_cmd(ccb,
856286965Sken			    /*retry_count*/ retry_count,
857286965Sken			    /*flags*/ CAM_DIR_OUT | CAM_DEV_QFRZDIS,
858286965Sken			    /*tag_action*/ CAM_TAG_ACTION_NONE,
859286965Sken			    /*protocol*/ AP_PROTO_PIO_OUT,
860286965Sken			    /*ata_flags*/ AP_FLAG_BYT_BLOK_BYTES |
861286965Sken					  AP_FLAG_TLEN_SECT_CNT |
862286965Sken					  AP_FLAG_TDIR_TO_DEV,
863286965Sken			    /*features*/ USE_OFFSETS_FEATURE,
864286965Sken			    /*sector_count*/ ATA_MAKE_SECTORS(pkt_size),
865286965Sken			    /*lba*/ ATA_MAKE_LBA(off, pkt_size),
866286965Sken			    /*command*/ ATA_DOWNLOAD_MICROCODE,
867300207Sken			    /*auxiliary*/ 0,
868286965Sken			    /*data_ptr*/ (uint8_t *)pkt_ptr,
869286965Sken			    /*dxfer_len*/ pkt_size,
870300207Sken			    /*cdb_storage*/ NULL,
871300207Sken			    /*cdb_storage_len*/ 0,
872286965Sken			    /*sense_len*/ SSD_FULL_SIZE,
873286965Sken			    /*timeout*/ timeout ? timeout : WB_TIMEOUT,
874286965Sken			    /*is48bit*/ 0,
875286965Sken			    /*devtype*/ devtype);
876300207Sken
877300207Sken			if (retval != 0) {
878300207Sken				warnx("%s: build_ata_cmd() failed, likely "
879300207Sken				    "programmer error", __func__);
880300207Sken				goto bailout;
881300207Sken			}
882286965Sken			break;
883237281Sscottl		}
884286965Sken		default:
885286965Sken			warnx("Unknown device type %d", devtype);
886286965Sken			retval = 1;
887286965Sken			goto bailout;
888286965Sken			break; /*NOTREACHED*/
889286965Sken		}
890227961Semaste		if (!sim_mode) {
891227961Semaste			/* Execute the command. */
892251743Smav			if (cam_send_ccb(cam_dev, ccb) < 0 ||
893251743Smav			    (ccb->ccb_h.status & CAM_STATUS_MASK) !=
894251743Smav			    CAM_REQ_CMP) {
895227961Semaste				warnx("Error writing image to device");
896241737Sed				if (printerrors)
897286965Sken					cam_error_print(cam_dev, ccb,
898286965Sken					    CAM_ESF_ALL, CAM_EPF_ALL, stderr);
899286965Sken				retval = 1;
900227961Semaste				goto bailout;
901227961Semaste			}
902286965Sken		} else if (printerrors) {
903286965Sken			cam_error_print(cam_dev, ccb, CAM_ESF_COMMAND, 0,
904286965Sken			    stdout);
905227961Semaste		}
906286965Sken
907227961Semaste		/* Prepare next round. */
908227961Semaste		pkt_count++;
909227961Semaste		pkt_ptr += pkt_size;
910227961Semaste		img_size -= pkt_size;
911227961Semaste	} while(!last_pkt);
912227961Semastebailout:
913286965Sken	if (quiet == 0)
914286965Sken		progress_complete(&progress, size - img_size);
915286965Sken	if (ccb != NULL)
916286965Sken		cam_freeccb(ccb);
917286965Sken	return (retval);
918227961Semaste}
919227961Semaste
920227961Semasteint
921227961Semastefwdownload(struct cam_device *device, int argc, char **argv,
922286965Sken    char *combinedopt, int printerrors, int retry_count, int timeout)
923227961Semaste{
924286965Sken	struct fw_vendor *vp;
925227961Semaste	char *fw_img_path = NULL;
926286965Sken	struct ata_params *ident_buf = NULL;
927286965Sken	camcontrol_devtype devtype;
928286965Sken	char *buf = NULL;
929227961Semaste	int img_size;
930227961Semaste	int c;
931227961Semaste	int sim_mode = 0;
932227961Semaste	int confirmed = 0;
933286965Sken	int quiet = 0;
934286965Sken	int retval = 0;
935227961Semaste
936227961Semaste	while ((c = getopt(argc, argv, combinedopt)) != -1) {
937227961Semaste		switch (c) {
938286965Sken		case 'f':
939286965Sken			fw_img_path = optarg;
940286965Sken			break;
941286965Sken		case 'q':
942286965Sken			quiet = 1;
943286965Sken			break;
944227961Semaste		case 's':
945227961Semaste			sim_mode = 1;
946227961Semaste			break;
947227961Semaste		case 'y':
948227961Semaste			confirmed = 1;
949227961Semaste			break;
950227961Semaste		default:
951227961Semaste			break;
952227961Semaste		}
953227961Semaste	}
954227961Semaste
955227961Semaste	if (fw_img_path == NULL)
956286965Sken		errx(1, "you must specify a firmware image file using -f "
957286965Sken		     "option");
958227961Semaste
959286965Sken	retval = get_device_type(device, retry_count, timeout, printerrors,
960286965Sken				 &devtype);
961286965Sken	if (retval != 0)
962286965Sken		errx(1, "Unable to determine device type");
963227961Semaste
964286965Sken	if ((devtype == CC_DT_ATA)
965286965Sken	 || (devtype == CC_DT_ATA_BEHIND_SCSI)) {
966286965Sken		union ccb *ccb;
967227961Semaste
968286965Sken		ccb = cam_getccb(device);
969286965Sken		if (ccb == NULL) {
970286965Sken			warnx("couldn't allocate CCB");
971286965Sken			retval = 1;
972286965Sken			goto bailout;
973286965Sken		}
974286965Sken
975286965Sken		if (ata_do_identify(device, retry_count, timeout, ccb,
976286965Sken		    		    &ident_buf) != 0) {
977286965Sken			cam_freeccb(ccb);
978286965Sken			retval = 1;
979286965Sken			goto bailout;
980286965Sken		}
981286965Sken	} else if (devtype != CC_DT_SCSI)
982286965Sken		errx(1, "Unsupported device type %d", devtype);
983286965Sken
984286965Sken	vp = fw_get_vendor(device, ident_buf);
985286965Sken	/*
986286965Sken	 * Bail out if we have an unknown vendor and this isn't an ATA
987286965Sken	 * disk.  For a SCSI disk, we have no chance of working properly
988286965Sken	 * with the default values in the VENDOR_UNKNOWN case.  For an ATA
989286965Sken	 * disk connected via an ATA transport, we may work for drives that
990286965Sken	 * support the ATA_DOWNLOAD_MICROCODE command.
991286965Sken	 */
992286965Sken	if (((vp == NULL)
993286965Sken	  || (vp->type == VENDOR_UNKNOWN))
994286965Sken	 && (devtype == CC_DT_SCSI))
995286965Sken		errx(1, "Unsupported device");
996286965Sken
997286965Sken	retval = fw_get_timeout(device, vp, retry_count, timeout);
998286965Sken	if (retval != 0) {
999286965Sken		warnx("Unable to get a firmware download timeout value");
1000286965Sken		goto bailout;
1001286965Sken	}
1002286965Sken
1003286965Sken	buf = fw_read_img(device, retry_count, timeout, quiet, fw_img_path,
1004286965Sken	    vp, &img_size);
1005286965Sken	if (buf == NULL) {
1006286965Sken		retval = 1;
1007286965Sken		goto bailout;
1008286965Sken	}
1009286965Sken
1010227961Semaste	if (!confirmed) {
1011227961Semaste		fprintf(stdout, "You are about to download firmware image (%s)"
1012227961Semaste		    " into the following device:\n",
1013227961Semaste		    fw_img_path);
1014286965Sken		if (devtype == CC_DT_SCSI) {
1015286965Sken			if (scsidoinquiry(device, argc, argv, combinedopt, 0,
1016286965Sken					  5000) != 0) {
1017286965Sken				warnx("Error sending inquiry");
1018286965Sken				retval = 1;
1019286965Sken				goto bailout;
1020286965Sken			}
1021286965Sken		} else {
1022286965Sken			printf("%s%d: ", device->device_name,
1023286965Sken			    device->dev_unit_num);
1024286965Sken			ata_print_ident(ident_buf);
1025286965Sken			camxferrate(device);
1026286965Sken			free(ident_buf);
1027286965Sken		}
1028286965Sken		fprintf(stdout, "Using a timeout of %u ms, which is %s.\n",
1029286965Sken			vp->timeout_ms,
1030286965Sken			fw_timeout_desc_table[vp->timeout_type].timeout_desc);
1031227961Semaste		fprintf(stdout, "\nIt may damage your drive. ");
1032286965Sken		if (!get_confirmation()) {
1033286965Sken			retval = 1;
1034286965Sken			goto bailout;
1035286965Sken		}
1036227961Semaste	}
1037286965Sken	if ((sim_mode != 0) && (quiet == 0))
1038227961Semaste		fprintf(stdout, "Running in simulation mode\n");
1039227961Semaste
1040241737Sed	if (fw_download_img(device, vp, buf, img_size, sim_mode, printerrors,
1041286965Sken	    quiet, retry_count, vp->timeout_ms, fw_img_path, devtype) != 0) {
1042227961Semaste		fprintf(stderr, "Firmware download failed\n");
1043286965Sken		retval = 1;
1044286965Sken		goto bailout;
1045286965Sken	} else if (quiet == 0)
1046227961Semaste		fprintf(stdout, "Firmware download successful\n");
1047227961Semaste
1048286965Skenbailout:
1049227961Semaste	free(buf);
1050286965Sken	return (retval);
1051227961Semaste}
1052227961Semaste
1053