2023-06-21 21:38:13 +07:00
|
|
|
#!/usr/bin/bash
|
2023-06-23 02:59:30 +07:00
|
|
|
|
2023-06-21 21:38:13 +07:00
|
|
|
HOME=$(pwd)
|
|
|
|
MODELSFILE="models.yml"
|
|
|
|
|
|
|
|
while IFS= read -r line; do
|
2023-06-24 04:41:11 +07:00
|
|
|
PAT_VERSION=$(echo "${line}" | cut -f 1 -d ' ')
|
|
|
|
PAT_BUILD=$(echo "${line}" | cut -f 2 -d ' ')
|
|
|
|
MODEL=$(echo "${line}" | cut -f 3 -d ' ')
|
|
|
|
BUILD=$(echo "${line}" | cut -f 4 -d ' ')
|
2023-06-21 21:38:13 +07:00
|
|
|
CACHE_PATH="${HOME}/cache"
|
|
|
|
RAMDISK_PATH="${CACHE_PATH}/ramdisk"
|
|
|
|
PAT_FILE="${MODEL}_${BUILD}.pat"
|
|
|
|
PAT_PATH="${CACHE_PATH}/dl/${PAT_FILE}"
|
|
|
|
EXTRACTOR_PATH="${CACHE_PATH}/extractor"
|
|
|
|
EXTRACTOR_BIN="syno_extract_system_patch"
|
|
|
|
UNTAR_PAT_PATH="${CACHE_PATH}/${MODEL}/${BUILD}"
|
|
|
|
DSMPATH="${HOME}/dsm"
|
|
|
|
DESTINATION="${DSMPATH}/${MODEL}/${BUILD}"
|
|
|
|
FILESPATH="${HOME}/files"
|
|
|
|
DESTINATIONFILES="${FILESPATH}/${MODEL}/${BUILD}"
|
2023-06-23 02:59:30 +07:00
|
|
|
if [ ! -f "${DESTINATIONFILES}/dsm.tar" ] || [ ! -f "${DESTINATION}" ]; then
|
2023-06-24 00:51:51 +07:00
|
|
|
PAT_MODEL="$(echo "${MODEL}" | sed 's/ /%20/')"
|
2023-06-23 02:59:30 +07:00
|
|
|
echo "${PAT_MODEL} ${VERSION} ${BUILD}"
|
|
|
|
|
2023-06-24 00:10:20 +07:00
|
|
|
PAT_LINK="${PAT_VERSION}/${PAT_BUILD}/DSM_${PAT_MODEL}_${BUILD}.pat"
|
2023-06-23 02:59:30 +07:00
|
|
|
PAT_URL="https://global.synologydownload.com/download/DSM/release/${PAT_LINK}"
|
2023-06-21 21:38:13 +07:00
|
|
|
|
2023-06-24 00:28:59 +07:00
|
|
|
speed_a=$(ping -c 1 -W 5 global.synologydownload.com | awk '/time=/ {print $7}' | cut -d '=' -f 2)
|
|
|
|
speed_b=$(ping -c 1 -W 5 global.download.synology.com | awk '/time=/ {print $7}' | cut -d '=' -f 2)
|
2023-06-24 04:41:11 +07:00
|
|
|
fastest=$(echo -e "global.synologydownload.com ${speed_a:-999}\nglobal.download.synology.com ${speed_b:-999}" | sort -k2rn | head -1 | awk '{print $1}')
|
2023-06-23 02:59:30 +07:00
|
|
|
|
2023-06-24 04:41:11 +07:00
|
|
|
mirror=$(echo ${PAT_URL} | sed 's|^http[s]*://\([^/]*\).*|\1|')
|
2023-06-24 00:28:59 +07:00
|
|
|
echo "$(printf "Based on the current network situation, switch to %s mirror for download." "${fastest}")"
|
2023-06-24 04:41:11 +07:00
|
|
|
PAT_URL=$(echo ${PAT_URL} | sed "s/${mirror}/${fastest}/")
|
2023-06-23 02:59:30 +07:00
|
|
|
|
|
|
|
mkdir -p "${CACHE_PATH}/dl"
|
2023-06-21 21:38:13 +07:00
|
|
|
|
2023-06-23 02:59:30 +07:00
|
|
|
if [ ! -f "${PAT_PATH}" ]; then
|
|
|
|
echo "Downloading ${PAT_FILE}"
|
|
|
|
# Discover remote file size
|
2023-06-24 00:28:59 +07:00
|
|
|
STATUS=$(curl -k -w "%{http_code}" -L "${PAT_URL}" -o "${PAT_PATH}" --progress-bar)
|
2023-06-23 02:59:30 +07:00
|
|
|
if [ $? -ne 0 -o ${STATUS} -ne 200 ]; then
|
|
|
|
rm "${PAT_PATH}"
|
|
|
|
echo "Error downloading"
|
|
|
|
fi
|
|
|
|
if [ -f "${PAT_PATH}" ]; then
|
2023-06-24 00:15:48 +07:00
|
|
|
|
|
|
|
mkdir -p "${DESTINATION}"
|
|
|
|
mkdir -p "${DESTINATIONFILES}"
|
|
|
|
|
|
|
|
echo -n "Checking hash of pat: "
|
2023-06-24 04:41:11 +07:00
|
|
|
HASH=$(md5sum ${PAT_PATH} | awk '{print$1}')
|
2023-06-24 00:15:48 +07:00
|
|
|
echo "OK"
|
|
|
|
echo "${HASH}" >"${DESTINATION}/pat_hash"
|
2023-06-24 00:28:59 +07:00
|
|
|
echo "${PAT_URL}" >"${DESTINATION}/pat_url"
|
2023-06-24 00:15:48 +07:00
|
|
|
|
2023-06-23 02:59:30 +07:00
|
|
|
rm -rf "${UNTAR_PAT_PATH}"
|
|
|
|
mkdir -p "${UNTAR_PAT_PATH}"
|
|
|
|
echo -n "Disassembling ${PAT_FILE}: "
|
2023-06-21 21:38:13 +07:00
|
|
|
|
2023-06-24 00:28:59 +07:00
|
|
|
header=$(od -bcN2 ${PAT_PATH} | head -1 | awk '{print $3}')
|
2023-06-23 02:59:30 +07:00
|
|
|
case ${header} in
|
|
|
|
105)
|
|
|
|
echo "Uncompressed tar"
|
|
|
|
isencrypted="no"
|
|
|
|
;;
|
|
|
|
213)
|
|
|
|
echo "Compressed tar"
|
|
|
|
isencrypted="no"
|
|
|
|
;;
|
|
|
|
255)
|
|
|
|
echo "Encrypted"
|
|
|
|
isencrypted="yes"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo -e "Could not determine if pat file is encrypted or not, maybe corrupted, try again!"
|
|
|
|
;;
|
|
|
|
esac
|
2023-06-21 21:38:13 +07:00
|
|
|
|
2023-06-23 02:59:30 +07:00
|
|
|
if [ "${isencrypted}" = "yes" ]; then
|
|
|
|
# Check existance of extractor
|
|
|
|
if [ -f "${EXTRACTOR_PATH}/${EXTRACTOR_BIN}" ]; then
|
|
|
|
echo "Extractor cached."
|
|
|
|
fi
|
|
|
|
# Uses the extractor to untar pat file
|
|
|
|
echo "Extracting..."
|
|
|
|
LD_LIBRARY_PATH="${EXTRACTOR_PATH}" "${EXTRACTOR_PATH}/${EXTRACTOR_BIN}" "${PAT_PATH}" "${UNTAR_PAT_PATH}"
|
|
|
|
else
|
|
|
|
echo "Extracting..."
|
|
|
|
tar -xf "${PAT_PATH}" -C "${UNTAR_PAT_PATH}"
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "Error extracting"
|
|
|
|
fi
|
2023-06-22 00:21:33 +07:00
|
|
|
fi
|
2023-06-21 21:38:13 +07:00
|
|
|
|
2023-06-23 02:59:30 +07:00
|
|
|
echo -n "Checking hash of zImage: "
|
2023-06-24 04:41:11 +07:00
|
|
|
HASH=$(sha256sum ${UNTAR_PAT_PATH}/zImage | awk '{print$1}')
|
2023-06-23 02:59:30 +07:00
|
|
|
echo "OK"
|
2023-06-24 00:15:48 +07:00
|
|
|
echo "${HASH}" >"${DESTINATION}/zImage_hash"
|
2023-06-21 21:38:13 +07:00
|
|
|
|
2023-06-23 02:59:30 +07:00
|
|
|
echo -n "Checking hash of ramdisk: "
|
2023-06-24 04:41:11 +07:00
|
|
|
HASH=$(sha256sum ${UNTAR_PAT_PATH}/rd.gz | awk '{print$1}')
|
2023-06-23 02:59:30 +07:00
|
|
|
echo "OK"
|
2023-06-24 00:15:48 +07:00
|
|
|
echo "${HASH}" >"${DESTINATION}/ramdisk_hash"
|
2023-06-21 21:38:13 +07:00
|
|
|
|
2023-06-23 02:59:30 +07:00
|
|
|
echo -n "Copying files: "
|
|
|
|
cp "${UNTAR_PAT_PATH}/grub_cksum.syno" "${DESTINATION}"
|
|
|
|
cp "${UNTAR_PAT_PATH}/GRUB_VER" "${DESTINATION}"
|
|
|
|
cp "${UNTAR_PAT_PATH}/grub_cksum.syno" "${DESTINATION}"
|
|
|
|
cp "${UNTAR_PAT_PATH}/GRUB_VER" "${DESTINATION}"
|
|
|
|
cp "${UNTAR_PAT_PATH}/zImage" "${DESTINATION}"
|
|
|
|
cp "${UNTAR_PAT_PATH}/rd.gz" "${DESTINATION}"
|
|
|
|
cd "${DESTINATION}"
|
|
|
|
tar -cf "${DESTINATIONFILES}/dsm.tar" .
|
|
|
|
rm -f "${PAT_PATH}"
|
|
|
|
rm -rf "${UNTAR_PAT_PATH}"
|
|
|
|
echo "DSM extract complete: ${MODEL}_${BUILD}"
|
|
|
|
else
|
|
|
|
echo "DSM extract Error: ${MODEL}_${BUILD}"
|
|
|
|
fi
|
2023-06-22 00:21:33 +07:00
|
|
|
fi
|
2023-06-23 02:59:30 +07:00
|
|
|
cd ${HOME}
|
2023-06-21 21:38:13 +07:00
|
|
|
fi
|
2023-06-22 00:21:33 +07:00
|
|
|
done < ${MODELSFILE}
|
|
|
|
|
|
|
|
rm -rf "${CACHE_PATH}/dl"
|