Skip to content

Instantly share code, notes, and snippets.

@R0rt1z2
Last active December 11, 2022 18:28
Show Gist options
  • Save R0rt1z2/63c556fefdacf3bc077047e7b0b3a7fa to your computer and use it in GitHub Desktop.
Save R0rt1z2/63c556fefdacf3bc077047e7b0b3a7fa to your computer and use it in GitHub Desktop.
Parse MediaTek bootloader logs (extracted with fastboot oem dump_pllklog)
# To install (and download at the same time) the tool in your system, run the following command:
# Invoke-WebRequest https://gist.githubusercontent.com/R0rt1z2/63c556fefdacf3bc077047e7b0b3a7fa/raw/install.ps1 | Invoke-Expression
# Note that you might have to disable Windows Defender to run this script.
# The URL where the file is going to be downloaded from
$URL = "https://gist.github.com/R0rt1z2/63c556fefdacf3bc077047e7b0b3a7fa/raw/parse_log.py"
# Check if Python is installed on the system
if (Get-Command python -ErrorAction SilentlyContinue) {
# If Python is installed, print a message and the version
Write-Host "[?] Python is installed on this system." -ForegroundColor Green
} else {
# Python is not installed on the system
Write-Host "[-] Python is not installed on the system" -ForegroundColor Red
exit 1
}
# Get the Python installation directory
$pythonDir = Split-Path ((Get-Command python).Source)
# If the script is running under a venv, bail out since we don't support that
if ($pythonDir -like "*\venv\*") {
Write-Host "[-] This script does not support running under a virtual environment." -ForegroundColor Red
exit 1
}
# If the path is in the Program Files directory, the script must be run as administrator
if ($pythonDir -like "C:\Program Files*") {
# Check if the script is running as administrator
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
# The script is not running as administrator
Write-Host "[-] The script must be run as administrator" -ForegroundColor Red
exit 1
}
}
# Tell the user where the file is going to be downloaded to
Write-Host "[?] Downloading the file to $pythonDir" -ForegroundColor Yellow
# If the 'Scripts' folder is under the installation folder of Python but it's not in the
# PATH environment variable, add it to the PATH environment variable.
if (Test-Path "$PythonPath\Scripts") {
if (-not ($env:PATH -match "$PythonPath\Scripts")) {
# Warn the user about the 'Scripts' folder not being in the PATH environment variable
Write-Host "[!] The '$PythonPath\Scripts' folder is not in the PATH environment variable" -ForegroundColor Yellow
# Also tell the user we're going to temporarily add it to the PATH environment variable
Write-Host "[!] We're going to temporarily add it to the PATH environment variable" -ForegroundColor Yellow
$env:PATH += ";$PythonPath\Scripts"
}
}
# Download the file and place it under the 'Scripts' folder of the installation folder of Python
# If error occurs, hide the error message
Invoke-WebRequest -Uri $URL -OutFile "$pythonDir\Scripts\parse_log.py" -ErrorAction SilentlyContinue
# Make sure the file was downloaded successfully
if (Test-Path "$pythonDir\Scripts\parse_log.py") {
# The file was downloaded successfully
Write-Host "[+] The file was downloaded successfully" -ForegroundColor Green
} else {
# The file was not downloaded successfully
Write-Host "[+] The file was not downloaded successfully" -ForegroundColor Red
exit 1
}
# Since Windows doesn't support shebangs, we need to create a batch file that will run the script
# with Python and pass the arguments to the script.
# The batch file will be placed under the 'Scripts' folder of the installation folder of Python
# and will be named 'parse_log.bat'
$BatchFile = @"
@echo off
python "%~dp0\parse_log.py" %*
"@ | Out-File "$pythonDir\Scripts\parse_log.bat" -Encoding ASCII
# Make sure the batch file was created successfully
if (Test-Path "$pythonDir\Scripts\parse_log.bat") {
# The batch file was created successfully
Write-Host "[+] The wrapper was created successfully" -ForegroundColor Green
} else {
# The batch file was not created successfully
Write-Host "[-] The wrapper was not created successfully" -ForegroundColor Red
exit 1
}
# Tell the user the tool has been installed but don't exit, since the user might want to see the
# output of the script.
Write-Host "[+] The tool has been installed" -ForegroundColor Green
# To install the tool with curl in your system, run the following command:
# curl -sSL https://gist.githubusercontent.com/R0rt1z2/63c556fefdacf3bc077047e7b0b3a7fa/raw/install.sh | bash
# The URL where the file is going to be downloaded from
URL="https://gist.githubusercontent.com/R0rt1z2/63c556fefdacf3bc077047e7b0b3a7fa/raw/parse_log.py"
# If the user has root privileges then the script will be installed in /usr/bin
# otherwise it will be installed in the user's home directory under the bin folder.
if [ "$EUID" -eq 0 ]; then
echo "Installing as root under /usr/bin"
wget -O /usr/bin/parse_log $URL
chmod +x /usr/bin/parse_log
else
echo "Installing as user under $HOME/bin"
mkdir -p ~/bin
wget -O ~/bin/parse_log $URL
chmod +x ~/bin/parse_log
fi
# We're done, bail out with a success exit code
exit 0
#!/usr/bin/env python3
# This program allows you to parse the log file from the bootloader
# and save it to a file or print it to the console. It accepts the
# following arguments:
# - The log file to parse
# - The "-s" parameter (optional)
# - The output file (optional)
import sys
def parse_log_file(log_file):
# Read all the lines in the log file
with open(log_file, "r") as f:
log_buf = f.readlines()
# Create an empty string to store the parsed log lines
log_lines = ""
# Loop through the log buffer and parse each line
for line in log_buf:
# If the line is either empty or ONLY contains (bootloader), skip it
if line == "\n" or line.rstrip() == "(bootloader)":
continue
# If the line contains (bootloader) (+ something else) that means it's a continuation of the previous line
if "(bootloader)" in line:
# Remove the (bootloader) part of the line since it's not needed anymore
line = line.replace("(bootloader) ", "")
# Remove the last newline character from the log_lines string
log_lines = log_lines[:-2]
# Add the line to the previous line
log_lines += line
else:
# Add a newline character to the end of the line
log_lines += line + "\n"
# Return the log_lines list
return log_lines
def main():
# Check if the user provided enough arguments. If not, print the
# usage and exit
if len(sys.argv) < 2:
exit("Usage: python3 parse_log.py <log_file> [-s] [output_file]")
# Parse the log file
log_lines = parse_log_file(sys.argv[1])
# Check if the user wants to print the parsed log file or save it
# (must provide the "-s" parameter and output file) to a file
if len(sys.argv) == 4 and sys.argv[2] == "-s":
# Save the parsed log file to a file
with open(sys.argv[3], "w") as f:
f.write(log_lines)
else:
# Print the parsed log file
print(log_lines)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment