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