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 DEFINE_SPINLOCK(flash_file_open_lock);
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 __user *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];
229 uf = (struct rtas_update_flash_t *) dp->data;
231 if (!strcmp(dp->name, FIRMWARE_FLASH_NAME)) {
232 get_flash_status_msg(uf->status, msg);
233 } else { /* FIRMWARE_UPDATE_NAME */
234 sprintf(msg, "%d\n", uf->status);
236 msglen = strlen(msg);
240 if (ppos && *ppos != 0)
241 return 0; /* be cheap */
243 if (!access_ok(VERIFY_WRITE, buf, msglen))
246 if (copy_to_user(buf, msg, msglen))
254 /* We could be much more efficient here. But to keep this function
255 * simple we allocate a page to the block list no matter how small the
256 * count is. If the system is low on memory it will be just as well
259 static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
260 size_t count, loff_t *off)
262 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
263 struct rtas_update_flash_t *uf;
266 struct flash_block_list *fl;
268 uf = (struct rtas_update_flash_t *) dp->data;
270 if (uf->status == FLASH_AUTH || count == 0)
271 return count; /* discard data */
273 /* In the case that the image is not ready for flashing, the memory
274 * allocated for the block list will be freed upon the release of the
277 if (uf->flist == NULL) {
278 uf->flist = (struct flash_block_list *) get_zeroed_page(GFP_KERNEL);
285 fl = fl->next; /* seek to last block_list for append */
286 next_free = fl->num_blocks;
287 if (next_free == FLASH_BLOCKS_PER_NODE) {
288 /* Need to allocate another block_list */
289 fl->next = (struct flash_block_list *)get_zeroed_page(GFP_KERNEL);
296 if (count > PAGE_SIZE)
298 p = (char *)get_zeroed_page(GFP_KERNEL);
302 if(copy_from_user(p, buffer, count)) {
303 free_page((unsigned long)p);
306 fl->blocks[next_free].data = p;
307 fl->blocks[next_free].length = count;
313 static int rtas_excl_open(struct inode *inode, struct file *file)
315 struct proc_dir_entry *dp = PDE(inode);
317 /* Enforce exclusive open with use count of PDE */
318 spin_lock(&flash_file_open_lock);
319 if (atomic_read(&dp->count) > 1) {
320 spin_unlock(&flash_file_open_lock);
324 atomic_inc(&dp->count);
325 spin_unlock(&flash_file_open_lock);
330 static int rtas_excl_release(struct inode *inode, struct file *file)
332 struct proc_dir_entry *dp = PDE(inode);
334 atomic_dec(&dp->count);
339 static void manage_flash(struct rtas_manage_flash_t *args_buf)
341 unsigned int wait_time;
345 rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1,
346 1, NULL, args_buf->op);
347 if (rc == RTAS_RC_BUSY)
349 else if (rtas_is_extended_busy(rc)) {
350 wait_time = rtas_extended_busy_delay_time(rc);
351 udelay(wait_time * 1000);
356 args_buf->status = rc;
359 static ssize_t manage_flash_read(struct file *file, char __user *buf,
360 size_t count, loff_t *ppos)
362 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
363 struct rtas_manage_flash_t *args_buf;
364 char msg[RTAS_MSG_MAXLEN];
367 args_buf = (struct rtas_manage_flash_t *) dp->data;
368 if (args_buf == NULL)
371 msglen = sprintf(msg, "%d\n", args_buf->status);
375 if (ppos && *ppos != 0)
376 return 0; /* be cheap */
378 if (!access_ok(VERIFY_WRITE, buf, msglen))
381 if (copy_to_user(buf, msg, msglen))
389 static ssize_t manage_flash_write(struct file *file, const char __user *buf,
390 size_t count, loff_t *off)
392 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
393 struct rtas_manage_flash_t *args_buf;
394 const char reject_str[] = "0";
395 const char commit_str[] = "1";
399 args_buf = (struct rtas_manage_flash_t *) dp->data;
400 if ((args_buf->status == MANAGE_AUTH) || (count == 0))
405 if (count > 9) count = 9;
406 if (copy_from_user (stkbuf, buf, count)) {
409 if (strncmp(stkbuf, reject_str, strlen(reject_str)) == 0)
410 op = RTAS_REJECT_TMP_IMG;
411 else if (strncmp(stkbuf, commit_str, strlen(commit_str)) == 0)
412 op = RTAS_COMMIT_TMP_IMG;
415 if (op == -1) /* buf is empty, or contains invalid string */
419 manage_flash(args_buf);
424 static void validate_flash(struct rtas_validate_flash_t *args_buf)
426 int token = rtas_token("ibm,validate-flash-image");
427 unsigned int wait_time;
433 spin_lock(&rtas_data_buf_lock);
434 memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE);
435 rc = rtas_call(token, 2, 2, &update_results,
436 (u32) __pa(rtas_data_buf), args_buf->buf_size);
437 memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE);
438 spin_unlock(&rtas_data_buf_lock);
440 if (rc == RTAS_RC_BUSY)
442 else if (rtas_is_extended_busy(rc)) {
443 wait_time = rtas_extended_busy_delay_time(rc);
444 udelay(wait_time * 1000);
449 args_buf->status = rc;
450 args_buf->update_results = update_results;
453 static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf,
458 if (args_buf->status >= VALIDATE_TMP_UPDATE) {
459 n = sprintf(msg, "%d\n", args_buf->update_results);
460 if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
461 (args_buf->update_results == VALIDATE_TMP_UPDATE))
462 n += sprintf(msg + n, "%s\n", args_buf->buf);
464 n = sprintf(msg, "%d\n", args_buf->status);
469 static ssize_t validate_flash_read(struct file *file, char __user *buf,
470 size_t count, loff_t *ppos)
472 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
473 struct rtas_validate_flash_t *args_buf;
474 char msg[RTAS_MSG_MAXLEN];
477 args_buf = (struct rtas_validate_flash_t *) dp->data;
479 if (ppos && *ppos != 0)
480 return 0; /* be cheap */
482 msglen = get_validate_flash_msg(args_buf, msg);
486 if (!access_ok(VERIFY_WRITE, buf, msglen))
489 if (copy_to_user(buf, msg, msglen))
497 static ssize_t validate_flash_write(struct file *file, const char __user *buf,
498 size_t count, loff_t *off)
500 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
501 struct rtas_validate_flash_t *args_buf;
504 args_buf = (struct rtas_validate_flash_t *) dp->data;
506 if (dp->data == NULL) {
507 dp->data = kmalloc(sizeof(struct rtas_validate_flash_t),
509 if (dp->data == NULL)
513 /* We are only interested in the first 4K of the
515 if ((*off >= VALIDATE_BUF_SIZE) ||
516 (args_buf->status == VALIDATE_AUTH)) {
521 if (*off + count >= VALIDATE_BUF_SIZE) {
522 count = VALIDATE_BUF_SIZE - *off;
523 args_buf->status = VALIDATE_READY;
525 args_buf->status = VALIDATE_INCOMPLETE;
528 if (!access_ok(VERIFY_READ, buf, count)) {
532 if (copy_from_user(args_buf->buf + *off, buf, count)) {
547 static int validate_flash_release(struct inode *inode, struct file *file)
549 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
550 struct rtas_validate_flash_t *args_buf;
552 args_buf = (struct rtas_validate_flash_t *) dp->data;
554 if (args_buf->status == VALIDATE_READY) {
555 args_buf->buf_size = VALIDATE_BUF_SIZE;
556 validate_flash(args_buf);
559 /* The matching atomic_inc was in rtas_excl_open() */
560 atomic_dec(&dp->count);
565 static void remove_flash_pde(struct proc_dir_entry *dp)
568 if (dp->data != NULL)
571 remove_proc_entry(dp->name, dp->parent);
575 static int initialize_flash_pde_data(const char *rtas_call_name,
577 struct proc_dir_entry *dp)
582 dp->data = kmalloc(buf_size, GFP_KERNEL);
583 if (dp->data == NULL) {
584 remove_flash_pde(dp);
588 memset(dp->data, 0, buf_size);
591 * This code assumes that the status int is the first member of the
594 status = (int *) dp->data;
595 token = rtas_token(rtas_call_name);
596 if (token == RTAS_UNKNOWN_SERVICE)
597 *status = FLASH_AUTH;
599 *status = FLASH_NO_OP;
604 static struct proc_dir_entry *create_flash_pde(const char *filename,
605 struct file_operations *fops)
607 struct proc_dir_entry *ent = NULL;
609 ent = create_proc_entry(filename, S_IRUSR | S_IWUSR, NULL);
612 ent->proc_fops = fops;
613 ent->owner = THIS_MODULE;
619 static struct file_operations rtas_flash_operations = {
620 .read = rtas_flash_read,
621 .write = rtas_flash_write,
622 .open = rtas_excl_open,
623 .release = rtas_flash_release,
626 static struct file_operations manage_flash_operations = {
627 .read = manage_flash_read,
628 .write = manage_flash_write,
629 .open = rtas_excl_open,
630 .release = rtas_excl_release,
633 static struct file_operations validate_flash_operations = {
634 .read = validate_flash_read,
635 .write = validate_flash_write,
636 .open = rtas_excl_open,
637 .release = validate_flash_release,
640 int __init rtas_flash_init(void)
644 if (rtas_token("ibm,update-flash-64-and-reboot") ==
645 RTAS_UNKNOWN_SERVICE) {
646 printk(KERN_ERR "rtas_flash: no firmware flash support\n");
650 firmware_flash_pde = create_flash_pde("ppc64/rtas/"
652 &rtas_flash_operations);
653 if (firmware_flash_pde == NULL) {
658 rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
659 sizeof(struct rtas_update_flash_t),
664 firmware_update_pde = create_flash_pde("ppc64/rtas/"
665 FIRMWARE_UPDATE_NAME,
666 &rtas_flash_operations);
667 if (firmware_update_pde == NULL) {
672 rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
673 sizeof(struct rtas_update_flash_t),
674 firmware_update_pde);
678 validate_pde = create_flash_pde("ppc64/rtas/" VALIDATE_FLASH_NAME,
679 &validate_flash_operations);
680 if (validate_pde == NULL) {
685 rc = initialize_flash_pde_data("ibm,validate-flash-image",
686 sizeof(struct rtas_validate_flash_t),
691 manage_pde = create_flash_pde("ppc64/rtas/" MANAGE_FLASH_NAME,
692 &manage_flash_operations);
693 if (manage_pde == NULL) {
698 rc = initialize_flash_pde_data("ibm,manage-flash-image",
699 sizeof(struct rtas_manage_flash_t),
707 remove_flash_pde(firmware_flash_pde);
708 remove_flash_pde(firmware_update_pde);
709 remove_flash_pde(validate_pde);
710 remove_flash_pde(manage_pde);
715 void __exit rtas_flash_cleanup(void)
717 remove_flash_pde(firmware_flash_pde);
718 remove_flash_pde(firmware_update_pde);
719 remove_flash_pde(validate_pde);
720 remove_flash_pde(manage_pde);
723 module_init(rtas_flash_init);
724 module_exit(rtas_flash_cleanup);
725 MODULE_LICENSE("GPL");