wok view syslinux/stuff/iso2exe/init @ rev 14261

syslinux/iso2exe: add NT+ support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Mar 30 18:36:46 2013 +0100 (2013-03-30)
parents 65b3fd0022ed
children 692d9593f959
line source
1 #!/bin/sh
3 DIALOG=dialog
5 get()
6 {
7 od -j $1 -N ${3:-2} -t u${3:-2} -An $2 2> /dev/null ||
8 dd if=$2 bs=1 skip=$1 count=${3:-2} 2> /dev/null | \
9 hexdump -e "\"\" 1/${3:-2} \"%d\""
10 }
12 getarg()
13 {
14 sed "/$1=/!d;s/.*$1=\\([^ ]*\\).*/\\1/" /proc/cmdline
15 }
17 gettazboot()
18 {
19 echo "Creating $(basename $1) ..."
20 O=$(($(get 66 /mnt/$ISO) - 0xE0))
21 L=$((0x8000 - $(get 24 /mnt/$ISO) - $O))
22 S=$((32+$L))
23 P=$((($S+511)/512))
24 E=$((4096-(32*$P)))
25 for i in 0x5A4D $(($S%512)) $P 0 2 $E -1 $((${2:-0}-16)) \
26 -2 0 256 -16 28 0x6C53 0x5469 0x7A61; do
27 printf '\\\\x%02X\\\\x%02X' $(($i&255)) $((($i>>8)&255)) | \
28 xargs echo -en
29 done > $1
30 dd bs=1 count=$L skip=$(echo $O) if=/mnt/$ISO >> $1 2> /dev/null
31 }
33 checkmagic()
34 {
35 [ -s $1 ] && [ $(getarg magic) == $(get 24 $1 4) ]
36 }
38 getiso()
39 {
40 mkdir -p /media/cdrom
41 blkid | while read dev info ; do
42 mount ${dev%:} /mnt
43 if checkmagic /mnt/$ISO; then
44 mount -o loop,ro /mnt/$ISO /media/cdrom
45 echo "Found $ISO on ${dev%:}"
46 break
47 fi
48 umount /mnt
49 done
50 }
52 uncpio()
53 {
54 echo "Extracting $(basename $1) ..."
55 case $(get 0 $1) in
56 *35615) zcat $1 ;;
57 *\ 93) unlzma -c $1 ;;
58 *) cat $1 ;;
59 esac | ( cd ${2:-/} ; cpio -idmu > /dev/null 2>&1 )
60 }
62 getuuid()
63 {
64 dev=$(mount | sed '/ \/mnt /!d;s/ .*//;s|/dev/||;q')
65 blkid | sed "/$dev:/!d;s/.*UUID=.\\([^ ]*\\)\".*/\\1/"
66 }
68 mkinitrd()
69 {
70 echo "Creating $(basename $1) ..."
71 for i in bin lib dev proc tmp mnt etc ; do
72 mkdir -p /tmp/fs/$i
73 done
74 for i in /dev/console /dev/null /dev/tty /dev/tty1 /dev/fuse \
75 /dev/hd* /dev/sd* ; do
76 cp -a $i /tmp/fs$i
77 done
78 for i in /bin/busybox $(which mount.posixovl) $(which blkid); do
79 cp $(LD_TRACE_LOADED_OBJECTS=1 /lib/ld*.so $i | \
80 sed 's|.*=> \(.*/lib/l[^ ]*\).*|\1|;/^\//!d') /tmp/fs/lib
81 cp $i /tmp/fs/bin
82 done
83 cp -a /lib/ld-* /tmp/fs/lib
84 for i in $(busybox | sed '/Current/,$!d'); do
85 ln -s busybox /tmp/fs/bin/${i%,}
86 done
87 ln -s /proc/mounts /tmp/fs/etc/mtab
88 cat > /tmp/fs/init <<EOT
89 #!/bin/sh
91 arg()
92 {
93 grep -q \$1 /proc/cmdline &&
94 val="\$(sed "s/.*\$1=\\([^ ]*\\).*/\\1/" < /proc/cmdline)" &&
95 echo "\$2 \$val"
96 }
98 mount -t proc /proc /proc
99 arg mount "Mount device"
100 mount \$( (blkid /dev/?d* || blkid) | grep \$val | sed 's/:.*//;q') /mnt
101 arg subroot "Change root to directory"
102 mount.posixovl -F /mnt/\$val
103 mount --bind /mnt /mnt/\$val/mnt/dos
104 LDSO=\$(ls /mnt/\$val/lib/ld-* | sed q)
105 umount /proc
106 export LD_LIBRARY_PATH=\$val/lib:\$val/usr/lib:/lib
107 exec /bin/switch_root /mnt \${LDSO#/mnt/} \$val/usr/sbin/chroot \$val /sbin/init
108 EOT
109 chmod +x /tmp/fs/init
110 ( cd /tmp/fs ; find * | cpio -o -H newc ) | lzma e $1 -si 2> /dev/null
111 rm -rf /tmp/fs
112 }
114 is_loram()
115 {
116 [ -s /lib/squashfs.ko* ]
117 }
119 doinstall()
120 {
121 mkdir /mnt/slitaz
122 mount.posixovl -F /mnt/slitaz || return
123 mkdir -p /mnt/slitaz/boot /mnt/slitaz/mnt/dos
124 if [ -d /media/cdrom/fs ]; then
125 cp -a /media/cdrom/fs/. /mnt/slitaz
126 elif is_loram ; then
127 for i in /media/cdrom/boot/rootfs?.* ; do
128 [ -s $(basename $i) ] && continue
129 cpio -i $i
130 done
131 for i in $(ls -r /media/cdrom/boot/rootfs*); do
132 mount -o loop,ro $i /sqfs/mnt
133 cp -a /sqfs/mnt/. /mnt/slitaz
134 umount -d /sqfs/mnt
135 done
136 else
137 for i in $(ls -r /media/cdrom/boot/rootfs*); do
138 uncpio $i /mnt/slitaz
139 done
140 fi
141 for i in /media/cdrom/boot/bzImage /media/cdrom/boot/*pxe* \
142 /media/cdrom/boot/isolinux/he* /media/cdrom/boot/isolinux/opt* \
143 /media/cdrom/README /media/cdrom/boot/memtest* ; do
144 [ -s $i ] && cp $i /mnt/slitaz/boot
145 done
146 umount -d /media/cdrom
147 gettazboot /mnt/slitaz/boot/tazboot.exe
148 mkinitrd /mnt/slitaz/boot/initrd
149 unix2dos > /mnt/slitaz/boot/tazboot.cmd <<EOT
150 kernel=\\slitaz\\boot\\bzimage
151 initrd=\\slitaz\\boot\\initrd
152 rw root=/dev/null mount=$(getuuid) subroot=slitaz autologin
153 EOT
154 unix2dos /mnt/slitaz/boot/he* /mnt/slitaz/boot/opt* \
155 /mnt/slitaz/boot/README
156 [ -x /mnt/slitaz/usr/sbin/mount.posixovl ] ||
157 cp $(which mount.posixovl) /mnt/slitaz/usr/sbin
158 ! grep -qs tazboot /mnt/boot.ini && unix2dos >> /mnt/boot.ini <<EOT
159 C:\\slitaz\\boot\\tazboot.exe="SliTaz"
160 EOT
161 grep -qs menuitem /mnt/config.sys && !grep -q tazboot /mnt/config.sys &&
162 sed -i 's/menudefault=/menuitem=slitaz,SliTaz\n&/' /mnt/config.sys &&
163 cat >> /mnt/config.sys <<EOT
164 [slitaz]
165 device=\\slitaz\\boot\\tazboot.exe
166 EOT
167 unix2dos /mnt/config.sys
168 }
170 install()
171 {
172 $DIALOG --clear \
173 --title " SliTaz UMSDOS like installation " \
174 --yes-label "Install" --yesno \
175 "\nSliTaz will be installed in the subdirectory \\slitaz of the current
176 DOS/Windows partition. You will see your files from /mnt/dos.\n\n
177 You can start SliTaz with \\slitaz\\boot\\tazboot.exe\n\n
178 To uninstall SliTaz, you have only to remove this directory.
179 The file \\boot.ini or \\config.sys may be modified too.\n\n
180 To do a traditional installation with disk partitioning,
181 start SliTaz Live with 'SliTaz RAM boot' menu.\n
182 " 16 70
183 [ $? -eq 0 -a -n "$(which mount.posixovl)" ] || return
184 doinstall
185 umount /proc
186 exec chroot /mnt/slitaz /sbin/init
187 }
189 tazboot()
190 {
191 $DIALOG --clear \
192 --title " SliTaz bootloader for DOS " \
193 --yes-label "Install" --yesno \
194 "\nThe file TAZBOOT.EXE will be created in the top directory. It supports
195 a bzImage linux kernel, multiple initramfs, a kernel command line and
196 an ISO image file loopback (retrieves files from an ISO file).\n\n
197 Usage: tazboot.exe [[@commands]|[kernel=<bzimage>]
198 [initrd=<rootfs>[,<rootfs2>...]] [iso=<isofile>] cmdline args ...]\n\n
199 Defaults: tazboot @tazboot.cmd or tazboot kernel=bzImage auto\n\n\
200 Examples for tazboot.cmd:\n\n\
201 iso=\\isos\\slitaz-4.0.iso\n\
202 kernel=boot/bzImage\n\
203 initrd=boot/rootfs4.gz,boot/rootfs3.gz,boot/rootfs2.gz,boot/rootfs1.gz\n\
204 rw root=/dev/null autologin\n\n\
205 kernel=\\slitaz\\vmlinuz root=/dev/sda5 ro\n\n
206 Unlike GRUB4DOS, it doesn't require unfragmented ISO image files.\n
207 " 24 78
208 [ $? -eq 0 ] || return
209 gettazboot /mnt/tazboot.exe
210 }
212 md5()
213 {
214 echo "Checking files..."
215 ( cd /media/cdrom ; md5sum -c md5sum ) > /tmp/data
216 $DIALOG --clear \
217 --title " Checked files " \
218 --textbox /tmp/data 24 78
219 rm -f /tmp/data
220 }
222 readme()
223 {
224 $DIALOG --clear \
225 --title " Readme " \
226 --textbox /media/cdrom/README 24 78
227 }
229 bootlog()
230 {
231 $DIALOG --clear \
232 --title " Linux boot messages " \
233 --textbox /tmp/dmesg 24 78
234 }
236 memtest()
237 {
238 $DIALOG --clear \
239 --title " Memtest86 " \
240 --yes-label "Install" --yesno \
241 "\nMemtest86 is a thorough, stand alone memory test for x86 architecture
242 computers. BIOS based memory tests are a quick, cursory check and often
243 miss many of the failures that are detected by Memtest86.\n
244 " 12 70
245 [ $? -eq 0 ] || return
246 cp /media/cdrom/boot/memtest /mnt
247 }
249 fdmemtest()
250 {
251 $DIALOG --clear \
252 --title " Create a Memtest86 boot floppy " \
253 --yes-label "Create floppy" --yesno \
254 "\nMemtest86 is a thorough, stand alone memory test for x86 architecture
255 computers. BIOS based memory tests are a quick, cursory check and often
256 miss many of the failures that are detected by Memtest86.\n\n
257 Please insert a blank disk in floppy drive.\n
258 " 12 70
259 [ $? -eq 0 ] || return
260 dd if=/media/cdrom/boot/memtest of=/dev/fd0
261 }
263 gpxe()
264 {
265 $DIALOG --clear \
266 --title " SliTaz Web boot " \
267 --yes-label "Install" --yesno \
268 "\nBoot your operating system from the internet and enjoy a full system
269 working entirely in RAM with speed and stability in mind. The Linux Kernel
270 and the complete SliTaz compressed root filesystem will be loaded into RAM
271 from the Web using PXE and HTTP protocols.\n
272 " 12 70
273 [ $? -eq 0 ] || return
274 cp /media/cdrom/boot/gpxe /mnt
275 }
277 fdgpxe()
278 {
279 $DIALOG --clear \
280 --title " Create a SliTaz Web boot floppy " \
281 --yes-label "Create floppy" --yesno \
282 "\nBoot your operating system from the internet and enjoy a full system
283 working entirely in RAM with speed and stability in mind. The Linux Kernel
284 and the complete SliTaz compressed root filesystem will be loaded into RAM
285 from the Web using PXE and HTTP protocols.\n\n
286 Please insert a blank disk in floppy drive.\n
287 " 12 70
288 [ $? -eq 0 ] || return
289 dd if=/media/cdrom/boot/gpxe of=/dev/fd0
290 }
292 cdfile()
293 {
294 [ -s /media/cdrom/$1 ] && echo -en "$2 ${3// /.}"
295 }
297 fdbootstrap()
298 {
299 sz=$(echo $(od -j 28 -N 1 -t u1 -An /mnt/$ISO))
300 if [ 0$sz -eq 0 ]; then
301 $DIALOG --clear \
302 --title " No floppy bootstrap available " \
303 --msgbox \
304 "\nThe floppy bootstrap code is not available in the ISO file.\n\n
305 Can't create the floppy.\n
306 " 9 70
307 else
308 $DIALOG --clear \
309 --title " Create a floppy bootstrap " \
310 --yes-label "Continue" --yesno \
311 "\nThe floppy will install a driver to access to the ISO file
312 on your hard disk and will emulate a CD-ROM during the boot process.\n\n
313 Please insert a floppy in drive now.\n
314 " 10 70
315 [ $? -eq 0 ] || return
316 dd if=/mnt/$ISO of=/dev/fd0 bs=1 count=$(($sz * 512)) \
317 skip=$(( $(od -j 60 -N 4 -t u4 -An) - ($sz * 512) ))
318 echo "$ISO" | dd of=/dev/fd0 bs=512 seek=1
319 fi
320 }
322 usbdev()
323 {
324 sleep 5
325 DEV="$(grep -l 1 /sys/block/*/removable | \
326 sed 's|/sys/block/\(.*\)/removable|\1|')"
327 [ -n "$DEV" ] || return
328 exec 3>&1
329 device=`$DIALOG --clear \
330 --title " Select your USB key " \
331 --menu "\nPlease select the USB key according to its known size.\n\n" \
332 14 70 4 \
333 $(for i in $DEV ; do
334 echo "/dev/$i $(( $(cat /sys/block/$i/size) / 1024 ))MB"
335 done) \
336 2>&1 1>&3`
337 retval=$?
338 exec 3>&-
339 [ $retval -eq 0 ]
340 }
342 usbbootkey()
343 {
344 $DIALOG --clear \
345 --title " Create a USB boot key " \
346 --yes-label "Continue" --yesno \
347 "\nThe USB key will be used like a CD-ROM. You will not be able to write
348 any data on it.\n\n
349 You should choose 'USB key read/write installation' to be
350 able to save the package updates or your own configuration and data files.\n\n
351 Please plug your USB stick in now.\n
352 " 13 70
353 [ $? -eq 0 ] || return
354 usbdev || return
355 dd if=/mnt/$ISO of=$device
356 if [ $(get 0 /mnt/$ISO) -eq 23117 ]; then # iso2exe ?
357 LOC=$(get 69 /mnt/$ISO 1)
358 # move isohybrid boot sector (partitionable)
359 dd if=/mnt/$ISO bs=512 count=1 skip=$LOC of=$device conv=notrunc
360 # move tazlito data (backward compatibility)
361 dd if=/mnt/$ISO bs=512 count=20 skip=$(($LOC+1)) of=$device seek=2 conv=notrunc
362 fi
363 }
365 usbkey()
366 {
367 $DIALOG --clear \
368 --title " Create a SliTaz USB key " \
369 --yes-label "Continue" --yesno \
370 "\nUnlike a hard drive install, the filesystem is kept in a compressed
371 rootfs.gz. The filesystem is loaded entirely into memory upon boot.
372 This should increase responsiveness, protect the filesystem against
373 accidental corruption and reduce read/writes to the USB drive.
374 Once setup, the tazusb utility can rewrite the root filesystem
375 with any changes you have made since booting up,
376 giving the effective benefits of a hard drive install.\n\n
377 /home is mounted on boot using the UUID of your particular flash drive.
378 Unlike a device name, the UUID has the benefit of never changing from machine
379 to machine.\n\n
380 Please plug your USB stick in now.\n
381 " 19 70
382 [ $? -eq 0 -a -n "$(which tazusb)" ] || return
383 usbdev || return
384 exec 3>&1
385 format=`$DIALOG --clear \
386 --title " Select the filesystem " \
387 --radiolist "\nPlease select the filesystem type to create.\n\n\
388 The filesystem creation will erase all the data \
389 in the USB key." 14 70 4 \
390 "none" "Do not erase the USB key" on \
391 "ext3" "Ext3 journaling filesystem" off \
392 "ext2" "Ext2 filesystem" off \
393 "fat32" "Windows FAT32 filesystem" off \
394 2>&1 1>&3`
395 retval=$?
396 exec 3>&-
397 [ $retval -eq 0 ] || return
398 [ "$format" != "none" ] && tazusb format $device "SliTaz" $format
399 tazusb gen-iso2usb /mnt/$ISO $device
400 }
402 mount_loram()
403 {
404 is_loram || return
405 insmod /lib/squashfs.ko* 2> /dev/null
406 if [ -d /media/cdrom/fs ]; then
407 ln -s /media/cdrom/fs /sqfs
408 else
409 mkdir /sqfs
410 mount -o loop,ro -t squashfs /rootfs*.gz /sqfs
411 fi
412 ln -s /sqfs/lib/* lib
413 ln -s /sqfs/usr /sqfs/var /
414 for i in dmesg basename tr od reboot poweroff getty sync ; do
415 ln -s /sqfs/bin/busybox /bin/$i
416 done
417 }
419 umount_loram()
420 {
421 is_loram || return
422 rm /var /usr
423 umount -d /sqfs
424 rmdir /sqfs
425 }
427 text()
428 {
429 umount_loram
430 umount -d /media/cdrom
431 umount /mnt
432 umount /proc
433 exec /init
434 }
436 live()
437 {
438 n=0
439 for i in $(ls -r /media/cdrom/boot/rootfs*); do
440 [ $((n++)) -eq 0 ] || uncpio $i
441 done
442 text
443 }
445 restart()
446 {
447 sync
448 [ ! -L /sqfs ] && umount -d /media/cdrom && umount /mnt
449 reboot -f
450 }
452 stop()
453 {
454 sync
455 [ ! -L /sqfs ] && umount -d /media/cdrom && umount /mnt
456 poweroff -f
457 }
459 shell()
460 {
461 getty -n -l /bin/ash 38400 tty1 || sh
462 }
464 BIN=bin/mount.posixovl
465 [ -x /usr/s$BIN ] || mv /bin/mount.posixovl.iso2exe \
466 /usr/s$BIN 2> /dev/null || mv /bin/mount.posixovl.iso2exe /$BIN
467 mount -t proc /proc /proc
468 ISO="$(getarg iso | sed 's/.://;s|\\|/|g')"
469 getiso
470 mount_loram
471 case "$(basename $ISO | tr [A-Z] [a-z])$(getarg mode)" in
472 *install*) install ;;
473 *live*) live ;;
474 *text*) text ;;
475 esac
476 which $DIALOG || live
477 dmesg > /tmp/dmesg
479 while true; do
480 exec 3>&1
481 value=`$DIALOG --clear \
482 --title " Welcome to SliTaz " \
483 --menu "\nPlease select" 19 70 11 \
484 "live" "SliTaz RAM boot" \
485 "text" "SliTaz RAM boot (text mode only)" \
486 $(cdfile README "readme" "Show the README file") \
487 $(cdfile md5sum "md5" "Check ISO files") \
488 "install" "Hard disk installation" \
489 "usbkey" "USB key read/write installation" \
490 "usbbootkey" "USB boot key (read only)" \
491 "fdbootstrap" "Floppy bootstrap" \
492 "tazboot" "Get tazboot.exe Linux loader" \
493 $(cdfile Xboot/memtest "memtest" "Get Memtest86") \
494 $(cdfile boot/memtest "fdmemtest" "Create a Memtest86 boot floppy") \
495 $(cdfile Xboot/gpxe "gpxe" "Get SliTaz Web boot utility") \
496 $(cdfile boot/gpxe "fdgpxe" "Create a SliTaz Web boot floppy") \
497 "restart" "Restart the computer" \
498 "stop" "Power off" \
499 "bootlog" "Linux boot messages" \
500 "shell" "Shell prompt" \
501 2>&1 1>&3`
502 retval=$?
503 exec 3>&-
504 [ $retval -eq 0 ] || continue
505 $value
506 done