nvram_64.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * c 2001 PPC 64 Team, IBM Corp
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * /dev/nvram driver for PPC64
  10. *
  11. * This perhaps should live in drivers/char
  12. *
  13. * TODO: Split the /dev/nvram part (that one can use
  14. * drivers/char/generic_nvram.c) from the arch & partition
  15. * parsing code.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/errno.h>
  20. #include <linux/fs.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/nvram.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/spinlock.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/nvram.h>
  29. #include <asm/rtas.h>
  30. #include <asm/prom.h>
  31. #include <asm/machdep.h>
  32. #undef DEBUG_NVRAM
  33. #define NVRAM_HEADER_LEN sizeof(struct nvram_header)
  34. #define NVRAM_BLOCK_LEN NVRAM_HEADER_LEN
  35. /* If change this size, then change the size of NVNAME_LEN */
  36. struct nvram_header {
  37. unsigned char signature;
  38. unsigned char checksum;
  39. unsigned short length;
  40. /* Terminating null required only for names < 12 chars. */
  41. char name[12];
  42. };
  43. struct nvram_partition {
  44. struct list_head partition;
  45. struct nvram_header header;
  46. unsigned int index;
  47. };
  48. static LIST_HEAD(nvram_partitions);
  49. static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
  50. {
  51. int size;
  52. if (ppc_md.nvram_size == NULL)
  53. return -ENODEV;
  54. size = ppc_md.nvram_size();
  55. switch (origin) {
  56. case 1:
  57. offset += file->f_pos;
  58. break;
  59. case 2:
  60. offset += size;
  61. break;
  62. }
  63. if (offset < 0)
  64. return -EINVAL;
  65. file->f_pos = offset;
  66. return file->f_pos;
  67. }
  68. static ssize_t dev_nvram_read(struct file *file, char __user *buf,
  69. size_t count, loff_t *ppos)
  70. {
  71. ssize_t ret;
  72. char *tmp = NULL;
  73. ssize_t size;
  74. ret = -ENODEV;
  75. if (!ppc_md.nvram_size)
  76. goto out;
  77. ret = 0;
  78. size = ppc_md.nvram_size();
  79. if (*ppos >= size || size < 0)
  80. goto out;
  81. count = min_t(size_t, count, size - *ppos);
  82. count = min(count, PAGE_SIZE);
  83. ret = -ENOMEM;
  84. tmp = kmalloc(count, GFP_KERNEL);
  85. if (!tmp)
  86. goto out;
  87. ret = ppc_md.nvram_read(tmp, count, ppos);
  88. if (ret <= 0)
  89. goto out;
  90. if (copy_to_user(buf, tmp, ret))
  91. ret = -EFAULT;
  92. out:
  93. kfree(tmp);
  94. return ret;
  95. }
  96. static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
  97. size_t count, loff_t *ppos)
  98. {
  99. ssize_t ret;
  100. char *tmp = NULL;
  101. ssize_t size;
  102. ret = -ENODEV;
  103. if (!ppc_md.nvram_size)
  104. goto out;
  105. ret = 0;
  106. size = ppc_md.nvram_size();
  107. if (*ppos >= size || size < 0)
  108. goto out;
  109. count = min_t(size_t, count, size - *ppos);
  110. count = min(count, PAGE_SIZE);
  111. ret = -ENOMEM;
  112. tmp = kmalloc(count, GFP_KERNEL);
  113. if (!tmp)
  114. goto out;
  115. ret = -EFAULT;
  116. if (copy_from_user(tmp, buf, count))
  117. goto out;
  118. ret = ppc_md.nvram_write(tmp, count, ppos);
  119. out:
  120. kfree(tmp);
  121. return ret;
  122. }
  123. static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
  124. unsigned long arg)
  125. {
  126. switch(cmd) {
  127. #ifdef CONFIG_PPC_PMAC
  128. case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
  129. printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
  130. case IOC_NVRAM_GET_OFFSET: {
  131. int part, offset;
  132. if (!machine_is(powermac))
  133. return -EINVAL;
  134. if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
  135. return -EFAULT;
  136. if (part < pmac_nvram_OF || part > pmac_nvram_NR)
  137. return -EINVAL;
  138. offset = pmac_get_partition(part);
  139. if (offset < 0)
  140. return offset;
  141. if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
  142. return -EFAULT;
  143. return 0;
  144. }
  145. #endif /* CONFIG_PPC_PMAC */
  146. default:
  147. return -EINVAL;
  148. }
  149. }
  150. const struct file_operations nvram_fops = {
  151. .owner = THIS_MODULE,
  152. .llseek = dev_nvram_llseek,
  153. .read = dev_nvram_read,
  154. .write = dev_nvram_write,
  155. .unlocked_ioctl = dev_nvram_ioctl,
  156. };
  157. static struct miscdevice nvram_dev = {
  158. NVRAM_MINOR,
  159. "nvram",
  160. &nvram_fops
  161. };
  162. #ifdef DEBUG_NVRAM
  163. static void __init nvram_print_partitions(char * label)
  164. {
  165. struct nvram_partition * tmp_part;
  166. printk(KERN_WARNING "--------%s---------\n", label);
  167. printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
  168. list_for_each_entry(tmp_part, &nvram_partitions, partition) {
  169. printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%12s\n",
  170. tmp_part->index, tmp_part->header.signature,
  171. tmp_part->header.checksum, tmp_part->header.length,
  172. tmp_part->header.name);
  173. }
  174. }
  175. #endif
  176. static int __init nvram_write_header(struct nvram_partition * part)
  177. {
  178. loff_t tmp_index;
  179. int rc;
  180. tmp_index = part->index;
  181. rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
  182. return rc;
  183. }
  184. static unsigned char __init nvram_checksum(struct nvram_header *p)
  185. {
  186. unsigned int c_sum, c_sum2;
  187. unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
  188. c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
  189. /* The sum may have spilled into the 3rd byte. Fold it back. */
  190. c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
  191. /* The sum cannot exceed 2 bytes. Fold it into a checksum */
  192. c_sum2 = (c_sum >> 8) + (c_sum << 8);
  193. c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
  194. return c_sum;
  195. }
  196. /**
  197. * nvram_remove_partition - Remove one or more partitions in nvram
  198. * @name: name of the partition to remove, or NULL for a
  199. * signature only match
  200. * @sig: signature of the partition(s) to remove
  201. */
  202. int __init nvram_remove_partition(const char *name, int sig)
  203. {
  204. struct nvram_partition *part, *prev, *tmp;
  205. int rc;
  206. list_for_each_entry(part, &nvram_partitions, partition) {
  207. if (part->header.signature != sig)
  208. continue;
  209. if (name && strncmp(name, part->header.name, 12))
  210. continue;
  211. /* Make partition a free partition */
  212. part->header.signature = NVRAM_SIG_FREE;
  213. strncpy(part->header.name, "wwwwwwwwwwww", 12);
  214. part->header.checksum = nvram_checksum(&part->header);
  215. rc = nvram_write_header(part);
  216. if (rc <= 0) {
  217. printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
  218. return rc;
  219. }
  220. }
  221. /* Merge contiguous ones */
  222. prev = NULL;
  223. list_for_each_entry_safe(part, tmp, &nvram_partitions, partition) {
  224. if (part->header.signature != NVRAM_SIG_FREE) {
  225. prev = NULL;
  226. continue;
  227. }
  228. if (prev) {
  229. prev->header.length += part->header.length;
  230. prev->header.checksum = nvram_checksum(&part->header);
  231. rc = nvram_write_header(part);
  232. if (rc <= 0) {
  233. printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
  234. return rc;
  235. }
  236. list_del(&part->partition);
  237. kfree(part);
  238. } else
  239. prev = part;
  240. }
  241. return 0;
  242. }
  243. /**
  244. * nvram_create_partition - Create a partition in nvram
  245. * @name: name of the partition to create
  246. * @sig: signature of the partition to create
  247. * @req_size: size of data to allocate in bytes
  248. * @min_size: minimum acceptable size (0 means req_size)
  249. *
  250. * Returns a negative error code or a positive nvram index
  251. * of the beginning of the data area of the newly created
  252. * partition. If you provided a min_size smaller than req_size
  253. * you need to query for the actual size yourself after the
  254. * call using nvram_partition_get_size().
  255. */
  256. loff_t __init nvram_create_partition(const char *name, int sig,
  257. int req_size, int min_size)
  258. {
  259. struct nvram_partition *part;
  260. struct nvram_partition *new_part;
  261. struct nvram_partition *free_part = NULL;
  262. static char nv_init_vals[16];
  263. loff_t tmp_index;
  264. long size = 0;
  265. int rc;
  266. /* Convert sizes from bytes to blocks */
  267. req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
  268. min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
  269. /* If no minimum size specified, make it the same as the
  270. * requested size
  271. */
  272. if (min_size == 0)
  273. min_size = req_size;
  274. if (min_size > req_size)
  275. return -EINVAL;
  276. /* Now add one block to each for the header */
  277. req_size += 1;
  278. min_size += 1;
  279. /* Find a free partition that will give us the maximum needed size
  280. If can't find one that will give us the minimum size needed */
  281. list_for_each_entry(part, &nvram_partitions, partition) {
  282. if (part->header.signature != NVRAM_SIG_FREE)
  283. continue;
  284. if (part->header.length >= req_size) {
  285. size = req_size;
  286. free_part = part;
  287. break;
  288. }
  289. if (part->header.length > size &&
  290. part->header.length >= min_size) {
  291. size = part->header.length;
  292. free_part = part;
  293. }
  294. }
  295. if (!size)
  296. return -ENOSPC;
  297. /* Create our OS partition */
  298. new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
  299. if (!new_part) {
  300. pr_err("nvram_create_os_partition: kmalloc failed\n");
  301. return -ENOMEM;
  302. }
  303. new_part->index = free_part->index;
  304. new_part->header.signature = sig;
  305. new_part->header.length = size;
  306. strncpy(new_part->header.name, name, 12);
  307. new_part->header.checksum = nvram_checksum(&new_part->header);
  308. rc = nvram_write_header(new_part);
  309. if (rc <= 0) {
  310. pr_err("nvram_create_os_partition: nvram_write_header "
  311. "failed (%d)\n", rc);
  312. return rc;
  313. }
  314. list_add_tail(&new_part->partition, &free_part->partition);
  315. /* Adjust or remove the partition we stole the space from */
  316. if (free_part->header.length > size) {
  317. free_part->index += size * NVRAM_BLOCK_LEN;
  318. free_part->header.length -= size;
  319. free_part->header.checksum = nvram_checksum(&free_part->header);
  320. rc = nvram_write_header(free_part);
  321. if (rc <= 0) {
  322. pr_err("nvram_create_os_partition: nvram_write_header "
  323. "failed (%d)\n", rc);
  324. return rc;
  325. }
  326. } else {
  327. list_del(&free_part->partition);
  328. kfree(free_part);
  329. }
  330. /* Clear the new partition */
  331. for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
  332. tmp_index < ((size - 1) * NVRAM_BLOCK_LEN);
  333. tmp_index += NVRAM_BLOCK_LEN) {
  334. rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
  335. if (rc <= 0) {
  336. pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc);
  337. return rc;
  338. }
  339. }
  340. return new_part->index + NVRAM_HEADER_LEN;
  341. }
  342. /**
  343. * nvram_get_partition_size - Get the data size of an nvram partition
  344. * @data_index: This is the offset of the start of the data of
  345. * the partition. The same value that is returned by
  346. * nvram_create_partition().
  347. */
  348. int nvram_get_partition_size(loff_t data_index)
  349. {
  350. struct nvram_partition *part;
  351. list_for_each_entry(part, &nvram_partitions, partition) {
  352. if (part->index + NVRAM_HEADER_LEN == data_index)
  353. return (part->header.length - 1) * NVRAM_BLOCK_LEN;
  354. }
  355. return -1;
  356. }
  357. /**
  358. * nvram_find_partition - Find an nvram partition by signature and name
  359. * @name: Name of the partition or NULL for any name
  360. * @sig: Signature to test against
  361. * @out_size: if non-NULL, returns the size of the data part of the partition
  362. */
  363. loff_t nvram_find_partition(const char *name, int sig, int *out_size)
  364. {
  365. struct nvram_partition *p;
  366. list_for_each_entry(p, &nvram_partitions, partition) {
  367. if (p->header.signature == sig &&
  368. (!name || !strncmp(p->header.name, name, 12))) {
  369. if (out_size)
  370. *out_size = (p->header.length - 1) *
  371. NVRAM_BLOCK_LEN;
  372. return p->index + NVRAM_HEADER_LEN;
  373. }
  374. }
  375. return 0;
  376. }
  377. int __init nvram_scan_partitions(void)
  378. {
  379. loff_t cur_index = 0;
  380. struct nvram_header phead;
  381. struct nvram_partition * tmp_part;
  382. unsigned char c_sum;
  383. char * header;
  384. int total_size;
  385. int err;
  386. if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
  387. return -ENODEV;
  388. total_size = ppc_md.nvram_size();
  389. header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
  390. if (!header) {
  391. printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
  392. return -ENOMEM;
  393. }
  394. while (cur_index < total_size) {
  395. err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
  396. if (err != NVRAM_HEADER_LEN) {
  397. printk(KERN_ERR "nvram_scan_partitions: Error parsing "
  398. "nvram partitions\n");
  399. goto out;
  400. }
  401. cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
  402. memcpy(&phead, header, NVRAM_HEADER_LEN);
  403. err = 0;
  404. c_sum = nvram_checksum(&phead);
  405. if (c_sum != phead.checksum) {
  406. printk(KERN_WARNING "WARNING: nvram partition checksum"
  407. " was %02x, should be %02x!\n",
  408. phead.checksum, c_sum);
  409. printk(KERN_WARNING "Terminating nvram partition scan\n");
  410. goto out;
  411. }
  412. if (!phead.length) {
  413. printk(KERN_WARNING "WARNING: nvram corruption "
  414. "detected: 0-length partition\n");
  415. goto out;
  416. }
  417. tmp_part = (struct nvram_partition *)
  418. kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
  419. err = -ENOMEM;
  420. if (!tmp_part) {
  421. printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
  422. goto out;
  423. }
  424. memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
  425. tmp_part->index = cur_index;
  426. list_add_tail(&tmp_part->partition, &nvram_partitions);
  427. cur_index += phead.length * NVRAM_BLOCK_LEN;
  428. }
  429. err = 0;
  430. #ifdef DEBUG_NVRAM
  431. nvram_print_partitions("NVRAM Partitions");
  432. #endif
  433. out:
  434. kfree(header);
  435. return err;
  436. }
  437. static int __init nvram_init(void)
  438. {
  439. int rc;
  440. BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
  441. if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
  442. return -ENODEV;
  443. rc = misc_register(&nvram_dev);
  444. if (rc != 0) {
  445. printk(KERN_ERR "nvram_init: failed to register device\n");
  446. return rc;
  447. }
  448. return rc;
  449. }
  450. void __exit nvram_cleanup(void)
  451. {
  452. misc_deregister( &nvram_dev );
  453. }
  454. module_init(nvram_init);
  455. module_exit(nvram_cleanup);
  456. MODULE_LICENSE("GPL");