1 /* of.c - Access the Open Firmware client interface. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/ieee1275/ieee1275.h>
21 #include <grub/types.h>
22 #include <grub/misc.h>
24 #define IEEE1275_PHANDLE_INVALID ((grub_ieee1275_cell_t) -1)
25 #define IEEE1275_IHANDLE_INVALID ((grub_ieee1275_cell_t) 0)
26 #define IEEE1275_CELL_INVALID ((grub_ieee1275_cell_t) -1)
31 grub_ieee1275_finddevice (const char *name, grub_ieee1275_phandle_t *phandlep)
33 struct find_device_args
35 struct grub_ieee1275_common_hdr common;
36 grub_ieee1275_cell_t device;
37 grub_ieee1275_cell_t phandle;
41 INIT_IEEE1275_COMMON (&args.common, "finddevice", 1, 1);
42 args.device = (grub_ieee1275_cell_t) name;
44 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
46 *phandlep = args.phandle;
47 if (args.phandle == IEEE1275_PHANDLE_INVALID)
53 grub_ieee1275_get_property (grub_ieee1275_phandle_t phandle,
54 const char *property, void *buf,
55 grub_size_t size, grub_ssize_t *actual)
57 struct get_property_args
59 struct grub_ieee1275_common_hdr common;
60 grub_ieee1275_cell_t phandle;
61 grub_ieee1275_cell_t prop;
62 grub_ieee1275_cell_t buf;
63 grub_ieee1275_cell_t buflen;
64 grub_ieee1275_cell_t size;
68 INIT_IEEE1275_COMMON (&args.common, "getprop", 4, 1);
69 args.phandle = phandle;
70 args.prop = (grub_ieee1275_cell_t) property;
71 args.buf = (grub_ieee1275_cell_t) buf;
72 args.buflen = (grub_ieee1275_cell_t) size;
74 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
77 *actual = (grub_ssize_t) args.size;
78 if (args.size == IEEE1275_CELL_INVALID)
84 grub_ieee1275_get_integer_property (grub_ieee1275_phandle_t phandle,
85 const char *property, grub_uint32_t *buf,
86 grub_size_t size, grub_ssize_t *actual)
89 ret = grub_ieee1275_get_property (phandle, property, (void *) buf, size, actual);
90 #ifndef GRUB_CPU_WORDS_BIGENDIAN
91 /* Integer properties are always in big endian. */
95 size /= sizeof (grub_uint32_t);
96 for (i = 0; i < size; i++)
97 buf[i] = grub_be_to_cpu32 (buf[i]);
104 grub_ieee1275_next_property (grub_ieee1275_phandle_t phandle, char *prev_prop,
107 struct get_property_args
109 struct grub_ieee1275_common_hdr common;
110 grub_ieee1275_cell_t phandle;
111 grub_ieee1275_cell_t prev_prop;
112 grub_ieee1275_cell_t next_prop;
113 grub_ieee1275_cell_t flags;
117 INIT_IEEE1275_COMMON (&args.common, "nextprop", 3, 1);
118 args.phandle = phandle;
119 args.prev_prop = (grub_ieee1275_cell_t) prev_prop;
120 args.next_prop = (grub_ieee1275_cell_t) prop;
121 args.flags = (grub_ieee1275_cell_t) -1;
123 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
125 return (int) args.flags;
129 grub_ieee1275_get_property_length (grub_ieee1275_phandle_t phandle,
130 const char *prop, grub_ssize_t *length)
132 struct get_property_args
134 struct grub_ieee1275_common_hdr common;
135 grub_ieee1275_cell_t phandle;
136 grub_ieee1275_cell_t prop;
137 grub_ieee1275_cell_t length;
141 INIT_IEEE1275_COMMON (&args.common, "getproplen", 2, 1);
142 args.phandle = phandle;
143 args.prop = (grub_ieee1275_cell_t) prop;
144 args.length = (grub_ieee1275_cell_t) -1;
146 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
148 *length = args.length;
149 if (args.length == IEEE1275_CELL_INVALID)
155 grub_ieee1275_instance_to_package (grub_ieee1275_ihandle_t ihandle,
156 grub_ieee1275_phandle_t *phandlep)
158 struct instance_to_package_args
160 struct grub_ieee1275_common_hdr common;
161 grub_ieee1275_cell_t ihandle;
162 grub_ieee1275_cell_t phandle;
166 INIT_IEEE1275_COMMON (&args.common, "instance-to-package", 1, 1);
167 args.ihandle = ihandle;
169 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
171 *phandlep = args.phandle;
172 if (args.phandle == IEEE1275_PHANDLE_INVALID)
178 grub_ieee1275_package_to_path (grub_ieee1275_phandle_t phandle,
179 char *path, grub_size_t len,
180 grub_ssize_t *actual)
182 struct instance_to_package_args
184 struct grub_ieee1275_common_hdr common;
185 grub_ieee1275_cell_t phandle;
186 grub_ieee1275_cell_t buf;
187 grub_ieee1275_cell_t buflen;
188 grub_ieee1275_cell_t actual;
192 INIT_IEEE1275_COMMON (&args.common, "package-to-path", 3, 1);
193 args.phandle = phandle;
194 args.buf = (grub_ieee1275_cell_t) path;
195 args.buflen = (grub_ieee1275_cell_t) len;
197 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
200 *actual = args.actual;
201 if (args.actual == IEEE1275_CELL_INVALID)
207 grub_ieee1275_instance_to_path (grub_ieee1275_ihandle_t ihandle,
208 char *path, grub_size_t len,
209 grub_ssize_t *actual)
211 struct instance_to_path_args
213 struct grub_ieee1275_common_hdr common;
214 grub_ieee1275_cell_t ihandle;
215 grub_ieee1275_cell_t buf;
216 grub_ieee1275_cell_t buflen;
217 grub_ieee1275_cell_t actual;
221 INIT_IEEE1275_COMMON (&args.common, "instance-to-path", 3, 1);
222 args.ihandle = ihandle;
223 args.buf = (grub_ieee1275_cell_t) path;
224 args.buflen = (grub_ieee1275_cell_t) len;
226 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
229 *actual = args.actual;
230 if (args.actual == IEEE1275_CELL_INVALID)
236 grub_ieee1275_write (grub_ieee1275_ihandle_t ihandle, const void *buffer,
237 grub_size_t len, grub_ssize_t *actualp)
241 struct grub_ieee1275_common_hdr common;
242 grub_ieee1275_cell_t ihandle;
243 grub_ieee1275_cell_t buf;
244 grub_ieee1275_cell_t len;
245 grub_ieee1275_cell_t actual;
249 INIT_IEEE1275_COMMON (&args.common, "write", 3, 1);
250 args.ihandle = ihandle;
251 args.buf = (grub_ieee1275_cell_t) buffer;
252 args.len = (grub_ieee1275_cell_t) len;
254 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
257 *actualp = args.actual;
262 grub_ieee1275_read (grub_ieee1275_ihandle_t ihandle, void *buffer,
263 grub_size_t len, grub_ssize_t *actualp)
267 struct grub_ieee1275_common_hdr common;
268 grub_ieee1275_cell_t ihandle;
269 grub_ieee1275_cell_t buf;
270 grub_ieee1275_cell_t len;
271 grub_ieee1275_cell_t actual;
275 INIT_IEEE1275_COMMON (&args.common, "read", 3, 1);
276 args.ihandle = ihandle;
277 args.buf = (grub_ieee1275_cell_t) buffer;
278 args.len = (grub_ieee1275_cell_t) len;
280 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
283 *actualp = args.actual;
288 grub_ieee1275_seek (grub_ieee1275_ihandle_t ihandle, grub_disk_addr_t pos,
289 grub_ssize_t *result)
293 struct grub_ieee1275_common_hdr common;
294 grub_ieee1275_cell_t ihandle;
295 grub_ieee1275_cell_t pos_hi;
296 grub_ieee1275_cell_t pos_lo;
297 grub_ieee1275_cell_t result;
301 INIT_IEEE1275_COMMON (&args.common, "seek", 3, 1);
302 args.ihandle = ihandle;
303 /* To prevent stupid gcc warning. */
304 #if GRUB_IEEE1275_CELL_SIZEOF >= 8
308 args.pos_hi = (grub_ieee1275_cell_t) (pos >> (8 * GRUB_IEEE1275_CELL_SIZEOF));
309 args.pos_lo = (grub_ieee1275_cell_t)
310 (pos & ((1ULL << (8 * GRUB_IEEE1275_CELL_SIZEOF)) - 1));
313 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
317 *result = args.result;
322 grub_ieee1275_peer (grub_ieee1275_phandle_t node,
323 grub_ieee1275_phandle_t *result)
327 struct grub_ieee1275_common_hdr common;
328 grub_ieee1275_cell_t node;
329 grub_ieee1275_cell_t result;
333 INIT_IEEE1275_COMMON (&args.common, "peer", 1, 1);
336 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
338 *result = args.result;
339 if (args.result == 0)
345 grub_ieee1275_child (grub_ieee1275_phandle_t node,
346 grub_ieee1275_phandle_t *result)
350 struct grub_ieee1275_common_hdr common;
351 grub_ieee1275_cell_t node;
352 grub_ieee1275_cell_t result;
356 INIT_IEEE1275_COMMON (&args.common, "child", 1, 1);
358 args.result = IEEE1275_PHANDLE_INVALID;
360 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
362 *result = args.result;
363 if (args.result == 0)
369 grub_ieee1275_parent (grub_ieee1275_phandle_t node,
370 grub_ieee1275_phandle_t *result)
374 struct grub_ieee1275_common_hdr common;
375 grub_ieee1275_cell_t node;
376 grub_ieee1275_cell_t result;
380 INIT_IEEE1275_COMMON (&args.common, "parent", 1, 1);
382 args.result = IEEE1275_PHANDLE_INVALID;
384 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
386 *result = args.result;
391 grub_ieee1275_interpret (const char *command, grub_ieee1275_cell_t *catch)
395 struct grub_ieee1275_common_hdr common;
396 grub_ieee1275_cell_t command;
397 grub_ieee1275_cell_t catch;
401 if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
404 INIT_IEEE1275_COMMON (&args.common, "interpret", 1, 1);
405 args.command = (grub_ieee1275_cell_t) command;
407 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
415 grub_ieee1275_enter (void)
419 struct grub_ieee1275_common_hdr common;
423 INIT_IEEE1275_COMMON (&args.common, "enter", 0, 0);
425 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
431 grub_ieee1275_exit (void)
435 struct grub_ieee1275_common_hdr common;
439 INIT_IEEE1275_COMMON (&args.common, "exit", 0, 0);
441 IEEE1275_CALL_ENTRY_FN (&args);
446 grub_ieee1275_open (const char *path, grub_ieee1275_ihandle_t *result)
450 struct grub_ieee1275_common_hdr common;
451 grub_ieee1275_cell_t path;
452 grub_ieee1275_cell_t result;
456 INIT_IEEE1275_COMMON (&args.common, "open", 1, 1);
457 args.path = (grub_ieee1275_cell_t) path;
459 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
461 *result = args.result;
462 if (args.result == IEEE1275_IHANDLE_INVALID)
468 grub_ieee1275_close (grub_ieee1275_ihandle_t ihandle)
472 struct grub_ieee1275_common_hdr common;
473 grub_ieee1275_cell_t ihandle;
477 INIT_IEEE1275_COMMON (&args.common, "close", 1, 0);
478 args.ihandle = ihandle;
480 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
487 grub_ieee1275_decode_unit4 (grub_ieee1275_ihandle_t ihandle,
488 void *addr, grub_size_t size,
489 grub_uint32_t *phy_lo, grub_uint32_t *phy_hi,
490 grub_uint32_t *lun_lo, grub_uint32_t *lun_hi)
494 struct grub_ieee1275_common_hdr common;
495 grub_ieee1275_cell_t method;
496 grub_ieee1275_cell_t ihandle;
497 grub_ieee1275_cell_t size;
498 grub_ieee1275_cell_t addr;
499 grub_ieee1275_cell_t catch_result;
500 grub_ieee1275_cell_t tgt_h;
501 grub_ieee1275_cell_t tgt_l;
502 grub_ieee1275_cell_t lun_h;
503 grub_ieee1275_cell_t lun_l;
507 INIT_IEEE1275_COMMON (&args.common, "call-method", 4, 5);
508 args.method = (grub_ieee1275_cell_t) "decode-unit";
509 args.ihandle = ihandle;
511 args.addr = (grub_ieee1275_cell_t) addr;
512 args.catch_result = 1;
514 if ((IEEE1275_CALL_ENTRY_FN (&args) == -1) || (args.catch_result))
516 grub_error (GRUB_ERR_OUT_OF_RANGE, "decode-unit failed\n");
520 *phy_lo = args.tgt_l;
521 *phy_hi = args.tgt_h;
522 *lun_lo = args.lun_l;
523 *lun_hi = args.lun_h;
528 grub_ieee1275_encode_uint4 (grub_ieee1275_ihandle_t ihandle,
529 grub_uint32_t phy_lo, grub_uint32_t phy_hi,
530 grub_uint32_t lun_lo, grub_uint32_t lun_hi,
536 struct grub_ieee1275_common_hdr common;
537 grub_ieee1275_cell_t method;
538 grub_ieee1275_cell_t ihandle;
539 grub_ieee1275_cell_t tgt_h;
540 grub_ieee1275_cell_t tgt_l;
541 grub_ieee1275_cell_t lun_h;
542 grub_ieee1275_cell_t lun_l;
543 grub_ieee1275_cell_t catch_result;
544 grub_ieee1275_cell_t size;
545 grub_ieee1275_cell_t addr;
549 INIT_IEEE1275_COMMON (&args.common, "call-method", 6, 3);
550 args.method = (grub_ieee1275_cell_t) "encode-unit";
551 args.ihandle = ihandle;
557 args.catch_result = 1;
559 if ((IEEE1275_CALL_ENTRY_FN (&args) == -1) || (args.catch_result))
561 grub_error (GRUB_ERR_OUT_OF_RANGE, "encode-unit failed\n");
565 addr = (void *)args.addr;
567 addr = grub_strdup ((char *)args.addr);
572 grub_ieee1275_claim (grub_addr_t addr, grub_size_t size, unsigned int align,
577 struct grub_ieee1275_common_hdr common;
578 grub_ieee1275_cell_t addr;
579 grub_ieee1275_cell_t size;
580 grub_ieee1275_cell_t align;
581 grub_ieee1275_cell_t base;
585 INIT_IEEE1275_COMMON (&args.common, "claim", 3, 1);
586 args.addr = (grub_ieee1275_cell_t) addr;
587 args.size = (grub_ieee1275_cell_t) size;
588 args.align = (grub_ieee1275_cell_t) align;
590 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
594 if (args.base == IEEE1275_CELL_INVALID)
600 grub_ieee1275_release (grub_addr_t addr, grub_size_t size)
604 struct grub_ieee1275_common_hdr common;
605 grub_ieee1275_cell_t addr;
606 grub_ieee1275_cell_t size;
610 INIT_IEEE1275_COMMON (&args.common, "release", 2, 0);
614 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
620 grub_ieee1275_set_property (grub_ieee1275_phandle_t phandle,
621 const char *propname, const void *buf,
622 grub_size_t size, grub_ssize_t *actual)
624 struct set_property_args
626 struct grub_ieee1275_common_hdr common;
627 grub_ieee1275_cell_t phandle;
628 grub_ieee1275_cell_t propname;
629 grub_ieee1275_cell_t buf;
630 grub_ieee1275_cell_t size;
631 grub_ieee1275_cell_t actual;
635 INIT_IEEE1275_COMMON (&args.common, "setprop", 4, 1);
636 args.size = (grub_ieee1275_cell_t) size;
637 args.buf = (grub_ieee1275_cell_t) buf;
638 args.propname = (grub_ieee1275_cell_t) propname;
639 args.phandle = (grub_ieee1275_cell_t) phandle;
641 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
643 *actual = args.actual;
644 if ((args.actual == IEEE1275_CELL_INVALID) || (args.actual != args.size))
650 grub_ieee1275_set_color (grub_ieee1275_ihandle_t ihandle,
651 int index, int r, int g, int b)
653 struct set_color_args
655 struct grub_ieee1275_common_hdr common;
656 grub_ieee1275_cell_t method;
657 grub_ieee1275_cell_t ihandle;
658 grub_ieee1275_cell_t index;
659 grub_ieee1275_cell_t b;
660 grub_ieee1275_cell_t g;
661 grub_ieee1275_cell_t r;
662 grub_ieee1275_cell_t catch_result;
666 INIT_IEEE1275_COMMON (&args.common, "call-method", 6, 1);
667 args.method = (grub_ieee1275_cell_t) "color!";
668 args.ihandle = ihandle;
674 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
676 return args.catch_result;
680 grub_ieee1275_milliseconds (grub_uint32_t *msecs)
682 struct milliseconds_args
684 struct grub_ieee1275_common_hdr common;
685 grub_ieee1275_cell_t msecs;
689 INIT_IEEE1275_COMMON (&args.common, "milliseconds", 0, 1);
691 if (IEEE1275_CALL_ENTRY_FN (&args) == -1)