fwdownload.c revision 299490
1227961Semaste/*- 2227961Semaste * Copyright (c) 2011 Sandvine Incorporated. All rights reserved. 3227961Semaste * Copyright (c) 2002-2011 Andre Albsmeier <andre@albsmeier.net> 4227961Semaste * All rights reserved. 5227961Semaste * 6227961Semaste * Redistribution and use in source and binary forms, with or without 7227961Semaste * modification, are permitted provided that the following conditions 8227961Semaste * are met: 9227961Semaste * 1. Redistributions of source code must retain the above copyright 10227961Semaste * notice, this list of conditions and the following disclaimer, 11227961Semaste * without modification, immediately at the beginning of the file. 12227961Semaste * 2. Redistributions in binary form must reproduce the above copyright 13227961Semaste * notice, this list of conditions and the following disclaimer in the 14227961Semaste * documentation and/or other materials provided with the distribution. 15227961Semaste * 16227961Semaste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17227961Semaste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18227961Semaste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19227961Semaste * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20227961Semaste * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21227961Semaste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22227961Semaste * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23227961Semaste * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24227961Semaste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25227961Semaste * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26227961Semaste */ 27227961Semaste 28227961Semaste/* 29228203Semaste * This software is derived from Andre Albsmeier's fwprog.c which contained 30228203Semaste * the following note: 31228203Semaste * 32228203Semaste * Many thanks goes to Marc Frajola <marc@terasolutions.com> from 33228203Semaste * TeraSolutions for the initial idea and his programme for upgrading 34228203Semaste * the firmware of I*M DDYS drives. 35228203Semaste */ 36228203Semaste 37228203Semaste/* 38227961Semaste * BEWARE: 39227961Semaste * 40227961Semaste * The fact that you see your favorite vendor listed below does not 41227961Semaste * imply that your equipment won't break when you use this software 42227961Semaste * with it. It only means that the firmware of at least one device type 43227961Semaste * of each vendor listed has been programmed successfully using this code. 44227961Semaste * 45227961Semaste * The -s option simulates a download but does nothing apart from that. 46227961Semaste * It can be used to check what chunk sizes would have been used with the 47227961Semaste * specified device. 48227961Semaste */ 49227961Semaste 50227961Semaste#include <sys/cdefs.h> 51227961Semaste__FBSDID("$FreeBSD: head/sbin/camcontrol/fwdownload.c 299490 2016-05-11 22:25:14Z cem $"); 52227961Semaste 53227961Semaste#include <sys/types.h> 54227961Semaste#include <sys/stat.h> 55227961Semaste 56227961Semaste#include <err.h> 57227961Semaste#include <fcntl.h> 58227961Semaste#include <stdio.h> 59227961Semaste#include <stdlib.h> 60227961Semaste#include <string.h> 61227961Semaste#include <unistd.h> 62227961Semaste 63227961Semaste#include <cam/scsi/scsi_all.h> 64227961Semaste#include <cam/scsi/scsi_message.h> 65227961Semaste#include <camlib.h> 66227961Semaste 67237281Sscottl#include "progress.h" 68237281Sscottl 69227961Semaste#include "camcontrol.h" 70227961Semaste 71286965Sken#define WB_TIMEOUT 50000 /* 50 seconds */ 72227961Semaste 73227961Semastetypedef enum { 74286965Sken VENDOR_HGST, 75227961Semaste VENDOR_HITACHI, 76227961Semaste VENDOR_HP, 77227961Semaste VENDOR_IBM, 78227961Semaste VENDOR_PLEXTOR, 79237281Sscottl VENDOR_QUALSTAR, 80227961Semaste VENDOR_QUANTUM, 81255310Sbryanv VENDOR_SAMSUNG, 82227961Semaste VENDOR_SEAGATE, 83286965Sken VENDOR_SMART, 84286965Sken VENDOR_ATA, 85227961Semaste VENDOR_UNKNOWN 86227961Semaste} fw_vendor_t; 87227961Semaste 88286965Sken/* 89286965Sken * FW_TUR_READY: The drive must return good status for a test unit ready. 90286965Sken * 91286965Sken * FW_TUR_NOT_READY: The drive must return not ready status for a test unit 92286965Sken * ready. You may want this in a removable media drive. 93286965Sken * 94286965Sken * FW_TUR_NA: It doesn't matter whether the drive is ready or not. 95286965Sken * This may be the case for a removable media drive. 96286965Sken */ 97286965Skentypedef enum { 98286965Sken FW_TUR_NONE, 99286965Sken FW_TUR_READY, 100286965Sken FW_TUR_NOT_READY, 101286965Sken FW_TUR_NA 102286965Sken} fw_tur_status; 103286965Sken 104286965Sken/* 105286965Sken * FW_TIMEOUT_DEFAULT: Attempt to probe for a WRITE BUFFER timeout 106286965Sken * value from the drive. If we get an answer, 107286965Sken * use the Recommended timeout. Otherwise, 108286965Sken * use the default value from the table. 109286965Sken * 110286965Sken * FW_TIMEOUT_DEV_REPORTED: The timeout value was probed directly from 111286965Sken * the device. 112286965Sken * 113286965Sken * FW_TIMEOUT_NO_PROBE: Do not ask the device for a WRITE BUFFER 114286965Sken * timeout value. Use the device-specific 115286965Sken * value. 116286965Sken * 117286965Sken * FW_TIMEOUT_USER_SPEC: The user specified a timeout on the command 118286965Sken * line with the -t option. This overrides any 119286965Sken * probe or default timeout. 120286965Sken */ 121286965Skentypedef enum { 122286965Sken FW_TIMEOUT_DEFAULT, 123286965Sken FW_TIMEOUT_DEV_REPORTED, 124286965Sken FW_TIMEOUT_NO_PROBE, 125286965Sken FW_TIMEOUT_USER_SPEC 126286965Sken} fw_timeout_type; 127286965Sken 128286965Sken/* 129286965Sken * type: Enumeration for the particular vendor. 130286965Sken * 131286965Sken * pattern: Pattern to match for the Vendor ID from the SCSI 132286965Sken * Inquiry data. 133286965Sken * 134286965Sken * dev_type: SCSI device type to match, or T_ANY to match any 135286965Sken * device from the given vendor. Note that if there 136286965Sken * is a specific device type listed for a particular 137286965Sken * vendor, it must be listed before a T_ANY entry. 138286965Sken * 139286965Sken * max_pkt_size: Maximum packet size when talking to a device. Note 140286965Sken * that although large data sizes may be supported by 141286965Sken * the target device, they may not be supported by the 142286965Sken * OS or the controller. 143286965Sken * 144286965Sken * cdb_byte2: This specifies byte 2 (byte 1 when counting from 0) 145286965Sken * of the CDB. This is generally the WRITE BUFFER mode. 146286965Sken * 147286965Sken * cdb_byte2_last: This specifies byte 2 for the last chunk of the 148286965Sken * download. 149286965Sken * 150286965Sken * inc_cdb_buffer_id: Increment the buffer ID by 1 for each chunk sent 151286965Sken * down to the drive. 152286965Sken * 153286965Sken * inc_cdb_offset: Increment the offset field in the CDB with the byte 154286965Sken * offset into the firmware file. 155286965Sken * 156286965Sken * tur_status: Pay attention to whether the device is ready before 157286965Sken * upgrading the firmware, or not. See above for the 158286965Sken * values. 159286965Sken */ 160227961Semastestruct fw_vendor { 161227961Semaste fw_vendor_t type; 162227961Semaste const char *pattern; 163286965Sken int dev_type; 164227961Semaste int max_pkt_size; 165227961Semaste u_int8_t cdb_byte2; 166227961Semaste u_int8_t cdb_byte2_last; 167227961Semaste int inc_cdb_buffer_id; 168227961Semaste int inc_cdb_offset; 169286965Sken fw_tur_status tur_status; 170286965Sken int timeout_ms; 171286965Sken fw_timeout_type timeout_type; 172227961Semaste}; 173227961Semaste 174286965Sken/* 175286965Sken * Vendor notes: 176286965Sken * 177286965Sken * HGST: The packets need to be sent in multiples of 4K. 178286965Sken * 179286965Sken * IBM: For LTO and TS drives, the buffer ID is ignored in mode 7 (and 180286965Sken * some other modes). It treats the request as a firmware download. 181286965Sken * The offset (and therefore the length of each chunk sent) needs 182286965Sken * to be a multiple of the offset boundary specified for firmware 183286965Sken * (buffer ID 4) in the read buffer command. At least for LTO-6, 184286965Sken * that seems to be 0, but using a 32K chunk size should satisfy 185286965Sken * most any alignment requirement. 186286965Sken * 187286965Sken * SmrtStor: Mode 5 is also supported, but since the firmware is 400KB or 188286965Sken * so, we can't fit it in a single request in most cases. 189286965Sken */ 190286965Skenstatic struct fw_vendor vendors_list[] = { 191286965Sken {VENDOR_HGST, "HGST", T_DIRECT, 192286965Sken 0x1000, 0x07, 0x07, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT}, 193286965Sken {VENDOR_HITACHI, "HITACHI", T_ANY, 194286965Sken 0x8000, 0x05, 0x05, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT}, 195286965Sken {VENDOR_HP, "HP", T_ANY, 196286965Sken 0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT}, 197286965Sken {VENDOR_IBM, "IBM", T_SEQUENTIAL, 198286965Sken 0x8000, 0x07, 0x07, 0, 1, FW_TUR_NA, 300 * 1000, FW_TIMEOUT_DEFAULT}, 199286965Sken {VENDOR_IBM, "IBM", T_ANY, 200286965Sken 0x8000, 0x05, 0x05, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT}, 201286965Sken {VENDOR_PLEXTOR, "PLEXTOR", T_ANY, 202286965Sken 0x2000, 0x04, 0x05, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT}, 203286965Sken {VENDOR_QUALSTAR, "QUALSTAR", T_ANY, 204286965Sken 0x2030, 0x05, 0x05, 0, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT}, 205286965Sken {VENDOR_QUANTUM, "QUANTUM", T_ANY, 206286965Sken 0x2000, 0x04, 0x05, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT}, 207286965Sken {VENDOR_SAMSUNG, "SAMSUNG", T_ANY, 208286965Sken 0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT}, 209286965Sken {VENDOR_SEAGATE, "SEAGATE", T_ANY, 210286965Sken 0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT}, 211286965Sken {VENDOR_SMART, "SmrtStor", T_DIRECT, 212286965Sken 0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT}, 213286965Sken 214286965Sken /* 215286965Sken * We match any ATA device. This is really just a placeholder, 216286965Sken * since we won't actually send a WRITE BUFFER with any of the 217286965Sken * listed parameters. If a SATA device is behind a SAS controller, 218286965Sken * the SCSI to ATA translation code (at least for LSI) doesn't 219298858Spfg * generally translate a SCSI WRITE BUFFER into an ATA DOWNLOAD 220286965Sken * MICROCODE command. So, we use the SCSI ATA PASS_THROUGH command 221286965Sken * to send the ATA DOWNLOAD MICROCODE command instead. 222286965Sken */ 223286965Sken {VENDOR_ATA, "ATA", T_ANY, 224286965Sken 0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, 225286965Sken FW_TIMEOUT_NO_PROBE}, 226286965Sken {VENDOR_UNKNOWN, NULL, T_ANY, 227286965Sken 0x0000, 0x00, 0x00, 0, 0, FW_TUR_NONE, WB_TIMEOUT, FW_TIMEOUT_DEFAULT} 228227961Semaste}; 229227961Semaste 230286965Skenstruct fw_timeout_desc { 231286965Sken fw_timeout_type timeout_type; 232286965Sken const char *timeout_desc; 233286965Sken}; 234286965Sken 235286965Skenstatic const struct fw_timeout_desc fw_timeout_desc_table[] = { 236286965Sken { FW_TIMEOUT_DEFAULT, "the default" }, 237286965Sken { FW_TIMEOUT_DEV_REPORTED, "recommended by this particular device" }, 238286965Sken { FW_TIMEOUT_NO_PROBE, "the default" }, 239286965Sken { FW_TIMEOUT_USER_SPEC, "what was specified on the command line" } 240286965Sken}; 241286965Sken 242237281Sscottl#ifndef ATA_DOWNLOAD_MICROCODE 243237281Sscottl#define ATA_DOWNLOAD_MICROCODE 0x92 244237281Sscottl#endif 245237281Sscottl 246237281Sscottl#define USE_OFFSETS_FEATURE 0x3 247237281Sscottl 248237281Sscottl#ifndef LOW_SECTOR_SIZE 249237281Sscottl#define LOW_SECTOR_SIZE 512 250237281Sscottl#endif 251237281Sscottl 252237281Sscottl#define ATA_MAKE_LBA(o, p) \ 253237281Sscottl ((((((o) / LOW_SECTOR_SIZE) >> 8) & 0xff) << 16) | \ 254237281Sscottl ((((o) / LOW_SECTOR_SIZE) & 0xff) << 8) | \ 255237281Sscottl ((((p) / LOW_SECTOR_SIZE) >> 8) & 0xff)) 256237281Sscottl 257237281Sscottl#define ATA_MAKE_SECTORS(p) (((p) / 512) & 0xff) 258237281Sscottl 259237281Sscottl#ifndef UNKNOWN_MAX_PKT_SIZE 260237281Sscottl#define UNKNOWN_MAX_PKT_SIZE 0x8000 261237281Sscottl#endif 262237281Sscottl 263286965Skenstatic struct fw_vendor *fw_get_vendor(struct cam_device *cam_dev, 264286965Sken struct ata_params *ident_buf); 265286965Skenstatic int fw_get_timeout(struct cam_device *cam_dev, struct fw_vendor *vp, 266286965Sken int retry_count, int timeout); 267286965Skenstatic int fw_validate_ibm(struct cam_device *dev, int retry_count, 268286965Sken int timeout, int fd, char *buf, 269286965Sken const char *fw_img_path, int quiet); 270286965Skenstatic char *fw_read_img(struct cam_device *dev, int retry_count, 271286965Sken int timeout, int quiet, const char *fw_img_path, 272286965Sken struct fw_vendor *vp, int *num_bytes); 273286965Skenstatic int fw_check_device_ready(struct cam_device *dev, 274286965Sken camcontrol_devtype devtype, 275286965Sken struct fw_vendor *vp, int printerrors, 276286965Sken int timeout); 277286965Skenstatic int fw_download_img(struct cam_device *cam_dev, 278286965Sken struct fw_vendor *vp, char *buf, int img_size, 279286965Sken int sim_mode, int printerrors, int quiet, 280286965Sken int retry_count, int timeout, const char */*name*/, 281286965Sken camcontrol_devtype devtype); 282227961Semaste 283227961Semaste/* 284227961Semaste * Find entry in vendors list that belongs to 285227961Semaste * the vendor of given cam device. 286227961Semaste */ 287286965Skenstatic struct fw_vendor * 288286965Skenfw_get_vendor(struct cam_device *cam_dev, struct ata_params *ident_buf) 289227961Semaste{ 290286965Sken char vendor[42]; 291286965Sken struct fw_vendor *vp; 292227961Semaste 293227961Semaste if (cam_dev == NULL) 294227961Semaste return (NULL); 295286965Sken 296286965Sken if (ident_buf != NULL) { 297286965Sken cam_strvis((u_char *)vendor, ident_buf->model, 298286965Sken sizeof(ident_buf->model), sizeof(vendor)); 299286965Sken for (vp = vendors_list; vp->pattern != NULL; vp++) { 300286965Sken if (vp->type == VENDOR_ATA) 301286965Sken return (vp); 302286965Sken } 303286965Sken } else { 304286965Sken cam_strvis((u_char *)vendor, (u_char *)cam_dev->inq_data.vendor, 305286965Sken sizeof(cam_dev->inq_data.vendor), sizeof(vendor)); 306286965Sken } 307227961Semaste for (vp = vendors_list; vp->pattern != NULL; vp++) { 308227961Semaste if (!cam_strmatch((const u_char *)vendor, 309286965Sken (const u_char *)vp->pattern, strlen(vendor))) { 310286965Sken if ((vp->dev_type == T_ANY) 311286965Sken || (vp->dev_type == SID_TYPE(&cam_dev->inq_data))) 312286965Sken break; 313286965Sken } 314227961Semaste } 315227961Semaste return (vp); 316227961Semaste} 317227961Semaste 318286965Skenstatic int 319286965Skenfw_get_timeout(struct cam_device *cam_dev, struct fw_vendor *vp, 320286965Sken int retry_count, int timeout) 321286965Sken{ 322286965Sken struct scsi_report_supported_opcodes_one *one; 323286965Sken struct scsi_report_supported_opcodes_timeout *td; 324286965Sken uint8_t *buf = NULL; 325286965Sken uint32_t fill_len = 0, cdb_len = 0, rec_timeout = 0; 326286965Sken int retval = 0; 327286965Sken 328286965Sken /* 329286965Sken * If the user has specified a timeout on the command line, we let 330286965Sken * him override any default or probed value. 331286965Sken */ 332286965Sken if (timeout != 0) { 333286965Sken vp->timeout_type = FW_TIMEOUT_USER_SPEC; 334286965Sken vp->timeout_ms = timeout; 335286965Sken goto bailout; 336286965Sken } 337286965Sken 338286965Sken /* 339286965Sken * Check to see whether we should probe for a timeout for this 340286965Sken * device. 341286965Sken */ 342286965Sken if (vp->timeout_type == FW_TIMEOUT_NO_PROBE) 343286965Sken goto bailout; 344286965Sken 345286965Sken retval = scsigetopcodes(/*device*/ cam_dev, 346286965Sken /*opcode_set*/ 1, 347286965Sken /*opcode*/ WRITE_BUFFER, 348286965Sken /*show_sa_errors*/ 1, 349286965Sken /*sa_set*/ 0, 350286965Sken /*service_action*/ 0, 351286965Sken /*timeout_desc*/ 1, 352286965Sken /*retry_count*/ retry_count, 353286965Sken /*timeout*/ 10000, 354286965Sken /*verbose*/ 0, 355286965Sken /*fill_len*/ &fill_len, 356286965Sken /*data_ptr*/ &buf); 357286965Sken /* 358286965Sken * It isn't an error if we can't get a timeout descriptor. We just 359286965Sken * continue on with the default timeout. 360286965Sken */ 361286965Sken if (retval != 0) { 362286965Sken retval = 0; 363286965Sken goto bailout; 364286965Sken } 365286965Sken 366286965Sken /* 367286965Sken * Even if the drive didn't return a SCSI error, if we don't have 368286965Sken * enough data to contain the one opcode descriptor, the CDB 369286965Sken * structure and a timeout descriptor, we don't have the timeout 370286965Sken * value we're looking for. So we'll just fall back to the 371286965Sken * default value. 372286965Sken */ 373286965Sken if (fill_len < (sizeof(*one) + sizeof(struct scsi_write_buffer) + 374286965Sken sizeof(*td))) 375286965Sken goto bailout; 376286965Sken 377286965Sken one = (struct scsi_report_supported_opcodes_one *)buf; 378286965Sken 379286965Sken /* 380286965Sken * If the drive claims to not support the WRITE BUFFER command... 381286965Sken * fall back to the default timeout value and let things fail on 382286965Sken * the actual firmware download. 383286965Sken */ 384286965Sken if ((one->support & RSO_ONE_SUP_MASK) == RSO_ONE_SUP_NOT_SUP) 385286965Sken goto bailout; 386286965Sken 387286965Sken cdb_len = scsi_2btoul(one->cdb_length); 388286965Sken td = (struct scsi_report_supported_opcodes_timeout *) 389286965Sken &buf[sizeof(*one) + cdb_len]; 390286965Sken 391286965Sken rec_timeout = scsi_4btoul(td->recommended_time); 392286965Sken /* 393286965Sken * If the recommended timeout is 0, then the device has probably 394286965Sken * returned a bogus value. 395286965Sken */ 396286965Sken if (rec_timeout == 0) 397286965Sken goto bailout; 398286965Sken 399286965Sken /* CAM timeouts are in ms */ 400286965Sken rec_timeout *= 1000; 401286965Sken 402286965Sken vp->timeout_ms = rec_timeout; 403286965Sken vp->timeout_type = FW_TIMEOUT_DEV_REPORTED; 404286965Sken 405286965Skenbailout: 406286965Sken return (retval); 407286965Sken} 408286965Sken 409286965Sken#define SVPD_IBM_FW_DESIGNATION 0x03 410286965Sken 411227961Semaste/* 412286965Sken * IBM LTO and TS tape drives have an INQUIRY VPD page 0x3 with the following 413286965Sken * format: 414286965Sken */ 415286965Skenstruct fw_ibm_tape_fw_designation { 416286965Sken uint8_t device; 417286965Sken uint8_t page_code; 418286965Sken uint8_t reserved; 419286965Sken uint8_t length; 420286965Sken uint8_t ascii_length; 421286965Sken uint8_t reserved2[3]; 422286965Sken uint8_t load_id[4]; 423286965Sken uint8_t fw_rev[4]; 424286965Sken uint8_t ptf_number[4]; 425286965Sken uint8_t patch_number[4]; 426286965Sken uint8_t ru_name[8]; 427286965Sken uint8_t lib_seq_num[5]; 428286965Sken}; 429286965Sken 430286965Sken/* 431286965Sken * The firmware for IBM tape drives has the following header format. The 432286965Sken * load_id and ru_name in the header file should match what is returned in 433286965Sken * VPD page 0x3. 434286965Sken */ 435286965Skenstruct fw_ibm_tape_fw_header { 436286965Sken uint8_t unspec[4]; 437286965Sken uint8_t length[4]; /* Firmware and header! */ 438286965Sken uint8_t load_id[4]; 439286965Sken uint8_t fw_rev[4]; 440286965Sken uint8_t reserved[8]; 441286965Sken uint8_t ru_name[8]; 442286965Sken}; 443286965Sken 444286965Skenstatic int 445286965Skenfw_validate_ibm(struct cam_device *dev, int retry_count, int timeout, int fd, 446286965Sken char *buf, const char *fw_img_path, int quiet) 447286965Sken{ 448286965Sken union ccb *ccb; 449286965Sken struct fw_ibm_tape_fw_designation vpd_page; 450286965Sken struct fw_ibm_tape_fw_header *header; 451286965Sken char drive_rev[sizeof(vpd_page.fw_rev) + 1]; 452286965Sken char file_rev[sizeof(vpd_page.fw_rev) + 1]; 453286965Sken int retval = 1; 454286965Sken 455286965Sken ccb = cam_getccb(dev); 456286965Sken if (ccb == NULL) { 457286965Sken warnx("couldn't allocate CCB"); 458286965Sken goto bailout; 459286965Sken } 460286965Sken 461286965Sken /* cam_getccb cleans up the header, caller has to zero the payload */ 462286965Sken bzero(&(&ccb->ccb_h)[1], 463286965Sken sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 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 669286965Sken bzero(&(&ccb->ccb_h)[1], 670286965Sken sizeof(union ccb) - sizeof(struct ccb_hdr)); 671286965Sken 672286965Sken if (devtype != CC_DT_SCSI) { 673286965Sken dxfer_len = sizeof(struct ata_params); 674286965Sken 675286965Sken ptr = (uint16_t *)malloc(dxfer_len); 676286965Sken if (ptr == NULL) { 677286965Sken warnx("can't malloc memory for identify"); 678286965Sken retval = -1; 679286965Sken goto bailout; 680286965Sken } 681286965Sken bzero(ptr, dxfer_len); 682286965Sken } 683286965Sken 684286965Sken switch (devtype) { 685286965Sken case CC_DT_SCSI: 686286965Sken scsi_test_unit_ready(&ccb->csio, 687286965Sken /*retries*/ 0, 688286965Sken /*cbfcnp*/ NULL, 689286965Sken /*tag_action*/ MSG_SIMPLE_Q_TAG, 690286965Sken /*sense_len*/ SSD_FULL_SIZE, 691286965Sken /*timeout*/ 5000); 692286965Sken break; 693286965Sken case CC_DT_ATA_BEHIND_SCSI: 694286965Sken case CC_DT_ATA: { 695286965Sken build_ata_cmd(ccb, 696286965Sken /*retries*/ 1, 697286965Sken /*flags*/ CAM_DIR_IN, 698286965Sken /*tag_action*/ MSG_SIMPLE_Q_TAG, 699286965Sken /*protocol*/ AP_PROTO_PIO_IN, 700286965Sken /*ata_flags*/ AP_FLAG_BYT_BLOK_BYTES | 701286965Sken AP_FLAG_TLEN_SECT_CNT | 702286965Sken AP_FLAG_TDIR_FROM_DEV, 703286965Sken /*features*/ 0, 704286965Sken /*sector_count*/ (uint8_t) dxfer_len, 705286965Sken /*lba*/ 0, 706286965Sken /*command*/ ATA_ATA_IDENTIFY, 707286965Sken /*data_ptr*/ (uint8_t *)ptr, 708286965Sken /*dxfer_len*/ dxfer_len, 709286965Sken /*sense_len*/ SSD_FULL_SIZE, 710286965Sken /*timeout*/ timeout ? timeout : 30 * 1000, 711286965Sken /*is48bit*/ 0, 712286965Sken /*devtype*/ devtype); 713286965Sken break; 714286965Sken } 715286965Sken default: 716286965Sken warnx("Unknown disk type %d", devtype); 717286965Sken retval = -1; 718286965Sken goto bailout; 719286965Sken break; /*NOTREACHED*/ 720286965Sken } 721286965Sken 722286965Sken ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 723286965Sken 724286965Sken retval = cam_send_ccb(dev, ccb); 725286965Sken if (retval != 0) { 726286965Sken warn("error sending %s CCB", (devtype == CC_DT_SCSI) ? 727286965Sken "Test Unit Ready" : "Identify"); 728286965Sken retval = -1; 729286965Sken goto bailout; 730286965Sken } 731286965Sken 732286965Sken if (((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 733286965Sken && (vp->tur_status == FW_TUR_READY)) { 734286965Sken warnx("Device is not ready"); 735286965Sken if (printerrors) 736286965Sken cam_error_print(dev, ccb, CAM_ESF_ALL, 737286965Sken CAM_EPF_ALL, stderr); 738286965Sken retval = 1; 739286965Sken goto bailout; 740286965Sken } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) 741286965Sken && (vp->tur_status == FW_TUR_NOT_READY)) { 742286965Sken warnx("Device cannot have media loaded when firmware is " 743286965Sken "downloaded"); 744286965Sken retval = 1; 745286965Sken goto bailout; 746286965Sken } 747286965Skenbailout: 748286965Sken if (ccb != NULL) 749286965Sken cam_freeccb(ccb); 750286965Sken 751286965Sken return (retval); 752286965Sken} 753286965Sken 754227961Semaste/* 755227961Semaste * Download firmware stored in buf to cam_dev. If simulation mode 756227961Semaste * is enabled, only show what packet sizes would be sent to the 757227961Semaste * device but do not sent any actual packets 758227961Semaste */ 759227961Semastestatic int 760286965Skenfw_download_img(struct cam_device *cam_dev, struct fw_vendor *vp, 761286965Sken char *buf, int img_size, int sim_mode, int printerrors, int quiet, 762286965Sken int retry_count, int timeout, const char *imgname, 763286965Sken camcontrol_devtype devtype) 764227961Semaste{ 765227961Semaste struct scsi_write_buffer cdb; 766237281Sscottl progress_t progress; 767286965Sken int size = 0; 768286965Sken union ccb *ccb = NULL; 769227961Semaste int pkt_count = 0; 770237281Sscottl int max_pkt_size; 771227961Semaste u_int32_t pkt_size = 0; 772227961Semaste char *pkt_ptr = buf; 773227961Semaste u_int32_t offset; 774227961Semaste int last_pkt = 0; 775286965Sken int retval = 0; 776227961Semaste 777286965Sken /* 778286965Sken * Check to see whether the device is ready to accept a firmware 779286965Sken * download. 780286965Sken */ 781286965Sken retval = fw_check_device_ready(cam_dev, devtype, vp, printerrors, 782286965Sken timeout); 783286965Sken if (retval != 0) 784286965Sken goto bailout; 785286965Sken 786227961Semaste if ((ccb = cam_getccb(cam_dev)) == NULL) { 787227961Semaste warnx("Could not allocate CCB"); 788286965Sken retval = 1; 789286965Sken goto bailout; 790227961Semaste } 791237281Sscottl 792286965Sken bzero(&(&ccb->ccb_h)[1], 793286965Sken sizeof(union ccb) - sizeof(struct ccb_hdr)); 794237281Sscottl 795237281Sscottl max_pkt_size = vp->max_pkt_size; 796286965Sken if (max_pkt_size == 0) 797237281Sscottl max_pkt_size = UNKNOWN_MAX_PKT_SIZE; 798286965Sken 799286965Sken pkt_size = max_pkt_size; 800237281Sscottl progress_init(&progress, imgname, size = img_size); 801227961Semaste /* Download single fw packets. */ 802227961Semaste do { 803237281Sscottl if (img_size <= max_pkt_size) { 804227961Semaste last_pkt = 1; 805227961Semaste pkt_size = img_size; 806227961Semaste } 807237281Sscottl progress_update(&progress, size - img_size); 808286965Sken if (((sim_mode == 0) && (quiet == 0)) 809286965Sken || ((sim_mode != 0) && (printerrors == 0))) 810286965Sken progress_draw(&progress); 811227961Semaste bzero(&cdb, sizeof(cdb)); 812286965Sken switch (devtype) { 813286965Sken case CC_DT_SCSI: 814237281Sscottl cdb.opcode = WRITE_BUFFER; 815237281Sscottl cdb.control = 0; 816237281Sscottl /* Parameter list length. */ 817237281Sscottl scsi_ulto3b(pkt_size, &cdb.length[0]); 818237281Sscottl offset = vp->inc_cdb_offset ? (pkt_ptr - buf) : 0; 819237281Sscottl scsi_ulto3b(offset, &cdb.offset[0]); 820286965Sken cdb.byte2 = last_pkt ? vp->cdb_byte2_last : 821286965Sken vp->cdb_byte2; 822237281Sscottl cdb.buffer_id = vp->inc_cdb_buffer_id ? pkt_count : 0; 823237281Sscottl /* Zero out payload of ccb union after ccb header. */ 824286965Sken bzero(&(&ccb->ccb_h)[1], 825237281Sscottl sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 826286965Sken /* 827286965Sken * Copy previously constructed cdb into ccb_scsiio 828286965Sken * struct. 829286965Sken */ 830237281Sscottl bcopy(&cdb, &ccb->csio.cdb_io.cdb_bytes[0], 831237281Sscottl sizeof(struct scsi_write_buffer)); 832237281Sscottl /* Fill rest of ccb_scsiio struct. */ 833286965Sken cam_fill_csio(&ccb->csio, /* ccb_scsiio*/ 834286965Sken retry_count, /* retries*/ 835286965Sken NULL, /* cbfcnp*/ 836286965Sken CAM_DIR_OUT | CAM_DEV_QFRZDIS, /* flags*/ 837286965Sken CAM_TAG_ACTION_NONE, /* tag_action*/ 838286965Sken (u_char *)pkt_ptr, /* data_ptr*/ 839286965Sken pkt_size, /* dxfer_len*/ 840286965Sken SSD_FULL_SIZE, /* sense_len*/ 841286965Sken sizeof(struct scsi_write_buffer), /* cdb_len*/ 842286965Sken timeout ? timeout : WB_TIMEOUT); /* timeout*/ 843286965Sken break; 844286965Sken case CC_DT_ATA: 845286965Sken case CC_DT_ATA_BEHIND_SCSI: { 846286965Sken uint32_t off; 847237281Sscottl 848286965Sken off = (uint32_t)(pkt_ptr - buf); 849286965Sken 850286965Sken build_ata_cmd(ccb, 851286965Sken /*retry_count*/ retry_count, 852286965Sken /*flags*/ CAM_DIR_OUT | CAM_DEV_QFRZDIS, 853286965Sken /*tag_action*/ CAM_TAG_ACTION_NONE, 854286965Sken /*protocol*/ AP_PROTO_PIO_OUT, 855286965Sken /*ata_flags*/ AP_FLAG_BYT_BLOK_BYTES | 856286965Sken AP_FLAG_TLEN_SECT_CNT | 857286965Sken AP_FLAG_TDIR_TO_DEV, 858286965Sken /*features*/ USE_OFFSETS_FEATURE, 859286965Sken /*sector_count*/ ATA_MAKE_SECTORS(pkt_size), 860286965Sken /*lba*/ ATA_MAKE_LBA(off, pkt_size), 861286965Sken /*command*/ ATA_DOWNLOAD_MICROCODE, 862286965Sken /*data_ptr*/ (uint8_t *)pkt_ptr, 863286965Sken /*dxfer_len*/ pkt_size, 864286965Sken /*sense_len*/ SSD_FULL_SIZE, 865286965Sken /*timeout*/ timeout ? timeout : WB_TIMEOUT, 866286965Sken /*is48bit*/ 0, 867286965Sken /*devtype*/ devtype); 868286965Sken break; 869237281Sscottl } 870286965Sken default: 871286965Sken warnx("Unknown device type %d", devtype); 872286965Sken retval = 1; 873286965Sken goto bailout; 874286965Sken break; /*NOTREACHED*/ 875286965Sken } 876227961Semaste if (!sim_mode) { 877227961Semaste /* Execute the command. */ 878251743Smav if (cam_send_ccb(cam_dev, ccb) < 0 || 879251743Smav (ccb->ccb_h.status & CAM_STATUS_MASK) != 880251743Smav CAM_REQ_CMP) { 881227961Semaste warnx("Error writing image to device"); 882241737Sed if (printerrors) 883286965Sken cam_error_print(cam_dev, ccb, 884286965Sken CAM_ESF_ALL, CAM_EPF_ALL, stderr); 885286965Sken retval = 1; 886227961Semaste goto bailout; 887227961Semaste } 888286965Sken } else if (printerrors) { 889286965Sken cam_error_print(cam_dev, ccb, CAM_ESF_COMMAND, 0, 890286965Sken stdout); 891227961Semaste } 892286965Sken 893227961Semaste /* Prepare next round. */ 894227961Semaste pkt_count++; 895227961Semaste pkt_ptr += pkt_size; 896227961Semaste img_size -= pkt_size; 897227961Semaste } while(!last_pkt); 898227961Semastebailout: 899286965Sken if (quiet == 0) 900286965Sken progress_complete(&progress, size - img_size); 901286965Sken if (ccb != NULL) 902286965Sken cam_freeccb(ccb); 903286965Sken return (retval); 904227961Semaste} 905227961Semaste 906227961Semasteint 907227961Semastefwdownload(struct cam_device *device, int argc, char **argv, 908286965Sken char *combinedopt, int printerrors, int retry_count, int timeout) 909227961Semaste{ 910286965Sken struct fw_vendor *vp; 911227961Semaste char *fw_img_path = NULL; 912286965Sken struct ata_params *ident_buf = NULL; 913286965Sken camcontrol_devtype devtype; 914286965Sken char *buf = NULL; 915227961Semaste int img_size; 916227961Semaste int c; 917227961Semaste int sim_mode = 0; 918227961Semaste int confirmed = 0; 919286965Sken int quiet = 0; 920286965Sken int retval = 0; 921227961Semaste 922227961Semaste while ((c = getopt(argc, argv, combinedopt)) != -1) { 923227961Semaste switch (c) { 924286965Sken case 'f': 925286965Sken fw_img_path = optarg; 926286965Sken break; 927286965Sken case 'q': 928286965Sken quiet = 1; 929286965Sken break; 930227961Semaste case 's': 931227961Semaste sim_mode = 1; 932227961Semaste break; 933227961Semaste case 'y': 934227961Semaste confirmed = 1; 935227961Semaste break; 936227961Semaste default: 937227961Semaste break; 938227961Semaste } 939227961Semaste } 940227961Semaste 941227961Semaste if (fw_img_path == NULL) 942286965Sken errx(1, "you must specify a firmware image file using -f " 943286965Sken "option"); 944227961Semaste 945286965Sken retval = get_device_type(device, retry_count, timeout, printerrors, 946286965Sken &devtype); 947286965Sken if (retval != 0) 948286965Sken errx(1, "Unable to determine device type"); 949227961Semaste 950286965Sken if ((devtype == CC_DT_ATA) 951286965Sken || (devtype == CC_DT_ATA_BEHIND_SCSI)) { 952286965Sken union ccb *ccb; 953227961Semaste 954286965Sken ccb = cam_getccb(device); 955286965Sken if (ccb == NULL) { 956286965Sken warnx("couldn't allocate CCB"); 957286965Sken retval = 1; 958286965Sken goto bailout; 959286965Sken } 960286965Sken 961286965Sken if (ata_do_identify(device, retry_count, timeout, ccb, 962286965Sken &ident_buf) != 0) { 963286965Sken cam_freeccb(ccb); 964286965Sken retval = 1; 965286965Sken goto bailout; 966286965Sken } 967286965Sken } else if (devtype != CC_DT_SCSI) 968286965Sken errx(1, "Unsupported device type %d", devtype); 969286965Sken 970286965Sken vp = fw_get_vendor(device, ident_buf); 971286965Sken /* 972286965Sken * Bail out if we have an unknown vendor and this isn't an ATA 973286965Sken * disk. For a SCSI disk, we have no chance of working properly 974286965Sken * with the default values in the VENDOR_UNKNOWN case. For an ATA 975286965Sken * disk connected via an ATA transport, we may work for drives that 976286965Sken * support the ATA_DOWNLOAD_MICROCODE command. 977286965Sken */ 978286965Sken if (((vp == NULL) 979286965Sken || (vp->type == VENDOR_UNKNOWN)) 980286965Sken && (devtype == CC_DT_SCSI)) 981286965Sken errx(1, "Unsupported device"); 982286965Sken 983286965Sken retval = fw_get_timeout(device, vp, retry_count, timeout); 984286965Sken if (retval != 0) { 985286965Sken warnx("Unable to get a firmware download timeout value"); 986286965Sken goto bailout; 987286965Sken } 988286965Sken 989286965Sken buf = fw_read_img(device, retry_count, timeout, quiet, fw_img_path, 990286965Sken vp, &img_size); 991286965Sken if (buf == NULL) { 992286965Sken retval = 1; 993286965Sken goto bailout; 994286965Sken } 995286965Sken 996227961Semaste if (!confirmed) { 997227961Semaste fprintf(stdout, "You are about to download firmware image (%s)" 998227961Semaste " into the following device:\n", 999227961Semaste fw_img_path); 1000286965Sken if (devtype == CC_DT_SCSI) { 1001286965Sken if (scsidoinquiry(device, argc, argv, combinedopt, 0, 1002286965Sken 5000) != 0) { 1003286965Sken warnx("Error sending inquiry"); 1004286965Sken retval = 1; 1005286965Sken goto bailout; 1006286965Sken } 1007286965Sken } else { 1008286965Sken printf("%s%d: ", device->device_name, 1009286965Sken device->dev_unit_num); 1010286965Sken ata_print_ident(ident_buf); 1011286965Sken camxferrate(device); 1012286965Sken free(ident_buf); 1013286965Sken } 1014286965Sken fprintf(stdout, "Using a timeout of %u ms, which is %s.\n", 1015286965Sken vp->timeout_ms, 1016286965Sken fw_timeout_desc_table[vp->timeout_type].timeout_desc); 1017227961Semaste fprintf(stdout, "\nIt may damage your drive. "); 1018286965Sken if (!get_confirmation()) { 1019286965Sken retval = 1; 1020286965Sken goto bailout; 1021286965Sken } 1022227961Semaste } 1023286965Sken if ((sim_mode != 0) && (quiet == 0)) 1024227961Semaste fprintf(stdout, "Running in simulation mode\n"); 1025227961Semaste 1026241737Sed if (fw_download_img(device, vp, buf, img_size, sim_mode, printerrors, 1027286965Sken quiet, retry_count, vp->timeout_ms, fw_img_path, devtype) != 0) { 1028227961Semaste fprintf(stderr, "Firmware download failed\n"); 1029286965Sken retval = 1; 1030286965Sken goto bailout; 1031286965Sken } else if (quiet == 0) 1032227961Semaste fprintf(stdout, "Firmware download successful\n"); 1033227961Semaste 1034286965Skenbailout: 1035227961Semaste free(buf); 1036286965Sken return (retval); 1037227961Semaste} 1038227961Semaste 1039