2 * Copyright (c) 2011 Sandvine Incorporated. All rights reserved.
3 * Copyright (c) 2002-2011 Andre Albsmeier <andre@albsmeier.net>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * This software is derived from Andre Albsmeier's fwprog.c which contained
32 * Many thanks goes to Marc Frajola <marc@terasolutions.com> from
33 * TeraSolutions for the initial idea and his programme for upgrading
34 * the firmware of I*M DDYS drives.
40 * The fact that you see your favorite vendor listed below does not
41 * imply that your equipment won't break when you use this software
42 * with it. It only means that the firmware of at least one device type
43 * of each vendor listed has been programmed successfully using this code.
45 * The -s option simulates a download but does nothing apart from that.
46 * It can be used to check what chunk sizes would have been used with the
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 #include <sys/types.h>
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_message.h>
69 #include "camcontrol.h"
71 #define WB_TIMEOUT 50000 /* 50 seconds */
89 * FW_TUR_READY: The drive must return good status for a test unit ready.
91 * FW_TUR_NOT_READY: The drive must return not ready status for a test unit
92 * ready. You may want this in a removable media drive.
94 * FW_TUR_NA: It doesn't matter whether the drive is ready or not.
95 * This may be the case for a removable media drive.
105 * FW_TIMEOUT_DEFAULT: Attempt to probe for a WRITE BUFFER timeout
106 * value from the drive. If we get an answer,
107 * use the Recommended timeout. Otherwise,
108 * use the default value from the table.
110 * FW_TIMEOUT_DEV_REPORTED: The timeout value was probed directly from
113 * FW_TIMEOUT_NO_PROBE: Do not ask the device for a WRITE BUFFER
114 * timeout value. Use the device-specific
117 * FW_TIMEOUT_USER_SPEC: The user specified a timeout on the command
118 * line with the -t option. This overrides any
119 * probe or default timeout.
123 FW_TIMEOUT_DEV_REPORTED,
129 * type: Enumeration for the particular vendor.
131 * pattern: Pattern to match for the Vendor ID from the SCSI
134 * dev_type: SCSI device type to match, or T_ANY to match any
135 * device from the given vendor. Note that if there
136 * is a specific device type listed for a particular
137 * vendor, it must be listed before a T_ANY entry.
139 * max_pkt_size: Maximum packet size when talking to a device. Note
140 * that although large data sizes may be supported by
141 * the target device, they may not be supported by the
142 * OS or the controller.
144 * cdb_byte2: This specifies byte 2 (byte 1 when counting from 0)
145 * of the CDB. This is generally the WRITE BUFFER mode.
147 * cdb_byte2_last: This specifies byte 2 for the last chunk of the
150 * inc_cdb_buffer_id: Increment the buffer ID by 1 for each chunk sent
153 * inc_cdb_offset: Increment the offset field in the CDB with the byte
154 * offset into the firmware file.
156 * tur_status: Pay attention to whether the device is ready before
157 * upgrading the firmware, or not. See above for the
166 u_int8_t cdb_byte2_last;
167 int inc_cdb_buffer_id;
169 fw_tur_status tur_status;
171 fw_timeout_type timeout_type;
177 * HGST: The packets need to be sent in multiples of 4K.
179 * IBM: For LTO and TS drives, the buffer ID is ignored in mode 7 (and
180 * some other modes). It treats the request as a firmware download.
181 * The offset (and therefore the length of each chunk sent) needs
182 * to be a multiple of the offset boundary specified for firmware
183 * (buffer ID 4) in the read buffer command. At least for LTO-6,
184 * that seems to be 0, but using a 32K chunk size should satisfy
185 * most any alignment requirement.
187 * SmrtStor: Mode 5 is also supported, but since the firmware is 400KB or
188 * so, we can't fit it in a single request in most cases.
190 static struct fw_vendor vendors_list[] = {
191 {VENDOR_HGST, "HGST", T_DIRECT,
192 0x1000, 0x07, 0x07, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
193 {VENDOR_HITACHI, "HITACHI", T_ANY,
194 0x8000, 0x05, 0x05, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
195 {VENDOR_HP, "HP", T_ANY,
196 0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
197 {VENDOR_IBM, "IBM", T_SEQUENTIAL,
198 0x8000, 0x07, 0x07, 0, 1, FW_TUR_NA, 300 * 1000, FW_TIMEOUT_DEFAULT},
199 {VENDOR_IBM, "IBM", T_ANY,
200 0x8000, 0x05, 0x05, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
201 {VENDOR_PLEXTOR, "PLEXTOR", T_ANY,
202 0x2000, 0x04, 0x05, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
203 {VENDOR_QUALSTAR, "QUALSTAR", T_ANY,
204 0x2030, 0x05, 0x05, 0, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
205 {VENDOR_QUANTUM, "QUANTUM", T_ANY,
206 0x2000, 0x04, 0x05, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
207 {VENDOR_SAMSUNG, "SAMSUNG", T_ANY,
208 0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
209 {VENDOR_SEAGATE, "SEAGATE", T_ANY,
210 0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
211 {VENDOR_SMART, "SmrtStor", T_DIRECT,
212 0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
215 * We match any ATA device. This is really just a placeholder,
216 * since we won't actually send a WRITE BUFFER with any of the
217 * listed parameters. If a SATA device is behind a SAS controller,
218 * the SCSI to ATA translation code (at least for LSI) doesn't
219 * generaly translate a SCSI WRITE BUFFER into an ATA DOWNLOAD
220 * MICROCODE command. So, we use the SCSI ATA PASS_THROUGH command
221 * to send the ATA DOWNLOAD MICROCODE command instead.
223 {VENDOR_ATA, "ATA", T_ANY,
224 0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT,
225 FW_TIMEOUT_NO_PROBE},
226 {VENDOR_UNKNOWN, NULL, T_ANY,
227 0x0000, 0x00, 0x00, 0, 0, FW_TUR_NONE, WB_TIMEOUT, FW_TIMEOUT_DEFAULT}
230 struct fw_timeout_desc {
231 fw_timeout_type timeout_type;
232 const char *timeout_desc;
235 static const struct fw_timeout_desc fw_timeout_desc_table[] = {
236 { FW_TIMEOUT_DEFAULT, "the default" },
237 { FW_TIMEOUT_DEV_REPORTED, "recommended by this particular device" },
238 { FW_TIMEOUT_NO_PROBE, "the default" },
239 { FW_TIMEOUT_USER_SPEC, "what was specified on the command line" }
242 #ifndef ATA_DOWNLOAD_MICROCODE
243 #define ATA_DOWNLOAD_MICROCODE 0x92
246 #define USE_OFFSETS_FEATURE 0x3
248 #ifndef LOW_SECTOR_SIZE
249 #define LOW_SECTOR_SIZE 512
252 #define ATA_MAKE_LBA(o, p) \
253 ((((((o) / LOW_SECTOR_SIZE) >> 8) & 0xff) << 16) | \
254 ((((o) / LOW_SECTOR_SIZE) & 0xff) << 8) | \
255 ((((p) / LOW_SECTOR_SIZE) >> 8) & 0xff))
257 #define ATA_MAKE_SECTORS(p) (((p) / 512) & 0xff)
259 #ifndef UNKNOWN_MAX_PKT_SIZE
260 #define UNKNOWN_MAX_PKT_SIZE 0x8000
263 static struct fw_vendor *fw_get_vendor(struct cam_device *cam_dev,
264 struct ata_params *ident_buf);
265 static int fw_get_timeout(struct cam_device *cam_dev, struct fw_vendor *vp,
266 int task_attr, int retry_count, int timeout);
267 static int fw_validate_ibm(struct cam_device *dev, int retry_count,
268 int timeout, int fd, char *buf,
269 const char *fw_img_path, int quiet);
270 static char *fw_read_img(struct cam_device *dev, int retry_count,
271 int timeout, int quiet, const char *fw_img_path,
272 struct fw_vendor *vp, int *num_bytes);
273 static int fw_check_device_ready(struct cam_device *dev,
274 camcontrol_devtype devtype,
275 struct fw_vendor *vp, int printerrors,
277 static int fw_download_img(struct cam_device *cam_dev,
278 struct fw_vendor *vp, char *buf, int img_size,
279 int sim_mode, int printerrors, int quiet,
280 int retry_count, int timeout, const char */*name*/,
281 camcontrol_devtype devtype);
284 * Find entry in vendors list that belongs to
285 * the vendor of given cam device.
287 static struct fw_vendor *
288 fw_get_vendor(struct cam_device *cam_dev, struct ata_params *ident_buf)
291 struct fw_vendor *vp;
296 if (ident_buf != NULL) {
297 cam_strvis((u_char *)vendor, ident_buf->model,
298 sizeof(ident_buf->model), sizeof(vendor));
299 for (vp = vendors_list; vp->pattern != NULL; vp++) {
300 if (vp->type == VENDOR_ATA)
304 cam_strvis((u_char *)vendor, (u_char *)cam_dev->inq_data.vendor,
305 sizeof(cam_dev->inq_data.vendor), sizeof(vendor));
307 for (vp = vendors_list; vp->pattern != NULL; vp++) {
308 if (!cam_strmatch((const u_char *)vendor,
309 (const u_char *)vp->pattern, strlen(vendor))) {
310 if ((vp->dev_type == T_ANY)
311 || (vp->dev_type == SID_TYPE(&cam_dev->inq_data)))
319 fw_get_timeout(struct cam_device *cam_dev, struct fw_vendor *vp,
320 int task_attr, int retry_count, int timeout)
322 struct scsi_report_supported_opcodes_one *one;
323 struct scsi_report_supported_opcodes_timeout *td;
325 uint32_t fill_len = 0, cdb_len = 0, rec_timeout = 0;
329 * If the user has specified a timeout on the command line, we let
330 * him override any default or probed value.
333 vp->timeout_type = FW_TIMEOUT_USER_SPEC;
334 vp->timeout_ms = timeout;
339 * Check to see whether we should probe for a timeout for this
342 if (vp->timeout_type == FW_TIMEOUT_NO_PROBE)
345 retval = scsigetopcodes(/*device*/ cam_dev,
347 /*opcode*/ WRITE_BUFFER,
348 /*show_sa_errors*/ 1,
350 /*service_action*/ 0,
352 /*task_attr*/ task_attr,
353 /*retry_count*/ retry_count,
356 /*fill_len*/ &fill_len,
359 * It isn't an error if we can't get a timeout descriptor. We just
360 * continue on with the default timeout.
368 * Even if the drive didn't return a SCSI error, if we don't have
369 * enough data to contain the one opcode descriptor, the CDB
370 * structure and a timeout descriptor, we don't have the timeout
371 * value we're looking for. So we'll just fall back to the
374 if (fill_len < (sizeof(*one) + sizeof(struct scsi_write_buffer) +
378 one = (struct scsi_report_supported_opcodes_one *)buf;
381 * If the drive claims to not support the WRITE BUFFER command...
382 * fall back to the default timeout value and let things fail on
383 * the actual firmware download.
385 if ((one->support & RSO_ONE_SUP_MASK) == RSO_ONE_SUP_NOT_SUP)
388 cdb_len = scsi_2btoul(one->cdb_length);
389 td = (struct scsi_report_supported_opcodes_timeout *)
390 &buf[sizeof(*one) + cdb_len];
392 rec_timeout = scsi_4btoul(td->recommended_time);
394 * If the recommended timeout is 0, then the device has probably
395 * returned a bogus value.
397 if (rec_timeout == 0)
400 /* CAM timeouts are in ms */
403 vp->timeout_ms = rec_timeout;
404 vp->timeout_type = FW_TIMEOUT_DEV_REPORTED;
410 #define SVPD_IBM_FW_DESIGNATION 0x03
413 * IBM LTO and TS tape drives have an INQUIRY VPD page 0x3 with the following
416 struct fw_ibm_tape_fw_designation {
421 uint8_t ascii_length;
422 uint8_t reserved2[3];
425 uint8_t ptf_number[4];
426 uint8_t patch_number[4];
428 uint8_t lib_seq_num[5];
432 * The firmware for IBM tape drives has the following header format. The
433 * load_id and ru_name in the header file should match what is returned in
436 struct fw_ibm_tape_fw_header {
438 uint8_t length[4]; /* Firmware and header! */
446 fw_validate_ibm(struct cam_device *dev, int retry_count, int timeout, int fd,
447 char *buf, const char *fw_img_path, int quiet)
450 struct fw_ibm_tape_fw_designation vpd_page;
451 struct fw_ibm_tape_fw_header *header;
452 char drive_rev[sizeof(vpd_page.fw_rev) + 1];
453 char file_rev[sizeof(vpd_page.fw_rev) + 1];
456 ccb = cam_getccb(dev);
458 warnx("couldn't allocate CCB");
462 /* cam_getccb cleans up the header, caller has to zero the payload */
463 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
465 bzero(&vpd_page, sizeof(vpd_page));
467 scsi_inquiry(&ccb->csio,
468 /*retries*/ retry_count,
470 /* tag_action */ MSG_SIMPLE_Q_TAG,
471 /* inq_buf */ (u_int8_t *)&vpd_page,
472 /* inq_len */ sizeof(vpd_page),
474 /* page_code */ SVPD_IBM_FW_DESIGNATION,
475 /* sense_len */ SSD_FULL_SIZE,
476 /* timeout */ timeout ? timeout : 5000);
478 /* Disable freezing the device queue */
479 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
481 if (retry_count != 0)
482 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
484 if (cam_send_ccb(dev, ccb) < 0) {
485 warn("error getting firmware designation page");
487 cam_error_print(dev, ccb, CAM_ESF_ALL,
488 CAM_EPF_ALL, stderr);
495 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
496 cam_error_print(dev, ccb, CAM_ESF_ALL,
497 CAM_EPF_ALL, stderr);
502 * Read the firmware header only.
504 if (read(fd, buf, sizeof(*header)) != sizeof(*header)) {
505 warn("unable to read %zu bytes from %s", sizeof(*header),
510 /* Rewind the file back to 0 for the full file read. */
511 if (lseek(fd, 0, SEEK_SET) == -1) {
512 warn("Unable to lseek");
516 header = (struct fw_ibm_tape_fw_header *)buf;
518 bzero(drive_rev, sizeof(drive_rev));
519 bcopy(vpd_page.fw_rev, drive_rev, sizeof(vpd_page.fw_rev));
520 bzero(file_rev, sizeof(file_rev));
521 bcopy(header->fw_rev, file_rev, sizeof(header->fw_rev));
524 fprintf(stdout, "Current Drive Firmware version: %s\n",
526 fprintf(stdout, "Firmware File version: %s\n", file_rev);
530 * For IBM tape drives the load ID and RU name reported by the
531 * drive should match what is in the firmware file.
533 if (bcmp(vpd_page.load_id, header->load_id,
534 MIN(sizeof(vpd_page.load_id), sizeof(header->load_id))) != 0) {
535 warnx("Drive Firmware load ID 0x%x does not match firmware "
536 "file load ID 0x%x", scsi_4btoul(vpd_page.load_id),
537 scsi_4btoul(header->load_id));
541 if (bcmp(vpd_page.ru_name, header->ru_name,
542 MIN(sizeof(vpd_page.ru_name), sizeof(header->ru_name))) != 0) {
543 warnx("Drive Firmware RU name 0x%jx does not match firmware "
544 "file RU name 0x%jx",
545 (uintmax_t)scsi_8btou64(vpd_page.ru_name),
546 (uintmax_t)scsi_8btou64(header->ru_name));
550 fprintf(stdout, "Firmware file is valid for this drive.\n");
559 * Allocate a buffer and read fw image file into it
560 * from given path. Number of bytes read is stored
564 fw_read_img(struct cam_device *dev, int retry_count, int timeout, int quiet,
565 const char *fw_img_path, struct fw_vendor *vp, int *num_bytes)
573 if ((fd = open(fw_img_path, O_RDONLY)) < 0) {
574 warn("Could not open image file %s", fw_img_path);
577 if (fstat(fd, &stbuf) < 0) {
578 warn("Could not stat image file %s", fw_img_path);
581 if ((img_size = stbuf.st_size) == 0) {
582 warnx("Zero length image file %s", fw_img_path);
585 if ((buf = malloc(img_size)) == NULL) {
586 warnx("Could not allocate buffer to read image file %s",
590 /* Skip headers if applicable. */
593 if (read(fd, buf, 16) != 16) {
594 warn("Could not read image file %s", fw_img_path);
597 if (lseek(fd, 0, SEEK_SET) == -1) {
598 warn("Unable to lseek");
601 if ((strncmp(buf, "SEAGATE,SEAGATE ", 16) == 0) ||
602 (img_size % 512 == 80))
605 case VENDOR_QUALSTAR:
606 skip_bytes = img_size % 1030;
609 if (vp->dev_type != T_SEQUENTIAL)
611 if (fw_validate_ibm(dev, retry_count, timeout, fd, buf,
612 fw_img_path, quiet) != 0)
619 if (skip_bytes != 0) {
620 fprintf(stdout, "Skipping %d byte header.\n", skip_bytes);
621 if (lseek(fd, skip_bytes, SEEK_SET) == -1) {
622 warn("Could not lseek");
625 img_size -= skip_bytes;
627 /* Read image into a buffer. */
628 if (read(fd, buf, img_size) != img_size) {
629 warn("Could not read image file %s", fw_img_path);
632 *num_bytes = img_size;
644 * Returns 0 for "success", where success means that the device has met the
645 * requirement in the vendor structure for being ready or not ready when
646 * firmware is downloaded.
648 * Returns 1 for a failure to be ready to accept a firmware download.
649 * (e.g., a drive needs to be ready, but returns not ready)
651 * Returns -1 for any other failure.
654 fw_check_device_ready(struct cam_device *dev, camcontrol_devtype devtype,
655 struct fw_vendor *vp, int printerrors, int timeout)
660 size_t dxfer_len = 0;
662 if ((ccb = cam_getccb(dev)) == NULL) {
663 warnx("Could not allocate CCB");
668 CCB_CLEAR_ALL_EXCEPT_HDR(ccb);
670 if (devtype != CC_DT_SCSI) {
671 dxfer_len = sizeof(struct ata_params);
673 ptr = (uint16_t *)malloc(dxfer_len);
675 warnx("can't malloc memory for identify");
679 bzero(ptr, dxfer_len);
684 scsi_test_unit_ready(&ccb->csio,
687 /*tag_action*/ MSG_SIMPLE_Q_TAG,
688 /*sense_len*/ SSD_FULL_SIZE,
691 case CC_DT_ATA_BEHIND_SCSI:
695 /*flags*/ CAM_DIR_IN,
696 /*tag_action*/ MSG_SIMPLE_Q_TAG,
697 /*protocol*/ AP_PROTO_PIO_IN,
698 /*ata_flags*/ AP_FLAG_BYT_BLOK_BYTES |
699 AP_FLAG_TLEN_SECT_CNT |
700 AP_FLAG_TDIR_FROM_DEV,
702 /*sector_count*/ (uint8_t) dxfer_len,
704 /*command*/ ATA_ATA_IDENTIFY,
705 /*data_ptr*/ (uint8_t *)ptr,
706 /*dxfer_len*/ dxfer_len,
707 /*sense_len*/ SSD_FULL_SIZE,
708 /*timeout*/ timeout ? timeout : 30 * 1000,
710 /*devtype*/ devtype);
714 warnx("Unknown disk type %d", devtype);
717 break; /*NOTREACHED*/
720 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
722 retval = cam_send_ccb(dev, ccb);
724 warn("error sending %s CCB", (devtype == CC_DT_SCSI) ?
725 "Test Unit Ready" : "Identify");
730 if (((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
731 && (vp->tur_status == FW_TUR_READY)) {
732 warnx("Device is not ready");
734 cam_error_print(dev, ccb, CAM_ESF_ALL,
735 CAM_EPF_ALL, stderr);
738 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
739 && (vp->tur_status == FW_TUR_NOT_READY)) {
740 warnx("Device cannot have media loaded when firmware is "
753 * Download firmware stored in buf to cam_dev. If simulation mode
754 * is enabled, only show what packet sizes would be sent to the
755 * device but do not sent any actual packets
758 fw_download_img(struct cam_device *cam_dev, struct fw_vendor *vp,
759 char *buf, int img_size, int sim_mode, int printerrors, int quiet,
760 int retry_count, int timeout, const char *imgname,
761 camcontrol_devtype devtype)
763 struct scsi_write_buffer cdb;
766 union ccb *ccb = NULL;
769 u_int32_t pkt_size = 0;
776 * Check to see whether the device is ready to accept a firmware
779 retval = fw_check_device_ready(cam_dev, devtype, vp, printerrors,
784 if ((ccb = cam_getccb(cam_dev)) == NULL) {
785 warnx("Could not allocate CCB");
790 CCB_CLEAR_ALL_EXCEPT_HDR(ccb);
792 max_pkt_size = vp->max_pkt_size;
793 if (max_pkt_size == 0)
794 max_pkt_size = UNKNOWN_MAX_PKT_SIZE;
796 pkt_size = max_pkt_size;
797 progress_init(&progress, imgname, size = img_size);
798 /* Download single fw packets. */
800 if (img_size <= max_pkt_size) {
804 progress_update(&progress, size - img_size);
805 if (((sim_mode == 0) && (quiet == 0))
806 || ((sim_mode != 0) && (printerrors == 0)))
807 progress_draw(&progress);
808 bzero(&cdb, sizeof(cdb));
811 cdb.opcode = WRITE_BUFFER;
813 /* Parameter list length. */
814 scsi_ulto3b(pkt_size, &cdb.length[0]);
815 offset = vp->inc_cdb_offset ? (pkt_ptr - buf) : 0;
816 scsi_ulto3b(offset, &cdb.offset[0]);
817 cdb.byte2 = last_pkt ? vp->cdb_byte2_last :
819 cdb.buffer_id = vp->inc_cdb_buffer_id ? pkt_count : 0;
820 /* Zero out payload of ccb union after ccb header. */
821 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
823 * Copy previously constructed cdb into ccb_scsiio
826 bcopy(&cdb, &ccb->csio.cdb_io.cdb_bytes[0],
827 sizeof(struct scsi_write_buffer));
828 /* Fill rest of ccb_scsiio struct. */
829 cam_fill_csio(&ccb->csio, /* ccb_scsiio*/
830 retry_count, /* retries*/
832 CAM_DIR_OUT | CAM_DEV_QFRZDIS, /* flags*/
833 CAM_TAG_ACTION_NONE, /* tag_action*/
834 (u_char *)pkt_ptr, /* data_ptr*/
835 pkt_size, /* dxfer_len*/
836 SSD_FULL_SIZE, /* sense_len*/
837 sizeof(struct scsi_write_buffer), /* cdb_len*/
838 timeout ? timeout : WB_TIMEOUT); /* timeout*/
841 case CC_DT_ATA_BEHIND_SCSI: {
844 off = (uint32_t)(pkt_ptr - buf);
847 /*retry_count*/ retry_count,
848 /*flags*/ CAM_DIR_OUT | CAM_DEV_QFRZDIS,
849 /*tag_action*/ CAM_TAG_ACTION_NONE,
850 /*protocol*/ AP_PROTO_PIO_OUT,
851 /*ata_flags*/ AP_FLAG_BYT_BLOK_BYTES |
852 AP_FLAG_TLEN_SECT_CNT |
854 /*features*/ USE_OFFSETS_FEATURE,
855 /*sector_count*/ ATA_MAKE_SECTORS(pkt_size),
856 /*lba*/ ATA_MAKE_LBA(off, pkt_size),
857 /*command*/ ATA_DOWNLOAD_MICROCODE,
858 /*data_ptr*/ (uint8_t *)pkt_ptr,
859 /*dxfer_len*/ pkt_size,
860 /*sense_len*/ SSD_FULL_SIZE,
861 /*timeout*/ timeout ? timeout : WB_TIMEOUT,
863 /*devtype*/ devtype);
867 warnx("Unknown device type %d", devtype);
870 break; /*NOTREACHED*/
873 /* Execute the command. */
874 if (cam_send_ccb(cam_dev, ccb) < 0 ||
875 (ccb->ccb_h.status & CAM_STATUS_MASK) !=
877 warnx("Error writing image to device");
879 cam_error_print(cam_dev, ccb,
880 CAM_ESF_ALL, CAM_EPF_ALL, stderr);
884 } else if (printerrors) {
885 cam_error_print(cam_dev, ccb, CAM_ESF_COMMAND, 0,
889 /* Prepare next round. */
892 img_size -= pkt_size;
896 progress_complete(&progress, size - img_size);
902 fwdownload(struct cam_device *device, int argc, char **argv,
903 char *combinedopt, int printerrors, int task_attr, int retry_count,
906 union ccb *ccb = NULL;
907 struct fw_vendor *vp;
908 char *fw_img_path = NULL;
909 struct ata_params *ident_buf = NULL;
910 camcontrol_devtype devtype;
919 while ((c = getopt(argc, argv, combinedopt)) != -1) {
922 fw_img_path = optarg;
938 if (fw_img_path == NULL)
939 errx(1, "you must specify a firmware image file using -f "
942 retval = get_device_type(device, retry_count, timeout, printerrors,
945 errx(1, "Unable to determine device type");
947 if ((devtype == CC_DT_ATA)
948 || (devtype == CC_DT_ATA_BEHIND_SCSI)) {
949 ccb = cam_getccb(device);
951 warnx("couldn't allocate CCB");
956 if (ata_do_identify(device, retry_count, timeout, ccb,
961 } else if (devtype != CC_DT_SCSI)
962 errx(1, "Unsupported device type %d", devtype);
964 vp = fw_get_vendor(device, ident_buf);
966 * Bail out if we have an unknown vendor and this isn't an ATA
967 * disk. For a SCSI disk, we have no chance of working properly
968 * with the default values in the VENDOR_UNKNOWN case. For an ATA
969 * disk connected via an ATA transport, we may work for drives that
970 * support the ATA_DOWNLOAD_MICROCODE command.
973 || (vp->type == VENDOR_UNKNOWN))
974 && (devtype == CC_DT_SCSI))
975 errx(1, "Unsupported device");
977 retval = fw_get_timeout(device, vp, task_attr, retry_count, timeout);
979 warnx("Unable to get a firmware download timeout value");
983 buf = fw_read_img(device, retry_count, timeout, quiet, fw_img_path,
991 fprintf(stdout, "You are about to download firmware image (%s)"
992 " into the following device:\n",
994 if (devtype == CC_DT_SCSI) {
995 if (scsidoinquiry(device, argc, argv, combinedopt,
996 MSG_SIMPLE_Q_TAG, 0, 5000) != 0) {
997 warnx("Error sending inquiry");
1002 printf("%s%d: ", device->device_name,
1003 device->dev_unit_num);
1004 ata_print_ident(ident_buf);
1005 camxferrate(device);
1008 fprintf(stdout, "Using a timeout of %u ms, which is %s.\n",
1010 fw_timeout_desc_table[vp->timeout_type].timeout_desc);
1011 fprintf(stdout, "\nIt may damage your drive. ");
1012 if (!get_confirmation()) {
1017 if ((sim_mode != 0) && (quiet == 0))
1018 fprintf(stdout, "Running in simulation mode\n");
1020 if (fw_download_img(device, vp, buf, img_size, sim_mode, printerrors,
1021 quiet, retry_count, vp->timeout_ms, fw_img_path, devtype) != 0) {
1022 fprintf(stderr, "Firmware download failed\n");
1025 } else if (quiet == 0)
1026 fprintf(stdout, "Firmware download successful\n");