rtas_flash.c 22.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8
/*
 *  c 2001 PPC 64 Team, IBM Corp
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 *
9
 * /proc/powerpc/rtas/firmware_flash interface
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17
 *
 * This file implements a firmware_flash interface to pump a firmware
 * image into the kernel.  At reboot time rtas_restart() will see the
 * firmware image and flash it as it reboots (see rtas.c).
 */

#include <linux/module.h>
#include <linux/init.h>
18
#include <linux/slab.h>
L
Linus Torvalds 已提交
19 20 21 22
#include <linux/proc_fs.h>
#include <asm/delay.h>
#include <asm/uaccess.h>
#include <asm/rtas.h>
23
#include <asm/abs_addr.h>
L
Linus Torvalds 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

#define MODULE_VERS "1.0"
#define MODULE_NAME "rtas_flash"

#define FIRMWARE_FLASH_NAME "firmware_flash"   
#define FIRMWARE_UPDATE_NAME "firmware_update"
#define MANAGE_FLASH_NAME "manage_flash"
#define VALIDATE_FLASH_NAME "validate_flash"

/* General RTAS Status Codes */
#define RTAS_RC_SUCCESS  0
#define RTAS_RC_HW_ERR	-1
#define RTAS_RC_BUSY	-2

/* Flash image status values */
#define FLASH_AUTH           -9002 /* RTAS Not Service Authority Partition */
#define FLASH_NO_OP          -1099 /* No operation initiated by user */	
#define FLASH_IMG_SHORT	     -1005 /* Flash image shorter than expected */
#define FLASH_IMG_BAD_LEN    -1004 /* Bad length value in flash list block */
#define FLASH_IMG_NULL_DATA  -1003 /* Bad data value in flash list block */
#define FLASH_IMG_READY      0     /* Firmware img ready for flash on reboot */

/* Manage image status values */
#define MANAGE_AUTH          -9002 /* RTAS Not Service Authority Partition */
#define MANAGE_ACTIVE_ERR    -9001 /* RTAS Cannot Overwrite Active Img */
#define MANAGE_NO_OP         -1099 /* No operation initiated by user */
#define MANAGE_PARAM_ERR     -3    /* RTAS Parameter Error */
#define MANAGE_HW_ERR        -1    /* RTAS Hardware Error */

/* Validate image status values */
#define VALIDATE_AUTH          -9002 /* RTAS Not Service Authority Partition */
#define VALIDATE_NO_OP         -1099 /* No operation initiated by the user */
#define VALIDATE_INCOMPLETE    -1002 /* User copied < VALIDATE_BUF_SIZE */
#define VALIDATE_READY	       -1001 /* Firmware image ready for validation */
#define VALIDATE_PARAM_ERR     -3    /* RTAS Parameter Error */
#define VALIDATE_HW_ERR        -1    /* RTAS Hardware Error */
#define VALIDATE_TMP_UPDATE    0     /* Validate Return Status */
#define VALIDATE_FLASH_AUTH    1     /* Validate Return Status */
#define VALIDATE_INVALID_IMG   2     /* Validate Return Status */
#define VALIDATE_CUR_UNKNOWN   3     /* Validate Return Status */
#define VALIDATE_TMP_COMMIT_DL 4     /* Validate Return Status */
#define VALIDATE_TMP_COMMIT    5     /* Validate Return Status */
#define VALIDATE_TMP_UPDATE_DL 6     /* Validate Return Status */

/* ibm,manage-flash-image operation tokens */
#define RTAS_REJECT_TMP_IMG   0
#define RTAS_COMMIT_TMP_IMG   1

/* Array sizes */
#define VALIDATE_BUF_SIZE 4096    
#define RTAS_MSG_MAXLEN   64

76 77 78 79
/* Quirk - RTAS requires 4k list length and block size */
#define RTAS_BLKLIST_LENGTH 4096
#define RTAS_BLK_SIZE 4096

80 81 82 83 84 85 86 87 88 89 90
struct flash_block {
	char *data;
	unsigned long length;
};

/* This struct is very similar but not identical to
 * that needed by the rtas flash update.
 * All we need to do for rtas is rewrite num_blocks
 * into a version/length and translate the pointers
 * to absolute.
 */
