📄 nvram_64.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 * * TODO: Split the /dev/nvram part (that one can use * drivers/char/generic_nvram.c) from the arch & partition * parsing code. */#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 <asm/machdep.h>#undef DEBUG_NVRAMstatic int nvram_scan_partitions(void);static int nvram_setup_partition(void);static int nvram_create_os_partition(void);static int nvram_remove_os_partition(void);static struct nvram_partition * nvram_part;static long nvram_error_log_index = -1;static long nvram_error_log_size = 0;int no_logging = 1; /* Until we initialize everything, * make sure we don't try logging * anything */extern volatile int error_log_cnt;struct err_log_info { int error_type; unsigned int seq_num;};static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin){ int size; if (ppc_md.nvram_size == NULL) return -ENODEV; size = ppc_md.nvram_size(); switch (origin) { case 1: offset += file->f_pos; break; case 2: offset += size; break; } if (offset < 0) return -EINVAL; file->f_pos = offset; return file->f_pos;}static ssize_t dev_nvram_read(struct file *file, char __user *buf, size_t count, loff_t *ppos){ ssize_t len; char *tmp_buffer; int size; if (ppc_md.nvram_size == NULL) return -ENODEV; size = ppc_md.nvram_size(); if (!access_ok(VERIFY_WRITE, buf, count)) return -EFAULT; if (*ppos >= size) return 0; if (count > size) count = size; tmp_buffer = (char *) kmalloc(count, GFP_KERNEL); if (!tmp_buffer) { printk(KERN_ERR "dev_read_nvram: kmalloc failed\n"); return -ENOMEM; } len = ppc_md.nvram_read(tmp_buffer, count, ppos); 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); return len;}static ssize_t dev_nvram_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos){ ssize_t len; char * tmp_buffer; int size; if (ppc_md.nvram_size == NULL) return -ENODEV; size = ppc_md.nvram_size(); if (!access_ok(VERIFY_READ, buf, count)) return -EFAULT; if (*ppos >= size) return 0; if (count > size) count = size; tmp_buffer = (char *) kmalloc(count, GFP_KERNEL); if (!tmp_buffer) { printk(KERN_ERR "dev_nvram_write: kmalloc failed\n"); return -ENOMEM; } if (copy_from_user(tmp_buffer, buf, count)) { kfree(tmp_buffer); return -EFAULT; } len = ppc_md.nvram_write(tmp_buffer, count, ppos); if ((long)len <= 0) { kfree(tmp_buffer); return len; } kfree(tmp_buffer); return len;}static int dev_nvram_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ switch(cmd) {#ifdef CONFIG_PPC_PMAC case OBSOLETE_PMAC_NVRAM_GET_OFFSET: printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n"); case IOC_NVRAM_GET_OFFSET: { int part, offset; if (_machine != PLATFORM_POWERMAC) return -EINVAL; if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0) return -EFAULT; if (part < pmac_nvram_OF || part > pmac_nvram_NR) return -EINVAL; offset = pmac_get_partition(part); if (offset < 0) return offset; if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0) return -EFAULT; return 0; }#endif /* CONFIG_PPC_PMAC */ } return -EINVAL;}struct file_operations nvram_fops = { .owner = THIS_MODULE, .llseek = dev_nvram_llseek, .read = dev_nvram_read, .write = dev_nvram_write, .ioctl = dev_nvram_ioctl,};static struct miscdevice nvram_dev = { NVRAM_MINOR, "nvram", &nvram_fops};#ifdef DEBUG_NVRAMstatic void nvram_print_partitions(char * label){ struct list_head * p; struct nvram_partition * tmp_part; printk(KERN_WARNING "--------%s---------\n", label); printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n"); list_for_each(p, &nvram_part->partition) { tmp_part = list_entry(p, struct nvram_partition, partition); printk(KERN_WARNING "%d \t%02x\t%02x\t%d\t%s\n", tmp_part->index, tmp_part->header.signature, tmp_part->header.checksum, tmp_part->header.length, tmp_part->header.name); }}#endifstatic int nvram_write_header(struct nvram_partition * part){ loff_t tmp_index; int rc; tmp_index = part->index; rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index); return rc;}static unsigned char nvram_checksum(struct nvram_header *p){ unsigned int c_sum, c_sum2; unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */ c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5]; /* The sum may have spilled into the 3rd byte. Fold it back. */ c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff; /* The sum cannot exceed 2 bytes. Fold it into a checksum */ c_sum2 = (c_sum >> 8) + (c_sum << 8); c_sum = ((c_sum + c_sum2) >> 8) & 0xff; return c_sum;}/* * Find an nvram partition, sig can be 0 for any * partition or name can be NULL for any name, else * tries to match both */struct nvram_partition *nvram_find_partition(int sig, const char *name){ struct nvram_partition * part; struct list_head * p; list_for_each(p, &nvram_part->partition) { part = list_entry(p, struct nvram_partition, partition); if (sig && part->header.signature != sig) continue; if (name && 0 != strncmp(name, part->header.name, 12)) continue; return part; } return NULL;}EXPORT_SYMBOL(nvram_find_partition);static int nvram_remove_os_partition(void){ struct list_head *i; struct list_head *j; struct nvram_partition * part; struct nvram_partition * cur_part; int rc; list_for_each(i, &nvram_part->partition) { part = list_entry(i, struct nvram_partition, partition); if (part->header.signature != NVRAM_SIG_OS) continue; /* Make os partition a free partition */ part->header.signature = NVRAM_SIG_FREE; sprintf(part->header.name, "wwwwwwwwwwww"); part->header.checksum = nvram_checksum(&part->header); /* Merge contiguous free partitions backwards */ list_for_each_prev(j, &part->partition) { cur_part = list_entry(j, struct nvram_partition, partition); if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) { break; } part->header.length += cur_part->header.length; part->header.checksum = nvram_checksum(&part->header); part->index = cur_part->index; list_del(&cur_part->partition); kfree(cur_part); j = &part->partition; /* fixup our loop */ } /* Merge contiguous free partitions forwards */ list_for_each(j, &part->partition) { cur_part = list_entry(j, struct nvram_partition, partition); if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) { break; } part->header.length += cur_part->header.length; part->header.checksum = nvram_checksum(&part->header); list_del(&cur_part->partition); kfree(cur_part); j = &part->partition; /* fixup our loop */ } rc = nvram_write_header(part); if (rc <= 0) { printk(KERN_ERR "nvram_remove_os_partition: nvram_write failed (%d)\n", rc); return rc; } } return 0;}/* nvram_create_os_partition * * Create a OS linux partition to buffer error logs. * Will create a partition starting at the first free * space found if space has enough room. */static int nvram_create_os_partition(void){ struct nvram_partition *part; struct nvram_partition *new_part; struct nvram_partition *free_part = NULL; int seq_init[2] = { 0, 0 }; loff_t tmp_index; long size = 0; int rc; /* Find a free partition that will give us the maximum needed size If can't find one that will give us the minimum size needed */ list_for_each_entry(part, &nvram_part->partition, partition) { if (part->header.signature != NVRAM_SIG_FREE) continue; if (part->header.length >= NVRAM_MAX_REQ) { size = NVRAM_MAX_REQ; free_part = part; break; } if (!size && part->header.length >= NVRAM_MIN_REQ) { size = NVRAM_MIN_REQ; free_part = part; } } if (!size) return -ENOSPC; /* Create our OS partition */ new_part = kmalloc(sizeof(*new_part), GFP_KERNEL); if (!new_part) { printk(KERN_ERR "nvram_create_os_partition: kmalloc failed\n"); return -ENOMEM; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -