-
Notifications
You must be signed in to change notification settings - Fork 2
/
LOADLINM.ASM
783 lines (727 loc) · 22.2 KB
/
LOADLINM.ASM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
; >>> this is file LOADLINM.ASM
;============================================================================
; LOADLIN v1.6 (C) 1994..1996 Hans Lermen (lermen@elserv.ffm.fgan.de)
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;
;----------------------------------------------------------------------------
; Comments and bug reports are welcome and may be sent to:
; E-Mail: lermen@elserv.ffm.fgan.de
; SnailMail: Hans Lermen
; Am Muehlenweg 38
; D53424 REMAGEN-Unkelbach
; GERMANY
;
;============================================================================
;---------------------------------------------------------------------------
call_pmode_routine proc near
; NOTE: must have called get_VCPI_interface before this
; _AND_ must be already moved high, else the pagetable
; will not be aligned.
; input:
; AX = offset of protected mode routine
; The called routine will have a pointer in ebp to a pushad_struct
; of the other input registers, and all segment register point to
; High_seg
;
@@regs struc
pushAD_struc @@
@@regs ends
pushf
CLI
pushad
mov ebp,esp
; ÚÄÄÄÄÄ IRET stack for return to VM86
push 0
push gs
push 0
push fs
push 0
push ds
push 0
push es
push 0
push ss
push ebp
sub sp,4 ; space for EFLAGS
push 0
push cs
push 0
push offset @@pmode_return
; ÀÄÄÄÄÄÄ IRET stack for return to VM86
mov cs:pmode_return_esp,esp
lea ax,@@pmode_task
movzx eax,ax
mov dword ptr protected_mode_target,eax
mov word ptr protected_mode_target+4,g_code
movzx esi,cs:High_Seg
shl esi,4
lea si,our_CR3[si]
mov ax,0DE0Ch
int emm_int ; jumps to protected mode
; and comes here in 16-bit protected mode
@@pmode_task:
mov ax,g_data
mov ss,ax
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax
mov esp,cs:pmode_return_esp
; ebp pointing to pushad_struct
mov ax, word ptr [bp].@@eax
call ax ; call the routine
; now we return to VM86 mode
cli ; to be save
mov ax,g_core ; selector for addressing first MByte
mov ds,ax
.386p
CLTS ; clear taskswitch flag
.386
mov eax,0DE0Ch
call fword ptr cs:server_vcpi_entry ; back to VM86
; and returns here in VM86 mode
@@pmode_return:
popad
popf
ret
call_pmode_routine endp
IF 0
AUXPAGE = (modul_end-code_org0) ; behind our code
ELSE
AUXPAGE = 0 ; at start of our segment, only used in protected mode
ENDIF
AUXPAGE_size = 02000h
AUXPAGE_entry = (AUXPAGE shr (12-2))
AUXPAGE_S = AUXPAGE
AUXPAGE_T = AUXPAGE+AUXPAGE_size
AUXPAGE_S_entry equ page0+AUXPAGE_entry[bx]
AUXPAGE_T_entry equ page0+(AUXPAGE_entry+(AUXPAGE_size shr (12-2)))[bx]
AUXPAGE_BITS = 3 ; writeable, present
move_anywhere_vcpi proc near
; input:
; ESI= linear source address
; EDI= linear target address
; CX= size to move, must be <= 4096 !!!
; output:
; all registers preserved
; we are in VM86 and at this point assume to have VCPI
push ax
lea ax,@@pmove
call call_pmode_routine
pop ax
ret
; this will be executed in 16-bit protected mode
@@pmove:
@@regs struc
pushAD_struc @@
@@regs ends
; save pagetable-entries
mov bx,High_Seg
shr bx,(12-4-2)
push AUXPAGE_S_entry
push AUXPAGE_S_entry+4
push AUXPAGE_T_entry
push AUXPAGE_T_entry+4
mmm macro reg,st
mov eax,[bp].@@e®
mov reg,ax
and reg,0fffh
lea reg,[reg]+AUXPAGE_&st
and ax,0f000h
or al,AUXPAGE_BITS
mov AUXPAGE_&st&_entry,eax
add eax,1000h
mov AUXPAGE_&st&_entry+4,eax
endm
mmm si,S ; map source
mmm di,T ; map target
.386p
mov eax,CR3 ; flush TLB
mov CR3,eax
.386
cld
movzx ecx,cx
ror ecx,2
rep movsd
rol ecx,2
rep movsb
; restore pagetable-entries
pop AUXPAGE_T_entry+4
pop AUXPAGE_T_entry
pop AUXPAGE_S_entry+4
pop AUXPAGE_S_entry
ret
move_anywhere_vcpi endp
;---------------------------------------------------------------
move_anywhere_INT15 proc near
; input: ESI = source linear address
; EDI = destination linear address
; CX = count
; returns:
; all registers preserved
;
@@descript struc
@@limit dw ?
@@base0 dw ?
@@base16 db ?
@@typbyte db ?
@@limit16 db ?
@@base24 db ?
@@descript ends
push_ es,eax,esi,edi,ecx
mov cs:@@src.@@base0,si
shld eax,esi,16
mov cs:@@src.@@base16,al
mov cs:@@src.@@base24,ah
mov cs:@@dest.@@base0,di
shld eax,edi,16
mov cs:@@dest.@@base16,al
mov cs:@@dest.@@base24,ah
mov cx,(1000h/2) ; force to be one page
; NOTE: INT15 moves are in WORDS,
; because we always move 1000h,
; we simple do it also for the last page
push cs
pop es
lea si,@@gdt
mov ax,08700h
int 15h
pop_ es,eax,esi,edi,ecx
ret
align qword
@@gdt @@descript <0,0,0,0,0,0>
@@descript <0,0,0,0,0,0>
@@src @@descript <0ffffh,0,0,093h,0,0>
@@dest @@descript <0ffffh,0,0,093h,0,0>
@@descript <0,0,0,0,0,0> ; BIOS CS
@@descript <0,0,0,0,0,0> ; BIOS SS
move_anywhere_INT15 endp
;============================================================================
DUMMY_XMS_BLOCK_START = 100000h
XMS_GET_VERSION = 0
XMS_QUERY_FREE = 8
XMS_ALLOC = 9
XMS_FREE = 0ah
XMS_MOVE_BLOCK = 0bh
XMS_LOCK_BLOCK = 0ch
XMS_UNLOCK_BLOCK = 0dh
xmscall macro function
mov ah,function
call cs:xms_entry
endm
check_for_XMS proc near
push_ ds,es,ax,bx
mov cs:xms_entry,0
mov cs:xms_avail,0
mov ax,4300h
INT 2fh
cmp al,80h
jnz @@ex
mov ax,4310h
INT 2fh
mov word ptr cs:xms_entry,bx
mov word ptr cs:xms_entry+2,es
xmscall XMS_GET_VERSION
cmp ax,200h
jb @@ex0
xmscall XMS_QUERY_FREE
or ax,ax
jz @@ex0
mov cs:xms_avail,ax
@@ex:
pop_ ds,es,ax,bx
cmp cs:xms_entry,0
ret
@@ex0:
mov cs:xms_entry,0
jmp @@ex
check_for_XMS endp
allocate_xms_block proc near
push_ bx,dx
cmp cs:xms_entry,0
je @@ex0
mov dx,cs:xms_avail
xmscall XMS_ALLOC
or ax,ax
je @@ex0
mov cs:xms_handle,dx
@@ex:
pop_ bx,dx
or eax,eax
ret
@@ex0:
xor eax,eax
jmp @@ex
allocate_xms_block endp
lock_xms_block proc near
; try to lock the XMS block
; (needed to get the phys.address)
push_ ax,bx,dx
mov dx,cs:xms_handle
xmscall XMS_LOCK_BLOCK
or ax,ax
je @@err
mov word ptr cs:xms_phys_addr,bx
mov word ptr cs:xms_phys_addr+2,dx
pop_ ax,bx,dx
ret
@@err:
push cs
pop ds
lea dx,@@tx
call print
jmp exit_to_dos
@@tx db 13,10,'Cannot lock XMS memory',13,10,'$'
lock_xms_block endp
free_xms_block proc near
push_ ax,bx,dx
mov dx,cs:xms_handle
xmscall XMS_UNLOCK_BLOCK
mov dx, cs:xms_handle
xmscall XMS_FREE
pop_ ax,bx,dx
ret
free_xms_block endp
move_anywhere_xms proc near
; input:
; ESI= linear source address (must be in lowmem )
; EDI= linear target address
; CX= size to move, must be <= 4096 !!! (actually _is_ always 4096)
; output:
; all registers preserved
cmp edi,DUMMY_XMS_BLOCK_START
jb move_simple
pushad
push ds
mov ecx,1000h ; force a length of one page to move
; NOTE: XMS moves have to be even,
; because we alway move 1000h,
; we simple do it also for the last page
mov @@length,ecx
sub edi,DUMMY_XMS_BLOCK_START
mov @@doffs,edi
mov di,xms_handle
mov @@dhandle,di
ror esi,4
mov word ptr @@soffs+2,si
xor si,si
rol esi,4
mov word ptr @@soffs,si
lea si,@@length
push cs
pop ds
xmscall XMS_MOVE_BLOCK
pop ds
popad
ret
@@length dd ?
@@shandle dw 0
@@soffs dd ?
@@dhandle dw ?
@@doffs dd ?
move_anywhere_xms endp
;--------------------------------------------------------------------
move_simple proc near
; input:
; ESI= linear source address
; EDI= linear target address
; CX= size to move, must be <= 4096 !!!
; output:
; all registers preserved
pushad
push_ ds,es
movzx ecx,cx
cld
ror edi,4
mov es,di
xor di,di
rol edi,4
ror esi,4
mov ds,si
xor si,si
rol esi,4
ror ecx,2
rep movsd
rol ecx,2
rep movsb
@@ex:
pop_ ds,es
popad
ret
move_simple endp
build_buffer_heap proc near
pushad
; first we setup the normal low heap
; (why not use it)
movzx eax,kernel_load_frame
shl eax,4
mov heap_ptr,eax
movzx eax,High_Seg ; NOTE: this is page aligned !
shl eax,4
mov heap_end,eax
sub eax,heap_ptr
shr eax,12
mov heap_max_pages,eax
; now we try to get some extended memory
cmp need_mapped_put_buffer,0
jz @@err
cmp cpu_type,cpu_386V86
je @@vcpi
; we check what we can use to access extended memory
call check_for_XMS
jz @@need_int15
cmp cs:xms_avail,128
jb @@need_int15
sub cs:xms_avail,64 ; leave a 64K minimum for system
call allocate_xms_block
jz @@need_int15
movzx eax,cs:xms_avail
shr eax,2 ; from 1KB to 4KB
add heap_max_pages,eax
mov high_heap_ptr,DUMMY_XMS_BLOCK_START
mov high_mem_access,USING_XMS
mov move_anywhere, offset move_anywhere_xms
@@ex:
mov eax,heap_max_pages
shl eax,12
mov load_buffer_size,eax
popad
ret
@@need_int15:
mov ax,08800h
int 15h
sub ax,64+64 ; leave HMA untouched, we need 64Kminimum
; (else we would waste more mem than winning)
jbe @@err
movzx eax,ax
shr eax,2 ; from 1KB to 4KB
add heap_max_pages,eax
mov high_heap_ptr,0110000h
mov high_mem_access,USING_INT15
mov move_anywhere, offset move_anywhere_int15
jmp @@ex
@@err:
mov high_mem_access,0
mov move_anywhere, offset move_simple
jmp @@ex
@@vcpi:
cmp have_VCPI,0
jz @@ex
push edx
mov ax,0DE03h ; get avail 4K VCPI-page
int emm_int
sub edx,(64/4) ; leave a 64K minimum for system
jb @@err
add heap_max_pages,edx
mov high_mem_access,USING_VCPI
mov move_anywhere, offset move_anywhere_vcpi
pop edx
jmp @@ex
build_buffer_heap endp
get_buffer_from_heap proc near
; output: EAX = linear address of 4K buffer, page aligned
; (if XMS, then the offset within the buffer)
; CARRY =1, if memory overflow
cmp heap_max_pages,0
jna @@err
dec heap_max_pages
mov eax,heap_ptr
cmp eax,heap_end
jnb @@high
add heap_ptr,1000h
@@ex1:
clc
@@ex:
ret
@@high:
cmp high_mem_access,USING_VCPI ; what heap are we using
je @@vcpi
mov eax,high_heap_ptr
add high_heap_ptr,1000h
jmp @@ex1
@@vcpi:
push edx
mov ax,0DE04h ; get 4K VCPI-page
int emm_int
mov eax,edx
and ax,0f000h
pop edx
jmp @@ex1
@@err:
xor eax,eax
stc
jmp @@ex
get_buffer_from_heap endp
free_extended_memory proc near
cmp need_mapped_put_buffer,0
jz @@ex
cmp high_mem_access,USING_XMS
je @@xms
cmp high_mem_access,USING_VCPI
jne @@ex
@@vcpi:
push_ eax,bx,edx
mov bx,word ptr pageadjlist.ncount
shl bx,2
@@loop:
sub bx,4
jb @@ex1
mov edx,pageadjlist.sources[bx]
cmp edx,0100000h
jb @@ex1
mov ax,0DE05h ; free 4K VCPI-page
int emm_int
jmp @@loop
@@ex1:
pop_ eax,bx,edx
@@ex:
ret
@@xms:
call free_xms_block
jmp @@ex
free_extended_memory endp
final_page_adjust_list_handling proc near
cmp need_mapped_put_buffer,0
jz @@ex
cmp high_mem_access,USING_XMS
jne @@ex
push_ eax,bx
call lock_xms_block
mov bx,word ptr pageadjlist.ncount
shl bx,2
@@loop:
sub bx,4
jb @@ex1
mov eax,pageadjlist.sources[bx]
sub eax,DUMMY_XMS_BLOCK_START
jb @@ex1
add eax,xms_phys_addr
mov pageadjlist.sources[bx],eax
jmp @@loop
@@ex1:
pop_ eax,bx
@@ex:
ret
final_page_adjust_list_handling endp
open_new_mapped_block proc near
; input:
; EDI = linear address of final target address
cmp need_mapped_put_buffer,0
jz @@ex
push_ bx,edi
mov bx,word ptr pageadjlist.number_of_blocks
shl bx,3 ; * sizeof(pblock)
and di,0f000h
mov pageadjlist.blocks.taddr[bx],edi
mov di,word ptr pageadjlist.ncount
mov pageadjlist.blocks.tstart[bx],di
mov pageadjlist.blocks.tcount[bx],0
inc pageadjlist.number_of_blocks
mov do_mapped_put_buffer,1 ; notice 'put_buffer' what to do
pop_ bx,edi
@@ex:
ret
open_new_mapped_block endp
map_high_page proc near
; input:
; EDI = linear address of source address
; because this is called strict sequentially, the target address
; is allways given by the next free entry in the pageadjlist
cmp need_mapped_put_buffer,0
jz @@ex
push_ bx,edi
mov bx,word ptr pageadjlist.ncount
shl bx,2
and di,0f000h
mov pageadjlist.sources[bx],edi
inc pageadjlist.ncount
mov bx,word ptr pageadjlist.number_of_blocks
dec bx
shl bx,3 ; * sizeof(pblock)
inc pageadjlist.blocks.tcount[bx]
pop_ bx,edi
@@ex:
ret
map_high_page endp
put_buffer proc near
; input:
; AX= count
; EDI= linear destination address (may be > 0100000h)
; DS:DX= source address in lowmem
; output:
; none, all registers preserved
; in case of memory overflow it doesn't return (jumps to exit_to_dos)
;
pushad
push_ ds,es
movzx ecx,ax
cmp cs:need_mapped_put_buffer,0
jnz @@mapped
; we can mov it normally
cld
ror edi,4
mov es,di
xor di,di
rol edi,4
mov si,dx
ror ecx,2
rep movsd
rol ecx,2
rep movsb
@@ex:
pop_ ds,es
popad
ret
@@mapped: ; do high move
call get_buffer_from_heap
jc @@err
; we don't need EDI any more, because
; if we come here we are writing
; strict sequentialy and have set the
; starting address of the block
; via open_new_mapped_block
mov edi,eax
mov si,ds
movzx esi,si
shl esi,4
movzx edx,dx
add esi,edx
call map_high_page
call move_anywhere
jmp @@ex
@@err:
pop_ ds,es
popad
DosCall DOS_CLOSE_FILE
lea dx,@@tx
call print
jmp exit_to_dos
@@tx db 13,10,'Out of memory (may be low or extended)',13,10,'$'
put_buffer endp
; ---------------------------------------------------------------
; initrd stuff
MAXPHYSMEM_FOR_INT15 = 1000000h
load_initrd proc near
cmp cs:option_initrd,0
jz @@ex
pushad
cmp need_mapped_put_buffer,0
jz @@err_unable
call get_effective_physmem
mov end_of_physmem,eax
cmp high_mem_access,USING_INT15
jne @@1
; INT15 on some BIOSes will not access above 16Mb
cmp eax,MAXPHYSMEM_FOR_INT15
jbe @@1
mov eax,MAXPHYSMEM_FOR_INT15
@@1:
mov @@end_of_physmem,eax
mov ax,DOS_OPEN_FILE shl 8
lea dx,rdimage_name
DosInt
jc @@err_open
mov fhandle,ax
mov bx,ax
call get_filesize
mov ramdisk_size,eax
neg eax
add eax,@@end_of_physmem
and ax,0f000h ; round down to full page boundary
mov ramdisk_image,eax
movzx eax,kernel_size
add ax,0ffh
mov al,0 ; round up to page boundary
shl eax,4 ; size of compressed kernel
mov ecx,eax
shl eax,1 ; estimated size of decompressed kernel
; (we assume a decompression rate of 1:2)
sub ecx,(90000h - 2000h) ; the low buffer size (for bzimages)
jb @@3
add eax,ecx ; add padding
@@3:
add eax,(100000h+2000h) ; (+ gunzip-heap), now we have end of kernel
cmp eax,ramdisk_image
jnb @@err_mem
; ok we have place
; now loading the kernel
; NOTE: needing EDI for open_new_mapped_block
mov edi,ramdisk_image
call open_new_mapped_block ; open the first block
mov bx,fhandle
mov ecx,ramdisk_size
mov print_dots,3
call read_handle ; read the ramdisk
call print_crlf
mov print_dots,0
call print_crlf
jc @@err_io
cmp eax,ecx
jnz @@err_io
; ok, all is read into memory
DosCall DOS_CLOSE_FILE
popad
@@ex:
ret
@@err:
popad
mov dx,@@errtx_addr
call print
jmp exit_to_dos
@@err_open:
mov @@errtx_addr,offset @@tx
jmp @@err
@@err_mem:
mov @@errtx_addr,offset @@txmem
jmp @@err
@@err_io:
mov @@errtx_addr,offset @@txio
jmp @@err
@@err_unable:
mov @@errtx_addr,offset @@txno
jmp @@err
@@end_of_physmem dd 0
@@tx db 13,10,"can't open image file for initrd",13,10,'$'
@@txmem db 13,10,"no place after kernel for initrd",13,10,'$'
@@txio db 13,10,"IO-error while reading initrd",13,10,'$'
@@txno db 13,10,"no support in setup for reading initrd",13,10,'$'
@@errtx_addr dw 0
load_initrd endp
get_filesize proc near
; input:
; bx = filehandle
; output:
; eax = filesize, all other registers reserved
push_ cx,dx,esi
mov ax,4201h ; seek to current position
xor cx,cx
xor dx,dx
DosInt ; ... and return current position in DX:AX
push_ ax,dx ; save it
mov ax,4202h ; seek to last position
xor cx,cx
xor dx,dx
DosInt ; ... and return EOF position in DX:AX
mov si,dx
shl esi,16
mov si,ax
pop_ dx,cx
mov ax,4200h ; seek to saved position
DosInt
mov eax,esi
pop_ cx,dx,esi
ret
get_filesize endp