wok-6.x view syslinux/stuff/iso2exe/init @ rev 13713
syslinux/iso2exe: add loram support
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Tue Dec 18 16:09:07 2012 +0100 (2012-12-18) |
parents | 58b695f73790 |
children | 72b5cd3cb23a |
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 34 /mnt/$ISO) - 0xE0))
21 L=$((0x8000 - $(get 28 /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 28 $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 /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 /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 cat > /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 /mnt/slitaz/boot/tazboot.cmd
156 [ -x /mnt/slitaz/usr/sbin/mount.posixovl ] ||
157 cp $(which mount.posixovl) /mnt/slitaz/usr/sbin
158 ! grep -qs tazboot /mnt/boot.ini && cat >> /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 }
169 install()
170 {
171 $DIALOG --clear \
172 --title " SliTaz UMSDOS like installation " \
173 --yes-label "Install" --yesno \
174 "\nSliTaz will be installed in the subdirectory \\slitaz of the current
175 DOS/Windows partition. You will see your files from /mnt/dos.\n\n
176 You can start SliTaz with \\slitaz\\boot\\tazboot.exe\n\n
177 To uninstall SliTaz, you have only to remove this directory.
178 The file \\boot.ini or \\config.sys may be modified too.\n\n
179 To do a traditional installation with disk partitioning,
180 start SliTaz Live with 'SliTaz RAM boot' menu.\n
181 " 16 70
182 [ $? -eq 0 -a -n "$(which mount.posixovl)" ] || return
183 doinstall
184 umount /proc
185 exec chroot /mnt/slitaz /sbin/init
186 }
188 tazboot()
189 {
190 $DIALOG --clear \
191 --title " SliTaz bootloader for DOS " \
192 --yes-label "Install" --yesno \
193 "\nThe file TAZBOOT.EXE will be created in the top directory. It supports
194 a bzImage linux kernel, multiple initramfs, a kernel command line and
195 an ISO image file loopback (retrieves files from an ISO file).\n\n
196 Usage: tazboot.exe [[@commands]|[kernel=<bzimage>]
197 [initrd=<rootfs>[,<rootfs2>...]] [iso=<isofile>] cmdline args ...]\n\n
198 Defaults: tazboot @tazboot.cmd or tazboot kernel=bzImage auto\n\n\
199 Examples for tazboot.cmd:\n\n\
200 iso=\\isos\\slitaz-4.0.iso\n\
201 kernel=boot/bzImage\n\
202 initrd=boot/rootfs4.gz,boot/rootfs3.gz,boot/rootfs2.gz,boot/rootfs1.gz\n\
203 rw root=/dev/null autologin\n\n\
204 kernel=\\slitaz\\vmlinuz root=/dev/sda5 ro\n\n
205 Unlike GRUB4DOS, it doesn't require unfragmented ISO image files.\n
206 " 24 78
207 [ $? -eq 0 ] || return
208 gettazboot /mnt/tazboot.exe
209 }
211 md5()
212 {
213 echo "Checking files..."
214 ( cd /media/cdrom ; md5sum -c md5sum ) > /tmp/data
215 $DIALOG --clear \
216 --title " Checked files " \
217 --textbox /tmp/data 24 78
218 rm -f /tmp/data
219 }
221 readme()
222 {
223 $DIALOG --clear \
224 --title " Readme " \
225 --textbox /media/cdrom/README 24 78
226 }
228 bootlog()
229 {
230 $DIALOG --clear \
231 --title " Linux boot messages " \
232 --textbox /tmp/dmesg 24 78
233 }
235 memtest()
236 {
237 $DIALOG --clear \
238 --title " Memtest86 " \
239 --yes-label "Install" --yesno \
240 "\nMemtest86 is a thorough, stand alone memory test for x86 architecture
241 computers. BIOS based memory tests are a quick, cursory check and often
242 miss many of the failures that are detected by Memtest86.\n
243 " 12 70
244 [ $? -eq 0 ] || return
245 cp /media/cdrom/boot/memtest /mnt
246 }
248 fdmemtest()
249 {
250 $DIALOG --clear \
251 --title " Create a Memtest86 boot floppy " \
252 --yes-label "Create floppy" --yesno \
253 "\nMemtest86 is a thorough, stand alone memory test for x86 architecture
254 computers. BIOS based memory tests are a quick, cursory check and often
255 miss many of the failures that are detected by Memtest86.\n\n
256 Please insert a blank disk in floppy drive.\n
257 " 12 70
258 [ $? -eq 0 ] || return
259 dd if=/media/cdrom/boot/memtest of=/dev/fd0
260 }
262 gpxe()
263 {
264 $DIALOG --clear \
265 --title " SliTaz Web boot " \
266 --yes-label "Install" --yesno \
267 "\nBoot your operating system from the internet and enjoy a full system
268 working entirely in RAM with speed and stability in mind. The Linux Kernel
269 and the complete SliTaz compressed root filesystem will be loaded into RAM
270 from the Web using PXE and HTTP protocols.\n
271 " 12 70
272 [ $? -eq 0 ] || return
273 cp /media/cdrom/boot/gpxe /mnt
274 }
276 fdgpxe()
277 {
278 $DIALOG --clear \
279 --title " Create a SliTaz Web boot floppy " \
280 --yes-label "Create floppy" --yesno \
281 "\nBoot your operating system from the internet and enjoy a full system
282 working entirely in RAM with speed and stability in mind. The Linux Kernel
283 and the complete SliTaz compressed root filesystem will be loaded into RAM
284 from the Web using PXE and HTTP protocols.\n\n
285 Please insert a blank disk in floppy drive.\n
286 " 12 70
287 [ $? -eq 0 ] || return
288 dd if=/media/cdrom/boot/gpxe of=/dev/fd0
289 }
291 cdfile()
292 {
293 [ -s /media/cdrom/$1 ] && echo -en "$2 ${3// /.}"
294 }
296 usbkey()
297 {
298 $DIALOG --clear \
299 --title " Create a SliTaz USB key " \
300 --yes-label "Continue" --yesno \
301 "\nUnlike a hard drive install, the filesystem is kept in a compressed
302 rootfs.gz. The filesystem is loaded entirely into memory upon boot.
303 This should increase responsiveness, protect the filesystem against
304 accidental corruption and reduce read/writes to the USB drive.
305 Once setup, the tazusb utility can rewrite the root filesystem
306 with any changes you have made since booting up,
307 giving the effective benefits of a hard drive install.\n\n
308 /home is mounted on boot using the UUID of your particular flash drive.
309 Unlike a device name, the UUID has the benefit of never changing from machine
310 to machine.\n\n
311 Please plug your USB stick in now.\n
312 " 18 70
313 [ $? -eq 0 ] || return
314 sleep 5
315 DEV="$(grep -l 1 /sys/block/*/removable | \
316 sed 's|/sys/block/\(.*\)/removable|\1|')"
317 [ -n "$DEV" ] || return
318 exec 3>&1
319 device=`$DIALOG --clear \
320 --title " Select your USB key " \
321 --menu "\nPlease select the USB key according to its known size.\n\n" \
322 14 70 4 \
323 $(for i in $DEV ; do
324 echo "/dev/$i $(( $(cat /sys/block/$i/size) / 1024 ))MB"
325 done) \
326 2>&1 1>&3`
327 retval=$?
328 exec 3>&-
329 [ $retval -eq 0 ] || continue
330 exec 3>&1
331 format=`$DIALOG --clear \
332 --title " Select the filesystem " \
333 --menu "\nPlease select the filesystem type to create.\n\n\
334 The filesystem creation will erase all the data \
335 in the USB key." 14 70 4 \
336 "none" "Do not erase the USB key" \
337 "ext3" "Ext3 journaling filesystem" \
338 "ext2" "Ext2 filesystem" \
339 "fat32" "Windows FAT32 filesystem" \
340 2>&1 1>&3`
341 retval=$?
342 exec 3>&-
343 [ $retval -eq 0 ] || continue
344 [ "$format" != "none" ] && tazusb format $device "SliTaz" $format
345 tazusb gen-iso2usb /mnt/$ISO $device
346 }
348 mount_loram()
349 {
350 is_loram || return
351 insmod /lib/squashfs.ko* 2> /dev/null
352 if [ -d /media/cdrom/fs ]; then
353 ln -s /media/cdrom/fs /sqfs
354 else
355 mkdir /sqfs
356 mount -o loop,ro -t squashfs /rootfs*.gz /sqfs
357 fi
358 ln -s /sqfs/lib/* lib
359 ln -s /sqfs/usr /sqfs/var /
360 for i in dmesg basename tr od reboot poweroff getty sync ; do
361 ln -s /sqfs/bin/busybox /bin/$i
362 done
363 }
365 umount_loram()
366 {
367 is_loram || return
368 rm /var /usr
369 umount -d /sqfs
370 rmdir /sqfs
371 }
373 text()
374 {
375 umount_loram
376 umount -d /media/cdrom
377 umount /mnt
378 umount /proc
379 exec /init
380 }
382 live()
383 {
384 n=0
385 for i in $(ls -r /media/cdrom/boot/rootfs*); do
386 [ $((n++)) -eq 0 ] || uncpio $i
387 done
388 text
389 }
391 restart()
392 {
393 sync
394 [ ! -L /sqfs ] && umount -d /media/cdrom && umount /mnt
395 reboot -f
396 }
398 stop()
399 {
400 sync
401 [ ! -L /sqfs ] && umount -d /media/cdrom && umount /mnt
402 poweroff -f
403 }
405 shell()
406 {
407 getty -n -l /bin/ash 38400 tty1 || sh
408 }
410 [ -x /usr/sbin/mount.posixovl ] ||
411 mv /bin/mount.posixovl.iso2exe /usr/sbin/mount.posixovl 2> /dev/null ||
412 mv /bin/mount.posixovl.iso2exe /bin/mount.posixovl
413 mount -t proc /proc /proc
414 ISO="$(getarg iso | sed 's/.://;s|\\|/|g')"
415 getiso
416 mount_loram
417 case "$(basename $ISO | tr [A-Z] [a-z])$(getarg mode)" in
418 *install*) install ;;
419 *live*) live ;;
420 *text*) text ;;
421 esac
422 dmesg > /tmp/dmesg
424 while true; do
425 exec 3>&1
426 value=`$DIALOG --clear \
427 --title " Welcome to SliTaz " \
428 --menu "\nPlease select" 17 70 9 \
429 "live" "SliTaz RAM boot" \
430 "text" "SliTaz RAM boot (text mode only)" \
431 $(cdfile README "readme" "Show the README file") \
432 $(cdfile md5sum "md5" "Check ISO files") \
433 "install" "Hard disk installation" \
434 "usbkey" "USB key installation" \
435 "tazboot" "Get tazboot.exe Linux loader" \
436 $(cdfile Xboot/memtest "memtest" "Get Memtest86") \
437 $(cdfile boot/memtest "fdmemtest" "Create a Memtest86 boot floppy") \
438 $(cdfile Xboot/gpxe "gpxe" "Get SliTaz Web boot utility") \
439 $(cdfile boot/gpxe "fdgpxe" "Create a SliTaz Web boot floppy") \
440 "restart" "Restart the computer" \
441 "stop" "Power off" \
442 "bootlog" "Linux boot messages" \
443 "shell" "Shell prompt" \
444 2>&1 1>&3`
445 retval=$?
446 exec 3>&-
447 [ $retval -eq 0 ] || continue
448 $value
449 done