2 Routines for talking to the Open Firmware PROM on
3 Power Macintosh computers.
5 Copyright (C) 1999 Benjamin Herrenschmidt
6 Copyright (C) 1999 Marius Vollmer
7 Copyright (C) 1996 Paul Mackerras.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "asm/processor.h"
34 #define READ_BLOCKS_USE_READ 1
38 ihandle prom_stdin, prom_stdout;
40 //extern int vsprintf(char *buf, const char *fmt, va_list args);
42 static ihandle prom_mem, prom_mmu;
43 static ihandle prom_chosen, prom_options;
53 call_prom (const char *service, int nargs, int nret, ...)
57 struct prom_args prom_args;
59 prom_args.service = service;
60 prom_args.nargs = nargs;
61 prom_args.nret = nret;
62 va_start (list, nret);
63 for (i = 0; i < nargs; ++i)
64 prom_args.args[i] = va_arg(list, void *);
66 for (i = 0; i < nret; ++i)
67 prom_args.args[i + nargs] = 0;
70 return prom_args.args[nargs];
76 call_prom_return (const char *service, int nargs, int nret, ...)
81 struct prom_args prom_args;
83 prom_args.service = service;
84 prom_args.nargs = nargs;
85 prom_args.nret = nret;
86 va_start (list, nret);
87 for (i = 0; i < nargs; ++i)
88 prom_args.args[i] = va_arg(list, void *);
89 for (i = 0; i < nret; ++i)
90 prom_args.args[i + nargs] = 0;
91 if (prom (&prom_args) != 0)
92 return PROM_INVALID_HANDLE;
94 result = prom_args.args[nargs];
95 for (i=1; i<nret; i++) {
96 void** rp = va_arg(list, void**);
97 *rp = prom_args.args[i+nargs];
106 call_method_1 (char *method, prom_handle h, int nargs, ...)
110 struct prom_args prom_args;
112 prom_args.service = "call-method";
113 prom_args.nargs = nargs+2;
115 prom_args.args[0] = method;
116 prom_args.args[1] = h;
117 va_start (list, nargs);
118 for (i = 0; i < nargs; ++i)
119 prom_args.args[2+i] = va_arg(list, void *);
121 prom_args.args[2+nargs] = 0;
122 prom_args.args[2+nargs+1] = 0;
126 if (prom_args.args[2+nargs] != 0)
128 prom_printf ("method '%s' failed %p\n", method, prom_args.args[2+nargs]);
131 return prom_args.args[2+nargs+1];
136 prom_finddevice (char *name)
138 return call_prom ("finddevice", 1, 1, name);
142 prom_findpackage(char *path)
144 return call_prom ("find-package", 1, 1, path);
148 prom_getprop (prom_handle pack, char *name, void *mem, int len)
150 return (int)call_prom ("getprop", 4, 1, pack, name, mem, len);
154 prom_get_chosen (char *name, void *mem, int len)
156 return prom_getprop (prom_chosen, name, mem, len);
160 prom_get_options (char *name, void *mem, int len)
162 if (prom_options == (void *)-1)
164 return prom_getprop (prom_options, name, mem, len);
168 prom_init (prom_entry pp)
172 prom_chosen = prom_finddevice ("/chosen");
173 if (prom_chosen == (void *)-1)
175 prom_options = prom_finddevice ("/options");
176 if (prom_get_chosen ("stdout", &prom_stdout, sizeof(prom_stdout)) <= 0)
178 if (prom_get_chosen ("stdin", &prom_stdin, sizeof(prom_stdin)) <= 0)
179 prom_abort ("\nCan't open stdin");
180 if (prom_get_chosen ("memory", &prom_mem, sizeof(prom_mem)) <= 0)
181 prom_abort ("\nCan't get mem handle");
182 if (prom_get_chosen ("mmu", &prom_mmu, sizeof(prom_mmu)) <= 0)
183 prom_abort ("\nCan't get mmu handle");
185 // move cursor to fresh line
188 /* Add a few OF methods (thanks Darwin) */
190 prom_printf ("Adding OF methods...\n");
194 /* All values in this forth code are in hex */
196 /* Those are a few utilities ripped from Apple */
197 ": D2NIP decode-int nip nip ;\r" // A useful function to save space
198 ": GPP$ get-package-property 0= ;\r" // Another useful function to save space
199 ": ^on0 0= if -1 throw then ;\r" // Bail if result zero
200 ": $CM $call-method ;\r"
203 /* Some forth words used by the release method */
205 " \" /chosen\" find-package if "
206 "dup \" memory\" rot GPP$ if "
207 "D2NIP swap " // ( MEMORY-ihandle "/chosen"-phandle )
208 "\" mmu\" rot GPP$ if "
209 "D2NIP " // ( MEMORY-ihandle MMU-ihandle )
211 "0 " // ( MEMORY-ihandle 0 )
229 prom_printf ("OF interface initialized.\n");
234 prom_open (char *spec)
236 return call_prom ("open", 1, 1, spec, strlen(spec));
240 prom_close (prom_handle file)
242 call_prom ("close", 1, 0, file);
246 prom_read (prom_handle file, void *buf, int n)
254 result = (int)call_prom ("read", 3, 1, file, buf, n);
257 call_prom("interpret", 1, 1, " 10 ms");
264 prom_write (prom_handle file, void *buf, int n)
266 return (int)call_prom ("write", 3, 1, file, buf, n);
270 prom_seek (prom_handle file, int pos)
272 int status = (int)call_prom ("seek", 3, 1, file, 0, pos);
273 return status == 0 || status == 1;
277 prom_lseek (prom_handle file, unsigned long long pos)
279 int status = (int)call_prom ("seek", 3, 1, file,
280 (unsigned int)(pos >> 32), (unsigned int)(pos & 0xffffffffUL));
281 return status == 0 || status == 1;
285 prom_loadmethod (prom_handle device, void* addr)
287 return (int)call_method_1 ("load", device, 1, addr);
291 prom_getblksize (prom_handle file)
293 return (int)call_method_1 ("block-size", file, 0);
297 prom_readblocks (prom_handle dev, int blockNum, int blockCount, void *buffer)
299 #if READ_BLOCKS_USE_READ
301 unsigned int blksize;
303 blksize = prom_getblksize(dev);
306 status = prom_seek(dev, blockNum * blksize);
309 prom_printf("Can't seek to 0x%x\n", blockNum * blksize);
312 status = prom_read(dev, buffer, blockCount * blksize);
313 // prom_printf("prom_readblocks, bl: %d, cnt: %d, status: %d\n",
314 // blockNum, blockCount, status);
316 return status == (blockCount * blksize);
324 result = call_method_1 ("read-blocks", dev, 3, buffer, blockNum, blockCount);
327 call_prom("interpret", 1, 1, " 10 ms");
340 while ((a = (int)call_prom ("read", 3, 1, prom_stdin, c, 4)) == 0)
343 prom_abort ("EOF on console\n");
344 if (a == 3 && c[0] == '\e' && c[1] == '[')
354 return (int) call_prom("read", 3, 1, prom_stdin, &ch, 1) > 0? ch: -1;
358 prom_putchar (char c)
361 call_prom ("write", 3, 1, prom_stdout, "\r\n", 2);
363 call_prom ("write", 3, 1, prom_stdout, &c, 1);
367 prom_puts (prom_handle file, char *s)
371 for (p = s; *p != 0; p = q)
373 for (q = p; *q != 0 && *q != '\n'; ++q)
376 call_prom ("write", 3, 1, file, p, q - p);
380 call_prom ("write", 3, 1, file, "\r\n", 2);
386 prom_vfprintf (prom_handle file, char *fmt, va_list ap)
388 static char printf_buf[1536];
389 vsprintf (printf_buf, fmt, ap);
390 prom_puts (file, printf_buf);
394 prom_vprintf (char *fmt, va_list ap)
396 static char printf_buf[1536];
397 vsprintf (printf_buf, fmt, ap);
398 prom_puts (prom_stdout, printf_buf);
402 prom_fprintf (prom_handle file, char *fmt, ...)
406 prom_vfprintf (file, fmt, ap);
411 prom_printf (char *fmt, ...)
415 prom_vfprintf (prom_stdout, fmt, ap);
420 prom_perror (int error, char *filename)
422 if (error == FILE_ERR_EOF)
423 prom_printf("%s: Unexpected End Of File\n", filename);
424 else if (error == FILE_ERR_NOTFOUND)
425 prom_printf("%s: No such file or directory\n", filename);
426 else if (error == FILE_CANT_SEEK)
427 prom_printf("%s: Seek error\n", filename);
428 else if (error == FILE_IOERR)
429 prom_printf("%s: Input/output error\n", filename);
430 else if (error == FILE_BAD_PATH)
431 prom_printf("%s: Path too long\n", filename);
432 else if (error == FILE_ERR_BAD_TYPE)
433 prom_printf("%s: Not a regular file\n", filename);
434 else if (error == FILE_ERR_NOTDIR)
435 prom_printf("%s: Not a directory\n", filename);
436 else if (error == FILE_ERR_BAD_FSYS)
437 prom_printf("%s: Unknown or corrupt filesystem\n", filename);
438 else if (error == FILE_ERR_SYMLINK_LOOP)
439 prom_printf("%s: Too many levels of symbolic links\n", filename);
440 else if (error == FILE_ERR_LENGTH)
441 prom_printf("%s: File too large\n", filename);
442 else if (error == FILE_ERR_FSBUSY)
443 prom_printf("%s: Filesystem busy\n", filename);
444 else if (error == FILE_ERR_BADDEV)
445 prom_printf("%s: Unable to open file, Invalid device\n", filename);
447 prom_printf("%s: Unknown error\n", filename);
451 prom_readline (char *prompt, char *buf, int len)
457 prom_puts (prom_stdout, prompt);
459 while (i < len-1 && (c = prom_getchar ()) != '\r')
467 prom_puts (prom_stdout, "\b \b");
473 else if (isprint (c))
485 #ifdef CONFIG_SET_COLORMAP
486 int prom_set_color(prom_handle device, int color, int r, int g, int b)
488 return (int)call_prom( "call-method", 6, 1, "color!", device, color, b, g, r );
490 #endif /* CONFIG_SET_COLORMAP */
495 call_prom ("exit", 0, 0);
499 prom_abort (char *fmt, ...)
503 prom_vfprintf (prom_stdout, fmt, ap);
509 prom_sleep (int seconds)
512 end = (prom_getms() + (seconds * 1000));
513 while (prom_getms() <= end);
517 prom_claim (void *virt, unsigned int size, unsigned int align)
519 return call_prom ("claim", 3, 1, virt, size, align);
523 prom_release(void *virt, unsigned int size)
525 // call_prom ("release", 2, 1, virt, size);
526 /* release in not enough, it needs also an unmap call. This bit of forth
527 * code inspired from Darwin's bootloader but could be replaced by direct
528 * calls to the MMU package if needed
530 call_prom ("interpret", 3, 1,
532 ".\" ReleaseMem:\" 2dup . . cr "
534 "over \" translate\" ^mmu " // Find out physical base
535 "^on0 " // Bail if translation failed
536 "drop " // Leaving phys on top of stack
537 "2dup \" unmap\" ^mmu " // Unmap the space first
538 "2dup \" release\" ^mmu " // Then free the virtual pages
539 "\" release\" ^mem " // Then free the physical pages
545 prom_map (void *phys, void *virt, int size)
547 unsigned long msr = mfmsr();
549 /* Only create a mapping if we're running with relocation enabled. */
550 if ( (msr & MSR_IR) && (msr & MSR_DR) )
551 call_method_1 ("map", prom_mmu, 4, -1, size, virt, phys);
555 prom_unmap (void *phys, void *virt, int size)
557 unsigned long msr = mfmsr();
559 /* Only unmap if we're running with relocation enabled. */
560 if ( (msr & MSR_IR) && (msr & MSR_DR) )
561 call_method_1 ("map", prom_mmu, 4, -1, size, virt, phys);
567 static char args[256];
570 l = prom_get_chosen ("bootargs", args, 255);
576 prom_setargs (char *args)
578 int l = strlen (args)+1;
579 if ((int)call_prom ("setprop", 4, 1, prom_chosen, "bootargs", args, l) != l)
580 prom_printf ("can't set args\n");
583 int prom_interpret (char *forth)
585 return (int)call_prom("interpret", 1, 1, forth);
591 return (int) call_prom("milliseconds", 0, 1);
597 call_prom("enter", 0, 0);