dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / fwflash / common / fwflash.c
bloba40cb69f233178fcbbeacfebc4e392f673295c1c
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
26 * fwflash.c
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <strings.h>
32 #include <errno.h>
33 #include <sys/queue.h>
34 #include <signal.h>
35 #include <locale.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <fcntl.h>
40 #include <dlfcn.h>
41 #include <dirent.h>
42 #include <sys/varargs.h>
43 #include <libintl.h> /* for gettext(3c) */
44 #include <libdevinfo.h>
45 #include <libscf_priv.h>
46 #include <fwflash/fwflash.h>
47 #include <sys/modctl.h> /* for MAXMODCONFNAME */
49 /* global arg list */
50 int fwflash_arg_list = 0;
51 char *filelist[10];
53 /* exposed global args */
54 di_node_t rootnode;
55 struct PLUGINLIST *fw_pluginlist;
56 struct DEVICELIST *fw_devices;
57 struct vrfyplugin *verifier;
58 struct fw_plugin *self;
59 int fwflash_debug = 0;
61 /* are we writing to flash? */
62 static int fwflash_in_write = 0;
65 * If we *must* track the version string for fwflash, then
66 * we should do so in this common file rather than the header
67 * file since it will then be in sync with what the customer
68 * sees. We should deprecate the "-v" option since it is not
69 * actually of any use - it doesn't line up with Mercurial's
70 * concept of the changeset.
72 #define FWFLASH_VERSION "v1.9"
73 #define FWFLASH_PROG_NAME "fwflash"
75 static int get_fileopts(char *options);
76 static int flash_device_list();
77 static int flash_load_plugins();
78 static int fwflash_update(char *device, char *filename, int flags);
79 static int fwflash_read_file(char *device, char *filename);
80 static int fwflash_list_fw(char *class);
81 static int fwflash_load_verifier(char *drv, char *vendorid, char *fwimg);
82 static void fwflash_intr(int sig);
83 static void fwflash_handle_signals(void);
84 static void fwflash_usage(char *arg);
85 static void fwflash_version(void);
86 static int confirm_target(struct devicelist *thisdev, char *file);
89 * FWFlash main code
91 int
92 main(int argc, char **argv)
94 int rv = FWFLASH_SUCCESS;
95 int i;
96 char ch;
97 char *read_file;
98 extern char *optarg;
99 char *devclass = NULL;
100 char *devpath = NULL;
102 /* local variables from env */
103 (void) setlocale(LC_ALL, "");
105 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
106 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it isn't. */
107 #endif
109 (void) textdomain(TEXT_DOMAIN);
111 read_file = NULL;
113 if (argc < 2) {
114 /* no args supplied */
115 fwflash_usage(NULL);
116 return (FWFLASH_FAILURE);
119 while ((ch = getopt(argc, argv, "hvylc:f:r:Qd:")) != EOF) {
120 switch (ch) {
121 case 'h':
122 fwflash_arg_list |= FWFLASH_HELP_FLAG;
123 break;
124 case 'v':
125 fwflash_arg_list |= FWFLASH_VER_FLAG;
126 break;
127 case 'y':
128 fwflash_arg_list |= FWFLASH_YES_FLAG;
129 break;
130 case 'l':
131 fwflash_arg_list |= FWFLASH_LIST_FLAG;
132 break;
133 case 'c':
134 fwflash_arg_list |= FWFLASH_CLASS_FLAG;
135 /* we validate later */
136 devclass = strdup(optarg);
137 break;
138 case 'd':
139 fwflash_arg_list |= FWFLASH_DEVICE_FLAG;
140 devpath = strdup(optarg);
141 break;
142 case 'f':
143 fwflash_arg_list |= FWFLASH_FW_FLAG;
144 if ((rv = get_fileopts(optarg)) != FWFLASH_SUCCESS) {
145 fwflash_usage(NULL);
146 return (FWFLASH_FAILURE);
148 break;
149 case 'r':
150 fwflash_arg_list |= FWFLASH_READ_FLAG;
151 read_file = strdup(optarg);
152 break;
153 case 'Q':
154 /* NOT in the manpage */
155 fwflash_debug = 1;
156 break;
157 /* illegal options */
158 default:
159 fwflash_usage(optarg);
160 return (FWFLASH_FAILURE);
164 /* Do Help */
165 if ((fwflash_arg_list & FWFLASH_HELP_FLAG) ||
166 ((fwflash_arg_list & FWFLASH_DEVICE_FLAG) &&
167 !((fwflash_arg_list & FWFLASH_FW_FLAG) ||
168 (fwflash_arg_list & FWFLASH_READ_FLAG)))) {
169 fwflash_usage(NULL);
170 return (FWFLASH_SUCCESS);
173 /* Do Version */
174 if (fwflash_arg_list == FWFLASH_VER_FLAG) {
175 fwflash_version();
176 return (FWFLASH_SUCCESS);
179 /* generate global list of devices */
180 if ((rv = flash_load_plugins()) != FWFLASH_SUCCESS) {
181 logmsg(MSG_ERROR,
182 gettext("Unable to load fwflash plugins\n"));
183 fwflash_intr(0);
184 return (rv);
187 if ((rv = flash_device_list()) != FWFLASH_SUCCESS) {
188 logmsg(MSG_ERROR,
189 gettext("No flashable devices in this system\n"));
190 fwflash_intr(0);
191 return (rv);
194 /* Do list */
195 if (fwflash_arg_list == (FWFLASH_LIST_FLAG) ||
196 fwflash_arg_list == (FWFLASH_LIST_FLAG | FWFLASH_CLASS_FLAG)) {
197 rv = fwflash_list_fw(devclass);
198 fwflash_intr(0);
199 return (rv);
202 fwflash_handle_signals();
204 /* Do flash update (write) */
205 if ((fwflash_arg_list == (FWFLASH_FW_FLAG | FWFLASH_DEVICE_FLAG)) ||
206 (fwflash_arg_list == (FWFLASH_FW_FLAG | FWFLASH_DEVICE_FLAG |
207 FWFLASH_YES_FLAG))) {
208 int fastreboot_disabled = 0;
209 /* the update function handles the real arg parsing */
210 i = 0;
211 while (filelist[i] != NULL) {
212 if ((rv = fwflash_update(devpath, filelist[i],
213 fwflash_arg_list)) == FWFLASH_SUCCESS) {
214 /* failed ops have already been noted */
215 if (!fastreboot_disabled &&
216 scf_fastreboot_default_set_transient(
217 B_FALSE) != SCF_SUCCESS)
218 logmsg(MSG_ERROR, gettext(
219 "Failed to disable fast "
220 "reboot.\n"));
221 else
222 fastreboot_disabled = 1;
223 logmsg(MSG_ERROR,
224 gettext("New firmware will be activated "
225 "after you reboot\n\n"));
227 ++i;
230 fwflash_intr(0);
231 return (rv);
234 /* Do flash read */
235 if ((fwflash_arg_list == (FWFLASH_READ_FLAG | FWFLASH_DEVICE_FLAG)) ||
236 (fwflash_arg_list == (FWFLASH_READ_FLAG | FWFLASH_DEVICE_FLAG |
237 FWFLASH_YES_FLAG))) {
238 rv = fwflash_read_file(devpath, read_file);
239 fwflash_intr(0);
240 return (rv);
243 fwflash_usage(NULL);
245 return (FWFLASH_FAILURE);
249 static int
250 flash_load_plugins()
253 int rval = FWFLASH_SUCCESS;
254 DIR *dirp;
255 struct dirent *plugdir;
256 char *plugname;
257 struct fw_plugin *tmpplug;
258 struct pluginlist *tmpelem;
259 void *sym;
260 char *fwplugdirpath, *tempdirpath;
263 #define CLOSEFREE() { \
264 (void) dlclose(tmpplug->handle); \
265 free(tmpplug); }
268 * Procedure:
270 * cd /usr/lib/fwflash/identify
271 * open each .so file found therein
272 * dlopen(.sofile)
273 * if it's one of our plugins, add it to fw_pluginlist;
275 * functions we need here include dlopen and dlsym.
277 * If we get to the end and fw_pluginlist struct is empty,
278 * return FWFLASH_FAILURE so we return to the shell.
281 if ((fwplugdirpath = calloc(1, MAXPATHLEN + 1)) == NULL) {
282 logmsg(MSG_ERROR,
283 gettext("Unable to malloc %d bytes while "
284 "trying to load plugins: %s\n"),
285 MAXPATHLEN + 1, strerror(errno));
286 return (FWFLASH_FAILURE);
289 tempdirpath = getenv("FWPLUGINDIR");
291 if ((fwflash_debug > 0) && (tempdirpath != NULL)) {
292 (void) strlcpy(fwplugdirpath, tempdirpath,
293 strlen(tempdirpath) + 1);
294 } else {
295 (void) strlcpy(fwplugdirpath, FWPLUGINDIR,
296 strlen(FWPLUGINDIR) + 1);
299 if ((dirp = opendir(fwplugdirpath)) == NULL) {
300 logmsg(MSG_ERROR,
301 gettext("Unable to open %s\n"),
302 fwplugdirpath);
303 return (errno);
306 if ((plugdir = calloc(1, sizeof (struct dirent) + MAXPATHLEN + 1))
307 == NULL) {
308 logmsg(MSG_ERROR,
309 gettext("Unable to malloc %d bytes while "
310 "trying to load plugins: %s\n"),
311 MAXPATHLEN + 1 + sizeof (struct dirent),
312 strerror(errno));
313 return (FWFLASH_FAILURE);
316 if ((fw_pluginlist = calloc(1, sizeof (struct fw_plugin)))
317 == NULL) {
318 logmsg(MSG_ERROR,
319 gettext("Unable to malloc %d bytes while "
320 "trying to load plugins: %s\n"),
321 sizeof (struct fw_plugin), strerror(errno));
322 return (FWFLASH_FAILURE);
325 TAILQ_INIT(fw_pluginlist);
327 while ((readdir_r(dirp, plugdir, &plugdir) == 0) && (plugdir != NULL)) {
329 errno = 0; /* remove chance of false results */
331 if ((plugdir->d_name[0] == '.') ||
332 (strstr(plugdir->d_name, ".so") == NULL)) {
333 continue;
336 if ((plugname = calloc(1, MAXPATHLEN + 1)) == NULL) {
337 logmsg(MSG_ERROR,
338 gettext("Unable to malloc %d bytes while "
339 "trying to load plugins: %s\n"),
340 MAXPATHLEN + 1, strerror(errno));
341 return (FWFLASH_FAILURE);
344 (void) snprintf(plugname, MAXPATHLEN, "%s/%s",
345 fwplugdirpath, plugdir->d_name);
347 /* start allocating storage */
348 if ((tmpelem = calloc(1, sizeof (struct pluginlist)))
349 == NULL) {
350 logmsg(MSG_ERROR,
351 gettext("Unable to malloc %d bytes while "
352 "trying to load plugins: %s\n"),
353 sizeof (struct pluginlist), strerror(errno));
354 return (FWFLASH_FAILURE);
357 if ((tmpplug = calloc(1, sizeof (struct fw_plugin)))
358 == NULL) {
359 logmsg(MSG_ERROR,
360 gettext("Unable to malloc %d bytes while "
361 "trying to load plugins: %s\n"),
362 sizeof (struct fw_plugin), strerror(errno));
363 return (FWFLASH_FAILURE);
366 /* load 'er up! */
367 tmpplug->handle = dlopen(plugname, RTLD_NOW);
368 if (tmpplug->handle == NULL) {
369 free(tmpplug);
370 continue; /* assume there are other plugins */
373 if ((tmpplug->filename = calloc(1, strlen(plugname) + 1))
374 == NULL) {
375 logmsg(MSG_ERROR,
376 gettext("Unable to allocate %d bytes for plugin "
377 "filename %s:%s\n"),
378 strlen(plugname) + 1, plugname,
379 strerror(errno));
380 return (rval);
383 (void) strlcpy(tmpplug->filename, plugname,
384 strlen(plugname) + 1);
386 /* now sanity check the file */
387 if ((sym = dlsym(tmpplug->handle, "drivername"))
388 != NULL) {
389 /* max length of drivername */
390 tmpplug->drvname = calloc(1, MAXMODCONFNAME);
392 /* are we doing double-time? */
393 if (strncmp((char *)sym, plugdir->d_name,
394 MAXMODCONFNAME) != 0) {
395 char *tempnm = calloc(1, MAXMODCONFNAME);
397 (void) memcpy(tempnm, plugdir->d_name,
398 MAXMODCONFNAME);
399 (void) strlcpy(tmpplug->drvname,
400 strtok(tempnm, "."),
401 strlen(plugdir->d_name) + 1);
402 free(tempnm);
403 } else {
404 (void) strlcpy(tmpplug->drvname,
405 (char *)sym, strlen(sym) + 1);
407 } else {
408 CLOSEFREE();
409 continue;
411 if ((sym = dlsym(tmpplug->handle, "fw_readfw"))
412 != NULL) {
413 tmpplug->fw_readfw = (int (*)())sym;
414 } else {
415 CLOSEFREE();
416 continue;
418 if ((sym = dlsym(tmpplug->handle, "fw_writefw"))
419 != NULL) {
420 tmpplug->fw_writefw = (int (*)())sym;
421 } else {
422 CLOSEFREE();
423 continue;
426 if ((sym = dlsym(tmpplug->handle, "fw_identify"))
427 != NULL) {
428 tmpplug->fw_identify =
429 (int (*)(int))sym;
430 } else {
431 CLOSEFREE();
432 continue;
434 if ((sym = dlsym(tmpplug->handle, "fw_devinfo"))
435 != NULL) {
436 tmpplug->fw_devinfo =
437 (int (*)(struct devicelist *))sym;
438 } else {
439 CLOSEFREE();
440 continue;
443 if ((sym = dlsym(tmpplug->handle, "plugin_version"))
444 != NULL) {
445 if ((*(int *)sym) >= FWPLUGIN_VERSION_2) {
446 if ((sym = dlsym(tmpplug->handle,
447 "fw_cleanup")) != NULL) {
448 tmpplug->fw_cleanup =
449 (void (*)(struct devicelist *))sym;
450 } else {
451 logmsg(MSG_ERROR,
452 gettext("ERROR: v2 plugin (%s) "
453 "has no fw_cleanup function\n"),
454 tmpplug->filename);
455 CLOSEFREE();
456 continue;
458 } else {
459 logmsg(MSG_INFO,
460 "Identification plugin %s defined "
461 "plugin_version < FWPLUGIN_VERSION_2 !");
465 if ((tmpelem->drvname = calloc(1, MAXMODCONFNAME))
466 == NULL) {
467 logmsg(MSG_ERROR,
468 gettext("Unable to allocate space for a"
469 "drivername %s\n"),
470 tmpplug->drvname);
471 return (FWFLASH_FAILURE);
474 (void) strlcpy(tmpelem->drvname, tmpplug->drvname,
475 strlen(tmpplug->drvname) + 1);
477 if ((tmpelem->filename = calloc(1,
478 strlen(tmpplug->filename) + 1)) == NULL) {
479 logmsg(MSG_ERROR,
480 gettext("Unable to allocate %d bytes for "
481 "filename %s\n"),
482 strlen(tmpplug->filename) + 1,
483 tmpplug->filename);
484 return (FWFLASH_FAILURE);
487 (void) strlcpy(tmpelem->filename, plugname,
488 strlen(plugname) + 1);
489 tmpelem->plugin = tmpplug;
491 /* CONSTCOND */
492 TAILQ_INSERT_TAIL(fw_pluginlist, tmpelem, nextplugin);
495 if ((plugdir == NULL) && TAILQ_EMPTY(fw_pluginlist)) {
496 return (FWFLASH_FAILURE);
499 if (errno != 0) {
500 logmsg(MSG_ERROR,
501 gettext("Error reading directory entry in %s\n"),
502 fwplugdirpath);
503 rval = errno;
506 free(fwplugdirpath);
507 free(plugdir);
508 (void) closedir(dirp);
509 return (rval);
513 * fwflash_load_verifier dlload()s the appropriate firmware image
514 * verification plugin, and attaches the designated fwimg's fd to
515 * the vrfyplugin structure so we only have to load the image in
516 * one place.
519 fwflash_load_verifier(char *drv, char *vendorid, char *fwimg)
522 int rv = FWFLASH_FAILURE;
523 int imgfd;
524 char *fwvrfydirpath, *tempdirpath, *filename;
525 char *clean; /* for the space-removed vid */
526 struct stat fwstat;
527 struct vrfyplugin *vrfy;
528 void *vrfysym;
531 * To make flashing multiple firmware images somewhat more
532 * efficient, we start this function by checking whether a
533 * verifier for this device has already been loaded. If it
534 * has been loaded, we replace the imgfile information, and
535 * then continue as if we were loading for the first time.
538 if (verifier != NULL) {
539 verifier->imgsize = 0;
540 verifier->flashbuf = 0; /* set by the verifier function */
542 if (verifier->imgfile != NULL) {
543 free(verifier->imgfile);
544 verifier->imgfile = NULL;
547 if (verifier->fwimage != NULL) {
548 free(verifier->fwimage);
549 verifier->fwimage = NULL;
551 } else {
552 if ((fwvrfydirpath = calloc(1, MAXPATHLEN + 1)) == NULL) {
553 logmsg(MSG_ERROR,
554 gettext("Unable to allocate space for a firmware "
555 "verifier file(1)"));
556 return (rv);
559 if ((filename = calloc(1, MAXPATHLEN + 1)) == NULL) {
560 logmsg(MSG_ERROR,
561 gettext("Unable to allocate space "
562 "for a firmware verifier file(2)"));
563 free(fwvrfydirpath);
564 return (rv);
568 * Since SCSI devices can have a vendor id of up to 8
569 * left-aligned and _space-padded_ characters, we first need to
570 * strip off any space characters before we try to make a
571 * filename out of it
573 clean = strtok(vendorid, " ");
574 if (clean == NULL) {
575 /* invalid vendorid, something's really wrong */
576 logmsg(MSG_ERROR,
577 gettext("Invalid vendorid (null) specified for "
578 "device\n"));
579 free(filename);
580 free(fwvrfydirpath);
581 return (rv);
584 tempdirpath = getenv("FWVERIFYPLUGINDIR");
586 if ((fwflash_debug > 0) && (tempdirpath != NULL)) {
587 (void) strlcpy(fwvrfydirpath, tempdirpath,
588 strlen(tempdirpath) + 1);
589 } else {
590 (void) strlcpy(fwvrfydirpath, FWVERIFYPLUGINDIR,
591 strlen(FWVERIFYPLUGINDIR) + 1);
594 if ((vrfy = calloc(1, sizeof (struct vrfyplugin))) == NULL) {
595 logmsg(MSG_ERROR,
596 gettext("Unable to allocate space "
597 "for a firmware verifier structure"));
598 free(filename);
599 free(fwvrfydirpath);
600 return (rv);
603 errno = 0; /* false positive removal */
605 (void) snprintf(filename, MAXPATHLEN, "%s/%s-%s.so",
606 fwvrfydirpath, drv, clean);
607 if ((vrfy->handle = dlopen(filename, RTLD_NOW)) == NULL) {
608 logmsg(MSG_INFO, gettext(dlerror()));
609 logmsg(MSG_INFO,
610 gettext("\nUnable to open verification plugin "
611 "%s. Looking for %s-GENERIC plugin instead.\n"),
612 filename, drv);
614 /* Try the drv-GENERIC.so form, _then_ die */
615 bzero(filename, strlen(filename) + 1);
616 (void) snprintf(filename, MAXPATHLEN,
617 "%s/%s-GENERIC.so", fwvrfydirpath, drv);
619 if ((vrfy->handle = dlopen(filename, RTLD_NOW))
620 == NULL) {
621 logmsg(MSG_INFO, gettext(dlerror()));
622 logmsg(MSG_ERROR,
623 gettext("\nUnable to open either "
624 "verification plugin %s/%s-%s.so or "
625 "generic plugin %s.\nUnable to verify "
626 "firmware image. Aborting.\n"),
627 fwvrfydirpath, drv, clean, filename);
628 free(filename);
629 free(fwvrfydirpath);
630 return (rv);
634 if ((vrfy->filename = calloc(1, strlen(filename) + 1))
635 == NULL) {
636 logmsg(MSG_ERROR,
637 gettext("Unable to allocate space to store "
638 "a verifier filename\n"));
639 free(filename);
640 free(fwvrfydirpath);
641 free(vrfy->handle);
642 return (rv);
644 (void) strlcpy(vrfy->filename, filename, strlen(filename) + 1);
646 if ((vrfysym = dlsym(vrfy->handle, "vendorvrfy")) == NULL) {
647 logmsg(MSG_ERROR,
648 gettext("%s is an invalid firmware verification "
649 "plugin."), filename);
650 (void) dlclose(vrfy->handle);
651 free(filename);
652 free(fwvrfydirpath);
653 free(vrfy);
654 return (rv);
655 } else {
656 vrfy->vendorvrfy =
657 (int (*)(struct devicelist *))vrfysym;
660 vrfysym = dlsym(vrfy->handle, "vendor");
662 if (vrfysym == NULL) {
663 logmsg(MSG_ERROR,
664 gettext("Invalid vendor (null) in verification "
665 "plugin %s\n"), filename);
666 (void) dlclose(vrfy->handle);
667 free(vrfy);
668 return (rv);
669 } else {
670 if (strncmp(vendorid, (char *)vrfysym,
671 strlen(vendorid)) != 0) {
672 logmsg(MSG_INFO,
673 "Using a sym-linked (%s -> %s) "
674 "verification plugin\n",
675 vendorid, vrfysym);
676 vrfy->vendor = calloc(1, strlen(vendorid) + 1);
677 } else {
678 vrfy->vendor = calloc(1, strlen(vrfysym) + 1);
680 (void) strlcpy(vrfy->vendor, (char *)vrfysym,
681 strlen(vendorid) + 1);
684 verifier = vrfy; /* a convenience variable */
685 free(filename);
686 free(fwvrfydirpath);
690 * We don't do any verification that the fw image file is in
691 * an approved location, but it's easy enough to modify this
692 * function to do so. The verification plugin should provide
693 * sufficient protection.
696 if ((imgfd = open(fwimg, O_RDONLY)) < 0) {
697 logmsg(MSG_ERROR,
698 gettext("Unable to open designated firmware "
699 "image file %s: %s\n"),
700 (fwimg != NULL) ? fwimg : "(null)",
701 strerror(errno));
702 rv = FWFLASH_FAILURE;
703 goto cleanup;
706 if (stat(fwimg, &fwstat) == -1) {
707 logmsg(MSG_ERROR,
708 gettext("Unable to stat() firmware image file "
709 "%s: %s\n"),
710 fwimg, strerror(errno));
711 rv = FWFLASH_FAILURE;
712 goto cleanup;
713 } else {
714 verifier->imgsize = fwstat.st_size;
715 if ((verifier->fwimage = calloc(1, verifier->imgsize))
716 == NULL) {
717 logmsg(MSG_ERROR,
718 gettext("Unable to load firmware image "
719 "%s: %s\n"),
720 fwimg, strerror(errno));
721 rv = FWFLASH_FAILURE;
722 goto cleanup;
726 errno = 0;
727 if ((rv = read(imgfd, verifier->fwimage,
728 (size_t)verifier->imgsize)) < verifier->imgsize) {
729 /* we haven't read enough data, bail */
730 logmsg(MSG_ERROR,
731 gettext("Failed to read sufficient data "
732 "(got %d bytes, expected %d bytes) from "
733 "firmware image file %s: %s\n"),
734 rv, verifier->imgsize,
735 verifier->filename, strerror(errno));
736 rv = FWFLASH_FAILURE;
737 } else {
738 rv = FWFLASH_SUCCESS;
741 if ((verifier->imgfile = calloc(1, strlen(fwimg) + 1)) == NULL) {
742 logmsg(MSG_ERROR,
743 gettext("Unable to save name of firmware image\n"));
744 rv = FWFLASH_FAILURE;
745 } else {
746 (void) strlcpy(verifier->imgfile, fwimg, strlen(fwimg) + 1);
749 if (rv != FWFLASH_SUCCESS) {
750 /* cleanup and let's get outta here */
751 cleanup:
752 free(verifier->filename);
753 free(verifier->vendor);
755 if (!(fwflash_arg_list & FWFLASH_READ_FLAG))
756 free(verifier->fwimage);
758 verifier->filename = NULL;
759 verifier->vendor = NULL;
760 verifier->vendorvrfy = NULL;
761 verifier->fwimage = NULL;
762 (void) dlclose(verifier->handle);
763 verifier->handle = NULL;
764 free(verifier);
765 if (imgfd >= 0) {
766 (void) close(imgfd);
768 verifier = NULL;
771 return (rv);
775 * cycles through the global list of plugins to find
776 * each flashable device, which is added to fw_devices
778 * Each plugin's identify routine must allocated storage
779 * as required.
781 * Each plugin's identify routine must return
782 * FWFLASH_FAILURE if it cannot find any devices
783 * which it handles.
785 static int
786 flash_device_list()
788 int rv = FWFLASH_FAILURE;
789 int startidx = 0;
790 int sumrv = 0;
791 struct pluginlist *plugins;
793 /* we open rootnode here, and close it in fwflash_intr */
794 if ((rootnode = di_init("/", DINFOCPYALL|DINFOFORCE)) == DI_NODE_NIL) {
795 logmsg(MSG_ERROR,
796 gettext("Unable to take device tree snapshot: %s\n"),
797 strerror(errno));
798 return (rv);
801 if ((fw_devices = calloc(1, sizeof (struct devicelist))) == NULL) {
802 logmsg(MSG_ERROR,
803 gettext("Unable to malloc %d bytes while "
804 "trying to find devices: %s\n"),
805 sizeof (struct devicelist), strerror(errno));
806 return (FWFLASH_FAILURE);
809 /* CONSTCOND */
810 TAILQ_INIT(fw_devices);
812 TAILQ_FOREACH(plugins, fw_pluginlist, nextplugin) {
813 self = plugins->plugin;
814 rv = plugins->plugin->fw_identify(startidx);
816 logmsg(MSG_INFO,
817 gettext("fwflash:flash_device_list() got %d from "
818 "identify routine\n"), rv);
820 /* only bump startidx if we've found at least one device */
821 if (rv == FWFLASH_SUCCESS) {
822 startidx += 100;
823 sumrv++;
824 } else {
825 logmsg(MSG_INFO,
826 gettext("No flashable devices attached with "
827 "the %s driver in this system\n"),
828 plugins->drvname);
832 if (sumrv > 0)
833 rv = FWFLASH_SUCCESS;
835 return (rv);
838 static int
839 fwflash_list_fw(char *class)
841 int rv = 0;
842 struct devicelist *curdev;
843 int header = 1;
845 TAILQ_FOREACH(curdev, fw_devices, nextdev) {
847 /* we're either class-conscious, or we're not */
848 if (((class != NULL) &&
849 ((strncmp(curdev->classname, "ALL", 3) == 0) ||
850 (strcmp(curdev->classname, class) == 0))) ||
851 (class == NULL)) {
853 if (header != 0) {
854 (void) fprintf(stdout,
855 gettext("List of available devices:\n"));
856 header--;
859 * If any plugin's fw_devinfo() function returns
860 * FWFLASH_FAILURE then we want to keep track of
861 * it. _Most_ plugins should always return
862 * FWFLASH_SUCCESS from this function. The only
863 * exception known at this point is the tavor plugin.
865 rv += curdev->plugin->fw_devinfo(curdev);
868 return (rv);
871 static int
872 fwflash_update(char *device, char *filename, int flags)
875 int rv = FWFLASH_FAILURE;
876 int needsfree = 0;
877 int found = 0;
878 struct devicelist *curdev;
879 char *realfile;
882 * Here's how we operate:
884 * We perform some basic checks on the args, then walk
885 * through the device list looking for the device which
886 * matches. We then load the appropriate verifier for the
887 * image file and device, verify the image, then call the
888 * fw_writefw() function of the appropriate plugin.
890 * There is no "force" flag to enable you to flash a firmware
891 * image onto an incompatible device because the verifier
892 * will return FWFLASH_FAILURE if the image doesn't match.
895 /* new firmware filename and device desc */
896 if (filename == NULL) {
897 logmsg(MSG_ERROR,
898 gettext("Invalid firmware filename (null)\n"));
899 return (FWFLASH_FAILURE);
902 if (device == NULL) {
903 logmsg(MSG_ERROR,
904 gettext("Invalid device requested (null)\n"));
905 return (FWFLASH_FAILURE);
908 if ((realfile = calloc(1, PATH_MAX + 1)) == NULL) {
909 logmsg(MSG_ERROR,
910 gettext("Unable to allocate space for device "
911 "filename, operation might fail if %s is"
912 "a symbolic link\n"),
913 device);
914 realfile = device;
915 } else {
917 * If realpath() succeeds, then we have a valid
918 * device filename in realfile.
920 if (realpath(device, realfile) == NULL) {
921 logmsg(MSG_ERROR,
922 gettext("Unable to resolve device filename"
923 ": %s\n"),
924 strerror(errno));
925 /* tidy up */
926 free(realfile);
927 /* realpath didn't succeed, use fallback */
928 realfile = device;
929 } else {
930 needsfree = 1;
934 logmsg(MSG_INFO,
935 gettext("fwflash_update: fw_filename (%s) device (%s)\n"),
936 filename, device);
938 TAILQ_FOREACH(curdev, fw_devices, nextdev) {
939 if (strcmp(curdev->access_devname, realfile) == 0) {
940 found++;
941 rv = fwflash_load_verifier(curdev->drvname,
942 curdev->ident->vid, filename);
943 if (rv == FWFLASH_FAILURE) {
944 logmsg(MSG_ERROR,
945 gettext("Unable to load verifier "
946 "for device %s\n"),
947 curdev->access_devname);
948 return (FWFLASH_FAILURE);
950 rv = verifier->vendorvrfy(curdev);
951 if (rv == FWFLASH_FAILURE) {
952 /* the verifier prints a message */
953 logmsg(MSG_INFO,
954 "verifier (%s) for %s :: %s returned "
955 "FWFLASH_FAILURE\n",
956 verifier->filename,
957 filename, curdev->access_devname);
958 return (rv);
961 if (((flags & FWFLASH_YES_FLAG) == FWFLASH_YES_FLAG) ||
962 (rv = confirm_target(curdev, filename)) ==
963 FWFLASH_YES_FLAG) {
964 logmsg(MSG_INFO,
965 "about to flash using plugin %s\n",
966 curdev->plugin->filename);
967 rv = curdev->plugin->fw_writefw(curdev,
968 filename);
969 if (rv == FWFLASH_FAILURE) {
970 logmsg(MSG_ERROR,
971 gettext("Failed to flash "
972 "firmware file %s on "
973 "device %s: %d\n"),
974 filename,
975 curdev->access_devname, rv);
977 } else {
978 logmsg(MSG_ERROR,
979 gettext("Flash operation not confirmed "
980 "by user\n"),
981 curdev->access_devname);
982 rv = FWFLASH_FAILURE;
987 if (!found)
988 /* report the same device that the user passed in */
989 logmsg(MSG_ERROR,
990 gettext("Device %s does not appear "
991 "to be flashable\n"),
992 ((strncmp(device, realfile, strlen(device)) == 0) ?
993 realfile : device));
995 if (needsfree)
996 free(realfile);
998 return (rv);
1002 * We validate that the device path is in our global device list and
1003 * that the filename exists, then palm things off to the relevant plugin.
1005 static int
1006 fwflash_read_file(char *device, char *filename)
1008 struct devicelist *curdev;
1009 int rv;
1010 int found = 0;
1012 /* new firmware filename and device desc */
1014 TAILQ_FOREACH(curdev, fw_devices, nextdev) {
1015 if (strncmp(curdev->access_devname, device,
1016 MAXPATHLEN) == 0) {
1017 rv = curdev->plugin->fw_readfw(curdev, filename);
1019 if (rv != FWFLASH_SUCCESS)
1020 logmsg(MSG_ERROR,
1021 gettext("Unable to write out firmware "
1022 "image for %s to file %s\n"),
1023 curdev->access_devname, filename);
1024 found++;
1029 if (!found) {
1030 logmsg(MSG_ERROR,
1031 gettext("No device matching %s was found.\n"),
1032 device);
1033 rv = FWFLASH_FAILURE;
1036 return (rv);
1039 static void
1040 fwflash_usage(char *arg)
1043 (void) fprintf(stderr, "\n");
1044 if (arg != NULL) {
1045 logmsg(MSG_ERROR,
1046 gettext("Invalid argument (%s) supplied\n"), arg);
1049 (void) fprintf(stderr, "\n");
1051 (void) fprintf(stdout, gettext("Usage:\n\t"));
1052 (void) fprintf(stdout, gettext("fwflash [-l [-c device_class "
1053 "| ALL]] | [-v] | [-h]\n\t"));
1054 (void) fprintf(stdout, gettext("fwflash [-f file1,file2,file3"
1055 ",... | -r file] [-y] -d device_path\n\n"));
1056 (void) fprintf(stdout, "\n"); /* workaround for xgettext */
1058 (void) fprintf(stdout,
1059 gettext("\t-l\t\tlist flashable devices in this system\n"
1060 "\t-c device_class limit search to a specific class\n"
1061 "\t\t\teg IB for InfiniBand, ses for SCSI Enclosures\n"
1062 "\t-v\t\tprint version number of fwflash utility\n"
1063 "\t-h\t\tprint this usage message\n\n"));
1064 (void) fprintf(stdout,
1065 gettext("\t-f file1,file2,file3,...\n"
1066 "\t\t\tfirmware image file list to flash\n"
1067 "\t-r file\t\tfile to dump device firmware to\n"
1068 "\t-y\t\tanswer Yes/Y/y to prompts\n"
1069 "\t-d device_path\tpathname of device to be flashed\n\n"));
1071 (void) fprintf(stdout,
1072 gettext("\tIf -d device_path is specified, then one of -f "
1073 "<files>\n"
1074 "\tor -r <file> must also be specified\n\n"));
1076 (void) fprintf(stdout,
1077 gettext("\tIf multiple firmware images are required to be "
1078 "flashed\n"
1079 "\tthey must be listed together, separated by commas. The\n"
1080 "\timages will be flashed in the order specified.\n\n"));
1082 (void) fprintf(stdout, "\n");
1085 static void
1086 fwflash_version(void)
1088 (void) fprintf(stdout, gettext("\n%s: "), FWFLASH_PROG_NAME);
1089 (void) fprintf(stdout, gettext("version %s\n"),
1090 FWFLASH_VERSION);
1093 static void
1094 fwflash_intr(int sig)
1097 struct devicelist *thisdev;
1098 struct pluginlist *thisplug;
1100 (void) signal(SIGINT, SIG_IGN);
1101 (void) signal(SIGTERM, SIG_IGN);
1102 (void) signal(SIGABRT, SIG_IGN);
1104 if (fwflash_in_write) {
1105 (void) fprintf(stderr,
1106 gettext("WARNING: firmware image may be corrupted\n\t"));
1107 (void) fprintf(stderr,
1108 gettext("Reflash firmware before rebooting!\n"));
1111 if (sig > 0) {
1112 (void) logmsg(MSG_ERROR, gettext("\n"));
1113 (void) logmsg(MSG_ERROR,
1114 gettext("fwflash exiting due to signal (%d)\n"), sig);
1118 * we need to close everything down properly, so
1119 * call the plugin closure routines
1121 if (fw_devices != NULL) {
1122 TAILQ_FOREACH(thisdev, fw_devices, nextdev) {
1123 if (thisdev->plugin->fw_cleanup != NULL) {
1125 * If we've got a cleanup routine, it
1126 * cleans up _everything_ for thisdev
1128 thisdev->plugin->fw_cleanup(thisdev);
1129 } else {
1130 /* free the components first */
1131 free(thisdev->access_devname);
1132 free(thisdev->drvname);
1133 free(thisdev->classname);
1134 free(thisdev->ident);
1135 /* We don't free address[] for old plugins */
1136 thisdev->ident = NULL;
1137 thisdev->plugin = NULL;
1139 /* CONSTCOND */
1140 TAILQ_REMOVE(fw_devices, thisdev, nextdev);
1144 if (fw_pluginlist != NULL) {
1145 TAILQ_FOREACH(thisplug, fw_pluginlist, nextplugin) {
1146 free(thisplug->filename);
1147 free(thisplug->drvname);
1148 free(thisplug->plugin->filename);
1149 free(thisplug->plugin->drvname);
1150 thisplug->filename = NULL;
1151 thisplug->drvname = NULL;
1152 thisplug->plugin->filename = NULL;
1153 thisplug->plugin->drvname = NULL;
1154 thisplug->plugin->fw_readfw = NULL;
1155 thisplug->plugin->fw_writefw = NULL;
1156 thisplug->plugin->fw_identify = NULL;
1157 thisplug->plugin->fw_devinfo = NULL;
1158 thisplug->plugin->fw_cleanup = NULL;
1159 (void) dlclose(thisplug->plugin->handle);
1160 thisplug->plugin->handle = NULL;
1161 free(thisplug->plugin);
1162 thisplug->plugin = NULL;
1163 /* CONSTCOND */
1164 TAILQ_REMOVE(fw_pluginlist, thisplug, nextplugin);
1168 if (verifier != NULL) {
1169 free(verifier->filename);
1170 free(verifier->vendor);
1171 free(verifier->imgfile);
1172 free(verifier->fwimage);
1173 verifier->filename = NULL;
1174 verifier->vendor = NULL;
1175 verifier->vendorvrfy = NULL;
1176 verifier->imgfile = NULL;
1177 verifier->fwimage = NULL;
1178 (void) dlclose(verifier->handle);
1179 verifier->handle = NULL;
1180 free(verifier);
1182 di_fini(rootnode);
1184 if (sig > 0)
1185 exit(FWFLASH_FAILURE);
1188 static void
1189 fwflash_handle_signals(void)
1191 if (signal(SIGINT, fwflash_intr) == SIG_ERR) {
1192 perror("signal");
1193 exit(FWFLASH_FAILURE);
1196 if (signal(SIGTERM, fwflash_intr) == SIG_ERR) {
1197 perror("signal");
1198 exit(FWFLASH_FAILURE);
1202 static int
1203 confirm_target(struct devicelist *thisdev, char *file)
1205 int resp;
1207 (void) fflush(stdin);
1208 (void) printf(gettext("About to update firmware on %s\n"),
1209 thisdev->access_devname);
1210 (void) printf(gettext("with file %s.\n"
1211 "Do you want to continue? (Y/N): "), file);
1213 resp = getchar();
1214 if (resp == 'Y' || resp == 'y') {
1215 return (FWFLASH_YES_FLAG);
1216 } else {
1217 logmsg(MSG_INFO, "flash operation NOT confirmed.\n");
1220 (void) fflush(stdin);
1221 return (FWFLASH_FAILURE);
1225 get_fileopts(char *options)
1228 int i;
1229 char *files;
1231 if (files = strtok(options, ",")) {
1232 /* we have more than one */
1233 if ((filelist[0] = calloc(1, MAXPATHLEN + 1)) == NULL) {
1234 logmsg(MSG_ERROR,
1235 gettext("Unable to allocate space for "
1236 "a firmware image filename\n"));
1237 return (FWFLASH_FAILURE);
1239 (void) strlcpy(filelist[0], files, strlen(files) + 1);
1240 i = 1;
1242 logmsg(MSG_INFO, "fwflash: filelist[0]: %s\n",
1243 filelist[0]);
1246 while (files = strtok(NULL, ",")) {
1247 if ((filelist[i] = calloc(1, MAXPATHLEN + 1))
1248 == NULL) {
1249 logmsg(MSG_ERROR,
1250 gettext("Unable to allocate space for "
1251 "a firmware image filename\n"));
1252 return (FWFLASH_FAILURE);
1254 (void) strlcpy(filelist[i], files,
1255 strlen(files) + 1);
1256 logmsg(MSG_INFO, "fwflash: filelist[%d]: %s\n",
1257 i, filelist[i]);
1258 ++i;
1260 } else {
1261 if ((filelist[0] = calloc(1, MAXPATHLEN + 1)) == NULL) {
1262 logmsg(MSG_ERROR,
1263 gettext("Unable to allocate space for "
1264 "a firmware image filename\n"));
1265 return (FWFLASH_FAILURE);
1267 (void) strlcpy(filelist[0], options, strlen(files) + 1);
1268 logmsg(MSG_INFO, "fwflash: filelist[0]: %s\n",
1269 filelist[0]);
1271 return (FWFLASH_SUCCESS);
1275 * code reuse - cheerfully borrowed from stmsboot_util.c
1277 void
1278 logmsg(int severity, const char *msg, ...)
1280 va_list ap;
1282 if ((severity > MSG_INFO) ||
1283 ((severity == MSG_INFO) && (fwflash_debug > 0))) {
1284 (void) fprintf(stderr, "%s: ", FWFLASH_PROG_NAME);
1285 va_start(ap, msg);
1286 (void) vfprintf(stderr, msg, ap);
1287 va_end(ap);