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