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/powerpc/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/slab.h>
19 #include <linux/proc_fs.h>
20 #include <linux/reboot.h>
21 #include <asm/delay.h>
22 #include <asm/uaccess.h>
25 #define MODULE_VERS "1.0"
26 #define MODULE_NAME "rtas_flash"
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"
33 /* General RTAS Status Codes */
34 #define RTAS_RC_SUCCESS 0
35 #define RTAS_RC_HW_ERR -1
36 #define RTAS_RC_BUSY -2
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 */
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 */
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 */
68 /* ibm,manage-flash-image operation tokens */
69 #define RTAS_REJECT_TMP_IMG 0
70 #define RTAS_COMMIT_TMP_IMG 1
73 #define VALIDATE_BUF_SIZE 4096
74 #define RTAS_MSG_MAXLEN 64
76 /* Quirk - RTAS requires 4k list length and block size */
77 #define RTAS_BLKLIST_LENGTH 4096
78 #define RTAS_BLK_SIZE 4096
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
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];
98 static struct flash_block_list *rtas_firmware_flash_list;
100 /* Use slab cache to guarantee 4k alignment */
101 static struct kmem_cache *flash_block_cache = NULL;
103 #define FLASH_BLOCK_LIST_VERSION (1UL)
105 /* Local copy of the flash block list.
106 * We only allow one open of the flash proc file and create this
107 * list as we go. The rtas_firmware_flash_list varable will be
108 * set once the data is fully read.
110 * For convenience as we build the list we use virtual addrs,
111 * we do not fill in the version number, and the length field
112 * is treated as the number of entries currently in the block
113 * (i.e. not a byte count). This is all fixed when calling
117 /* Status int must be first member of struct */
118 struct rtas_update_flash_t
120 int status; /* Flash update status */
121 struct flash_block_list *flist; /* Local copy of flash block list */
124 /* Status int must be first member of struct */
125 struct rtas_manage_flash_t
127 int status; /* Returned status */
128 unsigned int op; /* Reject or commit image */
131 /* Status int must be first member of struct */
132 struct rtas_validate_flash_t
134 int status; /* Returned status */
135 char buf[VALIDATE_BUF_SIZE]; /* Candidate image buffer */
136 unsigned int buf_size; /* Size of image buf */
137 unsigned int update_results; /* Update results token */
140 static DEFINE_SPINLOCK(flash_file_open_lock);
141 static struct proc_dir_entry *firmware_flash_pde;
142 static struct proc_dir_entry *firmware_update_pde;
143 static struct proc_dir_entry *validate_pde;
144 static struct proc_dir_entry *manage_pde;
146 /* Do simple sanity checks on the flash image. */
147 static int flash_list_valid(struct flash_block_list *flist)
149 struct flash_block_list *f;
151 unsigned long block_size, image_size;
153 /* Paranoid self test here. We also collect the image size. */
155 for (f = flist; f; f = f->next) {
156 for (i = 0; i < f->num_blocks; i++) {
157 if (f->blocks[i].data == NULL) {
158 return FLASH_IMG_NULL_DATA;
160 block_size = f->blocks[i].length;
161 if (block_size <= 0 || block_size > RTAS_BLK_SIZE) {
162 return FLASH_IMG_BAD_LEN;
164 image_size += block_size;
168 if (image_size < (256 << 10)) {
173 printk(KERN_INFO "FLASH: flash image with %ld bytes stored for hardware flash on reboot\n", image_size);
175 return FLASH_IMG_READY;
178 static void free_flash_list(struct flash_block_list *f)
180 struct flash_block_list *next;
184 for (i = 0; i < f->num_blocks; i++)
185 kmem_cache_free(flash_block_cache, f->blocks[i].data);
187 kmem_cache_free(flash_block_cache, f);
192 static int rtas_flash_release(struct inode *inode, struct file *file)
194 struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
195 struct rtas_update_flash_t *uf;
197 uf = (struct rtas_update_flash_t *) dp->data;
199 /* File was opened in write mode for a new flash attempt */
200 /* Clear saved list */
201 if (rtas_firmware_flash_list) {
202 free_flash_list(rtas_firmware_flash_list);
203 rtas_firmware_flash_list = NULL;
206 if (uf->status != FLASH_AUTH)
207 uf->status = flash_list_valid(uf->flist);
209 if (uf->status == FLASH_IMG_READY)
210 rtas_firmware_flash_list = uf->flist;
212 free_flash_list(uf->flist);
217 atomic_dec(&dp->count);
221 static void get_flash_status_msg(int status, char *buf)
227 msg = "error: this partition does not have service authority\n";
230 msg = "info: no firmware image for flash\n";
232 case FLASH_IMG_SHORT:
233 msg = "error: flash image short\n";
235 case FLASH_IMG_BAD_LEN:
236 msg = "error: internal error bad length\n";
238 case FLASH_IMG_NULL_DATA:
239 msg = "error: internal error null data\n";
241 case FLASH_IMG_READY:
242 msg = "ready: firmware image ready for flash on reboot\n";
245 sprintf(buf, "error: unexpected status value %d\n", status);
252 /* Reading the proc file will show status (not the firmware contents) */
253 static ssize_t rtas_flash_read(struct file *file, char __user *buf,
254 size_t count, loff_t *ppos)
256 struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
257 struct rtas_update_flash_t *uf;
258 char msg[RTAS_MSG_MAXLEN];
262 if (!strcmp(dp->name, FIRMWARE_FLASH_NAME)) {
263 get_flash_status_msg(uf->status, msg);
264 } else { /* FIRMWARE_UPDATE_NAME */
265 sprintf(msg, "%d\n", uf->status);
268 return simple_read_from_buffer(buf, count, ppos, msg, strlen(msg));
271 /* constructor for flash_block_cache */
272 void rtas_block_ctor(void *ptr)
274 memset(ptr, 0, RTAS_BLK_SIZE);
277 /* We could be much more efficient here. But to keep this function
278 * simple we allocate a page to the block list no matter how small the
279 * count is. If the system is low on memory it will be just as well
282 static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
283 size_t count, loff_t *off)
285 struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
286 struct rtas_update_flash_t *uf;
289 struct flash_block_list *fl;
291 uf = (struct rtas_update_flash_t *) dp->data;
293 if (uf->status == FLASH_AUTH || count == 0)
294 return count; /* discard data */
296 /* In the case that the image is not ready for flashing, the memory
297 * allocated for the block list will be freed upon the release of the
300 if (uf->flist == NULL) {
301 uf->flist = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
308 fl = fl->next; /* seek to last block_list for append */
309 next_free = fl->num_blocks;
310 if (next_free == FLASH_BLOCKS_PER_NODE) {
311 /* Need to allocate another block_list */
312 fl->next = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
319 if (count > RTAS_BLK_SIZE)
320 count = RTAS_BLK_SIZE;
321 p = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
325 if(copy_from_user(p, buffer, count)) {
326 kmem_cache_free(flash_block_cache, p);
329 fl->blocks[next_free].data = p;
330 fl->blocks[next_free].length = count;
336 static int rtas_excl_open(struct inode *inode, struct file *file)
338 struct proc_dir_entry *dp = PDE(inode);
340 /* Enforce exclusive open with use count of PDE */
341 spin_lock(&flash_file_open_lock);
342 if (atomic_read(&dp->count) > 2) {
343 spin_unlock(&flash_file_open_lock);
347 atomic_inc(&dp->count);
348 spin_unlock(&flash_file_open_lock);
353 static int rtas_excl_release(struct inode *inode, struct file *file)
355 struct proc_dir_entry *dp = PDE(inode);
357 atomic_dec(&dp->count);
362 static void manage_flash(struct rtas_manage_flash_t *args_buf)
367 rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1,
368 1, NULL, args_buf->op);
369 } while (rtas_busy_delay(rc));
371 args_buf->status = rc;
374 static ssize_t manage_flash_read(struct file *file, char __user *buf,
375 size_t count, loff_t *ppos)
377 struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
378 struct rtas_manage_flash_t *args_buf;
379 char msg[RTAS_MSG_MAXLEN];
383 if (args_buf == NULL)
386 msglen = sprintf(msg, "%d\n", args_buf->status);
388 return simple_read_from_buffer(buf, count, ppos, msg, msglen);
391 static ssize_t manage_flash_write(struct file *file, const char __user *buf,
392 size_t count, loff_t *off)
394 struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
395 struct rtas_manage_flash_t *args_buf;
396 const char reject_str[] = "0";
397 const char commit_str[] = "1";
401 args_buf = (struct rtas_manage_flash_t *) dp->data;
402 if ((args_buf->status == MANAGE_AUTH) || (count == 0))
407 if (count > 9) count = 9;
408 if (copy_from_user (stkbuf, buf, count)) {
411 if (strncmp(stkbuf, reject_str, strlen(reject_str)) == 0)
412 op = RTAS_REJECT_TMP_IMG;
413 else if (strncmp(stkbuf, commit_str, strlen(commit_str)) == 0)
414 op = RTAS_COMMIT_TMP_IMG;
417 if (op == -1) /* buf is empty, or contains invalid string */
421 manage_flash(args_buf);
426 static void validate_flash(struct rtas_validate_flash_t *args_buf)
428 int token = rtas_token("ibm,validate-flash-image");
434 spin_lock(&rtas_data_buf_lock);
435 memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE);
436 rc = rtas_call(token, 2, 2, &update_results,
437 (u32) __pa(rtas_data_buf), args_buf->buf_size);
438 memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE);
439 spin_unlock(&rtas_data_buf_lock);
440 } while (rtas_busy_delay(rc));
442 args_buf->status = rc;
443 args_buf->update_results = update_results;
446 static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf,
451 if (args_buf->status >= VALIDATE_TMP_UPDATE) {
452 n = sprintf(msg, "%d\n", args_buf->update_results);
453 if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
454 (args_buf->update_results == VALIDATE_TMP_UPDATE))
455 n += sprintf(msg + n, "%s\n", args_buf->buf);
457 n = sprintf(msg, "%d\n", args_buf->status);
462 static ssize_t validate_flash_read(struct file *file, char __user *buf,
463 size_t count, loff_t *ppos)
465 struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
466 struct rtas_validate_flash_t *args_buf;
467 char msg[RTAS_MSG_MAXLEN];
472 msglen = get_validate_flash_msg(args_buf, msg);
474 return simple_read_from_buffer(buf, count, ppos, msg, msglen);
477 static ssize_t validate_flash_write(struct file *file, const char __user *buf,
478 size_t count, loff_t *off)
480 struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
481 struct rtas_validate_flash_t *args_buf;
484 args_buf = (struct rtas_validate_flash_t *) dp->data;
486 if (dp->data == NULL) {
487 dp->data = kmalloc(sizeof(struct rtas_validate_flash_t),
489 if (dp->data == NULL)
493 /* We are only interested in the first 4K of the
495 if ((*off >= VALIDATE_BUF_SIZE) ||
496 (args_buf->status == VALIDATE_AUTH)) {
501 if (*off + count >= VALIDATE_BUF_SIZE) {
502 count = VALIDATE_BUF_SIZE - *off;
503 args_buf->status = VALIDATE_READY;
505 args_buf->status = VALIDATE_INCOMPLETE;
508 if (!access_ok(VERIFY_READ, buf, count)) {
512 if (copy_from_user(args_buf->buf + *off, buf, count)) {
527 static int validate_flash_release(struct inode *inode, struct file *file)
529 struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
530 struct rtas_validate_flash_t *args_buf;
532 args_buf = (struct rtas_validate_flash_t *) dp->data;
534 if (args_buf->status == VALIDATE_READY) {
535 args_buf->buf_size = VALIDATE_BUF_SIZE;
536 validate_flash(args_buf);
539 /* The matching atomic_inc was in rtas_excl_open() */
540 atomic_dec(&dp->count);
545 static void rtas_flash_firmware(int reboot_type)
547 unsigned long image_size;
548 struct flash_block_list *f, *next, *flist;
549 unsigned long rtas_block_list;
550 int i, status, update_token;
552 if (rtas_firmware_flash_list == NULL)
553 return; /* nothing to do */
555 if (reboot_type != SYS_RESTART) {
556 printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
557 printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
561 update_token = rtas_token("ibm,update-flash-64-and-reboot");
562 if (update_token == RTAS_UNKNOWN_SERVICE) {
563 printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot "
564 "is not available -- not a service partition?\n");
565 printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
570 * Just before starting the firmware flash, cancel the event scan work
571 * to avoid any soft lockup issues.
573 rtas_cancel_event_scan();
576 * NOTE: the "first" block must be under 4GB, so we create
577 * an entry with no data blocks in the reserved buffer in
578 * the kernel data segment.
580 spin_lock(&rtas_data_buf_lock);
581 flist = (struct flash_block_list *)&rtas_data_buf[0];
582 flist->num_blocks = 0;
583 flist->next = rtas_firmware_flash_list;
584 rtas_block_list = __pa(flist);
585 if (rtas_block_list >= 4UL*1024*1024*1024) {
586 printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
587 spin_unlock(&rtas_data_buf_lock);
591 printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
592 /* Update the block_list in place. */
593 rtas_firmware_flash_list = NULL; /* too hard to backout on error */
595 for (f = flist; f; f = next) {
596 /* Translate data addrs to absolute */
597 for (i = 0; i < f->num_blocks; i++) {
598 f->blocks[i].data = (char *)__pa(f->blocks[i].data);
599 image_size += f->blocks[i].length;
602 /* Don't translate NULL pointer for last entry */
604 f->next = (struct flash_block_list *)__pa(f->next);
607 /* make num_blocks into the version/length field */
608 f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
611 printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
612 printk(KERN_ALERT "FLASH: performing flash and reboot\n");
613 rtas_progress("Flashing \n", 0x0);
614 rtas_progress("Please Wait... ", 0x0);
615 printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n");
616 status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
617 switch (status) { /* should only get "bad" status */
619 printk(KERN_ALERT "FLASH: success\n");
622 printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n");
625 printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
628 printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n");
631 printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
634 spin_unlock(&rtas_data_buf_lock);
637 static void remove_flash_pde(struct proc_dir_entry *dp)
641 remove_proc_entry(dp->name, dp->parent);
645 static int initialize_flash_pde_data(const char *rtas_call_name,
647 struct proc_dir_entry *dp)
652 dp->data = kzalloc(buf_size, GFP_KERNEL);
653 if (dp->data == NULL) {
654 remove_flash_pde(dp);
659 * This code assumes that the status int is the first member of the
662 status = (int *) dp->data;
663 token = rtas_token(rtas_call_name);
664 if (token == RTAS_UNKNOWN_SERVICE)
665 *status = FLASH_AUTH;
667 *status = FLASH_NO_OP;
672 static struct proc_dir_entry *create_flash_pde(const char *filename,
673 const struct file_operations *fops)
675 return proc_create(filename, S_IRUSR | S_IWUSR, NULL, fops);
678 static const struct file_operations rtas_flash_operations = {
679 .owner = THIS_MODULE,
680 .read = rtas_flash_read,
681 .write = rtas_flash_write,
682 .open = rtas_excl_open,
683 .release = rtas_flash_release,
684 .llseek = default_llseek,
687 static const struct file_operations manage_flash_operations = {
688 .owner = THIS_MODULE,
689 .read = manage_flash_read,
690 .write = manage_flash_write,
691 .open = rtas_excl_open,
692 .release = rtas_excl_release,
693 .llseek = default_llseek,
696 static const struct file_operations validate_flash_operations = {
697 .owner = THIS_MODULE,
698 .read = validate_flash_read,
699 .write = validate_flash_write,
700 .open = rtas_excl_open,
701 .release = validate_flash_release,
702 .llseek = default_llseek,
705 static int __init rtas_flash_init(void)
709 if (rtas_token("ibm,update-flash-64-and-reboot") ==
710 RTAS_UNKNOWN_SERVICE) {
711 pr_info("rtas_flash: no firmware flash support\n");
715 firmware_flash_pde = create_flash_pde("powerpc/rtas/"
717 &rtas_flash_operations);
718 if (firmware_flash_pde == NULL) {
723 rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
724 sizeof(struct rtas_update_flash_t),
729 firmware_update_pde = create_flash_pde("powerpc/rtas/"
730 FIRMWARE_UPDATE_NAME,
731 &rtas_flash_operations);
732 if (firmware_update_pde == NULL) {
737 rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
738 sizeof(struct rtas_update_flash_t),
739 firmware_update_pde);
743 validate_pde = create_flash_pde("powerpc/rtas/" VALIDATE_FLASH_NAME,
744 &validate_flash_operations);
745 if (validate_pde == NULL) {
750 rc = initialize_flash_pde_data("ibm,validate-flash-image",
751 sizeof(struct rtas_validate_flash_t),
756 manage_pde = create_flash_pde("powerpc/rtas/" MANAGE_FLASH_NAME,
757 &manage_flash_operations);
758 if (manage_pde == NULL) {
763 rc = initialize_flash_pde_data("ibm,manage-flash-image",
764 sizeof(struct rtas_manage_flash_t),
769 rtas_flash_term_hook = rtas_flash_firmware;
771 flash_block_cache = kmem_cache_create("rtas_flash_cache",
772 RTAS_BLK_SIZE, RTAS_BLK_SIZE, 0,
774 if (!flash_block_cache) {
775 printk(KERN_ERR "%s: failed to create block cache\n",
783 remove_flash_pde(firmware_flash_pde);
784 remove_flash_pde(firmware_update_pde);
785 remove_flash_pde(validate_pde);
786 remove_flash_pde(manage_pde);
791 static void __exit rtas_flash_cleanup(void)
793 rtas_flash_term_hook = NULL;
795 if (flash_block_cache)
796 kmem_cache_destroy(flash_block_cache);
798 remove_flash_pde(firmware_flash_pde);
799 remove_flash_pde(firmware_update_pde);
800 remove_flash_pde(validate_pde);
801 remove_flash_pde(manage_pde);
804 module_init(rtas_flash_init);
805 module_exit(rtas_flash_cleanup);
806 MODULE_LICENSE("GPL");