Exemplo n.º 1
0
static int
really_install_package(const char *path)
{
    ui_set_background(BACKGROUND_ICON_INSTALLING);
    ui_print("正在查找刷机包...\n");
    ui_show_indeterminate_progress();
    LOGI("Update location: %s\n", path);

    if (ensure_path_mounted(path) != 0) {
        LOGE("Can't mount %s\n", path);
        return INSTALL_CORRUPT;
    }

    ui_print("正在打开刷机包...\n");

    int err;

    if (signature_check_enabled) {
        int numKeys;
        RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
        if (loadedKeys == NULL) {
            LOGE("Failed to load keys\n");
            return INSTALL_CORRUPT;
        }
        LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);

        // Give verification half the progress bar...
        ui_print("正在校验刷机包签名...\n");
        ui_show_progress(
                VERIFICATION_PROGRESS_FRACTION,
                VERIFICATION_PROGRESS_TIME);

        err = verify_file(path, loadedKeys, numKeys);
        free(loadedKeys);
        LOGI("verify_file returned %d\n", err);
        if (err != VERIFY_SUCCESS) {
            LOGE("signature verification failed\n");
            ui_show_text(1);
            if (!confirm_selection("安装未签名刷机包?", "是 - 安装未签名刷机包"))
                return INSTALL_CORRUPT;
        }
    }

    /* Try to open the package.
     */
    ZipArchive zip;
    err = mzOpenZipArchive(path, &zip);
    if (err != 0) {
        LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
        return INSTALL_CORRUPT;
    }

    /* Verify and install the contents of the package.
     */
    ui_print("正在安装刷机包...\n");
    return try_update_binary(path, &zip);
}
Exemplo n.º 2
0
int
install_package(const char *path)
{
    ui_set_background(BACKGROUND_ICON_INSTALLING);
    ui_print("Finding update package...\n");
    ui_show_indeterminate_progress();
    LOGI("Update location: %s\n", path);

    if (ensure_path_mounted(path) != 0) {
        LOGE("Can't mount %s\n", path);
        return INSTALL_CORRUPT;
    }

    ui_print("Opening update package...\n");

	//TODO: put this back at some point
	/*
    int numKeys;
    RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
    if (loadedKeys == NULL) {
        LOGE("Failed to load keys\n");
        return INSTALL_CORRUPT;
    }
    LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);

    // Give verification half the progress bar...
    ui_print("Verifying update package...\n");
    ui_show_progress(
            VERIFICATION_PROGRESS_FRACTION,
            VERIFICATION_PROGRESS_TIME);
	*/
    int err;
	/*
    err = verify_file(path, loadedKeys, numKeys);
    free(loadedKeys);
    LOGI("verify_file returned %d\n", err);
    if (err != VERIFY_SUCCESS) {
        LOGE("signature verification failed\n");
        return INSTALL_CORRUPT;
    }
	*/
	
    /* Try to open the package.
     */
    ZipArchive zip;
    err = mzOpenZipArchive(path, &zip);
    if (err != 0) {
        LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
        return INSTALL_CORRUPT;
    }

    /* Verify and install the contents of the package.
     */
    ui_print("Installing update...\n");
    return try_update_binary(path, &zip);
}
Exemplo n.º 3
0
/*
 * Function    : libaroma_zip
 * Return Value: LIBAROMA_ZIP
 * Descriptions: open zip file
 */