91
#define FLASH_BLOCKS_PER_NODE ((RTAS_BLKLIST_LENGTH - 16) / sizeof(struct flash_block))
92 93 94 95 96 97
struct flash_block_list {
	unsigned long num_blocks;
	struct flash_block_list *next;
	struct flash_block blocks[FLASH_BLOCKS_PER_NODE];
};

98
static struct flash_block_list *rtas_firmware_flash_list;
99

100
/* Use slab cache to guarantee 4k alignment */
101
static struct kmem_cache *flash_block_cache = NULL;
102

103 104
#define FLASH_BLOCK_LIST_VERSION (1UL)

L
Linus Torvalds 已提交
105 106
/* Local copy of the flash block list.
 * We only allow one open of the flash proc file and create this
107 108
 * list as we go.  The rtas_firmware_flash_list varable will be
 * set once the data is fully read.
L
Linus Torvalds 已提交
109 110 111 112
 *
 * For convenience as we build the list we use virtual addrs,
 * we do not fill in the version number, and the length field
 * is treated as the number of entries currently in the block
113 114
 * (i.e. not a byte count).  This is all fixed when calling 
 * the flash routine.
L
Linus Torvalds 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
 */

/* Status int must be first member of struct */
struct rtas_update_flash_t
{
	int status;			/* Flash update status */
	struct flash_block_list *flist; /* Local copy of flash block list */
};

/* Status int must be first member of struct */
struct rtas_manage_flash_t
{
	int status;			/* Returned status */
	unsigned int op;		/* Reject or commit image */
};

/* Status int must be first member of struct */
struct rtas_validate_flash_t
{
	int status;		 	/* Returned status */	
	char buf[VALIDATE_BUF_SIZE]; 	/* Candidate image buffer */
	unsigned int buf_size;		/* Size of image buf */
	unsigned int update_results;	/* Update results token */
};

static DEFINE_SPINLOCK(flash_file_open_lock);
static struct proc_dir_entry *firmware_flash_pde;
static struct proc_dir_entry *firmware_update_pde;
static struct proc_dir_entry *validate_pde;
static struct proc_dir_entry *manage_pde;

/* Do simple sanity checks on the flash image. */
static int flash_list_valid(struct flash_block_list *flist)
{
	struct flash_block_list *f;
	int i;
	unsigned long block_size, image_size;

	/* Paranoid self test here.  We also collect the image size. */
	image_size = 0;
	for (f = flist; f; f = f->next) {
		for (i = 0; i < f->num_blocks; i++) {
			if (f->blocks[i].data == NULL) {
				return FLASH_IMG_NULL_DATA;
			}
			block_size = f->blocks[i].length;
161
			if (block_size <= 0 || block_size > RTAS_BLK_SIZE) {
L
Linus Torvalds 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
				return FLASH_IMG_BAD_LEN;
			}
			image_size += block_size;
		}
	}

	if (image_size < (256 << 10)) {
		if (image_size < 2) 
			return FLASH_NO_OP;
	}

	printk(KERN_INFO "FLASH: flash image with %ld bytes stored for hardware flash on reboot\n", image_size);

	return FLASH_IMG_READY;
}

static void free_flash_list(struct flash_block_list *f)
{
	struct flash_block_list *next;
	int i;

	while (f) {
		for (i = 0; i < f->num_blocks; i++)
185
			kmem_cache_free(flash_block_cache, f->blocks[i].data);
L
Linus Torvalds 已提交
186
		next = f->next;
187
		kmem_cache_free(flash_block_cache, f);
L
Linus Torvalds 已提交
188 189 190 191 192 193
		f = next;
	}
}

static int rtas_flash_release(struct inode *inode, struct file *file)
{
J
Josef Sipek 已提交
194
	struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
L
Linus Torvalds 已提交
195 196 197 198 199 200
	struct rtas_update_flash_t *uf;
	
	uf = (struct rtas_update_flash_t *) dp->data;
	if (uf->flist) {    
		/* File was opened in write mode for a new flash attempt */
		/* Clear saved list */
201 202 203
		if (rtas_firmware_flash_list) {
			free_flash_list(rtas_firmware_flash_list);
			rtas_firmware_flash_list = NULL;
L
Linus Torvalds 已提交
204 205 206 207 208 209
		}

		if (uf->status != FLASH_AUTH)  
			uf->status = flash_list_valid(uf->flist);

		if (uf->status == FLASH_IMG_READY) 
210
			rtas_firmware_flash_list = uf->flist;
L
Linus Torvalds 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
		else
			free_flash_list(uf->flist);

		uf->flist = NULL;
	}

	atomic_dec(&dp->count);
	return 0;
}

