Re-apply 14698 - 14762 and fix resulting problem
[reactos.git] / reactos / boot / freeldr / freeldr / arch / powerpc / mach.c
1 /*
2 * FreeLoader PowerPC Part
3 * Copyright (C) 2005 Art Yerkes
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 #include "freeldr.h"
20 #include "machine.h"
21 #include "of.h"
22
23 extern void BootMain( char * );
24 extern char *GetFreeLoaderVersionString();
25 ULONG BootPartition = 0;
26 ULONG BootDrive = 0;
27
28 of_proxy ofproxy;
29 void *PageDirectoryStart, *PageDirectoryEnd;
30 static int chosen_package, stdin_handle;
31 BOOLEAN AcpiPresent = FALSE;
32 char BootPath[0x100];
33
34 void le_swap( const void *start_addr_v,
35 const void *end_addr_v,
36 const void *target_addr_v ) {
37 long *start_addr = (long *)ROUND_DOWN((long)start_addr_v,8),
38 *end_addr = (long *)ROUND_UP((long)end_addr_v,8),
39 *target_addr = (long *)ROUND_DOWN((long)target_addr_v,8);
40 long tmp;
41 while( start_addr <= end_addr ) {
42 tmp = start_addr[0];
43 target_addr[0] = REV(start_addr[1]);
44 target_addr[1] = REV(tmp);
45 start_addr += 2;
46 target_addr += 2;
47 }
48 }
49
50 int ofw_finddevice( const char *name ) {
51 int ret, len;
52
53 len = strlen(name);
54 le_swap( name, name + len, name );
55 ret = ofproxy( 0, (char *)name, NULL, NULL, NULL );
56 le_swap( name, name + len, name );
57 return ret;
58 }
59
60 int ofw_getprop( int package, const char *name, void *buffer, int buflen ) {
61 int ret, len = strlen(name);
62 le_swap( name, name + len, name );
63 le_swap( buffer, buffer + buflen, buffer );
64 ret = ofproxy
65 ( 4, (void *)package, (char *)name, buffer, (void *)buflen );
66 le_swap( buffer, buffer + buflen, buffer );
67 le_swap( name, name + len, name );
68 return ret;
69 }
70
71 int ofw_write( int handle, const char *data, int len ) {
72 int ret;
73 le_swap( data, data + len, data );
74 ret = ofproxy
75 ( 8, (void *)handle, (char *)data, (void *)len, NULL );
76 le_swap( data, data + len, data );
77 return ret;
78 }
79
80 int ofw_read( int handle, const char *data, int len ) {
81 int ret;
82 le_swap( data, data + len, data );
83 ret = ofproxy
84 ( 12, (void *)handle, (char *)data, (void *)len, NULL );
85 le_swap( data, data + len, data );
86 return ret;
87 }
88
89 void ofw_exit() {
90 ofproxy( 16, NULL, NULL, NULL, NULL );
91 }
92
93 void ofw_dumpregs() {
94 ofproxy( 20, NULL, NULL, NULL, NULL );
95 }
96
97 void ofw_print_string( const char *str ) {
98 int len = strlen(str);
99 le_swap( (char *)str, str + len, (char *)str );
100 ofproxy( 24, (void *)str, NULL, NULL, NULL );
101 le_swap( (char *)str, str + len, (char *)str );
102 }
103
104 void ofw_print_number( int num ) {
105 ofproxy( 28, (void *)num, NULL, NULL, NULL );
106 }
107
108 void PpcPutChar( int ch ) {
109 char buf[3];
110 if( ch == 0x0a ) { buf[0] = 0x0d; buf[1] = 0x0a; }
111 else { buf[0] = ch; buf[1] = 0; }
112 buf[2] = 0;
113 ofw_print_string( buf );
114 }
115
116 BOOL PpcConsKbHit() {
117 return TRUE;
118 }
119
120 int PpcConsGetCh() {
121 char buf;
122 ofw_read( stdin_handle, &buf, 1 );
123 return buf;
124 }
125
126 void PpcVideoClearScreen( UCHAR Attr ) {
127 ofw_print_string("ClearScreen\n");
128 }
129
130 VIDEODISPLAYMODE PpcVideoSetDisplayMode( char *DisplayMode, BOOL Init ) {
131 printf( "DisplayMode: %s %s\n", DisplayMode, Init ? "true" : "false" );
132 return VideoGraphicsMode;
133 }
134
135 /* FIXME: Query */
136 VOID PpcVideoGetDisplaySize( PULONG Width, PULONG Height, PULONG Depth ) {
137 ofw_print_string("GetDisplaySize\n");
138 *Width = 640;
139 *Height = 480;
140 *Depth = 8;
141 }
142
143 ULONG PpcVideoGetBufferSize() {
144 ULONG Width, Height, Depth;
145 ofw_print_string("PpcVideoGetBufferSize\n");
146 PpcVideoGetDisplaySize( &Width, &Height, &Depth );
147 return Width * Height * Depth / 8;
148 }
149
150 VOID PpcVideoSetTextCursorPosition( ULONG X, ULONG Y ) {
151 printf("SetTextCursorPosition(%d,%d)\n", X,Y);
152 }
153
154 VOID PpcVideoHideShowTextCursor( BOOL Show ) {
155 printf("HideShowTextCursor(%s)\n", Show ? "true" : "false");
156 }
157
158 VOID PpcVideoPutChar( int Ch, UCHAR Attr, unsigned X, unsigned Y ) {
159 printf( "\033[%d;%dH%c", Y, X, Ch );
160 }
161
162 VOID PpcVideoCopyOffScreenBufferToVRAM( PVOID Buffer ) {
163 printf( "CopyOffScreenBufferToVRAM(%x)\n", Buffer );
164 }
165
166 BOOL PpcVideoIsPaletteFixed() {
167 return FALSE;
168 }
169
170 VOID PpcVideoSetPaletteColor( UCHAR Color,
171 UCHAR Red, UCHAR Green, UCHAR Blue ) {
172 printf( "SetPaletteColor(%x,%x,%x,%x)\n", Color, Red, Green, Blue );
173 }
174
175 VOID PpcVideoGetPaletteColor( UCHAR Color,
176 UCHAR *Red, UCHAR *Green, UCHAR *Blue ) {
177 printf( "GetPaletteColor(%x)\n", Color);
178 }
179
180 VOID PpcVideoSync() {
181 printf( "Sync\n" );
182 }
183
184 VOID PpcVideoPrepareForReactOS() {
185 printf( "PrepareForReactOS\n");
186 }
187 /* XXX FIXME:
188 * According to the linux people (this is backed up by my own experience),
189 * the memory object in older ofw does not do getprop right.
190 *
191 * The "right" way is to probe the pci bridge. *sigh*
192 */
193 ULONG PpcGetMemoryMap( PBIOS_MEMORY_MAP BiosMemoryMap,
194 ULONG MaxMemoryMapSize ) {
195 printf("GetMemoryMap(chosen=%x)\n", chosen_package);
196
197 BiosMemoryMap[0].Type = MEMTYPE_USABLE;
198 BiosMemoryMap[0].BaseAddress = 0;
199 BiosMemoryMap[0].Length = 32 * 1024 * 1024; /* Assume 32 meg for now */
200
201 printf( "Returning memory map (%dk total)\n",
202 (int)BiosMemoryMap[0].Length / 1024 );
203
204 return 1;
205 }
206
207 BOOL PpcDiskReadLogicalSectors( ULONG DriveNumber, ULONGLONG SectorNumber,
208 ULONG SectorCount, PVOID Buffer ) {
209 printf("DiskReadLogicalSectors\n");
210 return FALSE;
211 }
212
213 BOOL PpcDiskGetPartitionEntry( ULONG DriveNumber, ULONG PartitionNumber,
214 PPARTITION_TABLE_ENTRY PartitionTableEntry ) {
215 printf("GetPartitionEntry(%d,%d)\n", DriveNumber, PartitionNumber);
216 return FALSE;
217 }
218
219 BOOL PpcDiskGetDriveGeometry( ULONG DriveNumber, PGEOMETRY DriveGeometry ) {
220 printf("GetGeometry(%d)\n", DriveNumber);
221 return FALSE;
222 }
223
224 ULONG PpcDiskGetCacheableBlockCount( ULONG DriveNumber ) {
225 printf("GetCacheableBlockCount\n");
226 return 0;
227 }
228
229 VOID PpcRTCGetCurrentDateTime( PULONG Hear, PULONG Month, PULONG Day,
230 PULONG Hour, PULONG Minute, PULONG Second ) {
231 printf("RTCGeturrentDateTime\n");
232 }
233
234 VOID PpcHwDetect() {
235 }
236
237 void PpcInit( of_proxy the_ofproxy ) {
238 ofproxy = the_ofproxy;
239 chosen_package = ofw_finddevice( "/chosen" );
240
241 ofw_getprop( chosen_package, "stdin",
242 &stdin_handle, sizeof(stdin_handle) );
243
244 stdin_handle = REV(stdin_handle);
245
246 MachVtbl.ConsPutChar = PpcPutChar;
247 MachVtbl.ConsKbHit = PpcConsKbHit;
248 MachVtbl.ConsGetCh = PpcConsGetCh;
249
250 printf("chosen_package = %x\n", chosen_package);
251
252 MachVtbl.VideoClearScreen = PpcVideoClearScreen;
253 MachVtbl.VideoSetDisplayMode = PpcVideoSetDisplayMode;
254 MachVtbl.VideoGetDisplaySize = PpcVideoGetDisplaySize;
255 MachVtbl.VideoGetBufferSize = PpcVideoGetBufferSize;
256 MachVtbl.VideoSetTextCursorPosition = PpcVideoSetTextCursorPosition;
257 MachVtbl.VideoHideShowTextCursor = PpcVideoHideShowTextCursor;
258 MachVtbl.VideoPutChar = PpcVideoPutChar;
259 MachVtbl.VideoCopyOffScreenBufferToVRAM =
260 PpcVideoCopyOffScreenBufferToVRAM;
261 MachVtbl.VideoIsPaletteFixed = PpcVideoIsPaletteFixed;
262 MachVtbl.VideoSetPaletteColor = PpcVideoSetPaletteColor;
263 MachVtbl.VideoGetPaletteColor = PpcVideoGetPaletteColor;
264 MachVtbl.VideoSync = PpcVideoSync;
265 MachVtbl.VideoPrepareForReactOS = PpcVideoPrepareForReactOS;
266
267 MachVtbl.GetMemoryMap = PpcGetMemoryMap;
268
269 MachVtbl.DiskReadLogicalSectors = PpcDiskReadLogicalSectors;
270 MachVtbl.DiskGetPartitionEntry = PpcDiskGetPartitionEntry;
271 MachVtbl.DiskGetDriveGeometry = PpcDiskGetDriveGeometry;
272 MachVtbl.DiskGetCacheableBlockCount = PpcDiskGetCacheableBlockCount;
273
274 MachVtbl.RTCGetCurrentDateTime = PpcRTCGetCurrentDateTime;
275
276 MachVtbl.HwDetect = PpcHwDetect;
277
278 printf( "FreeLDR version [%s]\n", GetFreeLoaderVersionString() );
279 BootMain("freeldr-ppc");
280 }
281
282 void MachInit(char *CmdLine) {
283 int len;
284 printf( "Determining boot device:\n" );
285 len = ofw_getprop(chosen_package, "bootpath",
286 BootPath, sizeof(BootPath));
287 printf( "Got %d bytes of path\n", len );
288 BootPath[len] = 0;
289 printf( "Boot Path: %s\n", BootPath );
290
291 printf( "FreeLDR starting\n" );
292 }
293
294 void FrLdrSetupPageDirectory() {
295 }
296
297 void beep() {
298 }
299
300 UCHAR STDCALL READ_PORT_UCHAR(PUCHAR Address) {
301 return 0xff;
302 }
303
304 void WRITE_PORT_UCHAR(PUCHAR Address, UCHAR Value) {
305 }