2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <sys/types.h>
19 #include <sys/sysmacros.h>
29 #include <hardware/hardware.h>
30 #include <hardware/boot_control.h>
34 void module_init(boot_control_module_t *module)
38 unsigned module_getNumberSlots(boot_control_module_t *module)
43 static bool get_dev_t_for_partition(const char *name, dev_t *out_device)
48 fd = boot_info_open_partition(name, NULL, O_RDONLY);
51 if (fstat(fd, &statbuf) != 0) {
52 fprintf(stderr, "WARNING: Error getting information about part %s: %s\n",
53 name, strerror(errno));
58 *out_device = statbuf.st_rdev;
62 unsigned module_getCurrentSlot(boot_control_module_t *module)
65 dev_t system_a_dev, system_b_dev;
67 if (stat("/system", &statbuf) != 0) {
68 fprintf(stderr, "WARNING: Error getting information about /system: %s\n",
73 if (!get_dev_t_for_partition("system_a", &system_a_dev) ||
74 !get_dev_t_for_partition("system_b", &system_b_dev))
77 if (statbuf.st_dev == system_a_dev) {
79 } else if (statbuf.st_dev == system_b_dev) {
82 fprintf(stderr, "WARNING: Error determining current slot "
83 "(/system dev_t of %d:%d does not match a=%d:%d or b=%d:%d)\n",
84 major(statbuf.st_dev), minor(statbuf.st_dev),
85 major(system_a_dev), minor(system_a_dev),
86 major(system_b_dev), minor(system_b_dev));
91 int module_markBootSuccessful(boot_control_module_t *module)
96 #define COPY_BUF_SIZE (1024*1024)
98 static bool copy_data(int src_fd, int dst_fd, size_t num_bytes)
100 char copy_buf[COPY_BUF_SIZE];
103 remaining = num_bytes;
104 while (remaining > 0) {
105 size_t num_to_read = remaining > COPY_BUF_SIZE ? COPY_BUF_SIZE : remaining;
108 num_read = read(src_fd, copy_buf, num_to_read);
109 } while (num_read == -1 && errno == EINTR);
111 fprintf(stderr, "Error reading %zd bytes from source: %s\n",
112 num_to_read, strerror(errno));
115 size_t num_to_write = num_read;
116 while (num_to_write > 0) {
117 size_t offset = num_read - num_to_write;
120 num_written = write(dst_fd, copy_buf + offset, num_to_write);
121 } while (num_written == -1 && errno == EINTR);
122 if (num_written <= 0) {
123 fprintf(stderr, "Error writing %zd bytes to destination: %s\n",
124 num_to_write, strerror(errno));
127 num_to_write -= num_written;
129 remaining -= num_read;
135 int module_setActiveBootSlot(boot_control_module_t *module, unsigned slot)
139 uint64_t src_size, dst_size;
145 if (!boot_info_load(&info)) {
146 fprintf(stderr, "WARNING: Error loading boot-info. Resetting.\n");
147 boot_info_reset(&info);
149 if (!boot_info_validate(&info)) {
150 fprintf(stderr, "WARNING: boot-info is invalid. Resetting.\n");
151 boot_info_reset(&info);
155 info.active_slot = slot;
156 info.slot_info[slot].bootable = true;
157 snprintf(info.bootctrl_suffix,
158 sizeof(info.bootctrl_suffix),
161 if (!boot_info_save(&info)) {
162 fprintf(stderr, "Error saving boot-info.\n");
166 // Finally copy the contents of boot_X into boot.
167 snprintf(src_name, sizeof(src_name), "boot_%c", slot + 'a');
168 src_fd = boot_info_open_partition(src_name, &src_size, O_RDONLY);
170 fprintf(stderr, "Error opening \"%s\" partition.\n", src_name);
174 dst_fd = boot_info_open_partition("boot", &dst_size, O_RDWR);
176 fprintf(stderr, "Error opening \"boot\" partition.\n");
181 if (src_size != dst_size) {
183 "src (%" PRIu64 " bytes) and dst (%" PRIu64 " bytes) "
184 "have different sizes.\n",
191 if (!copy_data(src_fd, dst_fd, src_size)) {
197 if (fsync(dst_fd) != 0) {
198 fprintf(stderr, "Error calling fsync on destination: %s\n",
208 int module_setSlotAsUnbootable(struct boot_control_module *module, unsigned slot)
215 if (!boot_info_load(&info)) {
216 fprintf(stderr, "WARNING: Error loading boot-info. Resetting.\n");
217 boot_info_reset(&info);
219 if (!boot_info_validate(&info)) {
220 fprintf(stderr, "WARNING: boot-info is invalid. Resetting.\n");
221 boot_info_reset(&info);
225 info.slot_info[slot].bootable = false;
227 if (!boot_info_save(&info)) {
228 fprintf(stderr, "Error saving boot-info.\n");
235 int module_isSlotBootable(struct boot_control_module *module, unsigned slot)
242 if (!boot_info_load(&info)) {
243 fprintf(stderr, "WARNING: Error loading boot-info. Resetting.\n");
244 boot_info_reset(&info);
246 if (!boot_info_validate(&info)) {
247 fprintf(stderr, "WARNING: boot-info is invalid. Resetting.\n");
248 boot_info_reset(&info);
252 return info.slot_info[slot].bootable;
255 const char* module_getSuffix(boot_control_module_t *module, unsigned slot)
257 static const char* suffix[2] = {"_a", "_b"};
263 static struct hw_module_methods_t module_methods = {
268 /* This boot_control HAL implementation emulates A/B by copying the
269 * contents of the boot partition of the requested slot to the boot
270 * partition. It hence works with bootloaders that are not yet aware
271 * of A/B. This code is only intended to be used for development.
274 boot_control_module_t HAL_MODULE_INFO_SYM = {
276 .tag = HARDWARE_MODULE_TAG,
277 .module_api_version = BOOT_CONTROL_MODULE_API_VERSION_0_1,
278 .hal_api_version = HARDWARE_HAL_API_VERSION,
279 .id = BOOT_CONTROL_HARDWARE_MODULE_ID,
280 .name = "Copy Implementation of boot_control HAL",
281 .author = "The Android Open Source Project",
282 .methods = &module_methods,
285 .getNumberSlots = module_getNumberSlots,
286 .getCurrentSlot = module_getCurrentSlot,
287 .markBootSuccessful = module_markBootSuccessful,
288 .setActiveBootSlot = module_setActiveBootSlot,
289 .setSlotAsUnbootable = module_setSlotAsUnbootable,
290 .isSlotBootable = module_isSlotBootable,
291 .getSuffix = module_getSuffix,