1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/proc_fs.h>
20#include <linux/reboot.h>
21#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
34#define RTAS_RC_SUCCESS 0
35#define RTAS_RC_HW_ERR -1
36#define RTAS_RC_BUSY -2
37
38
39#define FLASH_AUTH -9002
40#define FLASH_NO_OP -1099
41#define FLASH_IMG_SHORT -1005
42#define FLASH_IMG_BAD_LEN -1004
43#define FLASH_IMG_NULL_DATA -1003
44#define FLASH_IMG_READY 0
45
46
47#define MANAGE_AUTH -9002
48#define MANAGE_ACTIVE_ERR -9001
49#define MANAGE_NO_OP -1099
50#define MANAGE_PARAM_ERR -3
51#define MANAGE_HW_ERR -1
52
53
54#define VALIDATE_AUTH -9002
55#define VALIDATE_NO_OP -1099
56#define VALIDATE_INCOMPLETE -1002
57#define VALIDATE_READY -1001
58#define VALIDATE_PARAM_ERR -3
59#define VALIDATE_HW_ERR -1
60
61
62#define VALIDATE_TMP_UPDATE 0
63#define VALIDATE_FLASH_AUTH 1
64#define VALIDATE_INVALID_IMG 2
65#define VALIDATE_CUR_UNKNOWN 3
66
67
68
69
70#define VALIDATE_TMP_COMMIT_DL 4
71
72
73
74
75#define VALIDATE_TMP_COMMIT 5
76
77
78
79#define VALIDATE_TMP_UPDATE_DL 6
80
81
82
83
84#define VALIDATE_OUT_OF_WRNTY 7
85
86
87#define RTAS_REJECT_TMP_IMG 0
88#define RTAS_COMMIT_TMP_IMG 1
89
90
91#define VALIDATE_BUF_SIZE 4096
92#define VALIDATE_MSG_LEN 256
93#define RTAS_MSG_MAXLEN 64
94
95
96#define RTAS_BLKLIST_LENGTH 4096
97#define RTAS_BLK_SIZE 4096
98
99struct flash_block {
100 char *data;
101 unsigned long length;
102};
103
104
105
106
107
108
109
110#define FLASH_BLOCKS_PER_NODE ((RTAS_BLKLIST_LENGTH - 16) / sizeof(struct flash_block))
111struct flash_block_list {
112 unsigned long num_blocks;
113 struct flash_block_list *next;
114 struct flash_block blocks[FLASH_BLOCKS_PER_NODE];
115};
116
117static struct flash_block_list *rtas_firmware_flash_list;
118
119
120static struct kmem_cache *flash_block_cache = NULL;
121
122#define FLASH_BLOCK_LIST_VERSION (1UL)
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138struct rtas_update_flash_t
139{
140 int status;
141 struct flash_block_list *flist;
142};
143
144
145struct rtas_manage_flash_t
146{
147 int status;
148};
149
150
151struct rtas_validate_flash_t
152{
153 int status;
154 char *buf;
155 unsigned int buf_size;
156 unsigned int update_results;
157};
158
159static 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);
165
166
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
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;
181 if (block_size <= 0 || block_size > RTAS_BLK_SIZE) {
182 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++)
205 kmem_cache_free(flash_block_cache, f->blocks[i].data);
206 next = f->next;
207 kmem_cache_free(flash_block_cache, f);
208 f = next;
209 }
210}
211
212static int rtas_flash_release(struct inode *inode, struct file *file)
213{
214 struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
215
216 mutex_lock(&rtas_update_flash_mutex);
217
218 if (uf->flist) {
219
220
221 if (rtas_firmware_flash_list) {
222 free_flash_list(rtas_firmware_flash_list);
223 rtas_firmware_flash_list = NULL;
224 }
225
226 if (uf->status != FLASH_AUTH)
227 uf->status = flash_list_valid(uf->flist);
228
229 if (uf->status == FLASH_IMG_READY)
230 rtas_firmware_flash_list = uf->flist;
231 else
232 free_flash_list(uf->flist);
233
234 uf->flist = NULL;
235 }
236
237 mutex_unlock(&rtas_update_flash_mutex);
238 return 0;
239}
240
241static size_t get_flash_status_msg(int status, char *buf)
242{
243 const char *msg;
244 size_t len;
245
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:
266 return sprintf(buf, "error: unexpected status value %d\n",
267 status);
268 }
269
270 len = strlen(msg);
271 memcpy(buf, msg, len + 1);
272 return len;
273}
274
275
276static ssize_t rtas_flash_read_msg(struct file *file, char __user *buf,
277 size_t count, loff_t *ppos)
278{
279 struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
280 char msg[RTAS_MSG_MAXLEN];
281 size_t len;
282 int status;
283
284 mutex_lock(&rtas_update_flash_mutex);
285 status = uf->status;
286 mutex_unlock(&rtas_update_flash_mutex);
287
288
289 len = get_flash_status_msg(status, msg);
290 return simple_read_from_buffer(buf, count, ppos, msg, len);
291}
292
293static 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
305 sprintf(msg, "%d\n", status);
306 return simple_read_from_buffer(buf, count, ppos, msg, strlen(msg));
307}
308
309
310
311
312
313
314static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
315 size_t count, loff_t *off)
316{
317 struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
318 char *p;
319 int next_free, rc;
320 struct flash_block_list *fl;
321
322 mutex_lock(&rtas_update_flash_mutex);
323
324 if (uf->status == FLASH_AUTH || count == 0)
325 goto out;
326
327
328
329
330
331 if (uf->flist == NULL) {
332 uf->flist = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
333 if (!uf->flist)
334 goto nomem;
335 }
336
337 fl = uf->flist;
338 while (fl->next)
339 fl = fl->next;
340 next_free = fl->num_blocks;
341 if (next_free == FLASH_BLOCKS_PER_NODE) {
342
343 fl->next = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
344 if (!fl->next)
345 goto nomem;
346 fl = fl->next;
347 next_free = 0;
348 }
349
350 if (count > RTAS_BLK_SIZE)
351 count = RTAS_BLK_SIZE;
352 p = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
353 if (!p)
354 goto nomem;
355
356 if(copy_from_user(p, buffer, count)) {
357 kmem_cache_free(flash_block_cache, p);
358 rc = -EFAULT;
359 goto error;
360 }
361 fl->blocks[next_free].data = p;
362 fl->blocks[next_free].length = count;
363 fl->num_blocks++;
364out:
365 mutex_unlock(&rtas_update_flash_mutex);
366 return count;
367
368nomem:
369 rc = -ENOMEM;
370error:
371 mutex_unlock(&rtas_update_flash_mutex);
372 return rc;
373}
374
375
376
377
378static void manage_flash(struct rtas_manage_flash_t *args_buf, unsigned int op)
379{
380 s32 rc;
381
382 do {
383 rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1, 1,
384 NULL, op);
385 } while (rtas_busy_delay(rc));
386
387 args_buf->status = rc;
388}
389
390static ssize_t manage_flash_read(struct file *file, char __user *buf,
391 size_t count, loff_t *ppos)
392{
393 struct rtas_manage_flash_t *const args_buf = &rtas_manage_flash_data;
394 char msg[RTAS_MSG_MAXLEN];
395 int msglen, status;
396
397 mutex_lock(&rtas_manage_flash_mutex);
398 status = args_buf->status;
399 mutex_unlock(&rtas_manage_flash_mutex);
400
401 msglen = sprintf(msg, "%d\n", status);
402 return simple_read_from_buffer(buf, count, ppos, msg, msglen);
403}
404
405static ssize_t manage_flash_write(struct file *file, const char __user *buf,
406 size_t count, loff_t *off)
407{
408 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";
411 char stkbuf[10];
412 int op, rc;
413
414 mutex_lock(&rtas_manage_flash_mutex);
415
416 if ((args_buf->status == MANAGE_AUTH) || (count == 0))
417 goto out;
418
419 op = -1;
420 if (buf) {
421 if (count > 9) count = 9;
422 rc = -EFAULT;
423 if (copy_from_user (stkbuf, buf, count))
424 goto error;
425 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
431 if (op == -1) {
432 rc = -EINVAL;
433 goto error;
434 }
435
436 manage_flash(args_buf, op);
437out:
438 mutex_unlock(&rtas_manage_flash_mutex);
439 return count;
440
441error:
442 mutex_unlock(&rtas_manage_flash_mutex);
443 return rc;
444}
445
446
447
448
449static void validate_flash(struct rtas_validate_flash_t *args_buf)
450{
451 int token = rtas_token("ibm,validate-flash-image");
452 int update_results;
453 s32 rc;
454
455 rc = 0;
456 do {
457 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);
463 } while (rtas_busy_delay(rc));
464
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,
470 char *msg, int msglen)
471{
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))
478 n += snprintf(msg + n, msglen - n, "%s\n",
479 args_buf->buf);
480 } else {
481 n = sprintf(msg, "%d\n", args_buf->status);
482 }
483 return n;
484}
485
486static ssize_t validate_flash_read(struct file *file, char __user *buf,
487 size_t count, loff_t *ppos)
488{
489 struct rtas_validate_flash_t *const args_buf =
490 &rtas_validate_flash_data;
491 char msg[VALIDATE_MSG_LEN];
492 int msglen;
493
494 mutex_lock(&rtas_validate_flash_mutex);
495 msglen = get_validate_flash_msg(args_buf, msg, VALIDATE_MSG_LEN);
496 mutex_unlock(&rtas_validate_flash_mutex);
497
498 return simple_read_from_buffer(buf, count, ppos, msg, msglen);
499}
500
501static ssize_t validate_flash_write(struct file *file, const char __user *buf,
502 size_t count, loff_t *off)
503{
504 struct rtas_validate_flash_t *const args_buf =
505 &rtas_validate_flash_data;
506 int rc;
507
508 mutex_lock(&rtas_validate_flash_mutex);
509
510
511
512 if ((*off >= VALIDATE_BUF_SIZE) ||
513 (args_buf->status == VALIDATE_AUTH)) {
514 *off += count;
515 mutex_unlock(&rtas_validate_flash_mutex);
516 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:
538 mutex_unlock(&rtas_validate_flash_mutex);
539 return rc;
540}
541
542static int validate_flash_release(struct inode *inode, struct file *file)
543{
544 struct rtas_validate_flash_t *const args_buf =
545 &rtas_validate_flash_data;
546
547 mutex_lock(&rtas_validate_flash_mutex);
548
549 if (args_buf->status == VALIDATE_READY) {
550 args_buf->buf_size = VALIDATE_BUF_SIZE;
551 validate_flash(args_buf);
552 }
553
554 mutex_unlock(&rtas_validate_flash_mutex);
555 return 0;
556}
557
558
559
560
561static 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
568 if (rtas_firmware_flash_list == NULL)
569 return;
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
585
586
587
588
589 rtas_cancel_event_scan();
590
591
592
593
594
595
596 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;
600 rtas_block_list = __pa(flist);
601 if (rtas_block_list >= 4UL*1024*1024*1024) {
602 printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
603 spin_unlock(&rtas_data_buf_lock);
604 return;
605 }
606
607 printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
608
609 rtas_firmware_flash_list = NULL;
610 image_size = 0;
611 for (f = flist; f; f = next) {
612
613 for (i = 0; i < f->num_blocks; i++) {
614 f->blocks[i].data = (char *)cpu_to_be64(__pa(f->blocks[i].data));
615 image_size += f->blocks[i].length;
616 f->blocks[i].length = cpu_to_be64(f->blocks[i].length);
617 }
618 next = f->next;
619
620 if (f->next)
621 f->next = (struct flash_block_list *)cpu_to_be64(__pa(f->next));
622 else
623 f->next = NULL;
624
625 f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
626 f->num_blocks = cpu_to_be64(f->num_blocks);
627 }
628
629 printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
630 printk(KERN_ALERT "FLASH: performing flash and reboot\n");
631 rtas_progress("Flashing \n", 0x0);
632 rtas_progress("Please Wait... ", 0x0);
633 printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n");
634 status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
635 switch (status) {
636 case 0:
637 printk(KERN_ALERT "FLASH: success\n");
638 break;
639 case -1:
640 printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n");
641 break;
642 case -3:
643 printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
644 break;
645 case -4:
646 printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n");
647 break;
648 default:
649 printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
650 break;
651 }
652 spin_unlock(&rtas_data_buf_lock);
653}
654
655
656
657
658struct rtas_flash_file {
659 const char *filename;
660 const char *rtas_call_name;
661 int *status;
662 const struct file_operations fops;
663};
664
665static const struct rtas_flash_file rtas_flash_files[] = {
666 {
667 .filename = "powerpc/rtas/" FIRMWARE_FLASH_NAME,
668 .rtas_call_name = "ibm,update-flash-64-and-reboot",
669 .status = &rtas_update_flash_data.status,
670 .fops.read = rtas_flash_read_msg,
671 .fops.write = rtas_flash_write,
672 .fops.release = rtas_flash_release,
673 .fops.llseek = default_llseek,
674 },
675 {
676 .filename = "powerpc/rtas/" FIRMWARE_UPDATE_NAME,
677 .rtas_call_name = "ibm,update-flash-64-and-reboot",
678 .status = &rtas_update_flash_data.status,
679 .fops.read = rtas_flash_read_num,
680 .fops.write = rtas_flash_write,
681 .fops.release = rtas_flash_release,
682 .fops.llseek = default_llseek,
683 },
684 {
685 .filename = "powerpc/rtas/" VALIDATE_FLASH_NAME,
686 .rtas_call_name = "ibm,validate-flash-image",
687 .status = &rtas_validate_flash_data.status,
688 .fops.read = validate_flash_read,
689 .fops.write = validate_flash_write,
690 .fops.release = validate_flash_release,
691 .fops.llseek = default_llseek,
692 },
693 {
694 .filename = "powerpc/rtas/" MANAGE_FLASH_NAME,
695 .rtas_call_name = "ibm,manage-flash-image",
696 .status = &rtas_manage_flash_data.status,
697 .fops.read = manage_flash_read,
698 .fops.write = manage_flash_write,
699 .fops.llseek = default_llseek,
700 }
701};
702
703static int __init rtas_flash_init(void)
704{
705 int i;
706
707 if (rtas_token("ibm,update-flash-64-and-reboot") ==
708 RTAS_UNKNOWN_SERVICE) {
709 pr_info("rtas_flash: no firmware flash support\n");
710 return -EINVAL;
711 }
712
713 rtas_validate_flash_data.buf = kzalloc(VALIDATE_BUF_SIZE, GFP_KERNEL);
714 if (!rtas_validate_flash_data.buf)
715 return -ENOMEM;
716
717 flash_block_cache = kmem_cache_create("rtas_flash_cache",
718 RTAS_BLK_SIZE, RTAS_BLK_SIZE, 0,
719 NULL);
720 if (!flash_block_cache) {
721 printk(KERN_ERR "%s: failed to create block cache\n",
722 __func__);
723 goto enomem_buf;
724 }
725
726 for (i = 0; i < ARRAY_SIZE(rtas_flash_files); i++) {
727 const struct rtas_flash_file *f = &rtas_flash_files[i];
728 int token;
729
730 if (!proc_create(f->filename, S_IRUSR | S_IWUSR, NULL, &f->fops))
731 goto enomem;
732
733
734
735
736
737 token = rtas_token(f->rtas_call_name);
738 if (token == RTAS_UNKNOWN_SERVICE)
739 *f->status = FLASH_AUTH;
740 else
741 *f->status = FLASH_NO_OP;
742 }
743
744 rtas_flash_term_hook = rtas_flash_firmware;
745 return 0;
746
747enomem:
748 while (--i >= 0) {
749 const struct rtas_flash_file *f = &rtas_flash_files[i];
750 remove_proc_entry(f->filename, NULL);
751 }
752
753 kmem_cache_destroy(flash_block_cache);
754enomem_buf:
755 kfree(rtas_validate_flash_data.buf);
756 return -ENOMEM;
757}
758
759static void __exit rtas_flash_cleanup(void)
760{
761 int i;
762
763 rtas_flash_term_hook = NULL;
764
765 if (rtas_firmware_flash_list) {
766 free_flash_list(rtas_firmware_flash_list);
767 rtas_firmware_flash_list = NULL;
768 }
769
770 for (i = 0; i < ARRAY_SIZE(rtas_flash_files); i++) {
771 const struct rtas_flash_file *f = &rtas_flash_files[i];
772 remove_proc_entry(f->filename, NULL);
773 }
774
775 kmem_cache_destroy(flash_block_cache);
776 kfree(rtas_validate_flash_data.buf);
777}
778
779module_init(rtas_flash_init);
780module_exit(rtas_flash_cleanup);
781MODULE_LICENSE("GPL");
782