From 65342893b7c5dd4edc35c0b5e7655af608f42bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20Olguy=20Can=C3=A9us?= Date: Thu, 15 Oct 2020 15:26:43 -0700 Subject: [PATCH] ELBERT: fpga_util: verify parition before programming (#98) 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: https://github.com/facebookexternal/openbmc.arista/pull/98 Reviewed By: benwei13 fbshipit-source-id: e2d400e982 --- .../openbmc-utils/files/fpga_util.sh | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/meta-facebook/meta-elbert/recipes-utils/openbmc-utils/files/fpga_util.sh b/meta-facebook/meta-elbert/recipes-utils/openbmc-utils/files/fpga_util.sh index aa4c638a0b2e..e83aebb2994f 100644 --- a/meta-facebook/meta-elbert/recipes-utils/openbmc-utils/files/fpga_util.sh +++ b/meta-facebook/meta-elbert/recipes-utils/openbmc-utils/files/fpga_util.sh @@ -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" @@ -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 @@ -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 @@ -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 } @@ -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) @@ -356,7 +380,7 @@ 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 @@ -364,16 +388,16 @@ do_pim() { 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