static void get_flash_status_msg(int status, char *buf)
{
	char *msg;

	switch (status) {
	case FLASH_AUTH:
		msg = "error: this partition does not have service authority\n";
		break;
	case FLASH_NO_OP:
		msg = "info: no firmware image for flash\n";
		break;
	case FLASH_IMG_SHORT:
		msg = "error: flash image short\n";
		break;
	case FLASH_IMG_BAD_LEN:
		msg = "error: internal error bad length\n";
		break;
	case FLASH_IMG_NULL_DATA:
		msg = "error: internal error null data\n";
		break;
	case FLASH_IMG_READY:
		msg = "ready: firmware image ready for flash on reboot\n";
		break;
	default:
		sprintf(buf, "error: unexpected status value %d\n", status);
		return;
	}

	strcpy(buf, msg);	
}

/* Reading the proc file will show status (not the firmware contents) */
253
static ssize_t rtas_flash_read(struct file *file, char __user *buf,
L
Linus Torvalds 已提交
254 255
			       size_t count, loff_t *ppos)
{
J
Josef Sipek 已提交
256
	struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	struct rtas_update_flash_t *uf;
	char msg[RTAS_MSG_MAXLEN];
	int msglen;

	uf = (struct rtas_update_flash_t *) dp->data;

	if (!strcmp(dp->name, FIRMWARE_FLASH_NAME)) {
		get_flash_status_msg(uf->status, msg);
	} else {	   /* FIRMWARE_UPDATE_NAME */
		sprintf(msg, "%d\n", uf->status);
	}
	msglen = strlen(msg);
	if (msglen > count)
		msglen = count;

	if (ppos && *ppos != 0)
		return 0;	/* be cheap */

	if (!access_ok(VERIFY_WRITE, buf, msglen))
		return -EINVAL;

	if (copy_to_user(buf, msg, msglen))
		return -EFAULT;

	if (ppos)
		*ppos = msglen;
	return msglen;
}

286
/* constructor for flash_block_cache */
287
void rtas_block_ctor(void *ptr)
288 289 290 291
{
	memset(ptr, 0, RTAS_BLK_SIZE);
}

L
Linus Torvalds 已提交
292 293 294 295 296
/* We could be much more efficient here.  But to keep this function
 * simple we allocate a page to the block list no matter how small the
 * count is.  If the system is low on memory it will be just as well
 * that we fail....
 */
297
static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
L
Linus Torvalds 已提交
298 299
				size_t count, loff_t *off)
{
J
Josef Sipek 已提交
300
	struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
L
Linus Torvalds 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	struct rtas_update_flash_t *uf;
	char *p;
	int next_free;
	struct flash_block_list *fl;

	uf = (struct rtas_update_flash_t *) dp->data;

	if (uf->status == FLASH_AUTH || count == 0)
		return count;	/* discard data */

	/* In the case that the image is not ready for flashing, the memory
	 * allocated for the block list will be freed upon the release of the 
	 * proc file
	 */
	if (uf->flist == NULL) {
316
		uf->flist = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
L
Linus Torvalds 已提交
317 318 319 320 321 322 323 324 325 326
		if (!uf->flist)
			return -ENOMEM;
	}

	fl = uf->flist;
	while (fl->next)
		fl = fl->next; /* seek to last block_list for append */
	next_free = fl->num_blocks;
	if (next_free == FLASH_BLOCKS_PER_NODE) {
		/* Need to allocate another block_list */
327
		fl->next = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
L
Linus Torvalds 已提交
328 329 330 331 332 333
		if (!fl->next)
			return -ENOMEM;
		fl = fl->next;
		next_free = 0;
	}

334 335 336
	if (count > RTAS_BLK_SIZE)
		count = RTAS_BLK_SIZE;
	p = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
L
Linus Torvalds 已提交
337 338 339 340
	if (!p)
		return -ENOMEM;
	
	if(copy_from_user(p, buffer, count)) {
341
		kmem_cache_free(flash_block_cache, p);
L
Linus Torvalds 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
		return -EFAULT;
	}
	fl->blocks[next_free].data = p;
	fl->blocks[next_free].length = count;
	fl->num_blocks++;

	return count;
}

