]> git.sur5r.net Git - cc65/blob - libsrc/lynx/bootldr.s
Removed (pretty inconsistently used) tab chars from source code base.
[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 = $fb68
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 #$97        ; y = secondary loader size (151 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, $81, $ca, $33, $be, $80, $a2, $c4 
53         .byte $6d, $98, $fe, $8d, $bc, $66, $c0, $7a 
54         .byte $09, $50, $23, $28, $18, $c8, $06, $70 
55         .byte $58, $4f, $1b, $e1, $c7, $90, $08, $cd 
56         .byte $1a, $6e, $5a, $45, $32, $d7, $6d, $c6 
57         .byte $8a, $e5, $d8, $5c, $a0, $e8, $4f, $7a 
58         .byte $5f, $73, $8d, $22
59
60 ;**********************************
61 ; Now we have the secondary loader
62 ;**********************************
63         .org $fb68
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+1
80         eor     #$FF
81         tay
82         lda     _FileBlockOffset
83         eor     #$FF
84         tax
85         jsr     seclynxskip0
86
87         ; 4. Read in the main exe to RAM
88         lda     _FileDestAddr
89         ldx     _FileDestAddr+1
90         sta     _FileDestPtr
91         stx     _FileDestPtr+1
92         lda     _FileFileLen+1
93         eor     #$FF
94         tay
95         lda     _FileFileLen
96         eor     #$FF
97         tax
98         jsr     seclynxread0
99
100         ; 5. Jump to start of the main exe code
101         jmp     (_FileDestAddr)
102
103 ;**********************************
104 ; Skip bytes on bank 0
105 ; X:Y count (EOR $FFFF)
106 ;**********************************
107 seclynxskip0:
108         inx
109         bne @0
110         iny
111         beq exit
112 @0:     jsr secreadbyte0
113         bra seclynxskip0
114
115 ;**********************************
116 ; Read bytes from bank 0
117 ; X:Y count (EOR $ffff)
118 ;**********************************
119 seclynxread0:
120         inx
121         bne @1
122         iny
123         beq exit
124 @1:     jsr secreadbyte0
125         sta (_FileDestPtr)
126         inc _FileDestPtr
127         bne seclynxread0
128         inc _FileDestPtr+1
129         bra seclynxread0
130
131 ;**********************************
132 ; Read one byte from cartridge
133 ;**********************************
134 secreadbyte0:
135         lda RCART0
136         inc _FileBlockByte
137         bne exit
138         inc _FileBlockByte+1
139         bne exit
140
141 ;**********************************
142 ; Select a block 
143 ;**********************************
144 seclynxblock:
145         pha
146         phx
147         phy
148         lda __iodat
149         and #$fc
150         tay
151         ora #2
152         tax
153         lda _FileCurrBlock
154         inc _FileCurrBlock
155         sec
156         bra @2
157 @0:     bcc @1
158         stx IODAT
159         clc
160 @1:     inx
161         stx SYSCTL1
162         dex
163 @2:     stx SYSCTL1
164         rol
165         sty IODAT
166         bne @0
167         lda __iodat
168         sta IODAT
169         stz _FileBlockByte
170         lda #<($100-(>__BLOCKSIZE__))
171         sta _FileBlockByte+1
172         ply
173         plx
174         pla
175
176 exit:   rts
177
178         .reloc
179