Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1 | /* |
| 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 Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 17 | #pragma once |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 18 | |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 21 | |
| 22 | #include <memory> |
| 23 | #include <string> |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 24 | #include <string_view> |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 25 | |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 26 | #include <ziparchive/zip_archive.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 27 | |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 28 | #include "edify/expr.h" |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 29 | #include "edify/updater_interface.h" |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 30 | #include "otautil/error_code.h" |
| 31 | #include "otautil/sysutil.h" |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 32 | |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 33 | class Updater : public UpdaterInterface { |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 34 | public: |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 35 | explicit Updater(std::unique_ptr<UpdaterRuntimeInterface> run_time) |
| 36 | : runtime_(std::move(run_time)) {} |
| 37 | |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 38 | ~Updater() override; |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 39 | |
| 40 | // Memory-maps the OTA package and opens it as a zip file. Also sets up the command pipe and |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 41 | // UpdaterRuntime. |
| 42 | bool Init(int fd, const std::string_view package_filename, bool is_retry); |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 43 | |
| 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 Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 49 | void WriteToCommandPipe(const std::string_view message, bool flush = false) const override; |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 50 | |
| 51 | // Sends over the message to recovery to print it on the screen. |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 52 | void UiPrint(const std::string_view message) const override; |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 53 | |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 54 | 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 Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 60 | return package_handle_; |
| 61 | } |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 62 | std::string GetResult() const override { |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 63 | return result_; |
| 64 | } |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 65 | uint8_t* GetMappedPackageAddress() const override { |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 66 | return mapped_package_.addr; |
| 67 | } |
Robin Lee | 1cf8eb7 | 2019-07-05 22:56:20 +0200 | [diff] [blame] | 68 | size_t GetMappedPackageLength() const override { |
| 69 | return mapped_package_.length; |
| 70 | } |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 71 | |
| 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 Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 85 | std::unique_ptr<UpdaterRuntimeInterface> runtime_; |
| 86 | |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 87 | 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 Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 93 | |
| 94 | std::string result_; |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 95 | std::vector<std::string> skipped_functions_; |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 96 | }; |