static int rtas_excl_open(struct inode *inode, struct file *file)
{
	struct proc_dir_entry *dp = PDE(inode);

	/* Enforce exclusive open with use count of PDE */
	spin_lock(&flash_file_open_lock);
357
	if (atomic_read(&dp->count) > 2) {
L
Linus Torvalds 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
		spin_unlock(&flash_file_open_lock);
		return -EBUSY;
	}

	atomic_inc(&dp->count);
	spin_unlock(&flash_file_open_lock);
	
	return 0;
}

static int rtas_excl_release(struct inode *inode, struct file *file)
{
	struct proc_dir_entry *dp = PDE(inode);

	atomic_dec(&dp->count);

	return 0;
}

static void manage_flash(struct rtas_manage_flash_t *args_buf)
{
	s32 rc;

381
	do {
L
Linus Torvalds 已提交
382 383
		rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1, 
			       1, NULL, args_buf->op);
384
	} while (rtas_busy_delay(rc));
L
Linus Torvalds 已提交
385 386 387 388

	args_buf->status = rc;
}

389
static ssize_t manage_flash_read(struct file *file, char __user *buf,
L
Linus Torvalds 已提交
390 391
			       size_t count, loff_t *ppos)
{
J
Josef Sipek 已提交
392
	struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
L
Linus Torvalds 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
	struct rtas_manage_flash_t *args_buf;
	char msg[RTAS_MSG_MAXLEN];
	int msglen;

	args_buf = (struct rtas_manage_flash_t *) dp->data;
	if (args_buf == NULL)
		return 0;

	msglen = sprintf(msg, "%d\n", args_buf->status);
	if (msglen > count)
		msglen = count;

	if (ppos && *ppos != 0)
		return 0;	/* be cheap */

	if (!access_ok(VERIFY_WRITE, buf, msglen))
		return -EINVAL;

	if (copy_to_user(buf, msg, msglen))
		return -EFAULT;

	if (ppos)
		*ppos = msglen;
	return msglen;
}

419
static ssize_t manage_flash_write(struct file *file, const char __user *buf,
L
Linus Torvalds 已提交
420 421
				size_t count, loff_t *off)
{
J
Josef Sipek 已提交
422
	struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
L
Linus Torvalds 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
	struct rtas_manage_flash_t *args_buf;
	const char reject_str[] = "0";
	const char commit_str[] = "1";
	char stkbuf[10];
	int op;

	args_buf = (struct rtas_manage_flash_t *) dp->data;
	if ((args_buf->status == MANAGE_AUTH) || (count == 0))
		return count;
		
	op = -1;
	if (buf) {
		if (count > 9) count = 9;
		if (copy_from_user (stkbuf, buf, count)) {
			return -EFAULT;
		}
		if (strncmp(stkbuf, reject_str, strlen(reject_str)) == 0) 
			op = RTAS_REJECT_TMP_IMG;
		else if (strncmp(stkbuf, commit_str, strlen(commit_str)) == 0) 
			op = RTAS_COMMIT_TMP_IMG;
	}
	
	if (op == -1)   /* buf is empty, or contains invalid string */
		return -EINVAL;

	args_buf->op = op;
	manage_flash(args_buf);

	return count;
}

static void validate_flash(struct rtas_validate_flash_t *args_buf)
{
	int token = rtas_token("ibm,validate-flash-image");
	int update_results;
	s32 rc;	

	rc = 0;
461
	do {
L
Linus Torvalds 已提交
462 463 464 465 466 467
		spin_lock(&rtas_data_buf_lock);
		memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE);
		rc = rtas_call(token, 2, 2, &update_results, 
			       (u32) __pa(rtas_data_buf), args_buf->buf_size);
		memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE);
		spin_unlock(&rtas_data_buf_lock);
468
	} while (rtas_busy_delay(rc));
L
Linus Torvalds 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

	args_buf->status = rc;
	args_buf->update_results = update_results;
}

