wok-current view syslinux/stuff/iso2exe/boot.c @ rev 13697
syslinux/iso2exe: menu can create usbkey/floppy
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Fri Dec 14 15:43:12 2012 +0100 (2012-12-14) |
parents | 7d300004a3b8 |
children | b5ea41033c21 |
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 pop es
105 mov si, #0x82
106 mov di, #_args
107 mov cx, #0x7E/2
108 rep
109 seg cs
110 movsw
111 #endasm
112 }
113 }
114 if (cmdfile) {
115 int fd;
116 fd = open(cmdfile, O_RDONLY);;
117 if (fd != -1) {
118 read(fd, args, sizeof(args));
119 close(fd);
120 for (s = args; s < args + sizeof(args) -1; s++) {
121 if (*s == '\r') *s++ = ' ';
122 if (*s == '\n') *s = ' ';
123 }
124 }
125 }
126 for (s = args; s < args + sizeof(args); s++) {
127 if (*s == ' ') continue;
128 if (stricmp("kernel=", s) == 0)
129 kernel = s + 7;
130 else if (stricmp("initrd=", s) == 0)
131 initrd = s + 7;
132 else if (stricmp("iso=", s) == 0)
133 iso = s + 4;
134 else {
135 cmdline = s;
136 break;
137 }
138 while (*s && *s != ' ') s++;
139 *s = 0;
140 }
141 if (fakeopen(kernel) == -1)
142 usage(argv[0]);
143 loadkernel();
144 if (initrd) {
145 char *p, *q = initrd;
146 while (1) {
147 char c;
148 for (p = q; *p && *p != ','; p++);
149 c = *p;
150 *p = 0;
151 if (fakeopen(q) != -1)
152 loadinitrd();
153 if (c == 0)
154 break;
155 q = ++p;
156 }
157 }
158 bootlinux(cmdline);
159 }