OSDN Git Service

125d86cf0afc68f1e1689b34cb9bb8e436027791
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / arch / powerpc / kernel / nvram_64.c
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>
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 /* 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
54 static struct nvram_partition * nvram_part;
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 {
82         ssize_t ret;
83         char *tmp = NULL;
84         ssize_t size;
85
86         ret = -ENODEV;
87         if (!ppc_md.nvram_size)
88                 goto out;
89
90         ret = 0;
91         size = ppc_md.nvram_size();
92         if (*ppos >= size || size < 0)
93                 goto out;
94
95         count = min_t(size_t, count, size - *ppos);
96         count = min(count, PAGE_SIZE);
97
98         ret = -ENOMEM;
99         tmp = kmalloc(count, GFP_KERNEL);
100         if (!tmp)
101                 goto out;
102
103         ret = ppc_md.nvram_read(tmp, count, ppos);
104         if (ret <= 0)
105                 goto out;
106
107         if (copy_to_user(buf, tmp, ret))
108                 ret = -EFAULT;
109
110 out:
111         kfree(tmp);
112         return ret;
113
114 }
115
116 static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
117                           size_t count, loff_t *ppos)
118 {
119         ssize_t ret;
120         char *tmp = NULL;
121         ssize_t size;
122
123         ret = -ENODEV;
124         if (!ppc_md.nvram_size)
125                 goto out;
126
127         ret = 0;
128         size = ppc_md.nvram_size();
129         if (*ppos >= size || size < 0)
130                 goto out;
131
132         count = min_t(size_t, count, size - *ppos);
133         count = min(count, PAGE_SIZE);
134
135         ret = -ENOMEM;
136         tmp = kmalloc(count, GFP_KERNEL);
137         if (!tmp)
138                 goto out;
139
140         ret = -EFAULT;
141         if (copy_from_user(tmp, buf, count))
142                 goto out;
143
144         ret = ppc_md.nvram_write(tmp, count, ppos);
145
146 out:
147         kfree(tmp);
148         return ret;
149
150 }
151
152 static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
153                             unsigned long arg)
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
162                 if (!machine_is(powermac))
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 */
176         default:
177                 return -EINVAL;
178         }
179 }
180
181 const struct file_operations nvram_fops = {
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,
187 };
188
189 static struct miscdevice nvram_dev = {
190         NVRAM_MINOR,
191         "nvram",
192         &nvram_fops
193 };
194
195
196 #ifdef DEBUG_NVRAM
197 static void __init nvram_print_partitions(char * label)
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);
206                 printk(KERN_WARNING "%4d    \t%02x\t%02x\t%d\t%s\n",
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
215 static int __init nvram_write_header(struct nvram_partition * part)
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
227 static unsigned char __init nvram_checksum(struct nvram_header *p)
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
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
248 int __init nvram_remove_partition(const char *name, int sig)
249 {
250         struct nvram_partition *part, *prev, *tmp;
251         int rc;
252
253         list_for_each_entry(part, &nvram_part->partition, partition) {
254                 if (part->header.signature != sig)
255                         continue;
256                 if (name && strncmp(name, part->header.name, 12))
257                         continue;
258
259                 /* Make partition a free partition */
260                 part->header.signature = NVRAM_SIG_FREE;
261                 sprintf(part->header.name, "wwwwwwwwwwww");
262                 part->header.checksum = nvram_checksum(&part->header);
263                 rc = nvram_write_header(part);
264                 if (rc <= 0) {
265                         printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
266                         return rc;
267                 }
268         }
269
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;
289         }
290         
291         return 0;
292 }
293
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
298  * @req_size: size of data to allocate in bytes
299  * @min_size: minimum acceptable size (0 means req_size)
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().
306  */
307 loff_t __init nvram_create_partition(const char *name, int sig,
308                                      int req_size, int min_size)
309 {
310         struct nvram_partition *part;
311         struct nvram_partition *new_part;
312         struct nvram_partition *free_part = NULL;
313         static char nv_init_vals[16];
314         loff_t tmp_index;
315         long size = 0;
316         int rc;
317
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
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;
327         if (min_size > req_size)
328                 return -EINVAL;
329
330         /* Now add one block to each for the header */
331         req_size += 1;
332         min_size += 1;
333
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 */
336         list_for_each_entry(part, &nvram_part->partition, partition) {
337                 if (part->header.signature != NVRAM_SIG_FREE)
338                         continue;
339
340                 if (part->header.length >= req_size) {
341                         size = req_size;
342                         free_part = part;
343                         break;
344                 }
345                 if (part->header.length > size &&
346                     part->header.length >= min_size) {
347                         size = part->header.length;
348                         free_part = part;
349                 }
350         }
351         if (!size)
352                 return -ENOSPC;
353         
354         /* Create our OS partition */
355         new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
356         if (!new_part) {
357                 pr_err("nvram_create_os_partition: kmalloc failed\n");
358                 return -ENOMEM;
359         }
360
361         new_part->index = free_part->index;
362         new_part->header.signature = sig;
363         new_part->header.length = size;
364         strncpy(new_part->header.name, name, 12);
365         new_part->header.checksum = nvram_checksum(&new_part->header);
366
367         rc = nvram_write_header(new_part);
368         if (rc <= 0) {
369                 pr_err("nvram_create_os_partition: nvram_write_header "
370                        "failed (%d)\n", rc);
371                 return rc;
372         }
373         list_add_tail(&new_part->partition, &free_part->partition);
374
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 */
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                 }
400         }
401         
402         return new_part->index + NVRAM_HEADER_LEN;
403 }
404
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  */
411 int nvram_get_partition_size(loff_t data_index)
412 {
413         struct nvram_partition *part;
414         
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;
418         }
419         return -1;
420 }
421
422
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
445 int __init nvram_scan_partitions(void)
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
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)
464                 return -ENODEV;
465         total_size = ppc_md.nvram_size();
466         
467         header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
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
516 #ifdef DEBUG_NVRAM
517         nvram_print_partitions("NVRAM Partitions");
518 #endif
519
520  out:
521         kfree(header);
522         return err;
523 }
524
525 static int __init nvram_init(void)
526 {
527         int rc;
528         
529         BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
530
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         
540         return rc;
541 }
542
543 void __exit nvram_cleanup(void)
544 {
545         misc_deregister( &nvram_dev );
546 }
547
548 module_init(nvram_init);
549 module_exit(nvram_cleanup);
550 MODULE_LICENSE("GPL");