1227961Semaste/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
4227961Semaste * Copyright (c) 2011 Sandvine Incorporated. All rights reserved.
5227961Semaste * Copyright (c) 2002-2011 Andre Albsmeier <andre@albsmeier.net>
6227961Semaste * All rights reserved.
7227961Semaste *
8227961Semaste * Redistribution and use in source and binary forms, with or without
9227961Semaste * modification, are permitted provided that the following conditions
10227961Semaste * are met:
11227961Semaste * 1. Redistributions of source code must retain the above copyright
12227961Semaste *    notice, this list of conditions and the following disclaimer,
13227961Semaste *    without modification, immediately at the beginning of the file.
14227961Semaste * 2. Redistributions in binary form must reproduce the above copyright
15227961Semaste *    notice, this list of conditions and the following disclaimer in the
16227961Semaste *    documentation and/or other materials provided with the distribution.
17227961Semaste *
18227961Semaste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19227961Semaste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20227961Semaste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21227961Semaste * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22227961Semaste * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23227961Semaste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24227961Semaste * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25227961Semaste * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26227961Semaste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27227961Semaste * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28227961Semaste */
29227961Semaste
30227961Semaste/*
31228203Semaste * This software is derived from Andre Albsmeier's fwprog.c which contained
32228203Semaste * the following note:
33228203Semaste *
34228203Semaste * Many thanks goes to Marc Frajola <marc@terasolutions.com> from
35228203Semaste * TeraSolutions for the initial idea and his programme for upgrading
36228203Semaste * the firmware of I*M DDYS drives.
37228203Semaste */
38228203Semaste
39228203Semaste/*
40227961Semaste * BEWARE:
41227961Semaste *
42227961Semaste * The fact that you see your favorite vendor listed below does not
43227961Semaste * imply that your equipment won't break when you use this software
44227961Semaste * with it. It only means that the firmware of at least one device type
45227961Semaste * of each vendor listed has been programmed successfully using this code.
46227961Semaste *
47227961Semaste * The -s option simulates a download but does nothing apart from that.
48227961Semaste * It can be used to check what chunk sizes would have been used with the
49227961Semaste * specified device.
50227961Semaste */
51227961Semaste
52227961Semaste#include <sys/cdefs.h>
53227961Semaste__FBSDID("$FreeBSD: stable/11/sbin/camcontrol/fwdownload.c 352286 2019-09-13 14:43:05Z mav $");
54227961Semaste
55227961Semaste#include <sys/types.h>
56227961Semaste#include <sys/stat.h>
57227961Semaste
58227961Semaste#include <err.h>
59227961Semaste#include <fcntl.h>
60227961Semaste#include <stdio.h>
61227961Semaste#include <stdlib.h>
62227961Semaste#include <string.h>
63227961Semaste#include <unistd.h>
64227961Semaste
65227961Semaste#include <cam/scsi/scsi_all.h>
66227961Semaste#include <cam/scsi/scsi_message.h>
67227961Semaste#include <camlib.h>
68227961Semaste
69237281Sscottl#include "progress.h"
70237281Sscottl
71227961Semaste#include "camcontrol.h"
72227961Semaste
73286965Sken#define	WB_TIMEOUT 50000	/* 50 seconds */
74227961Semaste
75227961Semastetypedef enum {
76286965Sken	VENDOR_HGST,
77227961Semaste	VENDOR_HITACHI,
78227961Semaste	VENDOR_HP,
79227961Semaste	VENDOR_IBM,
80227961Semaste	VENDOR_PLEXTOR,
81237281Sscottl	VENDOR_QUALSTAR,
82227961Semaste	VENDOR_QUANTUM,
83255310Sbryanv	VENDOR_SAMSUNG,
84227961Semaste	VENDOR_SEAGATE,
85286965Sken	VENDOR_SMART,
86286965Sken	VENDOR_ATA,
87227961Semaste	VENDOR_UNKNOWN
88227961Semaste} fw_vendor_t;
89227961Semaste
90286965Sken/*
91286965Sken * FW_TUR_READY:     The drive must return good status for a test unit ready.
92286965Sken *
93286965Sken * FW_TUR_NOT_READY: The drive must return not ready status for a test unit
94286965Sken *		     ready.  You may want this in a removable media drive.
95286965Sken *
96286965Sken * FW_TUR_NA:	     It doesn't matter whether the drive is ready or not.
97286965Sken * 		     This may be the case for a removable media drive.
98286965Sken */
99286965Skentypedef enum {
100286965Sken	FW_TUR_NONE,
101286965Sken	FW_TUR_READY,
102286965Sken	FW_TUR_NOT_READY,
103286965Sken	FW_TUR_NA
104286965Sken} fw_tur_status;
105286965Sken
106286965Sken/*
107286965Sken * FW_TIMEOUT_DEFAULT:		Attempt to probe for a WRITE BUFFER timeout
108286965Sken *				value from the drive.  If we get an answer,
109286965Sken *				use the Recommended timeout.  Otherwise,
110286965Sken * 				use the default value from the table.
111286965Sken *
112286965Sken * FW_TIMEOUT_DEV_REPORTED:	The timeout value was probed directly from
113286965Sken *				the device.
114286965Sken *
115286965Sken * FW_TIMEOUT_NO_PROBE:		Do not ask the device for a WRITE BUFFER
116286965Sken * 				timeout value.  Use the device-specific
117286965Sken *				value.
118286965Sken *
119286965Sken * FW_TIMEOUT_USER_SPEC:	The user specified a timeout on the command
120286965Sken *				line with the -t option.  This overrides any
121286965Sken *				probe or default timeout.
122286965Sken */
123286965Skentypedef enum {
124286965Sken	FW_TIMEOUT_DEFAULT,
125286965Sken	FW_TIMEOUT_DEV_REPORTED,
126286965Sken	FW_TIMEOUT_NO_PROBE,
127286965Sken	FW_TIMEOUT_USER_SPEC
128286965Sken} fw_timeout_type;
129286965Sken
130286965Sken/*
131286965Sken * type: 		Enumeration for the particular vendor.
132286965Sken *
133286965Sken * pattern:		Pattern to match for the Vendor ID from the SCSI
134286965Sken *			Inquiry data.
135286965Sken *
136286965Sken * dev_type:		SCSI device type to match, or T_ANY to match any
137286965Sken *			device from the given vendor.  Note that if there
138286965Sken *			is a specific device type listed for a particular
139286965Sken *			vendor, it must be listed before a T_ANY entry.
140286965Sken *
141286965Sken * max_pkt_size:	Maximum packet size when talking to a device.  Note
142286965Sken *			that although large data sizes may be supported by
143286965Sken *			the target device, they may not be supported by the
144286965Sken *			OS or the controller.
145286965Sken *
146286965Sken * cdb_byte2:		This specifies byte 2 (byte 1 when counting from 0)
147286965Sken *			of the CDB.  This is generally the WRITE BUFFER mode.
148286965Sken *
149286965Sken * cdb_byte2_last:	This specifies byte 2 for the last chunk of the
150286965Sken *			download.
151286965Sken *
152286965Sken * inc_cdb_buffer_id:	Increment the buffer ID by 1 for each chunk sent
153286965Sken *			down to the drive.
154286965Sken *
155286965Sken * inc_cdb_offset:	Increment the offset field in the CDB with the byte
156286965Sken *			offset into the firmware file.
157286965Sken *
158286965Sken * tur_status:		Pay attention to whether the device is ready before
159286965Sken *			upgrading the firmware, or not.  See above for the
160286965Sken *			values.
161286965Sken */
162227961Semastestruct fw_vendor {
163227961Semaste	fw_vendor_t type;
164227961Semaste	const char *pattern;
165286965Sken	int dev_type;
166227961Semaste	int max_pkt_size;
167227961Semaste	u_int8_t cdb_byte2;
168227961Semaste	u_int8_t cdb_byte2_last;
169227961Semaste	int inc_cdb_buffer_id;
170227961Semaste	int inc_cdb_offset;
171286965Sken	fw_tur_status tur_status;
172286965Sken	int timeout_ms;
173286965Sken	fw_timeout_type timeout_type;
174227961Semaste};
175227961Semaste
176286965Sken/*
177286965Sken * Vendor notes:
178286965Sken *
179286965Sken * HGST:     The packets need to be sent in multiples of 4K.
180286965Sken *
181286965Sken * IBM:      For LTO and TS drives, the buffer ID is ignored in mode 7 (and
182286965Sken * 	     some other modes).  It treats the request as a firmware download.
183286965Sken *           The offset (and therefore the length of each chunk sent) needs
184286965Sken *           to be a multiple of the offset boundary specified for firmware
185286965Sken *           (buffer ID 4) in the read buffer command.  At least for LTO-6,
186286965Sken *           that seems to be 0, but using a 32K chunk size should satisfy
187286965Sken *           most any alignment requirement.
188286965Sken *
189286965Sken * SmrtStor: Mode 5 is also supported, but since the firmware is 400KB or
190286965Sken *           so, we can't fit it in a single request in most cases.
191286965Sken */
192286965Skenstatic struct fw_vendor vendors_list[] = {
193286965Sken	{VENDOR_HGST,	 	"HGST",		T_DIRECT,
194286965Sken	0x1000, 0x07, 0x07, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
195286965Sken	{VENDOR_HITACHI, 	"HITACHI",	T_ANY,
196286965Sken	0x8000, 0x05, 0x05, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
197286965Sken	{VENDOR_HP,	 	"HP",		T_ANY,
198286965Sken	0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
199286965Sken	{VENDOR_IBM,		"IBM",		T_SEQUENTIAL,
200286965Sken	0x8000, 0x07, 0x07, 0, 1, FW_TUR_NA, 300 * 1000, FW_TIMEOUT_DEFAULT},
201286965Sken	{VENDOR_IBM,		"IBM",		T_ANY,
202286965Sken	0x8000, 0x05, 0x05, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
203286965Sken	{VENDOR_PLEXTOR,	"PLEXTOR",	T_ANY,
204286965Sken	0x2000, 0x04, 0x05, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
205286965Sken	{VENDOR_QUALSTAR,	"QUALSTAR",	T_ANY,
206286965Sken	0x2030, 0x05, 0x05, 0, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
207286965Sken	{VENDOR_QUANTUM,	"QUANTUM",	T_ANY,
208286965Sken	0x2000, 0x04, 0x05, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
209286965Sken	{VENDOR_SAMSUNG,	"SAMSUNG",	T_ANY,
210286965Sken	0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
211286965Sken	{VENDOR_SEAGATE,	"SEAGATE",	T_ANY,
212286965Sken	0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
213286965Sken	{VENDOR_SMART,		"SmrtStor",	T_DIRECT,
214286965Sken	0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
215350771Smav	{VENDOR_HGST,	 	"WD",		T_DIRECT,
216350771Smav	0x1000, 0x07, 0x07, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
217350771Smav	{VENDOR_HGST,	 	"WDC",		T_DIRECT,
218350771Smav	0x1000, 0x07, 0x07, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
219286965Sken
220286965Sken	/*
221286965Sken	 * We match any ATA device.  This is really just a placeholder,
222286965Sken	 * since we won't actually send a WRITE BUFFER with any of the
223286965Sken	 * listed parameters.  If a SATA device is behind a SAS controller,
224286965Sken	 * the SCSI to ATA translation code (at least for LSI) doesn't
225298858Spfg	 * generally translate a SCSI WRITE BUFFER into an ATA DOWNLOAD
226286965Sken	 * MICROCODE command.  So, we use the SCSI ATA PASS_THROUGH command
227286965Sken	 * to send the ATA DOWNLOAD MICROCODE command instead.
228286965Sken	 */
229286965Sken	{VENDOR_ATA,		"ATA",		T_ANY,
230286965Sken	 0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT,
231286965Sken	 FW_TIMEOUT_NO_PROBE},
232286965Sken	{VENDOR_UNKNOWN,	NULL,		T_ANY,
233286965Sken	0x0000, 0x00, 0x00, 0, 0, FW_TUR_NONE, WB_TIMEOUT, FW_TIMEOUT_DEFAULT}
234227961Semaste};
235227961Semaste
236286965Skenstruct fw_timeout_desc {
237286965Sken	fw_timeout_type timeout_type;
238286965Sken	const char *timeout_desc;
239286965Sken};
240286965Sken
241286965Skenstatic const struct fw_timeout_desc fw_timeout_desc_table[] = {
242286965Sken	{ FW_TIMEOUT_DEFAULT, "the default" },
243286965Sken	{ FW_TIMEOUT_DEV_REPORTED, "recommended by this particular device" },
244286965Sken	{ FW_TIMEOUT_NO_PROBE, "the default" },
245286965Sken	{ FW_TIMEOUT_USER_SPEC, "what was specified on the command line" }
246286965Sken};
247286965Sken
248237281Sscottl#ifndef ATA_DOWNLOAD_MICROCODE
249237281Sscottl#define ATA_DOWNLOAD_MICROCODE	0x92
250237281Sscottl#endif
251237281Sscottl
252237281Sscottl#define USE_OFFSETS_FEATURE	0x3
253237281Sscottl
254237281Sscottl#ifndef LOW_SECTOR_SIZE
255237281Sscottl#define LOW_SECTOR_SIZE		512
256237281Sscottl#endif
257237281Sscottl
258237281Sscottl#define ATA_MAKE_LBA(o, p)	\
259237281Sscottl	((((((o) / LOW_SECTOR_SIZE) >> 8) & 0xff) << 16) | \
260237281Sscottl	  ((((o) / LOW_SECTOR_SIZE) & 0xff) << 8) | \
261237281Sscottl	  ((((p) / LOW_SECTOR_SIZE) >> 8) & 0xff))
262237281Sscottl
263237281Sscottl#define ATA_MAKE_SECTORS(p)	(((p) / 512) & 0xff)
264237281Sscottl
265237281Sscottl#ifndef UNKNOWN_MAX_PKT_SIZE
266237281Sscottl#define UNKNOWN_MAX_PKT_SIZE	0x8000
267237281Sscottl#endif
268237281Sscottl
269286965Skenstatic struct fw_vendor *fw_get_vendor(struct cam_device *cam_dev,
270286965Sken				       struct ata_params *ident_buf);
271286965Skenstatic int fw_get_timeout(struct cam_device *cam_dev, struct fw_vendor *vp,
272314220Sken			  int task_attr, int retry_count, int timeout);
273286965Skenstatic int fw_validate_ibm(struct cam_device *dev, int retry_count,
274286965Sken			   int timeout, int fd, char *buf,
275286965Sken			    const char *fw_img_path, int quiet);
276286965Skenstatic char *fw_read_img(struct cam_device *dev, int retry_count,
277286965Sken			 int timeout, int quiet, const char *fw_img_path,
278286965Sken			 struct fw_vendor *vp, int *num_bytes);
279286965Skenstatic int fw_check_device_ready(struct cam_device *dev,
280286965Sken				 camcontrol_devtype devtype,
281286965Sken				 struct fw_vendor *vp, int printerrors,
282286965Sken				 int timeout);
283286965Skenstatic int fw_download_img(struct cam_device *cam_dev,
284286965Sken			   struct fw_vendor *vp, char *buf, int img_size,
285286965Sken			   int sim_mode, int printerrors, int quiet,
286286965Sken			   int retry_count, int timeout, const char */*name*/,
287286965Sken			   camcontrol_devtype devtype);
288227961Semaste
289227961Semaste/*
290227961Semaste * Find entry in vendors list that belongs to
291227961Semaste * the vendor of given cam device.
292227961Semaste */
293286965Skenstatic struct fw_vendor *
294286965Skenfw_get_vendor(struct cam_device *cam_dev, struct ata_params *ident_buf)
295227961Semaste{
296286965Sken	char vendor[42];
297286965Sken	struct fw_vendor *vp;
298227961Semaste
299227961Semaste	if (cam_dev == NULL)
300227961Semaste		return (NULL);
301286965Sken
302286965Sken	if (ident_buf != NULL) {
303286965Sken		cam_strvis((u_char *)vendor, ident_buf->model,
304286965Sken		    sizeof(ident_buf->model), sizeof(vendor));
305286965Sken		for (vp = vendors_list; vp->pattern != NULL; vp++) {
306286965Sken			if (vp->type == VENDOR_ATA)
307286965Sken				return (vp);
308286965Sken		}
309286965Sken	} else {
310286965Sken		cam_strvis((u_char *)vendor, (u_char *)cam_dev->inq_data.vendor,
311286965Sken		    sizeof(cam_dev->inq_data.vendor), sizeof(vendor));
312286965Sken	}
313227961Semaste	for (vp = vendors_list; vp->pattern != NULL; vp++) {
314227961Semaste		if (!cam_strmatch((const u_char *)vendor,
315286965Sken		    (const u_char *)vp->pattern, strlen(vendor))) {
316286965Sken			if ((vp->dev_type == T_ANY)
317286965Sken			 || (vp->dev_type == SID_TYPE(&cam_dev->inq_data)))
318286965Sken				break;
319286965Sken		}
320227961Semaste	}
321227961Semaste	return (vp);
322227961Semaste}
323227961Semaste
324286965Skenstatic int
325286965Skenfw_get_timeout(struct cam_device *cam_dev, struct fw_vendor *vp,
326314220Sken	       int task_attr, int retry_count, int timeout)
327286965Sken{
328286965Sken	struct scsi_report_supported_opcodes_one *one;
329286965Sken	struct scsi_report_supported_opcodes_timeout *td;
330286965Sken	uint8_t *buf = NULL;
331286965Sken	uint32_t fill_len = 0, cdb_len = 0, rec_timeout = 0;
332286965Sken	int retval = 0;
333286965Sken
334286965Sken	/*
335286965Sken	 * If the user has specified a timeout on the command line, we let
336286965Sken	 * him override any default or probed value.
337286965Sken	 */
338286965Sken	if (timeout != 0) {
339286965Sken		vp->timeout_type = FW_TIMEOUT_USER_SPEC;
340286965Sken		vp->timeout_ms = timeout;
341286965Sken		goto bailout;
342286965Sken	}
343286965Sken
344286965Sken	/*
345286965Sken	 * Check to see whether we should probe for a timeout for this
346286965Sken	 * device.
347286965Sken	 */
348286965Sken	if (vp->timeout_type == FW_TIMEOUT_NO_PROBE)
349286965Sken		goto bailout;
350286965Sken
351286965Sken	retval = scsigetopcodes(/*device*/ cam_dev,
352286965Sken				/*opcode_set*/ 1,
353286965Sken				/*opcode*/ WRITE_BUFFER,
354286965Sken				/*show_sa_errors*/ 1,
355286965Sken				/*sa_set*/ 0,
356286965Sken				/*service_action*/ 0,
357286965Sken				/*timeout_desc*/ 1,
358314220Sken				/*task_attr*/ task_attr,
359286965Sken				/*retry_count*/ retry_count,
360286965Sken				/*timeout*/ 10000,
361286965Sken				/*verbose*/ 0,
362286965Sken				/*fill_len*/ &fill_len,
363286965Sken				/*data_ptr*/ &buf);
364286965Sken	/*
365286965Sken	 * It isn't an error if we can't get a timeout descriptor.  We just
366286965Sken	 * continue on with the default timeout.
367286965Sken	 */
368286965Sken	if (retval != 0) {
369286965Sken		retval = 0;
370286965Sken		goto bailout;
371286965Sken	}
372286965Sken
373286965Sken	/*
374286965Sken	 * Even if the drive didn't return a SCSI error, if we don't have
375286965Sken	 * enough data to contain the one opcode descriptor, the CDB
376286965Sken	 * structure and a timeout descriptor, we don't have the timeout
377286965Sken	 * value we're looking for.  So we'll just fall back to the
378286965Sken	 * default value.
379286965Sken	 */
380286965Sken	if (fill_len < (sizeof(*one) + sizeof(struct scsi_write_buffer) +
381286965Sken	    sizeof(*td)))
382286965Sken		goto bailout;
383286965Sken
384286965Sken	one = (struct scsi_report_supported_opcodes_one *)buf;
385286965Sken
386286965Sken	/*
387286965Sken	 * If the drive claims to not support the WRITE BUFFER command...
388286965Sken	 * fall back to the default timeout value and let things fail on
389286965Sken	 * the actual firmware download.
390286965Sken	 */
391286965Sken	if ((one->support & RSO_ONE_SUP_MASK) == RSO_ONE_SUP_NOT_SUP)
392286965Sken		goto bailout;
393286965Sken
394286965Sken	cdb_len = scsi_2btoul(one->cdb_length);
395286965Sken	td = (struct scsi_report_supported_opcodes_timeout *)
396286965Sken	    &buf[sizeof(*one) + cdb_len];
397286965Sken
398286965Sken	rec_timeout = scsi_4btoul(td->recommended_time);
399286965Sken	/*
400286965Sken	 * If the recommended timeout is 0, then the device has probably
401286965Sken	 * returned a bogus value.
402286965Sken	 */
403286965Sken	if (rec_timeout == 0)
404286965Sken		goto bailout;
405286965Sken
406286965Sken	/* CAM timeouts are in ms */
407286965Sken	rec_timeout *= 1000;
408286965Sken
409286965Sken	vp->timeout_ms = rec_timeout;
410286965Sken	vp->timeout_type = FW_TIMEOUT_DEV_REPORTED;
411286965Sken
412286965Skenbailout:
413286965Sken	return (retval);
414286965Sken}
415286965Sken
416286965Sken#define	SVPD_IBM_FW_DESIGNATION		0x03
417286965Sken
418227961Semaste/*
419286965Sken * IBM LTO and TS tape drives have an INQUIRY VPD page 0x3 with the following
420286965Sken * format:
421286965Sken */
422286965Skenstruct fw_ibm_tape_fw_designation {
423286965Sken	uint8_t	device;
424286965Sken	uint8_t page_code;
425286965Sken	uint8_t reserved;
426286965Sken	uint8_t length;
427286965Sken	uint8_t ascii_length;
428286965Sken	uint8_t reserved2[3];
429286965Sken	uint8_t load_id[4];
430286965Sken	uint8_t fw_rev[4];
431286965Sken	uint8_t ptf_number[4];
432286965Sken	uint8_t patch_number[4];
433286965Sken	uint8_t ru_name[8];
434286965Sken	uint8_t lib_seq_num[5];
435286965Sken};
436286965Sken
437286965Sken/*
438286965Sken * The firmware for IBM tape drives has the following header format.  The
439286965Sken * load_id and ru_name in the header file should match what is returned in
440286965Sken * VPD page 0x3.
441286965Sken */
442286965Skenstruct fw_ibm_tape_fw_header {
443286965Sken	uint8_t unspec[4];
444286965Sken	uint8_t length[4];		/* Firmware and header! */
445286965Sken	uint8_t load_id[4];
446286965Sken	uint8_t fw_rev[4];
447286965Sken	uint8_t reserved[8];
448286965Sken	uint8_t ru_name[8];
449286965Sken};
450286965Sken
451286965Skenstatic int
452286965Skenfw_validate_ibm(struct cam_device *dev, int retry_count, int timeout, int fd,
453286965Sken		char *buf, const char *fw_img_path, int quiet)
454286965Sken{
455286965Sken	union ccb *ccb;
456286965Sken	struct fw_ibm_tape_fw_designation vpd_page;
457286965Sken	struct fw_ibm_tape_fw_header *header;
458286965Sken	char drive_rev[sizeof(vpd_page.fw_rev) + 1];
459286965Sken	char file_rev[sizeof(vpd_page.fw_rev) + 1];
460286965Sken	int retval = 1;
461286965Sken
462286965Sken	ccb = cam_getccb(dev);
463286965Sken	if (ccb == NULL) {
464286965Sken		warnx("couldn't allocate CCB");
465286965Sken		goto bailout;
466286965Sken	}
467286965Sken
468286965Sken	/* cam_getccb cleans up the header, caller has to zero the payload */
469300547Struckman	CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
470286965Sken
471286965Sken	bzero(&vpd_page, sizeof(vpd_page));
472286965Sken
473286965Sken	scsi_inquiry(&ccb->csio,
474286965Sken		     /*retries*/ retry_count,
475286965Sken		     /*cbfcnp*/ NULL,
476286965Sken		     /* tag_action */ MSG_SIMPLE_Q_TAG,
477286965Sken		     /* inq_buf */ (u_int8_t *)&vpd_page,
478286965Sken		     /* inq_len */ sizeof(vpd_page),
479286965Sken		     /* evpd */ 1,
480286965Sken		     /* page_code */ SVPD_IBM_FW_DESIGNATION,
481286965Sken		     /* sense_len */ SSD_FULL_SIZE,
482286965Sken		     /* timeout */ timeout ? timeout : 5000);
483286965Sken
484286965Sken	/* Disable freezing the device queue */
485286965Sken	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
486286965Sken
487286965Sken	if (retry_count != 0)
488286965Sken		ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
489286965Sken
490286965Sken	if (cam_send_ccb(dev, ccb) < 0) {
491286965Sken		warn("error getting firmware designation page");
492286965Sken
493286965Sken		cam_error_print(dev, ccb, CAM_ESF_ALL,
494286965Sken				CAM_EPF_ALL, stderr);
495286965Sken
496286965Sken		cam_freeccb(ccb);
497299490Scem		ccb = NULL;
498286965Sken		goto bailout;
499286965Sken	}
500286965Sken
501286965Sken	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
502286965Sken		cam_error_print(dev, ccb, CAM_ESF_ALL,
503286965Sken				CAM_EPF_ALL, stderr);
504286965Sken		goto bailout;
505286965Sken	}
506286965Sken
507286965Sken	/*
508286965Sken	 * Read the firmware header only.
509286965Sken	 */
510286965Sken	if (read(fd, buf, sizeof(*header)) != sizeof(*header)) {
511286965Sken		warn("unable to read %zu bytes from %s", sizeof(*header),
512286965Sken		     fw_img_path);
513286965Sken		goto bailout;
514286965Sken	}
515286965Sken
516286965Sken	/* Rewind the file back to 0 for the full file read. */
517286965Sken	if (lseek(fd, 0, SEEK_SET) == -1) {
518286965Sken		warn("Unable to lseek");
519286965Sken		goto bailout;
520286965Sken	}
521286965Sken
522286965Sken	header = (struct fw_ibm_tape_fw_header *)buf;
523286965Sken
524286965Sken	bzero(drive_rev, sizeof(drive_rev));
525286965Sken	bcopy(vpd_page.fw_rev, drive_rev, sizeof(vpd_page.fw_rev));
526286965Sken	bzero(file_rev, sizeof(file_rev));
527286965Sken	bcopy(header->fw_rev, file_rev, sizeof(header->fw_rev));
528286965Sken
529286965Sken	if (quiet == 0) {
530286965Sken		fprintf(stdout, "Current Drive Firmware version: %s\n",
531286965Sken			drive_rev);
532286965Sken		fprintf(stdout, "Firmware File version: %s\n", file_rev);
533286965Sken	}
534286965Sken
535286965Sken	/*
536286965Sken	 * For IBM tape drives the load ID and RU name reported by the
537286965Sken	 * drive should match what is in the firmware file.
538286965Sken	 */
539286965Sken	if (bcmp(vpd_page.load_id, header->load_id,
540286965Sken		 MIN(sizeof(vpd_page.load_id), sizeof(header->load_id))) != 0) {
541286965Sken		warnx("Drive Firmware load ID 0x%x does not match firmware "
542286965Sken		      "file load ID 0x%x", scsi_4btoul(vpd_page.load_id),
543286965Sken		      scsi_4btoul(header->load_id));
544286965Sken		goto bailout;
545286965Sken	}
546286965Sken
547286965Sken	if (bcmp(vpd_page.ru_name, header->ru_name,
548286965Sken		 MIN(sizeof(vpd_page.ru_name), sizeof(header->ru_name))) != 0) {
549286965Sken		warnx("Drive Firmware RU name 0x%jx does not match firmware "
550286965Sken		      "file RU name 0x%jx",
551286965Sken		      (uintmax_t)scsi_8btou64(vpd_page.ru_name),
552286965Sken		      (uintmax_t)scsi_8btou64(header->ru_name));
553286965Sken		goto bailout;
554286965Sken	}
555286965Sken	if (quiet == 0)
556286965Sken		fprintf(stdout, "Firmware file is valid for this drive.\n");
557286965Sken	retval = 0;
558286965Skenbailout:
559318817Sasomers	cam_freeccb(ccb);
560286965Sken
561286965Sken	return (retval);
562286965Sken}
563286965Sken
564286965Sken/*
565227961Semaste * Allocate a buffer and read fw image file into it
566227961Semaste * from given path. Number of bytes read is stored
567227961Semaste * in num_bytes.
568227961Semaste */
569227961Semastestatic char *
570286965Skenfw_read_img(struct cam_device *dev, int retry_count, int timeout, int quiet,
571286965Sken	    const char *fw_img_path, struct fw_vendor *vp, int *num_bytes)
572227961Semaste{
573227961Semaste	int fd;
574227961Semaste	struct stat stbuf;
575227961Semaste	char *buf;
576227961Semaste	off_t img_size;
577227961Semaste	int skip_bytes = 0;
578227961Semaste
579227961Semaste	if ((fd = open(fw_img_path, O_RDONLY)) < 0) {
580227961Semaste		warn("Could not open image file %s", fw_img_path);
581227961Semaste		return (NULL);
582227961Semaste	}
583227961Semaste	if (fstat(fd, &stbuf) < 0) {
584227961Semaste		warn("Could not stat image file %s", fw_img_path);
585227961Semaste		goto bailout1;
586227961Semaste	}
587227961Semaste	if ((img_size = stbuf.st_size) == 0) {
588227961Semaste		warnx("Zero length image file %s", fw_img_path);
589227961Semaste		goto bailout1;
590227961Semaste	}
591227961Semaste	if ((buf = malloc(img_size)) == NULL) {
592227961Semaste		warnx("Could not allocate buffer to read image file %s",
593227961Semaste		    fw_img_path);
594227961Semaste		goto bailout1;
595227961Semaste	}
596227961Semaste	/* Skip headers if applicable. */
597227961Semaste	switch (vp->type) {
598227961Semaste	case VENDOR_SEAGATE:
599227961Semaste		if (read(fd, buf, 16) != 16) {
600227961Semaste			warn("Could not read image file %s", fw_img_path);
601227961Semaste			goto bailout;
602227961Semaste		}
603227961Semaste		if (lseek(fd, 0, SEEK_SET) == -1) {
604227961Semaste			warn("Unable to lseek");
605227961Semaste			goto bailout;
606227961Semaste		}
607227961Semaste		if ((strncmp(buf, "SEAGATE,SEAGATE ", 16) == 0) ||
608227961Semaste		    (img_size % 512 == 80))
609227961Semaste			skip_bytes = 80;
610227961Semaste		break;
611237281Sscottl	case VENDOR_QUALSTAR:
612237281Sscottl		skip_bytes = img_size % 1030;
613237281Sscottl		break;
614286965Sken	case VENDOR_IBM: {
615286965Sken		if (vp->dev_type != T_SEQUENTIAL)
616286965Sken			break;
617286965Sken		if (fw_validate_ibm(dev, retry_count, timeout, fd, buf,
618286965Sken				    fw_img_path, quiet) != 0)
619286965Sken			goto bailout;
620286965Sken		break;
621286965Sken	}
622227961Semaste	default:
623227961Semaste		break;
624227961Semaste	}
625227961Semaste	if (skip_bytes != 0) {
626227961Semaste		fprintf(stdout, "Skipping %d byte header.\n", skip_bytes);
627227961Semaste		if (lseek(fd, skip_bytes, SEEK_SET) == -1) {
628227961Semaste			warn("Could not lseek");
629227961Semaste			goto bailout;
630227961Semaste		}
631227961Semaste		img_size -= skip_bytes;
632227961Semaste	}
633227961Semaste	/* Read image into a buffer. */
634227961Semaste	if (read(fd, buf, img_size) != img_size) {
635227961Semaste		warn("Could not read image file %s", fw_img_path);
636227961Semaste		goto bailout;
637227961Semaste	}
638227961Semaste	*num_bytes = img_size;
639256113Semaste	close(fd);
640227961Semaste	return (buf);
641227961Semastebailout:
642227961Semaste	free(buf);
643227961Semastebailout1:
644227961Semaste	close(fd);
645227961Semaste	*num_bytes = 0;
646227961Semaste	return (NULL);
647227961Semaste}
648227961Semaste
649286965Sken/*
650286965Sken * Returns 0 for "success", where success means that the device has met the
651286965Sken * requirement in the vendor structure for being ready or not ready when
652286965Sken * firmware is downloaded.
653286965Sken *
654286965Sken * Returns 1 for a failure to be ready to accept a firmware download.
655286965Sken * (e.g., a drive needs to be ready, but returns not ready)
656286965Sken *
657286965Sken * Returns -1 for any other failure.
658286965Sken */
659286965Skenstatic int
660286965Skenfw_check_device_ready(struct cam_device *dev, camcontrol_devtype devtype,
661286965Sken		      struct fw_vendor *vp, int printerrors, int timeout)
662286965Sken{
663286965Sken	union ccb *ccb;
664286965Sken	int retval = 0;
665286965Sken	int16_t *ptr = NULL;
666286965Sken	size_t dxfer_len = 0;
667286965Sken
668286965Sken	if ((ccb = cam_getccb(dev)) == NULL) {
669286965Sken		warnx("Could not allocate CCB");
670286965Sken		retval = -1;
671286965Sken		goto bailout;
672286965Sken	}
673286965Sken
674300547Struckman	CCB_CLEAR_ALL_EXCEPT_HDR(ccb);
675286965Sken
676286965Sken	if (devtype != CC_DT_SCSI) {
677286965Sken		dxfer_len = sizeof(struct ata_params);
678286965Sken
679286965Sken		ptr = (uint16_t *)malloc(dxfer_len);
680286965Sken		if (ptr == NULL) {
681286965Sken			warnx("can't malloc memory for identify");
682286965Sken			retval = -1;
683286965Sken			goto bailout;
684286965Sken		}
685286965Sken		bzero(ptr, dxfer_len);
686286965Sken	}
687286965Sken
688286965Sken	switch (devtype) {
689286965Sken	case CC_DT_SCSI:
690286965Sken		scsi_test_unit_ready(&ccb->csio,
691286965Sken				     /*retries*/ 0,
692286965Sken				     /*cbfcnp*/ NULL,
693286965Sken				     /*tag_action*/ MSG_SIMPLE_Q_TAG,
694286965Sken		    		     /*sense_len*/ SSD_FULL_SIZE,
695286965Sken				     /*timeout*/ 5000);
696286965Sken		break;
697350796Smav	case CC_DT_SATL:
698286965Sken	case CC_DT_ATA: {
699300207Sken		retval = build_ata_cmd(ccb,
700286965Sken			     /*retries*/ 1,
701286965Sken			     /*flags*/ CAM_DIR_IN,
702286965Sken			     /*tag_action*/ MSG_SIMPLE_Q_TAG,
703286965Sken			     /*protocol*/ AP_PROTO_PIO_IN,
704352286Smav			     /*ata_flags*/ AP_FLAG_BYT_BLOK_BLOCKS |
705286965Sken					   AP_FLAG_TLEN_SECT_CNT |
706286965Sken					   AP_FLAG_TDIR_FROM_DEV,
707286965Sken			     /*features*/ 0,
708352286Smav			     /*sector_count*/ dxfer_len / 512,
709286965Sken			     /*lba*/ 0,
710286965Sken			     /*command*/ ATA_ATA_IDENTIFY,
711300207Sken			     /*auxiliary*/ 0,
712286965Sken			     /*data_ptr*/ (uint8_t *)ptr,
713286965Sken			     /*dxfer_len*/ dxfer_len,
714300207Sken			     /*cdb_storage*/ NULL,
715300207Sken			     /*cdb_storage_len*/ 0,
716286965Sken			     /*sense_len*/ SSD_FULL_SIZE,
717286965Sken			     /*timeout*/ timeout ? timeout : 30 * 1000,
718286965Sken			     /*is48bit*/ 0,
719286965Sken			     /*devtype*/ devtype);
720300207Sken		if (retval != 0) {
721300207Sken			retval = -1;
722300207Sken			warnx("%s: build_ata_cmd() failed, likely "
723300207Sken			    "programmer error", __func__);
724300207Sken			goto bailout;
725300207Sken		}
726286965Sken		break;
727286965Sken	}
728286965Sken	default:
729286965Sken		warnx("Unknown disk type %d", devtype);
730286965Sken		retval = -1;
731286965Sken		goto bailout;
732286965Sken		break; /*NOTREACHED*/
733286965Sken	}
734286965Sken
735286965Sken	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
736286965Sken
737286965Sken	retval = cam_send_ccb(dev, ccb);
738286965Sken	if (retval != 0) {
739286965Sken		warn("error sending %s CCB", (devtype == CC_DT_SCSI) ?
740286965Sken		     "Test Unit Ready" : "Identify");
741286965Sken		retval = -1;
742286965Sken		goto bailout;
743286965Sken	}
744286965Sken
745286965Sken	if (((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
746286965Sken	 && (vp->tur_status == FW_TUR_READY)) {
747286965Sken		warnx("Device is not ready");
748286965Sken		if (printerrors)
749286965Sken			cam_error_print(dev, ccb, CAM_ESF_ALL,
750286965Sken			    CAM_EPF_ALL, stderr);
751286965Sken		retval = 1;
752286965Sken		goto bailout;
753286965Sken	} else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
754286965Sken		&& (vp->tur_status == FW_TUR_NOT_READY)) {
755286965Sken		warnx("Device cannot have media loaded when firmware is "
756286965Sken		    "downloaded");
757286965Sken		retval = 1;
758286965Sken		goto bailout;
759286965Sken	}
760286965Skenbailout:
761318817Sasomers	free(ptr);
762318817Sasomers	cam_freeccb(ccb);
763286965Sken
764286965Sken	return (retval);
765286965Sken}
766286965Sken
767227961Semaste/*
768227961Semaste * Download firmware stored in buf to cam_dev. If simulation mode
769227961Semaste * is enabled, only show what packet sizes would be sent to the
770227961Semaste * device but do not sent any actual packets
771227961Semaste */
772227961Semastestatic int
773286965Skenfw_download_img(struct cam_device *cam_dev, struct fw_vendor *vp,
774286965Sken    char *buf, int img_size, int sim_mode, int printerrors, int quiet,
775286965Sken    int retry_count, int timeout, const char *imgname,
776286965Sken    camcontrol_devtype devtype)
777227961Semaste{
778227961Semaste	struct scsi_write_buffer cdb;
779237281Sscottl	progress_t progress;
780286965Sken	int size = 0;
781286965Sken	union ccb *ccb = NULL;
782227961Semaste	int pkt_count = 0;
783237281Sscottl	int max_pkt_size;
784227961Semaste	u_int32_t pkt_size = 0;
785227961Semaste	char *pkt_ptr = buf;
786227961Semaste	u_int32_t offset;
787227961Semaste	int last_pkt = 0;
788286965Sken	int retval = 0;
789227961Semaste
790286965Sken	/*
791286965Sken	 * Check to see whether the device is ready to accept a firmware
792286965Sken	 * download.
793286965Sken	 */
794286965Sken	retval = fw_check_device_ready(cam_dev, devtype, vp, printerrors,
795286965Sken				       timeout);
796286965Sken	if (retval != 0)
797286965Sken		goto bailout;
798286965Sken
799227961Semaste	if ((ccb = cam_getccb(cam_dev)) == NULL) {
800227961Semaste		warnx("Could not allocate CCB");
801286965Sken		retval = 1;
802286965Sken		goto bailout;
803227961Semaste	}
804237281Sscottl
805300547Struckman	CCB_CLEAR_ALL_EXCEPT_HDR(ccb);
806237281Sscottl
807237281Sscottl	max_pkt_size = vp->max_pkt_size;
808286965Sken	if (max_pkt_size == 0)
809237281Sscottl		max_pkt_size = UNKNOWN_MAX_PKT_SIZE;
810286965Sken
811286965Sken	pkt_size = max_pkt_size;
812237281Sscottl	progress_init(&progress, imgname, size = img_size);
813227961Semaste	/* Download single fw packets. */
814227961Semaste	do {
815237281Sscottl		if (img_size <= max_pkt_size) {
816227961Semaste			last_pkt = 1;
817227961Semaste			pkt_size = img_size;
818227961Semaste		}
819237281Sscottl		progress_update(&progress, size - img_size);
820286965Sken		if (((sim_mode == 0) && (quiet == 0))
821286965Sken		 || ((sim_mode != 0) && (printerrors == 0)))
822286965Sken			progress_draw(&progress);
823227961Semaste		bzero(&cdb, sizeof(cdb));
824286965Sken		switch (devtype) {
825286965Sken		case CC_DT_SCSI:
826237281Sscottl			cdb.opcode  = WRITE_BUFFER;
827237281Sscottl			cdb.control = 0;
828237281Sscottl			/* Parameter list length. */
829237281Sscottl			scsi_ulto3b(pkt_size, &cdb.length[0]);
830237281Sscottl			offset = vp->inc_cdb_offset ? (pkt_ptr - buf) : 0;
831237281Sscottl			scsi_ulto3b(offset, &cdb.offset[0]);
832286965Sken			cdb.byte2 = last_pkt ? vp->cdb_byte2_last :
833286965Sken					       vp->cdb_byte2;
834237281Sscottl			cdb.buffer_id = vp->inc_cdb_buffer_id ? pkt_count : 0;
835237281Sscottl			/* Zero out payload of ccb union after ccb header. */
836300547Struckman			CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
837286965Sken			/*
838286965Sken			 * Copy previously constructed cdb into ccb_scsiio
839286965Sken			 * struct.
840286965Sken			 */
841237281Sscottl			bcopy(&cdb, &ccb->csio.cdb_io.cdb_bytes[0],
842237281Sscottl			    sizeof(struct scsi_write_buffer));
843237281Sscottl			/* Fill rest of ccb_scsiio struct. */
844286965Sken			cam_fill_csio(&ccb->csio,		/* ccb_scsiio*/
845286965Sken			    retry_count,			/* retries*/
846286965Sken			    NULL,				/* cbfcnp*/
847286965Sken			    CAM_DIR_OUT | CAM_DEV_QFRZDIS,	/* flags*/
848286965Sken			    CAM_TAG_ACTION_NONE,		/* tag_action*/
849286965Sken			    (u_char *)pkt_ptr,			/* data_ptr*/
850286965Sken			    pkt_size,				/* dxfer_len*/
851286965Sken			    SSD_FULL_SIZE,			/* sense_len*/
852286965Sken			    sizeof(struct scsi_write_buffer),	/* cdb_len*/
853286965Sken			    timeout ? timeout : WB_TIMEOUT);	/* timeout*/
854286965Sken			break;
855286965Sken		case CC_DT_ATA:
856350796Smav		case CC_DT_SATL: {
857286965Sken			uint32_t	off;
858237281Sscottl
859286965Sken			off = (uint32_t)(pkt_ptr - buf);
860286965Sken
861300207Sken			retval = build_ata_cmd(ccb,
862286965Sken			    /*retry_count*/ retry_count,
863286965Sken			    /*flags*/ CAM_DIR_OUT | CAM_DEV_QFRZDIS,
864286965Sken			    /*tag_action*/ CAM_TAG_ACTION_NONE,
865286965Sken			    /*protocol*/ AP_PROTO_PIO_OUT,
866286965Sken			    /*ata_flags*/ AP_FLAG_BYT_BLOK_BYTES |
867286965Sken					  AP_FLAG_TLEN_SECT_CNT |
868286965Sken					  AP_FLAG_TDIR_TO_DEV,
869286965Sken			    /*features*/ USE_OFFSETS_FEATURE,
870286965Sken			    /*sector_count*/ ATA_MAKE_SECTORS(pkt_size),
871286965Sken			    /*lba*/ ATA_MAKE_LBA(off, pkt_size),
872286965Sken			    /*command*/ ATA_DOWNLOAD_MICROCODE,
873300207Sken			    /*auxiliary*/ 0,
874286965Sken			    /*data_ptr*/ (uint8_t *)pkt_ptr,
875286965Sken			    /*dxfer_len*/ pkt_size,
876300207Sken			    /*cdb_storage*/ NULL,
877300207Sken			    /*cdb_storage_len*/ 0,
878286965Sken			    /*sense_len*/ SSD_FULL_SIZE,
879286965Sken			    /*timeout*/ timeout ? timeout : WB_TIMEOUT,
880286965Sken			    /*is48bit*/ 0,
881286965Sken			    /*devtype*/ devtype);
882300207Sken
883300207Sken			if (retval != 0) {
884300207Sken				warnx("%s: build_ata_cmd() failed, likely "
885300207Sken				    "programmer error", __func__);
886300207Sken				goto bailout;
887300207Sken			}
888286965Sken			break;
889237281Sscottl		}
890286965Sken		default:
891286965Sken			warnx("Unknown device type %d", devtype);
892286965Sken			retval = 1;
893286965Sken			goto bailout;
894286965Sken			break; /*NOTREACHED*/
895286965Sken		}
896227961Semaste		if (!sim_mode) {
897227961Semaste			/* Execute the command. */
898251743Smav			if (cam_send_ccb(cam_dev, ccb) < 0 ||
899251743Smav			    (ccb->ccb_h.status & CAM_STATUS_MASK) !=
900251743Smav			    CAM_REQ_CMP) {
901227961Semaste				warnx("Error writing image to device");
902241737Sed				if (printerrors)
903286965Sken					cam_error_print(cam_dev, ccb,
904286965Sken					    CAM_ESF_ALL, CAM_EPF_ALL, stderr);
905286965Sken				retval = 1;
906227961Semaste				goto bailout;
907227961Semaste			}
908286965Sken		} else if (printerrors) {
909286965Sken			cam_error_print(cam_dev, ccb, CAM_ESF_COMMAND, 0,
910286965Sken			    stdout);
911227961Semaste		}
912286965Sken
913227961Semaste		/* Prepare next round. */
914227961Semaste		pkt_count++;
915227961Semaste		pkt_ptr += pkt_size;
916227961Semaste		img_size -= pkt_size;
917227961Semaste	} while(!last_pkt);
918227961Semastebailout:
919286965Sken	if (quiet == 0)
920286965Sken		progress_complete(&progress, size - img_size);
921318817Sasomers	cam_freeccb(ccb);
922286965Sken	return (retval);
923227961Semaste}
924227961Semaste
925227961Semasteint
926227961Semastefwdownload(struct cam_device *device, int argc, char **argv,
927314220Sken    char *combinedopt, int printerrors, int task_attr, int retry_count,
928314220Sken    int timeout)
929227961Semaste{
930318817Sasomers	union ccb *ccb = NULL;
931286965Sken	struct fw_vendor *vp;
932227961Semaste	char *fw_img_path = NULL;
933286965Sken	struct ata_params *ident_buf = NULL;
934286965Sken	camcontrol_devtype devtype;
935286965Sken	char *buf = NULL;
936227961Semaste	int img_size;
937227961Semaste	int c;
938227961Semaste	int sim_mode = 0;
939227961Semaste	int confirmed = 0;
940286965Sken	int quiet = 0;
941286965Sken	int retval = 0;
942227961Semaste
943227961Semaste	while ((c = getopt(argc, argv, combinedopt)) != -1) {
944227961Semaste		switch (c) {
945286965Sken		case 'f':
946286965Sken			fw_img_path = optarg;
947286965Sken			break;
948286965Sken		case 'q':
949286965Sken			quiet = 1;
950286965Sken			break;
951227961Semaste		case 's':
952227961Semaste			sim_mode = 1;
953227961Semaste			break;
954227961Semaste		case 'y':
955227961Semaste			confirmed = 1;
956227961Semaste			break;
957227961Semaste		default:
958227961Semaste			break;
959227961Semaste		}
960227961Semaste	}
961227961Semaste
962227961Semaste	if (fw_img_path == NULL)
963286965Sken		errx(1, "you must specify a firmware image file using -f "
964286965Sken		     "option");
965227961Semaste
966286965Sken	retval = get_device_type(device, retry_count, timeout, printerrors,
967286965Sken				 &devtype);
968286965Sken	if (retval != 0)
969286965Sken		errx(1, "Unable to determine device type");
970227961Semaste
971286965Sken	if ((devtype == CC_DT_ATA)
972350796Smav	 || (devtype == CC_DT_SATL)) {
973286965Sken		ccb = cam_getccb(device);
974286965Sken		if (ccb == NULL) {
975286965Sken			warnx("couldn't allocate CCB");
976286965Sken			retval = 1;
977286965Sken			goto bailout;
978286965Sken		}
979286965Sken
980286965Sken		if (ata_do_identify(device, retry_count, timeout, ccb,
981286965Sken		    		    &ident_buf) != 0) {
982286965Sken			retval = 1;
983286965Sken			goto bailout;
984286965Sken		}
985286965Sken	} else if (devtype != CC_DT_SCSI)
986286965Sken		errx(1, "Unsupported device type %d", devtype);
987286965Sken
988286965Sken	vp = fw_get_vendor(device, ident_buf);
989286965Sken	/*
990286965Sken	 * Bail out if we have an unknown vendor and this isn't an ATA
991286965Sken	 * disk.  For a SCSI disk, we have no chance of working properly
992286965Sken	 * with the default values in the VENDOR_UNKNOWN case.  For an ATA
993286965Sken	 * disk connected via an ATA transport, we may work for drives that
994286965Sken	 * support the ATA_DOWNLOAD_MICROCODE command.
995286965Sken	 */
996286965Sken	if (((vp == NULL)
997286965Sken	  || (vp->type == VENDOR_UNKNOWN))
998286965Sken	 && (devtype == CC_DT_SCSI))
999286965Sken		errx(1, "Unsupported device");
1000286965Sken
1001314220Sken	retval = fw_get_timeout(device, vp, task_attr, retry_count, timeout);
1002286965Sken	if (retval != 0) {
1003286965Sken		warnx("Unable to get a firmware download timeout value");
1004286965Sken		goto bailout;
1005286965Sken	}
1006286965Sken
1007286965Sken	buf = fw_read_img(device, retry_count, timeout, quiet, fw_img_path,
1008286965Sken	    vp, &img_size);
1009286965Sken	if (buf == NULL) {
1010286965Sken		retval = 1;
1011286965Sken		goto bailout;
1012286965Sken	}
1013286965Sken
1014227961Semaste	if (!confirmed) {
1015227961Semaste		fprintf(stdout, "You are about to download firmware image (%s)"
1016227961Semaste		    " into the following device:\n",
1017227961Semaste		    fw_img_path);
1018286965Sken		if (devtype == CC_DT_SCSI) {
1019314220Sken			if (scsidoinquiry(device, argc, argv, combinedopt,
1020314220Sken					  MSG_SIMPLE_Q_TAG, 0, 5000) != 0) {
1021286965Sken				warnx("Error sending inquiry");
1022286965Sken				retval = 1;
1023286965Sken				goto bailout;
1024286965Sken			}
1025286965Sken		} else {
1026286965Sken			printf("%s%d: ", device->device_name,
1027286965Sken			    device->dev_unit_num);
1028286965Sken			ata_print_ident(ident_buf);
1029286965Sken			camxferrate(device);
1030286965Sken			free(ident_buf);
1031286965Sken		}
1032286965Sken		fprintf(stdout, "Using a timeout of %u ms, which is %s.\n",
1033286965Sken			vp->timeout_ms,
1034286965Sken			fw_timeout_desc_table[vp->timeout_type].timeout_desc);
1035227961Semaste		fprintf(stdout, "\nIt may damage your drive. ");
1036286965Sken		if (!get_confirmation()) {
1037286965Sken			retval = 1;
1038286965Sken			goto bailout;
1039286965Sken		}
1040227961Semaste	}
1041286965Sken	if ((sim_mode != 0) && (quiet == 0))
1042227961Semaste		fprintf(stdout, "Running in simulation mode\n");
1043227961Semaste
1044241737Sed	if (fw_download_img(device, vp, buf, img_size, sim_mode, printerrors,
1045286965Sken	    quiet, retry_count, vp->timeout_ms, fw_img_path, devtype) != 0) {
1046227961Semaste		fprintf(stderr, "Firmware download failed\n");
1047286965Sken		retval = 1;
1048286965Sken		goto bailout;
1049286965Sken	} else if (quiet == 0)
1050227961Semaste		fprintf(stdout, "Firmware download successful\n");
1051227961Semaste
1052286965Skenbailout:
1053318817Sasomers	cam_freeccb(ccb);
1054227961Semaste	free(buf);
1055286965Sken	return (retval);
1056227961Semaste}
1057227961Semaste
1058