LIBAROMA_ZIP libaroma_zip(
    const char * filename) {
  LIBAROMA_ZIP zip = (LIBAROMA_ZIP) malloc(sizeof(ZipArchive));
  if (mzOpenZipArchive(filename, (ZipArchive *) zip) != 0) {
    ALOGW("libaroma_zip mzOpenZipArchive error (%s)", filename);
    free(zip);
    return NULL;
  }
  return zip;
} /* End of libaroma_zip */
Exemplo n.º 4
0
int
install_package(const char *path)
{
    ui_set_background(BACKGROUND_ICON_INSTALLING);
    ui_print("Finding update package...\n");
    ui_show_indeterminate_progress();
    LOGI("Update location: %s\n", path);

/*
    if (strcmp(root_path, ":") == 0) {
	    if (ensure_root_path_mounted(root_path) != 0) {
	        LOGE("Can't mount %s\n", root_path);
	        return INSTALL_CORRUPT;
	    }

	    if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
	        LOGE("Bad path %s\n", root_path);
	        return INSTALL_CORRUPT;
	    }
    } else {
	    path = strndup(path, root_path);
    }
*/

    ui_print("Opening update package...\n");
    LOGI("Update file path: %s\n", path);

/*
    int numKeys;
    RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
    if (loadedKeys == NULL) {
        LOGE("Failed to load keys\n");
        return INSTALL_CORRUPT;
    }
    LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
*/

    /* Try to open the package.
     */
    ZipArchive zip;
    int err = mzOpenZipArchive(path, &zip);
    if (err != 0) {
        LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
        return INSTALL_CORRUPT;
    }

    /* Verify and install the contents of the package.
     */
    // int status = handle_update_package(path, &zip, loadedKeys, numKeys);
    int status = handle_update_package(path, &zip);
    mzCloseZipArchive(&zip);
    // free(loadedKeys);
    return status;
}
extern "C" int TWinstall_zip(const char* path, int* wipe_cache) {
	int ret_val, zip_verify = 1, md5_return, key_count;
	twrpDigest md5sum;
	string strpath = path;
	ZipArchive Zip;

	if (strcmp(path, "error") == 0) {
		LOGERR("Failed to get adb sideload file: '%s'\n", path);
		return INSTALL_CORRUPT;
	}

	gui_print("Installing '%s'...\n", path);
	if (strlen(path) < 9 || strncmp(path, "/sideload", 9) != 0) {
		gui_print("Checking for MD5 file...\n");
		md5sum.setfn(strpath);
		md5_return = md5sum.verify_md5digest();
		if (md5_return == -2) { // md5 did not match
			LOGERR("Aborting zip install\n");
			return INSTALL_CORRUPT;
		}
	}

#ifndef TW_OEM_BUILD
	DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
#endif
	DataManager::SetProgress(0);

	MemMapping map;
	if (sysMapFile(path, &map) != 0) {
		LOGERR("Failed to sysMapFile '%s'\n", path);
        return -1;
    }

	if (zip_verify) {
		gui_print("Verifying zip signature...\n");
		ret_val = verify_file(map.addr, map.length);
		if (ret_val != VERIFY_SUCCESS) {
			LOGERR("Zip signature verification failed: %i\n", ret_val);
			sysReleaseMap(&map);
			return -1;
		} else {
			gui_print("Zip signature verified successfully.\n");
		}
	}
	ret_val = mzOpenZipArchive(map.addr, map.length, &Zip);
	if (ret_val != 0) {
		LOGERR("Zip file is corrupt!\n", path);
		sysReleaseMap(&map);
		return INSTALL_CORRUPT;
	}
	ret_val = Run_Update_Binary(path, &Zip, wipe_cache);
	sysReleaseMap(&map);
	return ret_val;
}
bool MROMInstaller::extractDir(const std::string& name, const std::string& dest)
{
	ZipArchive zip;

#if (ANDROID_VERSION >= 5)
	MemMapping map;
	if (sysMapFile(m_file.c_str(), &map) != 0) {
		LOGERR("Failed to sysMapFile '%s'\n", m_file.c_str());
		return false;
	}

	if (mzOpenZipArchive(map.addr, map.length, &zip) != 0)
#else
	if (mzOpenZipArchive(m_file.c_str(), &zip) != 0)
#endif
	{
		gui_print("Failed to open ZIP file %s\n", m_file.c_str());
#if (ANDROID_VERSION >= 5)
		sysReleaseMap(&map);
#endif
		return false;
	}

	// To create a consistent system image, never use the clock for timestamps.
    struct utimbuf timestamp = { 1217592000, 1217592000 };  // 8/1/2008 default
	bool success = mzExtractRecursive(&zip, name.c_str(), dest.c_str(), MZ_EXTRACT_FILES_ONLY, &timestamp, NULL, NULL, NULL);

	mzCloseZipArchive(&zip);
#if (ANDROID_VERSION >= 5)
	sysReleaseMap(&map);
#endif

	if(!success)
	{
		gui_print("Failed to extract dir %s from zip %s\n", name.c_str(), m_file.c_str());
		return false;
	}
	return true;
}
static int
really_install_package(const char *path, int* wipe_cache)
{
    ui->SetBackground(RecoveryUI::INSTALLING);
    ui->Print("Finding update package...\n");
    ui->SetProgressType(RecoveryUI::INDETERMINATE);
    LOGI("Update location: %s\n", path);

    if (ensure_path_mounted(path) != 0) {
        LOGE("Can't mount %s\n", path);
        return INSTALL_CORRUPT;
    }

    ui->Print("Opening update package...\n");

    int numKeys;
    RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
    if (loadedKeys == NULL) {
        LOGE("Failed to load keys\n");
        return INSTALL_CORRUPT;
    }
    LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);

    // Give verification half the progress bar...
    ui->Print("Verifying update package...\n");
    ui->SetProgressType(RecoveryUI::DETERMINATE);
    ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);

    int err;
    err = verify_file(path, loadedKeys, numKeys);
    free(loadedKeys);
    LOGI("verify_file returned %d\n", err);
    if (err != VERIFY_SUCCESS) {
        LOGE("signature verification failed\n");
        return INSTALL_CORRUPT;
    }

    /* Try to open the package.
     */
    ZipArchive zip;
    err = mzOpenZipArchive(path, &zip);
    if (err != 0) {
        LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
        return INSTALL_CORRUPT;
    }

    /* Verify and install the contents of the package.
     */
    ui->Print("Installing update...\n");
    return try_update_binary(path, &zip, wipe_cache);
}
Exemplo n.º 8
0
int open_ota_package(const char *root_path)
{
	int status = INSTALL_SUCCESS;
	int recovery_status = 0;
	ui_print("Opening OTA upgrade package...\n");

	char *c = root_path;
	const char *p;
	int len = strlen(root_path);
	int i;

	c = c + len-2;

	for(i=1;i<len-1;i++)
	{
		--c;
	}
	c[len-1] = '\0';
	p = c;

    int err;
    ZipArchive zip;
    err = mzOpenZipArchive(p, &zip);
    if (err != 0) {
        ui_print("Can't open '%s'\n(%s)\n", p, err != -1 ? strerror(err) : "bad");
        status = INSTALL_CORRUPT;
		goto exit;
    }
	
	ui_print("start update package file\n");
    status = handle_update_package(p, &zip);
	ui_print("install_success!!!\n");
    mzCloseZipArchive(&zip);

exit:
	if (status != INSTALL_SUCCESS)
		recovery_status = 1;
	else
		recovery_status = 0;

    return status;

}
Exemplo n.º 9
0
bool MROMInstaller::extractFile(const std::string& name, const std::string& dest)
{
	ZipArchive zip;
	MemMapping map;
	if (sysMapFile(m_file.c_str(), &map) != 0) {
		LOGERR("Failed to sysMapFile '%s'\n", m_file.c_str());
		return false;
	}

	if (mzOpenZipArchive(map.addr, map.length, &zip) != 0)
	{
		gui_print("Failed to open ZIP file %s\n", m_file.c_str());
		sysReleaseMap(&map);
		return false;
	}

	bool res = false;
	FILE* f = NULL;
	const ZipEntry* entry = mzFindZipEntry(&zip, name.c_str());
	if (entry == NULL)
	{
		gui_print("Could not find file %s in zip %s\n", name.c_str(), m_file.c_str());
		goto exit;
	}

	f = fopen(dest.c_str(), "wb");
	if(!f)
	{
		gui_print("Could not open dest file %s for writing!\n", dest.c_str());
		goto exit;
	}

	res = mzExtractZipEntryToFile(&zip, entry, fileno(f));
	if(!res)
		gui_print("Failed to extract file %s from the ZIP!", name.c_str());

	fclose(f);
exit:
	mzCloseZipArchive(&zip);
	sysReleaseMap(&map);
	return res;
}
Exemplo n.º 10
0
int extract_deltaupdate_binary(const char *path)
{
    int err;
    ZipArchive zip;

    // Try to open the package.
    err = mzOpenZipArchive(path, &zip);
    if (err != 0) {
        LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
        return INSTALL_ERROR;
    }

    const ZipEntry* dua_entry =
            mzFindZipEntry(&zip, ASSUMED_DELTAUPDATE_BINARY_NAME);
    if (dua_entry == NULL) {
        mzCloseZipArchive(&zip);
       LOGE("Can't find %s\n", ASSUMED_DELTAUPDATE_BINARY_NAME);
        return INSTALL_ERROR;
    }

    char* deltaupdate_agent = RUN_DELTAUPDATE_AGENT;
    unlink(deltaupdate_agent);
    int fd = creat(deltaupdate_agent, 0755);
    if (fd < 0) {
        mzCloseZipArchive(&zip);
        LOGE("Can't make %s\n", deltaupdate_agent);
        return INSTALL_ERROR;
    }

    bool ok = mzExtractZipEntryToFile(&zip, dua_entry, fd);
    close(fd);
    mzCloseZipArchive(&zip);

    if (!ok) {
        LOGE("Can't copy %s\n", ASSUMED_DELTAUPDATE_BINARY_NAME);
        return INSTALL_ERROR;
    }

    return 0;
}
Exemplo n.º 11
0
int
install_package(const char *root_path)
{
    ui_set_background(BACKGROUND_ICON_INSTALLING);
    ui_print("Finding update package...\n");
    LOGI("Update location: %s\n", root_path);

    if (ensure_root_path_mounted(root_path) != 0) {
        LOGE("Can't mount %s\n", root_path);
        return INSTALL_CORRUPT;
    }

    char path[PATH_MAX] = "";
    if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
        LOGE("Bad path %s\n", root_path);
        return INSTALL_CORRUPT;
    }

    ui_print("Opening update package...\n");
    LOGI("Update file path: %s\n", path);

    /* Try to open the package.
     */
    int err;
    ZipArchive zip;
    err = mzOpenZipArchive(path, &zip);
    if (err != 0) {
        LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
        return INSTALL_CORRUPT;
    }

    /* Verify and install the contents of the package.
     */
    int status = handle_update_package(path, &zip);
    mzCloseZipArchive(&zip);
    return status;
}
Exemplo n.º 12
0
int
install_package(const char *root_path)
{
    ui_set_background(BACKGROUND_ICON_INSTALLING);
    ui_print("查找升级包中...\n");
    LOGI("升级点: %s\n", root_path);

    if (ensure_root_path_mounted(root_path) != 0) {
        LOGE("无法挂载 %s\n", root_path);
        return INSTALL_CORRUPT;
    }

    char path[PATH_MAX] = "";
    if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
        LOGE("错误路径 %s\n", root_path);
        return INSTALL_CORRUPT;
    }

    ui_print("打开升级包中...\n");
    LOGI("升级文件路径: %s\n", path);

    /* Try to open the package.
     */
    int err;
    ZipArchive zip;
    err = mzOpenZipArchive(path, &zip);
    if (err != 0) {
        LOGE("无法打开%s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
        return INSTALL_CORRUPT;
    }

    /* Verify and install the contents of the package.
     */
    int status = handle_update_package(path, &zip);
    mzCloseZipArchive(&zip);
    return status;
}
Exemplo n.º 13
0
int
install_package(const char *root_path)
{
    int err;

    ui_set_background(BACKGROUND_ICON_INSTALLING);
    ui_print("Finding update package...\n");
    ui_show_indeterminate_progress();
    LOGI("Update location: %s\n", root_path);

    if (ensure_root_path_mounted(root_path) != 0) {
        LOGE("Can't mount %s\n", root_path);
        return INSTALL_CORRUPT;
    }

    char path[PATH_MAX] = "";
    if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
        LOGE("Bad path %s\n", root_path);
        return INSTALL_CORRUPT;
    }

    ui_print("Opening update package...\n");
    LOGI("Update file path: %s\n", path);

    if (signature_check_enabled) {
        int numKeys;
        RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
        if (loadedKeys == NULL) {
            LOGE("Failed to load keys\n");
            return INSTALL_CORRUPT;
        }
        LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);

        // Give verification half the progress bar...
        ui_print("Verifying update package...\n");
        ui_show_progress(
            VERIFICATION_PROGRESS_FRACTION,
            VERIFICATION_PROGRESS_TIME);

        err = verify_file(path, loadedKeys, numKeys);
        free(loadedKeys);
        LOGI("verify_file returned %d\n", err);
        if (err != VERIFY_SUCCESS) {
            LOGE("signature verification failed\n");
            LOGE("may need to disable signature verification\n");
            return INSTALL_CORRUPT;
        }
    }
    /* Try to open the package.
     */
    ZipArchive zip;
    err = mzOpenZipArchive(path, &zip);
    if (err != 0) {
        LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
        return INSTALL_CORRUPT;
    }

    /* Verify and install the contents of the package.
     */
    int status = handle_update_package(path, &zip);
    mzCloseZipArchive(&zip);
    return status;
}
Exemplo n.º 14
0
int
install_package(const char *root_path)
{
	recovery_status = 0;
	int status = INSTALL_SUCCESS;
    ui_set_background(BACKGROUND_ICON_INSTALLING);
    ui_print("Finding update package...\n");
    ui_show_indeterminate_progress();

    if (ensure_root_path_mounted(root_path) != 0) {
        ui_print("Can't mount %s\n", root_path);
        status = INSTALL_CORRUPT;
		goto exit;
    }

    char path[PATH_MAX] = "";
    if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
        ui_print("Bad path %s\n", root_path);
        status = INSTALL_CORRUPT;
		goto exit;
    }
	printf("package name = '%s'\t path_len= %d\n",path,strlen(path));
    ui_print("Opening update package...\n");
