nvram_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. if (!ppc_md.nvram_size) {
  75. ret = -ENODEV;
  76. goto out;
  77. }
  78. size = ppc_md.nvram_size();
  79. if (size < 0) {
  80. ret = size;
  81. goto out;
  82. }
  83. if (*ppos >= size) {
  84. ret = 0;
  85. goto out;
  86. }
  87. count = min_t(size_t, count, size - *ppos);
  88. count = min(count, PAGE_SIZE);
  89. tmp = kmalloc(count, GFP_KERNEL);
  90. if (!tmp) {
  91. ret = -ENOMEM;
  92. goto out;
  93. }
  94. ret = ppc_md.nvram_read(tmp, count, ppos);
  95. if (ret <= 0)
  96. goto out;
  97. if (copy_to_user(buf, tmp, ret))
  98. ret = -EFAULT;
  99. out:
  100. kfree(tmp);
  101. return ret;
  102. }
  103. static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
  104. size_t count, loff_t *ppos)
  105. {
  106. ssize_t ret;
  107. char *tmp = NULL;
  108. ssize_t size;
  109. ret = -ENODEV;
  110. if (!ppc_md.nvram_size)
  111. goto out;
  112. ret = 0;
  113. size = ppc_md.nvram_size();
  114. if (*ppos >= size || size < 0)
  115. goto out;
  116. count = min_t(size_t, count, size - *ppos);
  117. count = min(count, PAGE_SIZE);
  118. ret = -ENOMEM;
  119. tmp = kmalloc(count, GFP_KERNEL);
  120. if (!tmp)
  121. goto out;
  122. ret = -EFAULT;
  123. if (copy_from_user(tmp, buf, count))
  124. goto out;
  125. ret = ppc_md.nvram_write(tmp, count, ppos);
  126. out:
  127. kfree(tmp);
  128. return ret;
  129. }
  130. static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
  131. unsigned long arg)
  132. {
  133. switch(cmd) {
  134. #ifdef CONFIG_PPC_PMAC
  135. case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
  136. printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
  137. case IOC_NVRAM_GET_OFFSET: {
  138. int part, offset;
  139. if (!machine_is(powermac))
  140. return -EINVAL;
  141. if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
  142. return -EFAULT;
  143. if (part < pmac_nvram_OF || part > pmac_nvram_NR)
  144. return -EINVAL;
  145. offset = pmac_get_partition(part);
  146. if (offset < 0)
  147. return offset;
  148. if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
  149. return -EFAULT;
  150. return 0;
  151. }
  152. #endif /* CONFIG_PPC_PMAC */
  153. default:
  154. return -EINVAL;
  155. }
  156. }
  157. const struct file_operations nvram_fops = {
  158. .owner = THIS_MODULE,
  159. .llseek = dev_nvram_llseek,
  160. .read = dev_nvram_read,
  161. .write = dev_nvram_write,
  162. .unlocked_ioctl = dev_nvram_ioctl,
  163. };
  164. static struct miscdevice nvram_dev = {
  165. NVRAM_MINOR,
  166. "nvram",
  167. &nvram_fops
  168. };
  169. #ifdef DEBUG_NVRAM
  170. static void __init nvram_print_partitions(char * label)
  171. {
  172. struct nvram_partition * tmp_part;
  173. printk(KERN_WARNING "--------%s---------\n", label);
  174. printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
  175. list_for_each_entry(tmp_part, &nvram_partitions, partition) {
  176. printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%12s\n",
  177. tmp_part->index, tmp_part->header.signature,
  178. tmp_part->header.checksum, tmp_part->header.length,
  179. tmp_part->header.name);
  180. }
  181. }
  182. #endif
  183. static int __init nvram_write_header(struct nvram_partition * part)
  184. {
  185. loff_t tmp_index;
  186. int rc;
  187. struct nvram_header phead;
  188. memcpy(&phead, &part->header, NVRAM_HEADER_LEN);
  189. phead.length = cpu_to_be16(phead.length);
  190. tmp_index = part->index;
  191. rc = ppc_md.nvram_write((char *)&phead, NVRAM_HEADER_LEN, &tmp_index);
  192. return rc;
  193. }
  194. static unsigned char __init nvram_checksum(struct nvram_header *p)
  195. {
  196. unsigned int c_sum, c_sum2;
  197. unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
  198. c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
  199. /* The sum may have spilled into the 3rd byte. Fold it back. */
  200. c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
  201. /* The sum cannot exceed 2 bytes. Fold it into a checksum */
  202. c_sum2 = (c_sum >> 8) + (c_sum << 8);
  203. c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
  204. return c_sum;
  205. }
  206. /*
  207. * Per the criteria passed via nvram_remove_partition(), should this
  208. * partition be removed? 1=remove, 0=keep
  209. */
  210. static int nvram_can_remove_partition(struct nvram_partition *part,
  211. const char *name, int sig, const char *exceptions[])
  212. {
  213. if (part->header.signature != sig)
  214. return 0;
  215. if (name) {
  216. if (strncmp(name, part->header.name, 12))
  217. return 0;
  218. } else if (exceptions) {
  219. const char **except;
  220. for (except = exceptions; *except; except++) {
  221. if (!strncmp(*except, part->header.name, 12))
  222. return 0;
  223. }
  224. }
  225. return 1;
  226. }
  227. /**
  228. * nvram_remove_partition - Remove one or more partitions in nvram
  229. * @name: name of the partition to remove, or NULL for a
  230. * signature only match
  231. * @sig: signature of the partition(s) to remove
  232. * @exceptions: When removing all partitions with a matching signature,
  233. * leave these alone.
  234. */
  235. int __init nvram_remove_partition(const char *name, int sig,
  236. const char *exceptions[])
  237. {
  238. struct nvram_partition *part, *prev, *tmp;
  239. int rc;
  240. list_for_each_entry(part, &nvram_partitions, partition) {
  241. if (!nvram_can_remove_partition(part, name, sig, exceptions))
  242. continue;
  243. /* Make partition a free partition */
  244. part->header.signature = NVRAM_SIG_FREE;
  245. strncpy(part->header.name, "wwwwwwwwwwww", 12);
  246. part->header.checksum = nvram_checksum(&part->header);
  247. rc = nvram_write_header(part);
  248. if (rc <= 0) {
  249. printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
  250. return rc;
  251. }
  252. }
  253. /* Merge contiguous ones */
  254. prev = NULL;
  255. list_for_each_entry_safe(part, tmp, &nvram_partitions, partition) {
  256. if (part->header.signature != NVRAM_SIG_FREE) {
  257. prev = NULL;
  258. continue;
  259. }
  260. if (prev) {
  261. prev->header.length += part->header.length;
  262. prev->header.checksum = nvram_checksum(&part->header);
  263. rc = nvram_write_header(part);
  264. if (rc <= 0) {
  265. printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
  266. return rc;
  267. }
  268. list_del(&part->partition);
  269. kfree(part);
  270. } else
  271. prev = part;
  272. }
  273. return 0;
  274. }
  275. /**
  276. * nvram_create_partition - Create a partition in nvram
  277. * @name: name of the partition to create
  278. * @sig: signature of the partition to create
  279. * @req_size: size of data to allocate in bytes
  280. * @min_size: minimum acceptable size (0 means req_size)
  281. *
  282. * Returns a negative error code or a positive nvram index
  283. * of the beginning of the data area of the newly created
  284. * partition. If you provided a min_size smaller than req_size
  285. * you need to query for the actual size yourself after the
  286. * call using nvram_partition_get_size().
  287. */
  288. loff_t __init nvram_create_partition(const char *name, int sig,
  289. int req_size, int min_size)
  290. {
  291. struct nvram_partition *part;
  292. struct nvram_partition *new_part;
  293. struct nvram_partition *free_part = NULL;
  294. static char nv_init_vals[16];
  295. loff_t tmp_index;
  296. long size = 0;
  297. int rc;
  298. /* Convert sizes from bytes to blocks */
  299. req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
  300. min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
  301. /* If no minimum size specified, make it the same as the
  302. * requested size
  303. */
  304. if (min_size == 0)
  305. min_size = req_size;
  306. if (min_size > req_size)
  307. return -EINVAL;
  308. /* Now add one block to each for the header */
  309. req_size += 1;
  310. min_size += 1;
  311. /* Find a free partition that will give us the maximum needed size
  312. If can't find one that will give us the minimum size needed */
  313. list_for_each_entry(part, &nvram_partitions, partition) {
  314. if (part->header.signature != NVRAM_SIG_FREE)
  315. continue;
  316. if (part->header.length >= req_size) {
  317. size = req_size;
  318. free_part = part;
  319. break;
  320. }
  321. if (part->header.length > size &&
  322. part->header.length >= min_size) {
  323. size = part->header.length;
  324. free_part = part;
  325. }
  326. }
  327. if (!size)
  328. return -ENOSPC;
  329. /* Create our OS partition */
  330. new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
  331. if (!new_part) {
  332. pr_err("nvram_create_os_partition: kmalloc failed\n");
  333. return -ENOMEM;
  334. }
  335. new_part->index = free_part->index;
  336. new_part->header.signature = sig;
  337. new_part->header.length = size;
  338. strncpy(new_part->header.name, name, 12);
  339. new_part->header.checksum = nvram_checksum(&new_part->header);
  340. rc = nvram_write_header(new_part);
  341. if (rc <= 0) {
  342. pr_err("nvram_create_os_partition: nvram_write_header "
  343. "failed (%d)\n", rc);
  344. return rc;
  345. }
  346. list_add_tail(&new_part->partition, &free_part->partition);
  347. /* Adjust or remove the partition we stole the space from */
  348. if (free_part->header.length > size) {
  349. free_part->index += size * NVRAM_BLOCK_LEN;
  350. free_part->header.length -= size;
  351. free_part->header.checksum = nvram_checksum(&free_part->header);
  352. rc = nvram_write_header(free_part);
  353. if (rc <= 0) {
  354. pr_err("nvram_create_os_partition: nvram_write_header "
  355. "failed (%d)\n", rc);
  356. return rc;
  357. }
  358. } else {
  359. list_del(&free_part->partition);
  360. kfree(free_part);
  361. }
  362. /* Clear the new partition */
  363. for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
  364. tmp_index < ((size - 1) * NVRAM_BLOCK_LEN);
  365. tmp_index += NVRAM_BLOCK_LEN) {
  366. rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
  367. if (rc <= 0) {
  368. pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc);
  369. return rc;
  370. }
  371. }
  372. return new_part->index + NVRAM_HEADER_LEN;
  373. }
  374. /**
  375. * nvram_get_partition_size - Get the data size of an nvram partition
  376. * @data_index: This is the offset of the start of the data of
  377. * the partition. The same value that is returned by
  378. * nvram_create_partition().
  379. */
  380. int nvram_get_partition_size(loff_t data_index)
  381. {
  382. struct nvram_partition *part;
  383. list_for_each_entry(part, &nvram_partitions, partition) {
  384. if (part->index + NVRAM_HEADER_LEN == data_index)
  385. return (part->header.length - 1) * NVRAM_BLOCK_LEN;
  386. }
  387. return -1;
  388. }
  389. /**
  390. * nvram_find_partition - Find an nvram partition by signature and name
  391. * @name: Name of the partition or NULL for any name
  392. * @sig: Signature to test against
  393. * @out_size: if non-NULL, returns the size of the data part of the partition
  394. */
  395. loff_t nvram_find_partition(const char *name, int sig, int *out_size)
  396. {
  397. struct nvram_partition *p;
  398. list_for_each_entry(p, &nvram_partitions, partition) {
  399. if (p->header.signature == sig &&
  400. (!name || !strncmp(p->header.name, name, 12))) {
  401. if (out_size)
  402. *out_size = (p->header.length - 1) *
  403. NVRAM_BLOCK_LEN;
  404. return p->index + NVRAM_HEADER_LEN;
  405. }
  406. }
  407. return 0;
  408. }
  409. int __init nvram_scan_partitions(void)
  410. {
  411. loff_t cur_index = 0;
  412. struct nvram_header phead;
  413. struct nvram_partition * tmp_part;
  414. unsigned char c_sum;
  415. char * header;
  416. int total_size;
  417. int err;
  418. if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
  419. return -ENODEV;
  420. total_size = ppc_md.nvram_size();
  421. header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
  422. if (!header) {
  423. printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
  424. return -ENOMEM;
  425. }
  426. while (cur_index < total_size) {
  427. err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
  428. if (err != NVRAM_HEADER_LEN) {
  429. printk(KERN_ERR "nvram_scan_partitions: Error parsing "
  430. "nvram partitions\n");
  431. goto out;
  432. }
  433. cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
  434. memcpy(&phead, header, NVRAM_HEADER_LEN);
  435. phead.length = be16_to_cpu(phead.length);
  436. err = 0;
  437. c_sum = nvram_checksum(&phead);
  438. if (c_sum != phead.checksum) {
  439. printk(KERN_WARNING "WARNING: nvram partition checksum"
  440. " was %02x, should be %02x!\n",
  441. phead.checksum, c_sum);
  442. printk(KERN_WARNING "Terminating nvram partition scan\n");
  443. goto out;
  444. }
  445. if (!phead.length) {
  446. printk(KERN_WARNING "WARNING: nvram corruption "
  447. "detected: 0-length partition\n");
  448. goto out;
  449. }
  450. tmp_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
  451. err = -ENOMEM;
  452. if (!tmp_part) {
  453. printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
  454. goto out;
  455. }
  456. memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
  457. tmp_part->index = cur_index;
  458. list_add_tail(&tmp_part->partition, &nvram_partitions);
  459. cur_index += phead.length * NVRAM_BLOCK_LEN;
  460. }
  461. err = 0;
  462. #ifdef DEBUG_NVRAM
  463. nvram_print_partitions("NVRAM Partitions");
  464. #endif
  465. out:
  466. kfree(header);
  467. return err;
  468. }
  469. static int __init nvram_init(void)
  470. {
  471. int rc;
  472. BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
  473. if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
  474. return -ENODEV;
  475. rc = misc_register(&nvram_dev);
  476. if (rc != 0) {
  477. printk(KERN_ERR "nvram_init: failed to register device\n");
  478. return rc;
  479. }
  480. return rc;
  481. }
  482. void __exit nvram_cleanup(void)
  483. {
  484. misc_deregister( &nvram_dev );
  485. }
  486. module_init(nvram_init);
  487. module_exit(nvram_cleanup);
  488. MODULE_LICENSE("GPL");