nvram_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. tmp_index = part->index;
  188. rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
  189. return rc;
  190. }
  191. static unsigned char __init nvram_checksum(struct nvram_header *p)
  192. {
  193. unsigned int c_sum, c_sum2;
  194. unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
  195. c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
  196. /* The sum may have spilled into the 3rd byte. Fold it back. */
  197. c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
  198. /* The sum cannot exceed 2 bytes. Fold it into a checksum */
  199. c_sum2 = (c_sum >> 8) + (c_sum << 8);
  200. c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
  201. return c_sum;
  202. }
  203. /*
  204. * Per the criteria passed via nvram_remove_partition(), should this
  205. * partition be removed? 1=remove, 0=keep
  206. */
  207. static int nvram_can_remove_partition(struct nvram_partition *part,
  208. const char *name, int sig, const char *exceptions[])
  209. {
  210. if (part->header.signature != sig)
  211. return 0;
  212. if (name) {
  213. if (strncmp(name, part->header.name, 12))
  214. return 0;
  215. } else if (exceptions) {
  216. const char **except;
  217. for (except = exceptions; *except; except++) {
  218. if (!strncmp(*except, part->header.name, 12))
  219. return 0;
  220. }
  221. }
  222. return 1;
  223. }
  224. /**
  225. * nvram_remove_partition - Remove one or more partitions in nvram
  226. * @name: name of the partition to remove, or NULL for a
  227. * signature only match
  228. * @sig: signature of the partition(s) to remove
  229. * @exceptions: When removing all partitions with a matching signature,
  230. * leave these alone.
  231. */
  232. int __init nvram_remove_partition(const char *name, int sig,
  233. const char *exceptions[])
  234. {
  235. struct nvram_partition *part, *prev, *tmp;
  236. int rc;
  237. list_for_each_entry(part, &nvram_partitions, partition) {
  238. if (!nvram_can_remove_partition(part, name, sig, exceptions))
  239. continue;
  240. /* Make partition a free partition */
  241. part->header.signature = NVRAM_SIG_FREE;
  242. strncpy(part->header.name, "wwwwwwwwwwww", 12);
  243. part->header.checksum = nvram_checksum(&part->header);
  244. rc = nvram_write_header(part);
  245. if (rc <= 0) {
  246. printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
  247. return rc;
  248. }
  249. }
  250. /* Merge contiguous ones */
  251. prev = NULL;
  252. list_for_each_entry_safe(part, tmp, &nvram_partitions, partition) {
  253. if (part->header.signature != NVRAM_SIG_FREE) {
  254. prev = NULL;
  255. continue;
  256. }
  257. if (prev) {
  258. prev->header.length += part->header.length;
  259. prev->header.checksum = nvram_checksum(&part->header);
  260. rc = nvram_write_header(part);
  261. if (rc <= 0) {
  262. printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
  263. return rc;
  264. }
  265. list_del(&part->partition);
  266. kfree(part);
  267. } else
  268. prev = part;
  269. }
  270. return 0;
  271. }
  272. /**
  273. * nvram_create_partition - Create a partition in nvram
  274. * @name: name of the partition to create
  275. * @sig: signature of the partition to create
  276. * @req_size: size of data to allocate in bytes
  277. * @min_size: minimum acceptable size (0 means req_size)
  278. *
  279. * Returns a negative error code or a positive nvram index
  280. * of the beginning of the data area of the newly created
  281. * partition. If you provided a min_size smaller than req_size
  282. * you need to query for the actual size yourself after the
  283. * call using nvram_partition_get_size().
  284. */
  285. loff_t __init nvram_create_partition(const char *name, int sig,
  286. int req_size, int min_size)
  287. {
  288. struct nvram_partition *part;
  289. struct nvram_partition *new_part;
  290. struct nvram_partition *free_part = NULL;
  291. static char nv_init_vals[16];
  292. loff_t tmp_index;
  293. long size = 0;
  294. int rc;
  295. /* Convert sizes from bytes to blocks */
  296. req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
  297. min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
  298. /* If no minimum size specified, make it the same as the
  299. * requested size
  300. */
  301. if (min_size == 0)
  302. min_size = req_size;
  303. if (min_size > req_size)
  304. return -EINVAL;
  305. /* Now add one block to each for the header */
  306. req_size += 1;
  307. min_size += 1;
  308. /* Find a free partition that will give us the maximum needed size
  309. If can't find one that will give us the minimum size needed */
  310. list_for_each_entry(part, &nvram_partitions, partition) {
  311. if (part->header.signature != NVRAM_SIG_FREE)
  312. continue;
  313. if (part->header.length >= req_size) {
  314. size = req_size;
  315. free_part = part;
  316. break;
  317. }
  318. if (part->header.length > size &&
  319. part->header.length >= min_size) {
  320. size = part->header.length;
  321. free_part = part;
  322. }
  323. }
  324. if (!size)
  325. return -ENOSPC;
  326. /* Create our OS partition */
  327. new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
  328. if (!new_part) {
  329. pr_err("nvram_create_os_partition: kmalloc failed\n");
  330. return -ENOMEM;
  331. }
  332. new_part->index = free_part->index;
  333. new_part->header.signature = sig;
  334. new_part->header.length = size;
  335. strncpy(new_part->header.name, name, 12);
  336. new_part->header.checksum = nvram_checksum(&new_part->header);
  337. rc = nvram_write_header(new_part);
  338. if (rc <= 0) {
  339. pr_err("nvram_create_os_partition: nvram_write_header "
  340. "failed (%d)\n", rc);
  341. return rc;
  342. }
  343. list_add_tail(&new_part->partition, &free_part->partition);
  344. /* Adjust or remove the partition we stole the space from */
  345. if (free_part->header.length > size) {
  346. free_part->index += size * NVRAM_BLOCK_LEN;
  347. free_part->header.length -= size;
  348. free_part->header.checksum = nvram_checksum(&free_part->header);
  349. rc = nvram_write_header(free_part);
  350. if (rc <= 0) {
  351. pr_err("nvram_create_os_partition: nvram_write_header "
  352. "failed (%d)\n", rc);
  353. return rc;
  354. }
  355. } else {
  356. list_del(&free_part->partition);
  357. kfree(free_part);
  358. }
  359. /* Clear the new partition */
  360. for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
  361. tmp_index < ((size - 1) * NVRAM_BLOCK_LEN);
  362. tmp_index += NVRAM_BLOCK_LEN) {
  363. rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
  364. if (rc <= 0) {
  365. pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc);
  366. return rc;
  367. }
  368. }
  369. return new_part->index + NVRAM_HEADER_LEN;
  370. }
  371. /**
  372. * nvram_get_partition_size - Get the data size of an nvram partition
  373. * @data_index: This is the offset of the start of the data of
  374. * the partition. The same value that is returned by
  375. * nvram_create_partition().
  376. */
  377. int nvram_get_partition_size(loff_t data_index)
  378. {
  379. struct nvram_partition *part;
  380. list_for_each_entry(part, &nvram_partitions, partition) {
  381. if (part->index + NVRAM_HEADER_LEN == data_index)
  382. return (part->header.length - 1) * NVRAM_BLOCK_LEN;
  383. }
  384. return -1;
  385. }
  386. /**
  387. * nvram_find_partition - Find an nvram partition by signature and name
  388. * @name: Name of the partition or NULL for any name
  389. * @sig: Signature to test against
  390. * @out_size: if non-NULL, returns the size of the data part of the partition
  391. */
  392. loff_t nvram_find_partition(const char *name, int sig, int *out_size)
  393. {
  394. struct nvram_partition *p;
  395. list_for_each_entry(p, &nvram_partitions, partition) {
  396. if (p->header.signature == sig &&
  397. (!name || !strncmp(p->header.name, name, 12))) {
  398. if (out_size)
  399. *out_size = (p->header.length - 1) *
  400. NVRAM_BLOCK_LEN;
  401. return p->index + NVRAM_HEADER_LEN;
  402. }
  403. }
  404. return 0;
  405. }
  406. int __init nvram_scan_partitions(void)
  407. {
  408. loff_t cur_index = 0;
  409. struct nvram_header phead;
  410. struct nvram_partition * tmp_part;
  411. unsigned char c_sum;
  412. char * header;
  413. int total_size;
  414. int err;
  415. if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
  416. return -ENODEV;
  417. total_size = ppc_md.nvram_size();
  418. header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
  419. if (!header) {
  420. printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
  421. return -ENOMEM;
  422. }
  423. while (cur_index < total_size) {
  424. err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
  425. if (err != NVRAM_HEADER_LEN) {
  426. printk(KERN_ERR "nvram_scan_partitions: Error parsing "
  427. "nvram partitions\n");
  428. goto out;
  429. }
  430. cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
  431. memcpy(&phead, header, NVRAM_HEADER_LEN);
  432. err = 0;
  433. c_sum = nvram_checksum(&phead);
  434. if (c_sum != phead.checksum) {
  435. printk(KERN_WARNING "WARNING: nvram partition checksum"
  436. " was %02x, should be %02x!\n",
  437. phead.checksum, c_sum);
  438. printk(KERN_WARNING "Terminating nvram partition scan\n");
  439. goto out;
  440. }
  441. if (!phead.length) {
  442. printk(KERN_WARNING "WARNING: nvram corruption "
  443. "detected: 0-length partition\n");
  444. goto out;
  445. }
  446. tmp_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
  447. err = -ENOMEM;
  448. if (!tmp_part) {
  449. printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
  450. goto out;
  451. }
  452. memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
  453. tmp_part->index = cur_index;
  454. list_add_tail(&tmp_part->partition, &nvram_partitions);
  455. cur_index += phead.length * NVRAM_BLOCK_LEN;
  456. }
  457. err = 0;
  458. #ifdef DEBUG_NVRAM
  459. nvram_print_partitions("NVRAM Partitions");
  460. #endif
  461. out:
  462. kfree(header);
  463. return err;
  464. }
  465. static int __init nvram_init(void)
  466. {
  467. int rc;
  468. BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
  469. if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
  470. return -ENODEV;
  471. rc = misc_register(&nvram_dev);
  472. if (rc != 0) {
  473. printk(KERN_ERR "nvram_init: failed to register device\n");
  474. return rc;
  475. }
  476. return rc;
  477. }
  478. void __exit nvram_cleanup(void)
  479. {
  480. misc_deregister( &nvram_dev );
  481. }
  482. module_init(nvram_init);
  483. module_exit(nvram_cleanup);
  484. MODULE_LICENSE("GPL");