wok-next view syslinux/stuff/iso2exe/init @ rev 17562
syslinux/iso2exe: use iso size to compute md5sum
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Sat Feb 07 15:39:46 2015 +0100 (2015-02-07) |
parents | 26654c1a7ca7 |
children | 20fc9712d9c6 |
line source
1 #!/bin/sh
3 DIALOG=dialog
5 ddq()
6 {
7 dd $@ 2> /dev/null
8 }
10 get()
11 {
12 od -v -j $1 -N ${4:-${3:-2}} -t u${3:-2} -w${3:-2} -An $2 2>/dev/null ||
13 hexdump -v -s $1 -n ${4:-${3:-2}} -e "\"\" 1/${3:-2} \" %d\n\"" $2
14 }
16 getarg()
17 {
18 sed "/$1=/!d;s/.*$1=\\([^ ]*\\).*/\\1/" /proc/cmdline
19 }
21 clear()
22 {
23 echo -e "\x1B[1;1H\x1B[J"
24 }
26 tinydialog()
27 {
28 clear
29 label=""
30 while [ "$1" ]; do
31 case "$1" in
32 --title) title=" \x1B[30;47m$2\x1B[37;40m\n"
33 echo -e $title; shift ;;
34 --yes-label) label="$2" ; shift ;;
35 --textbox)
36 less "$2" < /dev/tty0 > /dev/tty0
37 break;;
38 --gauge)
39 t=" "
40 echo -e "$t$2\n"
41 while read pct ; do
42 s=" "
43 s="$s$pct%$s"
44 pct=$((($pct*63)/100))
45 echo -en "\r$t\x1B[30;47m$(echo "$s" | cut -c-$pct)\x1B[37;40m$(echo "$s" | cut -c$(($pct+1))-)"
46 done
47 break;;
48 --yesno)
49 while true; do
50 clear
51 echo "$2" | sed 's/\\n\\n/\\n/g;s/\\n/\n/g'
52 echo -en " <- 1:${label:-Yes} 2:Cancel\r"
53 read x
54 case "$x" in
55 ''|Y*|y*|1) return 0;;
56 N*|n*|2|0) return 1;;
57 esac
58 done ;;
59 --menu|--radiolist)
60 [ "$1" == "--menu" ] && shft=2 || shft=3
61 label=""
62 [ "$2" ] && label="\n$2"
63 shift 5
64 echo -e "$title$label\n0 Cancel" > /tmp/data
65 n=1
66 while [ "$1" ]; do
67 eval key_$n='$1'
68 echo "$((n++)) $2"
69 shift $shft
70 done >> /tmp/data
71 while ! grep -q "^$n " /tmp/data ; do
72 clear
73 [ $(wc -l < /tmp/data) -gt 22 ] &&
74 sed 's/..3.;4.m/===/g;$aPress q to continue' \
75 /tmp/data | less || cat /tmp/data
76 echo -en "\n <- Enter the selection number\r"
77 read n
78 done 2> /dev/null
79 rm -f /tmp/data
80 [ $n -eq 0 ] && return 1
81 eval echo -n \$key_$n 1>&2
82 return 0;;
83 esac
84 shift
85 done
86 }
88 mount_proc()
89 {
90 mount -t proc /proc /proc
91 mount -t sysfs /sys /sys
92 udevd --daemon 2> /dev/null && udevadm trigger && sleep 5
93 }
95 umount_proc()
96 {
97 killall udevd 2> /dev/null
98 umount /sys/fs/fuse/connections 2> /dev/null
99 umount /sys
100 umount /proc
101 }
103 bytes2bin()
104 {
105 for i in $@ ; do
106 printf '\\\\x%02X' $(($i&255)) | xargs echo -en
107 done
108 }
110 words2bin()
111 {
112 for i in $@ ; do
113 printf '\\\\x%02X\\\\x%02X' $(($i&255)) $((($i>>8)&255)) | \
114 xargs echo -en
115 done
116 }
118 gettazboot()
119 {
120 echo "Creating $(basename $1) ..."
121 O=$(($(get 66 /mnt/$ISO) - 0xC0))
122 L=$(($(get 20 /mnt/$ISO) - 0xC0 - $(get 24 /mnt/$ISO) - $O))
123 S=$((32+$L))
124 P=$((($S+511)/512))
125 E=$((4096-(32*$P)))
126 words2bin 0x5A4D $(($S%512)) $P 0 2 $E -1 $((${2:-0}-16)) \
127 -2 0 256 -16 28 0x6C53 0x5469 0x7A61 > $1
128 ddq bs=1 count=$L skip=$(echo $O) if=/mnt/$ISO >> $1
129 }
131 checkmagic()
132 {
133 [ -s $1 ] && [ $(getarg magic) == $(get 24 $1 4) ]
134 }
136 getiso()
137 {
138 mkdir -p /media/cdrom
139 for dev in /sys/block/?d?/?d??* ; do
140 mount /dev/$(basename $dev) /mnt
141 if checkmagic /mnt/$ISO; then
142 mount -o loop,ro /mnt/$ISO /media/cdrom
143 echo "Found $ISO on $(basename $dev)"
144 break
145 fi
146 umount /mnt
147 done
148 }
150 uncpio()
151 {
152 [ -s "$1" ] || return
153 echo -en "\n Extracting $(basename $1) ..."
154 case $(get 0 $1) in
155 *35615) ( zcat || gunzip ) ;;
156 *14333) unxz ;;
157 *\ 93) unlzma ;;
158 *) cat ;;
159 esac < $1 | ( cd ${2:-/} ; cpio -idmu > /dev/null 2>&1 )
160 }
162 dotwait()
163 {
164 echo -n "${1:-Install filesystem}.."
165 touch /tmp/wait
166 while [ -e /tmp/wait ]; do
167 echo -n "." > /dev/tty0
168 sleep 1
169 done &
170 }
172 getuuid()
173 {
174 dev=$(mount | sed '/ \/mnt /!d;s/ .*//;s|/dev/||;q')
175 blkid | sed "/$dev:/!d;s/.* UUID=.\\([^ ]*\\)\".*/\\1/"
176 }
178 tazusbinitfs()
179 {
180 PAD=$(($(stat -c %s $1) % 4))
181 [ $PAD -ne 0 ] && ddq if=/dev/zero bs=1 count=$((4 - $PAD)) >> $1
182 mkdir -p /tmp/fs/etc /tmp/fs/lib /tmp/fs/home
183 cp -a /etc/locale.conf /tmp/fs/etc 2> /dev/null
184 cp -a /etc/keymap.conf /tmp/fs/etc 2> /dev/null
185 sed 's/ .*//' /proc/modules | while read mod ; do
186 find /lib/modules/ | grep $mod.ko | \
187 sed 's|.*|cp & /tmp/fs/lib|' | sh
188 done
189 cat > /tmp/fs/init1 <<EOT
190 #!/bin/sh
191 sed -i 's|sbin/init|init2|' /init
192 exec /init
193 EOT
194 cat > /tmp/fs/init2 <<EOT
195 #!/bin/sh
197 mount -t proc /proc /proc
198 grep -q debug /proc/cmdline && debug=true && set -x
199 for i in /lib/*.ko* ; do insmod \$i 2> /dev/null ; done; sleep 2
200 v=\$(sed '/\\/home=/!d;s|.*/home=\\([^ ]*\\).*|\\1|' /proc/cmdline /cmdline 2> /dev/null)
201 mount / -o remount,rw
202 mkdir /mnt/dos
203 rm -f /cmdline 2> /dev/null
204 mount / -o remount,ro
205 mnt=/mnt/dos/\${v#*/}
206 dev=\$( (blkid /dev/[sh]d* || blkid) | grep \${v%%/*} | sed 's/:.*//;q')
207 echo "Mount \$dev in /mnt/dos for \$v..." | tee -a /run/boot.log
208 mount \$dev /mnt/dos
209 mount.posixovl -F \$mnt -- -oallow_other -odefault_permissions -osuid
210 mount --bind \$mnt /home
211 [ "\$debug" = "true" ] && sh
212 umount /proc
213 exec /sbin/init
214 EOT
215 chmod 755 /tmp/fs/init?
216 cp -a /tmp/fs/* /
217 ( cd /tmp/fs ; find * | cpio -o -H newc ) | gzip -9 >> $1
218 }
220 mkinitrd()
221 {
222 dotwait "Creating $(basename $1) "
223 for i in bin lib dev proc tmp mnt etc ; do
224 mkdir -p /tmp/fs/$i
225 done
226 for i in /dev/console /dev/null /dev/tty /dev/tty0 /dev/tty1 /dev/tty2 \
227 /dev/fuse /dev/hd* /dev/sd* ; do
228 cp -a $i /tmp/fs$i
229 done
230 for i in /bin/busybox $(which mount.posixovl) $(which blkid); do
231 cp $(LD_TRACE_LOADED_OBJECTS=1 /lib/ld*.so $i | \
232 sed 's|.*=> \(.*/lib/l[^ ]*\).*|\1|;/^\//!d') /tmp/fs/lib
233 cp $i /tmp/fs/bin
234 done
235 cp -a /lib/ld-* /tmp/fs/lib
236 for i in $(busybox | sed '/Current/,$!d'); do
237 [ -e /tmp/fs/bin/${i%,} ] || ln -s busybox /tmp/fs/bin/${i%,}
238 done
239 ln -s /proc/mounts /tmp/fs/etc/mtab
240 sed 's/ .*//' /proc/modules | while read mod ; do
241 find /lib/modules/ | grep $mod.ko | \
242 sed 's|.*|cp & /tmp/fs/lib|' | sh
243 done
244 cat > /tmp/fs/init <<EOT
245 #!/bin/sh
247 arg()
248 {
249 grep -q \$1 /proc/cmdline &&
250 val="\$(sed "s/.*\$1=\\([^ ]*\\).*/\\1/" < /proc/cmdline)" &&
251 echo "\$2 \$val"
252 }
254 mount -t proc /proc /proc
255 arg debug "shell" && debug=true && set -x
256 for i in /lib/*.ko* ; do insmod \$i 2> /dev/null ; done; sleep 2
257 arg mount "Mount device"
258 mount \$( (blkid /dev/[sh]d* || blkid) | grep \$val | sed 's/:.*//;q') /mnt
259 arg subroot "Change root to directory"
260 mount.posixovl -F /mnt/\$val -- -oallow_other -odefault_permissions -osuid
261 mount --bind /mnt /mnt/\$val/mnt/dos
262 mount -o size=0 -t tmpfs tmpfs /mnt/\$val/mnt/dos/\$val
263 LDSO=\$(ls /mnt/\$val/lib/ld-* | sed q)
264 export LD_LIBRARY_PATH=\$val/lib:\$val/usr/lib:/lib
265 [ "$debug" = "true" ] && sh
266 umount /proc
267 exec /bin/switch_root /mnt \${LDSO#/mnt/} \$val/usr/sbin/chroot \$val /sbin/init
268 EOT
269 chmod +x /tmp/fs/init
270 ( cd /tmp/fs ; find * | cpio -o -H newc ) | lzma e $1 -si 2> /dev/null
271 rm -rf /tmp/fs /tmp/wait
272 }
274 is_loram()
275 {
276 [ -s /lib/squashfs.ko* ]
277 }
279 ls_r()
280 {
281 ls -r $@ 2> /dev/null || ls $@
282 }
284 doinstall()
285 {
286 mkdir -p /mnt/slitaz/boot /mnt/slitaz/mnt/dos
287 if ! mount.posixovl -F /mnt/slitaz -- \
288 -oallow_other -odefault_permissions -osuid; then
289 echo "Can't install SliTaz. Abort."
290 sleep 5
291 return 1
292 fi
293 dowait "Install root filesystem in /slitaz.."
294 if [ "$1" ]; then
295 if [ -d /media/cdrom/fs ]; then
296 ( cd /mnt/slitaz/fs; find | cpio -o -H newc ) | gzip -9
297 else
298 ls_r /media/cdrom/boot/rootfs* | xargs cat
299 fi > /mnt/slitaz/boot/rootfs.gz
300 tazusbinitfs /mnt/slitaz/boot/rootfs.gz
301 initrd=rootfs.gz
302 extraargs="/home=$(getuuid)/slitaz rdinit=/init1"
303 else
304 if [ -d /media/cdrom/fs ]; then
305 cp -a /media/cdrom/fs/. /mnt/slitaz
306 elif is_loram ; then
307 for i in /media/cdrom/boot/rootfs?*.* ; do
308 [ -s $(basename $i) ] && continue
309 cpio -i $i
310 done
311 for i in $(ls_r /media/cdrom/boot/rootfs*); do
312 mount -o loop,ro $i /sqfs/mnt
313 cp -a /sqfs/mnt/. /mnt/slitaz
314 umount -d /sqfs/mnt
315 done
316 else
317 for i in $(ls_r /media/cdrom/boot/rootfs*); do
318 uncpio $i /mnt/slitaz
319 done
320 fi
321 cp -a /etc/locale.conf /tmp/slitaz/etc 2> /dev/null
322 cp -a /etc/keymap.conf /tmp/slitaz/etc 2> /dev/null
323 mkinitrd /mnt/slitaz/boot/initrd
324 initrd=initrd
325 extraargs="mount=$(getuuid) subroot=slitaz"
326 fi
327 echo -en "\nInstall boot files..."
328 for i in /media/cdrom/boot/bzImage /media/cdrom/boot/*pxe* \
329 /media/cdrom/boot/isolinux/he* /media/cdrom/boot/isolinux/opt* \
330 /media/cdrom/README /media/cdrom/boot/memtest* ; do
331 [ -s $i ] && cp $i /mnt/slitaz/boot
332 done
333 for i in /mnt/slitaz/boot/memtest /mnt/slitaz/boot/*pxe* ; do
334 [ $(get 0 $i 2> /dev/null || echo 0) -eq 23117 ] &&
335 mv $i $i.exe
336 done
337 gettazboot /mnt/slitaz/boot/tazboot.exe
338 unix2dos > /mnt/slitaz/boot/tazboot.cmd <<EOT
339 kernel=/slitaz/boot/bzimage
340 initrd=/slitaz/boot/$initrd
341 rw root=/dev/null $extraargs autologin
342 EOT
343 unix2dos /mnt/slitaz/boot/he* /mnt/slitaz/boot/opt* \
344 /mnt/slitaz/boot/README
345 [ -d /mnt/slitaz/usr/sbin -a ! -x /mnt/slitaz/usr/sbin/mount.posixovl ] &&
346 cp $(which mount.posixovl) /mnt/slitaz/usr/sbin
347 rm -f /tmp/wait
348 false &&
349 [ -s /mnt/boot.ini ] && ! grep -qs tazboot /mnt/boot.ini &&
350 echo "Update boot.ini ..." && unix2dos >> /mnt/boot.ini <<EOT
351 C:\\slitaz\\boot\\tazboot.exe="SliTaz"
352 EOT
353 false &&
354 grep -qis menuitem /mnt/config.sys && ! grep -qi tazboot /mnt/config.sys &&
355 echo "Update config.sys ..." &&
356 sed -i 's/menudefault/menuitem SLITAZ, SliTaz\r\n&/' /mnt/config.sys &&
357 unix2dos >> /mnt/config.sys <<EOT
358 [SLITAZ]
359 install=\\slitaz\\boot\\tazboot.exe
360 EOT
361 return 0
362 }
364 install()
365 {
366 $DIALOG --clear \
367 --title " SliTaz UMSDOS way installation " \
368 --yes-label "Install" --yesno \
369 "\nSliTaz will be installed in the subdirectory \\slitaz of the current
370 DOS/Windows partition. You will see your files from /mnt/dos.\n\n
371 You can start SliTaz with \\slitaz\\boot\\tazboot.exe\n\n
372 To uninstall SliTaz, you have only to remove this directory.
373 The file \\boot.ini or \\config.sys may be modified too.\n\n
374 SliTaz may run slowly on 'UMSDOS way' installation due to the
375 posixovl filesystem. The 'TAZUSB way' installation runs faster.\n\n
376 To do a traditional installation with disk partitioning,
377 start SliTaz Live with 'SliTaz RAM boot' menu.\n
378 " 19 70
379 [ $? -eq 0 ] || return
380 doinstall || return
381 [ -x /mnt/slitaz/sbin/init ] || return
382 umount -d /media/cdrom
383 umount_proc
384 exec chroot /mnt/slitaz /sbin/init
385 }
387 installtaz()
388 {
389 $DIALOG --clear \
390 --title " SliTaz TAZUSB way installation " \
391 --yes-label "Install" --yesno \
392 "\nSliTaz will be installed in the subdirectory \\slitaz of the current
393 DOS/Windows partition. You will see your files from /mnt/dos.\n\n
394 You can start SliTaz with \\slitaz\\boot\\tazboot.exe\n\n
395 To uninstall SliTaz, you have only to remove this directory.
396 The file \\boot.ini or \\config.sys may be modified too.\n\n
397 The filesystem is loaded entirely into memory upon boot to
398 increase responsiveness. Only /home lands on hard disk.\n\n
399 To do a traditional installation with disk partitioning,
400 start SliTaz Live with 'SliTaz RAM boot' menu.\n
401 " 19 70
402 [ $? -eq 0 ] || return
403 doinstall tazusblike || return
404 dotwait
405 if [ -d /media/cdrom/fs ]; then
406 cp -a /media/cdrom/fs/. /
407 else
408 for i in $(ls_r /media/cdrom/boot/rootfs*); do
409 uncpio $i
410 done
411 fi
412 cp /tmp/fs/etc/* /etc
413 echo "/home=$(getuuid)/slitaz" > /cmdline
414 rm -f /tmp/wait
415 [ -x /init1 ] || return
416 umount -d /media/cdrom
417 umount /mnt/slitaz
418 rm -f /dev/cdrom
419 umount /mnt
420 mkdir /mnt/dos
421 umount_proc
422 exec /init1
423 }
425 tazboot()
426 {
427 $DIALOG --clear \
428 --title " SliTaz bootloader for DOS " \
429 --yes-label "Install" --yesno \
430 "\nThe file TAZBOOT.EXE will be created in the top directory. It supports
431 any linux kernel, multiple initramfs, a kernel command line and
432 an ISO image file loopback (retrieves files from an ISO file).\n\n
433 Usage: tazboot.exe [[@commands]|[kernel=<bzimage>]
434 [initrd=<rootfs>[,<rootfs2>...]] [bootfrom=<isofile>] cmdline args ...]\n\n
435 Defaults: tazboot @tazboot.cmd or tazboot kernel=bzImage auto\n\n\
436 Examples for tazboot.cmd:\n\n\
437 bootfrom=\\isos\\slitaz-4.0.iso\n\
438 kernel=boot/bzImage\n\
439 initrd=boot/rootfs4.gz,boot/rootfs3.gz,boot/rootfs2.gz,boot/rootfs1.gz\n\
440 rw root=/dev/null autologin\n\n\
441 kernel=\\slitaz\\vmlinuz root=/dev/sda5 ro\n\n
442 Unlike GRUB4DOS, it doesn't require unfragmented ISO image files.\n
443 " 24 78
444 [ $? -eq 0 ] || return
445 gettazboot /mnt/tazboot.exe
446 }
448 md5()
449 {
450 dotwait "Checking files"
451 ( cd /media/cdrom ; ${1:-md5sum -c md5sum*} ) > /tmp/data
452 rm -f /tmp/wait
453 $DIALOG --clear \
454 --title " Checked files " \
455 --textbox /tmp/data 24 78
456 rm -f /tmp/data
457 }
459 gotcdfile()
460 {
461 for i in /media/cdrom/$1 /media/cdrom/*/$1 \
462 /media/cdrom/*/isolinux/$1 ; do
463 file=$(ls $i 2> /dev/null | sed q)
464 [ -s "$file" ] && break
465 done
466 }
468 sha()
469 {
470 gotcdfile sha*sum*
471 sha=$(basename $file)
472 md5 "${sha%sum*}sum -c ${file#/media/cdrom/}"
473 }
475 readme()
476 {
477 gotcdfile README*
478 $DIALOG --clear \
479 --title " Readme " \
480 --textbox $file 24 78
481 }
483 bootlog()
484 {
485 $DIALOG --clear \
486 --title " Linux boot messages " \
487 --textbox /tmp/dmesg 24 78
488 }
490 bzimage()
491 {
492 $DIALOG --clear \
493 --title " Create linux.exe ? " \
494 --yes-label "Install" --yesno \
495 "\nLinux.exe launches the linux kernel under DOS (in real mode only).
496 The cmdline arguments are supported except initrd=, vga= (you can try 'rdev -v')
497 and mem= (partially).\n\nExample:\nC:\\> linux.exe root=/dev/hda2 ro panic=60\n
498 " 12 70
499 [ $? -eq 0 ] || return
500 cp /media/cdrom/boot/bzImage /mnt/linux.exe
501 }
503 memtest()
504 {
505 $DIALOG --clear \
506 --title " Create memtest.exe ? " \
507 --yes-label "Install" --yesno \
508 "\nMemtest86 is a thorough, stand alone memory test for x86 architecture
509 computers. BIOS based memory tests are a quick, cursory check and often
510 miss many of the failures that are detected by Memtest86.\n
511 " 12 70
512 [ $? -eq 0 ] && gotcdfile memtest* && cp $file /mnt/memtest.exe
513 }
515 mkfat12()
516 {
517 [ $(($(get 0 $1) - 0x5A4D)) -eq 0 ] || return
518 J=$(($(get 3 $1 1) + 0x02))
519 R=$((1 + $(get 497 $1 1) + 1 + ($(get 500 $1)-1)/32))
520 [ $R -lt 2500 ] || return
521 [ $((($(get 500 $1)-1) & 31)) -lt 30 ] &&
522 ddq if=$file bs=32 count=1 seek=$(($R*16 - 1)) of=/dev/fd0
523 G="18 0 2 0 0 0 0 0"
524 [ $J -gt 25 ] || G=""
525 F=0
526 for i in 1 2 3; do
527 F=$((((2880-$R-$F-$F)*3+1023)/1024))
528 done
529 bytes2bin 0xEB $J 0x90 0x20 0x20 0x20 0x20 0x20 0x20 0x20 0x20 \
530 0 2 2 $(($R%256)) $(($R/256)) 2 64 0 64 11 0xF0 $F 0 \
531 $G | ddq bs=1 of=/dev/fd0
532 ddq if=/dev/zero bs=512 count=$((4+$F+$F)) seek=$R of=/dev/fd0
533 for i in $R $(($R+$F)) ; do
534 bytes2bin 0xF0 0xFF 0xFF | ddq bs=512 seek=$i of=/dev/fd0
535 done
536 echo -n $(basename $1) | ddq bs=1 seek=3 count=8 of=/dev/fd0
537 }
539 mkfloppy()
540 {
541 dotwait "Create a $(basename $1) boot floppy"
542 ddq if=$1 of=/dev/fd0
543 mkfat12 $1
544 rm -f /tmp/wait
545 }
547 fdmemtest()
548 {
549 $DIALOG --clear \
550 --title " Create a Memtest86 boot floppy " \
551 --yes-label "Create floppy" --yesno \
552 "\nMemtest86 is a thorough, stand alone memory test for x86 architecture
553 computers. BIOS based memory tests are a quick, cursory check and often
554 miss many of the failures that are detected by Memtest86.\n\n
555 Please insert a blank disk in floppy drive.\n
556 " 12 70
557 [ $? -eq 0 ] && gotcdfile memtest* && mkfloppy $file
558 }
560 pxe()
561 {
562 gotcdfile ?pxe
563 $DIALOG --clear \
564 --title " Create $(basename $file).exe ? " \
565 --yes-label "Install" --yesno \
566 "\nBoot your operating system from the internet and enjoy a full system
567 working entirely in RAM with speed and stability in mind. The Linux Kernel
568 and the complete SliTaz compressed root filesystem will be loaded into RAM
569 from the Web using PXE and HTTP protocols.\n
570 " 12 70
571 [ $? -eq 0 ] || return
572 cp $file /mnt/$(basename $file).exe
573 }
575 fdpxe()
576 {
577 $DIALOG --clear \
578 --title " Create a SliTaz Web boot floppy " \
579 --yes-label "Create floppy" --yesno \
580 "\nBoot your operating system from the internet and enjoy a full system
581 working entirely in RAM with speed and stability in mind. The Linux Kernel
582 and the complete SliTaz compressed root filesystem will be loaded into RAM
583 from the Web using PXE and HTTP protocols.\n\n
584 Please insert a blank disk in floppy drive.\n
585 " 12 70
586 [ $? -eq 0 ] && gotcdfile ?pxe && mkfloppy $file
587 }
589 gotposixovl()
590 {
591 mount.posixovl 2>&1 | grep -qi usage &&
592 echo -en "\"$1\" \"$2\""
593 }
595 xfile()
596 {
597 [ "$(which $1)" ] && echo -en "\"$2\" \"$3\""
598 }
600 cdfile()
601 {
602 gotcdfile "$1" && echo -en "\"$2\" \"$3\""
603 }
605 cdfilex()
606 {
607 [ "$(which kexec)" ] && cdfile "$@"
608 }
610 cdfilef()
611 {
612 [ -e /sys/block/fd0 ] && cdfile "$@"
613 }
615 cdexe()
616 {
617 gotcdfile "$1" &&
618 [ $(get 0 $file 2>/dev/null || echo 0) -eq 23117 ] &&
619 echo -en "\"$2\" \"$3\""
620 }
622 fddata()
623 {
624 [ -e /sys/block/fd0 ] &&
625 [ $(get 28 /mnt/$ISO 1 2> /dev/null || echo 0) -ne 0 ] &&
626 echo -en "\"$1\" \"$2\""
627 }
629 ishybrid()
630 {
631 C=$((2048*$(get $(((17*2048) + 71)) /mnt/$ISO 4)))
632 [ $(get $C /mnt/$ISO 4) -eq 1 ] || return
633 [ $(get $(($C+30)) /mnt/$ISO 4) -eq $((0x88AA55)) ] || return
634 C=$((2048*$(get $(($C+40)) /mnt/$ISO 4)))
635 [ $(get $(($C+64)) /mnt/$ISO 4) -eq 1886961915 ] &&
636 echo -en "\"$1\" \"$2\""
637 }
639 gotisomd5()
640 {
641 [ "$(which md5sum 2> /dev/null)" ] &&
642 [ $(get 18 /dev/loop0) -ne 0 ] && echo -en "\"$1\" \"$2\""
643 }
645 isomd5()
646 {
647 dotwait "Checking iso image"
648 [ "$(ddq if=/dev/loop0 bs=2k skip=16 \
649 count=$(echo $(get 32848 /dev/loop0)) | md5sum)" == \
650 "$(ddq if=/dev/loop0 bs=16 count=1 skip=2047 | od -N 16 -t x1 -An | \
651 sed 's/ //g') -" ] && echo "OK" || echo "ERROR"
652 echo -en "\rChecking iso hybrid boot"
653 n=$(($(get 2 /dev/loop0)-1+($(get 4 /dev/loop0)-1)*512))
654 s=$(get 0 /dev/loop0 2 $n | awk '{ i+= $0 } END { print i }')
655 [ $(((1+$s+$(get $(($n+1)) /dev/loop0 1)) % 65536)) -eq 0 ] &&
656 echo "OK" || echo "ERROR"
657 rm -f /tmp/wait
658 echo -e "\rPress RETURN to continue."
659 read n
660 }
662 fdbootstrap()
663 {
664 sz=$((512 * $(echo $(get 28 /mnt/$ISO 1))))
665 $DIALOG --clear \
666 --title " Create a floppy bootstrap " \
667 --yes-label "Continue" --yesno \
668 "\nThe floppy will install a driver to access the ISO file
669 on your hard disk and will emulate a CD-ROM during the boot process.\n\n
670 Please insert a floppy in drive now.\n
671 " 10 70
672 [ $? -eq 0 ] || return
673 ddq if=/mnt/$ISO of=/tmp/bootiso bs=1 count=512 \
674 skip=$(( $(get 66 /mnt/$ISO) - $sz ))
675 echo "$ISO" | ddq of=/tmp/bootiso bs=512 seek=1 count=1
676 ddq if=/mnt/$ISO of=/tmp/bootiso bs=1 count=$sz seek=2 \
677 skip=$(( $(get 66 /mnt/$ISO) - $sz + 512 ))
678 mkfloppy /tmp/bootiso
679 rm -f /tmp/bootiso
680 }
682 usbdev()
683 {
684 dotwait "Wait 5 seconds for USB devices"
685 sleep 5
686 rm -f /tmp/wait
687 DEV="$(grep -l 1 /sys/block/*/removable | \
688 sed 's|/sys/block/\(.*\)/removable|\1|')"
689 grep -qs 1 /sys/block/$DEV/ro && return
690 [ "$DEV" ] || return
691 cat > /tmp/dialog <<EOT
692 $DIALOG --clear \
693 --title " Select your USB key " \
694 --menu "\nPlease select the USB key according to its known size.\n\n" \
695 14 70 4 \
696 $(for i in $DEV ; do
697 echo -n "/dev/$i \"$(($(cat /sys/block/$i/size)/2048))MB $(cat /sys/block/$i/device/model 2> /dev/null)\" "
698 done) \
700 EOT
701 exec 3>&1
702 device=$(. /tmp/dialog 2>&1 1>&3)
703 retval=$?
704 exec 3>&-
705 [ $retval -eq 0 ]
706 }
708 tazusbmsg()
709 {
710 [ "$(which tazusb 2> /dev/null)" ] || return
711 echo "You should choose 'USB key read/write installation' to be
712 able to save the package updates or your own configuration and data files.\n\n"
713 }
715 usbbootkey()
716 {
717 $DIALOG --clear \
718 --title " Create a USB boot key " \
719 --yes-label "Continue" --yesno \
720 "\nThe USB key will be used like a CD-ROM. You will not be able to write
721 any data on the boot partition.\n\n
722 An extra FAT32 partition will be created with the remaining free space.\n\n
723 $(tazusbmsg)Please plug your USB stick in now.\n
724 " 16 70
725 [ $? -eq 0 ] || return
726 usbdev || return
728 # perform dd in progess bar
729 max=$(($(cat /sys/block/loop0/size)/2048))
730 i=0; ddq if=/mnt/$ISO bs=1024k | (
731 while ddq bs=1024k count=1 ; do
732 i=$(($i + 1))
733 [ $i -gt $max ] && break
734 echo $((($i*100)/$max)) | dialog --gauge \
735 " The ISO image transfer can be long. Please wait..." \
736 6 70 0 > /dev/tty0 2>&1
737 done ) > $device
739 # partition + fat32 format for the remining space
740 get 446 $device 2 12 | xargs echo | { read dx cx ol oh ll lh
741 if [ $dx -eq $((0x3F17)) ]; then
742 cx=$(($cx & 0xFF00))
743 ofs=$(($ll+($lh<<16)))
744 n=$(($(cat /sys/block/${device#/dev/}/size)-$ofs))
745 m=$(($cx+($n/8)))
746 [ $m -gt $((0x3FF00)) ] && m=$((0x3FF00))
747 m=$((($m & 0xFF00)+(($m>>16)<<6)))
748 words2bin 0 $((0x101+$cx)) 0x3F0B $((32+$m)) \
749 $ll $lh $(($n & 0xFFFF)) $(($n >> 16)) | \
750 ddq bs=1 seek=446 of=$device
751 if [ "$(which mkdosfs 2> /dev/null)" ]; then
752 losetup -o $((512*$ofs)) /dev/loop2 $device
753 mkdosfs -n "SLITAZ BOOT" /dev/loop2
754 words2bin $(($ofs & 0xFFFF)) $(($ofs >> 16)) | \
755 ddq bs=1 seek=28 of=/dev/loop2
756 losetup -d /dev/loop2
757 fi
758 fi ; }
759 }
761 usbkey()
762 {
763 $DIALOG --clear \
764 --title " Create a SliTaz USB key " \
765 --yes-label "Continue" --yesno \
766 "\nUnlike a hard drive install, the filesystem is kept in a compressed
767 rootfs.gz. The filesystem is loaded entirely into memory upon boot.
768 This should increase responsiveness, protect the filesystem against
769 accidental corruption and reduce read/writes to the USB drive.
770 Once setup, the tazusb utility can rewrite the root filesystem
771 with any changes you have made since booting up,
772 giving the effective benefits of a hard drive install.\n\n
773 /home is mounted on boot using the UUID of your particular flash drive.
774 Unlike a device name, the UUID has the benefit of never changing from machine
775 to machine.\n\n
776 Please plug your USB stick in now.\n
777 " 19 70
778 [ $? -eq 0 ] || return
779 usbdev || return
780 exec 3>&1
781 format=`$DIALOG --clear \
782 --title " Select the filesystem " \
783 --radiolist "\nPlease select the filesystem type to create.\n\n\
784 The filesystem creation will erase all the data \
785 in the USB key." 14 70 4 \
786 "none" "Do not erase the USB key" on \
787 "ext3" "Ext3 journaling filesystem" off \
788 "ext2" "Ext2 filesystem" off \
789 "fat32" "Windows FAT32 filesystem" off \
790 2>&1 1>&3`
791 retval=$?
792 exec 3>&-
793 [ $retval -eq 0 ] || return
794 [ "$format" != "none" ] && tazusb format $device "SliTaz" $format
795 tazusb gen-iso2usb /mnt/$ISO $device
796 }
798 mount_loram()
799 {
800 is_loram || return
801 insmod /lib/squashfs.ko* 2> /dev/null
802 if [ -d /media/cdrom/fs ]; then
803 ln -s /media/cdrom/fs /sqfs
804 else
805 mkdir /sqfs
806 mount -o loop,ro -t squashfs /rootfs*.gz /sqfs
807 fi
808 ln -s /sqfs/lib/* lib
809 ln -s /sqfs/usr /sqfs/var /
810 for i in dmesg basename tr od reboot poweroff getty sync ; do
811 ln -s /sqfs/bin/busybox /bin/$i
812 done
813 }
815 umount_loram()
816 {
817 is_loram || return
818 rm /var /usr
819 umount -d /sqfs
820 rmdir /sqfs
821 }
823 text()
824 {
825 umount_loram
826 umount -d /media/cdrom
827 rm -f /dev/cdrom
828 umount /mnt
829 umount_proc
830 exec /init
831 }
833 live()
834 {
835 n=0
836 dotwait "Extract filesystem..."
837 for i in $(ls_r /media/cdrom/boot/rootfs*gz); do
838 grep -q ' lm ' /proc/cpuinfo && [ -s ${i}64 ] && i=${i}64
839 [ $((n++)) -eq 0 ] || uncpio $i
840 done
841 rm -f /tmp/wait
842 text
843 }
845 dosync()
846 {
847 sync
848 [ ! -L /sqfs ] && umount -d /media/cdrom && umount /mnt
849 }
851 restart()
852 {
853 dosync
854 reboot -f
855 }
857 stop()
858 {
859 dosync
860 poweroff -f
861 }
863 dokexec()
864 {
865 kexec -l $file
866 dosync
867 kexec -e
868 }
870 runmemtest()
871 {
872 gotcdfile memtest* && dokexec
873 }
875 runpxe()
876 {
877 gotcdfile ?pxe && dokexec
878 }
880 shell()
881 {
882 trap text 2
883 getty -n -l /bin/ash 38400 tty1 || sh
884 }
886 BIN=bin/mount.posixovl
887 [ -x /usr/s$BIN ] || mv /bin/mount.posixovl.iso2exe \
888 /usr/s$BIN 2> /dev/null || mv /bin/mount.posixovl.iso2exe /$BIN 2> /dev/null
889 mount_proc
890 ISO="$(getarg bootfrom | sed 's/.://;s|\\|/|g')"
891 getiso
892 mount_loram
893 case "${ISO##*/}$(getarg mode)" in
894 *install*|*INSTALL*) install ;;
895 *live*|*LIVE*) live ;;
896 *text*|*TEXT*) text ;;
897 esac
898 which $DIALOG 2> /dev/null || DIALOG=tinydialog
899 dmesg > /tmp/dmesg
901 while true; do
902 trap shell 2
903 keymap="$(cat /etc/keymap.conf 2> /dev/null)"
904 locale="$(sed '/^LANG=/!d;s/.*=//' /etc/locale.conf 2> /dev/null)"
905 cat > /tmp/dialog <<EOT
906 $DIALOG --clear \
907 --title " Welcome to Linux " \
908 --menu "" 23 70 17 \
909 $(xfile tazkeymap "tazkeymap" "Select keyboard (${keymap:-none})") \
910 $(xfile tazlocale "tazlocale" "Select locale (${locale:-none})") \
911 $(cdfile boot/bzImage "live" "Linux RAM boot (full desktop)") \
912 "text" "Linux RAM boot" \
913 $(cdfile README* "readme" "Show the README file") \
914 $(gotisomd5 "isomd5" "Check the ISO image") \
915 $(cdfile md5sum* "md5" "Check the ISO files") \
916 $(cdfile sha*sum* "sha" "Check the ISO files") \
917 $(gotposixovl "install" "Hard disk installation (UMSDOS way)") \
918 $(gotposixovl "installtaz" "Hard disk installation (TAZUSB way)") \
919 $(xfile tazusb "usbkey" "USB key read/write installation") \
920 $(ishybrid "usbbootkey" "USB boot key (read only)") \
921 $(fddata "fdbootstrap" "Create a floppy bootstrap") \
922 "tazboot" "Get tazboot.exe Linux loader" \
923 $(cdexe boot/bzImage "bzimage" "Get linux DOS/EXE file") \
924 $(cdexe memtest* "memtest" "Get Memtest86 DOS/EXE file") \
925 $(cdfilef memtest* "fdmemtest" "Create a Memtest86 boot floppy") \
926 $(cdfilex memtest* "runmemtest" "Start Memtest86") \
927 $(cdexe ?pxe "pxe" "Get SliTaz Web boot DOS/EXE utility") \
928 $(cdfilef ?pxe "fdpxe" "Create a SliTaz Web boot floppy") \
929 $(cdfilex ?pxe "runpxe" "Start the SliTaz Web boot utility") \
930 $(xfile reboot "restart" "Restart the computer") \
931 $(xfile poweroff "stop" "Power off") \
932 "bootlog" "Linux boot messages" \
933 "shell" "Shell prompt" \
935 EOT
936 exec 3>&1
937 value=$(. /tmp/dialog 2>&1 1>&3)
938 retval=$?
939 exec 3>&-
940 [ $retval -eq 0 ] || continue
941 $value
942 done