1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2013 EMC Corp. 5 * All rights reserved. 6 * 7 * Copyright (C) 2012-2013 Intel Corporation 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/ioccom.h> 37 #include <sys/stat.h> 38 #include <sys/types.h> 39 40 #include <ctype.h> 41 #include <err.h> 42 #include <fcntl.h> 43 #include <inttypes.h> 44 #include <stdbool.h> 45 #include <stddef.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 51 #include "nvmecontrol.h" 52 53 static int 54 slot_has_valid_firmware(int fd, int slot) 55 { 56 struct nvme_firmware_page fw; 57 int has_fw = false; 58 59 read_logpage(fd, NVME_LOG_FIRMWARE_SLOT, 60 NVME_GLOBAL_NAMESPACE_TAG, &fw, sizeof(fw)); 61 62 if (fw.revision[slot-1] != 0LLU) 63 has_fw = true; 64 65 return (has_fw); 66 } 67 68 static void 69 read_image_file(char *path, void **buf, int32_t *size) 70 { 71 struct stat sb; 72 int32_t filesize; 73 int fd; 74 75 *size = 0; 76 *buf = NULL; 77 78 if ((fd = open(path, O_RDONLY)) < 0) 79 err(1, "unable to open '%s'", path); 80 if (fstat(fd, &sb) < 0) 81 err(1, "unable to stat '%s'", path); 82 83 /* 84 * The NVMe spec does not explicitly state a maximum firmware image 85 * size, although one can be inferred from the dword size limitation 86 * for the size and offset fields in the Firmware Image Download 87 * command. 88 * 89 * Technically, the max is UINT32_MAX * sizeof(uint32_t), since the 90 * size and offsets are specified in terms of dwords (not bytes), but 91 * realistically INT32_MAX is sufficient here and simplifies matters 92 * a bit. 93 */ 94 if (sb.st_size > INT32_MAX) 95 errx(1, "size of file '%s' is too large (%jd bytes)", 96 path, (intmax_t)sb.st_size); 97 filesize = (int32_t)sb.st_size; 98 if ((*buf = malloc(filesize)) == NULL) 99 errx(1, "unable to malloc %d bytes", filesize); 100 if ((*size = read(fd, *buf, filesize)) < 0) 101 err(1, "error reading '%s'", path); 102 /* XXX assuming no short reads */ 103 if (*size != filesize) 104 errx(1, 105 "error reading '%s' (read %d bytes, requested %d bytes)", 106 path, *size, filesize); 107 } 108 109 static void 110 update_firmware(int fd, uint8_t *payload, int32_t payload_size) 111 { 112 struct nvme_pt_command pt; 113 int32_t off, resid, size; 114 void *chunk; 115 116 off = 0; 117 resid = payload_size; 118 119 if ((chunk = aligned_alloc(PAGE_SIZE, NVME_MAX_XFER_SIZE)) == NULL) 120 errx(1, "unable to malloc %d bytes", NVME_MAX_XFER_SIZE); 121 122 while (resid > 0) { 123 size = (resid >= NVME_MAX_XFER_SIZE) ? 124 NVME_MAX_XFER_SIZE : resid; 125 memcpy(chunk, payload + off, size); 126 127 memset(&pt, 0, sizeof(pt)); 128 pt.cmd.opc_fuse = NVME_CMD_SET_OPC(NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD); 129 pt.cmd.cdw10 = htole32((size / sizeof(uint32_t)) - 1); 130 pt.cmd.cdw11 = htole32(off / sizeof(uint32_t)); 131 pt.buf = chunk; 132 pt.len = size; 133 pt.is_read = 0; 134 135 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 136 err(1, "firmware download request failed"); 137 138 if (nvme_completion_is_error(&pt.cpl)) 139 errx(1, "firmware download request returned error"); 140 141 resid -= size; 142 off += size; 143 } 144 } 145 146 static int 147 activate_firmware(int fd, int slot, int activate_action) 148 { 149 struct nvme_pt_command pt; 150 uint16_t sct, sc; 151 152 memset(&pt, 0, sizeof(pt)); 153 pt.cmd.opc_fuse = NVME_CMD_SET_OPC(NVME_OPC_FIRMWARE_ACTIVATE); 154 pt.cmd.cdw10 = htole32((activate_action << 3) | slot); 155 pt.is_read = 0; 156 157 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 158 err(1, "firmware activate request failed"); 159 160 sct = NVME_STATUS_GET_SCT(pt.cpl.status); 161 sc = NVME_STATUS_GET_SC(pt.cpl.status); 162 163 if (sct == NVME_SCT_COMMAND_SPECIFIC && 164 sc == NVME_SC_FIRMWARE_REQUIRES_RESET) 165 return 1; 166 167 if (nvme_completion_is_error(&pt.cpl)) 168 errx(1, "firmware activate request returned error"); 169 170 return 0; 171 } 172 173 static void 174 firmware_usage(void) 175 { 176 fprintf(stderr, "usage:\n"); 177 fprintf(stderr, FIRMWARE_USAGE); 178 exit(1); 179 } 180 181 void 182 firmware(int argc, char *argv[]) 183 { 184 int fd = -1, slot = 0; 185 int a_flag, s_flag, f_flag; 186 int activate_action, reboot_required; 187 int opt; 188 char *p, *image = NULL; 189 char *controller = NULL, prompt[64]; 190 void *buf = NULL; 191 int32_t size = 0; 192 uint16_t oacs_fw; 193 uint8_t fw_slot1_ro, fw_num_slots; 194 struct nvme_controller_data cdata; 195 196 a_flag = s_flag = f_flag = false; 197 198 while ((opt = getopt(argc, argv, "af:s:")) != -1) { 199 switch (opt) { 200 case 'a': 201 a_flag = true; 202 break; 203 case 's': 204 slot = strtol(optarg, &p, 0); 205 if (p != NULL && *p != '\0') { 206 fprintf(stderr, 207 "\"%s\" not valid slot.\n", 208 optarg); 209 firmware_usage(); 210 } else if (slot == 0) { 211 fprintf(stderr, 212 "0 is not a valid slot number. " 213 "Slot numbers start at 1.\n"); 214 firmware_usage(); 215 } else if (slot > 7) { 216 fprintf(stderr, 217 "Slot number %s specified which is " 218 "greater than max allowed slot number of " 219 "7.\n", optarg); 220 firmware_usage(); 221 } 222 s_flag = true; 223 break; 224 case 'f': 225 image = optarg; 226 f_flag = true; 227 break; 228 } 229 } 230 231 /* Check that a controller (and not a namespace) was specified. */ 232 if (optind >= argc || strstr(argv[optind], NVME_NS_PREFIX) != NULL) 233 firmware_usage(); 234 235 if (!f_flag && !a_flag) { 236 fprintf(stderr, 237 "Neither a replace ([-f path_to_firmware]) nor " 238 "activate ([-a]) firmware image action\n" 239 "was specified.\n"); 240 firmware_usage(); 241 } 242 243 if (!f_flag && a_flag && slot == 0) { 244 fprintf(stderr, 245 "Slot number to activate not specified.\n"); 246 firmware_usage(); 247 } 248 249 controller = argv[optind]; 250 open_dev(controller, &fd, 1, 1); 251 read_controller_data(fd, &cdata); 252 253 oacs_fw = (cdata.oacs >> NVME_CTRLR_DATA_OACS_FIRMWARE_SHIFT) & 254 NVME_CTRLR_DATA_OACS_FIRMWARE_MASK; 255 256 if (oacs_fw == 0) 257 errx(1, 258 "controller does not support firmware activate/download"); 259 260 fw_slot1_ro = (cdata.frmw >> NVME_CTRLR_DATA_FRMW_SLOT1_RO_SHIFT) & 261 NVME_CTRLR_DATA_FRMW_SLOT1_RO_MASK; 262 263 if (f_flag && slot == 1 && fw_slot1_ro) 264 errx(1, "slot %d is marked as read only", slot); 265 266 fw_num_slots = (cdata.frmw >> NVME_CTRLR_DATA_FRMW_NUM_SLOTS_SHIFT) & 267 NVME_CTRLR_DATA_FRMW_NUM_SLOTS_MASK; 268 269 if (slot > fw_num_slots) 270 errx(1, 271 "slot %d specified but controller only supports %d slots", 272 slot, fw_num_slots); 273 274 if (a_flag && !f_flag && !slot_has_valid_firmware(fd, slot)) 275 errx(1, 276 "slot %d does not contain valid firmware,\n" 277 "try 'nvmecontrol logpage -p 3 %s' to get a list " 278 "of available images\n", 279 slot, controller); 280 281 if (f_flag) 282 read_image_file(image, &buf, &size); 283 284 if (f_flag && a_flag) 285 printf("You are about to download and activate " 286 "firmware image (%s) to controller %s.\n" 287 "This may damage your controller and/or " 288 "overwrite an existing firmware image.\n", 289 image, controller); 290 else if (a_flag) 291 printf("You are about to activate a new firmware " 292 "image on controller %s.\n" 293 "This may damage your controller.\n", 294 controller); 295 else if (f_flag) 296 printf("You are about to download firmware image " 297 "(%s) to controller %s.\n" 298 "This may damage your controller and/or " 299 "overwrite an existing firmware image.\n", 300 image, controller); 301 302 printf("Are you sure you want to continue? (yes/no) "); 303 while (1) { 304 fgets(prompt, sizeof(prompt), stdin); 305 if (strncasecmp(prompt, "yes", 3) == 0) 306 break; 307 if (strncasecmp(prompt, "no", 2) == 0) 308 exit(1); 309 printf("Please answer \"yes\" or \"no\". "); 310 } 311 312 if (f_flag) { 313 update_firmware(fd, buf, size); 314 if (a_flag) 315 activate_action = NVME_AA_REPLACE_ACTIVATE; 316 else 317 activate_action = NVME_AA_REPLACE_NO_ACTIVATE; 318 } else { 319 activate_action = NVME_AA_ACTIVATE; 320 } 321 322 reboot_required = activate_firmware(fd, slot, activate_action); 323 324 if (a_flag) { 325 if (reboot_required) { 326 printf("New firmware image activated but requires " 327 "conventional reset (i.e. reboot) to " 328 "complete activation.\n"); 329 } else { 330 printf("New firmware image activated and will take " 331 "effect after next controller reset.\n" 332 "Controller reset can be initiated via " 333 "'nvmecontrol reset %s'\n", 334 controller); 335 } 336 } 337 338 close(fd); 339 exit(0); 340 } 341