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