Yegor Timoshenko | c2e4941 | 2018-10-07 01:58:27 +0000 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 2 | # |
Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 3 | # SPDX-License-Identifier: GPL-2.0-only |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 4 | |
Matt DeVillier | 648a44a | 2021-11-12 10:23:21 -0600 | [diff] [blame] | 5 | if [ ! -f "$1" ]; then |
| 6 | echo "Error: You must provide a valid filename" |
| 7 | exit 1 |
| 8 | fi |
Yegor Timoshenko | c2e4941 | 2018-10-07 01:58:27 +0000 | [diff] [blame] | 9 | |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 10 | IMAGE=$1 |
Matt DeVillier | 648a44a | 2021-11-12 10:23:21 -0600 | [diff] [blame] | 11 | # create new dir '$IMAGE-blobs' (less file extension) |
| 12 | DIR=$(basename $IMAGE) |
| 13 | DIR="${DIR%.*}-blobs" |
| 14 | mkdir -p $DIR |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 15 | |
Matt DeVillier | 648a44a | 2021-11-12 10:23:21 -0600 | [diff] [blame] | 16 | if [ -f ./cbfstool ]; then |
| 17 | CBFSTOOL="./cbfstool" |
| 18 | else |
| 19 | CBFSTOOL=$(command -v cbfstool) |
| 20 | fi |
| 21 | if [[ "$CBFSTOOL" = "" ]]; then |
| 22 | echo "Error: cbfstool must be in your path or exist locally" |
| 23 | exit 1 |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 24 | fi |
| 25 | |
Matt DeVillier | 648a44a | 2021-11-12 10:23:21 -0600 | [diff] [blame] | 26 | if [ -f ./ifdtool ]; then |
| 27 | IFDTOOL="./ifdtool" |
| 28 | else |
| 29 | IFDTOOL=$(which ifdtool) |
| 30 | fi |
| 31 | if [[ "$IFDTOOL" = "" ]]; then |
| 32 | echo "Error: ifdtool must be in your path or exist locally" |
| 33 | exit 1 |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 34 | fi |
| 35 | |
Matt DeVillier | 648a44a | 2021-11-12 10:23:21 -0600 | [diff] [blame] | 36 | # ensure valid coreboot image / get list of main COREBOOT CBFS contents |
| 37 | REGION="" |
| 38 | if ! $CBFSTOOL $IMAGE print >$DIR/cbfs.txt 2>/dev/null; then |
| 39 | # try using BOOT_STUB region |
| 40 | if ! $CBFSTOOL $IMAGE print -r BOOT_STUB >$DIR/cbfs.txt; then |
| 41 | echo "Error reading CBFS: $IMAGE is not a valid coreboot image" |
| 42 | exit 1 |
| 43 | else |
| 44 | REGION="-r BOOT_STUB" |
| 45 | fi |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 46 | fi |
| 47 | |
Matt DeVillier | 648a44a | 2021-11-12 10:23:21 -0600 | [diff] [blame] | 48 | echo "" |
| 49 | echo "Extracting blobs..." |
| 50 | echo "" |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 51 | |
Matt DeVillier | 648a44a | 2021-11-12 10:23:21 -0600 | [diff] [blame] | 52 | # extract flash regions |
| 53 | if ! $IFDTOOL -x $IMAGE >/dev/null; then |
| 54 | echo "Error reading flash descriptor/extracting flash regions" |
| 55 | exit 1 |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 56 | fi |
Matt DeVillier | 648a44a | 2021-11-12 10:23:21 -0600 | [diff] [blame] | 57 | # rename to normal convention; drop unused regions |
| 58 | mv flashregion_0_flashdescriptor.bin $DIR/flashdescriptor.bin |
| 59 | [ -f flashregion_2_intel_me.bin ] && mv flashregion_2_intel_me.bin $DIR/me.bin |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 60 | rm flashregion_*.bin |
Matt DeVillier | 648a44a | 2021-11-12 10:23:21 -0600 | [diff] [blame] | 61 | |
| 62 | # extract microcode |
| 63 | $CBFSTOOL $IMAGE extract $REGION -n cpu_microcode_blob.bin -f $DIR/cpu_microcode_blob.bin |
| 64 | |
| 65 | # extract VGA BIOS |
| 66 | VGA=$(grep pci $DIR/cbfs.txt | cut -f1 -d\ ) |
| 67 | if [ "$VGA" != "" ]; then |
| 68 | $CBFSTOOL $IMAGE extract $REGION -n $VGA -f $DIR/vgabios.bin |
| 69 | fi |
| 70 | |
| 71 | # extract MRC.bin |
| 72 | MRC=$(grep mrc.bin $DIR/cbfs.txt | cut -f1 -d\ ) |
| 73 | if [ "$MRC" != "" ]; then |
| 74 | $CBFSTOOL $IMAGE extract $REGION -n "$MRC" -f "$DIR/$MRC" |
| 75 | fi |
| 76 | |
| 77 | # extract refcode |
| 78 | REF=$(grep refcode $DIR/cbfs.txt | cut -f1 -d\ ) |
| 79 | if [ "$REF" != "" ]; then |
| 80 | $CBFSTOOL $IMAGE extract $REGION -n fallback/refcode -f "$DIR/refcode.elf" -m x86 |
| 81 | fi |
| 82 | |
| 83 | # extract FSP blobs |
| 84 | for FSP in $(grep fsp $DIR/cbfs.txt | cut -f1 -d\ ); do |
| 85 | $CBFSTOOL $IMAGE extract $REGION -n $FSP -f $DIR/$FSP |
| 86 | done |
| 87 | |
| 88 | # extract audio blobs |
| 89 | for AUD in $(grep -e "-2ch-" -e "-4ch-" $DIR/cbfs.txt | cut -f1 -d\ ); do |
| 90 | $CBFSTOOL $IMAGE extract $REGION -n $AUD -f $DIR/$AUD |
| 91 | done |
| 92 | |
| 93 | # extract VBTs |
| 94 | for VBT in $(grep vbt $DIR/cbfs.txt | cut -f1 -d\ ); do |
| 95 | $CBFSTOOL $IMAGE extract $REGION -n $VBT -f $DIR/$VBT |
| 96 | done |
| 97 | |
| 98 | # extract IFWI |
| 99 | IFWI=$(cbfstool $IMAGE layout -w | grep IFWI) |
| 100 | if [ "$IFWI" != "" ]; then |
| 101 | $CBFSTOOL $IMAGE read -r IFWI -f $DIR/ifwi.bin |
| 102 | fi |
| 103 | |
| 104 | # generate hashes |
| 105 | ( |
| 106 | cd $DIR |
| 107 | : >hashes.txt |
| 108 | for FILE in $(ls *.{bin,elf} 2>/dev/null); do |
| 109 | sha256sum $FILE >>hashes.txt |
| 110 | done |
| 111 | ) |
| 112 | |
| 113 | # a little housekeeping |
| 114 | rm $DIR/cbfs.txt |
| 115 | |
| 116 | echo "" |
| 117 | echo "All done" |