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; |
| 45 | char name[12]; |
| 46 | }; |
| 47 | |
| 48 | struct nvram_partition { |
| 49 | struct list_head partition; |
| 50 | struct nvram_header header; |
| 51 | unsigned int index; |
| 52 | }; |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | static struct nvram_partition * nvram_part; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin) |
| 57 | { |
| 58 | int size; |
| 59 | |
| 60 | if (ppc_md.nvram_size == NULL) |
| 61 | return -ENODEV; |
| 62 | size = ppc_md.nvram_size(); |
| 63 | |
| 64 | switch (origin) { |
| 65 | case 1: |
| 66 | offset += file->f_pos; |
| 67 | break; |
| 68 | case 2: |
| 69 | offset += size; |
| 70 | break; |
| 71 | } |
| 72 | if (offset < 0) |
| 73 | return -EINVAL; |
| 74 | file->f_pos = offset; |
| 75 | return file->f_pos; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | static ssize_t dev_nvram_read(struct file *file, char __user *buf, |
| 80 | size_t count, loff_t *ppos) |
| 81 | { |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 82 | ssize_t ret; |
| 83 | char *tmp = NULL; |
| 84 | ssize_t size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 86 | ret = -ENODEV; |
| 87 | if (!ppc_md.nvram_size) |
| 88 | goto out; |
| 89 | |
| 90 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | size = ppc_md.nvram_size(); |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 92 | if (*ppos >= size || size < 0) |
| 93 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 95 | count = min_t(size_t, count, size - *ppos); |
| 96 | count = min(count, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 98 | ret = -ENOMEM; |
| 99 | tmp = kmalloc(count, GFP_KERNEL); |
| 100 | if (!tmp) |
| 101 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 103 | ret = ppc_md.nvram_read(tmp, count, ppos); |
| 104 | if (ret <= 0) |
| 105 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 107 | if (copy_to_user(buf, tmp, ret)) |
| 108 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 110 | out: |
| 111 | kfree(tmp); |
| 112 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | } |
| 115 | |
| 116 | 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] | 117 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 119 | ssize_t ret; |
| 120 | char *tmp = NULL; |
| 121 | ssize_t size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 123 | ret = -ENODEV; |
| 124 | if (!ppc_md.nvram_size) |
| 125 | goto out; |
| 126 | |
| 127 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | size = ppc_md.nvram_size(); |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 129 | if (*ppos >= size || size < 0) |
| 130 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 132 | count = min_t(size_t, count, size - *ppos); |
| 133 | count = min(count, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 135 | ret = -ENOMEM; |
| 136 | tmp = kmalloc(count, GFP_KERNEL); |
| 137 | if (!tmp) |
| 138 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 140 | ret = -EFAULT; |
| 141 | if (copy_from_user(tmp, buf, count)) |
| 142 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 144 | ret = ppc_md.nvram_write(tmp, count, ppos); |
| 145 | |
| 146 | out: |
| 147 | kfree(tmp); |
| 148 | return ret; |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Thomas Gleixner | 3b03fec | 2009-10-14 22:42:28 +0000 | [diff] [blame] | 152 | static long dev_nvram_ioctl(struct file *file, unsigned int cmd, |
| 153 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
| 155 | switch(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: { |
| 160 | int part, offset; |
| 161 | |
Benjamin Herrenschmidt | e822250 | 2006-03-28 23:15:54 +1100 | [diff] [blame] | 162 | if (!machine_is(powermac)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | return -EINVAL; |
| 164 | if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0) |
| 165 | return -EFAULT; |
| 166 | if (part < pmac_nvram_OF || part > pmac_nvram_NR) |
| 167 | return -EINVAL; |
| 168 | offset = pmac_get_partition(part); |
| 169 | if (offset < 0) |
| 170 | return offset; |
| 171 | if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0) |
| 172 | return -EFAULT; |
| 173 | return 0; |
| 174 | } |
| 175 | #endif /* CONFIG_PPC_PMAC */ |
Stephen Rothwell | af30837 | 2006-03-23 17:38:10 +1100 | [diff] [blame] | 176 | default: |
| 177 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 181 | const struct file_operations nvram_fops = { |
Thomas Gleixner | 3b03fec | 2009-10-14 22:42:28 +0000 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | static struct miscdevice nvram_dev = { |
| 190 | NVRAM_MINOR, |
| 191 | "nvram", |
| 192 | &nvram_fops |
| 193 | }; |
| 194 | |
| 195 | |
| 196 | #ifdef DEBUG_NVRAM |
Thomas Gleixner | 32c105c | 2009-10-14 22:54:46 +0000 | [diff] [blame] | 197 | static void __init nvram_print_partitions(char * label) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
| 199 | struct list_head * p; |
| 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"); |
| 204 | list_for_each(p, &nvram_part->partition) { |
| 205 | tmp_part = list_entry(p, struct nvram_partition, partition); |
Will Schmidt | 5a43ee6 | 2006-04-26 11:09:46 -0500 | [diff] [blame] | 206 | printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | tmp_part->index, tmp_part->header.signature, |
| 208 | tmp_part->header.checksum, tmp_part->header.length, |
| 209 | tmp_part->header.name); |
| 210 | } |
| 211 | } |
| 212 | #endif |
| 213 | |
| 214 | |
Thomas Gleixner | 32c105c | 2009-10-14 22:54:46 +0000 | [diff] [blame] | 215 | static int __init nvram_write_header(struct nvram_partition * part) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
| 217 | loff_t tmp_index; |
| 218 | int rc; |
| 219 | |
| 220 | tmp_index = part->index; |
| 221 | rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index); |
| 222 | |
| 223 | return rc; |
| 224 | } |
| 225 | |
| 226 | |
Thomas Gleixner | 32c105c | 2009-10-14 22:54:46 +0000 | [diff] [blame] | 227 | static unsigned char __init nvram_checksum(struct nvram_header *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
| 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]; |
| 232 | |
| 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; |
| 238 | return c_sum; |
| 239 | } |
| 240 | |
Benjamin Herrenschmidt | fa2b4e5 | 2010-07-29 18:19:59 +1000 | [diff] [blame] | 241 | /** |
| 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 |
| 246 | */ |
| 247 | |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame^] | 248 | int __init nvram_remove_partition(const char *name, int sig) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
Benjamin Herrenschmidt | fa2b4e5 | 2010-07-29 18:19:59 +1000 | [diff] [blame] | 250 | struct nvram_partition *part, *prev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | int rc; |
| 252 | |
Benjamin Herrenschmidt | fa2b4e5 | 2010-07-29 18:19:59 +1000 | [diff] [blame] | 253 | list_for_each_entry(part, &nvram_part->partition, partition) { |
| 254 | if (part->header.signature != sig) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | continue; |
Benjamin Herrenschmidt | fa2b4e5 | 2010-07-29 18:19:59 +1000 | [diff] [blame] | 256 | if (name && strncmp(name, part->header.name, 12)) |
| 257 | continue; |
| 258 | |
| 259 | /* Make partition a free partition */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | part->header.signature = NVRAM_SIG_FREE; |
| 261 | sprintf(part->header.name, "wwwwwwwwwwww"); |
| 262 | part->header.checksum = nvram_checksum(&part->header); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | rc = nvram_write_header(part); |
| 264 | if (rc <= 0) { |
Benjamin Herrenschmidt | fa2b4e5 | 2010-07-29 18:19:59 +1000 | [diff] [blame] | 265 | printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | return rc; |
| 267 | } |
Benjamin Herrenschmidt | fa2b4e5 | 2010-07-29 18:19:59 +1000 | [diff] [blame] | 268 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
Benjamin Herrenschmidt | fa2b4e5 | 2010-07-29 18:19:59 +1000 | [diff] [blame] | 270 | /* Merge contiguous ones */ |
| 271 | prev = NULL; |
| 272 | list_for_each_entry_safe(part, tmp, &nvram_part->partition, partition) { |
| 273 | if (part->header.signature != NVRAM_SIG_FREE) { |
| 274 | prev = NULL; |
| 275 | continue; |
| 276 | } |
| 277 | if (prev) { |
| 278 | prev->header.length += part->header.length; |
| 279 | prev->header.checksum = nvram_checksum(&part->header); |
| 280 | rc = nvram_write_header(part); |
| 281 | if (rc <= 0) { |
| 282 | printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc); |
| 283 | return rc; |
| 284 | } |
| 285 | list_del(&part->partition); |
| 286 | kfree(part); |
| 287 | } else |
| 288 | prev = part; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 294 | /** |
| 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 |
Benjamin Herrenschmidt | 3667330 | 2010-07-29 18:18:44 +1000 | [diff] [blame] | 298 | * @req_size: size of data to allocate in bytes |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 299 | * @min_size: minimum acceptable size (0 means req_size) |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 300 | * |
| 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(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | */ |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame^] | 307 | loff_t __init nvram_create_partition(const char *name, int sig, |
| 308 | int req_size, int min_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
Arnd Bergmann | a341ad9 | 2005-06-28 20:33:49 +1000 | [diff] [blame] | 310 | struct nvram_partition *part; |
| 311 | struct nvram_partition *new_part; |
akpm@osdl.org | 0339ad7 | 2005-05-01 08:58:44 -0700 | [diff] [blame] | 312 | struct nvram_partition *free_part = NULL; |
Benjamin Herrenschmidt | cef0d5a | 2010-07-29 17:22:34 +1000 | [diff] [blame] | 313 | static char nv_init_vals[16]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | loff_t tmp_index; |
| 315 | long size = 0; |
| 316 | int rc; |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 317 | |
Benjamin Herrenschmidt | 3667330 | 2010-07-29 18:18:44 +1000 | [diff] [blame] | 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; |
| 321 | |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 322 | /* If no minimum size specified, make it the same as the |
| 323 | * requested size |
| 324 | */ |
| 325 | if (min_size == 0) |
| 326 | min_size = req_size; |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 327 | if (min_size > req_size) |
| 328 | return -EINVAL; |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 329 | |
Benjamin Herrenschmidt | 3667330 | 2010-07-29 18:18:44 +1000 | [diff] [blame] | 330 | /* Now add one block to each for the header */ |
| 331 | req_size += 1; |
| 332 | min_size += 1; |
| 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Arnd Bergmann | a341ad9 | 2005-06-28 20:33:49 +1000 | [diff] [blame] | 336 | list_for_each_entry(part, &nvram_part->partition, partition) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | if (part->header.signature != NVRAM_SIG_FREE) |
| 338 | continue; |
| 339 | |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 340 | if (part->header.length >= req_size) { |
| 341 | size = req_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | free_part = part; |
| 343 | break; |
| 344 | } |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 345 | if (part->header.length > size && |
| 346 | part->header.length >= min_size) { |
| 347 | size = part->header.length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | free_part = part; |
| 349 | } |
| 350 | } |
akpm@osdl.org | 0339ad7 | 2005-05-01 08:58:44 -0700 | [diff] [blame] | 351 | if (!size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
| 354 | /* Create our OS partition */ |
akpm@osdl.org | 0339ad7 | 2005-05-01 08:58:44 -0700 | [diff] [blame] | 355 | new_part = kmalloc(sizeof(*new_part), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | if (!new_part) { |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 357 | pr_err("nvram_create_os_partition: kmalloc failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | return -ENOMEM; |
| 359 | } |
| 360 | |
| 361 | new_part->index = free_part->index; |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 362 | new_part->header.signature = sig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | new_part->header.length = size; |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 364 | strncpy(new_part->header.name, name, 12); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | new_part->header.checksum = nvram_checksum(&new_part->header); |
| 366 | |
| 367 | rc = nvram_write_header(new_part); |
| 368 | if (rc <= 0) { |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 369 | pr_err("nvram_create_os_partition: nvram_write_header " |
| 370 | "failed (%d)\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | return rc; |
| 372 | } |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 373 | list_add_tail(&new_part->partition, &free_part->partition); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 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); |
| 381 | if (rc <= 0) { |
| 382 | pr_err("nvram_create_os_partition: nvram_write_header " |
| 383 | "failed (%d)\n", rc); |
| 384 | return rc; |
| 385 | } |
| 386 | } else { |
| 387 | list_del(&free_part->partition); |
| 388 | kfree(free_part); |
| 389 | } |
| 390 | |
| 391 | /* Clear the new partition */ |
Benjamin Herrenschmidt | cef0d5a | 2010-07-29 17:22:34 +1000 | [diff] [blame] | 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); |
| 396 | if (rc <= 0) { |
| 397 | pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc); |
| 398 | return rc; |
| 399 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 402 | return new_part->index + NVRAM_HEADER_LEN; |
| 403 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 405 | /** |
| 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(). |
| 410 | */ |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame^] | 411 | int nvram_get_partition_size(loff_t data_index) |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 412 | { |
| 413 | struct nvram_partition *part; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | } |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame] | 419 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | |
Benjamin Herrenschmidt | cf5cbf9 | 2010-08-02 10:01:58 +1000 | [diff] [blame] | 423 | /** |
| 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 |
| 428 | */ |
| 429 | loff_t nvram_find_partition(const char *name, int sig, int *out_size) |
| 430 | { |
| 431 | struct nvram_partition *p; |
| 432 | |
| 433 | list_for_each_entry(p, &nvram_part->partition, partition) { |
| 434 | if (p->header.signature == sig && |
| 435 | (!name || !strncmp(p->header.name, name, 12))) { |
| 436 | if (out_size) |
| 437 | *out_size = (p->header.length - 1) * |
| 438 | NVRAM_BLOCK_LEN; |
| 439 | return p->index + NVRAM_HEADER_LEN; |
| 440 | } |
| 441 | } |
| 442 | return 0; |
| 443 | } |
| 444 | |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame^] | 445 | int __init nvram_scan_partitions(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { |
| 447 | loff_t cur_index = 0; |
| 448 | struct nvram_header phead; |
| 449 | struct nvram_partition * tmp_part; |
| 450 | unsigned char c_sum; |
| 451 | char * header; |
| 452 | int total_size; |
| 453 | int err; |
| 454 | |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame^] | 455 | /* Initialize our anchor for the nvram partition list */ |
| 456 | nvram_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL); |
| 457 | if (!nvram_part) { |
| 458 | printk(KERN_ERR "nvram_init: Failed kmalloc\n"); |
| 459 | return -ENOMEM; |
| 460 | } |
| 461 | INIT_LIST_HEAD(&nvram_part->partition); |
| 462 | |
| 463 | if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | return -ENODEV; |
| 465 | total_size = ppc_md.nvram_size(); |
| 466 | |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 467 | header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | if (!header) { |
| 469 | printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n"); |
| 470 | return -ENOMEM; |
| 471 | } |
| 472 | |
| 473 | while (cur_index < total_size) { |
| 474 | |
| 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"); |
| 479 | goto out; |
| 480 | } |
| 481 | |
| 482 | cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */ |
| 483 | |
| 484 | memcpy(&phead, header, NVRAM_HEADER_LEN); |
| 485 | |
| 486 | err = 0; |
| 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"); |
| 493 | goto out; |
| 494 | } |
| 495 | if (!phead.length) { |
| 496 | printk(KERN_WARNING "WARNING: nvram corruption " |
| 497 | "detected: 0-length partition\n"); |
| 498 | goto out; |
| 499 | } |
| 500 | tmp_part = (struct nvram_partition *) |
| 501 | kmalloc(sizeof(struct nvram_partition), GFP_KERNEL); |
| 502 | err = -ENOMEM; |
| 503 | if (!tmp_part) { |
| 504 | printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n"); |
| 505 | goto out; |
| 506 | } |
| 507 | |
| 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); |
| 511 | |
| 512 | cur_index += phead.length * NVRAM_BLOCK_LEN; |
| 513 | } |
| 514 | err = 0; |
| 515 | |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame^] | 516 | #ifdef DEBUG_NVRAM |
| 517 | nvram_print_partitions("NVRAM Partitions"); |
| 518 | #endif |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | out: |
| 521 | kfree(header); |
| 522 | return err; |
| 523 | } |
| 524 | |
| 525 | static int __init nvram_init(void) |
| 526 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | int rc; |
| 528 | |
Benjamin Herrenschmidt | 578914c | 2010-07-29 17:21:17 +1000 | [diff] [blame] | 529 | BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16); |
| 530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0) |
| 532 | return -ENODEV; |
| 533 | |
| 534 | rc = misc_register(&nvram_dev); |
| 535 | if (rc != 0) { |
| 536 | printk(KERN_ERR "nvram_init: failed to register device\n"); |
| 537 | return rc; |
| 538 | } |
| 539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | return rc; |
| 541 | } |
| 542 | |
| 543 | void __exit nvram_cleanup(void) |
| 544 | { |
| 545 | misc_deregister( &nvram_dev ); |
| 546 | } |
| 547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | module_init(nvram_init); |
| 549 | module_exit(nvram_cleanup); |
| 550 | MODULE_LICENSE("GPL"); |