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

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