]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/powerpc/kernel/nvram_64.c
Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-artful-kernel.git] / arch / powerpc / kernel / nvram_64.c
CommitLineData
1da177e4
LT
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
18#include <linux/module.h>
19
20#include <linux/types.h>
21#include <linux/errno.h>
22#include <linux/fs.h>
23#include <linux/miscdevice.h>
24#include <linux/fcntl.h>
25#include <linux/nvram.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/spinlock.h>
29#include <asm/uaccess.h>
30#include <asm/nvram.h>
31#include <asm/rtas.h>
32#include <asm/prom.h>
33#include <asm/machdep.h>
1da177e4
LT
34
35#undef DEBUG_NVRAM
36
36673307
BH
37#define NVRAM_HEADER_LEN sizeof(struct nvram_header)
38#define NVRAM_BLOCK_LEN NVRAM_HEADER_LEN
74d51d02
BH
39
40/* If change this size, then change the size of NVNAME_LEN */
41struct nvram_header {
42 unsigned char signature;
43 unsigned char checksum;
44 unsigned short length;
6024ede9 45 /* Terminating null required only for names < 12 chars. */
74d51d02
BH
46 char name[12];
47};
48
49struct nvram_partition {
50 struct list_head partition;
51 struct nvram_header header;
52 unsigned int index;
53};
54
690d1a9b 55static LIST_HEAD(nvram_partitions);
1da177e4
LT
56
57static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
58{
59 int size;
60
61 if (ppc_md.nvram_size == NULL)
62 return -ENODEV;
63 size = ppc_md.nvram_size();
64
65 switch (origin) {
66 case 1:
67 offset += file->f_pos;
68 break;
69 case 2:
70 offset += size;
71 break;
72 }
73 if (offset < 0)
74 return -EINVAL;
75 file->f_pos = offset;
76 return file->f_pos;
77}
78
79
80static ssize_t dev_nvram_read(struct file *file, char __user *buf,
81 size_t count, loff_t *ppos)
82{
f9ce299f
AB
83 ssize_t ret;
84 char *tmp = NULL;
85 ssize_t size;
1da177e4 86
7029705a
CG
87 if (!ppc_md.nvram_size) {
88 ret = -ENODEV;
f9ce299f 89 goto out;
7029705a 90 }
f9ce299f 91
1da177e4 92 size = ppc_md.nvram_size();
7029705a
CG
93 if (size < 0) {
94 ret = size;
f9ce299f 95 goto out;
7029705a
CG
96 }
97
98 if (*ppos >= size) {
99 ret = 0;
100 goto out;
101 }
1da177e4 102
f9ce299f
AB
103 count = min_t(size_t, count, size - *ppos);
104 count = min(count, PAGE_SIZE);
1da177e4 105
f9ce299f 106 tmp = kmalloc(count, GFP_KERNEL);
7029705a
CG
107 if (!tmp) {
108 ret = -ENOMEM;
f9ce299f 109 goto out;
7029705a 110 }
1da177e4 111
f9ce299f
AB
112 ret = ppc_md.nvram_read(tmp, count, ppos);
113 if (ret <= 0)
114 goto out;
115
116 if (copy_to_user(buf, tmp, ret))
117 ret = -EFAULT;
1da177e4 118
f9ce299f
AB
119out:
120 kfree(tmp);
121 return ret;
1da177e4
LT
122
123}
124
125static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
f9ce299f 126 size_t count, loff_t *ppos)
1da177e4 127{
f9ce299f
AB
128 ssize_t ret;
129 char *tmp = NULL;
130 ssize_t size;
1da177e4 131
f9ce299f
AB
132 ret = -ENODEV;
133 if (!ppc_md.nvram_size)
134 goto out;
135
136 ret = 0;
1da177e4 137 size = ppc_md.nvram_size();
f9ce299f
AB
138 if (*ppos >= size || size < 0)
139 goto out;
1da177e4 140
f9ce299f
AB
141 count = min_t(size_t, count, size - *ppos);
142 count = min(count, PAGE_SIZE);
1da177e4 143
f9ce299f
AB
144 ret = -ENOMEM;
145 tmp = kmalloc(count, GFP_KERNEL);
146 if (!tmp)
147 goto out;
148
149 ret = -EFAULT;
150 if (copy_from_user(tmp, buf, count))
151 goto out;
152
153 ret = ppc_md.nvram_write(tmp, count, ppos);
154
155out:
156 kfree(tmp);
157 return ret;
1da177e4 158
1da177e4
LT
159}
160
3b03fecd
TG
161static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
162 unsigned long arg)
1da177e4
LT
163{
164 switch(cmd) {
165#ifdef CONFIG_PPC_PMAC
166 case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
167 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
168 case IOC_NVRAM_GET_OFFSET: {
169 int part, offset;
170
e8222502 171 if (!machine_is(powermac))
1da177e4
LT
172 return -EINVAL;
173 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
174 return -EFAULT;
175 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
176 return -EINVAL;
177 offset = pmac_get_partition(part);
178 if (offset < 0)
179 return offset;
180 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
181 return -EFAULT;
182 return 0;
183 }
184#endif /* CONFIG_PPC_PMAC */
af308377
SR
185 default:
186 return -EINVAL;
1da177e4 187 }
1da177e4
LT
188}
189
5dfe4c96 190const struct file_operations nvram_fops = {
3b03fecd
TG
191 .owner = THIS_MODULE,
192 .llseek = dev_nvram_llseek,
193 .read = dev_nvram_read,
194 .write = dev_nvram_write,
195 .unlocked_ioctl = dev_nvram_ioctl,
1da177e4
LT
196};
197
198static struct miscdevice nvram_dev = {
199 NVRAM_MINOR,
200 "nvram",
201 &nvram_fops
202};
203
204
205#ifdef DEBUG_NVRAM
32c105c3 206static void __init nvram_print_partitions(char * label)
1da177e4 207{
1da177e4
LT
208 struct nvram_partition * tmp_part;
209
210 printk(KERN_WARNING "--------%s---------\n", label);
211 printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
690d1a9b 212 list_for_each_entry(tmp_part, &nvram_partitions, partition) {
e0513d9e 213 printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%12.12s\n",
1da177e4
LT
214 tmp_part->index, tmp_part->header.signature,
215 tmp_part->header.checksum, tmp_part->header.length,
216 tmp_part->header.name);
217 }
218}
219#endif
220
221
32c105c3 222static int __init nvram_write_header(struct nvram_partition * part)
1da177e4
LT
223{
224 loff_t tmp_index;
225 int rc;
c81095a4
CLG
226 struct nvram_header phead;
227
228 memcpy(&phead, &part->header, NVRAM_HEADER_LEN);
229 phead.length = cpu_to_be16(phead.length);
230
1da177e4 231 tmp_index = part->index;
c81095a4 232 rc = ppc_md.nvram_write((char *)&phead, NVRAM_HEADER_LEN, &tmp_index);
1da177e4
LT
233
234 return rc;
235}
236
237
32c105c3 238static unsigned char __init nvram_checksum(struct nvram_header *p)
1da177e4
LT
239{
240 unsigned int c_sum, c_sum2;
241 unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
242 c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
243
244 /* The sum may have spilled into the 3rd byte. Fold it back. */
245 c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
246 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
247 c_sum2 = (c_sum >> 8) + (c_sum << 8);
248 c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
249 return c_sum;
250}
251
0f4ac132
JK
252/*
253 * Per the criteria passed via nvram_remove_partition(), should this
254 * partition be removed? 1=remove, 0=keep
255 */
256static int nvram_can_remove_partition(struct nvram_partition *part,
257 const char *name, int sig, const char *exceptions[])
258{
259 if (part->header.signature != sig)
260 return 0;
261 if (name) {
262 if (strncmp(name, part->header.name, 12))
263 return 0;
264 } else if (exceptions) {
265 const char **except;
266 for (except = exceptions; *except; except++) {
267 if (!strncmp(*except, part->header.name, 12))
268 return 0;
269 }
270 }
271 return 1;
272}
273
fa2b4e54
BH
274/**
275 * nvram_remove_partition - Remove one or more partitions in nvram
276 * @name: name of the partition to remove, or NULL for a
277 * signature only match
278 * @sig: signature of the partition(s) to remove
0f4ac132
JK
279 * @exceptions: When removing all partitions with a matching signature,
280 * leave these alone.
fa2b4e54
BH
281 */
282
0f4ac132
JK
283int __init nvram_remove_partition(const char *name, int sig,
284 const char *exceptions[])
1da177e4 285{
fa2b4e54 286 struct nvram_partition *part, *prev, *tmp;
1da177e4
LT
287 int rc;
288
690d1a9b 289 list_for_each_entry(part, &nvram_partitions, partition) {
0f4ac132 290 if (!nvram_can_remove_partition(part, name, sig, exceptions))
fa2b4e54
BH
291 continue;
292
293 /* Make partition a free partition */
1da177e4 294 part->header.signature = NVRAM_SIG_FREE;
6024ede9 295 strncpy(part->header.name, "wwwwwwwwwwww", 12);
1da177e4 296 part->header.checksum = nvram_checksum(&part->header);
1da177e4
LT
297 rc = nvram_write_header(part);
298 if (rc <= 0) {
fa2b4e54 299 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
1da177e4
LT
300 return rc;
301 }
fa2b4e54 302 }
1da177e4 303
fa2b4e54
BH
304 /* Merge contiguous ones */
305 prev = NULL;
690d1a9b 306 list_for_each_entry_safe(part, tmp, &nvram_partitions, partition) {
fa2b4e54
BH
307 if (part->header.signature != NVRAM_SIG_FREE) {
308 prev = NULL;
309 continue;
310 }
311 if (prev) {
312 prev->header.length += part->header.length;
313 prev->header.checksum = nvram_checksum(&part->header);
314 rc = nvram_write_header(part);
315 if (rc <= 0) {
316 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
317 return rc;
318 }
319 list_del(&part->partition);
320 kfree(part);
321 } else
322 prev = part;
1da177e4
LT
323 }
324
325 return 0;
326}
327
4e7c77a3
BH
328/**
329 * nvram_create_partition - Create a partition in nvram
330 * @name: name of the partition to create
331 * @sig: signature of the partition to create
36673307 332 * @req_size: size of data to allocate in bytes
4e7c77a3 333 * @min_size: minimum acceptable size (0 means req_size)
e49e2e87
BH
334 *
335 * Returns a negative error code or a positive nvram index
336 * of the beginning of the data area of the newly created
337 * partition. If you provided a min_size smaller than req_size
338 * you need to query for the actual size yourself after the
339 * call using nvram_partition_get_size().
1da177e4 340 */
edc79a2f
BH
341loff_t __init nvram_create_partition(const char *name, int sig,
342 int req_size, int min_size)
1da177e4 343{
a341ad97
AB
344 struct nvram_partition *part;
345 struct nvram_partition *new_part;
0339ad77 346 struct nvram_partition *free_part = NULL;
cef0d5ad 347 static char nv_init_vals[16];
1da177e4
LT
348 loff_t tmp_index;
349 long size = 0;
350 int rc;
4e7c77a3 351
36673307
BH
352 /* Convert sizes from bytes to blocks */
353 req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
354 min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
355
4e7c77a3
BH
356 /* If no minimum size specified, make it the same as the
357 * requested size
358 */
359 if (min_size == 0)
360 min_size = req_size;
e49e2e87
BH
361 if (min_size > req_size)
362 return -EINVAL;
4e7c77a3 363
36673307
BH
364 /* Now add one block to each for the header */
365 req_size += 1;
366 min_size += 1;
367
1da177e4
LT
368 /* Find a free partition that will give us the maximum needed size
369 If can't find one that will give us the minimum size needed */
690d1a9b 370 list_for_each_entry(part, &nvram_partitions, partition) {
1da177e4
LT
371 if (part->header.signature != NVRAM_SIG_FREE)
372 continue;
373
4e7c77a3
BH
374 if (part->header.length >= req_size) {
375 size = req_size;
1da177e4
LT
376 free_part = part;
377 break;
378 }
4e7c77a3
BH
379 if (part->header.length > size &&
380 part->header.length >= min_size) {
381 size = part->header.length;
1da177e4
LT
382 free_part = part;
383 }
384 }
0339ad77 385 if (!size)
1da177e4 386 return -ENOSPC;
1da177e4
LT
387
388 /* Create our OS partition */
0339ad77 389 new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
1da177e4 390 if (!new_part) {
e49e2e87 391 pr_err("nvram_create_os_partition: kmalloc failed\n");
1da177e4
LT
392 return -ENOMEM;
393 }
394
395 new_part->index = free_part->index;
4e7c77a3 396 new_part->header.signature = sig;
1da177e4 397 new_part->header.length = size;
4e7c77a3 398 strncpy(new_part->header.name, name, 12);
1da177e4
LT
399 new_part->header.checksum = nvram_checksum(&new_part->header);
400
401 rc = nvram_write_header(new_part);
402 if (rc <= 0) {
e49e2e87
BH
403 pr_err("nvram_create_os_partition: nvram_write_header "
404 "failed (%d)\n", rc);
1da177e4
LT
405 return rc;
406 }
e49e2e87
BH
407 list_add_tail(&new_part->partition, &free_part->partition);
408
409 /* Adjust or remove the partition we stole the space from */
410 if (free_part->header.length > size) {
411 free_part->index += size * NVRAM_BLOCK_LEN;
412 free_part->header.length -= size;
413 free_part->header.checksum = nvram_checksum(&free_part->header);
414 rc = nvram_write_header(free_part);
415 if (rc <= 0) {
416 pr_err("nvram_create_os_partition: nvram_write_header "
417 "failed (%d)\n", rc);
418 return rc;
419 }
420 } else {
421 list_del(&free_part->partition);
422 kfree(free_part);
423 }
1da177e4 424
e49e2e87 425 /* Clear the new partition */
cef0d5ad
BH
426 for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
427 tmp_index < ((size - 1) * NVRAM_BLOCK_LEN);
428 tmp_index += NVRAM_BLOCK_LEN) {
429 rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
430 if (rc <= 0) {
431 pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc);
432 return rc;
433 }
1da177e4
LT
434 }
435
e49e2e87
BH
436 return new_part->index + NVRAM_HEADER_LEN;
437}
1da177e4 438
e49e2e87
BH
439/**
440 * nvram_get_partition_size - Get the data size of an nvram partition
441 * @data_index: This is the offset of the start of the data of
442 * the partition. The same value that is returned by
443 * nvram_create_partition().
444 */
edc79a2f 445int nvram_get_partition_size(loff_t data_index)
e49e2e87
BH
446{
447 struct nvram_partition *part;
1da177e4 448
690d1a9b 449 list_for_each_entry(part, &nvram_partitions, partition) {
e49e2e87
BH
450 if (part->index + NVRAM_HEADER_LEN == data_index)
451 return (part->header.length - 1) * NVRAM_BLOCK_LEN;
1da177e4 452 }
e49e2e87 453 return -1;
1da177e4
LT
454}
455
456
cf5cbf9f
BH
457/**
458 * nvram_find_partition - Find an nvram partition by signature and name
459 * @name: Name of the partition or NULL for any name
460 * @sig: Signature to test against
461 * @out_size: if non-NULL, returns the size of the data part of the partition
462 */
463loff_t nvram_find_partition(const char *name, int sig, int *out_size)
464{
465 struct nvram_partition *p;
466
690d1a9b 467 list_for_each_entry(p, &nvram_partitions, partition) {
cf5cbf9f
BH
468 if (p->header.signature == sig &&
469 (!name || !strncmp(p->header.name, name, 12))) {
470 if (out_size)
471 *out_size = (p->header.length - 1) *
472 NVRAM_BLOCK_LEN;
473 return p->index + NVRAM_HEADER_LEN;
474 }
475 }
476 return 0;
477}
478
edc79a2f 479int __init nvram_scan_partitions(void)
1da177e4
LT
480{
481 loff_t cur_index = 0;
482 struct nvram_header phead;
483 struct nvram_partition * tmp_part;
484 unsigned char c_sum;
485 char * header;
486 int total_size;
487 int err;
488
edc79a2f 489 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
1da177e4
LT
490 return -ENODEV;
491 total_size = ppc_md.nvram_size();
492
5cbded58 493 header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
1da177e4
LT
494 if (!header) {
495 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
496 return -ENOMEM;
497 }
498
499 while (cur_index < total_size) {
500
501 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
502 if (err != NVRAM_HEADER_LEN) {
503 printk(KERN_ERR "nvram_scan_partitions: Error parsing "
504 "nvram partitions\n");
505 goto out;
506 }
507
508 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
509
510 memcpy(&phead, header, NVRAM_HEADER_LEN);
511
c81095a4
CLG
512 phead.length = be16_to_cpu(phead.length);
513
1da177e4
LT
514 err = 0;
515 c_sum = nvram_checksum(&phead);
516 if (c_sum != phead.checksum) {
517 printk(KERN_WARNING "WARNING: nvram partition checksum"
518 " was %02x, should be %02x!\n",
519 phead.checksum, c_sum);
520 printk(KERN_WARNING "Terminating nvram partition scan\n");
521 goto out;
522 }
523 if (!phead.length) {
524 printk(KERN_WARNING "WARNING: nvram corruption "
525 "detected: 0-length partition\n");
526 goto out;
527 }
6e51c9ff 528 tmp_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
1da177e4
LT
529 err = -ENOMEM;
530 if (!tmp_part) {
531 printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
532 goto out;
533 }
534
535 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
536 tmp_part->index = cur_index;
690d1a9b 537 list_add_tail(&tmp_part->partition, &nvram_partitions);
1da177e4
LT
538
539 cur_index += phead.length * NVRAM_BLOCK_LEN;
540 }
541 err = 0;
542
edc79a2f
BH
543#ifdef DEBUG_NVRAM
544 nvram_print_partitions("NVRAM Partitions");
545#endif
546
1da177e4
LT
547 out:
548 kfree(header);
549 return err;
550}
551
552static int __init nvram_init(void)
553{
1da177e4
LT
554 int rc;
555
578914cf
BH
556 BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
557
1da177e4
LT
558 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
559 return -ENODEV;
560
561 rc = misc_register(&nvram_dev);
562 if (rc != 0) {
563 printk(KERN_ERR "nvram_init: failed to register device\n");
564 return rc;
565 }
566
1da177e4
LT
567 return rc;
568}
569
e51df2c1 570static void __exit nvram_cleanup(void)
1da177e4
LT
571{
572 misc_deregister( &nvram_dev );
573}
574
1da177e4
LT
575module_init(nvram_init);
576module_exit(nvram_cleanup);
577MODULE_LICENSE("GPL");