wok view syslinux/stuff/iso2exe/bootiso.S @ rev 23875

Up alpine (2.23), ansible (2.9.10), wordpress (5.4.2)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Jun 27 16:47:12 2020 +0000 (2020-06-27)
parents a2e843f5d9e4
children 3d19917d3a03
line source
1 .text
2 .code16
3 .arch i8086
4 .org 0
6 CODESZ = 0x8000 // 16 sectors = 32Kb
7 #define EXEADRS(x) x+0xC0
8 #define EXELOC(x) x-0x40
9 #define EXESTR(x) x-0x7F40
11 .globl _start
12 _start:
13 decw %bp // Magic number: MZ
14 popw %dx
15 jmp start0 // Bytes on last page of file
16 .word (CODESZ+511)/512 // Pages in file
17 .word 0 // Relocations
18 .word (end_header-_start)/16 // Size of header in paragraphs
19 .word 4064-(CODESZ/16) // Minimum extra paragraphs needed
20 .word 4064-(CODESZ/16) // Maximum extra paragraphs needed
21 .word 0xFFF0 // Initial (relative) SS value
22 .word 0xFFFE // Initial SP value
23 magic:
24 .word 0 // Checksum
25 .word EXEADRS(exestart) // Initial IP value
26 .word 0xFFF0 // Initial (relative) CS value
27 initramfssize:
28 .word 0 // File address of relocation table
29 fdcnt: // Overlay number
30 .byte 0 // Bootstrap floppy sector count
31 .ascii "slitaz"
33 /////////////////////// Master Boot Record code //////////////////////////////
35 moved = 0x8000
36 start0:
37 //pushw %dx // restore %sp
38 //incw %bp // restore %bp
39 movw %ds, %ax
40 xorw %bx, %bx
41 movw %bx, %ds
42 movb $0x7C, %bh
43 pushw %ds
44 popw %ss
45 movw %bx, %sp
46 pushw %bx // return address
47 pushw %di
48 pushw %es // save %es:%di
49 pushw %si
50 cld
51 call setreg
52 rep
53 movsw
54 movw $0x80, %dx
55 ljmp $0, $moved+start2
56 .byte 0
57 // .org 60
58 // .long 0x0080 // PE header offset
59 .org 64
60 end_header:
61 comstart:
62 .word 0
64 .org 66
65 start2:
66 pushw %ax // original %ds
67 sti
68 dxloop:
69 call readsector1 // look for the boot device
70 repe
71 cmpsw
72 je dxfound
73 movb $0, %cl // ch = 0
74 addb $0x7D, %dl // try every hard disk
75 jno dxloop
77 dxfound:
78 call readsectorX // read isolinux boot sector
79 cmpw (%bx), %cx
80 movw $moved+nobsmsg, %si
81 jnc puts // read fail or no isohydrid boot sector
82 call bootpartition // assume DS=SS SI=BX=7C00 CX=0100 DL=<drive> DI=table+66
83 popw %ds
84 popw %si
85 popw %es
86 popw %di // isolinux boot needs %es:%di and %dx
87 putsret:
88 ret
90 putstrlp:
91 movw $7, %bx
92 movb $0xE, %ah
93 int $0x10
94 puts:
95 lodsb
96 cmp $0, %al
97 jnz putstrlp
98 halt:
99 hlt
100 jmp halt
101 nobsmsg:
102 .asciz "No isolinux."
104 .org 0x0080,0xEE
105 ////////////////////////////// EXE/PE header //////////////////////////////////
106 .org 0x0178,0xFF
107 ////////////////////////// partition boot code ////////////////////////////////
108 // assume DS=SS SI=BX=7C00 CX=0100 DL=<drive> DI=table+66
110 bootpartition:
111 movw $4,%cx
112 movw $16,%ax
113 next:
114 subw %ax,%di
115 cmpb %ch,-2(%di) // boot flag ?
116 loope next
117 cmpw $63,2-2(%di) // empty or isolinux partition ?
118 jbe default
119 .arch i486
120 pushl $0
121 pushl 8-2(%di)
122 pushw %cs
123 pushw %bx
124 pushw $1
125 pushw %ax
126 movw %sp,%si // assume %ds = %ss
127 movb $0x42,%ah
128 int $0x13
129 add $16,%sp
130 .arch i8086
131 default:
132 ret
134 .org 0x01A0,0xFF
135 readsectorX: // read isolinux boot sector
136 movb $3, %cl // isolinux/512
137 readsector1:
138 andb $0x83, %dl // disk and floppy disk
139 movw %cx, (%bx)
140 incw %cx
141 movw $0x201, %ax
142 int $0x13
143 setreg:
144 pushw %ds
145 popw %es
146 movw %bx, %si
147 movw $moved, %di
148 movw $0x0100, %cx
149 ret
150 .org 0x01B8,0xEE // partition table
152 .org 0x0270,0xFF
154 ////////////////////////////// DOS EXE code ///////////////////////////////////
156 bootdir:
157 .asciz "/boot"
158 linld:
159 .asciz "linld.com"
160 cmdline:
161 .ascii "-f "
162 // .ascii "image=\boot\"
163 // .ascii "bzImage "
164 .ascii "initrd="
165 rootfs:
166 .ascii "rootfs"
167 dotgz:
168 .ascii ".gz,! "
169 // .ascii ".gz,rootfs4.gz,! "
170 args:
171 // .ascii " rw "
172 // .ascii " mode=menu"
173 .ascii " autologin rdinit=/init.exe magic="
174 magicstr:
175 .ascii "0 bootfrom="
176 // .ascii "65535 bootfrom="
177 filename:
178 exestart:
179 movb $0x30,%ah // get DOS version
180 int $0x21
181 cmpb $3,%al
182 jc goabort
184 cld
185 movw EXEADRS(comstart),%si
186 movw $0xF000,%di
187 movb $4,%ch // 2k min
188 rep
189 movsw // helper
190 movw $129,%si
191 leaw args-cmdline(%si),%di
192 movb -1(%si),%cl
193 pushw %si
194 rep
195 movsb // user args
196 xchgw %ax,%di
197 popw %di
198 movw $EXEADRS(cmdline),%si
199 movb $args-cmdline,%cl
200 rep
201 movsb // files
202 xchgw %ax,%di
203 movb $filename-args,%cl
204 rep
205 movsb // end
206 pushw %di // filename
208 #define ISOSTATE 0xF000
209 #define ISO_OPEN iso_open
210 #define ISO_READDIR *0xF004
211 #define ISO_READMENU iso_readmenu
212 #define RUN_LINLD run_linld
213 movw ISOSTATE,%bp
214 xorw %ax,%ax
215 movw %bp,%di
216 movb $64,%cl
217 rep
218 stosb // clear BSS
220 movw %ax,%di
221 movw 0x2C(%di),%es
222 decw %cx
223 scalp:
224 repne
225 scasb
226 scasb
227 jne scalp
228 scasw
229 movw %di,%si // %es:%di = programme pathname
231 pushw %es
232 pushw %ds
233 popw %es
234 popw %ds
235 popw %di // filename
236 pushw %di
237 movb $0x60,%ah // canonicalize filename
238 int $0x21
239 pushw %es
240 popw %ds
241 #define LONG_FILENAME
242 #ifdef LONG_FILENAME
243 popw %si
244 pushw %si
245 movw $0x716C,%ax
246 xorw %bx,%bx // R/O
247 xorw %cx,%cx // attributes
248 cwd // action = open
249 stc
250 int $0x21
251 jnc opened
252 #endif
253 popw %dx
254 pushw %dx
255 movw $0x3D00,%ax
256 // movb $0,%cl
257 int $0x21
258 goabort:
259 jc abort
260 opened:
261 movw %ax,4(%bp) // fd
263 call ISO_READMENU
264 movw 6(%bp),%ax // magic = filemod
265 popw %bx
266 leaw magicstr-filename(%bx),%di
267 movb $10,%bl
268 xorw %cx,%cx
269 lp1:
270 xorw %dx,%dx
271 divw %bx
272 pushw %dx
273 incw %cx
274 orw %ax,%ax
275 jnz lp1
276 lp2:
277 popw %ax
278 addb $'0',%al
279 stosb // store magic
280 loop lp2
282 movw $EXEADRS(bootdir),%ax
283 call ISO_OPEN
284 orw $-1,18(%bp) // curdirsize = -1
285 xorw %ax,%ax
286 found:
287 incb %ah
288 patchtest:
289 pushw %ax
290 call ISO_READDIR
291 pushw %ds
292 popw %es
293 incw %ax
294 popw %ax
295 movw $EXEADRS(rootfs),%si
296 je dopatch
297 movw %si,%di
298 movw 16(%bp),%si // filename
299 movw $9,%cx
300 rep
301 cmpsb
302 jcxz found
303 cmpb $3,%cl
304 ja patchtest
305 decw %si
306 cmpb (%si),%al
307 ja patchtest
308 lodsb
309 jmp patchtest
310 done:
311 movw $128,%ax
312 subw %ax,%si
313 xchgw %ax,%si
314 decw %ax
315 movb %al,(%si) // cmdline length
317 movw $EXEADRS(linld),%ax
318 call ISO_OPEN
319 jc abort
320 movw 12(%bp),%cx // filesize
321 movw $0x100,%dx
322 call RUN_LINLD
323 abort:
324 movw $EXEADRS(stopmsg),%dx
325 movb $9,%ah
326 int $0x21
327 int $0x20
328 dopatch:
329 pushw %si
330 cmpw $0x200,%ax
331 je initrddone
332 movw $EXEADRS(dotgz),%di
333 decb %ah
334 je modify
335 scasw
336 scasw // addw $4,%di
337 movsw
338 movsw
339 movsw
340 modify:
341 stosb
342 movw $129+dotgz-cmdline,%si
343 movsb
344 movsw
345 movsw
346 initrddone:
347 movw $129+rootfs-cmdline,%di
348 popw %si
349 movw $args-rootfs,%cx
350 rep
351 movsb
353 shrink0:
354 movw $129,%si
355 shrink:
356 movw %si,%di
357 lodsb
358 cmpb $0,%al
359 je done
360 cmpb $' ',%al
361 jne shrink
362 cmpb (%si),%al
363 jne shrink
364 pack:
365 lodsb
366 stosb
367 cmpb $0,%al
368 jne pack
369 jmp shrink0
371 .org 0x0400,0xEE
372 .org 0x0600,0xFF
373 isolinux:
375 .arch i486
376 #define PARTITION_SUPPORT
377 /* -----------------------------------------------------------------------
378 *
379 * Copyright 2007-2009 H. Peter Anvin - All Rights Reserved
380 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
381 *
382 * Permission is hereby granted, free of charge, to any person
383 * obtaining a copy of this software and associated documentation
384 * files (the "Software"), to deal in the Software without
385 * restriction, including without limitation the rights to use,
386 * copy, modify, merge, publish, distribute, sublicense, and/or
387 * sell copies of the Software, and to permit persons to whom
388 * the Software is furnished to do so, subject to the following
389 * conditions:
390 *
391 * The above copyright notice and this permission notice shall
392 * be included in all copies or substantial portions of the Software.
393 *
394 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
395 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
396 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
397 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
398 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
399 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
400 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
401 * OTHER DEALINGS IN THE SOFTWARE.
402 *
403 * ----------------------------------------------------------------------- */
405 HYBRID_MAGIC = 0x7078c0fb
406 isolinux_start_hybrid = 0x7c00+64+4
408 isolinux_hybrid_signature = 64
410 stack = 0x7c00
411 ebios_flag = (stack-20)
413 BIOS_page = 0x462
415 _start2:
416 .byte 0x33, 0xed /* xorw %bp, %bp */
418 /* Check to see if we have a partition table entry */
419 #ifdef PARTITION_SUPPORT
420 xorl %ebx, %ebx
421 xorl %ecx, %ecx
422 andw %si, %si /* %si == 0 -> no partition data */
423 jz 1f
424 testb $0x7f, (%si) /* Invalid active flag field? */
425 jnz 1f
426 orb 4(%si), %cl /* Partition type zero == invalid? */
427 je 1f
429 cmpb $0xed, %cl /* EFI partition type? */
431 /* Get non-GPT partition information */
432 movl 8(%si), %ecx
434 jne 1f
435 cmpl $0x58504721, %eax /* !GPT signature in EAX? */
436 jne 1f
438 /* We have GPT partition information */
439 movl (32+20)(%si), %ecx
440 movl (36+20)(%si), %ebx
441 #endif
442 1:
443 movw %bp, %ds
444 movw $stack, %sp
446 /* We have no partition information */
447 #ifdef PARTITION_SUPPORT
448 pushl %ebx /* -4: partoffset_hi */
449 pushl %ecx /* -8: partoffset_lo */
450 #else
451 pushl $0 /* -4: partoffset_hi */
452 pushl $0 /* -8: partoffset_lo */
453 #endif
454 partoffset = -8
455 pushw %es /* -10: es:di -> $PnP header */
456 pushw %di /* -12: es:di -> $PnP header */
459 //ADJUST_DRIVE
460 pushw %dx /* -14: dl -> drive number */
461 driveno = -14
463 pushw %ds
464 popw %es
466 /* Copy down to 0:0x600 */
467 movw $2f-0x600+0x7C00, %si
468 movw $2f, %di
469 movb $(512 >> 8), %ch
470 pushw %es
471 pushw %di
472 rep; movsb
474 retf
475 2:
477 /* Check to see if we have EBIOS */
478 pushw %dx /* drive number */
479 movb $0x41, %ah /* %al == 0 already */
480 movw $0x55aa, %bx
481 //xorw %cx, %cx
482 xorb %dh, %dh
483 int $0x13
484 andw $1,%cx /* Bit 0 = fixed disk subset */
485 jz 1f
486 decw %cx /* Clear EBIOS flag. */
487 cmpw $0xaa55, %bx
488 jne 1f
489 incw %cx /* Set EBIOS flag. */
491 /* We have EBIOS; patch in the following code at
492 read_sector_cbios: movb $0x42, %ah ; jmp read_common */
493 movl $0xeb42b4+((read_common-read_sector_cbios-4) << 24), \
494 (read_sector_cbios)
495 1:
496 popw %dx
497 pushw %cx /* EBIOS flag */
499 /* Get (C)HS geometry */
500 movb $0x08, %ah
501 int $0x13
502 popw %bx /* EBIOS flag */
503 movzbw %dh, %ax /* dh = max head */
504 incw %ax /* From 0-based max to count */
505 pushw %ax /* -16: Save heads on the stack */
506 heads = -16
507 andw $0x3f, %cx /* Sector count */
508 pushw %cx /* -18: Save sectors on the stack */
509 sectors = -18
510 mulw %cx /* Heads*sectors -> sectors per cylinder */
512 pushw %bx /* -20: EBIOS flag */
514 /* Save sectors/cylinder in %esi */
515 pushw %dx
516 pushw %ax
517 popl %edi
519 /*
520 * Load sectors. We do this one at a time mostly to avoid
521 * pitfalls and to share code with the stock MBR code.
522 */
523 movw $0x7c00, %bx
524 movw %bx, %bp
525 xorl %eax, %eax
526 cdq
527 movb $17*4, %al /* EL TORITO spec */
528 call read_sector
529 movl 71(%bx), %eax /* catalog */
530 shll $2, %eax
531 call read_sector
532 movl 40(%bx), %eax /* boot code */
533 shll $2, %eax
534 movb $4, %cl /* Sector count */
536 2:
537 call read_sector
538 incl %eax
539 addb $(512 >> 8), %bh
540 loopw 2b
542 /*
543 * Okay, that actually worked... update the stack pointer
544 * and jump into isolinux.bin...
545 */
546 cmpl $HYBRID_MAGIC,isolinux_hybrid_signature(%bp)
547 jne error_os
549 cli
550 //movw $ebios_flag, %sp
552 /*
553 * Use a ljmpw here to work around a bug in some unknown version
554 * of gas or ld when it comes to jumping to an absolute symbol...
555 *
556 * Look more closely into it if we ever are short on space.
557 */
558 //ljmpw $0, $isolinux_start_hybrid
559 pushw $isolinux_start_hybrid
560 ret
562 /*
563 * read_sector: read a single sector pointed to by %eax to %es:%bx.
564 * All registers saved.
565 */
566 read_sector:
567 pushal
568 #ifdef PARTITION_SUPPORT
569 addl partoffset(%bp), %eax
570 adcl partoffset+4(%bp), %edx
571 #endif
572 pushl %edx /* MSW of LBA */
573 pushl %eax /* LSW of LBA */
574 pushw %es /* Buffer segment */
575 pushw %bx /* Buffer offset */
576 pushw $1 /* Sector count */
577 pushw $16 /* Size of packet */
578 movw %sp, %si
580 /* This chunk is skipped if we have ebios */
581 /* Do not clobber %eax before this chunk! */
582 /* This also relies on %bx and %edx as set up above. */
583 read_sector_cbios:
584 divl %edi
585 shlb $6, %ah
586 xchgb %al, %ah
587 xchgw %ax, %cx
588 xchgw %dx, %ax
589 divb sectors(%bp)
590 movb %al, %dh
591 orb %ah, %cl
592 incw %cx /* Sectors are 1-based */
593 movw $0x0201, %ax
595 read_common:
596 movb driveno(%bp), %dl
597 int $0x13
598 movw $disk_error, %si
599 jc error
600 addw $16, %sp /* Drop DAPA */
601 popal
602 ret
604 /*
605 * Print error messages. This is invoked with "call", with the
606 * error message at the return address.
607 */
608 error_os:
609 movw $bad_signature, %si
610 error:
611 lodsb
612 movb $0x0e, %ah
613 movb (BIOS_page), %bh
614 movb $0x07, %bl
615 int $0x10 /* May destroy %bp */
616 cmpb $10, %al /* Newline? */
617 jne error
619 int $0x18 /* Boot failure */
620 die:
621 hlt
622 jmp die
624 bad_signature:
625 .ascii "isolinux.bin "
626 .ascii "missing or corrupt.\r\n"
627 disk_error:
628 .ascii "Operating system "
629 .ascii "load error.\r\n"
631 .org 0x0750,0xBB
632 stopmsg:
633 .ascii "This program cannot be run in DOS mode.$"
634 .org 0x0778,0xEE
635 .org 0xF002-0xC0
636 iso_open:
637 .word 0
638 .word 0
639 iso_readmenu:
640 .word 0
641 run_linld:
643 .end