1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
34 #include <sys/ioccom.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37
38 #include <ctype.h>
39 #include <err.h>
40 #include <fcntl.h>
41 #include <inttypes.h>
42 #include <stdbool.h>
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sysexits.h>
48 #include <unistd.h>
49
50 #include "nvmecontrol.h"
51
52 /* Tables for command line parsing */
53
54 static cmd_fn_t firmware;
55
56 #define NONE 0xffffffffu
57 static struct options {
58 bool activate;
59 uint32_t slot;
60 const char *fw_img;
61 const char *dev;
62 } opt = {
63 .activate = false,
64 .slot = NONE,
65 .fw_img = NULL,
66 .dev = NULL,
67 };
68
69 static const struct opts firmware_opts[] = {
70 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
71 OPT("activate", 'a', arg_none, opt, activate,
72 "Attempt to activate firmware"),
73 OPT("slot", 's', arg_uint32, opt, slot,
74 "Slot to activate and/or download firmware to"),
75 OPT("firmware", 'f', arg_path, opt, fw_img,
76 "Firmware image to download"),
77 { NULL, 0, arg_none, NULL, NULL }
78 };
79 #undef OPT
80
81 static const struct args firmware_args[] = {
82 { arg_string, &opt.dev, "controller-id|namespace-id" },
83 { arg_none, NULL, NULL },
84 };
85
86 static struct cmd firmware_cmd = {
87 .name = "firmware",
88 .fn = firmware,
89 .descr = "Download firmware image to controller",
90 .ctx_size = sizeof(opt),
91 .opts = firmware_opts,
92 .args = firmware_args,
93 };
94
95 CMD_COMMAND(firmware_cmd);
96
97 /* End of tables for command line parsing */
98
99 static int
slot_has_valid_firmware(int fd,int slot)100 slot_has_valid_firmware(int fd, int slot)
101 {
102 struct nvme_firmware_page fw;
103 int has_fw = false;
104
105 read_logpage(fd, NVME_LOG_FIRMWARE_SLOT,
106 NVME_GLOBAL_NAMESPACE_TAG, 0, 0, 0, 0, 0, 0, 0,
107 &fw, sizeof(fw));
108
109 if (fw.revision[slot-1][0] != '\0')
110 has_fw = true;
111
112 return (has_fw);
113 }
114
115 static void
read_image_file(const char * path,void ** buf,int32_t * size)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(EX_NOINPUT, "unable to open '%s'", path);
127 if (fstat(fd, &sb) < 0)
128 err(EX_NOINPUT, "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(EX_USAGE, "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(EX_OSERR, "unable to malloc %d bytes", filesize);
147 if ((*size = read(fd, *buf, filesize)) < 0)
148 err(EX_IOERR, "error reading '%s'", path);
149 /* XXX assuming no short reads */
150 if (*size != filesize)
151 errx(EX_IOERR,
152 "error reading '%s' (read %d bytes, requested %d bytes)",
153 path, *size, filesize);
154 close(fd);
155 }
156
157 static void
update_firmware(int fd,uint8_t * payload,int32_t payload_size,uint8_t fwug)158 update_firmware(int fd, uint8_t *payload, int32_t payload_size, uint8_t fwug)
159 {
160 struct nvme_pt_command pt;
161 uint64_t max_xfer_size;
162 int32_t off;
163 uint32_t resid, size;
164 void *chunk;
165
166 off = 0;
167 resid = payload_size;
168
169 if (ioctl(fd, NVME_GET_MAX_XFER_SIZE, &max_xfer_size) < 0)
170 err(EX_IOERR, "query max transfer size failed");
171 if (fwug != 0 && fwug != 0xFF)
172 max_xfer_size = MIN(max_xfer_size, (uint64_t)fwug << 12);
173
174 if ((chunk = aligned_alloc(PAGE_SIZE, max_xfer_size)) == NULL)
175 errx(EX_OSERR, "unable to malloc %zd bytes", (size_t)max_xfer_size);
176
177 while (resid > 0) {
178 size = (resid >= max_xfer_size) ? max_xfer_size : resid;
179 memcpy(chunk, payload + off, size);
180
181 memset(&pt, 0, sizeof(pt));
182 pt.cmd.opc = NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD;
183 pt.cmd.cdw10 = htole32((size / sizeof(uint32_t)) - 1);
184 pt.cmd.cdw11 = htole32(off / sizeof(uint32_t));
185 pt.buf = chunk;
186 pt.len = size;
187 pt.is_read = 0;
188
189 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
190 err(EX_IOERR, "firmware download request failed");
191
192 if (nvme_completion_is_error(&pt.cpl))
193 errx(EX_IOERR, "firmware download request returned error");
194
195 resid -= size;
196 off += size;
197 }
198 free(chunk);
199 }
200
201 static int
activate_firmware(int fd,int slot,int activate_action)202 activate_firmware(int fd, int slot, int activate_action)
203 {
204 struct nvme_pt_command pt;
205 uint16_t sct, sc;
206
207 memset(&pt, 0, sizeof(pt));
208 pt.cmd.opc = NVME_OPC_FIRMWARE_ACTIVATE;
209 pt.cmd.cdw10 = htole32((activate_action << 3) | slot);
210 pt.is_read = 0;
211
212 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
213 err(EX_IOERR, "firmware activate request failed");
214
215 sct = NVME_STATUS_GET_SCT(pt.cpl.status);
216 sc = NVME_STATUS_GET_SC(pt.cpl.status);
217
218 if (sct == NVME_SCT_COMMAND_SPECIFIC &&
219 sc == NVME_SC_FIRMWARE_REQUIRES_RESET)
220 return 1;
221
222 if (nvme_completion_is_error(&pt.cpl))
223 errx(EX_IOERR, "firmware activate request returned error");
224
225 return 0;
226 }
227
228 static void
firmware(const struct cmd * f,int argc,char * argv[])229 firmware(const struct cmd *f, int argc, char *argv[])
230 {
231 int fd = -1;
232 int activate_action, reboot_required;
233 char prompt[64];
234 void *buf = NULL;
235 char *path;
236 int32_t size = 0, nsid;
237 uint16_t oacs_fw;
238 uint8_t fw_slot1_ro, fw_num_slots;
239 struct nvme_controller_data cdata;
240
241 if (arg_parse(argc, argv, f))
242 return;
243
244 if (opt.slot == 0) {
245 fprintf(stderr,
246 "0 is not a valid slot number. "
247 "Slot numbers start at 1.\n");
248 arg_help(argc, argv, f);
249 } else if (opt.slot > 7 && opt.slot != NONE) {
250 fprintf(stderr,
251 "Slot number %s specified which is "
252 "greater than max allowed slot number of "
253 "7.\n", optarg);
254 arg_help(argc, argv, f);
255 }
256
257 if (!opt.activate && opt.fw_img == NULL) {
258 fprintf(stderr,
259 "Neither a replace ([-f path_to_firmware]) nor "
260 "activate ([-a]) firmware image action\n"
261 "was specified.\n");
262 arg_help(argc, argv, f);
263 }
264
265 if (opt.activate && opt.fw_img == NULL && opt.slot == 0) {
266 fprintf(stderr,
267 "Slot number to activate not specified.\n");
268 arg_help(argc, argv, f);
269 }
270
271 open_dev(opt.dev, &fd, 1, 1);
272 get_nsid(fd, &path, &nsid);
273 if (nsid != 0) {
274 close(fd);
275 open_dev(path, &fd, 1, 1);
276 }
277 free(path);
278
279 if (read_controller_data(fd, &cdata))
280 errx(EX_IOERR, "Identify request failed");
281
282 oacs_fw = NVMEV(NVME_CTRLR_DATA_OACS_FIRMWARE, cdata.oacs);
283
284 if (oacs_fw == 0)
285 errx(EX_UNAVAILABLE,
286 "controller does not support firmware activate/download");
287
288 fw_slot1_ro = NVMEV(NVME_CTRLR_DATA_FRMW_SLOT1_RO, cdata.frmw);
289
290 if (opt.fw_img && opt.slot == 1 && fw_slot1_ro)
291 errx(EX_UNAVAILABLE, "slot %d is marked as read only", opt.slot);
292
293 fw_num_slots = NVMEV(NVME_CTRLR_DATA_FRMW_NUM_SLOTS, cdata.frmw);
294
295 if (opt.slot > fw_num_slots)
296 errx(EX_UNAVAILABLE,
297 "slot %d specified but controller only supports %d slots",
298 opt.slot, fw_num_slots);
299
300 if (opt.activate && opt.fw_img == NULL &&
301 !slot_has_valid_firmware(fd, opt.slot))
302 errx(EX_UNAVAILABLE,
303 "slot %d does not contain valid firmware,\n"
304 "try 'nvmecontrol logpage -p 3 %s' to get a list "
305 "of available images\n",
306 opt.slot, opt.dev);
307
308 if (opt.fw_img)
309 read_image_file(opt.fw_img, &buf, &size);
310
311 if (opt.fw_img != NULL&& opt.activate)
312 printf("You are about to download and activate "
313 "firmware image (%s) to controller %s.\n"
314 "This may damage your controller and/or "
315 "overwrite an existing firmware image.\n",
316 opt.fw_img, opt.dev);
317 else if (opt.activate)
318 printf("You are about to activate a new firmware "
319 "image on controller %s.\n"
320 "This may damage your controller.\n",
321 opt.dev);
322 else if (opt.fw_img != NULL)
323 printf("You are about to download firmware image "
324 "(%s) to controller %s.\n"
325 "This may damage your controller and/or "
326 "overwrite an existing firmware image.\n",
327 opt.fw_img, opt.dev);
328
329 printf("Are you sure you want to continue? (yes/no) ");
330 while (1) {
331 fgets(prompt, sizeof(prompt), stdin);
332 if (strncasecmp(prompt, "yes", 3) == 0)
333 break;
334 if (strncasecmp(prompt, "no", 2) == 0)
335 exit(EX_DATAERR);
336 printf("Please answer \"yes\" or \"no\". ");
337 }
338
339 if (opt.fw_img != NULL) {
340 update_firmware(fd, buf, size, cdata.fwug);
341 if (opt.activate)
342 activate_action = NVME_AA_REPLACE_ACTIVATE;
343 else
344 activate_action = NVME_AA_REPLACE_NO_ACTIVATE;
345 } else {
346 activate_action = NVME_AA_ACTIVATE;
347 }
348
349 reboot_required = activate_firmware(fd, opt.slot, activate_action);
350
351 if (opt.activate) {
352 if (reboot_required) {
353 printf("New firmware image activated but requires "
354 "conventional reset (i.e. reboot) to "
355 "complete activation.\n");
356 } else {
357 printf("New firmware image activated and will take "
358 "effect after next controller reset.\n"
359 "Controller reset can be initiated via "
360 "'nvmecontrol reset %s'\n",
361 opt.dev);
362 }
363 }
364
365 close(fd);
366 exit(0);
367 }
368