OTA流程如下:
1. 根据OTAService功能,选择update.zip更新包更新
2. 系统重启进入recovery命令
3. 执行升级命令
4. 重启
5. 开机进入系统
<1>用户通过OTA Service进入recovery mode
<2>系统默认升级包/mnt/sdcard/update.zip
<3>update_directory将"boot-recovery"和"recovery\n"写入BCB
<4>在really_install_package中验证更新包
<5>在try_update_binary执行2进制更新文件"/tmp/update_binary"
<6>利用pipe通信更新recovery UI
<7>finish_recovery() 擦除BCB
<8>reboot
更新脚本. updater-script
系统自带到ota_rom_target_files.py,编译时可以生成updater-script的脚本。需要时修改ota_rom_target_files.py
恢复出场设置流程如下:
1. 写命令来标记wipe内容
2. 系统重启进入recovery命令
3. 执行擦除命令
4. 重启
5. 开机进入系统
<1>用户在系统设置中选择“恢复出厂设置”
<2>系统将"wipe_data"命令写入/cache/recovery/command
<3>系统重启,并进入recover模式(/sbin/recovery)
<4>get_args()将"boot-recovery"和"--wipe_data"写入BCB(bootloader control block)
<5>erase_volume("/data") 格式化(擦除)DATA分区
<6>erase_volume("/cache")格式化(擦除)CACHE分区
<7>finish_recovery() 擦除BCB
<8>reboot
涉及的的关键文件:
重启跟的参数可以看这两个文件
ANDROID_ROOT\system\core\include\cutils\android_reboot.h
#ifndef __CUTILS_ANDROID_REBOOT_H__
#define __CUTILS_ANDROID_REBOOT_H__
__BEGIN_DECLS
/* Commands */
#define ANDROID_RB_RESTART 0xDEAD0001
#define ANDROID_RB_POWEROFF 0xDEAD0002
#define ANDROID_RB_RESTART2 0xDEAD0003
/* Flags */
#define ANDROID_RB_FLAG_NO_SYNC 0x1
#define ANDROID_RB_FLAG_NO_REMOUNT_RO 0x2
int android_reboot(int cmd, int flags, char *arg);
__END_DECLS
#endif /* __CUTILS_ANDROID_REBOOT_H__ */
ANDOID_ROOT\system\core\libcutils\android_reboot.c
#include <unistd.h>
#include <sys/reboot.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <cutils/android_reboot.h>
/* Check to see if /proc/mounts contains any writeable filesystems
* backed by a block device.
* Return true if none found, else return false.
*/
static int remount_ro_done(void)
{
FILE *f;
char mount_dev[256];
char mount_dir[256];
char mount_type[256];
char mount_opts[256];
int mount_freq;
int mount_passno;
int match;
int found_rw_fs = 0;
f = fopen("/proc/mounts", "r");
if (! f) {
/* If we can't read /proc/mounts, just give up */
return 1;
}
do {
match = fscanf(f, "%255s %255s %255s %255s %d %d\n",
mount_dev, mount_dir, mount_type,
mount_opts, &mount_freq, &mount_passno);
mount_dev[255] = 0;
mount_dir[255] = 0;
mount_type[255] = 0;
mount_opts[255] = 0;
if ((match == 6) && !strncmp(mount_dev, "/dev/block", 10) && strstr(mount_opts, "rw")) {
found_rw_fs = 1;
break;
}
} while (match != EOF);
fclose(f);
return !found_rw_fs;
}
/* Remounting filesystems read-only is difficult when there are files
* opened for writing or pending deletes on the filesystem. There is
* no way to force the remount with the mount(2) syscall. The magic sysrq
* 'u' command does an emergency remount read-only on all writable filesystems
* that have a block device (i.e. not tmpfs filesystems) by calling
* emergency_remount(), which knows how to force the remount to read-only.
* Unfortunately, that is asynchronous, and just schedules the work and
* returns. The best way to determine if it is done is to read /proc/mounts
* repeatedly until there are no more writable filesystems mounted on
* block devices.
*/
static void remount_ro(void)
{
int fd, cnt = 0;
/* Trigger the remount of the filesystems as read-only,
* which also marks them clean.
*/
fd = open("/proc/sysrq-trigger", O_WRONLY);
if (fd < 0) {
return;
}
write(fd, "u", 1);
close(fd);
/* Now poll /proc/mounts till it's done */
while (!remount_ro_done() && (cnt < 50)) {
usleep(100000);
cnt++;
}
return;
}
int android_reboot(int cmd, int flags, char *arg)
{
int ret;
if (!(flags & ANDROID_RB_FLAG_NO_SYNC))
sync();
if (!(flags & ANDROID_RB_FLAG_NO_REMOUNT_RO))
remount_ro();
switch (cmd) {
case ANDROID_RB_RESTART:
ret = reboot(RB_AUTOBOOT);
break;
case ANDROID_RB_POWEROFF:
ret = reboot(RB_POWER_OFF);
break;
case ANDROID_RB_RESTART2:
ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
LINUX_REBOOT_CMD_RESTART2, arg);
break;
default:
ret = -1;
}
return ret;
}
recovery内容:
ANDOID_ROOT\bootable\recovery\recovery.cpp
int
main(int argc, char **argv) {
time_t start = time(NULL);
// If these fail, there's not really anywhere to complain...
freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
printf("Starting recovery on %s", ctime(&start));
device_ui_init(&ui_parameters);
ui_init();
ui_set_background(BACKGROUND_ICON_INSTALLING);
load_volume_table();
get_args(&argc, &argv);
int previous_runs = 0;
const char *send_intent = NULL;
const char *update_package = NULL;
int wipe_data = 0, wipe_cache = 0;
int arg;
while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
switch (arg) {
case 'p': previous_runs = atoi(optarg); break;
case 's': send_intent = optarg; break;
case 'u': update_package = optarg; break;
case 'w': wipe_data = wipe_cache = 1; break;
case 'c': wipe_cache = 1; break;
case 't': ui_show_text(1); break;
case '?':
LOGE("Invalid command argument\n");
continue;
}
}
device_recovery_start();
printf("Command:");
for (arg = 0; arg < argc; arg++) {
printf(" \"%s\"", argv[arg]);
}
printf("\n");
if (update_package) {
// For backwards compatibility on the cache partition only, if
// we're given an old 'root' path "CACHE:foo", change it to
// "/cache/foo".
if (strncmp(update_package, "CACHE:", 6) == 0) {
int len = strlen(update_package) + 10;
char* modified_path = malloc(len);
strlcpy(modified_path, "/cache/", len);
strlcat(modified_path, update_package+6, len);
printf("(replacing path \"%s\" with \"%s\")\n",
update_package, modified_path);
update_package = modified_path;
}
}
printf("\n");
property_list(print_property, NULL);
printf("\n");
int status = INSTALL_SUCCESS;
if (update_package != NULL) {
status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE);
if (status == INSTALL_SUCCESS && wipe_cache) {
if (erase_volume("/cache")) {
LOGE("Cache wipe (requested by package) failed.");
}
}
if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
} else if (wipe_data) {
if (device_wipe_data()) status = INSTALL_ERROR;
if (erase_volume("/data")) status = INSTALL_ERROR;
if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
} else if (wipe_cache) {
if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
} else {
status = INSTALL_ERROR; // No command specified
}
if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
if (status != INSTALL_SUCCESS || ui_text_visible()) {
prompt_and_wait();
}
// Otherwise, get ready to boot the main system...
finish_recovery(send_intent);
ui_print("Rebooting...\n");
android_reboot(ANDROID_RB_RESTART, 0, 0);
return EXIT_SUCCESS;
}