change source file mode to 0644 instead of 0755
[profile/mobile/platform/kernel/u-boot-tm1.git] / property / recv_mode.c
1 #include <config.h>
2 #include <common.h>
3 #include <linux/types.h>
4 #include <asm/arch/bits.h>
5 #include <image.h>
6 #include <linux/string.h>
7 #include <android_bootimg.h>
8 #include <linux/mtd/mtd.h>
9 #include <linux/mtd/nand.h>
10 #include <nand.h>
11 #include <android_boot.h>
12 #include <environment.h>
13 #include <jffs2/jffs2.h>
14 #include <boot_mode.h>
15 #include <android_recovery.h>
16
17 #ifdef dprintf
18 #undef dprintf
19 #endif
20 #define dprintf(fmt, args...) printf(fmt, ##args)
21
22 void nv_patch(char * addr, int size)
23 {
24         int i = 0;
25         for(i=0; i<FIXNV_SIZE-size; i++){
26                 addr[size + i] = 0xff;
27         }
28         for(i=0; i<4; i++){
29                 addr[FIXNV_SIZE + i] = 0x5a;
30         }
31         *((unsigned int*)&addr[FIXNV_SIZE - 8]) = size;//keep the real  fixnv file size.
32         return;
33 }
34 extern int get_recovery_message(struct recovery_message *out);
35 extern int set_recovery_message(const struct recovery_message *in);
36 int read_update_header_for_bootloader(struct update_header *header)
37 {
38         return 0;
39 }
40
41 int update_firmware_image (struct update_header *header, char *name)
42 {
43
44         return 0;
45 }
46
47 /* Bootloader / Recovery Flow
48  *
49  * On every boot, the bootloader will read the recovery_message
50  * from flash and check the command field.  The bootloader should
51  * deal with the command field not having a 0 terminator correctly
52  * (so as to not crash if the block is invalid or corrupt).
53  *
54  * The bootloader will have to publish the partition that contains
55  * the recovery_message to the linux kernel so it can update it.
56  *
57  * if command == "boot-recovery" -> boot recovery.img
58  * else if command == "update-radio" -> update radio image (below)
59  * else -> boot boot.img (normal boot)
60  *
61  * Radio Update Flow
62  * 1. the bootloader will attempt to load and validate the header
63  * 2. if the header is invalid, status="invalid-update", goto #8
64  * 3. display the busy image on-screen
65  * 4. if the update image is invalid, status="invalid-radio-image", goto #8
66  * 5. attempt to update the firmware (depending on the command)
67  * 6. if successful, status="okay", goto #8
68  * 7. if failed, and the old image can still boot, status="failed-update"
69  * 8. write the recovery_message, leaving the recovery field
70  *    unchanged, updating status, and setting command to
71  *    "boot-recovery"
72  * 9. reboot
73  *
74  * The bootloader will not modify or erase the cache partition.
75  * It is recovery's responsibility to clean up the mess afterwards.
76  */
77
78 int get_mode_from_file(void)
79 {
80         struct recovery_message msg;
81         struct update_header header;
82         char partition_name[32];
83         unsigned valid_command = 0;
84
85         // get recovery message
86         if(get_recovery_message(&msg))
87                 return UNKNOW_REBOOT_MODE;
88         if (msg.command[0] != 0 && msg.command[0] != 255) {
89                 dprintf("Recovery command: %.*s\n", sizeof(msg.command), msg.command);
90         }
91         msg.command[sizeof(msg.command)-1] = '\0'; //Ensure termination
92
93         if (!strcmp("boot-recovery",msg.command)) {
94                 return RECOVERY_MODE;
95         }
96
97         if (!strcmp("update-radio",msg.command)) {
98                 valid_command = 1;
99                 strcpy(partition_name, "FOTA");
100         }
101
102         //Todo: Add support for bootloader update too.
103
104         if(!valid_command) {
105                 //We need not to do anything
106                 return 0; // Boot in normal mode
107         }
108
109         if (read_update_header_for_bootloader(&header)) {
110                 strcpy(msg.status, "invalid-update");
111                 goto SEND_RECOVERY_MSG;
112         }
113
114         if (update_firmware_image (&header, partition_name)) {
115                 strcpy(msg.status, "failed-update");
116                 goto SEND_RECOVERY_MSG;
117         }
118         strcpy(msg.status, "OKAY");
119
120 SEND_RECOVERY_MSG:
121         strcpy(msg.command, "boot-recovery");
122         set_recovery_message(&msg);     // send recovery message
123         reboot_devices(0);
124         return UNKNOW_REBOOT_MODE;
125 }
126
127 void recovery_mode(void)
128 {
129     printf("%s\n", __func__);
130
131 #if BOOT_NATIVE_LINUX
132     vlx_nand_boot(RECOVERY_PART, CONFIG_BOOTARGS, BACKLIGHT_ON);
133 #else
134     vlx_nand_boot(RECOVERY_PART, NULL, BACKLIGHT_ON);
135 #endif
136 }
137