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;
48 struct nvram_partition {
49 struct list_head partition;
50 struct nvram_header header;
54 static struct nvram_partition * nvram_part;
56 static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
60 if (ppc_md.nvram_size == NULL)
62 size = ppc_md.nvram_size();
66 offset += file->f_pos;
79 static ssize_t dev_nvram_read(struct file *file, char __user *buf,
80 size_t count, loff_t *ppos)
87 if (!ppc_md.nvram_size)
91 size = ppc_md.nvram_size();
92 if (*ppos >= size || size < 0)
95 count = min_t(size_t, count, size - *ppos);
96 count = min(count, PAGE_SIZE);
99 tmp = kmalloc(count, GFP_KERNEL);
103 ret = ppc_md.nvram_read(tmp, count, ppos);
107 if (copy_to_user(buf, tmp, ret))
116 static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
117 size_t count, loff_t *ppos)
124 if (!ppc_md.nvram_size)
128 size = ppc_md.nvram_size();
129 if (*ppos >= size || size < 0)
132 count = min_t(size_t, count, size - *ppos);
133 count = min(count, PAGE_SIZE);
136 tmp = kmalloc(count, GFP_KERNEL);
141 if (copy_from_user(tmp, buf, count))
144 ret = ppc_md.nvram_write(tmp, count, ppos);
152 static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
156 #ifdef CONFIG_PPC_PMAC
157 case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
158 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
159 case IOC_NVRAM_GET_OFFSET: {
162 if (!machine_is(powermac))
164 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
166 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
168 offset = pmac_get_partition(part);
171 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
175 #endif /* CONFIG_PPC_PMAC */
181 const struct file_operations nvram_fops = {
182 .owner = THIS_MODULE,
183 .llseek = dev_nvram_llseek,
184 .read = dev_nvram_read,
185 .write = dev_nvram_write,
186 .unlocked_ioctl = dev_nvram_ioctl,
189 static struct miscdevice nvram_dev = {
197 static void __init nvram_print_partitions(char * label)
199 struct list_head * p;
200 struct nvram_partition * tmp_part;
202 printk(KERN_WARNING "--------%s---------\n", label);
203 printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
204 list_for_each(p, &nvram_part->partition) {
205 tmp_part = list_entry(p, struct nvram_partition, partition);
206 printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%s\n",
207 tmp_part->index, tmp_part->header.signature,
208 tmp_part->header.checksum, tmp_part->header.length,
209 tmp_part->header.name);
215 static int __init nvram_write_header(struct nvram_partition * part)
220 tmp_index = part->index;
221 rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
227 static unsigned char __init nvram_checksum(struct nvram_header *p)
229 unsigned int c_sum, c_sum2;
230 unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
231 c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
233 /* The sum may have spilled into the 3rd byte. Fold it back. */
234 c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
235 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
236 c_sum2 = (c_sum >> 8) + (c_sum << 8);
237 c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
242 * nvram_remove_partition - Remove one or more partitions in nvram
243 * @name: name of the partition to remove, or NULL for a
244 * signature only match
245 * @sig: signature of the partition(s) to remove
248 int __init nvram_remove_partition(const char *name, int sig)
250 struct nvram_partition *part, *prev, *tmp;
253 list_for_each_entry(part, &nvram_part->partition, partition) {
254 if (part->header.signature != sig)
256 if (name && strncmp(name, part->header.name, 12))
259 /* Make partition a free partition */
260 part->header.signature = NVRAM_SIG_FREE;
261 sprintf(part->header.name, "wwwwwwwwwwww");
262 part->header.checksum = nvram_checksum(&part->header);
263 rc = nvram_write_header(part);
265 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
270 /* Merge contiguous ones */
272 list_for_each_entry_safe(part, tmp, &nvram_part->partition, partition) {
273 if (part->header.signature != NVRAM_SIG_FREE) {
278 prev->header.length += part->header.length;
279 prev->header.checksum = nvram_checksum(&part->header);
280 rc = nvram_write_header(part);
282 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
285 list_del(&part->partition);
295 * nvram_create_partition - Create a partition in nvram
296 * @name: name of the partition to create
297 * @sig: signature of the partition to create
298 * @req_size: size of data to allocate in bytes
299 * @min_size: minimum acceptable size (0 means req_size)
301 * Returns a negative error code or a positive nvram index
302 * of the beginning of the data area of the newly created
303 * partition. If you provided a min_size smaller than req_size
304 * you need to query for the actual size yourself after the
305 * call using nvram_partition_get_size().
307 loff_t __init nvram_create_partition(const char *name, int sig,
308 int req_size, int min_size)
310 struct nvram_partition *part;
311 struct nvram_partition *new_part;
312 struct nvram_partition *free_part = NULL;
313 static char nv_init_vals[16];
318 /* Convert sizes from bytes to blocks */
319 req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
320 min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
322 /* If no minimum size specified, make it the same as the
327 if (min_size > req_size)
330 /* Now add one block to each for the header */
334 /* Find a free partition that will give us the maximum needed size
335 If can't find one that will give us the minimum size needed */
336 list_for_each_entry(part, &nvram_part->partition, partition) {
337 if (part->header.signature != NVRAM_SIG_FREE)
340 if (part->header.length >= req_size) {
345 if (part->header.length > size &&
346 part->header.length >= min_size) {
347 size = part->header.length;
354 /* Create our OS partition */
355 new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
357 pr_err("nvram_create_os_partition: kmalloc failed\n");
361 new_part->index = free_part->index;
362 new_part->header.signature = sig;
363 new_part->header.length = size;
364 strncpy(new_part->header.name, name, 12);
365 new_part->header.checksum = nvram_checksum(&new_part->header);
367 rc = nvram_write_header(new_part);
369 pr_err("nvram_create_os_partition: nvram_write_header "
370 "failed (%d)\n", rc);
373 list_add_tail(&new_part->partition, &free_part->partition);
375 /* Adjust or remove the partition we stole the space from */
376 if (free_part->header.length > size) {
377 free_part->index += size * NVRAM_BLOCK_LEN;
378 free_part->header.length -= size;
379 free_part->header.checksum = nvram_checksum(&free_part->header);
380 rc = nvram_write_header(free_part);
382 pr_err("nvram_create_os_partition: nvram_write_header "
383 "failed (%d)\n", rc);
387 list_del(&free_part->partition);
391 /* Clear the new partition */
392 for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
393 tmp_index < ((size - 1) * NVRAM_BLOCK_LEN);
394 tmp_index += NVRAM_BLOCK_LEN) {
395 rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
397 pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc);
402 return new_part->index + NVRAM_HEADER_LEN;
406 * nvram_get_partition_size - Get the data size of an nvram partition
407 * @data_index: This is the offset of the start of the data of
408 * the partition. The same value that is returned by
409 * nvram_create_partition().
411 int nvram_get_partition_size(loff_t data_index)
413 struct nvram_partition *part;
415 list_for_each_entry(part, &nvram_part->partition, partition) {
416 if (part->index + NVRAM_HEADER_LEN == data_index)
417 return (part->header.length - 1) * NVRAM_BLOCK_LEN;
424 * nvram_find_partition - Find an nvram partition by signature and name
425 * @name: Name of the partition or NULL for any name
426 * @sig: Signature to test against
427 * @out_size: if non-NULL, returns the size of the data part of the partition
429 loff_t nvram_find_partition(const char *name, int sig, int *out_size)
431 struct nvram_partition *p;
433 list_for_each_entry(p, &nvram_part->partition, partition) {
434 if (p->header.signature == sig &&
435 (!name || !strncmp(p->header.name, name, 12))) {
437 *out_size = (p->header.length - 1) *
439 return p->index + NVRAM_HEADER_LEN;
445 int __init nvram_scan_partitions(void)
447 loff_t cur_index = 0;
448 struct nvram_header phead;
449 struct nvram_partition * tmp_part;
455 /* Initialize our anchor for the nvram partition list */
456 nvram_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
458 printk(KERN_ERR "nvram_init: Failed kmalloc\n");
461 INIT_LIST_HEAD(&nvram_part->partition);
463 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
465 total_size = ppc_md.nvram_size();
467 header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
469 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
473 while (cur_index < total_size) {
475 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
476 if (err != NVRAM_HEADER_LEN) {
477 printk(KERN_ERR "nvram_scan_partitions: Error parsing "
478 "nvram partitions\n");
482 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
484 memcpy(&phead, header, NVRAM_HEADER_LEN);
487 c_sum = nvram_checksum(&phead);
488 if (c_sum != phead.checksum) {
489 printk(KERN_WARNING "WARNING: nvram partition checksum"
490 " was %02x, should be %02x!\n",
491 phead.checksum, c_sum);
492 printk(KERN_WARNING "Terminating nvram partition scan\n");
496 printk(KERN_WARNING "WARNING: nvram corruption "
497 "detected: 0-length partition\n");
500 tmp_part = (struct nvram_partition *)
501 kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
504 printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
508 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
509 tmp_part->index = cur_index;
510 list_add_tail(&tmp_part->partition, &nvram_part->partition);
512 cur_index += phead.length * NVRAM_BLOCK_LEN;
517 nvram_print_partitions("NVRAM Partitions");
525 static int __init nvram_init(void)
529 BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
531 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
534 rc = misc_register(&nvram_dev);
536 printk(KERN_ERR "nvram_init: failed to register device\n");
543 void __exit nvram_cleanup(void)
545 misc_deregister( &nvram_dev );
548 module_init(nvram_init);
549 module_exit(nvram_cleanup);
550 MODULE_LICENSE("GPL");