xref: /OK3568_Linux_fs/external/rktoolkit/update.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (C) 2010 The Android Open Source Project
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Licensed under the Apache License, Version 2.0 (the "License");
5*4882a593Smuzhiyun  * you may not use this file except in compliance with the License.
6*4882a593Smuzhiyun  * You may obtain a copy of the License at
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *    http://www.apache.org/licenses/LICENSE-2.0
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * Unless required by applicable law or agreed to in writing, software
11*4882a593Smuzhiyun  * distributed under the License is distributed on an "AS IS" BASIS,
12*4882a593Smuzhiyun  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4882a593Smuzhiyun  * See the License for the specific language governing permissions and
14*4882a593Smuzhiyun  * limitations under the License.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /**
18*4882a593Smuzhiyun  * RecoverySystem contains methods for interacting with the Android
19*4882a593Smuzhiyun  * recovery system (the separate partition that can be used to install
20*4882a593Smuzhiyun  * system updates, wipe user data, etc.)
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun //#include <direct.h>
24*4882a593Smuzhiyun #include <linux/reboot.h>
25*4882a593Smuzhiyun //#include <io.h>
26*4882a593Smuzhiyun #include <stdio.h>
27*4882a593Smuzhiyun #include <string.h>
28*4882a593Smuzhiyun #include <sys/reboot.h>
29*4882a593Smuzhiyun #include <sys/stat.h>
30*4882a593Smuzhiyun #include <sys/types.h>
31*4882a593Smuzhiyun #include <unistd.h>
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #include "update_recv/update_recv.h"
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #define LOG_FILE_LEN 512
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #if 0
38*4882a593Smuzhiyun #define RECOVERY_PATH "/tmp/recovery"
39*4882a593Smuzhiyun #define LOG_FILE_PATH "/tmp/recovery/log"
40*4882a593Smuzhiyun #define COMMAND_FILE_PATH "/tmp/recovery/command"
41*4882a593Smuzhiyun #else
42*4882a593Smuzhiyun #define RECOVERY_PATH "/userdata/recovery"
43*4882a593Smuzhiyun #define LOG_FILE_PATH "/userdata/recovery/log"
44*4882a593Smuzhiyun #define COMMAND_FILE_PATH "/userdata/recovery/command"
45*4882a593Smuzhiyun #endif
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #define SD_UPDATE_FILE "/sdcard/update.img"
48*4882a593Smuzhiyun #define DATA_UPDATE_FILE "/userdata/update.img"
49*4882a593Smuzhiyun #define MISC_FILE_PATH "/dev/block/by-name/misc"
50*4882a593Smuzhiyun #define MISC_MSG_OFFSET 16 * 1024
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /* Bootloader Message (2-KiB)
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * This structure describes the content of a block in flash
55*4882a593Smuzhiyun  * that is used for recovery and the bootloader to talk to
56*4882a593Smuzhiyun  * each other.
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  * The command field is updated by linux when it wants to
59*4882a593Smuzhiyun  * reboot into recovery or to update radio or bootloader firmware.
60*4882a593Smuzhiyun  * It is also updated by the bootloader when firmware update
61*4882a593Smuzhiyun  * is complete (to boot into recovery for any final cleanup)
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * The status field is written by the bootloader after the
64*4882a593Smuzhiyun  * completion of an "update-radio" or "update-hboot" command.
65*4882a593Smuzhiyun  *
66*4882a593Smuzhiyun  * The recovery field is only written by linux and used
67*4882a593Smuzhiyun  * for the system to send a message to recovery or the
68*4882a593Smuzhiyun  * other way around.
69*4882a593Smuzhiyun  *
70*4882a593Smuzhiyun  * The stage field is written by packages which restart themselves
71*4882a593Smuzhiyun  * multiple times, so that the UI can reflect which invocation of the
72*4882a593Smuzhiyun  * package it is.  If the value is of the format "#/#" (eg, "1/3"),
73*4882a593Smuzhiyun  * the UI will add a simple indicator of that status.
74*4882a593Smuzhiyun  *
75*4882a593Smuzhiyun  * We used to have slot_suffix field for A/B boot control metadata in
76*4882a593Smuzhiyun  * this struct, which gets unintentionally cleared by recovery or
77*4882a593Smuzhiyun  * uncrypt. Move it into struct bootloader_message_ab to avoid the
78*4882a593Smuzhiyun  * issue.
79*4882a593Smuzhiyun  */
80*4882a593Smuzhiyun struct android_bootloader_message {
81*4882a593Smuzhiyun 	char command[32];
82*4882a593Smuzhiyun 	char status[32];
83*4882a593Smuzhiyun 	char recovery[768];
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	/* The 'recovery' field used to be 1024 bytes.	It has only ever
86*4882a593Smuzhiyun 	 * been used to store the recovery command line, so 768 bytes
87*4882a593Smuzhiyun 	 * should be plenty.  We carve off the last 256 bytes to store the
88*4882a593Smuzhiyun 	 * stage string (for multistage packages) and possible future
89*4882a593Smuzhiyun 	 * expansion. */
90*4882a593Smuzhiyun 	char stage[32];
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	/* The 'reserved' field used to be 224 bytes when it was initially
93*4882a593Smuzhiyun 	 * carved off from the 1024-byte recovery field. Bump it up to
94*4882a593Smuzhiyun 	 * 1184-byte so that the entire bootloader_message struct rounds up
95*4882a593Smuzhiyun 	 * to 2048-byte. */
96*4882a593Smuzhiyun 	char reserved[1184];
97*4882a593Smuzhiyun };
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun /**
102*4882a593Smuzhiyun  * Reboot into the recovery system with the supplied argument.
103*4882a593Smuzhiyun  * @param arg to pass to the recovery utility.
104*4882a593Smuzhiyun  */
bootCommand(char * arg)105*4882a593Smuzhiyun static void bootCommand(char *arg){
106*4882a593Smuzhiyun 	FILE *command_file;
107*4882a593Smuzhiyun 	FILE *log_file;
108*4882a593Smuzhiyun 	FILE *misc_file;
109*4882a593Smuzhiyun 	char blank[LOG_FILE_LEN];
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	if(!arg) return;
112*4882a593Smuzhiyun 	printf("command: %s\n", arg);
113*4882a593Smuzhiyun 	mkdir(RECOVERY_PATH,0775);
114*4882a593Smuzhiyun 	if((command_file = fopen(COMMAND_FILE_PATH,"wb")) == NULL){
115*4882a593Smuzhiyun 		printf("Open command file error.\n");
116*4882a593Smuzhiyun 		return;
117*4882a593Smuzhiyun 	}
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	if((log_file = fopen(LOG_FILE_PATH,"wb")) == NULL){
120*4882a593Smuzhiyun 		printf("Open log file error.\n");
121*4882a593Smuzhiyun 		return;
122*4882a593Smuzhiyun 	}
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	if((misc_file = fopen(MISC_FILE_PATH,"wb")) == NULL){
125*4882a593Smuzhiyun 		printf("Open misc file error.\n");
126*4882a593Smuzhiyun 		return;
127*4882a593Smuzhiyun 	}
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	printf("update: write command to command file: ");
130*4882a593Smuzhiyun 	fwrite(arg, strlen(arg), 1, command_file);
131*4882a593Smuzhiyun 	fwrite("\n", 1, 1, command_file);
132*4882a593Smuzhiyun 	fclose(command_file);
133*4882a593Smuzhiyun 	printf("done\n");
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	printf("update: write command to misc file: ");
136*4882a593Smuzhiyun 	fseek(misc_file, MISC_MSG_OFFSET, SEEK_SET);
137*4882a593Smuzhiyun 	struct android_bootloader_message msg;
138*4882a593Smuzhiyun 	memset(&msg, 0, sizeof(msg));
139*4882a593Smuzhiyun 	char recovery_str[] = "recovery\n";
140*4882a593Smuzhiyun 	strcpy(msg.command, "boot-recovery");
141*4882a593Smuzhiyun 	strcpy(msg.recovery, recovery_str);
142*4882a593Smuzhiyun 	memcpy(msg.recovery + strlen(recovery_str), arg, ((strlen(arg) > sizeof(msg.recovery))? sizeof(msg.recovery) : strlen(arg)));
143*4882a593Smuzhiyun 	msg.recovery[strlen(msg.recovery) + 1] = '\n';
144*4882a593Smuzhiyun 	//strlcat(msg.recovery, update_file, sizeof(msg.recovery));
145*4882a593Smuzhiyun 	//strlcat(msg.recovery, "\n", sizeof(msg.recovery));
146*4882a593Smuzhiyun 	//strlcpy(msg.systemFlag, "false", sizeof(msg.systemFlag));
147*4882a593Smuzhiyun 	fwrite(&msg, sizeof(msg), 1, misc_file);
148*4882a593Smuzhiyun 	fclose(misc_file);
149*4882a593Smuzhiyun 	printf("done\n");
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	memset(blank, 0, LOG_FILE_LEN);
152*4882a593Smuzhiyun 	fwrite(blank, LOG_FILE_LEN, 1, log_file);
153*4882a593Smuzhiyun 	fclose(log_file);
154*4882a593Smuzhiyun 	printf("update: reboot!\n");
155*4882a593Smuzhiyun 	//reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
156*4882a593Smuzhiyun 	//		 LINUX_REBOOT_CMD_RESTART2, "recovery");
157*4882a593Smuzhiyun 	sync();
158*4882a593Smuzhiyun 	reboot(RB_AUTOBOOT);
159*4882a593Smuzhiyun 	return;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun  /**
164*4882a593Smuzhiyun  * Reboots the device in order to install the given update
165*4882a593Smuzhiyun  * package.
166*4882a593Smuzhiyun  * Requires the {@link android.Manifest.permission#REBOOT} permission.
167*4882a593Smuzhiyun  *
168*4882a593Smuzhiyun  * @param packageFile  the update package to install.  Must be on
169*4882a593Smuzhiyun  * a partition mountable by recovery.  (The set of partitions
170*4882a593Smuzhiyun  * known to recovery may vary from device to device.  Generally,
171*4882a593Smuzhiyun  * /cache and /data are safe.)
172*4882a593Smuzhiyun  */
installPackage(char * update_file)173*4882a593Smuzhiyun static void installPackage(char *update_file){
174*4882a593Smuzhiyun 	char arg[512];
175*4882a593Smuzhiyun 	char *str_update_package = "--update_package=";
176*4882a593Smuzhiyun 	int str_update_package_len = strlen(str_update_package);
177*4882a593Smuzhiyun 	int str_update_file_len = strlen(update_file);
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	memset(arg, 0, 512);
180*4882a593Smuzhiyun 	strcpy(arg, str_update_package);
181*4882a593Smuzhiyun 	strcpy(arg + str_update_package_len, update_file);
182*4882a593Smuzhiyun 	arg[str_update_package_len + str_update_file_len] = 0;
183*4882a593Smuzhiyun 	bootCommand(arg);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun 
sdUpdate()186*4882a593Smuzhiyun static void sdUpdate(){
187*4882a593Smuzhiyun 	installPackage(SD_UPDATE_FILE);
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun 
dataUpdate()190*4882a593Smuzhiyun static void dataUpdate(){
191*4882a593Smuzhiyun 	installPackage(DATA_UPDATE_FILE);
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun /**
195*4882a593Smuzhiyun  * Reboots the device and wipes the user data partition.  This is
196*4882a593Smuzhiyun  * sometimes called a "factory reset", which is something of a
197*4882a593Smuzhiyun  * misnomer because the system partition is not restored to its
198*4882a593Smuzhiyun  * factory state.
199*4882a593Smuzhiyun  * Requires the {@link android.Manifest.permission#REBOOT} permission.
200*4882a593Smuzhiyun  *
201*4882a593Smuzhiyun  * @param context  the Context to use
202*4882a593Smuzhiyun  *
203*4882a593Smuzhiyun  */
rebootWipeUserData()204*4882a593Smuzhiyun void rebootWipeUserData(){
205*4882a593Smuzhiyun 	printf("update: --wipe_all\n");
206*4882a593Smuzhiyun 	bootCommand("--wipe_all");
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
rebootUpdate(char * path)209*4882a593Smuzhiyun int rebootUpdate(char *path){
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	if(path){
212*4882a593Smuzhiyun 		printf("find %s\n", path);
213*4882a593Smuzhiyun 		installPackage(path);
214*4882a593Smuzhiyun 		return 0;
215*4882a593Smuzhiyun 	}
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	if(access(DATA_UPDATE_FILE,F_OK) == -1){
218*4882a593Smuzhiyun 		printf("%s does not exist! try to use %s to update\n",
219*4882a593Smuzhiyun 			DATA_UPDATE_FILE, SD_UPDATE_FILE);
220*4882a593Smuzhiyun 		if(access(SD_UPDATE_FILE,F_OK) == -1){
221*4882a593Smuzhiyun 			printf("%s does not exist!\n", SD_UPDATE_FILE);
222*4882a593Smuzhiyun 			return -1;
223*4882a593Smuzhiyun 		}
224*4882a593Smuzhiyun 		printf("find %s\n", SD_UPDATE_FILE);
225*4882a593Smuzhiyun 		installPackage(SD_UPDATE_FILE);
226*4882a593Smuzhiyun 		return 0;
227*4882a593Smuzhiyun 	}
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	printf("find %s\n", DATA_UPDATE_FILE);
230*4882a593Smuzhiyun 	installPackage(DATA_UPDATE_FILE);
231*4882a593Smuzhiyun 	return 0;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
main(int argc,char ** argv)234*4882a593Smuzhiyun int main(int argc, char** argv){
235*4882a593Smuzhiyun 	char* partition_name = "recovery";
236*4882a593Smuzhiyun 	printf("update: Rockchip Update Tool\n");
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	if(argc == 1) {
239*4882a593Smuzhiyun 		rebootWipeUserData();
240*4882a593Smuzhiyun 	} else if(argc == 2){
241*4882a593Smuzhiyun 		if(!strcmp(argv[1], "ota") || !strcmp(argv[1], "update"))
242*4882a593Smuzhiyun 			rebootUpdate(0);
243*4882a593Smuzhiyun 		else if(!strcmp(argv[1], "factory") || !strcmp(argv[1], "reset"))
244*4882a593Smuzhiyun 			rebootWipeUserData();
245*4882a593Smuzhiyun 		else  return -1;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 		return 0;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	} else if(argc == 3){
250*4882a593Smuzhiyun 		if(!strcmp(argv[1], "ota") || !strcmp(argv[1], "update")) {
251*4882a593Smuzhiyun 			if(argv[2]) {
252*4882a593Smuzhiyun 				int ret;
253*4882a593Smuzhiyun 				ret = WriteFwData(argv[2], partition_name);
254*4882a593Smuzhiyun 				if (ret < 0) {
255*4882a593Smuzhiyun 					if (ret == -1) {
256*4882a593Smuzhiyun 						printf(" Update partition %s fail \n", partition_name);
257*4882a593Smuzhiyun 						//means no find recovery partition in update.img
258*4882a593Smuzhiyun 						//return -1;
259*4882a593Smuzhiyun 					} else if (ret == -2) {
260*4882a593Smuzhiyun 						printf("Some errors happen, update process break...\n");
261*4882a593Smuzhiyun 						return -1;
262*4882a593Smuzhiyun 					}
263*4882a593Smuzhiyun 				} else {
264*4882a593Smuzhiyun 					if (!CheckFwData(argv[2], partition_name)){
265*4882a593Smuzhiyun 						printf(" Check partition %s fail \n", partition_name);
266*4882a593Smuzhiyun 						return -1;
267*4882a593Smuzhiyun 					}
268*4882a593Smuzhiyun 				}
269*4882a593Smuzhiyun 				return rebootUpdate(argv[2]);
270*4882a593Smuzhiyun 			}
271*4882a593Smuzhiyun 		}
272*4882a593Smuzhiyun 	}
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	return -1;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun 
277