fwflash.c revision 41f7470a4f7ae77ab9654d259b22ed23840d0f38
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
/*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <errno.h>
#include <signal.h>
#include <locale.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <dirent.h>
#include <libintl.h> /* for gettext(3c) */
#include <libdevinfo.h>
#include <libscf_priv.h>
/* global arg list */
int fwflash_arg_list = 0;
char *filelist[10];
/* exposed global args */
struct PLUGINLIST *fw_pluginlist;
struct DEVICELIST *fw_devices;
struct vrfyplugin *verifier;
int fwflash_debug = 0;
/* are we writing to flash? */
static int fwflash_in_write = 0;
/*
* If we *must* track the version string for fwflash, then
* we should do so in this common file rather than the header
* file since it will then be in sync with what the customer
* sees. We should deprecate the "-v" option since it is not
* actually of any use - it doesn't line up with Mercurial's
* concept of the changeset.
*/
#define FWFLASH_VERSION "v1.9"
#define FWFLASH_PROG_NAME "fwflash"
static int get_fileopts(char *options);
static int flash_device_list();
static int flash_load_plugins();
static int fwflash_list_fw(char *class);
static void fwflash_intr(int sig);
static void fwflash_handle_signals(void);
static void fwflash_usage(char *arg);
static void fwflash_version(void);
/*
* FWFlash main code
*/
int
{
int rv = FWFLASH_SUCCESS;
int i;
char ch;
char *read_file;
extern char *optarg;
/* local variables from env */
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
if (argc < 2) {
/* no args supplied */
return (FWFLASH_FAILURE);
}
switch (ch) {
case 'h':
break;
case 'v':
break;
case 'y':
break;
case 'l':
break;
case 'c':
/* we validate later */
break;
case 'd':
break;
case 'f':
return (FWFLASH_FAILURE);
}
break;
case 'r':
break;
case 'Q':
/* NOT in the manpage */
fwflash_debug = 1;
break;
/* illegal options */
default:
return (FWFLASH_FAILURE);
}
}
/* Do Help */
if ((fwflash_arg_list & FWFLASH_HELP_FLAG) ||
((fwflash_arg_list & FWFLASH_DEVICE_FLAG) &&
!((fwflash_arg_list & FWFLASH_FW_FLAG) ||
(fwflash_arg_list & FWFLASH_READ_FLAG)))) {
return (FWFLASH_SUCCESS);
}
/* Do Version */
if (fwflash_arg_list == FWFLASH_VER_FLAG) {
return (FWFLASH_SUCCESS);
}
/* generate global list of devices */
gettext("Unable to load fwflash plugins\n"));
fwflash_intr(0);
return (rv);
}
gettext("No flashable devices in this system\n"));
fwflash_intr(0);
return (rv);
}
/* Do list */
if (fwflash_arg_list == (FWFLASH_LIST_FLAG) ||
fwflash_intr(0);
return (rv);
}
/* Do flash update (write) */
FWFLASH_YES_FLAG))) {
int fastreboot_disabled = 0;
/* the update function handles the real arg parsing */
i = 0;
fwflash_arg_list)) == FWFLASH_SUCCESS) {
/* failed ops have already been noted */
if (!fastreboot_disabled &&
B_FALSE) != SCF_SUCCESS)
"Failed to disable fast "
"reboot.\n"));
else
fastreboot_disabled = 1;
gettext("New firmware will be activated "
"after you reboot\n\n"));
}
++i;
}
fwflash_intr(0);
return (rv);
}
/* Do flash read */
FWFLASH_YES_FLAG))) {
fwflash_intr(0);
return (rv);
}
return (FWFLASH_FAILURE);
}
static int
{
int rval = FWFLASH_SUCCESS;
char *plugname;
struct pluginlist *tmpelem;
void *sym;
char *fwplugdirpath, *tempdirpath;
#define CLOSEFREE() { \
/*
* Procedure:
*
* open each .so file found therein
* dlopen(.sofile)
* if it's one of our plugins, add it to fw_pluginlist;
*
* functions we need here include dlopen and dlsym.
*
* If we get to the end and fw_pluginlist struct is empty,
* return FWFLASH_FAILURE so we return to the shell.
*/
gettext("Unable to malloc %d bytes while "
"trying to load plugins: %s\n"),
return (FWFLASH_FAILURE);
}
} else {
}
gettext("Unable to open %s\n"),
return (errno);
}
== NULL) {
gettext("Unable to malloc %d bytes while "
"trying to load plugins: %s\n"),
return (FWFLASH_FAILURE);
}
== NULL) {
gettext("Unable to malloc %d bytes while "
"trying to load plugins: %s\n"),
return (FWFLASH_FAILURE);
}
errno = 0; /* remove chance of false results */
continue;
}
gettext("Unable to malloc %d bytes while "
"trying to load plugins: %s\n"),
return (FWFLASH_FAILURE);
}
/* start allocating storage */
== NULL) {
gettext("Unable to malloc %d bytes while "
"trying to load plugins: %s\n"),
return (FWFLASH_FAILURE);
}
== NULL) {
gettext("Unable to malloc %d bytes while "
"trying to load plugins: %s\n"),
return (FWFLASH_FAILURE);
}
/* load 'er up! */
continue; /* assume there are other plugins */
}
== NULL) {
gettext("Unable to allocate %d bytes for plugin "
"filename %s:%s\n"),
return (rval);
}
/* now sanity check the file */
!= NULL) {
/* max length of drivername */
/* are we doing double-time? */
MAXMODCONFNAME) != 0) {
} else {
}
} else {
CLOSEFREE();
continue;
}
!= NULL) {
} else {
CLOSEFREE();
continue;
}
!= NULL) {
} else {
CLOSEFREE();
continue;
}
!= NULL) {
(int (*)(int))sym;
} else {
CLOSEFREE();
continue;
}
!= NULL) {
(int (*)(struct devicelist *))sym;
} else {
CLOSEFREE();
continue;
}
!= NULL) {
if ((*(int *)sym) >= FWPLUGIN_VERSION_2) {
"fw_cleanup")) != NULL) {
(void (*)(struct devicelist *))sym;
} else {
gettext("ERROR: v2 plugin (%s) "
"has no fw_cleanup function\n"),
CLOSEFREE();
continue;
}
} else {
"Identification plugin %s defined "
"plugin_version < FWPLUGIN_VERSION_2 !");
}
}
== NULL) {
gettext("Unable to allocate space for a"
"drivername %s\n"),
return (FWFLASH_FAILURE);
}
gettext("Unable to allocate %d bytes for "
"filename %s\n"),
return (FWFLASH_FAILURE);
}
/* CONSTCOND */
}
return (FWFLASH_FAILURE);
}
if (errno != 0) {
gettext("Error reading directory entry in %s\n"),
}
return (rval);
}
/*
* fwflash_load_verifier dlload()s the appropriate firmware image
* verification plugin, and attaches the designated fwimg's fd to
* the vrfyplugin structure so we only have to load the image in
* one place.
*/
int
{
int rv = FWFLASH_FAILURE;
int imgfd;
char *clean; /* for the space-removed vid */
struct vrfyplugin *vrfy;
void *vrfysym;
/*
* To make flashing multiple firmware images somewhat more
* efficient, we start this function by checking whether a
* verifier for this device has already been loaded. If it
* has been loaded, we replace the imgfile information, and
* then continue as if we were loading for the first time.
*/
}
}
} else {
gettext("Unable to allocate space for a firmware "
"verifier file(1)"));
return (rv);
}
gettext("Unable to allocate space "
"for a firmware verifier file(2)"));
return (rv);
}
/*
* Since SCSI devices can have a vendor id of up to 8
* left-aligned and _space-padded_ characters, we first need to
* strip off any space characters before we try to make a
* filename out of it
*/
/* invalid vendorid, something's really wrong */
gettext("Invalid vendorid (null) specified for "
"device\n"));
return (rv);
}
} else {
}
gettext("Unable to allocate space "
"for a firmware verifier structure"));
return (rv);
}
errno = 0; /* false positive removal */
gettext("\nUnable to open verification plugin "
"%s. Looking for %s-GENERIC plugin instead.\n"),
/* Try the drv-GENERIC.so form, _then_ die */
== NULL) {
gettext("\nUnable to open either "
"verification plugin %s/%s-%s.so or "
"generic plugin %s.\nUnable to verify "
"firmware image. Aborting.\n"),
return (rv);
}
}
== NULL) {
gettext("Unable to allocate space to store "
"a verifier filename\n"));
return (rv);
}
gettext("%s is an invalid firmware verification "
"plugin."), filename);
return (rv);
} else {
vrfy->vendorvrfy =
(int (*)(struct devicelist *))vrfysym;
}
gettext("Invalid vendor (null) in verification "
"plugin %s\n"), filename);
return (rv);
} else {
"Using a sym-linked (%s -> %s) "
"verification plugin\n",
} else {
}
}
}
/*
* We don't do any verification that the fw image file is in
* an approved location, but it's easy enough to modify this
* function to do so. The verification plugin should provide
* sufficient protection.
*/
gettext("Unable to open designated firmware "
"image file %s: %s\n"),
goto cleanup;
}
gettext("Unable to stat() firmware image file "
"%s: %s\n"),
goto cleanup;
} else {
== NULL) {
gettext("Unable to load firmware image "
"%s: %s\n"),
goto cleanup;
}
}
errno = 0;
/* we haven't read enough data, bail */
gettext("Failed to read sufficient data "
"(got %d bytes, expected %d bytes) from "
"firmware image file %s: %s\n"),
} else {
}
gettext("Unable to save name of firmware image\n"));
} else {
}
if (rv != FWFLASH_SUCCESS) {
/* cleanup and let's get outta here */
if (!(fwflash_arg_list & FWFLASH_READ_FLAG) &&
if (imgfd >= 0) {
}
}
return (rv);
}
/*
* cycles through the global list of plugins to find
* each flashable device, which is added to fw_devices
*
* Each plugin's identify routine must allocated storage
* as required.
*
* Each plugin's identify routine must return
* FWFLASH_FAILURE if it cannot find any devices
* which it handles.
*/
static int
{
int rv = FWFLASH_FAILURE;
int startidx = 0;
int sumrv = 0;
struct pluginlist *plugins;
/* we open rootnode here, and close it in fwflash_intr */
gettext("Unable to take device tree snapshot: %s\n"),
return (rv);
}
gettext("Unable to malloc %d bytes while "
"trying to find devices: %s\n"),
return (FWFLASH_FAILURE);
}
/* CONSTCOND */
gettext("fwflash:flash_device_list() got %d from "
"identify routine\n"), rv);
/* only bump startidx if we've found at least one device */
if (rv == FWFLASH_SUCCESS) {
startidx += 100;
sumrv++;
} else {
gettext("No flashable devices attached with "
"the %s driver in this system\n"),
}
}
if (sumrv > 0)
return (rv);
}
static int
fwflash_list_fw(char *class)
{
int rv = 0;
struct devicelist *curdev;
int header = 1;
/* we're either class-conscious, or we're not */
if (header != 0) {
gettext("List of available devices:\n"));
header--;
}
/*
* If any plugin's fw_devinfo() function returns
* FWFLASH_FAILURE then we want to keep track of
* it. _Most_ plugins should always return
* FWFLASH_SUCCESS from this function. The only
* exception known at this point is the tavor plugin.
*/
}
}
return (rv);
}
static int
{
int rv = FWFLASH_FAILURE;
int needsfree = 0;
int found = 0;
struct devicelist *curdev;
char *realfile;
/*
* Here's how we operate:
*
* We perform some basic checks on the args, then walk
* through the device list looking for the device which
* matches. We then load the appropriate verifier for the
* image file and device, verify the image, then call the
* fw_writefw() function of the appropriate plugin.
*
* There is no "force" flag to enable you to flash a firmware
* image onto an incompatible device because the verifier
* will return FWFLASH_FAILURE if the image doesn't match.
*/
/* new firmware filename and device desc */
gettext("Invalid firmware filename (null)\n"));
return (FWFLASH_FAILURE);
}
gettext("Invalid device requested (null)\n"));
return (FWFLASH_FAILURE);
}
gettext("Unable to allocate space for device "
"filename, operation might fail if %s is"
"a symbolic link\n"),
device);
} else {
/*
* If realpath() succeeds, then we have a valid
* device filename in realfile.
*/
gettext("Unable to resolve device filename"
": %s\n"),
/* tidy up */
/* realpath didn't succeed, use fallback */
} else {
needsfree = 1;
}
}
gettext("fwflash_update: fw_filename (%s) device (%s)\n"),
found++;
if (rv == FWFLASH_FAILURE) {
gettext("Unable to load verifier "
"for device %s\n"),
return (FWFLASH_FAILURE);
}
if (rv == FWFLASH_FAILURE) {
/* the verifier prints a message */
"verifier (%s) for %s :: %s returned "
"FWFLASH_FAILURE\n",
return (rv);
}
"about to flash using plugin %s\n",
filename);
if (rv == FWFLASH_FAILURE) {
gettext("Failed to flash "
"firmware file %s on "
"device %s: %d\n"),
}
} else {
gettext("Flash operation not confirmed "
"by user\n"),
}
}
}
if (!found)
/* report the same device that the user passed in */
gettext("Device %s does not appear "
"to be flashable\n"),
if (needsfree)
return (rv);
}
/*
* We validate that the device path is in our global device list and
* that the filename exists, then palm things off to the relevant plugin.
*/
static int
{
struct devicelist *curdev;
int rv;
int found = 0;
/* new firmware filename and device desc */
MAXPATHLEN) == 0) {
if (rv != FWFLASH_SUCCESS)
gettext("Unable to write out firmware "
"image for %s to file %s\n"),
found++;
}
}
if (!found) {
gettext("No device matching %s was found.\n"),
device);
}
return (rv);
}
static void
fwflash_usage(char *arg)
{
}
"| ALL]] | [-v] | [-h]\n\t"));
",... | -r file] [-y] -d device_path\n\n"));
gettext("\t-l\t\tlist flashable devices in this system\n"
"\t-c device_class limit search to a specific class\n"
"\t\t\teg IB for InfiniBand, ses for SCSI Enclosures\n"
"\t-v\t\tprint version number of fwflash utility\n"
"\t-h\t\tprint this usage message\n\n"));
gettext("\t-f file1,file2,file3,...\n"
"\t\t\tfirmware image file list to flash\n"
"\t-r file\t\tfile to dump device firmware to\n"
"\t-y\t\tanswer Yes/Y/y to prompts\n"
"\t-d device_path\tpathname of device to be flashed\n\n"));
gettext("\tIf -d device_path is specified, then one of -f "
"<files>\n"
"\tor -r <file> must also be specified\n\n"));
gettext("\tIf multiple firmware images are required to be "
"flashed\n"
"\tthey must be listed together, separated by commas. The\n"
"\timages will be flashed in the order specified.\n\n"));
}
static void
fwflash_version(void)
{
}
static void
fwflash_intr(int sig)
{
struct devicelist *thisdev;
struct pluginlist *thisplug;
if (fwflash_in_write) {
gettext("WARNING: firmware image may be corrupted\n\t"));
gettext("Reflash firmware before rebooting!\n"));
}
if (sig > 0) {
}
/*
* we need to close everything down properly, so
* call the plugin closure routines
*/
if (fw_devices != NULL) {
/*
* If we've got a cleanup routine, it
* cleans up _everything_ for thisdev
*/
} else {
/* free the components first */
/* We don't free address[] for old plugins */
}
/* CONSTCOND */
}
}
if (fw_pluginlist != NULL) {
/* CONSTCOND */
}
}
}
if (sig > 0)
}
static void
fwflash_handle_signals(void)
{
perror("signal");
}
perror("signal");
}
}
static int
{
int resp;
"Do you want to continue? (Y/N): "), file);
return (FWFLASH_YES_FLAG);
} else {
}
return (FWFLASH_FAILURE);
}
int
get_fileopts(char *options)
{
int i;
char *files;
/* we have more than one */
gettext("Unable to allocate space for "
"a firmware image filename\n"));
return (FWFLASH_FAILURE);
}
i = 1;
filelist[0]);
== NULL) {
gettext("Unable to allocate space for "
"a firmware image filename\n"));
return (FWFLASH_FAILURE);
}
i, filelist[i]);
++i;
}
} else {
gettext("Unable to allocate space for "
"a firmware image filename\n"));
return (FWFLASH_FAILURE);
}
filelist[0]);
}
return (FWFLASH_SUCCESS);
}
/*
* code reuse - cheerfully borrowed from stmsboot_util.c
*/
void
{
}
}