fwdownload.c revision 318817
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: stable/11/sbin/camcontrol/fwdownload.c 318817 2017-05-24 21:18:13Z asomers $"); 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, 266314220Sken int task_attr, 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, 320314220Sken int task_attr, 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, 352314220Sken /*task_attr*/ task_attr, 353286965Sken /*retry_count*/ retry_count, 354286965Sken /*timeout*/ 10000, 355286965Sken /*verbose*/ 0, 356286965Sken /*fill_len*/ &fill_len, 357286965Sken /*data_ptr*/ &buf); 358286965Sken /* 359286965Sken * It isn't an error if we can't get a timeout descriptor. We just 360286965Sken * continue on with the default timeout. 361286965Sken */ 362286965Sken if (retval != 0) { 363286965Sken retval = 0; 364286965Sken goto bailout; 365286965Sken } 366286965Sken 367286965Sken /* 368286965Sken * Even if the drive didn't return a SCSI error, if we don't have 369286965Sken * enough data to contain the one opcode descriptor, the CDB 370286965Sken * structure and a timeout descriptor, we don't have the timeout 371286965Sken * value we're looking for. So we'll just fall back to the 372286965Sken * default value. 373286965Sken */ 374286965Sken if (fill_len < (sizeof(*one) + sizeof(struct scsi_write_buffer) + 375286965Sken sizeof(*td))) 376286965Sken goto bailout; 377286965Sken 378286965Sken one = (struct scsi_report_supported_opcodes_one *)buf; 379286965Sken 380286965Sken /* 381286965Sken * If the drive claims to not support the WRITE BUFFER command... 382286965Sken * fall back to the default timeout value and let things fail on 383286965Sken * the actual firmware download. 384286965Sken */ 385286965Sken if ((one->support & RSO_ONE_SUP_MASK) == RSO_ONE_SUP_NOT_SUP) 386286965Sken goto bailout; 387286965Sken 388286965Sken cdb_len = scsi_2btoul(one->cdb_length); 389286965Sken td = (struct scsi_report_supported_opcodes_timeout *) 390286965Sken &buf[sizeof(*one) + cdb_len]; 391286965Sken 392286965Sken rec_timeout = scsi_4btoul(td->recommended_time); 393286965Sken /* 394286965Sken * If the recommended timeout is 0, then the device has probably 395286965Sken * returned a bogus value. 396286965Sken */ 397286965Sken if (rec_timeout == 0) 398286965Sken goto bailout; 399286965Sken 400286965Sken /* CAM timeouts are in ms */ 401286965Sken rec_timeout *= 1000; 402286965Sken 403286965Sken vp->timeout_ms = rec_timeout; 404286965Sken vp->timeout_type = FW_TIMEOUT_DEV_REPORTED; 405286965Sken 406286965Skenbailout: 407286965Sken return (retval); 408286965Sken} 409286965Sken 410286965Sken#define SVPD_IBM_FW_DESIGNATION 0x03 411286965Sken 412227961Semaste/* 413286965Sken * IBM LTO and TS tape drives have an INQUIRY VPD page 0x3 with the following 414286965Sken * format: 415286965Sken */ 416286965Skenstruct fw_ibm_tape_fw_designation { 417286965Sken uint8_t device; 418286965Sken uint8_t page_code; 419286965Sken uint8_t reserved; 420286965Sken uint8_t length; 421286965Sken uint8_t ascii_length; 422286965Sken uint8_t reserved2[3]; 423286965Sken uint8_t load_id[4]; 424286965Sken uint8_t fw_rev[4]; 425286965Sken uint8_t ptf_number[4]; 426286965Sken uint8_t patch_number[4]; 427286965Sken uint8_t ru_name[8]; 428286965Sken uint8_t lib_seq_num[5]; 429286965Sken}; 430286965Sken 431286965Sken/* 432286965Sken * The firmware for IBM tape drives has the following header format. The 433286965Sken * load_id and ru_name in the header file should match what is returned in 434286965Sken * VPD page 0x3. 435286965Sken */ 436286965Skenstruct fw_ibm_tape_fw_header { 437286965Sken uint8_t unspec[4]; 438286965Sken uint8_t length[4]; /* Firmware and header! */ 439286965Sken uint8_t load_id[4]; 440286965Sken uint8_t fw_rev[4]; 441286965Sken uint8_t reserved[8]; 442286965Sken uint8_t ru_name[8]; 443286965Sken}; 444286965Sken 445286965Skenstatic int 446286965Skenfw_validate_ibm(struct cam_device *dev, int retry_count, int timeout, int fd, 447286965Sken char *buf, const char *fw_img_path, int quiet) 448286965Sken{ 449286965Sken union ccb *ccb; 450286965Sken struct fw_ibm_tape_fw_designation vpd_page; 451286965Sken struct fw_ibm_tape_fw_header *header; 452286965Sken char drive_rev[sizeof(vpd_page.fw_rev) + 1]; 453286965Sken char file_rev[sizeof(vpd_page.fw_rev) + 1]; 454286965Sken int retval = 1; 455286965Sken 456286965Sken ccb = cam_getccb(dev); 457286965Sken if (ccb == NULL) { 458286965Sken warnx("couldn't allocate CCB"); 459286965Sken goto bailout; 460286965Sken } 461286965Sken 462286965Sken /* cam_getccb cleans up the header, caller has to zero the payload */ 463300547Struckman CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio); 464286965Sken 465286965Sken bzero(&vpd_page, sizeof(vpd_page)); 466286965Sken 467286965Sken scsi_inquiry(&ccb->csio, 468286965Sken /*retries*/ retry_count, 469286965Sken /*cbfcnp*/ NULL, 470286965Sken /* tag_action */ MSG_SIMPLE_Q_TAG, 471286965Sken /* inq_buf */ (u_int8_t *)&vpd_page, 472286965Sken /* inq_len */ sizeof(vpd_page), 473286965Sken /* evpd */ 1, 474286965Sken /* page_code */ SVPD_IBM_FW_DESIGNATION, 475286965Sken /* sense_len */ SSD_FULL_SIZE, 476286965Sken /* timeout */ timeout ? timeout : 5000); 477286965Sken 478286965Sken /* Disable freezing the device queue */ 479286965Sken ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 480286965Sken 481286965Sken if (retry_count != 0) 482286965Sken ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; 483286965Sken 484286965Sken if (cam_send_ccb(dev, ccb) < 0) { 485286965Sken warn("error getting firmware designation page"); 486286965Sken 487286965Sken cam_error_print(dev, ccb, CAM_ESF_ALL, 488286965Sken CAM_EPF_ALL, stderr); 489286965Sken 490286965Sken cam_freeccb(ccb); 491299490Scem ccb = NULL; 492286965Sken goto bailout; 493286965Sken } 494286965Sken 495286965Sken if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 496286965Sken cam_error_print(dev, ccb, CAM_ESF_ALL, 497286965Sken CAM_EPF_ALL, stderr); 498286965Sken goto bailout; 499286965Sken } 500286965Sken 501286965Sken /* 502286965Sken * Read the firmware header only. 503286965Sken */ 504286965Sken if (read(fd, buf, sizeof(*header)) != sizeof(*header)) { 505286965Sken warn("unable to read %zu bytes from %s", sizeof(*header), 506286965Sken fw_img_path); 507286965Sken goto bailout; 508286965Sken } 509286965Sken 510286965Sken /* Rewind the file back to 0 for the full file read. */ 511286965Sken if (lseek(fd, 0, SEEK_SET) == -1) { 512286965Sken warn("Unable to lseek"); 513286965Sken goto bailout; 514286965Sken } 515286965Sken 516286965Sken header = (struct fw_ibm_tape_fw_header *)buf; 517286965Sken 518286965Sken bzero(drive_rev, sizeof(drive_rev)); 519286965Sken bcopy(vpd_page.fw_rev, drive_rev, sizeof(vpd_page.fw_rev)); 520286965Sken bzero(file_rev, sizeof(file_rev)); 521286965Sken bcopy(header->fw_rev, file_rev, sizeof(header->fw_rev)); 522286965Sken 523286965Sken if (quiet == 0) { 524286965Sken fprintf(stdout, "Current Drive Firmware version: %s\n", 525286965Sken drive_rev); 526286965Sken fprintf(stdout, "Firmware File version: %s\n", file_rev); 527286965Sken } 528286965Sken 529286965Sken /* 530286965Sken * For IBM tape drives the load ID and RU name reported by the 531286965Sken * drive should match what is in the firmware file. 532286965Sken */ 533286965Sken if (bcmp(vpd_page.load_id, header->load_id, 534286965Sken MIN(sizeof(vpd_page.load_id), sizeof(header->load_id))) != 0) { 535286965Sken warnx("Drive Firmware load ID 0x%x does not match firmware " 536286965Sken "file load ID 0x%x", scsi_4btoul(vpd_page.load_id), 537286965Sken scsi_4btoul(header->load_id)); 538286965Sken goto bailout; 539286965Sken } 540286965Sken 541286965Sken if (bcmp(vpd_page.ru_name, header->ru_name, 542286965Sken MIN(sizeof(vpd_page.ru_name), sizeof(header->ru_name))) != 0) { 543286965Sken warnx("Drive Firmware RU name 0x%jx does not match firmware " 544286965Sken "file RU name 0x%jx", 545286965Sken (uintmax_t)scsi_8btou64(vpd_page.ru_name), 546286965Sken (uintmax_t)scsi_8btou64(header->ru_name)); 547286965Sken goto bailout; 548286965Sken } 549286965Sken if (quiet == 0) 550286965Sken fprintf(stdout, "Firmware file is valid for this drive.\n"); 551286965Sken retval = 0; 552286965Skenbailout: 553318817Sasomers 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: 755318817Sasomers free(ptr); 756318817Sasomers 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); 915318817Sasomers cam_freeccb(ccb); 916286965Sken return (retval); 917227961Semaste} 918227961Semaste 919227961Semasteint 920227961Semastefwdownload(struct cam_device *device, int argc, char **argv, 921314220Sken char *combinedopt, int printerrors, int task_attr, int retry_count, 922314220Sken int timeout) 923227961Semaste{ 924318817Sasomers union ccb *ccb = NULL; 925286965Sken struct fw_vendor *vp; 926227961Semaste char *fw_img_path = NULL; 927286965Sken struct ata_params *ident_buf = NULL; 928286965Sken camcontrol_devtype devtype; 929286965Sken char *buf = NULL; 930227961Semaste int img_size; 931227961Semaste int c; 932227961Semaste int sim_mode = 0; 933227961Semaste int confirmed = 0; 934286965Sken int quiet = 0; 935286965Sken int retval = 0; 936227961Semaste 937227961Semaste while ((c = getopt(argc, argv, combinedopt)) != -1) { 938227961Semaste switch (c) { 939286965Sken case 'f': 940286965Sken fw_img_path = optarg; 941286965Sken break; 942286965Sken case 'q': 943286965Sken quiet = 1; 944286965Sken break; 945227961Semaste case 's': 946227961Semaste sim_mode = 1; 947227961Semaste break; 948227961Semaste case 'y': 949227961Semaste confirmed = 1; 950227961Semaste break; 951227961Semaste default: 952227961Semaste break; 953227961Semaste } 954227961Semaste } 955227961Semaste 956227961Semaste if (fw_img_path == NULL) 957286965Sken errx(1, "you must specify a firmware image file using -f " 958286965Sken "option"); 959227961Semaste 960286965Sken retval = get_device_type(device, retry_count, timeout, printerrors, 961286965Sken &devtype); 962286965Sken if (retval != 0) 963286965Sken errx(1, "Unable to determine device type"); 964227961Semaste 965286965Sken if ((devtype == CC_DT_ATA) 966286965Sken || (devtype == CC_DT_ATA_BEHIND_SCSI)) { 967286965Sken ccb = cam_getccb(device); 968286965Sken if (ccb == NULL) { 969286965Sken warnx("couldn't allocate CCB"); 970286965Sken retval = 1; 971286965Sken goto bailout; 972286965Sken } 973286965Sken 974286965Sken if (ata_do_identify(device, retry_count, timeout, ccb, 975286965Sken &ident_buf) != 0) { 976286965Sken retval = 1; 977286965Sken goto bailout; 978286965Sken } 979286965Sken } else if (devtype != CC_DT_SCSI) 980286965Sken errx(1, "Unsupported device type %d", devtype); 981286965Sken 982286965Sken vp = fw_get_vendor(device, ident_buf); 983286965Sken /* 984286965Sken * Bail out if we have an unknown vendor and this isn't an ATA 985286965Sken * disk. For a SCSI disk, we have no chance of working properly 986286965Sken * with the default values in the VENDOR_UNKNOWN case. For an ATA 987286965Sken * disk connected via an ATA transport, we may work for drives that 988286965Sken * support the ATA_DOWNLOAD_MICROCODE command. 989286965Sken */ 990286965Sken if (((vp == NULL) 991286965Sken || (vp->type == VENDOR_UNKNOWN)) 992286965Sken && (devtype == CC_DT_SCSI)) 993286965Sken errx(1, "Unsupported device"); 994286965Sken 995314220Sken retval = fw_get_timeout(device, vp, task_attr, retry_count, timeout); 996286965Sken if (retval != 0) { 997286965Sken warnx("Unable to get a firmware download timeout value"); 998286965Sken goto bailout; 999286965Sken } 1000286965Sken 1001286965Sken buf = fw_read_img(device, retry_count, timeout, quiet, fw_img_path, 1002286965Sken vp, &img_size); 1003286965Sken if (buf == NULL) { 1004286965Sken retval = 1; 1005286965Sken goto bailout; 1006286965Sken } 1007286965Sken 1008227961Semaste if (!confirmed) { 1009227961Semaste fprintf(stdout, "You are about to download firmware image (%s)" 1010227961Semaste " into the following device:\n", 1011227961Semaste fw_img_path); 1012286965Sken if (devtype == CC_DT_SCSI) { 1013314220Sken if (scsidoinquiry(device, argc, argv, combinedopt, 1014314220Sken MSG_SIMPLE_Q_TAG, 0, 5000) != 0) { 1015286965Sken warnx("Error sending inquiry"); 1016286965Sken retval = 1; 1017286965Sken goto bailout; 1018286965Sken } 1019286965Sken } else { 1020286965Sken printf("%s%d: ", device->device_name, 1021286965Sken device->dev_unit_num); 1022286965Sken ata_print_ident(ident_buf); 1023286965Sken camxferrate(device); 1024286965Sken free(ident_buf); 1025286965Sken } 1026286965Sken fprintf(stdout, "Using a timeout of %u ms, which is %s.\n", 1027286965Sken vp->timeout_ms, 1028286965Sken fw_timeout_desc_table[vp->timeout_type].timeout_desc); 1029227961Semaste fprintf(stdout, "\nIt may damage your drive. "); 1030286965Sken if (!get_confirmation()) { 1031286965Sken retval = 1; 1032286965Sken goto bailout; 1033286965Sken } 1034227961Semaste } 1035286965Sken if ((sim_mode != 0) && (quiet == 0)) 1036227961Semaste fprintf(stdout, "Running in simulation mode\n"); 1037227961Semaste 1038241737Sed if (fw_download_img(device, vp, buf, img_size, sim_mode, printerrors, 1039286965Sken quiet, retry_count, vp->timeout_ms, fw_img_path, devtype) != 0) { 1040227961Semaste fprintf(stderr, "Firmware download failed\n"); 1041286965Sken retval = 1; 1042286965Sken goto bailout; 1043286965Sken } else if (quiet == 0) 1044227961Semaste fprintf(stdout, "Firmware download successful\n"); 1045227961Semaste 1046286965Skenbailout: 1047318817Sasomers cam_freeccb(ccb); 1048227961Semaste free(buf); 1049286965Sken return (retval); 1050227961Semaste} 1051227961Semaste 1052