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" |
| 30 | #include "mtdutils/mtdutils.h" |
| 31 | #include "roots.h" |
Elliott Hughes | bcabd09 | 2016-03-22 20:19:22 -0700 | [diff] [blame] | 32 | #include <android-base/unique_fd.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 33 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 34 | static int get_bootloader_message_mtd(bootloader_message* out, const Volume* v); |
| 35 | static int set_bootloader_message_mtd(const bootloader_message* in, const Volume* v); |
| 36 | static int get_bootloader_message_block(bootloader_message* out, const Volume* v); |
| 37 | 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] | 38 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 39 | int get_bootloader_message(bootloader_message* out) { |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 40 | Volume* v = volume_for_path("/misc"); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 41 | if (v == nullptr) { |
| 42 | LOGE("Cannot load volume /misc!\n"); |
| 43 | return -1; |
Adam Bliss | b2ceb69 | 2011-07-13 15:13:54 -0700 | [diff] [blame] | 44 | } |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 45 | if (strcmp(v->fs_type, "mtd") == 0) { |
| 46 | return get_bootloader_message_mtd(out, v); |
| 47 | } else if (strcmp(v->fs_type, "emmc") == 0) { |
| 48 | return get_bootloader_message_block(out, v); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 49 | } |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 50 | LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type); |
| 51 | return -1; |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 54 | int set_bootloader_message(const bootloader_message* in) { |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 55 | Volume* v = volume_for_path("/misc"); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 56 | if (v == nullptr) { |
| 57 | LOGE("Cannot load volume /misc!\n"); |
| 58 | return -1; |
Adam Bliss | b2ceb69 | 2011-07-13 15:13:54 -0700 | [diff] [blame] | 59 | } |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 60 | if (strcmp(v->fs_type, "mtd") == 0) { |
| 61 | return set_bootloader_message_mtd(in, v); |
| 62 | } else if (strcmp(v->fs_type, "emmc") == 0) { |
| 63 | return set_bootloader_message_block(in, v); |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 64 | } |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 65 | LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type); |
| 66 | return -1; |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 69 | // ------------------------------ |
| 70 | // for misc partitions on MTD |
| 71 | // ------------------------------ |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 72 | |
| 73 | static const int MISC_PAGES = 3; // number of pages to save |
| 74 | static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page |
| 75 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 76 | static int get_bootloader_message_mtd(bootloader_message* out, |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 77 | const Volume* v) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 78 | size_t write_size; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 79 | mtd_scan_partitions(); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 80 | const MtdPartition* part = mtd_find_partition_by_name(v->blk_device); |
| 81 | if (part == nullptr || mtd_partition_info(part, nullptr, nullptr, &write_size)) { |
| 82 | LOGE("failed to find \"%s\"\n", v->blk_device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 83 | return -1; |
| 84 | } |
| 85 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 86 | MtdReadContext* read = mtd_read_partition(part); |
| 87 | if (read == nullptr) { |
| 88 | LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | const ssize_t size = write_size * MISC_PAGES; |
| 93 | char data[size]; |
| 94 | ssize_t r = mtd_read_data(read, data, size); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 95 | if (r != size) LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 96 | mtd_read_close(read); |
| 97 | if (r != size) return -1; |
| 98 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 99 | memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out)); |
| 100 | return 0; |
| 101 | } |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 102 | static int set_bootloader_message_mtd(const bootloader_message* in, |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 103 | const Volume* v) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 104 | size_t write_size; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 105 | mtd_scan_partitions(); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 106 | const MtdPartition* part = mtd_find_partition_by_name(v->blk_device); |
| 107 | if (part == nullptr || mtd_partition_info(part, nullptr, nullptr, &write_size)) { |
| 108 | LOGE("failed to find \"%s\"\n", v->blk_device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 109 | return -1; |
| 110 | } |
| 111 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 112 | MtdReadContext* read = mtd_read_partition(part); |
| 113 | if (read == nullptr) { |
| 114 | LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | ssize_t size = write_size * MISC_PAGES; |
| 119 | char data[size]; |
| 120 | ssize_t r = mtd_read_data(read, data, size); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 121 | if (r != size) LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 122 | mtd_read_close(read); |
| 123 | if (r != size) return -1; |
| 124 | |
| 125 | memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in)); |
| 126 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 127 | MtdWriteContext* write = mtd_write_partition(part); |
| 128 | if (write == nullptr) { |
| 129 | LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 130 | return -1; |
| 131 | } |
| 132 | if (mtd_write_data(write, data, size) != size) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 133 | LOGE("failed to write \"%s\": %s\n", v->blk_device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 134 | mtd_write_close(write); |
| 135 | return -1; |
| 136 | } |
| 137 | if (mtd_write_close(write)) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 138 | LOGE("failed to finish \"%s\": %s\n", v->blk_device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : ""); |
| 143 | return 0; |
| 144 | } |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 145 | |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 146 | |
| 147 | // ------------------------------------ |
| 148 | // for misc partitions on block devices |
| 149 | // ------------------------------------ |
| 150 | |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 151 | static void wait_for_device(const char* fn) { |
| 152 | int tries = 0; |
| 153 | int ret; |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 154 | do { |
| 155 | ++tries; |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 156 | struct stat buf; |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 157 | ret = stat(fn, &buf); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 158 | if (ret == -1) { |
| 159 | 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] | 160 | sleep(1); |
| 161 | } |
| 162 | } while (ret && tries < 10); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 163 | |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 164 | if (ret) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 165 | printf("failed to stat \"%s\"\n", fn); |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 169 | static int get_bootloader_message_block(bootloader_message* out, |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 170 | const Volume* v) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 171 | wait_for_device(v->blk_device); |
| 172 | FILE* f = fopen(v->blk_device, "rb"); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 173 | if (f == nullptr) { |
| 174 | LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno)); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 175 | return -1; |
| 176 | } |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 177 | bootloader_message temp; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 178 | int count = fread(&temp, sizeof(temp), 1, f); |
| 179 | if (count != 1) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 180 | LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno)); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 181 | return -1; |
| 182 | } |
| 183 | if (fclose(f) != 0) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 184 | LOGE("failed to close \"%s\": %s\n", v->blk_device, strerror(errno)); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 185 | return -1; |
| 186 | } |
| 187 | memcpy(out, &temp, sizeof(temp)); |
| 188 | return 0; |
| 189 | } |
| 190 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 191 | static int set_bootloader_message_block(const bootloader_message* in, |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 192 | const Volume* v) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 193 | wait_for_device(v->blk_device); |
Elliott Hughes | bcabd09 | 2016-03-22 20:19:22 -0700 | [diff] [blame] | 194 | android::base::unique_fd fd(open(v->blk_device, O_WRONLY | O_SYNC)); |
| 195 | if (fd == -1) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 196 | LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno)); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 197 | return -1; |
| 198 | } |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 199 | |
| 200 | size_t written = 0; |
| 201 | const uint8_t* start = reinterpret_cast<const uint8_t*>(in); |
| 202 | size_t total = sizeof(*in); |
| 203 | while (written < total) { |
Elliott Hughes | bcabd09 | 2016-03-22 20:19:22 -0700 | [diff] [blame] | 204 | ssize_t wrote = TEMP_FAILURE_RETRY(write(fd, start + written, total - written)); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 205 | if (wrote == -1) { |
| 206 | LOGE("failed to write %" PRId64 " bytes: %s\n", |
| 207 | static_cast<off64_t>(written), strerror(errno)); |
| 208 | return -1; |
| 209 | } |
| 210 | written += wrote; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 211 | } |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 212 | |
Elliott Hughes | bcabd09 | 2016-03-22 20:19:22 -0700 | [diff] [blame] | 213 | if (fsync(fd) == -1) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 214 | LOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno)); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 215 | return -1; |
| 216 | } |
| 217 | return 0; |
| 218 | } |