-2

I am trying to do some analysis of SSD firmware and have found a bash script called firmware.sh that seems interesting to me. However, I really don't know what I am looking at here.

If anyone can help me understand what this code might be used for, or what it's doing, I would greatly appreciate it!

Here's the bash:

#!/bin/sh -e

FIRMWARE_DIRS="/lib/firmware /usr/local/lib/firmware"

err() {
   echo "$@" >&2
   if [ -x /usr/bin/logger ]; then
       /usr/bin/logger -t "${0##*/}[$$]" "$@"
   fi
}

if [ ! -e /sys$DEVPATH/loading ]; then
   err "udev firmware loader misses sysfs directory"
   exit 1
fi

for DIR in $FIRMWARE_DIRS; do
   [ -e "$DIR/$FIRMWARE" ] || continue
   echo 1 > /sys$DEVPATH/loading
   cat "$DIR/$FIRMWARE" > /sys$DEVPATH/data
   echo 0 > /sys$DEVPATH/loading
   exit 0
done

echo -1 > /sys$DEVPATH/loading
err "Cannot find  firmware file '$FIRMWARE'"
exit 1

Of particular interest to me is the for loop... I think I understand that the $NAME syntax is used for variables in bash but I don't know what those variables are referencing. Thank you for your consideration!

Derdle
  • 3
  • 1
  • this is a broad question. Have you tried to understand the inputs (environment variables, command line parameters, files, and directories being read, other programs being accessed) and the outputs (messages being printed, files being created) ? – Cristiano Araujo Jan 19 '21 at 14:43

1 Answers1

0

I'll try to explain this line by line.

FIRMWARE_DIRS="/lib/firmware /usr/local/lib/firmware"

FIRMWARE_DIRS is set up with two directories separated by a space. This is set up for the for loop later on in the script.

...
for DIR in $FIRMWARE_DIRS; do

For each loop, DIR is set to each directory stored in FIRMWARE_DIRS

   [ -e "$DIR/$FIRMWARE" ] || continue
  • [ denotes the start of a test, much like if, and ] marks the end of this test.
  • -e checks if the argument passed is a file or directory that exists.
  • || means or and anything to the right of this will execute if the test to the left fails.
  • continue stops the current iteration of a loop which starts on the next iteration.
  • FIRMWARE is presumably an environment variable set up prior to this script running. You can see its value if it has been set up on login by executing the command echo $FIRMWARE on the command line.

   echo 1 > /sys$DEVPATH/loading

Truncates the file /sys$DEVPATH/loading if it exists, then outputs the number 1 to this file.

   cat "$DIR/$FIRMWARE" > /sys$DEVPATH/data

Truncates the file /sys$DEVPATH/data if it exists, then outputs the contents of the file(s) $DIR/$FIRMWARE to /sys$DEVPATH/data. If FIRMWARE contains a wildcard *, it will copy the contents of all the files matched.

   echo 0 > /sys$DEVPATH/loading

Truncates the file sys$DEVPATH/loading if it exists, then outputs the number 0 to this file.

   exit 0

Exits the script with the return status 0 (means it completed OK). This has the effect of ending the script in the for loop at this point for any iteration which passed the test above (the one checking the file or directory exists).

Overall, it looks like it's checking for the first directory that exists in FIRMWARE_DIRS, copies one or more firmware files from there to another location (/sys$DEVPATH/data) and exits as soon as it's done that once.

Kind Stranger
  • 1,736
  • 13
  • 18