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 /* Tables for command line parsing */ 54 55 static cmd_fn_t firmware; 56 57 #define NONE 0xffffffffu 58 static struct options { 59 bool activate; 60 uint32_t slot; 61 const char *fw_img; 62 const char *dev; 63 } opt = { 64 .activate = false, 65 .slot = NONE, 66 .fw_img = NULL, 67 .dev = NULL, 68 }; 69 70 static const struct opts firmware_opts[] = { 71 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc } 72 OPT("activate", 'a', arg_none, opt, activate, 73 "Attempt to activate firmware"), 74 OPT("slot", 's', arg_uint32, opt, slot, 75 "Slot to activate and/or download firmware to"), 76 OPT("firmware", 'f', arg_path, opt, fw_img, 77 "Firmware image to download"), 78 { NULL, 0, arg_none, NULL, NULL } 79 }; 80 #undef OPT 81 82 static const struct args firmware_args[] = { 83 { arg_string, &opt.dev, "controller-id" }, 84 { arg_none, NULL, NULL }, 85 }; 86 87 static struct cmd firmware_cmd = { 88 .name = "firmware", 89 .fn = firmware, 90 .descr = "Download firmware image to controller", 91 .ctx_size = sizeof(opt), 92 .opts = firmware_opts, 93 .args = firmware_args, 94 }; 95 96 CMD_COMMAND(firmware_cmd); 97 98 /* End of tables for command line parsing */ 99 100 static int 101 slot_has_valid_firmware(int fd, int slot) 102 { 103 struct nvme_firmware_page fw; 104 int has_fw = false; 105 106 read_logpage(fd, NVME_LOG_FIRMWARE_SLOT, 107 NVME_GLOBAL_NAMESPACE_TAG, 0, 0, 0, &fw, sizeof(fw)); 108 109 if (fw.revision[slot-1] != 0LLU) 110 has_fw = true; 111 112 return (has_fw); 113 } 114 115 static void 116 read_image_file(const char *path, void **buf, int32_t *size) 117 { 118 struct stat sb; 119 int32_t filesize; 120 int fd; 121 122 *size = 0; 123 *buf = NULL; 124 125 if ((fd = open(path, O_RDONLY)) < 0) 126 err(1, "unable to open '%s'", path); 127 if (fstat(fd, &sb) < 0) 128 err(1, "unable to stat '%s'", path); 129 130 /* 131 * The NVMe spec does not explicitly state a maximum firmware image 132 * size, although one can be inferred from the dword size limitation 133 * for the size and offset fields in the Firmware Image Download 134 * command. 135 * 136 * Technically, the max is UINT32_MAX * sizeof(uint32_t), since the 137 * size and offsets are specified in terms of dwords (not bytes), but 138 * realistically INT32_MAX is sufficient here and simplifies matters 139 * a bit. 140 */ 141 if (sb.st_size > INT32_MAX) 142 errx(1, "size of file '%s' is too large (%jd bytes)", 143 path, (intmax_t)sb.st_size); 144 filesize = (int32_t)sb.st_size; 145 if ((*buf = malloc(filesize)) == NULL) 146 errx(1, "unable to malloc %d bytes", filesize); 147 if ((*size = read(fd, *buf, filesize)) < 0) 148 err(1, "error reading '%s'", path); 149 /* XXX assuming no short reads */ 150 if (*size != filesize) 151 errx(1, 152 "error reading '%s' (read %d bytes, requested %d bytes)", 153 path, *size, filesize); 154 } 155 156 static void 157 update_firmware(int fd, uint8_t *payload, int32_t payload_size) 158 { 159 struct nvme_pt_command pt; 160 int32_t off, resid, size; 161 void *chunk; 162 163 off = 0; 164 resid = payload_size; 165 166 if ((chunk = aligned_alloc(PAGE_SIZE, NVME_MAX_XFER_SIZE)) == NULL) 167 errx(1, "unable to malloc %d bytes", NVME_MAX_XFER_SIZE); 168 169 while (resid > 0) { 170 size = (resid >= NVME_MAX_XFER_SIZE) ? 171 NVME_MAX_XFER_SIZE : resid; 172 memcpy(chunk, payload + off, size); 173 174 memset(&pt, 0, sizeof(pt)); 175 pt.cmd.opc = NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD; 176 pt.cmd.cdw10 = htole32((size / sizeof(uint32_t)) - 1); 177 pt.cmd.cdw11 = htole32(off / sizeof(uint32_t)); 178 pt.buf = chunk; 179 pt.len = size; 180 pt.is_read = 0; 181 182 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 183 err(1, "firmware download request failed"); 184 185 if (nvme_completion_is_error(&pt.cpl)) 186 errx(1, "firmware download request returned error"); 187 188 resid -= size; 189 off += size; 190 } 191 } 192 193 static int 194 activate_firmware(int fd, int slot, int activate_action) 195 { 196 struct nvme_pt_command pt; 197 uint16_t sct, sc; 198 199 memset(&pt, 0, sizeof(pt)); 200 pt.cmd.opc = NVME_OPC_FIRMWARE_ACTIVATE; 201 pt.cmd.cdw10 = htole32((activate_action << 3) | slot); 202 pt.is_read = 0; 203 204 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 205 err(1, "firmware activate request failed"); 206 207 sct = NVME_STATUS_GET_SCT(pt.cpl.status); 208 sc = NVME_STATUS_GET_SC(pt.cpl.status); 209 210 if (sct == NVME_SCT_COMMAND_SPECIFIC && 211 sc == NVME_SC_FIRMWARE_REQUIRES_RESET) 212 return 1; 213 214 if (nvme_completion_is_error(&pt.cpl)) 215 errx(1, "firmware activate request returned error"); 216 217 return 0; 218 } 219 220 static void 221 firmware(const struct cmd *f, int argc, char *argv[]) 222 { 223 int fd = -1; 224 int activate_action, reboot_required; 225 char prompt[64]; 226 void *buf = NULL; 227 int32_t size = 0, nsid; 228 uint16_t oacs_fw; 229 uint8_t fw_slot1_ro, fw_num_slots; 230 struct nvme_controller_data cdata; 231 232 if (arg_parse(argc, argv, f)) 233 return; 234 235 if (opt.slot == 0) { 236 fprintf(stderr, 237 "0 is not a valid slot number. " 238 "Slot numbers start at 1.\n"); 239 arg_help(argc, argv, f); 240 } else if (opt.slot > 7 && opt.slot != NONE) { 241 fprintf(stderr, 242 "Slot number %s specified which is " 243 "greater than max allowed slot number of " 244 "7.\n", optarg); 245 arg_help(argc, argv, f); 246 } 247 248 if (!opt.activate && opt.fw_img == NULL) { 249 fprintf(stderr, 250 "Neither a replace ([-f path_to_firmware]) nor " 251 "activate ([-a]) firmware image action\n" 252 "was specified.\n"); 253 arg_help(argc, argv, f); 254 } 255 256 if (opt.activate && opt.fw_img == NULL && opt.slot == 0) { 257 fprintf(stderr, 258 "Slot number to activate not specified.\n"); 259 arg_help(argc, argv, f); 260 } 261 262 open_dev(opt.dev, &fd, 1, 1); 263 264 /* Check that a controller (and not a namespace) was specified. */ 265 get_nsid(fd, NULL, &nsid); 266 if (nsid != 0) { 267 close(fd); 268 arg_help(argc, argv, f); 269 } 270 271 read_controller_data(fd, &cdata); 272 273 oacs_fw = (cdata.oacs >> NVME_CTRLR_DATA_OACS_FIRMWARE_SHIFT) & 274 NVME_CTRLR_DATA_OACS_FIRMWARE_MASK; 275 276 if (oacs_fw == 0) 277 errx(1, 278 "controller does not support firmware activate/download"); 279 280 fw_slot1_ro = (cdata.frmw >> NVME_CTRLR_DATA_FRMW_SLOT1_RO_SHIFT) & 281 NVME_CTRLR_DATA_FRMW_SLOT1_RO_MASK; 282 283 if (opt.fw_img && opt.slot == 1 && fw_slot1_ro) 284 errx(1, "slot %d is marked as read only", opt.slot); 285 286 fw_num_slots = (cdata.frmw >> NVME_CTRLR_DATA_FRMW_NUM_SLOTS_SHIFT) & 287 NVME_CTRLR_DATA_FRMW_NUM_SLOTS_MASK; 288 289 if (opt.slot > fw_num_slots) 290 errx(1, 291 "slot %d specified but controller only supports %d slots", 292 opt.slot, fw_num_slots); 293 294 if (opt.activate && opt.fw_img == NULL && 295 !slot_has_valid_firmware(fd, opt.slot)) 296 errx(1, 297 "slot %d does not contain valid firmware,\n" 298 "try 'nvmecontrol logpage -p 3 %s' to get a list " 299 "of available images\n", 300 opt.slot, opt.dev); 301 302 if (opt.fw_img) 303 read_image_file(opt.fw_img, &buf, &size); 304 305 if (opt.fw_img != NULL&& opt.activate) 306 printf("You are about to download and activate " 307 "firmware image (%s) to controller %s.\n" 308 "This may damage your controller and/or " 309 "overwrite an existing firmware image.\n", 310 opt.fw_img, opt.dev); 311 else if (opt.activate) 312 printf("You are about to activate a new firmware " 313 "image on controller %s.\n" 314 "This may damage your controller.\n", 315 opt.dev); 316 else if (opt.fw_img != NULL) 317 printf("You are about to download firmware image " 318 "(%s) to controller %s.\n" 319 "This may damage your controller and/or " 320 "overwrite an existing firmware image.\n", 321 opt.fw_img, opt.dev); 322 323 printf("Are you sure you want to continue? (yes/no) "); 324 while (1) { 325 fgets(prompt, sizeof(prompt), stdin); 326 if (strncasecmp(prompt, "yes", 3) == 0) 327 break; 328 if (strncasecmp(prompt, "no", 2) == 0) 329 exit(1); 330 printf("Please answer \"yes\" or \"no\". "); 331 } 332 333 if (opt.fw_img != NULL) { 334 update_firmware(fd, buf, size); 335 if (opt.activate) 336 activate_action = NVME_AA_REPLACE_ACTIVATE; 337 else 338 activate_action = NVME_AA_REPLACE_NO_ACTIVATE; 339 } else { 340 activate_action = NVME_AA_ACTIVATE; 341 } 342 343 reboot_required = activate_firmware(fd, opt.slot, activate_action); 344 345 if (opt.activate) { 346 if (reboot_required) { 347 printf("New firmware image activated but requires " 348 "conventional reset (i.e. reboot) to " 349 "complete activation.\n"); 350 } else { 351 printf("New firmware image activated and will take " 352 "effect after next controller reset.\n" 353 "Controller reset can be initiated via " 354 "'nvmecontrol reset %s'\n", 355 opt.dev); 356 } 357 } 358 359 close(fd); 360 exit(0); 361 } 362