static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf, 
		                   char *msg)
{
	int n;

	if (args_buf->status >= VALIDATE_TMP_UPDATE) { 
		n = sprintf(msg, "%d\n", args_buf->update_results);
		if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
		    (args_buf->update_results == VALIDATE_TMP_UPDATE))
			n += sprintf(msg + n, "%s\n", args_buf->buf);
	} else {
		n = sprintf(msg, "%d\n", args_buf->status);
	}
	return n;
}

490
static ssize_t validate_flash_read(struct file *file, char __user *buf,
L
Linus Torvalds 已提交
491 492
			       size_t count, loff_t *ppos)
{
J
Josef Sipek 已提交
493
	struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
L
Linus Torvalds 已提交
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
	struct rtas_validate_flash_t *args_buf;
	char msg[RTAS_MSG_MAXLEN];
	int msglen;

	args_buf = (struct rtas_validate_flash_t *) dp->data;

	if (ppos && *ppos != 0)
		return 0;	/* be cheap */
	
	msglen = get_validate_flash_msg(args_buf, msg);
	if (msglen > count)
		msglen = count;

	if (!access_ok(VERIFY_WRITE, buf, msglen))
		return -EINVAL;

	if (copy_to_user(buf, msg, msglen))
		return -EFAULT;

	if (ppos)
		*ppos = msglen;
	return msglen;
}

518
static ssize_t validate_flash_write(struct file *file, const char __user *buf,
L
Linus Torvalds 已提交
519 520
				    size_t count, loff_t *off)
{
J
Josef Sipek 已提交
521
	struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
L
Linus Torvalds 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
	struct rtas_validate_flash_t *args_buf;
	int rc;

	args_buf = (struct rtas_validate_flash_t *) dp->data;

	if (dp->data == NULL) {
		dp->data = kmalloc(sizeof(struct rtas_validate_flash_t), 
				GFP_KERNEL);
		if (dp->data == NULL) 
			return -ENOMEM;
	}

	/* We are only interested in the first 4K of the
	 * candidate image */
	if ((*off >= VALIDATE_BUF_SIZE) || 
		(args_buf->status == VALIDATE_AUTH)) {
		*off += count;
		return count;
	}

	if (*off + count >= VALIDATE_BUF_SIZE)  {
		count = VALIDATE_BUF_SIZE - *off;
		args_buf->status = VALIDATE_READY;	
	} else {
		args_buf->status = VALIDATE_INCOMPLETE;
	}

	if (!access_ok(VERIFY_READ, buf, count)) {
		rc = -EFAULT;
		goto done;
	}
	if (copy_from_user(args_buf->buf + *off, buf, count)) {
		rc = -EFAULT;
		goto done;
	}

	*off += count;
	rc = count;
done:
	if (rc < 0) {
		kfree(dp->data);
		dp->data = NULL;
	}
	return rc;
}

static int validate_flash_release(struct inode *inode, struct file *file)
{
J
Josef Sipek 已提交
570
	struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
L
Linus Torvalds 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	struct rtas_validate_flash_t *args_buf;

	args_buf = (struct rtas_validate_flash_t *) dp->data;

	if (args_buf->status == VALIDATE_READY) {
		args_buf->buf_size = VALIDATE_BUF_SIZE;
		validate_flash(args_buf);
	}

	/* The matching atomic_inc was in rtas_excl_open() */
	atomic_dec(&dp->count);

	return 0;
}

586 587 588 589 590 591 592
static void rtas_flash_firmware(int reboot_type)
{
	unsigned long image_size;
	struct flash_block_list *f, *next, *flist;
	unsigned long rtas_block_list;
	int i, status, update_token;

593
	if (rtas_firmware_flash_list == NULL)
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
		return;		/* nothing to do */

	if (reboot_type != SYS_RESTART) {
		printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
		printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
		return;
	}

	update_token = rtas_token("ibm,update-flash-64-and-reboot");
	if (update_token == RTAS_UNKNOWN_SERVICE) {
		printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot "
		       "is not available -- not a service partition?\n");
		printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
		return;
	}

610 611 612 613
	/*
	 * NOTE: the "first" block must be under 4GB, so we create
	 * an entry with no data blocks in the reserved buffer in
	 * the kernel data segment.
614
	 */
