blob: bb12b3248f1310ea955f8eb444c81c36463f19c6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070034
35#undef DEBUG_NVRAM
36
Benjamin Herrenschmidt36673302010-07-29 18:18:44 +100037#define NVRAM_HEADER_LEN sizeof(struct nvram_header)
38#define NVRAM_BLOCK_LEN NVRAM_HEADER_LEN
Benjamin Herrenschmidt74d51d02010-07-29 14:45:24 +100039
40/* If change this size, then change the size of NVNAME_LEN */
41struct nvram_header {
42 unsigned char signature;
43 unsigned char checksum;
44 unsigned short length;
Jim Keniston6024ede2010-11-11 18:54:27 +000045 /* Terminating null required only for names < 12 chars. */
Benjamin Herrenschmidt74d51d02010-07-29 14:45:24 +100046 char name[12];
47};
48
49struct nvram_partition {
50 struct list_head partition;
51 struct nvram_header header;
52 unsigned int index;
53};
54
Jim Keniston690d1a92010-11-11 18:54:22 +000055static LIST_HEAD(nvram_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static 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
80static ssize_t dev_nvram_read(struct file *file, char __user *buf,
81 size_t count, loff_t *ppos)
82{
Arnd Bergmannf9ce2992005-12-09 19:21:44 +010083 ssize_t ret;
84 char *tmp = NULL;
85 ssize_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Arnd Bergmannf9ce2992005-12-09 19:21:44 +010087 ret = -ENODEV;
88 if (!ppc_md.nvram_size)
89 goto out;
90
91 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 size = ppc_md.nvram_size();
Arnd Bergmannf9ce2992005-12-09 19:21:44 +010093 if (*ppos >= size || size < 0)
94 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Arnd Bergmannf9ce2992005-12-09 19:21:44 +010096 count = min_t(size_t, count, size - *ppos);
97 count = min(count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Arnd Bergmannf9ce2992005-12-09 19:21:44 +010099 ret = -ENOMEM;
100 tmp = kmalloc(count, GFP_KERNEL);
101 if (!tmp)
102 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100104 ret = ppc_md.nvram_read(tmp, count, ppos);
105 if (ret <= 0)
106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100108 if (copy_to_user(buf, tmp, ret))
109 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100111out:
112 kfree(tmp);
113 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115}
116
117static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100118 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100120 ssize_t ret;
121 char *tmp = NULL;
122 ssize_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100124 ret = -ENODEV;
125 if (!ppc_md.nvram_size)
126 goto out;
127
128 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 size = ppc_md.nvram_size();
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100130 if (*ppos >= size || size < 0)
131 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100133 count = min_t(size_t, count, size - *ppos);
134 count = min(count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100136 ret = -ENOMEM;
137 tmp = kmalloc(count, GFP_KERNEL);
138 if (!tmp)
139 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100141 ret = -EFAULT;
142 if (copy_from_user(tmp, buf, count))
143 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100145 ret = ppc_md.nvram_write(tmp, count, ppos);
146
147out:
148 kfree(tmp);
149 return ret;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Thomas Gleixner3b03fec2009-10-14 22:42:28 +0000153static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
154 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
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 Herrenschmidte8222502006-03-28 23:15:54 +1100163 if (!machine_is(powermac))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 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 Rothwellaf308372006-03-23 17:38:10 +1100177 default:
178 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800182const struct file_operations nvram_fops = {
Thomas Gleixner3b03fec2009-10-14 22:42:28 +0000183 .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 Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
190static struct miscdevice nvram_dev = {
191 NVRAM_MINOR,
192 "nvram",
193 &nvram_fops
194};
195
196
197#ifdef DEBUG_NVRAM
Thomas Gleixner32c105c2009-10-14 22:54:46 +0000198static void __init nvram_print_partitions(char * label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 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 Keniston690d1a92010-11-11 18:54:22 +0000204 list_for_each_entry(tmp_part, &nvram_partitions, partition) {
Jim Keniston6024ede2010-11-11 18:54:27 +0000205 printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%12s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 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 Gleixner32c105c2009-10-14 22:54:46 +0000214static int __init nvram_write_header(struct nvram_partition * part)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
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 Gleixner32c105c2009-10-14 22:54:46 +0000226static unsigned char __init nvram_checksum(struct nvram_header *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
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
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000240/**
241 * nvram_remove_partition - Remove one or more partitions in nvram
242 * @name: name of the partition to remove, or NULL for a
243 * signature only match
244 * @sig: signature of the partition(s) to remove
245 */
246
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000247int __init nvram_remove_partition(const char *name, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000249 struct nvram_partition *part, *prev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 int rc;
251
Jim Keniston690d1a92010-11-11 18:54:22 +0000252 list_for_each_entry(part, &nvram_partitions, partition) {
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000253 if (part->header.signature != sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 continue;
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000255 if (name && strncmp(name, part->header.name, 12))
256 continue;
257
258 /* Make partition a free partition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 part->header.signature = NVRAM_SIG_FREE;
Jim Keniston6024ede2010-11-11 18:54:27 +0000260 strncpy(part->header.name, "wwwwwwwwwwww", 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 part->header.checksum = nvram_checksum(&part->header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 rc = nvram_write_header(part);
263 if (rc <= 0) {
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000264 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return rc;
266 }
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000269 /* Merge contiguous ones */
270 prev = NULL;
Jim Keniston690d1a92010-11-11 18:54:22 +0000271 list_for_each_entry_safe(part, tmp, &nvram_partitions, partition) {
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000272 if (part->header.signature != NVRAM_SIG_FREE) {
273 prev = NULL;
274 continue;
275 }
276 if (prev) {
277 prev->header.length += part->header.length;
278 prev->header.checksum = nvram_checksum(&part->header);
279 rc = nvram_write_header(part);
280 if (rc <= 0) {
281 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
282 return rc;
283 }
284 list_del(&part->partition);
285 kfree(part);
286 } else
287 prev = part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
290 return 0;
291}
292
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000293/**
294 * nvram_create_partition - Create a partition in nvram
295 * @name: name of the partition to create
296 * @sig: signature of the partition to create
Benjamin Herrenschmidt36673302010-07-29 18:18:44 +1000297 * @req_size: size of data to allocate in bytes
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000298 * @min_size: minimum acceptable size (0 means req_size)
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000299 *
300 * Returns a negative error code or a positive nvram index
301 * of the beginning of the data area of the newly created
302 * partition. If you provided a min_size smaller than req_size
303 * you need to query for the actual size yourself after the
304 * call using nvram_partition_get_size().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 */
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000306loff_t __init nvram_create_partition(const char *name, int sig,
307 int req_size, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Arnd Bergmanna341ad92005-06-28 20:33:49 +1000309 struct nvram_partition *part;
310 struct nvram_partition *new_part;
akpm@osdl.org0339ad72005-05-01 08:58:44 -0700311 struct nvram_partition *free_part = NULL;
Benjamin Herrenschmidtcef0d5a2010-07-29 17:22:34 +1000312 static char nv_init_vals[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 loff_t tmp_index;
314 long size = 0;
315 int rc;
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000316
Benjamin Herrenschmidt36673302010-07-29 18:18:44 +1000317 /* Convert sizes from bytes to blocks */
318 req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
319 min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
320
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000321 /* If no minimum size specified, make it the same as the
322 * requested size
323 */
324 if (min_size == 0)
325 min_size = req_size;
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000326 if (min_size > req_size)
327 return -EINVAL;
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000328
Benjamin Herrenschmidt36673302010-07-29 18:18:44 +1000329 /* Now add one block to each for the header */
330 req_size += 1;
331 min_size += 1;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* Find a free partition that will give us the maximum needed size
334 If can't find one that will give us the minimum size needed */
Jim Keniston690d1a92010-11-11 18:54:22 +0000335 list_for_each_entry(part, &nvram_partitions, partition) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (part->header.signature != NVRAM_SIG_FREE)
337 continue;
338
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000339 if (part->header.length >= req_size) {
340 size = req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 free_part = part;
342 break;
343 }
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000344 if (part->header.length > size &&
345 part->header.length >= min_size) {
346 size = part->header.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 free_part = part;
348 }
349 }
akpm@osdl.org0339ad72005-05-01 08:58:44 -0700350 if (!size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /* Create our OS partition */
akpm@osdl.org0339ad72005-05-01 08:58:44 -0700354 new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (!new_part) {
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000356 pr_err("nvram_create_os_partition: kmalloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -ENOMEM;
358 }
359
360 new_part->index = free_part->index;
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000361 new_part->header.signature = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 new_part->header.length = size;
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000363 strncpy(new_part->header.name, name, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 new_part->header.checksum = nvram_checksum(&new_part->header);
365
366 rc = nvram_write_header(new_part);
367 if (rc <= 0) {
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000368 pr_err("nvram_create_os_partition: nvram_write_header "
369 "failed (%d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return rc;
371 }
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000372 list_add_tail(&new_part->partition, &free_part->partition);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000374 /* Adjust or remove the partition we stole the space from */
375 if (free_part->header.length > size) {
376 free_part->index += size * NVRAM_BLOCK_LEN;
377 free_part->header.length -= size;
378 free_part->header.checksum = nvram_checksum(&free_part->header);
379 rc = nvram_write_header(free_part);
380 if (rc <= 0) {
381 pr_err("nvram_create_os_partition: nvram_write_header "
382 "failed (%d)\n", rc);
383 return rc;
384 }
385 } else {
386 list_del(&free_part->partition);
387 kfree(free_part);
388 }
389
390 /* Clear the new partition */
Benjamin Herrenschmidtcef0d5a2010-07-29 17:22:34 +1000391 for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
392 tmp_index < ((size - 1) * NVRAM_BLOCK_LEN);
393 tmp_index += NVRAM_BLOCK_LEN) {
394 rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
395 if (rc <= 0) {
396 pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc);
397 return rc;
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000401 return new_part->index + NVRAM_HEADER_LEN;
402}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000404/**
405 * nvram_get_partition_size - Get the data size of an nvram partition
406 * @data_index: This is the offset of the start of the data of
407 * the partition. The same value that is returned by
408 * nvram_create_partition().
409 */
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000410int nvram_get_partition_size(loff_t data_index)
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000411{
412 struct nvram_partition *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Jim Keniston690d1a92010-11-11 18:54:22 +0000414 list_for_each_entry(part, &nvram_partitions, partition) {
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000415 if (part->index + NVRAM_HEADER_LEN == data_index)
416 return (part->header.length - 1) * NVRAM_BLOCK_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000418 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421
Benjamin Herrenschmidtcf5cbf92010-08-02 10:01:58 +1000422/**
423 * nvram_find_partition - Find an nvram partition by signature and name
424 * @name: Name of the partition or NULL for any name
425 * @sig: Signature to test against
426 * @out_size: if non-NULL, returns the size of the data part of the partition
427 */
428loff_t nvram_find_partition(const char *name, int sig, int *out_size)
429{
430 struct nvram_partition *p;
431
Jim Keniston690d1a92010-11-11 18:54:22 +0000432 list_for_each_entry(p, &nvram_partitions, partition) {
Benjamin Herrenschmidtcf5cbf92010-08-02 10:01:58 +1000433 if (p->header.signature == sig &&
434 (!name || !strncmp(p->header.name, name, 12))) {
435 if (out_size)
436 *out_size = (p->header.length - 1) *
437 NVRAM_BLOCK_LEN;
438 return p->index + NVRAM_HEADER_LEN;
439 }
440 }
441 return 0;
442}
443
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000444int __init nvram_scan_partitions(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 loff_t cur_index = 0;
447 struct nvram_header phead;
448 struct nvram_partition * tmp_part;
449 unsigned char c_sum;
450 char * header;
451 int total_size;
452 int err;
453
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000454 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return -ENODEV;
456 total_size = ppc_md.nvram_size();
457
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800458 header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (!header) {
460 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
461 return -ENOMEM;
462 }
463
464 while (cur_index < total_size) {
465
466 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
467 if (err != NVRAM_HEADER_LEN) {
468 printk(KERN_ERR "nvram_scan_partitions: Error parsing "
469 "nvram partitions\n");
470 goto out;
471 }
472
473 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
474
475 memcpy(&phead, header, NVRAM_HEADER_LEN);
476
477 err = 0;
478 c_sum = nvram_checksum(&phead);
479 if (c_sum != phead.checksum) {
480 printk(KERN_WARNING "WARNING: nvram partition checksum"
481 " was %02x, should be %02x!\n",
482 phead.checksum, c_sum);
483 printk(KERN_WARNING "Terminating nvram partition scan\n");
484 goto out;
485 }
486 if (!phead.length) {
487 printk(KERN_WARNING "WARNING: nvram corruption "
488 "detected: 0-length partition\n");
489 goto out;
490 }
491 tmp_part = (struct nvram_partition *)
492 kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
493 err = -ENOMEM;
494 if (!tmp_part) {
495 printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
496 goto out;
497 }
498
499 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
500 tmp_part->index = cur_index;
Jim Keniston690d1a92010-11-11 18:54:22 +0000501 list_add_tail(&tmp_part->partition, &nvram_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 cur_index += phead.length * NVRAM_BLOCK_LEN;
504 }
505 err = 0;
506
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000507#ifdef DEBUG_NVRAM
508 nvram_print_partitions("NVRAM Partitions");
509#endif
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 out:
512 kfree(header);
513 return err;
514}
515
516static int __init nvram_init(void)
517{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 int rc;
519
Benjamin Herrenschmidt578914c2010-07-29 17:21:17 +1000520 BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
523 return -ENODEV;
524
525 rc = misc_register(&nvram_dev);
526 if (rc != 0) {
527 printk(KERN_ERR "nvram_init: failed to register device\n");
528 return rc;
529 }
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return rc;
532}
533
534void __exit nvram_cleanup(void)
535{
536 misc_deregister( &nvram_dev );
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539module_init(nvram_init);
540module_exit(nvram_cleanup);
541MODULE_LICENSE("GPL");