blob: 125d86cf0afc68f1e1689b34cb9bb8e436027791 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static struct nvram_partition * nvram_part;
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{
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 Schmidt5a43ee62006-04-26 11:09:46 -0500206 printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 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 Gleixner32c105c2009-10-14 22:54:46 +0000215static int __init nvram_write_header(struct nvram_partition * part)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
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 Gleixner32c105c2009-10-14 22:54:46 +0000227static unsigned char __init nvram_checksum(struct nvram_header *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
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 Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000241/**
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 Herrenschmidtedc79a22010-08-02 11:18:09 +1000248int __init nvram_remove_partition(const char *name, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000250 struct nvram_partition *part, *prev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 int rc;
252
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000253 list_for_each_entry(part, &nvram_part->partition, partition) {
254 if (part->header.signature != sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 continue;
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000256 if (name && strncmp(name, part->header.name, 12))
257 continue;
258
259 /* Make partition a free partition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 part->header.signature = NVRAM_SIG_FREE;
261 sprintf(part->header.name, "wwwwwwwwwwww");
262 part->header.checksum = nvram_checksum(&part->header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 rc = nvram_write_header(part);
264 if (rc <= 0) {
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000265 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return rc;
267 }
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000270 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
291 return 0;
292}
293
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000294/**
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 Herrenschmidt36673302010-07-29 18:18:44 +1000298 * @req_size: size of data to allocate in bytes
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000299 * @min_size: minimum acceptable size (0 means req_size)
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000300 *
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 Torvalds1da177e2005-04-16 15:20:36 -0700306 */
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000307loff_t __init nvram_create_partition(const char *name, int sig,
308 int req_size, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Arnd Bergmanna341ad92005-06-28 20:33:49 +1000310 struct nvram_partition *part;
311 struct nvram_partition *new_part;
akpm@osdl.org0339ad72005-05-01 08:58:44 -0700312 struct nvram_partition *free_part = NULL;
Benjamin Herrenschmidtcef0d5a2010-07-29 17:22:34 +1000313 static char nv_init_vals[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 loff_t tmp_index;
315 long size = 0;
316 int rc;
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000317
Benjamin Herrenschmidt36673302010-07-29 18:18:44 +1000318 /* 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 Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000322 /* 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 Herrenschmidte49e2e82010-07-29 17:38:55 +1000327 if (min_size > req_size)
328 return -EINVAL;
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000329
Benjamin Herrenschmidt36673302010-07-29 18:18:44 +1000330 /* Now add one block to each for the header */
331 req_size += 1;
332 min_size += 1;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /* 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 Bergmanna341ad92005-06-28 20:33:49 +1000336 list_for_each_entry(part, &nvram_part->partition, partition) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (part->header.signature != NVRAM_SIG_FREE)
338 continue;
339
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000340 if (part->header.length >= req_size) {
341 size = req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 free_part = part;
343 break;
344 }
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000345 if (part->header.length > size &&
346 part->header.length >= min_size) {
347 size = part->header.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 free_part = part;
349 }
350 }
akpm@osdl.org0339ad72005-05-01 08:58:44 -0700351 if (!size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* Create our OS partition */
akpm@osdl.org0339ad72005-05-01 08:58:44 -0700355 new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!new_part) {
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000357 pr_err("nvram_create_os_partition: kmalloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return -ENOMEM;
359 }
360
361 new_part->index = free_part->index;
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000362 new_part->header.signature = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 new_part->header.length = size;
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000364 strncpy(new_part->header.name, name, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 new_part->header.checksum = nvram_checksum(&new_part->header);
366
367 rc = nvram_write_header(new_part);
368 if (rc <= 0) {
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000369 pr_err("nvram_create_os_partition: nvram_write_header "
370 "failed (%d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return rc;
372 }
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000373 list_add_tail(&new_part->partition, &free_part->partition);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000375 /* 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 Herrenschmidtcef0d5a2010-07-29 17:22:34 +1000392 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 Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000402 return new_part->index + NVRAM_HEADER_LEN;
403}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000405/**
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 Herrenschmidtedc79a22010-08-02 11:18:09 +1000411int nvram_get_partition_size(loff_t data_index)
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000412{
413 struct nvram_partition *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000415 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 Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000419 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422
Benjamin Herrenschmidtcf5cbf92010-08-02 10:01:58 +1000423/**
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 */
429loff_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 Herrenschmidtedc79a22010-08-02 11:18:09 +1000445int __init nvram_scan_partitions(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
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 Herrenschmidtedc79a22010-08-02 11:18:09 +1000455 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700464 return -ENODEV;
465 total_size = ppc_md.nvram_size();
466
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800467 header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 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 Herrenschmidtedc79a22010-08-02 11:18:09 +1000516#ifdef DEBUG_NVRAM
517 nvram_print_partitions("NVRAM Partitions");
518#endif
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 out:
521 kfree(header);
522 return err;
523}
524
525static int __init nvram_init(void)
526{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 int rc;
528
Benjamin Herrenschmidt578914c2010-07-29 17:21:17 +1000529 BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 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 Torvalds1da177e2005-04-16 15:20:36 -0700540 return rc;
541}
542
543void __exit nvram_cleanup(void)
544{
545 misc_deregister( &nvram_dev );
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548module_init(nvram_init);
549module_exit(nvram_cleanup);
550MODULE_LICENSE("GPL");