wok-6.x view plop/stuff/pack @ rev 25506
memtest, pack: cleanup
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Thu Feb 09 16:28:01 2023 +0000 (21 months ago) |
parents | 20c2ee666646 |
children |
line source
1 #!/bin/sh
2 if [ "$1" == "--build" ]; then
3 set -- ${2:-bootloader.bin} ${3:-unpack.bin}
4 x=$(grep -s packed_syssize$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
5 [ -n "$x" ] && sed -i "s/XXXP/$((513+0x$x))/" $0 || sed -i "/XXXP$/d" $0
6 x=$(grep -s setup_byte$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
7 [ -n "$x" ] && sed -i "s/XXXSB/$((513+0x$x))/" $0 || sed -i "/XXXSB/d" $0
8 x=$(grep -s setup_word$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
9 [ -n "$x" ] && sed -i "s/XXXSW/$((514+0x$x))/" $0 || sed -i "/XXXSW/d" $0
10 x=$(grep -s setup_para$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
11 [ -n "$x" ] && sed -i "s/XXXSP/$((514+0x$x))/" $0 || sed -i "/XXXSP/d" $0
12 x=$(grep -s setup_seg$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
13 [ -n "$x" ] && sed -i "s/XXXSS/$((513+0x$x))/" $0 || sed -i "/XXXSS/d" $0
14 x=$(grep -s setup_keep$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
15 if [ -n "$x" ]; then
16 sed -i "s/XXXK2 //" $0
17 else
18 x=$(grep -s setup_syssize$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
19 if [ -n "$x" ]; then
20 sed -i "s/XXXK2/bs=500 skip=1/" $0
21 else
22 sed -i "s/XXXK2/bs=512 skip=1/" $0
23 fi
24 fi
25 x=$(grep -s double_packed$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
26 [ -n "$x" ] && sed -i "s/| compress /&& 2 /" $0
27 x=$(grep -s update_code32_start$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
28 [ -n "$x" ] && sed -i '/XXXU2/d; s| XXXU1||' $0 || sed -i '/XXXU1/d; s| XXXU2||' $0
29 x=$(grep -s lzsa.block$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
30 [ -n "$x" ] && sed -i 's| XXXR||' $0 || sed -i 's| XXXR| -r|;s|tail -c +4|cat|' $0
31 helpmsg=$(grep -s helpmsg$ ${1/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
32 helpend=$(grep -s helpend$ ${1/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
33 if [ -n "$helpmsg" ]; then
34 sed -i "s/YYY/$((0x$helpmsg))/" $0
35 sed -i "s/ZZZ/$((0x$helpend-0x$helpmsg))/" $0
36 else
37 sed -i '/YYY/d' $0
38 fi
39 cat >> $0 <<EOM
40 $(dd if=$1 bs=512 count=1 conv=sync 2> /dev/null | cat - $2 | gzip -9 | uuencode -m -)
41 EOT
42 EOM
43 sed -i '/--build/,/^fi/d' $0
44 exit
45 fi
47 store()
48 {
49 n=$1
50 for i in $(seq 1 ${4:-2}); do
51 printf '\\\\x%02X' $(($n & 255))
52 n=$(($n >> 8))
53 done | xargs echo -en | dd conv=notrunc bs=1 of=$2 seek=$3
54 }
56 bits()
57 {
58 sed "/^#define PROP_$1/!d;s|.*PROP_...||" unlzma.S
59 }
61 compress()
62 {
63 case "$PACKER" in
64 lz4) lz4 -l --best | tail -c +5 ;;
65 lzsa1) cat > /tmp/in$$$1 ; lzsa XXXR -f1 /tmp/in$$$1 /tmp/out$$$1 ; tail -c +4 /tmp/out$$$1 ; rm -f /tmp/in$$$1 /tmp/out$$$1 ;;
66 lzsa2) cat > /tmp/in$$$1 ; lzsa XXXR -f2 /tmp/in$$$1 /tmp/out$$$1 ; tail -c +4 /tmp/out$$$1 ; rm -f /tmp/in$$$1 /tmp/out$$$1 ;;
67 *) xz -z -e --format=raw --lzma1=lc=$(bits LC),lp=$(bits LP),pb=$(bits PB) --stdout
68 esac
69 }
71 main()
72 {
73 uudecode | gunzip >$2
74 dd if=$1 bs=1 skip=497 seek=497 count=15 of=$2 conv=notrunc
75 setup="$(echo $(od -j 497 -N 1 -dAn $1))"
76 [ $setup -eq 0 ] && setup=4
77 store $setup $2 497 1
78 setup=$((setup+1))
79 store $setup $2 XXXSB 1
80 store $setup $2 XXXSW 1
81 store $((32*setup+0xF000)) $2 XXXSP
82 store $((0x1000 - 32*setup)) $2 XXXSS
83 cp $1 /tmp/un$$ XXXU1
84 store 1 /tmp/un$$ 534 1 XXXU1
85 dd if=/tmp/un$$ XXXK2 | compress >>$2 XXXU1
86 rm -f /tmp/un$$ XXXU1
87 dd if=$1 XXXK2 | compress >>$2 XXXU2
88 size=$(stat -c %s $2)
89 syssize=$(((size-512*setup+15)/16))
90 store $((syssize + 0x1000 + 0x1000)) $2 XXXP
91 store $syssize $2 500
92 store $(((size+511)/512)) $2 4
93 store $(((size+15)/16)) $2 14
94 echo -e "$3" | dd conv=notrunc bs=1 seek=YYY of=$2 count=ZZZ
95 }
97 HELP="$(unix2dos <<EOT
98 Plop Boot Manager VERSION
99 EOT
100 )"
101 main $1 ${2:-$1.packed} "${3:-$HELP}" 2>/dev/null <<EOT