openprom.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * Linux/SPARC PROM Configuration Driver
  3. * Copyright (C) 1996 Thomas K. Dyas (tdyas@noc.rutgers.edu)
  4. * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
  5. *
  6. * This character device driver allows user programs to access the
  7. * PROM device tree. It is compatible with the SunOS /dev/openprom
  8. * driver and the NetBSD /dev/openprom driver. The SunOS eeprom
  9. * utility works without any modifications.
  10. *
  11. * The driver uses a minor number under the misc device major. The
  12. * file read/write mode determines the type of access to the PROM.
  13. * Interrupts are disabled whenever the driver calls into the PROM for
  14. * sanity's sake.
  15. */
  16. /* This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of the
  19. * License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful, but
  22. * WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. * General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. */
  30. #define PROMLIB_INTERNAL
  31. #include <linux/config.h>
  32. #include <linux/module.h>
  33. #include <linux/kernel.h>
  34. #include <linux/sched.h>
  35. #include <linux/errno.h>
  36. #include <linux/slab.h>
  37. #include <linux/string.h>
  38. #include <linux/miscdevice.h>
  39. #include <linux/init.h>
  40. #include <linux/fs.h>
  41. #include <asm/oplib.h>
  42. #include <asm/system.h>
  43. #include <asm/uaccess.h>
  44. #include <asm/openpromio.h>
  45. #ifdef CONFIG_PCI
  46. #include <linux/pci.h>
  47. #include <asm/pbm.h>
  48. #endif
  49. /* Private data kept by the driver for each descriptor. */
  50. typedef struct openprom_private_data
  51. {
  52. int current_node; /* Current node for SunOS ioctls. */
  53. int lastnode; /* Last valid node used by BSD ioctls. */
  54. } DATA;
  55. /* ID of the PROM node containing all of the EEPROM options. */
  56. static int options_node = 0;
  57. /*
  58. * Copy an openpromio structure into kernel space from user space.
  59. * This routine does error checking to make sure that all memory
  60. * accesses are within bounds. A pointer to the allocated openpromio
  61. * structure will be placed in "*opp_p". Return value is the length
  62. * of the user supplied buffer.
  63. */
  64. static int copyin(struct openpromio __user *info, struct openpromio **opp_p)
  65. {
  66. unsigned int bufsize;
  67. if (!info || !opp_p)
  68. return -EFAULT;
  69. if (get_user(bufsize, &info->oprom_size))
  70. return -EFAULT;
  71. if (bufsize == 0)
  72. return -EINVAL;
  73. /* If the bufsize is too large, just limit it.
  74. * Fix from Jason Rappleye.
  75. */
  76. if (bufsize > OPROMMAXPARAM)
  77. bufsize = OPROMMAXPARAM;
  78. if (!(*opp_p = kmalloc(sizeof(int) + bufsize + 1, GFP_KERNEL)))
  79. return -ENOMEM;
  80. memset(*opp_p, 0, sizeof(int) + bufsize + 1);
  81. if (copy_from_user(&(*opp_p)->oprom_array,
  82. &info->oprom_array, bufsize)) {
  83. kfree(*opp_p);
  84. return -EFAULT;
  85. }
  86. return bufsize;
  87. }
  88. static int getstrings(struct openpromio __user *info, struct openpromio **opp_p)
  89. {
  90. int n, bufsize;
  91. char c;
  92. if (!info || !opp_p)
  93. return -EFAULT;
  94. if (!(*opp_p = kmalloc(sizeof(int) + OPROMMAXPARAM + 1, GFP_KERNEL)))
  95. return -ENOMEM;
  96. memset(*opp_p, 0, sizeof(int) + OPROMMAXPARAM + 1);
  97. (*opp_p)->oprom_size = 0;
  98. n = bufsize = 0;
  99. while ((n < 2) && (bufsize < OPROMMAXPARAM)) {
  100. if (get_user(c, &info->oprom_array[bufsize])) {
  101. kfree(*opp_p);
  102. return -EFAULT;
  103. }
  104. if (c == '\0')
  105. n++;
  106. (*opp_p)->oprom_array[bufsize++] = c;
  107. }
  108. if (!n) {
  109. kfree(*opp_p);
  110. return -EINVAL;
  111. }
  112. return bufsize;
  113. }
  114. /*
  115. * Copy an openpromio structure in kernel space back to user space.
  116. */
  117. static int copyout(void __user *info, struct openpromio *opp, int len)
  118. {
  119. if (copy_to_user(info, opp, len))
  120. return -EFAULT;
  121. return 0;
  122. }
  123. /*
  124. * SunOS and Solaris /dev/openprom ioctl calls.
  125. */
  126. static int openprom_sunos_ioctl(struct inode * inode, struct file * file,
  127. unsigned int cmd, unsigned long arg, int node)
  128. {
  129. DATA *data = (DATA *) file->private_data;
  130. char buffer[OPROMMAXPARAM+1], *buf;
  131. struct openpromio *opp;
  132. int bufsize, len, error = 0;
  133. static int cnt;
  134. void __user *argp = (void __user *)arg;
  135. if (cmd == OPROMSETOPT)
  136. bufsize = getstrings(argp, &opp);
  137. else
  138. bufsize = copyin(argp, &opp);
  139. if (bufsize < 0)
  140. return bufsize;
  141. switch (cmd) {
  142. case OPROMGETOPT:
  143. case OPROMGETPROP:
  144. len = prom_getproplen(node, opp->oprom_array);
  145. if (len <= 0 || len > bufsize) {
  146. error = copyout(argp, opp, sizeof(int));
  147. break;
  148. }
  149. len = prom_getproperty(node, opp->oprom_array, buffer, bufsize);
  150. memcpy(opp->oprom_array, buffer, len);
  151. opp->oprom_array[len] = '\0';
  152. opp->oprom_size = len;
  153. error = copyout(argp, opp, sizeof(int) + bufsize);
  154. break;
  155. case OPROMNXTOPT:
  156. case OPROMNXTPROP:
  157. buf = prom_nextprop(node, opp->oprom_array, buffer);
  158. len = strlen(buf);
  159. if (len == 0 || len + 1 > bufsize) {
  160. error = copyout(argp, opp, sizeof(int));
  161. break;
  162. }
  163. memcpy(opp->oprom_array, buf, len);
  164. opp->oprom_array[len] = '\0';
  165. opp->oprom_size = ++len;
  166. error = copyout(argp, opp, sizeof(int) + bufsize);
  167. break;
  168. case OPROMSETOPT:
  169. case OPROMSETOPT2:
  170. buf = opp->oprom_array + strlen(opp->oprom_array) + 1;
  171. len = opp->oprom_array + bufsize - buf;
  172. error = prom_setprop(options_node, opp->oprom_array,
  173. buf, len);
  174. if (error < 0)
  175. error = -EINVAL;
  176. break;
  177. case OPROMNEXT:
  178. case OPROMCHILD:
  179. case OPROMSETCUR:
  180. if (bufsize < sizeof(int)) {
  181. error = -EINVAL;
  182. break;
  183. }
  184. node = *((int *) opp->oprom_array);
  185. switch (cmd) {
  186. case OPROMNEXT: node = __prom_getsibling(node); break;
  187. case OPROMCHILD: node = __prom_getchild(node); break;
  188. case OPROMSETCUR: break;
  189. }
  190. data->current_node = node;
  191. *((int *)opp->oprom_array) = node;
  192. opp->oprom_size = sizeof(int);
  193. error = copyout(argp, opp, bufsize + sizeof(int));
  194. break;
  195. case OPROMPCI2NODE:
  196. error = -EINVAL;
  197. if (bufsize >= 2*sizeof(int)) {
  198. #ifdef CONFIG_PCI
  199. struct pci_dev *pdev;
  200. struct pcidev_cookie *pcp;
  201. pdev = pci_find_slot (((int *) opp->oprom_array)[0],
  202. ((int *) opp->oprom_array)[1]);
  203. pcp = pdev->sysdata;
  204. if (pcp != NULL && pcp->prom_node != -1 && pcp->prom_node) {
  205. node = pcp->prom_node;
  206. data->current_node = node;
  207. *((int *)opp->oprom_array) = node;
  208. opp->oprom_size = sizeof(int);
  209. error = copyout(argp, opp, bufsize + sizeof(int));
  210. }
  211. #endif
  212. }
  213. break;
  214. case OPROMPATH2NODE:
  215. node = prom_finddevice(opp->oprom_array);
  216. data->current_node = node;
  217. *((int *)opp->oprom_array) = node;
  218. opp->oprom_size = sizeof(int);
  219. error = copyout(argp, opp, bufsize + sizeof(int));
  220. break;
  221. case OPROMGETBOOTARGS:
  222. buf = saved_command_line;
  223. len = strlen(buf);
  224. if (len > bufsize) {
  225. error = -EINVAL;
  226. break;
  227. }
  228. strcpy(opp->oprom_array, buf);
  229. opp->oprom_size = len;
  230. error = copyout(argp, opp, bufsize + sizeof(int));
  231. break;
  232. case OPROMU2P:
  233. case OPROMGETCONS:
  234. case OPROMGETFBNAME:
  235. if (cnt++ < 10)
  236. printk(KERN_INFO "openprom_sunos_ioctl: unimplemented ioctl\n");
  237. error = -EINVAL;
  238. break;
  239. default:
  240. if (cnt++ < 10)
  241. printk(KERN_INFO "openprom_sunos_ioctl: cmd 0x%X, arg 0x%lX\n", cmd, arg);
  242. error = -EINVAL;
  243. break;
  244. }
  245. kfree(opp);
  246. return error;
  247. }
  248. /* Return nonzero if a specific node is in the PROM device tree. */
  249. static int intree(int root, int node)
  250. {
  251. for (; root != 0; root = prom_getsibling(root))
  252. if (root == node || intree(prom_getchild(root),node))
  253. return 1;
  254. return 0;
  255. }
  256. /* Return nonzero if a specific node is "valid". */
  257. static int goodnode(int n, DATA *data)
  258. {
  259. if (n == data->lastnode || n == prom_root_node || n == options_node)
  260. return 1;
  261. if (n == 0 || n == -1 || !intree(prom_root_node,n))
  262. return 0;
  263. data->lastnode = n;
  264. return 1;
  265. }
  266. /* Copy in a whole string from userspace into kernelspace. */
  267. static int copyin_string(char __user *user, size_t len, char **ptr)
  268. {
  269. char *tmp;
  270. if ((ssize_t)len < 0 || (ssize_t)(len + 1) < 0)
  271. return -EINVAL;
  272. tmp = kmalloc(len + 1, GFP_KERNEL);
  273. if (!tmp)
  274. return -ENOMEM;
  275. if(copy_from_user(tmp, user, len)) {
  276. kfree(tmp);
  277. return -EFAULT;
  278. }
  279. tmp[len] = '\0';
  280. *ptr = tmp;
  281. return 0;
  282. }
  283. /*
  284. * NetBSD /dev/openprom ioctl calls.
  285. */
  286. static int openprom_bsd_ioctl(struct inode * inode, struct file * file,
  287. unsigned int cmd, unsigned long arg)
  288. {
  289. DATA *data = (DATA *) file->private_data;
  290. void __user *argp = (void __user *)arg;
  291. struct opiocdesc op;
  292. int error, node, len;
  293. char *str, *tmp;
  294. char buffer[64];
  295. static int cnt;
  296. switch (cmd) {
  297. case OPIOCGET:
  298. if (copy_from_user(&op, argp, sizeof(op)))
  299. return -EFAULT;
  300. if (!goodnode(op.op_nodeid,data))
  301. return -EINVAL;
  302. error = copyin_string(op.op_name, op.op_namelen, &str);
  303. if (error)
  304. return error;
  305. len = prom_getproplen(op.op_nodeid,str);
  306. if (len > op.op_buflen) {
  307. kfree(str);
  308. return -ENOMEM;
  309. }
  310. op.op_buflen = len;
  311. if (len <= 0) {
  312. kfree(str);
  313. /* Verified by the above copy_from_user */
  314. if (__copy_to_user(argp, &op,
  315. sizeof(op)))
  316. return -EFAULT;
  317. return 0;
  318. }
  319. tmp = kmalloc(len + 1, GFP_KERNEL);
  320. if (!tmp) {
  321. kfree(str);
  322. return -ENOMEM;
  323. }
  324. prom_getproperty(op.op_nodeid, str, tmp, len);
  325. tmp[len] = '\0';
  326. if (__copy_to_user(argp, &op, sizeof(op)) != 0
  327. || copy_to_user(op.op_buf, tmp, len) != 0)
  328. error = -EFAULT;
  329. kfree(tmp);
  330. kfree(str);
  331. return error;
  332. case OPIOCNEXTPROP:
  333. if (copy_from_user(&op, argp, sizeof(op)))
  334. return -EFAULT;
  335. if (!goodnode(op.op_nodeid,data))
  336. return -EINVAL;
  337. error = copyin_string(op.op_name, op.op_namelen, &str);
  338. if (error)
  339. return error;
  340. tmp = prom_nextprop(op.op_nodeid,str,buffer);
  341. if (tmp) {
  342. len = strlen(tmp);
  343. if (len > op.op_buflen)
  344. len = op.op_buflen;
  345. else
  346. op.op_buflen = len;
  347. } else {
  348. len = op.op_buflen = 0;
  349. }
  350. if (!access_ok(VERIFY_WRITE, argp, sizeof(op))) {
  351. kfree(str);
  352. return -EFAULT;
  353. }
  354. if (!access_ok(VERIFY_WRITE, op.op_buf, len)) {
  355. kfree(str);
  356. return -EFAULT;
  357. }
  358. error = __copy_to_user(argp, &op, sizeof(op));
  359. if (!error) error = __copy_to_user(op.op_buf, tmp, len);
  360. kfree(str);
  361. return error;
  362. case OPIOCSET:
  363. if (copy_from_user(&op, argp, sizeof(op)))
  364. return -EFAULT;
  365. if (!goodnode(op.op_nodeid,data))
  366. return -EINVAL;
  367. error = copyin_string(op.op_name, op.op_namelen, &str);
  368. if (error)
  369. return error;
  370. error = copyin_string(op.op_buf, op.op_buflen, &tmp);
  371. if (error) {
  372. kfree(str);
  373. return error;
  374. }
  375. len = prom_setprop(op.op_nodeid,str,tmp,op.op_buflen+1);
  376. if (len != op.op_buflen)
  377. return -EINVAL;
  378. kfree(str);
  379. kfree(tmp);
  380. return 0;
  381. case OPIOCGETOPTNODE:
  382. if (copy_to_user(argp, &options_node, sizeof(int)))
  383. return -EFAULT;
  384. return 0;
  385. case OPIOCGETNEXT:
  386. case OPIOCGETCHILD:
  387. if (copy_from_user(&node, argp, sizeof(int)))
  388. return -EFAULT;
  389. if (cmd == OPIOCGETNEXT)
  390. node = __prom_getsibling(node);
  391. else
  392. node = __prom_getchild(node);
  393. if (__copy_to_user(argp, &node, sizeof(int)))
  394. return -EFAULT;
  395. return 0;
  396. default:
  397. if (cnt++ < 10)
  398. printk(KERN_INFO "openprom_bsd_ioctl: cmd 0x%X\n", cmd);
  399. return -EINVAL;
  400. }
  401. }
  402. /*
  403. * Handoff control to the correct ioctl handler.
  404. */
  405. static int openprom_ioctl(struct inode * inode, struct file * file,
  406. unsigned int cmd, unsigned long arg)
  407. {
  408. DATA *data = (DATA *) file->private_data;
  409. static int cnt;
  410. switch (cmd) {
  411. case OPROMGETOPT:
  412. case OPROMNXTOPT:
  413. if ((file->f_mode & FMODE_READ) == 0)
  414. return -EPERM;
  415. return openprom_sunos_ioctl(inode, file, cmd, arg,
  416. options_node);
  417. case OPROMSETOPT:
  418. case OPROMSETOPT2:
  419. if ((file->f_mode & FMODE_WRITE) == 0)
  420. return -EPERM;
  421. return openprom_sunos_ioctl(inode, file, cmd, arg,
  422. options_node);
  423. case OPROMNEXT:
  424. case OPROMCHILD:
  425. case OPROMGETPROP:
  426. case OPROMNXTPROP:
  427. if ((file->f_mode & FMODE_READ) == 0)
  428. return -EPERM;
  429. return openprom_sunos_ioctl(inode, file, cmd, arg,
  430. data->current_node);
  431. case OPROMU2P:
  432. case OPROMGETCONS:
  433. case OPROMGETFBNAME:
  434. case OPROMGETBOOTARGS:
  435. case OPROMSETCUR:
  436. case OPROMPCI2NODE:
  437. case OPROMPATH2NODE:
  438. if ((file->f_mode & FMODE_READ) == 0)
  439. return -EPERM;
  440. return openprom_sunos_ioctl(inode, file, cmd, arg, 0);
  441. case OPIOCGET:
  442. case OPIOCNEXTPROP:
  443. case OPIOCGETOPTNODE:
  444. case OPIOCGETNEXT:
  445. case OPIOCGETCHILD:
  446. if ((file->f_mode & FMODE_READ) == 0)
  447. return -EBADF;
  448. return openprom_bsd_ioctl(inode,file,cmd,arg);
  449. case OPIOCSET:
  450. if ((file->f_mode & FMODE_WRITE) == 0)
  451. return -EBADF;
  452. return openprom_bsd_ioctl(inode,file,cmd,arg);
  453. default:
  454. if (cnt++ < 10)
  455. printk("openprom_ioctl: cmd 0x%X, arg 0x%lX\n", cmd, arg);
  456. return -EINVAL;
  457. }
  458. }
  459. static int openprom_open(struct inode * inode, struct file * file)
  460. {
  461. DATA *data;
  462. data = (DATA *) kmalloc(sizeof(DATA), GFP_KERNEL);
  463. if (!data)
  464. return -ENOMEM;
  465. data->current_node = prom_root_node;
  466. data->lastnode = prom_root_node;
  467. file->private_data = (void *)data;
  468. return 0;
  469. }
  470. static int openprom_release(struct inode * inode, struct file * file)
  471. {
  472. kfree(file->private_data);
  473. return 0;
  474. }
  475. static struct file_operations openprom_fops = {
  476. .owner = THIS_MODULE,
  477. .llseek = no_llseek,
  478. .ioctl = openprom_ioctl,
  479. .open = openprom_open,
  480. .release = openprom_release,
  481. };
  482. static struct miscdevice openprom_dev = {
  483. SUN_OPENPROM_MINOR, "openprom", &openprom_fops
  484. };
  485. static int __init openprom_init(void)
  486. {
  487. int error;
  488. error = misc_register(&openprom_dev);
  489. if (error) {
  490. printk(KERN_ERR "openprom: unable to get misc minor\n");
  491. return error;
  492. }
  493. options_node = prom_getchild(prom_root_node);
  494. options_node = prom_searchsiblings(options_node,"options");
  495. if (options_node == 0 || options_node == -1) {
  496. printk(KERN_ERR "openprom: unable to find options node\n");
  497. misc_deregister(&openprom_dev);
  498. return -EIO;
  499. }
  500. return 0;
  501. }
  502. static void __exit openprom_cleanup(void)
  503. {
  504. misc_deregister(&openprom_dev);
  505. }
  506. module_init(openprom_init);
  507. module_exit(openprom_cleanup);
  508. MODULE_LICENSE("GPL");