- First stab at running linuxbios without the old static device tree.
[coreboot.git] / src / arch / i386 / init / crt0.S.lb
1 /* -*- asm -*-
2  * $ $
3  *
4  */
5
6 /* 
7  * Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
8  *
9  * This file is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * Originally this code was part of ucl the data compression library
15  * for upx the ``Ultimate Packer of eXecutables''.
16  *
17  * - Converted to gas assembly, and refitted to work with etherboot.
18  *   Eric Biederman 20 Aug 2002
19  * - Merged the nrv2b decompressor into crt0.base of LinuxBIOS
20  *   Eric Biederman 26 Sept 2002
21  */
22
23
24 #include <arch/asm.h>
25 #include <arch/intel.h>
26 #include <console/loglevel.h>   
27
28 /*
29  * This is the entry code the code in .reset section
30  * jumps to this address.
31  *
32  */
33 .section ".rom.data", "a", @progbits
34 .section ".rom.text", "ax", @progbits
35
36         intel_chip_post_macro(0x01)             /* delay for chipsets */
37
38 #include "crt0_includes.h"
39
40         /* uses:         esp, ebx, ax, dx */
41 #define __CONSOLE_TX_STRING(string)     \
42         mov     string, %ebx    ; \
43         CALLSP(console_tx_string)
44
45 #if ASM_CONSOLE_LOGLEVEL > BIOS_DEBUG
46 #define CONSOLE_DEBUG_TX_STRING(string)        __CONSOLE_TX_STRING(string)
47 #else
48 #define CONSOLE_DEBUG_TX_STRING(string)
49 #endif
50
51         /* clear boot_complete flag */
52         xorl    %ebp, %ebp
53 __main:
54         CONSOLE_DEBUG_TX_STRING($str_copying_to_ram)
55
56         /*
57          *      Copy data into RAM and clear the BSS. Since these segments
58          *      isn\'t really that big we just copy/clear using bytes, not
59          *      double words.
60          */
61         intel_chip_post_macro(0x11)             /* post 11 */
62
63         cld                             /* clear direction flag */
64         
65         /* copy linuxBIOS from it's initial load location to 
66          * the location it is compiled to run at.
67          * Normally this is copying from FLASH ROM to RAM.
68          */
69 #if !CONFIG_COMPRESS
70         movl    $_liseg, %esi
71         movl    $_iseg,  %edi
72         movl    $_eiseg, %ecx
73         subl    %edi, %ecx
74         rep     movsb
75 #else
76         leal    4+_liseg, %esi
77         leal    _iseg, %edi
78         movl    %ebp, %esp      /* preserve %ebp */
79         movl    $-1, %ebp       /* last_m_off = -1 */
80         jmp     dcl1_n2b
81
82 /* ------------- DECOMPRESSION -------------
83
84  Input:
85    %esi - source
86    %edi - dest
87    %ebp - -1
88    cld
89
90  Output:
91    %eax - 0
92    %ecx - 0
93 */
94
95 .macro getbit bits
96 .if     \bits == 1
97         addl    %ebx, %ebx
98         jnz     1f
99 .endif
100         movl    (%esi), %ebx
101         subl    $-4, %esi       /* sets carry flag */
102         adcl    %ebx, %ebx
103 1:
104 .endm
105
106 decompr_literals_n2b:
107         movsb
108
109 decompr_loop_n2b:
110         addl    %ebx, %ebx
111         jnz     dcl2_n2b
112 dcl1_n2b:
113         getbit  32
114 dcl2_n2b:
115         jc      decompr_literals_n2b
116         xorl    %eax, %eax
117         incl    %eax            /* m_off = 1 */
118 loop1_n2b:
119         getbit  1
120         adcl    %eax, %eax      /* m_off = m_off*2 + getbit() */
121         getbit  1
122         jnc     loop1_n2b       /* while(!getbit()) */
123         xorl    %ecx, %ecx
124         subl    $3, %eax
125         jb      decompr_ebpeax_n2b      /* if (m_off == 2) goto decompr_ebpeax_n2b ? */
126         shll    $8, %eax        
127         movb    (%esi), %al     /* m_off = (m_off - 3)*256 + src[ilen++] */
128         incl    %esi
129         xorl    $-1, %eax       
130         jz      decompr_end_n2b /* if (m_off == 0xffffffff) goto decomp_end_n2b */
131         movl    %eax, %ebp      /* last_m_off = m_off ?*/
132 decompr_ebpeax_n2b:
133         getbit  1               
134         adcl    %ecx, %ecx      /* m_len = getbit() */
135         getbit  1
136         adcl    %ecx, %ecx      /* m_len = m_len*2 + getbit()) */
137         jnz     decompr_got_mlen_n2b    /* if (m_len == 0) goto decompr_got_mlen_n2b */
138         incl    %ecx            /* m_len++ */
139 loop2_n2b:
140         getbit  1       
141         adcl    %ecx, %ecx      /* m_len = m_len*2 + getbit() */
142         getbit  1
143         jnc     loop2_n2b       /* while(!getbit()) */
144         incl    %ecx
145         incl    %ecx            /* m_len += 2 */
146 decompr_got_mlen_n2b:
147         cmpl    $-0xd00, %ebp
148         adcl    $1, %ecx        /* m_len = m_len + 1 + (last_m_off > 0xd00) */
149         movl    %esi, %edx
150         leal    (%edi,%ebp), %esi       /* m_pos = dst + olen + -m_off  */
151         rep
152         movsb                   /* dst[olen++] = *m_pos++ while(m_len > 0) */
153         movl    %edx, %esi
154         jmp     decompr_loop_n2b
155 decompr_end_n2b:
156         intel_chip_post_macro(0x12)             /* post 12 */
157
158         movl    %esp, %ebp
159 #endif
160
161         CONSOLE_DEBUG_TX_STRING($str_pre_main)
162         leal    _iseg, %edi
163         jmp     %edi
164
165 .Lhlt:  
166         intel_chip_post_macro(0xee)     /* post fe */
167         hlt
168         jmp     .Lhlt
169
170 #if ASM_CONSOLE_LOGLEVEL > BIOS_DEBUG
171         /* Uses esp, ebx, ax, dx  */
172 console_tx_string:
173         mov     (%ebx), %al
174         inc     %ebx
175         cmp     $0, %al
176         jne     9f
177         RETSP
178 9:
179 /* Base Address */
180 #ifndef TTYS0_BASE
181 #define TTYS0_BASE      0x3f8
182 #endif
183 /* Data */
184 #define TTYS0_RBR (TTYS0_BASE+0x00)
185
186 /* Control */
187 #define TTYS0_TBR TTYS0_RBR
188 #define TTYS0_IER (TTYS0_BASE+0x01)
189 #define TTYS0_IIR (TTYS0_BASE+0x02)
190 #define TTYS0_FCR TTYS0_IIR
191 #define TTYS0_LCR (TTYS0_BASE+0x03)
192 #define TTYS0_MCR (TTYS0_BASE+0x04)
193 #define TTYS0_DLL TTYS0_RBR
194 #define TTYS0_DLM TTYS0_IER
195
196 /* Status */
197 #define TTYS0_LSR (TTYS0_BASE+0x05)
198 #define TTYS0_MSR (TTYS0_BASE+0x06)
199 #define TTYS0_SCR (TTYS0_BASE+0x07)
200         
201         mov     %al, %ah
202 10:     mov     $TTYS0_LSR, %dx
203         inb     %dx, %al
204         test    $0x20, %al
205         je      10b
206         mov     $TTYS0_TBR, %dx
207         mov     %ah, %al
208         outb    %al, %dx
209
210         jmp console_tx_string   
211
212 .section ".rom.data"
213 str_copying_to_ram:  .string "Copying LinuxBIOS to ram.\r\n"
214 str_pre_main:        .string "Jumping to LinuxBIOS.\r\n"
215 .previous
216
217 #endif /* ASM_CONSOLE_LOGLEVEL > BIOS_DEBUG */