blob: 4c6aa07ad0568993e431f01cc9b19f6a15ef845b [file] [log] [blame]
Hridya Valsaraju31d2c262018-07-20 13:35:50 -07001/*
2 * Copyright (C) 2018 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#pragma once
17
David Anderson12211d12018-07-24 15:21:20 -070018#include <optional>
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070019#include <string>
20
David Anderson12211d12018-07-24 15:21:20 -070021#include <android-base/unique_fd.h>
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070022#include <android/hardware/boot/1.0/IBootControl.h>
David Andersond25f1c32018-11-09 20:41:33 -080023#include <liblp/liblp.h>
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070024
David Anderson12211d12018-07-24 15:21:20 -070025// Logical partitions are only mapped to a block device as needed, and
26// immediately unmapped when no longer needed. In order to enforce this we
27// require accessing partitions through a Handle abstraction, which may perform
28// additional operations after closing its file descriptor.
29class PartitionHandle {
30 public:
31 PartitionHandle() {}
32 explicit PartitionHandle(const std::string& path) : path_(path) {}
David Anderson88ef0b12018-08-09 10:40:00 -070033 PartitionHandle(const std::string& path, std::function<void()>&& closer)
34 : path_(path), closer_(std::move(closer)) {}
35 PartitionHandle(PartitionHandle&& other) = default;
36 PartitionHandle& operator=(PartitionHandle&& other) = default;
37 ~PartitionHandle() {
38 if (closer_) {
39 // Make sure the device is closed first.
40 fd_ = {};
41 closer_();
42 }
43 }
David Anderson12211d12018-07-24 15:21:20 -070044 const std::string& path() const { return path_; }
45 int fd() const { return fd_.get(); }
46 void set_fd(android::base::unique_fd&& fd) { fd_ = std::move(fd); }
47
48 private:
49 std::string path_;
50 android::base::unique_fd fd_;
David Anderson88ef0b12018-08-09 10:40:00 -070051 std::function<void()> closer_;
David Anderson12211d12018-07-24 15:21:20 -070052};
53
54class FastbootDevice;
55
David Andersond25f1c32018-11-09 20:41:33 -080056// On normal devices, the super partition is always named "super". On retrofit
57// devices, the name must be derived from the partition name or current slot.
58// This helper assists in choosing the correct super for a given partition
59// name.
60std::string GetSuperSlotSuffix(FastbootDevice* device, const std::string& partition_name);
61
David Anderson12211d12018-07-24 15:21:20 -070062std::optional<std::string> FindPhysicalPartition(const std::string& name);
David Andersond25f1c32018-11-09 20:41:33 -080063bool LogicalPartitionExists(FastbootDevice* device, const std::string& name,
David Anderson88ef0b12018-08-09 10:40:00 -070064 bool* is_zero_length = nullptr);
David Anderson12211d12018-07-24 15:21:20 -070065bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle);
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070066bool GetSlotNumber(const std::string& slot, android::hardware::boot::V1_0::Slot* number);
David Anderson0f626632018-08-31 16:44:25 -070067std::vector<std::string> ListPartitions(FastbootDevice* device);
Hridya Valsarajudca328d2018-09-24 16:01:35 -070068bool GetDeviceLockStatus();
David Andersond25f1c32018-11-09 20:41:33 -080069
70// Update all copies of metadata.
71bool UpdateAllPartitionMetadata(const std::string& super_name,
72 const android::fs_mgr::LpMetadata& metadata);