blob: 783f56ea806953e2ff991129138eb69f06a19bdc [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
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028#include "bootloader.h"
29#include "common.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080030#include "roots.h"
Elliott Hughesbcabd092016-03-22 20:19:22 -070031#include <android-base/unique_fd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032
Tao Baobd82b272016-01-21 13:49:03 -080033static int get_bootloader_message_block(bootloader_message* out, const Volume* v);
34static int set_bootloader_message_block(const bootloader_message* in, const Volume* v);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035
Tao Baobd82b272016-01-21 13:49:03 -080036int get_bootloader_message(bootloader_message* out) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070037 Volume* v = volume_for_path("/misc");
Tao Baobd82b272016-01-21 13:49:03 -080038 if (v == nullptr) {
39 LOGE("Cannot load volume /misc!\n");
40 return -1;
Adam Blissb2ceb692011-07-13 15:13:54 -070041 }
Elliott Hughes63a31922016-06-09 17:41:22 -070042 if (strcmp(v->fs_type, "emmc") == 0) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070043 return get_bootloader_message_block(out, v);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070045 LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
46 return -1;
Doug Zongker04611da2010-08-12 15:35:29 -070047}
48
Tao Baobd82b272016-01-21 13:49:03 -080049int set_bootloader_message(const bootloader_message* in) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070050 Volume* v = volume_for_path("/misc");
Tao Baobd82b272016-01-21 13:49:03 -080051 if (v == nullptr) {
52 LOGE("Cannot load volume /misc!\n");
53 return -1;
Adam Blissb2ceb692011-07-13 15:13:54 -070054 }
Elliott Hughes63a31922016-06-09 17:41:22 -070055 if (strcmp(v->fs_type, "emmc") == 0) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070056 return set_bootloader_message_block(in, v);
Doug Zongker04611da2010-08-12 15:35:29 -070057 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070058 LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
59 return -1;
Doug Zongker04611da2010-08-12 15:35:29 -070060}
61
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070062// ------------------------------------
63// for misc partitions on block devices
64// ------------------------------------
65
Doug Zongkercfd256a2011-04-22 09:26:44 -070066static void wait_for_device(const char* fn) {
67 int tries = 0;
68 int ret;
Doug Zongkercfd256a2011-04-22 09:26:44 -070069 do {
70 ++tries;
Tao Baobd82b272016-01-21 13:49:03 -080071 struct stat buf;
Doug Zongkercfd256a2011-04-22 09:26:44 -070072 ret = stat(fn, &buf);
Tao Baobd82b272016-01-21 13:49:03 -080073 if (ret == -1) {
74 printf("failed to stat \"%s\" try %d: %s\n", fn, tries, strerror(errno));
Doug Zongkercfd256a2011-04-22 09:26:44 -070075 sleep(1);
76 }
77 } while (ret && tries < 10);
Tao Baobd82b272016-01-21 13:49:03 -080078
Doug Zongkercfd256a2011-04-22 09:26:44 -070079 if (ret) {
Tao Baobd82b272016-01-21 13:49:03 -080080 printf("failed to stat \"%s\"\n", fn);
Doug Zongkercfd256a2011-04-22 09:26:44 -070081 }
82}
83
Tao Baobd82b272016-01-21 13:49:03 -080084static int get_bootloader_message_block(bootloader_message* out,
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070085 const Volume* v) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080086 wait_for_device(v->blk_device);
87 FILE* f = fopen(v->blk_device, "rb");
Tao Baobd82b272016-01-21 13:49:03 -080088 if (f == nullptr) {
89 LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070090 return -1;
91 }
Tao Baobd82b272016-01-21 13:49:03 -080092 bootloader_message temp;
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070093 int count = fread(&temp, sizeof(temp), 1, f);
94 if (count != 1) {
Tao Baobd82b272016-01-21 13:49:03 -080095 LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070096 return -1;
97 }
98 if (fclose(f) != 0) {
Tao Baobd82b272016-01-21 13:49:03 -080099 LOGE("failed to close \"%s\": %s\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700100 return -1;
101 }
102 memcpy(out, &temp, sizeof(temp));
103 return 0;
104}
105
Tao Baobd82b272016-01-21 13:49:03 -0800106static int set_bootloader_message_block(const bootloader_message* in,
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700107 const Volume* v) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800108 wait_for_device(v->blk_device);
Elliott Hughesbcabd092016-03-22 20:19:22 -0700109 android::base::unique_fd fd(open(v->blk_device, O_WRONLY | O_SYNC));
110 if (fd == -1) {
Tao Baobd82b272016-01-21 13:49:03 -0800111 LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700112 return -1;
113 }
Tao Baobd82b272016-01-21 13:49:03 -0800114
115 size_t written = 0;
116 const uint8_t* start = reinterpret_cast<const uint8_t*>(in);
117 size_t total = sizeof(*in);
118 while (written < total) {
Elliott Hughesbcabd092016-03-22 20:19:22 -0700119 ssize_t wrote = TEMP_FAILURE_RETRY(write(fd, start + written, total - written));
Tao Baobd82b272016-01-21 13:49:03 -0800120 if (wrote == -1) {
121 LOGE("failed to write %" PRId64 " bytes: %s\n",
122 static_cast<off64_t>(written), strerror(errno));
123 return -1;
124 }
125 written += wrote;
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700126 }
Tao Baobd82b272016-01-21 13:49:03 -0800127
Elliott Hughesbcabd092016-03-22 20:19:22 -0700128 if (fsync(fd) == -1) {
Tao Baobd82b272016-01-21 13:49:03 -0800129 LOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700130 return -1;
131 }
132 return 0;
133}