wok-current rev 17451
syslinux/iso2exe: add --undo
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Wed Dec 17 12:25:20 2014 +0100 (2014-12-17) |
parents | 799c2ec31b04 |
children | 2168fa5dee59 |
files | syslinux/stuff/iso2exe/iso2exe.c syslinux/stuff/iso2exe/iso2exe.sh |
line diff
1.1 --- a/syslinux/stuff/iso2exe/iso2exe.c Tue Dec 16 18:59:28 2014 +0100 1.2 +++ b/syslinux/stuff/iso2exe/iso2exe.c Wed Dec 17 12:25:20 2014 +0100 1.3 @@ -1,6 +1,11 @@ 1.4 +#ifdef __TURBOC__ 1.5 +#include <io.h> 1.6 +#endif 1.7 #include <sys/types.h> 1.8 #include <fcntl.h> 1.9 #include <stdio.h> 1.10 +#include <stdlib.h> 1.11 +#include <string.h> 1.12 #ifdef WIN32 1.13 #include <windows.h> 1.14 #endif 1.15 @@ -10,109 +15,111 @@ 1.16 static char tazlitoinfo[10*1024]; 1.17 #define buffer tazlitoinfo 1.18 #define BUFFERSZ 2048 1.19 +#define WORD(n) * (unsigned short *) (n) 1.20 +#define LONG(n) * (unsigned long *) (n) 1.21 1.22 static void readsector(unsigned long sector) 1.23 { 1.24 if (lseek(fd, sector * BUFFERSZ, SEEK_SET) == -1 || 1.25 read(fd, buffer, BUFFERSZ) != BUFFERSZ) { 1.26 - puts("Read sector failure.\n"); 1.27 + puts(bootiso+READSECTORERR); 1.28 exit(1); 1.29 } 1.30 } 1.31 1.32 -static char *install(char *filename) 1.33 +static unsigned install(char *filename) 1.34 { 1.35 #define heads 64 1.36 #define sectors 32 1.37 #define partition 446 1.38 -#define trksz (512 * heads * sectors) 1.39 +#define trksz (512UL * heads * sectors) 1.40 unsigned long size, catalog, lba; 1.41 int cylinders, i, j, isohybrid; 1.42 unsigned n; 1.43 #ifndef WIN32 1.44 - char *bootiso; 1.45 for (bootiso = (char *) install; 1.46 - bootiso[0] != 'M' || bootiso[1] != 'Z' || bootiso[2] != 0xEB; 1.47 - bootiso++) if (bootiso < (char *) install) return "No bootiso data.\n"; 1.48 + bootiso[0] != 'M' || bootiso[1] != 'Z' || bootiso[2] != '\xEB'; 1.49 + bootiso++) if (bootiso < (char *) install) { 1.50 + bootiso = "No bootiso data"; 1.51 + return 0; 1.52 + } 1.53 #endif 1.54 if (!filename) 1.55 - return "Usage: isohybrid.exe file.iso [--forced]\n"; 1.56 + return USAGE; 1.57 fd = open(filename,O_RDWR|O_BINARY); 1.58 if (fd == -1) 1.59 - return "Can't open rw the iso file.\n"; 1.60 + return OPENERR; 1.61 1.62 if (forced == 0) { 1.63 status = 2; 1.64 - // Install hybridiso boot sector 1.65 + /* Install hybridiso boot sector */ 1.66 readsector(17UL); 1.67 - if (strncmp(buffer+7, "EL TORITO SPECIFICATION", 23)) 1.68 - return "No EL TORITO signature.\n"; 1.69 - catalog = * (unsigned long *) (buffer + 71); 1.70 + if (strncmp(buffer+7, bootiso+ELTORITOERR+ELTORITOOFS, 23)) 1.71 + return ELTORITOERR; 1.72 + catalog = LONG(buffer + 71); 1.73 readsector(catalog); 1.74 - if (* (unsigned long *) buffer != 1 || 1.75 - * (unsigned long *) (buffer + 30) != 0x88AA55) 1.76 - return "Invalid boot catalog.\n"; 1.77 - lba = * (unsigned long *) (buffer + 40); 1.78 + if (LONG(buffer) != 1 || LONG(buffer + 30) != 0x88AA55UL) 1.79 + return CATALOGERR; 1.80 + lba = LONG(buffer + 40); 1.81 readsector(lba); 1.82 - if (* (unsigned long *) (buffer + 64) != 1886961915) 1.83 - return "No isolinux.bin hybrid signature.\n"; 1.84 + if (LONG(buffer + 64) != 1886961915UL) 1.85 + return HYBRIDERR; 1.86 isohybrid = bootiso[69] * 512; 1.87 - * (unsigned long *) &bootiso[isohybrid + 432] = lba * 4; 1.88 - * (unsigned long *) &bootiso[isohybrid + 440] = rand(); 1.89 - * (unsigned long *) &bootiso[isohybrid + partition] = 0x10080; 1.90 - * (unsigned short *) &bootiso[isohybrid + 510] = 0xAA55; 1.91 + LONG(bootiso + isohybrid + 432) = lba * 4; 1.92 + LONG(bootiso + isohybrid + 440) = rand(); 1.93 + LONG(bootiso + isohybrid + partition) = 0x10080UL; 1.94 + WORD(bootiso + isohybrid + 510) = 0xAA55U; 1.95 size = lseek(fd, 0UL, SEEK_END); 1.96 cylinders = (size + trksz - 1) / trksz; 1.97 - bootiso[isohybrid + partition + 4] = 23; // "Windows hidden IFS" 1.98 + bootiso[isohybrid + partition + 4] = 23; /* "Windows hidden IFS" */ 1.99 bootiso[isohybrid + partition + 5] = heads - 1; 1.100 bootiso[isohybrid + partition + 6] = (((cylinders - 1) & 0x300) >> 2) + sectors; 1.101 bootiso[isohybrid + partition + 7] = (cylinders - 1) & 0xFF; 1.102 - * (unsigned long *) &bootiso[isohybrid + partition + 8] = 0; 1.103 - * (unsigned long *) &bootiso[isohybrid + partition + 12] = cylinders * sectors * heads; 1.104 + LONG(bootiso + isohybrid + partition + 8) = 0; 1.105 + LONG(bootiso + isohybrid + partition + 12) = cylinders * sectors * heads; 1.106 1.107 - // Copy the partition table 1.108 + /* Copy the partition table */ 1.109 memcpy(bootiso + 0x1BE, bootiso + isohybrid + 0x1BE, 66); 1.110 } 1.111 1.112 - // Install iso2exe boot sector 1.113 - * (unsigned short *) (bootiso + 26) = rand(); 1.114 + /* Install iso2exe boot sector */ 1.115 + WORD(bootiso + 26) = rand(); 1.116 1.117 - // read tazlito flavor data 1.118 + /* read tazlito flavor data */ 1.119 lseek(fd, 1024UL, SEEK_SET); 1.120 read(fd, tazlitoinfo, sizeof(tazlitoinfo)); 1.121 1.122 - // Update iso image 1.123 + /* Update iso image */ 1.124 n = (bootiso[69] + 1) * 512; 1.125 lseek(fd, 0UL, SEEK_SET); 1.126 - write(fd, bootiso, n); // EXE/PE + isohybrid mbr 1.127 + write(fd, bootiso, n); /* EXE/PE + isohybrid mbr */ 1.128 write(fd, tazlitoinfo, ((0x8000U - BOOTISOSZ) > sizeof(tazlitoinfo)) 1.129 ? sizeof(tazlitoinfo) : (0x8000U - BOOTISOSZ)); 1.130 - write(fd, bootiso + n, BOOTISOSZ - n); // COM + rootfs + EXE/DOS 1.131 + write(fd, bootiso + n, BOOTISOSZ - n); /* COM + rootfs + EXE/DOS */ 1.132 1.133 - // Compute the checksum 1.134 + /* Compute the checksum */ 1.135 lseek(fd, 0UL, SEEK_SET); 1.136 for (i = 66, n = 0, j = 0; j < 16; j++, i = 0) { 1.137 if (read(fd, buffer, BUFFERSZ) != BUFFERSZ) 1.138 goto nochksum; 1.139 for (; i < BUFFERSZ; i += 2) 1.140 - n += * (unsigned short *) (buffer + i); 1.141 + n += WORD(buffer + i); 1.142 } 1.143 - * (unsigned short *) (bootiso + 64) = -n; 1.144 + WORD(bootiso + 64) = -n; 1.145 lseek(fd, 0UL, SEEK_SET); 1.146 write(fd, bootiso, 512); 1.147 nochksum: 1.148 close(fd); 1.149 status = 0; 1.150 - return "Now you can create a USB key with your .iso file.\n" 1.151 - "Simply rename it to a .exe file and run it.\n"; 1.152 + return SUCCESSMSG; 1.153 } 1.154 1.155 int main(int argc, char *argv[]) 1.156 { 1.157 forced = (argc > 2); 1.158 - puts(install(argv[1])); 1.159 + puts(bootiso + install(argv[1])); 1.160 if (status > 1) 1.161 - puts("You can add --forced to proceed anayway"); 1.162 + puts(bootiso + FORCEMSG); 1.163 #ifdef WIN32 1.164 Sleep(2000); 1.165 #endif
2.1 --- a/syslinux/stuff/iso2exe/iso2exe.sh Tue Dec 16 18:59:28 2014 +0100 2.2 +++ b/syslinux/stuff/iso2exe/iso2exe.sh Wed Dec 17 12:25:20 2014 +0100 2.3 @@ -114,17 +114,48 @@ 2.4 add_doscom $DATA > /dev/null 2.5 add_fdbootstrap $DATA > /dev/null 2.6 name=${3:-bootiso} 2.7 + BOOTISOSZ=$((0x8000 - $OFS + $HSZ)) 2.8 cat <<EOT 2.9 2.10 -#define $(echo $name | tr '[a-z]' '[A-Z]')SZ $((0x8000 - $OFS + $HSZ)) 2.11 +#define $(echo $name | tr '[a-z]' '[A-Z]')SZ $BOOTISOSZ 2.12 2.13 #ifdef WIN32 2.14 static char $name[] = { 2.15 $(hexdump -v -n $HSZ -e '" " 16/1 "0x%02X, "' -e '" // %04.4_ax |" 16/1 "%_p" "| \n"' $DATA | sed 's/ 0x ,/ /g') 2.16 $(hexdump -v -s $OFS -e '" " 16/1 "0x%02X, "' -e '" // %04.4_ax |" 16/1 "%_p" "| \n"' $DATA | sed 's/ 0x ,/ /g') 2.17 +EOT 2.18 + 2.19 +for mode in data offset ; do 2.20 + ofs=0 2.21 + while read tag str; do 2.22 + if [ "$mode" == "data" ]; then 2.23 + echo -en "$str\0" | hexdump -v -e '" " 16/1 "0x%02X, "' \ 2.24 + -e '" // %04.4_ax |" 16/1 "%_p" "| \n"' | \ 2.25 + sed 's/ 0x ,/ /g' 2.26 + else 2.27 + if [ $ofs -eq 0 ]; then 2.28 + cat <<EOT 2.29 }; 2.30 +#else 2.31 +static char *$name; 2.32 #endif 2.33 +#define ELTORITOOFS 3 2.34 EOT 2.35 + fi 2.36 + echo "#define $tag $(($BOOTISOSZ+$ofs))" 2.37 + ofs=$(($(echo -en "$str\0" | wc -c)+$ofs)) 2.38 + fi 2.39 + done <<EOT 2.40 +READSECTORERR Read sector failure. 2.41 +USAGE Usage: isohybrid.exe file.iso [--forced] 2.42 +OPENERR Can't open r/w the iso file. 2.43 +ELTORITOERR No EL TORITO SPECIFICATION signature. 2.44 +CATALOGERR Invalid boot catalog. 2.45 +HYBRIDERR No isolinux.bin hybrid signature. 2.46 +SUCCESSMSG Now you can create a USB key with your .iso file.\\\\nSimply rename it to a .exe file and run it. 2.47 +FORCEMSG You can add --forced to proceed anyway. 2.48 +EOT 2.49 +done 2.50 rm -rf $DATA 2.51 exit ;; 2.52 --exe) 2.53 @@ -148,14 +179,24 @@ 2.54 main() 2.55 { 2.56 [ $(id -u) -ne 0 ] && exec su -c "$0 $@" < /dev/tty 2.57 - case "$1" in 2.58 - --get) shift 2.59 + case "${1/--/-}" in 2.60 + -get) shift 2.61 uudecode | unlzma | tar xOf - $@ 2.62 exit ;; 2.63 *) cat > /dev/null 2.64 esac 2.65 2.66 - [ ! -s "$1" ] && echo "usage: $0 image.iso" 1>&2 && exit 1 2.67 + [ ! -s "$1" ] && echo "usage: $0 image.iso [--undo]" 1>&2 && exit 1 2.68 + case "${2/--/-}" in 2.69 + -u*|-r*|-w*) 2.70 + case "$(get 0 $1)" in 2.71 + 23117) 2.72 + ddq if=$1 bs=512 count=2 skip=$(get 69 $1 1) of=$1 conv=notrunc 2.73 + ddq if=/dev/zero bs=1k seek=1 count=31 of=$1 conv=notrunc ;; 2.74 + *) ddq if=/dev/zero bs=1k count=32 of=$1 conv=notrunc ;; 2.75 + esac 2.76 + exit 0 2.77 + esac 2.78 case "$(get 0 $1)" in 2.79 23117) echo "The file $1 is already an EXE file." 1>&2 && exit 1;; 2.80 0) [ -x /usr/bin/isohybrid ] && isohybrid $1 && echo "Do isohybrid"