fwdownload.c revision 314220
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 314220 2017-02-24 20:47:56Z ken $"); 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: 553299490Scem if (ccb != NULL) 554299490Scem cam_freeccb(ccb); 555286965Sken 556286965Sken return (retval); 557286965Sken} 558286965Sken 559286965Sken/* 560227961Semaste * Allocate a buffer and read fw image file into it 561227961Semaste * from given path. Number of bytes read is stored 562227961Semaste * in num_bytes. 563227961Semaste */ 564227961Semastestatic char * 565286965Skenfw_read_img(struct cam_device *dev, int retry_count, int timeout, int quiet, 566286965Sken const char *fw_img_path, struct fw_vendor *vp, int *num_bytes) 567227961Semaste{ 568227961Semaste int fd; 569227961Semaste struct stat stbuf; 570227961Semaste char *buf; 571227961Semaste off_t img_size; 572227961Semaste int skip_bytes = 0; 573227961Semaste 574227961Semaste if ((fd = open(fw_img_path, O_RDONLY)) < 0) { 575227961Semaste warn("Could not open image file %s", fw_img_path); 576227961Semaste return (NULL); 577227961Semaste } 578227961Semaste if (fstat(fd, &stbuf) < 0) { 579227961Semaste warn("Could not stat image file %s", fw_img_path); 580227961Semaste goto bailout1; 581227961Semaste } 582227961Semaste if ((img_size = stbuf.st_size) == 0) { 583227961Semaste warnx("Zero length image file %s", fw_img_path); 584227961Semaste goto bailout1; 585227961Semaste } 586227961Semaste if ((buf = malloc(img_size)) == NULL) { 587227961Semaste warnx("Could not allocate buffer to read image file %s", 588227961Semaste fw_img_path); 589227961Semaste goto bailout1; 590227961Semaste } 591227961Semaste /* Skip headers if applicable. */ 592227961Semaste switch (vp->type) { 593227961Semaste case VENDOR_SEAGATE: 594227961Semaste if (read(fd, buf, 16) != 16) { 595227961Semaste warn("Could not read image file %s", fw_img_path); 596227961Semaste goto bailout; 597227961Semaste } 598227961Semaste if (lseek(fd, 0, SEEK_SET) == -1) { 599227961Semaste warn("Unable to lseek"); 600227961Semaste goto bailout; 601227961Semaste } 602227961Semaste if ((strncmp(buf, "SEAGATE,SEAGATE ", 16) == 0) || 603227961Semaste (img_size % 512 == 80)) 604227961Semaste skip_bytes = 80; 605227961Semaste break; 606237281Sscottl case VENDOR_QUALSTAR: 607237281Sscottl skip_bytes = img_size % 1030; 608237281Sscottl break; 609286965Sken case VENDOR_IBM: { 610286965Sken if (vp->dev_type != T_SEQUENTIAL) 611286965Sken break; 612286965Sken if (fw_validate_ibm(dev, retry_count, timeout, fd, buf, 613286965Sken fw_img_path, quiet) != 0) 614286965Sken goto bailout; 615286965Sken break; 616286965Sken } 617227961Semaste default: 618227961Semaste break; 619227961Semaste } 620227961Semaste if (skip_bytes != 0) { 621227961Semaste fprintf(stdout, "Skipping %d byte header.\n", skip_bytes); 622227961Semaste if (lseek(fd, skip_bytes, SEEK_SET) == -1) { 623227961Semaste warn("Could not lseek"); 624227961Semaste goto bailout; 625227961Semaste } 626227961Semaste img_size -= skip_bytes; 627227961Semaste } 628227961Semaste /* Read image into a buffer. */ 629227961Semaste if (read(fd, buf, img_size) != img_size) { 630227961Semaste warn("Could not read image file %s", fw_img_path); 631227961Semaste goto bailout; 632227961Semaste } 633227961Semaste *num_bytes = img_size; 634256113Semaste close(fd); 635227961Semaste return (buf); 636227961Semastebailout: 637227961Semaste free(buf); 638227961Semastebailout1: 639227961Semaste close(fd); 640227961Semaste *num_bytes = 0; 641227961Semaste return (NULL); 642227961Semaste} 643227961Semaste 644286965Sken/* 645286965Sken * Returns 0 for "success", where success means that the device has met the 646286965Sken * requirement in the vendor structure for being ready or not ready when 647286965Sken * firmware is downloaded. 648286965Sken * 649286965Sken * Returns 1 for a failure to be ready to accept a firmware download. 650286965Sken * (e.g., a drive needs to be ready, but returns not ready) 651286965Sken * 652286965Sken * Returns -1 for any other failure. 653286965Sken */ 654286965Skenstatic int 655286965Skenfw_check_device_ready(struct cam_device *dev, camcontrol_devtype devtype, 656286965Sken struct fw_vendor *vp, int printerrors, int timeout) 657286965Sken{ 658286965Sken union ccb *ccb; 659286965Sken int retval = 0; 660286965Sken int16_t *ptr = NULL; 661286965Sken size_t dxfer_len = 0; 662286965Sken 663286965Sken if ((ccb = cam_getccb(dev)) == NULL) { 664286965Sken warnx("Could not allocate CCB"); 665286965Sken retval = -1; 666286965Sken goto bailout; 667286965Sken } 668286965Sken 669300547Struckman CCB_CLEAR_ALL_EXCEPT_HDR(ccb); 670286965Sken 671286965Sken if (devtype != CC_DT_SCSI) { 672286965Sken dxfer_len = sizeof(struct ata_params); 673286965Sken 674286965Sken ptr = (uint16_t *)malloc(dxfer_len); 675286965Sken if (ptr == NULL) { 676286965Sken warnx("can't malloc memory for identify"); 677286965Sken retval = -1; 678286965Sken goto bailout; 679286965Sken } 680286965Sken bzero(ptr, dxfer_len); 681286965Sken } 682286965Sken 683286965Sken switch (devtype) { 684286965Sken case CC_DT_SCSI: 685286965Sken scsi_test_unit_ready(&ccb->csio, 686286965Sken /*retries*/ 0, 687286965Sken /*cbfcnp*/ NULL, 688286965Sken /*tag_action*/ MSG_SIMPLE_Q_TAG, 689286965Sken /*sense_len*/ SSD_FULL_SIZE, 690286965Sken /*timeout*/ 5000); 691286965Sken break; 692286965Sken case CC_DT_ATA_BEHIND_SCSI: 693286965Sken case CC_DT_ATA: { 694300207Sken retval = build_ata_cmd(ccb, 695286965Sken /*retries*/ 1, 696286965Sken /*flags*/ CAM_DIR_IN, 697286965Sken /*tag_action*/ MSG_SIMPLE_Q_TAG, 698286965Sken /*protocol*/ AP_PROTO_PIO_IN, 699286965Sken /*ata_flags*/ AP_FLAG_BYT_BLOK_BYTES | 700286965Sken AP_FLAG_TLEN_SECT_CNT | 701286965Sken AP_FLAG_TDIR_FROM_DEV, 702286965Sken /*features*/ 0, 703286965Sken /*sector_count*/ (uint8_t) dxfer_len, 704286965Sken /*lba*/ 0, 705286965Sken /*command*/ ATA_ATA_IDENTIFY, 706300207Sken /*auxiliary*/ 0, 707286965Sken /*data_ptr*/ (uint8_t *)ptr, 708286965Sken /*dxfer_len*/ dxfer_len, 709300207Sken /*cdb_storage*/ NULL, 710300207Sken /*cdb_storage_len*/ 0, 711286965Sken /*sense_len*/ SSD_FULL_SIZE, 712286965Sken /*timeout*/ timeout ? timeout : 30 * 1000, 713286965Sken /*is48bit*/ 0, 714286965Sken /*devtype*/ devtype); 715300207Sken if (retval != 0) { 716300207Sken retval = -1; 717300207Sken warnx("%s: build_ata_cmd() failed, likely " 718300207Sken "programmer error", __func__); 719300207Sken goto bailout; 720300207Sken } 721286965Sken break; 722286965Sken } 723286965Sken default: 724286965Sken warnx("Unknown disk type %d", devtype); 725286965Sken retval = -1; 726286965Sken goto bailout; 727286965Sken break; /*NOTREACHED*/ 728286965Sken } 729286965Sken 730286965Sken ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 731286965Sken 732286965Sken retval = cam_send_ccb(dev, ccb); 733286965Sken if (retval != 0) { 734286965Sken warn("error sending %s CCB", (devtype == CC_DT_SCSI) ? 735286965Sken "Test Unit Ready" : "Identify"); 736286965Sken retval = -1; 737286965Sken goto bailout; 738286965Sken } 739286965Sken 740286965Sken if (((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 741286965Sken && (vp->tur_status == FW_TUR_READY)) { 742286965Sken warnx("Device is not ready"); 743286965Sken if (printerrors) 744286965Sken cam_error_print(dev, ccb, CAM_ESF_ALL, 745286965Sken CAM_EPF_ALL, stderr); 746286965Sken retval = 1; 747286965Sken goto bailout; 748286965Sken } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) 749286965Sken && (vp->tur_status == FW_TUR_NOT_READY)) { 750286965Sken warnx("Device cannot have media loaded when firmware is " 751286965Sken "downloaded"); 752286965Sken retval = 1; 753286965Sken goto bailout; 754286965Sken } 755286965Skenbailout: 756286965Sken if (ccb != NULL) 757286965Sken cam_freeccb(ccb); 758286965Sken 759286965Sken return (retval); 760286965Sken} 761286965Sken 762227961Semaste/* 763227961Semaste * Download firmware stored in buf to cam_dev. If simulation mode 764227961Semaste * is enabled, only show what packet sizes would be sent to the 765227961Semaste * device but do not sent any actual packets 766227961Semaste */ 767227961Semastestatic int 768286965Skenfw_download_img(struct cam_device *cam_dev, struct fw_vendor *vp, 769286965Sken char *buf, int img_size, int sim_mode, int printerrors, int quiet, 770286965Sken int retry_count, int timeout, const char *imgname, 771286965Sken camcontrol_devtype devtype) 772227961Semaste{ 773227961Semaste struct scsi_write_buffer cdb; 774237281Sscottl progress_t progress; 775286965Sken int size = 0; 776286965Sken union ccb *ccb = NULL; 777227961Semaste int pkt_count = 0; 778237281Sscottl int max_pkt_size; 779227961Semaste u_int32_t pkt_size = 0; 780227961Semaste char *pkt_ptr = buf; 781227961Semaste u_int32_t offset; 782227961Semaste int last_pkt = 0; 783286965Sken int retval = 0; 784227961Semaste 785286965Sken /* 786286965Sken * Check to see whether the device is ready to accept a firmware 787286965Sken * download. 788286965Sken */ 789286965Sken retval = fw_check_device_ready(cam_dev, devtype, vp, printerrors, 790286965Sken timeout); 791286965Sken if (retval != 0) 792286965Sken goto bailout; 793286965Sken 794227961Semaste if ((ccb = cam_getccb(cam_dev)) == NULL) { 795227961Semaste warnx("Could not allocate CCB"); 796286965Sken retval = 1; 797286965Sken goto bailout; 798227961Semaste } 799237281Sscottl 800300547Struckman CCB_CLEAR_ALL_EXCEPT_HDR(ccb); 801237281Sscottl 802237281Sscottl max_pkt_size = vp->max_pkt_size; 803286965Sken if (max_pkt_size == 0) 804237281Sscottl max_pkt_size = UNKNOWN_MAX_PKT_SIZE; 805286965Sken 806286965Sken pkt_size = max_pkt_size; 807237281Sscottl progress_init(&progress, imgname, size = img_size); 808227961Semaste /* Download single fw packets. */ 809227961Semaste do { 810237281Sscottl if (img_size <= max_pkt_size) { 811227961Semaste last_pkt = 1; 812227961Semaste pkt_size = img_size; 813227961Semaste } 814237281Sscottl progress_update(&progress, size - img_size); 815286965Sken if (((sim_mode == 0) && (quiet == 0)) 816286965Sken || ((sim_mode != 0) && (printerrors == 0))) 817286965Sken progress_draw(&progress); 818227961Semaste bzero(&cdb, sizeof(cdb)); 819286965Sken switch (devtype) { 820286965Sken case CC_DT_SCSI: 821237281Sscottl cdb.opcode = WRITE_BUFFER; 822237281Sscottl cdb.control = 0; 823237281Sscottl /* Parameter list length. */ 824237281Sscottl scsi_ulto3b(pkt_size, &cdb.length[0]); 825237281Sscottl offset = vp->inc_cdb_offset ? (pkt_ptr - buf) : 0; 826237281Sscottl scsi_ulto3b(offset, &cdb.offset[0]); 827286965Sken cdb.byte2 = last_pkt ? vp->cdb_byte2_last : 828286965Sken vp->cdb_byte2; 829237281Sscottl cdb.buffer_id = vp->inc_cdb_buffer_id ? pkt_count : 0; 830237281Sscottl /* Zero out payload of ccb union after ccb header. */ 831300547Struckman CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio); 832286965Sken /* 833286965Sken * Copy previously constructed cdb into ccb_scsiio 834286965Sken * struct. 835286965Sken */ 836237281Sscottl bcopy(&cdb, &ccb->csio.cdb_io.cdb_bytes[0], 837237281Sscottl sizeof(struct scsi_write_buffer)); 838237281Sscottl /* Fill rest of ccb_scsiio struct. */ 839286965Sken cam_fill_csio(&ccb->csio, /* ccb_scsiio*/ 840286965Sken retry_count, /* retries*/ 841286965Sken NULL, /* cbfcnp*/ 842286965Sken CAM_DIR_OUT | CAM_DEV_QFRZDIS, /* flags*/ 843286965Sken CAM_TAG_ACTION_NONE, /* tag_action*/ 844286965Sken (u_char *)pkt_ptr, /* data_ptr*/ 845286965Sken pkt_size, /* dxfer_len*/ 846286965Sken SSD_FULL_SIZE, /* sense_len*/ 847286965Sken sizeof(struct scsi_write_buffer), /* cdb_len*/ 848286965Sken timeout ? timeout : WB_TIMEOUT); /* timeout*/ 849286965Sken break; 850286965Sken case CC_DT_ATA: 851286965Sken case CC_DT_ATA_BEHIND_SCSI: { 852286965Sken uint32_t off; 853237281Sscottl 854286965Sken off = (uint32_t)(pkt_ptr - buf); 855286965Sken 856300207Sken retval = build_ata_cmd(ccb, 857286965Sken /*retry_count*/ retry_count, 858286965Sken /*flags*/ CAM_DIR_OUT | CAM_DEV_QFRZDIS, 859286965Sken /*tag_action*/ CAM_TAG_ACTION_NONE, 860286965Sken /*protocol*/ AP_PROTO_PIO_OUT, 861286965Sken /*ata_flags*/ AP_FLAG_BYT_BLOK_BYTES | 862286965Sken AP_FLAG_TLEN_SECT_CNT | 863286965Sken AP_FLAG_TDIR_TO_DEV, 864286965Sken /*features*/ USE_OFFSETS_FEATURE, 865286965Sken /*sector_count*/ ATA_MAKE_SECTORS(pkt_size), 866286965Sken /*lba*/ ATA_MAKE_LBA(off, pkt_size), 867286965Sken /*command*/ ATA_DOWNLOAD_MICROCODE, 868300207Sken /*auxiliary*/ 0, 869286965Sken /*data_ptr*/ (uint8_t *)pkt_ptr, 870286965Sken /*dxfer_len*/ pkt_size, 871300207Sken /*cdb_storage*/ NULL, 872300207Sken /*cdb_storage_len*/ 0, 873286965Sken /*sense_len*/ SSD_FULL_SIZE, 874286965Sken /*timeout*/ timeout ? timeout : WB_TIMEOUT, 875286965Sken /*is48bit*/ 0, 876286965Sken /*devtype*/ devtype); 877300207Sken 878300207Sken if (retval != 0) { 879300207Sken warnx("%s: build_ata_cmd() failed, likely " 880300207Sken "programmer error", __func__); 881300207Sken goto bailout; 882300207Sken } 883286965Sken break; 884237281Sscottl } 885286965Sken default: 886286965Sken warnx("Unknown device type %d", devtype); 887286965Sken retval = 1; 888286965Sken goto bailout; 889286965Sken break; /*NOTREACHED*/ 890286965Sken } 891227961Semaste if (!sim_mode) { 892227961Semaste /* Execute the command. */ 893251743Smav if (cam_send_ccb(cam_dev, ccb) < 0 || 894251743Smav (ccb->ccb_h.status & CAM_STATUS_MASK) != 895251743Smav CAM_REQ_CMP) { 896227961Semaste warnx("Error writing image to device"); 897241737Sed if (printerrors) 898286965Sken cam_error_print(cam_dev, ccb, 899286965Sken CAM_ESF_ALL, CAM_EPF_ALL, stderr); 900286965Sken retval = 1; 901227961Semaste goto bailout; 902227961Semaste } 903286965Sken } else if (printerrors) { 904286965Sken cam_error_print(cam_dev, ccb, CAM_ESF_COMMAND, 0, 905286965Sken stdout); 906227961Semaste } 907286965Sken 908227961Semaste /* Prepare next round. */ 909227961Semaste pkt_count++; 910227961Semaste pkt_ptr += pkt_size; 911227961Semaste img_size -= pkt_size; 912227961Semaste } while(!last_pkt); 913227961Semastebailout: 914286965Sken if (quiet == 0) 915286965Sken progress_complete(&progress, size - img_size); 916286965Sken if (ccb != NULL) 917286965Sken cam_freeccb(ccb); 918286965Sken return (retval); 919227961Semaste} 920227961Semaste 921227961Semasteint 922227961Semastefwdownload(struct cam_device *device, int argc, char **argv, 923314220Sken char *combinedopt, int printerrors, int task_attr, int retry_count, 924314220Sken int timeout) 925227961Semaste{ 926286965Sken struct fw_vendor *vp; 927227961Semaste char *fw_img_path = NULL; 928286965Sken struct ata_params *ident_buf = NULL; 929286965Sken camcontrol_devtype devtype; 930286965Sken char *buf = NULL; 931227961Semaste int img_size; 932227961Semaste int c; 933227961Semaste int sim_mode = 0; 934227961Semaste int confirmed = 0; 935286965Sken int quiet = 0; 936286965Sken int retval = 0; 937227961Semaste 938227961Semaste while ((c = getopt(argc, argv, combinedopt)) != -1) { 939227961Semaste switch (c) { 940286965Sken case 'f': 941286965Sken fw_img_path = optarg; 942286965Sken break; 943286965Sken case 'q': 944286965Sken quiet = 1; 945286965Sken break; 946227961Semaste case 's': 947227961Semaste sim_mode = 1; 948227961Semaste break; 949227961Semaste case 'y': 950227961Semaste confirmed = 1; 951227961Semaste break; 952227961Semaste default: 953227961Semaste break; 954227961Semaste } 955227961Semaste } 956227961Semaste 957227961Semaste if (fw_img_path == NULL) 958286965Sken errx(1, "you must specify a firmware image file using -f " 959286965Sken "option"); 960227961Semaste 961286965Sken retval = get_device_type(device, retry_count, timeout, printerrors, 962286965Sken &devtype); 963286965Sken if (retval != 0) 964286965Sken errx(1, "Unable to determine device type"); 965227961Semaste 966286965Sken if ((devtype == CC_DT_ATA) 967286965Sken || (devtype == CC_DT_ATA_BEHIND_SCSI)) { 968286965Sken union ccb *ccb; 969227961Semaste 970286965Sken ccb = cam_getccb(device); 971286965Sken if (ccb == NULL) { 972286965Sken warnx("couldn't allocate CCB"); 973286965Sken retval = 1; 974286965Sken goto bailout; 975286965Sken } 976286965Sken 977286965Sken if (ata_do_identify(device, retry_count, timeout, ccb, 978286965Sken &ident_buf) != 0) { 979286965Sken cam_freeccb(ccb); 980286965Sken retval = 1; 981286965Sken goto bailout; 982286965Sken } 983286965Sken } else if (devtype != CC_DT_SCSI) 984286965Sken errx(1, "Unsupported device type %d", devtype); 985286965Sken 986286965Sken vp = fw_get_vendor(device, ident_buf); 987286965Sken /* 988286965Sken * Bail out if we have an unknown vendor and this isn't an ATA 989286965Sken * disk. For a SCSI disk, we have no chance of working properly 990286965Sken * with the default values in the VENDOR_UNKNOWN case. For an ATA 991286965Sken * disk connected via an ATA transport, we may work for drives that 992286965Sken * support the ATA_DOWNLOAD_MICROCODE command. 993286965Sken */ 994286965Sken if (((vp == NULL) 995286965Sken || (vp->type == VENDOR_UNKNOWN)) 996286965Sken && (devtype == CC_DT_SCSI)) 997286965Sken errx(1, "Unsupported device"); 998286965Sken 999314220Sken retval = fw_get_timeout(device, vp, task_attr, retry_count, timeout); 1000286965Sken if (retval != 0) { 1001286965Sken warnx("Unable to get a firmware download timeout value"); 1002286965Sken goto bailout; 1003286965Sken } 1004286965Sken 1005286965Sken buf = fw_read_img(device, retry_count, timeout, quiet, fw_img_path, 1006286965Sken vp, &img_size); 1007286965Sken if (buf == NULL) { 1008286965Sken retval = 1; 1009286965Sken goto bailout; 1010286965Sken } 1011286965Sken 1012227961Semaste if (!confirmed) { 1013227961Semaste fprintf(stdout, "You are about to download firmware image (%s)" 1014227961Semaste " into the following device:\n", 1015227961Semaste fw_img_path); 1016286965Sken if (devtype == CC_DT_SCSI) { 1017314220Sken if (scsidoinquiry(device, argc, argv, combinedopt, 1018314220Sken MSG_SIMPLE_Q_TAG, 0, 5000) != 0) { 1019286965Sken warnx("Error sending inquiry"); 1020286965Sken retval = 1; 1021286965Sken goto bailout; 1022286965Sken } 1023286965Sken } else { 1024286965Sken printf("%s%d: ", device->device_name, 1025286965Sken device->dev_unit_num); 1026286965Sken ata_print_ident(ident_buf); 1027286965Sken camxferrate(device); 1028286965Sken free(ident_buf); 1029286965Sken } 1030286965Sken fprintf(stdout, "Using a timeout of %u ms, which is %s.\n", 1031286965Sken vp->timeout_ms, 1032286965Sken fw_timeout_desc_table[vp->timeout_type].timeout_desc); 1033227961Semaste fprintf(stdout, "\nIt may damage your drive. "); 1034286965Sken if (!get_confirmation()) { 1035286965Sken retval = 1; 1036286965Sken goto bailout; 1037286965Sken } 1038227961Semaste } 1039286965Sken if ((sim_mode != 0) && (quiet == 0)) 1040227961Semaste fprintf(stdout, "Running in simulation mode\n"); 1041227961Semaste 1042241737Sed if (fw_download_img(device, vp, buf, img_size, sim_mode, printerrors, 1043286965Sken quiet, retry_count, vp->timeout_ms, fw_img_path, devtype) != 0) { 1044227961Semaste fprintf(stderr, "Firmware download failed\n"); 1045286965Sken retval = 1; 1046286965Sken goto bailout; 1047286965Sken } else if (quiet == 0) 1048227961Semaste fprintf(stdout, "Firmware download successful\n"); 1049227961Semaste 1050286965Skenbailout: 1051227961Semaste free(buf); 1052286965Sken return (retval); 1053227961Semaste} 1054227961Semaste 1055