Skip to content

Instantly share code, notes, and snippets.

@Linux4
Last active January 23, 2025 10:28
Show Gist options
  • Save Linux4/243e4aab83867e2b3f4322c2a0eb5a3e to your computer and use it in GitHub Desktop.
Save Linux4/243e4aab83867e2b3f4322c2a0eb5a3e to your computer and use it in GitHub Desktop.
Flash Samsung factory image packages on Linux
#!/bin/bash
if [ -z "$1" ] || [ ! -f "$1" ]; then
echo "Usage: $0 <zip> <wipe y/n>"
exit 1
fi
REQUIREMENTS="git g++ heimdall lz4 make unzip"
for REQUIREMENT in $REQUIREMENTS; do
[ -z "$(command -v $REQUIREMENT)" ] && echo "Error: Please install $REQUIREMENTS" && exit 1
done
ZIP="$(realpath $1)"
DIR="./tmp_${1}"
rm -rf "$DIR"
mkdir "$DIR"
cd "$DIR"
WIPE="$2"
[ "$WIPE" != "y" ] && WIPE="n"
CSC="HOME_CSC"
[ "$WIPE" = "y" ] && CSC="CSC"
for PART in BL AP CP "$CSC"; do
echo "Unpacking $PART"
unzip "$ZIP" "${PART}*"
tar xf "${PART}"*.md5
rm "${PART}"*.md5
done
# Extract PIT which is only in regular CSC package, not HOME_CSC
[ "$WIPE" = "n" ] && unzip -p "$ZIP" "CSC*" | tar xf - --wildcards "*.pit"
for LZ4 in *.lz4; do
lz4 -d "$LZ4"
rm -f "$LZ4"
done
PIT="$(ls *.pit)"
echo "Using PIT $PIT"
git clone https://github.com/Linux4/firmware-update
make -C firmware-update/tools/pithelper
GET_PARTITION="./firmware-update/tools/pithelper/get-pit-partition"
HEIMDALL_CMD="heimdall flash"
for PART in *; do
[ ! -f "$PART" ] && continue;
[ "$PART" = "$PIT" ] && continue;
[ "$WIPE" = "n" ] && [ "$(basename $PART)" = "userdata.img" ] && continue;
PARTITION="$($GET_PARTITION $PIT $(basename $PART))"
[ -z "$PARTITION" ] && echo "Error: $PART not assigned to any partition" && exit 1
HEIMDALL_CMD="$HEIMDALL_CMD --${PARTITION} ${PART}"
done
$HEIMDALL_CMD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment