blob: d5468292bbd757800f953221f2350bd166456fa3 [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>
24
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070025#include <ziparchive/zip_archive.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070026
Tianjie Xu58d59122019-05-03 01:05:04 -070027#include "edify/expr.h"
28#include "otautil/error_code.h"
29#include "otautil/sysutil.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070030
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070031struct selabel_handle;
Stephen Smalley779701d2012-02-09 14:13:23 -050032
Tianjie Xu58d59122019-05-03 01:05:04 -070033class Updater {
34 public:
35 ~Updater();
36
37 // Memory-maps the OTA package and opens it as a zip file. Also sets up the command pipe and
38 // selabel handle. TODO(xunchang) implement a run time environment class and move sehandle there.
39 bool Init(int fd, const std::string& package_filename, bool is_retry,
40 struct selabel_handle* sehandle);
41
42 // Parses and evaluates the updater-script in the OTA package. Reports the error code if the
43 // evaluation fails.
44 bool RunUpdate();
45
46 // Writes the message to command pipe, adds a new line in the end.
47 void WriteToCommandPipe(const std::string& message, bool flush = false) const;
48
49 // Sends over the message to recovery to print it on the screen.
50 void UiPrint(const std::string& message) const;
51
52 ZipArchiveHandle package_handle() const {
53 return package_handle_;
54 }
55 struct selabel_handle* sehandle() const {
56 return sehandle_;
57 }
58 std::string result() const {
59 return result_;
60 }
61
62 uint8_t* GetMappedPackageAddress() const {
63 return mapped_package_.addr;
64 }
65
66 private:
67 friend class UpdaterTestBase;
68 friend class UpdaterTest;
69 // Where in the package we expect to find the edify script to execute.
70 // (Note it's "updateR-script", not the older "update-script".)
71 static constexpr const char* SCRIPT_NAME = "META-INF/com/google/android/updater-script";
72
73 // Reads the entry |name| in the zip archive and put the result in |content|.
74 bool ReadEntryToString(ZipArchiveHandle za, const std::string& entry_name, std::string* content);
75
76 // Parses the error code embedded in state->errmsg; and reports the error code and cause code.
77 void ParseAndReportErrorCode(State* state);
78
79 MemMapping mapped_package_;
80 ZipArchiveHandle package_handle_{ nullptr };
81 std::string updater_script_;
82
83 bool is_retry_{ false };
84 std::unique_ptr<FILE, decltype(&fclose)> cmd_pipe_{ nullptr, fclose };
85 struct selabel_handle* sehandle_{ nullptr };
86
87 std::string result_;
88};