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