2 * c 2001 PPC 64 Team, IBM Corp
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.
9 * /dev/nvram driver for PPC64
11 * This perhaps should live in drivers/char
13 * TODO: Split the /dev/nvram part (that one can use
14 * drivers/char/generic_nvram.c) from the arch & partition
18 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/errno.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>
33 #include <asm/machdep.h>
37 #define NVRAM_HEADER_LEN sizeof(struct nvram_header)
38 #define NVRAM_BLOCK_LEN NVRAM_HEADER_LEN
40 /* If change this size, then change the size of NVNAME_LEN */
42 unsigned char signature
;
43 unsigned char checksum
;
44 unsigned short length
;
45 /* Terminating null required only for names < 12 chars. */
49 struct nvram_partition
{
50 struct list_head partition
;
51 struct nvram_header header
;
55 static LIST_HEAD(nvram_partitions
);
57 static loff_t
dev_nvram_llseek(struct file
*file
, loff_t offset
, int origin
)
61 if (ppc_md
.nvram_size
== NULL
)
63 size
= ppc_md
.nvram_size();
67 offset
+= file
->f_pos
;
80 static ssize_t
dev_nvram_read(struct file
*file
, char __user
*buf
,
81 size_t count
, loff_t
*ppos
)
87 if (!ppc_md
.nvram_size
) {
92 size
= ppc_md
.nvram_size();
103 count
= min_t(size_t, count
, size
- *ppos
);
104 count
= min(count
, PAGE_SIZE
);
106 tmp
= kmalloc(count
, GFP_KERNEL
);
112 ret
= ppc_md
.nvram_read(tmp
, count
, ppos
);
116 if (copy_to_user(buf
, tmp
, ret
))
125 static ssize_t
dev_nvram_write(struct file
*file
, const char __user
*buf
,
126 size_t count
, loff_t
*ppos
)
133 if (!ppc_md
.nvram_size
)
137 size
= ppc_md
.nvram_size();
138 if (*ppos
>= size
|| size
< 0)
141 count
= min_t(size_t, count
, size
- *ppos
);
142 count
= min(count
, PAGE_SIZE
);
145 tmp
= kmalloc(count
, GFP_KERNEL
);
150 if (copy_from_user(tmp
, buf
, count
))
153 ret
= ppc_md
.nvram_write(tmp
, count
, ppos
);
161 static long dev_nvram_ioctl(struct file
*file
, unsigned int 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
: {
171 if (!machine_is(powermac
))
173 if (copy_from_user(&part
, (void __user
*)arg
, sizeof(part
)) != 0)
175 if (part
< pmac_nvram_OF
|| part
> pmac_nvram_NR
)
177 offset
= pmac_get_partition(part
);
180 if (copy_to_user((void __user
*)arg
, &offset
, sizeof(offset
)) != 0)
184 #endif /* CONFIG_PPC_PMAC */
190 const struct file_operations nvram_fops
= {
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
,
198 static struct miscdevice nvram_dev
= {
206 static void __init
nvram_print_partitions(char * label
)
208 struct nvram_partition
* tmp_part
;
210 printk(KERN_WARNING
"--------%s---------\n", label
);
211 printk(KERN_WARNING
"indx\t\tsig\tchks\tlen\tname\n");
212 list_for_each_entry(tmp_part
, &nvram_partitions
, partition
) {
213 printk(KERN_WARNING
"%4d \t%02x\t%02x\t%d\t%12.12s\n",
214 tmp_part
->index
, tmp_part
->header
.signature
,
215 tmp_part
->header
.checksum
, tmp_part
->header
.length
,
216 tmp_part
->header
.name
);
222 static int __init
nvram_write_header(struct nvram_partition
* part
)
226 struct nvram_header phead
;
228 memcpy(&phead
, &part
->header
, NVRAM_HEADER_LEN
);
229 phead
.length
= cpu_to_be16(phead
.length
);
231 tmp_index
= part
->index
;
232 rc
= ppc_md
.nvram_write((char *)&phead
, NVRAM_HEADER_LEN
, &tmp_index
);
238 static unsigned char __init
nvram_checksum(struct nvram_header
*p
)
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];
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;
253 * Per the criteria passed via nvram_remove_partition(), should this
254 * partition be removed? 1=remove, 0=keep
256 static int nvram_can_remove_partition(struct nvram_partition
*part
,
257 const char *name
, int sig
, const char *exceptions
[])
259 if (part
->header
.signature
!= sig
)
262 if (strncmp(name
, part
->header
.name
, 12))
264 } else if (exceptions
) {
266 for (except
= exceptions
; *except
; except
++) {
267 if (!strncmp(*except
, part
->header
.name
, 12))
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
279 * @exceptions: When removing all partitions with a matching signature,
283 int __init
nvram_remove_partition(const char *name
, int sig
,
284 const char *exceptions
[])
286 struct nvram_partition
*part
, *prev
, *tmp
;
289 list_for_each_entry(part
, &nvram_partitions
, partition
) {
290 if (!nvram_can_remove_partition(part
, name
, sig
, exceptions
))
293 /* Make partition a free partition */
294 part
->header
.signature
= NVRAM_SIG_FREE
;
295 strncpy(part
->header
.name
, "wwwwwwwwwwww", 12);
296 part
->header
.checksum
= nvram_checksum(&part
->header
);
297 rc
= nvram_write_header(part
);
299 printk(KERN_ERR
"nvram_remove_partition: nvram_write failed (%d)\n", rc
);
304 /* Merge contiguous ones */
306 list_for_each_entry_safe(part
, tmp
, &nvram_partitions
, partition
) {
307 if (part
->header
.signature
!= NVRAM_SIG_FREE
) {
312 prev
->header
.length
+= part
->header
.length
;
313 prev
->header
.checksum
= nvram_checksum(&part
->header
);
314 rc
= nvram_write_header(part
);
316 printk(KERN_ERR
"nvram_remove_partition: nvram_write failed (%d)\n", rc
);
319 list_del(&part
->partition
);
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
332 * @req_size: size of data to allocate in bytes
333 * @min_size: minimum acceptable size (0 means req_size)
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().
341 loff_t __init
nvram_create_partition(const char *name
, int sig
,
342 int req_size
, int min_size
)
344 struct nvram_partition
*part
;
345 struct nvram_partition
*new_part
;
346 struct nvram_partition
*free_part
= NULL
;
347 static char nv_init_vals
[16];
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
;
356 /* If no minimum size specified, make it the same as the
361 if (min_size
> req_size
)
364 /* Now add one block to each for the header */
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 */
370 list_for_each_entry(part
, &nvram_partitions
, partition
) {
371 if (part
->header
.signature
!= NVRAM_SIG_FREE
)
374 if (part
->header
.length
>= req_size
) {
379 if (part
->header
.length
> size
&&
380 part
->header
.length
>= min_size
) {
381 size
= part
->header
.length
;
388 /* Create our OS partition */
389 new_part
= kmalloc(sizeof(*new_part
), GFP_KERNEL
);
391 pr_err("nvram_create_os_partition: kmalloc failed\n");
395 new_part
->index
= free_part
->index
;
396 new_part
->header
.signature
= sig
;
397 new_part
->header
.length
= size
;
398 strncpy(new_part
->header
.name
, name
, 12);
399 new_part
->header
.checksum
= nvram_checksum(&new_part
->header
);
401 rc
= nvram_write_header(new_part
);
403 pr_err("nvram_create_os_partition: nvram_write_header "
404 "failed (%d)\n", rc
);
407 list_add_tail(&new_part
->partition
, &free_part
->partition
);
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
);
416 pr_err("nvram_create_os_partition: nvram_write_header "
417 "failed (%d)\n", rc
);
421 list_del(&free_part
->partition
);
425 /* Clear the new partition */
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
);
431 pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc
);
436 return new_part
->index
+ NVRAM_HEADER_LEN
;
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().
445 int nvram_get_partition_size(loff_t data_index
)
447 struct nvram_partition
*part
;
449 list_for_each_entry(part
, &nvram_partitions
, partition
) {
450 if (part
->index
+ NVRAM_HEADER_LEN
== data_index
)
451 return (part
->header
.length
- 1) * NVRAM_BLOCK_LEN
;
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
463 loff_t
nvram_find_partition(const char *name
, int sig
, int *out_size
)
465 struct nvram_partition
*p
;
467 list_for_each_entry(p
, &nvram_partitions
, partition
) {
468 if (p
->header
.signature
== sig
&&
469 (!name
|| !strncmp(p
->header
.name
, name
, 12))) {
471 *out_size
= (p
->header
.length
- 1) *
473 return p
->index
+ NVRAM_HEADER_LEN
;
479 int __init
nvram_scan_partitions(void)
481 loff_t cur_index
= 0;
482 struct nvram_header phead
;
483 struct nvram_partition
* tmp_part
;
489 if (ppc_md
.nvram_size
== NULL
|| ppc_md
.nvram_size() <= 0)
491 total_size
= ppc_md
.nvram_size();
493 header
= kmalloc(NVRAM_HEADER_LEN
, GFP_KERNEL
);
495 printk(KERN_ERR
"nvram_scan_partitions: Failed kmalloc\n");
499 while (cur_index
< total_size
) {
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");
508 cur_index
-= NVRAM_HEADER_LEN
; /* nvram_read will advance us */
510 memcpy(&phead
, header
, NVRAM_HEADER_LEN
);
512 phead
.length
= be16_to_cpu(phead
.length
);
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");
524 printk(KERN_WARNING
"WARNING: nvram corruption "
525 "detected: 0-length partition\n");
528 tmp_part
= kmalloc(sizeof(struct nvram_partition
), GFP_KERNEL
);
531 printk(KERN_ERR
"nvram_scan_partitions: kmalloc failed\n");
535 memcpy(&tmp_part
->header
, &phead
, NVRAM_HEADER_LEN
);
536 tmp_part
->index
= cur_index
;
537 list_add_tail(&tmp_part
->partition
, &nvram_partitions
);
539 cur_index
+= phead
.length
* NVRAM_BLOCK_LEN
;
544 nvram_print_partitions("NVRAM Partitions");
552 static int __init
nvram_init(void)
556 BUILD_BUG_ON(NVRAM_BLOCK_LEN
!= 16);
558 if (ppc_md
.nvram_size
== NULL
|| ppc_md
.nvram_size() <= 0)
561 rc
= misc_register(&nvram_dev
);
563 printk(KERN_ERR
"nvram_init: failed to register device\n");
570 void __exit
nvram_cleanup(void)
572 misc_deregister( &nvram_dev
);
575 module_init(nvram_init
);
576 module_exit(nvram_cleanup
);
577 MODULE_LICENSE("GPL");