Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  c 2001 PPC 64 Team, IBM Corp
0004  *
0005  * /proc/powerpc/rtas/firmware_flash interface
0006  *
0007  * This file implements a firmware_flash interface to pump a firmware
0008  * image into the kernel.  At reboot time rtas_restart() will see the
0009  * firmware image and flash it as it reboots (see rtas.c).
0010  */
0011 
0012 #include <linux/module.h>
0013 #include <linux/init.h>
0014 #include <linux/slab.h>
0015 #include <linux/proc_fs.h>
0016 #include <linux/reboot.h>
0017 #include <asm/delay.h>
0018 #include <linux/uaccess.h>
0019 #include <asm/rtas.h>
0020 
0021 #define MODULE_VERS "1.0"
0022 #define MODULE_NAME "rtas_flash"
0023 
0024 #define FIRMWARE_FLASH_NAME "firmware_flash"   
0025 #define FIRMWARE_UPDATE_NAME "firmware_update"
0026 #define MANAGE_FLASH_NAME "manage_flash"
0027 #define VALIDATE_FLASH_NAME "validate_flash"
0028 
0029 /* General RTAS Status Codes */
0030 #define RTAS_RC_SUCCESS  0
0031 #define RTAS_RC_HW_ERR  -1
0032 #define RTAS_RC_BUSY    -2
0033 
0034 /* Flash image status values */
0035 #define FLASH_AUTH           -9002 /* RTAS Not Service Authority Partition */
0036 #define FLASH_NO_OP          -1099 /* No operation initiated by user */ 
0037 #define FLASH_IMG_SHORT      -1005 /* Flash image shorter than expected */
0038 #define FLASH_IMG_BAD_LEN    -1004 /* Bad length value in flash list block */
0039 #define FLASH_IMG_NULL_DATA  -1003 /* Bad data value in flash list block */
0040 #define FLASH_IMG_READY      0     /* Firmware img ready for flash on reboot */
0041 
0042 /* Manage image status values */
0043 #define MANAGE_AUTH          -9002 /* RTAS Not Service Authority Partition */
0044 #define MANAGE_ACTIVE_ERR    -9001 /* RTAS Cannot Overwrite Active Img */
0045 #define MANAGE_NO_OP         -1099 /* No operation initiated by user */
0046 #define MANAGE_PARAM_ERR     -3    /* RTAS Parameter Error */
0047 #define MANAGE_HW_ERR        -1    /* RTAS Hardware Error */
0048 
0049 /* Validate image status values */
0050 #define VALIDATE_AUTH          -9002 /* RTAS Not Service Authority Partition */
0051 #define VALIDATE_NO_OP         -1099 /* No operation initiated by the user */
0052 #define VALIDATE_INCOMPLETE    -1002 /* User copied < VALIDATE_BUF_SIZE */
0053 #define VALIDATE_READY         -1001 /* Firmware image ready for validation */
0054 #define VALIDATE_PARAM_ERR     -3    /* RTAS Parameter Error */
0055 #define VALIDATE_HW_ERR        -1    /* RTAS Hardware Error */
0056 
0057 /* ibm,validate-flash-image update result tokens */
0058 #define VALIDATE_TMP_UPDATE    0     /* T side will be updated */
0059 #define VALIDATE_FLASH_AUTH    1     /* Partition does not have authority */
0060 #define VALIDATE_INVALID_IMG   2     /* Candidate image is not valid */
0061 #define VALIDATE_CUR_UNKNOWN   3     /* Current fixpack level is unknown */
0062 /*
0063  * Current T side will be committed to P side before being replace with new
0064  * image, and the new image is downlevel from current image
0065  */
0066 #define VALIDATE_TMP_COMMIT_DL 4
0067 /*
0068  * Current T side will be committed to P side before being replaced with new
0069  * image
0070  */
0071 #define VALIDATE_TMP_COMMIT    5
0072 /*
0073  * T side will be updated with a downlevel image
0074  */
0075 #define VALIDATE_TMP_UPDATE_DL 6
0076 /*
0077  * The candidate image's release date is later than the system's firmware
0078  * service entitlement date - service warranty period has expired
0079  */
0080 #define VALIDATE_OUT_OF_WRNTY  7
0081 
0082 /* ibm,manage-flash-image operation tokens */
0083 #define RTAS_REJECT_TMP_IMG   0
0084 #define RTAS_COMMIT_TMP_IMG   1
0085 
0086 /* Array sizes */
0087 #define VALIDATE_BUF_SIZE 4096    
0088 #define VALIDATE_MSG_LEN  256
0089 #define RTAS_MSG_MAXLEN   64
0090 
0091 /* Quirk - RTAS requires 4k list length and block size */
0092 #define RTAS_BLKLIST_LENGTH 4096
0093 #define RTAS_BLK_SIZE 4096
0094 
0095 struct flash_block {
0096     char *data;
0097     unsigned long length;
0098 };
0099 
0100 /* This struct is very similar but not identical to
0101  * that needed by the rtas flash update.
0102  * All we need to do for rtas is rewrite num_blocks
0103  * into a version/length and translate the pointers
0104  * to absolute.
0105  */
0106 #define FLASH_BLOCKS_PER_NODE ((RTAS_BLKLIST_LENGTH - 16) / sizeof(struct flash_block))
0107 struct flash_block_list {
0108     unsigned long num_blocks;
0109     struct flash_block_list *next;
0110     struct flash_block blocks[FLASH_BLOCKS_PER_NODE];
0111 };
0112 
0113 static struct flash_block_list *rtas_firmware_flash_list;
0114 
0115 /* Use slab cache to guarantee 4k alignment */
0116 static struct kmem_cache *flash_block_cache = NULL;
0117 
0118 #define FLASH_BLOCK_LIST_VERSION (1UL)
0119 
0120 /*
0121  * Local copy of the flash block list.
0122  *
0123  * The rtas_firmware_flash_list variable will be
0124  * set once the data is fully read.
0125  *
0126  * For convenience as we build the list we use virtual addrs,
0127  * we do not fill in the version number, and the length field
0128  * is treated as the number of entries currently in the block
0129  * (i.e. not a byte count).  This is all fixed when calling 
0130  * the flash routine.
0131  */
0132 
0133 /* Status int must be first member of struct */
0134 struct rtas_update_flash_t
0135 {
0136     int status;         /* Flash update status */
0137     struct flash_block_list *flist; /* Local copy of flash block list */
0138 };
0139 
0140 /* Status int must be first member of struct */
0141 struct rtas_manage_flash_t
0142 {
0143     int status;         /* Returned status */
0144 };
0145 
0146 /* Status int must be first member of struct */
0147 struct rtas_validate_flash_t
0148 {
0149     int status;         /* Returned status */   
0150     char *buf;          /* Candidate image buffer */
0151     unsigned int buf_size;      /* Size of image buf */
0152     unsigned int update_results;    /* Update results token */
0153 };
0154 
0155 static struct rtas_update_flash_t rtas_update_flash_data;
0156 static struct rtas_manage_flash_t rtas_manage_flash_data;
0157 static struct rtas_validate_flash_t rtas_validate_flash_data;
0158 static DEFINE_MUTEX(rtas_update_flash_mutex);
0159 static DEFINE_MUTEX(rtas_manage_flash_mutex);
0160 static DEFINE_MUTEX(rtas_validate_flash_mutex);
0161 
0162 /* Do simple sanity checks on the flash image. */
0163 static int flash_list_valid(struct flash_block_list *flist)
0164 {
0165     struct flash_block_list *f;
0166     int i;
0167     unsigned long block_size, image_size;
0168 
0169     /* Paranoid self test here.  We also collect the image size. */
0170     image_size = 0;
0171     for (f = flist; f; f = f->next) {
0172         for (i = 0; i < f->num_blocks; i++) {
0173             if (f->blocks[i].data == NULL) {
0174                 return FLASH_IMG_NULL_DATA;
0175             }
0176             block_size = f->blocks[i].length;
0177             if (block_size <= 0 || block_size > RTAS_BLK_SIZE) {
0178                 return FLASH_IMG_BAD_LEN;
0179             }
0180             image_size += block_size;
0181         }
0182     }
0183 
0184     if (image_size < (256 << 10)) {
0185         if (image_size < 2) 
0186             return FLASH_NO_OP;
0187     }
0188 
0189     printk(KERN_INFO "FLASH: flash image with %ld bytes stored for hardware flash on reboot\n", image_size);
0190 
0191     return FLASH_IMG_READY;
0192 }
0193 
0194 static void free_flash_list(struct flash_block_list *f)
0195 {
0196     struct flash_block_list *next;
0197     int i;
0198 
0199     while (f) {
0200         for (i = 0; i < f->num_blocks; i++)
0201             kmem_cache_free(flash_block_cache, f->blocks[i].data);
0202         next = f->next;
0203         kmem_cache_free(flash_block_cache, f);
0204         f = next;
0205     }
0206 }
0207 
0208 static int rtas_flash_release(struct inode *inode, struct file *file)
0209 {
0210     struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
0211 
0212     mutex_lock(&rtas_update_flash_mutex);
0213 
0214     if (uf->flist) {    
0215         /* File was opened in write mode for a new flash attempt */
0216         /* Clear saved list */
0217         if (rtas_firmware_flash_list) {
0218             free_flash_list(rtas_firmware_flash_list);
0219             rtas_firmware_flash_list = NULL;
0220         }
0221 
0222         if (uf->status != FLASH_AUTH)  
0223             uf->status = flash_list_valid(uf->flist);
0224 
0225         if (uf->status == FLASH_IMG_READY) 
0226             rtas_firmware_flash_list = uf->flist;
0227         else
0228             free_flash_list(uf->flist);
0229 
0230         uf->flist = NULL;
0231     }
0232 
0233     mutex_unlock(&rtas_update_flash_mutex);
0234     return 0;
0235 }
0236 
0237 static size_t get_flash_status_msg(int status, char *buf)
0238 {
0239     const char *msg;
0240     size_t len;
0241 
0242     switch (status) {
0243     case FLASH_AUTH:
0244         msg = "error: this partition does not have service authority\n";
0245         break;
0246     case FLASH_NO_OP:
0247         msg = "info: no firmware image for flash\n";
0248         break;
0249     case FLASH_IMG_SHORT:
0250         msg = "error: flash image short\n";
0251         break;
0252     case FLASH_IMG_BAD_LEN:
0253         msg = "error: internal error bad length\n";
0254         break;
0255     case FLASH_IMG_NULL_DATA:
0256         msg = "error: internal error null data\n";
0257         break;
0258     case FLASH_IMG_READY:
0259         msg = "ready: firmware image ready for flash on reboot\n";
0260         break;
0261     default:
0262         return sprintf(buf, "error: unexpected status value %d\n",
0263                    status);
0264     }
0265 
0266     len = strlen(msg);
0267     memcpy(buf, msg, len + 1);
0268     return len;
0269 }
0270 
0271 /* Reading the proc file will show status (not the firmware contents) */
0272 static ssize_t rtas_flash_read_msg(struct file *file, char __user *buf,
0273                    size_t count, loff_t *ppos)
0274 {
0275     struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
0276     char msg[RTAS_MSG_MAXLEN];
0277     size_t len;
0278     int status;
0279 
0280     mutex_lock(&rtas_update_flash_mutex);
0281     status = uf->status;
0282     mutex_unlock(&rtas_update_flash_mutex);
0283 
0284     /* Read as text message */
0285     len = get_flash_status_msg(status, msg);
0286     return simple_read_from_buffer(buf, count, ppos, msg, len);
0287 }
0288 
0289 static ssize_t rtas_flash_read_num(struct file *file, char __user *buf,
0290                    size_t count, loff_t *ppos)
0291 {
0292     struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
0293     char msg[RTAS_MSG_MAXLEN];
0294     int status;
0295 
0296     mutex_lock(&rtas_update_flash_mutex);
0297     status = uf->status;
0298     mutex_unlock(&rtas_update_flash_mutex);
0299 
0300     /* Read as number */
0301     sprintf(msg, "%d\n", status);
0302     return simple_read_from_buffer(buf, count, ppos, msg, strlen(msg));
0303 }
0304 
0305 /* We could be much more efficient here.  But to keep this function
0306  * simple we allocate a page to the block list no matter how small the
0307  * count is.  If the system is low on memory it will be just as well
0308  * that we fail....
0309  */
0310 static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
0311                 size_t count, loff_t *off)
0312 {
0313     struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
0314     char *p;
0315     int next_free, rc;
0316     struct flash_block_list *fl;
0317 
0318     mutex_lock(&rtas_update_flash_mutex);
0319 
0320     if (uf->status == FLASH_AUTH || count == 0)
0321         goto out;   /* discard data */
0322 
0323     /* In the case that the image is not ready for flashing, the memory
0324      * allocated for the block list will be freed upon the release of the 
0325      * proc file
0326      */
0327     if (uf->flist == NULL) {
0328         uf->flist = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
0329         if (!uf->flist)
0330             goto nomem;
0331     }
0332 
0333     fl = uf->flist;
0334     while (fl->next)
0335         fl = fl->next; /* seek to last block_list for append */
0336     next_free = fl->num_blocks;
0337     if (next_free == FLASH_BLOCKS_PER_NODE) {
0338         /* Need to allocate another block_list */
0339         fl->next = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
0340         if (!fl->next)
0341             goto nomem;
0342         fl = fl->next;
0343         next_free = 0;
0344     }
0345 
0346     if (count > RTAS_BLK_SIZE)
0347         count = RTAS_BLK_SIZE;
0348     p = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
0349     if (!p)
0350         goto nomem;
0351     
0352     if(copy_from_user(p, buffer, count)) {
0353         kmem_cache_free(flash_block_cache, p);
0354         rc = -EFAULT;
0355         goto error;
0356     }
0357     fl->blocks[next_free].data = p;
0358     fl->blocks[next_free].length = count;
0359     fl->num_blocks++;
0360 out:
0361     mutex_unlock(&rtas_update_flash_mutex);
0362     return count;
0363 
0364 nomem:
0365     rc = -ENOMEM;
0366 error:
0367     mutex_unlock(&rtas_update_flash_mutex);
0368     return rc;
0369 }
0370 
0371 /*
0372  * Flash management routines.
0373  */
0374 static void manage_flash(struct rtas_manage_flash_t *args_buf, unsigned int op)
0375 {
0376     s32 rc;
0377 
0378     do {
0379         rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1, 1,
0380                    NULL, op);
0381     } while (rtas_busy_delay(rc));
0382 
0383     args_buf->status = rc;
0384 }
0385 
0386 static ssize_t manage_flash_read(struct file *file, char __user *buf,
0387                    size_t count, loff_t *ppos)
0388 {
0389     struct rtas_manage_flash_t *const args_buf = &rtas_manage_flash_data;
0390     char msg[RTAS_MSG_MAXLEN];
0391     int msglen, status;
0392 
0393     mutex_lock(&rtas_manage_flash_mutex);
0394     status = args_buf->status;
0395     mutex_unlock(&rtas_manage_flash_mutex);
0396 
0397     msglen = sprintf(msg, "%d\n", status);
0398     return simple_read_from_buffer(buf, count, ppos, msg, msglen);
0399 }
0400 
0401 static ssize_t manage_flash_write(struct file *file, const char __user *buf,
0402                 size_t count, loff_t *off)
0403 {
0404     struct rtas_manage_flash_t *const args_buf = &rtas_manage_flash_data;
0405     static const char reject_str[] = "0";
0406     static const char commit_str[] = "1";
0407     char stkbuf[10];
0408     int op, rc;
0409 
0410     mutex_lock(&rtas_manage_flash_mutex);
0411 
0412     if ((args_buf->status == MANAGE_AUTH) || (count == 0))
0413         goto out;
0414         
0415     op = -1;
0416     if (buf) {
0417         if (count > 9) count = 9;
0418         rc = -EFAULT;
0419         if (copy_from_user (stkbuf, buf, count))
0420             goto error;
0421         if (strncmp(stkbuf, reject_str, strlen(reject_str)) == 0) 
0422             op = RTAS_REJECT_TMP_IMG;
0423         else if (strncmp(stkbuf, commit_str, strlen(commit_str)) == 0) 
0424             op = RTAS_COMMIT_TMP_IMG;
0425     }
0426     
0427     if (op == -1) {   /* buf is empty, or contains invalid string */
0428         rc = -EINVAL;
0429         goto error;
0430     }
0431 
0432     manage_flash(args_buf, op);
0433 out:
0434     mutex_unlock(&rtas_manage_flash_mutex);
0435     return count;
0436 
0437 error:
0438     mutex_unlock(&rtas_manage_flash_mutex);
0439     return rc;
0440 }
0441 
0442 /*
0443  * Validation routines.
0444  */
0445 static void validate_flash(struct rtas_validate_flash_t *args_buf)
0446 {
0447     int token = rtas_token("ibm,validate-flash-image");
0448     int update_results;
0449     s32 rc; 
0450 
0451     rc = 0;
0452     do {
0453         spin_lock(&rtas_data_buf_lock);
0454         memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE);
0455         rc = rtas_call(token, 2, 2, &update_results, 
0456                    (u32) __pa(rtas_data_buf), args_buf->buf_size);
0457         memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE);
0458         spin_unlock(&rtas_data_buf_lock);
0459     } while (rtas_busy_delay(rc));
0460 
0461     args_buf->status = rc;
0462     args_buf->update_results = update_results;
0463 }
0464 
0465 static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf, 
0466                            char *msg, int msglen)
0467 {
0468     int n;
0469 
0470     if (args_buf->status >= VALIDATE_TMP_UPDATE) { 
0471         n = sprintf(msg, "%d\n", args_buf->update_results);
0472         if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
0473             (args_buf->update_results == VALIDATE_TMP_UPDATE))
0474             n += snprintf(msg + n, msglen - n, "%s\n",
0475                     args_buf->buf);
0476     } else {
0477         n = sprintf(msg, "%d\n", args_buf->status);
0478     }
0479     return n;
0480 }
0481 
0482 static ssize_t validate_flash_read(struct file *file, char __user *buf,
0483                    size_t count, loff_t *ppos)
0484 {
0485     struct rtas_validate_flash_t *const args_buf =
0486         &rtas_validate_flash_data;
0487     char msg[VALIDATE_MSG_LEN];
0488     int msglen;
0489 
0490     mutex_lock(&rtas_validate_flash_mutex);
0491     msglen = get_validate_flash_msg(args_buf, msg, VALIDATE_MSG_LEN);
0492     mutex_unlock(&rtas_validate_flash_mutex);
0493 
0494     return simple_read_from_buffer(buf, count, ppos, msg, msglen);
0495 }
0496 
0497 static ssize_t validate_flash_write(struct file *file, const char __user *buf,
0498                     size_t count, loff_t *off)
0499 {
0500     struct rtas_validate_flash_t *const args_buf =
0501         &rtas_validate_flash_data;
0502     int rc;
0503 
0504     mutex_lock(&rtas_validate_flash_mutex);
0505 
0506     /* We are only interested in the first 4K of the
0507      * candidate image */
0508     if ((*off >= VALIDATE_BUF_SIZE) || 
0509         (args_buf->status == VALIDATE_AUTH)) {
0510         *off += count;
0511         mutex_unlock(&rtas_validate_flash_mutex);
0512         return count;
0513     }
0514 
0515     if (*off + count >= VALIDATE_BUF_SIZE)  {
0516         count = VALIDATE_BUF_SIZE - *off;
0517         args_buf->status = VALIDATE_READY;  
0518     } else {
0519         args_buf->status = VALIDATE_INCOMPLETE;
0520     }
0521 
0522     if (!access_ok(buf, count)) {
0523         rc = -EFAULT;
0524         goto done;
0525     }
0526     if (copy_from_user(args_buf->buf + *off, buf, count)) {
0527         rc = -EFAULT;
0528         goto done;
0529     }
0530 
0531     *off += count;
0532     rc = count;
0533 done:
0534     mutex_unlock(&rtas_validate_flash_mutex);
0535     return rc;
0536 }
0537 
0538 static int validate_flash_release(struct inode *inode, struct file *file)
0539 {
0540     struct rtas_validate_flash_t *const args_buf =
0541         &rtas_validate_flash_data;
0542 
0543     mutex_lock(&rtas_validate_flash_mutex);
0544 
0545     if (args_buf->status == VALIDATE_READY) {
0546         args_buf->buf_size = VALIDATE_BUF_SIZE;
0547         validate_flash(args_buf);
0548     }
0549 
0550     mutex_unlock(&rtas_validate_flash_mutex);
0551     return 0;
0552 }
0553 
0554 /*
0555  * On-reboot flash update applicator.
0556  */
0557 static void rtas_flash_firmware(int reboot_type)
0558 {
0559     unsigned long image_size;
0560     struct flash_block_list *f, *next, *flist;
0561     unsigned long rtas_block_list;
0562     int i, status, update_token;
0563 
0564     if (rtas_firmware_flash_list == NULL)
0565         return;     /* nothing to do */
0566 
0567     if (reboot_type != SYS_RESTART) {
0568         printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
0569         printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
0570         return;
0571     }
0572 
0573     update_token = rtas_token("ibm,update-flash-64-and-reboot");
0574     if (update_token == RTAS_UNKNOWN_SERVICE) {
0575         printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot "
0576                "is not available -- not a service partition?\n");
0577         printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
0578         return;
0579     }
0580 
0581     /*
0582      * Just before starting the firmware flash, cancel the event scan work
0583      * to avoid any soft lockup issues.
0584      */
0585     rtas_cancel_event_scan();
0586 
0587     /*
0588      * NOTE: the "first" block must be under 4GB, so we create
0589      * an entry with no data blocks in the reserved buffer in
0590      * the kernel data segment.
0591      */
0592     spin_lock(&rtas_data_buf_lock);
0593     flist = (struct flash_block_list *)&rtas_data_buf[0];
0594     flist->num_blocks = 0;
0595     flist->next = rtas_firmware_flash_list;
0596     rtas_block_list = __pa(flist);
0597     if (rtas_block_list >= 4UL*1024*1024*1024) {
0598         printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
0599         spin_unlock(&rtas_data_buf_lock);
0600         return;
0601     }
0602 
0603     printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
0604     /* Update the block_list in place. */
0605     rtas_firmware_flash_list = NULL; /* too hard to backout on error */
0606     image_size = 0;
0607     for (f = flist; f; f = next) {
0608         /* Translate data addrs to absolute */
0609         for (i = 0; i < f->num_blocks; i++) {
0610             f->blocks[i].data = (char *)cpu_to_be64(__pa(f->blocks[i].data));
0611             image_size += f->blocks[i].length;
0612             f->blocks[i].length = cpu_to_be64(f->blocks[i].length);
0613         }
0614         next = f->next;
0615         /* Don't translate NULL pointer for last entry */
0616         if (f->next)
0617             f->next = (struct flash_block_list *)cpu_to_be64(__pa(f->next));
0618         else
0619             f->next = NULL;
0620         /* make num_blocks into the version/length field */
0621         f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
0622         f->num_blocks = cpu_to_be64(f->num_blocks);
0623     }
0624 
0625     printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
0626     printk(KERN_ALERT "FLASH: performing flash and reboot\n");
0627     rtas_progress("Flashing        \n", 0x0);
0628     rtas_progress("Please Wait...  ", 0x0);
0629     printk(KERN_ALERT "FLASH: this will take several minutes.  Do not power off!\n");
0630     status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
0631     switch (status) {   /* should only get "bad" status */
0632         case 0:
0633         printk(KERN_ALERT "FLASH: success\n");
0634         break;
0635         case -1:
0636         printk(KERN_ALERT "FLASH: hardware error.  Firmware may not be not flashed\n");
0637         break;
0638         case -3:
0639         printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform.  Firmware not flashed\n");
0640         break;
0641         case -4:
0642         printk(KERN_ALERT "FLASH: flash failed when partially complete.  System may not reboot\n");
0643         break;
0644         default:
0645         printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
0646         break;
0647     }
0648     spin_unlock(&rtas_data_buf_lock);
0649 }
0650 
0651 /*
0652  * Manifest of proc files to create
0653  */
0654 struct rtas_flash_file {
0655     const char *filename;
0656     const char *rtas_call_name;
0657     int *status;
0658     const struct proc_ops ops;
0659 };
0660 
0661 static const struct rtas_flash_file rtas_flash_files[] = {
0662     {
0663         .filename   = "powerpc/rtas/" FIRMWARE_FLASH_NAME,
0664         .rtas_call_name = "ibm,update-flash-64-and-reboot",
0665         .status     = &rtas_update_flash_data.status,
0666         .ops.proc_read  = rtas_flash_read_msg,
0667         .ops.proc_write = rtas_flash_write,
0668         .ops.proc_release = rtas_flash_release,
0669         .ops.proc_lseek = default_llseek,
0670     },
0671     {
0672         .filename   = "powerpc/rtas/" FIRMWARE_UPDATE_NAME,
0673         .rtas_call_name = "ibm,update-flash-64-and-reboot",
0674         .status     = &rtas_update_flash_data.status,
0675         .ops.proc_read  = rtas_flash_read_num,
0676         .ops.proc_write = rtas_flash_write,
0677         .ops.proc_release = rtas_flash_release,
0678         .ops.proc_lseek = default_llseek,
0679     },
0680     {
0681         .filename   = "powerpc/rtas/" VALIDATE_FLASH_NAME,
0682         .rtas_call_name = "ibm,validate-flash-image",
0683         .status     = &rtas_validate_flash_data.status,
0684         .ops.proc_read  = validate_flash_read,
0685         .ops.proc_write = validate_flash_write,
0686         .ops.proc_release = validate_flash_release,
0687         .ops.proc_lseek = default_llseek,
0688     },
0689     {
0690         .filename   = "powerpc/rtas/" MANAGE_FLASH_NAME,
0691         .rtas_call_name = "ibm,manage-flash-image",
0692         .status     = &rtas_manage_flash_data.status,
0693         .ops.proc_read  = manage_flash_read,
0694         .ops.proc_write = manage_flash_write,
0695         .ops.proc_lseek = default_llseek,
0696     }
0697 };
0698 
0699 static int __init rtas_flash_init(void)
0700 {
0701     int i;
0702 
0703     if (rtas_token("ibm,update-flash-64-and-reboot") ==
0704                RTAS_UNKNOWN_SERVICE) {
0705         pr_info("rtas_flash: no firmware flash support\n");
0706         return -EINVAL;
0707     }
0708 
0709     rtas_validate_flash_data.buf = kzalloc(VALIDATE_BUF_SIZE, GFP_KERNEL);
0710     if (!rtas_validate_flash_data.buf)
0711         return -ENOMEM;
0712 
0713     flash_block_cache = kmem_cache_create("rtas_flash_cache",
0714                           RTAS_BLK_SIZE, RTAS_BLK_SIZE, 0,
0715                           NULL);
0716     if (!flash_block_cache) {
0717         printk(KERN_ERR "%s: failed to create block cache\n",
0718                 __func__);
0719         goto enomem_buf;
0720     }
0721 
0722     for (i = 0; i < ARRAY_SIZE(rtas_flash_files); i++) {
0723         const struct rtas_flash_file *f = &rtas_flash_files[i];
0724         int token;
0725 
0726         if (!proc_create(f->filename, 0600, NULL, &f->ops))
0727             goto enomem;
0728 
0729         /*
0730          * This code assumes that the status int is the first member of the
0731          * struct
0732          */
0733         token = rtas_token(f->rtas_call_name);
0734         if (token == RTAS_UNKNOWN_SERVICE)
0735             *f->status = FLASH_AUTH;
0736         else
0737             *f->status = FLASH_NO_OP;
0738     }
0739 
0740     rtas_flash_term_hook = rtas_flash_firmware;
0741     return 0;
0742 
0743 enomem:
0744     while (--i >= 0) {
0745         const struct rtas_flash_file *f = &rtas_flash_files[i];
0746         remove_proc_entry(f->filename, NULL);
0747     }
0748 
0749     kmem_cache_destroy(flash_block_cache);
0750 enomem_buf:
0751     kfree(rtas_validate_flash_data.buf);
0752     return -ENOMEM;
0753 }
0754 
0755 static void __exit rtas_flash_cleanup(void)
0756 {
0757     int i;
0758 
0759     rtas_flash_term_hook = NULL;
0760 
0761     if (rtas_firmware_flash_list) {
0762         free_flash_list(rtas_firmware_flash_list);
0763         rtas_firmware_flash_list = NULL;
0764     }
0765 
0766     for (i = 0; i < ARRAY_SIZE(rtas_flash_files); i++) {
0767         const struct rtas_flash_file *f = &rtas_flash_files[i];
0768         remove_proc_entry(f->filename, NULL);
0769     }
0770 
0771     kmem_cache_destroy(flash_block_cache);
0772     kfree(rtas_validate_flash_data.buf);
0773 }
0774 
0775 module_init(rtas_flash_init);
0776 module_exit(rtas_flash_cleanup);
0777 MODULE_LICENSE("GPL");