if test -n ${distro_bootpart}; then setenv bootpart ${distro_bootpart}; else setenv bootpart 1; fi

if load ${devtype} ${devnum}:${bootpart} 0x12000000 /u-boot.imx; then
  echo "Loaded u-boot.imx";
  if sf probe 0; then
    echo "Found SPI flash";
    echo "  - erasing flash..";
    sf erase 0 0xC2000;

    echo "  - writing flash..";
    sf write 0x12000000 0x400 ${filesize};

    echo "  - verifying flash..";
    if sf read 0x12400000 0x400 ${filesize}; then
      if cmp.b 0x12000000 0x12400000 ${filesize}; then
        echo "U-Boot flashed successfully.";
      else
        echo "Error: verification failed.";
      fi
    else
      echo "Error: unable to read flash.";
    fi
  else
    echo "Error: unable to initialize flash.";
  fi
else
  echo "Error: u-boot.imx not found.";
fi
