blob: dad0bab30dd7f8b1bf134612c0e125ca1f8080ef [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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 Baobd82b272016-01-21 13:49:03 -080017#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 Sumrallf35d1ce2013-02-13 12:59:35 -080026#include <fs_mgr.h>
Tao Baobd82b272016-01-21 13:49:03 -080027
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070028#include <android-base/logging.h>
Elliott Hughesbcabd092016-03-22 20:19:22 -070029#include <android-base/unique_fd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080030
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070031#include "bootloader.h"
32#include "roots.h"
33
Tao Baobd82b272016-01-21 13:49:03 -080034static int get_bootloader_message_block(bootloader_message* out, const Volume* v);
35static int set_bootloader_message_block(const bootloader_message* in, const Volume* v);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036
Tao Baobd82b272016-01-21 13:49:03 -080037int get_bootloader_message(bootloader_message* out) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070038 Volume* v = volume_for_path("/misc");
Tao Baobd82b272016-01-21 13:49:03 -080039 if (v == nullptr) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070040 LOG(ERROR) << "Cannot load volume /misc!";
Tao Baobd82b272016-01-21 13:49:03 -080041 return -1;
Adam Blissb2ceb692011-07-13 15:13:54 -070042 }
Elliott Hughes63a31922016-06-09 17:41:22 -070043 if (strcmp(v->fs_type, "emmc") == 0) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070044 return get_bootloader_message_block(out, v);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045 }
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070046 LOG(ERROR) << "unknown misc partition fs_type \"" << v->fs_type << "\"";
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070047 return -1;
Doug Zongker04611da2010-08-12 15:35:29 -070048}
49
Tao Baobd82b272016-01-21 13:49:03 -080050int set_bootloader_message(const bootloader_message* in) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070051 Volume* v = volume_for_path("/misc");
Tao Baobd82b272016-01-21 13:49:03 -080052 if (v == nullptr) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070053 LOG(ERROR) << "Cannot load volume /misc!";
Tao Baobd82b272016-01-21 13:49:03 -080054 return -1;
Adam Blissb2ceb692011-07-13 15:13:54 -070055 }
Elliott Hughes63a31922016-06-09 17:41:22 -070056 if (strcmp(v->fs_type, "emmc") == 0) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070057 return set_bootloader_message_block(in, v);
Doug Zongker04611da2010-08-12 15:35:29 -070058 }
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070059 LOG(ERROR) << "unknown misc partition fs_type \"" << v->fs_type << "\"";
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070060 return -1;
Doug Zongker04611da2010-08-12 15:35:29 -070061}
62
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070063// ------------------------------------
64// for misc partitions on block devices
65// ------------------------------------
66
Doug Zongkercfd256a2011-04-22 09:26:44 -070067static void wait_for_device(const char* fn) {
68 int tries = 0;
69 int ret;
Doug Zongkercfd256a2011-04-22 09:26:44 -070070 do {
71 ++tries;
Tao Baobd82b272016-01-21 13:49:03 -080072 struct stat buf;
Doug Zongkercfd256a2011-04-22 09:26:44 -070073 ret = stat(fn, &buf);
Tao Baobd82b272016-01-21 13:49:03 -080074 if (ret == -1) {
75 printf("failed to stat \"%s\" try %d: %s\n", fn, tries, strerror(errno));
Doug Zongkercfd256a2011-04-22 09:26:44 -070076 sleep(1);
77 }
78 } while (ret && tries < 10);
Tao Baobd82b272016-01-21 13:49:03 -080079
Doug Zongkercfd256a2011-04-22 09:26:44 -070080 if (ret) {
Tao Baobd82b272016-01-21 13:49:03 -080081 printf("failed to stat \"%s\"\n", fn);
Doug Zongkercfd256a2011-04-22 09:26:44 -070082 }
83}
84
Tao Baobd82b272016-01-21 13:49:03 -080085static int get_bootloader_message_block(bootloader_message* out,
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070086 const Volume* v) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080087 wait_for_device(v->blk_device);
88 FILE* f = fopen(v->blk_device, "rb");
Tao Baobd82b272016-01-21 13:49:03 -080089 if (f == nullptr) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070090 PLOG(ERROR) << "failed to open \"" << v->blk_device << "\"";
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070091 return -1;
92 }
Tao Baobd82b272016-01-21 13:49:03 -080093 bootloader_message temp;
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070094 int count = fread(&temp, sizeof(temp), 1, f);
95 if (count != 1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070096 PLOG(ERROR) << "failed to read \"" << v->blk_device << "\"";
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070097 return -1;
98 }
99 if (fclose(f) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700100 PLOG(ERROR) << "failed to close \"" << v->blk_device << "\"";
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700101 return -1;
102 }
103 memcpy(out, &temp, sizeof(temp));
104 return 0;
105}
106
Tao Baobd82b272016-01-21 13:49:03 -0800107static int set_bootloader_message_block(const bootloader_message* in,
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700108 const Volume* v) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800109 wait_for_device(v->blk_device);
Elliott Hughesbcabd092016-03-22 20:19:22 -0700110 android::base::unique_fd fd(open(v->blk_device, O_WRONLY | O_SYNC));
111 if (fd == -1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700112 PLOG(ERROR) << "failed to open \"" << v->blk_device << "\"";
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700113 return -1;
114 }
Tao Baobd82b272016-01-21 13:49:03 -0800115
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 Hughesbcabd092016-03-22 20:19:22 -0700120 ssize_t wrote = TEMP_FAILURE_RETRY(write(fd, start + written, total - written));
Tao Baobd82b272016-01-21 13:49:03 -0800121 if (wrote == -1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700122 PLOG(ERROR) << "failed to write " << total << " bytes, " << written
123 << " bytes written";
Tao Baobd82b272016-01-21 13:49:03 -0800124 return -1;
125 }
126 written += wrote;
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700127 }
Tao Baobd82b272016-01-21 13:49:03 -0800128
Elliott Hughesbcabd092016-03-22 20:19:22 -0700129 if (fsync(fd) == -1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700130 PLOG(ERROR) << "failed to fsync \"" << v->blk_device << "\"";
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700131 return -1;
132 }
133 return 0;
134}