#if 0
    int numKeys;
    RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
    if (loadedKeys == NULL) {
        LOGE("Failed to load keys\n");
        return INSTALL_CORRUPT;
    }
    LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);

    // Give verification half the progress bar...
    ui_print("Verifying update package...\n");
    ui_show_progress(
            VERIFICATION_PROGRESS_FRACTION,
            VERIFICATION_PROGRESS_TIME);

    int err;
    err = verify_file(path, loadedKeys, numKeys);
    free(loadedKeys);
    LOGI("verify_file returned %d\n", err);
    if (err != VERIFY_SUCCESS) {
        LOGE("signature verification failed\n");
        return INSTALL_CORRUPT;
    }
#endif

    /* Try to open the package.
     */
    int err;
    ZipArchive zip;
    err = mzOpenZipArchive(path, &zip);
    if (err != 0) {
        ui_print("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
        status = INSTALL_CORRUPT;
		goto exit;
    }

	/*
	 * Verify hw version 
	 */
	/*
	if (check_hw_version(&zip)) {
		ui_print("HW version doesn't match!!!");
		status = INSTALL_CORRUPT;
    	mzCloseZipArchive(&zip);
		goto exit;
	}
*/
    /* Verify and install the contents of the package.
     */
	ui_print("start update package file\n");
    status = handle_update_package(path, &zip);
	ui_print("install_success!!!\n");
    mzCloseZipArchive(&zip);

exit:
	ensure_root_path_unmounted(root_path);
	if (status != INSTALL_SUCCESS)
		recovery_status = 1;
	else
		recovery_status = 0;

    return status;
}
Exemplo n.º 15
0
static int
really_install_package(const char *path, bool* wipe_cache, bool needs_mount)
{
    ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
    ui->Print("Finding update package...\n");
    // Give verification half the progress bar...
    ui->SetProgressType(RecoveryUI::DETERMINATE);
    ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
    LOGI("Update location: %s\n", path);

    // Map the update package into memory.
    ui->Print("Opening update package...\n");

    if (path && needs_mount) {
        if (path[0] == '@') {
            ensure_path_mounted(path+1);
        } else {
            ensure_path_mounted(path);
        }
    }

    MemMapping map;
    if (sysMapFile(path, &map) != 0) {
        LOGE("failed to map file\n");
        return INSTALL_CORRUPT;
    }

    int numKeys;
    Certificate* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
    if (loadedKeys == NULL) {
        LOGE("Failed to load keys\n");
        return INSTALL_CORRUPT;
    }
    LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);

    ui->Print("Verifying update package...\n");

    int err;
    err = verify_file(map.addr, map.length, loadedKeys, numKeys);
    free(loadedKeys);
    LOGI("verify_file returned %d\n", err);
    if (err != VERIFY_SUCCESS) {
        LOGE("signature verification failed\n");
        sysReleaseMap(&map);
        return INSTALL_CORRUPT;
    }

    /* Try to open the package.
     */
    ZipArchive zip;
    err = mzOpenZipArchive(map.addr, map.length, &zip);
    if (err != 0) {
        LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
        sysReleaseMap(&map);
        return INSTALL_CORRUPT;
    }

    /* Verify and install the contents of the package.
     */
    ui->Print("Installing update...\n");
    ui->SetEnableReboot(false);
    int result = try_update_binary(path, &zip, wipe_cache);
    ui->SetEnableReboot(true);
    ui->Print("\n");

    sysReleaseMap(&map);

    return result;
}
Exemplo n.º 16
0
int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate)
{
    int ret_val = 0;

	DataManager::SetValue("ui_progress", 0);

    if (filename.empty())
    {
        LOGE("No file specified.\n");
        return -1;
    }

    // We're going to jump to this page first, like a loading page
    gui_changePage(pageName);

    int fd = -1;
    ZipArchive zip;

    if (mzOpenZipArchive(filename.c_str(), &zip))
    {
        LOGE("Unable to open zip file.\n");
        return -1;
    }

    // Check the zip to see if it has a custom installer theme
	const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
    if (twrp != NULL)
    {
        unlink("/tmp/twrp.zip");
        fd = creat("/tmp/twrp.zip", 0666);
    }
    if (fd >= 0 && twrp != NULL && 
        mzExtractZipEntryToFile(&zip, twrp, fd) && 
        !PageManager::LoadPackage("install", "/tmp/twrp.zip"))
    {
        mzCloseZipArchive(&zip);
        PageManager::SelectPackage("install");
        gui_changePage("main");
    }
    else
    {
        // In this case, we just use the default page
        mzCloseZipArchive(&zip);
        gui_changePage(pageName);
    }
    if (fd >= 0)
        close(fd);

	if (simulate) {
		simulate_progress_bar();
	} else {
		ret_val = install_zip_package(filename.c_str());

		// Now, check if we need to ensure TWRP remains installed...
		struct stat st;
		if (stat("/sbin/installTwrp", &st) == 0)
		{
			DataManager::SetValue("tw_operation", "Configuring TWRP");
			DataManager::SetValue("tw_partition", "");
			ui_print("Configuring TWRP...\n");
			if (__system("/sbin/installTwrp reinstall") < 0)
			{
				ui_print("Unable to configure TWRP with this kernel.\n");
			}
		}
	}

    // Done
    DataManager::SetValue("ui_progress", 100);
    DataManager::SetValue("ui_progress", 0);
    return ret_val;
}
Exemplo n.º 17
0
static int
really_install_package(const char *path, int* wipe_cache)
{
    ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
#if 0 //wschen 2012-07-10
    ui->Print("Finding update package...\n");
#else
    LOGI("Finding update package...\n");
#endif
    ui->SetProgressType(RecoveryUI::INDETERMINATE);
    LOGI("Update location: %s\n", path);

    if (ensure_path_mounted(path) != 0) {
        LOGE("Can't mount %s\n", path);
#if 0 //wschen 2012-07-10
        return INSTALL_CORRUPT;
#else
        reset_mark_block();
        return INSTALL_NO_SDCARD;
#endif
    }

#if 0 //wschen 2012-07-10
    ui->Print("Opening update package...\n");
#else
    LOGI("Opening update package...\n");
#endif

    int numKeys;
    RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
    if (loadedKeys == NULL) {
        LOGE("Failed to load keys\n");
#if 0 //wschen 2012-07-10
        return INSTALL_CORRUPT;
#else
        reset_mark_block();
        return INSTALL_NO_KEY;
#endif
    }
    LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);

    // Give verification half the progress bar...
