2 * c 2001 PPC 64 Team, IBM Corp
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.
9 * /proc/ppc64/rtas/firmware_flash interface
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).
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/proc_fs.h>
19 #include <asm/delay.h>
20 #include <asm/uaccess.h>
22 #include <asm/abs_addr.h>
24 #define MODULE_VERS "1.0"
25 #define MODULE_NAME "rtas_flash"
27 #define FIRMWARE_FLASH_NAME "firmware_flash"
28 #define FIRMWARE_UPDATE_NAME "firmware_update"
29 #define MANAGE_FLASH_NAME "manage_flash"
30 #define VALIDATE_FLASH_NAME "validate_flash"
32 /* General RTAS Status Codes */
33 #define RTAS_RC_SUCCESS 0
34 #define RTAS_RC_HW_ERR -1
35 #define RTAS_RC_BUSY -2
37 /* Flash image status values */
38 #define FLASH_AUTH -9002 /* RTAS Not Service Authority Partition */
39 #define FLASH_NO_OP -1099 /* No operation initiated by user */
40 #define FLASH_IMG_SHORT -1005 /* Flash image shorter than expected */
41 #define FLASH_IMG_BAD_LEN -1004 /* Bad length value in flash list block */
42 #define FLASH_IMG_NULL_DATA -1003 /* Bad data value in flash list block */
43 #define FLASH_IMG_READY 0 /* Firmware img ready for flash on reboot */
45 /* Manage image status values */
46 #define MANAGE_AUTH -9002 /* RTAS Not Service Authority Partition */
47 #define MANAGE_ACTIVE_ERR -9001 /* RTAS Cannot Overwrite Active Img */
48 #define MANAGE_NO_OP -1099 /* No operation initiated by user */
49 #define MANAGE_PARAM_ERR -3 /* RTAS Parameter Error */
50 #define MANAGE_HW_ERR -1 /* RTAS Hardware Error */
52 /* Validate image status values */
53 #define VALIDATE_AUTH -9002 /* RTAS Not Service Authority Partition */
54 #define VALIDATE_NO_OP -1099 /* No operation initiated by the user */
55 #define VALIDATE_INCOMPLETE -1002 /* User copied < VALIDATE_BUF_SIZE */
56 #define VALIDATE_READY -1001 /* Firmware image ready for validation */
57 #define VALIDATE_PARAM_ERR -3 /* RTAS Parameter Error */
58 #define VALIDATE_HW_ERR -1 /* RTAS Hardware Error */
59 #define VALIDATE_TMP_UPDATE 0 /* Validate Return Status */
60 #define VALIDATE_FLASH_AUTH 1 /* Validate Return Status */
61 #define VALIDATE_INVALID_IMG 2 /* Validate Return Status */
62 #define VALIDATE_CUR_UNKNOWN 3 /* Validate Return Status */
63 #define VALIDATE_TMP_COMMIT_DL 4 /* Validate Return Status */
64 #define VALIDATE_TMP_COMMIT 5 /* Validate Return Status */
65 #define VALIDATE_TMP_UPDATE_DL 6 /* Validate Return Status */
67 /* ibm,manage-flash-image operation tokens */
68 #define RTAS_REJECT_TMP_IMG 0
69 #define RTAS_COMMIT_TMP_IMG 1
72 #define VALIDATE_BUF_SIZE 4096
73 #define RTAS_MSG_MAXLEN 64
80 /* This struct is very similar but not identical to
81 * that needed by the rtas flash update.
82 * All we need to do for rtas is rewrite num_blocks
83 * into a version/length and translate the pointers
86 #define FLASH_BLOCKS_PER_NODE ((PAGE_SIZE - 16) / sizeof(struct flash_block))
87 struct flash_block_list {
88 unsigned long num_blocks;
89 struct flash_block_list *next;
90 struct flash_block blocks[FLASH_BLOCKS_PER_NODE];
92 struct flash_block_list_header { /* just the header of flash_block_list */
93 unsigned long num_blocks;
94 struct flash_block_list *next;
97 static struct flash_block_list_header rtas_firmware_flash_list = {0, NULL};
99 #define FLASH_BLOCK_LIST_VERSION (1UL)
101 /* Local copy of the flash block list.
102 * We only allow one open of the flash proc file and create this
103 * list as we go. This list will be put in the
104 * rtas_firmware_flash_list var once it is fully read.
106 * For convenience as we build the list we use virtual addrs,
107 * we do not fill in the version number, and the length field
108 * is treated as the number of entries currently in the block
109 * (i.e. not a byte count). This is all fixed on release.
112 /* Status int must be first member of struct */
113 struct rtas_update_flash_t
115 int status; /* Flash update status */
116 struct flash_block_list *flist; /* Local copy of flash block list */
119 /* Status int must be first member of struct */
120 struct rtas_manage_flash_t
122 int status; /* Returned status */
123 unsigned int op; /* Reject or commit image */
126 /* Status int must be first member of struct */
127 struct rtas_validate_flash_t
129 int status; /* Returned status */
130 char buf[VALIDATE_BUF_SIZE]; /* Candidate image buffer */
131 unsigned int buf_size; /* Size of image buf */
132 unsigned int update_results; /* Update results token */
135 static DEFINE_SPINLOCK(flash_file_open_lock);
136 static struct proc_dir_entry *firmware_flash_pde;
137 static struct proc_dir_entry *firmware_update_pde;
138 static struct proc_dir_entry *validate_pde;
139 static struct proc_dir_entry *manage_pde;
141 /* Do simple sanity checks on the flash image. */
142 static int flash_list_valid(struct flash_block_list *flist)
144 struct flash_block_list *f;
146 unsigned long block_size, image_size;
148 /* Paranoid self test here. We also collect the image size. */
150 for (f = flist; f; f = f->next) {
151 for (i = 0; i < f->num_blocks; i++) {
152 if (f->blocks[i].data == NULL) {
153 return FLASH_IMG_NULL_DATA;
155 block_size = f->blocks[i].length;
156 if (block_size <= 0 || block_size > PAGE_SIZE) {
157 return FLASH_IMG_BAD_LEN;
159 image_size += block_size;
163 if (image_size < (256 << 10)) {
168 printk(KERN_INFO "FLASH: flash image with %ld bytes stored for hardware flash on reboot\n", image_size);
170 return FLASH_IMG_READY;
173 static void free_flash_list(struct flash_block_list *f)
175 struct flash_block_list *next;
179 for (i = 0; i < f->num_blocks; i++)
180 free_page((unsigned long)(f->blocks[i].data));
182 free_page((unsigned long)f);
187 static int rtas_flash_release(struct inode *inode, struct file *file)
189 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
190 struct rtas_update_flash_t *uf;
192 uf = (struct rtas_update_flash_t *) dp->data;
194 /* File was opened in write mode for a new flash attempt */
195 /* Clear saved list */
196 if (rtas_firmware_flash_list.next) {
197 free_flash_list(rtas_firmware_flash_list.next);
198 rtas_firmware_flash_list.next = NULL;
201 if (uf->status != FLASH_AUTH)
202 uf->status = flash_list_valid(uf->flist);
204 if (uf->status == FLASH_IMG_READY)
205 rtas_firmware_flash_list.next = uf->flist;
207 free_flash_list(uf->flist);
212 atomic_dec(&dp->count);
216 static void get_flash_status_msg(int status, char *buf)
222 msg = "error: this partition does not have service authority\n";
225 msg = "info: no firmware image for flash\n";
227 case FLASH_IMG_SHORT:
228 msg = "error: flash image short\n";
230 case FLASH_IMG_BAD_LEN:
231 msg = "error: internal error bad length\n";
233 case FLASH_IMG_NULL_DATA:
234 msg = "error: internal error null data\n";
236 case FLASH_IMG_READY:
237 msg = "ready: firmware image ready for flash on reboot\n";
240 sprintf(buf, "error: unexpected status value %d\n", status);
247 /* Reading the proc file will show status (not the firmware contents) */
248 static ssize_t rtas_flash_read(struct file *file, char __user *buf,
249 size_t count, loff_t *ppos)
251 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
252 struct rtas_update_flash_t *uf;
253 char msg[RTAS_MSG_MAXLEN];
256 uf = (struct rtas_update_flash_t *) dp->data;
258 if (!strcmp(dp->name, FIRMWARE_FLASH_NAME)) {
259 get_flash_status_msg(uf->status, msg);
260 } else { /* FIRMWARE_UPDATE_NAME */
261 sprintf(msg, "%d\n", uf->status);
263 msglen = strlen(msg);
267 if (ppos && *ppos != 0)
268 return 0; /* be cheap */
270 if (!access_ok(VERIFY_WRITE, buf, msglen))
273 if (copy_to_user(buf, msg, msglen))
281 /* We could be much more efficient here. But to keep this function
282 * simple we allocate a page to the block list no matter how small the
283 * count is. If the system is low on memory it will be just as well
286 static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
287 size_t count, loff_t *off)
289 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
290 struct rtas_update_flash_t *uf;
293 struct flash_block_list *fl;
295 uf = (struct rtas_update_flash_t *) dp->data;
297 if (uf->status == FLASH_AUTH || count == 0)
298 return count; /* discard data */
300 /* In the case that the image is not ready for flashing, the memory
301 * allocated for the block list will be freed upon the release of the
304 if (uf->flist == NULL) {
305 uf->flist = (struct flash_block_list *) get_zeroed_page(GFP_KERNEL);
312 fl = fl->next; /* seek to last block_list for append */
313 next_free = fl->num_blocks;
314 if (next_free == FLASH_BLOCKS_PER_NODE) {
315 /* Need to allocate another block_list */
316 fl->next = (struct flash_block_list *)get_zeroed_page(GFP_KERNEL);
323 if (count > PAGE_SIZE)
325 p = (char *)get_zeroed_page(GFP_KERNEL);
329 if(copy_from_user(p, buffer, count)) {
330 free_page((unsigned long)p);
333 fl->blocks[next_free].data = p;
334 fl->blocks[next_free].length = count;
340 static int rtas_excl_open(struct inode *inode, struct file *file)
342 struct proc_dir_entry *dp = PDE(inode);
344 /* Enforce exclusive open with use count of PDE */
345 spin_lock(&flash_file_open_lock);
346 if (atomic_read(&dp->count) > 1) {
347 spin_unlock(&flash_file_open_lock);
351 atomic_inc(&dp->count);
352 spin_unlock(&flash_file_open_lock);
357 static int rtas_excl_release(struct inode *inode, struct file *file)
359 struct proc_dir_entry *dp = PDE(inode);
361 atomic_dec(&dp->count);
366 static void manage_flash(struct rtas_manage_flash_t *args_buf)
368 unsigned int wait_time;
372 rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1,
373 1, NULL, args_buf->op);
374 if (rc == RTAS_RC_BUSY)
376 else if (rtas_is_extended_busy(rc)) {
377 wait_time = rtas_extended_busy_delay_time(rc);
378 udelay(wait_time * 1000);
383 args_buf->status = rc;
386 static ssize_t manage_flash_read(struct file *file, char __user *buf,
387 size_t count, loff_t *ppos)
389 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
390 struct rtas_manage_flash_t *args_buf;
391 char msg[RTAS_MSG_MAXLEN];
394 args_buf = (struct rtas_manage_flash_t *) dp->data;
395 if (args_buf == NULL)
398 msglen = sprintf(msg, "%d\n", args_buf->status);
402 if (ppos && *ppos != 0)
403 return 0; /* be cheap */
405 if (!access_ok(VERIFY_WRITE, buf, msglen))
408 if (copy_to_user(buf, msg, msglen))
416 static ssize_t manage_flash_write(struct file *file, const char __user *buf,
417 size_t count, loff_t *off)
419 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
420 struct rtas_manage_flash_t *args_buf;
421 const char reject_str[] = "0";
422 const char commit_str[] = "1";
426 args_buf = (struct rtas_manage_flash_t *) dp->data;
427 if ((args_buf->status == MANAGE_AUTH) || (count == 0))
432 if (count > 9) count = 9;
433 if (copy_from_user (stkbuf, buf, count)) {
436 if (strncmp(stkbuf, reject_str, strlen(reject_str)) == 0)
437 op = RTAS_REJECT_TMP_IMG;
438 else if (strncmp(stkbuf, commit_str, strlen(commit_str)) == 0)
439 op = RTAS_COMMIT_TMP_IMG;
442 if (op == -1) /* buf is empty, or contains invalid string */
446 manage_flash(args_buf);
451 static void validate_flash(struct rtas_validate_flash_t *args_buf)
453 int token = rtas_token("ibm,validate-flash-image");
454 unsigned int wait_time;
460 spin_lock(&rtas_data_buf_lock);
461 memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE);
462 rc = rtas_call(token, 2, 2, &update_results,
463 (u32) __pa(rtas_data_buf), args_buf->buf_size);
464 memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE);
465 spin_unlock(&rtas_data_buf_lock);
467 if (rc == RTAS_RC_BUSY)
469 else if (rtas_is_extended_busy(rc)) {
470 wait_time = rtas_extended_busy_delay_time(rc);
471 udelay(wait_time * 1000);
476 args_buf->status = rc;
477 args_buf->update_results = update_results;
480 static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf,
485 if (args_buf->status >= VALIDATE_TMP_UPDATE) {
486 n = sprintf(msg, "%d\n", args_buf->update_results);
487 if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
488 (args_buf->update_results == VALIDATE_TMP_UPDATE))
489 n += sprintf(msg + n, "%s\n", args_buf->buf);
491 n = sprintf(msg, "%d\n", args_buf->status);
496 static ssize_t validate_flash_read(struct file *file, char __user *buf,
497 size_t count, loff_t *ppos)
499 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
500 struct rtas_validate_flash_t *args_buf;
501 char msg[RTAS_MSG_MAXLEN];
504 args_buf = (struct rtas_validate_flash_t *) dp->data;
506 if (ppos && *ppos != 0)
507 return 0; /* be cheap */
509 msglen = get_validate_flash_msg(args_buf, msg);
513 if (!access_ok(VERIFY_WRITE, buf, msglen))
516 if (copy_to_user(buf, msg, msglen))
524 static ssize_t validate_flash_write(struct file *file, const char __user *buf,
525 size_t count, loff_t *off)
527 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
528 struct rtas_validate_flash_t *args_buf;
531 args_buf = (struct rtas_validate_flash_t *) dp->data;
533 if (dp->data == NULL) {
534 dp->data = kmalloc(sizeof(struct rtas_validate_flash_t),
536 if (dp->data == NULL)
540 /* We are only interested in the first 4K of the
542 if ((*off >= VALIDATE_BUF_SIZE) ||
543 (args_buf->status == VALIDATE_AUTH)) {
548 if (*off + count >= VALIDATE_BUF_SIZE) {
549 count = VALIDATE_BUF_SIZE - *off;
550 args_buf->status = VALIDATE_READY;
552 args_buf->status = VALIDATE_INCOMPLETE;
555 if (!access_ok(VERIFY_READ, buf, count)) {
559 if (copy_from_user(args_buf->buf + *off, buf, count)) {
574 static int validate_flash_release(struct inode *inode, struct file *file)
576 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
577 struct rtas_validate_flash_t *args_buf;
579 args_buf = (struct rtas_validate_flash_t *) dp->data;
581 if (args_buf->status == VALIDATE_READY) {
582 args_buf->buf_size = VALIDATE_BUF_SIZE;
583 validate_flash(args_buf);
586 /* The matching atomic_inc was in rtas_excl_open() */
587 atomic_dec(&dp->count);
592 static void rtas_flash_firmware(int reboot_type)
594 unsigned long image_size;
595 struct flash_block_list *f, *next, *flist;
596 unsigned long rtas_block_list;
597 int i, status, update_token;
599 if (rtas_firmware_flash_list.next == NULL)
600 return; /* nothing to do */
602 if (reboot_type != SYS_RESTART) {
603 printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
604 printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
608 update_token = rtas_token("ibm,update-flash-64-and-reboot");
609 if (update_token == RTAS_UNKNOWN_SERVICE) {
610 printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot "
611 "is not available -- not a service partition?\n");
612 printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
616 /* NOTE: the "first" block list is a global var with no data
617 * blocks in the kernel data segment. We do this because
618 * we want to ensure this block_list addr is under 4GB.
620 rtas_firmware_flash_list.num_blocks = 0;
621 flist = (struct flash_block_list *)&rtas_firmware_flash_list;
622 rtas_block_list = virt_to_abs(flist);
623 if (rtas_block_list >= 4UL*1024*1024*1024) {
624 printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
628 printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
629 /* Update the block_list in place. */
631 for (f = flist; f; f = next) {
632 /* Translate data addrs to absolute */
633 for (i = 0; i < f->num_blocks; i++) {
634 f->blocks[i].data = (char *)virt_to_abs(f->blocks[i].data);
635 image_size += f->blocks[i].length;
638 /* Don't translate NULL pointer for last entry */
640 f->next = (struct flash_block_list *)virt_to_abs(f->next);
643 /* make num_blocks into the version/length field */
644 f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
647 printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
648 printk(KERN_ALERT "FLASH: performing flash and reboot\n");
649 rtas_progress("Flashing \n", 0x0);
650 rtas_progress("Please Wait... ", 0x0);
651 printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n");
652 status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
653 switch (status) { /* should only get "bad" status */
655 printk(KERN_ALERT "FLASH: success\n");
658 printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n");
661 printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
664 printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n");
667 printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
672 static void remove_flash_pde(struct proc_dir_entry *dp)
677 remove_proc_entry(dp->name, dp->parent);
681 static int initialize_flash_pde_data(const char *rtas_call_name,
683 struct proc_dir_entry *dp)
688 dp->data = kmalloc(buf_size, GFP_KERNEL);
689 if (dp->data == NULL) {
690 remove_flash_pde(dp);
694 memset(dp->data, 0, buf_size);
697 * This code assumes that the status int is the first member of the
700 status = (int *) dp->data;
701 token = rtas_token(rtas_call_name);
702 if (token == RTAS_UNKNOWN_SERVICE)
703 *status = FLASH_AUTH;
705 *status = FLASH_NO_OP;
710 static struct proc_dir_entry *create_flash_pde(const char *filename,
711 struct file_operations *fops)
713 struct proc_dir_entry *ent = NULL;
715 ent = create_proc_entry(filename, S_IRUSR | S_IWUSR, NULL);
718 ent->proc_fops = fops;
719 ent->owner = THIS_MODULE;
725 static struct file_operations rtas_flash_operations = {
726 .read = rtas_flash_read,
727 .write = rtas_flash_write,
728 .open = rtas_excl_open,
729 .release = rtas_flash_release,
732 static struct file_operations manage_flash_operations = {
733 .read = manage_flash_read,
734 .write = manage_flash_write,
735 .open = rtas_excl_open,
736 .release = rtas_excl_release,
739 static struct file_operations validate_flash_operations = {
740 .read = validate_flash_read,
741 .write = validate_flash_write,
742 .open = rtas_excl_open,
743 .release = validate_flash_release,
746 int __init rtas_flash_init(void)
750 if (rtas_token("ibm,update-flash-64-and-reboot") ==
751 RTAS_UNKNOWN_SERVICE) {
752 printk(KERN_ERR "rtas_flash: no firmware flash support\n");
756 firmware_flash_pde = create_flash_pde("ppc64/rtas/"
758 &rtas_flash_operations);
759 if (firmware_flash_pde == NULL) {
764 rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
765 sizeof(struct rtas_update_flash_t),
770 firmware_update_pde = create_flash_pde("ppc64/rtas/"
771 FIRMWARE_UPDATE_NAME,
772 &rtas_flash_operations);
773 if (firmware_update_pde == NULL) {
778 rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
779 sizeof(struct rtas_update_flash_t),
780 firmware_update_pde);
784 validate_pde = create_flash_pde("ppc64/rtas/" VALIDATE_FLASH_NAME,
785 &validate_flash_operations);
786 if (validate_pde == NULL) {
791 rc = initialize_flash_pde_data("ibm,validate-flash-image",
792 sizeof(struct rtas_validate_flash_t),
797 manage_pde = create_flash_pde("ppc64/rtas/" MANAGE_FLASH_NAME,
798 &manage_flash_operations);
799 if (manage_pde == NULL) {
804 rc = initialize_flash_pde_data("ibm,manage-flash-image",
805 sizeof(struct rtas_manage_flash_t),
810 rtas_flash_term_hook = rtas_flash_firmware;
814 remove_flash_pde(firmware_flash_pde);
815 remove_flash_pde(firmware_update_pde);
816 remove_flash_pde(validate_pde);
817 remove_flash_pde(manage_pde);
822 void __exit rtas_flash_cleanup(void)
824 rtas_flash_term_hook = NULL;
825 remove_flash_pde(firmware_flash_pde);
826 remove_flash_pde(firmware_update_pde);
827 remove_flash_pde(validate_pde);
828 remove_flash_pde(manage_pde);
831 module_init(rtas_flash_init);
832 module_exit(rtas_flash_cleanup);
833 MODULE_LICENSE("GPL");