ieee1275.c 15 KB

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