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_get_devtype (char *device)
174 /* Find OF device phandle */
175 dev = prom_finddevice(device);
176 if (dev == PROM_INVALID_HANDLE) {
177 return FILE_ERR_BADDEV;
180 /* Check the kind of device */
181 result = prom_getprop(dev, "device_type", tmp, 63);
183 prom_printf("can't get <device_type> for device: %s\n", device);
184 return FILE_ERR_BADDEV;
187 if (!strcmp(tmp, "block"))
188 return FILE_DEVICE_BLOCK;
189 else if (!strcmp(tmp, "network"))
190 return FILE_DEVICE_NET;
192 prom_printf("Unkown device type <%s>\n", tmp);
193 return FILE_ERR_BADDEV;
198 prom_init (prom_entry pp)
202 prom_chosen = prom_finddevice ("/chosen");
203 if (prom_chosen == (void *)-1)
205 prom_options = prom_finddevice ("/options");
206 if (prom_get_chosen ("stdout", &prom_stdout, sizeof(prom_stdout)) <= 0)
208 if (prom_get_chosen ("stdin", &prom_stdin, sizeof(prom_stdin)) <= 0)
209 prom_abort ("\nCan't open stdin");
210 if (prom_get_chosen ("memory", &prom_mem, sizeof(prom_mem)) <= 0)
211 prom_abort ("\nCan't get mem handle");
212 if (prom_get_chosen ("mmu", &prom_mmu, sizeof(prom_mmu)) <= 0)
213 prom_abort ("\nCan't get mmu handle");
215 // move cursor to fresh line
218 /* Add a few OF methods (thanks Darwin) */
220 prom_printf ("Adding OF methods...\n");
224 /* All values in this forth code are in hex */
226 /* Those are a few utilities ripped from Apple */
227 ": D2NIP decode-int nip nip ;\r" // A useful function to save space
228 ": GPP$ get-package-property 0= ;\r" // Another useful function to save space
229 ": ^on0 0= if -1 throw then ;\r" // Bail if result zero
230 ": $CM $call-method ;\r"
233 /* Some forth words used by the release method */
235 " \" /chosen\" find-package if "
236 "dup \" memory\" rot GPP$ if "
237 "D2NIP swap " // ( MEMORY-ihandle "/chosen"-phandle )
238 "\" mmu\" rot GPP$ if "
239 "D2NIP " // ( MEMORY-ihandle MMU-ihandle )
241 "0 " // ( MEMORY-ihandle 0 )
259 prom_printf ("OF interface initialized.\n");
264 prom_open (char *spec)
266 return call_prom ("open", 1, 1, spec, strlen(spec));
270 prom_close (prom_handle file)
272 call_prom ("close", 1, 0, file);
276 prom_read (prom_handle file, void *buf, int n)
284 result = (int)call_prom ("read", 3, 1, file, buf, n);
287 call_prom("interpret", 1, 1, " 10 ms");
294 prom_write (prom_handle file, void *buf, int n)
296 return (int)call_prom ("write", 3, 1, file, buf, n);
300 prom_seek (prom_handle file, int pos)
302 int status = (int)call_prom ("seek", 3, 1, file, 0, pos);
303 return status == 0 || status == 1;
307 prom_lseek (prom_handle file, unsigned long long pos)
309 int status = (int)call_prom ("seek", 3, 1, file,
310 (unsigned int)(pos >> 32), (unsigned int)(pos & 0xffffffffUL));
311 return status == 0 || status == 1;
315 prom_loadmethod (prom_handle device, void* addr)
317 return (int)call_method_1 ("load", device, 1, addr);
321 prom_getblksize (prom_handle file)
323 return (int)call_method_1 ("block-size", file, 0);
327 prom_readblocks (prom_handle dev, int blockNum, int blockCount, void *buffer)
329 #if READ_BLOCKS_USE_READ
331 unsigned int blksize;
333 blksize = prom_getblksize(dev);
336 status = prom_seek(dev, blockNum * blksize);
339 prom_printf("Can't seek to 0x%x\n", blockNum * blksize);
342 status = prom_read(dev, buffer, blockCount * blksize);
343 // prom_printf("prom_readblocks, bl: %d, cnt: %d, status: %d\n",
344 // blockNum, blockCount, status);
346 return status == (blockCount * blksize);
354 result = call_method_1 ("read-blocks", dev, 3, buffer, blockNum, blockCount);
357 call_prom("interpret", 1, 1, " 10 ms");
370 while ((a = (int)call_prom ("read", 3, 1, prom_stdin, c, 4)) == 0)
373 prom_abort ("EOF on console\n");
374 if (a == 3 && c[0] == '\e' && c[1] == '[')
384 return (int) call_prom("read", 3, 1, prom_stdin, &ch, 1) > 0? ch: -1;
388 prom_putchar (char c)
391 call_prom ("write", 3, 1, prom_stdout, "\r\n", 2);
393 call_prom ("write", 3, 1, prom_stdout, &c, 1);
397 prom_puts (prom_handle file, char *s)
401 for (p = s; *p != 0; p = q)
403 for (q = p; *q != 0 && *q != '\n'; ++q)
406 call_prom ("write", 3, 1, file, p, q - p);
410 call_prom ("write", 3, 1, file, "\r\n", 2);
416 prom_vfprintf (prom_handle file, char *fmt, va_list ap)
418 static char printf_buf[1536];
419 vsprintf (printf_buf, fmt, ap);
420 prom_puts (file, printf_buf);
424 prom_vprintf (char *fmt, va_list ap)
426 static char printf_buf[1536];
427 vsprintf (printf_buf, fmt, ap);
428 prom_puts (prom_stdout, printf_buf);
432 prom_fprintf (prom_handle file, char *fmt, ...)
436 prom_vfprintf (file, fmt, ap);
441 prom_printf (char *fmt, ...)
445 prom_vfprintf (prom_stdout, fmt, ap);
450 prom_perror (int error, char *filename)
452 if (error == FILE_ERR_EOF)
453 prom_printf("%s: Unexpected End Of File\n", filename);
454 else if (error == FILE_ERR_NOTFOUND)
455 prom_printf("%s: No such file or directory\n", filename);
456 else if (error == FILE_CANT_SEEK)
457 prom_printf("%s: Seek error\n", filename);
458 else if (error == FILE_IOERR)
459 prom_printf("%s: Input/output error\n", filename);
460 else if (error == FILE_BAD_PATH)
461 prom_printf("%s: Path too long\n", filename);
462 else if (error == FILE_ERR_BAD_TYPE)
463 prom_printf("%s: Not a regular file\n", filename);
464 else if (error == FILE_ERR_NOTDIR)
465 prom_printf("%s: Not a directory\n", filename);
466 else if (error == FILE_ERR_BAD_FSYS)
467 prom_printf("%s: Unknown or corrupt filesystem\n", filename);
468 else if (error == FILE_ERR_SYMLINK_LOOP)
469 prom_printf("%s: Too many levels of symbolic links\n", filename);
470 else if (error == FILE_ERR_LENGTH)
471 prom_printf("%s: File too large\n", filename);
472 else if (error == FILE_ERR_FSBUSY)
473 prom_printf("%s: Filesystem busy\n", filename);
474 else if (error == FILE_ERR_BADDEV)
475 prom_printf("%s: Unable to open file, Invalid device\n", filename);
477 prom_printf("%s: Unknown error\n", filename);
481 prom_readline (char *prompt, char *buf, int len)
487 prom_puts (prom_stdout, prompt);
489 while (i < len-1 && (c = prom_getchar ()) != '\r')
497 prom_puts (prom_stdout, "\b \b");
503 else if (isprint (c))
515 #ifdef CONFIG_SET_COLORMAP
516 int prom_set_color(prom_handle device, int color, int r, int g, int b)
518 return (int)call_prom( "call-method", 6, 1, "color!", device, color, b, g, r );
520 #endif /* CONFIG_SET_COLORMAP */
525 call_prom ("exit", 0, 0);
529 prom_abort (char *fmt, ...)
533 prom_vfprintf (prom_stdout, fmt, ap);
539 prom_sleep (int seconds)
542 end = (prom_getms() + (seconds * 1000));
543 while (prom_getms() <= end);
547 prom_claim (void *virt, unsigned int size, unsigned int align)
549 return call_prom ("claim", 3, 1, virt, size, align);
553 prom_release(void *virt, unsigned int size)
555 // call_prom ("release", 2, 1, virt, size);
556 /* release in not enough, it needs also an unmap call. This bit of forth
557 * code inspired from Darwin's bootloader but could be replaced by direct
558 * calls to the MMU package if needed
560 call_prom ("interpret", 3, 1,
562 ".\" ReleaseMem:\" 2dup . . cr "
564 "over \" translate\" ^mmu " // Find out physical base
565 "^on0 " // Bail if translation failed
566 "drop " // Leaving phys on top of stack
567 "2dup \" unmap\" ^mmu " // Unmap the space first
568 "2dup \" release\" ^mmu " // Then free the virtual pages
569 "\" release\" ^mem " // Then free the physical pages
575 prom_map (void *phys, void *virt, int size)
577 unsigned long msr = mfmsr();
579 /* Only create a mapping if we're running with relocation enabled. */
580 if ( (msr & MSR_IR) && (msr & MSR_DR) )
581 call_method_1 ("map", prom_mmu, 4, -1, size, virt, phys);
585 prom_unmap (void *phys, void *virt, int size)
587 unsigned long msr = mfmsr();
589 /* Only unmap if we're running with relocation enabled. */
590 if ( (msr & MSR_IR) && (msr & MSR_DR) )
591 call_method_1 ("map", prom_mmu, 4, -1, size, virt, phys);
597 static char args[256];
600 l = prom_get_chosen ("bootargs", args, 255);
606 prom_setargs (char *args)
608 int l = strlen (args)+1;
609 if ((int)call_prom ("setprop", 4, 1, prom_chosen, "bootargs", args, l) != l)
610 prom_printf ("can't set args\n");
613 int prom_interpret (char *forth)
615 return (int)call_prom("interpret", 1, 1, forth);
621 return (int) call_prom("milliseconds", 0, 1);
627 call_prom("enter", 0, 0);