#if 0 //wschen 2012-07-10
    ui->Print("Verifying update package...\n");
#else
    LOGI("Verifying update package...\n");
#endif
    ui->SetProgressType(RecoveryUI::DETERMINATE);
    ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);

    int err;
    err = verify_file(path, loadedKeys, numKeys);
    free(loadedKeys);
    LOGI("verify_file returned %d\n", err);
    if (err != VERIFY_SUCCESS) {
        LOGE("signature verification failed\n");
#if 0 //wschen 2012-07-10
        return INSTALL_CORRUPT;
#else
        reset_mark_block();
        return INSTALL_SIGNATURE_ERROR;
#endif
    }

    /* Try to open the package.
     */
    ZipArchive zip;
    err = mzOpenZipArchive(path, &zip);
    if (err != 0) {
        LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
#if 1 //wschen 2012-07-10
        reset_mark_block();
#endif
        return INSTALL_CORRUPT;
    }

    /* ----------------------------- */
    /* SECURE BOOT CHECK             */    
    /* ----------------------------- */
#ifdef SUPPORT_SBOOT_UPDATE    
    if(0 != (err=sec_verify_img_info(&zip,false)))
    {
        return INSTALL_SECURE_CHECK_FAIL;
    }
    sec_mark_status(false);
#endif
    

