Pull esi-support into release branch
[pandora-kernel.git] / arch / powerpc / kernel / rtas_flash.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  * /proc/ppc64/rtas/firmware_flash interface
10  *
11  * This file implements a firmware_flash interface to pump a firmware
12  * image into the kernel.  At reboot time rtas_restart() will see the
13  * firmware image and flash it as it reboots (see rtas.c).
14  */
15
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/proc_fs.h>
19 #include <asm/delay.h>
20 #include <asm/uaccess.h>
21 #include <asm/rtas.h>
22 #include <asm/abs_addr.h>
23
24 #define MODULE_VERS "1.0"
25 #define MODULE_NAME "rtas_flash"
26
27 #define FIRMWARE_FLASH_NAME "firmware_flash"   
28 #define FIRMWARE_UPDATE_NAME "firmware_update"
29 #define MANAGE_FLASH_NAME "manage_flash"
30 #define VALIDATE_FLASH_NAME "validate_flash"
31
32 /* General RTAS Status Codes */
33 #define RTAS_RC_SUCCESS  0
34 #define RTAS_RC_HW_ERR  -1
35 #define RTAS_RC_BUSY    -2
36
37 /* Flash image status values */
38 #define FLASH_AUTH           -9002 /* RTAS Not Service Authority Partition */
39 #define FLASH_NO_OP          -1099 /* No operation initiated by user */ 
40 #define FLASH_IMG_SHORT      -1005 /* Flash image shorter than expected */
41 #define FLASH_IMG_BAD_LEN    -1004 /* Bad length value in flash list block */
42 #define FLASH_IMG_NULL_DATA  -1003 /* Bad data value in flash list block */
43 #define FLASH_IMG_READY      0     /* Firmware img ready for flash on reboot */
44
45 /* Manage image status values */
46 #define MANAGE_AUTH          -9002 /* RTAS Not Service Authority Partition */
47 #define MANAGE_ACTIVE_ERR    -9001 /* RTAS Cannot Overwrite Active Img */
48 #define MANAGE_NO_OP         -1099 /* No operation initiated by user */
49 #define MANAGE_PARAM_ERR     -3    /* RTAS Parameter Error */
50 #define MANAGE_HW_ERR        -1    /* RTAS Hardware Error */
51
52 /* Validate image status values */
53 #define VALIDATE_AUTH          -9002 /* RTAS Not Service Authority Partition */
54 #define VALIDATE_NO_OP         -1099 /* No operation initiated by the user */
55 #define VALIDATE_INCOMPLETE    -1002 /* User copied < VALIDATE_BUF_SIZE */
56 #define VALIDATE_READY         -1001 /* Firmware image ready for validation */
57 #define VALIDATE_PARAM_ERR     -3    /* RTAS Parameter Error */
58 #define VALIDATE_HW_ERR        -1    /* RTAS Hardware Error */
59 #define VALIDATE_TMP_UPDATE    0     /* Validate Return Status */
60 #define VALIDATE_FLASH_AUTH    1     /* Validate Return Status */
61 #define VALIDATE_INVALID_IMG   2     /* Validate Return Status */
62 #define VALIDATE_CUR_UNKNOWN   3     /* Validate Return Status */
63 #define VALIDATE_TMP_COMMIT_DL 4     /* Validate Return Status */
64 #define VALIDATE_TMP_COMMIT    5     /* Validate Return Status */
65 #define VALIDATE_TMP_UPDATE_DL 6     /* Validate Return Status */
66
67 /* ibm,manage-flash-image operation tokens */
68 #define RTAS_REJECT_TMP_IMG   0
69 #define RTAS_COMMIT_TMP_IMG   1
70
71 /* Array sizes */
72 #define VALIDATE_BUF_SIZE 4096    
73 #define RTAS_MSG_MAXLEN   64
74
75 struct flash_block {
76         char *data;
77         unsigned long length;
78 };
79
80 /* This struct is very similar but not identical to
81  * that needed by the rtas flash update.
82  * All we need to do for rtas is rewrite num_blocks
83  * into a version/length and translate the pointers
84  * to absolute.
85  */
86 #define FLASH_BLOCKS_PER_NODE ((PAGE_SIZE - 16) / sizeof(struct flash_block))
87 struct flash_block_list {
88         unsigned long num_blocks;
89         struct flash_block_list *next;
90         struct flash_block blocks[FLASH_BLOCKS_PER_NODE];
91 };
92 struct flash_block_list_header { /* just the header of flash_block_list */
93         unsigned long num_blocks;
94         struct flash_block_list *next;
95 };
96
97 static struct flash_block_list_header rtas_firmware_flash_list = {0, NULL};
98
99 #define FLASH_BLOCK_LIST_VERSION (1UL)
100
101 /* Local copy of the flash block list.
102  * We only allow one open of the flash proc file and create this
103  * list as we go.  This list will be put in the
104  * rtas_firmware_flash_list var once it is fully read.
105  *
106  * For convenience as we build the list we use virtual addrs,
107  * we do not fill in the version number, and the length field
108  * is treated as the number of entries currently in the block
109  * (i.e. not a byte count).  This is all fixed on release.
110  */
111
112 /* Status int must be first member of struct */
113 struct rtas_update_flash_t
114 {
115         int status;                     /* Flash update status */
116         struct flash_block_list *flist; /* Local copy of flash block list */
117 };
118
119 /* Status int must be first member of struct */
120 struct rtas_manage_flash_t
121 {
122         int status;                     /* Returned status */
123         unsigned int op;                /* Reject or commit image */
124 };
125
126 /* Status int must be first member of struct */
127 struct rtas_validate_flash_t
128 {
129         int status;                     /* Returned status */   
130         char buf[VALIDATE_BUF_SIZE];    /* Candidate image buffer */
131         unsigned int buf_size;          /* Size of image buf */
132         unsigned int update_results;    /* Update results token */
133 };
134
135 static DEFINE_SPINLOCK(flash_file_open_lock);
136 static struct proc_dir_entry *firmware_flash_pde;
137 static struct proc_dir_entry *firmware_update_pde;
138 static struct proc_dir_entry *validate_pde;
139 static struct proc_dir_entry *manage_pde;
140
141 /* Do simple sanity checks on the flash image. */
142 static int flash_list_valid(struct flash_block_list *flist)
143 {
144         struct flash_block_list *f;
145         int i;
146         unsigned long block_size, image_size;
147
148         /* Paranoid self test here.  We also collect the image size. */
149         image_size = 0;
150         for (f = flist; f; f = f->next) {
151                 for (i = 0; i < f->num_blocks; i++) {
152                         if (f->blocks[i].data == NULL) {
153                                 return FLASH_IMG_NULL_DATA;
154                         }
155                         block_size = f->blocks[i].length;
156                         if (block_size <= 0 || block_size > PAGE_SIZE) {
157                                 return FLASH_IMG_BAD_LEN;
158                         }
159                         image_size += block_size;
160                 }
161         }
162
163         if (image_size < (256 << 10)) {
164                 if (image_size < 2) 
165                         return FLASH_NO_OP;
166         }
167
168         printk(KERN_INFO "FLASH: flash image with %ld bytes stored for hardware flash on reboot\n", image_size);
169
170         return FLASH_IMG_READY;
171 }
172
173 static void free_flash_list(struct flash_block_list *f)
174 {
175         struct flash_block_list *next;
176         int i;
177
178         while (f) {
179                 for (i = 0; i < f->num_blocks; i++)
180                         free_page((unsigned long)(f->blocks[i].data));
181                 next = f->next;
182                 free_page((unsigned long)f);
183                 f = next;
184         }
185 }
186
187 static int rtas_flash_release(struct inode *inode, struct file *file)
188 {
189         struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
190         struct rtas_update_flash_t *uf;
191         
192         uf = (struct rtas_update_flash_t *) dp->data;
193         if (uf->flist) {    
194                 /* File was opened in write mode for a new flash attempt */
195                 /* Clear saved list */
196                 if (rtas_firmware_flash_list.next) {
197                         free_flash_list(rtas_firmware_flash_list.next);
198                         rtas_firmware_flash_list.next = NULL;
199                 }
200
201                 if (uf->status != FLASH_AUTH)  
202                         uf->status = flash_list_valid(uf->flist);
203
204                 if (uf->status == FLASH_IMG_READY) 
205                         rtas_firmware_flash_list.next = uf->flist;
206                 else
207                         free_flash_list(uf->flist);
208
209                 uf->flist = NULL;
210         }
211
212         atomic_dec(&dp->count);
213         return 0;
214 }
215
216 static void get_flash_status_msg(int status, char *buf)
217 {
218         char *msg;
219
220         switch (status) {
221         case FLASH_AUTH:
222                 msg = "error: this partition does not have service authority\n";
223                 break;
224         case FLASH_NO_OP:
225                 msg = "info: no firmware image for flash\n";
226                 break;
227         case FLASH_IMG_SHORT:
228                 msg = "error: flash image short\n";
229                 break;
230         case FLASH_IMG_BAD_LEN:
231                 msg = "error: internal error bad length\n";
232                 break;
233         case FLASH_IMG_NULL_DATA:
234                 msg = "error: internal error null data\n";
235                 break;
236         case FLASH_IMG_READY:
237                 msg = "ready: firmware image ready for flash on reboot\n";
238                 break;
239         default:
240                 sprintf(buf, "error: unexpected status value %d\n", status);
241                 return;
242         }
243
244         strcpy(buf, msg);       
245 }
246
247 /* Reading the proc file will show status (not the firmware contents) */
248 static ssize_t rtas_flash_read(struct file *file, char __user *buf,
249                                size_t count, loff_t *ppos)
250 {
251         struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
252         struct rtas_update_flash_t *uf;
253         char msg[RTAS_MSG_MAXLEN];
254         int msglen;
255
256         uf = (struct rtas_update_flash_t *) dp->data;
257
258         if (!strcmp(dp->name, FIRMWARE_FLASH_NAME)) {
259                 get_flash_status_msg(uf->status, msg);
260         } else {           /* FIRMWARE_UPDATE_NAME */
261                 sprintf(msg, "%d\n", uf->status);
262         }
263         msglen = strlen(msg);
264         if (msglen > count)
265                 msglen = count;
266
267         if (ppos && *ppos != 0)
268                 return 0;       /* be cheap */
269
270         if (!access_ok(VERIFY_WRITE, buf, msglen))
271                 return -EINVAL;
272
273         if (copy_to_user(buf, msg, msglen))
274                 return -EFAULT;
275
276         if (ppos)
277                 *ppos = msglen;
278         return msglen;
279 }
280
281 /* We could be much more efficient here.  But to keep this function
282  * simple we allocate a page to the block list no matter how small the
283  * count is.  If the system is low on memory it will be just as well
284  * that we fail....
285  */
286 static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
287                                 size_t count, loff_t *off)
288 {
289         struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
290         struct rtas_update_flash_t *uf;
291         char *p;
292         int next_free;
293         struct flash_block_list *fl;
294
295         uf = (struct rtas_update_flash_t *) dp->data;
296
297         if (uf->status == FLASH_AUTH || count == 0)
298                 return count;   /* discard data */
299
300         /* In the case that the image is not ready for flashing, the memory
301          * allocated for the block list will be freed upon the release of the 
302          * proc file
303          */
304         if (uf->flist == NULL) {
305                 uf->flist = (struct flash_block_list *) get_zeroed_page(GFP_KERNEL);
306                 if (!uf->flist)
307                         return -ENOMEM;
308         }
309
310         fl = uf->flist;
311         while (fl->next)
312                 fl = fl->next; /* seek to last block_list for append */
313         next_free = fl->num_blocks;
314         if (next_free == FLASH_BLOCKS_PER_NODE) {
315                 /* Need to allocate another block_list */
316                 fl->next = (struct flash_block_list *)get_zeroed_page(GFP_KERNEL);
317                 if (!fl->next)
318                         return -ENOMEM;
319                 fl = fl->next;
320                 next_free = 0;
321         }
322
323         if (count > PAGE_SIZE)
324                 count = PAGE_SIZE;
325         p = (char *)get_zeroed_page(GFP_KERNEL);
326         if (!p)
327                 return -ENOMEM;
328         
329         if(copy_from_user(p, buffer, count)) {
330                 free_page((unsigned long)p);
331                 return -EFAULT;
332         }
333         fl->blocks[next_free].data = p;
334         fl->blocks[next_free].length = count;
335         fl->num_blocks++;
336
337         return count;
338 }
339
340 static int rtas_excl_open(struct inode *inode, struct file *file)
341 {
342         struct proc_dir_entry *dp = PDE(inode);
343
344         /* Enforce exclusive open with use count of PDE */
345         spin_lock(&flash_file_open_lock);
346         if (atomic_read(&dp->count) > 1) {
347                 spin_unlock(&flash_file_open_lock);
348                 return -EBUSY;
349         }
350
351         atomic_inc(&dp->count);
352         spin_unlock(&flash_file_open_lock);
353         
354         return 0;
355 }
356
357 static int rtas_excl_release(struct inode *inode, struct file *file)
358 {
359         struct proc_dir_entry *dp = PDE(inode);
360
361         atomic_dec(&dp->count);
362
363         return 0;
364 }
365
366 static void manage_flash(struct rtas_manage_flash_t *args_buf)
367 {
368         s32 rc;
369
370         do {
371                 rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1, 
372                                1, NULL, args_buf->op);
373         } while (rtas_busy_delay(rc));
374
375         args_buf->status = rc;
376 }
377
378 static ssize_t manage_flash_read(struct file *file, char __user *buf,
379                                size_t count, loff_t *ppos)
380 {
381         struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
382         struct rtas_manage_flash_t *args_buf;
383         char msg[RTAS_MSG_MAXLEN];
384         int msglen;
385
386         args_buf = (struct rtas_manage_flash_t *) dp->data;
387         if (args_buf == NULL)
388                 return 0;
389
390         msglen = sprintf(msg, "%d\n", args_buf->status);
391         if (msglen > count)
392                 msglen = count;
393
394         if (ppos && *ppos != 0)
395                 return 0;       /* be cheap */
396
397         if (!access_ok(VERIFY_WRITE, buf, msglen))
398                 return -EINVAL;
399
400         if (copy_to_user(buf, msg, msglen))
401                 return -EFAULT;
402
403         if (ppos)
404                 *ppos = msglen;
405         return msglen;
406 }
407
408 static ssize_t manage_flash_write(struct file *file, const char __user *buf,
409                                 size_t count, loff_t *off)
410 {
411         struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
412         struct rtas_manage_flash_t *args_buf;
413         const char reject_str[] = "0";
414         const char commit_str[] = "1";
415         char stkbuf[10];
416         int op;
417
418         args_buf = (struct rtas_manage_flash_t *) dp->data;
419         if ((args_buf->status == MANAGE_AUTH) || (count == 0))
420                 return count;
421                 
422         op = -1;
423         if (buf) {
424                 if (count > 9) count = 9;
425                 if (copy_from_user (stkbuf, buf, count)) {
426                         return -EFAULT;
427                 }
428                 if (strncmp(stkbuf, reject_str, strlen(reject_str)) == 0) 
429                         op = RTAS_REJECT_TMP_IMG;
430                 else if (strncmp(stkbuf, commit_str, strlen(commit_str)) == 0) 
431                         op = RTAS_COMMIT_TMP_IMG;
432         }
433         
434         if (op == -1)   /* buf is empty, or contains invalid string */
435                 return -EINVAL;
436
437         args_buf->op = op;
438         manage_flash(args_buf);
439
440         return count;
441 }
442
443 static void validate_flash(struct rtas_validate_flash_t *args_buf)
444 {
445         int token = rtas_token("ibm,validate-flash-image");
446         int update_results;
447         s32 rc; 
448
449         rc = 0;
450         do {
451                 spin_lock(&rtas_data_buf_lock);
452                 memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE);
453                 rc = rtas_call(token, 2, 2, &update_results, 
454                                (u32) __pa(rtas_data_buf), args_buf->buf_size);
455                 memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE);
456                 spin_unlock(&rtas_data_buf_lock);
457         } while (rtas_busy_delay(rc));
458
459         args_buf->status = rc;
460         args_buf->update_results = update_results;
461 }
462
463 static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf, 
464                                    char *msg)
465 {
466         int n;
467
468         if (args_buf->status >= VALIDATE_TMP_UPDATE) { 
469                 n = sprintf(msg, "%d\n", args_buf->update_results);
470                 if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
471                     (args_buf->update_results == VALIDATE_TMP_UPDATE))
472                         n += sprintf(msg + n, "%s\n", args_buf->buf);
473         } else {
474                 n = sprintf(msg, "%d\n", args_buf->status);
475         }
476         return n;
477 }
478
479 static ssize_t validate_flash_read(struct file *file, char __user *buf,
480                                size_t count, loff_t *ppos)
481 {
482         struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
483         struct rtas_validate_flash_t *args_buf;
484         char msg[RTAS_MSG_MAXLEN];
485         int msglen;
486
487         args_buf = (struct rtas_validate_flash_t *) dp->data;
488
489         if (ppos && *ppos != 0)
490                 return 0;       /* be cheap */
491         
492         msglen = get_validate_flash_msg(args_buf, msg);
493         if (msglen > count)
494                 msglen = count;
495
496         if (!access_ok(VERIFY_WRITE, buf, msglen))
497                 return -EINVAL;
498
499         if (copy_to_user(buf, msg, msglen))
500                 return -EFAULT;
501
502         if (ppos)
503                 *ppos = msglen;
504         return msglen;
505 }
506
507 static ssize_t validate_flash_write(struct file *file, const char __user *buf,
508                                     size_t count, loff_t *off)
509 {
510         struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
511         struct rtas_validate_flash_t *args_buf;
512         int rc;
513
514         args_buf = (struct rtas_validate_flash_t *) dp->data;
515
516         if (dp->data == NULL) {
517                 dp->data = kmalloc(sizeof(struct rtas_validate_flash_t), 
518                                 GFP_KERNEL);
519                 if (dp->data == NULL) 
520                         return -ENOMEM;
521         }
522
523         /* We are only interested in the first 4K of the
524          * candidate image */
525         if ((*off >= VALIDATE_BUF_SIZE) || 
526                 (args_buf->status == VALIDATE_AUTH)) {
527                 *off += count;
528                 return count;
529         }
530
531         if (*off + count >= VALIDATE_BUF_SIZE)  {
532                 count = VALIDATE_BUF_SIZE - *off;
533                 args_buf->status = VALIDATE_READY;      
534         } else {
535                 args_buf->status = VALIDATE_INCOMPLETE;
536         }
537
538         if (!access_ok(VERIFY_READ, buf, count)) {
539                 rc = -EFAULT;
540                 goto done;
541         }
542         if (copy_from_user(args_buf->buf + *off, buf, count)) {
543                 rc = -EFAULT;
544                 goto done;
545         }
546
547         *off += count;
548         rc = count;
549 done:
550         if (rc < 0) {
551                 kfree(dp->data);
552                 dp->data = NULL;
553         }
554         return rc;
555 }
556
557 static int validate_flash_release(struct inode *inode, struct file *file)
558 {
559         struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
560         struct rtas_validate_flash_t *args_buf;
561
562         args_buf = (struct rtas_validate_flash_t *) dp->data;
563
564         if (args_buf->status == VALIDATE_READY) {
565                 args_buf->buf_size = VALIDATE_BUF_SIZE;
566                 validate_flash(args_buf);
567         }
568
569         /* The matching atomic_inc was in rtas_excl_open() */
570         atomic_dec(&dp->count);
571
572         return 0;
573 }
574
575 static void rtas_flash_firmware(int reboot_type)
576 {
577         unsigned long image_size;
578         struct flash_block_list *f, *next, *flist;
579         unsigned long rtas_block_list;
580         int i, status, update_token;
581
582         if (rtas_firmware_flash_list.next == NULL)
583                 return;         /* nothing to do */
584
585         if (reboot_type != SYS_RESTART) {
586                 printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
587                 printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
588                 return;
589         }
590
591         update_token = rtas_token("ibm,update-flash-64-and-reboot");
592         if (update_token == RTAS_UNKNOWN_SERVICE) {
593                 printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot "
594                        "is not available -- not a service partition?\n");
595                 printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
596                 return;
597         }
598
599         /* NOTE: the "first" block list is a global var with no data
600          * blocks in the kernel data segment.  We do this because
601          * we want to ensure this block_list addr is under 4GB.
602          */
603         rtas_firmware_flash_list.num_blocks = 0;
604         flist = (struct flash_block_list *)&rtas_firmware_flash_list;
605         rtas_block_list = virt_to_abs(flist);
606         if (rtas_block_list >= 4UL*1024*1024*1024) {
607                 printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
608                 return;
609         }
610
611         printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
612         /* Update the block_list in place. */
613         image_size = 0;
614         for (f = flist; f; f = next) {
615                 /* Translate data addrs to absolute */
616                 for (i = 0; i < f->num_blocks; i++) {
617                         f->blocks[i].data = (char *)virt_to_abs(f->blocks[i].data);
618                         image_size += f->blocks[i].length;
619                 }
620                 next = f->next;
621                 /* Don't translate NULL pointer for last entry */
622                 if (f->next)
623                         f->next = (struct flash_block_list *)virt_to_abs(f->next);
624                 else
625                         f->next = NULL;
626                 /* make num_blocks into the version/length field */
627                 f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
628         }
629
630         printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
631         printk(KERN_ALERT "FLASH: performing flash and reboot\n");
632         rtas_progress("Flashing        \n", 0x0);
633         rtas_progress("Please Wait...  ", 0x0);
634         printk(KERN_ALERT "FLASH: this will take several minutes.  Do not power off!\n");
635         status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
636         switch (status) {       /* should only get "bad" status */
637             case 0:
638                 printk(KERN_ALERT "FLASH: success\n");
639                 break;
640             case -1:
641                 printk(KERN_ALERT "FLASH: hardware error.  Firmware may not be not flashed\n");
642                 break;
643             case -3:
644                 printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform.  Firmware not flashed\n");
645                 break;
646             case -4:
647                 printk(KERN_ALERT "FLASH: flash failed when partially complete.  System may not reboot\n");
648                 break;
649             default:
650                 printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
651                 break;
652         }
653 }
654
655 static void remove_flash_pde(struct proc_dir_entry *dp)
656 {
657         if (dp) {
658                 kfree(dp->data);
659                 dp->owner = NULL;
660                 remove_proc_entry(dp->name, dp->parent);
661         }
662 }
663
664 static int initialize_flash_pde_data(const char *rtas_call_name,
665                                      size_t buf_size,
666                                      struct proc_dir_entry *dp)
667 {
668         int *status;
669         int token;
670
671         dp->data = kmalloc(buf_size, GFP_KERNEL);
672         if (dp->data == NULL) {
673                 remove_flash_pde(dp);
674                 return -ENOMEM;
675         }
676
677         memset(dp->data, 0, buf_size);
678
679         /*
680          * This code assumes that the status int is the first member of the
681          * struct 
682          */
683         status = (int *) dp->data;
684         token = rtas_token(rtas_call_name);
685         if (token == RTAS_UNKNOWN_SERVICE)
686                 *status = FLASH_AUTH;
687         else
688                 *status = FLASH_NO_OP;
689
690         return 0;
691 }
692
693 static struct proc_dir_entry *create_flash_pde(const char *filename,
694                                                struct file_operations *fops)
695 {
696         struct proc_dir_entry *ent = NULL;
697
698         ent = create_proc_entry(filename, S_IRUSR | S_IWUSR, NULL);
699         if (ent != NULL) {
700                 ent->nlink = 1;
701                 ent->proc_fops = fops;
702                 ent->owner = THIS_MODULE;
703         }
704
705         return ent;
706 }
707
708 static struct file_operations rtas_flash_operations = {
709         .read           = rtas_flash_read,
710         .write          = rtas_flash_write,
711         .open           = rtas_excl_open,
712         .release        = rtas_flash_release,
713 };
714
715 static struct file_operations manage_flash_operations = {
716         .read           = manage_flash_read,
717         .write          = manage_flash_write,
718         .open           = rtas_excl_open,
719         .release        = rtas_excl_release,
720 };
721
722 static struct file_operations validate_flash_operations = {
723         .read           = validate_flash_read,
724         .write          = validate_flash_write,
725         .open           = rtas_excl_open,
726         .release        = validate_flash_release,
727 };
728
729 int __init rtas_flash_init(void)
730 {
731         int rc;
732
733         if (rtas_token("ibm,update-flash-64-and-reboot") ==
734                        RTAS_UNKNOWN_SERVICE) {
735                 printk(KERN_ERR "rtas_flash: no firmware flash support\n");
736                 return 1;
737         }
738
739         firmware_flash_pde = create_flash_pde("ppc64/rtas/"
740                                               FIRMWARE_FLASH_NAME,
741                                               &rtas_flash_operations);
742         if (firmware_flash_pde == NULL) {
743                 rc = -ENOMEM;
744                 goto cleanup;
745         }
746
747         rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
748                                        sizeof(struct rtas_update_flash_t), 
749                                        firmware_flash_pde);
750         if (rc != 0)
751                 goto cleanup;
752
753         firmware_update_pde = create_flash_pde("ppc64/rtas/"
754                                                FIRMWARE_UPDATE_NAME,
755                                                &rtas_flash_operations);
756         if (firmware_update_pde == NULL) {
757                 rc = -ENOMEM;
758                 goto cleanup;
759         }
760
761         rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
762                                        sizeof(struct rtas_update_flash_t), 
763                                        firmware_update_pde);
764         if (rc != 0)
765                 goto cleanup;
766
767         validate_pde = create_flash_pde("ppc64/rtas/" VALIDATE_FLASH_NAME,
768                                         &validate_flash_operations);
769         if (validate_pde == NULL) {
770                 rc = -ENOMEM;
771                 goto cleanup;
772         }
773
774         rc = initialize_flash_pde_data("ibm,validate-flash-image",
775                                        sizeof(struct rtas_validate_flash_t), 
776                                        validate_pde);
777         if (rc != 0)
778                 goto cleanup;
779
780         manage_pde = create_flash_pde("ppc64/rtas/" MANAGE_FLASH_NAME,
781                                       &manage_flash_operations);
782         if (manage_pde == NULL) {
783                 rc = -ENOMEM;
784                 goto cleanup;
785         }
786
787         rc = initialize_flash_pde_data("ibm,manage-flash-image",
788                                        sizeof(struct rtas_manage_flash_t),
789                                        manage_pde);
790         if (rc != 0)
791                 goto cleanup;
792
793         rtas_flash_term_hook = rtas_flash_firmware;
794         return 0;
795
796 cleanup:
797         remove_flash_pde(firmware_flash_pde);
798         remove_flash_pde(firmware_update_pde);
799         remove_flash_pde(validate_pde);
800         remove_flash_pde(manage_pde);
801
802         return rc;
803 }
804
805 void __exit rtas_flash_cleanup(void)
806 {
807         rtas_flash_term_hook = NULL;
808         remove_flash_pde(firmware_flash_pde);
809         remove_flash_pde(firmware_update_pde);
810         remove_flash_pde(validate_pde);
811         remove_flash_pde(manage_pde);
812 }
813
814 module_init(rtas_flash_init);
815 module_exit(rtas_flash_cleanup);
816 MODULE_LICENSE("GPL");