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>
23 #define MODULE_VERS "1.0"
24 #define MODULE_NAME "rtas_flash"
26 #define FIRMWARE_FLASH_NAME "firmware_flash"
27 #define FIRMWARE_UPDATE_NAME "firmware_update"
28 #define MANAGE_FLASH_NAME "manage_flash"
29 #define VALIDATE_FLASH_NAME "validate_flash"
31 /* General RTAS Status Codes */
32 #define RTAS_RC_SUCCESS 0
33 #define RTAS_RC_HW_ERR -1
34 #define RTAS_RC_BUSY -2
36 /* Flash image status values */
37 #define FLASH_AUTH -9002 /* RTAS Not Service Authority Partition */
38 #define FLASH_NO_OP -1099 /* No operation initiated by user */
39 #define FLASH_IMG_SHORT -1005 /* Flash image shorter than expected */
40 #define FLASH_IMG_BAD_LEN -1004 /* Bad length value in flash list block */
41 #define FLASH_IMG_NULL_DATA -1003 /* Bad data value in flash list block */
42 #define FLASH_IMG_READY 0 /* Firmware img ready for flash on reboot */
44 /* Manage image status values */
45 #define MANAGE_AUTH -9002 /* RTAS Not Service Authority Partition */
46 #define MANAGE_ACTIVE_ERR -9001 /* RTAS Cannot Overwrite Active Img */
47 #define MANAGE_NO_OP -1099 /* No operation initiated by user */
48 #define MANAGE_PARAM_ERR -3 /* RTAS Parameter Error */
49 #define MANAGE_HW_ERR -1 /* RTAS Hardware Error */
51 /* Validate image status values */
52 #define VALIDATE_AUTH -9002 /* RTAS Not Service Authority Partition */
53 #define VALIDATE_NO_OP -1099 /* No operation initiated by the user */
54 #define VALIDATE_INCOMPLETE -1002 /* User copied < VALIDATE_BUF_SIZE */
55 #define VALIDATE_READY -1001 /* Firmware image ready for validation */
56 #define VALIDATE_PARAM_ERR -3 /* RTAS Parameter Error */
57 #define VALIDATE_HW_ERR -1 /* RTAS Hardware Error */
58 #define VALIDATE_TMP_UPDATE 0 /* Validate Return Status */
59 #define VALIDATE_FLASH_AUTH 1 /* Validate Return Status */
60 #define VALIDATE_INVALID_IMG 2 /* Validate Return Status */
61 #define VALIDATE_CUR_UNKNOWN 3 /* Validate Return Status */
62 #define VALIDATE_TMP_COMMIT_DL 4 /* Validate Return Status */
63 #define VALIDATE_TMP_COMMIT 5 /* Validate Return Status */
64 #define VALIDATE_TMP_UPDATE_DL 6 /* Validate Return Status */
66 /* ibm,manage-flash-image operation tokens */
67 #define RTAS_REJECT_TMP_IMG 0
68 #define RTAS_COMMIT_TMP_IMG 1
71 #define VALIDATE_BUF_SIZE 4096
72 #define RTAS_MSG_MAXLEN 64
74 /* Local copy of the flash block list.
75 * We only allow one open of the flash proc file and create this
76 * list as we go. This list will be put in the kernel's
77 * rtas_firmware_flash_list global var once it is fully read.
79 * For convenience as we build the list we use virtual addrs,
80 * we do not fill in the version number, and the length field
81 * is treated as the number of entries currently in the block
82 * (i.e. not a byte count). This is all fixed on release.
85 /* Status int must be first member of struct */
86 struct rtas_update_flash_t
88 int status; /* Flash update status */
89 struct flash_block_list *flist; /* Local copy of flash block list */
92 /* Status int must be first member of struct */
93 struct rtas_manage_flash_t
95 int status; /* Returned status */
96 unsigned int op; /* Reject or commit image */
99 /* Status int must be first member of struct */
100 struct rtas_validate_flash_t
102 int status; /* Returned status */
103 char buf[VALIDATE_BUF_SIZE]; /* Candidate image buffer */
104 unsigned int buf_size; /* Size of image buf */
105 unsigned int update_results; /* Update results token */
108 static spinlock_t flash_file_open_lock = SPIN_LOCK_UNLOCKED;
109 static struct proc_dir_entry *firmware_flash_pde;
110 static struct proc_dir_entry *firmware_update_pde;
111 static struct proc_dir_entry *validate_pde;
112 static struct proc_dir_entry *manage_pde;
114 /* Do simple sanity checks on the flash image. */
115 static int flash_list_valid(struct flash_block_list *flist)
117 struct flash_block_list *f;
119 unsigned long block_size, image_size;
121 /* Paranoid self test here. We also collect the image size. */
123 for (f = flist; f; f = f->next) {
124 for (i = 0; i < f->num_blocks; i++) {
125 if (f->blocks[i].data == NULL) {
126 return FLASH_IMG_NULL_DATA;
128 block_size = f->blocks[i].length;
129 if (block_size <= 0 || block_size > PAGE_SIZE) {
130 return FLASH_IMG_BAD_LEN;
132 image_size += block_size;
136 if (image_size < (256 << 10)) {
141 printk(KERN_INFO "FLASH: flash image with %ld bytes stored for hardware flash on reboot\n", image_size);
143 return FLASH_IMG_READY;
146 static void free_flash_list(struct flash_block_list *f)
148 struct flash_block_list *next;
152 for (i = 0; i < f->num_blocks; i++)
153 free_page((unsigned long)(f->blocks[i].data));
155 free_page((unsigned long)f);
160 static int rtas_flash_release(struct inode *inode, struct file *file)
162 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
163 struct rtas_update_flash_t *uf;
165 uf = (struct rtas_update_flash_t *) dp->data;
167 /* File was opened in write mode for a new flash attempt */
168 /* Clear saved list */
169 if (rtas_firmware_flash_list.next) {
170 free_flash_list(rtas_firmware_flash_list.next);
171 rtas_firmware_flash_list.next = NULL;
174 if (uf->status != FLASH_AUTH)
175 uf->status = flash_list_valid(uf->flist);
177 if (uf->status == FLASH_IMG_READY)
178 rtas_firmware_flash_list.next = uf->flist;
180 free_flash_list(uf->flist);
185 atomic_dec(&dp->count);
189 static void get_flash_status_msg(int status, char *buf)
195 msg = "error: this partition does not have service authority\n";
198 msg = "info: no firmware image for flash\n";
200 case FLASH_IMG_SHORT:
201 msg = "error: flash image short\n";
203 case FLASH_IMG_BAD_LEN:
204 msg = "error: internal error bad length\n";
206 case FLASH_IMG_NULL_DATA:
207 msg = "error: internal error null data\n";
209 case FLASH_IMG_READY:
210 msg = "ready: firmware image ready for flash on reboot\n";
213 sprintf(buf, "error: unexpected status value %d\n", status);
220 /* Reading the proc file will show status (not the firmware contents) */
221 static ssize_t rtas_flash_read(struct file *file, char *buf,
222 size_t count, loff_t *ppos)
224 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
225 struct rtas_update_flash_t *uf;
226 char msg[RTAS_MSG_MAXLEN];
230 uf = (struct rtas_update_flash_t *) dp->data;
232 if (!strcmp(dp->name, FIRMWARE_FLASH_NAME)) {
233 get_flash_status_msg(uf->status, msg);
234 } else { /* FIRMWARE_UPDATE_NAME */
235 sprintf(msg, "%d\n", uf->status);
237 msglen = strlen(msg);
241 if (ppos && *ppos != 0)
242 return 0; /* be cheap */
244 error = verify_area(VERIFY_WRITE, buf, msglen);
248 if (copy_to_user(buf, msg, msglen))
256 /* We could be much more efficient here. But to keep this function
257 * simple we allocate a page to the block list no matter how small the
258 * count is. If the system is low on memory it will be just as well
261 static ssize_t rtas_flash_write(struct file *file, const char *buffer,
262 size_t count, loff_t *off)
264 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
265 struct rtas_update_flash_t *uf;
268 struct flash_block_list *fl;
270 uf = (struct rtas_update_flash_t *) dp->data;
272 if (uf->status == FLASH_AUTH || count == 0)
273 return count; /* discard data */
275 /* In the case that the image is not ready for flashing, the memory
276 * allocated for the block list will be freed upon the release of the
279 if (uf->flist == NULL) {
280 uf->flist = (struct flash_block_list *) get_zeroed_page(GFP_KERNEL);
287 fl = fl->next; /* seek to last block_list for append */
288 next_free = fl->num_blocks;
289 if (next_free == FLASH_BLOCKS_PER_NODE) {
290 /* Need to allocate another block_list */
291 fl->next = (struct flash_block_list *)get_zeroed_page(GFP_KERNEL);
298 if (count > PAGE_SIZE)
300 p = (char *)get_zeroed_page(GFP_KERNEL);
304 if(copy_from_user(p, buffer, count)) {
305 free_page((unsigned long)p);
308 fl->blocks[next_free].data = p;
309 fl->blocks[next_free].length = count;
315 static int rtas_excl_open(struct inode *inode, struct file *file)
317 struct proc_dir_entry *dp = PDE(inode);
319 /* Enforce exclusive open with use count of PDE */
320 spin_lock(&flash_file_open_lock);
321 if (atomic_read(&dp->count) > 1) {
322 spin_unlock(&flash_file_open_lock);
326 atomic_inc(&dp->count);
327 spin_unlock(&flash_file_open_lock);
332 static int rtas_excl_release(struct inode *inode, struct file *file)
334 struct proc_dir_entry *dp = PDE(inode);
336 atomic_dec(&dp->count);
341 static void manage_flash(struct rtas_manage_flash_t *args_buf)
343 unsigned int wait_time;
347 rc = (s32) rtas_call(rtas_token("ibm,manage-flash-image"), 1,
348 1, NULL, (long) args_buf->op);
349 if (rc == RTAS_RC_BUSY)
351 else if (rtas_is_extended_busy(rc)) {
352 wait_time = rtas_extended_busy_delay_time(rc);
353 udelay(wait_time * 1000);
358 args_buf->status = rc;
361 static ssize_t manage_flash_read(struct file *file, char *buf,
362 size_t count, loff_t *ppos)
364 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
365 struct rtas_manage_flash_t *args_buf;
366 char msg[RTAS_MSG_MAXLEN];
370 args_buf = (struct rtas_manage_flash_t *) dp->data;
371 if (args_buf == NULL)
374 msglen = sprintf(msg, "%d\n", args_buf->status);
378 if (ppos && *ppos != 0)
379 return 0; /* be cheap */
381 error = verify_area(VERIFY_WRITE, buf, msglen);
385 if (copy_to_user(buf, msg, msglen))
393 static ssize_t manage_flash_write(struct file *file, const char *buf,
394 size_t count, loff_t *off)
396 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
397 struct rtas_manage_flash_t *args_buf;
398 const char reject_str[] = "0";
399 const char commit_str[] = "1";
403 args_buf = (struct rtas_manage_flash_t *) dp->data;
404 if ((args_buf->status == MANAGE_AUTH) || (count == 0))
409 if (count > 9) count = 9;
410 if (copy_from_user (stkbuf, buf, count)) {
413 if (strncmp(stkbuf, reject_str, strlen(reject_str)) == 0)
414 op = RTAS_REJECT_TMP_IMG;
415 else if (strncmp(stkbuf, commit_str, strlen(commit_str)) == 0)
416 op = RTAS_COMMIT_TMP_IMG;
419 if (op == -1) /* buf is empty, or contains invalid string */
423 manage_flash(args_buf);
428 static void validate_flash(struct rtas_validate_flash_t *args_buf)
430 int token = rtas_token("ibm,validate-flash-image");
431 unsigned int wait_time;
437 spin_lock(&rtas_data_buf_lock);
438 memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE);
439 rc = (s32) rtas_call(token, 2, 2, &update_results,
440 __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);
444 if (rc == RTAS_RC_BUSY)
446 else if (rtas_is_extended_busy(rc)) {
447 wait_time = rtas_extended_busy_delay_time(rc);
448 udelay(wait_time * 1000);
453 args_buf->status = rc;
454 args_buf->update_results = (u32) update_results;
457 static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf,
462 if (args_buf->status >= VALIDATE_TMP_UPDATE) {
463 n = sprintf(msg, "%d\n", args_buf->update_results);
464 if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
465 (args_buf->update_results == VALIDATE_TMP_UPDATE))
466 n += sprintf(msg + n, "%s\n", args_buf->buf);
468 n = sprintf(msg, "%d\n", args_buf->status);
473 static ssize_t validate_flash_read(struct file *file, char *buf,
474 size_t count, loff_t *ppos)
476 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
477 struct rtas_validate_flash_t *args_buf;
478 char msg[RTAS_MSG_MAXLEN];
482 args_buf = (struct rtas_validate_flash_t *) dp->data;
484 if (ppos && *ppos != 0)
485 return 0; /* be cheap */
487 msglen = get_validate_flash_msg(args_buf, msg);
491 error = verify_area(VERIFY_WRITE, buf, msglen);
495 if (copy_to_user(buf, msg, msglen))
503 static ssize_t validate_flash_write(struct file *file, const char *buf,
504 size_t count, loff_t *off)
506 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
507 struct rtas_validate_flash_t *args_buf;
510 args_buf = (struct rtas_validate_flash_t *) dp->data;
512 if (dp->data == NULL) {
513 dp->data = kmalloc(sizeof(struct rtas_validate_flash_t),
515 if (dp->data == NULL)
519 /* We are only interested in the first 4K of the
521 if ((*off >= VALIDATE_BUF_SIZE) ||
522 (args_buf->status == VALIDATE_AUTH)) {
527 if (*off + count >= VALIDATE_BUF_SIZE) {
528 count = VALIDATE_BUF_SIZE - *off;
529 args_buf->status = VALIDATE_READY;
531 args_buf->status = VALIDATE_INCOMPLETE;
534 if (verify_area(VERIFY_READ, buf, count)) {
538 if (copy_from_user(args_buf->buf + *off, buf, count)) {
553 static int validate_flash_release(struct inode *inode, struct file *file)
555 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
556 struct rtas_validate_flash_t *args_buf;
558 args_buf = (struct rtas_validate_flash_t *) dp->data;
560 if (args_buf->status == VALIDATE_READY) {
561 args_buf->buf_size = VALIDATE_BUF_SIZE;
562 validate_flash(args_buf);
565 atomic_dec(&dp->count);
570 static void remove_flash_pde(struct proc_dir_entry *dp)
573 if (dp->data != NULL)
575 remove_proc_entry(dp->name, NULL);
579 static int initialize_flash_pde_data(const char *rtas_call_name,
581 struct proc_dir_entry *dp)
586 dp->data = kmalloc(buf_size, GFP_KERNEL);
587 if (dp->data == NULL) {
588 remove_flash_pde(dp);
592 memset(dp->data, 0, buf_size);
595 * This code assumes that the status int is the first member of the
598 status = (int *) dp->data;
599 token = rtas_token(rtas_call_name);
600 if (token == RTAS_UNKNOWN_SERVICE)
601 *status = FLASH_AUTH;
603 *status = FLASH_NO_OP;
608 static struct proc_dir_entry *create_flash_pde(const char *filename,
609 struct file_operations *fops)
611 struct proc_dir_entry *ent = NULL;
613 ent = create_proc_entry(filename, S_IRUSR | S_IWUSR, NULL);
616 ent->proc_fops = fops;
617 ent->owner = THIS_MODULE;
623 static struct file_operations rtas_flash_operations = {
624 .read = rtas_flash_read,
625 .write = rtas_flash_write,
626 .open = rtas_excl_open,
627 .release = rtas_flash_release,
630 static struct file_operations manage_flash_operations = {
631 .read = manage_flash_read,
632 .write = manage_flash_write,
633 .open = rtas_excl_open,
634 .release = rtas_excl_release,
637 static struct file_operations validate_flash_operations = {
638 .read = validate_flash_read,
639 .write = validate_flash_write,
640 .open = rtas_excl_open,
641 .release = validate_flash_release,
644 int __init rtas_flash_init(void)
648 if (rtas_token("ibm,update-flash-64-and-reboot") ==
649 RTAS_UNKNOWN_SERVICE) {
650 printk(KERN_ERR "rtas_flash: no firmware flash support\n");
654 firmware_flash_pde = create_flash_pde("ppc64/rtas/"
656 &rtas_flash_operations);
657 if (firmware_flash_pde == NULL) {
662 rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
663 sizeof(struct rtas_update_flash_t),
668 firmware_update_pde = create_flash_pde("ppc64/rtas/"
669 FIRMWARE_UPDATE_NAME,
670 &rtas_flash_operations);
671 if (firmware_update_pde == NULL) {
676 rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
677 sizeof(struct rtas_update_flash_t),
678 firmware_update_pde);
682 validate_pde = create_flash_pde("ppc64/rtas/" VALIDATE_FLASH_NAME,
683 &validate_flash_operations);
684 if (validate_pde == NULL) {
689 rc = initialize_flash_pde_data("ibm,validate-flash-image",
690 sizeof(struct rtas_validate_flash_t),
695 manage_pde = create_flash_pde("ppc64/rtas" MANAGE_FLASH_NAME,
696 &manage_flash_operations);
697 if (manage_pde == NULL) {
702 rc = initialize_flash_pde_data("ibm,manage-flash-image",
703 sizeof(struct rtas_manage_flash_t),
711 remove_flash_pde(firmware_flash_pde);
712 remove_flash_pde(firmware_update_pde);
713 remove_flash_pde(validate_pde);
714 remove_flash_pde(manage_pde);
719 void __exit rtas_flash_cleanup(void)
721 remove_flash_pde(firmware_flash_pde);
722 remove_flash_pde(firmware_update_pde);
723 remove_flash_pde(validate_pde);
724 remove_flash_pde(manage_pde);
727 module_init(rtas_flash_init);
728 module_exit(rtas_flash_cleanup);
729 MODULE_LICENSE("GPL");