#ifdef SUPPORT_DATA_BACKUP_RESTORE //wschen 2011-03-09 

    if (check_part_size(&zip) != 0) {
        reset_mark_block();
        return INSTALL_ERROR;
    }
#endif //SUPPORT_DATA_BACKUP_RESTORE

    /* Verify and install the contents of the package.
     */
    ui->Print("Installing update...\n");
    return try_update_binary(path, &zip, wipe_cache);
}
Exemplo n.º 18
0
Value *FlashIfwiFn(const char *name, State * state, int argc, Expr * argv[])
{
	Value *ret = NULL;
	char *filename = NULL;
	int err, ifwi_bin_fd, dnx_bin_fd, i, num;
	char ifwi_name[128], dnx_name[128];
	ZipArchive ifwi_za;
	const ZipEntry *dnx_entry, *ifwi_entry;
	unsigned char *file_buf = NULL;
	size_t file_size;

	if (ReadArgs(state, argv, 1, &filename) < 0) {
		return NULL;
	}

	if (filename == NULL || strlen(filename) == 0) {
		ErrorAbort(state, "filename argument to %s can't be empty", name);
		goto done;
	}

	err = file_read(filename, (void **)&file_buf, &file_size);
	if (err) {
		ErrorAbort(state, "Failed to open zip archive file %s\n", filename);
		goto done;
	}

	err = mzOpenZipArchive(file_buf, file_size, &ifwi_za);
	if (err) {
		ErrorAbort(state, "Failed to open zip archive\n");
		goto done;
	}

	num = mzZipEntryCount(&ifwi_za);
	for (i = 0; i < num; i++) {
		ifwi_entry = mzGetZipEntryAt(&ifwi_za, i);
		if ((ifwi_entry->fileNameLen + 1) < sizeof(ifwi_name)) {
			strncpy(ifwi_name, ifwi_entry->fileName, ifwi_entry->fileNameLen);
			ifwi_name[ifwi_entry->fileNameLen] = '\0';
		} else {
			ErrorAbort(state, "ifwi file name is too big. Size max is:%d.\n", sizeof(ifwi_name));
			goto error;
		}
		if (strncmp(ifwi_name, IFWI_NAME, strlen(IFWI_NAME)))
			continue;

		if ((ifwi_bin_fd = open(IFWI_BIN_PATH, O_RDWR | O_TRUNC | O_CREAT, FILEMODE)) < 0) {
			ErrorAbort(state, "unable to create Extracted file:%s.\n", IFWI_BIN_PATH);
			goto error;
		}
		if ((dnx_bin_fd = open(DNX_BIN_PATH, O_RDWR | O_TRUNC | O_CREAT, FILEMODE)) < 0) {
			ErrorAbort(state, "unable to create Extracted file:%s.\n", IFWI_BIN_PATH);
			close(ifwi_bin_fd);
			goto error;
		}
		strcpy(dnx_name, "dnx_fwr");
		strncat(dnx_name, &(ifwi_name[strlen(IFWI_NAME)]), sizeof(dnx_name) - strlen("dnx_fwr") - 1);
		dnx_entry = mzFindZipEntry(&ifwi_za, dnx_name);

		if (dnx_entry == NULL) {
			ErrorAbort(state, "Could not find DNX entry");
			close(ifwi_bin_fd);
			close(dnx_bin_fd);
			goto error;
		}

		err = mzExtractZipEntryToFile(&ifwi_za, dnx_entry, dnx_bin_fd);
		if (!err) {
			ErrorAbort(state, "Failed to unzip %s\n", DNX_BIN_PATH);
			close(ifwi_bin_fd);
			close(dnx_bin_fd);
			goto error;
		}
		close(dnx_bin_fd);
		err = mzExtractZipEntryToFile(&ifwi_za, ifwi_entry, ifwi_bin_fd);
		if (!err) {
			ErrorAbort(state, "Failed to unzip %s\n", DNX_BIN_PATH);
			close(ifwi_bin_fd);
			goto error;
		}
		close(ifwi_bin_fd);
		int update_ifwi_file_scu_ipc(const char *dnx, const char *ifwi);
		update_ifwi_file_scu_ipc(DNX_BIN_PATH, IFWI_BIN_PATH);
	}

	ret = StringValue(strdup("t"));

error:
	mzCloseZipArchive(&ifwi_za);

done:
	free(file_buf);
	if (filename)
		free(filename);

	return ret;
}
Exemplo n.º 19
0
Value *FlashIfwiOrBomFn(enum flash_option_type flash_option, const char *name, State * state, int argc,
			Expr * argv[])
{
	Value *ret = NULL;
	char *filename = NULL;
	int err, i, num, buffsize;
	char ifwi_name[128];
	ZipArchive ifwi_za;
	const ZipEntry *ifwi_entry;
	unsigned char *buffer;
	unsigned char *file_buf = NULL;
	size_t file_size;
#ifdef TEE_FRAMEWORK
	char bom_token_name[128];
	const ZipEntry *bom_token_entry;
	int bom_token_buffsize;
	unsigned char *bom_token_buffer;
#endif

	if (ReadArgs(state, argv, 1, &filename) < 0) {
		return NULL;
	}

	if (filename == NULL || strlen(filename) == 0) {
		ErrorAbort(state, "filename argument to %s can't be empty", name);
		goto done;
	}

	err = file_read(filename, (void **)&file_buf, &file_size);
	if (err) {
		ErrorAbort(state, "Failed to open zip archive file %s\n", filename);
		goto done;
	}

	err = mzOpenZipArchive(file_buf, file_size, &ifwi_za);
	if (err) {
		ErrorAbort(state, "Failed to open zip archive\n");
		goto done;
	}

	num = mzZipEntryCount(&ifwi_za);
	for (i = 0; i < num; i++) {
		ifwi_entry = mzGetZipEntryAt(&ifwi_za, i);
		if ((ifwi_entry->fileNameLen + 1) < sizeof(ifwi_name)) {
			strncpy(ifwi_name, ifwi_entry->fileName, ifwi_entry->fileNameLen);
			ifwi_name[ifwi_entry->fileNameLen] = '\0';
		} else {
			ErrorAbort(state, "ifwi file name is too big. Size max is:%zd.\n", sizeof(ifwi_name));
			goto error;
		}
		if (strncmp(ifwi_name, IFWI_NAME, strlen(IFWI_NAME)))
			continue;
		buffsize = mzGetZipEntryUncompLen(ifwi_entry);
		if (buffsize <= 0) {
			ErrorAbort(state, "Bad ifwi_entry size : %d.\n", buffsize);
			goto error;
		}
		buffer = (unsigned char *)malloc(sizeof(unsigned char) * buffsize);
		if (buffer == NULL) {
			ErrorAbort(state, "Unable to alloc ifwi buffer of %d bytes.\n", buffsize);
			goto error;
		}
		err = mzExtractZipEntryToBuffer(&ifwi_za, ifwi_entry, buffer);
		if (!err) {
			ErrorAbort(state, "Failed to unzip %s\n", IFWI_BIN_PATH);
			free(buffer);
			goto error;
		}

		if (check_ifwi_file(buffer, buffsize) < 1) {
			free(buffer);
			continue;
		}

		if (flash_option == FLASH_BOM_TOKEN_BINARY) {
#ifdef TEE_FRAMEWORK
			strcpy(bom_token_name, BOM_TOKEN_NAME);
			strncat(bom_token_name, &(ifwi_name[strlen(IFWI_NAME)]),
				sizeof(bom_token_name) - strlen(BOM_TOKEN_NAME) - 1);
			bom_token_entry = mzFindZipEntry(&ifwi_za, bom_token_name);

			if (bom_token_entry != NULL) {
				bom_token_buffsize = mzGetZipEntryUncompLen(bom_token_entry);
				if (bom_token_buffsize <= 0) {
					ErrorAbort(state, "Bad bom_token_entry size : %d.\n",
						   bom_token_buffsize);
					free(buffer);
					goto error;
				}
				bom_token_buffer =
				    (unsigned char *)malloc(sizeof(unsigned char) * bom_token_buffsize);
				if (bom_token_buffer == NULL) {
					ErrorAbort(state, "Unable to alloc bom token buffer of %d bytes.\n",
						   bom_token_buffsize);
					free(buffer);
					goto error;
				}
				err = mzExtractZipEntryToBuffer(&ifwi_za, bom_token_entry, bom_token_buffer);
				if (!err) {
					ErrorAbort(state, "Failed to unzip %s.\n", IFWI_BIN_PATH);
					free(bom_token_buffer);
					free(buffer);
					goto error;
				}
				if (write_token(bom_token_buffer, bom_token_buffsize) == 0) {
					printf("BOM token written\n");
				} else {
					printf("Unable to write BOM token.\n");
					cancel_update(0, NULL);
					free(bom_token_buffer);
					free(buffer);
					ret = StringValue(strdup("fail"));
					goto error;
				}
				free(bom_token_buffer);
			}
#else
			printf("BOM token flashing not supported\n");
#endif
		} else if (flash_option == FLASH_IFWI_BINARY) {
			printf("Flashing IFWI\n");
			update_ifwi_file(buffer, buffsize);
		} else {
			ErrorAbort(state, "Don't know what to do with option %d\n", flash_option);
			free(buffer);
			goto error;
		}
		free(buffer);
	}

	ret = StringValue(strdup("t"));

error:
	mzCloseZipArchive(&ifwi_za);

done:
	free(file_buf);
	if (filename)
		free(filename);

	return ret;
}
/**
 * install_package() 的实现主体.
 * @param path
 *      待安装的 ota_pkg 的路径字串. 
 * @param install_file
 *      传入 install 流程的 log file 的路径. 
 * @param is_ru_pkg
 *      当前待安装的 ota_pkg 是否是 ru_pkg.
 */
