blob: c642f01329887bca409d8da81d58f1129c6df6cd [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 */
60#define VALIDATE_TMP_UPDATE 0 /* Validate Return Status */
61#define VALIDATE_FLASH_AUTH 1 /* Validate Return Status */
62#define VALIDATE_INVALID_IMG 2 /* Validate Return Status */
63#define VALIDATE_CUR_UNKNOWN 3 /* Validate Return Status */
64#define VALIDATE_TMP_COMMIT_DL 4 /* Validate Return Status */
65#define VALIDATE_TMP_COMMIT 5 /* Validate Return Status */
66#define VALIDATE_TMP_UPDATE_DL 6 /* Validate Return Status */
67
68/* ibm,manage-flash-image operation tokens */
69#define RTAS_REJECT_TMP_IMG 0
70#define RTAS_COMMIT_TMP_IMG 1
71
72/* Array sizes */
73#define VALIDATE_BUF_SIZE 4096
74#define RTAS_MSG_MAXLEN 64
75
John Roseae883ca2006-11-08 10:07:30 -060076/* Quirk - RTAS requires 4k list length and block size */
77#define RTAS_BLKLIST_LENGTH 4096
78#define RTAS_BLK_SIZE 4096
79
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +110080struct flash_block {
81 char *data;
82 unsigned long length;
83};
84
85/* This struct is very similar but not identical to
86 * that needed by the rtas flash update.
87 * All we need to do for rtas is rewrite num_blocks
88 * into a version/length and translate the pointers
89 * to absolute.
90 */
John Roseae883ca2006-11-08 10:07:30 -060091#define FLASH_BLOCKS_PER_NODE ((RTAS_BLKLIST_LENGTH - 16) / sizeof(struct flash_block))
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +110092struct flash_block_list {
93 unsigned long num_blocks;
94 struct flash_block_list *next;
95 struct flash_block blocks[FLASH_BLOCKS_PER_NODE];
96};
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +110097
Milton Millerbd2b64a2010-06-12 03:48:47 +000098static struct flash_block_list *rtas_firmware_flash_list;
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +110099
John Roseae883ca2006-11-08 10:07:30 -0600100/* Use slab cache to guarantee 4k alignment */
Christoph Lametere18b8902006-12-06 20:33:20 -0800101static struct kmem_cache *flash_block_cache = NULL;
John Roseae883ca2006-11-08 10:07:30 -0600102
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100103#define FLASH_BLOCK_LIST_VERSION (1UL)
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/* Local copy of the flash block list.
106 * We only allow one open of the flash proc file and create this
Milton Millerbd2b64a2010-06-12 03:48:47 +0000107 * list as we go. The rtas_firmware_flash_list varable will be
108 * set once the data is fully read.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 *
110 * For convenience as we build the list we use virtual addrs,
111 * we do not fill in the version number, and the length field
112 * is treated as the number of entries currently in the block
Milton Millerbd2b64a2010-06-12 03:48:47 +0000113 * (i.e. not a byte count). This is all fixed when calling
114 * the flash routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
116
117/* Status int must be first member of struct */
118struct rtas_update_flash_t
119{
120 int status; /* Flash update status */
121 struct flash_block_list *flist; /* Local copy of flash block list */
122};
123
124/* Status int must be first member of struct */
125struct rtas_manage_flash_t
126{
127 int status; /* Returned status */
128 unsigned int op; /* Reject or commit image */
129};
130
131/* Status int must be first member of struct */
132struct rtas_validate_flash_t
133{
134 int status; /* Returned status */
135 char buf[VALIDATE_BUF_SIZE]; /* Candidate image buffer */
136 unsigned int buf_size; /* Size of image buf */
137 unsigned int update_results; /* Update results token */
138};
139
140static DEFINE_SPINLOCK(flash_file_open_lock);
141static struct proc_dir_entry *firmware_flash_pde;
142static struct proc_dir_entry *firmware_update_pde;
143static struct proc_dir_entry *validate_pde;
144static struct proc_dir_entry *manage_pde;
145
146/* Do simple sanity checks on the flash image. */
147static int flash_list_valid(struct flash_block_list *flist)
148{
149 struct flash_block_list *f;
150 int i;
151 unsigned long block_size, image_size;
152
153 /* Paranoid self test here. We also collect the image size. */
154 image_size = 0;
155 for (f = flist; f; f = f->next) {
156 for (i = 0; i < f->num_blocks; i++) {
157 if (f->blocks[i].data == NULL) {
158 return FLASH_IMG_NULL_DATA;
159 }
160 block_size = f->blocks[i].length;
John Roseae883ca2006-11-08 10:07:30 -0600161 if (block_size <= 0 || block_size > RTAS_BLK_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return FLASH_IMG_BAD_LEN;
163 }
164 image_size += block_size;
165 }
166 }
167
168 if (image_size < (256 << 10)) {
169 if (image_size < 2)
170 return FLASH_NO_OP;
171 }
172
173 printk(KERN_INFO "FLASH: flash image with %ld bytes stored for hardware flash on reboot\n", image_size);
174
175 return FLASH_IMG_READY;
176}
177
178static void free_flash_list(struct flash_block_list *f)
179{
180 struct flash_block_list *next;
181 int i;
182
183 while (f) {
184 for (i = 0; i < f->num_blocks; i++)
John Roseae883ca2006-11-08 10:07:30 -0600185 kmem_cache_free(flash_block_cache, f->blocks[i].data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 next = f->next;
John Roseae883ca2006-11-08 10:07:30 -0600187 kmem_cache_free(flash_block_cache, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 f = next;
189 }
190}
191
192static int rtas_flash_release(struct inode *inode, struct file *file)
193{
Al Viro496ad9a2013-01-23 17:07:38 -0500194 struct proc_dir_entry *dp = PDE(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 struct rtas_update_flash_t *uf;
196
197 uf = (struct rtas_update_flash_t *) dp->data;
198 if (uf->flist) {
199 /* File was opened in write mode for a new flash attempt */
200 /* Clear saved list */
Milton Millerbd2b64a2010-06-12 03:48:47 +0000201 if (rtas_firmware_flash_list) {
202 free_flash_list(rtas_firmware_flash_list);
203 rtas_firmware_flash_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
206 if (uf->status != FLASH_AUTH)
207 uf->status = flash_list_valid(uf->flist);
208
209 if (uf->status == FLASH_IMG_READY)
Milton Millerbd2b64a2010-06-12 03:48:47 +0000210 rtas_firmware_flash_list = uf->flist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 else
212 free_flash_list(uf->flist);
213
214 uf->flist = NULL;
215 }
216
217 atomic_dec(&dp->count);
218 return 0;
219}
220
221static void get_flash_status_msg(int status, char *buf)
222{
223 char *msg;
224
225 switch (status) {
226 case FLASH_AUTH:
227 msg = "error: this partition does not have service authority\n";
228 break;
229 case FLASH_NO_OP:
230 msg = "info: no firmware image for flash\n";
231 break;
232 case FLASH_IMG_SHORT:
233 msg = "error: flash image short\n";
234 break;
235 case FLASH_IMG_BAD_LEN:
236 msg = "error: internal error bad length\n";
237 break;
238 case FLASH_IMG_NULL_DATA:
239 msg = "error: internal error null data\n";
240 break;
241 case FLASH_IMG_READY:
242 msg = "ready: firmware image ready for flash on reboot\n";
243 break;
244 default:
245 sprintf(buf, "error: unexpected status value %d\n", status);
246 return;
247 }
248
249 strcpy(buf, msg);
250}
251
252/* Reading the proc file will show status (not the firmware contents) */
Al Viroefa54572005-04-26 11:26:53 -0700253static ssize_t rtas_flash_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 size_t count, loff_t *ppos)
255{
Al Viro496ad9a2013-01-23 17:07:38 -0500256 struct proc_dir_entry *dp = PDE(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 struct rtas_update_flash_t *uf;
258 char msg[RTAS_MSG_MAXLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Akinobu Mita4c4a5cf2010-12-24 20:03:59 +0000260 uf = dp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (!strcmp(dp->name, FIRMWARE_FLASH_NAME)) {
263 get_flash_status_msg(uf->status, msg);
264 } else { /* FIRMWARE_UPDATE_NAME */
265 sprintf(msg, "%d\n", uf->status);
266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Akinobu Mita4c4a5cf2010-12-24 20:03:59 +0000268 return simple_read_from_buffer(buf, count, ppos, msg, strlen(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
John Roseae883ca2006-11-08 10:07:30 -0600271/* constructor for flash_block_cache */
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700272void rtas_block_ctor(void *ptr)
John Roseae883ca2006-11-08 10:07:30 -0600273{
274 memset(ptr, 0, RTAS_BLK_SIZE);
275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/* We could be much more efficient here. But to keep this function
278 * simple we allocate a page to the block list no matter how small the
279 * count is. If the system is low on memory it will be just as well
280 * that we fail....
281 */
Al Viroefa54572005-04-26 11:26:53 -0700282static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 size_t count, loff_t *off)
284{
Al Viro496ad9a2013-01-23 17:07:38 -0500285 struct proc_dir_entry *dp = PDE(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 struct rtas_update_flash_t *uf;
287 char *p;
288 int next_free;
289 struct flash_block_list *fl;
290
291 uf = (struct rtas_update_flash_t *) dp->data;
292
293 if (uf->status == FLASH_AUTH || count == 0)
294 return count; /* discard data */
295
296 /* In the case that the image is not ready for flashing, the memory
297 * allocated for the block list will be freed upon the release of the
298 * proc file
299 */
300 if (uf->flist == NULL) {
John Roseae883ca2006-11-08 10:07:30 -0600301 uf->flist = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (!uf->flist)
303 return -ENOMEM;
304 }
305
306 fl = uf->flist;
307 while (fl->next)
308 fl = fl->next; /* seek to last block_list for append */
309 next_free = fl->num_blocks;
310 if (next_free == FLASH_BLOCKS_PER_NODE) {
311 /* Need to allocate another block_list */
John Roseae883ca2006-11-08 10:07:30 -0600312 fl->next = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (!fl->next)
314 return -ENOMEM;
315 fl = fl->next;
316 next_free = 0;
317 }
318
John Roseae883ca2006-11-08 10:07:30 -0600319 if (count > RTAS_BLK_SIZE)
320 count = RTAS_BLK_SIZE;
321 p = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (!p)
323 return -ENOMEM;
324
325 if(copy_from_user(p, buffer, count)) {
John Roseae883ca2006-11-08 10:07:30 -0600326 kmem_cache_free(flash_block_cache, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return -EFAULT;
328 }
329 fl->blocks[next_free].data = p;
330 fl->blocks[next_free].length = count;
331 fl->num_blocks++;
332
333 return count;
334}
335
336static int rtas_excl_open(struct inode *inode, struct file *file)
337{
338 struct proc_dir_entry *dp = PDE(inode);
339
340 /* Enforce exclusive open with use count of PDE */
341 spin_lock(&flash_file_open_lock);
Maxim Shchetynin74848392008-04-02 00:12:20 +1100342 if (atomic_read(&dp->count) > 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 spin_unlock(&flash_file_open_lock);
344 return -EBUSY;
345 }
346
347 atomic_inc(&dp->count);
348 spin_unlock(&flash_file_open_lock);
349
350 return 0;
351}
352
353static int rtas_excl_release(struct inode *inode, struct file *file)
354{
355 struct proc_dir_entry *dp = PDE(inode);
356
357 atomic_dec(&dp->count);
358
359 return 0;
360}
361
362static void manage_flash(struct rtas_manage_flash_t *args_buf)
363{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 s32 rc;
365
John Rose507279d2006-06-05 16:31:48 -0500366 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1,
368 1, NULL, args_buf->op);
John Rose507279d2006-06-05 16:31:48 -0500369 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 args_buf->status = rc;
372}
373
Al Viroefa54572005-04-26 11:26:53 -0700374static ssize_t manage_flash_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 size_t count, loff_t *ppos)
376{
Al Viro496ad9a2013-01-23 17:07:38 -0500377 struct proc_dir_entry *dp = PDE(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 struct rtas_manage_flash_t *args_buf;
379 char msg[RTAS_MSG_MAXLEN];
380 int msglen;
381
Akinobu Mita4c4a5cf2010-12-24 20:03:59 +0000382 args_buf = dp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (args_buf == NULL)
384 return 0;
385
386 msglen = sprintf(msg, "%d\n", args_buf->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Akinobu Mita4c4a5cf2010-12-24 20:03:59 +0000388 return simple_read_from_buffer(buf, count, ppos, msg, msglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
Al Viroefa54572005-04-26 11:26:53 -0700391static ssize_t manage_flash_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 size_t count, loff_t *off)
393{
Al Viro496ad9a2013-01-23 17:07:38 -0500394 struct proc_dir_entry *dp = PDE(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct rtas_manage_flash_t *args_buf;
396 const char reject_str[] = "0";
397 const char commit_str[] = "1";
398 char stkbuf[10];
399 int op;
400
401 args_buf = (struct rtas_manage_flash_t *) dp->data;
402 if ((args_buf->status == MANAGE_AUTH) || (count == 0))
403 return count;
404
405 op = -1;
406 if (buf) {
407 if (count > 9) count = 9;
408 if (copy_from_user (stkbuf, buf, count)) {
409 return -EFAULT;
410 }
411 if (strncmp(stkbuf, reject_str, strlen(reject_str)) == 0)
412 op = RTAS_REJECT_TMP_IMG;
413 else if (strncmp(stkbuf, commit_str, strlen(commit_str)) == 0)
414 op = RTAS_COMMIT_TMP_IMG;
415 }
416
417 if (op == -1) /* buf is empty, or contains invalid string */
418 return -EINVAL;
419
420 args_buf->op = op;
421 manage_flash(args_buf);
422
423 return count;
424}
425
426static void validate_flash(struct rtas_validate_flash_t *args_buf)
427{
428 int token = rtas_token("ibm,validate-flash-image");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 int update_results;
430 s32 rc;
431
432 rc = 0;
John Rose507279d2006-06-05 16:31:48 -0500433 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 spin_lock(&rtas_data_buf_lock);
435 memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE);
436 rc = rtas_call(token, 2, 2, &update_results,
437 (u32) __pa(rtas_data_buf), args_buf->buf_size);
438 memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE);
439 spin_unlock(&rtas_data_buf_lock);
John Rose507279d2006-06-05 16:31:48 -0500440 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 args_buf->status = rc;
443 args_buf->update_results = update_results;
444}
445
446static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf,
447 char *msg)
448{
449 int n;
450
451 if (args_buf->status >= VALIDATE_TMP_UPDATE) {
452 n = sprintf(msg, "%d\n", args_buf->update_results);
453 if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
454 (args_buf->update_results == VALIDATE_TMP_UPDATE))
455 n += sprintf(msg + n, "%s\n", args_buf->buf);
456 } else {
457 n = sprintf(msg, "%d\n", args_buf->status);
458 }
459 return n;
460}
461
Al Viroefa54572005-04-26 11:26:53 -0700462static ssize_t validate_flash_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 size_t count, loff_t *ppos)
464{
Al Viro496ad9a2013-01-23 17:07:38 -0500465 struct proc_dir_entry *dp = PDE(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 struct rtas_validate_flash_t *args_buf;
467 char msg[RTAS_MSG_MAXLEN];
468 int msglen;
469
Akinobu Mita4c4a5cf2010-12-24 20:03:59 +0000470 args_buf = dp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 msglen = get_validate_flash_msg(args_buf, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Akinobu Mita4c4a5cf2010-12-24 20:03:59 +0000474 return simple_read_from_buffer(buf, count, ppos, msg, msglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Al Viroefa54572005-04-26 11:26:53 -0700477static ssize_t validate_flash_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 size_t count, loff_t *off)
479{
Al Viro496ad9a2013-01-23 17:07:38 -0500480 struct proc_dir_entry *dp = PDE(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct rtas_validate_flash_t *args_buf;
482 int rc;
483
484 args_buf = (struct rtas_validate_flash_t *) dp->data;
485
486 if (dp->data == NULL) {
487 dp->data = kmalloc(sizeof(struct rtas_validate_flash_t),
488 GFP_KERNEL);
489 if (dp->data == NULL)
490 return -ENOMEM;
491 }
492
493 /* We are only interested in the first 4K of the
494 * candidate image */
495 if ((*off >= VALIDATE_BUF_SIZE) ||
496 (args_buf->status == VALIDATE_AUTH)) {
497 *off += count;
498 return count;
499 }
500
501 if (*off + count >= VALIDATE_BUF_SIZE) {
502 count = VALIDATE_BUF_SIZE - *off;
503 args_buf->status = VALIDATE_READY;
504 } else {
505 args_buf->status = VALIDATE_INCOMPLETE;
506 }
507
508 if (!access_ok(VERIFY_READ, buf, count)) {
509 rc = -EFAULT;
510 goto done;
511 }
512 if (copy_from_user(args_buf->buf + *off, buf, count)) {
513 rc = -EFAULT;
514 goto done;
515 }
516
517 *off += count;
518 rc = count;
519done:
520 if (rc < 0) {
521 kfree(dp->data);
522 dp->data = NULL;
523 }
524 return rc;
525}
526
527static int validate_flash_release(struct inode *inode, struct file *file)
528{
Al Viro496ad9a2013-01-23 17:07:38 -0500529 struct proc_dir_entry *dp = PDE(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct rtas_validate_flash_t *args_buf;
531
532 args_buf = (struct rtas_validate_flash_t *) dp->data;
533
534 if (args_buf->status == VALIDATE_READY) {
535 args_buf->buf_size = VALIDATE_BUF_SIZE;
536 validate_flash(args_buf);
537 }
538
539 /* The matching atomic_inc was in rtas_excl_open() */
540 atomic_dec(&dp->count);
541
542 return 0;
543}
544
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100545static void rtas_flash_firmware(int reboot_type)
546{
547 unsigned long image_size;
548 struct flash_block_list *f, *next, *flist;
549 unsigned long rtas_block_list;
550 int i, status, update_token;
551
Milton Millerbd2b64a2010-06-12 03:48:47 +0000552 if (rtas_firmware_flash_list == NULL)
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100553 return; /* nothing to do */
554
555 if (reboot_type != SYS_RESTART) {
556 printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
557 printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
558 return;
559 }
560
561 update_token = rtas_token("ibm,update-flash-64-and-reboot");
562 if (update_token == RTAS_UNKNOWN_SERVICE) {
563 printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot "
564 "is not available -- not a service partition?\n");
565 printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
566 return;
567 }
568
Milton Millerbd2b64a2010-06-12 03:48:47 +0000569 /*
Ravi K. Nittaladf17f562011-10-03 21:49:53 +0000570 * Just before starting the firmware flash, cancel the event scan work
571 * to avoid any soft lockup issues.
572 */
573 rtas_cancel_event_scan();
574
575 /*
Milton Millerbd2b64a2010-06-12 03:48:47 +0000576 * NOTE: the "first" block must be under 4GB, so we create
577 * an entry with no data blocks in the reserved buffer in
578 * the kernel data segment.
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100579 */
Milton Millerbd2b64a2010-06-12 03:48:47 +0000580 spin_lock(&rtas_data_buf_lock);
581 flist = (struct flash_block_list *)&rtas_data_buf[0];
582 flist->num_blocks = 0;
583 flist->next = rtas_firmware_flash_list;
Michael Ellerman3d267522012-07-25 21:19:56 +0000584 rtas_block_list = __pa(flist);
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100585 if (rtas_block_list >= 4UL*1024*1024*1024) {
586 printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
Milton Millerbd2b64a2010-06-12 03:48:47 +0000587 spin_unlock(&rtas_data_buf_lock);
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100588 return;
589 }
590
591 printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
592 /* Update the block_list in place. */
Milton Millerbd2b64a2010-06-12 03:48:47 +0000593 rtas_firmware_flash_list = NULL; /* too hard to backout on error */
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100594 image_size = 0;
595 for (f = flist; f; f = next) {
596 /* Translate data addrs to absolute */
597 for (i = 0; i < f->num_blocks; i++) {
Michael Ellerman3d267522012-07-25 21:19:56 +0000598 f->blocks[i].data = (char *)__pa(f->blocks[i].data);
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100599 image_size += f->blocks[i].length;
600 }
601 next = f->next;
602 /* Don't translate NULL pointer for last entry */
603 if (f->next)
Michael Ellerman3d267522012-07-25 21:19:56 +0000604 f->next = (struct flash_block_list *)__pa(f->next);
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100605 else
606 f->next = NULL;
607 /* make num_blocks into the version/length field */
608 f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
609 }
610
611 printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
612 printk(KERN_ALERT "FLASH: performing flash and reboot\n");
613 rtas_progress("Flashing \n", 0x0);
614 rtas_progress("Please Wait... ", 0x0);
615 printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n");
616 status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
617 switch (status) { /* should only get "bad" status */
618 case 0:
619 printk(KERN_ALERT "FLASH: success\n");
620 break;
621 case -1:
622 printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n");
623 break;
624 case -3:
625 printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
626 break;
627 case -4:
628 printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n");
629 break;
630 default:
631 printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
632 break;
633 }
Milton Millerbd2b64a2010-06-12 03:48:47 +0000634 spin_unlock(&rtas_data_buf_lock);
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100635}
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637static void remove_flash_pde(struct proc_dir_entry *dp)
638{
639 if (dp) {
Jesper Juhl95eff202006-02-04 20:35:59 +0100640 kfree(dp->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 remove_proc_entry(dp->name, dp->parent);
642 }
643}
644
645static int initialize_flash_pde_data(const char *rtas_call_name,
646 size_t buf_size,
647 struct proc_dir_entry *dp)
648{
649 int *status;
650 int token;
651
Yan Burmanf8485352006-12-02 13:26:57 +0200652 dp->data = kzalloc(buf_size, GFP_KERNEL);
Julia Lawallbc269572012-10-21 00:52:05 +0000653 if (dp->data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /*
657 * This code assumes that the status int is the first member of the
658 * struct
659 */
660 status = (int *) dp->data;
661 token = rtas_token(rtas_call_name);
662 if (token == RTAS_UNKNOWN_SERVICE)
663 *status = FLASH_AUTH;
664 else
665 *status = FLASH_NO_OP;
666
667 return 0;
668}
669
670static struct proc_dir_entry *create_flash_pde(const char *filename,
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800671 const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Denis V. Lunev66747132008-04-29 01:02:26 -0700673 return proc_create(filename, S_IRUSR | S_IWUSR, NULL, fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800676static const struct file_operations rtas_flash_operations = {
Denis V. Lunev66747132008-04-29 01:02:26 -0700677 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 .read = rtas_flash_read,
679 .write = rtas_flash_write,
680 .open = rtas_excl_open,
681 .release = rtas_flash_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200682 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683};
684
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800685static const struct file_operations manage_flash_operations = {
Denis V. Lunev66747132008-04-29 01:02:26 -0700686 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 .read = manage_flash_read,
688 .write = manage_flash_write,
689 .open = rtas_excl_open,
690 .release = rtas_excl_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200691 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692};
693
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800694static const struct file_operations validate_flash_operations = {
Denis V. Lunev66747132008-04-29 01:02:26 -0700695 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 .read = validate_flash_read,
697 .write = validate_flash_write,
698 .open = rtas_excl_open,
699 .release = validate_flash_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200700 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701};
702
Michael Ellerman1c21a292008-05-08 14:27:19 +1000703static int __init rtas_flash_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 int rc;
706
707 if (rtas_token("ibm,update-flash-64-and-reboot") ==
708 RTAS_UNKNOWN_SERVICE) {
Anton Blanchard0e384982012-07-22 20:42:32 +0000709 pr_info("rtas_flash: no firmware flash support\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return 1;
711 }
712
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000713 firmware_flash_pde = create_flash_pde("powerpc/rtas/"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 FIRMWARE_FLASH_NAME,
715 &rtas_flash_operations);
716 if (firmware_flash_pde == NULL) {
717 rc = -ENOMEM;
718 goto cleanup;
719 }
720
721 rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
722 sizeof(struct rtas_update_flash_t),
723 firmware_flash_pde);
724 if (rc != 0)
725 goto cleanup;
726
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000727 firmware_update_pde = create_flash_pde("powerpc/rtas/"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 FIRMWARE_UPDATE_NAME,
729 &rtas_flash_operations);
730 if (firmware_update_pde == NULL) {
731 rc = -ENOMEM;
732 goto cleanup;
733 }
734
735 rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
736 sizeof(struct rtas_update_flash_t),
737 firmware_update_pde);
738 if (rc != 0)
739 goto cleanup;
740
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000741 validate_pde = create_flash_pde("powerpc/rtas/" VALIDATE_FLASH_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 &validate_flash_operations);
743 if (validate_pde == NULL) {
744 rc = -ENOMEM;
745 goto cleanup;
746 }
747
748 rc = initialize_flash_pde_data("ibm,validate-flash-image",
749 sizeof(struct rtas_validate_flash_t),
750 validate_pde);
751 if (rc != 0)
752 goto cleanup;
753
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000754 manage_pde = create_flash_pde("powerpc/rtas/" MANAGE_FLASH_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 &manage_flash_operations);
756 if (manage_pde == NULL) {
757 rc = -ENOMEM;
758 goto cleanup;
759 }
760
761 rc = initialize_flash_pde_data("ibm,manage-flash-image",
762 sizeof(struct rtas_manage_flash_t),
763 manage_pde);
764 if (rc != 0)
765 goto cleanup;
766
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100767 rtas_flash_term_hook = rtas_flash_firmware;
John Roseae883ca2006-11-08 10:07:30 -0600768
769 flash_block_cache = kmem_cache_create("rtas_flash_cache",
770 RTAS_BLK_SIZE, RTAS_BLK_SIZE, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900771 rtas_block_ctor);
John Roseae883ca2006-11-08 10:07:30 -0600772 if (!flash_block_cache) {
773 printk(KERN_ERR "%s: failed to create block cache\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100774 __func__);
John Roseae883ca2006-11-08 10:07:30 -0600775 rc = -ENOMEM;
776 goto cleanup;
777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return 0;
779
780cleanup:
781 remove_flash_pde(firmware_flash_pde);
782 remove_flash_pde(firmware_update_pde);
783 remove_flash_pde(validate_pde);
784 remove_flash_pde(manage_pde);
785
786 return rc;
787}
788
Michael Ellerman1c21a292008-05-08 14:27:19 +1000789static void __exit rtas_flash_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100791 rtas_flash_term_hook = NULL;
John Roseae883ca2006-11-08 10:07:30 -0600792
793 if (flash_block_cache)
794 kmem_cache_destroy(flash_block_cache);
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 remove_flash_pde(firmware_flash_pde);
797 remove_flash_pde(firmware_update_pde);
798 remove_flash_pde(validate_pde);
799 remove_flash_pde(manage_pde);
800}
801
802module_init(rtas_flash_init);
803module_exit(rtas_flash_cleanup);
804MODULE_LICENSE("GPL");