📄 nvram.c
字号:
/* * c 2001 PPC 64 Team, IBM Corp * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * * /dev/nvram driver for PPC64 * * This perhaps should live in drivers/char */#include <linux/module.h>#include <linux/types.h>#include <linux/errno.h>#include <linux/fs.h>#include <linux/miscdevice.h>#include <linux/fcntl.h>#include <linux/nvram.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/spinlock.h>#include <asm/uaccess.h>#include <asm/nvram.h>#include <asm/rtas.h>#include <asm/prom.h>#include <linux/string.h>/*#define DEBUG_NVRAM*/static int scan_nvram_partitions(void);static int setup_nvram_partition(void);static int create_os_nvram_partition(void);static int remove_os_nvram_partition(void);static unsigned char nvram_checksum(struct nvram_header *p);static int write_nvram_header(struct nvram_partition * part);static ssize_t __read_nvram(char *buf, size_t count, loff_t *index);static ssize_t __write_nvram(char *buf, size_t count, loff_t *index);static unsigned int rtas_nvram_size = 0;static unsigned int nvram_fetch, nvram_store;static char nvram_buf[NVRW_CNT]; /* assume this is in the first 4GB */static struct nvram_partition * nvram_part;static long error_log_nvram_index = -1;static long error_log_nvram_size = 0;static spinlock_t nvram_lock = SPIN_LOCK_UNLOCKED;volatile int no_more_logging = 1;extern volatile int error_log_cnt;struct err_log_info { int error_type; unsigned int seq_num;};static loff_t dev_ppc64_nvram_llseek(struct file *file, loff_t offset, int origin){ switch (origin) { case 1: offset += file->f_pos; break; case 2: offset += rtas_nvram_size; break; } if (offset < 0) return -EINVAL; file->f_pos = offset; return file->f_pos;}static ssize_t dev_ppc64_read_nvram(struct file *file, char *buf, size_t count, loff_t *ppos){ unsigned long len; char *tmp_buffer; loff_t pos = *ppos; if (verify_area(VERIFY_WRITE, buf, count)) return -EFAULT; if ((unsigned)pos != pos || pos >= rtas_nvram_size) return 0; if (count > rtas_nvram_size) count = rtas_nvram_size; tmp_buffer = kmalloc(count, GFP_KERNEL); if (!tmp_buffer) { printk(KERN_ERR "dev_ppc64_read_nvram: kmalloc failed\n"); return 0; } len = read_nvram(tmp_buffer, count, &pos); if ((long)len <= 0) { kfree(tmp_buffer); return len; } if (copy_to_user(buf, tmp_buffer, len)) { kfree(tmp_buffer); return -EFAULT; } kfree(tmp_buffer); *ppos = pos; return len;}static ssize_t dev_ppc64_write_nvram(struct file *file, const char *buf, size_t count, loff_t *ppos){ unsigned long len; char * tmp_buffer; loff_t pos = *ppos; if (verify_area(VERIFY_READ, buf, count)) return -EFAULT; if (pos != (unsigned) pos || pos >= rtas_nvram_size) return 0; if (count > rtas_nvram_size) count = rtas_nvram_size; tmp_buffer = kmalloc(count, GFP_KERNEL); if (!tmp_buffer) { printk(KERN_ERR "dev_ppc64_write_nvram: kmalloc failed\n"); return 0; } if (copy_from_user(tmp_buffer, buf, count)) { kfree(tmp_buffer); return -EFAULT; } len = write_nvram(tmp_buffer, count, &pos); *ppos = pos; kfree(tmp_buffer); return len;}static int dev_ppc64_nvram_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ return -EINVAL;}struct file_operations nvram_fops = { .owner = THIS_MODULE, .llseek = dev_ppc64_nvram_llseek, .read = dev_ppc64_read_nvram, .write = dev_ppc64_write_nvram, .ioctl = dev_ppc64_nvram_ioctl};static struct miscdevice nvram_dev = { NVRAM_MINOR, "nvram", &nvram_fops};ssize_t read_nvram(char *buf, size_t count, loff_t *index){ unsigned long s; ssize_t rc; spin_lock_irqsave(&nvram_lock, s); rc = __read_nvram(buf, count, index); spin_unlock_irqrestore(&nvram_lock, s); return rc;}static ssize_t __read_nvram(char *buf, size_t count, loff_t *index){ unsigned int i; unsigned long len; unsigned long remainder; char *p = buf; if (((*index + count) > rtas_nvram_size) || (count < 0)) return 0; if (count <= NVRW_CNT) { remainder = count; } else { remainder = count % NVRW_CNT; } if (remainder) { if((rtas_call(nvram_fetch, 3, 2, &len, *index, __pa(nvram_buf), remainder) != 0) || len != remainder) { return -EIO; } count -= remainder; memcpy(p, nvram_buf, remainder); p += remainder; } for (i = *index + remainder; count > 0 && i < rtas_nvram_size; count -= NVRW_CNT) { if ((rtas_call(nvram_fetch, 3, 2, &len, i, __pa(nvram_buf), NVRW_CNT) != 0) || len != NVRW_CNT) { return -EIO; } memcpy(p, nvram_buf, NVRW_CNT); p += NVRW_CNT; i += NVRW_CNT; } *index = i; return p - buf;}ssize_t write_nvram(char *buf, size_t count, loff_t *index){ unsigned long s; ssize_t rc; spin_lock_irqsave(&nvram_lock, s); rc = __write_nvram(buf, count, index); spin_unlock_irqrestore(&nvram_lock, s); return rc;}static ssize_t __write_nvram(char *buf, size_t count, loff_t *index){ unsigned int i; unsigned long len; const char *p = buf; unsigned long remainder; if (((*index + count) > rtas_nvram_size) || (count < 0)) return 0; if (count <= NVRW_CNT) { remainder = count; } else { remainder = count % NVRW_CNT; } if (remainder) { memcpy(nvram_buf, p, remainder); if((rtas_call(nvram_store, 3, 2, &len, *index, __pa(nvram_buf), remainder) != 0) || len != remainder) { return -EIO; } count -= remainder; p += remainder; } for (i = *index + remainder; count > 0 && i < rtas_nvram_size; count -= NVRW_CNT) { memcpy(nvram_buf, p, NVRW_CNT); if ((rtas_call(nvram_store, 3, 2, &len, i, __pa(nvram_buf), NVRW_CNT) != 0) || len != NVRW_CNT) { return -EIO; } p += NVRW_CNT; i += NVRW_CNT; } *index = i; return p - buf;}int __init nvram_init(void){ struct device_node *nvram; unsigned int *nbytes_p, proplen; int error; int rc; if ((nvram = find_type_devices("nvram")) != NULL) { nbytes_p = (unsigned int *)get_property(nvram, "#bytes", &proplen); if (nbytes_p && proplen == sizeof(unsigned int)) { rtas_nvram_size = *nbytes_p; } else { return -EIO; } } else { /* If we don't know how big NVRAM is then we shouldn't touch the nvram partitions */ return -EIO; } nvram_fetch = rtas_token("nvram-fetch"); if (nvram_fetch == RTAS_UNKNOWN_SERVICE) { printk("nvram_init: Does not support nvram-fetch\n"); return -EIO; } nvram_store = rtas_token("nvram-store"); if (nvram_store == RTAS_UNKNOWN_SERVICE) { printk("nvram_init: Does not support nvram-store\n"); return -EIO; } printk(KERN_INFO "PPC64 nvram contains %d bytes\n", rtas_nvram_size); rc = misc_register(&nvram_dev); if (rc) { printk(KERN_ERR "nvram_init: Failed misc_register (%d)\n", rc); /* Going to continue to setup nvram for internal * kernel services */ } /* initialize our anchor for the nvram partition list */ nvram_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL); if (!nvram_part) { printk(KERN_ERR "nvram_init: Failed kmalloc\n"); return -ENOMEM; } INIT_LIST_HEAD(&nvram_part->partition); /* Get all the NVRAM partitions */ error = scan_nvram_partitions(); if (error) { printk(KERN_ERR "nvram_init: Failed scan_nvram_partitions\n"); return error; } error = setup_nvram_partition(); if (error) { printk(KERN_WARNING "nvram_init: Could not find nvram partition" " for nvram buffered error logging.\n"); return error; }#ifdef DEBUG_NVRAM print_nvram_partitions("NVRAM Partitions");#endif return rc;}void __exit nvram_cleanup(void){ misc_deregister( &nvram_dev );}static int scan_nvram_partitions(void){ loff_t cur_index = 0; struct nvram_header phead; struct nvram_partition * tmp_part; unsigned char c_sum; long size; while (cur_index < rtas_nvram_size) { size = read_nvram((char *)&phead, NVRAM_HEADER_LEN, &cur_index); if (size != NVRAM_HEADER_LEN) { printk(KERN_ERR "scan_nvram_partitions: Error parsing " "nvram partitions\n"); return size; } cur_index -= NVRAM_HEADER_LEN; /* read_nvram will advance us */ c_sum = nvram_checksum(&phead); if (c_sum != phead.checksum) printk(KERN_WARNING "WARNING: nvram partition checksum " "was %02x, should be %02x!\n", phead.checksum, c_sum); tmp_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL); if (!tmp_part) { printk(KERN_ERR "scan_nvram_partitions: kmalloc failed\n"); return -ENOMEM; } memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN); tmp_part->index = cur_index; list_add_tail(&tmp_part->partition, &nvram_part->partition); cur_index += phead.length * NVRAM_BLOCK_LEN; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -