commit 85e58e5d3268e94f482df111f3dc931c2462d04b Author: timoxa0 Date: Wed Aug 14 15:26:55 2024 +0500 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d80ed8a --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +./dtb-6.1.10-nabu +./generate_gki_certificate.py +./linux.boot.img +./mkbootimg.py +./vmlinuz-6.1.10-nabu diff --git a/mklonimg.cmd b/mklonimg.cmd new file mode 100755 index 0000000..486f4be --- /dev/null +++ b/mklonimg.cmd @@ -0,0 +1,107 @@ +@(set "0=%~f0"^)#) & powershell -nop -c iex([io.file]::ReadAllText($env:0)) & exit /b +$CMDLINE = "root=PARTLABEL=linux loglevel=3" + +function Log { + Write-Host -NoNewline "==> " -ForegroundColor Green + Write-Host "$args" +} + +function LogErr { + Write-Host -NoNewline "==> " -ForegroundColor Red + Write-Host "$args" +} + + +$PYTHON = if (Get-Command python -ErrorAction SilentlyContinue) { + "python" +} else { + LogErr "Python 3 not found" + exit 1 +} + + +# Begin script + +if ($args.Count -eq 2) { + $VMLINUZ = $args[0] + $DTB = $args[1] + $INTERACTIVE = $false +} else { + $VMLINUZ = Read-Host "Drag and drop vmlinuz here and press enter" + $DTB = Read-Host "Drag and drop dtb here and press enter" + $INTERACTIVE = $true +} + +# Check if files exist +if (-not (Test-Path $VMLINUZ)) { + LogErr "vmlinuz not found at $VMLINUZ" + exit 1 +} + +if (-not (Test-Path $DTB)) { + LogErr "dtb not found at $DTB" + exit 1 +} + +# Download mkbootimg and related files if not present +if (-not (Test-Path ./mkbootimg.py) -or -not (Test-Path ./generate_gki_certificate.py)) { + Log "Downloading mkbootimg from Google" + + $err = $false + + try { + $mkbootimgUrl = "https://android.googlesource.com/platform/system/tools/mkbootimg/+/refs/heads/main/mkbootimg.py?format=text" + $mkbootimgContent = (Invoke-WebRequest -Uri $mkbootimgUrl).Content + $mkbootimgDecoded = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($mkbootimgContent)) + $mkbootimgDecoded = $mkbootimgDecoded -replace 'from gki.generate_gki_certificate import generate_gki_certificate', 'from generate_gki_certificate import generate_gki_certificate' + $mkbootimgDecoded | Out-File -FilePath ./mkbootimg.py -Encoding UTF8 + } catch { + $err = $true + } + + try { + $gkiCertUrl = "https://android.googlesource.com/platform/system/tools/mkbootimg/+/refs/heads/main/gki/generate_gki_certificate.py?format=text" + $gkiCertContent = (Invoke-WebRequest -Uri $gkiCertUrl).Content + $gkiCertDecoded = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($gkiCertContent)) + $gkiCertDecoded | Out-File -FilePath ./generate_gki_certificate.py -Encoding UTF8 + } catch { + $err = $true + } + + if ($err) { + LogErr "Failed to download mkbootimg" + exit 1 + } +} + +# Combine vmlinuz and dtb into a single zImage file +try { + Get-Content -Path $VMLINUZ, $DTB -AsByteStream -Read 1024 | Set-Content -Path .\zImage -AsByteStream +} catch { + try { + Get-Content -Path $VMLINUZ, $DTB -Encoding Byte -Read 1024 | Set-Content -Path .\zImage -Encoding Byte + } catch { + LogErr "Failed to create zImage" + exit 1 + } +} + +# Create the boot image using Python script +$pythonCmd = "$PYTHON mkbootimg.py --kernel zImage --cmdline `"$CMDLINE`" --base 0x00000000 --kernel_offset 0x00008000 --tags_offset 0x00000100 --pagesize 4096 --id -o linux.boot.img" +Invoke-Expression $pythonCmd | Out-Null + +if ($LASTEXITCODE -ne 0) { + LogErr "Failed to create boot image" + Remove-Item .\zImage + exit 1 +} else { + Log "Created: linux.boot.img" +} + +# Clean up +Remove-Item .\zImage +Remove-Item -Recurse -Force .\__pycache__ + +if ($INTERACTIVE) { + $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown'); +} diff --git a/mklonimg.sh b/mklonimg.sh new file mode 100755 index 0000000..65d58a0 --- /dev/null +++ b/mklonimg.sh @@ -0,0 +1,105 @@ +#!/usr/bin/env bash + +# mkbootimg is the property of The Android Open Source Project and licensed under the Apache License, Version 2.0 +# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +CMDLINE="root=PARTLABEL=linux loglevel=3" + +VMLINUZ="/boot/efi/vmlinuz-6.1.10-nabu" +DTB="/boot/efi/dtb-6.1.10-nabu" + +function log() { + printf "\e[1m\e[92m==>\e[0m \e[1m%s\e[0m\n" "$*" +} + +function log_err() { + printf "\e[1m\e[31m==>\e[0m \e[1m%s\e[0m\n" "$*" +} + +function sigterm_handler() { + printf "\e[1m\e[31m>\e[0m \e[1m%s\e[0m\n" "Shutdown signal received." + exit 1 +} + +trap 'trap " " SIGINT SIGTERM SIGHUP; kill 0; wait; sigterm_handler' SIGINT SIGTERM SIGHUP + +which base64 > /dev/null 2>&1 || { + log_err "base64 not found" + exit 1 +} + +which curl > /dev/null 2>&1 || { + log_err "curl not found" + exit 1 +} + +if which python3 > /dev/null 2>&1; then + PYTHON="python3" +elif which python > /dev/null 2>&1 && [[ "$(python -V)" =~ "Python 3" ]]; then + PYTHON="python" +else + log_err "python 3 not found" + exit 1 +fi + +if which doas > /dev/null 2>&1; then + SUDO=doas +elif which sudo > /dev/null 2>&1; then + SUDO=sudo +elif [ "$(id -u)" -eq "0" ]; then + SUDO="" +else + log_err "sudo/doas not found" +fi + + +# Begin script + +if [ $# -eq 2 ]; then + VMLINUZ="$1" + DTB="$2" +else + log "Using default vmlinuz and dtb path" + log "vmlinuz: $VMLINUZ" + log "dtb: $DTB" +fi + +[ -f "$VMLINUZ" ] || { + log_err "vmlinuz not found at $VMLINUZ" + exit 1 +} + +[ -f "$DTB" ] || { + log_err "dtb not found at $DTB" + exit 1 +} + +[ -r "$VMLINUZ" ] && [ -r "$DTB" ] && SUDO="" + +{ [ -f ./mkbootimg.py ] && [ -f ./generate_gki_certificate.py ]; } || { + log "Downloading mkbootimg from google" + curl -s "https://android.googlesource.com/platform/system/tools/mkbootimg/+/refs/heads/main/mkbootimg.py?format=text" \ + | base64 -d \ + | sed "s/from gki.generate_gki_certificate import generate_gki_certificate/from generate_gki_certificate import generate_gki_certificate/g" \ + > mkbootimg.py || ERR="1" + curl -s "https://android.googlesource.com/platform/system/tools/mkbootimg/+/refs/heads/main/gki/generate_gki_certificate.py?format=text" \ + | base64 -d \ + > generate_gki_certificate.py || ERR="1" + [[ "$ERR" -eq "1" ]] && { + log_err "Failed to download mkbootimg" + exit 1 + } +} + +$SUDO cat "$VMLINUZ" "$DTB" > zImage || { + log_err "Failed to create zImage" + exit 1 +} +$PYTHON mkbootimg.py --kernel zImage --cmdline "$CMDLINE" --base 0x00000000 --kernel_offset 0x00008000 --tags_offset 0x00000100 --pagesize 4096 --id -o linux.boot.img > /dev/null || { + log_err "Failed to create boot image" + rm ./zImage + exit 1 +} +rm ./zImage +rm -r ./__pycache__ +log "Created ./linux.boot.img"