The Pedigree Project  0.1
prom.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "prom.h"
21 
22 prom_entry prom;
23 void *prom_chosen;
24 void *prom_stdout;
25 void *prom_screen;
26 void *prom_mmu;
27 
28 void *call_prom(
29  const char *service, int nargs, int nret, void *a1, void *a2, void *a3,
30  void *a4, void *a5, void *a6)
31 {
32  prom_args pa;
33  pa.service = service;
34 
35  pa.nargs = nargs;
36  pa.nret = nret;
37  pa.args[0] = a1;
38  pa.args[1] = a2;
39  pa.args[2] = a3;
40  pa.args[3] = a4;
41  pa.args[4] = a5;
42  pa.args[5] = a6;
43  for (int i = 6; i < 10; i++)
44  pa.args[i] = 0;
45 
46  prom(&pa);
47  if (nret > 0)
48  return pa.args[nargs];
49  else
50  return 0;
51 }
52 
53 int prom_get_chosen(char *name, void *mem, int len)
54 {
55  return prom_getprop(prom_chosen, name, mem, len);
56 }
57 
58 void prom_init(prom_entry pe)
59 {
60  prom = pe;
61 
62  prom_chosen = prom_finddevice("/chosen");
63  if (prom_chosen == reinterpret_cast<void *>(-1))
64  prom_exit();
65  if (prom_get_chosen(
66  const_cast<char *>("stdout"), &prom_stdout, sizeof(prom_stdout)) <=
67  0)
68  prom_exit();
69  if (prom_get_chosen(
70  const_cast<char *>("mmu"), &prom_mmu, sizeof(prom_mmu)) <= 0)
71  prom_exit();
72  prom_screen = prom_finddevice("screen");
73  if (prom_screen == reinterpret_cast<void *>(-1))
74  prom_exit();
75 }
76 
77 void *prom_finddevice(const char *dev)
78 {
79  return call_prom(
80  "finddevice", 1, 1, reinterpret_cast<void *>(const_cast<char *>(dev)));
81 }
82 
83 int prom_exit()
84 {
85  call_prom("exit", 0, 0);
86  return 0;
87 }
88 
89 int prom_getprop(void *dev, char *name, void *buf, int len)
90 {
91  return reinterpret_cast<int>(call_prom(
92  "getprop", 4, 1, dev, reinterpret_cast<void *>(name), buf,
93  reinterpret_cast<void *>(len)));
94 }
95 
96 void prom_putchar(char c)
97 {
98  if (c == '\n')
99  call_prom(
100  "write", 3, 1, prom_stdout,
101  reinterpret_cast<void *>(const_cast<char *>("\r\n")),
102  reinterpret_cast<void *>(2));
103  else
104  call_prom(
105  "write", 3, 1, prom_stdout, reinterpret_cast<void *>(&c),
106  reinterpret_cast<void *>(1));
107 }
108 
109 void prom_map(unsigned int phys, unsigned int virt, unsigned int size)
110 {
111  call_prom(
112  "call-method", 6, 1,
113  reinterpret_cast<void *>(const_cast<char *>("map")),
114  reinterpret_cast<void *>(prom_mmu), reinterpret_cast<void *>(-1),
115  reinterpret_cast<void *>(size), reinterpret_cast<void *>(virt),
116  reinterpret_cast<void *>(phys));
117 }
Definition: prom.h:23
Definition: mem.c:283