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> 185a0e3ad6STejun Heo #include <linux/slab.h> 19f4fcbbe9SPaul Mackerras #include <linux/proc_fs.h> 20c5f41752SAmerigo Wang #include <linux/reboot.h> 21f4fcbbe9SPaul Mackerras #include <asm/delay.h> 22f4fcbbe9SPaul Mackerras #include <asm/uaccess.h> 23f4fcbbe9SPaul Mackerras #include <asm/rtas.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 */ 60f51005d7SVasant Hegde 61f51005d7SVasant Hegde /* ibm,validate-flash-image update result tokens */ 62f51005d7SVasant Hegde #define VALIDATE_TMP_UPDATE 0 /* T side will be updated */ 63f51005d7SVasant Hegde #define VALIDATE_FLASH_AUTH 1 /* Partition does not have authority */ 64f51005d7SVasant Hegde #define VALIDATE_INVALID_IMG 2 /* Candidate image is not valid */ 65f51005d7SVasant Hegde #define VALIDATE_CUR_UNKNOWN 3 /* Current fixpack level is unknown */ 66f51005d7SVasant Hegde /* 67f51005d7SVasant Hegde * Current T side will be committed to P side before being replace with new 68f51005d7SVasant Hegde * image, and the new image is downlevel from current image 69f51005d7SVasant Hegde */ 70f51005d7SVasant Hegde #define VALIDATE_TMP_COMMIT_DL 4 71f51005d7SVasant Hegde /* 72f51005d7SVasant Hegde * Current T side will be committed to P side before being replaced with new 73f51005d7SVasant Hegde * image 74f51005d7SVasant Hegde */ 75f51005d7SVasant Hegde #define VALIDATE_TMP_COMMIT 5 76f51005d7SVasant Hegde /* 77f51005d7SVasant Hegde * T side will be updated with a downlevel image 78f51005d7SVasant Hegde */ 79f51005d7SVasant Hegde #define VALIDATE_TMP_UPDATE_DL 6 80fd2d37c8SVasant Hegde /* 81fd2d37c8SVasant Hegde * The candidate image's release date is later than the system's firmware 82fd2d37c8SVasant Hegde * service entitlement date - service warranty period has expired 83fd2d37c8SVasant Hegde */ 84fd2d37c8SVasant Hegde #define VALIDATE_OUT_OF_WRNTY 7 85f4fcbbe9SPaul Mackerras 86f4fcbbe9SPaul Mackerras /* ibm,manage-flash-image operation tokens */ 87f4fcbbe9SPaul Mackerras #define RTAS_REJECT_TMP_IMG 0 88f4fcbbe9SPaul Mackerras #define RTAS_COMMIT_TMP_IMG 1 89f4fcbbe9SPaul Mackerras 90f4fcbbe9SPaul Mackerras /* Array sizes */ 91f4fcbbe9SPaul Mackerras #define VALIDATE_BUF_SIZE 4096 92a94a1472SVasant Hegde #define VALIDATE_MSG_LEN 256 93f4fcbbe9SPaul Mackerras #define RTAS_MSG_MAXLEN 64 94f4fcbbe9SPaul Mackerras 95ae883cabSJohn Rose /* Quirk - RTAS requires 4k list length and block size */ 96ae883cabSJohn Rose #define RTAS_BLKLIST_LENGTH 4096 97ae883cabSJohn Rose #define RTAS_BLK_SIZE 4096 98ae883cabSJohn Rose 99f4fcbbe9SPaul Mackerras struct flash_block { 100f4fcbbe9SPaul Mackerras char *data; 101f4fcbbe9SPaul Mackerras unsigned long length; 102f4fcbbe9SPaul Mackerras }; 103f4fcbbe9SPaul Mackerras 104f4fcbbe9SPaul Mackerras /* This struct is very similar but not identical to 105f4fcbbe9SPaul Mackerras * that needed by the rtas flash update. 106f4fcbbe9SPaul Mackerras * All we need to do for rtas is rewrite num_blocks 107f4fcbbe9SPaul Mackerras * into a version/length and translate the pointers 108f4fcbbe9SPaul Mackerras * to absolute. 109f4fcbbe9SPaul Mackerras */ 110ae883cabSJohn Rose #define FLASH_BLOCKS_PER_NODE ((RTAS_BLKLIST_LENGTH - 16) / sizeof(struct flash_block)) 111f4fcbbe9SPaul Mackerras struct flash_block_list { 112f4fcbbe9SPaul Mackerras unsigned long num_blocks; 113f4fcbbe9SPaul Mackerras struct flash_block_list *next; 114f4fcbbe9SPaul Mackerras struct flash_block blocks[FLASH_BLOCKS_PER_NODE]; 115f4fcbbe9SPaul Mackerras }; 116f4fcbbe9SPaul Mackerras 117bd2b64a1SMilton Miller static struct flash_block_list *rtas_firmware_flash_list; 118f4fcbbe9SPaul Mackerras 119ae883cabSJohn Rose /* Use slab cache to guarantee 4k alignment */ 120e18b890bSChristoph Lameter static struct kmem_cache *flash_block_cache = NULL; 121ae883cabSJohn Rose 122f4fcbbe9SPaul Mackerras #define FLASH_BLOCK_LIST_VERSION (1UL) 123f4fcbbe9SPaul Mackerras 124e8eeded3SDavid Howells /* 125e8eeded3SDavid Howells * Local copy of the flash block list. 126e8eeded3SDavid Howells * 127e8eeded3SDavid Howells * The rtas_firmware_flash_list varable will be 128bd2b64a1SMilton Miller * set once the data is fully read. 129f4fcbbe9SPaul Mackerras * 130f4fcbbe9SPaul Mackerras * For convenience as we build the list we use virtual addrs, 131f4fcbbe9SPaul Mackerras * we do not fill in the version number, and the length field 132f4fcbbe9SPaul Mackerras * is treated as the number of entries currently in the block 133bd2b64a1SMilton Miller * (i.e. not a byte count). This is all fixed when calling 134bd2b64a1SMilton Miller * the flash routine. 135f4fcbbe9SPaul Mackerras */ 136f4fcbbe9SPaul Mackerras 137f4fcbbe9SPaul Mackerras /* Status int must be first member of struct */ 138f4fcbbe9SPaul Mackerras struct rtas_update_flash_t 139f4fcbbe9SPaul Mackerras { 140f4fcbbe9SPaul Mackerras int status; /* Flash update status */ 141f4fcbbe9SPaul Mackerras struct flash_block_list *flist; /* Local copy of flash block list */ 142f4fcbbe9SPaul Mackerras }; 143f4fcbbe9SPaul Mackerras 144f4fcbbe9SPaul Mackerras /* Status int must be first member of struct */ 145f4fcbbe9SPaul Mackerras struct rtas_manage_flash_t 146f4fcbbe9SPaul Mackerras { 147f4fcbbe9SPaul Mackerras int status; /* Returned status */ 148f4fcbbe9SPaul Mackerras }; 149f4fcbbe9SPaul Mackerras 150f4fcbbe9SPaul Mackerras /* Status int must be first member of struct */ 151f4fcbbe9SPaul Mackerras struct rtas_validate_flash_t 152f4fcbbe9SPaul Mackerras { 153f4fcbbe9SPaul Mackerras int status; /* Returned status */ 154e8eeded3SDavid Howells char *buf; /* Candidate image buffer */ 155f4fcbbe9SPaul Mackerras unsigned int buf_size; /* Size of image buf */ 156f4fcbbe9SPaul Mackerras unsigned int update_results; /* Update results token */ 157f4fcbbe9SPaul Mackerras }; 158f4fcbbe9SPaul Mackerras 159e8eeded3SDavid Howells static struct rtas_update_flash_t rtas_update_flash_data; 160e8eeded3SDavid Howells static struct rtas_manage_flash_t rtas_manage_flash_data; 161e8eeded3SDavid Howells static struct rtas_validate_flash_t rtas_validate_flash_data; 162e8eeded3SDavid Howells static DEFINE_MUTEX(rtas_update_flash_mutex); 163e8eeded3SDavid Howells static DEFINE_MUTEX(rtas_manage_flash_mutex); 164e8eeded3SDavid Howells static DEFINE_MUTEX(rtas_validate_flash_mutex); 165f4fcbbe9SPaul Mackerras 166f4fcbbe9SPaul Mackerras /* Do simple sanity checks on the flash image. */ 167f4fcbbe9SPaul Mackerras static int flash_list_valid(struct flash_block_list *flist) 168f4fcbbe9SPaul Mackerras { 169f4fcbbe9SPaul Mackerras struct flash_block_list *f; 170f4fcbbe9SPaul Mackerras int i; 171f4fcbbe9SPaul Mackerras unsigned long block_size, image_size; 172f4fcbbe9SPaul Mackerras 173f4fcbbe9SPaul Mackerras /* Paranoid self test here. We also collect the image size. */ 174f4fcbbe9SPaul Mackerras image_size = 0; 175f4fcbbe9SPaul Mackerras for (f = flist; f; f = f->next) { 176f4fcbbe9SPaul Mackerras for (i = 0; i < f->num_blocks; i++) { 177f4fcbbe9SPaul Mackerras if (f->blocks[i].data == NULL) { 178f4fcbbe9SPaul Mackerras return FLASH_IMG_NULL_DATA; 179f4fcbbe9SPaul Mackerras } 180f4fcbbe9SPaul Mackerras block_size = f->blocks[i].length; 181ae883cabSJohn Rose if (block_size <= 0 || block_size > RTAS_BLK_SIZE) { 182f4fcbbe9SPaul Mackerras return FLASH_IMG_BAD_LEN; 183f4fcbbe9SPaul Mackerras } 184f4fcbbe9SPaul Mackerras image_size += block_size; 185f4fcbbe9SPaul Mackerras } 186f4fcbbe9SPaul Mackerras } 187f4fcbbe9SPaul Mackerras 188f4fcbbe9SPaul Mackerras if (image_size < (256 << 10)) { 189f4fcbbe9SPaul Mackerras if (image_size < 2) 190f4fcbbe9SPaul Mackerras return FLASH_NO_OP; 191f4fcbbe9SPaul Mackerras } 192f4fcbbe9SPaul Mackerras 193f4fcbbe9SPaul Mackerras printk(KERN_INFO "FLASH: flash image with %ld bytes stored for hardware flash on reboot\n", image_size); 194f4fcbbe9SPaul Mackerras 195f4fcbbe9SPaul Mackerras return FLASH_IMG_READY; 196f4fcbbe9SPaul Mackerras } 197f4fcbbe9SPaul Mackerras 198f4fcbbe9SPaul Mackerras static void free_flash_list(struct flash_block_list *f) 199f4fcbbe9SPaul Mackerras { 200f4fcbbe9SPaul Mackerras struct flash_block_list *next; 201f4fcbbe9SPaul Mackerras int i; 202f4fcbbe9SPaul Mackerras 203f4fcbbe9SPaul Mackerras while (f) { 204f4fcbbe9SPaul Mackerras for (i = 0; i < f->num_blocks; i++) 205ae883cabSJohn Rose kmem_cache_free(flash_block_cache, f->blocks[i].data); 206f4fcbbe9SPaul Mackerras next = f->next; 207ae883cabSJohn Rose kmem_cache_free(flash_block_cache, f); 208f4fcbbe9SPaul Mackerras f = next; 209f4fcbbe9SPaul Mackerras } 210f4fcbbe9SPaul Mackerras } 211f4fcbbe9SPaul Mackerras 212f4fcbbe9SPaul Mackerras static int rtas_flash_release(struct inode *inode, struct file *file) 213f4fcbbe9SPaul Mackerras { 214e8eeded3SDavid Howells struct rtas_update_flash_t *const uf = &rtas_update_flash_data; 215f4fcbbe9SPaul Mackerras 216e8eeded3SDavid Howells mutex_lock(&rtas_update_flash_mutex); 217e8eeded3SDavid Howells 218f4fcbbe9SPaul Mackerras if (uf->flist) { 219f4fcbbe9SPaul Mackerras /* File was opened in write mode for a new flash attempt */ 220f4fcbbe9SPaul Mackerras /* Clear saved list */ 221bd2b64a1SMilton Miller if (rtas_firmware_flash_list) { 222bd2b64a1SMilton Miller free_flash_list(rtas_firmware_flash_list); 223bd2b64a1SMilton Miller rtas_firmware_flash_list = NULL; 224f4fcbbe9SPaul Mackerras } 225f4fcbbe9SPaul Mackerras 226f4fcbbe9SPaul Mackerras if (uf->status != FLASH_AUTH) 227f4fcbbe9SPaul Mackerras uf->status = flash_list_valid(uf->flist); 228f4fcbbe9SPaul Mackerras 229f4fcbbe9SPaul Mackerras if (uf->status == FLASH_IMG_READY) 230bd2b64a1SMilton Miller rtas_firmware_flash_list = uf->flist; 231f4fcbbe9SPaul Mackerras else 232f4fcbbe9SPaul Mackerras free_flash_list(uf->flist); 233f4fcbbe9SPaul Mackerras 234f4fcbbe9SPaul Mackerras uf->flist = NULL; 235f4fcbbe9SPaul Mackerras } 236f4fcbbe9SPaul Mackerras 237e8eeded3SDavid Howells mutex_unlock(&rtas_update_flash_mutex); 238f4fcbbe9SPaul Mackerras return 0; 239f4fcbbe9SPaul Mackerras } 240f4fcbbe9SPaul Mackerras 241e8eeded3SDavid Howells static size_t get_flash_status_msg(int status, char *buf) 242f4fcbbe9SPaul Mackerras { 243e8eeded3SDavid Howells const char *msg; 244e8eeded3SDavid Howells size_t len; 245f4fcbbe9SPaul Mackerras 246f4fcbbe9SPaul Mackerras switch (status) { 247f4fcbbe9SPaul Mackerras case FLASH_AUTH: 248f4fcbbe9SPaul Mackerras msg = "error: this partition does not have service authority\n"; 249f4fcbbe9SPaul Mackerras break; 250f4fcbbe9SPaul Mackerras case FLASH_NO_OP: 251f4fcbbe9SPaul Mackerras msg = "info: no firmware image for flash\n"; 252f4fcbbe9SPaul Mackerras break; 253f4fcbbe9SPaul Mackerras case FLASH_IMG_SHORT: 254f4fcbbe9SPaul Mackerras msg = "error: flash image short\n"; 255f4fcbbe9SPaul Mackerras break; 256f4fcbbe9SPaul Mackerras case FLASH_IMG_BAD_LEN: 257f4fcbbe9SPaul Mackerras msg = "error: internal error bad length\n"; 258f4fcbbe9SPaul Mackerras break; 259f4fcbbe9SPaul Mackerras case FLASH_IMG_NULL_DATA: 260f4fcbbe9SPaul Mackerras msg = "error: internal error null data\n"; 261f4fcbbe9SPaul Mackerras break; 262f4fcbbe9SPaul Mackerras case FLASH_IMG_READY: 263f4fcbbe9SPaul Mackerras msg = "ready: firmware image ready for flash on reboot\n"; 264f4fcbbe9SPaul Mackerras break; 265f4fcbbe9SPaul Mackerras default: 266e8eeded3SDavid Howells return sprintf(buf, "error: unexpected status value %d\n", 267e8eeded3SDavid Howells status); 268f4fcbbe9SPaul Mackerras } 269f4fcbbe9SPaul Mackerras 270e8eeded3SDavid Howells len = strlen(msg); 271e8eeded3SDavid Howells memcpy(buf, msg, len + 1); 272e8eeded3SDavid Howells return len; 273f4fcbbe9SPaul Mackerras } 274f4fcbbe9SPaul Mackerras 275f4fcbbe9SPaul Mackerras /* Reading the proc file will show status (not the firmware contents) */ 276e8eeded3SDavid Howells static ssize_t rtas_flash_read_msg(struct file *file, char __user *buf, 277f4fcbbe9SPaul Mackerras size_t count, loff_t *ppos) 278f4fcbbe9SPaul Mackerras { 279e8eeded3SDavid Howells struct rtas_update_flash_t *const uf = &rtas_update_flash_data; 280f4fcbbe9SPaul Mackerras char msg[RTAS_MSG_MAXLEN]; 281e8eeded3SDavid Howells size_t len; 282e8eeded3SDavid Howells int status; 283f4fcbbe9SPaul Mackerras 284e8eeded3SDavid Howells mutex_lock(&rtas_update_flash_mutex); 285e8eeded3SDavid Howells status = uf->status; 286e8eeded3SDavid Howells mutex_unlock(&rtas_update_flash_mutex); 287f4fcbbe9SPaul Mackerras 288e8eeded3SDavid Howells /* Read as text message */ 289e8eeded3SDavid Howells len = get_flash_status_msg(status, msg); 290e8eeded3SDavid Howells return simple_read_from_buffer(buf, count, ppos, msg, len); 291f4fcbbe9SPaul Mackerras } 292f4fcbbe9SPaul Mackerras 293e8eeded3SDavid Howells static ssize_t rtas_flash_read_num(struct file *file, char __user *buf, 294e8eeded3SDavid Howells size_t count, loff_t *ppos) 295e8eeded3SDavid Howells { 296e8eeded3SDavid Howells struct rtas_update_flash_t *const uf = &rtas_update_flash_data; 297e8eeded3SDavid Howells char msg[RTAS_MSG_MAXLEN]; 298e8eeded3SDavid Howells int status; 299e8eeded3SDavid Howells 300e8eeded3SDavid Howells mutex_lock(&rtas_update_flash_mutex); 301e8eeded3SDavid Howells status = uf->status; 302e8eeded3SDavid Howells mutex_unlock(&rtas_update_flash_mutex); 303e8eeded3SDavid Howells 304e8eeded3SDavid Howells /* Read as number */ 305e8eeded3SDavid Howells sprintf(msg, "%d\n", status); 3064c4a5cf6SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, msg, strlen(msg)); 307f4fcbbe9SPaul Mackerras } 308f4fcbbe9SPaul Mackerras 309f4fcbbe9SPaul Mackerras /* We could be much more efficient here. But to keep this function 310f4fcbbe9SPaul Mackerras * simple we allocate a page to the block list no matter how small the 311f4fcbbe9SPaul Mackerras * count is. If the system is low on memory it will be just as well 312f4fcbbe9SPaul Mackerras * that we fail.... 313f4fcbbe9SPaul Mackerras */ 314f4fcbbe9SPaul Mackerras static ssize_t rtas_flash_write(struct file *file, const char __user *buffer, 315f4fcbbe9SPaul Mackerras size_t count, loff_t *off) 316f4fcbbe9SPaul Mackerras { 317e8eeded3SDavid Howells struct rtas_update_flash_t *const uf = &rtas_update_flash_data; 318f4fcbbe9SPaul Mackerras char *p; 319e8eeded3SDavid Howells int next_free, rc; 320f4fcbbe9SPaul Mackerras struct flash_block_list *fl; 321f4fcbbe9SPaul Mackerras 322e8eeded3SDavid Howells mutex_lock(&rtas_update_flash_mutex); 323f4fcbbe9SPaul Mackerras 324f4fcbbe9SPaul Mackerras if (uf->status == FLASH_AUTH || count == 0) 325e8eeded3SDavid Howells goto out; /* discard data */ 326f4fcbbe9SPaul Mackerras 327f4fcbbe9SPaul Mackerras /* In the case that the image is not ready for flashing, the memory 328f4fcbbe9SPaul Mackerras * allocated for the block list will be freed upon the release of the 329f4fcbbe9SPaul Mackerras * proc file 330f4fcbbe9SPaul Mackerras */ 331f4fcbbe9SPaul Mackerras if (uf->flist == NULL) { 332fb4696c3SVasant Hegde uf->flist = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL); 333f4fcbbe9SPaul Mackerras if (!uf->flist) 334e8eeded3SDavid Howells goto nomem; 335f4fcbbe9SPaul Mackerras } 336f4fcbbe9SPaul Mackerras 337f4fcbbe9SPaul Mackerras fl = uf->flist; 338f4fcbbe9SPaul Mackerras while (fl->next) 339f4fcbbe9SPaul Mackerras fl = fl->next; /* seek to last block_list for append */ 340f4fcbbe9SPaul Mackerras next_free = fl->num_blocks; 341f4fcbbe9SPaul Mackerras if (next_free == FLASH_BLOCKS_PER_NODE) { 342f4fcbbe9SPaul Mackerras /* Need to allocate another block_list */ 343fb4696c3SVasant Hegde fl->next = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL); 344f4fcbbe9SPaul Mackerras if (!fl->next) 345e8eeded3SDavid Howells goto nomem; 346f4fcbbe9SPaul Mackerras fl = fl->next; 347f4fcbbe9SPaul Mackerras next_free = 0; 348f4fcbbe9SPaul Mackerras } 349f4fcbbe9SPaul Mackerras 350ae883cabSJohn Rose if (count > RTAS_BLK_SIZE) 351ae883cabSJohn Rose count = RTAS_BLK_SIZE; 352fb4696c3SVasant Hegde p = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL); 353f4fcbbe9SPaul Mackerras if (!p) 354e8eeded3SDavid Howells goto nomem; 355f4fcbbe9SPaul Mackerras 356f4fcbbe9SPaul Mackerras if(copy_from_user(p, buffer, count)) { 357ae883cabSJohn Rose kmem_cache_free(flash_block_cache, p); 358e8eeded3SDavid Howells rc = -EFAULT; 359e8eeded3SDavid Howells goto error; 360f4fcbbe9SPaul Mackerras } 361f4fcbbe9SPaul Mackerras fl->blocks[next_free].data = p; 362f4fcbbe9SPaul Mackerras fl->blocks[next_free].length = count; 363f4fcbbe9SPaul Mackerras fl->num_blocks++; 364e8eeded3SDavid Howells out: 365e8eeded3SDavid Howells mutex_unlock(&rtas_update_flash_mutex); 366f4fcbbe9SPaul Mackerras return count; 367e8eeded3SDavid Howells 368e8eeded3SDavid Howells nomem: 369e8eeded3SDavid Howells rc = -ENOMEM; 370e8eeded3SDavid Howells error: 371e8eeded3SDavid Howells mutex_unlock(&rtas_update_flash_mutex); 372e8eeded3SDavid Howells return rc; 373f4fcbbe9SPaul Mackerras } 374f4fcbbe9SPaul Mackerras 375e8eeded3SDavid Howells /* 376e8eeded3SDavid Howells * Flash management routines. 377e8eeded3SDavid Howells */ 378e8eeded3SDavid Howells static void manage_flash(struct rtas_manage_flash_t *args_buf, unsigned int op) 379f4fcbbe9SPaul Mackerras { 380f4fcbbe9SPaul Mackerras s32 rc; 381f4fcbbe9SPaul Mackerras 382507279dbSJohn Rose do { 383e8eeded3SDavid Howells rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1, 1, 384e8eeded3SDavid Howells NULL, op); 385507279dbSJohn Rose } while (rtas_busy_delay(rc)); 386f4fcbbe9SPaul Mackerras 387f4fcbbe9SPaul Mackerras args_buf->status = rc; 388f4fcbbe9SPaul Mackerras } 389f4fcbbe9SPaul Mackerras 390f4fcbbe9SPaul Mackerras static ssize_t manage_flash_read(struct file *file, char __user *buf, 391f4fcbbe9SPaul Mackerras size_t count, loff_t *ppos) 392f4fcbbe9SPaul Mackerras { 393e8eeded3SDavid Howells struct rtas_manage_flash_t *const args_buf = &rtas_manage_flash_data; 394f4fcbbe9SPaul Mackerras char msg[RTAS_MSG_MAXLEN]; 395e8eeded3SDavid Howells int msglen, status; 396f4fcbbe9SPaul Mackerras 397e8eeded3SDavid Howells mutex_lock(&rtas_manage_flash_mutex); 398e8eeded3SDavid Howells status = args_buf->status; 399e8eeded3SDavid Howells mutex_unlock(&rtas_manage_flash_mutex); 400f4fcbbe9SPaul Mackerras 401e8eeded3SDavid Howells msglen = sprintf(msg, "%d\n", status); 4024c4a5cf6SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, msg, msglen); 403f4fcbbe9SPaul Mackerras } 404f4fcbbe9SPaul Mackerras 405f4fcbbe9SPaul Mackerras static ssize_t manage_flash_write(struct file *file, const char __user *buf, 406f4fcbbe9SPaul Mackerras size_t count, loff_t *off) 407f4fcbbe9SPaul Mackerras { 408e8eeded3SDavid Howells struct rtas_manage_flash_t *const args_buf = &rtas_manage_flash_data; 409e8eeded3SDavid Howells static const char reject_str[] = "0"; 410e8eeded3SDavid Howells static const char commit_str[] = "1"; 411f4fcbbe9SPaul Mackerras char stkbuf[10]; 412e8eeded3SDavid Howells int op, rc; 413f4fcbbe9SPaul Mackerras 414e8eeded3SDavid Howells mutex_lock(&rtas_manage_flash_mutex); 415e8eeded3SDavid Howells 416f4fcbbe9SPaul Mackerras if ((args_buf->status == MANAGE_AUTH) || (count == 0)) 417e8eeded3SDavid Howells goto out; 418f4fcbbe9SPaul Mackerras 419f4fcbbe9SPaul Mackerras op = -1; 420f4fcbbe9SPaul Mackerras if (buf) { 421f4fcbbe9SPaul Mackerras if (count > 9) count = 9; 422e8eeded3SDavid Howells rc = -EFAULT; 423e8eeded3SDavid Howells if (copy_from_user (stkbuf, buf, count)) 424e8eeded3SDavid Howells goto error; 425f4fcbbe9SPaul Mackerras if (strncmp(stkbuf, reject_str, strlen(reject_str)) == 0) 426f4fcbbe9SPaul Mackerras op = RTAS_REJECT_TMP_IMG; 427f4fcbbe9SPaul Mackerras else if (strncmp(stkbuf, commit_str, strlen(commit_str)) == 0) 428f4fcbbe9SPaul Mackerras op = RTAS_COMMIT_TMP_IMG; 429f4fcbbe9SPaul Mackerras } 430f4fcbbe9SPaul Mackerras 431e8eeded3SDavid Howells if (op == -1) { /* buf is empty, or contains invalid string */ 432e8eeded3SDavid Howells rc = -EINVAL; 433e8eeded3SDavid Howells goto error; 434f4fcbbe9SPaul Mackerras } 435f4fcbbe9SPaul Mackerras 436e8eeded3SDavid Howells manage_flash(args_buf, op); 437e8eeded3SDavid Howells out: 438e8eeded3SDavid Howells mutex_unlock(&rtas_manage_flash_mutex); 439e8eeded3SDavid Howells return count; 440e8eeded3SDavid Howells 441e8eeded3SDavid Howells error: 442e8eeded3SDavid Howells mutex_unlock(&rtas_manage_flash_mutex); 443e8eeded3SDavid Howells return rc; 444e8eeded3SDavid Howells } 445e8eeded3SDavid Howells 446e8eeded3SDavid Howells /* 447e8eeded3SDavid Howells * Validation routines. 448e8eeded3SDavid Howells */ 449f4fcbbe9SPaul Mackerras static void validate_flash(struct rtas_validate_flash_t *args_buf) 450f4fcbbe9SPaul Mackerras { 451f4fcbbe9SPaul Mackerras int token = rtas_token("ibm,validate-flash-image"); 452f4fcbbe9SPaul Mackerras int update_results; 453f4fcbbe9SPaul Mackerras s32 rc; 454f4fcbbe9SPaul Mackerras 455f4fcbbe9SPaul Mackerras rc = 0; 456507279dbSJohn Rose do { 457f4fcbbe9SPaul Mackerras spin_lock(&rtas_data_buf_lock); 458f4fcbbe9SPaul Mackerras memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE); 459f4fcbbe9SPaul Mackerras rc = rtas_call(token, 2, 2, &update_results, 460f4fcbbe9SPaul Mackerras (u32) __pa(rtas_data_buf), args_buf->buf_size); 461f4fcbbe9SPaul Mackerras memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE); 462f4fcbbe9SPaul Mackerras spin_unlock(&rtas_data_buf_lock); 463507279dbSJohn Rose } while (rtas_busy_delay(rc)); 464f4fcbbe9SPaul Mackerras 465f4fcbbe9SPaul Mackerras args_buf->status = rc; 466f4fcbbe9SPaul Mackerras args_buf->update_results = update_results; 467f4fcbbe9SPaul Mackerras } 468f4fcbbe9SPaul Mackerras 469f4fcbbe9SPaul Mackerras static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf, 470a94a1472SVasant Hegde char *msg, int msglen) 471f4fcbbe9SPaul Mackerras { 472f4fcbbe9SPaul Mackerras int n; 473f4fcbbe9SPaul Mackerras 474f4fcbbe9SPaul Mackerras if (args_buf->status >= VALIDATE_TMP_UPDATE) { 475f4fcbbe9SPaul Mackerras n = sprintf(msg, "%d\n", args_buf->update_results); 476f4fcbbe9SPaul Mackerras if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) || 477f4fcbbe9SPaul Mackerras (args_buf->update_results == VALIDATE_TMP_UPDATE)) 478a94a1472SVasant Hegde n += snprintf(msg + n, msglen - n, "%s\n", 479a94a1472SVasant Hegde args_buf->buf); 480f4fcbbe9SPaul Mackerras } else { 481f4fcbbe9SPaul Mackerras n = sprintf(msg, "%d\n", args_buf->status); 482f4fcbbe9SPaul Mackerras } 483f4fcbbe9SPaul Mackerras return n; 484f4fcbbe9SPaul Mackerras } 485f4fcbbe9SPaul Mackerras 486f4fcbbe9SPaul Mackerras static ssize_t validate_flash_read(struct file *file, char __user *buf, 487f4fcbbe9SPaul Mackerras size_t count, loff_t *ppos) 488f4fcbbe9SPaul Mackerras { 489e8eeded3SDavid Howells struct rtas_validate_flash_t *const args_buf = 490e8eeded3SDavid Howells &rtas_validate_flash_data; 491a94a1472SVasant Hegde char msg[VALIDATE_MSG_LEN]; 492f4fcbbe9SPaul Mackerras int msglen; 493f4fcbbe9SPaul Mackerras 494e8eeded3SDavid Howells mutex_lock(&rtas_validate_flash_mutex); 495a94a1472SVasant Hegde msglen = get_validate_flash_msg(args_buf, msg, VALIDATE_MSG_LEN); 496e8eeded3SDavid Howells mutex_unlock(&rtas_validate_flash_mutex); 497f4fcbbe9SPaul Mackerras 4984c4a5cf6SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, msg, msglen); 499f4fcbbe9SPaul Mackerras } 500f4fcbbe9SPaul Mackerras 501f4fcbbe9SPaul Mackerras static ssize_t validate_flash_write(struct file *file, const char __user *buf, 502f4fcbbe9SPaul Mackerras size_t count, loff_t *off) 503f4fcbbe9SPaul Mackerras { 504e8eeded3SDavid Howells struct rtas_validate_flash_t *const args_buf = 505e8eeded3SDavid Howells &rtas_validate_flash_data; 506f4fcbbe9SPaul Mackerras int rc; 507f4fcbbe9SPaul Mackerras 508e8eeded3SDavid Howells mutex_lock(&rtas_validate_flash_mutex); 509f4fcbbe9SPaul Mackerras 510f4fcbbe9SPaul Mackerras /* We are only interested in the first 4K of the 511f4fcbbe9SPaul Mackerras * candidate image */ 512f4fcbbe9SPaul Mackerras if ((*off >= VALIDATE_BUF_SIZE) || 513f4fcbbe9SPaul Mackerras (args_buf->status == VALIDATE_AUTH)) { 514f4fcbbe9SPaul Mackerras *off += count; 515e8eeded3SDavid Howells mutex_unlock(&rtas_validate_flash_mutex); 516f4fcbbe9SPaul Mackerras return count; 517f4fcbbe9SPaul Mackerras } 518f4fcbbe9SPaul Mackerras 519f4fcbbe9SPaul Mackerras if (*off + count >= VALIDATE_BUF_SIZE) { 520f4fcbbe9SPaul Mackerras count = VALIDATE_BUF_SIZE - *off; 521f4fcbbe9SPaul Mackerras args_buf->status = VALIDATE_READY; 522f4fcbbe9SPaul Mackerras } else { 523f4fcbbe9SPaul Mackerras args_buf->status = VALIDATE_INCOMPLETE; 524f4fcbbe9SPaul Mackerras } 525f4fcbbe9SPaul Mackerras 526f4fcbbe9SPaul Mackerras if (!access_ok(VERIFY_READ, buf, count)) { 527f4fcbbe9SPaul Mackerras rc = -EFAULT; 528f4fcbbe9SPaul Mackerras goto done; 529f4fcbbe9SPaul Mackerras } 530f4fcbbe9SPaul Mackerras if (copy_from_user(args_buf->buf + *off, buf, count)) { 531f4fcbbe9SPaul Mackerras rc = -EFAULT; 532f4fcbbe9SPaul Mackerras goto done; 533f4fcbbe9SPaul Mackerras } 534f4fcbbe9SPaul Mackerras 535f4fcbbe9SPaul Mackerras *off += count; 536f4fcbbe9SPaul Mackerras rc = count; 537f4fcbbe9SPaul Mackerras done: 538e8eeded3SDavid Howells mutex_unlock(&rtas_validate_flash_mutex); 539f4fcbbe9SPaul Mackerras return rc; 540f4fcbbe9SPaul Mackerras } 541f4fcbbe9SPaul Mackerras 542f4fcbbe9SPaul Mackerras static int validate_flash_release(struct inode *inode, struct file *file) 543f4fcbbe9SPaul Mackerras { 544e8eeded3SDavid Howells struct rtas_validate_flash_t *const args_buf = 545e8eeded3SDavid Howells &rtas_validate_flash_data; 546f4fcbbe9SPaul Mackerras 547e8eeded3SDavid Howells mutex_lock(&rtas_validate_flash_mutex); 548f4fcbbe9SPaul Mackerras 549f4fcbbe9SPaul Mackerras if (args_buf->status == VALIDATE_READY) { 550f4fcbbe9SPaul Mackerras args_buf->buf_size = VALIDATE_BUF_SIZE; 551f4fcbbe9SPaul Mackerras validate_flash(args_buf); 552f4fcbbe9SPaul Mackerras } 553f4fcbbe9SPaul Mackerras 554e8eeded3SDavid Howells mutex_unlock(&rtas_validate_flash_mutex); 555f4fcbbe9SPaul Mackerras return 0; 556f4fcbbe9SPaul Mackerras } 557f4fcbbe9SPaul Mackerras 558e8eeded3SDavid Howells /* 559e8eeded3SDavid Howells * On-reboot flash update applicator. 560e8eeded3SDavid Howells */ 561f4fcbbe9SPaul Mackerras static void rtas_flash_firmware(int reboot_type) 562f4fcbbe9SPaul Mackerras { 563f4fcbbe9SPaul Mackerras unsigned long image_size; 564f4fcbbe9SPaul Mackerras struct flash_block_list *f, *next, *flist; 565f4fcbbe9SPaul Mackerras unsigned long rtas_block_list; 566f4fcbbe9SPaul Mackerras int i, status, update_token; 567f4fcbbe9SPaul Mackerras 568bd2b64a1SMilton Miller if (rtas_firmware_flash_list == NULL) 569f4fcbbe9SPaul Mackerras return; /* nothing to do */ 570f4fcbbe9SPaul Mackerras 571f4fcbbe9SPaul Mackerras if (reboot_type != SYS_RESTART) { 572f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n"); 573f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n"); 574f4fcbbe9SPaul Mackerras return; 575f4fcbbe9SPaul Mackerras } 576f4fcbbe9SPaul Mackerras 577f4fcbbe9SPaul Mackerras update_token = rtas_token("ibm,update-flash-64-and-reboot"); 578f4fcbbe9SPaul Mackerras if (update_token == RTAS_UNKNOWN_SERVICE) { 579f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot " 580f4fcbbe9SPaul Mackerras "is not available -- not a service partition?\n"); 581f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: firmware will not be flashed\n"); 582f4fcbbe9SPaul Mackerras return; 583f4fcbbe9SPaul Mackerras } 584f4fcbbe9SPaul Mackerras 585bd2b64a1SMilton Miller /* 586df17f56dSRavi K. Nittala * Just before starting the firmware flash, cancel the event scan work 587df17f56dSRavi K. Nittala * to avoid any soft lockup issues. 588df17f56dSRavi K. Nittala */ 589df17f56dSRavi K. Nittala rtas_cancel_event_scan(); 590df17f56dSRavi K. Nittala 591df17f56dSRavi K. Nittala /* 592bd2b64a1SMilton Miller * NOTE: the "first" block must be under 4GB, so we create 593bd2b64a1SMilton Miller * an entry with no data blocks in the reserved buffer in 594bd2b64a1SMilton Miller * the kernel data segment. 595f4fcbbe9SPaul Mackerras */ 596bd2b64a1SMilton Miller spin_lock(&rtas_data_buf_lock); 597bd2b64a1SMilton Miller flist = (struct flash_block_list *)&rtas_data_buf[0]; 598bd2b64a1SMilton Miller flist->num_blocks = 0; 599bd2b64a1SMilton Miller flist->next = rtas_firmware_flash_list; 6003d267523SMichael Ellerman rtas_block_list = __pa(flist); 601f4fcbbe9SPaul Mackerras if (rtas_block_list >= 4UL*1024*1024*1024) { 602f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n"); 603bd2b64a1SMilton Miller spin_unlock(&rtas_data_buf_lock); 604f4fcbbe9SPaul Mackerras return; 605f4fcbbe9SPaul Mackerras } 606f4fcbbe9SPaul Mackerras 607f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n"); 608f4fcbbe9SPaul Mackerras /* Update the block_list in place. */ 609bd2b64a1SMilton Miller rtas_firmware_flash_list = NULL; /* too hard to backout on error */ 610f4fcbbe9SPaul Mackerras image_size = 0; 611f4fcbbe9SPaul Mackerras for (f = flist; f; f = next) { 612f4fcbbe9SPaul Mackerras /* Translate data addrs to absolute */ 613f4fcbbe9SPaul Mackerras for (i = 0; i < f->num_blocks; i++) { 6143d267523SMichael Ellerman f->blocks[i].data = (char *)__pa(f->blocks[i].data); 615f4fcbbe9SPaul Mackerras image_size += f->blocks[i].length; 616f4fcbbe9SPaul Mackerras } 617f4fcbbe9SPaul Mackerras next = f->next; 618f4fcbbe9SPaul Mackerras /* Don't translate NULL pointer for last entry */ 619f4fcbbe9SPaul Mackerras if (f->next) 6203d267523SMichael Ellerman f->next = (struct flash_block_list *)__pa(f->next); 621f4fcbbe9SPaul Mackerras else 622f4fcbbe9SPaul Mackerras f->next = NULL; 623f4fcbbe9SPaul Mackerras /* make num_blocks into the version/length field */ 624f4fcbbe9SPaul Mackerras f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16); 625f4fcbbe9SPaul Mackerras } 626f4fcbbe9SPaul Mackerras 627f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size); 628f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: performing flash and reboot\n"); 629f4fcbbe9SPaul Mackerras rtas_progress("Flashing \n", 0x0); 630f4fcbbe9SPaul Mackerras rtas_progress("Please Wait... ", 0x0); 631f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n"); 632f4fcbbe9SPaul Mackerras status = rtas_call(update_token, 1, 1, NULL, rtas_block_list); 633f4fcbbe9SPaul Mackerras switch (status) { /* should only get "bad" status */ 634f4fcbbe9SPaul Mackerras case 0: 635f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: success\n"); 636f4fcbbe9SPaul Mackerras break; 637f4fcbbe9SPaul Mackerras case -1: 638f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n"); 639f4fcbbe9SPaul Mackerras break; 640f4fcbbe9SPaul Mackerras case -3: 641f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n"); 642f4fcbbe9SPaul Mackerras break; 643f4fcbbe9SPaul Mackerras case -4: 644f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n"); 645f4fcbbe9SPaul Mackerras break; 646f4fcbbe9SPaul Mackerras default: 647f4fcbbe9SPaul Mackerras printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status); 648f4fcbbe9SPaul Mackerras break; 649f4fcbbe9SPaul Mackerras } 650bd2b64a1SMilton Miller spin_unlock(&rtas_data_buf_lock); 651f4fcbbe9SPaul Mackerras } 652f4fcbbe9SPaul Mackerras 653f4fcbbe9SPaul Mackerras /* 654e8eeded3SDavid Howells * Manifest of proc files to create 655f4fcbbe9SPaul Mackerras */ 656e8eeded3SDavid Howells struct rtas_flash_file { 657e8eeded3SDavid Howells const char *filename; 658e8eeded3SDavid Howells const char *rtas_call_name; 659e8eeded3SDavid Howells int *status; 660e8eeded3SDavid Howells const struct file_operations fops; 661e8eeded3SDavid Howells }; 662f4fcbbe9SPaul Mackerras 663e8eeded3SDavid Howells static const struct rtas_flash_file rtas_flash_files[] = { 664f4fcbbe9SPaul Mackerras { 665e8eeded3SDavid Howells .filename = "powerpc/rtas/" FIRMWARE_FLASH_NAME, 666e8eeded3SDavid Howells .rtas_call_name = "ibm,update-flash-64-and-reboot", 667e8eeded3SDavid Howells .status = &rtas_update_flash_data.status, 668e8eeded3SDavid Howells .fops.read = rtas_flash_read_msg, 669e8eeded3SDavid Howells .fops.write = rtas_flash_write, 670e8eeded3SDavid Howells .fops.release = rtas_flash_release, 671e8eeded3SDavid Howells .fops.llseek = default_llseek, 672e8eeded3SDavid Howells }, 673e8eeded3SDavid Howells { 674e8eeded3SDavid Howells .filename = "powerpc/rtas/" FIRMWARE_UPDATE_NAME, 675e8eeded3SDavid Howells .rtas_call_name = "ibm,update-flash-64-and-reboot", 676e8eeded3SDavid Howells .status = &rtas_update_flash_data.status, 677e8eeded3SDavid Howells .fops.read = rtas_flash_read_num, 678e8eeded3SDavid Howells .fops.write = rtas_flash_write, 679e8eeded3SDavid Howells .fops.release = rtas_flash_release, 680e8eeded3SDavid Howells .fops.llseek = default_llseek, 681e8eeded3SDavid Howells }, 682e8eeded3SDavid Howells { 683e8eeded3SDavid Howells .filename = "powerpc/rtas/" VALIDATE_FLASH_NAME, 684e8eeded3SDavid Howells .rtas_call_name = "ibm,validate-flash-image", 685e8eeded3SDavid Howells .status = &rtas_validate_flash_data.status, 686e8eeded3SDavid Howells .fops.read = validate_flash_read, 687e8eeded3SDavid Howells .fops.write = validate_flash_write, 688e8eeded3SDavid Howells .fops.release = validate_flash_release, 689e8eeded3SDavid Howells .fops.llseek = default_llseek, 690e8eeded3SDavid Howells }, 691e8eeded3SDavid Howells { 692e8eeded3SDavid Howells .filename = "powerpc/rtas/" MANAGE_FLASH_NAME, 693e8eeded3SDavid Howells .rtas_call_name = "ibm,manage-flash-image", 694e8eeded3SDavid Howells .status = &rtas_manage_flash_data.status, 695e8eeded3SDavid Howells .fops.read = manage_flash_read, 696e8eeded3SDavid Howells .fops.write = manage_flash_write, 697e8eeded3SDavid Howells .fops.llseek = default_llseek, 698f4fcbbe9SPaul Mackerras } 699f4fcbbe9SPaul Mackerras }; 700f4fcbbe9SPaul Mackerras 7011c21a293SMichael Ellerman static int __init rtas_flash_init(void) 702f4fcbbe9SPaul Mackerras { 703e8eeded3SDavid Howells int i; 704f4fcbbe9SPaul Mackerras 705f4fcbbe9SPaul Mackerras if (rtas_token("ibm,update-flash-64-and-reboot") == 706f4fcbbe9SPaul Mackerras RTAS_UNKNOWN_SERVICE) { 7070e384983SAnton Blanchard pr_info("rtas_flash: no firmware flash support\n"); 708*0c930692SAnton Blanchard return -EINVAL; 709f4fcbbe9SPaul Mackerras } 710f4fcbbe9SPaul Mackerras 711e8eeded3SDavid Howells rtas_validate_flash_data.buf = kzalloc(VALIDATE_BUF_SIZE, GFP_KERNEL); 712e8eeded3SDavid Howells if (!rtas_validate_flash_data.buf) 713e8eeded3SDavid Howells return -ENOMEM; 714ae883cabSJohn Rose 715ae883cabSJohn Rose flash_block_cache = kmem_cache_create("rtas_flash_cache", 716ae883cabSJohn Rose RTAS_BLK_SIZE, RTAS_BLK_SIZE, 0, 717fb4696c3SVasant Hegde NULL); 718ae883cabSJohn Rose if (!flash_block_cache) { 719ae883cabSJohn Rose printk(KERN_ERR "%s: failed to create block cache\n", 720e48b1b45SHarvey Harrison __func__); 721e8eeded3SDavid Howells goto enomem_buf; 722ae883cabSJohn Rose } 723e8eeded3SDavid Howells 724e8eeded3SDavid Howells for (i = 0; i < ARRAY_SIZE(rtas_flash_files); i++) { 725e8eeded3SDavid Howells const struct rtas_flash_file *f = &rtas_flash_files[i]; 726e8eeded3SDavid Howells int token; 727e8eeded3SDavid Howells 728e8eeded3SDavid Howells if (!proc_create(f->filename, S_IRUSR | S_IWUSR, NULL, &f->fops)) 729e8eeded3SDavid Howells goto enomem; 730e8eeded3SDavid Howells 731e8eeded3SDavid Howells /* 732e8eeded3SDavid Howells * This code assumes that the status int is the first member of the 733e8eeded3SDavid Howells * struct 734e8eeded3SDavid Howells */ 735e8eeded3SDavid Howells token = rtas_token(f->rtas_call_name); 736e8eeded3SDavid Howells if (token == RTAS_UNKNOWN_SERVICE) 737e8eeded3SDavid Howells *f->status = FLASH_AUTH; 738e8eeded3SDavid Howells else 739e8eeded3SDavid Howells *f->status = FLASH_NO_OP; 740e8eeded3SDavid Howells } 741e8eeded3SDavid Howells 742e8eeded3SDavid Howells rtas_flash_term_hook = rtas_flash_firmware; 743f4fcbbe9SPaul Mackerras return 0; 744f4fcbbe9SPaul Mackerras 745e8eeded3SDavid Howells enomem: 746e8eeded3SDavid Howells while (--i >= 0) { 747e8eeded3SDavid Howells const struct rtas_flash_file *f = &rtas_flash_files[i]; 748e8eeded3SDavid Howells remove_proc_entry(f->filename, NULL); 749e8eeded3SDavid Howells } 750f4fcbbe9SPaul Mackerras 751e8eeded3SDavid Howells kmem_cache_destroy(flash_block_cache); 752e8eeded3SDavid Howells enomem_buf: 753e8eeded3SDavid Howells kfree(rtas_validate_flash_data.buf); 754e8eeded3SDavid Howells return -ENOMEM; 755f4fcbbe9SPaul Mackerras } 756f4fcbbe9SPaul Mackerras 7571c21a293SMichael Ellerman static void __exit rtas_flash_cleanup(void) 758f4fcbbe9SPaul Mackerras { 759e8eeded3SDavid Howells int i; 760e8eeded3SDavid Howells 761f4fcbbe9SPaul Mackerras rtas_flash_term_hook = NULL; 762ae883cabSJohn Rose 763ad18a364SVasant Hegde if (rtas_firmware_flash_list) { 764ad18a364SVasant Hegde free_flash_list(rtas_firmware_flash_list); 765ad18a364SVasant Hegde rtas_firmware_flash_list = NULL; 766ad18a364SVasant Hegde } 767ad18a364SVasant Hegde 768e8eeded3SDavid Howells for (i = 0; i < ARRAY_SIZE(rtas_flash_files); i++) { 769e8eeded3SDavid Howells const struct rtas_flash_file *f = &rtas_flash_files[i]; 770e8eeded3SDavid Howells remove_proc_entry(f->filename, NULL); 771e8eeded3SDavid Howells } 772ae883cabSJohn Rose 773e8eeded3SDavid Howells kmem_cache_destroy(flash_block_cache); 774e8eeded3SDavid Howells kfree(rtas_validate_flash_data.buf); 775f4fcbbe9SPaul Mackerras } 776f4fcbbe9SPaul Mackerras 777f4fcbbe9SPaul Mackerras module_init(rtas_flash_init); 778f4fcbbe9SPaul Mackerras module_exit(rtas_flash_cleanup); 779f4fcbbe9SPaul Mackerras MODULE_LICENSE("GPL"); 780