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