static int
really_install_package(const char *path, int* wipe_cache, bool needs_mount, int is_ru_pkg)
{
    int ret = INSTALL_SUCCESS;
	//by [email protected]
	//if update loader, we hope not clear misc command.
	//default not clear misc command, let the update-script of update.zip to clear misc when no update loader.
	bNeedClearMisc = false;
    ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
    ui->Print("Finding update package...\n");
    // Give verification half the progress bar...
    ui->SetProgressType(RecoveryUI::DETERMINATE);
    ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
    LOGI("Update location: %s\n", path);

    // Map the update package into memory.
    ui->Print("Opening update package...\n");

	needs_mount = true;
    if (path && needs_mount) {
        if (path[0] == '@') {
            ensure_path_mounted(path+1);
        } else {
            ensure_path_mounted(path);
        }
    }

    MemMapping map;
    if (sysMapFile(path, &map) != 0) {
        LOGE("failed to map file\n");
        return INSTALL_CORRUPT;
    }

    int numKeys;
    Certificate* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
    if (loadedKeys == NULL) {
        LOGE("Failed to load keys\n");
        return INSTALL_CORRUPT;
    }
    LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);

    ui->Print("Verifying update package...\n");

    int err;
    err = verify_file(map.addr, map.length, loadedKeys, numKeys);
    free(loadedKeys);
    LOGI("verify_file returned %d\n", err);
    if (err != VERIFY_SUCCESS) {
        LOGE("signature verification failed\n");
        sysReleaseMap(&map);
        return INSTALL_CORRUPT;
    }

    /* Try to open the package.
     */
    ZipArchive zip;
    err = mzOpenZipArchive(map.addr, map.length, &zip);
    if (err != 0) {
        LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
        sysReleaseMap(&map);
        return INSTALL_CORRUPT;
    }

#ifdef USE_BOARD_ID
    ensure_path_mounted("/cust");
    ensure_path_mounted("/system");
    D("to restore system_partition.");
    restore();

    gIfBoardIdCustom = true;
#endif

