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