android
2024-07-25 2dcf9cc893a27c50d538baf8b5d47ff06fe495d8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/bin/bash
 
# this project absolute path
PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
 
# top project absolute path
TOP_PATH=$(realpath $PRJ_PATH/..)
 
# SDK build workspace
SDK_DIR=android_build
 
# binaries build prefix install path
PRFX_PATH=$PRJ_PATH/install
 
# binaries finally install path if needed
#INST_PATH=/tftp
 
# download taballs path
TARBALL_PATH=$PRJ_PATH/tarballs
 
# config file path
CONF_FILE=$TOP_PATH/config.json
 
#4g files path
FILES_PATCH_4G=$PRJ_PATH/patches/Quectel_RILv3.6.24
 
#wifi firmware path
FILES_PATCH_WIFI=$PRJ_PATH/patches/wifi/
 
# shell script will exit once get command error
set -e
 
#+-------------------------+
#| Shell script functions  |
#+-------------------------+
 
function pr_error() {
    echo -e "\033[40;31m $1 \033[0m"
}
 
function pr_warn() {
    echo -e "\033[40;33m $1 \033[0m"
}
 
function pr_info() {
    echo -e "\033[40;32m $1 \033[0m"
}
 
# decompress a packet to destination path
function do_unpack()
{
    tarball=$1
    dstpath='pwd'
 
    if [[ $# == 2 ]] ; then
        dstpath=$2
    fi
 
    pr_info "decompress $tarball => $dstpath"
 
    mkdir -p $dstpath
    case $tarball in
        *.tar.gz)
            tar -xzf $tarball -C $dstpath
            ;;
 
        *.tgz)
            tar -xzf $tarball -C $dstpath
            ;;
 
        *.tar.bz2)
            tar -xjf $tarball -C $dstpath
            ;;
 
        *.tar.xz)
            tar -xJf $tarball -C $dstpath
            ;;
 
        *.tar.zst)
            tar -I zstd -xf $tarball -C $dstpath
            ;;
 
        *.tar)
            tar -xf $tarball -C $dstpath
            ;;
 
        *.zip)
            unzip -qo $tarball -d $dstpath
            ;;
 
        *)
            pr_error "decompress Unsupport packet: $tarball"
            return 1;
            ;;
    esac
}
 
# parser configure file and export environment variable
function export_env()
{
    export BOARD=`jq -r ".board" $CONF_FILE | tr 'A-Z' 'a-z'`
    export SDK_VER=`jq -r ".android.sdk_ver" $CONF_FILE`
    export SDK_PATH=`jq -r ".android.sdk_path" $CONF_FILE`
 
    export ARCH=arm64
 
    if [[ $BOARD =~ igkboard-rk3568 ]] ; then
        LAUNCH_BOARD=rk3568_t-userdebug
    fi
 
    PYTHON_VERSION=$(python --version 2>&1 | awk '{print $2}' | cut -d. -f1)
    if [ $PYTHON_VERSION != "3" ] ; then
        PYTHON_VERSION=$(python --version 2>&1)
        pr_error "ERROR: This SDK build need Python3, current version is $PYTHON_VERSION"
        pr_warn "You can use 'sudo update-alternatives --config python' command to switch it"
        exit
    fi
}
 
function do_fetch()
{
    SDK_FPATH=$SDK_PATH/$SDK_VER
 
    SDK_FLAG=$SDK_DIR/kernel-5.10/Makefile
 
    cd $PRJ_PATH
 
    if [ -e $SDK_FLAG ] ;  then
        pr_warn "INFO: SDK source code fetched already, skip do fetch"
    fi
 
    if [ ! -d $SDK_DIR/.repo ] ; then
 
        if [ ! -d $SDK_FPATH ] ; then
            pr_error "ERROR: SDK package folder '$SDK_FPATH' doesn't exist!"
            exit
        fi
 
        SDK_FILE=`ls $SDK_FPATH/*.tar.bz2 | head -n 1`
 
        pr_info "decompress SDK tarball $SDK_FILE..."
        do_unpack $SDK_FILE $SDK_DIR
    fi
 
 
    if [ ! -e $SDK_FLAG ] ; then
        pr_info "repo sync to checkout source code..."
        cd $SDK_DIR
        ./.repo/repo/repo sync -l
    fi
}
 
function do_patch()
{
    PATCH_FLAG="$SDK_DIR/kernel-5.10/arch/arm64/configs/rockchip_defconfig"
 
    cd $PRJ_PATH
 
    if grep -q '^CONFIG_CAN=y' $PATCH_FLAG > /dev/null 2>&1 ; then
        pr_warn "INFO: Patch for Android SDK already"
        return ;
    fi
 
    pr_info "do patch for android source code"
    cd $SDK_DIR/kernel-5.10
    patch -p1 < $PRJ_PATH/patches/kernel.patch
    cd -
 
    cd $SDK_DIR/vendor/rockchip/common/
    patch -p1 < $PRJ_PATH/patches/4g_patch/vendor.patch
    cd -
 
    cd $SDK_DIR/device/rockchip/common/
    patch -p1 < $PRJ_PATH/patches/4g_patch/device_rockchip.patch
    cd -
 
    cd $SDK_DIR/device/google/atv/
    patch -p1 < $PRJ_PATH/patches/4g_patch/device_google.patch
    cd -
 
    cd $SDK_DIR/hardware/interfaces/
    patch -p1 < $PRJ_PATH/patches/4g_patch/hardware_interfaces.patch
    cd -
 
    cd $SDK_DIR/hardware/ril/
    patch -p1 < $PRJ_PATH/patches/4g_patch/hardware_ril.patch
    cd -
 
    cd $SDK_DIR/frameworks/opt/telephony/
    patch -p1 < $PRJ_PATH/patches/4g_patch/frameworks_opt_telephony.patch
    cd -
 
    pr_info "copy 4G RIL library..."
 
    cp -f $FILES_PATCH_4G/ip-* $SDK_DIR/vendor/rockchip/common/phone/etc/ppp/
    cp -f $FILES_PATCH_4G/ql-ril.conf $SDK_DIR/vendor/rockchip/common/phone/etc/ppp/
    cp -f $FILES_PATCH_4G/chat $SDK_DIR/vendor/rockchip/common/phone/bin/
    cp -f $FILES_PATCH_4G/libreference-ril.so $SDK_DIR/vendor/rockchip/common/phone/lib/libreference-ril-em05.so
 
    pr_info "copy wifi firmware..."
    cp -f $FILES_PATCH_WIFI/mt7601u.bin $SDK_DIR/vendor/rockchip/common/wifi/firmware
}
 
function do_build()
{
    cd $PRJ_PATH
 
    pr_info "start build $LAUNCH_BOARD..."
 
    cd $SDK_DIR
    source build/envsetup.sh
    lunch $LAUNCH_BOARD
    ./build.sh -AUCKu
 
    pr_info "System image will be installed to $SDK_DIR/rockdev/Image-rk3568_t"
    ls ./rockdev/Image-rk3568_t
}
 
#+-------------------------+
#| Shell script body entry |
#+-------------------------+
 
export_env
 
pr_warn "start build android sdk for ${BOARD}"
 
do_fetch
 
do_patch
 
do_build