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> |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 24 | #include <asm/abs_addr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #define MODULE_VERS "1.0" |
| 27 | #define MODULE_NAME "rtas_flash" |
| 28 | |
| 29 | #define FIRMWARE_FLASH_NAME "firmware_flash" |
| 30 | #define FIRMWARE_UPDATE_NAME "firmware_update" |
| 31 | #define MANAGE_FLASH_NAME "manage_flash" |
| 32 | #define VALIDATE_FLASH_NAME "validate_flash" |
| 33 | |
| 34 | /* General RTAS Status Codes */ |
| 35 | #define RTAS_RC_SUCCESS 0 |
| 36 | #define RTAS_RC_HW_ERR -1 |
| 37 | #define RTAS_RC_BUSY -2 |
| 38 | |
| 39 | /* Flash image status values */ |
| 40 | #define FLASH_AUTH -9002 /* RTAS Not Service Authority Partition */ |
| 41 | #define FLASH_NO_OP -1099 /* No operation initiated by user */ |
| 42 | #define FLASH_IMG_SHORT -1005 /* Flash image shorter than expected */ |
| 43 | #define FLASH_IMG_BAD_LEN -1004 /* Bad length value in flash list block */ |
| 44 | #define FLASH_IMG_NULL_DATA -1003 /* Bad data value in flash list block */ |
| 45 | #define FLASH_IMG_READY 0 /* Firmware img ready for flash on reboot */ |
| 46 | |
| 47 | /* Manage image status values */ |
| 48 | #define MANAGE_AUTH -9002 /* RTAS Not Service Authority Partition */ |
| 49 | #define MANAGE_ACTIVE_ERR -9001 /* RTAS Cannot Overwrite Active Img */ |
| 50 | #define MANAGE_NO_OP -1099 /* No operation initiated by user */ |
| 51 | #define MANAGE_PARAM_ERR -3 /* RTAS Parameter Error */ |
| 52 | #define MANAGE_HW_ERR -1 /* RTAS Hardware Error */ |
| 53 | |
| 54 | /* Validate image status values */ |
| 55 | #define VALIDATE_AUTH -9002 /* RTAS Not Service Authority Partition */ |
| 56 | #define VALIDATE_NO_OP -1099 /* No operation initiated by the user */ |
| 57 | #define VALIDATE_INCOMPLETE -1002 /* User copied < VALIDATE_BUF_SIZE */ |
| 58 | #define VALIDATE_READY -1001 /* Firmware image ready for validation */ |
| 59 | #define VALIDATE_PARAM_ERR -3 /* RTAS Parameter Error */ |
| 60 | #define VALIDATE_HW_ERR -1 /* RTAS Hardware Error */ |
| 61 | #define VALIDATE_TMP_UPDATE 0 /* Validate Return Status */ |
| 62 | #define VALIDATE_FLASH_AUTH 1 /* Validate Return Status */ |
| 63 | #define VALIDATE_INVALID_IMG 2 /* Validate Return Status */ |
| 64 | #define VALIDATE_CUR_UNKNOWN 3 /* Validate Return Status */ |
| 65 | #define VALIDATE_TMP_COMMIT_DL 4 /* Validate Return Status */ |
| 66 | #define VALIDATE_TMP_COMMIT 5 /* Validate Return Status */ |
| 67 | #define VALIDATE_TMP_UPDATE_DL 6 /* Validate Return Status */ |
| 68 | |
| 69 | /* ibm,manage-flash-image operation tokens */ |
| 70 | #define RTAS_REJECT_TMP_IMG 0 |
| 71 | #define RTAS_COMMIT_TMP_IMG 1 |
| 72 | |
| 73 | /* Array sizes */ |
| 74 | #define VALIDATE_BUF_SIZE 4096 |
| 75 | #define RTAS_MSG_MAXLEN 64 |
| 76 | |
John Rose | ae883ca | 2006-11-08 10:07:30 -0600 | [diff] [blame] | 77 | /* Quirk - RTAS requires 4k list length and block size */ |
| 78 | #define RTAS_BLKLIST_LENGTH 4096 |
| 79 | #define RTAS_BLK_SIZE 4096 |
| 80 | |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 81 | struct flash_block { |
| 82 | char *data; |
| 83 | unsigned long length; |
| 84 | }; |
| 85 | |
| 86 | /* This struct is very similar but not identical to |
| 87 | * that needed by the rtas flash update. |
| 88 | * All we need to do for rtas is rewrite num_blocks |
| 89 | * into a version/length and translate the pointers |
| 90 | * to absolute. |
| 91 | */ |
John Rose | ae883ca | 2006-11-08 10:07:30 -0600 | [diff] [blame] | 92 | #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] | 93 | struct flash_block_list { |
| 94 | unsigned long num_blocks; |
| 95 | struct flash_block_list *next; |
| 96 | struct flash_block blocks[FLASH_BLOCKS_PER_NODE]; |
| 97 | }; |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 98 | |
Milton Miller | bd2b64a | 2010-06-12 03:48:47 +0000 | [diff] [blame] | 99 | static struct flash_block_list *rtas_firmware_flash_list; |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 100 | |
John Rose | ae883ca | 2006-11-08 10:07:30 -0600 | [diff] [blame] | 101 | /* Use slab cache to guarantee 4k alignment */ |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 102 | static struct kmem_cache *flash_block_cache = NULL; |
John Rose | ae883ca | 2006-11-08 10:07:30 -0600 | [diff] [blame] | 103 | |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 104 | #define FLASH_BLOCK_LIST_VERSION (1UL) |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | /* Local copy of the flash block list. |
| 107 | * We only allow one open of the flash proc file and create this |
Milton Miller | bd2b64a | 2010-06-12 03:48:47 +0000 | [diff] [blame] | 108 | * list as we go. The rtas_firmware_flash_list varable will be |
| 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 */ |
| 129 | unsigned int op; /* Reject or commit image */ |
| 130 | }; |
| 131 | |
| 132 | /* Status int must be first member of struct */ |
| 133 | struct rtas_validate_flash_t |
| 134 | { |
| 135 | int status; /* Returned status */ |
| 136 | char buf[VALIDATE_BUF_SIZE]; /* Candidate image buffer */ |
| 137 | unsigned int buf_size; /* Size of image buf */ |
| 138 | unsigned int update_results; /* Update results token */ |
| 139 | }; |
| 140 | |
| 141 | static DEFINE_SPINLOCK(flash_file_open_lock); |
| 142 | static struct proc_dir_entry *firmware_flash_pde; |
| 143 | static struct proc_dir_entry *firmware_update_pde; |
| 144 | static struct proc_dir_entry *validate_pde; |
| 145 | static struct proc_dir_entry *manage_pde; |
| 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 | { |
Josef Sipek | b4d1ab5 | 2006-12-08 02:37:30 -0800 | [diff] [blame] | 195 | struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | struct rtas_update_flash_t *uf; |
| 197 | |
| 198 | uf = (struct rtas_update_flash_t *) dp->data; |
| 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 | |
| 218 | atomic_dec(&dp->count); |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static void get_flash_status_msg(int status, char *buf) |
| 223 | { |
| 224 | char *msg; |
| 225 | |
| 226 | switch (status) { |
| 227 | case FLASH_AUTH: |
| 228 | msg = "error: this partition does not have service authority\n"; |
| 229 | break; |
| 230 | case FLASH_NO_OP: |
| 231 | msg = "info: no firmware image for flash\n"; |
| 232 | break; |
| 233 | case FLASH_IMG_SHORT: |
| 234 | msg = "error: flash image short\n"; |
| 235 | break; |
| 236 | case FLASH_IMG_BAD_LEN: |
| 237 | msg = "error: internal error bad length\n"; |
| 238 | break; |
| 239 | case FLASH_IMG_NULL_DATA: |
| 240 | msg = "error: internal error null data\n"; |
| 241 | break; |
| 242 | case FLASH_IMG_READY: |
| 243 | msg = "ready: firmware image ready for flash on reboot\n"; |
| 244 | break; |
| 245 | default: |
| 246 | sprintf(buf, "error: unexpected status value %d\n", status); |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | strcpy(buf, msg); |
| 251 | } |
| 252 | |
| 253 | /* Reading the proc file will show status (not the firmware contents) */ |
Al Viro | efa5457 | 2005-04-26 11:26:53 -0700 | [diff] [blame] | 254 | static ssize_t rtas_flash_read(struct file *file, char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | size_t count, loff_t *ppos) |
| 256 | { |
Josef Sipek | b4d1ab5 | 2006-12-08 02:37:30 -0800 | [diff] [blame] | 257 | struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | struct rtas_update_flash_t *uf; |
| 259 | char msg[RTAS_MSG_MAXLEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
Akinobu Mita | 4c4a5cf | 2010-12-24 20:03:59 +0000 | [diff] [blame] | 261 | uf = dp->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
| 263 | if (!strcmp(dp->name, FIRMWARE_FLASH_NAME)) { |
| 264 | get_flash_status_msg(uf->status, msg); |
| 265 | } else { /* FIRMWARE_UPDATE_NAME */ |
| 266 | sprintf(msg, "%d\n", uf->status); |
| 267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Akinobu Mita | 4c4a5cf | 2010-12-24 20:03:59 +0000 | [diff] [blame] | 269 | return simple_read_from_buffer(buf, count, ppos, msg, strlen(msg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
| 271 | |
John Rose | ae883ca | 2006-11-08 10:07:30 -0600 | [diff] [blame] | 272 | /* constructor for flash_block_cache */ |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 273 | void rtas_block_ctor(void *ptr) |
John Rose | ae883ca | 2006-11-08 10:07:30 -0600 | [diff] [blame] | 274 | { |
| 275 | memset(ptr, 0, RTAS_BLK_SIZE); |
| 276 | } |
| 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | /* We could be much more efficient here. But to keep this function |
| 279 | * simple we allocate a page to the block list no matter how small the |
| 280 | * count is. If the system is low on memory it will be just as well |
| 281 | * that we fail.... |
| 282 | */ |
Al Viro | efa5457 | 2005-04-26 11:26:53 -0700 | [diff] [blame] | 283 | 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] | 284 | size_t count, loff_t *off) |
| 285 | { |
Josef Sipek | b4d1ab5 | 2006-12-08 02:37:30 -0800 | [diff] [blame] | 286 | struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | struct rtas_update_flash_t *uf; |
| 288 | char *p; |
| 289 | int next_free; |
| 290 | struct flash_block_list *fl; |
| 291 | |
| 292 | uf = (struct rtas_update_flash_t *) dp->data; |
| 293 | |
| 294 | if (uf->status == FLASH_AUTH || count == 0) |
| 295 | return count; /* discard data */ |
| 296 | |
| 297 | /* In the case that the image is not ready for flashing, the memory |
| 298 | * allocated for the block list will be freed upon the release of the |
| 299 | * proc file |
| 300 | */ |
| 301 | if (uf->flist == NULL) { |
John Rose | ae883ca | 2006-11-08 10:07:30 -0600 | [diff] [blame] | 302 | uf->flist = kmem_cache_alloc(flash_block_cache, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | if (!uf->flist) |
| 304 | return -ENOMEM; |
| 305 | } |
| 306 | |
| 307 | fl = uf->flist; |
| 308 | while (fl->next) |
| 309 | fl = fl->next; /* seek to last block_list for append */ |
| 310 | next_free = fl->num_blocks; |
| 311 | if (next_free == FLASH_BLOCKS_PER_NODE) { |
| 312 | /* Need to allocate another block_list */ |
John Rose | ae883ca | 2006-11-08 10:07:30 -0600 | [diff] [blame] | 313 | fl->next = kmem_cache_alloc(flash_block_cache, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | if (!fl->next) |
| 315 | return -ENOMEM; |
| 316 | fl = fl->next; |
| 317 | next_free = 0; |
| 318 | } |
| 319 | |
John Rose | ae883ca | 2006-11-08 10:07:30 -0600 | [diff] [blame] | 320 | if (count > RTAS_BLK_SIZE) |
| 321 | count = RTAS_BLK_SIZE; |
| 322 | p = kmem_cache_alloc(flash_block_cache, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | if (!p) |
| 324 | return -ENOMEM; |
| 325 | |
| 326 | if(copy_from_user(p, buffer, count)) { |
John Rose | ae883ca | 2006-11-08 10:07:30 -0600 | [diff] [blame] | 327 | kmem_cache_free(flash_block_cache, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | return -EFAULT; |
| 329 | } |
| 330 | fl->blocks[next_free].data = p; |
| 331 | fl->blocks[next_free].length = count; |
| 332 | fl->num_blocks++; |
| 333 | |
| 334 | return count; |
| 335 | } |
| 336 | |
| 337 | static int rtas_excl_open(struct inode *inode, struct file *file) |
| 338 | { |
| 339 | struct proc_dir_entry *dp = PDE(inode); |
| 340 | |
| 341 | /* Enforce exclusive open with use count of PDE */ |
| 342 | spin_lock(&flash_file_open_lock); |
Maxim Shchetynin | 7484839 | 2008-04-02 00:12:20 +1100 | [diff] [blame] | 343 | if (atomic_read(&dp->count) > 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | spin_unlock(&flash_file_open_lock); |
| 345 | return -EBUSY; |
| 346 | } |
| 347 | |
| 348 | atomic_inc(&dp->count); |
| 349 | spin_unlock(&flash_file_open_lock); |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static int rtas_excl_release(struct inode *inode, struct file *file) |
| 355 | { |
| 356 | struct proc_dir_entry *dp = PDE(inode); |
| 357 | |
| 358 | atomic_dec(&dp->count); |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static void manage_flash(struct rtas_manage_flash_t *args_buf) |
| 364 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | s32 rc; |
| 366 | |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 367 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1, |
| 369 | 1, NULL, args_buf->op); |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 370 | } while (rtas_busy_delay(rc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
| 372 | args_buf->status = rc; |
| 373 | } |
| 374 | |
Al Viro | efa5457 | 2005-04-26 11:26:53 -0700 | [diff] [blame] | 375 | static ssize_t manage_flash_read(struct file *file, char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | size_t count, loff_t *ppos) |
| 377 | { |
Josef Sipek | b4d1ab5 | 2006-12-08 02:37:30 -0800 | [diff] [blame] | 378 | struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | struct rtas_manage_flash_t *args_buf; |
| 380 | char msg[RTAS_MSG_MAXLEN]; |
| 381 | int msglen; |
| 382 | |
Akinobu Mita | 4c4a5cf | 2010-12-24 20:03:59 +0000 | [diff] [blame] | 383 | args_buf = dp->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | if (args_buf == NULL) |
| 385 | return 0; |
| 386 | |
| 387 | msglen = sprintf(msg, "%d\n", args_buf->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
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 | { |
Josef Sipek | b4d1ab5 | 2006-12-08 02:37:30 -0800 | [diff] [blame] | 395 | struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | struct rtas_manage_flash_t *args_buf; |
| 397 | const char reject_str[] = "0"; |
| 398 | const char commit_str[] = "1"; |
| 399 | char stkbuf[10]; |
| 400 | int op; |
| 401 | |
| 402 | args_buf = (struct rtas_manage_flash_t *) dp->data; |
| 403 | if ((args_buf->status == MANAGE_AUTH) || (count == 0)) |
| 404 | return count; |
| 405 | |
| 406 | op = -1; |
| 407 | if (buf) { |
| 408 | if (count > 9) count = 9; |
| 409 | if (copy_from_user (stkbuf, buf, count)) { |
| 410 | return -EFAULT; |
| 411 | } |
| 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 | |
| 418 | if (op == -1) /* buf is empty, or contains invalid string */ |
| 419 | return -EINVAL; |
| 420 | |
| 421 | args_buf->op = op; |
| 422 | manage_flash(args_buf); |
| 423 | |
| 424 | return count; |
| 425 | } |
| 426 | |
| 427 | static void validate_flash(struct rtas_validate_flash_t *args_buf) |
| 428 | { |
| 429 | int token = rtas_token("ibm,validate-flash-image"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | int update_results; |
| 431 | s32 rc; |
| 432 | |
| 433 | rc = 0; |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 434 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | spin_lock(&rtas_data_buf_lock); |
| 436 | memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE); |
| 437 | rc = rtas_call(token, 2, 2, &update_results, |
| 438 | (u32) __pa(rtas_data_buf), args_buf->buf_size); |
| 439 | memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE); |
| 440 | spin_unlock(&rtas_data_buf_lock); |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 441 | } while (rtas_busy_delay(rc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
| 443 | args_buf->status = rc; |
| 444 | args_buf->update_results = update_results; |
| 445 | } |
| 446 | |
| 447 | static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf, |
| 448 | char *msg) |
| 449 | { |
| 450 | int n; |
| 451 | |
| 452 | if (args_buf->status >= VALIDATE_TMP_UPDATE) { |
| 453 | n = sprintf(msg, "%d\n", args_buf->update_results); |
| 454 | if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) || |
| 455 | (args_buf->update_results == VALIDATE_TMP_UPDATE)) |
| 456 | n += sprintf(msg + n, "%s\n", args_buf->buf); |
| 457 | } else { |
| 458 | n = sprintf(msg, "%d\n", args_buf->status); |
| 459 | } |
| 460 | return n; |
| 461 | } |
| 462 | |
Al Viro | efa5457 | 2005-04-26 11:26:53 -0700 | [diff] [blame] | 463 | static ssize_t validate_flash_read(struct file *file, char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | size_t count, loff_t *ppos) |
| 465 | { |
Josef Sipek | b4d1ab5 | 2006-12-08 02:37:30 -0800 | [diff] [blame] | 466 | struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | struct rtas_validate_flash_t *args_buf; |
| 468 | char msg[RTAS_MSG_MAXLEN]; |
| 469 | int msglen; |
| 470 | |
Akinobu Mita | 4c4a5cf | 2010-12-24 20:03:59 +0000 | [diff] [blame] | 471 | args_buf = dp->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | msglen = get_validate_flash_msg(args_buf, msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
Akinobu Mita | 4c4a5cf | 2010-12-24 20:03:59 +0000 | [diff] [blame] | 475 | return simple_read_from_buffer(buf, count, ppos, msg, msglen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Al Viro | efa5457 | 2005-04-26 11:26:53 -0700 | [diff] [blame] | 478 | 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] | 479 | size_t count, loff_t *off) |
| 480 | { |
Josef Sipek | b4d1ab5 | 2006-12-08 02:37:30 -0800 | [diff] [blame] | 481 | struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | struct rtas_validate_flash_t *args_buf; |
| 483 | int rc; |
| 484 | |
| 485 | args_buf = (struct rtas_validate_flash_t *) dp->data; |
| 486 | |
| 487 | if (dp->data == NULL) { |
| 488 | dp->data = kmalloc(sizeof(struct rtas_validate_flash_t), |
| 489 | GFP_KERNEL); |
| 490 | if (dp->data == NULL) |
| 491 | return -ENOMEM; |
| 492 | } |
| 493 | |
| 494 | /* We are only interested in the first 4K of the |
| 495 | * candidate image */ |
| 496 | if ((*off >= VALIDATE_BUF_SIZE) || |
| 497 | (args_buf->status == VALIDATE_AUTH)) { |
| 498 | *off += count; |
| 499 | return count; |
| 500 | } |
| 501 | |
| 502 | if (*off + count >= VALIDATE_BUF_SIZE) { |
| 503 | count = VALIDATE_BUF_SIZE - *off; |
| 504 | args_buf->status = VALIDATE_READY; |
| 505 | } else { |
| 506 | args_buf->status = VALIDATE_INCOMPLETE; |
| 507 | } |
| 508 | |
| 509 | if (!access_ok(VERIFY_READ, buf, count)) { |
| 510 | rc = -EFAULT; |
| 511 | goto done; |
| 512 | } |
| 513 | if (copy_from_user(args_buf->buf + *off, buf, count)) { |
| 514 | rc = -EFAULT; |
| 515 | goto done; |
| 516 | } |
| 517 | |
| 518 | *off += count; |
| 519 | rc = count; |
| 520 | done: |
| 521 | if (rc < 0) { |
| 522 | kfree(dp->data); |
| 523 | dp->data = NULL; |
| 524 | } |
| 525 | return rc; |
| 526 | } |
| 527 | |
| 528 | static int validate_flash_release(struct inode *inode, struct file *file) |
| 529 | { |
Josef Sipek | b4d1ab5 | 2006-12-08 02:37:30 -0800 | [diff] [blame] | 530 | struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | struct rtas_validate_flash_t *args_buf; |
| 532 | |
| 533 | args_buf = (struct rtas_validate_flash_t *) dp->data; |
| 534 | |
| 535 | if (args_buf->status == VALIDATE_READY) { |
| 536 | args_buf->buf_size = VALIDATE_BUF_SIZE; |
| 537 | validate_flash(args_buf); |
| 538 | } |
| 539 | |
| 540 | /* The matching atomic_inc was in rtas_excl_open() */ |
| 541 | atomic_dec(&dp->count); |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 546 | static void rtas_flash_firmware(int reboot_type) |
| 547 | { |
| 548 | unsigned long image_size; |
| 549 | struct flash_block_list *f, *next, *flist; |
| 550 | unsigned long rtas_block_list; |
| 551 | int i, status, update_token; |
| 552 | |
Milton Miller | bd2b64a | 2010-06-12 03:48:47 +0000 | [diff] [blame] | 553 | if (rtas_firmware_flash_list == NULL) |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 554 | return; /* nothing to do */ |
| 555 | |
| 556 | if (reboot_type != SYS_RESTART) { |
| 557 | printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n"); |
| 558 | printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n"); |
| 559 | return; |
| 560 | } |
| 561 | |
| 562 | update_token = rtas_token("ibm,update-flash-64-and-reboot"); |
| 563 | if (update_token == RTAS_UNKNOWN_SERVICE) { |
| 564 | printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot " |
| 565 | "is not available -- not a service partition?\n"); |
| 566 | printk(KERN_ALERT "FLASH: firmware will not be flashed\n"); |
| 567 | return; |
| 568 | } |
| 569 | |
Milton Miller | bd2b64a | 2010-06-12 03:48:47 +0000 | [diff] [blame] | 570 | /* |
| 571 | * NOTE: the "first" block must be under 4GB, so we create |
| 572 | * an entry with no data blocks in the reserved buffer in |
| 573 | * the kernel data segment. |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 574 | */ |
Milton Miller | bd2b64a | 2010-06-12 03:48:47 +0000 | [diff] [blame] | 575 | spin_lock(&rtas_data_buf_lock); |
| 576 | flist = (struct flash_block_list *)&rtas_data_buf[0]; |
| 577 | flist->num_blocks = 0; |
| 578 | flist->next = rtas_firmware_flash_list; |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 579 | rtas_block_list = virt_to_abs(flist); |
| 580 | if (rtas_block_list >= 4UL*1024*1024*1024) { |
| 581 | 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] | 582 | spin_unlock(&rtas_data_buf_lock); |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 583 | return; |
| 584 | } |
| 585 | |
| 586 | printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n"); |
| 587 | /* Update the block_list in place. */ |
Milton Miller | bd2b64a | 2010-06-12 03:48:47 +0000 | [diff] [blame] | 588 | rtas_firmware_flash_list = NULL; /* too hard to backout on error */ |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 589 | image_size = 0; |
| 590 | for (f = flist; f; f = next) { |
| 591 | /* Translate data addrs to absolute */ |
| 592 | for (i = 0; i < f->num_blocks; i++) { |
| 593 | f->blocks[i].data = (char *)virt_to_abs(f->blocks[i].data); |
| 594 | image_size += f->blocks[i].length; |
| 595 | } |
| 596 | next = f->next; |
| 597 | /* Don't translate NULL pointer for last entry */ |
| 598 | if (f->next) |
| 599 | f->next = (struct flash_block_list *)virt_to_abs(f->next); |
| 600 | else |
| 601 | f->next = NULL; |
| 602 | /* make num_blocks into the version/length field */ |
| 603 | f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16); |
| 604 | } |
| 605 | |
| 606 | printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size); |
| 607 | printk(KERN_ALERT "FLASH: performing flash and reboot\n"); |
| 608 | rtas_progress("Flashing \n", 0x0); |
| 609 | rtas_progress("Please Wait... ", 0x0); |
| 610 | printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n"); |
| 611 | status = rtas_call(update_token, 1, 1, NULL, rtas_block_list); |
| 612 | switch (status) { /* should only get "bad" status */ |
| 613 | case 0: |
| 614 | printk(KERN_ALERT "FLASH: success\n"); |
| 615 | break; |
| 616 | case -1: |
| 617 | printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n"); |
| 618 | break; |
| 619 | case -3: |
| 620 | printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n"); |
| 621 | break; |
| 622 | case -4: |
| 623 | printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n"); |
| 624 | break; |
| 625 | default: |
| 626 | printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status); |
| 627 | break; |
| 628 | } |
Milton Miller | bd2b64a | 2010-06-12 03:48:47 +0000 | [diff] [blame] | 629 | spin_unlock(&rtas_data_buf_lock); |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 630 | } |
| 631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | static void remove_flash_pde(struct proc_dir_entry *dp) |
| 633 | { |
| 634 | if (dp) { |
Jesper Juhl | 95eff20 | 2006-02-04 20:35:59 +0100 | [diff] [blame] | 635 | kfree(dp->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | remove_proc_entry(dp->name, dp->parent); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | static int initialize_flash_pde_data(const char *rtas_call_name, |
| 641 | size_t buf_size, |
| 642 | struct proc_dir_entry *dp) |
| 643 | { |
| 644 | int *status; |
| 645 | int token; |
| 646 | |
Yan Burman | f848535 | 2006-12-02 13:26:57 +0200 | [diff] [blame] | 647 | dp->data = kzalloc(buf_size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | if (dp->data == NULL) { |
| 649 | remove_flash_pde(dp); |
| 650 | return -ENOMEM; |
| 651 | } |
| 652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | /* |
| 654 | * This code assumes that the status int is the first member of the |
| 655 | * struct |
| 656 | */ |
| 657 | status = (int *) dp->data; |
| 658 | token = rtas_token(rtas_call_name); |
| 659 | if (token == RTAS_UNKNOWN_SERVICE) |
| 660 | *status = FLASH_AUTH; |
| 661 | else |
| 662 | *status = FLASH_NO_OP; |
| 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | static struct proc_dir_entry *create_flash_pde(const char *filename, |
Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 668 | const struct file_operations *fops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | { |
Denis V. Lunev | 6674713 | 2008-04-29 01:02:26 -0700 | [diff] [blame] | 670 | return proc_create(filename, S_IRUSR | S_IWUSR, NULL, fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 673 | static const struct file_operations rtas_flash_operations = { |
Denis V. Lunev | 6674713 | 2008-04-29 01:02:26 -0700 | [diff] [blame] | 674 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | .read = rtas_flash_read, |
| 676 | .write = rtas_flash_write, |
| 677 | .open = rtas_excl_open, |
| 678 | .release = rtas_flash_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 679 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | }; |
| 681 | |
Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 682 | static const struct file_operations manage_flash_operations = { |
Denis V. Lunev | 6674713 | 2008-04-29 01:02:26 -0700 | [diff] [blame] | 683 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | .read = manage_flash_read, |
| 685 | .write = manage_flash_write, |
| 686 | .open = rtas_excl_open, |
| 687 | .release = rtas_excl_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 688 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | }; |
| 690 | |
Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 691 | static const struct file_operations validate_flash_operations = { |
Denis V. Lunev | 6674713 | 2008-04-29 01:02:26 -0700 | [diff] [blame] | 692 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | .read = validate_flash_read, |
| 694 | .write = validate_flash_write, |
| 695 | .open = rtas_excl_open, |
| 696 | .release = validate_flash_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 697 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | }; |
| 699 | |
Michael Ellerman | 1c21a29 | 2008-05-08 14:27:19 +1000 | [diff] [blame] | 700 | static int __init rtas_flash_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | { |
| 702 | int rc; |
| 703 | |
| 704 | if (rtas_token("ibm,update-flash-64-and-reboot") == |
| 705 | RTAS_UNKNOWN_SERVICE) { |
| 706 | printk(KERN_ERR "rtas_flash: no firmware flash support\n"); |
| 707 | return 1; |
| 708 | } |
| 709 | |
Benjamin Herrenschmidt | 188917e | 2009-09-24 19:29:13 +0000 | [diff] [blame] | 710 | firmware_flash_pde = create_flash_pde("powerpc/rtas/" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | FIRMWARE_FLASH_NAME, |
| 712 | &rtas_flash_operations); |
| 713 | if (firmware_flash_pde == NULL) { |
| 714 | rc = -ENOMEM; |
| 715 | goto cleanup; |
| 716 | } |
| 717 | |
| 718 | rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot", |
| 719 | sizeof(struct rtas_update_flash_t), |
| 720 | firmware_flash_pde); |
| 721 | if (rc != 0) |
| 722 | goto cleanup; |
| 723 | |
Benjamin Herrenschmidt | 188917e | 2009-09-24 19:29:13 +0000 | [diff] [blame] | 724 | firmware_update_pde = create_flash_pde("powerpc/rtas/" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | FIRMWARE_UPDATE_NAME, |
| 726 | &rtas_flash_operations); |
| 727 | if (firmware_update_pde == NULL) { |
| 728 | rc = -ENOMEM; |
| 729 | goto cleanup; |
| 730 | } |
| 731 | |
| 732 | rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot", |
| 733 | sizeof(struct rtas_update_flash_t), |
| 734 | firmware_update_pde); |
| 735 | if (rc != 0) |
| 736 | goto cleanup; |
| 737 | |
Benjamin Herrenschmidt | 188917e | 2009-09-24 19:29:13 +0000 | [diff] [blame] | 738 | validate_pde = create_flash_pde("powerpc/rtas/" VALIDATE_FLASH_NAME, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | &validate_flash_operations); |
| 740 | if (validate_pde == NULL) { |
| 741 | rc = -ENOMEM; |
| 742 | goto cleanup; |
| 743 | } |
| 744 | |
| 745 | rc = initialize_flash_pde_data("ibm,validate-flash-image", |
| 746 | sizeof(struct rtas_validate_flash_t), |
| 747 | validate_pde); |
| 748 | if (rc != 0) |
| 749 | goto cleanup; |
| 750 | |
Benjamin Herrenschmidt | 188917e | 2009-09-24 19:29:13 +0000 | [diff] [blame] | 751 | manage_pde = create_flash_pde("powerpc/rtas/" MANAGE_FLASH_NAME, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | &manage_flash_operations); |
| 753 | if (manage_pde == NULL) { |
| 754 | rc = -ENOMEM; |
| 755 | goto cleanup; |
| 756 | } |
| 757 | |
| 758 | rc = initialize_flash_pde_data("ibm,manage-flash-image", |
| 759 | sizeof(struct rtas_manage_flash_t), |
| 760 | manage_pde); |
| 761 | if (rc != 0) |
| 762 | goto cleanup; |
| 763 | |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 764 | rtas_flash_term_hook = rtas_flash_firmware; |
John Rose | ae883ca | 2006-11-08 10:07:30 -0600 | [diff] [blame] | 765 | |
| 766 | flash_block_cache = kmem_cache_create("rtas_flash_cache", |
| 767 | RTAS_BLK_SIZE, RTAS_BLK_SIZE, 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 768 | rtas_block_ctor); |
John Rose | ae883ca | 2006-11-08 10:07:30 -0600 | [diff] [blame] | 769 | if (!flash_block_cache) { |
| 770 | printk(KERN_ERR "%s: failed to create block cache\n", |
Harvey Harrison | e48b1b4 | 2008-03-29 08:21:07 +1100 | [diff] [blame] | 771 | __func__); |
John Rose | ae883ca | 2006-11-08 10:07:30 -0600 | [diff] [blame] | 772 | rc = -ENOMEM; |
| 773 | goto cleanup; |
| 774 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | return 0; |
| 776 | |
| 777 | cleanup: |
| 778 | remove_flash_pde(firmware_flash_pde); |
| 779 | remove_flash_pde(firmware_update_pde); |
| 780 | remove_flash_pde(validate_pde); |
| 781 | remove_flash_pde(manage_pde); |
| 782 | |
| 783 | return rc; |
| 784 | } |
| 785 | |
Michael Ellerman | 1c21a29 | 2008-05-08 14:27:19 +1000 | [diff] [blame] | 786 | static void __exit rtas_flash_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | { |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 788 | rtas_flash_term_hook = NULL; |
John Rose | ae883ca | 2006-11-08 10:07:30 -0600 | [diff] [blame] | 789 | |
| 790 | if (flash_block_cache) |
| 791 | kmem_cache_destroy(flash_block_cache); |
| 792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | remove_flash_pde(firmware_flash_pde); |
| 794 | remove_flash_pde(firmware_update_pde); |
| 795 | remove_flash_pde(validate_pde); |
| 796 | remove_flash_pde(manage_pde); |
| 797 | } |
| 798 | |
| 799 | module_init(rtas_flash_init); |
| 800 | module_exit(rtas_flash_cleanup); |
| 801 | MODULE_LICENSE("GPL"); |