wok-current view 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 source
1 #include <asm/limits.h>
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include "iso9660.h"
7 #include "bootlinux.h"
8 #include "libdos.h"
10 static void usage(char *iso)
11 {
12 printf("Usage: %s [[@commands]|[kernel=<bzimage>] \
13 [initrd=<rootfs>[,<rootfs2>...]] [iso=<isofile>] ...]\n\n\
14 Defaults: %s @tazboot.cmd or %s kernel=bzImage auto\n\n\
15 Examples for tazboot.cmd:\n\n\
16 iso=\\isos\\slitaz-4.0.iso\n\
17 kernel=boot/bzImage\n\
18 initrd=boot/rootfs4.gz,boot/rootfs3.gz,boot/rootfs2.gz,boot/rootfs1.gz\n\
19 rw root=/dev/null vga=normal autologin\n\n\
20 kernel=\\slitaz\\vmlinuz\n\
21 root=/dev/sda5 ro\n",iso,iso,iso);
22 exit(1);
23 }
25 static void bootiso(char **iso)
26 {
27 char *init = "rdinit=/init.exe", *mode="menu";
28 char c, *s, rootfs[16], cmdline[256];
29 int fd, restart;
30 unsigned long magic;
32 if (isoreset(*iso) || isoopen("boot")) return;
33 if (iso[1] && !strcmp(mode = iso[1], "text"))
34 init = "";
35 for (c = 0, restart = 1; isoreaddir(restart) == 0; restart = 0) {
36 if (strncmp(isofilename, "rootfs", 6) || c > s[6]) continue;
37 strcpy(rootfs, isofilename);
38 c = s[6];
39 }
40 if (isoopen(mode))
41 isoopen("bzImage");
42 loadkernel();
43 isoopen(rootfs);
44 loadinitrd();
45 lseek(isofd, 28, SEEK_SET);
46 read(isofd, &magic, 4);
47 isofilesize = magic & 0xFFFF;
48 isofileofs = 0x8000 - isofilesize;
49 loadinitrd();
50 close(isofd);
51 sprintf(cmdline,"rw root=/dev/null %s iso=%s magic=%lu mode=%s",
52 init, *iso, magic, mode);
53 bootlinux(cmdline);
54 }
56 static int stricmp(char *ref, char *s)
57 {
58 char c;
59 while (*ref) {
60 c = *s++;
61 if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
62 c -= *ref++;
63 if (c) return c;
64 }
65 return 0;
66 }
68 static char *iso;
69 static int fakeopen(char *file)
70 {
71 if (iso) {
72 isoreset(iso);
73 return isoopen(file);
74 }
75 close(isofd);
76 isofd = open(file, O_RDONLY);
77 if (isofd != -1) {
78 isofileofs = 0;
79 isofilesize = LONG_MAX;
80 }
81 return isofd;
82 }
84 static char args[2048];
85 int main(int argc, char *argv[])
86 {
87 char *kernel, *initrd, *cmdline, *cmdfile, *s;
89 argv[0] = progname();
90 bootiso(argv); // iso ? parsing is /init.exe stuff !
92 chdirname(*argv);
93 cmdfile = "tazboot.cmd";
94 kernel = "bzImage";
95 initrd = NULL;
96 cmdline = "auto";
97 if (argc > 1) {
98 if (argv[1][0] == '@')
99 cmdfile = argv[1] + 1;
100 else {
101 cmdfile = NULL;
102 #asm
103 push ds
104 push ds
105 pop es
106 push cs
107 pop ds
108 mov si, #0x82
109 mov di, #_args
110 mov cx, #0x7E/2
111 rep
112 movsw
113 pop ds
114 #endasm
115 }
116 }
117 if (cmdfile) {
118 int fd;
119 fd = open(cmdfile, O_RDONLY);;
120 if (fd != -1) {
121 read(fd, args, sizeof(args));
122 close(fd);
123 for (s = args; s < args + sizeof(args) -1; s++) {
124 if (*s == '\r') *s++ = ' ';
125 if (*s == '\n') *s = ' ';
126 }
127 }
128 }
129 for (s = args; s < args + sizeof(args); s++) {
130 if (*s == ' ') continue;
131 if (stricmp("kernel=", s) == 0)
132 kernel = s + 7;
133 else if (stricmp("initrd=", s) == 0)
134 initrd = s + 7;
135 else if (stricmp("iso=", s) == 0)
136 iso = s + 4;
137 else {
138 cmdline = s;
139 break;
140 }
141 while (*s && *s != ' ') s++;
142 *s = 0;
143 }
144 if (fakeopen(kernel) == -1)
145 usage(argv[0]);
146 loadkernel();
147 if (initrd) {
148 char *p, *q = initrd;
149 while (1) {
150 char c;
151 for (p = q; *p && *p != ','; p++);
152 c = *p;
153 *p = 0;
154 if (fakeopen(q) != -1)
155 loadinitrd();
156 if (c == 0)
157 break;
158 q = ++p;
159 }
160 }
161 bootlinux(cmdline);
162 }