wok view syslinux/stuff/iso2exe/boot.c @ rev 17160

syslinux/iso2exe: full zImage support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Sep 17 11:38:34 2014 +0200 (2014-09-17)
parents d814ca840c3c
children 054f70cb9bec
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,\\slitaz\\extrafs.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", *fmt="";
28 char c, rootfs[16], cmdline[256];
29 int 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)
37 || c > isofilename[6]) continue;
38 strcpy(rootfs, isofilename);
39 c = isofilename[6];
40 }
41 if (isoopen(mode))
42 isoopen("bzImage");
43 magic = loadkernel();
44 if (magic < 0x20630)
45 init = ""; // Does not support multiple initramfs
46 if (magic > 0) {
47 fmt = "rw root=/dev/null%s iso=%s magic=%lu mode=%s autologin";
48 if (rootfs[6] != '.' && !isoopen("rootfs.gz"))
49 loadinitrd(); // for loram
50 isoopen(rootfs);
51 loadinitrd();
52 if (*init) {
53 lseek(isofd, 24L, SEEK_SET);
54 read(isofd, &magic, 4);
55 isofilesize = magic & 0xFFFFL;
56 isofileofs = 0x7EE0L - isofilesize;
57 if (isofilesize) loadinitrd();
58 else init="";
59 }
60 }
61 sprintf(cmdline, fmt, init, *iso, magic, mode);
62 close(isofd);
63 bootlinux(cmdline);
64 }
66 static int stricmp(char *ref, char *s)
67 {
68 char c;
69 while (*ref) {
70 c = *s++;
71 if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
72 c -= *ref++;
73 if (c) return c;
74 }
75 return 0;
76 }
78 static int chkstatus(int status, char *name)
79 {
80 if (status == -1)
81 printf("%s not found.\n",name);
82 return status;
83 }
85 static char *iso;
86 static int fakeopen(char *file)
87 {
88 if (file) {
89 char *s = file;
90 while (*s && *s != '\r' && *s != '\n') s++;
91 *s = 0;
92 }
93 if (*file == '\\') {
94 static fd = -1;
95 if (fd >= 0) close(fd);
96 fd = chkstatus(open(file, O_RDONLY), file);
97 return fd;
98 }
99 if (iso) {
100 chkstatus(isoreset(iso), iso);
101 return chkstatus(isoopen(file), file);
102 }
103 close(isofd);
104 isofd = chkstatus(open(file, O_RDONLY), file);
105 if (isofd != -1) {
106 isofileofs = 0;
107 isofilesize = LONG_MAX;
108 }
109 return isofd;
110 }
112 static char args[2048];
113 int main(int argc, char *argv[])
114 {
115 char *kernel, *initrd, *cmdline, *cmdfile, *s;
117 argv[0] = progname();
118 bootiso(argv); // iso ? parsing is /init.exe stuff !
119 if (argc >= 2)
120 bootiso(argv + 1);
122 chdirname(*argv);
123 cmdfile = "tazboot.cmd";
124 kernel = "bzImage";
125 initrd = NULL;
126 cmdline = "auto";
127 if (argc > 1) {
128 if (argv[1][0] == '@')
129 cmdfile = argv[1] + 1;
130 else {
131 cmdfile = NULL;
132 #asm
133 push ds
134 pop es
135 mov si, #0x82
136 mov di, #_args
137 mov cx, #0x7E/2
138 rep
139 seg cs
140 movsw
141 #endasm
142 }
143 }
144 if (cmdfile) {
145 int fd;
146 fd = chkstatus(open(cmdfile, O_RDONLY), cmdfile);
147 if (fd != -1) {
148 read(fd, args, sizeof(args));
149 close(fd);
150 for (s = args; s < args + sizeof(args) -1; s++) {
151 if (*s == '\r') *s++ = ' ';
152 if (*s == '\n') *s = ' ';
153 }
154 }
155 }
156 for (s = args; s < args + sizeof(args); s++) {
157 if (*s == ' ') continue;
158 if (stricmp("kernel=", s) == 0)
159 kernel = s + 7;
160 else if (stricmp("initrd=", s) == 0)
161 initrd = s + 7;
162 else if (stricmp("iso=", s) == 0)
163 iso = s + 4;
164 else {
165 cmdline = s;
166 break;
167 }
168 while (*s && *s != ' ') s++;
169 *s = 0;
170 }
171 if (cmdline) {
172 char *last;
173 for (s = cmdline; *s && *s != '\r' && *s != '\n'; s++)
174 if (*s != ' ') last = s;
175 *++last = 0;
176 }
177 if (fakeopen(kernel) == -1)
178 usage(argv[0]);
179 loadkernel();
180 if (initrd) {
181 char *p, *q = initrd;
182 while (1) {
183 char c;
184 for (p = q; *p && *p != ','; p++);
185 c = *p;
186 *p = 0;
187 if (fakeopen(q) != -1)
188 loadinitrd();
189 if (c == 0)
190 break;
191 q = ++p;
192 }
193 }
194 bootlinux(cmdline);
195 }