ieee1275.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /* of.c - Access the Open Firmware client interface. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
  5. *
  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.
  10. *
  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.
  15. *
  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/>.
  18. */
  19. #include <grub/ieee1275/ieee1275.h>
  20. #include <grub/types.h>
  21. #define IEEE1275_PHANDLE_INVALID ((grub_ieee1275_cell_t) -1)
  22. #define IEEE1275_IHANDLE_INVALID ((grub_ieee1275_cell_t) 0)
  23. #define IEEE1275_CELL_INVALID ((grub_ieee1275_cell_t) -1)
  24. int
  25. grub_ieee1275_finddevice (const char *name, grub_ieee1275_phandle_t *phandlep)
  26. {
  27. struct find_device_args
  28. {
  29. struct grub_ieee1275_common_hdr common;
  30. grub_ieee1275_cell_t device;
  31. grub_ieee1275_cell_t phandle;
  32. }
  33. args;
  34. INIT_IEEE1275_COMMON (&args.common, "finddevice", 1, 1);
  35. args.device = (grub_ieee1275_cell_t) name;
  36. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  37. return -1;
  38. *phandlep = args.phandle;
  39. if (args.phandle == IEEE1275_PHANDLE_INVALID)
  40. return -1;
  41. return 0;
  42. }
  43. int
  44. grub_ieee1275_get_property (grub_ieee1275_phandle_t phandle,
  45. const char *property, void *buf,
  46. grub_size_t size, grub_ssize_t *actual)
  47. {
  48. struct get_property_args
  49. {
  50. struct grub_ieee1275_common_hdr common;
  51. grub_ieee1275_cell_t phandle;
  52. grub_ieee1275_cell_t prop;
  53. grub_ieee1275_cell_t buf;
  54. grub_ieee1275_cell_t buflen;
  55. grub_ieee1275_cell_t size;
  56. }
  57. args;
  58. INIT_IEEE1275_COMMON (&args.common, "getprop", 4, 1);
  59. args.phandle = phandle;
  60. args.prop = (grub_ieee1275_cell_t) property;
  61. args.buf = (grub_ieee1275_cell_t) buf;
  62. args.buflen = (grub_ieee1275_cell_t) size;
  63. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  64. return -1;
  65. if (actual)
  66. *actual = (grub_ssize_t) args.size;
  67. if (args.size == IEEE1275_CELL_INVALID)
  68. return -1;
  69. return 0;
  70. }
  71. int
  72. grub_ieee1275_get_integer_property (grub_ieee1275_phandle_t phandle,
  73. const char *property, grub_uint32_t *buf,
  74. grub_size_t size, grub_ssize_t *actual)
  75. {
  76. int ret;
  77. ret = grub_ieee1275_get_property (phandle, property, (void *) buf, size, actual);
  78. #ifndef GRUB_CPU_WORDS_BIGENDIAN
  79. /* Integer properties are always in big endian. */
  80. if (ret == 0)
  81. {
  82. unsigned int i;
  83. size /= sizeof (grub_uint32_t);
  84. for (i = 0; i < size; i++)
  85. buf[i] = grub_be_to_cpu32 (buf[i]);
  86. }
  87. #endif
  88. return ret;
  89. }
  90. int
  91. grub_ieee1275_next_property (grub_ieee1275_phandle_t phandle, char *prev_prop,
  92. char *prop)
  93. {
  94. struct get_property_args
  95. {
  96. struct grub_ieee1275_common_hdr common;
  97. grub_ieee1275_cell_t phandle;
  98. grub_ieee1275_cell_t prev_prop;
  99. grub_ieee1275_cell_t next_prop;
  100. grub_ieee1275_cell_t flags;
  101. }
  102. args;
  103. INIT_IEEE1275_COMMON (&args.common, "nextprop", 3, 1);
  104. args.phandle = phandle;
  105. args.prev_prop = (grub_ieee1275_cell_t) prev_prop;
  106. args.next_prop = (grub_ieee1275_cell_t) prop;
  107. args.flags = (grub_ieee1275_cell_t) -1;
  108. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  109. return -1;
  110. return (int) args.flags;
  111. }
  112. int
  113. grub_ieee1275_get_property_length (grub_ieee1275_phandle_t phandle,
  114. const char *prop, grub_ssize_t *length)
  115. {
  116. struct get_property_args
  117. {
  118. struct grub_ieee1275_common_hdr common;
  119. grub_ieee1275_cell_t phandle;
  120. grub_ieee1275_cell_t prop;
  121. grub_ieee1275_cell_t length;
  122. }
  123. args;
  124. INIT_IEEE1275_COMMON (&args.common, "getproplen", 2, 1);
  125. args.phandle = phandle;
  126. args.prop = (grub_ieee1275_cell_t) prop;
  127. args.length = (grub_ieee1275_cell_t) -1;
  128. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  129. return -1;
  130. *length = args.length;
  131. if (args.length == IEEE1275_CELL_INVALID)
  132. return -1;
  133. return 0;
  134. }
  135. int
  136. grub_ieee1275_instance_to_package (grub_ieee1275_ihandle_t ihandle,
  137. grub_ieee1275_phandle_t *phandlep)
  138. {
  139. struct instance_to_package_args
  140. {
  141. struct grub_ieee1275_common_hdr common;
  142. grub_ieee1275_cell_t ihandle;
  143. grub_ieee1275_cell_t phandle;
  144. }
  145. args;
  146. INIT_IEEE1275_COMMON (&args.common, "instance-to-package", 1, 1);
  147. args.ihandle = ihandle;
  148. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  149. return -1;
  150. *phandlep = args.phandle;
  151. if (args.phandle == IEEE1275_PHANDLE_INVALID)
  152. return -1;
  153. return 0;
  154. }
  155. int
  156. grub_ieee1275_package_to_path (grub_ieee1275_phandle_t phandle,
  157. char *path, grub_size_t len,
  158. grub_ssize_t *actual)
  159. {
  160. struct instance_to_package_args
  161. {
  162. struct grub_ieee1275_common_hdr common;
  163. grub_ieee1275_cell_t phandle;
  164. grub_ieee1275_cell_t buf;
  165. grub_ieee1275_cell_t buflen;
  166. grub_ieee1275_cell_t actual;
  167. }
  168. args;
  169. INIT_IEEE1275_COMMON (&args.common, "package-to-path", 3, 1);
  170. args.phandle = phandle;
  171. args.buf = (grub_ieee1275_cell_t) path;
  172. args.buflen = (grub_ieee1275_cell_t) len;
  173. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  174. return -1;
  175. if (actual)
  176. *actual = args.actual;
  177. if (args.actual == IEEE1275_CELL_INVALID)
  178. return -1;
  179. return 0;
  180. }
  181. int
  182. grub_ieee1275_instance_to_path (grub_ieee1275_ihandle_t ihandle,
  183. char *path, grub_size_t len,
  184. grub_ssize_t *actual)
  185. {
  186. struct instance_to_path_args
  187. {
  188. struct grub_ieee1275_common_hdr common;
  189. grub_ieee1275_cell_t ihandle;
  190. grub_ieee1275_cell_t buf;
  191. grub_ieee1275_cell_t buflen;
  192. grub_ieee1275_cell_t actual;
  193. }
  194. args;
  195. INIT_IEEE1275_COMMON (&args.common, "instance-to-path", 3, 1);
  196. args.ihandle = ihandle;
  197. args.buf = (grub_ieee1275_cell_t) path;
  198. args.buflen = (grub_ieee1275_cell_t) len;
  199. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  200. return -1;
  201. if (actual)
  202. *actual = args.actual;
  203. if (args.actual == IEEE1275_CELL_INVALID)
  204. return -1;
  205. return 0;
  206. }
  207. int
  208. grub_ieee1275_write (grub_ieee1275_ihandle_t ihandle, const void *buffer,
  209. grub_size_t len, grub_ssize_t *actualp)
  210. {
  211. struct write_args
  212. {
  213. struct grub_ieee1275_common_hdr common;
  214. grub_ieee1275_cell_t ihandle;
  215. grub_ieee1275_cell_t buf;
  216. grub_ieee1275_cell_t len;
  217. grub_ieee1275_cell_t actual;
  218. }
  219. args;
  220. INIT_IEEE1275_COMMON (&args.common, "write", 3, 1);
  221. args.ihandle = ihandle;
  222. args.buf = (grub_ieee1275_cell_t) buffer;
  223. args.len = (grub_ieee1275_cell_t) len;
  224. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  225. return -1;
  226. if (actualp)
  227. *actualp = args.actual;
  228. return 0;
  229. }
  230. int
  231. grub_ieee1275_read (grub_ieee1275_ihandle_t ihandle, void *buffer,
  232. grub_size_t len, grub_ssize_t *actualp)
  233. {
  234. struct write_args
  235. {
  236. struct grub_ieee1275_common_hdr common;
  237. grub_ieee1275_cell_t ihandle;
  238. grub_ieee1275_cell_t buf;
  239. grub_ieee1275_cell_t len;
  240. grub_ieee1275_cell_t actual;
  241. }
  242. args;
  243. INIT_IEEE1275_COMMON (&args.common, "read", 3, 1);
  244. args.ihandle = ihandle;
  245. args.buf = (grub_ieee1275_cell_t) buffer;
  246. args.len = (grub_ieee1275_cell_t) len;
  247. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  248. return -1;
  249. if (actualp)
  250. *actualp = args.actual;
  251. return 0;
  252. }
  253. int
  254. grub_ieee1275_seek (grub_ieee1275_ihandle_t ihandle, grub_disk_addr_t pos,
  255. grub_ssize_t *result)
  256. {
  257. struct write_args
  258. {
  259. struct grub_ieee1275_common_hdr common;
  260. grub_ieee1275_cell_t ihandle;
  261. grub_ieee1275_cell_t pos_hi;
  262. grub_ieee1275_cell_t pos_lo;
  263. grub_ieee1275_cell_t result;
  264. }
  265. args;
  266. INIT_IEEE1275_COMMON (&args.common, "seek", 3, 1);
  267. args.ihandle = ihandle;
  268. /* To prevent stupid gcc warning. */
  269. #if GRUB_IEEE1275_CELL_SIZEOF >= 8
  270. args.pos_hi = 0;
  271. args.pos_lo = pos;
  272. #else
  273. args.pos_hi = (grub_ieee1275_cell_t) (pos >> (8 * GRUB_IEEE1275_CELL_SIZEOF));
  274. args.pos_lo = (grub_ieee1275_cell_t)
  275. (pos & ((1ULL << (8 * GRUB_IEEE1275_CELL_SIZEOF)) - 1));
  276. #endif
  277. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  278. return -1;
  279. if (result)
  280. *result = args.result;
  281. return 0;
  282. }
  283. int
  284. grub_ieee1275_peer (grub_ieee1275_phandle_t node,
  285. grub_ieee1275_phandle_t *result)
  286. {
  287. struct peer_args
  288. {
  289. struct grub_ieee1275_common_hdr common;
  290. grub_ieee1275_cell_t node;
  291. grub_ieee1275_cell_t result;
  292. }
  293. args;
  294. INIT_IEEE1275_COMMON (&args.common, "peer", 1, 1);
  295. args.node = node;
  296. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  297. return -1;
  298. *result = args.result;
  299. if (args.result == 0)
  300. return -1;
  301. return 0;
  302. }
  303. int
  304. grub_ieee1275_child (grub_ieee1275_phandle_t node,
  305. grub_ieee1275_phandle_t *result)
  306. {
  307. struct child_args
  308. {
  309. struct grub_ieee1275_common_hdr common;
  310. grub_ieee1275_cell_t node;
  311. grub_ieee1275_cell_t result;
  312. }
  313. args;
  314. INIT_IEEE1275_COMMON (&args.common, "child", 1, 1);
  315. args.node = node;
  316. args.result = IEEE1275_PHANDLE_INVALID;
  317. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  318. return -1;
  319. *result = args.result;
  320. if (args.result == 0)
  321. return -1;
  322. return 0;
  323. }
  324. int
  325. grub_ieee1275_parent (grub_ieee1275_phandle_t node,
  326. grub_ieee1275_phandle_t *result)
  327. {
  328. struct parent_args
  329. {
  330. struct grub_ieee1275_common_hdr common;
  331. grub_ieee1275_cell_t node;
  332. grub_ieee1275_cell_t result;
  333. }
  334. args;
  335. INIT_IEEE1275_COMMON (&args.common, "parent", 1, 1);
  336. args.node = node;
  337. args.result = IEEE1275_PHANDLE_INVALID;
  338. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  339. return -1;
  340. *result = args.result;
  341. return 0;
  342. }
  343. int
  344. grub_ieee1275_interpret (const char *command, grub_ieee1275_cell_t *catch)
  345. {
  346. struct enter_args
  347. {
  348. struct grub_ieee1275_common_hdr common;
  349. grub_ieee1275_cell_t command;
  350. grub_ieee1275_cell_t catch;
  351. }
  352. args;
  353. if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
  354. return -1;
  355. INIT_IEEE1275_COMMON (&args.common, "interpret", 1, 1);
  356. args.command = (grub_ieee1275_cell_t) command;
  357. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  358. return -1;
  359. if (catch)
  360. *catch = args.catch;
  361. return 0;
  362. }
  363. int
  364. grub_ieee1275_enter (void)
  365. {
  366. struct enter_args
  367. {
  368. struct grub_ieee1275_common_hdr common;
  369. }
  370. args;
  371. INIT_IEEE1275_COMMON (&args.common, "enter", 0, 0);
  372. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  373. return -1;
  374. return 0;
  375. }
  376. void
  377. grub_ieee1275_exit (void)
  378. {
  379. struct exit_args
  380. {
  381. struct grub_ieee1275_common_hdr common;
  382. }
  383. args;
  384. INIT_IEEE1275_COMMON (&args.common, "exit", 0, 0);
  385. IEEE1275_CALL_ENTRY_FN (&args);
  386. for (;;) ;
  387. }
  388. int
  389. grub_ieee1275_open (const char *path, grub_ieee1275_ihandle_t *result)
  390. {
  391. struct open_args
  392. {
  393. struct grub_ieee1275_common_hdr common;
  394. grub_ieee1275_cell_t path;
  395. grub_ieee1275_cell_t result;
  396. }
  397. args;
  398. INIT_IEEE1275_COMMON (&args.common, "open", 1, 1);
  399. args.path = (grub_ieee1275_cell_t) path;
  400. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  401. return -1;
  402. *result = args.result;
  403. if (args.result == IEEE1275_IHANDLE_INVALID)
  404. return -1;
  405. return 0;
  406. }
  407. int
  408. grub_ieee1275_close (grub_ieee1275_ihandle_t ihandle)
  409. {
  410. struct close_args
  411. {
  412. struct grub_ieee1275_common_hdr common;
  413. grub_ieee1275_cell_t ihandle;
  414. }
  415. args;
  416. INIT_IEEE1275_COMMON (&args.common, "close", 1, 0);
  417. args.ihandle = ihandle;
  418. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  419. return -1;
  420. return 0;
  421. }
  422. int
  423. grub_ieee1275_claim (grub_addr_t addr, grub_size_t size, unsigned int align,
  424. grub_addr_t *result)
  425. {
  426. struct claim_args
  427. {
  428. struct grub_ieee1275_common_hdr common;
  429. grub_ieee1275_cell_t addr;
  430. grub_ieee1275_cell_t size;
  431. grub_ieee1275_cell_t align;
  432. grub_ieee1275_cell_t base;
  433. }
  434. args;
  435. INIT_IEEE1275_COMMON (&args.common, "claim", 3, 1);
  436. args.addr = (grub_ieee1275_cell_t) addr;
  437. args.size = (grub_ieee1275_cell_t) size;
  438. args.align = (grub_ieee1275_cell_t) align;
  439. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  440. return -1;
  441. if (result)
  442. *result = args.base;
  443. if (args.base == IEEE1275_CELL_INVALID)
  444. return -1;
  445. return 0;
  446. }
  447. int
  448. grub_ieee1275_release (grub_addr_t addr, grub_size_t size)
  449. {
  450. struct release_args
  451. {
  452. struct grub_ieee1275_common_hdr common;
  453. grub_ieee1275_cell_t addr;
  454. grub_ieee1275_cell_t size;
  455. }
  456. args;
  457. INIT_IEEE1275_COMMON (&args.common, "release", 2, 0);
  458. args.addr = addr;
  459. args.size = size;
  460. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  461. return -1;
  462. return 0;
  463. }
  464. int
  465. grub_ieee1275_set_property (grub_ieee1275_phandle_t phandle,
  466. const char *propname, const void *buf,
  467. grub_size_t size, grub_ssize_t *actual)
  468. {
  469. struct set_property_args
  470. {
  471. struct grub_ieee1275_common_hdr common;
  472. grub_ieee1275_cell_t phandle;
  473. grub_ieee1275_cell_t propname;
  474. grub_ieee1275_cell_t buf;
  475. grub_ieee1275_cell_t size;
  476. grub_ieee1275_cell_t actual;
  477. }
  478. args;
  479. INIT_IEEE1275_COMMON (&args.common, "setprop", 4, 1);
  480. args.size = (grub_ieee1275_cell_t) size;
  481. args.buf = (grub_ieee1275_cell_t) buf;
  482. args.propname = (grub_ieee1275_cell_t) propname;
  483. args.phandle = (grub_ieee1275_cell_t) phandle;
  484. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  485. return -1;
  486. *actual = args.actual;
  487. if ((args.actual == IEEE1275_CELL_INVALID) || (args.actual != args.size))
  488. return -1;
  489. return 0;
  490. }
  491. int
  492. grub_ieee1275_set_color (grub_ieee1275_ihandle_t ihandle,
  493. int index, int r, int g, int b)
  494. {
  495. struct set_color_args
  496. {
  497. struct grub_ieee1275_common_hdr common;
  498. grub_ieee1275_cell_t method;
  499. grub_ieee1275_cell_t ihandle;
  500. grub_ieee1275_cell_t index;
  501. grub_ieee1275_cell_t b;
  502. grub_ieee1275_cell_t g;
  503. grub_ieee1275_cell_t r;
  504. grub_ieee1275_cell_t catch_result;
  505. }
  506. args;
  507. INIT_IEEE1275_COMMON (&args.common, "call-method", 6, 1);
  508. args.method = (grub_ieee1275_cell_t) "color!";
  509. args.ihandle = ihandle;
  510. args.index = index;
  511. args.r = r;
  512. args.g = g;
  513. args.b = b;
  514. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  515. return -1;
  516. return args.catch_result;
  517. }
  518. int
  519. grub_ieee1275_milliseconds (grub_uint32_t *msecs)
  520. {
  521. struct milliseconds_args
  522. {
  523. struct grub_ieee1275_common_hdr common;
  524. grub_ieee1275_cell_t msecs;
  525. }
  526. args;
  527. INIT_IEEE1275_COMMON (&args.common, "milliseconds", 0, 1);
  528. if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
  529. return -1;
  530. *msecs = args.msecs;
  531. return 0;
  532. }