615 616 617 618
	spin_lock(&rtas_data_buf_lock);
	flist = (struct flash_block_list *)&rtas_data_buf[0];
	flist->num_blocks = 0;
	flist->next = rtas_firmware_flash_list;
619 620 621
	rtas_block_list = virt_to_abs(flist);
	if (rtas_block_list >= 4UL*1024*1024*1024) {
		printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
622
		spin_unlock(&rtas_data_buf_lock);
623 624 625 626 627
		return;
	}

	printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
	/* Update the block_list in place. */
628
	rtas_firmware_flash_list = NULL; /* too hard to backout on error */
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
	image_size = 0;
	for (f = flist; f; f = next) {
		/* Translate data addrs to absolute */
		for (i = 0; i < f->num_blocks; i++) {
			f->blocks[i].data = (char *)virt_to_abs(f->blocks[i].data);
			image_size += f->blocks[i].length;
		}
		next = f->next;
		/* Don't translate NULL pointer for last entry */
		if (f->next)
			f->next = (struct flash_block_list *)virt_to_abs(f->next);
		else
			f->next = NULL;
		/* make num_blocks into the version/length field */
		f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
	}

	printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
	printk(KERN_ALERT "FLASH: performing flash and reboot\n");
	rtas_progress("Flashing        \n", 0x0);
	rtas_progress("Please Wait...  ", 0x0);
	printk(KERN_ALERT "FLASH: this will take several minutes.  Do not power off!\n");
	status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
	switch (status) {	/* should only get "bad" status */
	    case 0:
		printk(KERN_ALERT "FLASH: success\n");
		break;
	    case -1:
		printk(KERN_ALERT "FLASH: hardware error.  Firmware may not be not flashed\n");
		break;
	    case -3:
		printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform.  Firmware not flashed\n");
		break;
	    case -4:
		printk(KERN_ALERT "FLASH: flash failed when partially complete.  System may not reboot\n");
		break;
	    default:
		printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
		break;
	}
669
	spin_unlock(&rtas_data_buf_lock);
670 671
}

L
Linus Torvalds 已提交
672 673 674
static void remove_flash_pde(struct proc_dir_entry *dp)
{
	if (dp) {
675
		kfree(dp->data);
L
Linus Torvalds 已提交
676 677 678 679 680 681 682 683 684 685 686
		remove_proc_entry(dp->name, dp->parent);
	}
}

static int initialize_flash_pde_data(const char *rtas_call_name,
				     size_t buf_size,
				     struct proc_dir_entry *dp)
{
	int *status;
	int token;

687
	dp->data = kzalloc(buf_size, GFP_KERNEL);
L
Linus Torvalds 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
	if (dp->data == NULL) {
		remove_flash_pde(dp);
		return -ENOMEM;
	}

	/*
	 * This code assumes that the status int is the first member of the
	 * struct 
	 */
	status = (int *) dp->data;
	token = rtas_token(rtas_call_name);
	if (token == RTAS_UNKNOWN_SERVICE)
		*status = FLASH_AUTH;
	else
		*status = FLASH_NO_OP;

	return 0;
}

static struct proc_dir_entry *create_flash_pde(const char *filename,
708
					       const struct file_operations *fops)
L
Linus Torvalds 已提交
709
{
710
	return proc_create(filename, S_IRUSR | S_IWUSR, NULL, fops);
L
Linus Torvalds 已提交
711 712
}

713
static const struct file_operations rtas_flash_operations = {
714
	.owner		= THIS_MODULE,
L
Linus Torvalds 已提交
715 716 717 718
	.read		= rtas_flash_read,
	.write		= rtas_flash_write,
	.open		= rtas_excl_open,
	.release	= rtas_flash_release,
719
	.llseek		= default_llseek,
L
Linus Torvalds 已提交
720 721
};

722
static const struct file_operations manage_flash_operations = {
723
	.owner		= THIS_MODULE,
L
Linus Torvalds 已提交
724 725 726 727
	.read		= manage_flash_read,
	.write		= manage_flash_write,
	.open		= rtas_excl_open,
	.release	= rtas_excl_release,
728
	.llseek		= default_llseek,
L
Linus Torvalds 已提交
729 730
};

