xref: /haiku/src/system/boot/platform/atari_m68k/shell.S (revision 020cbad9d40235a2c50a81a42d69912a5ff8fbc4)
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/zbeos 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/boot/platform/atari_m68k/prg.ld generated/objects/haiku/m68k/release/system/boot/boot_loader_atari_m68k
29 */
30
31#include "atari_memory_map.h"
32#include "toscalls.h"
33
34// 1 enabled verbose output
35//#define DEBUG 1
36
37#define GLOBAL(x) .globl x ; x
38#define FUNCTION(x) .global x; .type x,@function; x
39
40#define DRIVE_RETRIES 3
41	// when the drive reading fails for some reason, it will
42	// retry this many times until it will report a failure
43
44//.text
45_bs_entry:
46/* main entry point, both from the floppy boot and .prg */
47	bra.s	real_entry
48
49//FAT lookalike to avoid nasty things from happening
50// http://alive.atari.org/alive10/btmania.php
51	.ascii	"Haiku0"
52	.byte	0xbe, 0x50, 0x38 // id
53	//LE
54	.byte	0x00, 0x02	//BPS
55	.byte	0x02		//SPC
56	//.byte	0x00		//???
57	.byte	0x00, 0x02	//RES - number of reserved sectors
58	.byte	0x00//0x02	//NFATS
59	.byte	0x00/*70*/, 0x00	//NDIRS
60	.byte	0xa0, 0x05		//NSECTS
61	.byte	0xf8			//MEDIA
62	.byte	0x05, 0x00		//SPF
63	.byte	0x09, 0x00		//SPT
64	.byte	0x02, 0x00		//NSIDES
65	.byte	0x00, 0x00		//NHID
66	// we're done
67
68sNumSectors:
69	// this location will contain the length of the boot loader as
70	// written by the "makeflop" command in 512 byte blocks
71	// 0x180 is the allowed maximum, as the zipped TAR with the
72	// kernel and the boot module might start at offset 192 kB
73	.word	0x0300 //0x0180
74
75real_entry:
76
77#if 0
78	lea		h1,%a0
79	bsr		puts
80	move.l		#0x1234aa55,%d0
81	bsr		putx
82#endif
83
84
85	// first, determine if .prg (user) or bootsect (super)
86	// Super()
87	move.l		#SUP_INQUIRE,-(%sp)
88	move.w		#0x20,-(%sp)
89	trap		#1
90	addq.l		#6,%sp
91
92	bsr		putx
93
94	cmp.l		#SUP_USER,%d0
95	bne			floppy_start
96	lea		h2,%a0
97	bsr		puts
98
99	bra			prg_start
100	//bra			bootsect_start
101
102/*
103 * floppy boot support code
104 */
105
106floppy_start:
107	lea		h3,%a0
108	bsr		puts
109	lea		str,%a0
110	bsr		puts
111	//bra floppy_start
112
113	// no interrupt
114	or.w		#0x0700,%sr
115
116	//XXX: check for enough RAM
117
118	// load the rest
119	move.w		sNumSectors,%d2
120	// load at base + this code.
121	move.l		#(ATARI_ZBEOS_BASE+512),%a2
122	//move.l		%a2,%d0
123	//bsr putx
124	bsr			load_sectors
125	tst.w			%d0
126	beq			floppy_done
127	lea			failure_string,%a0
128	bsr			puts
129	bra			spin
130floppy_done:
131	lea		h4,%a0
132	bsr		puts
133	// setup stack
134	move.l		#ATARI_ZBEOS_STACK_BASE,%sp
135	lea		h5,%a0
136	bsr		puts
137	move.l		#0,%d0
138	//jmp			ATARI_ZBEOS_BASE+512
139	jmp		_start
140
141	//rts
142
143/**	Loads %d2 sectors from floppy disk, starting at head XXX %dh, sector %cx.
144 *	The data is loaded to %a2.
145 */
146
147load_sectors:
148
149#if 1
150	/* it seems to skip 9 every 9 sectors, buggy side handling ? */
151	// Rwabs
152	//move.l		#1,-(%sp)
153	move.w		#0,-(%sp)	// A:
154	//move.w		#2,-(%sp)	// C:
155	//move.w		#-1,-(%sp)	// 2nd sector
156	move.w		#1,-(%sp)	// 2nd sector
157	move.w		%d2,-(%sp)
158	move.l		%a2,-(%sp)
159	//move.w		#RW_READ+RW_NOTRANSLATE,-(%sp)
160	move.w		#RW_READ+RW_NOMEDIACH,-(%sp)
161	move.w		#4,-(%sp)
162	trap		#13
163	add.l		#14,%sp
164#endif
165#if 0
166	// d2:
167
168	//move.w		%d2,-(%sp)
169	move.w		#9,-(%sp)
170	move.w		#0,-(%sp)
171	move.w		#0,-(%sp)
172	move.w		#1,-(%sp)
173	move.w		#0,-(%sp)
174	move.l		#0,-(%sp)
175	//move.w		#0,-(%sp)
176	//move.l		%a2,-(%sp)
177	move.l		#ATARI_ZBEOS_BASE,-(%sp)
178	move.w		#8,-(%sp)	// floprd
179	trap		#XBIOS_TRAP
180	add.l		#20,%sp
181#endif
182
183	bsr	putx
184	rts
185
186floppy_end:
187//	.org	FAILURE_STRING
188failure_string:
189//	.string " Loading failed! Press key to reboot.\r\n"
190
191//	.org	DOT_STRING
192	.string	"."
193
194/*
195 * \AUTO\HAIKU.PRG and ARAnyM BOOTSTRAP() support code
196 */
197
198prg_start:
199	//lea		h4,%a0
200	//bsr		puts
201
202	// .prg:
203	// we need to switch to supervisor mode anyway
204	move.l		#SUP_SET,-(%sp)
205	move.w		#0x20,-(%sp)
206	trap		#1
207	addq.l		#6,%sp
208	move.l		%d0,saved_super_stack
209
210#if 1
211			//_membot
212	move.l		#0x432,%a0
213	move.l		(%a0),%d0
214	bsr		putx
215
216			//_memtop
217	move.l		#0x436,%a0
218	move.l		(%a0),%d0
219	bsr		putx
220
221			//_v_bas_ad
222	move.l		#0x44e,%a0
223	move.l		(%a0),%d0
224	bsr		putx
225#endif
226	// disable interrupts
227	//or.w		#0x0700,%sr
228
229	lea		h5,%a0
230	bsr		puts
231
232	// copy the rest of the prg
233	move.l		sNumSectors,%d0
234	sub.l		#1,%d0
235	lea		_bs_entry,%a0
236	move.l		#ATARI_ZBEOS_BASE,%a1
237
238nextsect:
239	move.l		#512/4-1,%d1
240copysect_loop:
241	move.l		(%a0)+,(%a1)+
242	dbf		%d1,copysect_loop
243	dbf		%d0,nextsect
244
245	lea		msg_j1,%a0
246	bsr		puts
247	// all done
248spin:
249	//bra			spin
250
251super_done:
252	// XXX: copy the rest !
253	move.l		#0,%d0
254	//jmp			ATARI_ZBEOS_BASE+512
255	jmp			_start
256
257saved_super_stack:
258	.long	0
259
260GLOBAL(gBootedFromImage):
261	.byte	0
262
263GLOBAL(gBootDriveID):
264	.byte	0
265
266GLOBAL(gBootPartitionOffset):
267	.long	0
268
269
270
271putx:
272	movem.l	%d0-%d2/%a0-%a2,-(%sp)
273	move.l	#8-1,%d2
274	move.l	%d0,%d1
275putxloop:
276	move.l	%d1,%d0
277	lsl.l	#4,%d1
278	//swap	%d0
279	//lsr.l	#8,%d0
280	//lsr.l	#4,%d0
281	rol.l	#4,%d0
282	and.l	#0x0f,%d0
283	cmp.b	#9,%d0
284	ble	putx0
285	add.b	#'a'-'0'-10,%d0
286	//bra	putxdisp
287putx0:
288	add.b	#'0',%d0
289putxdisp:
290	bsr	putc
291	dbf	%d2,putxloop
292	move.b	#'\r',%d0
293	bsr	putc
294	move.b	#'\n',%d0
295	bsr	putc
296	movem.l	(%sp)+,%d0-%d2/%a0-%a2
297	rts
298
299	lea.l	str,%a0
300puts:
301.loopt:
302	move.b	(%a0)+,%d0
303	beq	.strout
304	bsr	putc
305	bra	.loopt
306.strout:
307	move.b	#'\r',%d0
308	bsr	putc
309	move.b	#'\n',%d0
310	bsr	putc
311	rts
312
313.loop:
314	move	#'.',%d0
315	bsr		putc
316	bra		.loop
317	rts
318
319
320/* prints the char in d0.b to the console */
321putc:
322	movem.l	%d0-%d2/%a0-%a2,-(%sp)
323	move.w	%d0,-(%sp)
324	move.w	#DEV_CON,-(%sp)	// DEV_CON
325	move.w	#3,-(%sp)	// Bconout
326	trap	#13
327	add.l	#6,%sp
328	movem.l	(%sp)+,%d0-%d2/%a0-%a2
329	rts
330
331str:
332	.string "Haiku!"
333h1:
334	.string "H1"
335h2:
336	.string "H2"
337h3:
338	.string "H3"
339h4:
340	.string "H4"
341h5:
342	.string "H5"
343unimpl:
344	.string "Unimplemented."
345msg_j1:
346	.string "jumping to zbeos."
347
348
349shell_end:
350	//.fill	(0x01fe - shell_end), 1, 0x55
351	.org	0x01fe
352	.word	0xaa55-1	// will be replaced by the one calculated by the build.
353				// we make sure we PCs don't try to execute it.
354		// this bumps the "start" label to offset 0x0200 as
355		// expected by the BFS boot loader, and also marks
356		// this block as valid boot block for the BIOS
357		// XXX: actually TOS wants a real checksum here so sum is 0x1234!
358
359