Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to UN1CA #279

Open
wants to merge 3 commits into
base: fourteen
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 120 additions & 21 deletions buildenv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,68 @@

set -o allexport

PACKAGES=(
libbrotli-dev liblz4-dev libzstd-dev protobuf-compiler libgtest-dev
ffmpeg webp zipalign clang build-essential cmake openjdk-17-jdk
libncurses-dev bison flex libssl-dev libelf-dev python3
python-is-python3 cpio attr zip libprotobuf-dev make
libpcre2-dev ccache npm
)

echo "Checking dependencies..."
Copy link
Contributor

@Fede2782 Fede2782 Dec 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way you are considering that only Debian-based distros will be used for ROM building. You should adapt this logic to check if distro is Debian based. Possibly some packages are also available only in certain distros/releases.

Openjdk 11 is the minimum required version. And if I remember correctly also unzip package has to be installed.


MISSING_PACKAGES=()
for PACKAGE in "${PACKAGES[@]}"; do
if ! dpkg -s "$PACKAGE" >/dev/null 2>&1; then
MISSING_PACKAGES+=("$PACKAGE")
fi
done

if [ ${#MISSING_PACKAGES[@]} -eq 0 ]; then
echo "All dependencies are already installed."
else
echo "The following packages are missing: ${MISSING_PACKAGES[*]}"
echo "Attempting to install missing packages..."
sudo apt update
sudo apt install "${MISSING_PACKAGES[@]}"
fi

echo "Checking for missing Git submodules..."

if [ -f .gitmodules ]; then
UNINITIALIZED_SUBMODULES=$(git submodule status | grep '^-' || true)

if [ -n "$UNINITIALIZED_SUBMODULES" ]; then
echo "The following submodules are missing or uninitialized:"
echo "$UNINITIALIZED_SUBMODULES"
echo "Initializing and cloning submodules..."
git submodule update --init --recursive
if [ $? -eq 0 ]; then
echo "Submodules initialized and cloned successfully."
else
echo "Failed to clone submodules. Please check your repository configuration."
exit 1
fi
else
echo "All submodules are already initialized."
fi
else
echo "No submodules found in this repository."
fi

#create venv if not exist
if [ ! -f "venv/bin/activate" ]; then
echo "Virtual environment not found. Creating a new one..."
python3 -m venv venv
else
echo "Virtual environment already exists."
fi

source venv/bin/activate

pip3 install git+https://github.com/ananjaser1211/samloader.git &> /dev/null


# [
SRC_DIR="$(git rev-parse --show-toplevel)"
OUT_DIR="$SRC_DIR/out"
Expand All @@ -32,14 +94,30 @@ TOOLS_DIR="$OUT_DIR/tools/bin"

PATH="$TOOLS_DIR:$PATH"

run_cmd()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks workflows. You should change them too.

unica()
{
local CMD=$1
local CMDS
CMDS="$(find "scripts" -mindepth 1 -maxdepth 1 -type f -printf '%f\n' | sed "s/.sh//g")"

local YELLOW="\033[1;33m"
local CYAN="\033[1;36m"
local RESET_COLOR="\033[0m"

if [ -z "$CMD" ] || [ "$CMD" = "-h" ]; then
echo -e "Available cmds:\n$CMDS"
echo -e "Available cmds:"
for script in "scripts"/*.sh; do
CMD_NAME=$(basename "$script" .sh)
HELP_LINES=$(grep "^# CMD_HELP" "$script" | sed 's/^# CMD_HELP[[:space:]]*//')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought of mine, I think creating a function inside each script which prints the help is a bit better than this actual implementation.


if [ -n "$HELP_LINES" ]; then
echo -e "${YELLOW}$CMD_NAME${RESET_COLOR}:"
echo -e "${CYAN}$HELP_LINES${RESET_COLOR}"
echo ""
else
echo -e "${YELLOW}$CMD_NAME${RESET_COLOR} - No help available"
fi
done
return 1
elif ! echo "$CMDS" | grep -q -w "$CMD"; then
echo "\"$CMD\" is not valid."
Expand All @@ -50,31 +128,52 @@ run_cmd()
bash -e "$SRC_DIR/scripts/$CMD.sh" "$@"
fi
}

# ]

TARGETS="$(ls "$SRC_DIR/target")"
TARGETS=($(ls "$SRC_DIR/target"))

if [ "$#" != 1 ]; then
if [ "$#" -ne 1 ]; then
echo "Usage: source buildenv.sh <target>"
echo -e "Available devices:\n$TARGETS"
return 1
elif ! echo "$TARGETS" | grep -q -w "$1"; then
echo "\"$1\" is not valid target."
echo -e "Available devices:\n$TARGETS"
return 1
echo "No target specified. Please choose from the available devices below:"

select TARGET in "${TARGETS[@]}"; do
if [ -n "$TARGET" ]; then
echo "You selected: $TARGET"
export SELECTED_TARGET="$TARGET"
break
else
echo "Invalid selection. Please try again."
fi
done

if [ -z "$SELECTED_TARGET" ]; then
echo "No valid target selected. Exiting."
return 1
fi
else
mkdir -p "$OUT_DIR"
run_cmd build_dependencies || return 1
[ -f "$OUT_DIR/config.sh" ] && unset $(sed "/Automatically/d" "$OUT_DIR/config.sh" | cut -d= -f1)
bash "$SRC_DIR/scripts/internal/gen_config_file.sh" "$1" || return 1
source "$OUT_DIR/config.sh"

echo "=============================="
sed "/Automatically/d" "$OUT_DIR/config.sh"
echo "=============================="
if ! printf "%s\n" "${TARGETS[@]}" | grep -q -w "$1"; then
echo "\"$1\" is not a valid target."
echo "Available devices:"
printf "%s\n" "${TARGETS[@]}"
return 1
fi
SELECTED_TARGET="$1"
fi

mkdir -p "$OUT_DIR"
unica build_dependencies || return 1

if [ -f "$OUT_DIR/config.sh" ]; then
unset $(sed "/Automatically/d" "$OUT_DIR/config.sh" | cut -d= -f1)
fi

unset TARGETS
set +o allexport
bash "$SRC_DIR/scripts/internal/gen_config_file.sh" "$SELECTED_TARGET" || return 1
source "$OUT_DIR/config.sh"

echo "=============================="
sed "/Automatically/d" "$OUT_DIR/config.sh"
echo "type "unica -h" for help"


return 0
5 changes: 5 additions & 0 deletions scripts/apktool.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

# CMD_HELP Usage: apktool d[ecode]/b[uild] <apk> (<apk>...)
# CMD_HELP APK/JAR path MUST not be full and match an existing file inside work_dir
# CMD_HELP Output files will be stored in ($APKTOOL_DIR)
# CMD_HELP Recompiled apk will be copied back to its original directory

set -eu
shopt -s nullglob

Expand Down
2 changes: 2 additions & 0 deletions scripts/build_fs_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

# CMD_HELP Builds the finished system.img... if already at that point.

set -eu

# [
Expand Down
8 changes: 8 additions & 0 deletions scripts/cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

# CMD_HELP Available options (multiple can be accepted):
# CMD_HELP "all"
# CMD_HELP "odin"
# CMD_HELP "fw"
# CMD_HELP "apktool"
# CMD_HELP "work_dir"
# CMD_HELP "tools"

set -eu

ALL=false
Expand Down
63 changes: 51 additions & 12 deletions scripts/download_fw.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

# shellcheck disable=SC2162

# CMD_HELP Downloads the Firmware needed for selected device.

set -e

# [
Expand All @@ -27,25 +29,62 @@ GET_LATEST_FIRMWARE()
| grep latest | sed 's/^[^>]*>//' | sed 's/<.*//'
}

DOWNLOAD_FIRMWARE()
{
DOWNLOAD_FIRMWARE() {
local PDR
PDR="$(pwd)"

cd "$ODIN_DIR"
{ samfirm -m "$MODEL" -r "$REGION" -i "$IMEI" > /dev/null; } 2>&1 \
&& touch "$ODIN_DIR/${MODEL}_${REGION}/.downloaded" \
|| exit 1
[ -f "$ODIN_DIR/${MODEL}_${REGION}/.downloaded" ] && {
echo -n "$(find "$ODIN_DIR/${MODEL}_${REGION}" -name "AP*" -exec basename {} \; | cut -d "_" -f 2)/"
echo -n "$(find "$ODIN_DIR/${MODEL}_${REGION}" -name "CSC*" -exec basename {} \; | cut -d "_" -f 3)/"
echo -n "$(find "$ODIN_DIR/${MODEL}_${REGION}" -name "CP*" -exec basename {} \; | cut -d "_" -f 2)"
} >> "$ODIN_DIR/${MODEL}_${REGION}/.downloaded"
cd "$ODIN_DIR" || { echo "Failed to enter ODIN_DIR: $ODIN_DIR"; exit 1; }

# Directory for firmware download
local TARGET_DIR="${ODIN_DIR}/${MODEL}_${REGION}"

# Ensure the target directory exists
if [ ! -d "$TARGET_DIR" ]; then
mkdir -p "$TARGET_DIR" || { echo "Failed to create directory: $TARGET_DIR"; exit 1; }
fi

# Determine whether to use -s or -i flag based on the length of $IMEI
local SAMLOADER_FLAG
if [ "${#IMEI}" -eq 11 ]; then
SAMLOADER_FLAG="-s $IMEI"
else
SAMLOADER_FLAG="-i $IMEI"
fi

# Run samloader and check for success
echo "Downloading firmware for $MODEL in region $REGION..."
if ! samloader -m "$MODEL" -r "$REGION" $SAMLOADER_FLAG download -O "$TARGET_DIR"; then
echo "Firmware download failed."
exit 1
fi

# Mark download as successful
touch "$TARGET_DIR/.downloaded"

# Find and unzip firmware zip
local ZIP_FILE
ZIP_FILE=$(find "$TARGET_DIR" -name "*.zip" | head -n 1)
if [ -f "$ZIP_FILE" ]; then
echo "Unzipping $ZIP_FILE..."
unzip -q "$ZIP_FILE" -d "$TARGET_DIR" || { echo "Failed to unzip $ZIP_FILE"; exit 1; }
rm "$ZIP_FILE" # Optional: Remove the ZIP file after extracting
else
echo "No ZIP file found in $TARGET_DIR"
exit 1
fi

# Write firmware details to .downloaded file
{
echo -n "$(find "$TARGET_DIR" -name "AP*" -exec basename {} \; | cut -d "_" -f 2)/"
echo -n "$(find "$TARGET_DIR" -name "CSC*" -exec basename {} \; | cut -d "_" -f 3)/"
echo -n "$(find "$TARGET_DIR" -name "CP*" -exec basename {} \; | cut -d "_" -f 2)"
} >> "$TARGET_DIR/.downloaded"

echo ""
cd "$PDR"
cd "$PDR" || exit
}


FIRMWARES=( "$SOURCE_FIRMWARE" "$TARGET_FIRMWARE" )
IFS=':' read -a SOURCE_EXTRA_FIRMWARES <<< "$SOURCE_EXTRA_FIRMWARES"
if [ "${#SOURCE_EXTRA_FIRMWARES[@]}" -ge 1 ]; then
Expand Down
2 changes: 2 additions & 0 deletions scripts/extract_fw.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

# shellcheck disable=SC2162

# CMD_HELP Extracts downloaded Firmware

set -e

# [
Expand Down
2 changes: 2 additions & 0 deletions scripts/make_rom.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

# CMD_HELP Executes series of commands. Downloads, extracts, patches and packages the Rom.

set -Eeuo pipefail
START=$SECONDS

Expand Down
2 changes: 2 additions & 0 deletions scripts/print_modules_info.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

# shellcheck disable=SC2001

# CMD_HELP Prints the available module informations.

set -Ee

# [
Expand Down
Loading