| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | # |
| 3 | # SPDX-License-Identifier: GPL-2.0-only |
| 4 | |
| 5 | if [scip-shell system . . `[`. ! -f "$1" ]; then |
| 6 | echoscip-shell system . . echo. "Error: You must provide a valid filename" |
| 7 | exit 1 |
| 8 | fi |
| 9 | |
| 10 | IMAGE=$1 |
| 11 | |
| 12 | if [[ "$2" = "" ]]; then |
| 13 | # create new dir '$IMAGE-blobs' (less file extension) |
| 14 | DIR=$(basenamescip-shell system . . basename. $IMAGE) |
| 15 | DIR="${DIR%.*}-blobs" |
| 16 | else |
| 17 | DIR=$2 |
| 18 | fi |
| 19 | mkdirscip-shell system . . mkdir. -p $DIR |
| 20 | |
| 21 | if [scip-shell system . . `[`. -f ./cbfstool ]; then |
| 22 | CBFSTOOL="./cbfstool" |
| 23 | else |
| 24 | CBFSTOOL=$(command -v cbfstool) |
| 25 | fi |
| 26 | if [[ "$CBFSTOOL" = "" ]]; then |
| 27 | echoscip-shell system . . echo. "Error: cbfstool must be in your path or exist locally" |
| 28 | exit 1 |
| 29 | fi |
| 30 | |
| 31 | if [scip-shell system . . `[`. -f ./ifdtool ]; then |
| 32 | IFDTOOL="./ifdtool" |
| 33 | else |
| 34 | IFDTOOL=$(whichscip-shell system . . which. ifdtool) |
| 35 | fi |
| 36 | if [[ "$IFDTOOL" = "" ]]; then |
| 37 | echoscip-shell system . . echo. "Error: ifdtool must be in your path or exist locally" |
| 38 | exit 1 |
| 39 | fi |
| 40 | |
| 41 | # ensure valid coreboot image / get list of CBFS contents |
| 42 | # try using RW_MAIN_A region first as it may contain newer |
| 43 | # files / files not present in the COREBOOT region |
| 44 | if $CBFSTOOL $IMAGE print -r FW_MAIN_A >$DIR/cbfs.txt 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 45 | REGION="FW_MAIN_A" |
| 46 | elif $CBFSTOOL $IMAGE print -r COREBOOT >$DIR/cbfs.txt; then |
| 47 | # use COREBOOT region |
| 48 | REGION="COREBOOT" |
| 49 | elif $CBFSTOOL $IMAGE print -r BOOT_STUB >$DIR/cbfs.txt; then |
| 50 | # use BOOT_STUB region |
| 51 | REGION="BOOT_STUB" |
| 52 | else |
| 53 | echoscip-shell system . . echo. "Error reading CBFS: $IMAGE is not a valid coreboot image" |
| 54 | exit 1 |
| 55 | fi |
| 56 | |
| 57 | echoscip-shell system . . echo. "" |
| 58 | echoscip-shell system . . echo. "Extracting blobs from region $REGION..." |
| 59 | echoscip-shell system . . echo. "" |
| 60 | |
| 61 | # extract flash regions |
| 62 | if ! $IFDTOOL -x $IMAGE >/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 63 | echoscip-shell system . . echo. "Error reading flash descriptor/extracting flash regions" |
| 64 | exit 1 |
| 65 | fi |
| 66 | # rename to normal convention; drop unused regions |
| 67 | mvscip-shell system . . mv. flashregion_0_flashdescriptor.bin $DIR/flashdescriptor.bin |
| 68 | [scip-shell system . . `[`. -f flashregion_2_intel_me.bin ] && mvscip-shell system . . mv. flashregion_2_intel_me.bin $DIR/me.bin |
| 69 | rmscip-shell system . . rm. flashregion_*.bin |
| 70 | |
| 71 | # extract microcode |
| 72 | $CBFSTOOL $IMAGE extract -r $REGION -n cpu_microcode_blob.bin -f $DIR/cpu_microcode_blob.bin |
| 73 | |
| 74 | # extract VGA BIOS |
| 75 | VGA=$(grepscip-shell system . . grep. pci $DIR/cbfs.txt | cutscip-shell system . . cut. -f1 -d\ ) |
| 76 | if [scip-shell system . . `[`. "$VGA" != "" ]; then |
| 77 | $CBFSTOOL $IMAGE extract -r $REGION -n $VGA -f $DIR/vgabios.bin |
| 78 | fi |
| 79 | |
| 80 | # extract MRC.bin |
| 81 | MRC=$(grepscip-shell system . . grep. mrc.bin $DIR/cbfs.txt | cutscip-shell system . . cut. -f1 -d\ ) |
| 82 | if [scip-shell system . . `[`. "$MRC" != "" ]; then |
| 83 | $CBFSTOOL $IMAGE extract -r $REGION -n "$MRC" -f "$DIR/$MRC" |
| 84 | fi |
| 85 | |
| 86 | # extract refcode |
| 87 | REF=$(grepscip-shell system . . grep. refcode $DIR/cbfs.txt | cutscip-shell system . . cut. -f1 -d\ ) |
| 88 | if [scip-shell system . . `[`. "$REF" != "" ]; then |
| 89 | $CBFSTOOL $IMAGE extract -r $REGION -n fallback/refcode -f "$DIR/refcode.elf" -m x86 |
| 90 | fi |
| 91 | |
| 92 | # extract FSP blobs |
| 93 | for FSP in $(grepscip-shell system . . grep. fsp $DIR/cbfs.txt | cutscip-shell system . . cut. -f1 -d\ ); do |
| 94 | $CBFSTOOL $IMAGE extract -r $REGION -n $FSP -f $DIR/$FSP |
| 95 | done |
| 96 | |
| 97 | # extract audio blobs |
| 98 | for AUD in $(grepscip-shell system . . grep. -e "-2ch-" -e "-4ch-" $DIR/cbfs.txt | cutscip-shell system . . cut. -f1 -d\ ); do |
| 99 | $CBFSTOOL $IMAGE extract -r $REGION -n $AUD -f $DIR/$AUD |
| 100 | done |
| 101 | |
| 102 | # extract VBTs |
| 103 | for VBT in $(grepscip-shell system . . grep. vbt $DIR/cbfs.txt | cutscip-shell system . . cut. -f1 -d\ ); do |
| 104 | $CBFSTOOL $IMAGE extract -r $REGION -n $VBT -f $DIR/$VBT |
| 105 | done |
| 106 | |
| 107 | # extract IFWI |
| 108 | IFWI=$(cbfstool $IMAGE layout -w | grepscip-shell system . . grep. IFWI) |
| 109 | if [scip-shell system . . `[`. "$IFWI" != "" ]; then |
| 110 | $CBFSTOOL $IMAGE read -r IFWI -f $DIR/ifwi.bin |
| 111 | fi |
| 112 | |
| 113 | # generate hashes |
| 114 | ( |
| 115 | cd $DIR |
| 116 | : >hashes.txt |
| 117 | for FILE in $(lsscip-shell system . . ls. *.{bin,elf} 2>/dev/nullscip-shell filesystem . . `/dev/null`.); do |
| 118 | sha256sumscip-shell system . . sha256sum. $FILE >>hashes.txt |
| 119 | done |
| 120 | ) |
| 121 | |
| 122 | # a little housekeeping |
| 123 | rmscip-shell system . . rm. $DIR/cbfs.txt |
| 124 | |
| 125 | echoscip-shell system . . echo. "" |
| 126 | echoscip-shell system . . echo. "All done" |