Deleted Added
full compact
firmware.c (252672) firmware.c (253109)
1/*-
2 * Copyright (c) 2013 EMC Corp.
3 * All rights reserved.
4 *
5 * Copyright (C) 2012-2013 Intel Corporation
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 14 unchanged lines hidden (view full) ---

23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2013 EMC Corp.
3 * All rights reserved.
4 *
5 * Copyright (C) 2012-2013 Intel Corporation
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 14 unchanged lines hidden (view full) ---

23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sbin/nvmecontrol/firmware.c 252672 2013-07-04 00:26:24Z jimharris $");
31__FBSDID("$FreeBSD: head/sbin/nvmecontrol/firmware.c 253109 2013-07-09 21:14:15Z jimharris $");
32
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>
32
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 <errno.h>
39#include
40#include <fcntl.h>
40#include <fcntl.h>
41#include <inttypes.h>
41#include <stdbool.h>
42#include <stddef.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
42#include <stdbool.h>
43#include <stddef.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
46#include <sysexits.h>
47#include <unistd.h>
48
49#include "nvmecontrol.h"
50
51static int
52slot_has_valid_firmware(int fd, int slot)
53{
54 struct nvme_firmware_page fw;

--- 4 unchanged lines hidden (view full) ---

59
60 if (fw.revision[slot-1] != 0LLU)
61 has_fw = true;
62
63 return (has_fw);
64}
65
66static void
47#include <unistd.h>
48
49#include "nvmecontrol.h"
50
51static int
52slot_has_valid_firmware(int fd, int slot)
53{
54 struct nvme_firmware_page fw;

--- 4 unchanged lines hidden (view full) ---

59
60 if (fw.revision[slot-1] != 0LLU)
61 has_fw = true;
62
63 return (has_fw);
64}
65
66static void
67read_image_file(char *path, void **buf, ssize_t *size)
67read_image_file(char *path, void **buf, int32_t *size)
68{
69 struct stat sb;
68{
69 struct stat sb;
70 int32_t filesize;
70 int fd;
71
72 *size = 0;
73 *buf = NULL;
74
71 int fd;
72
73 *size = 0;
74 *buf = NULL;
75
75 if ((fd = open(path, O_RDONLY)) < 0) {
76 fprintf(stderr, "Unable to open '%s'.\n", path);
77 exit(EX_IOERR);
78 }
79 if (fstat(fd, &sb) < 0) {
80 fprintf(stderr, "Unable to stat '%s'.\n", path);
81 close(fd);
82 exit(EX_IOERR);
83 }
84 if ((*buf = malloc(sb.st_size)) == NULL) {
85 fprintf(stderr, "Unable to malloc %jd bytes.\n",
86 sb.st_size);
87 close(fd);
88 exit(EX_IOERR);
89 }
90 if ((*size = read(fd, *buf, sb.st_size)) < 0) {
91 fprintf(stderr, "Error reading '%s', errno=%d (%s)\n",
92 path, errno, strerror(errno));
93 close(fd);
94 exit(EX_IOERR);
95 }
96 if (*size != sb.st_size) {
97 fprintf(stderr, "Error reading '%s', "
98 "read %zd bytes, requested %jd bytes\n",
99 path, *size, sb.st_size);
100 close(fd);
101 exit(EX_IOERR);
102 }
76 if ((fd = open(path, O_RDONLY)) < 0)
77 err(1, "unable to open '%s'", path);
78 if (fstat(fd, &sb) < 0)
79 err(1, "unable to stat '%s'", path);
80
81 /*
82 * The NVMe spec does not explicitly state a maximum firmware image
83 * size, although one can be inferred from the dword size limitation
84 * for the size and offset fields in the Firmware Image Download
85 * command.
86 *
87 * Technically, the max is UINT32_MAX * sizeof(uint32_t), since the
88 * size and offsets are specified in terms of dwords (not bytes), but
89 * realistically INT32_MAX is sufficient here and simplifies matters
90 * a bit.
91 */
92 if (sb.st_size > INT32_MAX)
93 errx(1, "size of file '%s' is too large (%jd bytes)",
94 path, (intmax_t)sb.st_size);
95 filesize = (int32_t)sb.st_size;
96 if ((*buf = malloc(filesize)) == NULL)
97 errx(1, "unable to malloc %zd bytes", filesize);
98 if ((*size = read(fd, *buf, filesize)) < 0)
99 err(1, "error reading '%s'", path);
100 /* XXX assuming no short reads */
101 if (*size != filesize)
102 errx(1,
103 "error reading '%s' (read %d bytes, requested %d bytes)",
104 path, *size, filesize);
103}
104
105static void
105}
106
107static void
106update_firmware(int fd, uint8_t *payload, uint32_t payload_size)
108update_firmware(int fd, uint8_t *payload, int32_t payload_size)
107{
108 struct nvme_pt_command pt;
109{
110 struct nvme_pt_command pt;
109 size_t size;
111 int32_t off, resid, size;
110 void *chunk;
112 void *chunk;
111 uint32_t off, resid;
112 int exit_code = EX_OK;
113
114 off = 0;
115 resid = payload_size;
116
113
114 off = 0;
115 resid = payload_size;
116
117 if ((chunk = malloc((size_t)NVME_MAX_XFER_SIZE)) == NULL) {
118 printf("Unable to malloc %d bytes.\n", NVME_MAX_XFER_SIZE);
119 exit(EX_IOERR);
120 }
117 if ((chunk = malloc(NVME_MAX_XFER_SIZE)) == NULL)
118 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
119
120 while (resid > 0) {
121 size = (resid >= NVME_MAX_XFER_SIZE) ?
122 NVME_MAX_XFER_SIZE : resid;
123 memcpy(chunk, payload + off, size);
124
125 memset(&pt, 0, sizeof(pt));
126 pt.cmd.opc = NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD;
127 pt.cmd.cdw10 = (size / sizeof(uint32_t)) - 1;
128 pt.cmd.cdw11 = (off / sizeof(uint32_t));
129 pt.buf = chunk;
130 pt.len = size;
131 pt.is_read = 0;
132
135 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) {
136 printf("Firmware image download request failed. "
137 "errno=%d (%s)\n",
138 errno, strerror(errno));
139 exit_code = EX_IOERR;
140 break;
141 }
133 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
134 err(1, "firmware download request failed");
142
135
143 if (nvme_completion_is_error(&pt.cpl)) {
144 printf("Passthrough command returned error.\n");
145 exit_code = EX_IOERR;
146 break;
147 }
136 if (nvme_completion_is_error(&pt.cpl))
137 errx(1, "firmware download request returned error");
148
149 resid -= size;
150 off += size;
151 }
138
139 resid -= size;
140 off += size;
141 }
152
153 if (exit_code != EX_OK)
154 exit(exit_code);
155}
156
157static void
158activate_firmware(int fd, int slot, int activate_action)
159{
160 struct nvme_pt_command pt;
161
162 memset(&pt, 0, sizeof(pt));
163 pt.cmd.opc = NVME_OPC_FIRMWARE_ACTIVATE;
164 pt.cmd.cdw10 = (activate_action << 3) | slot;
165 pt.is_read = 0;
166
142}
143
144static void
145activate_firmware(int fd, int slot, int activate_action)
146{
147 struct nvme_pt_command pt;
148
149 memset(&pt, 0, sizeof(pt));
150 pt.cmd.opc = NVME_OPC_FIRMWARE_ACTIVATE;
151 pt.cmd.cdw10 = (activate_action << 3) | slot;
152 pt.is_read = 0;
153
167 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) {
168 printf("Firmware activate request failed. errno=%d (%s)\n",
169 errno, strerror(errno));
170 exit(EX_IOERR);
171 }
154 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
155 err(1, "firmware activate request failed");
172
156
173 if (nvme_completion_is_error(&pt.cpl)) {
174 printf("Passthrough command returned error.\n");
175 exit(EX_IOERR);
176 }
157 if (nvme_completion_is_error(&pt.cpl))
158 errx(1, "firmware activate request returned error");
177}
178
179static void
180firmware_usage(void)
181{
182 fprintf(stderr, "usage:\n");
183 fprintf(stderr, FIRMWARE_USAGE);
159}
160
161static void
162firmware_usage(void)
163{
164 fprintf(stderr, "usage:\n");
165 fprintf(stderr, FIRMWARE_USAGE);
184 exit(EX_USAGE);
166 exit(1);
185}
186
187void
188firmware(int argc, char *argv[])
189{
190 int fd = -1, slot = 0;
191 int a_flag, s_flag, f_flag;
192 char ch, *p, *image = NULL;
193 char *controller = NULL, prompt[64];
194 void *buf = NULL;
167}
168
169void
170firmware(int argc, char *argv[])
171{
172 int fd = -1, slot = 0;
173 int a_flag, s_flag, f_flag;
174 char ch, *p, *image = NULL;
175 char *controller = NULL, prompt[64];
176 void *buf = NULL;
195 ssize_t size;
177 int32_t size = 0;
196 struct nvme_controller_data cdata;
197
198 a_flag = s_flag = f_flag = false;
199
200 while ((ch = getopt(argc, argv, "af:s:")) != -1) {
201 switch (ch) {
202 case 'a':
203 a_flag = true;

--- 43 unchanged lines hidden (view full) ---

247 "Slot number to activate not specified.\n");
248 firmware_usage();
249 }
250
251 controller = argv[optind];
252 open_dev(controller, &fd, 1, 1);
253 read_controller_data(fd, &cdata);
254
178 struct nvme_controller_data cdata;
179
180 a_flag = s_flag = f_flag = false;
181
182 while ((ch = getopt(argc, argv, "af:s:")) != -1) {
183 switch (ch) {
184 case 'a':
185 a_flag = true;

--- 43 unchanged lines hidden (view full) ---

229 "Slot number to activate not specified.\n");
230 firmware_usage();
231 }
232
233 controller = argv[optind];
234 open_dev(controller, &fd, 1, 1);
235 read_controller_data(fd, &cdata);
236
255 if (cdata.oacs.firmware == 0) {
256 fprintf(stderr,
257 "Controller does not support firmware "
258 "activate/download.\n");
259 exit(EX_IOERR);
260 }
237 if (cdata.oacs.firmware == 0)
238 errx(1,
239 "controller does not support firmware activate/download");
261
240
262 if (f_flag && slot == 1 && cdata.frmw.slot1_ro) {
263 fprintf(stderr, "Slot %d is marked as read only.\n", slot);
264 exit(EX_IOERR);
265 }
241 if (f_flag && slot == 1 && cdata.frmw.slot1_ro)
242 errx(1, "slot %d is marked as read only", slot);
266
243
267 if (slot > cdata.frmw.num_slots) {
268 fprintf(stderr,
269 "Slot %d was specified but controller only "
270 "supports %d firmware slots.\n",
244 if (slot > cdata.frmw.num_slots)
245 errx(1,
246 "slot %d specified but controller only supports %d slots",
271 slot, cdata.frmw.num_slots);
247 slot, cdata.frmw.num_slots);
272 exit(EX_IOERR);
273 }
274
248
275 if (!slot_has_valid_firmware(fd, slot)) {
276 fprintf(stderr,
277 "Slot %d does not contain valid firmware.\n"
278 "Try 'nvmecontrol logpage -p 3 %s' to get a list "
279 "of available firmware images.\n",
249 if (!slot_has_valid_firmware(fd, slot))
250 errx(1,
251 "slot %d does not contain valid firmware,\n"
252 "try 'nvmecontrol logpage -p 3 %s' to get a list "
253 "of available images\n",
280 slot, controller);
254 slot, controller);
281 exit(EX_IOERR);
282 }
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)

