1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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>
34
35#undef DEBUG_NVRAM
36
37#define NVRAM_HEADER_LEN sizeof(struct nvram_header)
38#define NVRAM_BLOCK_LEN NVRAM_HEADER_LEN
39
40
41struct nvram_header {
42 unsigned char signature;
43 unsigned char checksum;
44 unsigned short length;
45
46 char name[12];
47};
48
49struct nvram_partition {
50 struct list_head partition;
51 struct nvram_header header;
52 unsigned int index;
53};
54
55static LIST_HEAD(nvram_partitions);
56
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{
83 ssize_t ret;
84 char *tmp = NULL;
85 ssize_t size;
86
87 if (!ppc_md.nvram_size) {
88 ret = -ENODEV;
89 goto out;
90 }
91
92 size = ppc_md.nvram_size();
93 if (size < 0) {
94 ret = size;
95 goto out;
96 }
97
98 if (*ppos >= size) {
99 ret = 0;
100 goto out;
101 }
102
103 count = min_t(size_t, count, size - *ppos);
104 count = min(count, PAGE_SIZE);
105
106 tmp = kmalloc(count, GFP_KERNEL);
107 if (!tmp) {
108 ret = -ENOMEM;
109 goto out;
110 }
111
112 ret = ppc_md.nvram_read(tmp, count, ppos);
113 if (ret <= 0)
114 goto out;
115
116 if (copy_to_user(buf, tmp, ret))
117 ret = -EFAULT;
118
119out:
120 kfree(tmp);
121 return ret;
122
123}
124
125static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
126 size_t count, loff_t *ppos)
127{
128 ssize_t ret;
129 char *tmp = NULL;
130 ssize_t size;
131
132 ret = -ENODEV;
133 if (!ppc_md.nvram_size)
134 goto out;
135
136 ret = 0;
137 size = ppc_md.nvram_size();
138 if (*ppos >= size || size < 0)
139 goto out;
140
141 count = min_t(size_t, count, size - *ppos);
142 count = min(count, PAGE_SIZE);
143
144 ret = -ENOMEM;
145 tmp = kmalloc(count, GFP_KERNEL);
146 if (!tmp)
147 goto out;
148
149 ret = -EFAULT;
150 if (copy_from_user(tmp, buf, count))
151 goto out;
152
153 ret = ppc_md.nvram_write(tmp, count, ppos);
154
155out:
156 kfree(tmp);
157 return ret;
158
159}
160
161static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
162 unsigned long arg)
163{
164 switch(cmd) {
165#ifdef CONFIG_PPC_PMAC
166 case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
167 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
168 case IOC_NVRAM_GET_OFFSET: {
169 int part, offset;
170
171 if (!machine_is(powermac))
172 return -EINVAL;
173 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
174 return -EFAULT;
175 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
176 return -EINVAL;
177 offset = pmac_get_partition(part);
178 if (offset < 0)
179 return offset;
180 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
181 return -EFAULT;
182 return 0;
183 }
184#endif
185 default:
186 return -EINVAL;
187 }
188}
189
190const struct file_operations nvram_fops = {
191 .owner = THIS_MODULE,
192 .llseek = dev_nvram_llseek,
193 .read = dev_nvram_read,
194 .write = dev_nvram_write,
195 .unlocked_ioctl = dev_nvram_ioctl,
196};
197
198static struct miscdevice nvram_dev = {
199 NVRAM_MINOR,
200 "nvram",
201 &nvram_fops
202};
203
204
205#ifdef DEBUG_NVRAM
206static void __init nvram_print_partitions(char * label)
207{
208 struct nvram_partition * tmp_part;
209
210 printk(KERN_WARNING "--------%s---------\n", label);
211 printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
212 list_for_each_entry(tmp_part, &nvram_partitions, partition) {
213 printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%12s\n",
214 tmp_part->index, tmp_part->header.signature,
215 tmp_part->header.checksum, tmp_part->header.length,
216 tmp_part->header.name);
217 }
218}
219#endif
220
221
222static int __init nvram_write_header(struct nvram_partition * part)
223{
224 loff_t tmp_index;
225 int rc;
226
227 tmp_index = part->index;
228 rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
229
230 return rc;
231}
232
233
234static unsigned char __init nvram_checksum(struct nvram_header *p)
235{
236 unsigned int c_sum, c_sum2;
237 unsigned short *sp = (unsigned short *)p->name;
238 c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
239
240
241 c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
242
243 c_sum2 = (c_sum >> 8) + (c_sum << 8);
244 c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
245 return c_sum;
246}
247
248
249
250
251
252static int nvram_can_remove_partition(struct nvram_partition *part,
253 const char *name, int sig, const char *exceptions[])
254{
255 if (part->header.signature != sig)
256 return 0;
257 if (name) {
258 if (strncmp(name, part->header.name, 12))
259 return 0;
260 } else if (exceptions) {
261 const char **except;
262 for (except = exceptions; *except; except++) {
263 if (!strncmp(*except, part->header.name, 12))
264 return 0;
265 }
266 }
267 return 1;
268}
269
270
271
272
273
274
275
276
277
278
279int __init nvram_remove_partition(const char *name, int sig,
280 const char *exceptions[])
281{
282 struct nvram_partition *part, *prev, *tmp;
283 int rc;
284
285 list_for_each_entry(part, &nvram_partitions, partition) {
286 if (!nvram_can_remove_partition(part, name, sig, exceptions))
287 continue;
288
289
290 part->header.signature = NVRAM_SIG_FREE;
291 strncpy(part->header.name, "wwwwwwwwwwww", 12);
292 part->header.checksum = nvram_checksum(&part->header);
293 rc = nvram_write_header(part);
294 if (rc <= 0) {
295 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
296 return rc;
297 }
298 }
299
300
301 prev = NULL;
302 list_for_each_entry_safe(part, tmp, &nvram_partitions, partition) {
303 if (part->header.signature != NVRAM_SIG_FREE) {
304 prev = NULL;
305 continue;
306 }
307 if (prev) {
308 prev->header.length += part->header.length;
309 prev->header.checksum = nvram_checksum(&part->header);
310 rc = nvram_write_header(part);
311 if (rc <= 0) {
312 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
313 return rc;
314 }
315 list_del(&part->partition);
316 kfree(part);
317 } else
318 prev = part;
319 }
320
321 return 0;
322}
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337loff_t __init nvram_create_partition(const char *name, int sig,
338 int req_size, int min_size)
339{
340 struct nvram_partition *part;
341 struct nvram_partition *new_part;
342 struct nvram_partition *free_part = NULL;
343 static char nv_init_vals[16];
344 loff_t tmp_index;
345 long size = 0;
346 int rc;
347
348
349 req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
350 min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
351
352
353
354
355 if (min_size == 0)
356 min_size = req_size;
357 if (min_size > req_size)
358 return -EINVAL;
359
360
361 req_size += 1;
362 min_size += 1;
363
364
365
366 list_for_each_entry(part, &nvram_partitions, partition) {
367 if (part->header.signature != NVRAM_SIG_FREE)
368 continue;
369
370 if (part->header.length >= req_size) {
371 size = req_size;
372 free_part = part;
373 break;
374 }
375 if (part->header.length > size &&
376 part->header.length >= min_size) {
377 size = part->header.length;
378 free_part = part;
379 }
380 }
381 if (!size)
382 return -ENOSPC;
383
384
385 new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
386 if (!new_part) {
387 pr_err("nvram_create_os_partition: kmalloc failed\n");
388 return -ENOMEM;
389 }
390
391 new_part->index = free_part->index;
392 new_part->header.signature = sig;
393 new_part->header.length = size;
394 strncpy(new_part->header.name, name, 12);
395 new_part->header.checksum = nvram_checksum(&new_part->header);
396
397 rc = nvram_write_header(new_part);
398 if (rc <= 0) {
399 pr_err("nvram_create_os_partition: nvram_write_header "
400 "failed (%d)\n", rc);
401 return rc;
402 }
403 list_add_tail(&new_part->partition, &free_part->partition);
404
405
406 if (free_part->header.length > size) {
407 free_part->index += size * NVRAM_BLOCK_LEN;
408 free_part->header.length -= size;
409 free_part->header.checksum = nvram_checksum(&free_part->header);
410 rc = nvram_write_header(free_part);
411 if (rc <= 0) {
412 pr_err("nvram_create_os_partition: nvram_write_header "
413 "failed (%d)\n", rc);
414 return rc;
415 }
416 } else {
417 list_del(&free_part->partition);
418 kfree(free_part);
419 }
420
421
422 for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
423 tmp_index < ((size - 1) * NVRAM_BLOCK_LEN);
424 tmp_index += NVRAM_BLOCK_LEN) {
425 rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
426 if (rc <= 0) {
427 pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc);
428 return rc;
429 }
430 }
431
432 return new_part->index + NVRAM_HEADER_LEN;
433}
434
435
436
437
438
439
440
441int nvram_get_partition_size(loff_t data_index)
442{
443 struct nvram_partition *part;
444
445 list_for_each_entry(part, &nvram_partitions, partition) {
446 if (part->index + NVRAM_HEADER_LEN == data_index)
447 return (part->header.length - 1) * NVRAM_BLOCK_LEN;
448 }
449 return -1;
450}
451
452
453
454
455
456
457
458
459loff_t nvram_find_partition(const char *name, int sig, int *out_size)
460{
461 struct nvram_partition *p;
462
463 list_for_each_entry(p, &nvram_partitions, partition) {
464 if (p->header.signature == sig &&
465 (!name || !strncmp(p->header.name, name, 12))) {
466 if (out_size)
467 *out_size = (p->header.length - 1) *
468 NVRAM_BLOCK_LEN;
469 return p->index + NVRAM_HEADER_LEN;
470 }
471 }
472 return 0;
473}
474
475int __init nvram_scan_partitions(void)
476{
477 loff_t cur_index = 0;
478 struct nvram_header phead;
479 struct nvram_partition * tmp_part;
480 unsigned char c_sum;
481 char * header;
482 int total_size;
483 int err;
484
485 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
486 return -ENODEV;
487 total_size = ppc_md.nvram_size();
488
489 header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
490 if (!header) {
491 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
492 return -ENOMEM;
493 }
494
495 while (cur_index < total_size) {
496
497 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
498 if (err != NVRAM_HEADER_LEN) {
499 printk(KERN_ERR "nvram_scan_partitions: Error parsing "
500 "nvram partitions\n");
501 goto out;
502 }
503
504 cur_index -= NVRAM_HEADER_LEN;
505
506 memcpy(&phead, header, NVRAM_HEADER_LEN);
507
508 err = 0;
509 c_sum = nvram_checksum(&phead);
510 if (c_sum != phead.checksum) {
511 printk(KERN_WARNING "WARNING: nvram partition checksum"
512 " was %02x, should be %02x!\n",
513 phead.checksum, c_sum);
514 printk(KERN_WARNING "Terminating nvram partition scan\n");
515 goto out;
516 }
517 if (!phead.length) {
518 printk(KERN_WARNING "WARNING: nvram corruption "
519 "detected: 0-length partition\n");
520 goto out;
521 }
522 tmp_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
523 err = -ENOMEM;
524 if (!tmp_part) {
525 printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
526 goto out;
527 }
528
529 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
530 tmp_part->index = cur_index;
531 list_add_tail(&tmp_part->partition, &nvram_partitions);
532
533 cur_index += phead.length * NVRAM_BLOCK_LEN;
534 }
535 err = 0;
536
537#ifdef DEBUG_NVRAM
538 nvram_print_partitions("NVRAM Partitions");
539#endif
540
541 out:
542 kfree(header);
543 return err;
544}
545
546static int __init nvram_init(void)
547{
548 int rc;
549
550 BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
551
552 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
553 return -ENODEV;
554
555 rc = misc_register(&nvram_dev);
556 if (rc != 0) {
557 printk(KERN_ERR "nvram_init: failed to register device\n");
558 return rc;
559 }
560
561 return rc;
562}
563
564void __exit nvram_cleanup(void)
565{
566 misc_deregister( &nvram_dev );
567}
568
569module_init(nvram_init);
570module_exit(nvram_cleanup);
571MODULE_LICENSE("GPL");
572