wok-tiny view linux/stuff/bundle @ rev 129

linux: fix bundle build
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Aug 17 18:51:03 2017 +0200 (2017-08-17)
parents 93ea2d4c06f3
children c82d2b7a51b4
line source
1 #!/bin/sh
3 ddq()
4 {
5 dd "$@" 2> /dev/null
6 }
8 store()
9 {
10 n=$2; for i in $(seq 8 8 32); do
11 printf '\\\\x%02X' $(($n & 255))
12 n=$(($n >> 8))
13 done | xargs echo -en | ddq bs=1 conv=notrunc of="$3" seek=$(($1))
14 }
16 get()
17 {
18 echo $(od -j $(($1)) -N ${3:-4} -t u${3:-4} -An "$2")
19 }
21 end()
22 {
23 echo $(( $(get 0x1F1 "$1" 1)*32 +($(get 0x1F4 "$1") +31 +${2:-1})&-${2:-1} ))
24 }
26 #install-begin
27 sed '1,/^bundle.bin$/!d' $0 > $0.sh
28 gzip -9 < bundle.bin | uuencode -m - >> $0.sh
29 sed '1,/^bundle.bin$/d' $0 >> $0.sh
30 sed -i -e '/^bundle.bin$/d' -e '/install-begin$/,/install-end$/d' $0.sh
31 chmod +x $0.sh
32 exit
33 #install-end
34 if [ ! -s "$1" -a -z "$4" ]; then
35 p=$(basename "$0")
36 cat <<EOT
37 Usage: $p kernel [initrd] [@]
38 or: $p menufile "menu title" file1 "menu entry 1" ...
39 EOT
40 elif [ -n "$4" ]; then
41 out="$1"
42 uudecode <<EOT | gunzip > "$out"
43 bundle.bin
44 EOT
45 pos=$(get 0x1F2 "$out")
46 [ $(get $(($pos - 3)) "$out") -eq 24937 ] && crc=2 || crc=0
47 echo -en "$2\0" | ddq bs=1 of="$out" seek=$pos conv=notrunc
48 pos=$(($pos+${#2}+1))
49 shift 2
50 free=$((0x1F1 - 2 - $pos - $crc))
51 p=1
52 while [ -n "$2" -a $free -ge ${#2} ]; do
53 s=$((($(stat -c "%s" "$1")+511)/512))
54 x="$(printf '\\x%02x\\x%02x' $(($p % 256)) $(($p / 256)) )"
55 echo -en "$x$2\0" | ddq bs=1 of="$out" seek=$pos conv=notrunc
56 p=$(($p+$s))
57 pos=$(($pos+2+${#2}+1))
58 cat "$1" /dev/zero | ddq bs=512 count=$s >> "$out"
59 if [ $crc -ne 0 ]; then
60 x=$(cat "$1" /dev/zero | ddq bs=512 count=$s | od -v \
61 -t u2 -w2 -An|awk '{i+=$0} END {print (i % 65536)}')
62 x="$(printf '\\x%02x\\x%02x' $(($x % 256)) $(($x / 256)) )"
63 echo -en "$x" | ddq bs=1 of="$out" seek=$pos conv=notrunc
64 pos=$(($pos+2))
65 x="$(printf '\\x%02x\\x%02x' $(($s % 256)) $(($s / 256)) )"
66 echo -en "$x" | ddq bs=1 of="$out" seek=$pos conv=notrunc
67 pos=$(($pos+2))
68 fi
69 shift 2
70 done
71 [ -n "$2" ] && echo "Warning: skip $@" 1>&2 && exit 1
72 elif [ -s "$2" ]; then
73 base_initrd=$((0x00300000))
74 size_initrd=$(stat -c %s "$2")
75 [ $(($base_initrd + $size_initrd)) -gt $((0x1000000)) ] &&
76 base_initrd=$((0x01400000))
77 [ "$3" ] && base_initrd=$(($3))
78 printf "initrd @%X %X\n" $base_initrd $size_initrd
79 store 0x218 $base_initrd "$1"
80 store 0x21C $size_initrd "$1"
81 ddq bs=16 seek=$(end "$1" 32) of="$1" if="$2"
82 ls -l "$1"
83 else
84 if [ $(get 0x1F1 "$1" 1) -eq 0 ]; then
85 menu=$(get 0x1F2 "$1")
86 [ $(get $(($menu -3)) "$1" 2) -eq 24937 ] && skip=4 || skip=0
87 ddq bs=1 skip=$menu count=$((0x1F3 - $menu)) if="$1" | \
88 od -v -t u1 -w1 -An | awk -vx=$skip '{
89 if (--skip >= 0) next
90 if (--bytes < 0) {
91 if ($1 == 0) {
92 if (mul > 0) {
93 if (sector == 0) exit
94 skip=x; print sector " " s
95 }
96 s=""; sector=0; mul=1; bytes=2
97 }
98 else { c=sprintf("%c",$1); s=s c }
99 }
100 else { sector += $0*mul; mul *= 256 }
101 }' | while read s name; do
102 cnt=$(get $(($s*512 + 0x1F4)) "$1")
103 cnt=$(($cnt+32*(1+$(get $(($s*512 + 0x1F1)) "$1" 1))))
104 x=0
105 if [ $(get $(($s*512 + 0x202)) "$1" 2) -eq 25672 ]; then
106 x=$(get $(($s*512 + 0x21C)) "$1")
107 [ $x -ne 0 ] && x=$(((($cnt+31)/32)*512+$x)) &&
108 cnt=$((($x+15)/16))
109 fi
110 ddq bs=16 skip=$((32*$s)) count=$cnt if="$1" of="$name"
111 [ $x -ne 0 ] && ddq bs=1 seek=$x count=0 of="$name"
112 ls -l "$name"
113 done
114 else
115 base_initrd=$(get 0x218 "$1")
116 size_initrd=$(get 0x21C "$1")
117 [ $base_initrd -ne 0 ] &&
118 printf "initrd @%X\n" $base_initrd &&
119 ddq bs=1 skip=$(($(end "$1" 32) * 16)) count=$size_initrd \
120 if="$1" of=initrd && ls -l initrd
121 store 0x218 0 "$1"
122 store 0x21C 0 "$1"
123 ddq bs=16 count=0 seek=$(end "$1") of="$1"
124 ls -l "$1"
125 fi
126 fi
127 true