blob: b8a50fa5875bd0f9b63c86adc154710d3fe635cd [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;
45 char name[12];
46};
47
48struct nvram_partition {
49 struct list_head partition;
50 struct nvram_header header;
51 unsigned int index;
52};
53
Jim Keniston690d1a92010-11-11 18:54:22 +000054static LIST_HEAD(nvram_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56static 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
79static ssize_t dev_nvram_read(struct file *file, char __user *buf,
80 size_t count, loff_t *ppos)
81{
Arnd Bergmannf9ce2992005-12-09 19:21:44 +010082 ssize_t ret;
83 char *tmp = NULL;
84 ssize_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Arnd Bergmannf9ce2992005-12-09 19:21:44 +010086 ret = -ENODEV;
87 if (!ppc_md.nvram_size)
88 goto out;
89
90 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 size = ppc_md.nvram_size();
Arnd Bergmannf9ce2992005-12-09 19:21:44 +010092 if (*ppos >= size || size < 0)
93 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Arnd Bergmannf9ce2992005-12-09 19:21:44 +010095 count = min_t(size_t, count, size - *ppos);
96 count = min(count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Arnd Bergmannf9ce2992005-12-09 19:21:44 +010098 ret = -ENOMEM;
99 tmp = kmalloc(count, GFP_KERNEL);
100 if (!tmp)
101 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100103 ret = ppc_md.nvram_read(tmp, count, ppos);
104 if (ret <= 0)
105 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100107 if (copy_to_user(buf, tmp, ret))
108 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100110out:
111 kfree(tmp);
112 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114}
115
116static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100117 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100119 ssize_t ret;
120 char *tmp = NULL;
121 ssize_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100123 ret = -ENODEV;
124 if (!ppc_md.nvram_size)
125 goto out;
126
127 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 size = ppc_md.nvram_size();
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100129 if (*ppos >= size || size < 0)
130 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100132 count = min_t(size_t, count, size - *ppos);
133 count = min(count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100135 ret = -ENOMEM;
136 tmp = kmalloc(count, GFP_KERNEL);
137 if (!tmp)
138 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100140 ret = -EFAULT;
141 if (copy_from_user(tmp, buf, count))
142 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100144 ret = ppc_md.nvram_write(tmp, count, ppos);
145
146out:
147 kfree(tmp);
148 return ret;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Thomas Gleixner3b03fec2009-10-14 22:42:28 +0000152static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
153 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
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 Herrenschmidte8222502006-03-28 23:15:54 +1100162 if (!machine_is(powermac))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 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 Rothwellaf308372006-03-23 17:38:10 +1100176 default:
177 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800181const struct file_operations nvram_fops = {
Thomas Gleixner3b03fec2009-10-14 22:42:28 +0000182 .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 Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
189static struct miscdevice nvram_dev = {
190 NVRAM_MINOR,
191 "nvram",
192 &nvram_fops
193};
194
195
196#ifdef DEBUG_NVRAM
Thomas Gleixner32c105c2009-10-14 22:54:46 +0000197static void __init nvram_print_partitions(char * label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct nvram_partition * tmp_part;
200
201 printk(KERN_WARNING "--------%s---------\n", label);
202 printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
Jim Keniston690d1a92010-11-11 18:54:22 +0000203 list_for_each_entry(tmp_part, &nvram_partitions, partition) {
Will Schmidt5a43ee62006-04-26 11:09:46 -0500204 printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 tmp_part->index, tmp_part->header.signature,
206 tmp_part->header.checksum, tmp_part->header.length,
207 tmp_part->header.name);
208 }
209}
210#endif
211
212
Thomas Gleixner32c105c2009-10-14 22:54:46 +0000213static int __init nvram_write_header(struct nvram_partition * part)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 loff_t tmp_index;
216 int rc;
217
218 tmp_index = part->index;
219 rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
220
221 return rc;
222}
223
224
Thomas Gleixner32c105c2009-10-14 22:54:46 +0000225static unsigned char __init nvram_checksum(struct nvram_header *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 unsigned int c_sum, c_sum2;
228 unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
229 c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
230
231 /* The sum may have spilled into the 3rd byte. Fold it back. */
232 c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
233 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
234 c_sum2 = (c_sum >> 8) + (c_sum << 8);
235 c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
236 return c_sum;
237}
238
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000239/**
240 * nvram_remove_partition - Remove one or more partitions in nvram
241 * @name: name of the partition to remove, or NULL for a
242 * signature only match
243 * @sig: signature of the partition(s) to remove
244 */
245
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000246int __init nvram_remove_partition(const char *name, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000248 struct nvram_partition *part, *prev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 int rc;
250
Jim Keniston690d1a92010-11-11 18:54:22 +0000251 list_for_each_entry(part, &nvram_partitions, partition) {
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000252 if (part->header.signature != sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 continue;
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000254 if (name && strncmp(name, part->header.name, 12))
255 continue;
256
257 /* Make partition a free partition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 part->header.signature = NVRAM_SIG_FREE;
259 sprintf(part->header.name, "wwwwwwwwwwww");
260 part->header.checksum = nvram_checksum(&part->header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 rc = nvram_write_header(part);
262 if (rc <= 0) {
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000263 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return rc;
265 }
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000268 /* Merge contiguous ones */
269 prev = NULL;
Jim Keniston690d1a92010-11-11 18:54:22 +0000270 list_for_each_entry_safe(part, tmp, &nvram_partitions, partition) {
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000271 if (part->header.signature != NVRAM_SIG_FREE) {
272 prev = NULL;
273 continue;
274 }
275 if (prev) {
276 prev->header.length += part->header.length;
277 prev->header.checksum = nvram_checksum(&part->header);
278 rc = nvram_write_header(part);
279 if (rc <= 0) {
280 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
281 return rc;
282 }
283 list_del(&part->partition);
284 kfree(part);
285 } else
286 prev = part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
289 return 0;
290}
291
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000292/**
293 * nvram_create_partition - Create a partition in nvram
294 * @name: name of the partition to create
295 * @sig: signature of the partition to create
Benjamin Herrenschmidt36673302010-07-29 18:18:44 +1000296 * @req_size: size of data to allocate in bytes
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000297 * @min_size: minimum acceptable size (0 means req_size)
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000298 *
299 * Returns a negative error code or a positive nvram index
300 * of the beginning of the data area of the newly created
301 * partition. If you provided a min_size smaller than req_size
302 * you need to query for the actual size yourself after the
303 * call using nvram_partition_get_size().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000305loff_t __init nvram_create_partition(const char *name, int sig,
306 int req_size, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Arnd Bergmanna341ad92005-06-28 20:33:49 +1000308 struct nvram_partition *part;
309 struct nvram_partition *new_part;
akpm@osdl.org0339ad72005-05-01 08:58:44 -0700310 struct nvram_partition *free_part = NULL;
Benjamin Herrenschmidtcef0d5a2010-07-29 17:22:34 +1000311 static char nv_init_vals[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 loff_t tmp_index;
313 long size = 0;
314 int rc;
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000315
Benjamin Herrenschmidt36673302010-07-29 18:18:44 +1000316 /* Convert sizes from bytes to blocks */
317 req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
318 min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
319
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000320 /* If no minimum size specified, make it the same as the
321 * requested size
322 */
323 if (min_size == 0)
324 min_size = req_size;
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000325 if (min_size > req_size)
326 return -EINVAL;
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000327
Benjamin Herrenschmidt36673302010-07-29 18:18:44 +1000328 /* Now add one block to each for the header */
329 req_size += 1;
330 min_size += 1;
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /* Find a free partition that will give us the maximum needed size
333 If can't find one that will give us the minimum size needed */
Jim Keniston690d1a92010-11-11 18:54:22 +0000334 list_for_each_entry(part, &nvram_partitions, partition) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (part->header.signature != NVRAM_SIG_FREE)
336 continue;
337
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000338 if (part->header.length >= req_size) {
339 size = req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 free_part = part;
341 break;
342 }
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000343 if (part->header.length > size &&
344 part->header.length >= min_size) {
345 size = part->header.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 free_part = part;
347 }
348 }
akpm@osdl.org0339ad72005-05-01 08:58:44 -0700349 if (!size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 /* Create our OS partition */
akpm@osdl.org0339ad72005-05-01 08:58:44 -0700353 new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!new_part) {
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000355 pr_err("nvram_create_os_partition: kmalloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return -ENOMEM;
357 }
358
359 new_part->index = free_part->index;
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000360 new_part->header.signature = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 new_part->header.length = size;
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000362 strncpy(new_part->header.name, name, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 new_part->header.checksum = nvram_checksum(&new_part->header);
364
365 rc = nvram_write_header(new_part);
366 if (rc <= 0) {
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000367 pr_err("nvram_create_os_partition: nvram_write_header "
368 "failed (%d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return rc;
370 }
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000371 list_add_tail(&new_part->partition, &free_part->partition);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000373 /* Adjust or remove the partition we stole the space from */
374 if (free_part->header.length > size) {
375 free_part->index += size * NVRAM_BLOCK_LEN;
376 free_part->header.length -= size;
377 free_part->header.checksum = nvram_checksum(&free_part->header);
378 rc = nvram_write_header(free_part);
379 if (rc <= 0) {
380 pr_err("nvram_create_os_partition: nvram_write_header "
381 "failed (%d)\n", rc);
382 return rc;
383 }
384 } else {
385 list_del(&free_part->partition);
386 kfree(free_part);
387 }
388
389 /* Clear the new partition */
Benjamin Herrenschmidtcef0d5a2010-07-29 17:22:34 +1000390 for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
391 tmp_index < ((size - 1) * NVRAM_BLOCK_LEN);
392 tmp_index += NVRAM_BLOCK_LEN) {
393 rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
394 if (rc <= 0) {
395 pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc);
396 return rc;
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000400 return new_part->index + NVRAM_HEADER_LEN;
401}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000403/**
404 * nvram_get_partition_size - Get the data size of an nvram partition
405 * @data_index: This is the offset of the start of the data of
406 * the partition. The same value that is returned by
407 * nvram_create_partition().
408 */
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000409int nvram_get_partition_size(loff_t data_index)
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000410{
411 struct nvram_partition *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Jim Keniston690d1a92010-11-11 18:54:22 +0000413 list_for_each_entry(part, &nvram_partitions, partition) {
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000414 if (part->index + NVRAM_HEADER_LEN == data_index)
415 return (part->header.length - 1) * NVRAM_BLOCK_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000417 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420
Benjamin Herrenschmidtcf5cbf92010-08-02 10:01:58 +1000421/**
422 * nvram_find_partition - Find an nvram partition by signature and name
423 * @name: Name of the partition or NULL for any name
424 * @sig: Signature to test against
425 * @out_size: if non-NULL, returns the size of the data part of the partition
426 */
427loff_t nvram_find_partition(const char *name, int sig, int *out_size)
428{
429 struct nvram_partition *p;
430
Jim Keniston690d1a92010-11-11 18:54:22 +0000431 list_for_each_entry(p, &nvram_partitions, partition) {
Benjamin Herrenschmidtcf5cbf92010-08-02 10:01:58 +1000432 if (p->header.signature == sig &&
433 (!name || !strncmp(p->header.name, name, 12))) {
434 if (out_size)
435 *out_size = (p->header.length - 1) *
436 NVRAM_BLOCK_LEN;
437 return p->index + NVRAM_HEADER_LEN;
438 }
439 }
440 return 0;
441}
442
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000443int __init nvram_scan_partitions(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 loff_t cur_index = 0;
446 struct nvram_header phead;
447 struct nvram_partition * tmp_part;
448 unsigned char c_sum;
449 char * header;
450 int total_size;
451 int err;
452
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000453 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return -ENODEV;
455 total_size = ppc_md.nvram_size();
456
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800457 header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (!header) {
459 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
460 return -ENOMEM;
461 }
462
463 while (cur_index < total_size) {
464
465 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
466 if (err != NVRAM_HEADER_LEN) {
467 printk(KERN_ERR "nvram_scan_partitions: Error parsing "
468 "nvram partitions\n");
469 goto out;
470 }
471
472 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
473
474 memcpy(&phead, header, NVRAM_HEADER_LEN);
475
476 err = 0;
477 c_sum = nvram_checksum(&phead);
478 if (c_sum != phead.checksum) {
479 printk(KERN_WARNING "WARNING: nvram partition checksum"
480 " was %02x, should be %02x!\n",
481 phead.checksum, c_sum);
482 printk(KERN_WARNING "Terminating nvram partition scan\n");
483 goto out;
484 }
485 if (!phead.length) {
486 printk(KERN_WARNING "WARNING: nvram corruption "
487 "detected: 0-length partition\n");
488 goto out;
489 }
490 tmp_part = (struct nvram_partition *)
491 kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
492 err = -ENOMEM;
493 if (!tmp_part) {
494 printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
495 goto out;
496 }
497
498 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
499 tmp_part->index = cur_index;
Jim Keniston690d1a92010-11-11 18:54:22 +0000500 list_add_tail(&tmp_part->partition, &nvram_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 cur_index += phead.length * NVRAM_BLOCK_LEN;
503 }
504 err = 0;
505
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000506#ifdef DEBUG_NVRAM
507 nvram_print_partitions("NVRAM Partitions");
508#endif
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 out:
511 kfree(header);
512 return err;
513}
514
515static int __init nvram_init(void)
516{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 int rc;
518
Benjamin Herrenschmidt578914c2010-07-29 17:21:17 +1000519 BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
522 return -ENODEV;
523
524 rc = misc_register(&nvram_dev);
525 if (rc != 0) {
526 printk(KERN_ERR "nvram_init: failed to register device\n");
527 return rc;
528 }
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return rc;
531}
532
533void __exit nvram_cleanup(void)
534{
535 misc_deregister( &nvram_dev );
536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538module_init(nvram_init);
539module_exit(nvram_cleanup);
540MODULE_LICENSE("GPL");