--- 9 unchanged lines hidden (view full) ---

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)
255
256 if (f_flag && a_flag)
257 printf("You are about to download and activate "
258 "firmware image (%s) to controller %s.\n"
259 "This may damage your controller and/or "
260 "overwrite an existing firmware image.\n",
261 image, controller);
262 else if (a_flag)

--- 9 unchanged lines hidden (view full) ---

272 image, controller);
273
274 printf("Are you sure you want to continue? (yes/no) ");
275 while (1) {
276 fgets(prompt, sizeof(prompt), stdin);
277 if (strncasecmp(prompt, "yes", 3) == 0)
278 break;
279 if (strncasecmp(prompt, "no", 2) == 0)
308 exit(EX_OK);
280 exit(1);
309 printf("Please answer \"yes\" or \"no\". ");
310 }
311
312 if (f_flag) {
313 read_image_file(image, &buf, &size);
314 update_firmware(fd, buf, size);
315 if (a_flag)
316 activate_firmware(fd, slot,

--- 9 unchanged lines hidden (view full) ---

326 printf("New firmware image activated and will take "
327 "effect after next controller reset.\n"
328 "Controller reset can be initiated via "
329 "'nvmecontrol reset %s'\n",
330 controller);
331 }
332
333 close(fd);
281 printf("Please answer \"yes\" or \"no\". ");
282 }
283
284 if (f_flag) {
285 read_image_file(image, &buf, &size);
286 update_firmware(fd, buf, size);
287 if (a_flag)
288 activate_firmware(fd, slot,

--- 9 unchanged lines hidden (view full) ---

298 printf("New firmware image activated and will take "
299 "effect after next controller reset.\n"
300 "Controller reset can be initiated via "
301 "'nvmecontrol reset %s'\n",
302 controller);
303 }
304
305 close(fd);
334 exit(EX_OK);
306 exit(0);
335}
307}