| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *  c 2001 PPC 64 Team, IBM Corp | 
|  | 3 | * | 
|  | 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. | 
|  | 8 | * | 
|  | 9 | * /dev/nvram driver for PPC64 | 
|  | 10 | * | 
|  | 11 | * This perhaps should live in drivers/char | 
|  | 12 | * | 
|  | 13 | * TODO: Split the /dev/nvram part (that one can use | 
|  | 14 | *       drivers/char/generic_nvram.c) from the arch & partition | 
|  | 15 | *       parsing code. | 
|  | 16 | */ | 
|  | 17 |  | 
|  | 18 | #include <linux/module.h> | 
|  | 19 |  | 
|  | 20 | #include <linux/types.h> | 
|  | 21 | #include <linux/errno.h> | 
|  | 22 | #include <linux/fs.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> | 
|  | 31 | #include <asm/rtas.h> | 
|  | 32 | #include <asm/prom.h> | 
|  | 33 | #include <asm/machdep.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 |  | 
|  | 35 | #undef DEBUG_NVRAM | 
|  | 36 |  | 
| Benjamin Herrenschmidt | 3667330 | 2010-07-29 18:18:44 +1000 | [diff] [blame] | 37 | #define NVRAM_HEADER_LEN	sizeof(struct nvram_header) | 
|  | 38 | #define NVRAM_BLOCK_LEN		NVRAM_HEADER_LEN | 
| Benjamin Herrenschmidt | 74d51d0 | 2010-07-29 14:45:24 +1000 | [diff] [blame] | 39 |  | 
|  | 40 | /* If change this size, then change the size of NVNAME_LEN */ | 
|  | 41 | struct nvram_header { | 
|  | 42 | unsigned char signature; | 
|  | 43 | unsigned char checksum; | 
|  | 44 | unsigned short length; | 
| Jim Keniston | 6024ede | 2010-11-11 18:54:27 +0000 | [diff] [blame] | 45 | /* Terminating null required only for names < 12 chars. */ | 
| Benjamin Herrenschmidt | 74d51d0 | 2010-07-29 14:45:24 +1000 | [diff] [blame] | 46 | char name[12]; | 
|  | 47 | }; | 
|  | 48 |  | 
|  | 49 | struct nvram_partition { | 
|  | 50 | struct list_head partition; | 
|  | 51 | struct nvram_header header; | 
|  | 52 | unsigned int index; | 
|  | 53 | }; | 
|  | 54 |  | 
| Jim Keniston | 690d1a9 | 2010-11-11 18:54:22 +0000 | [diff] [blame] | 55 | static LIST_HEAD(nvram_partitions); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 |  | 
|  | 57 | static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin) | 
|  | 58 | { | 
|  | 59 | int size; | 
|  | 60 |  | 
|  | 61 | if (ppc_md.nvram_size == NULL) | 
|  | 62 | return -ENODEV; | 
|  | 63 | size = ppc_md.nvram_size(); | 
|  | 64 |  | 
|  | 65 | switch (origin) { | 
|  | 66 | case 1: | 
|  | 67 | offset += file->f_pos; | 
|  | 68 | break; | 
|  | 69 | case 2: | 
|  | 70 | offset += size; | 
|  | 71 | break; | 
|  | 72 | } | 
|  | 73 | if (offset < 0) | 
|  | 74 | return -EINVAL; | 
|  | 75 | file->f_pos = offset; | 
|  | 76 | return file->f_pos; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 |  | 
|  | 80 | static ssize_t dev_nvram_read(struct file *file, char __user *buf, | 
|  | 81 | size_t count, loff_t *ppos) | 
|  | 82 | { | 
| Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 83 | ssize_t ret; | 
|  | 84 | char *tmp = NULL; | 
|  | 85 | ssize_t size; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 |  | 
| Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 87 | ret = -ENODEV; | 
|  | 88 | if (!ppc_md.nvram_size) | 
|  | 89 | goto out; | 
|  | 90 |  | 
|  | 91 | ret = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | size = ppc_md.nvram_size(); | 
| Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 93 | if (*ppos >= size || size < 0) | 
|  | 94 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 |  | 
| Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 96 | count = min_t(size_t, count, size - *ppos); | 
|  | 97 | count = min(count, PAGE_SIZE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 |  | 
| Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 99 | ret = -ENOMEM; | 
|  | 100 | tmp = kmalloc(count, GFP_KERNEL); | 
|  | 101 | if (!tmp) | 
|  | 102 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 |  | 
| Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 104 | ret = ppc_md.nvram_read(tmp, count, ppos); | 
|  | 105 | if (ret <= 0) | 
|  | 106 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 |  | 
| Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 108 | if (copy_to_user(buf, tmp, ret)) | 
|  | 109 | ret = -EFAULT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 |  | 
| Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 111 | out: | 
|  | 112 | kfree(tmp); | 
|  | 113 | return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 |  | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | static ssize_t dev_nvram_write(struct file *file, const char __user *buf, | 
| Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 118 | size_t count, loff_t *ppos) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { | 
| Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 120 | ssize_t ret; | 
|  | 121 | char *tmp = NULL; | 
|  | 122 | ssize_t size; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 |  | 
| Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 124 | ret = -ENODEV; | 
|  | 125 | if (!ppc_md.nvram_size) | 
|  | 126 | goto out; | 
|  | 127 |  | 
|  | 128 | ret = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | size = ppc_md.nvram_size(); | 
| Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 130 | if (*ppos >= size || size < 0) | 
|  | 131 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 |  | 
| Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 133 | count = min_t(size_t, count, size - *ppos); | 
|  | 134 | count = min(count, PAGE_SIZE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 |  | 
| Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 136 | ret = -ENOMEM; | 
|  | 137 | tmp = kmalloc(count, GFP_KERNEL); | 
|  | 138 | if (!tmp) | 
|  | 139 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 |  | 
| Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 141 | ret = -EFAULT; | 
|  | 142 | if (copy_from_user(tmp, buf, count)) | 
|  | 143 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 |  | 
| Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 145 | ret = ppc_md.nvram_write(tmp, count, ppos); | 
|  | 146 |  | 
|  | 147 | out: | 
|  | 148 | kfree(tmp); | 
|  | 149 | return ret; | 
|  | 150 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } | 
|  | 152 |  | 
| Thomas Gleixner | 3b03fec | 2009-10-14 22:42:28 +0000 | [diff] [blame] | 153 | static long dev_nvram_ioctl(struct file *file, unsigned int cmd, | 
|  | 154 | unsigned long arg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | { | 
|  | 156 | switch(cmd) { | 
|  | 157 | #ifdef CONFIG_PPC_PMAC | 
|  | 158 | case OBSOLETE_PMAC_NVRAM_GET_OFFSET: | 
|  | 159 | printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n"); | 
|  | 160 | case IOC_NVRAM_GET_OFFSET: { | 
|  | 161 | int part, offset; | 
|  | 162 |  | 
| Benjamin Herrenschmidt | e822250 | 2006-03-28 23:15:54 +1100 | [diff] [blame] | 163 | if (!machine_is(powermac)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | return -EINVAL; | 
|  | 165 | if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0) | 
|  | 166 | return -EFAULT; | 
|  | 167 | if (part < pmac_nvram_OF || part > pmac_nvram_NR) | 
|  | 168 | return -EINVAL; | 
|  | 169 | offset = pmac_get_partition(part); | 
|  | 170 | if (offset < 0) | 
|  | 171 | return offset; | 
|  | 172 | if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0) | 
|  | 173 | return -EFAULT; | 
|  | 174 | return 0; | 
|  | 175 | } | 
|  | 176 | #endif /* CONFIG_PPC_PMAC */ | 
| Stephen Rothwell | af30837 | 2006-03-23 17:38:10 +1100 | [diff] [blame] | 177 | default: | 
|  | 178 | return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } | 
|  | 181 |  | 
| Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 182 | const struct file_operations nvram_fops = { | 
| Thomas Gleixner | 3b03fec | 2009-10-14 22:42:28 +0000 | [diff] [blame] | 183 | .owner		= THIS_MODULE, | 
|  | 184 | .llseek		= dev_nvram_llseek, | 
|  | 185 | .read		= dev_nvram_read, | 
|  | 186 | .write		= dev_nvram_write, | 
|  | 187 | .unlocked_ioctl	= dev_nvram_ioctl, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | }; | 
|  | 189 |  | 
|  | 190 | static struct miscdevice nvram_dev = { | 
|  | 191 | NVRAM_MINOR, | 
|  | 192 | "nvram", | 
|  | 193 | &nvram_fops | 
|  | 194 | }; | 
|  | 195 |  | 
|  | 196 |  | 
|  | 197 | #ifdef DEBUG_NVRAM | 
| Thomas Gleixner | 32c105c | 2009-10-14 22:54:46 +0000 | [diff] [blame] | 198 | static void __init nvram_print_partitions(char * label) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | struct nvram_partition * tmp_part; | 
|  | 201 |  | 
|  | 202 | printk(KERN_WARNING "--------%s---------\n", label); | 
|  | 203 | printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n"); | 
| Jim Keniston | 690d1a9 | 2010-11-11 18:54:22 +0000 | [diff] [blame] | 204 | list_for_each_entry(tmp_part, &nvram_partitions, partition) { | 
| Jim Keniston | 6024ede | 2010-11-11 18:54:27 +0000 | [diff] [blame] | 205 | printk(KERN_WARNING "%4d    \t%02x\t%02x\t%d\t%12s\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | tmp_part->index, tmp_part->header.signature, | 
|  | 207 | tmp_part->header.checksum, tmp_part->header.length, | 
|  | 208 | tmp_part->header.name); | 
|  | 209 | } | 
|  | 210 | } | 
|  | 211 | #endif | 
|  | 212 |  | 
|  | 213 |  | 
| Thomas Gleixner | 32c105c | 2009-10-14 22:54:46 +0000 | [diff] [blame] | 214 | static int __init nvram_write_header(struct nvram_partition * part) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { | 
|  | 216 | loff_t tmp_index; | 
|  | 217 | int rc; | 
|  | 218 |  | 
|  | 219 | tmp_index = part->index; | 
|  | 220 | rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index); | 
|  | 221 |  | 
|  | 222 | return rc; | 
|  | 223 | } | 
|  | 224 |  | 
|  | 225 |  | 
| Thomas Gleixner | 32c105c | 2009-10-14 22:54:46 +0000 | [diff] [blame] | 226 | static unsigned char __init nvram_checksum(struct nvram_header *p) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | { | 
|  | 228 | unsigned int c_sum, c_sum2; | 
|  | 229 | unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */ | 
|  | 230 | c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5]; | 
|  | 231 |  | 
|  | 232 | /* The sum may have spilled into the 3rd byte.  Fold it back. */ | 
|  | 233 | c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff; | 
|  | 234 | /* The sum cannot exceed 2 bytes.  Fold it into a checksum */ | 
|  | 235 | c_sum2 = (c_sum >> 8) + (c_sum << 8); | 
|  | 236 | c_sum = ((c_sum + c_sum2) >> 8) & 0xff; | 
|  | 237 | return c_sum; | 
|  | 238 | } | 
|  | 239 |  | 
| Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 240 | /* | 
|  | 241 | * Per the criteria passed via nvram_remove_partition(), should this | 
|  | 242 | * partition be removed?  1=remove, 0=keep | 
|  | 243 | */ | 
|  | 244 | static int nvram_can_remove_partition(struct nvram_partition *part, | 
|  | 245 | const char *name, int sig, const char *exceptions[]) | 
|  | 246 | { | 
|  | 247 | if (part->header.signature != sig) | 
|  | 248 | return 0; | 
|  | 249 | if (name) { | 
|  | 250 | if (strncmp(name, part->header.name, 12)) | 
|  | 251 | return 0; | 
|  | 252 | } else if (exceptions) { | 
|  | 253 | const char **except; | 
|  | 254 | for (except = exceptions; *except; except++) { | 
|  | 255 | if (!strncmp(*except, part->header.name, 12)) | 
|  | 256 | return 0; | 
|  | 257 | } | 
|  | 258 | } | 
|  | 259 | return 1; | 
|  | 260 | } | 
|  | 261 |  | 
| Benjamin Herrenschmidt | fa2b4e5 | 2010-07-29 18:19:59 +1000 | [diff] [blame] | 262 | /** | 
|  | 263 | * nvram_remove_partition - Remove one or more partitions in nvram | 
|  | 264 | * @name: name of the partition to remove, or NULL for a | 
|  | 265 | *        signature only match | 
|  | 266 | * @sig: signature of the partition(s) to remove | 
| Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 267 | * @exceptions: When removing all partitions with a matching signature, | 
|  | 268 | *        leave these alone. | 
| Benjamin Herrenschmidt | fa2b4e5 | 2010-07-29 18:19:59 +1000 | [diff] [blame] | 269 | */ | 
|  | 270 |  | 
| Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 271 | int __init nvram_remove_partition(const char *name, int sig, | 
|  | 272 | const char *exceptions[]) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { | 
| Benjamin Herrenschmidt | fa2b4e5 | 2010-07-29 18:19:59 +1000 | [diff] [blame] | 274 | struct nvram_partition *part, *prev, *tmp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | int rc; | 
|  | 276 |  | 
| Jim Keniston | 690d1a9 | 2010-11-11 18:54:22 +0000 | [diff] [blame] | 277 | list_for_each_entry(part, &nvram_partitions, partition) { | 
| Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 278 | if (!nvram_can_remove_partition(part, name, sig, exceptions)) | 
| Benjamin Herrenschmidt | fa2b4e5 | 2010-07-29 18:19:59 +1000 | [diff] [blame] | 279 | continue; | 
|  | 280 |  | 
|  | 281 | /* Make partition a free partition */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | part->header.signature = NVRAM_SIG_FREE; | 
| Jim Keniston | 6024ede | 2010-11-11 18:54:27 +0000 | [diff] [blame] | 283 | strncpy(part->header.name, "wwwwwwwwwwww", 12); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | part->header.checksum = nvram_checksum(&part->header); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | rc = nvram_write_header(part); | 
|  | 286 | if (rc <= 0) { | 
| Benjamin Herrenschmidt | fa2b4e5 | 2010-07-29 18:19:59 +1000 | [diff] [blame] | 287 | printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | return rc; | 
|  | 289 | } | 
| Benjamin Herrenschmidt | fa2b4e5 | 2010-07-29 18:19:59 +1000 | [diff] [blame] | 290 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 |  | 
| Benjamin Herrenschmidt | fa2b4e5 | 2010-07-29 18:19:59 +1000 | [diff] [blame] | 292 | /* Merge contiguous ones */ | 
|  | 293 | prev = NULL; | 
| Jim Keniston | 690d1a9 | 2010-11-11 18:54:22 +0000 | [diff] [blame] | 294 | list_for_each_entry_safe(part, tmp, &nvram_partitions, partition) { | 
| Benjamin Herrenschmidt | fa2b4e5 | 2010-07-29 18:19:59 +1000 | [diff] [blame] | 295 | if (part->header.signature != NVRAM_SIG_FREE) { | 
|  | 296 | prev = NULL; | 
|  | 297 | continue; | 
|  | 298 | } | 
|  | 299 | if (prev) { | 
|  | 300 | prev->header.length += part->header.length; | 
|  | 301 | prev->header.checksum = nvram_checksum(&part->header); | 
|  | 302 | rc = nvram_write_header(part); | 
|  | 303 | if (rc <= 0) { | 
|  | 304 | printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc); | 
|  | 305 | return rc; | 
|  | 306 | } | 
|  | 307 | list_del(&part->partition); | 
|  | 308 | kfree(part); | 
|  | 309 | } else | 
|  | 310 | prev = part; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } | 
|  | 312 |  | 
|  | 313 | return 0; | 
|  | 314 | } | 
|  | 315 |  | 
| Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 316 | /** | 
|  | 317 | * nvram_create_partition - Create a partition in nvram | 
|  | 318 | * @name: name of the partition to create | 
|  | 319 | * @sig: signature of the partition to create | 
| Benjamin Herrenschmidt | 3667330 | 2010-07-29 18:18:44 +1000 | [diff] [blame] | 320 | * @req_size: size of data to allocate in bytes | 
| Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 321 | * @min_size: minimum acceptable size (0 means req_size) | 
| Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 322 | * | 
|  | 323 | * Returns a negative error code or a positive nvram index | 
|  | 324 | * of the beginning of the data area of the newly created | 
|  | 325 | * partition. If you provided a min_size smaller than req_size | 
|  | 326 | * you need to query for the actual size yourself after the | 
|  | 327 | * call using nvram_partition_get_size(). | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | */ | 
| Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 329 | loff_t __init nvram_create_partition(const char *name, int sig, | 
|  | 330 | int req_size, int min_size) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | { | 
| Arnd Bergmann | a341ad9 | 2005-06-28 20:33:49 +1000 | [diff] [blame] | 332 | struct nvram_partition *part; | 
|  | 333 | struct nvram_partition *new_part; | 
| akpm@osdl.org | 0339ad7 | 2005-05-01 08:58:44 -0700 | [diff] [blame] | 334 | struct nvram_partition *free_part = NULL; | 
| Benjamin Herrenschmidt | cef0d5a | 2010-07-29 17:22:34 +1000 | [diff] [blame] | 335 | static char nv_init_vals[16]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | loff_t tmp_index; | 
|  | 337 | long size = 0; | 
|  | 338 | int rc; | 
| Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 339 |  | 
| Benjamin Herrenschmidt | 3667330 | 2010-07-29 18:18:44 +1000 | [diff] [blame] | 340 | /* Convert sizes from bytes to blocks */ | 
|  | 341 | req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN; | 
|  | 342 | min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN; | 
|  | 343 |  | 
| Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 344 | /* If no minimum size specified, make it the same as the | 
|  | 345 | * requested size | 
|  | 346 | */ | 
|  | 347 | if (min_size == 0) | 
|  | 348 | min_size = req_size; | 
| Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 349 | if (min_size > req_size) | 
|  | 350 | return -EINVAL; | 
| Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 351 |  | 
| Benjamin Herrenschmidt | 3667330 | 2010-07-29 18:18:44 +1000 | [diff] [blame] | 352 | /* Now add one block to each for the header */ | 
|  | 353 | req_size += 1; | 
|  | 354 | min_size += 1; | 
|  | 355 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | /* Find a free partition that will give us the maximum needed size | 
|  | 357 | If can't find one that will give us the minimum size needed */ | 
| Jim Keniston | 690d1a9 | 2010-11-11 18:54:22 +0000 | [diff] [blame] | 358 | list_for_each_entry(part, &nvram_partitions, partition) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | if (part->header.signature != NVRAM_SIG_FREE) | 
|  | 360 | continue; | 
|  | 361 |  | 
| Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 362 | if (part->header.length >= req_size) { | 
|  | 363 | size = req_size; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | free_part = part; | 
|  | 365 | break; | 
|  | 366 | } | 
| Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 367 | if (part->header.length > size && | 
|  | 368 | part->header.length >= min_size) { | 
|  | 369 | size = part->header.length; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | free_part = part; | 
|  | 371 | } | 
|  | 372 | } | 
| akpm@osdl.org | 0339ad7 | 2005-05-01 08:58:44 -0700 | [diff] [blame] | 373 | if (!size) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | return -ENOSPC; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 |  | 
|  | 376 | /* Create our OS partition */ | 
| akpm@osdl.org | 0339ad7 | 2005-05-01 08:58:44 -0700 | [diff] [blame] | 377 | new_part = kmalloc(sizeof(*new_part), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | if (!new_part) { | 
| Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 379 | pr_err("nvram_create_os_partition: kmalloc failed\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | return -ENOMEM; | 
|  | 381 | } | 
|  | 382 |  | 
|  | 383 | new_part->index = free_part->index; | 
| Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 384 | new_part->header.signature = sig; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | new_part->header.length = size; | 
| Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 386 | strncpy(new_part->header.name, name, 12); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | new_part->header.checksum = nvram_checksum(&new_part->header); | 
|  | 388 |  | 
|  | 389 | rc = nvram_write_header(new_part); | 
|  | 390 | if (rc <= 0) { | 
| Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 391 | pr_err("nvram_create_os_partition: nvram_write_header " | 
|  | 392 | "failed (%d)\n", rc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | return rc; | 
|  | 394 | } | 
| Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 395 | list_add_tail(&new_part->partition, &free_part->partition); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 |  | 
| Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 397 | /* Adjust or remove the partition we stole the space from */ | 
|  | 398 | if (free_part->header.length > size) { | 
|  | 399 | free_part->index += size * NVRAM_BLOCK_LEN; | 
|  | 400 | free_part->header.length -= size; | 
|  | 401 | free_part->header.checksum = nvram_checksum(&free_part->header); | 
|  | 402 | rc = nvram_write_header(free_part); | 
|  | 403 | if (rc <= 0) { | 
|  | 404 | pr_err("nvram_create_os_partition: nvram_write_header " | 
|  | 405 | "failed (%d)\n", rc); | 
|  | 406 | return rc; | 
|  | 407 | } | 
|  | 408 | } else { | 
|  | 409 | list_del(&free_part->partition); | 
|  | 410 | kfree(free_part); | 
|  | 411 | } | 
|  | 412 |  | 
|  | 413 | /* Clear the new partition */ | 
| Benjamin Herrenschmidt | cef0d5a | 2010-07-29 17:22:34 +1000 | [diff] [blame] | 414 | for (tmp_index = new_part->index + NVRAM_HEADER_LEN; | 
|  | 415 | tmp_index <  ((size - 1) * NVRAM_BLOCK_LEN); | 
|  | 416 | tmp_index += NVRAM_BLOCK_LEN) { | 
|  | 417 | rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index); | 
|  | 418 | if (rc <= 0) { | 
|  | 419 | pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc); | 
|  | 420 | return rc; | 
|  | 421 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } | 
|  | 423 |  | 
| Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 424 | return new_part->index + NVRAM_HEADER_LEN; | 
|  | 425 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 |  | 
| Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 427 | /** | 
|  | 428 | * nvram_get_partition_size - Get the data size of an nvram partition | 
|  | 429 | * @data_index: This is the offset of the start of the data of | 
|  | 430 | *              the partition. The same value that is returned by | 
|  | 431 | *              nvram_create_partition(). | 
|  | 432 | */ | 
| Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 433 | int nvram_get_partition_size(loff_t data_index) | 
| Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 434 | { | 
|  | 435 | struct nvram_partition *part; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 |  | 
| Jim Keniston | 690d1a9 | 2010-11-11 18:54:22 +0000 | [diff] [blame] | 437 | list_for_each_entry(part, &nvram_partitions, partition) { | 
| Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 438 | if (part->index + NVRAM_HEADER_LEN == data_index) | 
|  | 439 | return (part->header.length - 1) * NVRAM_BLOCK_LEN; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } | 
| Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 441 | return -1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | } | 
|  | 443 |  | 
|  | 444 |  | 
| Benjamin Herrenschmidt | cf5cbf9 | 2010-08-02 10:01:58 +1000 | [diff] [blame] | 445 | /** | 
|  | 446 | * nvram_find_partition - Find an nvram partition by signature and name | 
|  | 447 | * @name: Name of the partition or NULL for any name | 
|  | 448 | * @sig: Signature to test against | 
|  | 449 | * @out_size: if non-NULL, returns the size of the data part of the partition | 
|  | 450 | */ | 
|  | 451 | loff_t nvram_find_partition(const char *name, int sig, int *out_size) | 
|  | 452 | { | 
|  | 453 | struct nvram_partition *p; | 
|  | 454 |  | 
| Jim Keniston | 690d1a9 | 2010-11-11 18:54:22 +0000 | [diff] [blame] | 455 | list_for_each_entry(p, &nvram_partitions, partition) { | 
| Benjamin Herrenschmidt | cf5cbf9 | 2010-08-02 10:01:58 +1000 | [diff] [blame] | 456 | if (p->header.signature == sig && | 
|  | 457 | (!name || !strncmp(p->header.name, name, 12))) { | 
|  | 458 | if (out_size) | 
|  | 459 | *out_size = (p->header.length - 1) * | 
|  | 460 | NVRAM_BLOCK_LEN; | 
|  | 461 | return p->index + NVRAM_HEADER_LEN; | 
|  | 462 | } | 
|  | 463 | } | 
|  | 464 | return 0; | 
|  | 465 | } | 
|  | 466 |  | 
| Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 467 | int __init nvram_scan_partitions(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | { | 
|  | 469 | loff_t cur_index = 0; | 
|  | 470 | struct nvram_header phead; | 
|  | 471 | struct nvram_partition * tmp_part; | 
|  | 472 | unsigned char c_sum; | 
|  | 473 | char * header; | 
|  | 474 | int total_size; | 
|  | 475 | int err; | 
|  | 476 |  | 
| Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 477 | if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | return -ENODEV; | 
|  | 479 | total_size = ppc_md.nvram_size(); | 
|  | 480 |  | 
| Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 481 | header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | if (!header) { | 
|  | 483 | printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n"); | 
|  | 484 | return -ENOMEM; | 
|  | 485 | } | 
|  | 486 |  | 
|  | 487 | while (cur_index < total_size) { | 
|  | 488 |  | 
|  | 489 | err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index); | 
|  | 490 | if (err != NVRAM_HEADER_LEN) { | 
|  | 491 | printk(KERN_ERR "nvram_scan_partitions: Error parsing " | 
|  | 492 | "nvram partitions\n"); | 
|  | 493 | goto out; | 
|  | 494 | } | 
|  | 495 |  | 
|  | 496 | cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */ | 
|  | 497 |  | 
|  | 498 | memcpy(&phead, header, NVRAM_HEADER_LEN); | 
|  | 499 |  | 
|  | 500 | err = 0; | 
|  | 501 | c_sum = nvram_checksum(&phead); | 
|  | 502 | if (c_sum != phead.checksum) { | 
|  | 503 | printk(KERN_WARNING "WARNING: nvram partition checksum" | 
|  | 504 | " was %02x, should be %02x!\n", | 
|  | 505 | phead.checksum, c_sum); | 
|  | 506 | printk(KERN_WARNING "Terminating nvram partition scan\n"); | 
|  | 507 | goto out; | 
|  | 508 | } | 
|  | 509 | if (!phead.length) { | 
|  | 510 | printk(KERN_WARNING "WARNING: nvram corruption " | 
|  | 511 | "detected: 0-length partition\n"); | 
|  | 512 | goto out; | 
|  | 513 | } | 
|  | 514 | tmp_part = (struct nvram_partition *) | 
|  | 515 | kmalloc(sizeof(struct nvram_partition), GFP_KERNEL); | 
|  | 516 | err = -ENOMEM; | 
|  | 517 | if (!tmp_part) { | 
|  | 518 | printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n"); | 
|  | 519 | goto out; | 
|  | 520 | } | 
|  | 521 |  | 
|  | 522 | memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN); | 
|  | 523 | tmp_part->index = cur_index; | 
| Jim Keniston | 690d1a9 | 2010-11-11 18:54:22 +0000 | [diff] [blame] | 524 | list_add_tail(&tmp_part->partition, &nvram_partitions); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 |  | 
|  | 526 | cur_index += phead.length * NVRAM_BLOCK_LEN; | 
|  | 527 | } | 
|  | 528 | err = 0; | 
|  | 529 |  | 
| Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 530 | #ifdef DEBUG_NVRAM | 
|  | 531 | nvram_print_partitions("NVRAM Partitions"); | 
|  | 532 | #endif | 
|  | 533 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | out: | 
|  | 535 | kfree(header); | 
|  | 536 | return err; | 
|  | 537 | } | 
|  | 538 |  | 
|  | 539 | static int __init nvram_init(void) | 
|  | 540 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | int rc; | 
|  | 542 |  | 
| Benjamin Herrenschmidt | 578914c | 2010-07-29 17:21:17 +1000 | [diff] [blame] | 543 | BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16); | 
|  | 544 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0) | 
|  | 546 | return  -ENODEV; | 
|  | 547 |  | 
|  | 548 | rc = misc_register(&nvram_dev); | 
|  | 549 | if (rc != 0) { | 
|  | 550 | printk(KERN_ERR "nvram_init: failed to register device\n"); | 
|  | 551 | return rc; | 
|  | 552 | } | 
|  | 553 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | return rc; | 
|  | 555 | } | 
|  | 556 |  | 
|  | 557 | void __exit nvram_cleanup(void) | 
|  | 558 | { | 
|  | 559 | misc_deregister( &nvram_dev ); | 
|  | 560 | } | 
|  | 561 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | module_init(nvram_init); | 
|  | 563 | module_exit(nvram_cleanup); | 
|  | 564 | MODULE_LICENSE("GPL"); |