1f4fcbbe9SPaul Mackerras /* 2f4fcbbe9SPaul Mackerras * c 2001 PPC 64 Team, IBM Corp 3f4fcbbe9SPaul Mackerras * 4f4fcbbe9SPaul Mackerras * This program is free software; you can redistribute it and/or 5f4fcbbe9SPaul Mackerras * modify it under the terms of the GNU General Public License 6f4fcbbe9SPaul Mackerras * as published by the Free Software Foundation; either version 7f4fcbbe9SPaul Mackerras * 2 of the License, or (at your option) any later version. 8f4fcbbe9SPaul Mackerras * 9188917e1SBenjamin Herrenschmidt * /proc/powerpc/rtas/firmware_flash interface 10f4fcbbe9SPaul Mackerras * 11f4fcbbe9SPaul Mackerras * This file implements a firmware_flash interface to pump a firmware 12f4fcbbe9SPaul Mackerras * image into the kernel. At reboot time rtas_restart() will see the 13f4fcbbe9SPaul Mackerras * firmware image and flash it as it reboots (see rtas.c). 14f4fcbbe9SPaul Mackerras */ 15f4fcbbe9SPaul Mackerras 16f4fcbbe9SPaul Mackerras #include <linux/module.h> 17f4fcbbe9SPaul Mackerras #include <linux/init.h> 18*5a0e3ad6STejun Heo #include <linux/slab.h> 19f4fcbbe9SPaul Mackerras #include <linux/proc_fs.h> 20f4fcbbe9SPaul Mackerras #include <asm/delay.h> 21f4fcbbe9SPaul Mackerras #include <asm/uaccess.h> 22f4fcbbe9SPaul Mackerras #include <asm/rtas.h> 23f4fcbbe9SPaul Mackerras #include <asm/abs_addr.h> 24f4fcbbe9SPaul Mackerras 25f4fcbbe9SPaul Mackerras #define MODULE_VERS "1.0" 26f4fcbbe9SPaul Mackerras #define MODULE_NAME "rtas_flash" 27f4fcbbe9SPaul Mackerras 28f4fcbbe9SPaul Mackerras #define FIRMWARE_FLASH_NAME "firmware_flash" 29f4fcbbe9SPaul Mackerras #define FIRMWARE_UPDATE_NAME "firmware_update" 30f4fcbbe9SPaul Mackerras #define MANAGE_FLASH_NAME "manage_flash" 31f4fcbbe9SPaul Mackerras #define VALIDATE_FLASH_NAME "validate_flash" 32f4fcbbe9SPaul Mackerras 33f4fcbbe9SPaul Mackerras /* General RTAS Status Codes */ 34f4fcbbe9SPaul Mackerras #define RTAS_RC_SUCCESS 0 35f4fcbbe9SPaul Mackerras #define RTAS_RC_HW_ERR -1 36f4fcbbe9SPaul Mackerras #define RTAS_RC_BUSY -2 37f4fcbbe9SPaul Mackerras 38f4fcbbe9SPaul Mackerras /* Flash image status values */ 39f4fcbbe9SPaul Mackerras #define FLASH_AUTH -9002 /* RTAS Not Service Authority Partition */ 40f4fcbbe9SPaul Mackerras #define FLASH_NO_OP -1099 /* No operation initiated by user */ 41f4fcbbe9SPaul Mackerras #define FLASH_IMG_SHORT -1005 /* Flash image shorter than expected */ 42f4fcbbe9SPaul Mackerras #define FLASH_IMG_BAD_LEN -1004 /* Bad length value in flash list block */ 43f4fcbbe9SPaul Mackerras #define FLASH_IMG_NULL_DATA -1003 /* Bad data value in flash list block */ 44f4fcbbe9SPaul Mackerras #define FLASH_IMG_READY 0 /* Firmware img ready for flash on reboot */ 45f4fcbbe9SPaul Mackerras 46f4fcbbe9SPaul Mackerras /* Manage image status values */ 47f4fcbbe9SPaul Mackerras #define MANAGE_AUTH -9002 /* RTAS Not Service Authority Partition */ 48f4fcbbe9SPaul Mackerras #define MANAGE_ACTIVE_ERR -9001 /* RTAS Cannot Overwrite Active Img */ 49f4fcbbe9SPaul Mackerras #define MANAGE_NO_OP -1099 /* No operation initiated by user */ 50f4fcbbe9SPaul Mackerras #define MANAGE_PARAM_ERR -3 /* RTAS Parameter Error */ 51f4fcbbe9SPaul Mackerras #define MANAGE_HW_ERR -1 /* RTAS Hardware Error */ 52f4fcbbe9SPaul Mackerras 53f4fcbbe9SPaul Mackerras /* Validate image status values */ 54f4fcbbe9SPaul Mackerras #define VALIDATE_AUTH -9002 /* RTAS Not Service Authority Partition */ 55f4fcbbe9SPaul Mackerras #define VALIDATE_NO_OP -1099 /* No operation initiated by the user */ 56f4fcbbe9SPaul Mackerras #define VALIDATE_INCOMPLETE -1002 /* User copied < VALIDATE_BUF_SIZE */ 57f4fcbbe9SPaul Mackerras #define VALIDATE_READY -1001 /* Firmware image ready for validation */ 58f4fcbbe9SPaul Mackerras #define VALIDATE_PARAM_ERR -3 /* RTAS Parameter Error */ 59f4fcbbe9SPaul Mackerras #define VALIDATE_HW_ERR -1 /* RTAS Hardware Error */ 60f4fcbbe9SPaul Mackerras #define VALIDATE_TMP_UPDATE 0 /* Validate Return Status */ 61f4fcbbe9SPaul Mackerras #define VALIDATE_FLASH_AUTH 1 /* Validate Return Status */ 62f4fcbbe9SPaul Mackerras #define VALIDATE_INVALID_IMG 2 /* Validate Return Status */ 63f4fcbbe9SPaul Mackerras #define VALIDATE_CUR_UNKNOWN 3 /* Validate Return Status */ 64f4fcbbe9SPaul Mackerras #define VALIDATE_TMP_COMMIT_DL 4 /* Validate Return Status */ 65f4fcbbe9SPaul Mackerras #define VALIDATE_TMP_COMMIT 5 /* Validate Return Status */ 66f4fcbbe9SPaul Mackerras #define VALIDATE_TMP_UPDATE_DL 6 /* Validate Return Status */ 67f4fcbbe9SPaul Mackerras 68f4fcbbe9SPaul Mackerras /* ibm,manage-flash-image operation tokens */ 69f4fcbbe9SPaul Mackerras #define RTAS_REJECT_TMP_IMG 0 70f4fcbbe9SPaul Mackerras #define RTAS_COMMIT_TMP_IMG 1 71f4fcbbe9SPaul Mackerras 72f4fcbbe9SPaul Mackerras /* Array sizes */ 73f4fcbbe9SPaul Mackerras #define VALIDATE_BUF_SIZE 4096 74f4fcbbe9SPaul Mackerras #define RTAS_MSG_MAXLEN 64 75f4fcbbe9SPaul Mackerras 76ae883cabSJohn Rose /* Quirk - RTAS requires 4k list length and block size */ 77ae883cabSJohn Rose #define RTAS_BLKLIST_LENGTH 4096 78ae883cabSJohn Rose #define RTAS_BLK_SIZE 4096 79ae883cabSJohn Rose 80f4fcbbe9SPaul Mackerras struct flash_block { 81f4fcbbe9SPaul Mackerras char *data; 82f4fcbbe9SPaul Mackerras unsigned long length; 83f4fcbbe9SPaul Mackerras }; 84f4fcbbe9SPaul Mackerras 85f4fcbbe9SPaul Mackerras /* This struct is very similar but not identical to 86f4fcbbe9SPaul Mackerras * that needed by the rtas flash update. 87f4fcbbe9SPaul Mackerras * All we need to do for rtas is rewrite num_blocks 88f4fcbbe9SPaul Mackerras * into a version/length and translate the pointers 89f4fcbbe9SPaul Mackerras * to absolute. 90f4fcbbe9SPaul Mackerras */ 91ae883cabSJohn Rose #define FLASH_BLOCKS_PER_NODE ((RTAS_BLKLIST_LENGTH - 16) / sizeof(struct flash_block)) 92f4fcbbe9SPaul Mackerras struct flash_block_list { 93f4fcbbe9SPaul Mackerras unsigned long num_blocks; 94f4fcbbe9SPaul Mackerras struct flash_block_list *next; 95f4fcbbe9SPaul Mackerras struct flash_block blocks[FLASH_BLOCKS_PER_NODE]; 96f4fcbbe9SPaul Mackerras }; 97f4fcbbe9SPaul Mackerras struct flash_block_list_header { /* just the header of flash_block_list */ 98f4fcbbe9SPaul Mackerras unsigned long num_blocks; 99f4fcbbe9SPaul Mackerras struct flash_block_list *next; 100f4fcbbe9SPaul Mackerras }; 101f4fcbbe9SPaul Mackerras 102f4fcbbe9SPaul Mackerras static struct flash_block_list_header rtas_firmware_flash_list = {0, NULL}; 103f4fcbbe9SPaul Mackerras 104ae883cabSJohn Rose /* Use slab cache to guarantee 4k alignment */ 105e18b890bSChristoph Lameter static struct kmem_cache *flash_block_cache = NULL; 106ae883cabSJohn Rose 107f4fcbbe9SPaul Mackerras #define FLASH_BLOCK_LIST_VERSION (1UL) 108f4fcbbe9SPaul Mackerras 109f4fcbbe9SPaul Mackerras /* Local copy of the flash block list. 110f4fcbbe9SPaul Mackerras * We only allow one open of the flash proc file and create this 111f4fcbbe9SPaul Mackerras * list as we go. This list will be put in the 112f4fcbbe9SPaul Mackerras * rtas_firmware_flash_list var once it is fully read. 113f4fcbbe9SPaul Mackerras * 114f4fcbbe9SPaul Mackerras * For convenience as we build the list we use virtual addrs, 115f4fcbbe9SPaul Mackerras * we do not fill in the version number, and the length field 116f4fcbbe9SPaul Mackerras * is treated as the number of entries currently in the block 117f4fcbbe9SPaul Mackerras * (i.e. not a byte count). This is all fixed on release. 118f4fcbbe9SPaul Mackerras */ 119f4fcbbe9SPaul Mackerras 120f4fcbbe9SPaul Mackerras /* Status int must be first member of struct */ 121f4fcbbe9SPaul Mackerras struct rtas_update_flash_t 122f4fcbbe9SPaul Mackerras { 123f4fcbbe9SPaul Mackerras int status; /* Flash update status */ 124f4fcbbe9SPaul Mackerras struct flash_block_list *flist; /* Local copy of flash block list */ 125f4fcbbe9SPaul Mackerras }; 126f4fcbbe9SPaul Mackerras 127f4fcbbe9SPaul Mackerras /* Status int must be first member of struct */ 128f4fcbbe9SPaul Mackerras struct rtas_manage_flash_t 129f4fcbbe9SPaul Mackerras { 130f4fcbbe9SPaul Mackerras int status; /* Returned status */ 131f4fcbbe9SPaul Mackerras unsigned int op; /* Reject or commit image */ 132f4fcbbe9SPaul Mackerras }; 133f4fcbbe9SPaul Mackerras 134f4fcbbe9SPaul Mackerras /* Status int must be first member of struct */ 135f4fcbbe9SPaul Mackerras struct rtas_validate_flash_t 136f4fcbbe9SPaul Mackerras { 137f4fcbbe9SPaul Mackerras int status; /* Returned status */ 138f4fcbbe9SPaul Mackerras char buf[VALIDATE_BUF_SIZE]; /* Candidate image buffer */ 139f4fcbbe9SPaul Mackerras unsigned int buf_size; /* Size of image buf */ 140f4fcbbe9SPaul Mackerras unsigned int update_results; /* Update results token */ 141f4fcbbe9SPaul Mackerras }; 142f4fcbbe9SPaul Mackerras 143f4fcbbe9SPaul Mackerras static DEFINE_SPINLOCK(flash_file_open_lock); 144f4fcbbe9SPaul Mackerras static struct proc_dir_entry *firmware_flash_pde; 145f4fcbbe9SPaul Mackerras static struct proc_dir_entry *firmware_update_pde; 146f4fcbbe9SPaul Mackerras static struct proc_dir_entry *validate_pde; 147f4fcbbe9SPaul Mackerras static struct proc_dir_entry *manage_pde; 148f4fcbbe9SPaul Mackerras 149f4fcbbe9SPaul Mackerras /* Do simple sanity checks on the flash image. */ 150f4fcbbe9SPaul Mackerras static int flash_list_valid(struct flash_block_list *flist) 151f4fcbbe9SPaul Mackerras { 152f4fcbbe9SPaul Mackerras struct flash_block_list *f; 153f4fcbbe9SPaul Mackerras int i; 154f4fcbbe9SPaul Mackerras unsigned long block_size, image_size; 155f4fcbbe9SPaul Mackerras 156f4fcbbe9SPaul Mackerras /* Paranoid self test here. We also collect the image size. */ 157f4fcbbe9SPaul Mackerras image_size = 0; 158f4fcbbe9SPaul Mackerras for (f = flist; f; f = f->next) { 159f4fcbbe9SPaul Mackerras for (i = 0; i < f->num_blocks; i++) { 160f4fcbbe9SPaul Mackerras if (f->blocks[i].data == NULL) { 161f4fcbbe9SPaul Mackerras return FLASH_IMG_NULL_DATA; 162f4fcbbe9SPaul Mackerras } 163f4fcbbe9SPaul Mackerras block_size = f->blocks[i].length; 164ae883cabSJohn Rose if (block_size <= 0 || block_size > RTAS_BLK_SIZE) { 165f4fcbbe9SPaul Mackerras return FLASH_IMG_BAD_LEN; 166f4fcbbe9SPaul Mackerras } 167f4fcbbe9SPaul Mackerras image_size += block_size; 168f4fcbbe9SPaul Mackerras } 169f4fcbbe9SPaul Mackerras } 170f4fcbbe9SPaul Mackerras 171f4fcbbe9SPaul Mackerras if (image_size < (256 << 10)) { 172f4fcbbe9SPaul Mackerras if (image_size < 2) 173f4fcbbe9SPaul Mackerras return FLASH_NO_OP; 174f4fcbbe9SPaul Mackerras } 175f4fcbbe9SPaul Mackerras 176f4fcbbe9SPaul Mackerras printk(KERN_INFO "FLASH: flash image with %ld bytes stored for hardware flash on reboot\n", image_size); 177f4fcbbe9SPaul Mackerras 178f4fcbbe9SPaul Mackerras return FLASH_IMG_READY; 179f4fcbbe9SPaul Mackerras } 180f4fcbbe9SPaul Mackerras 181f4fcbbe9SPaul Mackerras static void free_flash_list(struct flash_block_list *f) 182f4fcbbe9SPaul Mackerras { 183f4fcbbe9SPaul Mackerras struct flash_block_list *next; 184f4fcbbe9SPaul Mackerras int i; 185f4fcbbe9SPaul Mackerras 186f4fcbbe9SPaul Mackerras while (f) { 187f4fcbbe9SPaul Mackerras for (i = 0; i < f->num_blocks; i++) 188ae883cabSJohn Rose kmem_cache_free(flash_block_cache, f->blocks[i].data); 189f4fcbbe9SPaul Mackerras next = f->next; 190ae883cabSJohn Rose kmem_cache_free(flash_block_cache, f); 191f4fcbbe9SPaul Mackerras f = next; 192f4fcbbe9SPaul Mackerras } 193f4fcbbe9SPaul Mackerras } 194f4fcbbe9SPaul Mackerras 195f4fcbbe9SPaul Mackerras static int rtas_flash_release(struct inode *inode, struct file *file) 196f4fcbbe9SPaul Mackerras { 197b4d1ab58SJosef Sipek struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode); 198f4fcbbe9SPaul Mackerras struct rtas_update_flash_t *uf; 199f4fcbbe9SPaul Mackerras 200f4fcbbe9SPaul Mackerras uf = (struct rtas_update_flash_t *) dp->data; 201f4fcbbe9SPaul Mackerras if (uf->flist) { 202f4fcbbe9SPaul Mackerras /* File was opened in write mode for a new flash attempt */ 203f4fcbbe9SPaul Mackerras /* Clear saved list */ 204f4fcbbe9SPaul Mackerras if (rtas_firmware_flash_list.next) { 205f4fcbbe9SPaul Mackerras free_flash_list(rtas_firmware_flash_list.next); 206f4fcbbe9SPaul Mackerras rtas_firmware_flash_list.next = NULL; 207f4fcbbe9SPaul Mackerras } 208f4fcbbe9SPaul Mackerras 209f4fcbbe9SPaul Mackerras if (uf->status != FLASH_AUTH) 210f4fcbbe9SPaul Mackerras uf->status = flash_list_valid(uf->flist); 211f4fcbbe9SPaul Mackerras 212f4fcbbe9SPaul Mackerras if (uf->status == FLASH_IMG_READY) 213f4fcbbe9SPaul Mackerras rtas_firmware_flash_list.next = uf->flist; 214f4fcbbe9SPaul Mackerras else 215f4fcbbe9SPaul Mackerras free_flash_list(uf->flist); 216f4fcbbe9SPaul Mackerras 217f4fcbbe9SPaul Mackerras uf->flist = NULL; 218f4fcbbe9SPaul Mackerras } 219f4fcbbe9SPaul Mackerras 220f4fcbbe9SPaul Mackerras atomic_dec(&dp->count); 221f4fcbbe9SPaul Mackerras return 0; 222f4fcbbe9SPaul Mackerras } 223f4fcbbe9SPaul Mackerras 224f4fcbbe9SPaul Mackerras static void get_flash_status_msg(int status, char *buf) 225f4fcbbe9SPaul Mackerras { 226f4fcbbe9SPaul Mackerras char *msg; 227f4fcbbe9SPaul Mackerras 228f4fcbbe9SPaul Mackerras switch (status) { 229f4fcbbe9SPaul Mackerras case FLASH_AUTH: 230f4fcbbe9SPaul Mackerras msg = "error: this partition does not have service authority\n"; 231f4fcbbe9SPaul Mackerras break; 232f4fcbbe9SPaul Mackerras case FLASH_NO_OP: 233f4fcbbe9SPaul Mackerras msg = "info: no firmware image for flash\n"; 234f4fcbbe9SPaul Mackerras break; 235f4fcbbe9SPaul Mackerras case FLASH_IMG_SHORT: 236f4fcbbe9SPaul Mackerras msg = "error: flash image short\n"; 237f4fcbbe9SPaul Mackerras break; 238f4fcbbe9SPaul Mackerras case FLASH_IMG_BAD_LEN: 239f4fcbbe9SPaul Mackerras msg = "error: internal error bad length\n"; 240f4fcbbe9SPaul Mackerras break; 241f4fcbbe9SPaul Mackerras case FLASH_IMG_NULL_DATA: 242f4fcbbe9SPaul Mackerras msg = "error: internal error null data\n"; 243f4fcbbe9SPaul Mackerras break; 244f4fcbbe9SPaul Mackerras case FLASH_IMG_READY: 245f4fcbbe9SPaul Mackerras msg = "ready: firmware image ready for flash on reboot\n"; 246f4fcbbe9SPaul Mackerras break; 247f4fcbbe9SPaul Mackerras default: 248f4fcbbe9SPaul Mackerras sprintf(buf, "error: unexpected status value %d\n", status); 249f4fcbbe9SPaul Mackerras return; 250f4fcbbe9SPaul Mackerras } 251f4fcbbe9SPaul Mackerras 252f4fcbbe9SPaul Mackerras strcpy(buf, msg); 253f4fcbbe9SPaul Mackerras } 254f4fcbbe9SPaul Mackerras 255f4fcbbe9SPaul Mackerras /* Reading the proc file will show status (not the firmware contents) */ 256f4fcbbe9SPaul Mackerras static ssize_t rtas_flash_read(struct file *file, char __user *buf, 257f4fcbbe9SPaul Mackerras size_t count, loff_t *ppos) 258f4fcbbe9SPaul Mackerras { 259b4d1ab58SJosef Sipek struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode); 260f4fcbbe9SPaul Mackerras struct rtas_update_flash_t *uf; 261f4fcbbe9SPaul Mackerras char msg[RTAS_MSG_MAXLEN]; 262f4fcbbe9SPaul Mackerras int msglen; 263f4fcbbe9SPaul Mackerras 264f4fcbbe9SPaul Mackerras uf = (struct rtas_update_flash_t *) dp->data; 265f4fcbbe9SPaul Mackerras 266f4fcbbe9SPaul Mackerras if (!strcmp(dp->name, FIRMWARE_FLASH_NAME)) { 267f4fcbbe9SPaul Mackerras get_flash_status_msg(uf->status, msg); 268f4fcbbe9SPaul Mackerras } else { /* FIRMWARE_UPDATE_NAME */ 269f4fcbbe9SPaul Mackerras sprintf(msg, "%d\n", uf->status); 270f4fcbbe9SPaul Mackerras } 271f4fcbbe9SPaul Mackerras msglen = strlen(msg); 272f4fcbbe9SPaul Mackerras if (msglen > count) 273f4fcbbe9SPaul Mackerras msglen = count; 274f4fcbbe9SPaul Mackerras 275f4fcbbe9SPaul Mackerras if (ppos && *ppos != 0) 276f4fcbbe9SPaul Mackerras return 0; /* be cheap */ 277f4fcbbe9SPaul Mackerras 278f4fcbbe9SPaul Mackerras if (!access_ok(VERIFY_WRITE, buf, msglen)) 279f4fcbbe9SPaul Mackerras return -EINVAL; 280f4fcbbe9SPaul Mackerras 281f4fcbbe9SPaul Mackerras if (copy_to_user(buf, msg, msglen)) 282f4fcbbe9SPaul Mackerras return -EFAULT; 283f4fcbbe9SPaul Mackerras 284f4fcbbe9SPaul Mackerras if (ppos) 285f4fcbbe9SPaul Mackerras *ppos = msglen; 286f4fcbbe9SPaul Mackerras return msglen; 287f4fcbbe9SPaul Mackerras } 288f4fcbbe9SPaul Mackerras 289ae883cabSJohn Rose /* constructor for flash_block_cache */ 29051cc5068SAlexey Dobriyan void rtas_block_ctor(void *ptr) 291ae883cabSJohn Rose { 292ae883cabSJohn Rose memset(ptr, 0, RTAS_BLK_SIZE); 293ae883cabSJohn Rose } 294ae883cabSJohn Rose 295f4fcbbe9SPaul Mackerras /* We could be much more efficient here. But to keep this function 296f4fcbbe9SPaul Mackerras * simple we allocate a page to the block list no matter how small the 297f4fcbbe9SPaul Mackerras * count is. If the system is low on memory it will be just as well 298f4fcbbe9SPaul Mackerras * that we fail.... 299f4fcbbe9SPaul Mackerras */ 300f4fcbbe9SPaul Mackerras static ssize_t rtas_flash_write(struct file *file, const char __user *buffer, 301f4fcbbe9SPaul Mackerras size_t count, loff_t *off) 302f4fcbbe9SPaul Mackerras { 303b4d1ab58SJosef Sipek struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode); 304f4fcbbe9SPaul Mackerras struct rtas_update_flash_t *uf; 305f4fcbbe9SPaul Mackerras char *p; 306f4fcbbe9SPaul Mackerras int next_free; 307f4fcbbe9SPaul Mackerras struct flash_block_list *fl; 308f4fcbbe9SPaul Mackerras 309f4fcbbe9SPaul Mackerras uf = (struct rtas_update_flash_t *) dp->data; 310f4fcbbe9SPaul Mackerras 311f4fcbbe9SPaul Mackerras if (uf->status == FLASH_AUTH || count == 0) 312f4fcbbe9SPaul Mackerras return count; /* discard data */ 313f4fcbbe9SPaul Mackerras 314f4fcbbe9SPaul Mackerras /* In the case that the image is not ready for flashing, the memory 315f4fcbbe9SPaul Mackerras * allocated for the block list will be freed upon the release of the 316f4fcbbe9SPaul Mackerras * proc file 317f4fcbbe9SPaul Mackerras */ 318f4fcbbe9SPaul Mackerras if (uf->flist == NULL) { 319ae883cabSJohn Rose uf->flist = kmem_cache_alloc(flash_block_cache, GFP_KERNEL); 320f4fcbbe9SPaul Mackerras if (!uf->flist) 321f4fcbbe9SPaul Mackerras return -ENOMEM; 322f4fcbbe9SPaul Mackerras } 323f4fcbbe9SPaul Mackerras 324f4fcbbe9SPaul Mackerras fl = uf->flist; 325f4fcbbe9SPaul Mackerras while (fl->next) 326f4fcbbe9SPaul Mackerras fl = fl->next; /* seek to last block_list for append */ 327f4fcbbe9SPaul Mackerras next_free = fl->num_blocks; 328f4fcbbe9SPaul Mackerras if (next_free == FLASH_BLOCKS_PER_NODE) { 329f4fcbbe9SPaul Mackerras /* Need to allocate another block_list */ 330ae883cabSJohn Rose fl->next = kmem_cache_alloc(flash_block_cache, GFP_KERNEL); 331f4fcbbe9SPaul Mackerras if (!fl->next) 332f4fcbbe9SPaul Mackerras return -ENOMEM; 333f4fcbbe9SPaul Mackerras fl = fl->next; 334f4fcbbe9SPaul Mackerras next_free = 0; 335f4fcbbe9SPaul Mackerras } 336f4fcbbe9SPaul Mackerras 337ae883cabSJohn Rose if (count > RTAS_BLK_SIZE) 338ae883cabSJohn Rose count = RTAS_BLK_SIZE; 339ae883cabSJohn Rose p = kmem_cache_alloc(flash_block_cache, GFP_KERNEL); 340f4fcbbe9SPaul Mackerras if (!p) 341f4fcbbe9SPaul Mackerras return -ENOMEM; 342f4fcbbe9SPaul Mackerras 343f4fcbbe9SPaul Mackerras if(copy_from_user(p, buffer, count)) { 344ae883cabSJohn Rose kmem_cache_free(flash_block_cache, p); 345f4fcbbe9SPaul Mackerras return -EFAULT; 346f4fcbbe9SPaul Mackerras } 347f4fcbbe9SPaul Mackerras fl->blocks[next_free].data = p; 348f4fcbbe9SPaul Mackerras fl->blocks[next_free].length = count; 349f4fcbbe9SPaul Mackerras fl->num_blocks++; 350f4fcbbe9SPaul Mackerras 351f4fcbbe9SPaul Mackerras return count; 352f4fcbbe9SPaul Mackerras } 353f4fcbbe9SPaul Mackerras 354f4fcbbe9SPaul Mackerras static int rtas_excl_open(struct inode *inode, struct file *file) 355f4fcbbe9SPaul Mackerras { 356f4fcbbe9SPaul Mackerras struct proc_dir_entry *dp = PDE(inode); 357f4fcbbe9SPaul Mackerras 358f4fcbbe9SPaul Mackerras /* Enforce exclusive open with use count of PDE */ 359f4fcbbe9SPaul Mackerras spin_lock(&flash_file_open_lock); 36074848398SMaxim Shchetynin if (atomic_read(&dp->count) > 2) { 361f4fcbbe9SPaul Mackerras spin_unlock(&flash_file_open_lock); 362f4fcbbe9SPaul Mackerras return -EBUSY; 363f4fcbbe9SPaul Mackerras } 364f4fcbbe9SPaul Mackerras 365f4fcbbe9SPaul Mackerras atomic_inc(&dp->count); 366f4fcbbe9SPaul Mackerras spin_unlock(&flash_file_open_lock); 367f4fcbbe9SPaul Mackerras 368f4fcbbe9SPaul Mackerras return 0; 369f4fcbbe9SPaul Mackerras } 370f4fcbbe9SPaul Mackerras 371f4fcbbe9SPaul Mackerras static int rtas_excl_release(struct inode *inode, struct file *file) 372f4fcbbe9SPaul Mackerras { 373f4fcbbe9SPaul Mackerras struct proc_dir_entry *dp = PDE(inode); 374f4fcbbe9SPaul Mackerras 375f4fcbbe9SPaul Mackerras atomic_dec(&dp->count); 376f4fcbbe9SPaul Mackerras 377f4fcbbe9SPaul Mackerras return 0; 378f4fcbbe9SPaul Mackerras } 379f4fcbbe9SPaul Mackerras 380f4fcbbe9SPaul Mackerras static void manage_flash(struct rtas_manage_flash_t *args_buf) 381f4fcbbe9SPaul Mackerras { 382f4fcbbe9SPaul Mackerras s32 rc; 383f4fcbbe9SPaul Mackerras 384507279dbSJohn Rose do { 385f4fcbbe9SPaul Mackerras rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1, 386f4fcbbe9SPaul Mackerras 1, NULL, args_buf->op); 387507279dbSJohn Rose } while (rtas_busy_delay(rc)); 388f4fcbbe9SPaul Mackerras 389f4fcbbe9SPaul Mackerras args_buf->status = rc; 390f4fcbbe9SPaul Mackerras } 391f4fcbbe9SPaul Mackerras 392f4fcbbe9SPaul Mackerras static ssize_t manage_flash_read(struct file *file, char __user *buf, 393f4fcbbe9SPaul Mackerras size_t count, loff_t *ppos) 394f4fcbbe9SPaul Mackerras { 395b4d1ab58SJosef Sipek struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode); 396f4fcbbe9SPaul Mackerras struct rtas_manage_flash_t *args_buf; 397f4fcbbe9SPaul Mackerras char msg[RTAS_MSG_MAXLEN]; 398f4fcbbe9SPaul Mackerras int msglen; 399f4fcbbe9SPaul Mackerras 400f4fcbbe9SPaul Mackerras args_buf = (struct rtas_manage_flash_t *) dp->data; 401f4fcbbe9SPaul Mackerras if (args_buf == NULL) 402f4fcbbe9SPaul Mackerras return 0; 403f4fcbbe9SPaul Mackerras 404f4fcbbe9SPaul Mackerras msglen = sprintf(msg, "%d\n", args_buf->status); 405f4fcbbe9SPaul Mackerras if (msglen > count) 406f4fcbbe9SPaul Mackerras msglen = count; 407f4fcbbe9SPaul Mackerras 408f4fcbbe9SPaul Mackerras if (ppos && *ppos != 0) 409f4fcbbe9SPaul Mackerras return 0; /* be cheap */ 410f4fcbbe9SPaul Mackerras 411f4fcbbe9SPaul Mackerras if (!access_ok(VERIFY_WRITE, buf, msglen)) 412f4fcbbe9SPaul Mackerras return -EINVAL; 413f4fcbbe9SPaul Mackerras 414f4fcbbe9SPaul Mackerras if (copy_to_user(buf, msg, msglen)) 415f4fcbbe9SPaul Mackerras return -EFAULT; 416f4fcbbe9SPaul Mackerras 417f4fcbbe9SPaul Mackerras if (ppos) 418f4fcbbe9SPaul Mackerras *ppos = msglen; 419f4fcbbe9SPaul Mackerras return msglen; 420f4fcbbe9SPaul Mackerras } 421f4fcbbe9SPaul Mackerras 422f4fcbbe9SPaul Mackerras static ssize_t manage_flash_write(struct file *file, const char __user *buf, 423f4fcbbe9SPaul Mackerras size_t count, loff_t *off) 424f4fcbbe9SPaul Mackerras { 425b4d1ab58SJosef Sipek struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode); 426f4fcbbe9SPaul Mackerras struct rtas_manage_flash_t *args_buf; 427f4fcbbe9SPaul Mackerras const char reject_str[] = "0"; 428f4fcbbe9SPaul Mackerras const char commit_str[] = "1"; 429f4fcbbe9SPaul Mackerras char stkbuf[10]; 430f4fcbbe9SPaul Mackerras int op; 431f4fcbbe9SPaul Mackerras 432f4fcbbe9SPaul Mackerras args_buf = (struct rtas_manage_flash_t *) dp->data; 433f4fcbbe9SPaul Mackerras if ((args_buf->status == MANAGE_AUTH) || (count == 0)) 434f4fcbbe9SPaul Mackerras return count; 435f4fcbbe9SPaul Mackerras 436f4fcbbe9SPaul Mackerras op = -1; 437f4fcbbe9SPaul Mackerras if (buf) { 438f4fcbbe9SPaul Mackerras if (count > 9) count = 9; 439f4fcbbe9SPaul Mackerras if (copy_from_user (stkbuf, buf, count)) { 440f4fcbbe9SPaul Mackerras return -EFAULT; 441f4fcbbe9SPaul Mackerras } 442f4fcbbe9SPaul Mackerras if (strncmp(stkbuf, reject_str, strlen(reject_str)) == 0) 443f4fcbbe9SPaul Mackerras op = RTAS_REJECT_TMP_IMG; 444f4fcbbe9SPaul Mackerras else if (strncmp(stkbuf, commit_str, strlen(commit_str)) == 0) 445f4fcbbe9SPaul Mackerras op = RTAS_COMMIT_TMP_IMG; 446f4fcbbe9SPaul Mackerras } 447f4fcbbe9SPaul Mackerras 448f4fcbbe9SPaul Mackerras if (op == -1) /* buf is empty, or contains invalid string */ 449f4fcbbe9SPaul Mackerras return -EINVAL; 450f4fcbbe9SPaul Mackerras 451f4fcbbe9SPaul Mackerras args_buf->op = op; 452f4fcbbe9SPaul Mackerras manage_flash(args_buf); 453f4fcbbe9SPaul Mackerras 454f4fcbbe9SPaul Mackerras return count; 455f4fcbbe9SPaul Mackerras } 456f4fcbbe9SPaul Mackerras 457f4fcbbe9SPaul Mackerras static void validate_flash(struct rtas_validate_flash_t *args_buf) 458f4fcbbe9SPaul Mackerras { 459f4fcbbe9SPaul Mackerras int token = rtas_token("ibm,validate-flash-image"); 460f4fcbbe9SPaul Mackerras int update_results; 461f4fcbbe9SPaul Mackerras s32 rc; 462f4fcbbe9SPaul Mackerras 463f4fcbbe9SPaul Mackerras rc = 0; 464507279dbSJohn Rose do { 465f4fcbbe9SPaul Mackerras spin_lock(&rtas_data_buf_lock); 466f4fcbbe9SPaul Mackerras memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE); 467f4fcbbe9SPaul Mackerras rc = rtas_call(token, 2, 2, &update_results, 468f4fcbbe9SPaul Mackerras (u32) __pa(rtas_data_buf), args_buf->buf_size); 469f4fcbbe9SPaul Mackerras memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE); 470f4fcbbe9SPaul Mackerras spin_unlock(&rtas_data_buf_lock); 471507279dbSJohn Rose } while (rtas_busy_delay(rc)); 472f4fcbbe9SPaul Mackerras 473f4fcbbe9SPaul Mackerras args_buf->status = rc; 474f4fcbbe9SPaul Mackerras args_buf->update_results = update_results; 475f4fcbbe9SPaul Mackerras } 476f4fcbbe9SPaul Mackerras 477f4fcbbe9SPaul Mackerras static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf, 478f4fcbbe9SPaul Mackerras char *msg) 479f4fcbbe9SPaul Mackerras { 480f4fcbbe9SPaul Mackerras int n; 481f4fcbbe9SPaul Mackerras 482f4fcbbe9SPaul Mackerras if (args_buf->status >= VALIDATE_TMP_UPDATE) { 483f4fcbbe9SPaul Mackerras n = sprintf(msg, "%d\n", args_buf->update_results); 484f4fcbbe9SPaul Mackerras if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) || 485f4fcbbe9SPaul Mackerras (args_buf->update_results == VALIDATE_TMP_UPDATE)) 486f4fcbbe9SPaul Mackerras n += sprintf(msg + n, "%s\n", args_buf->buf); 487f4fcbbe9SPaul Mackerras } else { 488f4fcbbe9SPaul Mackerras n = sprintf(msg, "%d\n", args_buf->status); 489f4fcbbe9SPaul Mackerras } 490f4fcbbe9SPaul Mackerras return n; 491f4fcbbe9SPaul Mackerras } 492f4fcbbe9SPaul Mackerras 493f4fcbbe9SPaul Mackerras static ssize_t validate_flash_read(struct file *file, char __user *buf, 494f4fcbbe9SPaul Mackerras size_t count, loff_t *ppos) 495f4fcbbe9SPaul Mackerras { 496b4d1ab58SJosef Sipek struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode); 497f4fcbbe9SPaul Mackerras struct rtas_validate_flash_t *args_buf; 498f4fcbbe9SPaul Mackerras char msg[RTAS_MSG_MAXLEN]; 499f4fcbbe9SPaul Mackerras int msglen; 500f4fcbbe9SPaul Mackerras 501f4fcbbe9SPaul Mackerras args_buf = (struct rtas_validate_flash_t *) dp->data; 502f4fcbbe9SPaul Mackerras 503f4fcbbe9SPaul Mackerras if (ppos && *ppos != 0) 504f4fcbbe9SPaul Mackerras return 0; /* be cheap */ 505f4fcbbe9SPaul Mackerras 506f4fcbbe9SPaul Mackerras msglen = get_validate_flash_msg(args_buf, msg); 507f4fcbbe9SPaul Mackerras if (msglen > count) 508f4fcbbe9SPaul Mackerras msglen = count; 509f4fcbbe9SPaul Mackerras 510f4fcbbe9SPaul Mackerras if (!access_ok(VERIFY_WRITE, buf, msglen)) 511f4fcbbe9SPaul Mackerras return -EINVAL; 512f4fcbbe9SPaul Mackerras 513f4fcbbe9SPaul Mackerras if (copy_to_user(buf, msg, msglen)) 514f4fcbbe9SPaul Mackerras return -EFAULT; 515f4fcbbe9SPaul Mackerras 516f4fcbbe9SPaul Mackerras if (ppos) 517f4fcbbe9SPaul Mackerras *ppos = msglen; 518f4fcbbe9SPaul Mackerras return msglen; 519f4fcbbe9SPaul Mackerras } 520f4fcbbe9SPaul Mackerras 521f4fcbbe9SPaul Mackerras static ssize_t validate_flash_write(struct file *file, const char __user *buf, 522f4fcbbe9SPaul Mackerras size_t count, loff_t *off) 523f4fcbbe9SPaul Mackerras { 524b4d1ab58SJosef Sipek struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode); 525f4fcbbe9SPaul Mackerras struct rtas_validate_flash_t *args_buf; 526f4fcbbe9SPaul Mackerras int rc; 527f4fcbbe9SPaul Mackerras 528f4fcbbe9SPaul Mackerras args_buf = (struct rtas_validate_flash_t *) dp->data; 529f4fcbbe9SPaul Mackerras 530f4fcbbe9SPaul Mackerras if (dp->data == NULL) { 531f4fcbbe9SPaul Mackerras dp->data = kmalloc(sizeof(struct rtas_validate_flash_t), 532f4fcbbe9SPaul Mackerras GFP_KERNEL); 533f4fcbbe9SPaul Mackerras if (dp->data == NULL) 534f4fcbbe9SPaul Mackerras return -ENOMEM; 535f4fcbbe9SPaul Mackerras } 536f4fcbbe9SPaul Mackerras 537f4fcbbe9SPaul Mackerras /* We are only interested in the first 4K of the 538f4fcbbe9SPaul Mackerras * candidate image */ 539f4fcbbe9SPaul Mackerras if ((*off >= VALIDATE_BUF_SIZE) || 540f4fcbbe9SPaul Mackerras (args_buf->status == VALIDATE_AUTH)) { 541f4fcbbe9SPaul Mackerras *off += count; 542f4fcbbe9SPaul Mackerras return count; 543f4fcbbe9SPaul Mackerras } 544f4fcbbe9SPaul Mackerras 545f4fcbbe9SPaul Mackerras if (*off + count >= VALIDATE_BUF_SIZE) { 546f4fcbbe9SPaul Mackerras count = VALIDATE_BUF_SIZE - *off; 547f4fcbbe9SPaul Mackerras args_buf->status = VALIDATE_READY; 548f4fcbbe9SPaul Mackerras } else { 549f4fcbbe9SPaul Mackerras args_buf->status = VALIDATE_INCOMPLETE; 550f4fcbbe9SPaul Mackerras } 551f4fcbbe9SPaul Mackerras 552f4fcbbe9SPaul Mackerras if (!access_ok(VERIFY_READ, buf, count)) { 553f4fcbbe9SPaul Mackerras rc = -EFAULT; 554f4fcbbe9SPaul Mackerras goto done; 555f4fcbbe9SPaul Mackerras } 556f4fcbbe9SPaul Mackerras if (copy_from_user(args_buf->buf + *off, buf, count)) { 557f4fcbbe9SPaul Mackerras rc = -EFAULT; 558f4fcbbe9SPaul Mackerras goto done; 559f4fcbbe9SPaul Mackerras } 560f4fcbbe9SPaul Mackerras 561f4fcbbe9SPaul Mackerras *off += count; 562f4fcbbe9SPaul Mackerras rc = count; 563f4fcbbe9SPaul Mackerras done: 564f4fcbbe9SPaul Mackerras if (rc < 0) { 565f4fcbbe9SPaul Mackerras kfree(dp->data); 566f4fcbbe9SPaul Mackerras dp->data = NULL; 567f4fcbbe9SPaul Mackerras } 568f4fcbbe9SPaul Mackerras return rc; 569f4fcbbe9SPaul Mackerras } 570f4fcbbe9SPaul Mackerras 571f4fcbbe9SPaul Mackerras static int validate_flash_release(struct inode *inode, struct file *file) 572f4fcbbe9SPaul Mackerras { 573b4d1ab58SJosef Sipek struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode); 574f4fcbbe9SPaul Mackerras struct rtas_validate_flash_t *args_buf; 575f4fcbbe9SPaul Mackerras 576f4fcbbe9SPaul Mackerras args_buf = (struct rtas_validate_flash_t *) dp->data; 577f4fcbbe9SPaul Mackerras 578f4fcbbe9SPaul Mackerras if (args_buf->status == VALIDATE_READY) { 579f4fcbbe9SPaul Mackerras args_buf->buf_size = VALIDATE_BUF_SIZE; 580f4fcbbe9SPaul Mackerras validate_flash(args_buf); 581f4fcbbe9SPaul Mackerras } 582f4fcbbe9SPaul Mackerras 583f4fcbbe9SPaul Mackerras /* The matching atomic_inc was in rtas_excl_open() */ 584f4fcbbe9SPaul Mackerras atomic_dec(&dp->count); 585f4fcbbe9SPaul Mackerras 586f4fcbbe9SPaul Mackerras return 0; 587f4fcbbe9SPaul Mackerras } 588f4fcbbe9SPaul Mackerras 589f4fcbbe9SPaul Mackerras static void rtas_flash_firmware(int reboot_type) 590f4fcbbe9SPaul Mackerras { 591f4fcbbe9SPaul Mackerras unsigned long image_size; 592f4fcbbe9SPaul Mackerras struct flash_block_list *f, *next, *flist; 593f4fcbbe9SPaul Mackerras unsigned long rtas_block_list; 594f4fcbbe9SPaul Mackerras int i, status, update_token; 595f4fcbbe9SPaul Mackerras 596f4fcbbe9SPaul Mackerras if (rtas_firmware_flash_list.next == NULL) 597f4fcbbe9SPaul Mackerras return; /* nothing to do */ 598f4fcbbe9SPaul Mackerras 599f4fcbbe9SPaul Mackerras if (reboot_type != SYS_RESTART) { 600f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n"); 601f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n"); 602f4fcbbe9SPaul Mackerras return; 603f4fcbbe9SPaul Mackerras } 604f4fcbbe9SPaul Mackerras 605f4fcbbe9SPaul Mackerras update_token = rtas_token("ibm,update-flash-64-and-reboot"); 606f4fcbbe9SPaul Mackerras if (update_token == RTAS_UNKNOWN_SERVICE) { 607f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot " 608f4fcbbe9SPaul Mackerras "is not available -- not a service partition?\n"); 609f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: firmware will not be flashed\n"); 610f4fcbbe9SPaul Mackerras return; 611f4fcbbe9SPaul Mackerras } 612f4fcbbe9SPaul Mackerras 613f4fcbbe9SPaul Mackerras /* NOTE: the "first" block list is a global var with no data 614f4fcbbe9SPaul Mackerras * blocks in the kernel data segment. We do this because 615f4fcbbe9SPaul Mackerras * we want to ensure this block_list addr is under 4GB. 616f4fcbbe9SPaul Mackerras */ 617f4fcbbe9SPaul Mackerras rtas_firmware_flash_list.num_blocks = 0; 618f4fcbbe9SPaul Mackerras flist = (struct flash_block_list *)&rtas_firmware_flash_list; 619f4fcbbe9SPaul Mackerras rtas_block_list = virt_to_abs(flist); 620f4fcbbe9SPaul Mackerras if (rtas_block_list >= 4UL*1024*1024*1024) { 621f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n"); 622f4fcbbe9SPaul Mackerras return; 623f4fcbbe9SPaul Mackerras } 624f4fcbbe9SPaul Mackerras 625f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n"); 626f4fcbbe9SPaul Mackerras /* Update the block_list in place. */ 627f4fcbbe9SPaul Mackerras image_size = 0; 628f4fcbbe9SPaul Mackerras for (f = flist; f; f = next) { 629f4fcbbe9SPaul Mackerras /* Translate data addrs to absolute */ 630f4fcbbe9SPaul Mackerras for (i = 0; i < f->num_blocks; i++) { 631f4fcbbe9SPaul Mackerras f->blocks[i].data = (char *)virt_to_abs(f->blocks[i].data); 632f4fcbbe9SPaul Mackerras image_size += f->blocks[i].length; 633f4fcbbe9SPaul Mackerras } 634f4fcbbe9SPaul Mackerras next = f->next; 635f4fcbbe9SPaul Mackerras /* Don't translate NULL pointer for last entry */ 636f4fcbbe9SPaul Mackerras if (f->next) 637f4fcbbe9SPaul Mackerras f->next = (struct flash_block_list *)virt_to_abs(f->next); 638f4fcbbe9SPaul Mackerras else 639f4fcbbe9SPaul Mackerras f->next = NULL; 640f4fcbbe9SPaul Mackerras /* make num_blocks into the version/length field */ 641f4fcbbe9SPaul Mackerras f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16); 642f4fcbbe9SPaul Mackerras } 643f4fcbbe9SPaul Mackerras 644f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size); 645f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: performing flash and reboot\n"); 646f4fcbbe9SPaul Mackerras rtas_progress("Flashing \n", 0x0); 647f4fcbbe9SPaul Mackerras rtas_progress("Please Wait... ", 0x0); 648f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n"); 649f4fcbbe9SPaul Mackerras status = rtas_call(update_token, 1, 1, NULL, rtas_block_list); 650f4fcbbe9SPaul Mackerras switch (status) { /* should only get "bad" status */ 651f4fcbbe9SPaul Mackerras case 0: 652f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: success\n"); 653f4fcbbe9SPaul Mackerras break; 654f4fcbbe9SPaul Mackerras case -1: 655f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n"); 656f4fcbbe9SPaul Mackerras break; 657f4fcbbe9SPaul Mackerras case -3: 658f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n"); 659f4fcbbe9SPaul Mackerras break; 660f4fcbbe9SPaul Mackerras case -4: 661f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n"); 662f4fcbbe9SPaul Mackerras break; 663f4fcbbe9SPaul Mackerras default: 664f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status); 665f4fcbbe9SPaul Mackerras break; 666f4fcbbe9SPaul Mackerras } 667f4fcbbe9SPaul Mackerras } 668f4fcbbe9SPaul Mackerras 669f4fcbbe9SPaul Mackerras static void remove_flash_pde(struct proc_dir_entry *dp) 670f4fcbbe9SPaul Mackerras { 671f4fcbbe9SPaul Mackerras if (dp) { 672f4fcbbe9SPaul Mackerras kfree(dp->data); 673f4fcbbe9SPaul Mackerras remove_proc_entry(dp->name, dp->parent); 674f4fcbbe9SPaul Mackerras } 675f4fcbbe9SPaul Mackerras } 676f4fcbbe9SPaul Mackerras 677f4fcbbe9SPaul Mackerras static int initialize_flash_pde_data(const char *rtas_call_name, 678f4fcbbe9SPaul Mackerras size_t buf_size, 679f4fcbbe9SPaul Mackerras struct proc_dir_entry *dp) 680f4fcbbe9SPaul Mackerras { 681f4fcbbe9SPaul Mackerras int *status; 682f4fcbbe9SPaul Mackerras int token; 683f4fcbbe9SPaul Mackerras 684f8485350SYan Burman dp->data = kzalloc(buf_size, GFP_KERNEL); 685f4fcbbe9SPaul Mackerras if (dp->data == NULL) { 686f4fcbbe9SPaul Mackerras remove_flash_pde(dp); 687f4fcbbe9SPaul Mackerras return -ENOMEM; 688f4fcbbe9SPaul Mackerras } 689f4fcbbe9SPaul Mackerras 690f4fcbbe9SPaul Mackerras /* 691f4fcbbe9SPaul Mackerras * This code assumes that the status int is the first member of the 692f4fcbbe9SPaul Mackerras * struct 693f4fcbbe9SPaul Mackerras */ 694f4fcbbe9SPaul Mackerras status = (int *) dp->data; 695f4fcbbe9SPaul Mackerras token = rtas_token(rtas_call_name); 696f4fcbbe9SPaul Mackerras if (token == RTAS_UNKNOWN_SERVICE) 697f4fcbbe9SPaul Mackerras *status = FLASH_AUTH; 698f4fcbbe9SPaul Mackerras else 699f4fcbbe9SPaul Mackerras *status = FLASH_NO_OP; 700f4fcbbe9SPaul Mackerras 701f4fcbbe9SPaul Mackerras return 0; 702f4fcbbe9SPaul Mackerras } 703f4fcbbe9SPaul Mackerras 704f4fcbbe9SPaul Mackerras static struct proc_dir_entry *create_flash_pde(const char *filename, 7055dfe4c96SArjan van de Ven const struct file_operations *fops) 706f4fcbbe9SPaul Mackerras { 70766747138SDenis V. Lunev return proc_create(filename, S_IRUSR | S_IWUSR, NULL, fops); 708f4fcbbe9SPaul Mackerras } 709f4fcbbe9SPaul Mackerras 7105dfe4c96SArjan van de Ven static const struct file_operations rtas_flash_operations = { 71166747138SDenis V. Lunev .owner = THIS_MODULE, 712f4fcbbe9SPaul Mackerras .read = rtas_flash_read, 713f4fcbbe9SPaul Mackerras .write = rtas_flash_write, 714f4fcbbe9SPaul Mackerras .open = rtas_excl_open, 715f4fcbbe9SPaul Mackerras .release = rtas_flash_release, 716f4fcbbe9SPaul Mackerras }; 717f4fcbbe9SPaul Mackerras 7185dfe4c96SArjan van de Ven static const struct file_operations manage_flash_operations = { 71966747138SDenis V. Lunev .owner = THIS_MODULE, 720f4fcbbe9SPaul Mackerras .read = manage_flash_read, 721f4fcbbe9SPaul Mackerras .write = manage_flash_write, 722f4fcbbe9SPaul Mackerras .open = rtas_excl_open, 723f4fcbbe9SPaul Mackerras .release = rtas_excl_release, 724f4fcbbe9SPaul Mackerras }; 725f4fcbbe9SPaul Mackerras 7265dfe4c96SArjan van de Ven static const struct file_operations validate_flash_operations = { 72766747138SDenis V. Lunev .owner = THIS_MODULE, 728f4fcbbe9SPaul Mackerras .read = validate_flash_read, 729f4fcbbe9SPaul Mackerras .write = validate_flash_write, 730f4fcbbe9SPaul Mackerras .open = rtas_excl_open, 731f4fcbbe9SPaul Mackerras .release = validate_flash_release, 732f4fcbbe9SPaul Mackerras }; 733f4fcbbe9SPaul Mackerras 7341c21a293SMichael Ellerman static int __init rtas_flash_init(void) 735f4fcbbe9SPaul Mackerras { 736f4fcbbe9SPaul Mackerras int rc; 737f4fcbbe9SPaul Mackerras 738f4fcbbe9SPaul Mackerras if (rtas_token("ibm,update-flash-64-and-reboot") == 739f4fcbbe9SPaul Mackerras RTAS_UNKNOWN_SERVICE) { 740f4fcbbe9SPaul Mackerras printk(KERN_ERR "rtas_flash: no firmware flash support\n"); 741f4fcbbe9SPaul Mackerras return 1; 742f4fcbbe9SPaul Mackerras } 743f4fcbbe9SPaul Mackerras 744188917e1SBenjamin Herrenschmidt firmware_flash_pde = create_flash_pde("powerpc/rtas/" 745f4fcbbe9SPaul Mackerras FIRMWARE_FLASH_NAME, 746f4fcbbe9SPaul Mackerras &rtas_flash_operations); 747f4fcbbe9SPaul Mackerras if (firmware_flash_pde == NULL) { 748f4fcbbe9SPaul Mackerras rc = -ENOMEM; 749f4fcbbe9SPaul Mackerras goto cleanup; 750f4fcbbe9SPaul Mackerras } 751f4fcbbe9SPaul Mackerras 752f4fcbbe9SPaul Mackerras rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot", 753f4fcbbe9SPaul Mackerras sizeof(struct rtas_update_flash_t), 754f4fcbbe9SPaul Mackerras firmware_flash_pde); 755f4fcbbe9SPaul Mackerras if (rc != 0) 756f4fcbbe9SPaul Mackerras goto cleanup; 757f4fcbbe9SPaul Mackerras 758188917e1SBenjamin Herrenschmidt firmware_update_pde = create_flash_pde("powerpc/rtas/" 759f4fcbbe9SPaul Mackerras FIRMWARE_UPDATE_NAME, 760f4fcbbe9SPaul Mackerras &rtas_flash_operations); 761f4fcbbe9SPaul Mackerras if (firmware_update_pde == NULL) { 762f4fcbbe9SPaul Mackerras rc = -ENOMEM; 763f4fcbbe9SPaul Mackerras goto cleanup; 764f4fcbbe9SPaul Mackerras } 765f4fcbbe9SPaul Mackerras 766f4fcbbe9SPaul Mackerras rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot", 767f4fcbbe9SPaul Mackerras sizeof(struct rtas_update_flash_t), 768f4fcbbe9SPaul Mackerras firmware_update_pde); 769f4fcbbe9SPaul Mackerras if (rc != 0) 770f4fcbbe9SPaul Mackerras goto cleanup; 771f4fcbbe9SPaul Mackerras 772188917e1SBenjamin Herrenschmidt validate_pde = create_flash_pde("powerpc/rtas/" VALIDATE_FLASH_NAME, 773f4fcbbe9SPaul Mackerras &validate_flash_operations); 774f4fcbbe9SPaul Mackerras if (validate_pde == NULL) { 775f4fcbbe9SPaul Mackerras rc = -ENOMEM; 776f4fcbbe9SPaul Mackerras goto cleanup; 777f4fcbbe9SPaul Mackerras } 778f4fcbbe9SPaul Mackerras 779f4fcbbe9SPaul Mackerras rc = initialize_flash_pde_data("ibm,validate-flash-image", 780f4fcbbe9SPaul Mackerras sizeof(struct rtas_validate_flash_t), 781f4fcbbe9SPaul Mackerras validate_pde); 782f4fcbbe9SPaul Mackerras if (rc != 0) 783f4fcbbe9SPaul Mackerras goto cleanup; 784f4fcbbe9SPaul Mackerras 785188917e1SBenjamin Herrenschmidt manage_pde = create_flash_pde("powerpc/rtas/" MANAGE_FLASH_NAME, 786f4fcbbe9SPaul Mackerras &manage_flash_operations); 787f4fcbbe9SPaul Mackerras if (manage_pde == NULL) { 788f4fcbbe9SPaul Mackerras rc = -ENOMEM; 789f4fcbbe9SPaul Mackerras goto cleanup; 790f4fcbbe9SPaul Mackerras } 791f4fcbbe9SPaul Mackerras 792f4fcbbe9SPaul Mackerras rc = initialize_flash_pde_data("ibm,manage-flash-image", 793f4fcbbe9SPaul Mackerras sizeof(struct rtas_manage_flash_t), 794f4fcbbe9SPaul Mackerras manage_pde); 795f4fcbbe9SPaul Mackerras if (rc != 0) 796f4fcbbe9SPaul Mackerras goto cleanup; 797f4fcbbe9SPaul Mackerras 798f4fcbbe9SPaul Mackerras rtas_flash_term_hook = rtas_flash_firmware; 799ae883cabSJohn Rose 800ae883cabSJohn Rose flash_block_cache = kmem_cache_create("rtas_flash_cache", 801ae883cabSJohn Rose RTAS_BLK_SIZE, RTAS_BLK_SIZE, 0, 80220c2df83SPaul Mundt rtas_block_ctor); 803ae883cabSJohn Rose if (!flash_block_cache) { 804ae883cabSJohn Rose printk(KERN_ERR "%s: failed to create block cache\n", 805e48b1b45SHarvey Harrison __func__); 806ae883cabSJohn Rose rc = -ENOMEM; 807ae883cabSJohn Rose goto cleanup; 808ae883cabSJohn Rose } 809f4fcbbe9SPaul Mackerras return 0; 810f4fcbbe9SPaul Mackerras 811f4fcbbe9SPaul Mackerras cleanup: 812f4fcbbe9SPaul Mackerras remove_flash_pde(firmware_flash_pde); 813f4fcbbe9SPaul Mackerras remove_flash_pde(firmware_update_pde); 814f4fcbbe9SPaul Mackerras remove_flash_pde(validate_pde); 815f4fcbbe9SPaul Mackerras remove_flash_pde(manage_pde); 816f4fcbbe9SPaul Mackerras 817f4fcbbe9SPaul Mackerras return rc; 818f4fcbbe9SPaul Mackerras } 819f4fcbbe9SPaul Mackerras 8201c21a293SMichael Ellerman static void __exit rtas_flash_cleanup(void) 821f4fcbbe9SPaul Mackerras { 822f4fcbbe9SPaul Mackerras rtas_flash_term_hook = NULL; 823ae883cabSJohn Rose 824ae883cabSJohn Rose if (flash_block_cache) 825ae883cabSJohn Rose kmem_cache_destroy(flash_block_cache); 826ae883cabSJohn Rose 827f4fcbbe9SPaul Mackerras remove_flash_pde(firmware_flash_pde); 828f4fcbbe9SPaul Mackerras remove_flash_pde(firmware_update_pde); 829f4fcbbe9SPaul Mackerras remove_flash_pde(validate_pde); 830f4fcbbe9SPaul Mackerras remove_flash_pde(manage_pde); 831f4fcbbe9SPaul Mackerras } 832f4fcbbe9SPaul Mackerras 833f4fcbbe9SPaul Mackerras module_init(rtas_flash_init); 834f4fcbbe9SPaul Mackerras module_exit(rtas_flash_cleanup); 835f4fcbbe9SPaul Mackerras MODULE_LICENSE("GPL"); 836