1/*
2 * Copyright 2004-2005, Axel D��rfler, axeld@pinc-software.de. All rights reserved.
3 * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net.
4 * Copyright 2007, Haiku, Inc. All Rights Reserved.
5 * Distributed under the terms of the MIT license.
6 *
7 * Author:
8 *		Fran��ois Revol, revol@free.fr.
9 */
10
11/**	This file contains the boot floppy and BFS boot block entry points for
12 *	the stage 2 boot loader.
13 * x86 ahead:
14 *	The floppy entry point is at offset 0. It's loaded at 0x07c0:0x000. It
15 *	will load the rest of the loader to 0x1000:0x0200 and execute it.
16 *	The BFS boot block will load the whole stage 2 loader to 0x1000:0x0000
17 *	and will then jump to 0x1000:0x0200 as its entry point.
18 *	This code will then switch to protected mode and will directly call
19 *	the entry function of the embedded ELF part of the loader.
20 */
21
22/*
23 * generate boot floppy:
24 * cd src/system/boot/platform/atari_m68k/ ; make fixup_tos_floppy_chksum; cd -
25 * dd if=generated/objects/haiku/m68k/release/system/boot/haiku_loader of=~/floppy.img bs=512 count=20 conv=notrunc
26 * src/system/boot/platform/atari_m68k/fixup_tos_floppy_chksum ~/floppy.img
27 * generate .prg:
28 * generated.m68k/cross-tools/bin/m68k-unknown-haiku-ld -o haiku.prg -T src/system/ldscripts/m68k/boot_prg_atari_m68k.ld generated/objects/haiku/m68k/release/system/boot/boot_loader_atari_m68k
29 */
30
31/*
32 * references :
33 * http://ftp.netbsd.org/pub/NetBSD/NetBSD-release-4-0/src/sys/arch/amiga/stand/bootblock/
34 * http://wandel.ca/homepage/execdis/virus_disassembly.txt
35 * http://ciarang.com/wiki/page/Minimal_Amiga_Boot_Code
36 *
37 */
38
39#include "amiga_memory_map.h"
40#include "rom_calls.h"
41
42
43// 1 enabled verbose output
44//#define DEBUG 1
45
46#define GLOBAL(x) .globl x ; x
47#define FUNCTION(x) .global x; .type x,@function; x
48
49#define DRIVE_RETRIES 3
50	// when the drive reading fails for some reason, it will
51	// retry this many times until it will report a failure
52
53
54#define SECTSIZE 512
55
56// .text
57_bs_entry:
58	// Amiga boot sector
59_floppy_entry:
60	.ascii	"DOS"
61	.byte	0
62_floppy_checksum:
63	.long	0
64_floppy_osversion: // ??
65	//.long	0x370
66	.long	0x09D5A859 // dos private ???
67
68/*
69 * floppy boot support code
70 */
71
72floppy_start:
73	// save some regs just in case
74	movem.l		%d1-%d7/%a0-%a6,-(%sp)
75
76	// save the provided IOReq pointer
77	lea		sBootIOReq(%pc),%a2
78	move.l	%a1,(%a2)
79
80	// seems like a6 is already set to ExecBase when called from the ROM ?
81	move.l	4.w,%a6	// exec base
82	//jsr	_LVOFindResident(%a6)
83	lea	SysBase(%pc),%a2
84	move.l	%a6,(%a2)
85
86
87//	lea	dosname_boot(%pc),%a1
88//	move.l	4.w,%a6	// exec base
89//	jsr	_LVOFindResident(%a6)
90//	lea	DosBase(%pc),%a2
91//	tst.l	%d0
92//	beq.s	_floppy_err
93//	move.l	%d0,(%a2)
94
95	// needed to display alerts
96	lea	intname_boot(%pc),%a1
97	jsr	_LVOOldOpenLibrary(%a6)
98	tst.l	%d0
99	beq		_floppy_err
100	lea	IntuitionBase(%pc),%a2
101	move.l	%d0,(%a2)
102
103	// allocate absolute memory to put the stack & bootloader at,
104	// just to make sure AmigaOS won't write there.
105
106	move.l	#(AMIGA_ZBEOS_STACK_END-AMIGA_ZBEOS_STACK_BASE+AMIGA_ZBEOS_MAX),%d0
107	move.l	#AMIGA_ZBEOS_STACK_BASE,%a1
108	jsr		_LVOAllocAbs(%a6)
109	tst.l	%d0
110	beq.s	_floppy_err_alert
111
112	//lea		alert_extra(%pc),%a2
113	//addq.b	#1,(%a2)
114	move.l	#0xdeadbeef,%d0
115	bsr		putx
116
117	// copy haiku_loader to AMIGA_ZBEOS_BASE
118
119
120
121	lea		sBootIOReq,%a1
122	move.l	(%a1),%a1
123	move	#CMD_READ,28(%a1)				//io_Command
124	//move.l	#0x200,%d0
125	//XXX
126	//mulu	sNumSectors,%d0
127	//move.l	%d0,36(%a1)				//io_Length
128	move.l	#0x200,36(%a1)				//io_Length
129	move.l	#AMIGA_ZBEOS_BASE,40(%a1)	//io_Data
130	move.l	#0,44(%a1)					//io_Offset
131	move.w	sNumSectors,%d2
132
133next_sector:
134	move.w	%d2,%d0
135	bsr		putx
136
137	move.l	%a1,-(%sp)
138	jsr		_LVODoIO(%a6)
139	move.l	(%sp)+,%a1
140
141	tst.l	%d0
142	bne	_floppy_err_alert
143
144	add.l	#0x200,40(%a1)			//io_Data
145	add.l	#0x200,44(%a1)			//io_Offset
146	//subq.w	#1,sNumSectors
147	subq.w	#1,%d2
148	bne		next_sector
149
150	move.l	#0xf100f100,%d0
151	bsr		putx
152
153	//lea		alert_extra(%pc),%a2
154	//move.b	#'F',(%a2)
155
156//	move.l
157//	cmp.l	32(%a1),36(%a1)
158//	blt.s	_floppy_err_alert
159
160	//lea		alert_extra(%pc),%a2
161	//addq.b	#1,(%a2)
162
163	bra.s	floppy_done
164
165
166//_continue:
167//	lea	dosname_boot(%pc),%a1
168//	move.l	4.w,%a6	// exec base
169//	jsr	_LVOFindResident(%a6)
170//	lea	_dosbase(%pc),%a2
171//	tst.l	%d0
172//	beq.s	_floppy_err
173//	move.l	%d0,(%a2)
174//	// pop up saved regs
175//	movem.l		(%sp)+,%d1-%d7/%a0-%a6
176//	move.l	_dosbase,%a0
177//	move.l	0x16(%a0),%a0
178//	moveq	#0,%d0
179//	rts
180
181GLOBAL(_floppy_err_alert):
182	lea		IntuitionBase(%pc),%a6
183	move.l	(%a6),%a6
184	lea		alert_data(%pc),%a0
185	moveq	#0,%d0
186	move.l	#30,%d1
187	jsr		_LVODisplayAlert(%a6)
188
189_floppy_err:
190	// pop up saved regs
191	movem.l		(%sp)+,%d1-%d7/%a0-%a6
192	moveq	#-1,%d0
193	rts
194
195
196
197
198
199
200
201floppy_done:
202	// setup stack
203	move.l		#AMIGA_ZBEOS_STACK_END,%sp
204	//jmp			AMIGA_ZBEOS_BASE+512
205
206	//move.w		TOSVAR_bootdev,%d0
207	// XXX:	 use uint16 ??
208	//move.b		%d0,AMIGA_ZBEOS_BASE + gBootDriveID - _bs_entry
209	move.b		#1,AMIGA_ZBEOS_BASE + gBootedFromImage - _bs_entry
210	// XXX: copy the rest !
211
212	move.b		#'S',AMIGA_ZBEOS_BASE + alert_extra - _bs_entry
213	// Copy open library handles
214	move.l		SysBase(%pc),AMIGA_ZBEOS_BASE + SysBase - _bs_entry
215	move.l		IntuitionBase(%pc),AMIGA_ZBEOS_BASE + IntuitionBase - _bs_entry
216
217	move.l		#0,%d0
218
219	// jump to C++ code
220	jmp		_start
221
222
223putx:
224	movem.l	%d0-%d2/%a0-%a2,-(%sp)
225	lea		alert_extra(%pc),%a2
226	move.l	#8-1,%d2
227	move.l	%d0,%d1
228putxloop:
229	move.l	%d1,%d0
230	lsl.l	#4,%d1
231	//swap	%d0
232	//lsr.l	#8,%d0
233	//lsr.l	#4,%d0
234	rol.l	#4,%d0
235	and.l	#0x0f,%d0
236	cmp.b	#9,%d0
237	ble	putx0
238	add.b	#'a'-'0'-10,%d0
239	//bra	putxdisp
240putx0:
241	add.b	#'0',%d0
242putxdisp:
243	move.b	%d0,(%a2)+
244	dbf	%d2,putxloop
245//	bsr	putcrlf
246	movem.l	(%sp)+,%d0-%d2/%a0-%a2
247	rts
248
249
250	// ATARI
251#if 0
252
253
254floppy_end:
255//	.org	FAILURE_STRING
256failure_string:
257//	.string " Loading failed! Press key to reboot.\r\n"
258	.string " Loading failed! Press key.\r\n"
259//	.string "FAIL"
260
261//	.org	DOT_STRING
262//	.string	"."
263
264str:
265	.string "Haiku!"
266msg_j1:
267	.string "Jumping to haiku_loader."
268
269#endif
270
271sNumSectors:
272	// this location will contain the length of the boot loader as
273	// written by the "makeflop" command in 512 byte blocks
274	// 0x180 is the allowed maximum, as the zipped TAR with the
275	// kernel and the boot module might start at offset 192 kB
276	//.word	0x0300 //0x0180
277	.word	BOOT_ARCHIVE_IMAGE_OFFSET*2
278
279sBootIOReq:
280	.long	0
281
282dosname_boot:
283	.ascii	"dos.library"
284	.byte	0
285
286intname_boot:
287	.ascii	"intuition.library"
288	.byte	0
289
290alert_data:
291	.word	10
292	.byte	12
293	.ascii	"Error loading Haiku: "
294alert_extra:
295	.ascii	"XXXXXXXX\0"
296	.byte	0
297
298GLOBAL(SysBase):
299	.long	0
300GLOBAL(DosBase):
301	.long	0
302GLOBAL(IntuitionBase):
303	.long	0
304
305end_buff:
306// equ *-_floppy_entry
307	.dcb.b	(1024)-(end_buff-_floppy_entry),0
308
309
310
311//XXX: put bfs_start here
312
313/*
314 * \AUTO\HAIKU.PRG and ARAnyM BOOTSTRAP() support code
315 */
316
317#if 0
318
319super_done:
320	// XXX: copy the rest !
321	move.b		#AMIGA_BOOT_DRVAPI_FLOPPY,AMIGA_ZBEOS_BASE + gBootDriveAPI - _bs_entry
322	move.b		#0,AMIGA_ZBEOS_BASE + gBootDriveID - _bs_entry
323	move.b		#1,AMIGA_ZBEOS_BASE + gBootedFromImage - _bs_entry
324
325	move.l		#0,%d0
326#endif
327
328	//jmp			AMIGA_ZBEOS_BASE+512
329	jmp			_start
330
331saved_super_stack:
332	.long	0
333
334GLOBAL(gBootedFromImage):
335	.byte	0
336
337GLOBAL(gBootDriveID):
338	.byte	0
339
340GLOBAL(gBootPartitionOffset):
341	.long	0
342
343