blob: 8676b603848a9fee34b4e3180c22520a3e9302d6 [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tianjie Xu58d59122019-05-03 01:05:04 -070017#pragma once
Doug Zongker9931f7f2009-06-10 14:11:53 -070018
Tianjie Xu58d59122019-05-03 01:05:04 -070019#include <stdint.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070020#include <stdio.h>
Tianjie Xu58d59122019-05-03 01:05:04 -070021
22#include <memory>
23#include <string>
Tianjie Xu1536db82019-05-14 10:54:43 -070024#include <string_view>
Tianjie Xu58d59122019-05-03 01:05:04 -070025
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070026#include <ziparchive/zip_archive.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070027
Tianjie Xu58d59122019-05-03 01:05:04 -070028#include "edify/expr.h"
Tianjie Xu1536db82019-05-14 10:54:43 -070029#include "edify/updater_interface.h"
Tianjie Xu58d59122019-05-03 01:05:04 -070030#include "otautil/error_code.h"
31#include "otautil/sysutil.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070032
Tianjie Xu1536db82019-05-14 10:54:43 -070033class Updater : public UpdaterInterface {
Tianjie Xu58d59122019-05-03 01:05:04 -070034 public:
Tianjie Xu1536db82019-05-14 10:54:43 -070035 explicit Updater(std::unique_ptr<UpdaterRuntimeInterface> run_time)
36 : runtime_(std::move(run_time)) {}
37
Tianjie Xu1536db82019-05-14 10:54:43 -070038 ~Updater() override;
Tianjie Xu58d59122019-05-03 01:05:04 -070039
40 // Memory-maps the OTA package and opens it as a zip file. Also sets up the command pipe and
Tianjie Xu1536db82019-05-14 10:54:43 -070041 // UpdaterRuntime.
42 bool Init(int fd, const std::string_view package_filename, bool is_retry);
Tianjie Xu58d59122019-05-03 01:05:04 -070043
44 // Parses and evaluates the updater-script in the OTA package. Reports the error code if the
45 // evaluation fails.
46 bool RunUpdate();
47
48 // Writes the message to command pipe, adds a new line in the end.
Tianjie Xu1536db82019-05-14 10:54:43 -070049 void WriteToCommandPipe(const std::string_view message, bool flush = false) const override;
Tianjie Xu58d59122019-05-03 01:05:04 -070050
51 // Sends over the message to recovery to print it on the screen.
Tianjie Xu1536db82019-05-14 10:54:43 -070052 void UiPrint(const std::string_view message) const override;
Tianjie Xu58d59122019-05-03 01:05:04 -070053
Tianjie Xu1536db82019-05-14 10:54:43 -070054 std::string FindBlockDeviceName(const std::string_view name) const override;
55
56 UpdaterRuntimeInterface* GetRuntime() const override {
57 return runtime_.get();
58 }
59 ZipArchiveHandle GetPackageHandle() const override {
Tianjie Xu58d59122019-05-03 01:05:04 -070060 return package_handle_;
61 }
Tianjie Xu1536db82019-05-14 10:54:43 -070062 std::string GetResult() const override {
Tianjie Xu58d59122019-05-03 01:05:04 -070063 return result_;
64 }
Tianjie Xu1536db82019-05-14 10:54:43 -070065 uint8_t* GetMappedPackageAddress() const override {
Tianjie Xu58d59122019-05-03 01:05:04 -070066 return mapped_package_.addr;
67 }
Robin Lee1cf8eb72019-07-05 22:56:20 +020068 size_t GetMappedPackageLength() const override {
69 return mapped_package_.length;
70 }
Tianjie Xu58d59122019-05-03 01:05:04 -070071
72 private:
73 friend class UpdaterTestBase;
74 friend class UpdaterTest;
75 // Where in the package we expect to find the edify script to execute.
76 // (Note it's "updateR-script", not the older "update-script".)
77 static constexpr const char* SCRIPT_NAME = "META-INF/com/google/android/updater-script";
78
79 // Reads the entry |name| in the zip archive and put the result in |content|.
80 bool ReadEntryToString(ZipArchiveHandle za, const std::string& entry_name, std::string* content);
81
82 // Parses the error code embedded in state->errmsg; and reports the error code and cause code.
83 void ParseAndReportErrorCode(State* state);
84
Tianjie Xu1536db82019-05-14 10:54:43 -070085 std::unique_ptr<UpdaterRuntimeInterface> runtime_;
86
Tianjie Xu58d59122019-05-03 01:05:04 -070087 MemMapping mapped_package_;
88 ZipArchiveHandle package_handle_{ nullptr };
89 std::string updater_script_;
90
91 bool is_retry_{ false };
92 std::unique_ptr<FILE, decltype(&fclose)> cmd_pipe_{ nullptr, fclose };
Tianjie Xu58d59122019-05-03 01:05:04 -070093
94 std::string result_;
Tianjie Xu1536db82019-05-14 10:54:43 -070095 std::vector<std::string> skipped_functions_;
Tianjie Xu58d59122019-05-03 01:05:04 -070096};