16 #include <linux/module.h>
18 #include <linux/slab.h>
20 #include <linux/reboot.h>
21 #include <asm/delay.h>
22 #include <asm/uaccess.h>
25 #define MODULE_VERS "1.0"
26 #define MODULE_NAME "rtas_flash"
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"
34 #define RTAS_RC_SUCCESS 0
35 #define RTAS_RC_HW_ERR -1
36 #define RTAS_RC_BUSY -2
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
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
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 #define VALIDATE_TMP_UPDATE 0
61 #define VALIDATE_FLASH_AUTH 1
62 #define VALIDATE_INVALID_IMG 2
63 #define VALIDATE_CUR_UNKNOWN 3
64 #define VALIDATE_TMP_COMMIT_DL 4
65 #define VALIDATE_TMP_COMMIT 5
66 #define VALIDATE_TMP_UPDATE_DL 6
69 #define RTAS_REJECT_TMP_IMG 0
70 #define RTAS_COMMIT_TMP_IMG 1
73 #define VALIDATE_BUF_SIZE 4096
74 #define RTAS_MSG_MAXLEN 64
77 #define RTAS_BLKLIST_LENGTH 4096
78 #define RTAS_BLK_SIZE 4096
91 #define FLASH_BLOCKS_PER_NODE ((RTAS_BLKLIST_LENGTH - 16) / sizeof(struct flash_block))
103 #define FLASH_BLOCK_LIST_VERSION (1UL)
155 for (f = flist;
f; f = f->
next) {
160 block_size = f->
blocks[
i].length;
168 if (image_size < (256 << 10)) {
173 printk(
KERN_INFO "FLASH: flash image with %ld bytes stored for hardware flash on reboot\n", image_size);
201 if (rtas_firmware_flash_list) {
202 free_flash_list(rtas_firmware_flash_list);
203 rtas_firmware_flash_list =
NULL;
210 rtas_firmware_flash_list = uf->
flist;
212 free_flash_list(uf->
flist);
221 static void get_flash_status_msg(
int status,
char *
buf)
227 msg =
"error: this partition does not have service authority\n";
230 msg =
"info: no firmware image for flash\n";
233 msg =
"error: flash image short\n";
236 msg =
"error: internal error bad length\n";
239 msg =
"error: internal error null data\n";
242 msg =
"ready: firmware image ready for flash on reboot\n";
245 sprintf(buf,
"error: unexpected status value %d\n", status);
253 static ssize_t rtas_flash_read(
struct file *file,
char __user *buf,
254 size_t count, loff_t *ppos)
263 get_flash_status_msg(uf->
status, msg);
282 static ssize_t rtas_flash_write(
struct file *file,
const char __user *
buffer,
283 size_t count, loff_t *off)
329 fl->
blocks[next_free].data =
p;
336 static int rtas_excl_open(
struct inode *inode,
struct file *file)
341 spin_lock(&flash_file_open_lock);
343 spin_unlock(&flash_file_open_lock);
348 spin_unlock(&flash_file_open_lock);
353 static int rtas_excl_release(
struct inode *inode,
struct file *file)
374 static ssize_t manage_flash_read(
struct file *file,
char __user *buf,
375 size_t count, loff_t *ppos)
383 if (args_buf ==
NULL)
391 static ssize_t manage_flash_write(
struct file *file,
const char __user *buf,
392 size_t count, loff_t *off)
396 const char reject_str[] =
"0";
397 const char commit_str[] =
"1";
407 if (count > 9) count = 9;
413 else if (
strncmp(stkbuf, commit_str,
strlen(commit_str)) == 0)
421 manage_flash(args_buf);
434 spin_lock(&rtas_data_buf_lock);
436 rc =
rtas_call(token, 2, 2, &update_results,
439 spin_unlock(&rtas_data_buf_lock);
462 static ssize_t validate_flash_read(
struct file *file,
char __user *buf,
463 size_t count, loff_t *ppos)
472 msglen = get_validate_flash_msg(args_buf, msg);
477 static ssize_t validate_flash_write(
struct file *file,
const char __user *buf,
478 size_t count, loff_t *off)
527 static int validate_flash_release(
struct inode *inode,
struct file *file)
536 validate_flash(args_buf);
549 unsigned long rtas_block_list;
552 if (rtas_firmware_flash_list ==
NULL)
561 update_token =
rtas_token(
"ibm,update-flash-64-and-reboot");
562 if (update_token == RTAS_UNKNOWN_SERVICE) {
564 "is not available -- not a service partition?\n");
580 spin_lock(&rtas_data_buf_lock);
583 flist->
next = rtas_firmware_flash_list;
584 rtas_block_list =
__pa(flist);
585 if (rtas_block_list >= 4
UL*1024*1024*1024) {
586 printk(
KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
587 spin_unlock(&rtas_data_buf_lock);
593 rtas_firmware_flash_list =
NULL;
595 for (f = flist;
f; f =
next) {
599 image_size += f->
blocks[
i].length;
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);
622 printk(
KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n");
625 printk(
KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
628 printk(
KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n");
634 spin_unlock(&rtas_data_buf_lock);
645 static int initialize_flash_pde_data(
const char *rtas_call_name,
654 remove_flash_pde(dp);
662 status = (
int *) dp->
data;
664 if (token == RTAS_UNKNOWN_SERVICE)
680 .read = rtas_flash_read,
681 .write = rtas_flash_write,
682 .open = rtas_excl_open,
683 .release = rtas_flash_release,
689 .read = manage_flash_read,
690 .write = manage_flash_write,
691 .open = rtas_excl_open,
692 .release = rtas_excl_release,
698 .read = validate_flash_read,
699 .write = validate_flash_write,
700 .open = rtas_excl_open,
701 .release = validate_flash_release,
705 static int __init rtas_flash_init(
void)
709 if (
rtas_token(
"ibm,update-flash-64-and-reboot") ==
710 RTAS_UNKNOWN_SERVICE) {
711 pr_info(
"rtas_flash: no firmware flash support\n");
715 firmware_flash_pde = create_flash_pde(
"powerpc/rtas/"
717 &rtas_flash_operations);
718 if (firmware_flash_pde ==
NULL) {
723 rc = initialize_flash_pde_data(
"ibm,update-flash-64-and-reboot",
729 firmware_update_pde = create_flash_pde(
"powerpc/rtas/"
731 &rtas_flash_operations);
732 if (firmware_update_pde ==
NULL) {
737 rc = initialize_flash_pde_data(
"ibm,update-flash-64-and-reboot",
739 firmware_update_pde);
744 &validate_flash_operations);
745 if (validate_pde ==
NULL) {
750 rc = initialize_flash_pde_data(
"ibm,validate-flash-image",
757 &manage_flash_operations);
758 if (manage_pde ==
NULL) {
763 rc = initialize_flash_pde_data(
"ibm,manage-flash-image",
774 if (!flash_block_cache) {
783 remove_flash_pde(firmware_flash_pde);
784 remove_flash_pde(firmware_update_pde);
785 remove_flash_pde(validate_pde);
786 remove_flash_pde(manage_pde);
791 static void __exit rtas_flash_cleanup(
void)
795 if (flash_block_cache)
798 remove_flash_pde(firmware_flash_pde);
799 remove_flash_pde(firmware_update_pde);
800 remove_flash_pde(validate_pde);
801 remove_flash_pde(manage_pde);