b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | set -e |
| 3 | |
| 4 | if [ $# -eq 0 -o $# -eq 1 -a "$1" = help ]; then |
| 5 | echo "Usage: $0 <add | del | list | help> [arguments...]" |
| 6 | echo " run $0 help <command> to show help for <command>" |
| 7 | exit 0 |
| 8 | fi |
| 9 | |
| 10 | HELP=false |
| 11 | if [ "$1" = help ]; then |
| 12 | HELP=true |
| 13 | shift |
| 14 | fi |
| 15 | |
| 16 | die () { |
| 17 | if [ $# -gt 0 ]; then |
| 18 | echo "$@" |
| 19 | fi |
| 20 | exit 1 |
| 21 | } |
| 22 | |
| 23 | row() { |
| 24 | FMT="%s" |
| 25 | for c in `seq 2 $#`; do |
| 26 | FMT="%-15s $FMT" |
| 27 | done |
| 28 | printf "$FMT\n" "$@" |
| 29 | } |
| 30 | |
| 31 | MMP_DIR=target/linux/mmp |
| 32 | |
| 33 | case "$1" in |
| 34 | add) |
| 35 | RET=0 |
| 36 | if [ $# -lt 5 ]; then |
| 37 | echo "Insufficient arguments" |
| 38 | RET=1 |
| 39 | HELP=true |
| 40 | fi |
| 41 | if $HELP; then |
| 42 | echo "$0 add <template_profile> <chip> <name> <code> [kernel_config_suffix]" |
| 43 | echo " Add a new profile named <chip>p<code> based on template config file <template_profile> and specified params" |
| 44 | echo " Model name would be <name><code>, BLF_PREFIX and FOTA_PREFIX would be p<code>" |
| 45 | echo "Sample usage: $0 add asr1803p401 asr1802s NEZAS 201" |
| 46 | exit $RET |
| 47 | fi |
| 48 | TMPL="config/defconfig_$2" |
| 49 | [ -s "$TMPL" ] || die "Invalid template file $TMPL" |
| 50 | TMPL_CHIP="$(sed 's/^CONFIG_TARGET_mmp_\([^_]*\)=y$/\1/;t;d' "$TMPL")" |
| 51 | [ -n "$TMPL_CHIP" ] || die "No chip found in $TMPL" |
| 52 | TMPL_MODL="$(sed "s/^CONFIG_TARGET_mmp_${TMPL_CHIP}_\\([^_]*\)=y$/\\1/;t;d" "$TMPL")" |
| 53 | [ -n "$TMPL_MODL" ] || die "No model found in $TMPL" |
| 54 | TMPL_NAME="$(echo "$TMPL_MODL" | sed 's/[0-9].*$//')" |
| 55 | TMPL_NAME_L="$(echo "$TMPL_NAME" | tr A-Z a-z)" |
| 56 | TMPL_CODE="$(echo "$TMPL_MODL" | sed 's/^[^0-9]*//')" |
| 57 | TMPL_CONF=CONFIG_TARGET_mmp_${TMPL_CHIP}_${TMPL_MODL} |
| 58 | TMPL_KCFG="$(echo $MMP_DIR/$TMPL_CHIP/config-* | sed 's/ .*//')" |
| 59 | [ -s "$TMPL_KCFG" ] || die "No kernel config found for $TMPL_CHIP" |
| 60 | TMPL_OEMF=$MMP_DIR/$TMPL_CHIP/oem_fs |
| 61 | [ -d "$TMPL_OEMF" ] || die "No oem_fs found for $TMPL_CHIP" |
| 62 | CHIP=$3 |
| 63 | [ -n "$CHIP" ] || die "Empty chip" |
| 64 | NAME=$4 |
| 65 | [ -n "$NAME" ] || die "Empty name" |
| 66 | NAME_L="$(echo "$NAME" | tr A-Z a-z)" |
| 67 | CODE=$5 |
| 68 | [ -n "$CODE" ] || die "Empty code" |
| 69 | CONF=CONFIG_TARGET_mmp_${CHIP}_$NAME$CODE |
| 70 | KCONF=$6 |
| 71 | TGTF="config/defconfig_${CHIP}p$CODE" |
| 72 | [ ! -e "$TGTF" ] || die "$TGTF already exists" |
| 73 | KCFG=$MMP_DIR/$CHIP/`basename $TMPL_KCFG` |
| 74 | OEMF=$MMP_DIR/$CHIP/oem_fs |
| 75 | TGTM=$MMP_DIR/$CHIP/target.mk |
| 76 | CPFX="$(echo $CHIP | sed 's/[0-9].*//')" |
| 77 | CSFX="$(echo $CHIP | sed 's/^[^0-9]*//')" |
| 78 | [ "$CPFX$CSFX" = "$CHIP" ] || die "Unexpected $CHIP" |
| 79 | CPFX_U="$(echo $CPFX | tr a-z A-Z)" |
| 80 | TMPL_CPFX="$(echo $TMPL_CHIP | sed 's/[0-9].*//')" |
| 81 | TMPL_CSFX="$(echo $TMPL_CHIP | sed 's/^[^0-9]*//')" |
| 82 | [ "$TMPL_CPFX$TMPL_CSFX" = "$TMPL_CHIP" ] || die "Unexpected $TMPL_CHIP" |
| 83 | TMPL_CPFX_U="$(echo $TMPL_CPFX | tr a-z A-Z)" |
| 84 | set -x |
| 85 | set -e |
| 86 | mkdir -p $MMP_DIR/$CHIP $MMP_DIR/$CHIP/profiles |
| 87 | [ -s $KCFG ] || cp $TMPL_KCFG $KCFG |
| 88 | [ -d $OEMF ] || cp -a $TMPL_OEMF $OEMF |
| 89 | if [ ! -s $TGTM ] || [ `grep -Fc $CHIP $TGTM` -ne 3 ]; then |
| 90 | cp $MMP_DIR/$TMPL_CHIP/target.mk $TGTM |
| 91 | sed -i "s/$TMPL_CHIP/$CHIP/g;s/$TMPL_CPFX_U/$CPFX_U/g;s/$TMPL_CSFX/$CSFX/g" $TGTM |
| 92 | fi |
| 93 | if ! grep -Fq 'define Model' $MMP_DIR/$CHIP/profiles/*.mk 2>/dev/null; then |
| 94 | PROF=$MMP_DIR/$CHIP/profiles/$CPFX.mk |
| 95 | cp $MMP_DIR/$TMPL_CHIP/profiles/*.mk $PROF |
| 96 | sed -i '/^define Profile/,/\$(eval \$(call Model,/d' $PROF |
| 97 | else |
| 98 | PROF=`grep -Fl 'define Model' $MMP_DIR/$CHIP/profiles/*.mk | head -1` |
| 99 | fi |
| 100 | sed -i ":n;/^$/{N;s/^\\n$//;t n;};/^\n*define Profile\/$NAME$CODE$/,/^endef/d;/,$NAME$CODE[,)]/d" $PROF |
| 101 | cat >> $PROF << EOF |
| 102 | |
| 103 | define Profile/$NAME$CODE |
| 104 | NAME:=$CPFX_U `echo $NAME | awk '{printf("%c%s\n", substr($0,1,1), tolower(substr($0,2)))}'` Profile $CODE |
| 105 | endef |
| 106 | \$(eval \$(call Profile,$NAME$CODE)) |
| 107 | \$(eval \$(call Model,$NAME$CODE,${CHIP}p$CODE)) |
| 108 | EOF |
| 109 | [ -d $MMP_DIR/$TMPL_CHIP/$TMPL_MODL ] && cp -a $MMP_DIR/$TMPL_CHIP/$TMPL_MODL $MMP_DIR/$CHIP/$NAME$CODE |
| 110 | SUBT="$(sed 's/^\s*\<SUBTARGETS\>[^=]*=\s*//;t n;d;:n;s/ \+$//;s/ \+/\n/g' $MMP_DIR/Makefile)" |
| 111 | if ! echo "$SUBT" | grep -Fx $CHIP; then |
| 112 | sed -i "/^\\s*\\<SUBTARGETS\\>[^=]*=/{s/ *$/ $CHIP/}" $MMP_DIR/Makefile |
| 113 | fi |
| 114 | TMPL_RULE="$(awk "/^ifeq \\(\\\$\\($TMPL_CONF\\),y\\)/{++x;print;next};x{print;if(\$1~/^if/)++x;else if(\$1==\"endif\")--x}" rules.mk)" |
| 115 | if [ -n "$TMPL_RULE" ]; then |
| 116 | awk "/^ifeq \\(\\\$\\(CONFIG_TARGET_mmp_/{++x;if(/$CONF\\),y\\)/)y=NR};!y{print};x{if(\$1~/^if/&&!/CONFIG_TARGET_mmp_/)++x;else if(\$1==\"endif\"){--x;if(!x){getline;if(/^ *$/)n=NR;else n=NR-1;if(y){m=n-y+1;y=0;if(n<NR)print}else print}}};END{print n-m >\"rules.mk.nr\"}" rules.mk > rules.mk.tmp |
| 117 | #diff -u rules.mk rules.mk.tmp || true |
| 118 | LNUM="$(cat rules.mk.nr)" |
| 119 | head -$LNUM rules.mk.tmp > rules.mk |
| 120 | echo "$TMPL_RULE" | sed "s/$TMPL_CHIP/$CHIP/g;s/$TMPL_MODL/$NAME$CODE/g;s/$TMPL_CODE/$CODE/g" >> rules.mk |
| 121 | echo >> rules.mk |
| 122 | tail -n +`expr $LNUM + 1` rules.mk.tmp >> rules.mk |
| 123 | #diff -u rules.mk.tmp rules.mk || true |
| 124 | rm rules.mk.tmp rules.mk.nr |
| 125 | else |
| 126 | echo "Warning: rules.mk not updated" |
| 127 | fi |
| 128 | TMPL_IMG_MKFL="$(awk "/^ifeq \\(\\\$\\(CONFIG_TARGET_mmp_$TMPL_CHIP\\),y\\)/{++x;print;next};x{print;if(\$1~/^if/)++x;else if(\$1==\"endif\")--x}" $MMP_DIR/image/Makefile)" |
| 129 | if [ -n "$TMPL_IMG_MKFL" ]; then |
| 130 | awk "/^ifeq \\(\\\$\\(CONFIG_TARGET_mmp_/{++x;if(/CONFIG_TARGET_mmp_$CHIP\\),y\\)/)y=NR};!y{print};x{if(\$1~/^if/&&!/CONFIG_TARGET_mmp_/)++x;else if(\$1==\"endif\"){--x;if(!x){getline;if(/^ *$/)n=NR;else n=NR-1;if(y){m=n-y+1;y=0;if(n<NR)print}else print}}};END{print n-m >\"image.mk.nr\"}" $MMP_DIR/image/Makefile > image.mk.tmp |
| 131 | #diff -u $MMP_DIR/image/Makefile image.mk.tmp || true |
| 132 | LNUM="$(cat image.mk.nr)" |
| 133 | head -$LNUM image.mk.tmp > $MMP_DIR/image/Makefile |
| 134 | echo "$TMPL_IMG_MKFL" | sed "s/$TMPL_CHIP/$CHIP/g;s/$TMPL_MODL/$NAME$CODE/g;s/$TMPL_NAME/$NAME/g;s/$TMPL_CODE/$CODE/g" >> $MMP_DIR/image/Makefile |
| 135 | echo >> $MMP_DIR/image/Makefile |
| 136 | tail -n +`expr $LNUM + 1` image.mk.tmp >> $MMP_DIR/image/Makefile |
| 137 | #diff -u image.mk.tmp $MMP_DIR/image/Makefile || true |
| 138 | rm image.mk.tmp image.mk.nr |
| 139 | else |
| 140 | echo "Warning: image/Makefile not updated" |
| 141 | fi |
| 142 | cp $TMPL $TGTF |
| 143 | if [ $TMPL_CHIP = $CHIP ]; then |
| 144 | sed -i "s/$NAME$CODE/$TMPL_MODL/g;t;s/$TMPL_MODL/$NAME$CODE/g;s/p$TMPL_CODE/p$CODE/g" $TGTF |
| 145 | else |
| 146 | sed -i "s/$CHIP/$TMPL_CHIP/g;t;s/$TMPL_CHIP/$CHIP/g;s/$TMPL_MODL/$NAME$CODE/g;s/$TMPL_NAME_L/$NAME_L/g;s/p$TMPL_CODE/p$CODE/g" $TGTF |
| 147 | fi |
| 148 | #diff -u $TMPL $TGTF || true |
| 149 | make prepare-tmpinfo |
| 150 | ./scripts/config/conf --defconfig=$TGTF -w $TGTF.new Config.in |
| 151 | #diff -u $TGTF $TGTF.new || true |
| 152 | mv $TGTF.new $TGTF |
| 153 | set +x |
| 154 | sh $0 list | grep -F ${CHIP}p$CODE |
| 155 | ;; |
| 156 | list) |
| 157 | if $HELP; then |
| 158 | echo "$0 list" |
| 159 | echo " show the list of defined profiles, as well as the" |
| 160 | echo " list of unused target configurations in uboot /obm" |
| 161 | exit 0 |
| 162 | fi |
| 163 | [ -d "$MMP_DIR" ] || die "Please run under openwrt directory" |
| 164 | PROFILES= |
| 165 | MODELS= |
| 166 | for c in $MMP_DIR/*/profiles/*.mk; do |
| 167 | [ -s "$c" ] || die "Invalid file $c" |
| 168 | CHIP_DIR="$(echo "$c" | sed "s=^$MMP_DIR/==;s=/profiles.*==")" |
| 169 | echo "$CHIP_DIR" | grep -Fvq / || die "Slash in $CHIP_DIR" |
| 170 | for d in `sed "s/^.*(call Profile,//;t n;d;:n;s/).*//" "$c"`; do |
| 171 | MODELS="$MODELS $d" |
| 172 | p="$(sed "s/^.*(call Model,$d,//;t n;d;:n;s/).*//" "$c")" |
| 173 | [ -n "$p" ] || die "Missing model $d" |
| 174 | PROFILES="$PROFILES $p" |
| 175 | eval "PROFILE_$d=$p" |
| 176 | eval "MODEL_$p=$d" |
| 177 | eval "CHIP_$p=$CHIP_DIR" |
| 178 | done |
| 179 | done |
| 180 | UBOOT_CONF_LIST="$(sed -n '/^define uboot\//,/^endef$/{s/^ *DEPS:=@//;t n;d;:n;s/[()]//g;s/||/\n/g;s/ //g;p}' package/boot/uboot-mmp/Makefile | sed 's/^/CONFIG_/')" |
| 181 | OBM_CONF_LIST="$(sed -n '/^define obm\//,/^endef$/{s/^ *DEPS:=@//;t n;d;:n;s/[()]//g;s/||/\n/g;s/ //g;p}' package/boot/obm-mmp/Makefile | sed 's/^/CONFIG_/')" |
| 182 | for p in $PROFILES; do |
| 183 | eval "CONF=CONFIG_TARGET_mmp_\${CHIP_$p}_\${MODEL_$p}" |
| 184 | CFG="$(grep -Fxl $CONF=y config/* | sed 's=^.*/defconfig_==')" |
| 185 | if [ -z "$CFG" ]; then |
| 186 | echo "Warning: profile $p has no config" |
| 187 | elif [ "$CFG" != $p ]; then |
| 188 | CFG="$(echo -n "$CFG" | tr '\n' ',')" |
| 189 | echo "Warning: profile $p has config: $CFG" |
| 190 | echo "$CFG" | grep -q "^$p," || echo " without $p" |
| 191 | fi |
| 192 | eval "CONFIG_$p=\"$CFG\"" |
| 193 | if [ -z "$CFG" ]; then |
| 194 | VAL=XXX |
| 195 | else |
| 196 | if [ "$CFG" != $p ]; then |
| 197 | CFG="$(echo "$CFG" | sed 's/,.*//')" |
| 198 | fi |
| 199 | if eval grep -Fxq CONFIG_TARGET_mmp_\${CHIP_$p}=y config/defconfig_$CFG; then |
| 200 | VAL=OO |
| 201 | else |
| 202 | VAL=XO |
| 203 | fi |
| 204 | if eval grep -Fxq CONFIG_TARGET_ARCH_PACKAGES=\\\"\${CHIP_$p}\\\" config/defconfig_$CFG; then |
| 205 | VAL=${VAL}O |
| 206 | else |
| 207 | VAL=${VAL}X |
| 208 | fi |
| 209 | fi |
| 210 | if true; then |
| 211 | if grep -Fxq "ifeq (\$($CONF),y)" rules.mk; then |
| 212 | VAL=${VAL}O |
| 213 | c="$(sed -n "/^ifeq (\\\$($CONF),y)$/,/^endif$/{s/.* ARCH_PROFILE.*= *//;t n;d;:n;p}" rules.mk)" |
| 214 | if [ "$c" = "$p" ]; then |
| 215 | VAL=${VAL}O |
| 216 | else |
| 217 | VAL=${VAL}X |
| 218 | fi |
| 219 | c="$(sed -n "/^ifeq (\\\$($CONF),y)$/,/^endif$/{s/.* BLF_PREFIX.*= *//;t n;d;:n;p}" rules.mk)" |
| 220 | eval "BLF_$p=\"$c\"" |
| 221 | c="$(sed -n "/^ifeq (\\\$($CONF),y)$/,/^endif$/{s/.* FOTA_PREFIX.*= *//;t n;d;:n;p}" rules.mk)" |
| 222 | eval "FOTA_$p=\"$c\"" |
| 223 | c="$(sed -n "/^ifeq (\\\$($CONF),y)$/,/^endif$/{s/.* KERNEL_CONFIG_FILE_NAME_APPENDIX.*= *//;t n;d;:n;p}" rules.mk)" |
| 224 | eval "KCONF_$p=\"$c\"" |
| 225 | else |
| 226 | VAL=${VAL}XX |
| 227 | fi |
| 228 | fi |
| 229 | if echo "$UBOOT_CONF_LIST" | grep -Fxq "$CONF"; then |
| 230 | VAL=${VAL}O |
| 231 | else |
| 232 | VAL=${VAL}X |
| 233 | fi |
| 234 | if echo "$OBM_CONF_LIST" | grep -Fxq "$CONF"; then |
| 235 | VAL=${VAL}O |
| 236 | else |
| 237 | VAL=${VAL}X |
| 238 | fi |
| 239 | eval "VALID_$p=$VAL" |
| 240 | done |
| 241 | row Profile Chip Model BLF/FOTA/Kconf Config "Validity(O/X)" |
| 242 | for p in $PROFILES; do |
| 243 | eval row \$p \${CHIP_$p} \${MODEL_$p} \"\${BLF_$p}/\${FOTA_$p}/\${KCONF_$p}\" \"\${CONFIG_$p}\" \${VALID_$p} |
| 244 | done |
| 245 | for c in `echo "$UBOOT_CONF_LIST |
| 246 | $OBM_CONF_LIST" | sort | uniq | sed 's/^\s*CONFIG_TARGET_mmp_//'`; do |
| 247 | d="$(echo "$c" | sed 's/^[^_]*_//')" |
| 248 | if [ -n "$(eval echo "\$PROFILE_$d")" ]; then |
| 249 | continue |
| 250 | fi |
| 251 | CHIP_DIR="$(echo "$c" | sed 's/_.*//')" |
| 252 | CONF="CONFIG_TARGET_mmp_$c" |
| 253 | CFG="$(grep -Fxl $CONF=y config/* | sed 's=^.*/defconfig_==')" |
| 254 | if [ -z "$CFG" ]; then |
| 255 | VAL=XXX |
| 256 | else |
| 257 | CFG="$(echo "$CFG" | head -1)" |
| 258 | if grep -Fxq CONFIG_TARGET_mmp_$CHIP_DIR=y config/defconfig_$CFG; then |
| 259 | VAL=OO |
| 260 | else |
| 261 | VAL=XO |
| 262 | fi |
| 263 | if grep -Fxq "CONFIG_TARGET_ARCH_PACKAGES=\"$CHIP_DIR\"" config/defconfig_$CFG; then |
| 264 | VAL=${VAL}O |
| 265 | else |
| 266 | VAL=${VAL}X |
| 267 | fi |
| 268 | fi |
| 269 | if true; then |
| 270 | if grep -Fxq "ifeq (\$($CONF),y)" rules.mk; then |
| 271 | VAL=${VAL}O |
| 272 | c="$(sed -n "/^ifeq (\\\$($CONF),y)$/,/^endif$/{s/.* ARCH_PROFILE.*= *//;t n;d;:n;p}" rules.mk)" |
| 273 | if [ "$c" = "$CFG" ]; then |
| 274 | VAL=${VAL}O |
| 275 | else |
| 276 | VAL=${VAL}X |
| 277 | fi |
| 278 | c="$(sed -n "/^ifeq (\\\$($CONF),y)$/,/^endif$/{s/.* BLF_PREFIX.*= *//;t n;d;:n;p}" rules.mk)" |
| 279 | BLF="$c" |
| 280 | c="$(sed -n "/^ifeq (\\\$($CONF),y)$/,/^endif$/{s/.* FOTA_PREFIX.*= *//;t n;d;:n;p}" rules.mk)" |
| 281 | FOTA="$c" |
| 282 | c="$(sed -n "/^ifeq (\\\$($CONF),y)$/,/^endif$/{s/.* KERNEL_CONFIG_FILE_NAME_APPENDIX.*= *//;t n;d;:n;p}" rules.mk)" |
| 283 | KCONF="$c" |
| 284 | else |
| 285 | VAL=${VAL}XX |
| 286 | BLF= |
| 287 | FOTA= |
| 288 | KCONF= |
| 289 | fi |
| 290 | fi |
| 291 | if echo "$UBOOT_CONF_LIST" | grep -Fxq "$CONF"; then |
| 292 | VAL=${VAL}O |
| 293 | else |
| 294 | VAL=${VAL}X |
| 295 | fi |
| 296 | if echo "$OBM_CONF_LIST" | grep -Fxq "$CONF"; then |
| 297 | VAL=${VAL}O |
| 298 | else |
| 299 | VAL=${VAL}X |
| 300 | fi |
| 301 | row "" "$CHIP_DIR" "$d" "$BLF/$FOTA/$KCONF" "$CFG" "$VAL" |
| 302 | done |
| 303 | ;; |
| 304 | |
| 305 | *) |
| 306 | die "Unknown command $1" |
| 307 | ;; |
| 308 | esac |