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 = NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD; 129 pt.cmd.cdw10 = (size / sizeof(uint32_t)) - 1; 130 pt.cmd.cdw11 = (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 151 memset(&pt, 0, sizeof(pt)); 152 pt.cmd.opc = NVME_OPC_FIRMWARE_ACTIVATE; 153 pt.cmd.cdw10 = (activate_action << 3) | slot; 154 pt.is_read = 0; 155 156 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 157 err(1, "firmware activate request failed"); 158 159 if (pt.cpl.status.sct == NVME_SCT_COMMAND_SPECIFIC && 160 pt.cpl.status.sc == NVME_SC_FIRMWARE_REQUIRES_RESET) 161 return 1; 162 163 if (nvme_completion_is_error(&pt.cpl)) 164 errx(1, "firmware activate request returned error"); 165 166 return 0; 167 } 168 169 static void 170 firmware_usage(void) 171 { 172 fprintf(stderr, "usage:\n"); 173 fprintf(stderr, FIRMWARE_USAGE); 174 exit(1); 175 } 176 177 void 178 firmware(int argc, char *argv[]) 179 { 180 int fd = -1, slot = 0; 181 int a_flag, s_flag, f_flag; 182 int activate_action, reboot_required; 183 char ch, *p, *image = NULL; 184 char *controller = NULL, prompt[64]; 185 void *buf = NULL; 186 int32_t size = 0; 187 struct nvme_controller_data cdata; 188 189 a_flag = s_flag = f_flag = false; 190 191 while ((ch = getopt(argc, argv, "af:s:")) != -1) { 192 switch (ch) { 193 case 'a': 194 a_flag = true; 195 break; 196 case 's': 197 slot = strtol(optarg, &p, 0); 198 if (p != NULL && *p != '\0') { 199 fprintf(stderr, 200 "\"%s\" not valid slot.\n", 201 optarg); 202 firmware_usage(); 203 } else if (slot == 0) { 204 fprintf(stderr, 205 "0 is not a valid slot number. " 206 "Slot numbers start at 1.\n"); 207 firmware_usage(); 208 } else if (slot > 7) { 209 fprintf(stderr, 210 "Slot number %s specified which is " 211 "greater than max allowed slot number of " 212 "7.\n", optarg); 213 firmware_usage(); 214 } 215 s_flag = true; 216 break; 217 case 'f': 218 image = optarg; 219 f_flag = true; 220 break; 221 } 222 } 223 224 /* Check that a controller (and not a namespace) was specified. */ 225 if (optind >= argc || strstr(argv[optind], NVME_NS_PREFIX) != NULL) 226 firmware_usage(); 227 228 if (!f_flag && !a_flag) { 229 fprintf(stderr, 230 "Neither a replace ([-f path_to_firmware]) nor " 231 "activate ([-a]) firmware image action\n" 232 "was specified.\n"); 233 firmware_usage(); 234 } 235 236 if (!f_flag && a_flag && slot == 0) { 237 fprintf(stderr, 238 "Slot number to activate not specified.\n"); 239 firmware_usage(); 240 } 241 242 controller = argv[optind]; 243 open_dev(controller, &fd, 1, 1); 244 read_controller_data(fd, &cdata); 245 246 if (cdata.oacs.firmware == 0) 247 errx(1, 248 "controller does not support firmware activate/download"); 249 250 if (f_flag && slot == 1 && cdata.frmw.slot1_ro) 251 errx(1, "slot %d is marked as read only", slot); 252 253 if (slot > cdata.frmw.num_slots) 254 errx(1, 255 "slot %d specified but controller only supports %d slots", 256 slot, cdata.frmw.num_slots); 257 258 if (a_flag && !f_flag && !slot_has_valid_firmware(fd, slot)) 259 errx(1, 260 "slot %d does not contain valid firmware,\n" 261 "try 'nvmecontrol logpage -p 3 %s' to get a list " 262 "of available images\n", 263 slot, controller); 264 265 if (f_flag) 266 read_image_file(image, &buf, &size); 267 268 if (f_flag && a_flag) 269 printf("You are about to download and activate " 270 "firmware image (%s) to controller %s.\n" 271 "This may damage your controller and/or " 272 "overwrite an existing firmware image.\n", 273 image, controller); 274 else if (a_flag) 275 printf("You are about to activate a new firmware " 276 "image on controller %s.\n" 277 "This may damage your controller.\n", 278 controller); 279 else if (f_flag) 280 printf("You are about to download firmware image " 281 "(%s) to controller %s.\n" 282 "This may damage your controller and/or " 283 "overwrite an existing firmware image.\n", 284 image, controller); 285 286 printf("Are you sure you want to continue? (yes/no) "); 287 while (1) { 288 fgets(prompt, sizeof(prompt), stdin); 289 if (strncasecmp(prompt, "yes", 3) == 0) 290 break; 291 if (strncasecmp(prompt, "no", 2) == 0) 292 exit(1); 293 printf("Please answer \"yes\" or \"no\". "); 294 } 295 296 if (f_flag) { 297 update_firmware(fd, buf, size); 298 if (a_flag) 299 activate_action = NVME_AA_REPLACE_ACTIVATE; 300 else 301 activate_action = NVME_AA_REPLACE_NO_ACTIVATE; 302 } else { 303 activate_action = NVME_AA_ACTIVATE; 304 } 305 306 reboot_required = activate_firmware(fd, slot, activate_action); 307 308 if (a_flag) { 309 if (reboot_required) { 310 printf("New firmware image activated but requires " 311 "conventional reset (i.e. reboot) to " 312 "complete activation.\n"); 313 } else { 314 printf("New firmware image activated and will take " 315 "effect after next controller reset.\n" 316 "Controller reset can be initiated via " 317 "'nvmecontrol reset %s'\n", 318 controller); 319 } 320 } 321 322 close(fd); 323 exit(0); 324 } 325