The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <inttypes.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
| 25 | |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 26 | #include <fs_mgr.h> |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 27 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 28 | #include "bootloader.h" |
| 29 | #include "common.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 30 | #include "roots.h" |
Elliott Hughes | bcabd09 | 2016-03-22 20:19:22 -0700 | [diff] [blame] | 31 | #include <android-base/unique_fd.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 32 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 33 | static int get_bootloader_message_block(bootloader_message* out, const Volume* v); |
| 34 | static int set_bootloader_message_block(const bootloader_message* in, const Volume* v); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 35 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 36 | int get_bootloader_message(bootloader_message* out) { |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 37 | Volume* v = volume_for_path("/misc"); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 38 | if (v == nullptr) { |
| 39 | LOGE("Cannot load volume /misc!\n"); |
| 40 | return -1; |
Adam Bliss | b2ceb69 | 2011-07-13 15:13:54 -0700 | [diff] [blame] | 41 | } |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 42 | if (strcmp(v->fs_type, "emmc") == 0) { |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 43 | return get_bootloader_message_block(out, v); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 44 | } |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 45 | LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type); |
| 46 | return -1; |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 49 | int set_bootloader_message(const bootloader_message* in) { |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 50 | Volume* v = volume_for_path("/misc"); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 51 | if (v == nullptr) { |
| 52 | LOGE("Cannot load volume /misc!\n"); |
| 53 | return -1; |
Adam Bliss | b2ceb69 | 2011-07-13 15:13:54 -0700 | [diff] [blame] | 54 | } |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 55 | if (strcmp(v->fs_type, "emmc") == 0) { |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 56 | return set_bootloader_message_block(in, v); |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 57 | } |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 58 | LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type); |
| 59 | return -1; |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 62 | // ------------------------------------ |
| 63 | // for misc partitions on block devices |
| 64 | // ------------------------------------ |
| 65 | |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 66 | static void wait_for_device(const char* fn) { |
| 67 | int tries = 0; |
| 68 | int ret; |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 69 | do { |
| 70 | ++tries; |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 71 | struct stat buf; |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 72 | ret = stat(fn, &buf); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 73 | if (ret == -1) { |
| 74 | printf("failed to stat \"%s\" try %d: %s\n", fn, tries, strerror(errno)); |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 75 | sleep(1); |
| 76 | } |
| 77 | } while (ret && tries < 10); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 78 | |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 79 | if (ret) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 80 | printf("failed to stat \"%s\"\n", fn); |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 84 | static int get_bootloader_message_block(bootloader_message* out, |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 85 | const Volume* v) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 86 | wait_for_device(v->blk_device); |
| 87 | FILE* f = fopen(v->blk_device, "rb"); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 88 | if (f == nullptr) { |
| 89 | LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno)); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 90 | return -1; |
| 91 | } |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 92 | bootloader_message temp; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 93 | int count = fread(&temp, sizeof(temp), 1, f); |
| 94 | if (count != 1) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 95 | LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno)); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 96 | return -1; |
| 97 | } |
| 98 | if (fclose(f) != 0) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 99 | LOGE("failed to close \"%s\": %s\n", v->blk_device, strerror(errno)); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 100 | return -1; |
| 101 | } |
| 102 | memcpy(out, &temp, sizeof(temp)); |
| 103 | return 0; |
| 104 | } |
| 105 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 106 | static int set_bootloader_message_block(const bootloader_message* in, |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 107 | const Volume* v) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 108 | wait_for_device(v->blk_device); |
Elliott Hughes | bcabd09 | 2016-03-22 20:19:22 -0700 | [diff] [blame] | 109 | android::base::unique_fd fd(open(v->blk_device, O_WRONLY | O_SYNC)); |
| 110 | if (fd == -1) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 111 | LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno)); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 112 | return -1; |
| 113 | } |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 114 | |
| 115 | size_t written = 0; |
| 116 | const uint8_t* start = reinterpret_cast<const uint8_t*>(in); |
| 117 | size_t total = sizeof(*in); |
| 118 | while (written < total) { |
Elliott Hughes | bcabd09 | 2016-03-22 20:19:22 -0700 | [diff] [blame] | 119 | ssize_t wrote = TEMP_FAILURE_RETRY(write(fd, start + written, total - written)); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 120 | if (wrote == -1) { |
| 121 | LOGE("failed to write %" PRId64 " bytes: %s\n", |
| 122 | static_cast<off64_t>(written), strerror(errno)); |
| 123 | return -1; |
| 124 | } |
| 125 | written += wrote; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 126 | } |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 127 | |
Elliott Hughes | bcabd09 | 2016-03-22 20:19:22 -0700 | [diff] [blame] | 128 | if (fsync(fd) == -1) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 129 | LOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno)); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 130 | return -1; |
| 131 | } |
| 132 | return 0; |
| 133 | } |