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 | |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 28 | #include <android-base/file.h> |
Yabin Cui | bf049bf | 2016-06-21 11:00:44 -0700 | [diff] [blame] | 29 | #include <android-base/unique_fd.h> |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 30 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 31 | #include "bootloader.h" |
| 32 | #include "common.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 33 | #include "roots.h" |
| 34 | |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 35 | static bool read_misc_partition(const Volume* v, size_t offset, size_t size, std::string* out); |
| 36 | static bool write_misc_partition(const Volume* v, size_t offset, const std::string& in); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 37 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 38 | int get_bootloader_message(bootloader_message* out) { |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 39 | Volume* v = volume_for_path("/misc"); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 40 | if (v == nullptr) { |
| 41 | LOGE("Cannot load volume /misc!\n"); |
| 42 | return -1; |
Adam Bliss | b2ceb69 | 2011-07-13 15:13:54 -0700 | [diff] [blame] | 43 | } |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 44 | if (strcmp(v->fs_type, "emmc") == 0) { |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 45 | std::string s; |
| 46 | if (!read_misc_partition(v, BOOTLOADER_MESSAGE_OFFSET_IN_MISC, sizeof(bootloader_message), |
| 47 | &s)) { |
| 48 | return -1; |
| 49 | } |
| 50 | memcpy(out, s.data(), s.size()); |
| 51 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 52 | } |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 53 | LOGE("Unknown misc partition fs_type \"%s\"\n", v->fs_type); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 54 | return -1; |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 57 | bool read_wipe_package(size_t size, std::string* out) { |
| 58 | Volume* v = volume_for_path("/misc"); |
| 59 | if (v == nullptr) { |
| 60 | LOGE("Cannot load volume /misc!\n"); |
| 61 | return false; |
| 62 | } |
| 63 | if (strcmp(v->fs_type, "mtd") == 0) { |
| 64 | LOGE("Read wipe package on mtd is not supported.\n"); |
| 65 | return false; |
| 66 | } else if (strcmp(v->fs_type, "emmc") == 0) { |
| 67 | return read_misc_partition(v, WIPE_PACKAGE_OFFSET_IN_MISC, size, out); |
| 68 | } |
| 69 | LOGE("Unknown misc partition fs_type \"%s\"\n", v->fs_type); |
| 70 | return false; |
| 71 | } |
| 72 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 73 | int set_bootloader_message(const bootloader_message* in) { |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 74 | Volume* v = volume_for_path("/misc"); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 75 | if (v == nullptr) { |
| 76 | LOGE("Cannot load volume /misc!\n"); |
| 77 | return -1; |
Adam Bliss | b2ceb69 | 2011-07-13 15:13:54 -0700 | [diff] [blame] | 78 | } |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 79 | if (strcmp(v->fs_type, "emmc") == 0) { |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 80 | std::string s(reinterpret_cast<const char*>(in), sizeof(*in)); |
| 81 | bool success = write_misc_partition(v, BOOTLOADER_MESSAGE_OFFSET_IN_MISC, s); |
| 82 | return success ? 0 : -1; |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 83 | } |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 84 | LOGE("Unknown misc partition fs_type \"%s\"\n", v->fs_type); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 85 | return -1; |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 88 | // ------------------------------------ |
| 89 | // for misc partitions on block devices |
| 90 | // ------------------------------------ |
| 91 | |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 92 | static void wait_for_device(const char* fn) { |
| 93 | int tries = 0; |
| 94 | int ret; |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 95 | do { |
| 96 | ++tries; |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 97 | struct stat buf; |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 98 | ret = stat(fn, &buf); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 99 | if (ret == -1) { |
| 100 | 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] | 101 | sleep(1); |
| 102 | } |
| 103 | } while (ret && tries < 10); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 104 | |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 105 | if (ret) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 106 | printf("failed to stat \"%s\"\n", fn); |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 110 | static bool read_misc_partition(const Volume* v, size_t offset, size_t size, std::string* out) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 111 | wait_for_device(v->blk_device); |
Yabin Cui | bf049bf | 2016-06-21 11:00:44 -0700 | [diff] [blame] | 112 | android::base::unique_fd fd(open(v->blk_device, O_RDONLY)); |
| 113 | if (fd == -1) { |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 114 | LOGE("Failed to open \"%s\": %s\n", v->blk_device, strerror(errno)); |
| 115 | return false; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 116 | } |
Yabin Cui | bf049bf | 2016-06-21 11:00:44 -0700 | [diff] [blame] | 117 | if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 118 | LOGE("Failed to lseek \"%s\": %s\n", v->blk_device, strerror(errno)); |
| 119 | return false; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 120 | } |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 121 | out->resize(size); |
Yabin Cui | bf049bf | 2016-06-21 11:00:44 -0700 | [diff] [blame] | 122 | if (!android::base::ReadFully(fd, &(*out)[0], size)) { |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 123 | LOGE("Failed to read \"%s\": %s\n", v->blk_device, strerror(errno)); |
| 124 | return false; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 125 | } |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 126 | return true; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 129 | static bool write_misc_partition(const Volume* v, size_t offset, const std::string& in) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 130 | wait_for_device(v->blk_device); |
Elliott Hughes | bcabd09 | 2016-03-22 20:19:22 -0700 | [diff] [blame] | 131 | android::base::unique_fd fd(open(v->blk_device, O_WRONLY | O_SYNC)); |
| 132 | if (fd == -1) { |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 133 | LOGE("Failed to open \"%s\": %s\n", v->blk_device, strerror(errno)); |
| 134 | return false; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 135 | } |
Yabin Cui | bf049bf | 2016-06-21 11:00:44 -0700 | [diff] [blame] | 136 | if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 137 | LOGE("Failed to lseek \"%s\": %s\n", v->blk_device, strerror(errno)); |
| 138 | return false; |
| 139 | } |
Yabin Cui | bf049bf | 2016-06-21 11:00:44 -0700 | [diff] [blame] | 140 | if (!android::base::WriteFully(fd, in.data(), in.size())) { |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 141 | LOGE("Failed to write \"%s\": %s\n", v->blk_device, strerror(errno)); |
| 142 | return false; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 143 | } |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 144 | |
Elliott Hughes | bcabd09 | 2016-03-22 20:19:22 -0700 | [diff] [blame] | 145 | if (fsync(fd) == -1) { |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 146 | LOGE("Failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno)); |
| 147 | return false; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 148 | } |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 149 | return true; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 150 | } |