fwdownload.c revision 227961
1/*- 2 * Copyright (c) 2011 Sandvine Incorporated. All rights reserved. 3 * Copyright (c) 2002-2011 Andre Albsmeier <andre@albsmeier.net> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 15 * 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. 26 */ 27 28/* 29 * BEWARE: 30 * 31 * The fact that you see your favorite vendor listed below does not 32 * imply that your equipment won't break when you use this software 33 * with it. It only means that the firmware of at least one device type 34 * of each vendor listed has been programmed successfully using this code. 35 * 36 * The -s option simulates a download but does nothing apart from that. 37 * It can be used to check what chunk sizes would have been used with the 38 * specified device. 39 */ 40 41#include <sys/cdefs.h> 42__FBSDID("$FreeBSD: head/sbin/camcontrol/fwdownload.c 227961 2011-11-25 04:03:37Z emaste $"); 43 44#include <sys/types.h> 45#include <sys/stat.h> 46 47#include <err.h> 48#include <fcntl.h> 49#include <stdio.h> 50#include <stdlib.h> 51#include <string.h> 52#include <unistd.h> 53 54#include <cam/scsi/scsi_all.h> 55#include <cam/scsi/scsi_message.h> 56#include <camlib.h> 57 58#include "camcontrol.h" 59 60#define CMD_TIMEOUT 50000 /* 50 seconds */ 61 62typedef enum { 63 VENDOR_HITACHI, 64 VENDOR_HP, 65 VENDOR_IBM, 66 VENDOR_PLEXTOR, 67 VENDOR_QUANTUM, 68 VENDOR_SEAGATE, 69 VENDOR_UNKNOWN 70} fw_vendor_t; 71 72struct fw_vendor { 73 fw_vendor_t type; 74 const char *pattern; 75 int max_pkt_size; 76 u_int8_t cdb_byte2; 77 u_int8_t cdb_byte2_last; 78 int inc_cdb_buffer_id; 79 int inc_cdb_offset; 80}; 81 82struct fw_vendor vendors_list[] = { 83 {VENDOR_HITACHI, "HITACHI", 0x8000, 0x05, 0x05, 1, 0}, 84 {VENDOR_HP, "HP", 0x8000, 0x07, 0x07, 0, 1}, 85 {VENDOR_IBM, "IBM", 0x8000, 0x05, 0x05, 1, 0}, 86 {VENDOR_PLEXTOR, "PLEXTOR", 0x2000, 0x04, 0x05, 0, 1}, 87 {VENDOR_QUANTUM, "QUANTUM", 0x2000, 0x04, 0x05, 0, 1}, 88 {VENDOR_SEAGATE, "SEAGATE", 0x8000, 0x07, 0x07, 0, 1}, 89 {VENDOR_UNKNOWN, NULL, 0x0000, 0x00, 0x00, 0, 0} 90}; 91 92static struct fw_vendor *fw_get_vendor(struct cam_device *cam_dev); 93static char *fw_read_img(char *fw_img_path, struct fw_vendor *vp, 94 int *num_bytes); 95static int fw_download_img(struct cam_device *cam_dev, 96 struct fw_vendor *vp, char *buf, int img_size, 97 int sim_mode, int verbose, int retry_count, int timeout); 98 99/* 100 * Find entry in vendors list that belongs to 101 * the vendor of given cam device. 102 */ 103static struct fw_vendor * 104fw_get_vendor(struct cam_device *cam_dev) 105{ 106 char vendor[SID_VENDOR_SIZE + 1]; 107 struct fw_vendor *vp; 108 109 if (cam_dev == NULL) 110 return (NULL); 111 cam_strvis((u_char *)vendor, (u_char *)cam_dev->inq_data.vendor, 112 sizeof(cam_dev->inq_data.vendor), sizeof(vendor)); 113 for (vp = vendors_list; vp->pattern != NULL; vp++) { 114 if (!cam_strmatch((const u_char *)vendor, 115 (const u_char *)vp->pattern, strlen(vendor))) 116 break; 117 } 118 return (vp); 119} 120 121/* 122 * Allocate a buffer and read fw image file into it 123 * from given path. Number of bytes read is stored 124 * in num_bytes. 125 */ 126static char * 127fw_read_img(char *fw_img_path, struct fw_vendor *vp, int *num_bytes) 128{ 129 int fd; 130 struct stat stbuf; 131 char *buf; 132 off_t img_size; 133 int skip_bytes = 0; 134 135 if ((fd = open(fw_img_path, O_RDONLY)) < 0) { 136 warn("Could not open image file %s", fw_img_path); 137 return (NULL); 138 } 139 if (fstat(fd, &stbuf) < 0) { 140 warn("Could not stat image file %s", fw_img_path); 141 goto bailout1; 142 } 143 if ((img_size = stbuf.st_size) == 0) { 144 warnx("Zero length image file %s", fw_img_path); 145 goto bailout1; 146 } 147 if ((buf = malloc(img_size)) == NULL) { 148 warnx("Could not allocate buffer to read image file %s", 149 fw_img_path); 150 goto bailout1; 151 } 152 /* Skip headers if applicable. */ 153 switch (vp->type) { 154 case VENDOR_SEAGATE: 155 if (read(fd, buf, 16) != 16) { 156 warn("Could not read image file %s", fw_img_path); 157 goto bailout; 158 } 159 if (lseek(fd, 0, SEEK_SET) == -1) { 160 warn("Unable to lseek"); 161 goto bailout; 162 } 163 if ((strncmp(buf, "SEAGATE,SEAGATE ", 16) == 0) || 164 (img_size % 512 == 80)) 165 skip_bytes = 80; 166 break; 167 default: 168 break; 169 } 170 if (skip_bytes != 0) { 171 fprintf(stdout, "Skipping %d byte header.\n", skip_bytes); 172 if (lseek(fd, skip_bytes, SEEK_SET) == -1) { 173 warn("Could not lseek"); 174 goto bailout; 175 } 176 img_size -= skip_bytes; 177 } 178 /* Read image into a buffer. */ 179 if (read(fd, buf, img_size) != img_size) { 180 warn("Could not read image file %s", fw_img_path); 181 goto bailout; 182 } 183 *num_bytes = img_size; 184 return (buf); 185bailout: 186 free(buf); 187bailout1: 188 close(fd); 189 *num_bytes = 0; 190 return (NULL); 191} 192 193/* 194 * Download firmware stored in buf to cam_dev. If simulation mode 195 * is enabled, only show what packet sizes would be sent to the 196 * device but do not sent any actual packets 197 */ 198static int 199fw_download_img(struct cam_device *cam_dev, struct fw_vendor *vp, 200 char *buf, int img_size, int sim_mode, int verbose, int retry_count, 201 int timeout) 202{ 203 struct scsi_write_buffer cdb; 204 union ccb *ccb; 205 int pkt_count = 0; 206 u_int32_t pkt_size = 0; 207 char *pkt_ptr = buf; 208 u_int32_t offset; 209 int last_pkt = 0; 210 211 if ((ccb = cam_getccb(cam_dev)) == NULL) { 212 warnx("Could not allocate CCB"); 213 return (1); 214 } 215 scsi_test_unit_ready(&ccb->csio, 0, NULL, MSG_SIMPLE_Q_TAG, 216 SSD_FULL_SIZE, 5000); 217 /* Disable freezing the device queue. */ 218 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 219 if (cam_send_ccb(cam_dev, ccb) < 0) { 220 warnx("Error sending test unit ready"); 221 if (verbose) 222 cam_error_print(cam_dev, ccb, CAM_ESF_ALL, 223 CAM_EPF_ALL, stderr); 224 cam_freeccb(ccb); 225 return(1); 226 } 227 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 228 warnx("Device is not ready"); 229 if (verbose) 230 cam_error_print(cam_dev, ccb, CAM_ESF_ALL, 231 CAM_EPF_ALL, stderr); 232 cam_freeccb(ccb); 233 return (1); 234 } 235 pkt_size = vp->max_pkt_size; 236 if (verbose || sim_mode) { 237 fprintf(stdout, 238 "--------------------------------------------------\n"); 239 fprintf(stdout, 240 "PktNo. PktSize BytesRemaining LastPkt\n"); 241 fprintf(stdout, 242 "--------------------------------------------------\n"); 243 } 244 /* Download single fw packets. */ 245 do { 246 if (img_size <= vp->max_pkt_size) { 247 last_pkt = 1; 248 pkt_size = img_size; 249 } 250 if (verbose || sim_mode) 251 fprintf(stdout, "%3u %5u (0x%05X) %7u (0x%06X) " 252 "%d\n", pkt_count, pkt_size, pkt_size, 253 img_size - pkt_size, img_size - pkt_size, 254 last_pkt); 255 bzero(&cdb, sizeof(cdb)); 256 cdb.opcode = WRITE_BUFFER; 257 cdb.control = 0; 258 /* Parameter list length. */ 259 scsi_ulto3b(pkt_size, &cdb.length[0]); 260 offset = vp->inc_cdb_offset ? (pkt_ptr - buf) : 0; 261 scsi_ulto3b(offset, &cdb.offset[0]); 262 cdb.byte2 = last_pkt ? vp->cdb_byte2_last : vp->cdb_byte2; 263 cdb.buffer_id = vp->inc_cdb_buffer_id ? pkt_count : 0; 264 /* Zero out payload of ccb union after ccb header. */ 265 bzero((u_char *)ccb + sizeof(struct ccb_hdr), 266 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 267 /* Copy previously constructed cdb into ccb_scsiio struct. */ 268 bcopy(&cdb, &ccb->csio.cdb_io.cdb_bytes[0], 269 sizeof(struct scsi_write_buffer)); 270 /* Fill rest of ccb_scsiio struct. */ 271 if (!sim_mode) { 272 cam_fill_csio(&ccb->csio, /* ccb_scsiio */ 273 retry_count, /* retries */ 274 NULL, /* cbfcnp */ 275 CAM_DIR_OUT | CAM_DEV_QFRZDIS, /* flags */ 276 CAM_TAG_ACTION_NONE, /* tag_action */ 277 (u_char *)pkt_ptr, /* data_ptr */ 278 pkt_size, /* dxfer_len */ 279 SSD_FULL_SIZE, /* sense_len */ 280 sizeof(struct scsi_write_buffer), /* cdb_len */ 281 timeout ? timeout : CMD_TIMEOUT); /* timeout */ 282 /* Execute the command. */ 283 if (cam_send_ccb(cam_dev, ccb) < 0) { 284 warnx("Error writing image to device"); 285 if (verbose) 286 cam_error_print(cam_dev, ccb, CAM_ESF_ALL, 287 CAM_EPF_ALL, stderr); 288 goto bailout; 289 } 290 } 291 /* Prepare next round. */ 292 pkt_count++; 293 pkt_ptr += pkt_size; 294 img_size -= pkt_size; 295 } while(!last_pkt); 296 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 297 if (verbose) 298 cam_error_print(cam_dev, ccb, CAM_ESF_ALL, 299 CAM_EPF_ALL, stderr); 300 goto bailout; 301 } 302 cam_freeccb(ccb); 303 return (0); 304bailout: 305 cam_freeccb(ccb); 306 return (1); 307} 308 309int 310fwdownload(struct cam_device *device, int argc, char **argv, 311 char *combinedopt, int verbose, int retry_count, int timeout) 312{ 313 struct fw_vendor *vp; 314 char *fw_img_path = NULL; 315 char *buf; 316 int img_size; 317 int c; 318 int sim_mode = 0; 319 int confirmed = 0; 320 321 while ((c = getopt(argc, argv, combinedopt)) != -1) { 322 switch (c) { 323 case 's': 324 sim_mode = 1; 325 confirmed = 1; 326 break; 327 case 'f': 328 fw_img_path = optarg; 329 break; 330 case 'y': 331 confirmed = 1; 332 break; 333 default: 334 break; 335 } 336 } 337 338 if (fw_img_path == NULL) 339 errx(1, 340 "you must specify a firmware image file using -f option"); 341 342 vp = fw_get_vendor(device); 343 if (vp == NULL || vp->type == VENDOR_UNKNOWN) 344 errx(1, "Unsupported device"); 345 346 buf = fw_read_img(fw_img_path, vp, &img_size); 347 if (buf == NULL) 348 goto fail; 349 350 if (!confirmed) { 351 fprintf(stdout, "You are about to download firmware image (%s)" 352 " into the following device:\n", 353 fw_img_path); 354 if (scsidoinquiry(device, argc, argv, combinedopt, 0, 355 5000) != 0) { 356 warnx("Error sending inquiry"); 357 goto fail; 358 } 359 fprintf(stdout, "\nIt may damage your drive. "); 360 if (!get_confirmation()) 361 goto fail; 362 } 363 if (sim_mode) 364 fprintf(stdout, "Running in simulation mode\n"); 365 366 if (fw_download_img(device, vp, buf, img_size, sim_mode, verbose, 367 retry_count, timeout) != 0) { 368 fprintf(stderr, "Firmware download failed\n"); 369 goto fail; 370 } else 371 fprintf(stdout, "Firmware download successful\n"); 372 373 free(buf); 374 return (0); 375fail: 376 if (buf != NULL) 377 free(buf); 378 return (1); 379} 380 381