blob: 658e89d2025b0b2dd65bb812d2c89d65867c1015 [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
Vasant Hegdea94a1472013-05-07 16:54:47 +000092#define VALIDATE_MSG_LEN 256
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#define RTAS_MSG_MAXLEN 64
94
John Roseae883ca2006-11-08 10:07:30 -060095/* Quirk - RTAS requires 4k list length and block size */
96#define RTAS_BLKLIST_LENGTH 4096
97#define RTAS_BLK_SIZE 4096
98
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +110099struct flash_block {
100 char *data;
101 unsigned long length;
102};
103
104/* This struct is very similar but not identical to
105 * that needed by the rtas flash update.
106 * All we need to do for rtas is rewrite num_blocks
107 * into a version/length and translate the pointers
108 * to absolute.
109 */
John Roseae883ca2006-11-08 10:07:30 -0600110#define FLASH_BLOCKS_PER_NODE ((RTAS_BLKLIST_LENGTH - 16) / sizeof(struct flash_block))
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100111struct flash_block_list {
112 unsigned long num_blocks;
113 struct flash_block_list *next;
114 struct flash_block blocks[FLASH_BLOCKS_PER_NODE];
115};
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100116
Milton Millerbd2b64a2010-06-12 03:48:47 +0000117static struct flash_block_list *rtas_firmware_flash_list;
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100118
John Roseae883ca2006-11-08 10:07:30 -0600119/* Use slab cache to guarantee 4k alignment */
Christoph Lametere18b8902006-12-06 20:33:20 -0800120static struct kmem_cache *flash_block_cache = NULL;
John Roseae883ca2006-11-08 10:07:30 -0600121
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100122#define FLASH_BLOCK_LIST_VERSION (1UL)
123
David Howellse8eeded2013-04-13 00:48:49 +0100124/*
125 * Local copy of the flash block list.
126 *
127 * The rtas_firmware_flash_list varable will be
Milton Millerbd2b64a2010-06-12 03:48:47 +0000128 * set once the data is fully read.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 *
130 * For convenience as we build the list we use virtual addrs,
131 * we do not fill in the version number, and the length field
132 * is treated as the number of entries currently in the block
Milton Millerbd2b64a2010-06-12 03:48:47 +0000133 * (i.e. not a byte count). This is all fixed when calling
134 * the flash routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 */
136
137/* Status int must be first member of struct */
138struct rtas_update_flash_t
139{
140 int status; /* Flash update status */
141 struct flash_block_list *flist; /* Local copy of flash block list */
142};
143
144/* Status int must be first member of struct */
145struct rtas_manage_flash_t
146{
147 int status; /* Returned status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
150/* Status int must be first member of struct */
151struct rtas_validate_flash_t
152{
153 int status; /* Returned status */
David Howellse8eeded2013-04-13 00:48:49 +0100154 char *buf; /* Candidate image buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 unsigned int buf_size; /* Size of image buf */
156 unsigned int update_results; /* Update results token */
157};
158
David Howellse8eeded2013-04-13 00:48:49 +0100159static struct rtas_update_flash_t rtas_update_flash_data;
160static struct rtas_manage_flash_t rtas_manage_flash_data;
161static struct rtas_validate_flash_t rtas_validate_flash_data;
162static DEFINE_MUTEX(rtas_update_flash_mutex);
163static DEFINE_MUTEX(rtas_manage_flash_mutex);
164static DEFINE_MUTEX(rtas_validate_flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166/* Do simple sanity checks on the flash image. */
167static int flash_list_valid(struct flash_block_list *flist)
168{
169 struct flash_block_list *f;
170 int i;
171 unsigned long block_size, image_size;
172
173 /* Paranoid self test here. We also collect the image size. */
174 image_size = 0;
175 for (f = flist; f; f = f->next) {
176 for (i = 0; i < f->num_blocks; i++) {
177 if (f->blocks[i].data == NULL) {
178 return FLASH_IMG_NULL_DATA;
179 }
180 block_size = f->blocks[i].length;
John Roseae883ca2006-11-08 10:07:30 -0600181 if (block_size <= 0 || block_size > RTAS_BLK_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return FLASH_IMG_BAD_LEN;
183 }
184 image_size += block_size;
185 }
186 }
187
188 if (image_size < (256 << 10)) {
189 if (image_size < 2)
190 return FLASH_NO_OP;
191 }
192
193 printk(KERN_INFO "FLASH: flash image with %ld bytes stored for hardware flash on reboot\n", image_size);
194
195 return FLASH_IMG_READY;
196}
197
198static void free_flash_list(struct flash_block_list *f)
199{
200 struct flash_block_list *next;
201 int i;
202
203 while (f) {
204 for (i = 0; i < f->num_blocks; i++)
John Roseae883ca2006-11-08 10:07:30 -0600205 kmem_cache_free(flash_block_cache, f->blocks[i].data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 next = f->next;
John Roseae883ca2006-11-08 10:07:30 -0600207 kmem_cache_free(flash_block_cache, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 f = next;
209 }
210}
211
212static int rtas_flash_release(struct inode *inode, struct file *file)
213{
David Howellse8eeded2013-04-13 00:48:49 +0100214 struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
215
216 mutex_lock(&rtas_update_flash_mutex);
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (uf->flist) {
219 /* File was opened in write mode for a new flash attempt */
220 /* Clear saved list */
Milton Millerbd2b64a2010-06-12 03:48:47 +0000221 if (rtas_firmware_flash_list) {
222 free_flash_list(rtas_firmware_flash_list);
223 rtas_firmware_flash_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
226 if (uf->status != FLASH_AUTH)
227 uf->status = flash_list_valid(uf->flist);
228
229 if (uf->status == FLASH_IMG_READY)
Milton Millerbd2b64a2010-06-12 03:48:47 +0000230 rtas_firmware_flash_list = uf->flist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 else
232 free_flash_list(uf->flist);
233
234 uf->flist = NULL;
235 }
236
David Howellse8eeded2013-04-13 00:48:49 +0100237 mutex_unlock(&rtas_update_flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return 0;
239}
240
David Howellse8eeded2013-04-13 00:48:49 +0100241static size_t get_flash_status_msg(int status, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
David Howellse8eeded2013-04-13 00:48:49 +0100243 const char *msg;
244 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 switch (status) {
247 case FLASH_AUTH:
248 msg = "error: this partition does not have service authority\n";
249 break;
250 case FLASH_NO_OP:
251 msg = "info: no firmware image for flash\n";
252 break;
253 case FLASH_IMG_SHORT:
254 msg = "error: flash image short\n";
255 break;
256 case FLASH_IMG_BAD_LEN:
257 msg = "error: internal error bad length\n";
258 break;
259 case FLASH_IMG_NULL_DATA:
260 msg = "error: internal error null data\n";
261 break;
262 case FLASH_IMG_READY:
263 msg = "ready: firmware image ready for flash on reboot\n";
264 break;
265 default:
David Howellse8eeded2013-04-13 00:48:49 +0100266 return sprintf(buf, "error: unexpected status value %d\n",
267 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269
David Howellse8eeded2013-04-13 00:48:49 +0100270 len = strlen(msg);
271 memcpy(buf, msg, len + 1);
272 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275/* Reading the proc file will show status (not the firmware contents) */
David Howellse8eeded2013-04-13 00:48:49 +0100276static ssize_t rtas_flash_read_msg(struct file *file, char __user *buf,
277 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
David Howellse8eeded2013-04-13 00:48:49 +0100279 struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 char msg[RTAS_MSG_MAXLEN];
David Howellse8eeded2013-04-13 00:48:49 +0100281 size_t len;
282 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
David Howellse8eeded2013-04-13 00:48:49 +0100284 mutex_lock(&rtas_update_flash_mutex);
285 status = uf->status;
286 mutex_unlock(&rtas_update_flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
David Howellse8eeded2013-04-13 00:48:49 +0100288 /* Read as text message */
289 len = get_flash_status_msg(status, msg);
290 return simple_read_from_buffer(buf, count, ppos, msg, len);
291}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
David Howellse8eeded2013-04-13 00:48:49 +0100293static ssize_t rtas_flash_read_num(struct file *file, char __user *buf,
294 size_t count, loff_t *ppos)
295{
296 struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
297 char msg[RTAS_MSG_MAXLEN];
298 int status;
299
300 mutex_lock(&rtas_update_flash_mutex);
301 status = uf->status;
302 mutex_unlock(&rtas_update_flash_mutex);
303
304 /* Read as number */
305 sprintf(msg, "%d\n", status);
Akinobu Mita4c4a5cf2010-12-24 20:03:59 +0000306 return simple_read_from_buffer(buf, count, ppos, msg, strlen(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309/* We could be much more efficient here. But to keep this function
310 * simple we allocate a page to the block list no matter how small the
311 * count is. If the system is low on memory it will be just as well
312 * that we fail....
313 */
Al Viroefa54572005-04-26 11:26:53 -0700314static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 size_t count, loff_t *off)
316{
David Howellse8eeded2013-04-13 00:48:49 +0100317 struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 char *p;
David Howellse8eeded2013-04-13 00:48:49 +0100319 int next_free, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 struct flash_block_list *fl;
321
David Howellse8eeded2013-04-13 00:48:49 +0100322 mutex_lock(&rtas_update_flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 if (uf->status == FLASH_AUTH || count == 0)
David Howellse8eeded2013-04-13 00:48:49 +0100325 goto out; /* discard data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /* In the case that the image is not ready for flashing, the memory
328 * allocated for the block list will be freed upon the release of the
329 * proc file
330 */
331 if (uf->flist == NULL) {
Vasant Hegdefb4696c2013-04-28 18:43:56 +0000332 uf->flist = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (!uf->flist)
David Howellse8eeded2013-04-13 00:48:49 +0100334 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
337 fl = uf->flist;
338 while (fl->next)
339 fl = fl->next; /* seek to last block_list for append */
340 next_free = fl->num_blocks;
341 if (next_free == FLASH_BLOCKS_PER_NODE) {
342 /* Need to allocate another block_list */
Vasant Hegdefb4696c2013-04-28 18:43:56 +0000343 fl->next = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (!fl->next)
David Howellse8eeded2013-04-13 00:48:49 +0100345 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 fl = fl->next;
347 next_free = 0;
348 }
349
John Roseae883ca2006-11-08 10:07:30 -0600350 if (count > RTAS_BLK_SIZE)
351 count = RTAS_BLK_SIZE;
Vasant Hegdefb4696c2013-04-28 18:43:56 +0000352 p = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (!p)
David Howellse8eeded2013-04-13 00:48:49 +0100354 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 if(copy_from_user(p, buffer, count)) {
John Roseae883ca2006-11-08 10:07:30 -0600357 kmem_cache_free(flash_block_cache, p);
David Howellse8eeded2013-04-13 00:48:49 +0100358 rc = -EFAULT;
359 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361 fl->blocks[next_free].data = p;
362 fl->blocks[next_free].length = count;
363 fl->num_blocks++;
David Howellse8eeded2013-04-13 00:48:49 +0100364out:
365 mutex_unlock(&rtas_update_flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return count;
David Howellse8eeded2013-04-13 00:48:49 +0100367
368nomem:
369 rc = -ENOMEM;
370error:
371 mutex_unlock(&rtas_update_flash_mutex);
372 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
David Howellse8eeded2013-04-13 00:48:49 +0100375/*
376 * Flash management routines.
377 */
378static void manage_flash(struct rtas_manage_flash_t *args_buf, unsigned int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 s32 rc;
381
John Rose507279d2006-06-05 16:31:48 -0500382 do {
David Howellse8eeded2013-04-13 00:48:49 +0100383 rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1, 1,
384 NULL, op);
John Rose507279d2006-06-05 16:31:48 -0500385 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 args_buf->status = rc;
388}
389
Al Viroefa54572005-04-26 11:26:53 -0700390static ssize_t manage_flash_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 size_t count, loff_t *ppos)
392{
David Howellse8eeded2013-04-13 00:48:49 +0100393 struct rtas_manage_flash_t *const args_buf = &rtas_manage_flash_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 char msg[RTAS_MSG_MAXLEN];
David Howellse8eeded2013-04-13 00:48:49 +0100395 int msglen, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
David Howellse8eeded2013-04-13 00:48:49 +0100397 mutex_lock(&rtas_manage_flash_mutex);
398 status = args_buf->status;
399 mutex_unlock(&rtas_manage_flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
David Howellse8eeded2013-04-13 00:48:49 +0100401 msglen = sprintf(msg, "%d\n", status);
Akinobu Mita4c4a5cf2010-12-24 20:03:59 +0000402 return simple_read_from_buffer(buf, count, ppos, msg, msglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Al Viroefa54572005-04-26 11:26:53 -0700405static ssize_t manage_flash_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 size_t count, loff_t *off)
407{
David Howellse8eeded2013-04-13 00:48:49 +0100408 struct rtas_manage_flash_t *const args_buf = &rtas_manage_flash_data;
409 static const char reject_str[] = "0";
410 static const char commit_str[] = "1";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 char stkbuf[10];
David Howellse8eeded2013-04-13 00:48:49 +0100412 int op, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
David Howellse8eeded2013-04-13 00:48:49 +0100414 mutex_lock(&rtas_manage_flash_mutex);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if ((args_buf->status == MANAGE_AUTH) || (count == 0))
David Howellse8eeded2013-04-13 00:48:49 +0100417 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 op = -1;
420 if (buf) {
421 if (count > 9) count = 9;
David Howellse8eeded2013-04-13 00:48:49 +0100422 rc = -EFAULT;
423 if (copy_from_user (stkbuf, buf, count))
424 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (strncmp(stkbuf, reject_str, strlen(reject_str)) == 0)
426 op = RTAS_REJECT_TMP_IMG;
427 else if (strncmp(stkbuf, commit_str, strlen(commit_str)) == 0)
428 op = RTAS_COMMIT_TMP_IMG;
429 }
430
David Howellse8eeded2013-04-13 00:48:49 +0100431 if (op == -1) { /* buf is empty, or contains invalid string */
432 rc = -EINVAL;
433 goto error;
434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
David Howellse8eeded2013-04-13 00:48:49 +0100436 manage_flash(args_buf, op);
437out:
438 mutex_unlock(&rtas_manage_flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return count;
David Howellse8eeded2013-04-13 00:48:49 +0100440
441error:
442 mutex_unlock(&rtas_manage_flash_mutex);
443 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
David Howellse8eeded2013-04-13 00:48:49 +0100446/*
447 * Validation routines.
448 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449static void validate_flash(struct rtas_validate_flash_t *args_buf)
450{
451 int token = rtas_token("ibm,validate-flash-image");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int update_results;
453 s32 rc;
454
455 rc = 0;
John Rose507279d2006-06-05 16:31:48 -0500456 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 spin_lock(&rtas_data_buf_lock);
458 memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE);
459 rc = rtas_call(token, 2, 2, &update_results,
460 (u32) __pa(rtas_data_buf), args_buf->buf_size);
461 memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE);
462 spin_unlock(&rtas_data_buf_lock);
John Rose507279d2006-06-05 16:31:48 -0500463 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 args_buf->status = rc;
466 args_buf->update_results = update_results;
467}
468
469static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf,
Vasant Hegdea94a1472013-05-07 16:54:47 +0000470 char *msg, int msglen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 int n;
473
474 if (args_buf->status >= VALIDATE_TMP_UPDATE) {
475 n = sprintf(msg, "%d\n", args_buf->update_results);
476 if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
477 (args_buf->update_results == VALIDATE_TMP_UPDATE))
Vasant Hegdea94a1472013-05-07 16:54:47 +0000478 n += snprintf(msg + n, msglen - n, "%s\n",
479 args_buf->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 } else {
481 n = sprintf(msg, "%d\n", args_buf->status);
482 }
483 return n;
484}
485
Al Viroefa54572005-04-26 11:26:53 -0700486static ssize_t validate_flash_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 size_t count, loff_t *ppos)
488{
David Howellse8eeded2013-04-13 00:48:49 +0100489 struct rtas_validate_flash_t *const args_buf =
490 &rtas_validate_flash_data;
Vasant Hegdea94a1472013-05-07 16:54:47 +0000491 char msg[VALIDATE_MSG_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int msglen;
493
David Howellse8eeded2013-04-13 00:48:49 +0100494 mutex_lock(&rtas_validate_flash_mutex);
Vasant Hegdea94a1472013-05-07 16:54:47 +0000495 msglen = get_validate_flash_msg(args_buf, msg, VALIDATE_MSG_LEN);
David Howellse8eeded2013-04-13 00:48:49 +0100496 mutex_unlock(&rtas_validate_flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Akinobu Mita4c4a5cf2010-12-24 20:03:59 +0000498 return simple_read_from_buffer(buf, count, ppos, msg, msglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Al Viroefa54572005-04-26 11:26:53 -0700501static ssize_t validate_flash_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 size_t count, loff_t *off)
503{
David Howellse8eeded2013-04-13 00:48:49 +0100504 struct rtas_validate_flash_t *const args_buf =
505 &rtas_validate_flash_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 int rc;
507
David Howellse8eeded2013-04-13 00:48:49 +0100508 mutex_lock(&rtas_validate_flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 /* We are only interested in the first 4K of the
511 * candidate image */
512 if ((*off >= VALIDATE_BUF_SIZE) ||
513 (args_buf->status == VALIDATE_AUTH)) {
514 *off += count;
David Howellse8eeded2013-04-13 00:48:49 +0100515 mutex_unlock(&rtas_validate_flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return count;
517 }
518
519 if (*off + count >= VALIDATE_BUF_SIZE) {
520 count = VALIDATE_BUF_SIZE - *off;
521 args_buf->status = VALIDATE_READY;
522 } else {
523 args_buf->status = VALIDATE_INCOMPLETE;
524 }
525
526 if (!access_ok(VERIFY_READ, buf, count)) {
527 rc = -EFAULT;
528 goto done;
529 }
530 if (copy_from_user(args_buf->buf + *off, buf, count)) {
531 rc = -EFAULT;
532 goto done;
533 }
534
535 *off += count;
536 rc = count;
537done:
David Howellse8eeded2013-04-13 00:48:49 +0100538 mutex_unlock(&rtas_validate_flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return rc;
540}
541
542static int validate_flash_release(struct inode *inode, struct file *file)
543{
David Howellse8eeded2013-04-13 00:48:49 +0100544 struct rtas_validate_flash_t *const args_buf =
545 &rtas_validate_flash_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
David Howellse8eeded2013-04-13 00:48:49 +0100547 mutex_lock(&rtas_validate_flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 if (args_buf->status == VALIDATE_READY) {
550 args_buf->buf_size = VALIDATE_BUF_SIZE;
551 validate_flash(args_buf);
552 }
553
David Howellse8eeded2013-04-13 00:48:49 +0100554 mutex_unlock(&rtas_validate_flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return 0;
556}
557
David Howellse8eeded2013-04-13 00:48:49 +0100558/*
559 * On-reboot flash update applicator.
560 */
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100561static void rtas_flash_firmware(int reboot_type)
562{
563 unsigned long image_size;
564 struct flash_block_list *f, *next, *flist;
565 unsigned long rtas_block_list;
566 int i, status, update_token;
567
Milton Millerbd2b64a2010-06-12 03:48:47 +0000568 if (rtas_firmware_flash_list == NULL)
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100569 return; /* nothing to do */
570
571 if (reboot_type != SYS_RESTART) {
572 printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
573 printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
574 return;
575 }
576
577 update_token = rtas_token("ibm,update-flash-64-and-reboot");
578 if (update_token == RTAS_UNKNOWN_SERVICE) {
579 printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot "
580 "is not available -- not a service partition?\n");
581 printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
582 return;
583 }
584
Milton Millerbd2b64a2010-06-12 03:48:47 +0000585 /*
Ravi K. Nittaladf17f562011-10-03 21:49:53 +0000586 * Just before starting the firmware flash, cancel the event scan work
587 * to avoid any soft lockup issues.
588 */
589 rtas_cancel_event_scan();
590
591 /*
Milton Millerbd2b64a2010-06-12 03:48:47 +0000592 * NOTE: the "first" block must be under 4GB, so we create
593 * an entry with no data blocks in the reserved buffer in
594 * the kernel data segment.
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100595 */
Milton Millerbd2b64a2010-06-12 03:48:47 +0000596 spin_lock(&rtas_data_buf_lock);
597 flist = (struct flash_block_list *)&rtas_data_buf[0];
598 flist->num_blocks = 0;
599 flist->next = rtas_firmware_flash_list;
Michael Ellerman3d267522012-07-25 21:19:56 +0000600 rtas_block_list = __pa(flist);
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100601 if (rtas_block_list >= 4UL*1024*1024*1024) {
602 printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
Milton Millerbd2b64a2010-06-12 03:48:47 +0000603 spin_unlock(&rtas_data_buf_lock);
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100604 return;
605 }
606
607 printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
608 /* Update the block_list in place. */
Milton Millerbd2b64a2010-06-12 03:48:47 +0000609 rtas_firmware_flash_list = NULL; /* too hard to backout on error */
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100610 image_size = 0;
611 for (f = flist; f; f = next) {
612 /* Translate data addrs to absolute */
613 for (i = 0; i < f->num_blocks; i++) {
Michael Ellerman3d267522012-07-25 21:19:56 +0000614 f->blocks[i].data = (char *)__pa(f->blocks[i].data);
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100615 image_size += f->blocks[i].length;
616 }
617 next = f->next;
618 /* Don't translate NULL pointer for last entry */
619 if (f->next)
Michael Ellerman3d267522012-07-25 21:19:56 +0000620 f->next = (struct flash_block_list *)__pa(f->next);
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100621 else
622 f->next = NULL;
623 /* make num_blocks into the version/length field */
624 f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
625 }
626
627 printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
628 printk(KERN_ALERT "FLASH: performing flash and reboot\n");
629 rtas_progress("Flashing \n", 0x0);
630 rtas_progress("Please Wait... ", 0x0);
631 printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n");
632 status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
633 switch (status) { /* should only get "bad" status */
634 case 0:
635 printk(KERN_ALERT "FLASH: success\n");
636 break;
637 case -1:
638 printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n");
639 break;
640 case -3:
641 printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
642 break;
643 case -4:
644 printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n");
645 break;
646 default:
647 printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
648 break;
649 }
Milton Millerbd2b64a2010-06-12 03:48:47 +0000650 spin_unlock(&rtas_data_buf_lock);
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100651}
652
David Howellse8eeded2013-04-13 00:48:49 +0100653/*
654 * Manifest of proc files to create
655 */
656struct rtas_flash_file {
657 const char *filename;
658 const char *rtas_call_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 int *status;
David Howellse8eeded2013-04-13 00:48:49 +0100660 const struct file_operations fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661};
662
David Howellse8eeded2013-04-13 00:48:49 +0100663static const struct rtas_flash_file rtas_flash_files[] = {
664 {
665 .filename = "powerpc/rtas/" FIRMWARE_FLASH_NAME,
666 .rtas_call_name = "ibm,update-flash-64-and-reboot",
667 .status = &rtas_update_flash_data.status,
668 .fops.read = rtas_flash_read_msg,
669 .fops.write = rtas_flash_write,
670 .fops.release = rtas_flash_release,
671 .fops.llseek = default_llseek,
672 },
673 {
674 .filename = "powerpc/rtas/" FIRMWARE_UPDATE_NAME,
675 .rtas_call_name = "ibm,update-flash-64-and-reboot",
676 .status = &rtas_update_flash_data.status,
677 .fops.read = rtas_flash_read_num,
678 .fops.write = rtas_flash_write,
679 .fops.release = rtas_flash_release,
680 .fops.llseek = default_llseek,
681 },
682 {
683 .filename = "powerpc/rtas/" VALIDATE_FLASH_NAME,
684 .rtas_call_name = "ibm,validate-flash-image",
685 .status = &rtas_validate_flash_data.status,
686 .fops.read = validate_flash_read,
687 .fops.write = validate_flash_write,
688 .fops.release = validate_flash_release,
689 .fops.llseek = default_llseek,
690 },
691 {
692 .filename = "powerpc/rtas/" MANAGE_FLASH_NAME,
693 .rtas_call_name = "ibm,manage-flash-image",
694 .status = &rtas_manage_flash_data.status,
695 .fops.read = manage_flash_read,
696 .fops.write = manage_flash_write,
697 .fops.llseek = default_llseek,
698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699};
700
Michael Ellerman1c21a292008-05-08 14:27:19 +1000701static int __init rtas_flash_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
David Howellse8eeded2013-04-13 00:48:49 +0100703 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (rtas_token("ibm,update-flash-64-and-reboot") ==
706 RTAS_UNKNOWN_SERVICE) {
Anton Blanchard0e384982012-07-22 20:42:32 +0000707 pr_info("rtas_flash: no firmware flash support\n");
Anton Blanchard0c930692014-04-14 21:23:32 +1000708 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
David Howellse8eeded2013-04-13 00:48:49 +0100711 rtas_validate_flash_data.buf = kzalloc(VALIDATE_BUF_SIZE, GFP_KERNEL);
712 if (!rtas_validate_flash_data.buf)
713 return -ENOMEM;
John Roseae883ca2006-11-08 10:07:30 -0600714
715 flash_block_cache = kmem_cache_create("rtas_flash_cache",
David Howellse8eeded2013-04-13 00:48:49 +0100716 RTAS_BLK_SIZE, RTAS_BLK_SIZE, 0,
Linus Torvalds5a148af2013-05-02 10:16:16 -0700717 NULL);
John Roseae883ca2006-11-08 10:07:30 -0600718 if (!flash_block_cache) {
719 printk(KERN_ERR "%s: failed to create block cache\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100720 __func__);
David Howellse8eeded2013-04-13 00:48:49 +0100721 goto enomem_buf;
John Roseae883ca2006-11-08 10:07:30 -0600722 }
David Howellse8eeded2013-04-13 00:48:49 +0100723
724 for (i = 0; i < ARRAY_SIZE(rtas_flash_files); i++) {
725 const struct rtas_flash_file *f = &rtas_flash_files[i];
726 int token;
727
728 if (!proc_create(f->filename, S_IRUSR | S_IWUSR, NULL, &f->fops))
729 goto enomem;
730
731 /*
732 * This code assumes that the status int is the first member of the
733 * struct
734 */
735 token = rtas_token(f->rtas_call_name);
736 if (token == RTAS_UNKNOWN_SERVICE)
737 *f->status = FLASH_AUTH;
738 else
739 *f->status = FLASH_NO_OP;
740 }
741
742 rtas_flash_term_hook = rtas_flash_firmware;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return 0;
744
David Howellse8eeded2013-04-13 00:48:49 +0100745enomem:
746 while (--i >= 0) {
747 const struct rtas_flash_file *f = &rtas_flash_files[i];
748 remove_proc_entry(f->filename, NULL);
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
David Howellse8eeded2013-04-13 00:48:49 +0100751 kmem_cache_destroy(flash_block_cache);
752enomem_buf:
753 kfree(rtas_validate_flash_data.buf);
754 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Michael Ellerman1c21a292008-05-08 14:27:19 +1000757static void __exit rtas_flash_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
David Howellse8eeded2013-04-13 00:48:49 +0100759 int i;
760
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100761 rtas_flash_term_hook = NULL;
John Roseae883ca2006-11-08 10:07:30 -0600762
Vasant Hegdead18a362013-02-08 01:18:36 +0000763 if (rtas_firmware_flash_list) {
764 free_flash_list(rtas_firmware_flash_list);
765 rtas_firmware_flash_list = NULL;
766 }
767
David Howellse8eeded2013-04-13 00:48:49 +0100768 for (i = 0; i < ARRAY_SIZE(rtas_flash_files); i++) {
769 const struct rtas_flash_file *f = &rtas_flash_files[i];
770 remove_proc_entry(f->filename, NULL);
771 }
John Roseae883ca2006-11-08 10:07:30 -0600772
David Howellse8eeded2013-04-13 00:48:49 +0100773 kmem_cache_destroy(flash_block_cache);
774 kfree(rtas_validate_flash_data.buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777module_init(rtas_flash_init);
778module_exit(rtas_flash_cleanup);
779MODULE_LICENSE("GPL");