wok diff syslinux/stuff/iso2exe/iso2exe.c @ rev 14150

syslinux: add isohybrid.exe
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Mar 04 09:52:12 2013 +0100 (2013-03-04)
parents
children ab3f0098073a
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/syslinux/stuff/iso2exe/iso2exe.c	Mon Mar 04 09:52:12 2013 +0100
     1.3 @@ -0,0 +1,92 @@
     1.4 +#include <sys/types.h>
     1.5 +#include <fcntl.h>
     1.6 +#include <stdio.h>
     1.7 +#include "iso2exe.h"
     1.8 +
     1.9 +static int fd;
    1.10 +static char buffer[2048];
    1.11 +
    1.12 +static void quit(char *msg)
    1.13 +{
    1.14 +	fprintf(stderr,"%s.\n", msg);
    1.15 +	exit(1);
    1.16 +}
    1.17 +
    1.18 +static void readsector(unsigned long sector)
    1.19 +{
    1.20 +	if (lseek(fd, sector * sizeof(buffer), SEEK_SET) == -1 ||
    1.21 +	    read(fd, buffer, sizeof(buffer)) != sizeof(buffer))
    1.22 +		quit("read sector failure");
    1.23 +}
    1.24 +
    1.25 +int main(int argc, char *argv[])
    1.26 +{
    1.27 +#define heads 64
    1.28 +#define sectors 32
    1.29 +#define partition 446
    1.30 +#define trksz (512 * heads * sectors)
    1.31 +	unsigned long size, catalog, lba;
    1.32 +	int cylinders, i, j;
    1.33 +	unsigned n;
    1.34 +#ifndef WIN32
    1.35 +	char *bootiso;
    1.36 +	for (bootiso = (char *) main;
    1.37 +	     bootiso[0] != 'M' || bootiso[1] != 'Z' || bootiso[2] != 0xEB;
    1.38 +	     bootiso++) if (bootiso < (char *) main) quit("bootiso not found");
    1.39 +#endif
    1.40 +	if (argc < 2)
    1.41 +		quit("Usage : isohybrid.exe file.iso");
    1.42 +	fd = open(argv[1],O_RDWR|O_BINARY);
    1.43 +	if (fd == -1)
    1.44 +		quit("Can't open rw");
    1.45 +
    1.46 +	// Install hybridiso boot sector
    1.47 +	readsector(17UL);
    1.48 +	if (strncmp(buffer+7, "EL TORITO SPECIFICATION", 23))
    1.49 +		quit("No EL TORITO boot record found");
    1.50 +	catalog = * (unsigned long *) (buffer + 71);
    1.51 +	readsector(catalog);
    1.52 +	if (* (unsigned long *) buffer != 1 || 
    1.53 +	    * (unsigned long *) (buffer + 30) != 0x88AA55)
    1.54 +	    	quit("invalid boot catalog.");
    1.55 +	lba = * (unsigned long *) (buffer + 40);
    1.56 +	readsector(lba);
    1.57 +	if (* (unsigned long *) (buffer + 64) != 1886961915)
    1.58 +		quit("no isolinux.bin hybrid signature in bootloader");
    1.59 +	* (unsigned long *)  &bootiso[512 + 432] = lba * 4;
    1.60 +	* (unsigned long *)  &bootiso[512 + 440] = rand();
    1.61 +	* (unsigned long *)  &bootiso[512 + partition] = 0x10080;
    1.62 +	* (unsigned short *) &bootiso[512 + 510] = 0xAA55;
    1.63 +	size = lseek(fd, 0, SEEK_END);
    1.64 +	cylinders = (size + trksz - 1) / trksz;
    1.65 +	bootiso[512 + partition + 4] = 23; // "Windows hidden IFS"
    1.66 +	bootiso[512 + partition + 5] = heads - 1;
    1.67 +	bootiso[512 + partition + 6] = (((cylinders - 1) & 0x300) >> 2) + sectors;
    1.68 +	bootiso[512 + partition + 7] = (cylinders - 1) & 0xFF;
    1.69 +	* (unsigned long *) &bootiso[512 + partition + 8] = 0;
    1.70 +	* (unsigned long *) &bootiso[512 + partition + 12] = cylinders * sectors * heads;
    1.71 +
    1.72 +	// Install iso2exe boot sector
    1.73 +	memcpy(bootiso + 512 - 66, bootiso + 1024 - 66, 66); 
    1.74 +	* (unsigned short *) (bootiso + 26) = rand();
    1.75 +
    1.76 +	// Update iso image
    1.77 +	lseek(fd, 0, SEEK_SET);
    1.78 +	write(fd, bootiso, 1024);
    1.79 +	lseek(fd, 0x8400 - BOOTISOSZ, SEEK_SET);
    1.80 +	write(fd, bootiso + 1024, BOOTISOSZ - 1024);
    1.81 +
    1.82 +	// Compute the checksum
    1.83 +	lseek(fd, 0, SEEK_SET);
    1.84 +	for (i = 66, n = 0, j = 0; j < 16; j++, i = 0) {
    1.85 +		if (read(fd, buffer, sizeof(buffer)) != sizeof(buffer))
    1.86 +			goto nochksum;
    1.87 +		for (; i < sizeof(buffer); i += 2)
    1.88 +			n += * (unsigned short *) (buffer + i);
    1.89 +	}
    1.90 +	* (unsigned short *) (bootiso + 64) = -n;
    1.91 +	lseek(fd, 0, SEEK_SET);
    1.92 +	write(fd, bootiso, 512);
    1.93 +nochksum:
    1.94 +	close(fd);
    1.95 +}