#include <sys/types.h>
#include <sys/stat.h>
#include <err.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <cam/cam.h>
#include <cam/scsi/scsi_all.h>
#include <cam/scsi/scsi_pass.h>
#include <cam/scsi/scsi_message.h>
#include <camlib.h>
#include "progress.h"
#include "camcontrol.h"
#define WB_TIMEOUT 50000
typedef enum {
VENDOR_HGST,
VENDOR_HITACHI,
VENDOR_HP,
VENDOR_IBM,
VENDOR_PLEXTOR,
VENDOR_QUALSTAR,
VENDOR_QUANTUM,
VENDOR_SAMSUNG,
VENDOR_SEAGATE,
VENDOR_SMART,
VENDOR_TOSHIBA,
VENDOR_ATA,
VENDOR_UNKNOWN
} fw_vendor_t;
typedef enum {
FW_TUR_NONE,
FW_TUR_READY,
FW_TUR_NOT_READY,
FW_TUR_NA
} fw_tur_status;
typedef enum {
FW_TIMEOUT_DEFAULT,
FW_TIMEOUT_DEV_REPORTED,
FW_TIMEOUT_NO_PROBE,
FW_TIMEOUT_USER_SPEC
} fw_timeout_type;
struct fw_vendor {
fw_vendor_t type;
const char *pattern;
int dev_type;
int max_pkt_size;
uint8_t cdb_byte2;
uint8_t cdb_byte2_last;
int inc_cdb_buffer_id;
int inc_cdb_offset;
fw_tur_status tur_status;
int timeout_ms;
fw_timeout_type timeout_type;
};
static struct fw_vendor vendors_list[] = {
{VENDOR_HGST, "HGST", T_DIRECT,
0x1000, 0x07, 0x07, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
{VENDOR_HITACHI, "HITACHI", T_ANY,
0x8000, 0x05, 0x05, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
{VENDOR_HP, "HP", T_ANY,
0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
{VENDOR_IBM, "IBM", T_SEQUENTIAL,
0x8000, 0x07, 0x07, 0, 1, FW_TUR_NA, 300 * 1000, FW_TIMEOUT_DEFAULT},
{VENDOR_IBM, "IBM", T_ANY,
0x8000, 0x05, 0x05, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
{VENDOR_PLEXTOR, "PLEXTOR", T_ANY,
0x2000, 0x04, 0x05, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
{VENDOR_QUALSTAR, "QUALSTAR", T_ANY,
0x2030, 0x05, 0x05, 0, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
{VENDOR_QUANTUM, "QUANTUM", T_ANY,
0x2000, 0x04, 0x05, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
{VENDOR_SAMSUNG, "SAMSUNG", T_ANY,
0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
{VENDOR_SEAGATE, "SEAGATE", T_ANY,
0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
{VENDOR_SMART, "SmrtStor", T_DIRECT,
0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
{VENDOR_TOSHIBA, "TOSHIBA", T_DIRECT,
0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
{VENDOR_HGST, "WD", T_DIRECT,
0x1000, 0x07, 0x07, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
{VENDOR_HGST, "WDC", T_DIRECT,
0x1000, 0x07, 0x07, 1, 0, FW_TUR_READY, WB_TIMEOUT, FW_TIMEOUT_DEFAULT},
{VENDOR_ATA, "ATA", T_ANY,
0x8000, 0x07, 0x07, 0, 1, FW_TUR_READY, WB_TIMEOUT,
FW_TIMEOUT_NO_PROBE},
{VENDOR_UNKNOWN, NULL, T_ANY,
0x0000, 0x00, 0x00, 0, 0, FW_TUR_NONE, WB_TIMEOUT, FW_TIMEOUT_DEFAULT}
};
struct fw_timeout_desc {
fw_timeout_type timeout_type;
const char *timeout_desc;
};
static const struct fw_timeout_desc fw_timeout_desc_table[] = {
{ FW_TIMEOUT_DEFAULT, "the default" },
{ FW_TIMEOUT_DEV_REPORTED, "recommended by this particular device" },
{ FW_TIMEOUT_NO_PROBE, "the default" },
{ FW_TIMEOUT_USER_SPEC, "what was specified on the command line" }
};
#ifndef ATA_DOWNLOAD_MICROCODE
#define ATA_DOWNLOAD_MICROCODE 0x92
#endif
#define USE_OFFSETS_FEATURE 0x3
#ifndef LOW_SECTOR_SIZE
#define LOW_SECTOR_SIZE 512
#endif
#define ATA_MAKE_LBA(o, p) \
((((((o) / LOW_SECTOR_SIZE) >> 8) & 0xff) << 16) | \
((((o) / LOW_SECTOR_SIZE) & 0xff) << 8) | \
((((p) / LOW_SECTOR_SIZE) >> 8) & 0xff))
#define ATA_MAKE_SECTORS(p) (((p) / 512) & 0xff)
#ifndef UNKNOWN_MAX_PKT_SIZE
#define UNKNOWN_MAX_PKT_SIZE 0x8000
#endif
static struct fw_vendor *fw_get_vendor(struct cam_device *cam_dev,
struct ata_params *ident_buf);
static int fw_get_timeout(struct cam_device *cam_dev, struct fw_vendor *vp,
int task_attr, int retry_count, int timeout);
static int fw_validate_ibm(struct cam_device *dev, int retry_count,
int timeout, int fd, char *buf,
const char *fw_img_path, int quiet);
static char *fw_read_img(struct cam_device *dev, int retry_count,
int timeout, int quiet, const char *fw_img_path,
struct fw_vendor *vp, int *num_bytes);
static int fw_check_device_ready(struct cam_device *dev,
camcontrol_devtype devtype,
struct fw_vendor *vp, int printerrors,
int timeout);
static int fw_download_img(struct cam_device *cam_dev,
struct fw_vendor *vp, char *buf, int img_size,
int sim_mode, int printerrors, int quiet,
int retry_count, int timeout, const char *,
camcontrol_devtype devtype);
static struct fw_vendor *
fw_get_vendor(struct cam_device *cam_dev, struct ata_params *ident_buf)
{
char vendor[42];
struct fw_vendor *vp;
if (cam_dev == NULL)
return (NULL);
if (ident_buf != NULL) {
cam_strvis((u_char *)vendor, ident_buf->model,
sizeof(ident_buf->model), sizeof(vendor));
for (vp = vendors_list; vp->pattern != NULL; vp++) {
if (vp->type == VENDOR_ATA)
return (vp);
}
} else {
cam_strvis((u_char *)vendor, (u_char *)cam_dev->inq_data.vendor,
sizeof(cam_dev->inq_data.vendor), sizeof(vendor));
}
for (vp = vendors_list; vp->pattern != NULL; vp++) {
if (!cam_strmatch((const u_char *)vendor,
(const u_char *)vp->pattern, strlen(vendor))) {
if ((vp->dev_type == T_ANY)
|| (vp->dev_type == SID_TYPE(&cam_dev->inq_data)))
break;
}
}
return (vp);
}
static int
fw_get_timeout(struct cam_device *cam_dev, struct fw_vendor *vp,
int task_attr, int retry_count, int timeout)
{
struct scsi_report_supported_opcodes_one *one;
struct scsi_report_supported_opcodes_timeout *td;
uint8_t *buf = NULL;
uint32_t fill_len = 0, cdb_len = 0, rec_timeout = 0;
int retval = 0;
if (timeout != 0) {
vp->timeout_type = FW_TIMEOUT_USER_SPEC;
vp->timeout_ms = timeout;
goto bailout;
}
if (vp->timeout_type == FW_TIMEOUT_NO_PROBE)
goto bailout;
retval = scsigetopcodes( cam_dev,
1,
WRITE_BUFFER,
1,
0,
0,
1,
task_attr,
retry_count,
10000,
0,
&fill_len,
&buf);
if (retval != 0) {
retval = 0;
goto bailout;
}
if (fill_len < (sizeof(*one) + sizeof(struct scsi_write_buffer) +
sizeof(*td)))
goto bailout;
one = (struct scsi_report_supported_opcodes_one *)buf;
if ((one->support & RSO_ONE_SUP_MASK) == RSO_ONE_SUP_NOT_SUP)
goto bailout;
cdb_len = scsi_2btoul(one->cdb_length);
td = (struct scsi_report_supported_opcodes_timeout *)
&buf[sizeof(*one) + cdb_len];
rec_timeout = scsi_4btoul(td->recommended_time);
if (rec_timeout == 0)
goto bailout;
rec_timeout *= 1000;
vp->timeout_ms = rec_timeout;
vp->timeout_type = FW_TIMEOUT_DEV_REPORTED;
bailout:
return (retval);
}
#define SVPD_IBM_FW_DESIGNATION 0x03
struct fw_ibm_tape_fw_designation {
uint8_t device;
uint8_t page_code;
uint8_t reserved;
uint8_t length;
uint8_t ascii_length;
uint8_t reserved2[3];
uint8_t load_id[4];
uint8_t fw_rev[4];
uint8_t ptf_number[4];
uint8_t patch_number[4];
uint8_t ru_name[8];
uint8_t lib_seq_num[5];
};
struct fw_ibm_tape_fw_header {
uint8_t unspec[4];
uint8_t length[4];
uint8_t load_id[4];
uint8_t fw_rev[4];
uint8_t reserved[8];
uint8_t ru_name[8];
};
static int
fw_validate_ibm(struct cam_device *dev, int retry_count, int timeout, int fd,
char *buf, const char *fw_img_path, int quiet)
{
union ccb *ccb;
struct fw_ibm_tape_fw_designation vpd_page;
struct fw_ibm_tape_fw_header *header;
char drive_rev[sizeof(vpd_page.fw_rev) + 1];
char file_rev[sizeof(vpd_page.fw_rev) + 1];
int retval = 1;
ccb = cam_getccb(dev);
if (ccb == NULL) {
warnx("couldn't allocate CCB");
goto bailout;
}
bzero(&vpd_page, sizeof(vpd_page));
scsi_inquiry(&ccb->csio,
retry_count,
NULL,
MSG_SIMPLE_Q_TAG,
(uint8_t *)&vpd_page,
sizeof(vpd_page),
1,
SVPD_IBM_FW_DESIGNATION,
SSD_FULL_SIZE,
timeout ? timeout : 5000);
ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
if (retry_count != 0)
ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
if (cam_send_ccb(dev, ccb) < 0) {
warn("error getting firmware designation page");
cam_error_print(dev, ccb, CAM_ESF_ALL,
CAM_EPF_ALL, stderr);
cam_freeccb(ccb);
ccb = NULL;
goto bailout;
}
if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
cam_error_print(dev, ccb, CAM_ESF_ALL,
CAM_EPF_ALL, stderr);
goto bailout;
}
if (read(fd, buf, sizeof(*header)) != sizeof(*header)) {
warn("unable to read %zu bytes from %s", sizeof(*header),
fw_img_path);
goto bailout;
}
if (lseek(fd, 0, SEEK_SET) == -1) {
warn("Unable to lseek");
goto bailout;
}
header = (struct fw_ibm_tape_fw_header *)buf;
bzero(drive_rev, sizeof(drive_rev));
bcopy(vpd_page.fw_rev, drive_rev, sizeof(vpd_page.fw_rev));
bzero(file_rev, sizeof(file_rev));
bcopy(header->fw_rev, file_rev, sizeof(header->fw_rev));
if (quiet == 0) {
fprintf(stdout, "Current Drive Firmware version: %s\n",
drive_rev);
fprintf(stdout, "Firmware File version: %s\n", file_rev);
}
if (bcmp(vpd_page.load_id, header->load_id,
MIN(sizeof(vpd_page.load_id), sizeof(header->load_id))) != 0) {
warnx("Drive Firmware load ID 0x%x does not match firmware "
"file load ID 0x%x", scsi_4btoul(vpd_page.load_id),
scsi_4btoul(header->load_id));
goto bailout;
}
if (bcmp(vpd_page.ru_name, header->ru_name,
MIN(sizeof(vpd_page.ru_name), sizeof(header->ru_name))) != 0) {
warnx("Drive Firmware RU name 0x%jx does not match firmware "
"file RU name 0x%jx",
(uintmax_t)scsi_8btou64(vpd_page.ru_name),
(uintmax_t)scsi_8btou64(header->ru_name));
goto bailout;
}
if (quiet == 0)
fprintf(stdout, "Firmware file is valid for this drive.\n");
retval = 0;
bailout:
cam_freeccb(ccb);
return (retval);
}
static char *
fw_read_img(struct cam_device *dev, int retry_count, int timeout, int quiet,
const char *fw_img_path, struct fw_vendor *vp, int *num_bytes)
{
int fd;
struct stat stbuf;
char *buf;
off_t img_size;
int skip_bytes = 0;
if ((fd = open(fw_img_path, O_RDONLY)) < 0) {
warn("Could not open image file %s", fw_img_path);
return (NULL);
}
if (fstat(fd, &stbuf) < 0) {
warn("Could not stat image file %s", fw_img_path);
goto bailout1;
}
if ((img_size = stbuf.st_size) == 0) {
warnx("Zero length image file %s", fw_img_path);
goto bailout1;
}
if ((buf = malloc(img_size)) == NULL) {
warnx("Could not allocate buffer to read image file %s",
fw_img_path);
goto bailout1;
}
switch (vp->type) {
case VENDOR_SEAGATE:
if (read(fd, buf, 16) != 16) {
warn("Could not read image file %s", fw_img_path);
goto bailout;
}
if (lseek(fd, 0, SEEK_SET) == -1) {
warn("Unable to lseek");
goto bailout;
}
if ((strncmp(buf, "SEAGATE,SEAGATE ", 16) == 0) ||
(img_size % 512 == 80))
skip_bytes = 80;
break;
case VENDOR_QUALSTAR:
skip_bytes = img_size % 1030;
break;
case VENDOR_IBM: {
if (vp->dev_type != T_SEQUENTIAL)
break;
if (fw_validate_ibm(dev, retry_count, timeout, fd, buf,
fw_img_path, quiet) != 0)
goto bailout;
break;
}
default:
break;
}
if (skip_bytes != 0) {
fprintf(stdout, "Skipping %d byte header.\n", skip_bytes);
if (lseek(fd, skip_bytes, SEEK_SET) == -1) {
warn("Could not lseek");
goto bailout;
}
img_size -= skip_bytes;
}
if (read(fd, buf, img_size) != img_size) {
warn("Could not read image file %s", fw_img_path);
goto bailout;
}
*num_bytes = img_size;
close(fd);
return (buf);
bailout:
free(buf);
bailout1:
close(fd);
*num_bytes = 0;
return (NULL);
}
static int
fw_check_device_ready(struct cam_device *dev, camcontrol_devtype devtype,
struct fw_vendor *vp, int printerrors, int timeout)
{
union ccb *ccb;
int retval = 0;
int16_t *ptr = NULL;
size_t dxfer_len = 0;
if ((ccb = cam_getccb(dev)) == NULL) {
warnx("Could not allocate CCB");
retval = -1;
goto bailout;
}
if (devtype != CC_DT_SCSI) {
dxfer_len = sizeof(struct ata_params);
ptr = (uint16_t *)malloc(dxfer_len);
if (ptr == NULL) {
warnx("can't malloc memory for identify");
retval = -1;
goto bailout;
}
bzero(ptr, dxfer_len);
}
switch (devtype) {
case CC_DT_SCSI:
scsi_test_unit_ready(&ccb->csio,
0,
NULL,
MSG_SIMPLE_Q_TAG,
SSD_FULL_SIZE,
5000);
break;
case CC_DT_SATL:
case CC_DT_ATA: {
retval = build_ata_cmd(ccb,
1,
CAM_DIR_IN,
MSG_SIMPLE_Q_TAG,
AP_PROTO_PIO_IN,
AP_FLAG_BYT_BLOK_BLOCKS |
AP_FLAG_TLEN_SECT_CNT |
AP_FLAG_TDIR_FROM_DEV,
0,
dxfer_len / 512,
0,
ATA_ATA_IDENTIFY,
0,
(uint8_t *)ptr,
dxfer_len,
NULL,
0,
SSD_FULL_SIZE,
timeout ? timeout : 30 * 1000,
0,
devtype);
if (retval != 0) {
retval = -1;
warnx("%s: build_ata_cmd() failed, likely "
"programmer error", __func__);
goto bailout;
}
break;
}
default:
warnx("Unknown disk type %d", devtype);
retval = -1;
goto bailout;
break;
}
ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
retval = cam_send_ccb(dev, ccb);
if (retval != 0) {
warn("error sending %s CCB", (devtype == CC_DT_SCSI) ?
"Test Unit Ready" : "Identify");
retval = -1;
goto bailout;
}
if (((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
&& (vp->tur_status == FW_TUR_READY)) {
warnx("Device is not ready");
if (printerrors)
cam_error_print(dev, ccb, CAM_ESF_ALL,
CAM_EPF_ALL, stderr);
retval = 1;
goto bailout;
} else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
&& (vp->tur_status == FW_TUR_NOT_READY)) {
warnx("Device cannot have media loaded when firmware is "
"downloaded");
retval = 1;
goto bailout;
}
bailout:
free(ptr);
cam_freeccb(ccb);
return (retval);
}
static int
fw_rescan_target(struct cam_device *dev, bool printerrors, bool sim_mode)
{
union ccb ccb;
int fd;
printf("Rescanning target %d:%d:* to pick up new fw revision / parameters.\n",
dev->path_id, dev->target_id);
if (sim_mode)
return (0);
if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
warnx("error opening transport layer device %s\n",
XPT_DEVICE);
warn("%s", XPT_DEVICE);
return (1);
}
bzero(&ccb, sizeof(union ccb));
ccb.ccb_h.func_code = XPT_SCAN_TGT;
ccb.ccb_h.path_id = dev->path_id;
ccb.ccb_h.target_id = dev->target_id;
ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
ccb.crcn.flags = CAM_EXPECT_INQ_CHANGE;
ccb.ccb_h.pinfo.priority = 5;
if (ioctl(fd, CAMIOCOMMAND, &ccb) < 0) {
warn("CAMIOCOMMAND XPT_SCAN_TGT ioctl failed");
close(fd);
return (1);
}
if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
warn("Can't send rescan lun");
if (printerrors)
cam_error_print(dev, &ccb, CAM_ESF_ALL, CAM_EPF_ALL,
stderr);
close(fd);
return (1);
}
close(fd);
return (0);
}
static int
fw_download_img(struct cam_device *cam_dev, struct fw_vendor *vp,
char *buf, int img_size, int sim_mode, int printerrors, int quiet,
int retry_count, int timeout, const char *imgname,
camcontrol_devtype devtype)
{
struct scsi_write_buffer cdb;
progress_t progress;
int size = 0;
union ccb *ccb = NULL;
int pkt_count = 0;
int max_pkt_size;
uint32_t pkt_size = 0;
char *pkt_ptr = buf;
uint32_t offset;
int last_pkt = 0;
int retval = 0;
retval = fw_check_device_ready(cam_dev, devtype, vp, printerrors,
timeout);
if (retval != 0)
goto bailout;
if ((ccb = cam_getccb(cam_dev)) == NULL) {
warnx("Could not allocate CCB");
retval = 1;
goto bailout;
}
max_pkt_size = vp->max_pkt_size;
if (max_pkt_size == 0)
max_pkt_size = UNKNOWN_MAX_PKT_SIZE;
pkt_size = max_pkt_size;
progress_init(&progress, imgname, size = img_size);
do {
if (img_size <= max_pkt_size) {
last_pkt = 1;
pkt_size = img_size;
}
progress_update(&progress, size - img_size);
if (((sim_mode == 0) && (quiet == 0))
|| ((sim_mode != 0) && (printerrors == 0)))
progress_draw(&progress);
bzero(&cdb, sizeof(cdb));
switch (devtype) {
case CC_DT_SCSI:
cdb.opcode = WRITE_BUFFER;
cdb.control = 0;
scsi_ulto3b(pkt_size, &cdb.length[0]);
offset = vp->inc_cdb_offset ? (pkt_ptr - buf) : 0;
scsi_ulto3b(offset, &cdb.offset[0]);
cdb.byte2 = last_pkt ? vp->cdb_byte2_last :
vp->cdb_byte2;
cdb.buffer_id = vp->inc_cdb_buffer_id ? pkt_count : 0;
CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
bcopy(&cdb, &ccb->csio.cdb_io.cdb_bytes[0],
sizeof(struct scsi_write_buffer));
cam_fill_csio(&ccb->csio,
retry_count,
NULL,
CAM_DIR_OUT | CAM_DEV_QFRZDIS,
CAM_TAG_ACTION_NONE,
(u_char *)pkt_ptr,
pkt_size,
SSD_FULL_SIZE,
sizeof(struct scsi_write_buffer),
timeout ? timeout : WB_TIMEOUT);
break;
case CC_DT_ATA:
case CC_DT_SATL: {
uint32_t off;
off = (uint32_t)(pkt_ptr - buf);
retval = build_ata_cmd(ccb,
retry_count,
CAM_DIR_OUT | CAM_DEV_QFRZDIS,
CAM_TAG_ACTION_NONE,
AP_PROTO_PIO_OUT,
AP_FLAG_BYT_BLOK_BYTES |
AP_FLAG_TLEN_SECT_CNT |
AP_FLAG_TDIR_TO_DEV,
USE_OFFSETS_FEATURE,
ATA_MAKE_SECTORS(pkt_size),
ATA_MAKE_LBA(off, pkt_size),
ATA_DOWNLOAD_MICROCODE,
0,
(uint8_t *)pkt_ptr,
pkt_size,
NULL,
0,
SSD_FULL_SIZE,
timeout ? timeout : WB_TIMEOUT,
0,
devtype);
if (retval != 0) {
warnx("%s: build_ata_cmd() failed, likely "
"programmer error", __func__);
goto bailout;
}
break;
}
default:
warnx("Unknown device type %d", devtype);
retval = 1;
goto bailout;
break;
}
if (!sim_mode) {
if (cam_send_ccb(cam_dev, ccb) < 0 ||
(ccb->ccb_h.status & CAM_STATUS_MASK) !=
CAM_REQ_CMP) {
warnx("Error writing image to device");
if (printerrors)
cam_error_print(cam_dev, ccb,
CAM_ESF_ALL, CAM_EPF_ALL, stderr);
retval = 1;
goto bailout;
}
} else if (printerrors) {
cam_error_print(cam_dev, ccb, CAM_ESF_COMMAND, 0,
stdout);
}
pkt_count++;
pkt_ptr += pkt_size;
img_size -= pkt_size;
} while(!last_pkt);
bailout:
if (quiet == 0)
progress_complete(&progress, size - img_size);
cam_freeccb(ccb);
if (retval == 0) {
fw_rescan_target(cam_dev, printerrors, sim_mode);
}
return (retval);
}
int
fwdownload(struct cam_device *device, int argc, char **argv,
char *combinedopt, int printerrors, int task_attr, int retry_count,
int timeout)
{
union ccb *ccb = NULL;
struct fw_vendor *vp;
char *fw_img_path = NULL;
struct ata_params *ident_buf = NULL;
camcontrol_devtype devtype;
char *buf = NULL;
int img_size;
int c;
int sim_mode = 0;
int confirmed = 0;
int quiet = 0;
int retval = 0;
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'f':
fw_img_path = optarg;
break;
case 'q':
quiet = 1;
break;
case 's':
sim_mode = 1;
break;
case 'y':
confirmed = 1;
break;
default:
break;
}
}
if (fw_img_path == NULL)
errx(1, "you must specify a firmware image file using -f "
"option");
retval = get_device_type(device, retry_count, timeout, printerrors,
&devtype);
if (retval != 0)
errx(1, "Unable to determine device type");
if ((devtype == CC_DT_ATA)
|| (devtype == CC_DT_SATL)) {
ccb = cam_getccb(device);
if (ccb == NULL) {
warnx("couldn't allocate CCB");
retval = 1;
goto bailout;
}
if (ata_do_identify(device, retry_count, timeout, ccb,
&ident_buf) != 0) {
retval = 1;
goto bailout;
}
} else if (devtype != CC_DT_SCSI)
errx(1, "Unsupported device type %d", devtype);
vp = fw_get_vendor(device, ident_buf);
if (((vp == NULL)
|| (vp->type == VENDOR_UNKNOWN))
&& (devtype == CC_DT_SCSI))
errx(1, "Unsupported device");
retval = fw_get_timeout(device, vp, task_attr, retry_count, timeout);
if (retval != 0) {
warnx("Unable to get a firmware download timeout value");
goto bailout;
}
buf = fw_read_img(device, retry_count, timeout, quiet, fw_img_path,
vp, &img_size);
if (buf == NULL) {
retval = 1;
goto bailout;
}
if (!confirmed) {
fprintf(stdout, "You are about to download firmware image (%s)"
" into the following device:\n",
fw_img_path);
if (devtype == CC_DT_SCSI) {
if (scsidoinquiry(device, argc, argv, combinedopt,
MSG_SIMPLE_Q_TAG, 0, 5000) != 0) {
warnx("Error sending inquiry");
retval = 1;
goto bailout;
}
} else {
printf("%s%d: ", device->device_name,
device->dev_unit_num);
ata_print_ident(ident_buf);
camxferrate(device);
free(ident_buf);
}
fprintf(stdout, "Using a timeout of %u ms, which is %s.\n",
vp->timeout_ms,
fw_timeout_desc_table[vp->timeout_type].timeout_desc);
fprintf(stdout, "\nIt may damage your drive. ");
if (!get_confirmation()) {
retval = 1;
goto bailout;
}
}
if ((sim_mode != 0) && (quiet == 0))
fprintf(stdout, "Running in simulation mode\n");
if (fw_download_img(device, vp, buf, img_size, sim_mode, printerrors,
quiet, retry_count, vp->timeout_ms, fw_img_path, devtype) != 0) {
fprintf(stderr, "Firmware download failed\n");
retval = 1;
goto bailout;
} else if (quiet == 0)
fprintf(stdout, "Firmware download successful\n");
bailout:
cam_freeccb(ccb);
free(buf);
return (retval);
}