731
static const struct file_operations validate_flash_operations = {
732
	.owner		= THIS_MODULE,
L
Linus Torvalds 已提交
733 734 735 736
	.read		= validate_flash_read,
	.write		= validate_flash_write,
	.open		= rtas_excl_open,
	.release	= validate_flash_release,
737
	.llseek		= default_llseek,
L
Linus Torvalds 已提交
738 739
};

740
static int __init rtas_flash_init(void)
L
Linus Torvalds 已提交
741 742 743 744 745 746 747 748 749
{
	int rc;

	if (rtas_token("ibm,update-flash-64-and-reboot") ==
		       RTAS_UNKNOWN_SERVICE) {
		printk(KERN_ERR "rtas_flash: no firmware flash support\n");
		return 1;
	}

750
	firmware_flash_pde = create_flash_pde("powerpc/rtas/"
L
Linus Torvalds 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763
					      FIRMWARE_FLASH_NAME,
					      &rtas_flash_operations);
	if (firmware_flash_pde == NULL) {
		rc = -ENOMEM;
		goto cleanup;
	}

	rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
			 	       sizeof(struct rtas_update_flash_t), 
				       firmware_flash_pde);
	if (rc != 0)
		goto cleanup;

764
	firmware_update_pde = create_flash_pde("powerpc/rtas/"
L
Linus Torvalds 已提交
765 766 767 768 769 770 771 772 773 774 775 776 777
					       FIRMWARE_UPDATE_NAME,
					       &rtas_flash_operations);
	if (firmware_update_pde == NULL) {
		rc = -ENOMEM;
		goto cleanup;
	}

	rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
			 	       sizeof(struct rtas_update_flash_t), 
				       firmware_update_pde);
	if (rc != 0)
		goto cleanup;

778
	validate_pde = create_flash_pde("powerpc/rtas/" VALIDATE_FLASH_NAME,
L
Linus Torvalds 已提交
779 780 781 782 783 784 785 786 787 788 789 790
			      		&validate_flash_operations);
	if (validate_pde == NULL) {
		rc = -ENOMEM;
		goto cleanup;
	}

	rc = initialize_flash_pde_data("ibm,validate-flash-image",
		                       sizeof(struct rtas_validate_flash_t), 
				       validate_pde);
	if (rc != 0)
		goto cleanup;

791
	manage_pde = create_flash_pde("powerpc/rtas/" MANAGE_FLASH_NAME,
L
Linus Torvalds 已提交
792 793 794 795 796 797 798 799 800 801 802 803
				      &manage_flash_operations);
	if (manage_pde == NULL) {
		rc = -ENOMEM;
		goto cleanup;
	}

	rc = initialize_flash_pde_data("ibm,manage-flash-image",
			               sizeof(struct rtas_manage_flash_t),
				       manage_pde);
	if (rc != 0)
		goto cleanup;

804
	rtas_flash_term_hook = rtas_flash_firmware;
805 806 807

	flash_block_cache = kmem_cache_create("rtas_flash_cache",
				RTAS_BLK_SIZE, RTAS_BLK_SIZE, 0,
808
				rtas_block_ctor);
809 810
	if (!flash_block_cache) {
		printk(KERN_ERR "%s: failed to create block cache\n",
811
				__func__);
812 813 814
		rc = -ENOMEM;
		goto cleanup;
	}
L
Linus Torvalds 已提交
815 816 817 818 819 820 821 822 823 824 825
	return 0;

cleanup:
	remove_flash_pde(firmware_flash_pde);
	remove_flash_pde(firmware_update_pde);
	remove_flash_pde(validate_pde);
	remove_flash_pde(manage_pde);

	return rc;
}

826
static void __exit rtas_flash_cleanup(void)
L
Linus Torvalds 已提交
827
{
828
	rtas_flash_term_hook = NULL;
829 830 831 832

	if (flash_block_cache)
		kmem_cache_destroy(flash_block_cache);

L
Linus Torvalds 已提交
833 834 835 836 837 838 839 840 841
	remove_flash_pde(firmware_flash_pde);
	remove_flash_pde(firmware_update_pde);
	remove_flash_pde(validate_pde);
	remove_flash_pde(manage_pde);
}

module_init(rtas_flash_init);
module_exit(rtas_flash_cleanup);
MODULE_LICENSE("GPL");