#ifdef USE_RADICAL_UPDATE
    
    // .KP : restore_fw_in_ota_ver : 
    // 无论安装 ru_pkg 还是 original_ota_pkg 之前, 都要 restore 可能的 backup_of_fws_in_ota_ver. 
    // 这样即便连续多次安装 ru_pkg, /radical_update/backup_of_fws_in_ota_ver 中保存的都是 fws_in_ota_ver. 
    // 才能保证, 后续若安装 ota_diff_pkg, 不会失败. 

    if ( 0 != ensure_path_mounted(RU_PARTITION_MOUNT_PATH) )
    {
        SET_ERROR_AND_JUMP("fail to mount ru_partition", ret, INSTALL_ERROR, EXIT);
    }
    if ( 0 != ensure_path_mounted(SYSTEM_PARTITION_MOUNT_PATH) )
    {
        SET_ERROR_AND_JUMP("fail to mount system_partition", ret, INSTALL_ERROR, EXIT);
    }

    if ( RadicalUpdate_isApplied() )
    {
        I("a ru_pkg is applied, to restore backup_of_fws_in_ota_ver to system_partition.");
        CHECK_FUNC_CALL( RadicalUpdate_restoreFirmwaresInOtaVer() , ret, EXIT);
    }
    else 
    {
        D("no ru_pkg is applied.");
    }
    
    if ( 0 != ensure_path_unmounted(RU_PARTITION_MOUNT_PATH) )
    {
        SET_ERROR_AND_JUMP("fail to unmount ru_partition", ret, INSTALL_ERROR, EXIT);
    }
    if ( 0 != ensure_path_unmounted(SYSTEM_PARTITION_MOUNT_PATH) )
    {
        SET_ERROR_AND_JUMP("fail to unmount system_partition", ret, INSTALL_ERROR, EXIT);
    }
#endif

    /* Verify and install the contents of the package.
     */
    ui->Print("Installing update...\n");
    ui->SetEnableReboot(false);
    ret = try_update_binary(path, &zip, wipe_cache);
    ui->SetEnableReboot(true);
    ui->Print("\n");

    sysReleaseMap(&map);

