wok-6.x view syslinux/stuff/iso2exe/boot.c @ rev 14264
syslinux/iso2exe: fix initrd offset
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Mon Apr 01 09:45:40 2013 +0200 (2013-04-01) |
parents | 65b3fd0022ed |
children | 19258b949c1b |
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 if (loadkernel() < 0x20630)
43 init = ""; // Does not support multiple initramfs
44 isoopen(rootfs);
45 loadinitrd();
46 lseek(isofd, 24, SEEK_SET);
47 read(isofd, &magic, 4);
48 isofilesize = magic & 0xFFFF;
49 isofileofs = 0x7EE0 - isofilesize;
50 loadinitrd();
51 close(isofd);
52 sprintf(cmdline,"rw root=/dev/null %s iso=%s magic=%lu mode=%s",
53 init, *iso, magic, mode);
54 bootlinux(cmdline);
55 }
57 static int stricmp(char *ref, char *s)
58 {
59 char c;
60 while (*ref) {
61 c = *s++;
62 if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
63 c -= *ref++;
64 if (c) return c;
65 }
66 return 0;
67 }
69 static char *iso;
70 static int fakeopen(char *file)
71 {
72 if (iso) {
73 isoreset(iso);
74 return isoopen(file);
75 }
76 close(isofd);
77 isofd = open(file, O_RDONLY);
78 if (isofd != -1) {
79 isofileofs = 0;
80 isofilesize = LONG_MAX;
81 }
82 return isofd;
83 }
85 static char args[2048];
86 int main(int argc, char *argv[])
87 {
88 char *kernel, *initrd, *cmdline, *cmdfile, *s;
90 argv[0] = progname();
91 bootiso(argv + (argc == 2)); // iso ? parsing is /init.exe stuff !
93 chdirname(*argv);
94 cmdfile = "tazboot.cmd";
95 kernel = "bzImage";
96 initrd = NULL;
97 cmdline = "auto";
98 if (argc > 1) {
99 if (argv[1][0] == '@')
100 cmdfile = argv[1] + 1;
101 else {
102 cmdfile = NULL;
103 #asm
104 push ds
105 pop es
106 mov si, #0x82
107 mov di, #_args
108 mov cx, #0x7E/2
109 rep
110 seg cs
111 movsw
112 #endasm
113 }
114 }
115 if (cmdfile) {
116 int fd;
117 fd = open(cmdfile, O_RDONLY);;
118 if (fd != -1) {
119 read(fd, args, sizeof(args));
120 close(fd);
121 for (s = args; s < args + sizeof(args) -1; s++) {
122 if (*s == '\r') *s++ = ' ';
123 if (*s == '\n') *s = ' ';
124 }
125 }
126 }
127 for (s = args; s < args + sizeof(args); s++) {
128 if (*s == ' ') continue;
129 if (stricmp("kernel=", s) == 0)
130 kernel = s + 7;
131 else if (stricmp("initrd=", s) == 0)
132 initrd = s + 7;
133 else if (stricmp("iso=", s) == 0)
134 iso = s + 4;
135 else {
136 cmdline = s;
137 break;
138 }
139 while (*s && *s != ' ') s++;
140 *s = 0;
141 }
142 if (fakeopen(kernel) == -1)
143 usage(argv[0]);
144 loadkernel();
145 if (initrd) {
146 char *p, *q = initrd;
147 while (1) {
148 char c;
149 for (p = q; *p && *p != ','; p++);
150 c = *p;
151 *p = 0;
152 if (fakeopen(q) != -1)
153 loadinitrd();
154 if (c == 0)
155 break;
156 q = ++p;
157 }
158 }
159 bootlinux(cmdline);
160 }