blob: ca0bdeeee6f1c67f946869842c786936868e308c [file] [log] [blame]
Yegor Timoshenkoc2e49412018-10-07 01:58:27 +00001#!/usr/bin/env bash
Joe Pillow9a4881a2016-02-19 15:18:14 -08002#
Patrick Georgi7333a112020-05-08 20:48:04 +02003# SPDX-License-Identifier: GPL-2.0-only
Joe Pillow9a4881a2016-02-19 15:18:14 -08004
Matt DeVillier648a44a2021-11-12 10:23:21 -06005if [ ! -f "$1" ]; then
6 echo "Error: You must provide a valid filename"
7 exit 1
8fi
Yegor Timoshenkoc2e49412018-10-07 01:58:27 +00009
Joe Pillow9a4881a2016-02-19 15:18:14 -080010IMAGE=$1
Matt DeVillier554c13d2023-02-01 12:33:52 -060011
12if [[ "$2" = "" ]]; then
13 # create new dir '$IMAGE-blobs' (less file extension)
14 DIR=$(basename $IMAGE)
15 DIR="${DIR%.*}-blobs"
16else
17 DIR=$2
18fi
Matt DeVillier648a44a2021-11-12 10:23:21 -060019mkdir -p $DIR
Joe Pillow9a4881a2016-02-19 15:18:14 -080020
Matt DeVillier648a44a2021-11-12 10:23:21 -060021if [ -f ./cbfstool ]; then
22 CBFSTOOL="./cbfstool"
23else
24 CBFSTOOL=$(command -v cbfstool)
25fi
26if [[ "$CBFSTOOL" = "" ]]; then
27 echo "Error: cbfstool must be in your path or exist locally"
28 exit 1
Joe Pillow9a4881a2016-02-19 15:18:14 -080029fi
30
Matt DeVillier648a44a2021-11-12 10:23:21 -060031if [ -f ./ifdtool ]; then
32 IFDTOOL="./ifdtool"
33else
34 IFDTOOL=$(which ifdtool)
35fi
36if [[ "$IFDTOOL" = "" ]]; then
37 echo "Error: ifdtool must be in your path or exist locally"
38 exit 1
Joe Pillow9a4881a2016-02-19 15:18:14 -080039fi
40
Matt DeVillier0923c622022-10-15 12:04:08 -050041# 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
44if $CBFSTOOL $IMAGE print -r FW_MAIN_A >$DIR/cbfs.txt 2>/dev/null; then
45 REGION="FW_MAIN_A"
46elif $CBFSTOOL $IMAGE print -r COREBOOT >$DIR/cbfs.txt; then
47 # use COREBOOT region
48 REGION="COREBOOT"
49elif $CBFSTOOL $IMAGE print -r BOOT_STUB >$DIR/cbfs.txt; then
50 # use BOOT_STUB region
51 REGION="BOOT_STUB"
52else
53 echo "Error reading CBFS: $IMAGE is not a valid coreboot image"
54 exit 1
Joe Pillow9a4881a2016-02-19 15:18:14 -080055fi
56
Matt DeVillier648a44a2021-11-12 10:23:21 -060057echo ""
Matt DeVillier0923c622022-10-15 12:04:08 -050058echo "Extracting blobs from region $REGION..."
Matt DeVillier648a44a2021-11-12 10:23:21 -060059echo ""
Joe Pillow9a4881a2016-02-19 15:18:14 -080060
Matt DeVillier648a44a2021-11-12 10:23:21 -060061# extract flash regions
62if ! $IFDTOOL -x $IMAGE >/dev/null; then
63 echo "Error reading flash descriptor/extracting flash regions"
64 exit 1
Joe Pillow9a4881a2016-02-19 15:18:14 -080065fi
Matt DeVillier648a44a2021-11-12 10:23:21 -060066# rename to normal convention; drop unused regions
67mv flashregion_0_flashdescriptor.bin $DIR/flashdescriptor.bin
68[ -f flashregion_2_intel_me.bin ] && mv flashregion_2_intel_me.bin $DIR/me.bin
Joe Pillow9a4881a2016-02-19 15:18:14 -080069rm flashregion_*.bin
Matt DeVillier648a44a2021-11-12 10:23:21 -060070
71# extract microcode
Matt DeVillier0923c622022-10-15 12:04:08 -050072$CBFSTOOL $IMAGE extract -r $REGION -n cpu_microcode_blob.bin -f $DIR/cpu_microcode_blob.bin
Matt DeVillier648a44a2021-11-12 10:23:21 -060073
74# extract VGA BIOS
75VGA=$(grep pci $DIR/cbfs.txt | cut -f1 -d\ )
76if [ "$VGA" != "" ]; then
Matt DeVillier0923c622022-10-15 12:04:08 -050077 $CBFSTOOL $IMAGE extract -r $REGION -n $VGA -f $DIR/vgabios.bin
Matt DeVillier648a44a2021-11-12 10:23:21 -060078fi
79
80# extract MRC.bin
81MRC=$(grep mrc.bin $DIR/cbfs.txt | cut -f1 -d\ )
82if [ "$MRC" != "" ]; then
Matt DeVillier0923c622022-10-15 12:04:08 -050083 $CBFSTOOL $IMAGE extract -r $REGION -n "$MRC" -f "$DIR/$MRC"
Matt DeVillier648a44a2021-11-12 10:23:21 -060084fi
85
86# extract refcode
87REF=$(grep refcode $DIR/cbfs.txt | cut -f1 -d\ )
88if [ "$REF" != "" ]; then
Matt DeVillier0923c622022-10-15 12:04:08 -050089 $CBFSTOOL $IMAGE extract -r $REGION -n fallback/refcode -f "$DIR/refcode.elf" -m x86
Matt DeVillier648a44a2021-11-12 10:23:21 -060090fi
91
92# extract FSP blobs
93for FSP in $(grep fsp $DIR/cbfs.txt | cut -f1 -d\ ); do
Matt DeVillier0923c622022-10-15 12:04:08 -050094 $CBFSTOOL $IMAGE extract -r $REGION -n $FSP -f $DIR/$FSP
Matt DeVillier648a44a2021-11-12 10:23:21 -060095done
96
97# extract audio blobs
98for AUD in $(grep -e "-2ch-" -e "-4ch-" $DIR/cbfs.txt | cut -f1 -d\ ); do
Matt DeVillier0923c622022-10-15 12:04:08 -050099 $CBFSTOOL $IMAGE extract -r $REGION -n $AUD -f $DIR/$AUD
Matt DeVillier648a44a2021-11-12 10:23:21 -0600100done
101
102# extract VBTs
103for VBT in $(grep vbt $DIR/cbfs.txt | cut -f1 -d\ ); do
Matt DeVillier0923c622022-10-15 12:04:08 -0500104 $CBFSTOOL $IMAGE extract -r $REGION -n $VBT -f $DIR/$VBT
Matt DeVillier648a44a2021-11-12 10:23:21 -0600105done
106
107# extract IFWI
108IFWI=$(cbfstool $IMAGE layout -w | grep IFWI)
109if [ "$IFWI" != "" ]; then
110 $CBFSTOOL $IMAGE read -r IFWI -f $DIR/ifwi.bin
111fi
112
113# generate hashes
114(
115 cd $DIR
116 : >hashes.txt
117 for FILE in $(ls *.{bin,elf} 2>/dev/null); do
118 sha256sum $FILE >>hashes.txt
119 done
120)
121
122# a little housekeeping
123rm $DIR/cbfs.txt
124
125echo ""
126echo "All done"