blob: 243e184cbe45aaa4fb3f8cd4fbbf9aec3ca8b3e3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Herrenschmidt188917e2009-09-24 19:29:13 +00009 * /proc/powerpc/rtas/firmware_flash interface
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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 Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/proc_fs.h>
Amerigo Wangc5f41752011-07-25 17:13:10 -070020#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/delay.h>
22#include <asm/uaccess.h>
23#include <asm/rtas.h>
24
25#define MODULE_VERS "1.0"
26#define MODULE_NAME "rtas_flash"
27
28#define FIRMWARE_FLASH_NAME "firmware_flash"
29#define FIRMWARE_UPDATE_NAME "firmware_update"
30#define MANAGE_FLASH_NAME "manage_flash"
31#define VALIDATE_FLASH_NAME "validate_flash"
32
33/* General RTAS Status Codes */
34#define RTAS_RC_SUCCESS 0
35#define RTAS_RC_HW_ERR -1
36#define RTAS_RC_BUSY -2
37
38/* Flash image status values */
39#define FLASH_AUTH -9002 /* RTAS Not Service Authority Partition */
40#define FLASH_NO_OP -1099 /* No operation initiated by user */
41#define FLASH_IMG_SHORT -1005 /* Flash image shorter than expected */
42#define FLASH_IMG_BAD_LEN -1004 /* Bad length value in flash list block */
43#define FLASH_IMG_NULL_DATA -1003 /* Bad data value in flash list block */
44#define FLASH_IMG_READY 0 /* Firmware img ready for flash on reboot */
45
46/* Manage image status values */
47#define MANAGE_AUTH -9002 /* RTAS Not Service Authority Partition */
48#define MANAGE_ACTIVE_ERR -9001 /* RTAS Cannot Overwrite Active Img */
49#define MANAGE_NO_OP -1099 /* No operation initiated by user */
50#define MANAGE_PARAM_ERR -3 /* RTAS Parameter Error */
51#define MANAGE_HW_ERR -1 /* RTAS Hardware Error */
52
53/* Validate image status values */
54#define VALIDATE_AUTH -9002 /* RTAS Not Service Authority Partition */
55#define VALIDATE_NO_OP -1099 /* No operation initiated by the user */
56#define VALIDATE_INCOMPLETE -1002 /* User copied < VALIDATE_BUF_SIZE */
57#define VALIDATE_READY -1001 /* Firmware image ready for validation */
58#define VALIDATE_PARAM_ERR -3 /* RTAS Parameter Error */
59#define VALIDATE_HW_ERR -1 /* RTAS Hardware Error */
Vasant Hegdef51005d2013-04-23 04:20:48 +000060
61/* ibm,validate-flash-image update result tokens */
62#define VALIDATE_TMP_UPDATE 0 /* T side will be updated */
63#define VALIDATE_FLASH_AUTH 1 /* Partition does not have authority */
64#define VALIDATE_INVALID_IMG 2 /* Candidate image is not valid */
65#define VALIDATE_CUR_UNKNOWN 3 /* Current fixpack level is unknown */
66/*
67 * Current T side will be committed to P side before being replace with new
68 * image, and the new image is downlevel from current image
69 */
70#define VALIDATE_TMP_COMMIT_DL 4
71/*
72 * Current T side will be committed to P side before being replaced with new
73 * image
74 */
75#define VALIDATE_TMP_COMMIT 5
76/*
77 * T side will be updated with a downlevel image
78 */
79#define VALIDATE_TMP_UPDATE_DL 6
Vasant Hegdefd2d37c2013-04-23 04:22:22 +000080/*
81 * The candidate image's release date is later than the system's firmware
82 * service entitlement date - service warranty period has expired
83 */
84#define VALIDATE_OUT_OF_WRNTY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/* ibm,manage-flash-image operation tokens */
87#define RTAS_REJECT_TMP_IMG 0
88#define RTAS_COMMIT_TMP_IMG 1
89
90/* Array sizes */
91#define VALIDATE_BUF_SIZE 4096
92#define RTAS_MSG_MAXLEN 64
93
John Roseae883ca2006-11-08 10:07:30 -060094/* Quirk - RTAS requires 4k list length and block size */
95#define RTAS_BLKLIST_LENGTH 4096
96#define RTAS_BLK_SIZE 4096
97
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +110098struct flash_block {
99 char *data;
100 unsigned long length;
101};
102
103/* This struct is very similar but not identical to
104 * that needed by the rtas flash update.
105 * All we need to do for rtas is rewrite num_blocks
106 * into a version/length and translate the pointers
107 * to absolute.
108 */
John Roseae883ca2006-11-08 10:07:30 -0600109#define FLASH_BLOCKS_PER_NODE ((RTAS_BLKLIST_LENGTH - 16) / sizeof(struct flash_block))
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100110struct flash_block_list {
111 unsigned long num_blocks;
112 struct flash_block_list *next;
113 struct flash_block blocks[FLASH_BLOCKS_PER_NODE];
114};
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100115
Milton Millerbd2b64a2010-06-12 03:48:47 +0000116static struct flash_block_list *rtas_firmware_flash_list;
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100117
John Roseae883ca2006-11-08 10:07:30 -0600118/* Use slab cache to guarantee 4k alignment */
Christoph Lametere18b8902006-12-06 20:33:20 -0800119static struct kmem_cache *flash_block_cache = NULL;
John Roseae883ca2006-11-08 10:07:30 -0600120
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100121#define FLASH_BLOCK_LIST_VERSION (1UL)
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/* Local copy of the flash block list.
124 * We only allow one open of the flash proc file and create this
Milton Millerbd2b64a2010-06-12 03:48:47 +0000125 * list as we go. The rtas_firmware_flash_list varable will be
126 * set once the data is fully read.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 *
128 * For convenience as we build the list we use virtual addrs,
129 * we do not fill in the version number, and the length field
130 * is treated as the number of entries currently in the block
Milton Millerbd2b64a2010-06-12 03:48:47 +0000131 * (i.e. not a byte count). This is all fixed when calling
132 * the flash routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
134
135/* Status int must be first member of struct */
136struct rtas_update_flash_t
137{
138 int status; /* Flash update status */
139 struct flash_block_list *flist; /* Local copy of flash block list */
140};
141
142/* Status int must be first member of struct */
143struct rtas_manage_flash_t
144{
145 int status; /* Returned status */
146 unsigned int op; /* Reject or commit image */
147};
148
149/* Status int must be first member of struct */
150struct rtas_validate_flash_t
151{
152 int status; /* Returned status */
153 char buf[VALIDATE_BUF_SIZE]; /* Candidate image buffer */
154 unsigned int buf_size; /* Size of image buf */
155 unsigned int update_results; /* Update results token */
156};
157
158static DEFINE_SPINLOCK(flash_file_open_lock);
159static struct proc_dir_entry *firmware_flash_pde;
160static struct proc_dir_entry *firmware_update_pde;
161static struct proc_dir_entry *validate_pde;
162static struct proc_dir_entry *manage_pde;
163
164/* Do simple sanity checks on the flash image. */
165static int flash_list_valid(struct flash_block_list *flist)
166{
167 struct flash_block_list *f;
168 int i;
169 unsigned long block_size, image_size;
170
171 /* Paranoid self test here. We also collect the image size. */
172 image_size = 0;
173 for (f = flist; f; f = f->next) {
174 for (i = 0; i < f->num_blocks; i++) {
175 if (f->blocks[i].data == NULL) {
176 return FLASH_IMG_NULL_DATA;
177 }
178 block_size = f->blocks[i].length;
John Roseae883ca2006-11-08 10:07:30 -0600179 if (block_size <= 0 || block_size > RTAS_BLK_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return FLASH_IMG_BAD_LEN;
181 }
182 image_size += block_size;
183 }
184 }
185
186 if (image_size < (256 << 10)) {
187 if (image_size < 2)
188 return FLASH_NO_OP;
189 }
190
191 printk(KERN_INFO "FLASH: flash image with %ld bytes stored for hardware flash on reboot\n", image_size);
192
193 return FLASH_IMG_READY;
194}
195
196static void free_flash_list(struct flash_block_list *f)
197{
198 struct flash_block_list *next;
199 int i;
200
201 while (f) {
202 for (i = 0; i < f->num_blocks; i++)
John Roseae883ca2006-11-08 10:07:30 -0600203 kmem_cache_free(flash_block_cache, f->blocks[i].data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 next = f->next;
John Roseae883ca2006-11-08 10:07:30 -0600205 kmem_cache_free(flash_block_cache, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 f = next;
207 }
208}
209
210static int rtas_flash_release(struct inode *inode, struct file *file)
211{
Al Viro496ad9a2013-01-23 17:07:38 -0500212 struct proc_dir_entry *dp = PDE(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 struct rtas_update_flash_t *uf;
214
215 uf = (struct rtas_update_flash_t *) dp->data;
216 if (uf->flist) {
217 /* File was opened in write mode for a new flash attempt */
218 /* Clear saved list */
Milton Millerbd2b64a2010-06-12 03:48:47 +0000219 if (rtas_firmware_flash_list) {
220 free_flash_list(rtas_firmware_flash_list);
221 rtas_firmware_flash_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223
224 if (uf->status != FLASH_AUTH)
225 uf->status = flash_list_valid(uf->flist);
226
227 if (uf->status == FLASH_IMG_READY)
Milton Millerbd2b64a2010-06-12 03:48:47 +0000228 rtas_firmware_flash_list = uf->flist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 else
230 free_flash_list(uf->flist);
231
232 uf->flist = NULL;
233 }
234
235 atomic_dec(&dp->count);
236 return 0;
237}
238
239static void get_flash_status_msg(int status, char *buf)
240{
241 char *msg;
242
243 switch (status) {
244 case FLASH_AUTH:
245 msg = "error: this partition does not have service authority\n";
246 break;
247 case FLASH_NO_OP:
248 msg = "info: no firmware image for flash\n";
249 break;
250 case FLASH_IMG_SHORT:
251 msg = "error: flash image short\n";
252 break;
253 case FLASH_IMG_BAD_LEN:
254 msg = "error: internal error bad length\n";
255 break;
256 case FLASH_IMG_NULL_DATA:
257 msg = "error: internal error null data\n";
258 break;
259 case FLASH_IMG_READY:
260 msg = "ready: firmware image ready for flash on reboot\n";
261 break;
262 default:
263 sprintf(buf, "error: unexpected status value %d\n", status);
264 return;
265 }
266
267 strcpy(buf, msg);
268}
269
270/* Reading the proc file will show status (not the firmware contents) */
Al Viroefa54572005-04-26 11:26:53 -0700271static ssize_t rtas_flash_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 size_t count, loff_t *ppos)
273{
Al Viro496ad9a2013-01-23 17:07:38 -0500274 struct proc_dir_entry *dp = PDE(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 struct rtas_update_flash_t *uf;
276 char msg[RTAS_MSG_MAXLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Akinobu Mita4c4a5cf2010-12-24 20:03:59 +0000278 uf = dp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 if (!strcmp(dp->name, FIRMWARE_FLASH_NAME)) {
281 get_flash_status_msg(uf->status, msg);
282 } else { /* FIRMWARE_UPDATE_NAME */
283 sprintf(msg, "%d\n", uf->status);
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Akinobu Mita4c4a5cf2010-12-24 20:03:59 +0000286 return simple_read_from_buffer(buf, count, ppos, msg, strlen(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/* We could be much more efficient here. But to keep this function
290 * simple we allocate a page to the block list no matter how small the
291 * count is. If the system is low on memory it will be just as well
292 * that we fail....
293 */
Al Viroefa54572005-04-26 11:26:53 -0700294static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 size_t count, loff_t *off)
296{
Al Viro496ad9a2013-01-23 17:07:38 -0500297 struct proc_dir_entry *dp = PDE(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct rtas_update_flash_t *uf;
299 char *p;
300 int next_free;
301 struct flash_block_list *fl;
302
303 uf = (struct rtas_update_flash_t *) dp->data;
304
305 if (uf->status == FLASH_AUTH || count == 0)
306 return count; /* discard data */
307
308 /* In the case that the image is not ready for flashing, the memory
309 * allocated for the block list will be freed upon the release of the
310 * proc file
311 */
312 if (uf->flist == NULL) {
Vasant Hegdefb4696c2013-04-28 18:43:56 +0000313 uf->flist = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (!uf->flist)
315 return -ENOMEM;
316 }
317
318 fl = uf->flist;
319 while (fl->next)
320 fl = fl->next; /* seek to last block_list for append */
321 next_free = fl->num_blocks;
322 if (next_free == FLASH_BLOCKS_PER_NODE) {
323 /* Need to allocate another block_list */
Vasant Hegdefb4696c2013-04-28 18:43:56 +0000324 fl->next = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (!fl->next)
326 return -ENOMEM;
327 fl = fl->next;
328 next_free = 0;
329 }
330
John Roseae883ca2006-11-08 10:07:30 -0600331 if (count > RTAS_BLK_SIZE)
332 count = RTAS_BLK_SIZE;
Vasant Hegdefb4696c2013-04-28 18:43:56 +0000333 p = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (!p)
335 return -ENOMEM;
336
337 if(copy_from_user(p, buffer, count)) {
John Roseae883ca2006-11-08 10:07:30 -0600338 kmem_cache_free(flash_block_cache, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return -EFAULT;
340 }
341 fl->blocks[next_free].data = p;
342 fl->blocks[next_free].length = count;
343 fl->num_blocks++;
344
345 return count;
346}
347
348static int rtas_excl_open(struct inode *inode, struct file *file)
349{
350 struct proc_dir_entry *dp = PDE(inode);
351
352 /* Enforce exclusive open with use count of PDE */
353 spin_lock(&flash_file_open_lock);
Maxim Shchetynin74848392008-04-02 00:12:20 +1100354 if (atomic_read(&dp->count) > 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 spin_unlock(&flash_file_open_lock);
356 return -EBUSY;
357 }
358
359 atomic_inc(&dp->count);
360 spin_unlock(&flash_file_open_lock);
361
362 return 0;
363}
364
365static int rtas_excl_release(struct inode *inode, struct file *file)
366{
367 struct proc_dir_entry *dp = PDE(inode);
368
369 atomic_dec(&dp->count);
370
371 return 0;
372}
373
374static void manage_flash(struct rtas_manage_flash_t *args_buf)
375{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 s32 rc;
377
John Rose507279d2006-06-05 16:31:48 -0500378 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1,
380 1, NULL, args_buf->op);
John Rose507279d2006-06-05 16:31:48 -0500381 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 args_buf->status = rc;
384}
385
Al Viroefa54572005-04-26 11:26:53 -0700386static ssize_t manage_flash_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 size_t count, loff_t *ppos)
388{
Al Viro496ad9a2013-01-23 17:07:38 -0500389 struct proc_dir_entry *dp = PDE(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct rtas_manage_flash_t *args_buf;
391 char msg[RTAS_MSG_MAXLEN];
392 int msglen;
393
Akinobu Mita4c4a5cf2010-12-24 20:03:59 +0000394 args_buf = dp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (args_buf == NULL)
396 return 0;
397
398 msglen = sprintf(msg, "%d\n", args_buf->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Akinobu Mita4c4a5cf2010-12-24 20:03:59 +0000400 return simple_read_from_buffer(buf, count, ppos, msg, msglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Al Viroefa54572005-04-26 11:26:53 -0700403static ssize_t manage_flash_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 size_t count, loff_t *off)
405{
Al Viro496ad9a2013-01-23 17:07:38 -0500406 struct proc_dir_entry *dp = PDE(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 struct rtas_manage_flash_t *args_buf;
408 const char reject_str[] = "0";
409 const char commit_str[] = "1";
410 char stkbuf[10];
411 int op;
412
413 args_buf = (struct rtas_manage_flash_t *) dp->data;
414 if ((args_buf->status == MANAGE_AUTH) || (count == 0))
415 return count;
416
417 op = -1;
418 if (buf) {
419 if (count > 9) count = 9;
420 if (copy_from_user (stkbuf, buf, count)) {
421 return -EFAULT;
422 }
423 if (strncmp(stkbuf, reject_str, strlen(reject_str)) == 0)
424 op = RTAS_REJECT_TMP_IMG;
425 else if (strncmp(stkbuf, commit_str, strlen(commit_str)) == 0)
426 op = RTAS_COMMIT_TMP_IMG;
427 }
428
429 if (op == -1) /* buf is empty, or contains invalid string */
430 return -EINVAL;
431
432 args_buf->op = op;
433 manage_flash(args_buf);
434
435 return count;
436}
437
438static void validate_flash(struct rtas_validate_flash_t *args_buf)
439{
440 int token = rtas_token("ibm,validate-flash-image");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 int update_results;
442 s32 rc;
443
444 rc = 0;
John Rose507279d2006-06-05 16:31:48 -0500445 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 spin_lock(&rtas_data_buf_lock);
447 memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE);
448 rc = rtas_call(token, 2, 2, &update_results,
449 (u32) __pa(rtas_data_buf), args_buf->buf_size);
450 memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE);
451 spin_unlock(&rtas_data_buf_lock);
John Rose507279d2006-06-05 16:31:48 -0500452 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 args_buf->status = rc;
455 args_buf->update_results = update_results;
456}
457
458static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf,
459 char *msg)
460{
461 int n;
462
463 if (args_buf->status >= VALIDATE_TMP_UPDATE) {
464 n = sprintf(msg, "%d\n", args_buf->update_results);
465 if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
466 (args_buf->update_results == VALIDATE_TMP_UPDATE))
467 n += sprintf(msg + n, "%s\n", args_buf->buf);
468 } else {
469 n = sprintf(msg, "%d\n", args_buf->status);
470 }
471 return n;
472}
473
Al Viroefa54572005-04-26 11:26:53 -0700474static ssize_t validate_flash_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 size_t count, loff_t *ppos)
476{
Al Viro496ad9a2013-01-23 17:07:38 -0500477 struct proc_dir_entry *dp = PDE(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct rtas_validate_flash_t *args_buf;
479 char msg[RTAS_MSG_MAXLEN];
480 int msglen;
481
Akinobu Mita4c4a5cf2010-12-24 20:03:59 +0000482 args_buf = dp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 msglen = get_validate_flash_msg(args_buf, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Akinobu Mita4c4a5cf2010-12-24 20:03:59 +0000486 return simple_read_from_buffer(buf, count, ppos, msg, msglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Al Viroefa54572005-04-26 11:26:53 -0700489static ssize_t validate_flash_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 size_t count, loff_t *off)
491{
Al Viro496ad9a2013-01-23 17:07:38 -0500492 struct proc_dir_entry *dp = PDE(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct rtas_validate_flash_t *args_buf;
494 int rc;
495
496 args_buf = (struct rtas_validate_flash_t *) dp->data;
497
498 if (dp->data == NULL) {
499 dp->data = kmalloc(sizeof(struct rtas_validate_flash_t),
500 GFP_KERNEL);
501 if (dp->data == NULL)
502 return -ENOMEM;
503 }
504
505 /* We are only interested in the first 4K of the
506 * candidate image */
507 if ((*off >= VALIDATE_BUF_SIZE) ||
508 (args_buf->status == VALIDATE_AUTH)) {
509 *off += count;
510 return count;
511 }
512
513 if (*off + count >= VALIDATE_BUF_SIZE) {
514 count = VALIDATE_BUF_SIZE - *off;
515 args_buf->status = VALIDATE_READY;
516 } else {
517 args_buf->status = VALIDATE_INCOMPLETE;
518 }
519
520 if (!access_ok(VERIFY_READ, buf, count)) {
521 rc = -EFAULT;
522 goto done;
523 }
524 if (copy_from_user(args_buf->buf + *off, buf, count)) {
525 rc = -EFAULT;
526 goto done;
527 }
528
529 *off += count;
530 rc = count;
531done:
532 if (rc < 0) {
533 kfree(dp->data);
534 dp->data = NULL;
535 }
536 return rc;
537}
538
539static int validate_flash_release(struct inode *inode, struct file *file)
540{
Al Viro496ad9a2013-01-23 17:07:38 -0500541 struct proc_dir_entry *dp = PDE(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct rtas_validate_flash_t *args_buf;
543
544 args_buf = (struct rtas_validate_flash_t *) dp->data;
545
546 if (args_buf->status == VALIDATE_READY) {
547 args_buf->buf_size = VALIDATE_BUF_SIZE;
548 validate_flash(args_buf);
549 }
550
551 /* The matching atomic_inc was in rtas_excl_open() */
552 atomic_dec(&dp->count);
553
554 return 0;
555}
556
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100557static 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 Millerbd2b64a2010-06-12 03:48:47 +0000564 if (rtas_firmware_flash_list == NULL)
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100565 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 Millerbd2b64a2010-06-12 03:48:47 +0000581 /*
Ravi K. Nittaladf17f562011-10-03 21:49:53 +0000582 * 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 Millerbd2b64a2010-06-12 03:48:47 +0000588 * 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 Mackerrasf4fcbbe2005-11-03 14:41:19 +1100591 */
Milton Millerbd2b64a2010-06-12 03:48:47 +0000592 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 Ellerman3d267522012-07-25 21:19:56 +0000596 rtas_block_list = __pa(flist);
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100597 if (rtas_block_list >= 4UL*1024*1024*1024) {
598 printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
Milton Millerbd2b64a2010-06-12 03:48:47 +0000599 spin_unlock(&rtas_data_buf_lock);
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100600 return;
601 }
602
603 printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
604 /* Update the block_list in place. */
Milton Millerbd2b64a2010-06-12 03:48:47 +0000605 rtas_firmware_flash_list = NULL; /* too hard to backout on error */
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100606 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++) {
Michael Ellerman3d267522012-07-25 21:19:56 +0000610 f->blocks[i].data = (char *)__pa(f->blocks[i].data);
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100611 image_size += f->blocks[i].length;
612 }
613 next = f->next;
614 /* Don't translate NULL pointer for last entry */
615 if (f->next)
Michael Ellerman3d267522012-07-25 21:19:56 +0000616 f->next = (struct flash_block_list *)__pa(f->next);
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100617 else
618 f->next = NULL;
619 /* make num_blocks into the version/length field */
620 f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
621 }
622
623 printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
624 printk(KERN_ALERT "FLASH: performing flash and reboot\n");
625 rtas_progress("Flashing \n", 0x0);
626 rtas_progress("Please Wait... ", 0x0);
627 printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n");
628 status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
629 switch (status) { /* should only get "bad" status */
630 case 0:
631 printk(KERN_ALERT "FLASH: success\n");
632 break;
633 case -1:
634 printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n");
635 break;
636 case -3:
637 printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
638 break;
639 case -4:
640 printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n");
641 break;
642 default:
643 printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
644 break;
645 }
Milton Millerbd2b64a2010-06-12 03:48:47 +0000646 spin_unlock(&rtas_data_buf_lock);
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100647}
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649static void remove_flash_pde(struct proc_dir_entry *dp)
650{
651 if (dp) {
Jesper Juhl95eff202006-02-04 20:35:59 +0100652 kfree(dp->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 remove_proc_entry(dp->name, dp->parent);
654 }
655}
656
657static int initialize_flash_pde_data(const char *rtas_call_name,
658 size_t buf_size,
659 struct proc_dir_entry *dp)
660{
661 int *status;
662 int token;
663
Yan Burmanf8485352006-12-02 13:26:57 +0200664 dp->data = kzalloc(buf_size, GFP_KERNEL);
Julia Lawallbc269572012-10-21 00:52:05 +0000665 if (dp->data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /*
669 * This code assumes that the status int is the first member of the
670 * struct
671 */
672 status = (int *) dp->data;
673 token = rtas_token(rtas_call_name);
674 if (token == RTAS_UNKNOWN_SERVICE)
675 *status = FLASH_AUTH;
676 else
677 *status = FLASH_NO_OP;
678
679 return 0;
680}
681
682static struct proc_dir_entry *create_flash_pde(const char *filename,
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800683 const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Denis V. Lunev66747132008-04-29 01:02:26 -0700685 return proc_create(filename, S_IRUSR | S_IWUSR, NULL, fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800688static const struct file_operations rtas_flash_operations = {
Denis V. Lunev66747132008-04-29 01:02:26 -0700689 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 .read = rtas_flash_read,
691 .write = rtas_flash_write,
692 .open = rtas_excl_open,
693 .release = rtas_flash_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200694 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695};
696
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800697static const struct file_operations manage_flash_operations = {
Denis V. Lunev66747132008-04-29 01:02:26 -0700698 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 .read = manage_flash_read,
700 .write = manage_flash_write,
701 .open = rtas_excl_open,
702 .release = rtas_excl_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200703 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704};
705
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800706static const struct file_operations validate_flash_operations = {
Denis V. Lunev66747132008-04-29 01:02:26 -0700707 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 .read = validate_flash_read,
709 .write = validate_flash_write,
710 .open = rtas_excl_open,
711 .release = validate_flash_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200712 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713};
714
Michael Ellerman1c21a292008-05-08 14:27:19 +1000715static int __init rtas_flash_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 int rc;
718
719 if (rtas_token("ibm,update-flash-64-and-reboot") ==
720 RTAS_UNKNOWN_SERVICE) {
Anton Blanchard0e384982012-07-22 20:42:32 +0000721 pr_info("rtas_flash: no firmware flash support\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return 1;
723 }
724
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000725 firmware_flash_pde = create_flash_pde("powerpc/rtas/"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 FIRMWARE_FLASH_NAME,
727 &rtas_flash_operations);
728 if (firmware_flash_pde == NULL) {
729 rc = -ENOMEM;
730 goto cleanup;
731 }
732
733 rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
734 sizeof(struct rtas_update_flash_t),
735 firmware_flash_pde);
736 if (rc != 0)
737 goto cleanup;
738
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000739 firmware_update_pde = create_flash_pde("powerpc/rtas/"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 FIRMWARE_UPDATE_NAME,
741 &rtas_flash_operations);
742 if (firmware_update_pde == NULL) {
743 rc = -ENOMEM;
744 goto cleanup;
745 }
746
747 rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
748 sizeof(struct rtas_update_flash_t),
749 firmware_update_pde);
750 if (rc != 0)
751 goto cleanup;
752
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000753 validate_pde = create_flash_pde("powerpc/rtas/" VALIDATE_FLASH_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 &validate_flash_operations);
755 if (validate_pde == NULL) {
756 rc = -ENOMEM;
757 goto cleanup;
758 }
759
760 rc = initialize_flash_pde_data("ibm,validate-flash-image",
761 sizeof(struct rtas_validate_flash_t),
762 validate_pde);
763 if (rc != 0)
764 goto cleanup;
765
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000766 manage_pde = create_flash_pde("powerpc/rtas/" MANAGE_FLASH_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 &manage_flash_operations);
768 if (manage_pde == NULL) {
769 rc = -ENOMEM;
770 goto cleanup;
771 }
772
773 rc = initialize_flash_pde_data("ibm,manage-flash-image",
774 sizeof(struct rtas_manage_flash_t),
775 manage_pde);
776 if (rc != 0)
777 goto cleanup;
778
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100779 rtas_flash_term_hook = rtas_flash_firmware;
John Roseae883ca2006-11-08 10:07:30 -0600780
781 flash_block_cache = kmem_cache_create("rtas_flash_cache",
782 RTAS_BLK_SIZE, RTAS_BLK_SIZE, 0,
Vasant Hegdefb4696c2013-04-28 18:43:56 +0000783 NULL);
John Roseae883ca2006-11-08 10:07:30 -0600784 if (!flash_block_cache) {
785 printk(KERN_ERR "%s: failed to create block cache\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100786 __func__);
John Roseae883ca2006-11-08 10:07:30 -0600787 rc = -ENOMEM;
788 goto cleanup;
789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return 0;
791
792cleanup:
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 return rc;
799}
800
Michael Ellerman1c21a292008-05-08 14:27:19 +1000801static void __exit rtas_flash_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100803 rtas_flash_term_hook = NULL;
John Roseae883ca2006-11-08 10:07:30 -0600804
Vasant Hegdead18a362013-02-08 01:18:36 +0000805 if (rtas_firmware_flash_list) {
806 free_flash_list(rtas_firmware_flash_list);
807 rtas_firmware_flash_list = NULL;
808 }
809
John Roseae883ca2006-11-08 10:07:30 -0600810 if (flash_block_cache)
811 kmem_cache_destroy(flash_block_cache);
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 remove_flash_pde(firmware_flash_pde);
814 remove_flash_pde(firmware_update_pde);
815 remove_flash_pde(validate_pde);
816 remove_flash_pde(manage_pde);
817}
818
819module_init(rtas_flash_init);
820module_exit(rtas_flash_cleanup);
821MODULE_LICENSE("GPL");