1 /*
2  * Copyright (C) 2019 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 
17 package android.gsi;
18 
19 import android.gsi.AvbPublicKey;
20 import android.gsi.MappedImage;
21 import android.gsi.IProgressCallback;
22 
23 /** {@hide} */
24 interface IImageService {
25     /* These flags match fiemap::ImageManager::CreateBackingImage. */
26     const int CREATE_IMAGE_DEFAULT = 0x0;
27     const int CREATE_IMAGE_READONLY = 0x1;
28     const int CREATE_IMAGE_ZERO_FILL = 0x2;
29 
30     /* Successfully returned */
31     const int IMAGE_OK = 0;
32     /* Generic error code */
33     const int IMAGE_ERROR = 1;
34 
35     /**
36      * Create an image that can be mapped as a block device.
37      *
38      * This call will fail if running a GSI.
39      *
40      * @param name          Image name. If the image already exists, the call will fail.
41      * @param size          Image size, in bytes. If too large, or not enough space is
42      *                      free, the call will fail.
43      * @param readonly      If readonly, MapBackingImage() will configure the device as
44      *                      readonly.
45      * @param on_progress   Progress callback. It is invoked when there is an interesting update.
46      *                      For each invocation, |current| is the number of bytes actually written,
47      *                      and |total| is set to |size|.
48      * @throws ServiceSpecificException if any error occurs. Exception code is a
49      *                      FiemapStatus::ErrorCode value.
50      */
createBackingImage(@tf8InCpp String name, long size, int flags, @nullable IProgressCallback on_progress)51     void createBackingImage(@utf8InCpp String name, long size, int flags,
52                             @nullable IProgressCallback on_progress);
53 
54     /**
55      * Delete an image created with createBackingImage.
56      *
57      * @param name          Image name as passed to createBackingImage().
58      * @throws ServiceSpecificException if any error occurs.
59      */
deleteBackingImage(@tf8InCpp String name)60     void deleteBackingImage(@utf8InCpp String name);
61 
62     /**
63      * Map an image, created with createBackingImage, such that it is accessible as a
64      * block device.
65      *
66      * @param name          Image name as passed to createBackingImage().
67      * @param timeout_ms    Time to wait for a valid mapping, in milliseconds. This must be more
68      *                      than zero; 10 seconds is recommended.
69      * @param mapping       Information about the newly mapped block device.
70      */
mapImageDevice(@tf8InCpp String name, int timeout_ms, out MappedImage mapping)71     void mapImageDevice(@utf8InCpp String name, int timeout_ms, out MappedImage mapping);
72 
73     /**
74      * Unmap a block device previously mapped with mapBackingImage. This step is necessary before
75      * calling deleteBackingImage.
76      *
77      * @param name          Image name as passed to createBackingImage().
78      */
unmapImageDevice(@tf8InCpp String name)79     void unmapImageDevice(@utf8InCpp String name);
80 
81     /**
82      * Returns whether or not a backing image exists.
83      *
84      * @param name          Image name as passed to createBackingImage().
85      */
backingImageExists(@tf8InCpp String name)86     boolean backingImageExists(@utf8InCpp String name);
87 
88     /**
89      * Returns whether or not the named image is mapped.
90      *
91      * @param name          Image name as passed to createBackingImage().
92      */
isImageMapped(@tf8InCpp String name)93     boolean isImageMapped(@utf8InCpp String name);
94 
95     /**
96      * Retrieve AVB public key from an image.
97      * If the image is already mapped then it works the same as
98      * IGsiService::getAvbPublicKey(). Otherwise this will attempt to
99      * map / unmap the partition image upon enter / return.
100      *
101      * @param name          Image name as passed to createBackingImage().
102      * @param dst           Output of the AVB public key.
103      * @return              0 on success, an error code on failure.
104      */
getAvbPublicKey(@tf8InCpp String name, out AvbPublicKey dst)105     int getAvbPublicKey(@utf8InCpp String name, out AvbPublicKey dst);
106 
107     /**
108      * Get all installed backing image names
109      *
110      * @return list of installed backing image names
111      */
getAllBackingImages()112     @utf8InCpp List<String> getAllBackingImages();
113 
114     /**
115      * Writes a given amount of zeros in image file.
116      *
117      * @param name          Image name. If the image does not exist, the call
118      *                      will fail.
119      * @param bytes         Number of zeros to be written, starting from the
120      *                      beginning. If bytes is equal to 0, then the whole
121      *                      image file is filled with zeros.
122      * @throws ServiceSpecificException if any error occurs. Exception code is a
123      *                      FiemapStatus::ErrorCode value.
124      */
zeroFillNewImage(@tf8InCpp String name, long bytes)125     void zeroFillNewImage(@utf8InCpp String name, long bytes);
126 
127     /**
128      * Find and remove all images in the containing folder of this instance.
129      */
removeAllImages()130     void removeAllImages();
131 
132     /**
133      * Remove all images that were marked as disabled in recovery.
134      */
removeDisabledImages()135     void removeDisabledImages();
136 
137     /**
138      * Return the block device path of a mapped image, or an empty string if not mapped.
139      */
getMappedImageDevice(@tf8InCpp String name)140     @utf8InCpp String getMappedImageDevice(@utf8InCpp String name);
141 }
142