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"
35 #define READ_BLOCKS_USE_READ 1
39 ihandle prom_stdin, prom_stdout;
41 //extern int vsprintf(char *buf, const char *fmt, va_list args);
43 static ihandle prom_mem, prom_mmu;
44 static ihandle prom_chosen, prom_options;
54 call_prom (const char *service, int nargs, int nret, ...)
58 struct prom_args prom_args;
60 prom_args.service = service;
61 prom_args.nargs = nargs;
62 prom_args.nret = nret;
63 va_start (list, nret);
64 for (i = 0; i < nargs; ++i)
65 prom_args.args[i] = va_arg(list, void *);
67 for (i = 0; i < nret; ++i)
68 prom_args.args[i + nargs] = 0;
71 return prom_args.args[nargs];
77 call_prom_return (const char *service, int nargs, int nret, ...)
82 struct prom_args prom_args;
84 prom_args.service = service;
85 prom_args.nargs = nargs;
86 prom_args.nret = nret;
87 va_start (list, nret);
88 for (i = 0; i < nargs; ++i)
89 prom_args.args[i] = va_arg(list, void *);
90 for (i = 0; i < nret; ++i)
91 prom_args.args[i + nargs] = 0;
92 if (prom (&prom_args) != 0)
93 return PROM_INVALID_HANDLE;
95 result = prom_args.args[nargs];
96 for (i=1; i<nret; i++) {
97 void** rp = va_arg(list, void**);
98 *rp = prom_args.args[i+nargs];
107 call_method_1 (char *method, prom_handle h, int nargs, ...)
111 struct prom_args prom_args;
113 prom_args.service = "call-method";
114 prom_args.nargs = nargs+2;
116 prom_args.args[0] = method;
117 prom_args.args[1] = h;
118 va_start (list, nargs);
119 for (i = 0; i < nargs; ++i)
120 prom_args.args[2+i] = va_arg(list, void *);
122 prom_args.args[2+nargs] = 0;
123 prom_args.args[2+nargs+1] = 0;
127 if (prom_args.args[2+nargs] != 0)
129 prom_printf ("method '%s' failed %p\n", method, prom_args.args[2+nargs]);
132 return prom_args.args[2+nargs+1];
137 prom_finddevice (char *name)
139 return call_prom ("finddevice", 1, 1, name);
143 prom_findpackage(char *path)
145 return call_prom ("find-package", 1, 1, path);
149 prom_getprop (prom_handle pack, char *name, void *mem, int len)
151 return (int)call_prom ("getprop", 4, 1, pack, name, mem, len);
155 prom_get_chosen (char *name, void *mem, int len)
157 return prom_getprop (prom_chosen, name, mem, len);
161 prom_get_options (char *name, void *mem, int len)
163 if (prom_options == (void *)-1)
165 return prom_getprop (prom_options, name, mem, len);
169 prom_get_devtype (char *device)
175 /* Find OF device phandle */
176 dev = prom_finddevice(device);
177 if (dev == PROM_INVALID_HANDLE) {
178 return FILE_ERR_BADDEV;
181 /* Check the kind of device */
182 result = prom_getprop(dev, "device_type", tmp, 63);
184 prom_printf("can't get <device_type> for device: %s\n", device);
185 return FILE_ERR_BADDEV;
188 if (!strcmp(tmp, "block"))
189 return FILE_DEVICE_BLOCK;
190 else if (!strcmp(tmp, "network"))
191 return FILE_DEVICE_NET;
193 prom_printf("Unkown device type <%s>\n", tmp);
194 return FILE_ERR_BADDEV;
199 prom_init (prom_entry pp)
203 prom_chosen = prom_finddevice ("/chosen");
204 if (prom_chosen == (void *)-1)
206 prom_options = prom_finddevice ("/options");
207 if (prom_get_chosen ("stdout", &prom_stdout, sizeof(prom_stdout)) <= 0)
209 if (prom_get_chosen ("stdin", &prom_stdin, sizeof(prom_stdin)) <= 0)
210 prom_abort ("\nCan't open stdin");
211 if (prom_get_chosen ("memory", &prom_mem, sizeof(prom_mem)) <= 0)
212 prom_abort ("\nCan't get mem handle");
213 if (prom_get_chosen ("mmu", &prom_mmu, sizeof(prom_mmu)) <= 0)
214 prom_abort ("\nCan't get mmu handle");
216 // move cursor to fresh line
219 /* Add a few OF methods (thanks Darwin) */
221 prom_printf ("Adding OF methods...\n");
225 /* All values in this forth code are in hex */
227 /* Those are a few utilities ripped from Apple */
228 ": D2NIP decode-int nip nip ;\r" // A useful function to save space
229 ": GPP$ get-package-property 0= ;\r" // Another useful function to save space
230 ": ^on0 0= if -1 throw then ;\r" // Bail if result zero
231 ": $CM $call-method ;\r"
234 /* Some forth words used by the release method */
236 " \" /chosen\" find-package if "
237 "dup \" memory\" rot GPP$ if "
238 "D2NIP swap " // ( MEMORY-ihandle "/chosen"-phandle )
239 "\" mmu\" rot GPP$ if "
240 "D2NIP " // ( MEMORY-ihandle MMU-ihandle )
242 "0 " // ( MEMORY-ihandle 0 )
259 DEBUG_F("OF interface initialized.\n");
263 prom_open (char *spec)
265 return call_prom ("open", 1, 1, spec, strlen(spec));
269 prom_close (prom_handle file)
271 call_prom ("close", 1, 0, file);
275 prom_read (prom_handle file, void *buf, int n)
283 result = (int)call_prom ("read", 3, 1, file, buf, n);
286 call_prom("interpret", 1, 1, " 10 ms");
293 prom_write (prom_handle file, void *buf, int n)
295 return (int)call_prom ("write", 3, 1, file, buf, n);
299 prom_seek (prom_handle file, int pos)
301 int status = (int)call_prom ("seek", 3, 1, file, 0, pos);
302 return status == 0 || status == 1;
306 prom_lseek (prom_handle file, unsigned long long pos)
308 int status = (int)call_prom ("seek", 3, 1, file,
309 (unsigned int)(pos >> 32), (unsigned int)(pos & 0xffffffffUL));
310 return status == 0 || status == 1;
314 prom_loadmethod (prom_handle device, void* addr)
316 return (int)call_method_1 ("load", device, 1, addr);
320 prom_getblksize (prom_handle file)
322 return (int)call_method_1 ("block-size", file, 0);
326 prom_readblocks (prom_handle dev, int blockNum, int blockCount, void *buffer)
328 #if READ_BLOCKS_USE_READ
330 unsigned int blksize;
332 blksize = prom_getblksize(dev);
335 status = prom_seek(dev, blockNum * blksize);
338 prom_printf("Can't seek to 0x%x\n", blockNum * blksize);
341 status = prom_read(dev, buffer, blockCount * blksize);
342 // prom_printf("prom_readblocks, bl: %d, cnt: %d, status: %d\n",
343 // blockNum, blockCount, status);
345 return status == (blockCount * blksize);
353 result = call_method_1 ("read-blocks", dev, 3, buffer, blockNum, blockCount);
356 call_prom("interpret", 1, 1, " 10 ms");
369 while ((a = (int)call_prom ("read", 3, 1, prom_stdin, c, 4)) == 0)
372 prom_abort ("EOF on console\n");
373 if (a == 3 && c[0] == '\e' && c[1] == '[')
383 return (int) call_prom("read", 3, 1, prom_stdin, &ch, 1) > 0? ch: -1;
387 prom_putchar (char c)
390 call_prom ("write", 3, 1, prom_stdout, "\r\n", 2);
392 call_prom ("write", 3, 1, prom_stdout, &c, 1);
396 prom_puts (prom_handle file, char *s)
400 for (p = s; *p != 0; p = q)
402 for (q = p; *q != 0 && *q != '\n'; ++q)
405 call_prom ("write", 3, 1, file, p, q - p);
409 call_prom ("write", 3, 1, file, "\r\n", 2);
415 prom_vfprintf (prom_handle file, char *fmt, va_list ap)
417 static char printf_buf[2048];
418 vsprintf (printf_buf, fmt, ap);
419 prom_puts (file, printf_buf);
423 prom_vprintf (char *fmt, va_list ap)
425 static char printf_buf[2048];
426 vsprintf (printf_buf, fmt, ap);
427 prom_puts (prom_stdout, printf_buf);
431 prom_fprintf (prom_handle file, char *fmt, ...)
435 prom_vfprintf (file, fmt, ap);
440 prom_printf (char *fmt, ...)
444 prom_vfprintf (prom_stdout, fmt, ap);
449 prom_perror (int error, char *filename)
451 if (error == FILE_ERR_EOF)
452 prom_printf("%s: Unexpected End Of File\n", filename);
453 else if (error == FILE_ERR_NOTFOUND)
454 prom_printf("%s: No such file or directory\n", filename);
455 else if (error == FILE_CANT_SEEK)
456 prom_printf("%s: Seek error\n", filename);
457 else if (error == FILE_IOERR)
458 prom_printf("%s: Input/output error\n", filename);
459 else if (error == FILE_BAD_PATH)
460 prom_printf("%s: Path too long\n", filename);
461 else if (error == FILE_ERR_BAD_TYPE)
462 prom_printf("%s: Not a regular file\n", filename);
463 else if (error == FILE_ERR_NOTDIR)
464 prom_printf("%s: Not a directory\n", filename);
465 else if (error == FILE_ERR_BAD_FSYS)
466 prom_printf("%s: Unknown or corrupt filesystem\n", filename);
467 else if (error == FILE_ERR_SYMLINK_LOOP)
468 prom_printf("%s: Too many levels of symbolic links\n", filename);
469 else if (error == FILE_ERR_LENGTH)
470 prom_printf("%s: File too large\n", filename);
471 else if (error == FILE_ERR_FSBUSY)
472 prom_printf("%s: Filesystem busy\n", filename);
473 else if (error == FILE_ERR_BADDEV)
474 prom_printf("%s: Unable to open file, Invalid device\n", filename);
476 prom_printf("%s: Unknown error\n", filename);
480 prom_readline (char *prompt, char *buf, int len)
486 prom_puts (prom_stdout, prompt);
488 while (i < len-1 && (c = prom_getchar ()) != '\r')
496 prom_puts (prom_stdout, "\b \b");
502 else if (isprint (c))
514 #ifdef CONFIG_SET_COLORMAP
515 int prom_set_color(prom_handle device, int color, int r, int g, int b)
517 return (int)call_prom( "call-method", 6, 1, "color!", device, color, b, g, r );
519 #endif /* CONFIG_SET_COLORMAP */
524 call_prom ("exit", 0, 0);
528 prom_abort (char *fmt, ...)
532 prom_vfprintf (prom_stdout, fmt, ap);
538 prom_sleep (int seconds)
541 end = (prom_getms() + (seconds * 1000));
542 while (prom_getms() <= end);
546 prom_claim (void *virt, unsigned int size, unsigned int align)
548 return call_prom ("claim", 3, 1, virt, size, align);
552 prom_release(void *virt, unsigned int size)
554 // call_prom ("release", 2, 1, virt, size);
555 /* release in not enough, it needs also an unmap call. This bit of forth
556 * code inspired from Darwin's bootloader but could be replaced by direct
557 * calls to the MMU package if needed
559 call_prom ("interpret", 3, 1,
561 ".\" ReleaseMem:\" 2dup . . cr "
563 "over \" translate\" ^mmu " // Find out physical base
564 "^on0 " // Bail if translation failed
565 "drop " // Leaving phys on top of stack
566 "2dup \" unmap\" ^mmu " // Unmap the space first
567 "2dup \" release\" ^mmu " // Then free the virtual pages
568 "\" release\" ^mem " // Then free the physical pages
574 prom_map (void *phys, void *virt, int size)
576 unsigned long msr = mfmsr();
578 /* Only create a mapping if we're running with relocation enabled. */
579 if ( (msr & MSR_IR) && (msr & MSR_DR) )
580 call_method_1 ("map", prom_mmu, 4, -1, size, virt, phys);
584 prom_unmap (void *phys, void *virt, int size)
586 unsigned long msr = mfmsr();
588 /* Only unmap if we're running with relocation enabled. */
589 if ( (msr & MSR_IR) && (msr & MSR_DR) )
590 call_method_1 ("map", prom_mmu, 4, -1, size, virt, phys);
596 static char args[256];
599 l = prom_get_chosen ("bootargs", args, 255);
605 prom_setargs (char *args)
607 int l = strlen (args)+1;
608 if ((int)call_prom ("setprop", 4, 1, prom_chosen, "bootargs", args, l) != l)
609 prom_printf ("can't set args\n");
612 int prom_interpret (char *forth)
614 return (int)call_prom("interpret", 1, 1, forth);
620 return (int) call_prom("milliseconds", 0, 1);
626 call_prom("enter", 0, 0);
631 * c-file-style: "k&r"