]> git.sur5r.net Git - cc65/blob - libsrc/lynx/bootldr.s
Added a new flags byte to the TGI headers. Bumped the API version.
[cc65] / libsrc / lynx / bootldr.s
1 ;
2 ; Karri Kaksonen, 2011
3 ;
4 ; This bootloader creates a signed binary so that the Lynx will accept it.
5 ;
6         .include "lynx.inc"
7         .include "extzp.inc"
8         .import         __BLOCKSIZE__
9         .export         __BOOTLDR__: absolute = 1
10
11
12 ; ------------------------------------------------------------------------
13 ; Bootloader
14
15         .segment "BOOTLDR"
16 ;**********************************
17 ; Here is the bootloader in plaintext
18 ; The idea is to make the smalles possible encrypted loader as decryption
19 ; is very slow. The minimum size is 49 bytes plus a zero byte.
20 ;**********************************
21 ;       EXE = $f000
22 ;
23 ;       .org $0200
24 ;
25 ;       ; 1. force Mikey to be in memory
26 ;       stz     MAPCTL
27 ;
28 ;       ; 3. set ComLynx to open collector
29 ;       lda #4          ; a = 00000100
30 ;       sta SERCTL      ; set the ComLynx to open collector
31 ;
32 ;       ; 4. make sure the ROM is powered on
33 ;       lda #8          ; a = 00001000
34 ;       sta IODAT       ; set the ROM power to on
35 ;
36 ;       ; 5. read in secondary exe + 8 bytes from the cart and store it in $f000
37 ;       ldx #0          ; x = 0
38 ;       ldy #$AB        ; y = secondary loader size (171 bytes)
39 ;rloop1: lda RCART0     ; read a byte from the cart
40 ;       sta EXE,X       ; EXE[X] = a
41 ;       inx             ; x++
42 ;       dey             ; y--
43 ;       bne rloop1      ; loops until y wraps
44 ;
45 ;       ; 6. jump to secondary loader
46 ;       jmp EXE         ; run the secondary loader
47 ;
48 ;       .reloc
49 ;**********************************
50 ; After compilation, encryption and obfuscation it turns into this.
51 ;**********************************
52         .byte $ff, $dc, $e3, $bd, $bc, $7f, $f8, $94 
53         .byte $b7, $dd, $68, $bb, $da, $5b, $50, $5c 
54         .byte $ea, $9f, $2b, $df, $96, $80, $3f, $7e 
55         .byte $ef, $15, $81, $ae, $ad, $e4, $6e, $b3 
56         .byte $46, $d7, $72, $58, $f7, $76, $8a, $4a 
57         .byte $c7, $99, $bd, $ff, $02, $3e, $5b, $3f 
58         .byte $0c, $49, $1b, $22
59
60 ;**********************************
61 ; Now we have the secondary loader
62 ;**********************************
63         .org $f000
64         ; 1. Read in the 1st File-entry (main exe) in FileEntry
65         ldx #$00
66         ldy #8
67 rloop:  lda RCART0      ; read a byte from the cart
68         sta _FileEntry,X ; EXE[X] = a
69         inx
70         dey
71         bne rloop
72
73         ; 2. Set the block hardware to the main exe start
74         lda     _FileStartBlock
75         sta     _FileCurrBlock
76         jsr     seclynxblock
77
78         ; 3. Skip over the block offset
79         lda     _FileBlockOffset
80         ldx     _FileBlockOffset+1
81         phx                             ; The BLL kit uses negative offsets
82         plx                             ; while the basic Lynx uses positive
83         bmi     @1                      ; Make all offsets negative
84         eor     #$FF
85         pha
86         txa
87         eor     #$FF
88         bra     @2
89 @1:     pha
90         txa
91 @2:     tay
92         plx
93         jsr     seclynxskip0
94
95         ; 4. Read in the main exe to RAM
96         lda     _FileDestAddr
97         ldx     _FileDestAddr+1
98         sta     _FileDestPtr
99         stx     _FileDestPtr+1
100         lda     _FileFileLen
101         ldx     _FileFileLen+1
102         phx                     ; The BLL kit uses negative counts
103         plx                     ; while the basic Lynx uses positive
104         bmi     @3              ; make all counts negative
105         eor     #$FF
106         pha
107         txa
108         eor     #$FF
109         bra     @4
110 @3:     pha
111         txa
112 @4:     tay
113         plx
114         jsr     seclynxread0
115
116         ; 5. Jump to start of the main exe code
117         jmp     (_FileDestAddr)
118
119 ;**********************************
120 ; Skip bytes on bank 0
121 ; X:Y count (EOR $FFFF)
122 ;**********************************
123 seclynxskip0:
124         inx
125         bne @0
126         iny
127         beq exit
128 @0:     jsr secreadbyte0
129         bra seclynxskip0
130
131 ;**********************************
132 ; Read bytes from bank 0
133 ; X:Y count (EOR $ffff)
134 ;**********************************
135 seclynxread0:
136         inx
137         bne @1
138         iny
139         beq exit
140 @1:     jsr secreadbyte0
141         sta (_FileDestPtr)
142         inc _FileDestPtr
143         bne seclynxread0
144         inc _FileDestPtr+1
145         bra seclynxread0
146
147 ;**********************************
148 ; Read one byte from cartridge
149 ;**********************************
150 secreadbyte0:
151         lda RCART0
152         inc _FileBlockByte
153         bne exit
154         inc _FileBlockByte+1
155         bne exit
156
157 ;**********************************
158 ; Select a block 
159 ;**********************************
160 seclynxblock:
161         pha
162         phx
163         phy
164         lda __iodat
165         and #$fc
166         tay
167         ora #2
168         tax
169         lda _FileCurrBlock
170         inc _FileCurrBlock
171         sec
172         bra @2
173 @0:     bcc @1
174         stx IODAT
175         clc
176 @1:     inx
177         stx SYSCTL1
178         dex
179 @2:     stx SYSCTL1
180         rol
181         sty IODAT
182         bne @0
183         lda __iodat
184         sta IODAT
185         stz _FileBlockByte
186         lda #<($100-(>__BLOCKSIZE__))
187         sta _FileBlockByte+1
188         ply
189         plx
190         pla
191
192 exit:   rts
193         .reloc
194