wok view syslinux/stuff/iso2exe/bootlinux.c @ rev 15981
syslinux-tools: add meminfo.exe
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Sat Mar 01 19:10:36 2014 +0000 (2014-03-01) |
parents | df021be024df |
children | 647e4ebbbcae |
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
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 unsigned extendedramsizeinkb(void)
107 {
108 #asm
109 mov ah, #0x88
110 int 0x15
111 jnc gottop
112 xor ax, ax
113 gottop:
114 #endasm
115 }
117 static void load(struct mem *p, unsigned long size)
118 {
119 if (vm86())
120 die("Need real mode");
121 switch (p->align) {
122 case 0: // kernel
123 switch (dosversion()) {
124 case 3: case 4: case 6: break;
125 default:
126 printf("DOS %d not supported.\nTrying anyway...\n",
127 versiondos);
128 }
129 p->align = PAGE_SIZE;
130 break;
131 case 4096: // first initrd : skip mapping hole before 16M
132 initrd_addr = (extendedramsizeinkb() > 24000U) ?
133 0x1000000 : p->base;
134 p->align = 4;
135 }
136 while (size) {
137 int n, s = sizeof(buffer);
138 for (n = 0; n < s; n++) buffer[n] = 0;
139 if (s > size) s = size;
140 n = isoread(buffer, s);
141 movehi();
142 if (n != -1) {
143 p->base += n;
144 size -= n;
145 }
146 if (s != n) break;
147 }
148 initrd_size = p->base - initrd_addr;
149 p->base += p->align - 1;
150 p->base &= - p->align;
151 }
153 static unsigned setupofs = 0;
155 void movesetup(void)
156 {
157 #asm
158 pusha
159 push #SETUP_SEGMENT
160 pop es
161 mov si, #_buffer
162 mov di, _setupofs
163 mov cx, #BUFFERSZ/2
164 rep
165 movsw
166 mov _setupofs, di
167 popa
168 #endasm
169 }
171 static unsigned getcs(void)
172 {
173 #asm
174 mov ax, cs
175 #endasm
176 }
178 unsigned long loadkernel(void)
179 {
180 unsigned setup, n = BUFFERSZ;
181 unsigned long syssize = 0, kernel_version = 0;
183 do {
184 isoread(buffer, n);
185 if (setupofs == 0) {
186 if (* (unsigned short *) (buffer + BOOTFLAG) != 0xAA55)
187 die("The kernel is not bootable");
188 setup = (1 + buffer[SETUPSECTORS]) << 9;
189 if (setup == 512) setup = 5 << 9;
190 syssize = * (unsigned long *) (buffer + SYSSIZE) << 4;
191 version = * (unsigned short *) (buffer + VERSION);
192 #define HDRS 0x53726448
193 if (* (unsigned long *) (buffer + HEADER) != HDRS)
194 version = 0;
195 if (version < 0x204)
196 syssize &= 0x000FFFFFUL;
197 if (version) {
198 #ifdef REALMODE_SWITCH
199 extern int far_realmode_switch();
200 #asm
201 jmp end_realmode_switch
202 _far_realmode_switch:
203 call REALMODE_SWITCH
204 cli
205 mov al, #0x80 // Disable NMI
206 out 0x70, al
207 retf
208 end_realmode_switch:
209 #endasm
210 * (unsigned short *) (buffer + RMSWOFS) =
211 far_realmode_switch;
212 * (unsigned short *) (buffer + RMSWSEG) =
213 getcs();
214 #endif
215 kernelmem.base =
216 * (unsigned long *) (buffer + SYSTEMCODE);
217 * (unsigned short *) (buffer + HEAPPTR) =
218 0x9B00;
219 // buffer[LOADFLAGS] |= 0x80;
220 * (unsigned short *) (buffer + LOADERTYPE) |=
221 0x80FF;
222 }
223 if (!version || !(buffer[LOADFLAGS] & 1)) {
224 #ifdef ZIMAGE_SUPPORT
225 #asm
226 pusha
227 mov cx, #0x8000
228 mov es, cx
229 xor si, si
230 xor di, di
231 rep
232 movsw // move 64K data
233 push es
234 pop ds
235 push es
236 pop ss
237 mov ch, #0x70
238 mov es, cx
239 mov ch, #0x80
240 rep
241 seg cs
242 movsw // move 64K code
243 popa
244 jmpi relocated, #0x7000
245 relocated:
246 #endasm
247 kernelmem.base = 0x10000;
248 if (syssize > 0x60000) /* 384K max */
249 #endif
250 die("Not a bzImage format");
251 }
252 }
253 movesetup();
254 setup -= n;
255 n = (setup > BUFFERSZ) ? BUFFERSZ : setup;
256 } while (setup > 0);
258 #asm
259 push ds
260 push #SETUP_SEGMENT
261 pop ds
262 mov si, #0x200
263 mov eax, #0x53726448 // HdrS
264 cdq // clear edx
265 cmp [si+2], eax
266 jne noversion
267 add si, [si+14]
268 mov cx, #3
269 xor ax, ax
270 nextdigit:
271 shl al, #4
272 shl ax, #4
273 next:
274 lodsb
275 sub al, #0x30
276 cmp al, #9
277 jbe nextdigit
278 shl eax, #16
279 shld edx, eax, #8
280 loop next
281 pop ds
282 mov .loadkernel.kernel_version[bp], edx
283 noversion:
284 #endasm
285 load(&kernelmem, syssize);
286 return kernel_version;
287 }
289 void loadinitrd(void)
290 {
291 if (version)
292 load(&initrdmem, isofilesize);
293 }
295 void bootlinux(char *cmdline)
296 {
297 #asm
298 push #SETUP_SEGMENT
299 pop es
300 mov eax, _initrd_addr
301 or eax, eax
302 jz no_initrd
303 mov di, #0x218
304 stosd
305 mov eax, _initrd_size
306 stosd
307 no_initrd:
308 #endasm
309 if (cmdline) {
310 if (version <= 0x201) {
311 #asm
312 mov di, #0x0020
313 mov ax, #0xA33F
314 stosw
315 mov ax, #CMDLINE_OFFSET
316 stosw
317 #endasm
318 }
319 else {
320 #asm
321 mov di, #0x0228
322 mov eax, #SETUP_SEGMENT*16+CMDLINE_OFFSET
323 stosd
324 #endasm
325 }
326 #asm
327 xchg ax, di
328 mov si, .bootlinux.cmdline[bp]
329 copy:
330 lodsb
331 stosb
332 or al,al
333 jne copy
334 #endasm
335 }
336 #asm
337 push es
338 pop ds
339 push ds
340 pop ss
341 mov sp, #CMDLINE_OFFSET
342 jmpi 0, #0x9020
343 #endasm
344 }