EXIT:
    return ret;
}
Exemplo n.º 21
0
int main(int argc, char** argv) {
    // Various things log information to stdout or stderr more or less
    // at random.  The log file makes more sense if buffering is
    // turned off so things appear in the right order.
    setbuf(stdout, NULL);
    setbuf(stderr, NULL);

    if (argc != 4) {
        fprintf(stderr, "unexpected number of arguments (%d)\n", argc);
        return 1;
    }

    char* version = argv[1];
    if ((version[0] != '1' && version[0] != '2' && version[0] != '3') ||
        version[1] != '\0') {
        // We support version 1, 2, or 3.
        fprintf(stderr, "wrong updater binary API; expected 1, 2, or 3; "
                        "got %s\n",
                argv[1]);
	//printf("Wrong updater binary API; expected 1, 2, or 3; we just got '%s' \n",argv[1]);
        return 2;
    }

    // Set up the pipe for sending commands back to the parent process.

    int fd = atoi(argv[2]);
    FILE* cmd_pipe = fdopen(fd, "wb");
    setlinebuf(cmd_pipe);

    // Extract the script from the package.

    char* package_data = argv[3];
    ZipArchive za;
    int err;
    err = mzOpenZipArchive(package_data, &za);
    if (err != 0) {
        fprintf(stderr, "failed to open package %s: %s\n",
                package_data, strerror(err));
        return 3;
    }

    const ZipEntry* script_entry = mzFindZipEntry(&za, SCRIPT_NAME);
    if (script_entry == NULL) {
        fprintf(stderr, "failed to find %s in %s\n", SCRIPT_NAME, package_data);
        return 4;
    }

    char* script = malloc(script_entry->uncompLen+1);
    if (!mzReadZipEntry(&za, script_entry, script, script_entry->uncompLen)) {
        fprintf(stderr, "failed to read script from package\n");
        return 5;
    }
    script[script_entry->uncompLen] = '\0';

    // Configure edify's functions.

    RegisterBuiltins();
    RegisterInstallFunctions();
    RegisterDeviceExtensions();
    FinishRegistration();

    // Parse the script.

    Expr* root;
    int error_count = 0;
    yy_scan_string(script);
    int error = yyparse(&root, &error_count);
    if (error != 0 || error_count > 0) {
        fprintf(stderr, "%d parse errors\n", error_count);
        return 6;
    }

    struct selinux_opt seopts[] = {
      { SELABEL_OPT_PATH, "/file_contexts" }
    };

    sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);

    if (!sehandle) {
        fprintf(stderr, "Warning:  No file_contexts\n");
        // fprintf(cmd_pipe, "ui_print Warning: No file_contexts\n");
    }

    // Evaluate the parsed script.

    UpdaterInfo updater_info;
    updater_info.cmd_pipe = cmd_pipe;
    updater_info.package_zip = &za;
    updater_info.version = atoi(version);

    State state;
    state.cookie = &updater_info;
    state.script = script;
    state.errmsg = NULL;

    char* result = Evaluate(&state, root);
    if (result == NULL) {
        if (state.errmsg == NULL) {
            fprintf(stderr, "script aborted (no error message)\n");
            fprintf(cmd_pipe, "ui_print script aborted (no error message)\n");
        } else {
            fprintf(stderr, "script aborted: %s\n", state.errmsg);
            char* line = strtok(state.errmsg, "\n");
            while (line) {
                fprintf(cmd_pipe, "ui_print %s\n", line);
                line = strtok(NULL, "\n");
            }
            fprintf(cmd_pipe, "ui_print\n");
        }
        free(state.errmsg);
        return 7;
    } else {
        fprintf(stderr, "script result was [%s]\n", result);
        free(result);
    }

    if (updater_info.package_zip) {
        mzCloseZipArchive(updater_info.package_zip);
    }
    free(script);

    return 0;
}
Exemplo n.º 22
0
int main(int argc, char** argv) {
    // Various things log information to stdout or stderr more or less
    // at random (though we've tried to standardize on stdout).  The
    // log file makes more sense if buffering is turned off so things
    // appear in the right order.
    setbuf(stdout, NULL);
    setbuf(stderr, NULL);

    if (argc != 4) {
        printf("unexpected number of arguments (%d)\n", argc);
        return 1;
    }

    char* version = argv[1];
    if ((version[0] != '1' && version[0] != '2' && version[0] != '3') ||
        version[1] != '\0') {
        // We support version 1, 2, or 3.
        printf("wrong updater binary API; expected 1, 2, or 3; "
                        "got %s\n",
                argv[1]);
        return 2;
    }

    // Set up the pipe for sending commands back to the parent process.

    int fd = atoi(argv[2]);
    FILE* cmd_pipe = fdopen(fd, "wb");
    setlinebuf(cmd_pipe);

    // Extract the script from the package.

    const char* package_filename = argv[3];
    MemMapping map;
    if (sysMapFile(package_filename, &map) != 0) {
        printf("failed to map package %s\n", argv[3]);
        return 3;
    }
    ZipArchive za;
    int err;
    err = mzOpenZipArchive(map.addr, map.length, &za);
    if (err != 0) {
        printf("failed to open package %s: %s\n",
               argv[3], strerror(err));
        return 3;
    }

    const ZipEntry* script_entry = mzFindZipEntry(&za, SCRIPT_NAME);
    if (script_entry == NULL) {
        printf("failed to find %s in %s\n", SCRIPT_NAME, package_filename);
        return 4;
    }

    char* script = malloc(script_entry->uncompLen+1);
    if (!mzReadZipEntry(&za, script_entry, script, script_entry->uncompLen)) {
        printf("failed to read script from package\n");
        return 5;
    }
    script[script_entry->uncompLen] = '\0';

    const ZipEntry* file_contexts_entry = mzFindZipEntry(&za, SELINUX_CONTEXTS_ZIP);
    if (file_contexts_entry != NULL) {
        int file_contexts_fd = creat(SELINUX_CONTEXTS_TMP, 0644);
		if (file_contexts_fd < 0) {
			fprintf(stderr, "Could not extract %s to '%s'\n", SELINUX_CONTEXTS_ZIP, SELINUX_CONTEXTS_TMP);
			return 3;
		}

		int ret_val = mzExtractZipEntryToFile(&za, file_contexts_entry, file_contexts_fd);
		close(file_contexts_fd);

		if (!ret_val) {
			fprintf(stderr, "Could not extract '%s'\n", SELINUX_CONTEXTS_ZIP);
			return 3;
		}
    }

    // Configure edify's functions.

    RegisterBuiltins();
    RegisterInstallFunctions();
    RegisterBlockImageFunctions();
    RegisterDeviceExtensions();
    FinishRegistration();

    // Parse the script.

    Expr* root;
    int error_count = 0;
    int error = parse_string(script, &root, &error_count);
    if (error != 0 || error_count > 0) {
        printf("%d parse errors\n", error_count);
        return 6;
    }

    if (access(SELINUX_CONTEXTS_TMP, R_OK) == 0) {
        struct selinux_opt seopts[] = {
          { SELABEL_OPT_PATH, SELINUX_CONTEXTS_TMP }
        };

        sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
    } else {
        struct selinux_opt seopts[] = {
          { SELABEL_OPT_PATH, "/file_contexts" }
        };

        sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
    }

    if (!sehandle) {
        fprintf(cmd_pipe, "ui_print Warning: No file_contexts\n");
    }

    // Evaluate the parsed script.

    UpdaterInfo updater_info;
    updater_info.cmd_pipe = cmd_pipe;
    updater_info.package_zip = &za;
    updater_info.version = atoi(version);
    updater_info.package_zip_addr = map.addr;
    updater_info.package_zip_len = map.length;

    State state;
    state.cookie = &updater_info;
    state.script = script;
    state.errmsg = NULL;

    char* result = Evaluate(&state, root);
    if (result == NULL) {
        if (state.errmsg == NULL) {
            printf("script aborted (no error message)\n");
            fprintf(cmd_pipe, "ui_print script aborted (no error message)\n");
        } else {
            printf("script aborted: %s\n", state.errmsg);
            char* line = strtok(state.errmsg, "\n");
            while (line) {
                fprintf(cmd_pipe, "ui_print %s\n", line);
                line = strtok(NULL, "\n");
            }
            fprintf(cmd_pipe, "ui_print\n");
        }
        free(state.errmsg);
        return 7;
    } else {
        fprintf(cmd_pipe, "ui_print script succeeded: result was [%s]\n", result);
        free(result);
    }

    if (updater_info.package_zip) {
        mzCloseZipArchive(updater_info.package_zip);
    }
    sysReleaseMap(&map);
    free(script);

    return 0;
}
std::string MROMInstaller::open(const std::string& file)
{
	char* manifest = NULL;
	const ZipEntry *script_entry;
	ZipArchive zip;

#if (ANDROID_VERSION >= 5)
	MemMapping map;
	if (sysMapFile(file.c_str(), &map) != 0) {
		LOGERR("Failed to sysMapFile '%s'\n", file.c_str());
		return false;
	}

	if (mzOpenZipArchive(map.addr, map.length, &zip) != 0)
#else
	if (mzOpenZipArchive(file.c_str(), &zip) != 0)
#endif
		return "Failed to open installer file!";

	script_entry = mzFindZipEntry(&zip, "manifest.txt");
	if(!script_entry)
	{
		mzCloseZipArchive(&zip);
#if (ANDROID_VERSION >= 5)
		sysReleaseMap(&map);
#endif
		return "Failed to find manifest.txt";
	}

	int res = read_data(&zip, script_entry, &manifest, NULL);

	mzCloseZipArchive(&zip);
#if (ANDROID_VERSION >= 5)
	sysReleaseMap(&map);
#endif

	if(res < 0)
		return "Failed to read manifest.txt!";

	int line_cnt = 1;
	for(char *line = strtok(manifest, "\r\n"); line; line = strtok(NULL, "\r\n"), ++line_cnt)
	{
		if(line[0] == '#')
			continue;

		char *val = strchr(line, '=');
		if(!val)
			continue;

		std::string key = std::string(line, val-line);
		++val; // skip '=' char

		char *start = strchr(val, '"');
		char *end = strrchr(val, '"');

		if(!start || start == end || start+1 == end)
			gui_print("Line %d: failed to parse string\n", line_cnt);
		else
		{
			++start;
			m_vals[key] = std::string(start, end-start);
			LOGI("MROMInstaller: got tag %s=%s\n", key.c_str(), m_vals[key].c_str());
		}
	}

	free(manifest);

	static const char* needed[] = {
		"manifest_ver", "devices", "base_folders"
	};

	for(uint32_t i = 0; i < sizeof(needed)/sizeof(needed[0]); ++i)
	{
		std::map<std::string, std::string>::const_iterator itr = m_vals.find(needed[i]);
		if(itr == m_vals.end())
			return std::string("Required key not found in manifest: ") + needed[i];
	}

	m_file = file;
	return std::string();
}