Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extractor for *.FL[12] files #162

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions scripts/extract_from_ISO.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/bin/bash
#
# Copyright (C) 2019 onwards, Samveen S. Gulati
#
# Script to extract FL1 or FL2 files from Lenovo BIOS update ISO image
#

# EL TORITO offset count (thanks to Hamish Coleman)
FAT_OFFSET=71680
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure you are aware - this number has been known to change, and is an artifact of the Lenovo build process (and not guaranteed). There is a way to calculate it, if needed


USAGE="${0} [[-1|--fl1] [-2|--fl2]] {[-i|--iso] ISO_FILE} {[-d|--desc] DESC_FILE} [[-m|--model] PREFIX] [[-s|--suffix] SUFFIX] | [-h|--help]
-1|--fl1 Extract BIOS update file (.FL1)
-2|--fl2 Extract EC update file (.FL2) (Default if no option)
-i|--iso Update ISO to extract the file from
-d|--desc Path of Descriptions.txt file
-m|--model Optional model prefix to attach to extracted filenames.
-s|--suffix Optional suffix to attach to extracted filenames.
-h|--help Show this help"

# Get and parse options
OPTIONS=$(getopt -o 12i:d:m:s:h --long fl1,fl2,iso:,desc:,model:,suffix:,help -n "$0" -- "$@")
# Note the quotes around `$OPTIONS': they are essential!
eval set -- "$OPTIONS"

while true ; do
case "$1" in
-1|--fl1) FL1=Y ; shift ;;
-2|--fl2) FL2=Y ; shift ;;
-i|--iso) ISO=$2 ; shift 2 ;;
-d|--desc) DESC=$2 ; shift 2 ;;
-m|--model) MODEL="${2%%.}." ; shift 2 ;;
-s|--suffix) SUFFIX=".${2##.}" ; shift 2 ;;
-h|--help) echo "$USAGE"; exit ;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done

# Reasonable Defaults: extract only FL2
if [[ -z $FL1 ]] && [[ -z $FL2 ]]; then
FL2=Y
fi

[[ -n "$ISO" ]] || { echo -e "Error: Missing ISO filename\n${USAGE}" >&2; exit 2; }
[[ -f "$ISO" ]] || { echo -e "Error: ${ISO} not found or not a file\n${USAGE}" >&2; exit 4; }
[[ -f "$DESC" ]] || { echo -e "Error: Missing Descriptions file.\n${USAGE}" >&2; exit 8; }
[[ -n "$MODEL" ]] && echo "Using ${MODEL} as prefix" >&2 || echo "No model name provided. Not using any prefix" >&2
[[ -n "$SUFFIX" ]] && echo "Using ${SUFFIX} as suffix" >&2 || echo "Not using any suffix." >&2

# ./scripts/ISO_copyFL2 from_iso $< $@ $(1)

if ! which mdir >/dev/null; then
echo "Fatal: mtools package not installed or not in path" >&2
exit 16
fi

read -r FL1VERNUM FL1VER FL2VERNUM FL2VER < <(grep -E "^${ISO%%.iso*}" ../Descriptions.txt| \
Copy link
Owner

@hamishcoleman hamishcoleman Mar 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean "$DESC" here, instead of ../Descriptions.txt ?

Also, you are treating a free-form text string as a set of fixed format fields when you extract the various version numbers.

Copy link
Author

@samveen samveen Mar 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean "$DESC" here, instead of ../Descriptions.txt ?

Yeah. That's a typo. I'll fix it.

Also, you are treating a free-form text string as a set of fixed format fields when you extract the various version numbers.

I looked at the format of the file, and I see that there is a bit of commonality between all the version lines, so I'm counting on this structure. The issue is that for the EC versioning. there is no way that I know off to figure out the EC version number from the contents of a given ISO. However, I'm hoping there is something, which I have not yet figured out myself, which allows that. It is possible for the BIOS version string (not the number itself), which I can implement.

Any suggestions?

grep BIOS| \
grep -v BROKEN| \
sed 's/.\+ BIOS //; s/ EC / /; s/[()]//g')

# The "internal" filename pattern to match - used in the BIOS update image
FILELIST="$(mdir -/ -b -f -i "$ISO"@@"$FAT_OFFSET")"

if [[ -n $FL1 ]]; then
FL1PATH="$(echo "$FILELIST" |grep '.FL1$')"
if [ -z "$FL1PATH" ]; then
echo "Error: could not find any files in $ISO matching *.FL1" >&2
exit 32
fi
if [[ $(echo "$FL1PATH" |wc -l) -gt 1 ]]; then
echo "Error: $ISO has more than one FL1 files" >&2
echo "$FL1PATH"
exit 64
fi


FL1NAME="${MODEL}${FL1VER}.${FL1PATH##*/}${SUFFIX}"
echo "Extracting $FL1PATH (ver $FL1VERNUM) to ${FL1NAME/$/s}"
mcopy -n -i "$ISO"@@"$FAT_OFFSET" "$FL1PATH" "${FL1NAME/\$/s}"
fi

if [[ -n $FL2 ]]; then
FL2PATH="$(echo "$FILELIST" |grep '.FL2$')"
if [ -z "$FL2PATH" ]; then
echo "Error: could not find any files in $ISO matching *.FL2" >&2
exit 32
fi
if [[ $(echo "$FL2PATH" |wc -l) -gt 1 ]]; then
echo "Error: $ISO has more than one FL2 files" >&2
echo "$FL2PATH"
exit 64
fi
FL2NAME="${MODEL}${FL2VER}.${FL2PATH##*/}${SUFFIX}"
echo "Extracting $FL2PATH (ver $FL2VERNUM) to ${FL2NAME/$/s}"
mcopy -n -i "$ISO"@@"$FAT_OFFSET" "$FL2PATH" "${FL2NAME/$/s}"
fi

# vim: expandtab sw=4 ts=4: