2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 Sandvine Incorporated. All rights reserved.
5 * Copyright (c) 2002-2011 Andre Albsmeier <andre@albsmeier.net>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * This software is derived from Andre Albsmeier's fwprog.c which contained
34 * Many thanks goes to Marc Frajola <marc@terasolutions.com> from
35 * TeraSolutions for the initial idea and his programme for upgrading
36 * the firmware of I*M DDYS drives.
42 * The fact that you see your favorite vendor listed below does not
43 * imply that your equipment won't break when you use this software
44 * with it. It only means that the firmware of at least one device type
45 * of each vendor listed has been programmed successfully using this code.
47 * The -s option simulates a download but does nothing apart from that.
48 * It can be used to check what chunk sizes would have been used with the
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
55 #include <sys/types.h>
65 #include <cam/scsi/scsi_all.h>
66 #include <cam/scsi/scsi_message.h>
71 #include "camcontrol.h"
73 #define WB_TIMEOUT 50000 /* 50 seconds */
91 * FW_TUR_READY: The drive must return good status for a test unit ready.
93 * FW_TUR_NOT_READY: The drive must return not ready status for a test unit
94 * ready. You may want this in a removable media drive.
96 * FW_TUR_NA: It doesn't matter whether the drive is ready or not.
97 * This may be the case for a removable media drive.
107 * FW_TIMEOUT_DEFAULT: Attempt to probe for a WRITE BUFFER timeout
108 * value from the drive. If we get an answer,
109 * use the Recommended timeout. Otherwise,
110 * use the default value from the table.
112 * FW_TIMEOUT_DEV_REPORTED: The timeout value was probed directly from
115 * FW_TIMEOUT_NO_PROBE: Do not ask the device for a WRITE BUFFER
116 * timeout value. Use the device-specific
119 * FW_TIMEOUT_USER_SPEC: The user specified a timeout on the command
120 * line with the -t option. This overrides any
121 * probe or default timeout.
125 FW_TIMEOUT_DEV_REPORTED,
131 * type: Enumeration for the particular vendor.
133 * pattern: Pattern to match for the Vendor ID from the SCSI
136 * dev_type: SCSI device type to match, or T_ANY to match any
137 * device from the given vendor. Note that if there
138 * is a specific device type listed for a particular
139 * vendor, it must be listed before a T_ANY entry.
141 * max_pkt_size: Maximum packet size when talking to a device. Note
142 * that although large data sizes may be supported by
143 * the target device, they may not be supported by the
144 * OS or the controller.
146 * cdb_byte2: This specifies byte 2 (byte 1 when counting from 0)
147 * of the CDB. This is generally the WRITE BUFFER mode.
149 * cdb_byte2_last: This specifies byte 2 for the last chunk of the
152 * inc_cdb_buffer_id: Increment the buffer ID by 1 for each chunk sent
155 * inc_cdb_offset: Increment the offset field in the CDB with the byte
156 * offset into the firmware file.
158 * tur_status: Pay attention to whether the device is ready before
159 * upgrading the firmware, or not. See above for the
168 u_int8_t cdb_byte2_last;
169 int inc_cdb_buffer_id;
171 fw_tur_status tur_status;
173 fw_timeout_type timeout_type;
179 * HGST: The packets need to be sent in multiples of 4K.
181 * IBM: For LTO and TS drives, the buffer ID is ignored in mode 7 (and
182 * some other modes). It treats the request as a firmware download.
183 * The offset (and therefore the length of each chunk sent) needs
184 * to be a multiple of the offset boundary specified for firmware
185 * (buffer ID 4) in the read buffer command. At least for LTO-6,
186 * that seems to be 0, but using a 32K chunk size should satisfy
187 * most any alignment requirement.
189 * SmrtStor: Mode 5 is also supported, but since the firmware is 400KB or
190 * so, we can't fit it in a single request in most cases.
192 static struct fw_vendor vendors_list[] = {
193 {VENDOR_HGST, "HGST", T_DIRECT,
194 0x1000, 0x07, 0x07, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
195 {VENDOR_HITACHI, "HITACHI", T_ANY,
196 0x8000, 0x05, 0x05, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
197 {VENDOR_HP, "HP", T_ANY,
198 0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
199 {VENDOR_IBM, "IBM", T_SEQUENTIAL,
200 0x8000, 0x07, 0x07, 0, 1, FW_TUR_NA, 300 * 1000, FW_TIMEOUT_DEFAULT},
201 {VENDOR_IBM, "IBM", T_ANY,
202 0x8000, 0x05, 0x05, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
203 {VENDOR_PLEXTOR, "PLEXTOR", T_ANY,
204 0x2000, 0x04, 0x05, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
205 {VENDOR_QUALSTAR, "QUALSTAR", T_ANY,
206 0x2030, 0x05, 0x05, 0, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
207 {VENDOR_QUANTUM, "QUANTUM", T_ANY,
208 0x2000, 0x04, 0x05, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
209 {VENDOR_SAMSUNG, "SAMSUNG", T_ANY,
210 0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
211 {VENDOR_SEAGATE, "SEAGATE", T_ANY,
212 0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
213 {VENDOR_SMART, "SmrtStor", T_DIRECT,
214 0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
215 {VENDOR_HGST, "WD", T_DIRECT,
216 0x1000, 0x07, 0x07, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
217 {VENDOR_HGST, "WDC", T_DIRECT,
218 0x1000, 0x07, 0x07, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
221 * We match any ATA device. This is really just a placeholder,
222 * since we won't actually send a WRITE BUFFER with any of the
223 * listed parameters. If a SATA device is behind a SAS controller,
224 * the SCSI to ATA translation code (at least for LSI) doesn't
225 * generally translate a SCSI WRITE BUFFER into an ATA DOWNLOAD
226 * MICROCODE command. So, we use the SCSI ATA PASS_THROUGH command
227 * to send the ATA DOWNLOAD MICROCODE command instead.
229 {VENDOR_ATA, "ATA", T_ANY,
230 0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT,
231 FW_TIMEOUT_NO_PROBE},
232 {VENDOR_UNKNOWN, NULL, T_ANY,
233 0x0000, 0x00, 0x00, 0, 0, FW_TUR_NONE, WB_TIMEOUT, FW_TIMEOUT_DEFAULT}
236 struct fw_timeout_desc {
237 fw_timeout_type timeout_type;
238 const char *timeout_desc;
241 static const struct fw_timeout_desc fw_timeout_desc_table[] = {
242 { FW_TIMEOUT_DEFAULT, "the default" },
243 { FW_TIMEOUT_DEV_REPORTED, "recommended by this particular device" },
244 { FW_TIMEOUT_NO_PROBE, "the default" },
245 { FW_TIMEOUT_USER_SPEC, "what was specified on the command line" }
248 #ifndef ATA_DOWNLOAD_MICROCODE
249 #define ATA_DOWNLOAD_MICROCODE 0x92
252 #define USE_OFFSETS_FEATURE 0x3
254 #ifndef LOW_SECTOR_SIZE
255 #define LOW_SECTOR_SIZE 512
258 #define ATA_MAKE_LBA(o, p) \
259 ((((((o) / LOW_SECTOR_SIZE) >> 8) & 0xff) << 16) | \
260 ((((o) / LOW_SECTOR_SIZE) & 0xff) << 8) | \
261 ((((p) / LOW_SECTOR_SIZE) >> 8) & 0xff))
263 #define ATA_MAKE_SECTORS(p) (((p) / 512) & 0xff)
265 #ifndef UNKNOWN_MAX_PKT_SIZE
266 #define UNKNOWN_MAX_PKT_SIZE 0x8000
269 static struct fw_vendor *fw_get_vendor(struct cam_device *cam_dev,
270 struct ata_params *ident_buf);
271 static int fw_get_timeout(struct cam_device *cam_dev, struct fw_vendor *vp,
272 int task_attr, int retry_count, int timeout);
273 static int fw_validate_ibm(struct cam_device *dev, int retry_count,
274 int timeout, int fd, char *buf,
275 const char *fw_img_path, int quiet);
276 static char *fw_read_img(struct cam_device *dev, int retry_count,
277 int timeout, int quiet, const char *fw_img_path,
278 struct fw_vendor *vp, int *num_bytes);
279 static int fw_check_device_ready(struct cam_device *dev,
280 camcontrol_devtype devtype,
281 struct fw_vendor *vp, int printerrors,
283 static int fw_download_img(struct cam_device *cam_dev,
284 struct fw_vendor *vp, char *buf, int img_size,
285 int sim_mode, int printerrors, int quiet,
286 int retry_count, int timeout, const char */*name*/,
287 camcontrol_devtype devtype);
290 * Find entry in vendors list that belongs to
291 * the vendor of given cam device.
293 static struct fw_vendor *
294 fw_get_vendor(struct cam_device *cam_dev, struct ata_params *ident_buf)
297 struct fw_vendor *vp;
302 if (ident_buf != NULL) {
303 cam_strvis((u_char *)vendor, ident_buf->model,
304 sizeof(ident_buf->model), sizeof(vendor));
305 for (vp = vendors_list; vp->pattern != NULL; vp++) {
306 if (vp->type == VENDOR_ATA)
310 cam_strvis((u_char *)vendor, (u_char *)cam_dev->inq_data.vendor,
311 sizeof(cam_dev->inq_data.vendor), sizeof(vendor));
313 for (vp = vendors_list; vp->pattern != NULL; vp++) {
314 if (!cam_strmatch((const u_char *)vendor,
315 (const u_char *)vp->pattern, strlen(vendor))) {
316 if ((vp->dev_type == T_ANY)
317 || (vp->dev_type == SID_TYPE(&cam_dev->inq_data)))
325 fw_get_timeout(struct cam_device *cam_dev, struct fw_vendor *vp,
326 int task_attr, int retry_count, int timeout)
328 struct scsi_report_supported_opcodes_one *one;
329 struct scsi_report_supported_opcodes_timeout *td;
331 uint32_t fill_len = 0, cdb_len = 0, rec_timeout = 0;
335 * If the user has specified a timeout on the command line, we let
336 * him override any default or probed value.
339 vp->timeout_type = FW_TIMEOUT_USER_SPEC;
340 vp->timeout_ms = timeout;
345 * Check to see whether we should probe for a timeout for this
348 if (vp->timeout_type == FW_TIMEOUT_NO_PROBE)
351 retval = scsigetopcodes(/*device*/ cam_dev,
353 /*opcode*/ WRITE_BUFFER,
354 /*show_sa_errors*/ 1,
356 /*service_action*/ 0,
358 /*task_attr*/ task_attr,
359 /*retry_count*/ retry_count,
362 /*fill_len*/ &fill_len,
365 * It isn't an error if we can't get a timeout descriptor. We just
366 * continue on with the default timeout.
374 * Even if the drive didn't return a SCSI error, if we don't have
375 * enough data to contain the one opcode descriptor, the CDB
376 * structure and a timeout descriptor, we don't have the timeout
377 * value we're looking for. So we'll just fall back to the
380 if (fill_len < (sizeof(*one) + sizeof(struct scsi_write_buffer) +
384 one = (struct scsi_report_supported_opcodes_one *)buf;
387 * If the drive claims to not support the WRITE BUFFER command...
388 * fall back to the default timeout value and let things fail on
389 * the actual firmware download.
391 if ((one->support & RSO_ONE_SUP_MASK) == RSO_ONE_SUP_NOT_SUP)
394 cdb_len = scsi_2btoul(one->cdb_length);
395 td = (struct scsi_report_supported_opcodes_timeout *)
396 &buf[sizeof(*one) + cdb_len];
398 rec_timeout = scsi_4btoul(td->recommended_time);
400 * If the recommended timeout is 0, then the device has probably
401 * returned a bogus value.
403 if (rec_timeout == 0)
406 /* CAM timeouts are in ms */
409 vp->timeout_ms = rec_timeout;
410 vp->timeout_type = FW_TIMEOUT_DEV_REPORTED;
416 #define SVPD_IBM_FW_DESIGNATION 0x03
419 * IBM LTO and TS tape drives have an INQUIRY VPD page 0x3 with the following
422 struct fw_ibm_tape_fw_designation {
427 uint8_t ascii_length;
428 uint8_t reserved2[3];
431 uint8_t ptf_number[4];
432 uint8_t patch_number[4];
434 uint8_t lib_seq_num[5];
438 * The firmware for IBM tape drives has the following header format. The
439 * load_id and ru_name in the header file should match what is returned in
442 struct fw_ibm_tape_fw_header {
444 uint8_t length[4]; /* Firmware and header! */
452 fw_validate_ibm(struct cam_device *dev, int retry_count, int timeout, int fd,
453 char *buf, const char *fw_img_path, int quiet)
456 struct fw_ibm_tape_fw_designation vpd_page;
457 struct fw_ibm_tape_fw_header *header;
458 char drive_rev[sizeof(vpd_page.fw_rev) + 1];
459 char file_rev[sizeof(vpd_page.fw_rev) + 1];
462 ccb = cam_getccb(dev);
464 warnx("couldn't allocate CCB");
468 /* cam_getccb cleans up the header, caller has to zero the payload */
469 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
471 bzero(&vpd_page, sizeof(vpd_page));
473 scsi_inquiry(&ccb->csio,
474 /*retries*/ retry_count,
476 /* tag_action */ MSG_SIMPLE_Q_TAG,
477 /* inq_buf */ (u_int8_t *)&vpd_page,
478 /* inq_len */ sizeof(vpd_page),
480 /* page_code */ SVPD_IBM_FW_DESIGNATION,
481 /* sense_len */ SSD_FULL_SIZE,
482 /* timeout */ timeout ? timeout : 5000);
484 /* Disable freezing the device queue */
485 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
487 if (retry_count != 0)
488 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
490 if (cam_send_ccb(dev, ccb) < 0) {
491 warn("error getting firmware designation page");
493 cam_error_print(dev, ccb, CAM_ESF_ALL,
494 CAM_EPF_ALL, stderr);
501 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
502 cam_error_print(dev, ccb, CAM_ESF_ALL,
503 CAM_EPF_ALL, stderr);
508 * Read the firmware header only.
510 if (read(fd, buf, sizeof(*header)) != sizeof(*header)) {
511 warn("unable to read %zu bytes from %s", sizeof(*header),
516 /* Rewind the file back to 0 for the full file read. */
517 if (lseek(fd, 0, SEEK_SET) == -1) {
518 warn("Unable to lseek");
522 header = (struct fw_ibm_tape_fw_header *)buf;
524 bzero(drive_rev, sizeof(drive_rev));
525 bcopy(vpd_page.fw_rev, drive_rev, sizeof(vpd_page.fw_rev));
526 bzero(file_rev, sizeof(file_rev));
527 bcopy(header->fw_rev, file_rev, sizeof(header->fw_rev));
530 fprintf(stdout, "Current Drive Firmware version: %s\n",
532 fprintf(stdout, "Firmware File version: %s\n", file_rev);
536 * For IBM tape drives the load ID and RU name reported by the
537 * drive should match what is in the firmware file.
539 if (bcmp(vpd_page.load_id, header->load_id,
540 MIN(sizeof(vpd_page.load_id), sizeof(header->load_id))) != 0) {
541 warnx("Drive Firmware load ID 0x%x does not match firmware "
542 "file load ID 0x%x", scsi_4btoul(vpd_page.load_id),
543 scsi_4btoul(header->load_id));
547 if (bcmp(vpd_page.ru_name, header->ru_name,
548 MIN(sizeof(vpd_page.ru_name), sizeof(header->ru_name))) != 0) {
549 warnx("Drive Firmware RU name 0x%jx does not match firmware "
550 "file RU name 0x%jx",
551 (uintmax_t)scsi_8btou64(vpd_page.ru_name),
552 (uintmax_t)scsi_8btou64(header->ru_name));
556 fprintf(stdout, "Firmware file is valid for this drive.\n");
565 * Allocate a buffer and read fw image file into it
566 * from given path. Number of bytes read is stored
570 fw_read_img(struct cam_device *dev, int retry_count, int timeout, int quiet,
571 const char *fw_img_path, struct fw_vendor *vp, int *num_bytes)
579 if ((fd = open(fw_img_path, O_RDONLY)) < 0) {
580 warn("Could not open image file %s", fw_img_path);
583 if (fstat(fd, &stbuf) < 0) {
584 warn("Could not stat image file %s", fw_img_path);
587 if ((img_size = stbuf.st_size) == 0) {
588 warnx("Zero length image file %s", fw_img_path);
591 if ((buf = malloc(img_size)) == NULL) {
592 warnx("Could not allocate buffer to read image file %s",
596 /* Skip headers if applicable. */
599 if (read(fd, buf, 16) != 16) {
600 warn("Could not read image file %s", fw_img_path);
603 if (lseek(fd, 0, SEEK_SET) == -1) {
604 warn("Unable to lseek");
607 if ((strncmp(buf, "SEAGATE,SEAGATE ", 16) == 0) ||
608 (img_size % 512 == 80))
611 case VENDOR_QUALSTAR:
612 skip_bytes = img_size % 1030;
615 if (vp->dev_type != T_SEQUENTIAL)
617 if (fw_validate_ibm(dev, retry_count, timeout, fd, buf,
618 fw_img_path, quiet) != 0)
625 if (skip_bytes != 0) {
626 fprintf(stdout, "Skipping %d byte header.\n", skip_bytes);
627 if (lseek(fd, skip_bytes, SEEK_SET) == -1) {
628 warn("Could not lseek");
631 img_size -= skip_bytes;
633 /* Read image into a buffer. */
634 if (read(fd, buf, img_size) != img_size) {
635 warn("Could not read image file %s", fw_img_path);
638 *num_bytes = img_size;
650 * Returns 0 for "success", where success means that the device has met the
651 * requirement in the vendor structure for being ready or not ready when
652 * firmware is downloaded.
654 * Returns 1 for a failure to be ready to accept a firmware download.
655 * (e.g., a drive needs to be ready, but returns not ready)
657 * Returns -1 for any other failure.
660 fw_check_device_ready(struct cam_device *dev, camcontrol_devtype devtype,
661 struct fw_vendor *vp, int printerrors, int timeout)
666 size_t dxfer_len = 0;
668 if ((ccb = cam_getccb(dev)) == NULL) {
669 warnx("Could not allocate CCB");
674 CCB_CLEAR_ALL_EXCEPT_HDR(ccb);
676 if (devtype != CC_DT_SCSI) {
677 dxfer_len = sizeof(struct ata_params);
679 ptr = (uint16_t *)malloc(dxfer_len);
681 warnx("can't malloc memory for identify");
685 bzero(ptr, dxfer_len);
690 scsi_test_unit_ready(&ccb->csio,
693 /*tag_action*/ MSG_SIMPLE_Q_TAG,
694 /*sense_len*/ SSD_FULL_SIZE,
699 retval = build_ata_cmd(ccb,
701 /*flags*/ CAM_DIR_IN,
702 /*tag_action*/ MSG_SIMPLE_Q_TAG,
703 /*protocol*/ AP_PROTO_PIO_IN,
704 /*ata_flags*/ AP_FLAG_BYT_BLOK_BLOCKS |
705 AP_FLAG_TLEN_SECT_CNT |
706 AP_FLAG_TDIR_FROM_DEV,
708 /*sector_count*/ dxfer_len / 512,
710 /*command*/ ATA_ATA_IDENTIFY,
712 /*data_ptr*/ (uint8_t *)ptr,
713 /*dxfer_len*/ dxfer_len,
714 /*cdb_storage*/ NULL,
715 /*cdb_storage_len*/ 0,
716 /*sense_len*/ SSD_FULL_SIZE,
717 /*timeout*/ timeout ? timeout : 30 * 1000,
719 /*devtype*/ devtype);
722 warnx("%s: build_ata_cmd() failed, likely "
723 "programmer error", __func__);
729 warnx("Unknown disk type %d", devtype);
732 break; /*NOTREACHED*/
735 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
737 retval = cam_send_ccb(dev, ccb);
739 warn("error sending %s CCB", (devtype == CC_DT_SCSI) ?
740 "Test Unit Ready" : "Identify");
745 if (((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
746 && (vp->tur_status == FW_TUR_READY)) {
747 warnx("Device is not ready");
749 cam_error_print(dev, ccb, CAM_ESF_ALL,
750 CAM_EPF_ALL, stderr);
753 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
754 && (vp->tur_status == FW_TUR_NOT_READY)) {
755 warnx("Device cannot have media loaded when firmware is "
768 * Download firmware stored in buf to cam_dev. If simulation mode
769 * is enabled, only show what packet sizes would be sent to the
770 * device but do not sent any actual packets
773 fw_download_img(struct cam_device *cam_dev, struct fw_vendor *vp,
774 char *buf, int img_size, int sim_mode, int printerrors, int quiet,
775 int retry_count, int timeout, const char *imgname,
776 camcontrol_devtype devtype)
778 struct scsi_write_buffer cdb;
781 union ccb *ccb = NULL;
784 u_int32_t pkt_size = 0;
791 * Check to see whether the device is ready to accept a firmware
794 retval = fw_check_device_ready(cam_dev, devtype, vp, printerrors,
799 if ((ccb = cam_getccb(cam_dev)) == NULL) {
800 warnx("Could not allocate CCB");
805 CCB_CLEAR_ALL_EXCEPT_HDR(ccb);
807 max_pkt_size = vp->max_pkt_size;
808 if (max_pkt_size == 0)
809 max_pkt_size = UNKNOWN_MAX_PKT_SIZE;
811 pkt_size = max_pkt_size;
812 progress_init(&progress, imgname, size = img_size);
813 /* Download single fw packets. */
815 if (img_size <= max_pkt_size) {
819 progress_update(&progress, size - img_size);
820 if (((sim_mode == 0) && (quiet == 0))
821 || ((sim_mode != 0) && (printerrors == 0)))
822 progress_draw(&progress);
823 bzero(&cdb, sizeof(cdb));
826 cdb.opcode = WRITE_BUFFER;
828 /* Parameter list length. */
829 scsi_ulto3b(pkt_size, &cdb.length[0]);
830 offset = vp->inc_cdb_offset ? (pkt_ptr - buf) : 0;
831 scsi_ulto3b(offset, &cdb.offset[0]);
832 cdb.byte2 = last_pkt ? vp->cdb_byte2_last :
834 cdb.buffer_id = vp->inc_cdb_buffer_id ? pkt_count : 0;
835 /* Zero out payload of ccb union after ccb header. */
836 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
838 * Copy previously constructed cdb into ccb_scsiio
841 bcopy(&cdb, &ccb->csio.cdb_io.cdb_bytes[0],
842 sizeof(struct scsi_write_buffer));
843 /* Fill rest of ccb_scsiio struct. */
844 cam_fill_csio(&ccb->csio, /* ccb_scsiio*/
845 retry_count, /* retries*/
847 CAM_DIR_OUT | CAM_DEV_QFRZDIS, /* flags*/
848 CAM_TAG_ACTION_NONE, /* tag_action*/
849 (u_char *)pkt_ptr, /* data_ptr*/
850 pkt_size, /* dxfer_len*/
851 SSD_FULL_SIZE, /* sense_len*/
852 sizeof(struct scsi_write_buffer), /* cdb_len*/
853 timeout ? timeout : WB_TIMEOUT); /* timeout*/
859 off = (uint32_t)(pkt_ptr - buf);
861 retval = build_ata_cmd(ccb,
862 /*retry_count*/ retry_count,
863 /*flags*/ CAM_DIR_OUT | CAM_DEV_QFRZDIS,
864 /*tag_action*/ CAM_TAG_ACTION_NONE,
865 /*protocol*/ AP_PROTO_PIO_OUT,
866 /*ata_flags*/ AP_FLAG_BYT_BLOK_BYTES |
867 AP_FLAG_TLEN_SECT_CNT |
869 /*features*/ USE_OFFSETS_FEATURE,
870 /*sector_count*/ ATA_MAKE_SECTORS(pkt_size),
871 /*lba*/ ATA_MAKE_LBA(off, pkt_size),
872 /*command*/ ATA_DOWNLOAD_MICROCODE,
874 /*data_ptr*/ (uint8_t *)pkt_ptr,
875 /*dxfer_len*/ pkt_size,
876 /*cdb_storage*/ NULL,
877 /*cdb_storage_len*/ 0,
878 /*sense_len*/ SSD_FULL_SIZE,
879 /*timeout*/ timeout ? timeout : WB_TIMEOUT,
881 /*devtype*/ devtype);
884 warnx("%s: build_ata_cmd() failed, likely "
885 "programmer error", __func__);
891 warnx("Unknown device type %d", devtype);
894 break; /*NOTREACHED*/
897 /* Execute the command. */
898 if (cam_send_ccb(cam_dev, ccb) < 0 ||
899 (ccb->ccb_h.status & CAM_STATUS_MASK) !=
901 warnx("Error writing image to device");
903 cam_error_print(cam_dev, ccb,
904 CAM_ESF_ALL, CAM_EPF_ALL, stderr);
908 } else if (printerrors) {
909 cam_error_print(cam_dev, ccb, CAM_ESF_COMMAND, 0,
913 /* Prepare next round. */
916 img_size -= pkt_size;
920 progress_complete(&progress, size - img_size);
926 fwdownload(struct cam_device *device, int argc, char **argv,
927 char *combinedopt, int printerrors, int task_attr, int retry_count,
930 union ccb *ccb = NULL;
931 struct fw_vendor *vp;
932 char *fw_img_path = NULL;
933 struct ata_params *ident_buf = NULL;
934 camcontrol_devtype devtype;
943 while ((c = getopt(argc, argv, combinedopt)) != -1) {
946 fw_img_path = optarg;
962 if (fw_img_path == NULL)
963 errx(1, "you must specify a firmware image file using -f "
966 retval = get_device_type(device, retry_count, timeout, printerrors,
969 errx(1, "Unable to determine device type");
971 if ((devtype == CC_DT_ATA)
972 || (devtype == CC_DT_SATL)) {
973 ccb = cam_getccb(device);
975 warnx("couldn't allocate CCB");
980 if (ata_do_identify(device, retry_count, timeout, ccb,
985 } else if (devtype != CC_DT_SCSI)
986 errx(1, "Unsupported device type %d", devtype);
988 vp = fw_get_vendor(device, ident_buf);
990 * Bail out if we have an unknown vendor and this isn't an ATA
991 * disk. For a SCSI disk, we have no chance of working properly
992 * with the default values in the VENDOR_UNKNOWN case. For an ATA
993 * disk connected via an ATA transport, we may work for drives that
994 * support the ATA_DOWNLOAD_MICROCODE command.
997 || (vp->type == VENDOR_UNKNOWN))
998 && (devtype == CC_DT_SCSI))
999 errx(1, "Unsupported device");
1001 retval = fw_get_timeout(device, vp, task_attr, retry_count, timeout);
1003 warnx("Unable to get a firmware download timeout value");
1007 buf = fw_read_img(device, retry_count, timeout, quiet, fw_img_path,
1015 fprintf(stdout, "You are about to download firmware image (%s)"
1016 " into the following device:\n",
1018 if (devtype == CC_DT_SCSI) {
1019 if (scsidoinquiry(device, argc, argv, combinedopt,
1020 MSG_SIMPLE_Q_TAG, 0, 5000) != 0) {
1021 warnx("Error sending inquiry");
1026 printf("%s%d: ", device->device_name,
1027 device->dev_unit_num);
1028 ata_print_ident(ident_buf);
1029 camxferrate(device);
1032 fprintf(stdout, "Using a timeout of %u ms, which is %s.\n",
1034 fw_timeout_desc_table[vp->timeout_type].timeout_desc);
1035 fprintf(stdout, "\nIt may damage your drive. ");
1036 if (!get_confirmation()) {
1041 if ((sim_mode != 0) && (quiet == 0))
1042 fprintf(stdout, "Running in simulation mode\n");
1044 if (fw_download_img(device, vp, buf, img_size, sim_mode, printerrors,
1045 quiet, retry_count, vp->timeout_ms, fw_img_path, devtype) != 0) {
1046 fprintf(stderr, "Firmware download failed\n");
1049 } else if (quiet == 0)
1050 fprintf(stdout, "Firmware download successful\n");