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%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
)
227 tmp_index
= part
->index
;
228 rc
= ppc_md
.nvram_write((char *)&part
->header
, NVRAM_HEADER_LEN
, &tmp_index
);
234 static unsigned char __init
nvram_checksum(struct nvram_header
*p
)
236 unsigned int c_sum
, c_sum2
;
237 unsigned short *sp
= (unsigned short *)p
->name
; /* assume 6 shorts */
238 c_sum
= p
->signature
+ p
->length
+ sp
[0] + sp
[1] + sp
[2] + sp
[3] + sp
[4] + sp
[5];
240 /* The sum may have spilled into the 3rd byte. Fold it back. */
241 c_sum
= ((c_sum
& 0xffff) + (c_sum
>> 16)) & 0xffff;
242 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
243 c_sum2
= (c_sum
>> 8) + (c_sum
<< 8);
244 c_sum
= ((c_sum
+ c_sum2
) >> 8) & 0xff;
249 * Per the criteria passed via nvram_remove_partition(), should this
250 * partition be removed? 1=remove, 0=keep
252 static int nvram_can_remove_partition(struct nvram_partition
*part
,
253 const char *name
, int sig
, const char *exceptions
[])
255 if (part
->header
.signature
!= sig
)
258 if (strncmp(name
, part
->header
.name
, 12))
260 } else if (exceptions
) {
262 for (except
= exceptions
; *except
; except
++) {
263 if (!strncmp(*except
, part
->header
.name
, 12))
271 * nvram_remove_partition - Remove one or more partitions in nvram
272 * @name: name of the partition to remove, or NULL for a
273 * signature only match
274 * @sig: signature of the partition(s) to remove
275 * @exceptions: When removing all partitions with a matching signature,
279 int __init
nvram_remove_partition(const char *name
, int sig
,
280 const char *exceptions
[])
282 struct nvram_partition
*part
, *prev
, *tmp
;
285 list_for_each_entry(part
, &nvram_partitions
, partition
) {
286 if (!nvram_can_remove_partition(part
, name
, sig
, exceptions
))
289 /* Make partition a free partition */
290 part
->header
.signature
= NVRAM_SIG_FREE
;
291 strncpy(part
->header
.name
, "wwwwwwwwwwww", 12);
292 part
->header
.checksum
= nvram_checksum(&part
->header
);
293 rc
= nvram_write_header(part
);
295 printk(KERN_ERR
"nvram_remove_partition: nvram_write failed (%d)\n", rc
);
300 /* Merge contiguous ones */
302 list_for_each_entry_safe(part
, tmp
, &nvram_partitions
, partition
) {
303 if (part
->header
.signature
!= NVRAM_SIG_FREE
) {
308 prev
->header
.length
+= part
->header
.length
;
309 prev
->header
.checksum
= nvram_checksum(&part
->header
);
310 rc
= nvram_write_header(part
);
312 printk(KERN_ERR
"nvram_remove_partition: nvram_write failed (%d)\n", rc
);
315 list_del(&part
->partition
);
325 * nvram_create_partition - Create a partition in nvram
326 * @name: name of the partition to create
327 * @sig: signature of the partition to create
328 * @req_size: size of data to allocate in bytes
329 * @min_size: minimum acceptable size (0 means req_size)
331 * Returns a negative error code or a positive nvram index
332 * of the beginning of the data area of the newly created
333 * partition. If you provided a min_size smaller than req_size
334 * you need to query for the actual size yourself after the
335 * call using nvram_partition_get_size().
337 loff_t __init
nvram_create_partition(const char *name
, int sig
,
338 int req_size
, int min_size
)
340 struct nvram_partition
*part
;
341 struct nvram_partition
*new_part
;
342 struct nvram_partition
*free_part
= NULL
;
343 static char nv_init_vals
[16];
348 /* Convert sizes from bytes to blocks */
349 req_size
= _ALIGN_UP(req_size
, NVRAM_BLOCK_LEN
) / NVRAM_BLOCK_LEN
;
350 min_size
= _ALIGN_UP(min_size
, NVRAM_BLOCK_LEN
) / NVRAM_BLOCK_LEN
;
352 /* If no minimum size specified, make it the same as the
357 if (min_size
> req_size
)
360 /* Now add one block to each for the header */
364 /* Find a free partition that will give us the maximum needed size
365 If can't find one that will give us the minimum size needed */
366 list_for_each_entry(part
, &nvram_partitions
, partition
) {
367 if (part
->header
.signature
!= NVRAM_SIG_FREE
)
370 if (part
->header
.length
>= req_size
) {
375 if (part
->header
.length
> size
&&
376 part
->header
.length
>= min_size
) {
377 size
= part
->header
.length
;
384 /* Create our OS partition */
385 new_part
= kmalloc(sizeof(*new_part
), GFP_KERNEL
);
387 pr_err("nvram_create_os_partition: kmalloc failed\n");
391 new_part
->index
= free_part
->index
;
392 new_part
->header
.signature
= sig
;
393 new_part
->header
.length
= size
;
394 strncpy(new_part
->header
.name
, name
, 12);
395 new_part
->header
.checksum
= nvram_checksum(&new_part
->header
);
397 rc
= nvram_write_header(new_part
);
399 pr_err("nvram_create_os_partition: nvram_write_header "
400 "failed (%d)\n", rc
);
403 list_add_tail(&new_part
->partition
, &free_part
->partition
);
405 /* Adjust or remove the partition we stole the space from */
406 if (free_part
->header
.length
> size
) {
407 free_part
->index
+= size
* NVRAM_BLOCK_LEN
;
408 free_part
->header
.length
-= size
;
409 free_part
->header
.checksum
= nvram_checksum(&free_part
->header
);
410 rc
= nvram_write_header(free_part
);
412 pr_err("nvram_create_os_partition: nvram_write_header "
413 "failed (%d)\n", rc
);
417 list_del(&free_part
->partition
);
421 /* Clear the new partition */
422 for (tmp_index
= new_part
->index
+ NVRAM_HEADER_LEN
;
423 tmp_index
< ((size
- 1) * NVRAM_BLOCK_LEN
);
424 tmp_index
+= NVRAM_BLOCK_LEN
) {
425 rc
= ppc_md
.nvram_write(nv_init_vals
, NVRAM_BLOCK_LEN
, &tmp_index
);
427 pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc
);
432 return new_part
->index
+ NVRAM_HEADER_LEN
;
436 * nvram_get_partition_size - Get the data size of an nvram partition
437 * @data_index: This is the offset of the start of the data of
438 * the partition. The same value that is returned by
439 * nvram_create_partition().
441 int nvram_get_partition_size(loff_t data_index
)
443 struct nvram_partition
*part
;
445 list_for_each_entry(part
, &nvram_partitions
, partition
) {
446 if (part
->index
+ NVRAM_HEADER_LEN
== data_index
)
447 return (part
->header
.length
- 1) * NVRAM_BLOCK_LEN
;
454 * nvram_find_partition - Find an nvram partition by signature and name
455 * @name: Name of the partition or NULL for any name
456 * @sig: Signature to test against
457 * @out_size: if non-NULL, returns the size of the data part of the partition
459 loff_t
nvram_find_partition(const char *name
, int sig
, int *out_size
)
461 struct nvram_partition
*p
;
463 list_for_each_entry(p
, &nvram_partitions
, partition
) {
464 if (p
->header
.signature
== sig
&&
465 (!name
|| !strncmp(p
->header
.name
, name
, 12))) {
467 *out_size
= (p
->header
.length
- 1) *
469 return p
->index
+ NVRAM_HEADER_LEN
;
475 int __init
nvram_scan_partitions(void)
477 loff_t cur_index
= 0;
478 struct nvram_header phead
;
479 struct nvram_partition
* tmp_part
;
485 if (ppc_md
.nvram_size
== NULL
|| ppc_md
.nvram_size() <= 0)
487 total_size
= ppc_md
.nvram_size();
489 header
= kmalloc(NVRAM_HEADER_LEN
, GFP_KERNEL
);
491 printk(KERN_ERR
"nvram_scan_partitions: Failed kmalloc\n");
495 while (cur_index
< total_size
) {
497 err
= ppc_md
.nvram_read(header
, NVRAM_HEADER_LEN
, &cur_index
);
498 if (err
!= NVRAM_HEADER_LEN
) {
499 printk(KERN_ERR
"nvram_scan_partitions: Error parsing "
500 "nvram partitions\n");
504 cur_index
-= NVRAM_HEADER_LEN
; /* nvram_read will advance us */
506 memcpy(&phead
, header
, NVRAM_HEADER_LEN
);
509 c_sum
= nvram_checksum(&phead
);
510 if (c_sum
!= phead
.checksum
) {
511 printk(KERN_WARNING
"WARNING: nvram partition checksum"
512 " was %02x, should be %02x!\n",
513 phead
.checksum
, c_sum
);
514 printk(KERN_WARNING
"Terminating nvram partition scan\n");
518 printk(KERN_WARNING
"WARNING: nvram corruption "
519 "detected: 0-length partition\n");
522 tmp_part
= kmalloc(sizeof(struct nvram_partition
), GFP_KERNEL
);
525 printk(KERN_ERR
"nvram_scan_partitions: kmalloc failed\n");
529 memcpy(&tmp_part
->header
, &phead
, NVRAM_HEADER_LEN
);
530 tmp_part
->index
= cur_index
;
531 list_add_tail(&tmp_part
->partition
, &nvram_partitions
);
533 cur_index
+= phead
.length
* NVRAM_BLOCK_LEN
;
538 nvram_print_partitions("NVRAM Partitions");
546 static int __init
nvram_init(void)
550 BUILD_BUG_ON(NVRAM_BLOCK_LEN
!= 16);
552 if (ppc_md
.nvram_size
== NULL
|| ppc_md
.nvram_size() <= 0)
555 rc
= misc_register(&nvram_dev
);
557 printk(KERN_ERR
"nvram_init: failed to register device\n");
564 void __exit
nvram_cleanup(void)
566 misc_deregister( &nvram_dev
);
569 module_init(nvram_init
);
570 module_exit(nvram_cleanup
);
571 MODULE_LICENSE("GPL");