wok-next view syslinux/stuff/iso2exe/bootlinux.c @ rev 15271
lzma: remove wrong error trigger
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Wed Sep 25 13:07:34 2013 +0000 (2013-09-25) |
parents | bf8be127c60b |
children | df021be024df |
line source
1 #include <stdio.h>
2 #include "iso9660.h"
4 static unsigned version;
5 #define SETUPSECTORS 0x1F1
6 #define ROFLAG 0x1F2
7 #define SYSSIZE 0x1F4
8 #define VIDEOMODE 0x1FA
9 #define BOOTFLAG 0x1FE
10 #define HEADER 0x202
11 #define VERSION 0x206
12 #define RMSWOFS 0x208
13 #define RMSWSEG 0x20A
14 #define LOADERTYPE 0x210
15 #define LOADFLAGS 0x211
16 #define SYSTEMCODE 0x214
17 #define INITRDCODE 0x218
18 #define INITRDSIZE 0x21C
19 #define HEAPPTR 0x224
20 #define CMDLINE 0x228
22 #define SETUP_SEGMENT 0x9000
23 #define CMDLINE_OFFSET 0x9E00
25 #define PAGE_BITS 12
26 #define PAGE_SIZE 4096
27 #define BUFFERSZ PAGE_SIZE
28 static char buffer[BUFFERSZ];
29 static unsigned long initrd_addr, initrd_size;
31 static int may_exit_dos = 1;
32 static void die(char *msg)
33 {
34 printf("%s.\n", msg);
35 if (may_exit_dos)
36 exit(1);
37 while (1);
38 }
40 static int vm86(void)
41 {
42 #asm
43 smsw ax
44 and ax, #1 // 0:realmode 1:vm86
45 #endasm
46 }
48 static struct mem {
49 unsigned long base;
50 int align;
51 } kernelmem = { 0x100000, 0 };
53 #define initrdmem kernelmem
55 static void movehi(void)
56 {
57 #asm
58 pusha
59 xor di, di // 30
60 mov cx, #9 // 2E..1E
61 zero1:
62 push di
63 loop zero1
64 push dword [_kernelmem] // 1A p->base
65 push #-1 // 18
66 push di // 16
67 xor eax, eax
68 cdq
69 mov dx, ds
70 shl edx, #4
71 mov ax, #_buffer
72 add edx, eax
73 push edx // 12 linear_address(buffer)
74 push #-1 // 10
75 mov cl, #8 // 0E..00
76 zero2:
77 push di
78 loop zero2
79 mov ch, #BUFFERSZ/512
80 push ss
81 pop es
82 mov si, sp
83 mov ax, #0x8793
84 mov [si+0x15], al
85 xchg [si+0x1D], al
86 mov [si+0x1F], al // bits 24..31, doesn't work for me :(
87 int 0x15
88 add sp, #0x30
89 popa
90 #endasm
91 }
93 #undef ZIMAGE_SUPPORT /* Does not work... */
95 static int versiondos;
96 static int dosversion(void)
97 {
98 #asm
99 mov ah, #0x30
100 int 0x21
101 cbw
102 mov _versiondos, ax
103 #endasm
104 }
106 static void load(struct mem *p, unsigned long size)
107 {
108 if (vm86())
109 die("Need real mode");
110 switch (p->align) {
111 case 0: // kernel
112 switch (dosversion()) {
113 case 3: case 4: case 6: break;
114 default:
115 printf("DOS %d not supported.\nTrying anyway...\n",
116 versiondos);
117 }
118 p->align = PAGE_SIZE;
119 break;
120 case 4096: // first initrd
121 initrd_addr = p->base;
122 p->align = 4;
123 }
124 while (size) {
125 int n, s = sizeof(buffer);
126 for (n = 0; n < s; n++) buffer[n] = 0;
127 if (s > size) s = size;
128 n = isoread(buffer, s);
129 movehi();
130 if (n != -1) {
131 p->base += n;
132 size -= n;
133 }
134 if (s != n) break;
135 }
136 initrd_size = p->base - initrd_addr;
137 p->base += p->align - 1;
138 p->base &= - p->align;
139 }
141 static unsigned setupofs = 0;
143 void movesetup(void)
144 {
145 #asm
146 pusha
147 push #SETUP_SEGMENT
148 pop es
149 mov si, #_buffer
150 mov di, _setupofs
151 mov cx, #BUFFERSZ/2
152 rep
153 movsw
154 mov _setupofs, di
155 popa
156 #endasm
157 }
159 static unsigned getcs(void)
160 {
161 #asm
162 mov ax, cs
163 #endasm
164 }
166 unsigned long loadkernel(void)
167 {
168 unsigned setup, n = BUFFERSZ;
169 unsigned long syssize = 0, kernel_version = 0;
171 do {
172 isoread(buffer, n);
173 if (setupofs == 0) {
174 if (* (unsigned short *) (buffer + BOOTFLAG) != 0xAA55)
175 die("The kernel is not bootable");
176 setup = (1 + buffer[SETUPSECTORS]) << 9;
177 if (setup == 512) setup = 5 << 9;
178 syssize = * (unsigned long *) (buffer + SYSSIZE) << 4;
179 version = * (unsigned short *) (buffer + VERSION);
180 #define HDRS 0x53726448
181 if (* (unsigned long *) (buffer + HEADER) != HDRS)
182 version = 0;
183 if (version < 0x204)
184 syssize &= 0x000FFFFFUL;
185 if (version) {
186 #ifdef REALMODE_SWITCH
187 extern int far_realmode_switch();
188 #asm
189 jmp end_realmode_switch
190 _far_realmode_switch:
191 call REALMODE_SWITCH
192 cli
193 mov al, #0x80 // Disable NMI
194 out 0x70, al
195 retf
196 end_realmode_switch:
197 #endasm
198 * (unsigned short *) (buffer + RMSWOFS) =
199 far_realmode_switch;
200 * (unsigned short *) (buffer + RMSWSEG) =
201 getcs();
202 #endif
203 kernelmem.base =
204 * (unsigned long *) (buffer + SYSTEMCODE);
205 * (unsigned short *) (buffer + HEAPPTR) =
206 0x9B00;
207 // buffer[LOADFLAGS] |= 0x80;
208 * (unsigned short *) (buffer + LOADERTYPE) |=
209 0x80FF;
210 }
211 if (!version || !(buffer[LOADFLAGS] & 1)) {
212 #ifdef ZIMAGE_SUPPORT
213 #asm
214 pusha
215 mov cx, #0x8000
216 mov es, cx
217 xor si, si
218 xor di, di
219 rep
220 movsw // move 64K data
221 push es
222 pop ds
223 push es
224 pop ss
225 mov ch, #0x70
226 mov es, cx
227 mov ch, #0x80
228 rep
229 seg cs
230 movsw // move 64K code
231 popa
232 jmpi relocated, #0x7000
233 relocated:
234 #endasm
235 kernelmem.base = 0x10000;
236 if (syssize > 0x60000) /* 384K max */
237 #endif
238 die("Not a bzImage format");
239 }
240 }
241 movesetup();
242 setup -= n;
243 n = (setup > BUFFERSZ) ? BUFFERSZ : setup;
244 } while (setup > 0);
246 #asm
247 push ds
248 push #SETUP_SEGMENT
249 pop ds
250 mov si, #0x200
251 mov eax, #0x53726448 // HdrS
252 cdq // clear edx
253 cmp [si+2], eax
254 jne noversion
255 add si, [si+14]
256 mov cx, #3
257 xor ax, ax
258 nextdigit:
259 shl al, #4
260 shl ax, #4
261 next:
262 lodsb
263 sub al, #0x30
264 cmp al, #9
265 jbe nextdigit
266 shl eax, #16
267 shld edx, eax, #8
268 loop next
269 pop ds
270 mov .loadkernel.kernel_version[bp], edx
271 noversion:
272 #endasm
273 load(&kernelmem, syssize);
274 return kernel_version;
275 }
277 void loadinitrd(void)
278 {
279 if (version)
280 load(&initrdmem, isofilesize);
281 }
283 void bootlinux(char *cmdline)
284 {
285 #asm
286 push #SETUP_SEGMENT
287 pop es
288 mov eax, _initrd_addr
289 or eax, eax
290 jz no_initrd
291 mov di, #0x218
292 stosd
293 mov eax, _initrd_size
294 stosd
295 no_initrd:
296 #endasm
297 if (cmdline) {
298 if (version <= 0x201) {
299 #asm
300 mov di, #0x0020
301 mov ax, #0xA33F
302 stosw
303 mov ax, #CMDLINE_OFFSET
304 stosw
305 #endasm
306 }
307 else {
308 #asm
309 mov di, #0x0228
310 mov eax, #SETUP_SEGMENT*16+CMDLINE_OFFSET
311 stosd
312 #endasm
313 }
314 #asm
315 xchg ax, di
316 mov si, .bootlinux.cmdline[bp]
317 copy:
318 lodsb
319 stosb
320 or al,al
321 jne copy
322 #endasm
323 }
324 #asm
325 push es
326 pop ds
327 push ds
328 pop ss
329 mov sp, #CMDLINE_OFFSET
330 jmpi 0, #0x9020
331 #endasm
332 }