Skip to content

Instantly share code, notes, and snippets.

@Heus-Sueh
Last active July 29, 2024 14:29
Show Gist options
  • Save Heus-Sueh/900acea3403a91eadd615c76e5d885fc to your computer and use it in GitHub Desktop.
Save Heus-Sueh/900acea3403a91eadd615c76e5d885fc to your computer and use it in GitHub Desktop.
A script that converts fastboot steps in xml to bash script for use on linux systems
#!/usr/bin/env bash
# Make bash more sane
set -euo pipefail
# PT-BR
# Para usar esse script você precisa dar permissão de execução, usando `chmod +x caminho_para_script.sh`
# Como usar: `./xml2bash caminho_para_flashfile.xml arquivo_de_saida.sh`
# Lembrando que o script gerado deve estar no diretório da rom para conseguir dar flash.
# EN-US
# To use this script you need to give it execution permission, using `chmod +x path_to_script.sh`
# Usage: `./xml2bash path_to_flashfile.xml output_file.sh`
# Remember that the generated script must be in the ROM directory to be able to flash it.
# Function to convert XML steps into fastboot commands
xml_file="$1"
output_sh="$2"
# Check if XML file exists
if [ ! -f "$xml_file" ]; then
echo "Error: XML file '$xml_file' not found."
exit 1
fi
convert_xml_to_bash() {
# Extracting header
phone_model=$(xmllint --xpath 'string(//header/phone_model/@model)' "$xml_file")
software_version=$(xmllint --xpath 'string(//header/software_version/@version)' "$xml_file")
subsidy_lock_config=$(xmllint --xpath 'string(//header/subsidy_lock_config/@name)' "$xml_file")
regulatory_config=$(xmllint --xpath 'string(//header/regulatory_config/@name)' "$xml_file")
sparsing_enabled=$(xmllint --xpath 'string(//header/sparsing/@enabled)' "$xml_file")
max_sparse_size=$(xmllint --xpath 'string(//header/sparsing/@max-sparse-size)' "$xml_file")
interface_name=$(xmllint --xpath 'string(//header/interfaces/interface/@name)' "$xml_file")
cat <<EOF
#!/bin/env bash
# Make bash more sane
set -euo pipefail
# Header
PHONE_MODEL="$phone_model"
SOFTWARE_VERSION="$software_version"
SUBSIDY_LOCK_CONFIG="$subsidy_lock_config"
REGULATORY_CONFIG="$regulatory_config"
SPARSING_ENABLED=$sparsing_enabled
MAX_SPARSE_SIZE=$max_sparse_size
INTERFACE="$interface_name"
erase() {
echo "Erasing partitions..."
EOF
# Extracting erase steps
xmllint --xpath '//step[@operation="erase"]' "$xml_file" | grep -oP 'partition="\K[^"]+' | while read -r partition; do
echo " \$SUDO fastboot erase $partition"
done
cat <<EOF
}
flash() {
echo "Flashing..."
EOF
# Extracting flash steps
xmllint --xpath '//step[@operation="flash"]' "$xml_file" | grep -oP 'partition="\K[^"]+|filename="\K[^"]+' | paste - - | while read -r filename partition; do
echo " \$SUDO fastboot flash $partition $filename"
done
cat <<EOF
}
main() {
echo '*** This script requires root privileges. ***'
SUDO=
if [ "\$UID" != "0" ]; then
if [ -e /usr/bin/sudo -o -e /bin/sudo ]; then
SUDO=sudo
else
echo '*** This script requires root privileges.'
exit 0
fi
fi
echo "Getting Devices..."
\$SUDO fastboot devices
echo "Getting var..."
\$SUDO fastboot getvar max-sparse-size
echo "Setting oem..."
\$SUDO fastboot oem fb_mode_set
erase
flash
\$SUDO fastboot oem fb_mode_clear
\$SUDO fastboot reboot
echo "Flashing completed!"
}
main
EOF
}
# Generate Bash script from XML
convert_xml_to_bash >"$output_sh"
# Make the output file executable
chmod +x "$output_sh"
echo "Script created: $output_sh"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment