###############################################################################
# #
# IPFire.org - A linux based firewall #
-# Copyright (C) 2010 IPFire Team <info@ipfire.org> #
+# Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# #
###############################################################################
-#lfs change the url while build!
-IPFireISO=ipfire.iso
-#
-
-#Get user defined download from boot cmdline
-grep "netinstall=" /proc/cmdline > /dev/null && CMDLINE=1
-if ( [ "$CMDLINE" == "1" ]); then
- read CMDLINE < /proc/cmdline
- POS=${CMDLINE%%netinstall*}
- POS=${#POS}
- IPFireISO=`echo ${CMDLINE:POS} | cut -d"=" -f2 | cut -d" " -f1`
+function download() {
+ wget -U "IPFire-NetInstall/2.x" "$@"
+}
+
+if [ $# -lt 2 ]; then
+ echo "$0: Insufficient number of arguments" >&2
+ exit 2
+fi
+
+OUTPUT="${1}"
+URL="${2}"
+
+# Mount a tmpfs which is big enough to hold the ISO image
+OUTPUT_DIR="${OUTPUT%/*}"
+
+mkdir -p "${OUTPUT_DIR}"
+if ! mount -t tmpfs none "${OUTPUT_DIR}" -o size=512M; then
+ echo "Could not mount tmpfs to ${OUTPUT_DIR}" >&2
+ exit 1
fi
-echo
-echo "Configure Network with DHCP..."
-dhcpcd
-echo
-echo "Sleep 15s..."
-sleep 15
-echo
-echo "Download with wget..."
-wget $IPFireISO -O /tmp/download.iso -t3 -U IPFire_NetInstall/2.x
-wget $IPFireISO.md5 -O /tmp/download.iso.md5 -t3 -U IPFire_NetInstall/2.x
-echo
-echo "Checking download..."
-md5_file=`md5sum /tmp/download.iso | cut -d" " -f1`
-md5_down=`cat /tmp/download.iso.md5 | cut -d" " -f1`
-if [ "$md5_file" == "$md5_down" ]; then
- echo -n "/tmp/download.iso" > /tmp/source_device
- exit 0
+echo "Downloading ${URL}..."
+if ! download -O "${OUTPUT}" "${URL}"; then
+ echo "Download failed" >&2
+
+ rm -f "${OUTPUT}"
+ exit 1
fi
-echo "Error - SKIP"
-exit 10
+
+# Download went well. Checking for BLAKE2 sum
+if download -O "${OUTPUT}.b2" "${URL}.b2" &>/dev/null; then
+ # Read downloaded checksum
+ read -r b2sum rest < "${OUTPUT}.b2"
+ rm -f "${OUTPUT}.b2"
+
+ # Compute checkum of downloaded image file
+ read -r b2sum_image rest <<< "$(b2sum "${OUTPUT}")"
+
+ if [ "${b2sum}" != "${b2sum_image}" ]; then
+ echo "BLAKE2 checksum mismatch: ${b2sum} != ${b2sum_image}" >&2
+ exit 2
+ fi
+fi
+
+exit 0