nvram_64.c 14 KB

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