]> git.ozlabs.org Git - yaboot.git/blob - second/prom.c
Certain levels of IBM firmware will allow the system to boot from an
[yaboot.git] / second / prom.c
1 /*
2  *  prom.c - Routines for talking to the Open Firmware PROM
3  *
4  *  Copyright (C) 2001, 2002 Ethan Benson
5  *
6  *  Copyright (C) 1999 Benjamin Herrenschmidt
7  *
8  *  Copyright (C) 1999 Marius Vollmer
9  *
10  *  Copyright (C) 1996 Paul Mackerras.
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #include "prom.h"
28 #include "stdarg.h"
29 #include "stddef.h"
30 #include "stdlib.h"
31 #include "types.h"
32 #include "ctype.h"
33 #include "asm/processor.h"
34 #include "errors.h"
35 #include "debug.h"
36
37 #define READ_BLOCKS_USE_READ    1
38
39 prom_entry prom;
40
41 ihandle prom_stdin, prom_stdout;
42
43 //extern int vsprintf(char *buf, const char *fmt, va_list args);
44
45 static ihandle prom_mem, prom_mmu;
46 static ihandle prom_chosen, prom_options;
47
48 struct prom_args {
49      const char *service;
50      int nargs;
51      int nret;
52      void *args[10];
53 };
54
55 void *
56 call_prom (const char *service, int nargs, int nret, ...)
57 {
58      va_list list;
59      int i;
60      struct prom_args prom_args;
61
62      prom_args.service = service;
63      prom_args.nargs = nargs;
64      prom_args.nret = nret;
65      va_start (list, nret);
66      for (i = 0; i < nargs; ++i)
67           prom_args.args[i] = va_arg(list, void *);
68      va_end(list);
69      for (i = 0; i < nret; ++i)
70           prom_args.args[i + nargs] = 0;
71      prom (&prom_args);
72      if (nret > 0)
73           return prom_args.args[nargs];
74      else
75           return 0;
76 }
77
78 void *
79 call_prom_return (const char *service, int nargs, int nret, ...)
80 {
81      va_list list;
82      int i;
83      void* result;
84      struct prom_args prom_args;
85
86      prom_args.service = service;
87      prom_args.nargs = nargs;
88      prom_args.nret = nret;
89      va_start (list, nret);
90      for (i = 0; i < nargs; ++i)
91           prom_args.args[i] = va_arg(list, void *);
92      for (i = 0; i < nret; ++i)
93           prom_args.args[i + nargs] = 0;
94      if (prom (&prom_args) != 0)
95           return PROM_INVALID_HANDLE;
96      if (nret > 0) {
97           result = prom_args.args[nargs];
98           for (i=1; i<nret; i++) {
99                void** rp = va_arg(list, void**);
100                *rp = prom_args.args[i+nargs];
101           }
102      } else
103           result = 0;
104      va_end(list);
105      return result;
106 }
107
108 static void *
109 call_method_1 (char *method, prom_handle h, int nargs, ...)
110 {
111      va_list list;
112      int i;
113      struct prom_args prom_args;
114
115      prom_args.service = "call-method";
116      prom_args.nargs = nargs+2;
117      prom_args.nret = 2;
118      prom_args.args[0] = method;
119      prom_args.args[1] = h;
120      va_start (list, nargs);
121      for (i = 0; i < nargs; ++i)
122           prom_args.args[2+i] = va_arg(list, void *);
123      va_end(list);
124      prom_args.args[2+nargs] = 0;
125      prom_args.args[2+nargs+1] = 0;
126
127      prom (&prom_args);
128
129      if (prom_args.args[2+nargs] != 0)
130      {
131           prom_printf ("method '%s' failed %p\n", method, prom_args.args[2+nargs]);
132           return 0;
133      }
134      return prom_args.args[2+nargs+1];
135 }
136
137
138 prom_handle
139 prom_finddevice (char *name)
140 {
141      return call_prom ("finddevice", 1, 1, name);
142 }
143
144 prom_handle
145 prom_findpackage(char *path)
146 {
147      return call_prom ("find-package", 1, 1, path);
148 }
149
150 int
151 prom_getprop (prom_handle pack, char *name, void *mem, int len)
152 {
153      return (int)call_prom ("getprop", 4, 1, pack, name, mem, len);
154 }
155
156 int
157 prom_get_chosen (char *name, void *mem, int len)
158 {
159      return prom_getprop (prom_chosen, name, mem, len);
160 }
161
162 int
163 prom_get_options (char *name, void *mem, int len)
164 {
165      if (prom_options == (void *)-1)
166           return -1;
167      return prom_getprop (prom_options, name, mem, len);
168 }
169
170 int
171 prom_get_devtype (char *device)
172 {
173      phandle    dev;
174      int        result;
175      char       tmp[64];
176
177      if (strstr(device, TOK_ISCSI))
178           device = strcpy(tmp, "/vdevice/gscsi/disk");
179
180      /* Find OF device phandle */
181      dev = prom_finddevice(device);
182      if (dev == PROM_INVALID_HANDLE) {
183           return FILE_ERR_BADDEV;
184      }
185
186      /* Check the kind of device */
187      result = prom_getprop(dev, "device_type", tmp, 63);
188      if (result == -1) {
189           prom_printf("can't get <device_type> for device: %s\n", device);
190           return FILE_ERR_BADDEV;
191      }
192      tmp[result] = 0;
193      if (!strcmp(tmp, "block"))
194           return FILE_DEVICE_BLOCK;
195      else if (!strcmp(tmp, "network"))
196           return FILE_DEVICE_NET;
197      else {
198           prom_printf("Unkown device type <%s>\n", tmp);
199           return FILE_ERR_BADDEV;
200      }
201 }
202
203 void
204 prom_init (prom_entry pp)
205 {
206      prom = pp;
207
208      prom_chosen = prom_finddevice ("/chosen");
209      if (prom_chosen == (void *)-1)
210           prom_exit ();
211      prom_options = prom_finddevice ("/options");
212      if (prom_get_chosen ("stdout", &prom_stdout, sizeof(prom_stdout)) <= 0)
213           prom_exit();
214      if (prom_get_chosen ("stdin", &prom_stdin, sizeof(prom_stdin)) <= 0)
215           prom_abort ("\nCan't open stdin");
216      if (prom_get_chosen ("memory", &prom_mem, sizeof(prom_mem)) <= 0)
217           prom_abort ("\nCan't get mem handle");
218      if (prom_get_chosen ("mmu", &prom_mmu, sizeof(prom_mmu)) <= 0)
219           prom_abort ("\nCan't get mmu handle");
220
221   // move cursor to fresh line
222      prom_printf ("\n");
223
224      /* Add a few OF methods (thanks Darwin) */
225 #if DEBUG
226      prom_printf ("Adding OF methods...\n");
227 #endif
228
229      prom_interpret (
230           /* All values in this forth code are in hex */
231           "hex "
232           /* Those are a few utilities ripped from Apple */
233           ": D2NIP decode-int nip nip ;\r"      // A useful function to save space
234           ": GPP$ get-package-property 0= ;\r"  // Another useful function to save space
235           ": ^on0 0= if -1 throw then ;\r"      // Bail if result zero
236           ": $CM $call-method ;\r"
237           );
238
239      /* Some forth words used by the release method */
240      prom_interpret (
241           " \" /chosen\" find-package if "
242                  "dup \" memory\" rot GPP$ if "
243                          "D2NIP swap "                           // ( MEMORY-ihandle "/chosen"-phandle )
244                          "\" mmu\" rot GPP$ if "
245                                  "D2NIP "                                // ( MEMORY-ihandle MMU-ihandle )
246                          "else "
247                                  "0 "                                    // ( MEMORY-ihandle 0 )
248                          "then "
249                  "else "
250                          "0 0 "                                          // ( 0 0 )
251                  "then "
252           "else "
253                  "0 0 "                                                  // ( 0 0 )
254           "then\r"
255           "value mmu# "
256           "value mem# "
257           );
258
259      prom_interpret (
260           ": ^mem mem# $CM ; "
261           ": ^mmu mmu# $CM ; "
262           );
263
264      DEBUG_F("OF interface initialized.\n");
265 }
266
267 prom_handle
268 prom_open (char *spec)
269 {
270      return call_prom ("open", 1, 1, spec, strlen(spec));
271 }
272
273 void
274 prom_close (prom_handle file)
275 {
276      call_prom ("close", 1, 0, file);
277 }
278
279 int
280 prom_read (prom_handle file, void *buf, int n)
281 {
282      int result = 0;
283      int retries = 10;
284
285      if (n == 0)
286           return 0;
287      while(--retries) {
288           result = (int)call_prom ("read", 3, 1, file, buf, n);
289           if (result != 0)
290                break;
291           call_prom("interpret", 1, 1, " 10 ms");
292      }
293
294      return result;
295 }
296
297 int
298 prom_write (prom_handle file, void *buf, int n)
299 {
300      return (int)call_prom ("write", 3, 1, file, buf, n);
301 }
302
303 int
304 prom_seek (prom_handle file, int pos)
305 {
306      int status = (int)call_prom ("seek", 3, 1, file, 0, pos);
307      return status == 0 || status == 1;
308 }
309
310 int
311 prom_lseek (prom_handle file, unsigned long long pos)
312 {
313      int status = (int)call_prom ("seek", 3, 1, file,
314                                   (unsigned int)(pos >> 32), (unsigned int)(pos & 0xffffffffUL));
315      return status == 0 || status == 1;
316 }
317
318 int
319 prom_loadmethod (prom_handle device, void* addr)
320 {
321      return (int)call_method_1 ("load", device, 1, addr);
322 }
323
324 int
325 prom_getblksize (prom_handle file)
326 {
327      return (int)call_method_1 ("block-size", file, 0);
328 }
329
330 int
331 prom_readblocks (prom_handle dev, int blockNum, int blockCount, void *buffer)
332 {
333 #if READ_BLOCKS_USE_READ
334      int status;
335      unsigned int blksize;
336
337      blksize = prom_getblksize(dev);
338      if (blksize <= 1)
339           blksize = 512;
340      status = prom_seek(dev, blockNum * blksize);
341      if (status != 1) {
342           return 0;
343           prom_printf("Can't seek to 0x%x\n", blockNum * blksize);
344      }
345
346      status = prom_read(dev, buffer, blockCount * blksize);
347 //  prom_printf("prom_readblocks, bl: %d, cnt: %d, status: %d\n",
348 //      blockNum, blockCount, status);
349
350      return status == (blockCount * blksize);
351 #else
352      int result;
353      int retries = 10;
354
355      if (blockCount == 0)
356           return blockCount;
357      while(--retries) {
358           result = call_method_1 ("read-blocks", dev, 3, buffer, blockNum, blockCount);
359           if (result != 0)
360                break;
361           call_prom("interpret", 1, 1, " 10 ms");
362      }
363
364      return result;
365 #endif
366 }
367
368 int
369 prom_getchar ()
370 {
371      char c[4];
372      int a;
373
374      while ((a = (int)call_prom ("read", 3, 1, prom_stdin, c, 4)) == 0)
375           ;
376      if (a == -1)
377           prom_abort ("EOF on console\n");
378      if (a == 3 && c[0] == '\e' && c[1] == '[')
379           return 0x100 | c[2];
380      return c[0];
381 }
382
383 int
384 prom_nbgetchar()
385 {
386      char ch;
387
388      return (int) call_prom("read", 3, 1, prom_stdin, &ch, 1) > 0? ch: -1;
389 }
390
391 void
392 prom_putchar (char c)
393 {
394      if (c == '\n')
395           call_prom ("write", 3, 1, prom_stdout, "\r\n", 2);
396      else
397           call_prom ("write", 3, 1, prom_stdout, &c, 1);
398 }
399
400 void
401 prom_puts (prom_handle file, char *s)
402 {
403      const char *p, *q;
404
405      for (p = s; *p != 0; p = q)
406      {
407           for (q = p; *q != 0 && *q != '\n'; ++q)
408                ;
409           if (q > p)
410                call_prom ("write", 3, 1, file, p, q - p);
411           if (*q != 0)
412           {
413                ++q;
414                call_prom ("write", 3, 1, file, "\r\n", 2);
415           }
416      }
417 }
418
419 void
420 prom_vfprintf (prom_handle file, char *fmt, va_list ap)
421 {
422      static char printf_buf[2048];
423      vsprintf (printf_buf, fmt, ap);
424      prom_puts (file, printf_buf);
425 }
426
427 void
428 prom_vprintf (char *fmt, va_list ap)
429 {
430      static char printf_buf[2048];
431      vsprintf (printf_buf, fmt, ap);
432      prom_puts (prom_stdout, printf_buf);
433 }
434
435 void
436 prom_fprintf (prom_handle file, char *fmt, ...)
437 {
438      va_list ap;
439      va_start (ap, fmt);
440      prom_vfprintf (file, fmt, ap);
441      va_end (ap);
442 }
443
444 void
445 prom_printf (char *fmt, ...)
446 {
447      va_list ap;
448      va_start (ap, fmt);
449      prom_vfprintf (prom_stdout, fmt, ap);
450      va_end (ap);
451 }
452
453 void
454 prom_perror (int error, char *filename)
455 {
456      if (error == FILE_ERR_EOF)
457           prom_printf("%s: Unexpected End Of File\n", filename);
458      else if (error == FILE_ERR_NOTFOUND)
459           prom_printf("%s: No such file or directory\n", filename);
460      else if (error == FILE_CANT_SEEK)
461           prom_printf("%s: Seek error\n", filename);
462      else if (error == FILE_IOERR)
463           prom_printf("%s: Input/output error\n", filename);
464      else if (error == FILE_BAD_PATH)
465           prom_printf("%s: Path too long\n", filename);
466      else if (error == FILE_ERR_BAD_TYPE)
467           prom_printf("%s: Not a regular file\n", filename);
468      else if (error == FILE_ERR_NOTDIR)
469           prom_printf("%s: Not a directory\n", filename);
470      else if (error == FILE_ERR_BAD_FSYS)
471           prom_printf("%s: Unknown or corrupt filesystem\n", filename);
472      else if (error == FILE_ERR_SYMLINK_LOOP)
473           prom_printf("%s: Too many levels of symbolic links\n", filename);
474      else if (error == FILE_ERR_LENGTH)
475           prom_printf("%s: File too large\n", filename);
476      else if (error == FILE_ERR_FSBUSY)
477           prom_printf("%s: Filesystem busy\n", filename);
478      else if (error == FILE_ERR_BADDEV)
479           prom_printf("%s: Unable to open file, Invalid device\n", filename);
480      else
481           prom_printf("%s: Unknown error\n", filename);
482 }
483
484 void
485 prom_readline (char *prompt, char *buf, int len)
486 {
487      int i = 0;
488      int c;
489
490      if (prompt)
491           prom_puts (prom_stdout, prompt);
492
493      while (i < len-1 && (c = prom_getchar ()) != '\r')
494      {
495           if (c >= 0x100)
496                continue;
497           if (c == 8)
498           {
499                if (i > 0)
500                {
501                     prom_puts (prom_stdout, "\b \b");
502                     i--;
503                }
504                else
505                     prom_putchar ('\a');
506           }
507           else if (isprint (c))
508           {
509                prom_putchar (c);
510                buf[i++] = c;
511           }
512           else
513                prom_putchar ('\a');
514      }
515      prom_putchar ('\n');
516      buf[i] = 0;
517 }
518
519 #ifdef CONFIG_SET_COLORMAP
520 int prom_set_color(prom_handle device, int color, int r, int g, int b)
521 {
522      return (int)call_prom( "call-method", 6, 1, "color!", device, color, b, g, r );
523 }
524 #endif /* CONFIG_SET_COLORMAP */
525
526 void
527 prom_exit ()
528 {
529      call_prom ("exit", 0, 0);
530 }
531
532 void
533 prom_abort (char *fmt, ...)
534 {
535      va_list ap;
536      va_start (ap, fmt);
537      prom_vfprintf (prom_stdout, fmt, ap);
538      va_end (ap);
539      prom_exit ();
540 }
541
542 void
543 prom_sleep (int seconds)
544 {
545      int end;
546      end = (prom_getms() + (seconds * 1000));
547      while (prom_getms() <= end);
548 }
549
550 void *
551 prom_claim (void *virt, unsigned int size, unsigned int align)
552 {
553      return call_prom ("claim", 3, 1, virt, size, align);
554 }
555
556 void
557 prom_release(void *virt, unsigned int size)
558 {
559      call_prom ("release", 2, 0, virt, size);
560 #if 0 /* this is bullshit, newworld OF RELEASE method works fine. */
561
562      /* release in not enough, it needs also an unmap call. This bit of forth
563       * code inspired from Darwin's bootloader but could be replaced by direct
564       * calls to the MMU package if needed
565       */
566      call_prom ("interpret", 3, 1,
567 #if DEBUG
568                 ".\" ReleaseMem:\" 2dup . . cr "
569 #endif
570                 "over \" translate\" ^mmu "             // Find out physical base
571                 "^on0 "                                 // Bail if translation failed
572                 "drop "                                 // Leaving phys on top of stack
573                 "2dup \" unmap\" ^mmu "                 // Unmap the space first
574                 "2dup \" release\" ^mmu "               // Then free the virtual pages
575                 "\" release\" ^mem "                    // Then free the physical pages
576                 ,size, virt
577           );
578 #endif /* bullshit */
579 }
580
581 void
582 prom_map (void *phys, void *virt, int size)
583 {
584      unsigned long msr = mfmsr();
585
586      /* Only create a mapping if we're running with relocation enabled. */
587      if ( (msr & MSR_IR) && (msr & MSR_DR) )
588           call_method_1 ("map", prom_mmu, 4, -1, size, virt, phys);
589 }
590
591 void
592 prom_unmap (void *phys, void *virt, int size)
593 {
594      unsigned long msr = mfmsr();
595
596      /* Only unmap if we're running with relocation enabled. */
597      if ( (msr & MSR_IR) && (msr & MSR_DR) )
598           call_method_1 ("map", prom_mmu, 4, -1, size, virt, phys);
599 }
600
601 char *
602 prom_getargs ()
603 {
604      static char args[256];
605      int l;
606
607      l = prom_get_chosen ("bootargs", args, 255);
608      args[l] = '\0';
609      return args;
610 }
611
612 void
613 prom_setargs (char *args)
614 {
615      int l = strlen (args)+1;
616      if ((int)call_prom ("setprop", 4, 1, prom_chosen, "bootargs", args, l) != l)
617           prom_printf ("can't set args\n");
618 }
619
620 int prom_interpret (char *forth)
621 {
622      return (int)call_prom("interpret", 1, 1, forth);
623 }
624
625 int
626 prom_getms(void)
627 {
628      return (int) call_prom("milliseconds", 0, 1);
629 }
630
631 void
632 prom_pause(void)
633 {
634      call_prom("enter", 0, 0);
635 }
636
637 /*
638  * Local variables:
639  * c-file-style: "k&r"
640  * c-basic-offset: 5
641  * End:
642  */