Created
January 21, 2026 04:30
-
-
Save jhalon/97d5d14aa4554bb7df555e6261ef867c to your computer and use it in GitHub Desktop.
Build luadec for TP-Link router firmware. Applies OpenWRT patches + TP-Link's custom opcode shuffling from their GPL source code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -e # Exit on any error | |
| # Colors | |
| GREEN='\033[0;32m' | |
| BLUE='\033[0;34m' | |
| YELLOW='\033[0;33m' | |
| RED='\033[0;31m' | |
| NC='\033[0m' # No Color | |
| echo -e "${GREEN}==================================" | |
| echo "Building luadec with OpenWRT + TP-Link patches" | |
| echo -e "==================================${NC}" | |
| # Configuration | |
| OPENWRT_BRANCH="master" | |
| PATCH_DIR="patches.$OPENWRT_BRANCH" | |
| # Replace with GPL Code of Target | |
| TPLINK_GPL_URL="https://static.tp-link.com/upload/gpl-code/2025/202510/20251021/GPL_AX1800v5.tar.gz" | |
| # Install dependencies | |
| echo -e "${BLUE}[1/8] Installing dependencies...${NC}" | |
| sudo apt-get update > /dev/null 2>&1 | |
| sudo apt-get install -y libncurses-dev libreadline-dev curl git build-essential wget > /dev/null 2>&1 | |
| # Clone luadec | |
| echo -e "${BLUE}[2/8] Cloning luadec repository...${NC}" | |
| if [ -d "luadec" ]; then | |
| echo -e "${YELLOW} > Directory already exists, skipping${NC}" | |
| cd luadec | |
| else | |
| git clone https://github.com/viruscamp/luadec > /dev/null 2>&1 | |
| cd luadec | |
| fi | |
| # Initialize lua-5.1 submodule | |
| echo -e "${BLUE}[3/8] Initializing lua-5.1 submodule...${NC}" | |
| git submodule update --init lua-5.1 > /dev/null 2>&1 | |
| # Download OpenWRT patches | |
| echo -e "${BLUE}[4/8] Downloading OpenWRT Lua patches...${NC}" | |
| mkdir -p "$PATCH_DIR" | |
| cd "$PATCH_DIR" | |
| PATCHES=$(curl -sSL -H 'Accept: application/vnd.github.v3+json' \ | |
| "https://api.github.com/repos/openwrt/openwrt/contents/package/utils/lua/patches?ref=$OPENWRT_BRANCH" \ | |
| | grep -oP '"name":\s*"\K[^"]*\.patch' | sort) | |
| if [ -z "$PATCHES" ]; then | |
| echo -e "${RED}ERROR: No patches found!${NC}" | |
| exit 1 | |
| fi | |
| echo -e "${YELLOW} > Found $(echo "$PATCHES" | wc -l) patches${NC}" | |
| for patch in $PATCHES; do | |
| wget -q "https://raw.githubusercontent.com/openwrt/openwrt/$OPENWRT_BRANCH/package/utils/lua/patches/$patch" -O "$patch" 2>&1 | |
| done | |
| cd .. | |
| # Apply OpenWRT patches to lua-5.1 | |
| echo -e "${BLUE}[5/8] Applying OpenWRT patches...${NC}" | |
| cd lua-5.1 | |
| for patch_file in ../"$PATCH_DIR"/*.patch; do | |
| if [ -f "$patch_file" ]; then | |
| patch -p1 < "$patch_file" > /dev/null 2>&1 || { | |
| echo -e "${YELLOW} > WARNING: Patch $(basename "$patch_file") failed${NC}" | |
| } | |
| fi | |
| done | |
| # Download and extract TP-Link GPL code | |
| echo -e "${BLUE}[6/8] Downloading TP-Link GPL code...${NC}" | |
| cd ../.. | |
| if [ ! -f "GPL_AX1800v5.tar.gz" ]; then | |
| echo -e "${YELLOW} > Downloading archive...${NC}" | |
| wget -q "$TPLINK_GPL_URL" 2>&1 | |
| fi | |
| if [ ! -d "GPL_AX1800v5" ]; then | |
| echo -e "${YELLOW} > Extracting archive...${NC}" | |
| tar -xzf GPL_AX1800v5.tar.gz 2>&1 | |
| fi | |
| # Copy TP-Link's modified files | |
| echo -e "${YELLOW} > Applying TP-Link modifications...${NC}" | |
| cd luadec/lua-5.1/src | |
| MODIFIED_FILES=$(diff -qr ../../../GPL_AX1800v5/Iplatform/openwrt/ibase/lua/src/src/ . 2>/dev/null | grep "differ$" | wc -l) | |
| echo -e "${YELLOW} > Patching $MODIFIED_FILES modified files${NC}" | |
| diff -qr ../../../GPL_AX1800v5/Iplatform/openwrt/ibase/lua/src/src/ . 2>/dev/null | grep "differ$" | awk '{print $2}' | sed 's|../../../GPL_AX1800v5/Iplatform/openwrt/ibase/lua/src/src/||' | while read file; do | |
| cp "../../../GPL_AX1800v5/Iplatform/openwrt/ibase/lua/src/src/$file" "$file" 2>&1 | |
| done | |
| # Fix Makefile and compile | |
| echo -e "${BLUE}[7/8] Compiling lua-5.1...${NC}" | |
| MAKEFILE="Makefile" | |
| cp "$MAKEFILE" "$MAKEFILE.bak" | |
| sed -i '/# USE_READLINE=1/a PKG_VERSION = 5.1.5' "$MAKEFILE" | |
| sed -i 's/CFLAGS= -O2 -Wall $(MYCFLAGS)/CFLAGS= -fPIC -O2 -Wall $(MYCFLAGS)/' "$MAKEFILE" | |
| sed -i 's/$(CC) -o $@ -L\. -llua $(MYLDFLAGS) $(LUA_O) $(LIBS)/$(CC) -o $@ $(LUA_O) $(MYLDFLAGS) -L. -llua $(LIBS)/' "$MAKEFILE" | |
| sed -i 's/$(CC) -o $@ -L\. -llua $(MYLDFLAGS) $(LUAC_O) $(LIBS)/$(CC) -o $@ $(LUAC_O) $(MYLDFLAGS) -L. -llua $(LIBS)/' "$MAKEFILE" | |
| make linux > /dev/null 2>&1 || { | |
| echo -e "${RED} > Compilation failed! Showing output:${NC}" | |
| make linux 2>&1 | tail -20 | |
| exit 1 | |
| } | |
| # Set library path for runtime | |
| export LD_LIBRARY_PATH=$(pwd):$LD_LIBRARY_PATH | |
| # Compile luadec | |
| echo -e "${BLUE}[8/8] Compiling luadec...${NC}" | |
| cd ../../luadec | |
| make LUAVER=5.1 > /dev/null 2>&1 || { | |
| echo -e "${RED} > Compilation failed! Showing output:${NC}" | |
| make LUAVER=5.1 2>&1 | tail -20 | |
| exit 1 | |
| } | |
| echo "" | |
| echo -e "${GREEN}==================================" | |
| echo "Build complete!" | |
| echo -e "==================================${NC}" | |
| echo "" | |
| echo -e "luadec binary: ${YELLOW}$(pwd)/luadec${NC}" | |
| echo "" | |
| echo "Test with:" | |
| echo -e " ${YELLOW}./luadec /path/to/tplink/file.lua${NC}" | |
| echo "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment