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