Skip to content

Commit

Permalink
ELBERT: fpga_util: verify parition before programming (#98)
Browse files Browse the repository at this point in the history
Summary:
ELBERT: fpga_util: verify parition before programming

Add some additional checks in fpga_util to prevent programming the wrong image type in the partition. Only enforcing partition 1, 2 at the moment.

Testing:

# Program the wrong image type
root@bmc-oob:/tmp# sha1sum elbert_pim_pim16q_0x6v4.abin
0c4520e203726c6f0edd07afd10b348c314cd4e9  elbert_pim_pim16q_0x6v4.abin
root@bmc-oob:/tmp# fpga_util.sh pim8ddm program elbert_pim_pim16q_0x6v4.abin
Detected PIM Flash device.
Selected partition pim8ddm to program.
FPGA type in image, 1, does not match expected FPGA type, 2, for partition pim8ddm

# Program the wrong image type
root@bmc-oob:/tmp# sha1sum elbert_pim_pim8ddm_0x7v3.abin
1cb2ed7139d62aceaf3642bfe80b9d28430f9c11  elbert_pim_pim8ddm_0x7v3.abin
root@bmc-oob:/tmp# fpga_util.sh pim16q program elbert_pim_pim8ddm_0x7v3.abin
Detected PIM Flash device.
Selected partition pim16q to program.
FPGA type in image, 2, does not match expected FPGA type, 1, for partition pim16q

#Test programming correct image
root@bmc-oob:/tmp# fpga_util.sh pim16q program elbert_pim_pim16q_0x6v4.abin
Detected PIM Flash device.
Selected partition pim16q to program.
/tmp/stripped_pim_image size 442733 < 33554432 (32MB)
Resizing /tmp/stripped_pim_image by 33111699 bytes to /tmp/pim_image_32mb.bin
flashrom v1.2 on Linux 5.6.19-elbert (armv7l)
flashrom is free software, get the source code at https://flashrom.org

Using region: "pim16q".
Using clock_gettime for delay loops (clk_id: 1, resolution: 1ns).
Using default 2000kHz clock. Use 'spispeed' parameter to override.
Found Micron flash chip "MT25QL256" (32768 kB, SPI) on linux_spi.
===
This flash part has status UNTESTED for operations: PROBE READ ERASE WRITE
The test status of this chip may have been updated in the latest development
version of flashrom. If you are running the latest development version,
please email a report to flashrom@flashrom.org if any of the above operations
work correctly for you with this flash chip. Please include the flashrom log
file for all operations you tested (see the man page for details), and mention
which mainboard or programmer you tested in the subject line.
Thanks for your help!
Reading old flash chip contents... done.
Erasing and writing flash chip... Erase/write done.
Verifying flash... VERIFIED.

# Running just the verify command. This is not needed right after as program command also verifies.
root@bmc-oob:/tmp# fpga_util.sh pim16q verify elbert_pim_pim16q_0x6v4.abin
Detected PIM Flash device.
Selected partition pim16q to program.
/tmp/stripped_pim_image size 442733 < 33554432 (32MB)
Resizing /tmp/stripped_pim_image by 33111699 bytes to /tmp/pim_image_32mb.bin
flashrom v1.2 on Linux 5.6.19-elbert (armv7l)
flashrom is free software, get the source code at https://flashrom.org

Using region: "pim16q".
Using clock_gettime for delay loops (clk_id: 1, resolution: 1ns).
Using default 2000kHz clock. Use 'spispeed' parameter to override.
Found Micron flash chip "MT25QL256" (32768 kB, SPI) on linux_spi.
===
This flash part has status UNTESTED for operations: PROBE READ ERASE WRITE
The test status of this chip may have been updated in the latest development
version of flashrom. If you are running the latest development version,
please email a report to flashrom@flashrom.org if any of the above operations
work correctly for you with this flash chip. Please include the flashrom log
file for all operations you tested (see the man page for details), and mention
which mainboard or programmer you tested in the subject line.
Thanks for your help!
Verifying flash... VERIFIED.

Pull Request resolved: facebookexternal/openbmc.arista#98

Reviewed By: benwei13

fbshipit-source-id: e2d400e982
  • Loading branch information
joancaneus authored and facebook-github-bot committed Oct 17, 2020
1 parent 65a776a commit 6534289
Showing 1 changed file with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,23 +181,30 @@ do_fan() {
program_spi_image() {
ORIGINAL_IMAGE="$1" # Image path
CHIP_TYPE="$2" # Chip type
ACTION="${4^^}" # Program or verify

case "${3^^}" in # Selects passed partition name
PIM_BASE)
PARTITION="pim_base"
SKIP_MB=0
# TODO: Restrict PIM_BASE later
# FPGA_TYPE=0
FPGA_TYPE=-1
;;
PIM16Q)
PARTITION="pim16q"
SKIP_MB=1
FPGA_TYPE=1
;;
PIM8DDM)
PARTITION="pim8ddm"
SKIP_MB=2
FPGA_TYPE=2
;;
FULL)
PARTITION="full" # Flash entire spi flash
SKIP_MB=0
FPGA_TYPE=-1
;;
*)
echo "Unknown region $3 specified"
Expand All @@ -207,6 +214,17 @@ program_spi_image() {

echo "Selected partition $PARTITION to program."

if [ "$FPGA_TYPE" -ge 0 ] ; then
# Verify the image type
IMG_FPGA_TYPE=$(head -c3 "$ORIGINAL_IMAGE" | hexdump -v -e '/1 "%u\n"' | \
tail -n1)
if [ "$IMG_FPGA_TYPE" != "$FPGA_TYPE" ]; then
echo "FPGA type in image, $IMG_FPGA_TYPE, does not match expected" \
"FPGA type, $FPGA_TYPE, for partition $PARTITION"
exit 1
fi
fi

# We need to 32 MB binary, we will fill if needed with 1s.
# We will only program/verify the specified image partition
EEPROM_SIZE="33554432" # 32 MB
Expand All @@ -218,11 +236,17 @@ program_spi_image() {
exit 1
fi

OPERATION="-w"
if [ "$ACTION" = "VERIFY" ]
then
OPERATION="-v"
fi

fill=$((EEPROM_SIZE - IMAGE_SIZE))
if [ "$fill" = 0 ] ; then
echo "$ORIGINAL_IMAGE already 32MB, no need to resize..."
flashrom -p linux_spi:dev=/dev/"$SMB_SPIDEV" -c "$CHIP_TYPE" -N \
--layout /etc/elbert_pim.layout --image "$PARTITION" -w "$ORIGINAL_IMAGE"
--layout /etc/elbert_pim.layout --image "$PARTITION" $OPERATION "$ORIGINAL_IMAGE"
else
bs=1048576 # 1MB blocks

Expand Down Expand Up @@ -252,7 +276,7 @@ program_spi_image() {
fi
# Program the 32MB pim image
flashrom -p linux_spi:dev=/dev/"$SMB_SPIDEV" -c "$CHIP_TYPE" -N \
--layout /etc/elbert_pim.layout --image "$PARTITION" -w "$TEMP_IMAGE"
--layout /etc/elbert_pim.layout --image "$PARTITION" $OPERATION "$TEMP_IMAGE"
rm "$TEMP_IMAGE"
fi
}
Expand Down Expand Up @@ -330,12 +354,12 @@ strip_pim_header() {

do_pim() {
case "${1^^}" in
PROGRAM)
PROGRAM|VERIFY)
# Image with header stripped
BIN_IMAGE="/tmp/stripped_pim_image"
connect_pim_flash
strip_pim_header "$2" "$BIN_IMAGE"
program_spi_image "$BIN_IMAGE" "MT25QL256" "$3"
program_spi_image "$BIN_IMAGE" "MT25QL256" "$3" "$1"
rm "$BIN_IMAGE"
;;
READ)
Expand All @@ -356,24 +380,24 @@ do_pim() {
fi
;;
*)
echo "PIM only supports program/read/erase action. Exiting..."
echo "PIM only supports program/verify/read/erase action. Exiting..."
exit 1
;;
esac
}

do_th4_qspi() {
case "${1^^}" in
PROGRAM)
PROGRAM|VERIFY)
connect_th4_qspi_flash
program_spi_image "$2" "MT25QU256"
program_spi_image "$2" "MT25QU256" full "$1"
;;
READ)
connect_th4_qspi_flash
flashrom -p linux_spi:dev=/dev/"$SMB_SPIDEV" -c "MT25QU256" -r "$2"
;;
*)
echo "TH4 QSPI only supports program/read action. Exiting..."
echo "TH4 QSPI only supports program/verify/read action. Exiting..."
exit 1
;;
esac
Expand Down

0 comments on commit 6534289

Please sign in to comment.