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