blob: 669327d3fe673303a3fc6b625b4136d89f4a39b6 [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 DeVillier648a44a2021-11-12 10:23:21 -060011# create new dir '$IMAGE-blobs' (less file extension)
12DIR=$(basename $IMAGE)
13DIR="${DIR%.*}-blobs"
14mkdir -p $DIR
Joe Pillow9a4881a2016-02-19 15:18:14 -080015
Matt DeVillier648a44a2021-11-12 10:23:21 -060016if [ -f ./cbfstool ]; then
17 CBFSTOOL="./cbfstool"
18else
19 CBFSTOOL=$(command -v cbfstool)
20fi
21if [[ "$CBFSTOOL" = "" ]]; then
22 echo "Error: cbfstool must be in your path or exist locally"
23 exit 1
Joe Pillow9a4881a2016-02-19 15:18:14 -080024fi
25
Matt DeVillier648a44a2021-11-12 10:23:21 -060026if [ -f ./ifdtool ]; then
27 IFDTOOL="./ifdtool"
28else
29 IFDTOOL=$(which ifdtool)
30fi
31if [[ "$IFDTOOL" = "" ]]; then
32 echo "Error: ifdtool must be in your path or exist locally"
33 exit 1
Joe Pillow9a4881a2016-02-19 15:18:14 -080034fi
35
Matt DeVillier0923c622022-10-15 12:04:08 -050036# ensure valid coreboot image / get list of CBFS contents
37# try using RW_MAIN_A region first as it may contain newer
38# files / files not present in the COREBOOT region
39if $CBFSTOOL $IMAGE print -r FW_MAIN_A >$DIR/cbfs.txt 2>/dev/null; then
40 REGION="FW_MAIN_A"
41elif $CBFSTOOL $IMAGE print -r COREBOOT >$DIR/cbfs.txt; then
42 # use COREBOOT region
43 REGION="COREBOOT"
44elif $CBFSTOOL $IMAGE print -r BOOT_STUB >$DIR/cbfs.txt; then
45 # use BOOT_STUB region
46 REGION="BOOT_STUB"
47else
48 echo "Error reading CBFS: $IMAGE is not a valid coreboot image"
49 exit 1
Joe Pillow9a4881a2016-02-19 15:18:14 -080050fi
51
Matt DeVillier648a44a2021-11-12 10:23:21 -060052echo ""
Matt DeVillier0923c622022-10-15 12:04:08 -050053echo "Extracting blobs from region $REGION..."
Matt DeVillier648a44a2021-11-12 10:23:21 -060054echo ""
Joe Pillow9a4881a2016-02-19 15:18:14 -080055
Matt DeVillier648a44a2021-11-12 10:23:21 -060056# extract flash regions
57if ! $IFDTOOL -x $IMAGE >/dev/null; then
58 echo "Error reading flash descriptor/extracting flash regions"
59 exit 1
Joe Pillow9a4881a2016-02-19 15:18:14 -080060fi
Matt DeVillier648a44a2021-11-12 10:23:21 -060061# rename to normal convention; drop unused regions
62mv flashregion_0_flashdescriptor.bin $DIR/flashdescriptor.bin
63[ -f flashregion_2_intel_me.bin ] && mv flashregion_2_intel_me.bin $DIR/me.bin
Joe Pillow9a4881a2016-02-19 15:18:14 -080064rm flashregion_*.bin
Matt DeVillier648a44a2021-11-12 10:23:21 -060065
66# extract microcode
Matt DeVillier0923c622022-10-15 12:04:08 -050067$CBFSTOOL $IMAGE extract -r $REGION -n cpu_microcode_blob.bin -f $DIR/cpu_microcode_blob.bin
Matt DeVillier648a44a2021-11-12 10:23:21 -060068
69# extract VGA BIOS
70VGA=$(grep pci $DIR/cbfs.txt | cut -f1 -d\ )
71if [ "$VGA" != "" ]; then
Matt DeVillier0923c622022-10-15 12:04:08 -050072 $CBFSTOOL $IMAGE extract -r $REGION -n $VGA -f $DIR/vgabios.bin
Matt DeVillier648a44a2021-11-12 10:23:21 -060073fi
74
75# extract MRC.bin
76MRC=$(grep mrc.bin $DIR/cbfs.txt | cut -f1 -d\ )
77if [ "$MRC" != "" ]; then
Matt DeVillier0923c622022-10-15 12:04:08 -050078 $CBFSTOOL $IMAGE extract -r $REGION -n "$MRC" -f "$DIR/$MRC"
Matt DeVillier648a44a2021-11-12 10:23:21 -060079fi
80
81# extract refcode
82REF=$(grep refcode $DIR/cbfs.txt | cut -f1 -d\ )
83if [ "$REF" != "" ]; then
Matt DeVillier0923c622022-10-15 12:04:08 -050084 $CBFSTOOL $IMAGE extract -r $REGION -n fallback/refcode -f "$DIR/refcode.elf" -m x86
Matt DeVillier648a44a2021-11-12 10:23:21 -060085fi
86
87# extract FSP blobs
88for FSP in $(grep fsp $DIR/cbfs.txt | cut -f1 -d\ ); do
Matt DeVillier0923c622022-10-15 12:04:08 -050089 $CBFSTOOL $IMAGE extract -r $REGION -n $FSP -f $DIR/$FSP
Matt DeVillier648a44a2021-11-12 10:23:21 -060090done
91
92# extract audio blobs
93for AUD in $(grep -e "-2ch-" -e "-4ch-" $DIR/cbfs.txt | cut -f1 -d\ ); do
Matt DeVillier0923c622022-10-15 12:04:08 -050094 $CBFSTOOL $IMAGE extract -r $REGION -n $AUD -f $DIR/$AUD
Matt DeVillier648a44a2021-11-12 10:23:21 -060095done
96
97# extract VBTs
98for VBT in $(grep vbt $DIR/cbfs.txt | cut -f1 -d\ ); do
Matt DeVillier0923c622022-10-15 12:04:08 -050099 $CBFSTOOL $IMAGE extract -r $REGION -n $VBT -f $DIR/$VBT
Matt DeVillier648a44a2021-11-12 10:23:21 -0600100done
101
102# extract IFWI
103IFWI=$(cbfstool $IMAGE layout -w | grep IFWI)
104if [ "$IFWI" != "" ]; then
105 $CBFSTOOL $IMAGE read -r IFWI -f $DIR/ifwi.bin
106fi
107
108# generate hashes
109(
110 cd $DIR
111 : >hashes.txt
112 for FILE in $(ls *.{bin,elf} 2>/dev/null); do
113 sha256sum $FILE >>hashes.txt
114 done
115)
116
117# a little housekeeping
118rm $DIR/cbfs.txt
119
120echo ""
121echo "All done"