wok-current diff syslinux/stuff/iso2exe/boot.c @ rev 13691
syslinux: add iso2exe
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Thu Dec 13 14:33:27 2012 +0100 (2012-12-13) |
parents | |
children | d47403fdd900 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/syslinux/stuff/iso2exe/boot.c Thu Dec 13 14:33:27 2012 +0100 1.3 @@ -0,0 +1,162 @@ 1.4 +#include <asm/limits.h> 1.5 +#include <sys/types.h> 1.6 +#include <unistd.h> 1.7 +#include <fcntl.h> 1.8 +#include <stdio.h> 1.9 +#include "iso9660.h" 1.10 +#include "bootlinux.h" 1.11 +#include "libdos.h" 1.12 + 1.13 +static void usage(char *iso) 1.14 +{ 1.15 + printf("Usage: %s [[@commands]|[kernel=<bzimage>] \ 1.16 +[initrd=<rootfs>[,<rootfs2>...]] [iso=<isofile>] ...]\n\n\ 1.17 +Defaults: %s @tazboot.cmd or %s kernel=bzImage auto\n\n\ 1.18 +Examples for tazboot.cmd:\n\n\ 1.19 + iso=\\isos\\slitaz-4.0.iso\n\ 1.20 + kernel=boot/bzImage\n\ 1.21 + initrd=boot/rootfs4.gz,boot/rootfs3.gz,boot/rootfs2.gz,boot/rootfs1.gz\n\ 1.22 + rw root=/dev/null vga=normal autologin\n\n\ 1.23 + kernel=\\slitaz\\vmlinuz\n\ 1.24 + root=/dev/sda5 ro\n",iso,iso,iso); 1.25 + exit(1); 1.26 +} 1.27 + 1.28 +static void bootiso(char **iso) 1.29 +{ 1.30 + char *init = "rdinit=/init.exe", *mode="menu"; 1.31 + char c, *s, rootfs[16], cmdline[256]; 1.32 + int fd, restart; 1.33 + unsigned long magic; 1.34 + 1.35 + if (isoreset(*iso) || isoopen("boot")) return; 1.36 + if (iso[1] && !strcmp(mode = iso[1], "text")) 1.37 + init = ""; 1.38 + for (c = 0, restart = 1; isoreaddir(restart) == 0; restart = 0) { 1.39 + if (strncmp(isofilename, "rootfs", 6) || c > s[6]) continue; 1.40 + strcpy(rootfs, isofilename); 1.41 + c = s[6]; 1.42 + } 1.43 + if (isoopen(mode)) 1.44 + isoopen("bzImage"); 1.45 + loadkernel(); 1.46 + isoopen(rootfs); 1.47 + loadinitrd(); 1.48 + lseek(isofd, 28, SEEK_SET); 1.49 + read(isofd, &magic, 4); 1.50 + isofilesize = magic & 0xFFFF; 1.51 + isofileofs = 0x8000 - isofilesize; 1.52 + loadinitrd(); 1.53 + close(isofd); 1.54 + sprintf(cmdline,"rw root=/dev/null %s iso=%s magic=%lu mode=%s", 1.55 + init, *iso, magic, mode); 1.56 + bootlinux(cmdline); 1.57 +} 1.58 + 1.59 +static int stricmp(char *ref, char *s) 1.60 +{ 1.61 + char c; 1.62 + while (*ref) { 1.63 + c = *s++; 1.64 + if (c >= 'A' && c <= 'Z') c += 'a' - 'A'; 1.65 + c -= *ref++; 1.66 + if (c) return c; 1.67 + } 1.68 + return 0; 1.69 +} 1.70 + 1.71 +static char *iso; 1.72 +static int fakeopen(char *file) 1.73 +{ 1.74 + if (iso) { 1.75 + isoreset(iso); 1.76 + return isoopen(file); 1.77 + } 1.78 + close(isofd); 1.79 + isofd = open(file, O_RDONLY); 1.80 + if (isofd != -1) { 1.81 + isofileofs = 0; 1.82 + isofilesize = LONG_MAX; 1.83 + } 1.84 + return isofd; 1.85 +} 1.86 + 1.87 +static char args[2048]; 1.88 +int main(int argc, char *argv[]) 1.89 +{ 1.90 + char *kernel, *initrd, *cmdline, *cmdfile, *s; 1.91 + 1.92 + argv[0] = progname(); 1.93 + bootiso(argv); // iso ? parsing is /init.exe stuff ! 1.94 + 1.95 + chdirname(*argv); 1.96 + cmdfile = "tazboot.cmd"; 1.97 + kernel = "bzImage"; 1.98 + initrd = NULL; 1.99 + cmdline = "auto"; 1.100 + if (argc > 1) { 1.101 + if (argv[1][0] == '@') 1.102 + cmdfile = argv[1] + 1; 1.103 + else { 1.104 + cmdfile = NULL; 1.105 +#asm 1.106 + push ds 1.107 + push ds 1.108 + pop es 1.109 + push cs 1.110 + pop ds 1.111 + mov si, #0x82 1.112 + mov di, #_args 1.113 + mov cx, #0x7E/2 1.114 + rep 1.115 + movsw 1.116 + pop ds 1.117 +#endasm 1.118 + } 1.119 + } 1.120 + if (cmdfile) { 1.121 + int fd; 1.122 + fd = open(cmdfile, O_RDONLY);; 1.123 + if (fd != -1) { 1.124 + read(fd, args, sizeof(args)); 1.125 + close(fd); 1.126 + for (s = args; s < args + sizeof(args) -1; s++) { 1.127 + if (*s == '\r') *s++ = ' '; 1.128 + if (*s == '\n') *s = ' '; 1.129 + } 1.130 + } 1.131 + } 1.132 + for (s = args; s < args + sizeof(args); s++) { 1.133 + if (*s == ' ') continue; 1.134 + if (stricmp("kernel=", s) == 0) 1.135 + kernel = s + 7; 1.136 + else if (stricmp("initrd=", s) == 0) 1.137 + initrd = s + 7; 1.138 + else if (stricmp("iso=", s) == 0) 1.139 + iso = s + 4; 1.140 + else { 1.141 + cmdline = s; 1.142 + break; 1.143 + } 1.144 + while (*s && *s != ' ') s++; 1.145 + *s = 0; 1.146 + } 1.147 + if (fakeopen(kernel) == -1) 1.148 + usage(argv[0]); 1.149 + loadkernel(); 1.150 + if (initrd) { 1.151 + char *p, *q = initrd; 1.152 + while (1) { 1.153 + char c; 1.154 + for (p = q; *p && *p != ','; p++); 1.155 + c = *p; 1.156 + *p = 0; 1.157 + if (fakeopen(q) != -1) 1.158 + loadinitrd(); 1.159 + if (c == 0) 1.160 + break; 1.161 + q = ++p; 1.162 + } 1.163 + } 1.164 + bootlinux(cmdline); 1.165 +}